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
5364b16a95b9cd2da4c4577bde4ae99448af0a77
f20db13587f4dd28a4b1fbd31953afd491691fa0
/library/system/io.lean
d8bab91544b772a3e57a46f8ee4b06430f421ae8
[ "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
5,866
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch and Leonardo de Moura -/ import system.io_interface /- The following constants have a builtin implementation -/ constant io_core : Type β†’ Type β†’ Type @[instance] constant monad_io_impl : monad_io io_core @[instance] constant monad_io_terminal_impl : monad_io_terminal io_core @[instance] constant monad_io_file_system_impl : monad_io_file_system io_core @[instance] constant monad_io_environment_impl : monad_io_environment io_core @[instance] constant monad_io_process_impl : monad_io_process io_core instance io_core_is_monad (e : Type) : monad (io_core e) := monad_io_is_monad io_core e instance io_core_is_monad_fail : monad_fail (io_core io.error) := monad_io_is_monad_fail io_core instance io_core_is_alternative : alternative (io_core io.error) := monad_io_is_alternative io_core @[reducible] def io (Ξ± : Type) := io_core io.error Ξ± namespace io /- Remark: the following definitions can be generalized and defined for any (m : Type -> Type -> Type) that implements the required type classes. However, the generalized versions are very inconvenient to use, (example: `#eval io.put_str "hello world"` does not work because we don't have enough information to infer `m`.). -/ def iterate {e Ξ±} (a : Ξ±) (f : Ξ± β†’ io_core e (option Ξ±)) : io_core e Ξ± := monad_io.iterate e Ξ± a f def forever {e} (a : io_core e unit) : io_core e unit := iterate () $ Ξ» _, a >> return (some ()) -- TODO(Leo): delete after we merge #1881 def catch {e₁ eβ‚‚ Ξ±} (a : io_core e₁ Ξ±) (b : e₁ β†’ io_core eβ‚‚ Ξ±) : io_core eβ‚‚ Ξ± := monad_io.catch e₁ eβ‚‚ Ξ± a b def finally {Ξ± e} (a : io_core e Ξ±) (cleanup : io_core e unit) : io_core e Ξ± := do res ← catch (sum.inr <$> a) (return ∘ sum.inl), cleanup, match res with | sum.inr res := return res | sum.inl error := monad_io.fail _ _ _ error end protected def fail {Ξ± : Type} (s : string) : io Ξ± := monad_io.fail io_core _ _ (io.error.other s) def put_str : string β†’ io unit := monad_io_terminal.put_str io_core def put_str_ln (s : string) : io unit := put_str s >> put_str "\n" def get_line : io string := monad_io_terminal.get_line io_core def cmdline_args : io (list string) := return (monad_io_terminal.cmdline_args io_core) def print {Ξ±} [has_to_string Ξ±] (s : Ξ±) : io unit := put_str ∘ to_string $ s def print_ln {Ξ±} [has_to_string Ξ±] (s : Ξ±) : io unit := print s >> put_str "\n" def handle : Type := monad_io.handle io_core def mk_file_handle (s : string) (m : mode) (bin : bool := ff) : io handle := monad_io_file_system.mk_file_handle io_core s m bin def stdin : io handle := monad_io_file_system.stdin io_core def stderr : io handle := monad_io_file_system.stderr io_core def stdout : io handle := monad_io_file_system.stdout io_core namespace env def get (env_var : string) : io (option string) := monad_io_environment.get_env io_core env_var /-- get the current working directory -/ def get_cwd : io string := monad_io_environment.get_cwd io_core /-- set the current working directory -/ def set_cwd (cwd : string) : io unit := monad_io_environment.set_cwd io_core cwd end env namespace fs def is_eof : handle β†’ io bool := monad_io_file_system.is_eof def flush : handle β†’ io unit := monad_io_file_system.flush def close : handle β†’ io unit := monad_io_file_system.close def read : handle β†’ nat β†’ io char_buffer := monad_io_file_system.read def write : handle β†’ char_buffer β†’ io unit := monad_io_file_system.write def get_char (h : handle) : io char := do b ← read h 1, if h : b.size = 1 then return $ b.read ⟨0, h.symm β–Έ zero_lt_one⟩ else io.fail "get_char failed" def get_line : handle β†’ io char_buffer := monad_io_file_system.get_line def put_char (h : handle) (c : char) : io unit := write h (mk_buffer.push_back c) def put_str (h : handle) (s : string) : io unit := write h (mk_buffer.append_string s) def put_str_ln (h : handle) (s : string) : io unit := put_str h s >> put_str h "\n" def read_to_end (h : handle) : io char_buffer := iterate mk_buffer $ Ξ» r, do done ← is_eof h, if done then return none else do c ← read h 1024, return $ some (r ++ c) def read_file (s : string) (bin := ff) : io char_buffer := do h ← mk_file_handle s io.mode.read bin, read_to_end h end fs namespace proc def child : Type := monad_io_process.child io_core def child.stdin : child β†’ handle := monad_io_process.stdin def child.stdout : child β†’ handle := monad_io_process.stdout def child.stderr : child β†’ handle := monad_io_process.stderr def spawn (p : io.process.spawn_args) : io child := monad_io_process.spawn io_core p def wait (c : child) : io nat := monad_io_process.wait c end proc end io meta constant format.print_using : format β†’ options β†’ io unit meta definition format.print (fmt : format) : io unit := format.print_using fmt options.mk meta definition pp_using {Ξ± : Type} [has_to_format Ξ±] (a : Ξ±) (o : options) : io unit := format.print_using (to_fmt a) o meta definition pp {Ξ± : Type} [has_to_format Ξ±] (a : Ξ±) : io unit := format.print (to_fmt a) /-- Run the external process specified by `args`. The process will run to completion with its output captured by a pipe, and read into `string` which is then returned. -/ def io.cmd (args : io.process.spawn_args) : io string := do child ← io.proc.spawn { stdout := io.process.stdio.piped, ..args }, buf ← io.fs.read_to_end child.stdout, exitv ← io.proc.wait child, when (exitv β‰  0) $ io.fail $ "process exited with status " ++ repr exitv, return buf.to_string /-- Lift a monadic `io` action into the `tactic` monad. -/ meta constant tactic.run_io {Ξ± : Type} : io Ξ± β†’ tactic Ξ±
e4523dd61df08cfee802fa48bb36d5e5947d1e2d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/finset/lattice.lean
dec7cb992bdec0a4b27074eac7a80df0f96ba4a3
[ "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
59,974
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.finset.fold import data.finset.option import data.finset.prod import data.multiset.lattice import order.complete_lattice /-! # Lattice operations on finsets -/ variables {Ξ± Ξ² Ξ³ ΞΉ : Type*} namespace finset open multiset order_dual /-! ### sup -/ section sup -- TODO: define with just `[has_bot Ξ±]` where some lemmas hold without requiring `[order_bot Ξ±]` variables [semilattice_sup Ξ±] [order_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 g : Ξ² β†’ Ξ±} lemma sup_def : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (βˆ… : finset Ξ²).sup f = βŠ₯ := fold_empty @[simp] lemma sup_cons {b : Ξ²} (h : b βˆ‰ s) : (cons b s h).sup f = f b βŠ” s.sup f := fold_cons h @[simp] lemma sup_insert [decidable_eq Ξ²] {b : Ξ²} : (insert b s : finset Ξ²).sup f = f b βŠ” s.sup f := fold_insert_idem lemma sup_image [decidable_eq Ξ²] (s : finset Ξ³) (f : Ξ³ β†’ Ξ²) (g : Ξ² β†’ Ξ±) : (s.image f).sup g = s.sup (g ∘ f) := fold_image_idem @[simp] lemma sup_map (s : finset Ξ³) (f : Ξ³ β†ͺ Ξ²) (g : Ξ² β†’ Ξ±) : (s.map f).sup g = s.sup (g ∘ f) := fold_map @[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] lemma sup_sup : s.sup (f βŠ” g) = s.sup f βŠ” s.sup g := begin refine finset.cons_induction_on s _ (Ξ» b t _ h, _), { rw [sup_empty, sup_empty, sup_empty, bot_sup_eq] }, { rw [sup_cons, sup_cons, sup_cons, h], exact sup_sup_sup_comm _ _ _ _ } end 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] protected 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 @[simp] lemma sup_bUnion [decidable_eq Ξ²] (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ²) : (s.bUnion t).sup f = s.sup (Ξ» x, (t x).sup f) := eq_of_forall_ge_iff $ Ξ» c, by simp [@forall_swap _ Ξ²] lemma sup_const {s : finset Ξ²} (h : s.nonempty) (c : Ξ±) : s.sup (Ξ» _, c) = c := eq_of_forall_ge_iff $ Ξ» b, finset.sup_le_iff.trans h.forall_const @[simp] lemma sup_bot (s : finset Ξ²) : s.sup (Ξ» _, βŠ₯) = (βŠ₯ : Ξ±) := begin obtain rfl | hs := s.eq_empty_or_nonempty, { exact sup_empty }, { exact sup_const hs _ } end lemma sup_ite (p : Ξ² β†’ Prop) [decidable_pred p] : s.sup (Ξ» i, ite (p i) (f i) (g i)) = (s.filter p).sup f βŠ” (s.filter (Ξ» i, Β¬ p i)).sup g := fold_ite _ lemma sup_le {a : Ξ±} : (βˆ€b ∈ s, f b ≀ a) β†’ s.sup f ≀ a := finset.sup_le_iff.2 lemma le_sup {b : Ξ²} (hb : b ∈ s) : f b ≀ s.sup f := finset.sup_le_iff.1 le_rfl _ 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) lemma sup_comm (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² β†’ Ξ³ β†’ Ξ±) : s.sup (Ξ» b, t.sup (f b)) = t.sup (Ξ» c, s.sup (Ξ» b, f b c)) := begin refine eq_of_forall_ge_iff (Ξ» a, _), simp_rw finset.sup_le_iff, exact ⟨λ h c hc b hb, h b hb c hc, Ξ» h b hb c hc, h c hc b hb⟩, end @[simp] lemma sup_attach (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : s.attach.sup (Ξ» x, f x) = s.sup f := (s.attach.sup_map (function.embedding.subtype _) f).symm.trans $ congr_arg _ attach_map_val /-- See also `finset.product_bUnion`. -/ lemma sup_product_left (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² Γ— Ξ³ β†’ Ξ±) : (s Γ—Λ’ t).sup f = s.sup (Ξ» i, t.sup $ Ξ» i', f ⟨i, i'⟩) := begin refine le_antisymm _ (sup_le (Ξ» i hi, sup_le $ Ξ» i' hi', le_sup $ mem_product.2 ⟨hi, hi'⟩)), refine sup_le _, rintro ⟨i, i'⟩ hi, rw mem_product at hi, refine le_trans _ (le_sup hi.1), convert le_sup hi.2, end lemma sup_product_right (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² Γ— Ξ³ β†’ Ξ±) : (s Γ—Λ’ t).sup f = t.sup (Ξ» i', s.sup $ Ξ» i, f ⟨i, i'⟩) := by rw [sup_product_left, sup_comm] @[simp] lemma sup_erase_bot [decidable_eq Ξ±] (s : finset Ξ±) : (s.erase βŠ₯).sup id = s.sup id := begin refine (sup_mono (s.erase_subset _)).antisymm (finset.sup_le_iff.2 $ Ξ» a ha, _), obtain rfl | ha' := eq_or_ne a βŠ₯, { exact bot_le }, { exact le_sup (mem_erase.2 ⟨ha', ha⟩) } end lemma sup_sdiff_right {Ξ± Ξ² : Type*} [generalized_boolean_algebra Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±) (a : Ξ±) : s.sup (Ξ» b, f b \ a) = s.sup f \ a := begin refine finset.cons_induction_on s _ (Ξ» b t _ h, _), { rw [sup_empty, sup_empty, bot_sdiff] }, { rw [sup_cons, sup_cons, h, sup_sdiff] } end lemma comp_sup_eq_sup_comp [semilattice_sup Ξ³] [order_bot Ξ³] {s : finset Ξ²} {f : Ξ² β†’ Ξ±} (g : Ξ± β†’ Ξ³) (g_sup : βˆ€ x y, g (x βŠ” y) = g x βŠ” g y) (bot : g βŠ₯ = βŠ₯) : g (s.sup f) = s.sup (g ∘ f) := finset.cons_induction_on s bot (Ξ» c t hc ih, by rw [sup_cons, sup_cons, g_sup, ih]) /-- Computing `sup` in a subtype (closed under `sup`) is the same as computing it in `Ξ±`. -/ lemma sup_coe {P : Ξ± β†’ Prop} {Pbot : P βŠ₯} {Psup : βˆ€{{x y}}, P x β†’ P y β†’ P (x βŠ” y)} (t : finset Ξ²) (f : Ξ² β†’ {x : Ξ± // P x}) : (@sup _ _ (subtype.semilattice_sup Psup) (subtype.order_bot Pbot) t f : Ξ±) = t.sup (Ξ» x, f x) := by { rw [comp_sup_eq_sup_comp coe]; intros; refl } @[simp] lemma sup_to_finset {Ξ± Ξ²} [decidable_eq Ξ²] (s : finset Ξ±) (f : Ξ± β†’ multiset Ξ²) : (s.sup f).to_finset = s.sup (Ξ» x, (f x).to_finset) := comp_sup_eq_sup_comp multiset.to_finset to_finset_union rfl lemma _root_.list.foldr_sup_eq_sup_to_finset [decidable_eq Ξ±] (l : list Ξ±) : l.foldr (βŠ”) βŠ₯ = l.to_finset.sup id := begin rw [←coe_fold_r, ←multiset.fold_dedup_idem, sup_def, ←list.to_finset_coe, to_finset_val, multiset.map_id], refl end 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⟩ lemma sup_induction {p : Ξ± β†’ Prop} (hb : p βŠ₯) (hp : βˆ€ a₁, p a₁ β†’ βˆ€ aβ‚‚, p aβ‚‚ β†’ p (a₁ βŠ” aβ‚‚)) (hs : βˆ€ b ∈ s, p (f b)) : p (s.sup f) := begin induction s using finset.cons_induction with c s hc ih, { exact hb, }, { rw sup_cons, apply hp, { exact hs c (mem_cons.2 (or.inl rfl)), }, { exact ih (Ξ» b h, hs b (mem_cons.2 (or.inr h))), }, }, end lemma sup_le_of_le_directed {Ξ± : Type*} [semilattice_sup Ξ±] [order_bot Ξ±] (s : set Ξ±) (hs : s.nonempty) (hdir : directed_on (≀) s) (t : finset Ξ±) : (βˆ€ x ∈ t, βˆƒ y ∈ s, x ≀ y) β†’ βˆƒ x, x ∈ s ∧ t.sup id ≀ x := begin classical, apply finset.induction_on t, { simpa only [forall_prop_of_true, and_true, forall_prop_of_false, bot_le, not_false_iff, sup_empty, forall_true_iff, not_mem_empty], }, { intros a r har ih h, have incs : ↑r βŠ† ↑(insert a r), by { rw finset.coe_subset, apply finset.subset_insert, }, -- x ∈ s is above the sup of r obtain ⟨x, ⟨hxs, hsx_sup⟩⟩ := ih (Ξ» x hx, h x $ incs hx), -- y ∈ s is above a obtain ⟨y, hys, hay⟩ := h a (finset.mem_insert_self a r), -- z ∈ s is above x and y obtain ⟨z, hzs, ⟨hxz, hyz⟩⟩ := hdir x hxs y hys, use [z, hzs], rw [sup_insert, id.def, sup_le_iff], exact ⟨le_trans hay hyz, le_trans hsx_sup hxz⟩, }, end -- If we acquire sublattices -- the hypotheses should be reformulated as `s : subsemilattice_sup_bot` lemma sup_mem (s : set Ξ±) (w₁ : βŠ₯ ∈ s) (wβ‚‚ : βˆ€ x y ∈ s, x βŠ” y ∈ s) {ΞΉ : Type*} (t : finset ΞΉ) (p : ΞΉ β†’ Ξ±) (h : βˆ€ i ∈ t, p i ∈ s) : t.sup p ∈ s := @sup_induction _ _ _ _ _ _ (∈ s) w₁ wβ‚‚ h @[simp] lemma sup_eq_bot_iff (f : Ξ² β†’ Ξ±) (S : finset Ξ²) : S.sup f = βŠ₯ ↔ βˆ€ s ∈ S, f s = βŠ₯ := begin classical, induction S using finset.induction with a S haS hi; simp [*], end 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) lemma sup_id_eq_Sup [complete_lattice Ξ±] (s : finset Ξ±) : s.sup id = Sup s := by simp [Sup_eq_supr, sup_eq_supr] lemma sup_id_set_eq_sUnion (s : finset (set Ξ±)) : s.sup id = ⋃₀(↑s) := sup_id_eq_Sup _ @[simp] lemma sup_set_eq_bUnion (s : finset Ξ±) (f : Ξ± β†’ set Ξ²) : s.sup f = ⋃ x ∈ s, f x := sup_eq_supr _ _ lemma sup_eq_Sup_image [complete_lattice Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : s.sup f = Sup (f '' s) := begin classical, rw [←finset.coe_image, ←sup_id_eq_Sup, sup_image, function.comp.left_id], end /-! ### inf -/ section inf -- TODO: define with just `[has_top Ξ±]` where some lemmas hold without requiring `[order_top Ξ±]` variables [semilattice_inf Ξ±] [order_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 g : Ξ² β†’ Ξ±} lemma inf_def : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (βˆ… : finset Ξ²).inf f = ⊀ := fold_empty @[simp] lemma inf_cons {b : Ξ²} (h : b βˆ‰ s) : (cons b s h).inf f = f b βŠ“ s.inf f := @sup_cons Ξ±α΅’α΅ˆ _ _ _ _ _ _ h @[simp] lemma inf_insert [decidable_eq Ξ²] {b : Ξ²} : (insert b s : finset Ξ²).inf f = f b βŠ“ s.inf f := fold_insert_idem lemma inf_image [decidable_eq Ξ²] (s : finset Ξ³) (f : Ξ³ β†’ Ξ²) (g : Ξ² β†’ Ξ±) : (s.image f).inf g = s.inf (g ∘ f) := fold_image_idem @[simp] lemma inf_map (s : finset Ξ³) (f : Ξ³ β†ͺ Ξ²) (g : Ξ² β†’ Ξ±) : (s.map f).inf g = s.inf (g ∘ f) := fold_map @[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 := @sup_union Ξ±α΅’α΅ˆ _ _ _ _ _ _ _ lemma inf_inf : s.inf (f βŠ“ g) = s.inf f βŠ“ s.inf g := @sup_sup Ξ±α΅’α΅ˆ _ _ _ _ _ _ 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 @[simp] lemma inf_bUnion [decidable_eq Ξ²] (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ²) : (s.bUnion t).inf f = s.inf (Ξ» x, (t x).inf f) := @sup_bUnion Ξ±α΅’α΅ˆ _ _ _ _ _ _ _ _ lemma inf_const {s : finset Ξ²} (h : s.nonempty) (c : Ξ±) : s.inf (Ξ» _, c) = c := @sup_const Ξ±α΅’α΅ˆ _ _ _ _ h _ @[simp] lemma inf_top (s : finset Ξ²) : s.inf (Ξ» _, ⊀) = (⊀ : Ξ±) := @sup_bot Ξ±α΅’α΅ˆ _ _ _ _ lemma le_inf_iff {a : Ξ±} : a ≀ s.inf f ↔ βˆ€ b ∈ s, a ≀ f b := @finset.sup_le_iff Ξ±α΅’α΅ˆ _ _ _ _ _ _ lemma inf_le {b : Ξ²} (hb : b ∈ s) : s.inf f ≀ f b := le_inf_iff.1 le_rfl _ 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 inf_attach (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : s.attach.inf (Ξ» x, f x) = s.inf f := @sup_attach Ξ±α΅’α΅ˆ _ _ _ _ _ lemma inf_comm (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² β†’ Ξ³ β†’ Ξ±) : s.inf (Ξ» b, t.inf (f b)) = t.inf (Ξ» c, s.inf (Ξ» b, f b c)) := @sup_comm Ξ±α΅’α΅ˆ _ _ _ _ _ _ _ lemma inf_product_left (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² Γ— Ξ³ β†’ Ξ±) : (s Γ—Λ’ t).inf f = s.inf (Ξ» i, t.inf $ Ξ» i', f ⟨i, i'⟩) := @sup_product_left Ξ±α΅’α΅ˆ _ _ _ _ _ _ _ lemma inf_product_right (s : finset Ξ²) (t : finset Ξ³) (f : Ξ² Γ— Ξ³ β†’ Ξ±) : (s Γ—Λ’ t).inf f = t.inf (Ξ» i', s.inf $ Ξ» i, f ⟨i, i'⟩) := @sup_product_right Ξ±α΅’α΅ˆ _ _ _ _ _ _ _ @[simp] lemma inf_erase_top [decidable_eq Ξ±] (s : finset Ξ±) : (s.erase ⊀).inf id = s.inf id := @sup_erase_bot Ξ±α΅’α΅ˆ _ _ _ _ lemma sup_sdiff_left {Ξ± Ξ² : Type*} [boolean_algebra Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±) (a : Ξ±) : s.sup (Ξ» b, a \ f b) = a \ s.inf f := begin refine finset.cons_induction_on s _ (Ξ» b t _ h, _), { rw [sup_empty, inf_empty, sdiff_top] }, { rw [sup_cons, inf_cons, h, sdiff_inf] } end lemma inf_sdiff_left {Ξ± Ξ² : Type*} [boolean_algebra Ξ±] {s : finset Ξ²} (hs : s.nonempty) (f : Ξ² β†’ Ξ±) (a : Ξ±) : s.inf (Ξ» b, a \ f b) = a \ s.sup f := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [sup_singleton, inf_singleton] }, { rw [sup_cons, inf_cons, h, sdiff_sup] } end lemma inf_sdiff_right {Ξ± Ξ² : Type*} [boolean_algebra Ξ±] {s : finset Ξ²} (hs : s.nonempty) (f : Ξ² β†’ Ξ±) (a : Ξ±) : s.inf (Ξ» b, f b \ a) = s.inf f \ a := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [inf_singleton, inf_singleton] }, { rw [inf_cons, inf_cons, h, inf_sdiff] } end lemma comp_inf_eq_inf_comp [semilattice_inf Ξ³] [order_top Ξ³] {s : finset Ξ²} {f : Ξ² β†’ Ξ±} (g : Ξ± β†’ Ξ³) (g_inf : βˆ€ x y, g (x βŠ“ y) = g x βŠ“ g y) (top : g ⊀ = ⊀) : g (s.inf f) = s.inf (g ∘ f) := @comp_sup_eq_sup_comp Ξ±α΅’α΅ˆ _ Ξ³α΅’α΅ˆ _ _ _ _ _ _ _ g_inf top /-- Computing `inf` in a subtype (closed under `inf`) is the same as computing it in `Ξ±`. -/ lemma inf_coe {P : Ξ± β†’ Prop} {Ptop : P ⊀} {Pinf : βˆ€{{x y}}, P x β†’ P y β†’ P (x βŠ“ y)} (t : finset Ξ²) (f : Ξ² β†’ {x : Ξ± // P x}) : (@inf _ _ (subtype.semilattice_inf Pinf) (subtype.order_top Ptop) t f : Ξ±) = t.inf (Ξ» x, f x) := @sup_coe Ξ±α΅’α΅ˆ _ _ _ _ Ptop Pinf t f lemma _root_.list.foldr_inf_eq_inf_to_finset [decidable_eq Ξ±] (l : list Ξ±) : l.foldr (βŠ“) ⊀ = l.to_finset.inf id := begin rw [←coe_fold_r, ←multiset.fold_dedup_idem, inf_def, ←list.to_finset_coe, to_finset_val, multiset.map_id], refl end lemma inf_induction {p : Ξ± β†’ Prop} (ht : p ⊀) (hp : βˆ€ a₁, p a₁ β†’ βˆ€ aβ‚‚, p aβ‚‚ β†’ p (a₁ βŠ“ aβ‚‚)) (hs : βˆ€ b ∈ s, p (f b)) : p (s.inf f) := @sup_induction Ξ±α΅’α΅ˆ _ _ _ _ _ _ ht hp hs lemma inf_mem (s : set Ξ±) (w₁ : ⊀ ∈ s) (wβ‚‚ : βˆ€ x y ∈ s, x βŠ“ y ∈ s) {ΞΉ : Type*} (t : finset ΞΉ) (p : ΞΉ β†’ Ξ±) (h : βˆ€ i ∈ t, p i ∈ s) : t.inf p ∈ s := @inf_induction _ _ _ _ _ _ (∈ s) w₁ wβ‚‚ h @[simp] lemma inf_eq_top_iff (f : Ξ² β†’ Ξ±) (S : finset Ξ²) : S.inf f = ⊀ ↔ βˆ€ s ∈ S, f s = ⊀ := @finset.sup_eq_bot_iff Ξ±α΅’α΅ˆ _ _ _ _ _ end inf @[simp] lemma to_dual_sup [semilattice_sup Ξ±] [order_bot Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : to_dual (s.sup f) = s.inf (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf [semilattice_inf Ξ±] [order_top Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : to_dual (s.inf f) = s.sup (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup [semilattice_inf Ξ±] [order_top Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±α΅’α΅ˆ) : of_dual (s.sup f) = s.inf (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf [semilattice_sup Ξ±] [order_bot Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±α΅’α΅ˆ) : of_dual (s.inf f) = s.sup (of_dual ∘ f) := rfl section distrib_lattice variables [distrib_lattice Ξ±] section order_bot variables [order_bot Ξ±] {s : finset Ξ²} {f : Ξ² β†’ Ξ±} {a : Ξ±} lemma sup_inf_distrib_left (s : finset ΞΉ) (f : ΞΉ β†’ Ξ±) (a : Ξ±) : a βŠ“ s.sup f = s.sup (Ξ» i, a βŠ“ f i) := begin induction s using finset.cons_induction with i s hi h, { simp_rw [finset.sup_empty, inf_bot_eq] }, { rw [sup_cons, sup_cons, inf_sup_left, h] } end lemma sup_inf_distrib_right (s : finset ΞΉ) (f : ΞΉ β†’ Ξ±) (a : Ξ±) : s.sup f βŠ“ a = s.sup (Ξ» i, f i βŠ“ a) := by { rw [_root_.inf_comm, s.sup_inf_distrib_left], simp_rw _root_.inf_comm } lemma disjoint_sup_right : disjoint a (s.sup f) ↔ βˆ€ i ∈ s, disjoint a (f i) := by simp only [disjoint_iff, sup_inf_distrib_left, sup_eq_bot_iff] lemma disjoint_sup_left : disjoint (s.sup f) a ↔ βˆ€ i ∈ s, disjoint (f i) a := by simp only [disjoint_iff, sup_inf_distrib_right, sup_eq_bot_iff] end order_bot section order_top variables [order_top Ξ±] lemma inf_sup_distrib_left (s : finset ΞΉ) (f : ΞΉ β†’ Ξ±) (a : Ξ±) : a βŠ” s.inf f = s.inf (Ξ» i, a βŠ” f i) := @sup_inf_distrib_left Ξ±α΅’α΅ˆ _ _ _ _ _ _ lemma inf_sup_distrib_right (s : finset ΞΉ) (f : ΞΉ β†’ Ξ±) (a : Ξ±) : s.inf f βŠ” a = s.inf (Ξ» i, f i βŠ” a) := @sup_inf_distrib_right Ξ±α΅’α΅ˆ _ _ _ _ _ _ end order_top end distrib_lattice section linear_order variables [linear_order Ξ±] section order_bot variables [order_bot Ξ±] {s : finset ΞΉ} {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma comp_sup_eq_sup_comp_of_is_total [semilattice_sup Ξ²] [order_bot Ξ²] (g : Ξ± β†’ Ξ²) (mono_g : monotone g) (bot : g βŠ₯ = βŠ₯) : g (s.sup f) = s.sup (g ∘ f) := comp_sup_eq_sup_comp g mono_g.map_sup bot @[simp] protected lemma le_sup_iff (ha : βŠ₯ < a) : a ≀ s.sup f ↔ βˆƒ b ∈ s, a ≀ f b := ⟨finset.cons_induction_on s (Ξ» h, absurd h (not_le_of_lt ha)) (Ξ» c t hc ih, by simpa using @or.rec _ _ (βˆƒ b, (b = c ∨ b ∈ t) ∧ a ≀ f b) (Ξ» h, ⟨c, or.inl rfl, h⟩) (Ξ» h, let ⟨b, hb, hle⟩ := ih h in ⟨b, or.inr hb, hle⟩)), (Ξ» ⟨b, hb, hle⟩, trans hle (le_sup hb))⟩ @[simp] protected lemma lt_sup_iff : a < s.sup f ↔ βˆƒ b ∈ s, a < f b := ⟨finset.cons_induction_on s (Ξ» h, absurd h not_lt_bot) (Ξ» c t hc ih, by simpa using @or.rec _ _ (βˆƒ b, (b = c ∨ b ∈ t) ∧ a < f b) (Ξ» h, ⟨c, or.inl rfl, h⟩) (Ξ» h, let ⟨b, hb, hlt⟩ := ih h in ⟨b, or.inr hb, hlt⟩)), (Ξ» ⟨b, hb, hlt⟩, lt_of_lt_of_le hlt (le_sup hb))⟩ @[simp] protected lemma sup_lt_iff (ha : βŠ₯ < a) : s.sup f < a ↔ βˆ€ b ∈ s, f b < a := ⟨(Ξ» hs b hb, lt_of_le_of_lt (le_sup hb) hs), finset.cons_induction_on s (Ξ» _, ha) (Ξ» c t hc, by simpa only [sup_cons, sup_lt_iff, mem_cons, forall_eq_or_imp] using and.imp_right)⟩ end order_bot section order_top variables [order_top Ξ±] {s : finset ΞΉ} {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma comp_inf_eq_inf_comp_of_is_total [semilattice_inf Ξ²] [order_top Ξ²] (g : Ξ± β†’ Ξ²) (mono_g : monotone g) (top : g ⊀ = ⊀) : g (s.inf f) = s.inf (g ∘ f) := comp_inf_eq_inf_comp g mono_g.map_inf top @[simp] protected lemma inf_le_iff (ha : a < ⊀) : s.inf f ≀ a ↔ βˆƒ b ∈ s, f b ≀ a := @finset.le_sup_iff Ξ±α΅’α΅ˆ _ _ _ _ _ _ ha @[simp] protected lemma inf_lt_iff : s.inf f < a ↔ βˆƒ b ∈ s, f b < a := @finset.lt_sup_iff Ξ±α΅’α΅ˆ _ _ _ _ _ _ @[simp] protected lemma lt_inf_iff (ha : a < ⊀) : a < s.inf f ↔ βˆ€ b ∈ s, a < f b := @finset.sup_lt_iff Ξ±α΅’α΅ˆ _ _ _ _ _ _ ha end order_top end linear_order lemma inf_eq_infi [complete_lattice Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : s.inf f = β¨… a ∈ s, f a := @sup_eq_supr _ Ξ²α΅’α΅ˆ _ _ _ lemma inf_id_eq_Inf [complete_lattice Ξ±] (s : finset Ξ±) : s.inf id = Inf s := @sup_id_eq_Sup Ξ±α΅’α΅ˆ _ _ lemma inf_id_set_eq_sInter (s : finset (set Ξ±)) : s.inf id = β‹‚β‚€(↑s) := inf_id_eq_Inf _ @[simp] lemma inf_set_eq_bInter (s : finset Ξ±) (f : Ξ± β†’ set Ξ²) : s.inf f = β‹‚ x ∈ s, f x := inf_eq_infi _ _ lemma inf_eq_Inf_image [complete_lattice Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : s.inf f = Inf (f '' s) := @sup_eq_Sup_image _ Ξ²α΅’α΅ˆ _ _ _ section sup' variables [semilattice_sup Ξ±] lemma sup_of_mem {s : finset Ξ²} (f : Ξ² β†’ Ξ±) {b : Ξ²} (h : b ∈ s) : βˆƒ (a : Ξ±), s.sup (coe ∘ f : Ξ² β†’ with_bot Ξ±) = ↑a := Exists.imp (Ξ» a, Exists.fst) (@le_sup (with_bot Ξ±) _ _ _ _ _ _ h (f b) rfl) /-- Given nonempty finset `s` then `s.sup' H f` is the supremum of its image under `f` in (possibly unbounded) join-semilattice `Ξ±`, where `H` is a proof of nonemptiness. If `Ξ±` has a bottom element you may instead use `finset.sup` which does not require `s` nonempty. -/ def sup' (s : finset Ξ²) (H : s.nonempty) (f : Ξ² β†’ Ξ±) : Ξ± := with_bot.unbot (s.sup (coe ∘ f)) (by simpa using H) variables {s : finset Ξ²} (H : s.nonempty) (f : Ξ² β†’ Ξ±) @[simp] lemma coe_sup' : ((s.sup' H f : Ξ±) : with_bot Ξ±) = s.sup (coe ∘ f) := by rw [sup', with_bot.coe_unbot] @[simp] lemma sup'_cons {b : Ξ²} {hb : b βˆ‰ s} {h : (cons b s hb).nonempty} : (cons b s hb).sup' h f = f b βŠ” s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_cons, with_bot.coe_sup], } @[simp] lemma sup'_insert [decidable_eq Ξ²] {b : Ξ²} {h : (insert b s).nonempty} : (insert b s).sup' h f = f b βŠ” s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_insert, with_bot.coe_sup], } @[simp] lemma sup'_singleton {b : Ξ²} {h : ({b} : finset Ξ²).nonempty} : ({b} : finset Ξ²).sup' h f = f b := rfl lemma sup'_le {a : Ξ±} (hs : βˆ€ b ∈ s, f b ≀ a) : s.sup' H f ≀ a := by { rw [←with_bot.coe_le_coe, coe_sup'], exact sup_le (Ξ» b h, with_bot.coe_le_coe.2 $ hs b h), } lemma le_sup' {b : Ξ²} (h : b ∈ s) : f b ≀ s.sup' ⟨b, h⟩ f := by { rw [←with_bot.coe_le_coe, coe_sup'], exact le_sup h, } @[simp] lemma sup'_const (a : Ξ±) : s.sup' H (Ξ» b, a) = a := begin apply le_antisymm, { apply sup'_le, intros, exact le_rfl, }, { apply le_sup' (Ξ» b, a) H.some_spec, } end @[simp] lemma sup'_le_iff {a : Ξ±} : s.sup' H f ≀ a ↔ βˆ€ b ∈ s, f b ≀ a := iff.intro (Ξ» h b hb, trans (le_sup' f hb) h) (sup'_le H f) lemma sup'_bUnion [decidable_eq Ξ²] {s : finset Ξ³} (Hs : s.nonempty) {t : Ξ³ β†’ finset Ξ²} (Ht : βˆ€ b, (t b).nonempty) : (s.bUnion t).sup' (Hs.bUnion (Ξ» b _, Ht b)) f = s.sup' Hs (Ξ» b, (t b).sup' (Ht b) f) := eq_of_forall_ge_iff $ Ξ» c, by simp [@forall_swap _ Ξ²] lemma comp_sup'_eq_sup'_comp [semilattice_sup Ξ³] {s : finset Ξ²} (H : s.nonempty) {f : Ξ² β†’ Ξ±} (g : Ξ± β†’ Ξ³) (g_sup : βˆ€ x y, g (x βŠ” y) = g x βŠ” g y) : g (s.sup' H f) = s.sup' H (g ∘ f) := begin rw [←with_bot.coe_eq_coe, coe_sup'], let g' := with_bot.map g, show g' ↑(s.sup' H f) = s.sup (Ξ» a, g' ↑(f a)), rw coe_sup', refine comp_sup_eq_sup_comp g' _ rfl, intros f₁ fβ‚‚, induction f₁ using with_bot.rec_bot_coe, { rw [bot_sup_eq], exact bot_sup_eq.symm, }, { induction fβ‚‚ using with_bot.rec_bot_coe, { refl }, { exact congr_arg coe (g_sup f₁ fβ‚‚) } } end lemma sup'_induction {p : Ξ± β†’ Prop} (hp : βˆ€ a₁, p a₁ β†’ βˆ€ aβ‚‚, p aβ‚‚ β†’ p (a₁ βŠ” aβ‚‚)) (hs : βˆ€ b ∈ s, p (f b)) : p (s.sup' H f) := begin show @with_bot.rec_bot_coe Ξ± (Ξ» _, Prop) true p ↑(s.sup' H f), rw coe_sup', refine sup_induction trivial _ hs, rintro (_|a₁) h₁ aβ‚‚ hβ‚‚, { rw [with_bot.none_eq_bot, bot_sup_eq], exact hβ‚‚ }, cases aβ‚‚, exacts [h₁, hp a₁ h₁ aβ‚‚ hβ‚‚] end lemma sup'_mem (s : set Ξ±) (w : βˆ€ x y ∈ s, x βŠ” y ∈ s) {ΞΉ : Type*} (t : finset ΞΉ) (H : t.nonempty) (p : ΞΉ β†’ Ξ±) (h : βˆ€ i ∈ t, p i ∈ s) : t.sup' H p ∈ s := sup'_induction H p w h @[congr] lemma sup'_congr {t : finset Ξ²} {f g : Ξ² β†’ Ξ±} (h₁ : s = t) (hβ‚‚ : βˆ€ x ∈ s, f x = g x) : s.sup' H f = t.sup' (h₁ β–Έ H) g := begin subst s, refine eq_of_forall_ge_iff (Ξ» c, _), simp only [sup'_le_iff, hβ‚‚] { contextual := tt } end @[simp] lemma sup'_map {s : finset Ξ³} {f : Ξ³ β†ͺ Ξ²} (g : Ξ² β†’ Ξ±) (hs : (s.map f).nonempty) (hs': s.nonempty := finset.map_nonempty.mp hs) : (s.map f).sup' hs g = s.sup' hs' (g ∘ f) := by rw [←with_bot.coe_eq_coe, coe_sup', sup_map, coe_sup'] end sup' section inf' variables [semilattice_inf Ξ±] lemma inf_of_mem {s : finset Ξ²} (f : Ξ² β†’ Ξ±) {b : Ξ²} (h : b ∈ s) : βˆƒ (a : Ξ±), s.inf (coe ∘ f : Ξ² β†’ with_top Ξ±) = ↑a := @sup_of_mem Ξ±α΅’α΅ˆ _ _ _ f _ h /-- Given nonempty finset `s` then `s.inf' H f` is the infimum of its image under `f` in (possibly unbounded) meet-semilattice `Ξ±`, where `H` is a proof of nonemptiness. If `Ξ±` has a top element you may instead use `finset.inf` which does not require `s` nonempty. -/ def inf' (s : finset Ξ²) (H : s.nonempty) (f : Ξ² β†’ Ξ±) : Ξ± := with_top.untop (s.inf (coe ∘ f)) (by simpa using H) variables {s : finset Ξ²} (H : s.nonempty) (f : Ξ² β†’ Ξ±) {a : Ξ±} {b : Ξ²} @[simp] lemma coe_inf' : ((s.inf' H f : Ξ±) : with_top Ξ±) = s.inf (coe ∘ f) := @coe_sup' Ξ±α΅’α΅ˆ _ _ _ H f @[simp] lemma inf'_cons {b : Ξ²} {hb : b βˆ‰ s} {h : (cons b s hb).nonempty} : (cons b s hb).inf' h f = f b βŠ“ s.inf' H f := @sup'_cons Ξ±α΅’α΅ˆ _ _ _ H f _ _ h @[simp] lemma inf'_insert [decidable_eq Ξ²] {b : Ξ²} {h : (insert b s).nonempty} : (insert b s).inf' h f = f b βŠ“ s.inf' H f := @sup'_insert Ξ±α΅’α΅ˆ _ _ _ H f _ _ h @[simp] lemma inf'_singleton {b : Ξ²} {h : ({b} : finset Ξ²).nonempty} : ({b} : finset Ξ²).inf' h f = f b := rfl lemma le_inf' (hs : βˆ€ b ∈ s, a ≀ f b) : a ≀ s.inf' H f := @sup'_le Ξ±α΅’α΅ˆ _ _ _ H f _ hs lemma inf'_le (h : b ∈ s) : s.inf' ⟨b, h⟩ f ≀ f b := @le_sup' Ξ±α΅’α΅ˆ _ _ _ f _ h @[simp] lemma inf'_const (a : Ξ±) : s.inf' H (Ξ» b, a) = a := @sup'_const Ξ±α΅’α΅ˆ _ _ _ H _ @[simp] lemma le_inf'_iff : a ≀ s.inf' H f ↔ βˆ€ b ∈ s, a ≀ f b := @sup'_le_iff Ξ±α΅’α΅ˆ _ _ _ H f _ lemma inf'_bUnion [decidable_eq Ξ²] {s : finset Ξ³} (Hs : s.nonempty) {t : Ξ³ β†’ finset Ξ²} (Ht : βˆ€ b, (t b).nonempty) : (s.bUnion t).inf' (Hs.bUnion (Ξ» b _, Ht b)) f = s.inf' Hs (Ξ» b, (t b).inf' (Ht b) f) := @sup'_bUnion Ξ±α΅’α΅ˆ _ _ _ _ _ _ Hs _ Ht lemma comp_inf'_eq_inf'_comp [semilattice_inf Ξ³] {s : finset Ξ²} (H : s.nonempty) {f : Ξ² β†’ Ξ±} (g : Ξ± β†’ Ξ³) (g_inf : βˆ€ x y, g (x βŠ“ y) = g x βŠ“ g y) : g (s.inf' H f) = s.inf' H (g ∘ f) := @comp_sup'_eq_sup'_comp Ξ±α΅’α΅ˆ _ Ξ³α΅’α΅ˆ _ _ _ H f g g_inf lemma inf'_induction {p : Ξ± β†’ Prop} (hp : βˆ€ a₁, p a₁ β†’ βˆ€ aβ‚‚, p aβ‚‚ β†’ p (a₁ βŠ“ aβ‚‚)) (hs : βˆ€ b ∈ s, p (f b)) : p (s.inf' H f) := @sup'_induction Ξ±α΅’α΅ˆ _ _ _ H f _ hp hs lemma inf'_mem (s : set Ξ±) (w : βˆ€ x y ∈ s, x βŠ“ y ∈ s) {ΞΉ : Type*} (t : finset ΞΉ) (H : t.nonempty) (p : ΞΉ β†’ Ξ±) (h : βˆ€ i ∈ t, p i ∈ s) : t.inf' H p ∈ s := inf'_induction H p w h @[congr] lemma inf'_congr {t : finset Ξ²} {f g : Ξ² β†’ Ξ±} (h₁ : s = t) (hβ‚‚ : βˆ€ x ∈ s, f x = g x) : s.inf' H f = t.inf' (h₁ β–Έ H) g := @sup'_congr Ξ±α΅’α΅ˆ _ _ _ H _ _ _ h₁ hβ‚‚ @[simp] lemma inf'_map {s : finset Ξ³} {f : Ξ³ β†ͺ Ξ²} (g : Ξ² β†’ Ξ±) (hs : (s.map f).nonempty) (hs': s.nonempty := finset.map_nonempty.mp hs) : (s.map f).inf' hs g = s.inf' hs' (g ∘ f) := @sup'_map Ξ±α΅’α΅ˆ _ _ _ _ _ _ hs hs' end inf' section sup variables [semilattice_sup Ξ±] [order_bot Ξ±] lemma sup'_eq_sup {s : finset Ξ²} (H : s.nonempty) (f : Ξ² β†’ Ξ±) : s.sup' H f = s.sup f := le_antisymm (sup'_le H f (Ξ» b, le_sup)) (sup_le (Ξ» b, le_sup' f)) lemma sup_closed_of_sup_closed {s : set Ξ±} (t : finset Ξ±) (htne : t.nonempty) (h_subset : ↑t βŠ† s) (h : βˆ€ a b ∈ s, a βŠ” b ∈ s) : t.sup id ∈ s := sup'_eq_sup htne id β–Έ sup'_induction _ _ h h_subset lemma coe_sup_of_nonempty {s : finset Ξ²} (h : s.nonempty) (f : Ξ² β†’ Ξ±) : (↑(s.sup f) : with_bot Ξ±) = s.sup (coe ∘ f) := by simp only [←sup'_eq_sup h, coe_sup' h] end sup section inf variables [semilattice_inf Ξ±] [order_top Ξ±] lemma inf'_eq_inf {s : finset Ξ²} (H : s.nonempty) (f : Ξ² β†’ Ξ±) : s.inf' H f = s.inf f := @sup'_eq_sup Ξ±α΅’α΅ˆ _ _ _ _ H f lemma inf_closed_of_inf_closed {s : set Ξ±} (t : finset Ξ±) (htne : t.nonempty) (h_subset : ↑t βŠ† s) (h : βˆ€ a b ∈ s, a βŠ“ b ∈ s) : t.inf id ∈ s := @sup_closed_of_sup_closed Ξ±α΅’α΅ˆ _ _ _ t htne h_subset h lemma coe_inf_of_nonempty {s : finset Ξ²} (h : s.nonempty) (f : Ξ² β†’ Ξ±): (↑(s.inf f) : with_top Ξ±) = s.inf (Ξ» i, f i) := @coe_sup_of_nonempty Ξ±α΅’α΅ˆ _ _ _ _ h f end inf section sup variables {C : Ξ² β†’ Type*} [Ξ  (b : Ξ²), semilattice_sup (C b)] [Ξ  (b : Ξ²), order_bot (C b)] @[simp] protected lemma sup_apply (s : finset Ξ±) (f : Ξ± β†’ Ξ  (b : Ξ²), C b) (b : Ξ²) : s.sup f b = s.sup (Ξ» a, f a b) := comp_sup_eq_sup_comp (Ξ» x : Ξ  b : Ξ², C b, x b) (Ξ» i j, rfl) rfl end sup section inf variables {C : Ξ² β†’ Type*} [Ξ  (b : Ξ²), semilattice_inf (C b)] [Ξ  (b : Ξ²), order_top (C b)] @[simp] protected lemma inf_apply (s : finset Ξ±) (f : Ξ± β†’ Ξ  (b : Ξ²), C b) (b : Ξ²) : s.inf f b = s.inf (Ξ» a, f a b) := @finset.sup_apply _ _ (Ξ» b, (C b)α΅’α΅ˆ) _ _ s f b end inf section sup' variables {C : Ξ² β†’ Type*} [Ξ  (b : Ξ²), semilattice_sup (C b)] @[simp] protected lemma sup'_apply {s : finset Ξ±} (H : s.nonempty) (f : Ξ± β†’ Ξ  (b : Ξ²), C b) (b : Ξ²) : s.sup' H f b = s.sup' H (Ξ» a, f a b) := comp_sup'_eq_sup'_comp H (Ξ» x : Ξ  b : Ξ², C b, x b) (Ξ» i j, rfl) end sup' section inf' variables {C : Ξ² β†’ Type*} [Ξ  (b : Ξ²), semilattice_inf (C b)] @[simp] protected lemma inf'_apply {s : finset Ξ±} (H : s.nonempty) (f : Ξ± β†’ Ξ  (b : Ξ²), C b) (b : Ξ²) : s.inf' H f b = s.inf' H (Ξ» a, f a b) := @finset.sup'_apply _ _ (Ξ» b, (C b)α΅’α΅ˆ) _ _ H f b end inf' @[simp] lemma to_dual_sup' [semilattice_sup Ξ±] {s : finset ΞΉ} (hs : s.nonempty) (f : ΞΉ β†’ Ξ±) : to_dual (s.sup' hs f) = s.inf' hs (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf' [semilattice_inf Ξ±] {s : finset ΞΉ} (hs : s.nonempty) (f : ΞΉ β†’ Ξ±) : to_dual (s.inf' hs f) = s.sup' hs (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup' [semilattice_inf Ξ±] {s : finset ΞΉ} (hs : s.nonempty) (f : ΞΉ β†’ Ξ±α΅’α΅ˆ) : of_dual (s.sup' hs f) = s.inf' hs (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf' [semilattice_sup Ξ±] {s : finset ΞΉ} (hs : s.nonempty) (f : ΞΉ β†’ Ξ±α΅’α΅ˆ) : of_dual (s.inf' hs f) = s.sup' hs (of_dual ∘ f) := rfl section linear_order variables [linear_order Ξ±] {s : finset ΞΉ} (H : s.nonempty) {f : ΞΉ β†’ Ξ±} {a : Ξ±} @[simp] lemma le_sup'_iff : a ≀ s.sup' H f ↔ βˆƒ b ∈ s, a ≀ f b := begin rw [←with_bot.coe_le_coe, coe_sup', finset.le_sup_iff (with_bot.bot_lt_coe a)], exact bex_congr (Ξ» b hb, with_bot.coe_le_coe), end @[simp] lemma lt_sup'_iff : a < s.sup' H f ↔ βˆƒ b ∈ s, a < f b := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.lt_sup_iff], exact bex_congr (Ξ» b hb, with_bot.coe_lt_coe), end @[simp] lemma sup'_lt_iff : s.sup' H f < a ↔ βˆ€ i ∈ s, f i < a := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.sup_lt_iff (with_bot.bot_lt_coe a)], exact ball_congr (Ξ» b hb, with_bot.coe_lt_coe), end @[simp] lemma inf'_le_iff : s.inf' H f ≀ a ↔ βˆƒ i ∈ s, f i ≀ a := @le_sup'_iff Ξ±α΅’α΅ˆ _ _ _ H f _ @[simp] lemma inf'_lt_iff : s.inf' H f < a ↔ βˆƒ i ∈ s, f i < a := @lt_sup'_iff Ξ±α΅’α΅ˆ _ _ _ H f _ @[simp] lemma lt_inf'_iff : a < s.inf' H f ↔ βˆ€ i ∈ s, a < f i := @sup'_lt_iff Ξ±α΅’α΅ˆ _ _ _ H f _ lemma exists_mem_eq_sup' (f : ΞΉ β†’ Ξ±) : βˆƒ i, i ∈ s ∧ s.sup' H f = f i := begin refine H.cons_induction (Ξ» c, _) (Ξ» c s hc hs ih, _), { exact ⟨c, mem_singleton_self c, rfl⟩, }, { rcases ih with ⟨b, hb, h'⟩, rw [sup'_cons hs, h'], cases total_of (≀) (f b) (f c) with h h, { exact ⟨c, mem_cons.2 (or.inl rfl), sup_eq_left.2 h⟩, }, { exact ⟨b, mem_cons.2 (or.inr hb), sup_eq_right.2 h⟩, }, }, end lemma exists_mem_eq_inf' (f : ΞΉ β†’ Ξ±) : βˆƒ i, i ∈ s ∧ s.inf' H f = f i := @exists_mem_eq_sup' Ξ±α΅’α΅ˆ _ _ _ H f lemma exists_mem_eq_sup [order_bot Ξ±] (s : finset ΞΉ) (h : s.nonempty) (f : ΞΉ β†’ Ξ±) : βˆƒ i, i ∈ s ∧ s.sup f = f i := sup'_eq_sup h f β–Έ exists_mem_eq_sup' h f lemma exists_mem_eq_inf [order_top Ξ±] (s : finset ΞΉ) (h : s.nonempty) (f : ΞΉ β†’ Ξ±) : βˆƒ i, i ∈ s ∧ s.inf f = f i := @exists_mem_eq_sup Ξ±α΅’α΅ˆ _ _ _ _ h f end linear_order /-! ### max and min of finite sets -/ section max_min variables [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 `βŠ₯` otherwise. It belongs to `with_bot Ξ±`. If you want to get an element of `Ξ±`, see `s.max'`. -/ protected def max (s : finset Ξ±) : with_bot Ξ± := sup s coe lemma max_eq_sup_coe {s : finset Ξ±} : s.max = s.sup coe := rfl theorem max_eq_sup_with_bot (s : finset Ξ±) : s.max = sup s coe := rfl @[simp] theorem max_empty : (βˆ… : finset Ξ±).max = βŠ₯ := rfl @[simp] theorem max_insert {a : Ξ±} {s : finset Ξ±} : (insert a s).max = max a s.max := fold_insert_idem @[simp] theorem max_singleton {a : Ξ±} : finset.max {a} = (a : with_bot Ξ±) := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset Ξ±} {a : Ξ±} (h : a ∈ s) : βˆƒ (b : Ξ±), s.max = b := (@le_sup (with_bot Ξ±) _ _ _ _ _ _ h _ rfl).imp $ Ξ» b, Exists.fst theorem max_of_nonempty {s : finset Ξ±} (h : s.nonempty) : βˆƒ (a : Ξ±), s.max = a := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_bot {s : finset Ξ±} : s.max = βŠ₯ ↔ 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 : Ξ±}, s.max = a β†’ a ∈ s := finset.induction_on s (Ξ» _ H, by cases H) (Ξ» b s _ (ih : βˆ€ {a : Ξ±}, s.max = a β†’ a ∈ s) a (h : (insert b s).max = a), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases max_choice ↑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) lemma coe_le_max_of_mem {a : Ξ±} {s : finset Ξ±} (as : a ∈ s) : ↑a ≀ s.max := le_sup as lemma not_mem_of_max_lt_coe {a : Ξ±} {s : finset Ξ±} (h : s.max < a) : a βˆ‰ s := mt coe_le_max_of_mem h.not_le theorem le_max_of_mem {s : finset Ξ±} {a b : Ξ±} (h₁ : a ∈ s) (hβ‚‚ : s.max = b) : a ≀ b := with_bot.coe_le_coe.mp $ (coe_le_max_of_mem h₁).trans hβ‚‚.le theorem not_mem_of_max_lt {s : finset Ξ±} {a b : Ξ±} (h₁ : b < a) (hβ‚‚ : s.max = ↑b) : a βˆ‰ s := finset.not_mem_of_max_lt_coe $ hβ‚‚.trans_lt $ with_bot.coe_lt_coe.mpr h₁ lemma max_mono {s t : finset Ξ±} (st : s βŠ† t) : s.max ≀ t.max := sup_mono st lemma max_le {M : with_bot Ξ±} {s : finset Ξ±} (st : βˆ€ a : Ξ±, a ∈ s β†’ (a : with_bot Ξ±) ≀ M) : s.max ≀ M := sup_le st /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `⊀` otherwise. It belongs to `with_top Ξ±`. If you want to get an element of `Ξ±`, see `s.min'`. -/ protected def min (s : finset Ξ±) : with_top Ξ± := inf s coe theorem min_eq_inf_with_top (s : finset Ξ±) : s.min = inf s coe := rfl @[simp] theorem min_empty : (βˆ… : finset Ξ±).min = ⊀ := rfl @[simp] theorem min_insert {a : Ξ±} {s : finset Ξ±} : (insert a s).min = min ↑a s.min := fold_insert_idem @[simp] theorem min_singleton {a : Ξ±} : finset.min {a} = (a : with_top Ξ±) := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset Ξ±} {a : Ξ±} (h : a ∈ s) : βˆƒ b : Ξ±, s.min = b := (@inf_le (with_top Ξ±) _ _ _ _ _ _ h _ rfl).imp $ Ξ» b, Exists.fst theorem min_of_nonempty {s : finset Ξ±} (h : s.nonempty) : βˆƒ a : Ξ±, s.min = a := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_top {s : finset Ξ±} : s.min = ⊀ ↔ 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 : Ξ±}, s.min = a β†’ a ∈ s := @mem_of_max Ξ±α΅’α΅ˆ _ s lemma min_le_coe_of_mem {a : Ξ±} {s : finset Ξ±} (as : a ∈ s) : s.min ≀ a := inf_le as lemma not_mem_of_coe_lt_min {a : Ξ±} {s : finset Ξ±} (h : ↑a < s.min) : a βˆ‰ s := mt min_le_coe_of_mem h.not_le theorem min_le_of_mem {s : finset Ξ±} {a b : Ξ±} (h₁ : b ∈ s) (hβ‚‚ : s.min = a) : a ≀ b := with_top.coe_le_coe.mp $ hβ‚‚.ge.trans (min_le_coe_of_mem h₁) theorem not_mem_of_lt_min {s : finset Ξ±} {a b : Ξ±} (h₁ : a < b) (hβ‚‚ : s.min = ↑b) : a βˆ‰ s := finset.not_mem_of_coe_lt_min $ ( with_top.coe_lt_coe.mpr h₁).trans_eq hβ‚‚.symm lemma min_mono {s t : finset Ξ±} (st : s βŠ† t) : t.min ≀ s.min := inf_mono st lemma le_min {m : with_top Ξ±} {s : finset Ξ±} (st : βˆ€ a : Ξ±, a ∈ s β†’ m ≀ a) : m ≀ s.min := le_inf st /-- 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 `with_top Ξ±`. -/ def min' (s : finset Ξ±) (H : s.nonempty) : Ξ± := inf' s H id /-- 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 `with_bot Ξ±`. -/ def max' (s : finset Ξ±) (H : s.nonempty) : Ξ± := sup' s H id variables (s : finset Ξ±) (H : s.nonempty) {x : Ξ±} theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min', finset.min] theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≀ x := min_le_of_mem H2 (with_top.coe_untop _ _).symm theorem le_min' (x) (H2 : βˆ€ y ∈ s, x ≀ y) : x ≀ s.min' H := H2 _ $ min'_mem _ _ theorem is_least_min' : is_least ↑s (s.min' H) := ⟨min'_mem _ _, min'_le _⟩ @[simp] lemma le_min'_iff {x} : x ≀ s.min' H ↔ βˆ€ y ∈ s, x ≀ y := le_is_glb_iff (is_least_min' s H).is_glb /-- `{a}.min' _` is `a`. -/ @[simp] lemma min'_singleton (a : Ξ±) : ({a} : finset Ξ±).min' (singleton_nonempty _) = a := by simp [min'] theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max', finset.max] theorem le_max' (x) (H2 : x ∈ s) : x ≀ s.max' ⟨x, H2⟩ := le_max_of_mem H2 (with_bot.coe_unbot _ _).symm theorem max'_le (x) (H2 : βˆ€ y ∈ s, y ≀ x) : s.max' H ≀ x := H2 _ $ max'_mem _ _ theorem is_greatest_max' : is_greatest ↑s (s.max' H) := ⟨max'_mem _ _, le_max' _⟩ @[simp] lemma max'_le_iff {x} : s.max' H ≀ x ↔ βˆ€ y ∈ s, y ≀ x := is_lub_le_iff (is_greatest_max' s H).is_lub @[simp] lemma max'_lt_iff {x} : s.max' H < x ↔ βˆ€ y ∈ s, y < x := ⟨λ Hlt y hy, (s.le_max' y hy).trans_lt Hlt, Ξ» H, H _ $ s.max'_mem _⟩ @[simp] lemma lt_min'_iff : x < s.min' H ↔ βˆ€ y ∈ s, x < y := @max'_lt_iff Ξ±α΅’α΅ˆ _ _ H _ lemma max'_eq_sup' : s.max' H = s.sup' H id := eq_of_forall_ge_iff $ Ξ» a, (max'_le_iff _ _).trans (sup'_le_iff _ _).symm lemma min'_eq_inf' : s.min' H = s.inf' H id := @max'_eq_sup' Ξ±α΅’α΅ˆ _ s H /-- `{a}.max' _` is `a`. -/ @[simp] lemma max'_singleton (a : Ξ±) : ({a} : finset Ξ±).max' (singleton_nonempty _) = a := by simp [max'] theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i β‰  j) : s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ := is_glb_lt_is_lub_of_ne (s.is_least_min' _).is_glb (s.is_greatest_max' _).is_lub H1 H2 H3 /-- 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' (finset.card_pos.mp $ lt_trans zero_lt_one hβ‚‚) < s.max' (finset.card_pos.mp $ lt_trans zero_lt_one hβ‚‚) := begin rcases one_lt_card.1 hβ‚‚ with ⟨a, ha, b, hb, hab⟩, exact s.min'_lt_max' ha hb hab end lemma map_of_dual_min (s : finset Ξ±α΅’α΅ˆ) : s.min.map of_dual = (s.image of_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_of_dual_max (s : finset Ξ±α΅’α΅ˆ) : s.max.map of_dual = (s.image of_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma map_to_dual_min (s : finset Ξ±) : s.min.map to_dual = (s.image to_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_to_dual_max (s : finset Ξ±) : s.max.map to_dual = (s.image to_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma of_dual_min' {s : finset Ξ±α΅’α΅ˆ} (hs : s.nonempty) : of_dual (min' s hs) = max' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma of_dual_max' {s : finset Ξ±α΅’α΅ˆ} (hs : s.nonempty) : of_dual (max' s hs) = min' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_min' {s : finset Ξ±} (hs : s.nonempty) : to_dual (min' s hs) = max' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_max' {s : finset Ξ±} (hs : s.nonempty) : to_dual (max' s hs) = min' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma max'_subset {s t : finset Ξ±} (H : s.nonempty) (hst : s βŠ† t) : s.max' H ≀ t.max' (H.mono hst) := le_max' _ _ (hst (s.max'_mem H)) lemma min'_subset {s t : finset Ξ±} (H : s.nonempty) (hst : s βŠ† t) : t.min' (H.mono hst) ≀ s.min' H := min'_le _ _ (hst (s.min'_mem H)) lemma max'_insert (a : Ξ±) (s : finset Ξ±) (H : s.nonempty) : (insert a s).max' (s.insert_nonempty a) = max (s.max' H) a := (is_greatest_max' _ _).unique $ by { rw [coe_insert, max_comm], exact (is_greatest_max' _ _).insert _ } lemma min'_insert (a : Ξ±) (s : finset Ξ±) (H : s.nonempty) : (insert a s).min' (s.insert_nonempty a) = min (s.min' H) a := (is_least_min' _ _).unique $ by { rw [coe_insert, min_comm], exact (is_least_min' _ _).insert _ } lemma lt_max'_of_mem_erase_max' [decidable_eq Ξ±] {a : Ξ±} (ha : a ∈ s.erase (s.max' H)) : a < s.max' H := lt_of_le_of_ne (le_max' _ _ (mem_of_mem_erase ha)) $ ne_of_mem_of_not_mem ha $ not_mem_erase _ _ lemma min'_lt_of_mem_erase_min' [decidable_eq Ξ±] {a : Ξ±} (ha : a ∈ s.erase (s.min' H)) : s.min' H < a := @lt_max'_of_mem_erase_max' Ξ±α΅’α΅ˆ _ s H _ a ha @[simp] lemma max'_image [linear_order Ξ²] {f : Ξ± β†’ Ξ²} (hf : monotone f) (s : finset Ξ±) (h : (s.image f).nonempty) : (s.image f).max' h = f (s.max' ((nonempty.image_iff f).mp h)) := begin refine le_antisymm (max'_le _ _ _ (Ξ» y hy, _)) (le_max' _ _ (mem_image.mpr ⟨_, max'_mem _ _, rfl⟩)), obtain ⟨x, hx, rfl⟩ := mem_image.mp hy, exact hf (le_max' _ _ hx) end @[simp] lemma min'_image [linear_order Ξ²] {f : Ξ± β†’ Ξ²} (hf : monotone f) (s : finset Ξ±) (h : (s.image f).nonempty) : (s.image f).min' h = f (s.min' ((nonempty.image_iff f).mp h)) := begin convert @max'_image Ξ±α΅’α΅ˆ Ξ²α΅’α΅ˆ _ _ (Ξ» a : Ξ±α΅’α΅ˆ, to_dual (f (of_dual a))) (by simpa) _ _; convert h, rw nonempty.image_iff, end lemma coe_max' {s : finset Ξ±} (hs : s.nonempty) : ↑(s.max' hs) = s.max := coe_sup' hs id lemma coe_min' {s : finset Ξ±} (hs : s.nonempty) : ↑(s.min' hs) = s.min := coe_inf' hs id lemma max'_erase_ne_self {s : finset Ξ±} (s0 : (s.erase x).nonempty) : (s.erase x).max' s0 β‰  x := ne_of_mem_erase (max'_mem _ s0) lemma min'_erase_ne_self {s : finset Ξ±} (s0 : (s.erase x).nonempty) : (s.erase x).min' s0 β‰  x := ne_of_mem_erase (min'_mem _ s0) lemma max_erase_ne_self {s : finset Ξ±} : (s.erase x).max β‰  x := begin by_cases s0 : (s.erase x).nonempty, { refine ne_of_eq_of_ne (coe_max' s0).symm _, exact with_bot.coe_eq_coe.not.mpr (max'_erase_ne_self _) }, { rw [not_nonempty_iff_eq_empty.mp s0, max_empty], exact with_bot.bot_ne_coe } end lemma min_erase_ne_self {s : finset Ξ±} : (s.erase x).min β‰  x := by convert @max_erase_ne_self Ξ±α΅’α΅ˆ _ _ _ /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset Ξ±` provided that: * it is true on the empty `finset`, * for every `s : finset Ξ±` and an element `a` strictly greater than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max [decidable_eq Ξ±] {p : finset Ξ± β†’ Prop} (s : finset Ξ±) (h0 : p βˆ…) (step : βˆ€ a s, (βˆ€ x ∈ s, x < a) β†’ p s β†’ p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases s.eq_empty_or_nonempty with rfl|hne, { exact h0 }, { have H : s.max' hne ∈ s, from max'_mem s hne, rw ← insert_erase H, exact step _ _ (Ξ» x, s.lt_max'_of_mem_erase_max' hne) (ihs _ $ erase_ssubset H) } end /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset Ξ±` provided that: * it is true on the empty `finset`, * for every `s : finset Ξ±` and an element `a` strictly less than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min [decidable_eq Ξ±] {p : finset Ξ± β†’ Prop} (s : finset Ξ±) (h0 : p βˆ…) (step : βˆ€ a s, (βˆ€ x ∈ s, a < x) β†’ p s β†’ p (insert a s)) : p s := @induction_on_max Ξ±α΅’α΅ˆ _ _ _ s h0 step end max_min section max_min_induction_value variables [linear_order Ξ±] [linear_order Ξ²] /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset Ξ±` provided that: * it is true on the empty `finset`, * for every `s : finset Ξ±` and an element `a` such that for elements of `s` denoted by `x` we have `f x ≀ f a`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max_value [decidable_eq ΞΉ] (f : ΞΉ β†’ Ξ±) {p : finset ΞΉ β†’ Prop} (s : finset ΞΉ) (h0 : p βˆ…) (step : βˆ€ a s, a βˆ‰ s β†’ (βˆ€ x ∈ s, f x ≀ f a) β†’ p s β†’ p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases (s.image f).eq_empty_or_nonempty with hne|hne, { simp only [image_eq_empty] at hne, simp only [hne, h0] }, { have H : (s.image f).max' hne ∈ (s.image f), from max'_mem (s.image f) hne, simp only [mem_image, exists_prop] at H, rcases H with ⟨a, has, hfa⟩, rw ← insert_erase has, refine step _ _ (not_mem_erase a s) (Ξ» x hx, _) (ihs _ $ erase_ssubset has), rw hfa, exact le_max' _ _ (mem_image_of_mem _ $ mem_of_mem_erase hx) } end /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset Ξ±` provided that: * it is true on the empty `finset`, * for every `s : finset Ξ±` and an element `a` such that for elements of `s` denoted by `x` we have `f a ≀ f x`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min_value [decidable_eq ΞΉ] (f : ΞΉ β†’ Ξ±) {p : finset ΞΉ β†’ Prop} (s : finset ΞΉ) (h0 : p βˆ…) (step : βˆ€ a s, a βˆ‰ s β†’ (βˆ€ x ∈ s, f a ≀ f x) β†’ p s β†’ p (insert a s)) : p s := @induction_on_max_value Ξ±α΅’α΅ˆ ΞΉ _ _ _ _ s h0 step end max_min_induction_value 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 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' := @exists_max_image Ξ±α΅’α΅ˆ Ξ² _ s f h end exists_max_min end finset namespace multiset lemma map_finset_sup [decidable_eq Ξ±] [decidable_eq Ξ²] (s : finset Ξ³) (f : Ξ³ β†’ multiset Ξ²) (g : Ξ² β†’ Ξ±) (hg : function.injective g) : map g (s.sup f) = s.sup (map g ∘ f) := finset.comp_sup_eq_sup_comp _ (Ξ» _ _, map_union hg) (map_zero _) lemma count_finset_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 lemma mem_sup {Ξ± Ξ²} [decidable_eq Ξ²] {s : finset Ξ±} {f : Ξ± β†’ multiset Ξ²} {x : Ξ²} : x ∈ s.sup f ↔ βˆƒ v ∈ s, x ∈ f v := begin classical, apply s.induction_on, { simp }, { intros a s has hxs, rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union], split, { intro hxi, cases hxi with hf hf, { refine ⟨a, _, hf⟩, simp only [true_or, eq_self_iff_true, finset.mem_insert] }, { rcases hxs.mp hf with ⟨v, hv, hfv⟩, refine ⟨v, _, hfv⟩, simp only [hv, or_true, finset.mem_insert] } }, { rintros ⟨v, hv, hfv⟩, rw [finset.mem_insert] at hv, rcases hv with rfl | hv, { exact or.inl hfv }, { refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } }, end end multiset namespace finset lemma mem_sup {Ξ± Ξ²} [decidable_eq Ξ²] {s : finset Ξ±} {f : Ξ± β†’ finset Ξ²} {x : Ξ²} : x ∈ s.sup f ↔ βˆƒ v ∈ s, x ∈ f v := begin change _ ↔ βˆƒ v ∈ s, x ∈ (f v).val, rw [←multiset.mem_sup, ←multiset.mem_to_finset, sup_to_finset], simp_rw [val_to_finset], end lemma sup_eq_bUnion {Ξ± Ξ²} [decidable_eq Ξ²] (s : finset Ξ±) (t : Ξ± β†’ finset Ξ²) : s.sup t = s.bUnion t := by { ext, rw [mem_sup, mem_bUnion], } @[simp] lemma sup_singleton'' [decidable_eq Ξ±] (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : s.sup (Ξ» b, {f b}) = s.image f := by { ext a, rw [mem_sup, mem_image], simp only [mem_singleton, eq_comm] } @[simp] lemma sup_singleton' [decidable_eq Ξ±] (s : finset Ξ±) : s.sup singleton = s := (s.sup_singleton'' _).trans image_id end finset section lattice variables {ΞΉ' : Sort*} [complete_lattice Ξ±] /-- Supremum of `s i`, `i : ΞΉ`, is equal to the supremum over `t : finset ΞΉ` of suprema `⨆ i ∈ t, s i`. This version assumes `ΞΉ` is a `Type*`. See `supr_eq_supr_finset'` for a version that works for `ΞΉ : Sort*`. -/ lemma supr_eq_supr_finset (s : ΞΉ β†’ Ξ±) : (⨆i, s i) = (⨆t:finset ΞΉ, ⨆i∈t, s i) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le (by simp) $ le_rfl) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end /-- Supremum of `s i`, `i : ΞΉ`, is equal to the supremum over `t : finset ΞΉ` of suprema `⨆ i ∈ t, s i`. This version works for `ΞΉ : Sort*`. See `supr_eq_supr_finset` for a version that assumes `ΞΉ : Type*` but has no `plift`s. -/ lemma supr_eq_supr_finset' (s : ΞΉ' β†’ Ξ±) : (⨆i, s i) = (⨆t:finset (plift ΞΉ'), ⨆i∈t, s (plift.down i)) := by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl /-- Infimum of `s i`, `i : ΞΉ`, is equal to the infimum over `t : finset ΞΉ` of infima `β¨… i ∈ t, s i`. This version assumes `ΞΉ` is a `Type*`. See `infi_eq_infi_finset'` for a version that works for `ΞΉ : Sort*`. -/ lemma infi_eq_infi_finset (s : ΞΉ β†’ Ξ±) : (β¨… i, s i) = β¨… (t : finset ΞΉ) (i ∈ t), s i := @supr_eq_supr_finset Ξ±α΅’α΅ˆ _ _ _ /-- Infimum of `s i`, `i : ΞΉ`, is equal to the infimum over `t : finset ΞΉ` of infima `β¨… i ∈ t, s i`. This version works for `ΞΉ : Sort*`. See `infi_eq_infi_finset` for a version that assumes `ΞΉ : Type*` but has no `plift`s. -/ lemma infi_eq_infi_finset' (s : ΞΉ' β†’ Ξ±) : (β¨…i, s i) = (β¨…t:finset (plift ΞΉ'), β¨…i∈t, s (plift.down i)) := @supr_eq_supr_finset' Ξ±α΅’α΅ˆ _ _ _ end lattice namespace set variables {ΞΉ' : Sort*} /-- Union of an indexed family of sets `s : ΞΉ β†’ set Ξ±` is equal to the union of the unions of finite subfamilies. This version assumes `ΞΉ : Type*`. See also `Union_eq_Union_finset'` for a version that works for `ΞΉ : Sort*`. -/ lemma Union_eq_Union_finset (s : ΞΉ β†’ set Ξ±) : (⋃i, s i) = (⋃t:finset ΞΉ, ⋃i∈t, s i) := supr_eq_supr_finset s /-- Union of an indexed family of sets `s : ΞΉ β†’ set Ξ±` is equal to the union of the unions of finite subfamilies. This version works for `ΞΉ : Sort*`. See also `Union_eq_Union_finset` for a version that assumes `ΞΉ : Type*` but avoids `plift`s in the right hand side. -/ lemma Union_eq_Union_finset' (s : ΞΉ' β†’ set Ξ±) : (⋃i, s i) = (⋃t:finset (plift ΞΉ'), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset' s /-- Intersection of an indexed family of sets `s : ΞΉ β†’ set Ξ±` is equal to the intersection of the intersections of finite subfamilies. This version assumes `ΞΉ : Type*`. See also `Inter_eq_Inter_finset'` for a version that works for `ΞΉ : Sort*`. -/ lemma Inter_eq_Inter_finset (s : ΞΉ β†’ set Ξ±) : (β‹‚i, s i) = (β‹‚t:finset ΞΉ, β‹‚i∈t, s i) := infi_eq_infi_finset s /-- Intersection of an indexed family of sets `s : ΞΉ β†’ set Ξ±` is equal to the intersection of the intersections of finite subfamilies. This version works for `ΞΉ : Sort*`. See also `Inter_eq_Inter_finset` for a version that assumes `ΞΉ : Type*` but avoids `plift`s in the right hand side. -/ 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 /-! ### Interaction with ordered algebra structures -/ lemma sup_mul_le_mul_sup_of_nonneg [linear_ordered_semiring Ξ±] [order_bot Ξ±] {a b : ΞΉ β†’ Ξ±} (s : finset ΞΉ) (ha : βˆ€ i ∈ s, 0 ≀ a i) (hb : βˆ€ i ∈ s, 0 ≀ b i) : s.sup (a * b) ≀ s.sup a * s.sup b := finset.sup_le $ Ξ» i hi, mul_le_mul (le_sup hi) (le_sup hi) (hb _ hi) ((ha _ hi).trans $ le_sup hi) lemma mul_inf_le_inf_mul_of_nonneg [linear_ordered_semiring Ξ±] [order_top Ξ±] {a b : ΞΉ β†’ Ξ±} (s : finset ΞΉ) (ha : βˆ€ i ∈ s, 0 ≀ a i) (hb : βˆ€ i ∈ s, 0 ≀ b i) : s.inf a * s.inf b ≀ s.inf (a * b) := finset.le_inf $ Ξ» i hi, mul_le_mul (inf_le hi) (inf_le hi) (finset.le_inf hb) (ha i hi) lemma sup'_mul_le_mul_sup'_of_nonneg [linear_ordered_semiring Ξ±] {a b : ΞΉ β†’ Ξ±} (s : finset ΞΉ) (H : s.nonempty) (ha : βˆ€ i ∈ s, 0 ≀ a i) (hb : βˆ€ i ∈ s, 0 ≀ b i) : s.sup' H (a * b) ≀ s.sup' H a * s.sup' H b := sup'_le _ _ $ Ξ» i hi, mul_le_mul (le_sup' _ hi) (le_sup' _ hi) (hb _ hi) ((ha _ hi).trans $ le_sup' _ hi) lemma inf'_mul_le_mul_inf'_of_nonneg [linear_ordered_semiring Ξ±] {a b : ΞΉ β†’ Ξ±} (s : finset ΞΉ) (H : s.nonempty) (ha : βˆ€ i ∈ s, 0 ≀ a i) (hb : βˆ€ i ∈ s, 0 ≀ b i) : s.inf' H a * s.inf' H b ≀ s.inf' H (a * b) := le_inf' _ _ $ Ξ» i hi, mul_le_mul (inf'_le _ hi) (inf'_le _ hi) (le_inf' _ _ hb) (ha _ hi) open function /-! ### Interaction with big lattice/set operations -/ section lattice 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 variables [complete_lattice Ξ²] theorem supr_singleton (a : Ξ±) (s : Ξ± β†’ Ξ²) : (⨆ x ∈ ({a} : finset Ξ±), s x) = s a := by simp theorem infi_singleton (a : Ξ±) (s : Ξ± β†’ Ξ²) : (β¨… x ∈ ({a} : finset Ξ±), s x) = s a := by simp lemma supr_option_to_finset (o : option Ξ±) (f : Ξ± β†’ Ξ²) : (⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x := by simp lemma infi_option_to_finset (o : option Ξ±) (f : Ξ± β†’ Ξ²) : (β¨… x ∈ o.to_finset, f x) = β¨… x ∈ o, f x := @supr_option_to_finset _ Ξ²α΅’α΅ˆ _ _ _ variables [decidable_eq Ξ±] theorem supr_union {f : Ξ± β†’ Ξ²} {s t : finset Ξ±} : (⨆ x ∈ s βˆͺ t, f x) = (⨆x∈s, f x) βŠ” (⨆x∈t, f x) := by simp [supr_or, supr_sup_eq] theorem infi_union {f : Ξ± β†’ Ξ²} {s t : finset Ξ±} : (β¨… x ∈ s βˆͺ t, f x) = (β¨… x ∈ s, f x) βŠ“ (β¨… x ∈ t, f x) := @supr_union Ξ± Ξ²α΅’α΅ˆ _ _ _ _ _ lemma supr_insert (a : Ξ±) (s : finset Ξ±) (t : Ξ± β†’ Ξ²) : (⨆ x ∈ insert a s, t x) = t a βŠ” (⨆ x ∈ s, t x) := by { rw insert_eq, simp only [supr_union, finset.supr_singleton] } lemma infi_insert (a : Ξ±) (s : finset Ξ±) (t : Ξ± β†’ Ξ²) : (β¨… x ∈ insert a s, t x) = t a βŠ“ (β¨… x ∈ s, t x) := @supr_insert Ξ± Ξ²α΅’α΅ˆ _ _ _ _ _ lemma supr_finset_image {f : Ξ³ β†’ Ξ±} {g : Ξ± β†’ Ξ²} {s : finset Ξ³} : (⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) := by rw [← supr_coe, coe_image, supr_image, supr_coe] lemma sup_finset_image {Ξ² Ξ³ : Type*} [semilattice_sup Ξ²] [order_bot Ξ²] (f : Ξ³ β†’ Ξ±) (g : Ξ± β†’ Ξ²) (s : finset Ξ³) : (s.image f).sup g = s.sup (g ∘ f) := begin classical, induction s using finset.induction_on with a s' ha ih; simp * end lemma infi_finset_image {f : Ξ³ β†’ Ξ±} {g : Ξ± β†’ Ξ²} {s : finset Ξ³} : (β¨… x ∈ s.image f, g x) = (β¨… y ∈ s, g (f y)) := by rw [← infi_coe, coe_image, infi_image, infi_coe] lemma supr_insert_update {x : Ξ±} {t : finset Ξ±} (f : Ξ± β†’ Ξ²) {s : Ξ²} (hx : x βˆ‰ t) : (⨆ (i ∈ insert x t), function.update f x s i) = (s βŠ” ⨆ (i ∈ t), f i) := begin simp only [finset.supr_insert, update_same], rcongr i hi, apply update_noteq, rintro rfl, exact hx hi end lemma infi_insert_update {x : Ξ±} {t : finset Ξ±} (f : Ξ± β†’ Ξ²) {s : Ξ²} (hx : x βˆ‰ t) : (β¨… (i ∈ insert x t), update f x s i) = (s βŠ“ β¨… (i ∈ t), f i) := @supr_insert_update Ξ± Ξ²α΅’α΅ˆ _ _ _ _ f _ hx lemma supr_bUnion (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ±) (f : Ξ± β†’ Ξ²) : (⨆ y ∈ s.bUnion t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y := by simp [@supr_comm _ Ξ±, supr_and] lemma infi_bUnion (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ±) (f : Ξ± β†’ Ξ²) : (β¨… y ∈ s.bUnion t, f y) = β¨… (x ∈ s) (y ∈ t x), f y := @supr_bUnion _ Ξ²α΅’α΅ˆ _ _ _ _ _ _ end lattice theorem set_bUnion_coe (s : finset Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ (↑s : set Ξ±), t x) = ⋃ x ∈ s, t x := rfl theorem set_bInter_coe (s : finset Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ (↑s : set Ξ±), t x) = β‹‚ x ∈ s, t x := rfl theorem set_bUnion_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ ({a} : finset Ξ±), s x) = s a := supr_singleton a s theorem set_bInter_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ ({a} : finset Ξ±), s x) = s a := infi_singleton a s @[simp] lemma set_bUnion_preimage_singleton (f : Ξ± β†’ Ξ²) (s : finset Ξ²) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := set.bUnion_preimage_singleton f s lemma set_bUnion_option_to_finset (o : option Ξ±) (f : Ξ± β†’ set Ξ²) : (⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x := supr_option_to_finset o f lemma set_bInter_option_to_finset (o : option Ξ±) (f : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ o.to_finset, f x) = β‹‚ x ∈ o, f x := infi_option_to_finset o f lemma subset_set_bUnion_of_mem {s : finset Ξ±} {f : Ξ± β†’ set Ξ²} {x : Ξ±} (h : x ∈ s) : f x βŠ† ⋃ (y ∈ s), f y := show f x ≀ (⨆ y ∈ s, f y), from le_supr_of_le x $ le_supr _ h variables [decidable_eq Ξ±] lemma set_bUnion_union (s t : finset Ξ±) (u : Ξ± β†’ set Ξ²) : (⋃ x ∈ s βˆͺ t, u x) = (⋃ x ∈ s, u x) βˆͺ (⋃ x ∈ t, u x) := supr_union lemma set_bInter_inter (s t : finset Ξ±) (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ s βˆͺ t, u x) = (β‹‚ x ∈ s, u x) ∩ (β‹‚ x ∈ t, u x) := infi_union lemma set_bUnion_insert (a : Ξ±) (s : finset Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ insert a s, t x) = t a βˆͺ (⋃ x ∈ s, t x) := supr_insert a s t lemma set_bInter_insert (a : Ξ±) (s : finset Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ insert a s, t x) = t a ∩ (β‹‚ x ∈ s, t x) := infi_insert a s t lemma set_bUnion_finset_image {f : Ξ³ β†’ Ξ±} {g : Ξ± β†’ set Ξ²} {s : finset Ξ³} : (⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) := supr_finset_image lemma set_bInter_finset_image {f : Ξ³ β†’ Ξ±} {g : Ξ± β†’ set Ξ²} {s : finset Ξ³} : (β‹‚ x ∈ s.image f, g x) = (β‹‚ y ∈ s, g (f y)) := infi_finset_image lemma set_bUnion_insert_update {x : Ξ±} {t : finset Ξ±} (f : Ξ± β†’ set Ξ²) {s : set Ξ²} (hx : x βˆ‰ t) : (⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s βˆͺ ⋃ (i ∈ t), f i) := supr_insert_update f hx lemma set_bInter_insert_update {x : Ξ±} {t : finset Ξ±} (f : Ξ± β†’ set Ξ²) {s : set Ξ²} (hx : x βˆ‰ t) : (β‹‚ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ β‹‚ (i ∈ t), f i) := infi_insert_update f hx lemma set_bUnion_bUnion (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ±) (f : Ξ± β†’ set Ξ²) : (⋃ y ∈ s.bUnion t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y := supr_bUnion s t f lemma set_bInter_bUnion (s : finset Ξ³) (t : Ξ³ β†’ finset Ξ±) (f : Ξ± β†’ set Ξ²) : (β‹‚ y ∈ s.bUnion t, f y) = β‹‚ (x ∈ s) (y ∈ t x), f y := infi_bUnion s t f end finset
ed12aaefca2400b4264263150401a4ea869de9e8
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Meta/WHNF.lean
5cb4a335819db8da3d4a60523d3e9e46b74ab5a4
[ "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
36,599
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ToExpr import Lean.AuxRecursor import Lean.ProjFns import Lean.Structure import Lean.Util.Recognizers import Lean.Meta.Basic import Lean.Meta.GetConst import Lean.Meta.FunInfo import Lean.Meta.Match.MatcherInfo import Lean.Meta.Match.MatchPatternAttr namespace Lean.Meta -- =========================== /-! # Smart unfolding support -/ -- =========================== /-- Forward declaration. It is defined in the module `src/Lean/Elab/PreDefinition/Structural/Eqns.lean`. It is possible to avoid this hack if we move `Structural.EqnInfo` and `Structural.eqnInfoExt` to this module. -/ @[extern "lean_get_structural_rec_arg_pos"] opaque getStructuralRecArgPos? (declName : Name) : CoreM (Option Nat) def smartUnfoldingSuffix := "_sunfold" @[inline] def mkSmartUnfoldingNameFor (declName : Name) : Name := Name.mkStr declName smartUnfoldingSuffix def hasSmartUnfoldingDecl (env : Environment) (declName : Name) : Bool := env.contains (mkSmartUnfoldingNameFor declName) register_builtin_option smartUnfolding : Bool := { defValue := true descr := "when computing weak head normal form, use auxiliary definition created for functions defined by structural recursion" } /-- Add auxiliary annotation to indicate the `match`-expression `e` must be reduced when performing smart unfolding. -/ def markSmartUnfoldingMatch (e : Expr) : Expr := mkAnnotation `sunfoldMatch e def smartUnfoldingMatch? (e : Expr) : Option Expr := annotation? `sunfoldMatch e /-- Add auxiliary annotation to indicate expression `e` (a `match` alternative rhs) was successfully reduced by smart unfolding. -/ def markSmartUnfoldingMatchAlt (e : Expr) : Expr := mkAnnotation `sunfoldMatchAlt e def smartUnfoldingMatchAlt? (e : Expr) : Option Expr := annotation? `sunfoldMatchAlt e -- =========================== /-! # Helper methods -/ -- =========================== def isAuxDef (constName : Name) : MetaM Bool := do let env ← getEnv return isAuxRecursor env constName || isNoConfusion env constName @[inline] private def matchConstAux {Ξ±} (e : Expr) (failK : Unit β†’ MetaM Ξ±) (k : ConstantInfo β†’ List Level β†’ MetaM Ξ±) : MetaM Ξ± := match e with | Expr.const name lvls => do let (some cinfo) ← getConst? name | failK () k cinfo lvls | _ => failK () -- =========================== /-! # Helper functions for reducing recursors -/ -- =========================== private def getFirstCtor (d : Name) : MetaM (Option Name) := do let some (ConstantInfo.inductInfo { ctors := ctor::_, ..}) ← getConstNoEx? d | pure none return some ctor private def mkNullaryCtor (type : Expr) (nparams : Nat) : MetaM (Option Expr) := do match type.getAppFn with | Expr.const d lvls => let (some ctor) ← getFirstCtor d | pure none return mkAppN (mkConst ctor lvls) (type.getAppArgs.shrink nparams) | _ => return none private def getRecRuleFor (recVal : RecursorVal) (major : Expr) : Option RecursorRule := match major.getAppFn with | Expr.const fn _ => recVal.rules.find? fun r => r.ctor == fn | _ => none private def toCtorWhenK (recVal : RecursorVal) (major : Expr) : MetaM Expr := do let majorType ← inferType major let majorType ← instantiateMVars (← whnf majorType) let majorTypeI := majorType.getAppFn if !majorTypeI.isConstOf recVal.getInduct then return major else if majorType.hasExprMVar && majorType.getAppArgs[recVal.numParams:].any Expr.hasExprMVar then return major else do let (some newCtorApp) ← mkNullaryCtor majorType recVal.numParams | pure major let newType ← inferType newCtorApp /- TODO: check whether changing reducibility to default hurts performance here. We do that to make sure auxiliary `Eq.rec` introduced by the `match`-compiler are reduced even when `TransparencyMode.reducible` (like in `simp`). We use `withNewMCtxDepth` to make sure metavariables at `majorType` are not assigned. For example, given `major : Eq ?x y`, we don't want to apply K by assigning `?x := y`. -/ if (← withAtLeastTransparency TransparencyMode.default <| withNewMCtxDepth <| isDefEq majorType newType) then return newCtorApp else return major /-- Create the `i`th projection `major`. It tries to use the auto-generated projection functions if available. Otherwise falls back to `Expr.proj`. -/ def mkProjFn (ctorVal : ConstructorVal) (us : List Level) (params : Array Expr) (i : Nat) (major : Expr) : CoreM Expr := do match getStructureInfo? (← getEnv) ctorVal.induct with | none => return mkProj ctorVal.induct i major | some info => match info.getProjFn? i with | none => return mkProj ctorVal.induct i major | some projFn => return mkApp (mkAppN (mkConst projFn us) params) major /-- If `major` is not a constructor application, and its type is a structure `C ...`, then return `C.mk major.1 ... major.n` \pre `inductName` is `C`. If `Meta.Config.etaStruct` is `false` or the condition above does not hold, this method just returns `major`. -/ private def toCtorWhenStructure (inductName : Name) (major : Expr) : MetaM Expr := do unless (← useEtaStruct inductName) do return major let env ← getEnv if !isStructureLike env inductName then return major else if let some _ := major.isConstructorApp? env then return major else let majorType ← inferType major let majorType ← instantiateMVars (← whnf majorType) let majorTypeI := majorType.getAppFn if !majorTypeI.isConstOf inductName then return major match majorType.getAppFn with | Expr.const d us => if (← whnfD (← inferType majorType)) == mkSort levelZero then return major -- We do not perform eta for propositions, see implementation in the kernel else let some ctorName ← getFirstCtor d | pure major let ctorInfo ← getConstInfoCtor ctorName let params := majorType.getAppArgs.shrink ctorInfo.numParams let mut result := mkAppN (mkConst ctorName us) params for i in [:ctorInfo.numFields] do result := mkApp result (← mkProjFn ctorInfo us params i major) return result | _ => return major /-- Auxiliary function for reducing recursor applications. -/ private def reduceRec (recVal : RecursorVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) : MetaM Ξ± := let majorIdx := recVal.getMajorIdx if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩ let mut major ← whnf major if recVal.k then major ← toCtorWhenK recVal major major := major.toCtorIfLit major ← toCtorWhenStructure recVal.getInduct major match getRecRuleFor recVal major with | some rule => let majorArgs := major.getAppArgs if recLvls.length != recVal.levelParams.length then failK () else let rhs := rule.rhs.instantiateLevelParams recVal.levelParams recLvls -- Apply parameters, motives and minor premises from recursor application. let rhs := mkAppRange rhs 0 (recVal.numParams+recVal.numMotives+recVal.numMinors) recArgs /- The number of parameters in the constructor is not necessarily equal to the number of parameters in the recursor when we have nested inductive types. -/ let nparams := majorArgs.size - rule.nfields let rhs := mkAppRange rhs nparams majorArgs.size majorArgs let rhs := mkAppRange rhs (majorIdx + 1) recArgs.size recArgs successK rhs | none => failK () else failK () -- =========================== /-! # Helper functions for reducing Quot.lift and Quot.ind -/ -- =========================== /-- Auxiliary function for reducing `Quot.lift` and `Quot.ind` applications. -/ private def reduceQuotRec (recVal : QuotVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) : MetaM Ξ± := let process (majorPos argPos : Nat) : MetaM Ξ± := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩ let major ← whnf major match major with | Expr.app (Expr.app (Expr.app (Expr.const majorFn _) _) _) majorArg => do let some (ConstantInfo.quotInfo { kind := QuotKind.ctor, .. }) ← getConstNoEx? majorFn | failK () let f := recArgs[argPos]! let r := mkApp f majorArg let recArity := majorPos + 1 successK <| mkAppRange r recArity recArgs.size recArgs | _ => failK () else failK () match recVal.kind with | QuotKind.lift => process 5 3 | QuotKind.ind => process 4 3 | _ => failK () -- =========================== /-! # Helper function for extracting "stuck term" -/ -- =========================== mutual private partial def isRecStuck? (recVal : RecursorVal) (recArgs : Array Expr) : MetaM (Option MVarId) := if recVal.k then -- TODO: improve this case return none else do let majorIdx := recVal.getMajorIdx if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩ let major ← whnf major getStuckMVar? major else return none private partial def isQuotRecStuck? (recVal : QuotVal) (recArgs : Array Expr) : MetaM (Option MVarId) := let process? (majorPos : Nat) : MetaM (Option MVarId) := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩ let major ← whnf major getStuckMVar? major else return none match recVal.kind with | QuotKind.lift => process? 5 | QuotKind.ind => process? 4 | _ => return none /-- Return `some (Expr.mvar mvarId)` if metavariable `mvarId` is blocking reduction. -/ partial def getStuckMVar? (e : Expr) : MetaM (Option MVarId) := do match e with | .mdata _ e => getStuckMVar? e | .proj _ _ e => getStuckMVar? (← whnf e) | .mvar .. => let e ← instantiateMVars e match e with | .mvar mvarId => return some mvarId | _ => getStuckMVar? e | .app f .. => let f := f.getAppFn match f with | .mvar .. => let e ← instantiateMVars e match e.getAppFn with | .mvar mvarId => return some mvarId | _ => getStuckMVar? e | .const fName _ => match (← getConstNoEx? fName) with | some <| .recInfo recVal => isRecStuck? recVal e.getAppArgs | some <| .quotInfo recVal => isQuotRecStuck? recVal e.getAppArgs | _ => unless e.hasExprMVar do return none -- Projection function support let some projInfo ← getProjectionFnInfo? fName | return none -- This branch is relevant if `e` is a type class projection that is stuck because the instance has not been synthesized yet. unless projInfo.fromClass do return none let args := e.getAppArgs -- First check whether `e`s instance is stuck. if let some major := args.get? projInfo.numParams then if let some mvarId ← getStuckMVar? major then return mvarId /- Then, recurse on the explicit arguments We want to detect the stuck instance in terms such as `HAdd.hAdd Nat Nat Nat (instHAdd Nat instAddNat) n (OfNat.ofNat Nat 2 ?m)` See issue https://github.com/leanprover/lean4/issues/1408 for an example where this is needed. -/ let info ← getFunInfo f for pinfo in info.paramInfo, arg in args do if pinfo.isExplicit then if let some mvarId ← getStuckMVar? arg then return some mvarId return none | .proj _ _ e => getStuckMVar? (← whnf e) | _ => return none | _ => return none end -- =========================== /-! # Weak Head Normal Form auxiliary combinators -/ -- =========================== /-- Auxiliary combinator for handling easy WHNF cases. It takes a function for handling the "hard" cases as an argument -/ @[specialize] partial def whnfEasyCases (e : Expr) (k : Expr β†’ MetaM Expr) : MetaM Expr := do match e with | .forallE .. => return e | .lam .. => return e | .sort .. => return e | .lit .. => return e | .bvar .. => unreachable! | .letE .. => k e | .const .. => k e | .app .. => k e | .proj .. => k e | .mdata _ e => whnfEasyCases e k | .fvar fvarId => let decl ← fvarId.getDecl match decl with | .cdecl .. => return e | .ldecl (value := v) (nonDep := nonDep) .. => let cfg ← getConfig if nonDep && !cfg.zetaNonDep then return e else if cfg.trackZeta then modify fun s => { s with zetaFVarIds := s.zetaFVarIds.insert fvarId } whnfEasyCases v k | .mvar mvarId => match (← getExprMVarAssignment? mvarId) with | some v => whnfEasyCases v k | none => return e @[specialize] private def deltaDefinition (c : ConstantInfo) (lvls : List Level) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) : MetaM Ξ± := do if c.levelParams.length != lvls.length then failK () else successK (← instantiateValueLevelParams c lvls) @[specialize] private def deltaBetaDefinition (c : ConstantInfo) (lvls : List Level) (revArgs : Array Expr) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) (preserveMData := false) : MetaM Ξ± := do if c.levelParams.length != lvls.length then failK () else let val ← instantiateValueLevelParams c lvls let val := val.betaRev revArgs (preserveMData := preserveMData) successK val inductive ReduceMatcherResult where | reduced (val : Expr) | stuck (val : Expr) | notMatcher | partialApp /-- The "match" compiler uses `if-then-else` expressions and other auxiliary declarations to compile match-expressions such as ``` match v with | 'a' => 1 | 'b' => 2 | _ => 3 ``` because it is more efficient than using `casesOn` recursors. The method `reduceMatcher?` fails if these auxiliary definitions (e.g., `ite`) cannot be unfolded in the current transparency setting. This is problematic because tactics such as `simp` use `TransparencyMode.reducible`, and most users assume that expressions such as ``` match 0 with | 0 => 1 | 100 => 2 | _ => 3 ``` should reduce in any transparency mode. Thus, we define a custom `canUnfoldAtMatcher` predicate for `whnfMatcher`. This solution is not very modular because modications at the `match` compiler require changes here. We claim this is defensible because it is reducing the auxiliary declaration defined by the `match` compiler. Alternative solution: tactics that use `TransparencyMode.reducible` should rely on the equations we generated for match-expressions. This solution is also not perfect because the match-expression above will not reduce during type checking when we are not using `TransparencyMode.default` or `TransparencyMode.all`. -/ def canUnfoldAtMatcher (cfg : Config) (info : ConstantInfo) : CoreM Bool := do match cfg.transparency with | TransparencyMode.all => return true | TransparencyMode.default => return true | _ => if (← isReducible info.name) || isGlobalInstance (← getEnv) info.name then return true else if hasMatchPatternAttribute (← getEnv) info.name then return true else return info.name == ``ite || info.name == ``dite || info.name == ``decEq || info.name == ``Nat.decEq || info.name == ``Char.ofNat || info.name == ``Char.ofNatAux || info.name == ``String.decEq || info.name == ``List.hasDecEq || info.name == ``Fin.ofNat || info.name == ``UInt8.ofNat || info.name == ``UInt8.decEq || info.name == ``UInt16.ofNat || info.name == ``UInt16.decEq || info.name == ``UInt32.ofNat || info.name == ``UInt32.decEq || info.name == ``UInt64.ofNat || info.name == ``UInt64.decEq /- Remark: we need to unfold the following two definitions because they are used for `Fin`, and lazy unfolding at `isDefEq` does not unfold projections. -/ || info.name == ``HMod.hMod || info.name == ``Mod.mod private def whnfMatcher (e : Expr) : MetaM Expr := do /- When reducing `match` expressions, if the reducibility setting is at `TransparencyMode.reducible`, we increase it to `TransparencyMode.instance`. We use the `TransparencyMode.reducible` in many places (e.g., `simp`), and this setting prevents us from reducing `match` expressions where the discriminants are terms such as `OfNat.ofNat Ξ± n inst`. For example, `simp [Int.div]` will not unfold the application `Int.div 2 1` occuring in the target. TODO: consider other solutions; investigate whether the solution above produces counterintuitive behavior. -/ let mut transparency ← getTransparency if transparency == TransparencyMode.reducible then transparency := TransparencyMode.instances withTransparency transparency <| withReader (fun ctx => { ctx with canUnfold? := canUnfoldAtMatcher }) do whnf e def reduceMatcher? (e : Expr) : MetaM ReduceMatcherResult := do match e.getAppFn with | Expr.const declName declLevels => let some info ← getMatcherInfo? declName | return ReduceMatcherResult.notMatcher let args := e.getAppArgs let prefixSz := info.numParams + 1 + info.numDiscrs if args.size < prefixSz + info.numAlts then return ReduceMatcherResult.partialApp else let constInfo ← getConstInfo declName let f ← instantiateValueLevelParams constInfo declLevels let auxApp := mkAppN f args[0:prefixSz] let auxAppType ← inferType auxApp forallBoundedTelescope auxAppType info.numAlts fun hs _ => do let auxApp ← whnfMatcher (mkAppN auxApp hs) let auxAppFn := auxApp.getAppFn let mut i := prefixSz for h in hs do if auxAppFn == h then let result := mkAppN args[i]! auxApp.getAppArgs let result := mkAppN result args[prefixSz + info.numAlts:args.size] return ReduceMatcherResult.reduced result.headBeta i := i + 1 return ReduceMatcherResult.stuck auxApp | _ => pure ReduceMatcherResult.notMatcher private def projectCore? (e : Expr) (i : Nat) : MetaM (Option Expr) := do let e := e.toCtorIfLit matchConstCtor e.getAppFn (fun _ => pure none) fun ctorVal _ => let numArgs := e.getAppNumArgs let idx := ctorVal.numParams + i if idx < numArgs then return some (e.getArg! idx) else return none def project? (e : Expr) (i : Nat) : MetaM (Option Expr) := do projectCore? (← whnf e) i /-- Reduce kernel projection `Expr.proj ..` expression. -/ def reduceProj? (e : Expr) : MetaM (Option Expr) := do match e with | Expr.proj _ i c => project? c i | _ => return none /-- Auxiliary method for reducing terms of the form `?m t_1 ... t_n` where `?m` is delayed assigned. Recall that we can only expand a delayed assignment when all holes/metavariables in the assigned value have been "filled". -/ private def whnfDelayedAssigned? (f' : Expr) (e : Expr) : MetaM (Option Expr) := do if f'.isMVar then match (← getDelayedMVarAssignment? f'.mvarId!) with | none => return none | some { fvars, mvarIdPending } => let args := e.getAppArgs if fvars.size > args.size then -- Insufficient number of argument to expand delayed assignment return none else let newVal ← instantiateMVars (mkMVar mvarIdPending) if newVal.hasExprMVar then -- Delayed assignment still contains metavariables return none else let newVal := newVal.abstract fvars let result := newVal.instantiateRevRange 0 fvars.size args return mkAppRange result fvars.size args.size args else return none /-- Apply beta-reduction, zeta-reduction (i.e., unfold let local-decls), iota-reduction, expand let-expressions, expand assigned meta-variables. The parameter `deltaAtProj` controls how to reduce projections `s.i`. If `deltaAtProj == true`, then delta reduction is used to reduce `s` (i.e., `whnf` is used), otherwise `whnfCore`. We only set this flag to `false` when implementing `isDefEq`. -/ partial def whnfCore (e : Expr) (deltaAtProj : Bool := true) : MetaM Expr := go e where go (e : Expr) : MetaM Expr := whnfEasyCases e fun e => do trace[Meta.whnf] e match e with | Expr.const .. => pure e | Expr.letE _ _ v b _ => go <| b.instantiate1 v | Expr.app f .. => let f := f.getAppFn let f' ← go f if f'.isLambda then let revArgs := e.getAppRevArgs go <| f'.betaRev revArgs else if let some eNew ← whnfDelayedAssigned? f' e then go eNew else let e := if f == f' then e else e.updateFn f' match (← reduceMatcher? e) with | ReduceMatcherResult.reduced eNew => go eNew | ReduceMatcherResult.partialApp => pure e | ReduceMatcherResult.stuck _ => pure e | ReduceMatcherResult.notMatcher => matchConstAux f' (fun _ => return e) fun cinfo lvls => match cinfo with | ConstantInfo.recInfo rec => reduceRec rec lvls e.getAppArgs (fun _ => return e) go | ConstantInfo.quotInfo rec => reduceQuotRec rec lvls e.getAppArgs (fun _ => return e) go | c@(ConstantInfo.defnInfo _) => do if (← isAuxDef c.name) then deltaBetaDefinition c lvls e.getAppRevArgs (fun _ => return e) go else return e | _ => return e | Expr.proj _ i c => let c ← if deltaAtProj then whnf c else whnfCore c match (← projectCore? c i) with | some e => go e | none => return e | _ => unreachable! /-- Recall that `_sunfold` auxiliary definitions contains the markers: `markSmartUnfoldingMatch` (*) and `markSmartUnfoldingMatchAlt` (**). For example, consider the following definition ``` def r (i j : Nat) : Nat := i + match j with | Nat.zero => 1 | Nat.succ j => i + match j with | Nat.zero => 2 | Nat.succ j => r i j ``` produces the following `_sunfold` auxiliary definition with the markers ``` def r._sunfold (i j : Nat) : Nat := i + (*) match j with | Nat.zero => (**) 1 | Nat.succ j => i + (*) match j with | Nat.zero => (**) 2 | Nat.succ j => (**) r i j ``` `match` expressions marked with `markSmartUnfoldingMatch` (*) must be reduced, otherwise the resulting term is not definitionally equal to the given expression. The recursion may be interrupted as soon as the annotation `markSmartUnfoldingAlt` (**) is reached. For example, the term `r i j.succ.succ` reduces to the definitionally equal term `i + i * r i j` -/ partial def smartUnfoldingReduce? (e : Expr) : MetaM (Option Expr) := go e |>.run where go (e : Expr) : OptionT MetaM Expr := do match e with | Expr.letE n t v b _ => withLetDecl n t (← go v) fun x => do mkLetFVars #[x] (← go (b.instantiate1 x)) | Expr.lam .. => lambdaTelescope e fun xs b => do mkLambdaFVars xs (← go b) | Expr.app f a .. => return mkApp (← go f) (← go a) | Expr.proj _ _ s => return e.updateProj! (← go s) | Expr.mdata _ b => if let some m := smartUnfoldingMatch? e then goMatch m else return e.updateMData! (← go b) | _ => return e goMatch (e : Expr) : OptionT MetaM Expr := do match (← reduceMatcher? e) with | ReduceMatcherResult.reduced e => if let some alt := smartUnfoldingMatchAlt? e then return alt else go e | ReduceMatcherResult.stuck e' => let mvarId ← getStuckMVar? e' /- Try to "unstuck" by resolving pending TC problems -/ if (← Meta.synthPending mvarId) then goMatch e else failure | _ => failure mutual /-- Auxiliary method for unfolding a class projection. -/ partial def unfoldProjInst? (e : Expr) : MetaM (Option Expr) := do match e.getAppFn with | Expr.const declName .. => match (← getProjectionFnInfo? declName) with | some { fromClass := true, .. } => match (← withDefault <| unfoldDefinition? e) with | none => return none | some e => match (← withReducibleAndInstances <| reduceProj? e.getAppFn) with | none => return none | some r => return mkAppN r e.getAppArgs |>.headBeta | _ => return none | _ => return none /-- Auxiliary method for unfolding a class projection. when transparency is set to `TransparencyMode.instances`. Recall that class instance projections are not marked with `[reducible]` because we want them to be in "reducible canonical form". -/ partial def unfoldProjInstWhenIntances? (e : Expr) : MetaM (Option Expr) := do if (← getTransparency) != TransparencyMode.instances then return none else unfoldProjInst? e /-- Unfold definition using "smart unfolding" if possible. -/ partial def unfoldDefinition? (e : Expr) : MetaM (Option Expr) := match e with | Expr.app f _ => matchConstAux f.getAppFn (fun _ => unfoldProjInstWhenIntances? e) fun fInfo fLvls => do if fInfo.levelParams.length != fLvls.length then return none else let unfoldDefault (_ : Unit) : MetaM (Option Expr) := if fInfo.hasValue then deltaBetaDefinition fInfo fLvls e.getAppRevArgs (fun _ => pure none) (fun e => pure (some e)) else return none if smartUnfolding.get (← getOptions) then match ((← getEnv).find? (mkSmartUnfoldingNameFor fInfo.name)) with | some fAuxInfo@(ConstantInfo.defnInfo _) => -- We use `preserveMData := true` to make sure the smart unfolding annotation are not erased in an over-application. deltaBetaDefinition fAuxInfo fLvls e.getAppRevArgs (preserveMData := true) (fun _ => pure none) fun e₁ => do let some r ← smartUnfoldingReduce? e₁ | return none /- If `smartUnfoldingReduce?` succeeds, we should still check whether the argument the structural recursion is recursing on reduces to a constructor. This extra check is necessary in definitions (see issue #1081) such as ``` inductive Vector (Ξ± : Type u) : Nat β†’ Type u where | nil : Vector Ξ± 0 | cons : Ξ± β†’ Vector Ξ± n β†’ Vector Ξ± (n+1) def Vector.insert (a: Ξ±) (i : Fin (n+1)) (xs : Vector Ξ± n) : Vector Ξ± (n+1) := match i, xs with | ⟨0, _⟩, xs => cons a xs | ⟨i+1, h⟩, cons x xs => cons x (xs.insert a ⟨i, Nat.lt_of_succ_lt_succ h⟩) ``` The structural recursion is being performed using the vector `xs`. That is, we used `Vector.brecOn` to define `Vector.insert`. Thus, an application `xs.insert a ⟨0, h⟩` is **not** definitionally equal to `Vector.cons a xs` because `xs` is not a constructor application (the `Vector.brecOn` application is blocked). Remark 1: performing structural recursion on `Fin (n+1)` is not an option here because it is a `Subtype` and and the repacking in recursive applications confuses the structural recursion module. Remark 2: the match expression reduces reduces to `cons a xs` when the discriminants are `⟨0, h⟩` and `xs`. Remark 3: this check is unnecessary in most cases, but we don't need dependent elimination to trigger the issue fixed by this extra check. Here is another example that triggers the issue fixed by this check. ``` def f : Nat β†’ Nat β†’ Nat | 0, y => y | x+1, y+1 => f (x-2) y | x+1, 0 => 0 theorem ex : f 0 y = y := rfl ``` Remark 4: the `return some r` in the following `let` is not a typo. Binport generated .olean files do not store the position of recursive arguments for definitions using structural recursion. Thus, we should keep `return some r` until Mathlib has been ported to Lean 3. Note that the `Vector` example above does not even work in Lean 3. -/ let some recArgPos ← getStructuralRecArgPos? fInfo.name | return some r let numArgs := e.getAppNumArgs if recArgPos >= numArgs then return none let recArg := e.getArg! recArgPos numArgs if !(← whnfMatcher recArg).isConstructorApp (← getEnv) then return none return some r | _ => if (← getMatcherInfo? fInfo.name).isSome then -- Recall that `whnfCore` tries to reduce "matcher" applications. return none else unfoldDefault () else unfoldDefault () | Expr.const declName lvls => do if smartUnfolding.get (← getOptions) && (← getEnv).contains (mkSmartUnfoldingNameFor declName) then return none else let (some (cinfo@(ConstantInfo.defnInfo _))) ← getConstNoEx? declName | pure none deltaDefinition cinfo lvls (fun _ => pure none) (fun e => pure (some e)) | _ => return none end def unfoldDefinition (e : Expr) : MetaM Expr := do let some e ← unfoldDefinition? e | throwError "failed to unfold definition{indentExpr e}" return e @[specialize] partial def whnfHeadPred (e : Expr) (pred : Expr β†’ MetaM Bool) : MetaM Expr := whnfEasyCases e fun e => do let e ← whnfCore e if (← pred e) then match (← unfoldDefinition? e) with | some e => whnfHeadPred e pred | none => return e else return e def whnfUntil (e : Expr) (declName : Name) : MetaM (Option Expr) := do let e ← whnfHeadPred e (fun e => return !e.isAppOf declName) if e.isAppOf declName then return e else return none /-- Try to reduce matcher/recursor/quot applications. We say they are all "morally" recursor applications. -/ def reduceRecMatcher? (e : Expr) : MetaM (Option Expr) := do if !e.isApp then return none else match (← reduceMatcher? e) with | ReduceMatcherResult.reduced e => return e | _ => matchConstAux e.getAppFn (fun _ => pure none) fun cinfo lvls => do match cinfo with | ConstantInfo.recInfo Β«recΒ» => reduceRec Β«recΒ» lvls e.getAppArgs (fun _ => pure none) (fun e => pure (some e)) | ConstantInfo.quotInfo Β«recΒ» => reduceQuotRec Β«recΒ» lvls e.getAppArgs (fun _ => pure none) (fun e => pure (some e)) | c@(ConstantInfo.defnInfo _) => if (← isAuxDef c.name) then deltaBetaDefinition c lvls e.getAppRevArgs (fun _ => pure none) (fun e => pure (some e)) else return none | _ => return none unsafe def reduceBoolNativeUnsafe (constName : Name) : MetaM Bool := evalConstCheck Bool `Bool constName unsafe def reduceNatNativeUnsafe (constName : Name) : MetaM Nat := evalConstCheck Nat `Nat constName @[implementedBy reduceBoolNativeUnsafe] opaque reduceBoolNative (constName : Name) : MetaM Bool @[implementedBy reduceNatNativeUnsafe] opaque reduceNatNative (constName : Name) : MetaM Nat def reduceNative? (e : Expr) : MetaM (Option Expr) := match e with | Expr.app (Expr.const fName _) (Expr.const argName _) => if fName == ``Lean.reduceBool then do return toExpr (← reduceBoolNative argName) else if fName == ``Lean.reduceNat then do return toExpr (← reduceNatNative argName) else return none | _ => return none @[inline] def withNatValue {Ξ±} (a : Expr) (k : Nat β†’ MetaM (Option Ξ±)) : MetaM (Option Ξ±) := do let a ← whnf a match a with | Expr.const `Nat.zero _ => k 0 | Expr.lit (Literal.natVal v) => k v | _ => return none def reduceUnaryNatOp (f : Nat β†’ Nat) (a : Expr) : MetaM (Option Expr) := withNatValue a fun a => return mkRawNatLit <| f a def reduceBinNatOp (f : Nat β†’ Nat β†’ Nat) (a b : Expr) : MetaM (Option Expr) := withNatValue a fun a => withNatValue b fun b => do trace[Meta.isDefEq.whnf.reduceBinOp] "{a} op {b}" return mkRawNatLit <| f a b def reduceBinNatPred (f : Nat β†’ Nat β†’ Bool) (a b : Expr) : MetaM (Option Expr) := do withNatValue a fun a => withNatValue b fun b => return toExpr <| f a b def reduceNat? (e : Expr) : MetaM (Option Expr) := if e.hasFVar || e.hasMVar then return none else match e with | Expr.app (Expr.const fn _) a => if fn == ``Nat.succ then reduceUnaryNatOp Nat.succ a else return none | Expr.app (Expr.app (Expr.const fn _) a1) a2 => if fn == ``Nat.add then reduceBinNatOp Nat.add a1 a2 else if fn == ``Nat.sub then reduceBinNatOp Nat.sub a1 a2 else if fn == ``Nat.mul then reduceBinNatOp Nat.mul a1 a2 else if fn == ``Nat.div then reduceBinNatOp Nat.div a1 a2 else if fn == ``Nat.mod then reduceBinNatOp Nat.mod a1 a2 else if fn == ``Nat.beq then reduceBinNatPred Nat.beq a1 a2 else if fn == ``Nat.ble then reduceBinNatPred Nat.ble a1 a2 else return none | _ => return none @[inline] private def useWHNFCache (e : Expr) : MetaM Bool := do -- We cache only closed terms without expr metavars. -- Potential refinement: cache if `e` is not stuck at a metavariable if e.hasFVar || e.hasExprMVar || (← read).canUnfold?.isSome then return false else match (← getConfig).transparency with | TransparencyMode.default => return true | TransparencyMode.all => return true | _ => return false @[inline] private def cached? (useCache : Bool) (e : Expr) : MetaM (Option Expr) := do if useCache then match (← getConfig).transparency with | TransparencyMode.default => return (← get).cache.whnfDefault.find? e | TransparencyMode.all => return (← get).cache.whnfAll.find? e | _ => unreachable! else return none private def cache (useCache : Bool) (e r : Expr) : MetaM Expr := do if useCache then match (← getConfig).transparency with | TransparencyMode.default => modify fun s => { s with cache.whnfDefault := s.cache.whnfDefault.insert e r } | TransparencyMode.all => modify fun s => { s with cache.whnfAll := s.cache.whnfAll.insert e r } | _ => unreachable! return r @[export lean_whnf] partial def whnfImp (e : Expr) : MetaM Expr := withIncRecDepth <| whnfEasyCases e fun e => do checkMaxHeartbeats "whnf" let useCache ← useWHNFCache e match (← cached? useCache e) with | some e' => pure e' | none => let e' ← whnfCore e match (← reduceNat? e') with | some v => cache useCache e v | none => match (← reduceNative? e') with | some v => cache useCache e v | none => match (← unfoldDefinition? e') with | some e => whnfImp e | none => cache useCache e e' /-- If `e` is a projection function that satisfies `p`, then reduce it -/ def reduceProjOf? (e : Expr) (p : Name β†’ Bool) : MetaM (Option Expr) := do if !e.isApp then pure none else match e.getAppFn with | Expr.const name .. => do let env ← getEnv match env.getProjectionStructureName? name with | some structName => if p structName then Meta.unfoldDefinition? e else pure none | none => pure none | _ => pure none builtin_initialize registerTraceClass `Meta.whnf registerTraceClass `Meta.isDefEq.whnf.reduceBinOp end Lean.Meta
290f7e0cdaed6e1de517cc979d0e01ffd276b626
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/data/matrix/char_p.lean
a68b50a620dafaec545dc5b87407c86a300c18ed
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
580
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 data.matrix.basic import algebra.char_p /-! # Matrices in prime characteristic -/ open matrix variables {n : Type*} [fintype n] {R : Type*} [ring R] instance matrix.char_p [decidable_eq n] [nonempty n] (p : β„•) [char_p R p] : char_p (matrix n n R) p := ⟨begin intro k, rw [← char_p.cast_eq_zero_iff R p k, ← nat.cast_zero, ← (scalar n).map_nat_cast], convert scalar_inj, {simp}, {assumption} end⟩
476bcdd4338d10146daee29cfeb193bc82c3dec8
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/algebra/ordered_group.lean
d6c8c9d6c0da94c0c1291f877d6146aaf3940574
[ "Apache-2.0" ]
permissive
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
1,066
lean
namespace galois section ordered_cancel_comm_monoid universe u variable {Ξ± : Type u} variables [ordered_cancel_comm_monoid Ξ±] {a b c : Ξ±} @[simp] lemma add_le_add_iff_left (a : Ξ±) {b c : Ξ±} : a + b ≀ a + c ↔ b ≀ c := ⟨le_of_add_le_add_left, Ξ» h, add_le_add_left h _⟩ @[simp] lemma add_le_add_iff_right (c : Ξ±) : a + c ≀ b + c ↔ a ≀ b := add_comm c a β–Έ add_comm c b β–Έ add_le_add_iff_left c @[simp] lemma add_lt_add_iff_left (a : Ξ±) {b c : Ξ±} : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, Ξ» h, add_lt_add_left h _⟩ @[simp] lemma add_lt_add_iff_right (c : Ξ±) : a + c < b + c ↔ a < b := add_comm c a β–Έ add_comm c b β–Έ add_lt_add_iff_left c @[simp] lemma le_add_iff_nonneg_right (a : Ξ±) {b : Ξ±} : a ≀ a + b ↔ 0 ≀ b := have a + 0 ≀ a + b ↔ 0 ≀ b, from add_le_add_iff_left a, by rwa add_zero at this @[simp] lemma lt_add_iff_pos_right (a : Ξ±) {b : Ξ±} : a < a + b ↔ 0 < b := have a + 0 < a + b ↔ 0 < b, from add_lt_add_iff_left a, by rwa add_zero at this end ordered_cancel_comm_monoid end galois
38c123af41a1f5116085e4a2434ea210ae78327b
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/interactive/hole4.lean
23b32790f6481d5d4dfd0a95bd07c4892f012629
[ "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
78
lean
def x : nat β†’ nat := {! Ξ» a, _ !} --^ "command": "hole", "action": "Use"
46c8f382cabc33d50343d3f3252313ada3050945
ea4aee6b11f86433e69bb5e50d0259e056d0ae61
/src/tidy/loop_detection.lean
fbdc72c2f18f498dd7a36266d9279655a2d45499
[]
no_license
timjb/lean-tidy
e18feff0b7f0aad08c614fb4d34aaf527bf21e20
e767e259bf76c69edfd4ab8af1b76e6f1ed67f48
refs/heads/master
1,624,861,693,182
1,504,411,006,000
1,504,411,006,000
103,740,824
0
0
null
1,505,553,968,000
1,505,553,968,000
null
UTF-8
Lean
false
false
3,205
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import .tactic_states import .hash_target open tactic universe variables u v structure loop_detection_state := ( allowed_collisions : nat ) ( past_goals : list string ) meta def unreachable {Ξ± : Sort u} : Ξ± := undefined_core "unreachable" meta def detect_looping { Οƒ Ξ± : Type } [ underlying_tactic_state Οƒ ] ( t : interaction_monad (Οƒ Γ— loop_detection_state) Ξ± ) ( allowed_collisions : nat := 0 ) : interaction_monad Οƒ Ξ± := Ξ» s, match (hash_target s) with | result.success hash s' := (t (s, ⟨ allowed_collisions, [ hash ] ⟩ )).map(Ξ» s'', s''.1) | result.exception pos msg s' := unreachable end meta def instrument_for_loop_detection { Οƒ Ξ± : Type } [uts : underlying_tactic_state Οƒ] ( t : interaction_monad Οƒ Ξ± ) : interaction_monad (Οƒ Γ— loop_detection_state) Ξ± := Ξ» (s : Οƒ Γ— loop_detection_state), match t s.1 with | result.success a s' := match (hash_target (@underlying_tactic_state.to_tactic_state _ uts s')) with -- FIXME why doesn't the coercion work? why can't Lean find uts itself? | result.success hash s'' := if s.2.past_goals.mem hash then if s.2.allowed_collisions > 0 then result.success a (s', ⟨ s.2.allowed_collisions - 1, s.2.past_goals ⟩ ) else match (@tactic.fail Ξ± string _ ("detected looping" /-++ "\n" ++ s.2.past_goals.to_string ++ "\n" ++ hash-/)) s'' with -- FIXME this duplicates code above | result.success a s''' := unreachable | result.exception pos msg s''' := result.exception pos msg (s', s.2) end else result.success a (s', ⟨ s.2.allowed_collisions, hash :: s.2.past_goals ⟩ ) | _ := unreachable end | result.exception msg pos s' := result.exception msg pos (s', s.2) end meta instance lift_to_loop_detection_tactic : tactic_lift loop_detection_state := ⟨ Ξ» { Οƒ Ξ± : Type } [uts : underlying_tactic_state Οƒ], @instrument_for_loop_detection Οƒ Ξ± uts ⟩ meta def interactive_simp := `[simp] lemma looping_test_1 (a : empty): 1 = 1 := begin success_if_fail { detect_looping $ skip }, success_if_fail { detect_looping $ skip >> skip }, refl end lemma looping_test_2 (a : empty): 1 = 1 := begin detect_looping $ interactive_simp end
0955069b6bbffb45b2129cac895062dbd39bfc23
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/normed_space/multilinear.lean
4524c1722d310e44626380910a9fa92751edacc9
[ "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
73,395
lean
/- Copyright (c) 2020 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import analysis.normed_space.operator_norm import topology.algebra.multilinear /-! # Operator norm on the space of continuous multilinear maps When `f` is a continuous multilinear map in finitely many variables, we define its norm `βˆ₯fβˆ₯` as the smallest number such that `βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯` for all `m`. We show that it is indeed a norm, and prove its basic properties. ## Main results Let `f` be a multilinear map in finitely many variables. * `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0` with `βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯` for all `m`. * `continuous_of_bound`, conversely, asserts that this bound implies continuity. * `mk_continuous` constructs the associated continuous multilinear map. Let `f` be a continuous multilinear map in finitely many variables. * `βˆ₯fβˆ₯` is its norm, i.e., the smallest number such that `βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯` for all `m`. * `le_op_norm f m` asserts the fundamental inequality `βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯`. * `norm_image_sub_le f m₁ mβ‚‚` gives a control of the difference `f m₁ - f mβ‚‚` in terms of `βˆ₯fβˆ₯` and `βˆ₯m₁ - mβ‚‚βˆ₯`. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a continuous multilinear function `f` in `n+1` variables into a continuous linear function taking values in continuous multilinear functions in `n` variables, and also into a continuous multilinear function in `n` variables taking values in continuous linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). They induce continuous linear equivalences between spaces of continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n` variables taking values in continuous linear functions), called respectively `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. ## Implementation notes We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity that we should deal with multilinear maps in several variables. The currying/uncurrying constructions are based on those in `multilinear.lean`. From the mathematical point of view, all the results follow from the results on operator norm in one variable, by applying them to one variable after the other through currying. However, this is only well defined when there is an order on the variables (for instance on `fin n`) although the final result is independent of the order. While everything could be done following this approach, it turns out that direct proofs are easier and more efficient. -/ noncomputable theory open_locale classical big_operators nnreal open finset metric local attribute [instance, priority 1001] add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_module -- hack to speed up simp when dealing with complicated types local attribute [-instance] unique.subsingleton pi.subsingleton /-! ### Type variables We use the following type variables in this file: * `π•œ` : a `nondiscrete_normed_field`; * `ΞΉ`, `ΞΉ'` : finite index types with decidable equality; * `E`, `E₁` : families of normed vector spaces over `π•œ` indexed by `i : ΞΉ`; * `E'` : a family of normed vector spaces over `π•œ` indexed by `i' : ΞΉ'`; * `Ei` : a family of normed vector spaces over `π•œ` indexed by `i : fin (nat.succ n)`; * `G`, `G'` : normed vector spaces over `π•œ`. -/ universes u v v' wE wE₁ wE' wEi wG wG' variables {π•œ : Type u} {ΞΉ : Type v} {ΞΉ' : Type v'} {n : β„•} {E : ΞΉ β†’ Type wE} {E₁ : ΞΉ β†’ Type wE₁} {E' : ΞΉ' β†’ Type wE'} {Ei : fin n.succ β†’ Type wEi} {G : Type wG} {G' : Type wG'} [decidable_eq ΞΉ] [fintype ΞΉ] [decidable_eq ΞΉ'] [fintype ΞΉ'] [nondiscrete_normed_field π•œ] [Ξ  i, normed_group (E i)] [Ξ  i, normed_space π•œ (E i)] [Ξ  i, normed_group (E₁ i)] [Ξ  i, normed_space π•œ (E₁ i)] [Ξ  i, normed_group (E' i)] [Ξ  i, normed_space π•œ (E' i)] [Ξ  i, normed_group (Ei i)] [Ξ  i, normed_space π•œ (Ei i)] [normed_group G] [normed_space π•œ G] [normed_group G'] [normed_space π•œ G'] /-! ### Continuity properties of multilinear maps We relate continuity of multilinear maps to the inequality `βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯`, in both directions. Along the way, we prove useful bounds on the difference `βˆ₯f m₁ - f mβ‚‚βˆ₯`. -/ namespace multilinear_map variable (f : multilinear_map π•œ E G) /-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality `βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯` on a shell `Ξ΅ i / βˆ₯c iβˆ₯ < βˆ₯m iβˆ₯ < Ξ΅ i` for some positive numbers `Ξ΅ i` and elements `c i : π•œ`, `1 < βˆ₯c iβˆ₯`, then it satisfies this inequality for all `m`. -/ lemma bound_of_shell {Ξ΅ : ΞΉ β†’ ℝ} {C : ℝ} (hΞ΅ : βˆ€ i, 0 < Ξ΅ i) {c : ΞΉ β†’ π•œ} (hc : βˆ€ i, 1 < βˆ₯c iβˆ₯) (hf : βˆ€ m : Ξ  i, E i, (βˆ€ i, Ξ΅ i / βˆ₯c iβˆ₯ ≀ βˆ₯m iβˆ₯) β†’ (βˆ€ i, βˆ₯m iβˆ₯ < Ξ΅ i) β†’ βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) (m : Ξ  i, E i) : βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯ := begin rcases em (βˆƒ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm], { simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] }, choose Ξ΄ hΞ΄0 hΞ΄m_lt hle_Ξ΄m hΞ΄inv using Ξ» i, rescale_to_shell (hc i) (hΞ΅ i) (hm i), have hΞ΄0 : 0 < ∏ i, βˆ₯Ξ΄ iβˆ₯, from prod_pos (Ξ» i _, norm_pos_iff.2 (hΞ΄0 i)), simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hΞ΄0] using hf (Ξ» i, Ξ΄ i β€’ m i) hle_Ξ΄m hΞ΄m_lt, end /-- If a multilinear map in finitely many variables on normed spaces is continuous, then it satisfies the inequality `βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯`, for some `C` which can be chosen to be positive. -/ theorem exists_bound_of_continuous (hf : continuous f) : βˆƒ (C : ℝ), 0 < C ∧ (βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) := begin by_cases hΞΉ : nonempty ΞΉ, swap, { refine ⟨βˆ₯f 0βˆ₯ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, Ξ» m, _⟩, obtain rfl : m = 0, from funext (Ξ» i, (hΞΉ ⟨i⟩).elim), simp [univ_eq_empty.2 hΞΉ, zero_le_one] }, resetI, obtain ⟨Ρ : ℝ, Ξ΅0 : 0 < Ξ΅, hΞ΅ : βˆ€ m : Ξ  i, E i, βˆ₯m - 0βˆ₯ < Ξ΅ β†’ βˆ₯f m - f 0βˆ₯ < 1⟩ := normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one, simp only [sub_zero, f.map_zero] at hΞ΅, rcases normed_field.exists_one_lt_norm π•œ with ⟨c, hc⟩, have : 0 < (βˆ₯cβˆ₯ / Ξ΅) ^ fintype.card ΞΉ, from pow_pos (div_pos (zero_lt_one.trans hc) Ξ΅0) _, refine ⟨_, this, _⟩, refine f.bound_of_shell (Ξ» _, Ξ΅0) (Ξ» _, hc) (Ξ» m hcm hm, _), refine (hΞ΅ m ((pi_norm_lt_iff Ξ΅0).2 hm)).le.trans _, rw [← div_le_iff' this, one_div, ← inv_pow', inv_div, fintype.card, ← prod_const], exact prod_le_prod (Ξ» _ _, div_nonneg Ξ΅0.le (norm_nonneg _)) (Ξ» i _, hcm i) end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f mβ‚‚` using the multilinearity. Here, we give a precise but hard to use version. See `norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads `βˆ₯f m - f m'βˆ₯ ≀ C * βˆ₯m 1 - m' 1βˆ₯ * max βˆ₯m 2βˆ₯ βˆ₯m' 2βˆ₯ * max βˆ₯m 3βˆ₯ βˆ₯m' 3βˆ₯ * ... * max βˆ₯m nβˆ₯ βˆ₯m' nβˆ₯ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`. -/ lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≀ C) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) (m₁ mβ‚‚ : Ξ i, E i) : βˆ₯f m₁ - f mβ‚‚βˆ₯ ≀ C * βˆ‘ i, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯ := begin have A : βˆ€(s : finset ΞΉ), βˆ₯f m₁ - f (s.piecewise mβ‚‚ m₁)βˆ₯ ≀ C * βˆ‘ i in s, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯, { refine finset.induction (by simp) _, assume i s his Hrec, have I : βˆ₯f (s.piecewise mβ‚‚ m₁) - f ((insert i s).piecewise mβ‚‚ m₁)βˆ₯ ≀ C * ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯, { have A : ((insert i s).piecewise mβ‚‚ m₁) = function.update (s.piecewise mβ‚‚ m₁) i (mβ‚‚ i) := s.piecewise_insert _ _ _, have B : s.piecewise mβ‚‚ m₁ = function.update (s.piecewise mβ‚‚ m₁) i (m₁ i), { ext j, by_cases h : j = i, { rw h, simp [his] }, { simp [h] } }, rw [B, A, ← f.map_sub], apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC), refine prod_le_prod (Ξ»j hj, norm_nonneg _) (Ξ»j hj, _), by_cases h : j = i, { rw h, simp }, { by_cases h' : j ∈ s; simp [h', h, le_refl] } }, calc βˆ₯f m₁ - f ((insert i s).piecewise mβ‚‚ m₁)βˆ₯ ≀ βˆ₯f m₁ - f (s.piecewise mβ‚‚ m₁)βˆ₯ + βˆ₯f (s.piecewise mβ‚‚ m₁) - f ((insert i s).piecewise mβ‚‚ m₁)βˆ₯ : by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ } ... ≀ (C * βˆ‘ i in s, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯) + C * ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯ : add_le_add Hrec I ... = C * βˆ‘ i in insert i s, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯ : by simp [his, add_comm, left_distrib] }, convert A univ, simp end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f mβ‚‚` using the multilinearity. Here, we give a usable but not very precise version. See `norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is `βˆ₯f m - f m'βˆ₯ ≀ C * card ΞΉ * βˆ₯m - m'βˆ₯ * (max βˆ₯mβˆ₯ βˆ₯m'βˆ₯) ^ (card ΞΉ - 1)`. -/ lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≀ C) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) (m₁ mβ‚‚ : Ξ i, E i) : βˆ₯f m₁ - f mβ‚‚βˆ₯ ≀ C * (fintype.card ΞΉ) * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1) * βˆ₯m₁ - mβ‚‚βˆ₯ := begin have A : βˆ€ (i : ΞΉ), ∏ j, (if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯) ≀ βˆ₯m₁ - mβ‚‚βˆ₯ * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1), { assume i, calc ∏ j, (if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯) ≀ ∏ j : ΞΉ, function.update (Ξ» j, max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) i (βˆ₯m₁ - mβ‚‚βˆ₯) j : begin apply prod_le_prod, { assume j hj, by_cases h : j = i; simp [h, norm_nonneg] }, { assume j hj, by_cases h : j = i, { rw h, simp, exact norm_le_pi_norm (m₁ - mβ‚‚) i }, { simp [h, max_le_max, norm_le_pi_norm] } } end ... = βˆ₯m₁ - mβ‚‚βˆ₯ * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1) : by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } }, calc βˆ₯f m₁ - f mβ‚‚βˆ₯ ≀ C * βˆ‘ i, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯ : f.norm_image_sub_le_of_bound' hC H m₁ mβ‚‚ ... ≀ C * βˆ‘ i, βˆ₯m₁ - mβ‚‚βˆ₯ * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1) : mul_le_mul_of_nonneg_left (sum_le_sum (Ξ»i hi, A i)) hC ... = C * (fintype.card ΞΉ) * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1) * βˆ₯m₁ - mβ‚‚βˆ₯ : by { rw [sum_const, card_univ, nsmul_eq_mul], ring } end /-- If a multilinear map satisfies an inequality `βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯`, then it is continuous. -/ theorem continuous_of_bound (C : ℝ) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) : continuous f := begin let D := max C 1, have D_pos : 0 ≀ D := le_trans zero_le_one (le_max_right _ _), replace H : βˆ€ m, βˆ₯f mβˆ₯ ≀ D * ∏ i, βˆ₯m iβˆ₯, { assume m, apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _), exact prod_nonneg (Ξ»(i : ΞΉ) hi, norm_nonneg (m i)) }, refine continuous_iff_continuous_at.2 (Ξ»m, _), refine continuous_at_of_locally_lipschitz zero_lt_one (D * (fintype.card ΞΉ) * (βˆ₯mβˆ₯ + 1) ^ (fintype.card ΞΉ - 1)) (Ξ»m' h', _), rw [dist_eq_norm, dist_eq_norm], have : 0 ≀ (max βˆ₯m'βˆ₯ βˆ₯mβˆ₯), by simp, have : (max βˆ₯m'βˆ₯ βˆ₯mβˆ₯) ≀ βˆ₯mβˆ₯ + 1, by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm], calc βˆ₯f m' - f mβˆ₯ ≀ D * (fintype.card ΞΉ) * (max βˆ₯m'βˆ₯ βˆ₯mβˆ₯) ^ (fintype.card ΞΉ - 1) * βˆ₯m' - mβˆ₯ : f.norm_image_sub_le_of_bound D_pos H m' m ... ≀ D * (fintype.card ΞΉ) * (βˆ₯mβˆ₯ + 1) ^ (fintype.card ΞΉ - 1) * βˆ₯m' - mβˆ₯ : by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left] end /-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness condition. -/ def mk_continuous (C : ℝ) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) : continuous_multilinear_map π•œ E G := { cont := f.continuous_of_bound C H, ..f } @[simp] lemma coe_mk_continuous (C : ℝ) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) : ⇑(f.mk_continuous C H) = f := rfl /-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on the other coordinates, then the resulting restricted function satisfies an inequality `βˆ₯f.restr vβˆ₯ ≀ C * βˆ₯zβˆ₯^(n-k) * Ξ  βˆ₯v iβˆ₯` if the original function satisfies `βˆ₯f vβˆ₯ ≀ C * Ξ  βˆ₯v iβˆ₯`. -/ lemma restr_norm_le {k n : β„•} (f : (multilinear_map π•œ (Ξ» i : fin n, G) G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ} (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) (v : fin k β†’ G) : βˆ₯f.restr s hk z vβˆ₯ ≀ C * βˆ₯zβˆ₯ ^ (n - k) * ∏ i, βˆ₯v iβˆ₯ := begin rw [mul_right_comm, mul_assoc], convert H _ using 2, simp only [apply_dite norm, fintype.prod_dite, prod_const (βˆ₯zβˆ₯), finset.card_univ, fintype.card_of_subtype sᢜ (Ξ» x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe, ← (s.order_iso_of_fin hk).symm.bijective.prod_comp (Ξ» x, βˆ₯v xβˆ₯)], refl end end multilinear_map /-! ### Continuous multilinear maps We define the norm `βˆ₯fβˆ₯` of a continuous multilinear map `f` in finitely many variables as the smallest number such that `βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯` for all `m`. We show that this defines a normed space structure on `continuous_multilinear_map π•œ E G`. -/ namespace continuous_multilinear_map variables (c : π•œ) (f g : continuous_multilinear_map π•œ E G) (m : Ξ i, E i) theorem bound : βˆƒ (C : ℝ), 0 < C ∧ (βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) := f.to_multilinear_map.exists_bound_of_continuous f.2 open real /-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/ def op_norm := Inf {c | 0 ≀ (c : ℝ) ∧ βˆ€ m, βˆ₯f mβˆ₯ ≀ c * ∏ i, βˆ₯m iβˆ₯} instance has_op_norm : has_norm (continuous_multilinear_map π•œ E G) := ⟨op_norm⟩ lemma norm_def : βˆ₯fβˆ₯ = Inf {c | 0 ≀ (c : ℝ) ∧ βˆ€ m, βˆ₯f mβˆ₯ ≀ c * ∏ i, βˆ₯m iβˆ₯} := rfl -- So that invocations of `real.Inf_le` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : continuous_multilinear_map π•œ E G} : βˆƒ c, c ∈ {c | 0 ≀ c ∧ βˆ€ m, βˆ₯f mβˆ₯ ≀ c * ∏ i, βˆ₯m iβˆ₯} := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : continuous_multilinear_map π•œ E G} : bdd_below {c | 0 ≀ c ∧ βˆ€ m, βˆ₯f mβˆ₯ ≀ c * ∏ i, βˆ₯m iβˆ₯} := ⟨0, Ξ» _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≀ βˆ₯fβˆ₯ := lb_le_Inf _ bounds_nonempty (Ξ» _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm of a continuous multilinear map: `βˆ₯f mβˆ₯` is bounded by `βˆ₯fβˆ₯` times the product of the `βˆ₯m iβˆ₯`. -/ theorem le_op_norm : βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ := begin have A : 0 ≀ ∏ i, βˆ₯m iβˆ₯ := prod_nonneg (Ξ»j hj, norm_nonneg _), cases A.eq_or_lt with h hlt, { rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩, rw norm_eq_zero at hi, have : f m = 0 := f.map_coord_zero i hi, rw [this, norm_zero], exact mul_nonneg (op_norm_nonneg f) A }, { rw [← div_le_iff hlt], apply (le_Inf _ bounds_nonempty bounds_bdd_below).2, rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc } end theorem le_of_op_norm_le {C : ℝ} (h : βˆ₯fβˆ₯ ≀ C) : βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯ := (f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ Ξ» i _, norm_nonneg (m i)) lemma ratio_le_op_norm : βˆ₯f mβˆ₯ / ∏ i, βˆ₯m iβˆ₯ ≀ βˆ₯fβˆ₯ := div_le_of_nonneg_of_le_mul (prod_nonneg $ Ξ» i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m) /-- The image of the unit ball under a continuous multilinear map is bounded. -/ lemma unit_le_op_norm (h : βˆ₯mβˆ₯ ≀ 1) : βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ := calc βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ : f.le_op_norm m ... ≀ βˆ₯fβˆ₯ * ∏ i : ΞΉ, 1 : mul_le_mul_of_nonneg_left (prod_le_prod (Ξ»i hi, norm_nonneg _) (Ξ»i hi, le_trans (norm_le_pi_norm _ _) h)) (op_norm_nonneg f) ... = βˆ₯fβˆ₯ : by simp /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≀ M) (hM : βˆ€ m, βˆ₯f mβˆ₯ ≀ M * ∏ i, βˆ₯m iβˆ₯) : βˆ₯fβˆ₯ ≀ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : βˆ₯f + gβˆ₯ ≀ βˆ₯fβˆ₯ + βˆ₯gβˆ₯ := Inf_le _ bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), Ξ» x, by { rw add_mul, exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- A continuous linear map is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : βˆ₯fβˆ₯ = 0 ↔ f = 0 := begin split, { assume h, ext m, simpa [h] using f.le_op_norm m }, { rintro rfl, apply le_antisymm (op_norm_le_bound 0 le_rfl (Ξ»m, _)) (op_norm_nonneg _), simp } end variables {π•œ' : Type*} [nondiscrete_normed_field π•œ'] [normed_algebra π•œ' π•œ] [normed_space π•œ' G] [is_scalar_tower π•œ' π•œ G] lemma op_norm_smul_le (c : π•œ') : βˆ₯c β€’ fβˆ₯ ≀ βˆ₯cβˆ₯ * βˆ₯fβˆ₯ := (c β€’ f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) begin intro m, erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end lemma op_norm_neg : βˆ₯-fβˆ₯ = βˆ₯fβˆ₯ := by { rw norm_def, apply congr_arg, ext, simp } /-- Continuous multilinear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (continuous_multilinear_map π•œ E G) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ instance to_normed_space : normed_space π•œ' (continuous_multilinear_map π•œ E G) := ⟨λ c f, f.op_norm_smul_le c⟩ theorem le_op_norm_mul_prod_of_le {b : ΞΉ β†’ ℝ} (hm : βˆ€ i, βˆ₯m iβˆ₯ ≀ b i) : βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, b i := (f.le_op_norm m).trans $ mul_le_mul_of_nonneg_left (prod_le_prod (Ξ» _ _, norm_nonneg _) (Ξ» i _, hm i)) (norm_nonneg f) theorem le_op_norm_mul_pow_card_of_le {b : ℝ} (hm : βˆ€ i, βˆ₯m iβˆ₯ ≀ b) : βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * b ^ fintype.card ΞΉ := by simpa only [prod_const] using f.le_op_norm_mul_prod_of_le m hm theorem le_op_norm_mul_pow_of_le {Ei : fin n β†’ Type*} [Ξ  i, normed_group (Ei i)] [Ξ  i, normed_space π•œ (Ei i)] (f : continuous_multilinear_map π•œ Ei G) (m : Ξ  i, Ei i) {b : ℝ} (hm : βˆ₯mβˆ₯ ≀ b) : βˆ₯f mβˆ₯ ≀ βˆ₯fβˆ₯ * b ^ n := by simpa only [fintype.card_fin] using f.le_op_norm_mul_pow_card_of_le m (Ξ» i, (norm_le_pi_norm m i).trans hm) /-- The fundamental property of the operator norm of a continuous multilinear map: `βˆ₯f mβˆ₯` is bounded by `βˆ₯fβˆ₯` times the product of the `βˆ₯m iβˆ₯`, `nnnorm` version. -/ theorem le_op_nnnorm : nnnorm (f m) ≀ nnnorm f * ∏ i, nnnorm (m i) := nnreal.coe_le_coe.1 $ by { push_cast, exact f.le_op_norm m } theorem le_of_op_nnnorm_le {C : ℝβ‰₯0} (h : nnnorm f ≀ C) : nnnorm (f m) ≀ C * ∏ i, nnnorm (m i) := (f.le_op_nnnorm m).trans $ mul_le_mul' h le_rfl lemma op_norm_prod (f : continuous_multilinear_map π•œ E G) (g : continuous_multilinear_map π•œ E G') : βˆ₯f.prod gβˆ₯ = max (βˆ₯fβˆ₯) (βˆ₯gβˆ₯) := le_antisymm (op_norm_le_bound _ (norm_nonneg (f, g)) (Ξ» m, have H : 0 ≀ ∏ i, βˆ₯m iβˆ₯, from prod_nonneg $ Ξ» _ _, norm_nonneg _, by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H] using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $ max_le (f.op_norm_le_bound (norm_nonneg _) $ Ξ» m, (le_max_left _ _).trans ((f.prod g).le_op_norm _)) (g.op_norm_le_bound (norm_nonneg _) $ Ξ» m, (le_max_right _ _).trans ((f.prod g).le_op_norm _)) lemma norm_pi {ΞΉ' : Type v'} [fintype ΞΉ'] {E' : ΞΉ' β†’ Type wE'} [Ξ  i', normed_group (E' i')] [Ξ  i', normed_space π•œ (E' i')] (f : Ξ  i', continuous_multilinear_map π•œ E (E' i')) : βˆ₯pi fβˆ₯ = βˆ₯fβˆ₯ := begin apply le_antisymm, { refine (op_norm_le_bound _ (norm_nonneg f) (Ξ» m, _)), dsimp, rw pi_norm_le_iff, exacts [Ξ» i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i), mul_nonneg (norm_nonneg f) (prod_nonneg $ Ξ» _ _, norm_nonneg _)] }, { refine (pi_norm_le_iff (norm_nonneg _)).2 (Ξ» i, _), refine (op_norm_le_bound _ (norm_nonneg _) (Ξ» m, _)), refine le_trans _ ((pi f).le_op_norm m), convert norm_le_pi_norm (Ξ» j, f j m) i } end section variables (π•œ E E' G G') /-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/ def prodL : (continuous_multilinear_map π•œ E G) Γ— (continuous_multilinear_map π•œ E G') ≃ₗᡒ[π•œ] continuous_multilinear_map π•œ E (G Γ— G') := { to_fun := Ξ» f, f.1.prod f.2, inv_fun := Ξ» f, ((continuous_linear_map.fst π•œ G G').comp_continuous_multilinear_map f, (continuous_linear_map.snd π•œ G G').comp_continuous_multilinear_map f), map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl, left_inv := Ξ» f, by ext; refl, right_inv := Ξ» f, by ext; refl, norm_map' := Ξ» f, op_norm_prod f.1 f.2 } /-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/ def piβ‚—α΅’ {ΞΉ' : Type v'} [fintype ΞΉ'] {E' : ΞΉ' β†’ Type wE'} [Ξ  i', normed_group (E' i')] [Ξ  i', normed_space π•œ (E' i')] : @linear_isometry_equiv π•œ (Ξ  i', continuous_multilinear_map π•œ E (E' i')) (continuous_multilinear_map π•œ E (Ξ  i, E' i)) _ _ _ (@pi.module ΞΉ' _ π•œ _ _ (Ξ» i', infer_instance)) _ := { to_linear_equiv := -- note: `pi_linear_equiv` does not unify correctly here, presumably due to issues with dependent -- typeclass arguments. { map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl, .. pi_equiv, }, norm_map' := norm_pi } end section restrict_scalars variables [Ξ  i, normed_space π•œ' (E i)] [βˆ€ i, is_scalar_tower π•œ' π•œ (E i)] @[simp] lemma norm_restrict_scalars : βˆ₯f.restrict_scalars π•œ'βˆ₯ = βˆ₯fβˆ₯ := by simp only [norm_def, coe_restrict_scalars] variable (π•œ') /-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/ def restrict_scalars_linear : continuous_multilinear_map π•œ E G β†’L[π•œ'] continuous_multilinear_map π•œ' E G := linear_map.mk_continuous { to_fun := restrict_scalars π•œ', map_add' := Ξ» m₁ mβ‚‚, rfl, map_smul' := Ξ» c m, rfl } 1 $ Ξ» f, by simp variable {π•œ'} lemma continuous_restrict_scalars : continuous (restrict_scalars π•œ' : continuous_multilinear_map π•œ E G β†’ continuous_multilinear_map π•œ' E G) := (restrict_scalars_linear π•œ').continuous end restrict_scalars /-- The difference `f m₁ - f mβ‚‚` is controlled in terms of `βˆ₯fβˆ₯` and `βˆ₯m₁ - mβ‚‚βˆ₯`, precise version. For a less precise but more usable version, see `norm_image_sub_le`. The bound reads `βˆ₯f m - f m'βˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯m 1 - m' 1βˆ₯ * max βˆ₯m 2βˆ₯ βˆ₯m' 2βˆ₯ * max βˆ₯m 3βˆ₯ βˆ₯m' 3βˆ₯ * ... * max βˆ₯m nβˆ₯ βˆ₯m' nβˆ₯ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`.-/ lemma norm_image_sub_le' (m₁ mβ‚‚ : Ξ i, E i) : βˆ₯f m₁ - f mβ‚‚βˆ₯ ≀ βˆ₯fβˆ₯ * βˆ‘ i, ∏ j, if j = i then βˆ₯m₁ i - mβ‚‚ iβˆ₯ else max βˆ₯m₁ jβˆ₯ βˆ₯mβ‚‚ jβˆ₯ := f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _ /-- The difference `f m₁ - f mβ‚‚` is controlled in terms of `βˆ₯fβˆ₯` and `βˆ₯m₁ - mβ‚‚βˆ₯`, less precise version. For a more precise but less usable version, see `norm_image_sub_le'`. The bound is `βˆ₯f m - f m'βˆ₯ ≀ βˆ₯fβˆ₯ * card ΞΉ * βˆ₯m - m'βˆ₯ * (max βˆ₯mβˆ₯ βˆ₯m'βˆ₯) ^ (card ΞΉ - 1)`.-/ lemma norm_image_sub_le (m₁ mβ‚‚ : Ξ i, E i) : βˆ₯f m₁ - f mβ‚‚βˆ₯ ≀ βˆ₯fβˆ₯ * (fintype.card ΞΉ) * (max βˆ₯m₁βˆ₯ βˆ₯mβ‚‚βˆ₯) ^ (fintype.card ΞΉ - 1) * βˆ₯m₁ - mβ‚‚βˆ₯ := f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _ /-- Applying a multilinear map to a vector is continuous in both coordinates. -/ lemma continuous_eval : continuous (Ξ» p : continuous_multilinear_map π•œ E G Γ— Ξ  i, E i, p.1 p.2) := begin apply continuous_iff_continuous_at.2 (Ξ»p, _), apply continuous_at_of_locally_lipschitz zero_lt_one ((βˆ₯pβˆ₯ + 1) * (fintype.card ΞΉ) * (βˆ₯pβˆ₯ + 1) ^ (fintype.card ΞΉ - 1) + ∏ i, βˆ₯p.2 iβˆ₯) (Ξ»q hq, _), have : 0 ≀ (max βˆ₯q.2βˆ₯ βˆ₯p.2βˆ₯), by simp, have : 0 ≀ βˆ₯pβˆ₯ + 1, by simp [le_trans zero_le_one], have A : βˆ₯qβˆ₯ ≀ βˆ₯pβˆ₯ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq), have : (max βˆ₯q.2βˆ₯ βˆ₯p.2βˆ₯) ≀ βˆ₯pβˆ₯ + 1 := le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (by simp [A, -add_comm, zero_le_one]), have : βˆ€ (i : ΞΉ), i ∈ univ β†’ 0 ≀ βˆ₯p.2 iβˆ₯ := Ξ» i hi, norm_nonneg _, calc dist (q.1 q.2) (p.1 p.2) ≀ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _ ... = βˆ₯q.1 q.2 - q.1 p.2βˆ₯ + βˆ₯q.1 p.2 - p.1 p.2βˆ₯ : by rw [dist_eq_norm, dist_eq_norm] ... ≀ βˆ₯q.1βˆ₯ * (fintype.card ΞΉ) * (max βˆ₯q.2βˆ₯ βˆ₯p.2βˆ₯) ^ (fintype.card ΞΉ - 1) * βˆ₯q.2 - p.2βˆ₯ + βˆ₯q.1 - p.1βˆ₯ * ∏ i, βˆ₯p.2 iβˆ₯ : add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2) ... ≀ (βˆ₯pβˆ₯ + 1) * (fintype.card ΞΉ) * (βˆ₯pβˆ₯ + 1) ^ (fintype.card ΞΉ - 1) * βˆ₯q - pβˆ₯ + βˆ₯q - pβˆ₯ * ∏ i, βˆ₯p.2 iβˆ₯ : by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg, mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg, norm_fst_le (q - p), prod_nonneg] ... = ((βˆ₯pβˆ₯ + 1) * (fintype.card ΞΉ) * (βˆ₯pβˆ₯ + 1) ^ (fintype.card ΞΉ - 1) + (∏ i, βˆ₯p.2 iβˆ₯)) * dist q p : by { rw dist_eq_norm, ring } end lemma continuous_eval_left (m : Ξ  i, E i) : continuous (Ξ» p : continuous_multilinear_map π•œ E G, p m) := continuous_eval.comp (continuous_id.prod_mk continuous_const) lemma has_sum_eval {Ξ± : Type*} {p : Ξ± β†’ continuous_multilinear_map π•œ E G} {q : continuous_multilinear_map π•œ E G} (h : has_sum p q) (m : Ξ  i, E i) : has_sum (Ξ» a, p a m) (q m) := begin dsimp [has_sum] at h ⊒, convert ((continuous_eval_left m).tendsto _).comp h, ext s, simp end lemma tsum_eval {Ξ± : Type*} {p : Ξ± β†’ continuous_multilinear_map π•œ E G} (hp : summable p) (m : Ξ  i, E i) : (βˆ‘' a, p a) m = βˆ‘' a, p a m := (has_sum_eval hp.has_sum m).tsum_eq.symm open_locale topological_space open filter /-- If the target space is complete, the space of continuous multilinear maps with its norm is also complete. The proof is essentially the same as for the space of continuous linear maps (modulo the addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear case from the multilinear case via a currying isomorphism. However, this would mess up imports, and it is more satisfactory to have the simplest case as a standalone proof. -/ instance [complete_space G] : complete_space (continuous_multilinear_map π•œ E G) := begin have nonneg : βˆ€ (v : Ξ  i, E i), 0 ≀ ∏ i, βˆ₯v iβˆ₯ := Ξ» v, finset.prod_nonneg (Ξ» i hi, norm_nonneg _), -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (Ξ» f hf, _), -- We now expand out the definition of a Cauchy sequence, rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, -- and establish that the evaluation at any point `v : Ξ  i, E i` is Cauchy. have cau : βˆ€ v, cauchy_seq (Ξ» n, f n v), { assume v, apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, βˆ₯v iβˆ₯, Ξ» n, _, _, _⟩, { exact mul_nonneg (b0 n) (nonneg v) }, { assume n m N hn hm, rw dist_eq_norm, apply le_trans ((f n - f m).le_op_norm v) _, exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) }, { simpa using b_lim.mul tendsto_const_nhds } }, -- We assemble the limits points of those Cauchy sequences -- (which exist as `G` is complete) -- into a function which we call `F`. choose F hF using Ξ»v, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `F` is multilinear, let Fmult : multilinear_map π•œ E G := { to_fun := F, map_add' := Ξ» v i x y, begin have A := hF (function.update v i (x + y)), have B := (hF (function.update v i x)).add (hF (function.update v i y)), simp at A B, exact tendsto_nhds_unique A B end, map_smul' := Ξ» v i c x, begin have A := hF (function.update v i (c β€’ x)), have B := filter.tendsto.smul (@tendsto_const_nhds _ β„• _ c _) (hF (function.update v i x)), simp at A B, exact tendsto_nhds_unique A B end }, -- and that `F` has norm at most `(b 0 + βˆ₯f 0βˆ₯)`. have Fnorm : βˆ€ v, βˆ₯F vβˆ₯ ≀ (b 0 + βˆ₯f 0βˆ₯) * ∏ i, βˆ₯v iβˆ₯, { assume v, have A : βˆ€ n, βˆ₯f n vβˆ₯ ≀ (b 0 + βˆ₯f 0βˆ₯) * ∏ i, βˆ₯v iβˆ₯, { assume n, apply le_trans ((f n).le_op_norm _) _, apply mul_le_mul_of_nonneg_right _ (nonneg v), calc βˆ₯f nβˆ₯ = βˆ₯(f n - f 0) + f 0βˆ₯ : by { congr' 1, abel } ... ≀ βˆ₯f n - f 0βˆ₯ + βˆ₯f 0βˆ₯ : norm_add_le _ _ ... ≀ b 0 + βˆ₯f 0βˆ₯ : begin apply add_le_add_right, simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _) end }, exact le_of_tendsto (hF v).norm (eventually_of_forall A) }, -- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence. let Fcont := Fmult.mk_continuous _ Fnorm, use Fcont, -- Our last task is to establish convergence to `F` in norm. have : βˆ€ n, βˆ₯f n - Fcontβˆ₯ ≀ b n, { assume n, apply op_norm_le_bound _ (b0 n) (Ξ» v, _), have A : βˆ€αΆ  m in at_top, βˆ₯(f n - f m) vβˆ₯ ≀ b n * ∏ i, βˆ₯v iβˆ₯, { refine eventually_at_top.2 ⟨n, Ξ» m hm, _⟩, apply le_trans ((f n - f m).le_op_norm _) _, exact mul_le_mul_of_nonneg_right (b_bound n m n (le_refl _) hm) (nonneg v) }, have B : tendsto (Ξ» m, βˆ₯(f n - f m) vβˆ₯) at_top (𝓝 (βˆ₯(f n - Fcont) vβˆ₯)) := tendsto.norm (tendsto_const_nhds.sub (hF v)), exact le_of_tendsto B A }, erw tendsto_iff_norm_tendsto_zero, exact squeeze_zero (Ξ» n, norm_nonneg _) this b_lim, end end continuous_multilinear_map /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map π•œ E G) {C : ℝ} (hC : 0 ≀ C) (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) : βˆ₯f.mk_continuous C Hβˆ₯ ≀ C := continuous_multilinear_map.op_norm_le_bound _ hC (Ξ»m, H m) /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map π•œ E G) {C : ℝ} (H : βˆ€ m, βˆ₯f mβˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯) : βˆ₯f.mk_continuous C Hβˆ₯ ≀ max C 0 := continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $ Ξ» m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _) (prod_nonneg $ Ξ» _ _, norm_nonneg _) namespace continuous_multilinear_map /-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : β„•} (f : (G [Γ—n]β†’L[π•œ] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) : G [Γ—k]β†’L[π•œ] G' := (f.to_multilinear_map.restr s hk z).mk_continuous (βˆ₯fβˆ₯ * βˆ₯zβˆ₯^(n-k)) $ Ξ» v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _ lemma norm_restr {k n : β„•} (f : G [Γ—n]β†’L[π•œ] G') (s : finset (fin n)) (hk : s.card = k) (z : G) : βˆ₯f.restr s hk zβˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯zβˆ₯ ^ (n - k) := begin apply multilinear_map.mk_continuous_norm_le, exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _) end section variables (π•œ ΞΉ) (A : Type*) [normed_comm_ring A] [normed_algebra π•œ A] /-- The continuous multilinear map on `A^ΞΉ`, where `A` is a normed commutative algebra over `π•œ`, associating to `m` the product of all the `m i`. See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/ protected def mk_pi_algebra : continuous_multilinear_map π•œ (Ξ» i : ΞΉ, A) A := @multilinear_map.mk_continuous π•œ ΞΉ (Ξ» i : ΞΉ, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra π•œ ΞΉ A) (if nonempty ΞΉ then 1 else βˆ₯(1 : A)βˆ₯) $ begin intro m, by_cases hΞΉ : nonempty ΞΉ, { resetI, simp [hΞΉ, norm_prod_le' univ univ_nonempty] }, { simp [eq_empty_of_not_nonempty hΞΉ univ, hΞΉ] } end variables {A π•œ ΞΉ} @[simp] lemma mk_pi_algebra_apply (m : ΞΉ β†’ A) : continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ A m = ∏ i, m i := rfl lemma norm_mk_pi_algebra_le [nonempty ΞΉ] : βˆ₯continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ Aβˆ₯ ≀ 1 := calc βˆ₯continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ Aβˆ₯ ≀ if nonempty ΞΉ then 1 else βˆ₯(1 : A)βˆ₯ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = _ : if_pos β€Ή_β€Ί lemma norm_mk_pi_algebra_of_empty [is_empty ΞΉ] : βˆ₯continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ Aβˆ₯ = βˆ₯(1 : A)βˆ₯ := begin apply le_antisymm, calc βˆ₯continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ Aβˆ₯ ≀ if nonempty ΞΉ then 1 else βˆ₯(1 : A)βˆ₯ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = βˆ₯(1 : A)βˆ₯ : if_neg (not_nonempty_iff.mpr β€Ή_β€Ί), convert ratio_le_op_norm _ (Ξ» _, (1 : A)), simp [eq_empty_of_is_empty (univ : finset ΞΉ)], end @[simp] lemma norm_mk_pi_algebra [norm_one_class A] : βˆ₯continuous_multilinear_map.mk_pi_algebra π•œ ΞΉ Aβˆ₯ = 1 := begin casesI is_empty_or_nonempty ΞΉ, { simp [norm_mk_pi_algebra_of_empty] }, { refine le_antisymm norm_mk_pi_algebra_le _, convert ratio_le_op_norm _ (Ξ» _, 1); [skip, apply_instance], simp }, end end section variables (π•œ n) (A : Type*) [normed_ring A] [normed_algebra π•œ A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `π•œ`, associating to `m` the product of all the `m i`. See also: `multilinear_map.mk_pi_algebra`. -/ protected def mk_pi_algebra_fin : continuous_multilinear_map π•œ (Ξ» i : fin n, A) A := @multilinear_map.mk_continuous π•œ (fin n) (Ξ» i : fin n, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra_fin π•œ n A) (nat.cases_on n βˆ₯(1 : A)βˆ₯ (Ξ» _, 1)) $ begin intro m, cases n, { simp }, { have : @list.of_fn A n.succ m β‰  [] := by simp, simpa [← fin.prod_of_fn] using list.norm_prod_le' this } end variables {A π•œ n} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n β†’ A) : continuous_multilinear_map.mk_pi_algebra_fin π•œ n A m = (list.of_fn m).prod := rfl lemma norm_mk_pi_algebra_fin_succ_le : βˆ₯continuous_multilinear_map.mk_pi_algebra_fin π•œ n.succ Aβˆ₯ ≀ 1 := multilinear_map.mk_continuous_norm_le _ zero_le_one _ lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) : βˆ₯continuous_multilinear_map.mk_pi_algebra_fin π•œ n Aβˆ₯ ≀ 1 := by cases n; [exact hn.false.elim, exact norm_mk_pi_algebra_fin_succ_le] lemma norm_mk_pi_algebra_fin_zero : βˆ₯continuous_multilinear_map.mk_pi_algebra_fin π•œ 0 Aβˆ₯ = βˆ₯(1 : A)βˆ₯ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, convert ratio_le_op_norm _ (Ξ» _, 1); [simp, apply_instance] end @[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] : βˆ₯continuous_multilinear_map.mk_pi_algebra_fin π•œ n Aβˆ₯ = 1 := begin cases n, { simp [norm_mk_pi_algebra_fin_zero] }, { refine le_antisymm norm_mk_pi_algebra_fin_succ_le _, convert ratio_le_op_norm _ (Ξ» _, 1); [skip, apply_instance], simp } end end variables (π•œ ΞΉ) /-- The canonical continuous multilinear map on `π•œ^ΞΉ`, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_field (z : G) : continuous_multilinear_map π•œ (Ξ»(i : ΞΉ), π•œ) G := @multilinear_map.mk_continuous π•œ ΞΉ (Ξ»(i : ΞΉ), π•œ) G _ _ _ _ _ _ _ (multilinear_map.mk_pi_ring π•œ ΞΉ z) (βˆ₯zβˆ₯) (Ξ» m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.norm_prod, mul_comm]) variables {π•œ ΞΉ} @[simp] lemma mk_pi_field_apply (z : G) (m : ΞΉ β†’ π•œ) : (continuous_multilinear_map.mk_pi_field π•œ ΞΉ z : (ΞΉ β†’ π•œ) β†’ G) m = (∏ i, m i) β€’ z := rfl lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map π•œ (Ξ»(i : ΞΉ), π•œ) G) : continuous_multilinear_map.mk_pi_field π•œ ΞΉ (f (Ξ»i, 1)) = f := to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self variables (π•œ ΞΉ G) /-- Continuous multilinear maps on `π•œ^n` with values in `G` are in bijection with `G`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `continuous_multilinear_map.pi_field_equiv_aux`. The continuous linear equivalence is `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv_aux : G ≃ₗ[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : ΞΉ), π•œ) G) := { to_fun := Ξ» z, continuous_multilinear_map.mk_pi_field π•œ ΞΉ z, inv_fun := Ξ» f, f (Ξ»i, 1), map_add' := Ξ» z z', by { ext m, simp [smul_add] }, map_smul' := Ξ» c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := Ξ» z, by simp, right_inv := Ξ» f, f.mk_pi_field_apply_one_eq_self } /-- Continuous multilinear maps on `π•œ^n` with values in `G` are in bijection with `G`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a continuous linear equivalence in `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv : G ≃L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : ΞΉ), π•œ) G) := { continuous_to_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux π•œ ΞΉ G).to_linear_map.continuous_of_bound (1 : ℝ) (Ξ»z, _), rw one_mul, change βˆ₯continuous_multilinear_map.mk_pi_field π•œ ΞΉ zβˆ₯ ≀ βˆ₯zβˆ₯, exact multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end, continuous_inv_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux π•œ ΞΉ G).symm.to_linear_map.continuous_of_bound (1 : ℝ) (Ξ»f, _), rw one_mul, change βˆ₯f (Ξ»i, 1)βˆ₯ ≀ βˆ₯fβˆ₯, apply @continuous_multilinear_map.unit_le_op_norm π•œ ΞΉ (Ξ» (i : ΞΉ), π•œ) G _ _ _ _ _ _ _ f, simp [pi_norm_le_iff zero_le_one, le_refl] end, .. continuous_multilinear_map.pi_field_equiv_aux π•œ ΞΉ G } end continuous_multilinear_map namespace continuous_linear_map lemma norm_comp_continuous_multilinear_map_le (g : G β†’L[π•œ] G') (f : continuous_multilinear_map π•œ E G) : βˆ₯g.comp_continuous_multilinear_map fβˆ₯ ≀ βˆ₯gβˆ₯ * βˆ₯fβˆ₯ := continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ Ξ» m, calc βˆ₯g (f m)βˆ₯ ≀ βˆ₯gβˆ₯ * (βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯) : g.le_op_norm_of_le $ f.le_op_norm _ ... = _ : (mul_assoc _ _ _).symm /-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/ def comp_continuous_multilinear_mapL : (G β†’L[π•œ] G') β†’L[π•œ] continuous_multilinear_map π•œ E G β†’L[π•œ] continuous_multilinear_map π•œ E G' := linear_map.mk_continuousβ‚‚ (linear_map.mkβ‚‚ π•œ comp_continuous_multilinear_map (Ξ» f₁ fβ‚‚ g, rfl) (Ξ» c f g, rfl) (Ξ» f g₁ gβ‚‚, by { ext1, apply f.map_add }) (Ξ» c f g, by { ext1, simp })) 1 $ Ξ» f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g } /-- Flip arguments in `f : G β†’L[π•œ] continuous_multilinear_map π•œ E G'` to get `continuous_multilinear_map π•œ E (G β†’L[π•œ] G')` -/ def flip_multilinear (f : G β†’L[π•œ] continuous_multilinear_map π•œ E G') : continuous_multilinear_map π•œ E (G β†’L[π•œ] G') := multilinear_map.mk_continuous { to_fun := Ξ» m, linear_map.mk_continuous { to_fun := Ξ» x, f x m, map_add' := Ξ» x y, by simp only [map_add, continuous_multilinear_map.add_apply], map_smul' := Ξ» c x, by simp only [continuous_multilinear_map.smul_apply, map_smul]} (βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯) $ Ξ» x, by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) }, map_add' := Ξ» m i x y, by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk, linear_map.mk_continuous_apply]}, map_smul' := Ξ» m i c x, by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk, linear_map.mk_continuous_apply, pi.smul_apply]} } βˆ₯fβˆ₯ $ Ξ» m, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg f) (prod_nonneg $ Ξ» i hi, norm_nonneg (m i))) _ end continuous_linear_map open continuous_multilinear_map namespace multilinear_map /-- Given a map `f : G β†’β‚—[π•œ] multilinear_map π•œ E G'` and an estimate `H : βˆ€ x m, βˆ₯f x mβˆ₯ ≀ C * βˆ₯xβˆ₯ * ∏ i, βˆ₯m iβˆ₯`, construct a continuous linear map from `G` to `continuous_multilinear_map π•œ E G'`. In order to lift, e.g., a map `f : (multilinear_map π•œ E G) β†’β‚—[π•œ] multilinear_map π•œ E' G'` to a map `(continuous_multilinear_map π•œ E G) β†’L[π•œ] continuous_multilinear_map π•œ E' G'`, one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear` which is a linear map from `continuous_multilinear_map π•œ E G` to `multilinear_map π•œ E' G'`. -/ def mk_continuous_linear (f : G β†’β‚—[π•œ] multilinear_map π•œ E G') (C : ℝ) (H : βˆ€ x m, βˆ₯f x mβˆ₯ ≀ C * βˆ₯xβˆ₯ * ∏ i, βˆ₯m iβˆ₯) : G β†’L[π•œ] continuous_multilinear_map π•œ E G' := linear_map.mk_continuous { to_fun := Ξ» x, (f x).mk_continuous (C * βˆ₯xβˆ₯) $ H x, map_add' := Ξ» x y, by { ext1, simp }, map_smul' := Ξ» c x, by { ext1, simp } } (max C 0) $ Ξ» x, ((f x).mk_continuous_norm_le' _).trans_eq $ by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul] lemma mk_continuous_linear_norm_le' (f : G β†’β‚—[π•œ] multilinear_map π•œ E G') (C : ℝ) (H : βˆ€ x m, βˆ₯f x mβˆ₯ ≀ C * βˆ₯xβˆ₯ * ∏ i, βˆ₯m iβˆ₯) : βˆ₯mk_continuous_linear f C Hβˆ₯ ≀ max C 0 := begin dunfold mk_continuous_linear, exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_linear_norm_le (f : G β†’β‚—[π•œ] multilinear_map π•œ E G') {C : ℝ} (hC : 0 ≀ C) (H : βˆ€ x m, βˆ₯f x mβˆ₯ ≀ C * βˆ₯xβˆ₯ * ∏ i, βˆ₯m iβˆ₯) : βˆ₯mk_continuous_linear f C Hβˆ₯ ≀ C := (mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC) /-- Given a map `f : multilinear_map π•œ E (multilinear_map π•œ E' G)` and an estimate `H : βˆ€ m m', βˆ₯f m m'βˆ₯ ≀ C * ∏ i, βˆ₯m iβˆ₯ * ∏ i, βˆ₯m' iβˆ₯`, upgrade all `multilinear_map`s in the type to `continuous_multilinear_map`s. -/ def mk_continuous_multilinear (f : multilinear_map π•œ E (multilinear_map π•œ E' G)) (C : ℝ) (H : βˆ€ m₁ mβ‚‚, βˆ₯f m₁ mβ‚‚βˆ₯ ≀ C * (∏ i, βˆ₯m₁ iβˆ₯) * ∏ i, βˆ₯mβ‚‚ iβˆ₯) : continuous_multilinear_map π•œ E (continuous_multilinear_map π•œ E' G) := mk_continuous { to_fun := Ξ» m, mk_continuous (f m) (C * ∏ i, βˆ₯m iβˆ₯) $ H m, map_add' := Ξ» m i x y, by { ext1, simp }, map_smul' := Ξ» m i c x, by { ext1, simp } } (max C 0) $ Ξ» m, ((f m).mk_continuous_norm_le' _).trans_eq $ by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (Ξ» _ _, norm_nonneg _) } @[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map π•œ E (multilinear_map π•œ E' G)) {C : ℝ} (H : βˆ€ m₁ mβ‚‚, βˆ₯f m₁ mβ‚‚βˆ₯ ≀ C * (∏ i, βˆ₯m₁ iβˆ₯) * ∏ i, βˆ₯mβ‚‚ iβˆ₯) (m : Ξ  i, E i) : ⇑(mk_continuous_multilinear f C H m) = f m := rfl lemma mk_continuous_multilinear_norm_le' (f : multilinear_map π•œ E (multilinear_map π•œ E' G)) (C : ℝ) (H : βˆ€ m₁ mβ‚‚, βˆ₯f m₁ mβ‚‚βˆ₯ ≀ C * (∏ i, βˆ₯m₁ iβˆ₯) * ∏ i, βˆ₯mβ‚‚ iβˆ₯) : βˆ₯mk_continuous_multilinear f C Hβˆ₯ ≀ max C 0 := begin dunfold mk_continuous_multilinear, exact mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_multilinear_norm_le (f : multilinear_map π•œ E (multilinear_map π•œ E' G)) {C : ℝ} (hC : 0 ≀ C) (H : βˆ€ m₁ mβ‚‚, βˆ₯f m₁ mβ‚‚βˆ₯ ≀ C * (∏ i, βˆ₯m₁ iβˆ₯) * ∏ i, βˆ₯mβ‚‚ iβˆ₯) : βˆ₯mk_continuous_multilinear f C Hβˆ₯ ≀ C := (mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC) end multilinear_map namespace continuous_multilinear_map lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map π•œ E₁ G) (f : Ξ  i, E i β†’L[π•œ] E₁ i) : βˆ₯g.comp_continuous_linear_map fβˆ₯ ≀ βˆ₯gβˆ₯ * ∏ i, βˆ₯f iβˆ₯ := op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ Ξ» i hi, norm_nonneg _) $ Ξ» m, calc βˆ₯g (Ξ» i, f i (m i))βˆ₯ ≀ βˆ₯gβˆ₯ * ∏ i, βˆ₯f i (m i)βˆ₯ : g.le_op_norm _ ... ≀ βˆ₯gβˆ₯ * ∏ i, (βˆ₯f iβˆ₯ * βˆ₯m iβˆ₯) : mul_le_mul_of_nonneg_left (prod_le_prod (Ξ» _ _, norm_nonneg _) (Ξ» i hi, (f i).le_op_norm (m i))) (norm_nonneg g) ... = (βˆ₯gβˆ₯ * ∏ i, βˆ₯f iβˆ₯) * ∏ i, βˆ₯m iβˆ₯ : by rw [prod_mul_distrib, mul_assoc] /-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map. This implementation fixes `f : Ξ  i, E i β†’L[π•œ] E₁ i`. TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of issues with class instances. -/ def comp_continuous_linear_mapL (f : Ξ  i, E i β†’L[π•œ] E₁ i) : continuous_multilinear_map π•œ E₁ G β†’L[π•œ] continuous_multilinear_map π•œ E G := linear_map.mk_continuous { to_fun := Ξ» g, g.comp_continuous_linear_map f, map_add' := Ξ» g₁ gβ‚‚, rfl, map_smul' := Ξ» c g, rfl } (∏ i, βˆ₯f iβˆ₯) $ Ξ» g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _) @[simp] lemma comp_continuous_linear_mapL_apply (g : continuous_multilinear_map π•œ E₁ G) (f : Ξ  i, E i β†’L[π•œ] E₁ i) : comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f := rfl lemma norm_comp_continuous_linear_mapL_le (f : Ξ  i, E i β†’L[π•œ] E₁ i) : βˆ₯@comp_continuous_linear_mapL π•œ ΞΉ E E₁ G _ _ _ _ _ _ _ _ _ fβˆ₯ ≀ (∏ i, βˆ₯f iβˆ₯) := linear_map.mk_continuous_norm_le _ (prod_nonneg $ Ξ» i _, norm_nonneg _) _ end continuous_multilinear_map section currying /-! ### Currying We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`). The inverse operations are called `uncurry_left` and `uncurry_right`. We also register continuous linear equiv versions of these correspondences, in `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. -/ open fin function lemma continuous_linear_map.norm_map_tail_le (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) (m : Ξ i, Ei i) : βˆ₯f (m 0) (tail m)βˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ := calc βˆ₯f (m 0) (tail m)βˆ₯ ≀ βˆ₯f (m 0)βˆ₯ * ∏ i, βˆ₯(tail m) iβˆ₯ : (f (m 0)).le_op_norm _ ... ≀ (βˆ₯fβˆ₯ * βˆ₯m 0βˆ₯) * ∏ i, βˆ₯(tail m) iβˆ₯ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (Ξ»i hi, norm_nonneg _)) ... = βˆ₯fβˆ₯ * (βˆ₯m 0βˆ₯ * ∏ i, βˆ₯(tail m) iβˆ₯) : by ring ... = βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ : by { rw prod_univ_succ, refl } lemma continuous_multilinear_map.norm_map_init_le (f : continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) (m : Ξ i, Ei i) : βˆ₯f (init m) (m (last n))βˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ := calc βˆ₯f (init m) (m (last n))βˆ₯ ≀ βˆ₯f (init m)βˆ₯ * βˆ₯m (last n)βˆ₯ : (f (init m)).le_op_norm _ ... ≀ (βˆ₯fβˆ₯ * (∏ i, βˆ₯(init m) iβˆ₯)) * βˆ₯m (last n)βˆ₯ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _) ... = βˆ₯fβˆ₯ * ((∏ i, βˆ₯(init m) iβˆ₯) * βˆ₯m (last n)βˆ₯) : mul_assoc _ _ _ ... = βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯ : by { rw prod_univ_cast_succ, refl } lemma continuous_multilinear_map.norm_map_cons_le (f : continuous_multilinear_map π•œ Ei G) (x : Ei 0) (m : Ξ (i : fin n), Ei i.succ) : βˆ₯f (cons x m)βˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯xβˆ₯ * ∏ i, βˆ₯m iβˆ₯ := calc βˆ₯f (cons x m)βˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯cons x m iβˆ₯ : f.le_op_norm _ ... = (βˆ₯fβˆ₯ * βˆ₯xβˆ₯) * ∏ i, βˆ₯m iβˆ₯ : by { rw prod_univ_succ, simp [mul_assoc] } lemma continuous_multilinear_map.norm_map_snoc_le (f : continuous_multilinear_map π•œ Ei G) (m : Ξ (i : fin n), Ei i.cast_succ) (x : Ei (last n)) : βˆ₯f (snoc m x)βˆ₯ ≀ βˆ₯fβˆ₯ * (∏ i, βˆ₯m iβˆ₯) * βˆ₯xβˆ₯ := calc βˆ₯f (snoc m x)βˆ₯ ≀ βˆ₯fβˆ₯ * ∏ i, βˆ₯snoc m x iβˆ₯ : f.le_op_norm _ ... = βˆ₯fβˆ₯ * (∏ i, βˆ₯m iβˆ₯) * βˆ₯xβˆ₯ : by { rw prod_univ_cast_succ, simp [mul_assoc] } /-! #### Left currying -/ /-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def continuous_linear_map.uncurry_left (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) : continuous_multilinear_map π•œ Ei G := (@linear_map.uncurry_left π•œ n Ei G _ _ _ _ _ (continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous (βˆ₯fβˆ₯) (Ξ»m, continuous_linear_map.norm_map_tail_le f m) @[simp] lemma continuous_linear_map.uncurry_left_apply (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) (m : Ξ i, Ei i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain a continuous linear map into continuous multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def continuous_multilinear_map.curry_left (f : continuous_multilinear_map π•œ Ei G) : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G) := linear_map.mk_continuous { -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear -- map to_fun := Ξ»x, (f.to_multilinear_map.curry_left x).mk_continuous (βˆ₯fβˆ₯ * βˆ₯xβˆ₯) (f.norm_map_cons_le x), map_add' := Ξ»x y, by { ext m, exact f.cons_add m x y }, map_smul' := Ξ»c x, by { ext m, exact f.cons_smul m c x } } -- then register its continuity thanks to its boundedness properties. (βˆ₯fβˆ₯) (Ξ»x, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _) @[simp] lemma continuous_multilinear_map.curry_left_apply (f : continuous_multilinear_map π•œ Ei G) (x : Ei 0) (m : Ξ (i : fin n), Ei i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma continuous_linear_map.curry_uncurry_left (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, continuous_linear_map.uncurry_left_apply, continuous_multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma continuous_multilinear_map.uncurry_curry_left (f : continuous_multilinear_map π•œ Ei G) : f.curry_left.uncurry_left = f := continuous_multilinear_map.to_multilinear_map_inj $ f.to_multilinear_map.uncurry_curry_left variables (π•œ Ei G) /-- The space of continuous multilinear maps on `Ξ (i : fin (n+1)), E i` is canonically isomorphic to the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on `Ξ (i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in `continuous_multilinear_curry_left_equiv π•œ E Eβ‚‚`. The algebraic version (without topology) is given in `multilinear_curry_left_equiv π•œ E Eβ‚‚`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_left_equiv : (Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) ≃ₗᡒ[π•œ] (continuous_multilinear_map π•œ Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_linear_map.uncurry_left, map_add' := Ξ»f₁ fβ‚‚, by { ext m, refl }, map_smul' := Ξ»c f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_left, left_inv := continuous_linear_map.curry_uncurry_left, right_inv := continuous_multilinear_map.uncurry_curry_left } (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (Ξ» f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {π•œ Ei G} @[simp] lemma continuous_multilinear_curry_left_equiv_apply (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ» i : fin n, Ei i.succ) G)) (v : Ξ  i, Ei i) : continuous_multilinear_curry_left_equiv π•œ Ei G f v = f (v 0) (tail v) := rfl @[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply (f : continuous_multilinear_map π•œ Ei G) (x : Ei 0) (v : Ξ  i : fin n, Ei i.succ) : (continuous_multilinear_curry_left_equiv π•œ Ei G).symm f x v = f (cons x v) := rfl @[simp] lemma continuous_multilinear_map.curry_left_norm (f : continuous_multilinear_map π•œ Ei G) : βˆ₯f.curry_leftβˆ₯ = βˆ₯fβˆ₯ := (continuous_multilinear_curry_left_equiv π•œ Ei G).symm.norm_map f @[simp] lemma continuous_linear_map.uncurry_left_norm (f : Ei 0 β†’L[π•œ] (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.succ) G)) : βˆ₯f.uncurry_leftβˆ₯ = βˆ₯fβˆ₯ := (continuous_multilinear_curry_left_equiv π•œ Ei G).norm_map f /-! #### Right currying -/ /-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/ def continuous_multilinear_map.uncurry_right (f : continuous_multilinear_map π•œ (Ξ» i : fin n, Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) : continuous_multilinear_map π•œ Ei G := let f' : multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’β‚—[π•œ] G) := { to_fun := Ξ» m, (f m).to_linear_map, map_add' := Ξ» m i x y, by simp, map_smul' := Ξ» m i c x, by simp } in (@multilinear_map.uncurry_right π•œ n Ei G _ _ _ _ _ f').mk_continuous (βˆ₯fβˆ₯) (Ξ»m, f.norm_map_init_le m) @[simp] lemma continuous_multilinear_map.uncurry_right_apply (f : continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) (m : Ξ i, Ei i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain a continuous multilinear map in `n` variables into continuous linear maps, given by `m ↦ (x ↦ f (snoc m x))`. -/ def continuous_multilinear_map.curry_right (f : continuous_multilinear_map π•œ Ei G) : continuous_multilinear_map π•œ (Ξ» i : fin n, Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G) := let f' : multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G) := { to_fun := Ξ»m, (f.to_multilinear_map.curry_right m).mk_continuous (βˆ₯fβˆ₯ * ∏ i, βˆ₯m iβˆ₯) $ Ξ»x, f.norm_map_snoc_le m x, map_add' := Ξ» m i x y, by { simp, refl }, map_smul' := Ξ» m i c x, by { simp, refl } } in f'.mk_continuous (βˆ₯fβˆ₯) (Ξ»m, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (prod_nonneg (Ξ»j hj, norm_nonneg _))) _) @[simp] lemma continuous_multilinear_map.curry_right_apply (f : continuous_multilinear_map π•œ Ei G) (m : Ξ  i : fin n, Ei i.cast_succ) (x : Ei (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma continuous_multilinear_map.curry_uncurry_right (f : continuous_multilinear_map π•œ (Ξ» i : fin n, Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, continuous_multilinear_map.curry_right_apply, continuous_multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma continuous_multilinear_map.uncurry_curry_right (f : continuous_multilinear_map π•œ Ei G) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (π•œ Ei G) /-- The space of continuous multilinear maps on `Ξ (i : fin (n+1)), Ei i` is canonically isomorphic to the space of continuous multilinear maps on `Ξ (i : fin n), Ei i.cast_succ` with values in the space of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv π•œ Ei G`. The algebraic version (without topology) is given in `multilinear_curry_right_equiv π•œ Ei G`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv : (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) ≃ₗᡒ[π•œ] (continuous_multilinear_map π•œ Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_multilinear_map.uncurry_right, map_add' := Ξ»f₁ fβ‚‚, by { ext m, refl }, map_smul' := Ξ»c f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_right, left_inv := continuous_multilinear_map.curry_uncurry_right, right_inv := continuous_multilinear_map.uncurry_curry_right } (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables (n G') /-- The space of continuous multilinear maps on `Ξ (i : fin (n+1)), G` is canonically isomorphic to the space of continuous multilinear maps on `Ξ (i : fin n), G` with values in the space of continuous linear maps on `G`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' π•œ n G G'`. For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there are no dependent types, use the primed version as it helps Lean a lot for unification. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv' : (G [Γ—n]β†’L[π•œ] (G β†’L[π•œ] G')) ≃ₗᡒ[π•œ] (G [Γ—n.succ]β†’L[π•œ] G') := continuous_multilinear_curry_right_equiv π•œ (Ξ» (i : fin n.succ), G) G' variables {n π•œ G Ei G'} @[simp] lemma continuous_multilinear_curry_right_equiv_apply (f : (continuous_multilinear_map π•œ (Ξ»(i : fin n), Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G))) (v : Ξ  i, Ei i) : (continuous_multilinear_curry_right_equiv π•œ Ei G) f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply (f : continuous_multilinear_map π•œ Ei G) (v : Ξ  (i : fin n), Ei i.cast_succ) (x : Ei (last n)) : (continuous_multilinear_curry_right_equiv π•œ Ei G).symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_apply' (f : G [Γ—n]β†’L[π•œ] (G β†’L[π•œ] G')) (v : Ξ  (i : fin n.succ), G) : continuous_multilinear_curry_right_equiv' π•œ n G G' f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply' (f : G [Γ—n.succ]β†’L[π•œ] G') (v : Ξ  (i : fin n), G) (x : G) : (continuous_multilinear_curry_right_equiv' π•œ n G G').symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_map.curry_right_norm (f : continuous_multilinear_map π•œ Ei G) : βˆ₯f.curry_rightβˆ₯ = βˆ₯fβˆ₯ := (continuous_multilinear_curry_right_equiv π•œ Ei G).symm.norm_map f @[simp] lemma continuous_multilinear_map.uncurry_right_norm (f : continuous_multilinear_map π•œ (Ξ» i : fin n, Ei i.cast_succ) (Ei (last n) β†’L[π•œ] G)) : βˆ₯f.uncurry_rightβˆ₯ = βˆ₯fβˆ₯ := (continuous_multilinear_curry_right_equiv π•œ Ei G).norm_map f /-! #### Currying with `0` variables The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!). Therefore, the space of continuous multilinear maps on `(fin 0) β†’ G` with values in `Eβ‚‚` is isomorphic (and even isometric) to `Eβ‚‚`. As this is the zeroth step in the construction of iterated derivatives, we register this isomorphism. -/ section local attribute [instance] unique.subsingleton variables {π•œ G G'} /-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/ def continuous_multilinear_map.uncurry0 (f : continuous_multilinear_map π•œ (Ξ» (i : fin 0), G) G') : G' := f 0 variables (π•œ G) /-- Associating to an element `x` of a vector space `Eβ‚‚` the continuous multilinear map in `0` variables taking the (unique) value `x` -/ def continuous_multilinear_map.curry0 (x : G') : G [Γ—0]β†’L[π•œ] G' := { to_fun := Ξ»m, x, map_add' := Ξ» m i, fin.elim0 i, map_smul' := Ξ» m i, fin.elim0 i, cont := continuous_const } variable {G} @[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) β†’ G) : continuous_multilinear_map.curry0 π•œ G x m = x := rfl variable {π•œ} @[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [Γ—0]β†’L[π•œ] G') : f.uncurry0 = f 0 := rfl @[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [Γ—0]β†’L[π•œ] G') {x : fin 0 β†’ G} : continuous_multilinear_map.curry0 π•œ G (f x) = f := by { ext m, simp [(subsingleton.elim _ _ : x = m)] } lemma continuous_multilinear_map.uncurry0_curry0 (f : G [Γ—0]β†’L[π•œ] G') : continuous_multilinear_map.curry0 π•œ G (f.uncurry0) = f := by simp variables (π•œ G) @[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') : (continuous_multilinear_map.curry0 π•œ G x).uncurry0 = x := rfl @[simp] lemma continuous_multilinear_map.curry0_norm (x : G') : βˆ₯continuous_multilinear_map.curry0 π•œ G xβˆ₯ = βˆ₯xβˆ₯ := begin apply le_antisymm, { exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (Ξ»m, by simp) }, { simpa using (continuous_multilinear_map.curry0 π•œ G x).le_op_norm 0 } end variables {π•œ G} @[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [Γ—0]β†’L[π•œ] G') {x : fin 0 β†’ G} : βˆ₯f xβˆ₯ = βˆ₯fβˆ₯ := begin have : x = 0 := subsingleton.elim _ _, subst this, refine le_antisymm (by simpa using f.le_op_norm 0) _, have : βˆ₯continuous_multilinear_map.curry0 π•œ G (f.uncurry0)βˆ₯ ≀ βˆ₯f.uncurry0βˆ₯ := continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (Ξ»m, by simp [-continuous_multilinear_map.apply_zero_curry0]), simpa end lemma continuous_multilinear_map.uncurry0_norm (f : G [Γ—0]β†’L[π•œ] G') : βˆ₯f.uncurry0βˆ₯ = βˆ₯fβˆ₯ := by simp variables (π•œ G G') /-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear maps in `0` variables with values in this normed space. The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_fin0 : (G [Γ—0]β†’L[π•œ] G') ≃ₗᡒ[π•œ] G' := { to_fun := Ξ»f, continuous_multilinear_map.uncurry0 f, inv_fun := Ξ»f, continuous_multilinear_map.curry0 π•œ G f, map_add' := Ξ»f g, rfl, map_smul' := Ξ»c f, rfl, left_inv := continuous_multilinear_map.uncurry0_curry0, right_inv := continuous_multilinear_map.curry0_uncurry0 π•œ G, norm_map' := continuous_multilinear_map.uncurry0_norm } variables {π•œ G G'} @[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [Γ—0]β†’L[π•œ] G') : continuous_multilinear_curry_fin0 π•œ G G' f = f 0 := rfl @[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) β†’ G) : (continuous_multilinear_curry_fin0 π•œ G G').symm x v = x := rfl end /-! #### With 1 variable -/ variables (π•œ G G') /-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from `G` to `G'`. -/ def continuous_multilinear_curry_fin1 : (G [Γ—1]β†’L[π•œ] G') ≃ₗᡒ[π•œ] (G β†’L[π•œ] G') := (continuous_multilinear_curry_right_equiv π•œ (Ξ» (i : fin 1), G) G').symm.trans (continuous_multilinear_curry_fin0 π•œ G (G β†’L[π•œ] G')) variables {π•œ G G'} @[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [Γ—1]β†’L[π•œ] G') (x : G) : continuous_multilinear_curry_fin1 π•œ G G' f x = f (fin.snoc 0 x) := rfl @[simp] lemma continuous_multilinear_curry_fin1_symm_apply (f : G β†’L[π•œ] G') (v : (fin 1) β†’ G) : (continuous_multilinear_curry_fin1 π•œ G G').symm f v = f (v 0) := rfl namespace continuous_multilinear_map variables (π•œ G G') /-- An equivalence of the index set defines a linear isometric equivalence between the spaces of multilinear maps. -/ def dom_dom_congr (Οƒ : ΞΉ ≃ ΞΉ') : continuous_multilinear_map π•œ (Ξ» _ : ΞΉ, G) G' ≃ₗᡒ[π•œ] continuous_multilinear_map π•œ (Ξ» _ : ΞΉ', G) G' := linear_isometry_equiv.of_bounds { to_fun := Ξ» f, (multilinear_map.dom_dom_congr Οƒ f.to_multilinear_map).mk_continuous βˆ₯fβˆ₯ $ Ξ» m, (f.le_op_norm (Ξ» i, m (Οƒ i))).trans_eq $ by rw [← Οƒ.prod_comp], inv_fun := Ξ» f, (multilinear_map.dom_dom_congr Οƒ.symm f.to_multilinear_map).mk_continuous βˆ₯fβˆ₯ $ Ξ» m, (f.le_op_norm (Ξ» i, m (Οƒ.symm i))).trans_eq $ by rw [← Οƒ.symm.prod_comp], left_inv := Ξ» f, ext $ Ξ» m, congr_arg f $ by simp only [Οƒ.symm_apply_apply], right_inv := Ξ» f, ext $ Ξ» m, congr_arg f $ by simp only [Οƒ.apply_symm_apply], map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl } (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {π•œ G G'} section variable [decidable_eq (ΞΉ βŠ• ΞΉ')] /-- A continuous multilinear map with variables indexed by `ΞΉ βŠ• ΞΉ'` defines a continuous multilinear map with variables indexed by `ΞΉ` taking values in the space of continuous multilinear maps with variables indexed by `ΞΉ'`. -/ def curry_sum (f : continuous_multilinear_map π•œ (Ξ» x : ΞΉ βŠ• ΞΉ', G) G') : continuous_multilinear_map π•œ (Ξ» x : ΞΉ, G) (continuous_multilinear_map π•œ (Ξ» x : ΞΉ', G) G') := multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (βˆ₯fβˆ₯) $ Ξ» m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m') @[simp] lemma curry_sum_apply (f : continuous_multilinear_map π•œ (Ξ» x : ΞΉ βŠ• ΞΉ', G) G') (m : ΞΉ β†’ G) (m' : ΞΉ' β†’ G) : f.curry_sum m m' = f (sum.elim m m') := rfl /-- A continuous multilinear map with variables indexed by `ΞΉ` taking values in the space of continuous multilinear maps with variables indexed by `ΞΉ'` defines a continuous multilinear map with variables indexed by `ΞΉ βŠ• ΞΉ'`. -/ def uncurry_sum (f : continuous_multilinear_map π•œ (Ξ» x : ΞΉ, G) (continuous_multilinear_map π•œ (Ξ» x : ΞΉ', G) G')) : continuous_multilinear_map π•œ (Ξ» x : ΞΉ βŠ• ΞΉ', G) G' := multilinear_map.mk_continuous (to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (βˆ₯fβˆ₯) $ Ξ» m, by simpa [fintype.prod_sum_type, mul_assoc] using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _) @[simp] lemma uncurry_sum_apply (f : continuous_multilinear_map π•œ (Ξ» x : ΞΉ, G) (continuous_multilinear_map π•œ (Ξ» x : ΞΉ', G) G')) (m : ΞΉ βŠ• ΞΉ' β†’ G) : f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) := rfl variables (π•œ ΞΉ ΞΉ' G G') /-- Linear isometric equivalence between the space of continuous multilinear maps with variables indexed by `ΞΉ βŠ• ΞΉ'` and the space of continuous multilinear maps with variables indexed by `ΞΉ` taking values in the space of continuous multilinear maps with variables indexed by `ΞΉ'`. The forward and inverse functions are `continuous_multilinear_map.curry_sum` and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need some properties of `linear_isometry_equiv`. -/ def curry_sum_equiv : continuous_multilinear_map π•œ (Ξ» x : ΞΉ βŠ• ΞΉ', G) G' ≃ₗᡒ[π•œ] continuous_multilinear_map π•œ (Ξ» x : ΞΉ, G) (continuous_multilinear_map π•œ (Ξ» x : ΞΉ', G) G') := linear_isometry_equiv.of_bounds { to_fun := curry_sum, inv_fun := uncurry_sum, map_add' := Ξ» f g, by { ext, refl }, map_smul' := Ξ» c f, by { ext, refl }, left_inv := Ξ» f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) }, right_inv := Ξ» f, by { ext m₁ mβ‚‚, change f _ _ = f _ _, rw [sum.elim_comp_inl, sum.elim_comp_inr] } } (Ξ» f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _) (Ξ» f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) end section variables (π•œ G G') {k l : β„•} {s : finset (fin n)} /-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality `l`, then the space of continuous multilinear maps `G [Γ—n]β†’L[π•œ] G'` of `n` variables is isomorphic to the space of continuous multilinear maps `G [Γ—k]β†’L[π•œ] G [Γ—l]β†’L[π•œ] G'` of `k` variables taking values in the space of continuous multilinear maps of `l` variables. -/ def curry_fin_finset {k l n : β„•} {s : finset (fin n)} (hk : s.card = k) (hl : sᢜ.card = l) : (G [Γ—n]β†’L[π•œ] G') ≃ₗᡒ[π•œ] (G [Γ—k]β†’L[π•œ] G [Γ—l]β†’L[π•œ] G') := (dom_dom_congr π•œ G G' (fin_sum_equiv_of_finset hk hl).symm).trans (curry_sum_equiv π•œ (fin k) (fin l) G G') variables {π•œ G G'} @[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᢜ.card = l) (f : G [Γ—n]β†’L[π•œ] G') (mk : fin k β†’ G) (ml : fin l β†’ G) : curry_fin_finset π•œ G G' hk hl f mk ml = f (Ξ» i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) := rfl @[simp] lemma curry_fin_finset_symm_apply (hk : s.card = k) (hl : sᢜ.card = l) (f : G [Γ—k]β†’L[π•œ] G [Γ—l]β†’L[π•œ] G') (m : fin n β†’ G) : (curry_fin_finset π•œ G G' hk hl).symm f m = f (Ξ» i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i)) (Ξ» i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) := rfl @[simp] lemma curry_fin_finset_symm_apply_piecewise_const (hk : s.card = k) (hl : sᢜ.card = l) (f : G [Γ—k]β†’L[π•œ] G [Γ—l]β†’L[π•œ] G') (x y : G) : (curry_fin_finset π•œ G G' hk hl).symm f (s.piecewise (Ξ» _, x) (Ξ» _, y)) = f (Ξ» _, x) (Ξ» _, y) := multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y @[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᢜ.card = l) (f : G [Γ—k]β†’L[π•œ] G [Γ—l]β†’L[π•œ] G') (x : G) : (curry_fin_finset π•œ G G' hk hl).symm f (Ξ» _, x) = f (Ξ» _, x) (Ξ» _, x) := rfl @[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᢜ.card = l) (f : G [Γ—n]β†’L[π•œ] G') (x y : G) : curry_fin_finset π•œ G G' hk hl f (Ξ» _, x) (Ξ» _, y) = f (s.piecewise (Ξ» _, x) (Ξ» _, y)) := begin refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails rw linear_isometry_equiv.symm_apply_apply end end end continuous_multilinear_map end currying
2a5ccfe6f5cd7c6a0042d9fdb708aa5170810a41
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/tools/super/clause.lean
bb59c37d47d177a4e3d8345be702987fd1dadfad
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
8,974
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import init.meta.tactic .utils .trim open expr list tactic monad decidable namespace super meta def is_local_not (local_false : expr) (e : expr) : option expr := match e with | (pi _ _ a b) := if b = local_false then some a else none | _ := if local_false = false_ then is_not e else none end meta structure clause := (num_quants : β„•) (num_lits : β„•) (proof : expr) (type : expr) (local_false : expr) namespace clause private meta def tactic_format (c : clause) : tactic format := do prf_fmt : format ← pp (proof c), type_fmt ← pp (type c), loc_fls_fmt ← pp c^.local_false, return $ prf_fmt ++ to_fmt " : " ++ type_fmt ++ to_fmt " (" ++ to_fmt (num_quants c) ++ to_fmt " quants, " ++ to_fmt (num_lits c) ++ to_fmt " lits)" meta instance : has_to_tactic_format clause := ⟨tactic_format⟩ meta def num_binders (c : clause) : β„• := num_quants c + num_lits c meta def inst (c : clause) (e : expr) : clause := (if num_quants c > 0 then mk (num_quants c - 1) (num_lits c) else mk 0 (num_lits c - 1)) (app (proof c) e) (instantiate_var (binding_body (type c)) e) c^.local_false meta def instn (c : clause) (es : list expr) : clause := foldr (Ξ»e c', inst c' e) c es meta def open_const (c : clause) : tactic (clause Γ— expr) := do n ← mk_fresh_name, b ← return $ local_const n (binding_name (type c)) (binding_info (type c)) (binding_domain (type c)), return (inst c b, b) meta def open_meta (c : clause) : tactic (clause Γ— expr) := do b ← mk_meta_var (binding_domain (type c)), return (inst c b, b) meta def close_const (c : clause) (e : expr) : clause := match e with | local_const uniq pp info t := let abst_type' := abstract_local (type c) (local_uniq_name e) in let type' := pi pp binder_info.default t (abstract_local (type c) uniq) in let abs_prf := abstract_local (proof c) uniq in let proof' := lambdas [e] c^.proof in if num_quants c > 0 ∨ has_var abst_type' then { c with num_quants := c^.num_quants + 1, proof := proof', type := type' } else { c with num_lits := c^.num_lits + 1, proof := proof', type := type' } | _ := ⟨0, 0, default expr, default expr, default expr⟩ end meta def open_constn : clause β†’ β„• β†’ tactic (clause Γ— list expr) | c 0 := return (c, nil) | c (n+1) := do (c', b) ← open_const c, (c'', bs) ← open_constn c' n, return (c'', b::bs) meta def open_metan : clause β†’ β„• β†’ tactic (clause Γ— list expr) | c 0 := return (c, nil) | c (n+1) := do (c', b) ← open_meta c, (c'', bs) ← open_metan c' n, return (c'', b::bs) meta def close_constn : clause β†’ list expr β†’ clause | c [] := c | c (b::bs') := close_const (close_constn c bs') b set_option eqn_compiler.max_steps 500 private meta def parse_clause (local_false : expr) : expr β†’ expr β†’ tactic clause | proof (pi n bi d b) := do lc_n ← mk_fresh_name, lc ← return $ local_const lc_n n bi d, c ← parse_clause (app proof lc) (instantiate_var b lc), return $ c^.close_const $ local_const lc_n n binder_info.default d | proof (app (const ``not []) formula) := parse_clause proof (formula^.imp false_) | proof type := if type = local_false then do return { num_quants := 0, num_lits := 0, proof := proof, type := type, local_false := local_false } else do univ ← infer_univ type, not_type ← return $ imp type local_false, parse_clause (lam `H binder_info.default not_type $ app (mk_var 0) proof) (imp not_type local_false) meta def of_proof_and_type (local_false proof type : expr) : tactic clause := parse_clause local_false proof type meta def of_proof (local_false proof : expr) : tactic clause := do type ← infer_type proof, of_proof_and_type local_false proof type meta def of_classical_proof (proof : expr) : tactic clause := of_proof false_ proof meta def inst_mvars (c : clause) : tactic clause := do proof' ← instantiate_mvars (proof c), type' ← instantiate_mvars (type c), return { c with proof := proof', type := type' } meta inductive literal | left : expr β†’ literal | right : expr β†’ literal namespace literal meta instance : decidable_eq literal := by mk_dec_eq_instance meta def formula : literal β†’ expr | (left f) := f | (right f) := f meta def is_neg : literal β†’ bool | (left _) := tt | (right _) := ff meta def is_pos (l : literal) : bool := bnot l^.is_neg meta def to_formula (l : literal) : tactic expr := if is_neg l then mk_mapp ``not [some (formula l)] else return (formula l) meta def type_str : literal β†’ string | (literal.left _) := "left" | (literal.right _) := "right" meta instance : has_to_tactic_format literal := ⟨λl, do pp_f ← pp l^.formula, return $ to_fmt l^.type_str ++ " (" ++ pp_f ++ ")"⟩ end literal private meta def get_binding_body : expr β†’ β„• β†’ expr | e 0 := e | e (i+1) := get_binding_body e^.binding_body i meta def get_binder (e : expr) (i : nat) := binding_domain (get_binding_body e i) meta def validate (c : clause) : tactic unit := do concl ← return $ get_binding_body c^.type c^.num_binders, unify concl c^.local_false <|> (do pp_concl ← pp concl, pp_lf ← pp c^.local_false, fail $ to_fmt "wrong local false: " ++ pp_concl ++ " =!= " ++ pp_lf), type' ← infer_type c^.proof, unify c^.type type' <|> (do pp_ty ← pp c^.type, pp_ty' ← pp type', fail (to_fmt "wrong type: " ++ pp_ty ++ " =!= " ++ pp_ty')) meta def get_lit (c : clause) (i : nat) : literal := let bind := get_binder (type c) (num_quants c + i) in match is_local_not c^.local_false bind with | some formula := literal.right formula | none := literal.left bind end meta def lits_where (c : clause) (p : literal β†’ bool) : list nat := list.filter (Ξ»l, p (get_lit c l)) (range (num_lits c)) meta def get_lits (c : clause) : list literal := list.map (get_lit c) (range c^.num_lits) meta def is_maximal (gt : expr β†’ expr β†’ bool) (c : clause) (i : nat) : bool := list.empty (list.filter (Ξ»j, gt (get_lit c j)^.formula (get_lit c i)^.formula) (range c^.num_lits)) meta def normalize (c : clause) : tactic clause := do opened ← open_constn c (num_binders c), lconsts_in_types ← return $ contained_lconsts_list (list.map local_type opened.2), quants' ← return $ list.filter (Ξ»lc, rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2, lits' ← return $ list.filter (Ξ»lc, Β¬rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2, return $ close_constn opened.1 (quants' ++ lits') meta def whnf_head_lit (c : clause) : tactic clause := do atom' ← whnf $ literal.formula $ get_lit c 0, return $ if literal.is_neg (get_lit c 0) then { c with type := imp atom' (binding_body c^.type) } else { c with type := imp (app (const ``not []) atom') c^.type^.binding_body } end clause meta def unify_lit (l1 l2 : clause.literal) : tactic unit := if clause.literal.is_pos l1 = clause.literal.is_pos l2 then unify_core transparency.none (clause.literal.formula l1) (clause.literal.formula l2) else fail "cannot unify literals" -- FIXME: this is most definitely broken with meta-variables that were already in the goal meta def sort_and_constify_metas : list expr β†’ tactic (list expr) | exprs_with_metas := do inst_exprs ← mapm instantiate_mvars exprs_with_metas, metas ← return $ inst_exprs >>= get_metas, match list.filter (Ξ»m, Β¬has_meta_var (get_meta_type m)) metas with | [] := if metas^.empty then return [] else do for' metas (Ξ»m, do trace (expr.to_string m), t ← infer_type m, trace (expr.to_string t)), fail "could not sort metas" | ((mvar n t) :: _) := do t' ← infer_type (mvar n t), uniq ← mk_fresh_name, c ← return (local_const uniq uniq binder_info.default t'), unify c (mvar n t), rest ← sort_and_constify_metas metas, return (rest ++ [c]) | _ := failed end namespace clause meta def meta_closure (metas : list expr) (qf : clause) : tactic clause := do bs ← sort_and_constify_metas metas, qf' ← clause.inst_mvars qf, clause.inst_mvars $ clause.close_constn qf' bs private meta def distinct' (local_false : expr) : list expr β†’ expr β†’ clause | [] proof := ⟨ 0, 0, proof, local_false, local_false ⟩ | (h::hs) proof := let (dups, rest) := partition (Ξ»h' : expr, h^.local_type = h'^.local_type) hs, proof_wo_dups := foldl (Ξ»proof (h' : expr), instantiate_var (abstract_local proof h'^.local_uniq_name) h) proof dups in (distinct' rest proof_wo_dups)^.close_const h meta def distinct (c : clause) : tactic clause := do (qf, vs) ← c^.open_constn c^.num_quants, (fls, hs) ← qf^.open_constn qf^.num_lits, return $ (distinct' c^.local_false hs fls^.proof)^.close_constn vs end clause end super
4c963c646cbc2a9a60eedb45a05ee364b9ef5170
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set/basic.lean
9ac1590fab6494efde0783f36d01b6e6db9f4dd0
[ "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
77,683
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import order.symm_diff import logic.function.iterate /-! # Basic properties of sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X β†’ Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coercion from `set Ξ±` to `Type*` sending `s` to the corresponding subtype `β†₯s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : Ξ± β†’ Ξ²` is a function, - `s : set Ξ±` and `s₁ sβ‚‚ : set Ξ±` are subsets of `Ξ±` - `t : set Ξ²` is a subset of `Ξ²`. Definitions in the file: * `nonempty s : Prop` : the predicate `s β‰  βˆ…`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `nontrivial s : Prop` : the predicate saying that `s` has at least two distinct elements. * `inclusion s₁ sβ‚‚ : β†₯s₁ β†’ β†₯sβ‚‚` : the map `β†₯s₁ β†’ β†₯sβ‚‚` induced by an inclusion `s₁ βŠ† sβ‚‚`. ## Notation * `sᢜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s β‰  βˆ…` or `βˆƒ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set Ξ±`, do not use `subtype s`. Instead use `β†₯s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ open function universes u v w x namespace set variables {Ξ± : Type*} {s t : set Ξ±} instance : has_le (set Ξ±) := ⟨λ s t, βˆ€ ⦃x⦄, x ∈ s β†’ x ∈ t⟩ instance : has_subset (set Ξ±) := ⟨(≀)⟩ instance {Ξ± : Type*} : boolean_algebra (set Ξ±) := { sup := Ξ» s t, {x | x ∈ s ∨ x ∈ t}, le := (≀), lt := Ξ» s t, s βŠ† t ∧ Β¬t βŠ† s, inf := Ξ» s t, {x | x ∈ s ∧ x ∈ t}, bot := βˆ…, compl := Ξ» s, {x | x βˆ‰ s}, top := univ, sdiff := Ξ» s t, {x | x ∈ s ∧ x βˆ‰ t}, .. (infer_instance : boolean_algebra (Ξ± β†’ Prop)) } instance : has_ssubset (set Ξ±) := ⟨(<)⟩ instance : has_union (set Ξ±) := ⟨(βŠ”)⟩ instance : has_inter (set Ξ±) := ⟨(βŠ“)⟩ @[simp] lemma top_eq_univ : (⊀ : set Ξ±) = univ := rfl @[simp] lemma bot_eq_empty : (βŠ₯ : set Ξ±) = βˆ… := rfl @[simp] lemma sup_eq_union : ((βŠ”) : set Ξ± β†’ set Ξ± β†’ set Ξ±) = (βˆͺ) := rfl @[simp] lemma inf_eq_inter : ((βŠ“) : set Ξ± β†’ set Ξ± β†’ set Ξ±) = (∩) := rfl @[simp] lemma le_eq_subset : ((≀) : set Ξ± β†’ set Ξ± β†’ Prop) = (βŠ†) := rfl @[simp] lemma lt_eq_ssubset : ((<) : set Ξ± β†’ set Ξ± β†’ Prop) = (βŠ‚) := rfl lemma le_iff_subset : s ≀ t ↔ s βŠ† t := iff.rfl lemma lt_iff_ssubset : s < t ↔ s βŠ‚ t := iff.rfl alias le_iff_subset ↔ _root_.has_le.le.subset _root_.has_subset.subset.le alias lt_iff_ssubset ↔ _root_.has_lt.lt.ssubset _root_.has_ssubset.ssubset.lt /-- Coercion from a set to the corresponding subtype. -/ instance {Ξ± : Type u} : has_coe_to_sort (set Ξ±) (Type u) := ⟨λ s, {x // x ∈ s}⟩ instance pi_set_coe.can_lift (ΞΉ : Type u) (Ξ± : Ξ  i : ΞΉ, Type v) [ne : Ξ  i, nonempty (Ξ± i)] (s : set ΞΉ) : can_lift (Ξ  i : s, Ξ± i) (Ξ  i, Ξ± i) (Ξ» f i, f i) (Ξ» _, true) := pi_subtype.can_lift ΞΉ Ξ± s instance pi_set_coe.can_lift' (ΞΉ : Type u) (Ξ± : Type v) [ne : nonempty Ξ±] (s : set ΞΉ) : can_lift (s β†’ Ξ±) (ΞΉ β†’ Ξ±) (Ξ» f i, f i) (Ξ» _, true) := pi_set_coe.can_lift ΞΉ (Ξ» _, Ξ±) s end set section set_coe variables {Ξ± : Type u} theorem set.coe_eq_subtype (s : set Ξ±) : β†₯s = {x // x ∈ s} := rfl @[simp] theorem set.coe_set_of (p : Ξ± β†’ Prop) : β†₯{x | p x} = {x // p x} := 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 theorem set_coe.exists' {s : set Ξ±} {p : Ξ  x, x ∈ s β†’ Prop} : (βˆƒ x (h : x ∈ s), p x h) ↔ (βˆƒ x : s, p x x.2) := (@set_coe.exists _ _ $ Ξ» x, p x.1 x.2).symm theorem set_coe.forall' {s : set Ξ±} {p : Ξ  x, x ∈ s β†’ Prop} : (βˆ€ x (h : x ∈ s), p x h) ↔ (βˆ€ x : s, p x x.2) := (@set_coe.forall _ _ $ Ξ» x, p x.1 x.2).symm @[simp] theorem set_coe_cast : βˆ€ {s t : set Ξ±} (H' : s = t) (H : β†₯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 /-- See also `subtype.prop` -/ lemma subtype.mem {Ξ± : Type*} {s : set Ξ±} (p : s) : (p : Ξ±) ∈ s := p.prop /-- Duplicate of `eq.subset'`, which currently has elaboration problems. -/ lemma eq.subset {Ξ±} {s t : set Ξ±} : s = t β†’ s βŠ† t := eq.subset' namespace set variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {a b : Ξ±} {s s₁ sβ‚‚ t t₁ tβ‚‚ u : set Ξ±} instance : inhabited (set Ξ±) := βŸ¨βˆ…βŸ© @[ext] 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 {x : Ξ±} {s t : set Ξ±} (hx : x ∈ s) (h : s βŠ† t) : x ∈ t := h hx lemma forall_in_swap {p : Ξ± β†’ Ξ² β†’ Prop} : (βˆ€ (a ∈ s) b, p a b) ↔ βˆ€ b (a ∈ s), p a b := by tauto /-! ### Lemmas about `mem` and `set_of` -/ lemma mem_set_of {a : Ξ±} {p : Ξ± β†’ Prop} : a ∈ {x | p x} ↔ p a := iff.rfl /-- If `h : a ∈ {x | p x}` then `h.out : p x`. These are definitionally equal, but this can nevertheless be useful for various reasons, e.g. to apply further projection notation or in an argument to `simp`. -/ lemma _root_.has_mem.mem.out {p : Ξ± β†’ Prop} {a : Ξ±} (h : a ∈ {x | p x}) : p a := h theorem nmem_set_of_iff {a : Ξ±} {p : Ξ± β†’ Prop} : a βˆ‰ {x | p x} ↔ Β¬ p a := iff.rfl @[simp] theorem set_of_mem_eq {s : set Ξ±} : {x | x ∈ s} = s := rfl theorem set_of_set {s : set Ξ±} : set_of s = s := rfl lemma set_of_app_iff {p : Ξ± β†’ Prop} {x : Ξ±} : {x | p x} x ↔ p x := iff.rfl theorem mem_def {a : Ξ±} {s : set Ξ±} : a ∈ s ↔ s a := iff.rfl lemma set_of_bijective : bijective (set_of : (Ξ± β†’ Prop) β†’ set Ξ±) := bijective_id @[simp] theorem set_of_subset_set_of {p q : Ξ± β†’ Prop} : {a | p a} βŠ† {a | q a} ↔ (βˆ€a, p a β†’ q a) := iff.rfl lemma set_of_and {p q : Ξ± β†’ Prop} : {a | p a ∧ q a} = {a | p a} ∩ {a | q a} := rfl lemma set_of_or {p q : Ξ± β†’ Prop} : {a | p a ∨ q a} = {a | p a} βˆͺ {a | q a} := rfl /-! ### Subset and strict subset relations -/ instance : is_refl (set Ξ±) (βŠ†) := has_le.le.is_refl instance : is_trans (set Ξ±) (βŠ†) := has_le.le.is_trans instance : is_antisymm (set Ξ±) (βŠ†) := has_le.le.is_antisymm instance : is_irrefl (set Ξ±) (βŠ‚) := has_lt.lt.is_irrefl instance : is_trans (set Ξ±) (βŠ‚) := has_lt.lt.is_trans instance : is_asymm (set Ξ±) (βŠ‚) := has_lt.lt.is_asymm instance : is_nonstrict_strict_order (set Ξ±) (βŠ†) (βŠ‚) := ⟨λ _ _, iff.rfl⟩ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? lemma subset_def : (s βŠ† t) = βˆ€ x, x ∈ s β†’ x ∈ t := rfl lemma ssubset_def : s βŠ‚ t = (s βŠ† t ∧ Β¬ t βŠ† s) := rfl @[refl] theorem subset.refl (a : set Ξ±) : a βŠ† a := assume x, id theorem subset.rfl {s : set Ξ±} : s βŠ† s := subset.refl s @[trans] theorem subset.trans {a b c : set Ξ±} (ab : a βŠ† b) (bc : b βŠ† c) : a βŠ† c := Ξ» x h, bc $ ab h @[trans] theorem mem_of_eq_of_mem {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 := set.ext $ Ξ» x, ⟨@h₁ _, @hβ‚‚ _⟩ theorem subset.antisymm_iff {a b : set Ξ±} : a = b ↔ a βŠ† b ∧ b βŠ† a := ⟨λ e, ⟨e.subset, e.symm.subset⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, subset.antisymm h₁ hβ‚‚βŸ© -- an alternative name theorem eq_of_subset_of_subset {a b : set Ξ±} : a βŠ† b β†’ b βŠ† a β†’ a = b := subset.antisymm theorem mem_of_subset_of_mem {s₁ sβ‚‚ : set Ξ±} {a : Ξ±} (h : s₁ βŠ† sβ‚‚) : a ∈ s₁ β†’ a ∈ sβ‚‚ := @h _ theorem not_mem_subset (h : s βŠ† t) : a βˆ‰ t β†’ a βˆ‰ s := mt $ mem_of_subset_of_mem h theorem not_subset : (Β¬ s βŠ† t) ↔ βˆƒa ∈ s, a βˆ‰ t := by simp only [subset_def, not_forall] /-! ### Definition of strict subsets `s βŠ‚ t` and basic properties. -/ protected theorem eq_or_ssubset_of_subset (h : s βŠ† t) : s = t ∨ s βŠ‚ t := eq_or_lt_of_le h lemma exists_of_ssubset {s t : set Ξ±} (h : s βŠ‚ t) : (βˆƒx∈t, x βˆ‰ s) := not_subset.1 h.2 protected lemma ssubset_iff_subset_ne {s t : set Ξ±} : s βŠ‚ t ↔ s βŠ† t ∧ s β‰  t := @lt_iff_le_and_ne (set Ξ±) _ s t lemma ssubset_iff_of_subset {s t : set Ξ±} (h : s βŠ† t) : s βŠ‚ t ↔ βˆƒ x ∈ t, x βˆ‰ s := ⟨exists_of_ssubset, Ξ» ⟨x, hxt, hxs⟩, ⟨h, Ξ» h, hxs $ h hxt⟩⟩ protected lemma ssubset_of_ssubset_of_subset {s₁ sβ‚‚ s₃ : set Ξ±} (hs₁sβ‚‚ : s₁ βŠ‚ sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ† s₃) : s₁ βŠ‚ s₃ := ⟨subset.trans hs₁sβ‚‚.1 hsβ‚‚s₃, Ξ» hs₃s₁, hs₁sβ‚‚.2 (subset.trans hsβ‚‚s₃ hs₃s₁)⟩ protected lemma ssubset_of_subset_of_ssubset {s₁ sβ‚‚ s₃ : set Ξ±} (hs₁sβ‚‚ : s₁ βŠ† sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ‚ s₃) : s₁ βŠ‚ s₃ := ⟨subset.trans hs₁sβ‚‚ hsβ‚‚s₃.1, Ξ» hs₃s₁, hsβ‚‚s₃.2 (subset.trans hs₃s₁ hs₁sβ‚‚)⟩ theorem not_mem_empty (x : Ξ±) : Β¬ (x ∈ (βˆ… : set Ξ±)) := id @[simp] theorem not_not_mem : Β¬ (a βˆ‰ s) ↔ a ∈ s := not_not /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `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 : set Ξ±) : Prop := βˆƒ x, x ∈ s @[simp] lemma nonempty_coe_sort {s : set Ξ±} : nonempty β†₯s ↔ s.nonempty := nonempty_subtype alias nonempty_coe_sort ↔ _ nonempty.coe_sort lemma nonempty_def : s.nonempty ↔ βˆƒ x, x ∈ s := iff.rfl lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩ theorem nonempty.not_subset_empty : s.nonempty β†’ Β¬(s βŠ† βˆ…) | ⟨x, hx⟩ hs := hs hx /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected noncomputable def nonempty.some (h : s.nonempty) : Ξ± := classical.some h protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h lemma nonempty.mono (ht : s βŠ† t) (hs : s.nonempty) : t.nonempty := hs.imp ht lemma nonempty_of_not_subset (h : Β¬s βŠ† t) : (s \ t).nonempty := let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩ lemma nonempty_of_ssubset (ht : s βŠ‚ t) : (t \ s).nonempty := nonempty_of_not_subset ht.2 lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left lemma nonempty_of_ssubset' (ht : s βŠ‚ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff lemma nonempty.inl (hs : s.nonempty) : (s βˆͺ t).nonempty := hs.imp $ Ξ» _, or.inl lemma nonempty.inr (ht : t.nonempty) : (s βˆͺ t).nonempty := ht.imp $ Ξ» _, or.inr @[simp] lemma union_nonempty : (s βˆͺ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ Ξ» _, and.right lemma inter_nonempty : (s ∩ t).nonempty ↔ βˆƒ x, x ∈ s ∧ x ∈ t := iff.rfl lemma inter_nonempty_iff_exists_left : (s ∩ t).nonempty ↔ βˆƒ x ∈ s, x ∈ t := by simp_rw [inter_nonempty, exists_prop] lemma inter_nonempty_iff_exists_right : (s ∩ t).nonempty ↔ βˆƒ x ∈ t, x ∈ s := by simp_rw [inter_nonempty, exists_prop, and_comm] lemma nonempty_iff_univ_nonempty : nonempty Ξ± ↔ (univ : set Ξ±).nonempty := ⟨λ ⟨x⟩, ⟨x, trivial⟩, Ξ» ⟨x, _⟩, ⟨x⟩⟩ @[simp] lemma univ_nonempty : βˆ€ [h : nonempty Ξ±], (univ : set Ξ±).nonempty | ⟨x⟩ := ⟨x, trivial⟩ lemma nonempty.to_subtype : s.nonempty β†’ nonempty s := nonempty_subtype.2 lemma nonempty.to_type : s.nonempty β†’ nonempty Ξ± := Ξ» ⟨x, hx⟩, ⟨x⟩ instance [nonempty Ξ±] : nonempty (set.univ : set Ξ±) := set.univ_nonempty.to_subtype lemma nonempty_of_nonempty_subtype [nonempty s] : s.nonempty := nonempty_subtype.mp β€Ή_β€Ί /-! ### Lemmas about the empty set -/ theorem empty_def : (βˆ… : set Ξ±) = {x | false} := rfl @[simp] theorem mem_empty_iff_false (x : Ξ±) : x ∈ (βˆ… : set Ξ±) ↔ false := iff.rfl @[simp] theorem set_of_false : {a : Ξ± | false} = βˆ… := rfl @[simp] theorem empty_subset (s : set Ξ±) : βˆ… βŠ† s. theorem subset_empty_iff {s : set Ξ±} : s βŠ† βˆ… ↔ s = βˆ… := (subset.antisymm_iff.trans $ and_iff_left (empty_subset _)).symm theorem eq_empty_iff_forall_not_mem {s : set Ξ±} : s = βˆ… ↔ βˆ€ x, x βˆ‰ s := subset_empty_iff.symm lemma eq_empty_of_forall_not_mem (h : βˆ€ x, x βˆ‰ s) : s = βˆ… := subset_empty_iff.1 h theorem eq_empty_of_subset_empty {s : set Ξ±} : s βŠ† βˆ… β†’ s = βˆ… := subset_empty_iff.1 theorem eq_empty_of_is_empty [is_empty Ξ±] (s : set Ξ±) : s = βˆ… := eq_empty_of_subset_empty $ Ξ» x hx, is_empty_elim x /-- There is exactly one set of a type that is empty. -/ instance unique_empty [is_empty Ξ±] : unique (set Ξ±) := { default := βˆ…, uniq := eq_empty_of_is_empty } /-- See also `set.nonempty_iff_ne_empty`. -/ lemma not_nonempty_iff_eq_empty {s : set Ξ±} : Β¬s.nonempty ↔ s = βˆ… := by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists] /-- See also `set.not_nonempty_iff_eq_empty`. -/ lemma nonempty_iff_ne_empty : s.nonempty ↔ s β‰  βˆ… := not_nonempty_iff_eq_empty.not_right alias nonempty_iff_ne_empty ↔ nonempty.ne_empty _ @[simp] lemma not_nonempty_empty : Β¬(βˆ… : set Ξ±).nonempty := Ξ» ⟨x, hx⟩, hx @[simp] lemma is_empty_coe_sort {s : set Ξ±} : is_empty β†₯s ↔ s = βˆ… := not_iff_not.1 $ by simpa using nonempty_iff_ne_empty lemma eq_empty_or_nonempty (s : set Ξ±) : s = βˆ… ∨ s.nonempty := or_iff_not_imp_left.2 nonempty_iff_ne_empty.2 theorem subset_eq_empty {s t : set Ξ±} (h : t βŠ† s) (e : s = βˆ…) : t = βˆ… := subset_empty_iff.1 $ e β–Έ h theorem ball_empty_iff {p : Ξ± β†’ Prop} : (βˆ€ x ∈ (βˆ… : set Ξ±), p x) ↔ true := iff_true_intro $ Ξ» x, false.elim instance (Ξ± : Type u) : is_empty.{u+1} (βˆ… : set Ξ±) := ⟨λ x, x.2⟩ @[simp] lemma empty_ssubset : βˆ… βŠ‚ s ↔ s.nonempty := (@bot_lt_iff_ne_bot (set Ξ±) _ _ _).trans nonempty_iff_ne_empty.symm alias empty_ssubset ↔ _ nonempty.empty_ssubset /-! ### Universal set. In Lean `@univ Ξ±` (or `univ : set Ξ±`) is the set that contains all elements of type `Ξ±`. Mathematically it is the same as `Ξ±` but it has a different type. -/ @[simp] theorem set_of_true : {x : Ξ± | true} = univ := rfl @[simp, mfld_simps] theorem mem_univ (x : Ξ±) : x ∈ @univ Ξ± := trivial @[simp] lemma univ_eq_empty_iff : (univ : set Ξ±) = βˆ… ↔ is_empty Ξ± := eq_empty_iff_forall_not_mem.trans ⟨λ H, ⟨λ x, H x trivial⟩, Ξ» H x _, @is_empty.false Ξ± H x⟩ theorem empty_ne_univ [nonempty Ξ±] : (βˆ… : set Ξ±) β‰  univ := Ξ» e, not_is_empty_of_nonempty Ξ± $ univ_eq_empty_iff.1 e.symm @[simp] theorem subset_univ (s : set Ξ±) : s βŠ† univ := Ξ» x H, trivial theorem univ_subset_iff {s : set Ξ±} : univ βŠ† s ↔ s = univ := @top_le_iff _ _ _ s alias univ_subset_iff ↔ eq_univ_of_univ_subset _ theorem eq_univ_iff_forall {s : set Ξ±} : s = univ ↔ βˆ€ x, x ∈ s := univ_subset_iff.symm.trans $ forall_congr $ Ξ» x, imp_iff_right trivial theorem eq_univ_of_forall {s : set Ξ±} : (βˆ€ x, x ∈ s) β†’ s = univ := eq_univ_iff_forall.2 lemma nonempty.eq_univ [subsingleton Ξ±] : s.nonempty β†’ s = univ := by { rintro ⟨x, hx⟩, refine eq_univ_of_forall (Ξ» y, by rwa subsingleton.elim y x) } lemma eq_univ_of_subset {s t : set Ξ±} (h : s βŠ† t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset $ hs β–Έ h lemma exists_mem_of_nonempty (Ξ±) : βˆ€ [nonempty Ξ±], βˆƒx:Ξ±, x ∈ (univ : set Ξ±) | ⟨x⟩ := ⟨x, trivial⟩ lemma ne_univ_iff_exists_not_mem {Ξ± : Type*} (s : set Ξ±) : s β‰  univ ↔ βˆƒ a, a βˆ‰ s := by rw [←not_forall, ←eq_univ_iff_forall] lemma not_subset_iff_exists_mem_not_mem {Ξ± : Type*} {s t : set Ξ±} : Β¬ s βŠ† t ↔ βˆƒ x, x ∈ s ∧ x βˆ‰ t := by simp [subset_def] lemma univ_unique [unique Ξ±] : @set.univ Ξ± = {default} := set.ext $ Ξ» x, iff_of_true trivial $ subsingleton.elim x default lemma ssubset_univ_iff : s βŠ‚ univ ↔ s β‰  univ := lt_top_iff_ne_top instance nontrivial_of_nonempty [nonempty Ξ±] : nontrivial (set Ξ±) := βŸ¨βŸ¨βˆ…, univ, empty_ne_univ⟩⟩ /-! ### Lemmas about 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₃ @[simp] theorem mem_union (x : Ξ±) (a b : set Ξ±) : x ∈ a βˆͺ b ↔ (x ∈ a ∨ x ∈ b) := iff.rfl @[simp] theorem union_self (a : set Ξ±) : a βˆͺ a = a := ext $ Ξ» x, or_self _ @[simp] theorem union_empty (a : set Ξ±) : a βˆͺ βˆ… = a := ext $ Ξ» x, or_false _ @[simp] theorem empty_union (a : set Ξ±) : βˆ… βˆͺ a = a := ext $ Ξ» x, false_or _ theorem union_comm (a b : set Ξ±) : a βˆͺ b = b βˆͺ a := ext $ Ξ» x, or.comm theorem union_assoc (a b c : set Ξ±) : (a βˆͺ b) βˆͺ c = a βˆͺ (b βˆͺ c) := ext $ Ξ» 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₃) := ext $ Ξ» x, or.left_comm theorem union_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := ext $ Ξ» x, or.right_comm @[simp] theorem union_eq_left_iff_subset {s t : set Ξ±} : s βˆͺ t = s ↔ t βŠ† s := sup_eq_left @[simp] theorem union_eq_right_iff_subset {s t : set Ξ±} : s βˆͺ t = t ↔ s βŠ† t := sup_eq_right theorem union_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ t = t := union_eq_right_iff_subset.mpr h theorem union_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s βˆͺ t = s := union_eq_left_iff_subset.mpr h @[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 := Ξ» x, or.rec (@sr _) (@tr _) @[simp] theorem union_subset_iff {s t u : set Ξ±} : s βˆͺ t βŠ† u ↔ s βŠ† u ∧ t βŠ† u := (forall_congr (by exact Ξ» x, or_imp_distrib)).trans forall_and_distrib theorem union_subset_union {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} (h₁ : s₁ βŠ† sβ‚‚) (hβ‚‚ : t₁ βŠ† tβ‚‚) : s₁ βˆͺ t₁ βŠ† sβ‚‚ βˆͺ tβ‚‚ := Ξ» x, or.imp (@h₁ _) (@hβ‚‚ _) theorem union_subset_union_left {s₁ sβ‚‚ : set Ξ±} (t) (h : s₁ βŠ† sβ‚‚) : s₁ βˆͺ t βŠ† sβ‚‚ βˆͺ t := union_subset_union h subset.rfl theorem union_subset_union_right (s) {t₁ tβ‚‚ : set Ξ±} (h : t₁ βŠ† tβ‚‚) : s βˆͺ t₁ βŠ† s βˆͺ tβ‚‚ := union_subset_union subset.rfl h lemma subset_union_of_subset_left {s t : set Ξ±} (h : s βŠ† t) (u : set Ξ±) : s βŠ† t βˆͺ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set Ξ±} (h : s βŠ† u) (t : set Ξ±) : s βŠ† t βˆͺ u := subset.trans h (subset_union_right t u) lemma union_congr_left (ht : t βŠ† s βˆͺ u) (hu : u βŠ† s βˆͺ t) : s βˆͺ t = s βŠ” u := sup_congr_left ht hu lemma union_congr_right (hs : s βŠ† t βˆͺ u) (ht : t βŠ† s βˆͺ u) : s βˆͺ u = t βˆͺ u := sup_congr_right hs ht lemma union_eq_union_iff_left : s βˆͺ t = s βˆͺ u ↔ t βŠ† s βˆͺ u ∧ u βŠ† s βˆͺ t := sup_eq_sup_iff_left lemma union_eq_union_iff_right : s βˆͺ u = t βˆͺ u ↔ s βŠ† t βˆͺ u ∧ t βŠ† s βˆͺ u := sup_eq_sup_iff_right @[simp] theorem union_empty_iff {s t : set Ξ±} : s βˆͺ t = βˆ… ↔ s = βˆ… ∧ t = βˆ… := by simp only [← subset_empty_iff]; exact union_subset_iff @[simp] lemma union_univ {s : set Ξ±} : s βˆͺ univ = univ := sup_top_eq @[simp] lemma univ_union {s : set Ξ±} : univ βˆͺ s = univ := top_sup_eq /-! ### Lemmas about intersection -/ theorem inter_def {s₁ sβ‚‚ : set Ξ±} : s₁ ∩ sβ‚‚ = {a | a ∈ s₁ ∧ a ∈ sβ‚‚} := rfl @[simp, mfld_simps] theorem mem_inter_iff (x : Ξ±) (a b : set Ξ±) : x ∈ a ∩ b ↔ (x ∈ a ∧ x ∈ b) := iff.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 $ Ξ» x, and_self _ @[simp] theorem inter_empty (a : set Ξ±) : a ∩ βˆ… = βˆ… := ext $ Ξ» x, and_false _ @[simp] theorem empty_inter (a : set Ξ±) : βˆ… ∩ a = βˆ… := ext $ Ξ» x, false_and _ theorem inter_comm (a b : set Ξ±) : a ∩ b = b ∩ a := ext $ Ξ» x, and.comm theorem inter_assoc (a b c : set Ξ±) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext $ Ξ» 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₃) := ext $ Ξ» x, and.left_comm theorem inter_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := ext $ Ξ» x, and.right_comm @[simp, mfld_simps] theorem inter_subset_left (s t : set Ξ±) : s ∩ t βŠ† s := Ξ» x, and.left @[simp] theorem inter_subset_right (s t : set Ξ±) : s ∩ t βŠ† t := Ξ» x, and.right theorem subset_inter {s t r : set Ξ±} (rs : r βŠ† s) (rt : r βŠ† t) : r βŠ† s ∩ t := Ξ» x h, ⟨rs h, rt h⟩ @[simp] theorem subset_inter_iff {s t r : set Ξ±} : r βŠ† s ∩ t ↔ r βŠ† s ∧ r βŠ† t := (forall_congr (by exact Ξ» x, imp_and_distrib)).trans forall_and_distrib @[simp] theorem inter_eq_left_iff_subset {s t : set Ξ±} : s ∩ t = s ↔ s βŠ† t := inf_eq_left @[simp] theorem inter_eq_right_iff_subset {s t : set Ξ±} : s ∩ t = t ↔ t βŠ† s := inf_eq_right theorem inter_eq_self_of_subset_left {s t : set Ξ±} : s βŠ† t β†’ s ∩ t = s := inter_eq_left_iff_subset.mpr theorem inter_eq_self_of_subset_right {s t : set Ξ±} : t βŠ† s β†’ s ∩ t = t := inter_eq_right_iff_subset.mpr lemma inter_congr_left (ht : s ∩ u βŠ† t) (hu : s ∩ t βŠ† u) : s ∩ t = s ∩ u := inf_congr_left ht hu lemma inter_congr_right (hs : t ∩ u βŠ† s) (ht : s ∩ u βŠ† t) : s ∩ u = t ∩ u := inf_congr_right hs ht lemma inter_eq_inter_iff_left : s ∩ t = s ∩ u ↔ s ∩ u βŠ† t ∧ s ∩ t βŠ† u := inf_eq_inf_iff_left lemma inter_eq_inter_iff_right : s ∩ u = t ∩ u ↔ t ∩ u βŠ† s ∧ s ∩ u βŠ† t := inf_eq_inf_iff_right @[simp, mfld_simps] theorem inter_univ (a : set Ξ±) : a ∩ univ = a := inf_top_eq @[simp, mfld_simps] theorem univ_inter (a : set Ξ±) : univ ∩ a = a := top_inf_eq theorem inter_subset_inter {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} (h₁ : s₁ βŠ† t₁) (hβ‚‚ : sβ‚‚ βŠ† tβ‚‚) : s₁ ∩ sβ‚‚ βŠ† t₁ ∩ tβ‚‚ := Ξ» x, and.imp (@h₁ _) (@hβ‚‚ _) theorem inter_subset_inter_left {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : s ∩ u βŠ† t ∩ u := inter_subset_inter H subset.rfl theorem inter_subset_inter_right {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : u ∩ s βŠ† u ∩ t := inter_subset_inter subset.rfl H theorem union_inter_cancel_left {s t : set Ξ±} : (s βˆͺ t) ∩ s = s := inter_eq_self_of_subset_right $ subset_union_left _ _ theorem union_inter_cancel_right {s t : set Ξ±} : (s βˆͺ t) ∩ t = t := inter_eq_self_of_subset_right $ subset_union_right _ _ lemma inter_set_of_eq_sep (s : set Ξ±) (p : Ξ± β†’ Prop) : s ∩ {a | p a} = {a ∈ s | p a} := rfl lemma set_of_inter_eq_sep (p : Ξ± β†’ Prop) (s : set Ξ±) : {a | p a} ∩ s = {a ∈ s | p a} := inter_comm _ _ /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set Ξ±) : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_union_distrib_left {s t u : set Ξ±} : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : set Ξ±) : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_inter_distrib_right {s t u : set Ξ±} : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : set Ξ±) : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_inter_distrib_left {s t u : set Ξ±} : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_distrib_right (s t u : set Ξ±) : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right theorem inter_union_distrib_right {s t u : set Ξ±} : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right lemma union_union_distrib_left (s t u : set Ξ±) : s βˆͺ (t βˆͺ u) = (s βˆͺ t) βˆͺ (s βˆͺ u) := sup_sup_distrib_left _ _ _ lemma union_union_distrib_right (s t u : set Ξ±) : (s βˆͺ t) βˆͺ u = (s βˆͺ u) βˆͺ (t βˆͺ u) := sup_sup_distrib_right _ _ _ lemma inter_inter_distrib_left (s t u : set Ξ±) : s ∩ (t ∩ u) = (s ∩ t) ∩ (s ∩ u) := inf_inf_distrib_left _ _ _ lemma inter_inter_distrib_right (s t u : set Ξ±) : (s ∩ t) ∩ u = (s ∩ u) ∩ (t ∩ u) := inf_inf_distrib_right _ _ _ lemma union_union_union_comm (s t u v : set Ξ±) : (s βˆͺ t) βˆͺ (u βˆͺ v) = (s βˆͺ u) βˆͺ (t βˆͺ v) := sup_sup_sup_comm _ _ _ _ lemma inter_inter_inter_comm (s t u v : set Ξ±) : (s ∩ t) ∩ (u ∩ v) = (s ∩ u) ∩ (t ∩ v) := inf_inf_inf_comm _ _ _ _ /-! ### Lemmas about `insert` `insert Ξ± s` is the set `{Ξ±} βˆͺ s`. -/ theorem insert_def (x : Ξ±) (s : set Ξ±) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem subset_insert (x : Ξ±) (s : set Ξ±) : s βŠ† insert x s := Ξ» y, or.inr 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 lemma mem_of_mem_insert_of_ne : b ∈ insert a s β†’ b β‰  a β†’ b ∈ s := or.resolve_left lemma eq_of_not_mem_of_mem_insert : b ∈ insert a s β†’ b βˆ‰ s β†’ b = a := or.resolve_right @[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 := ext $ Ξ» x, or_iff_right_of_imp $ Ξ» e, e.symm β–Έ h lemma ne_insert_of_not_mem {s : set Ξ±} (t : set Ξ±) {a : Ξ±} : a βˆ‰ s β†’ s β‰  insert a t := mt $ Ξ» e, e.symm β–Έ mem_insert _ _ @[simp] lemma insert_eq_self : insert a s = s ↔ a ∈ s := ⟨λ h, h β–Έ mem_insert _ _, insert_eq_of_mem⟩ lemma insert_ne_self : insert a s β‰  s ↔ a βˆ‰ s := insert_eq_self.not theorem insert_subset : insert a s βŠ† t ↔ (a ∈ t ∧ s βŠ† t) := by simp only [subset_def, or_imp_distrib, forall_and_distrib, forall_eq, mem_insert_iff] theorem insert_subset_insert (h : s βŠ† t) : insert a s βŠ† insert a t := Ξ» x, or.imp_right (@h _) theorem insert_subset_insert_iff (ha : a βˆ‰ s) : insert a s βŠ† insert a t ↔ s βŠ† t := begin refine ⟨λ h x hx, _, insert_subset_insert⟩, rcases h (subset_insert _ _ hx) with (rfl|hxt), exacts [(ha hx).elim, hxt] end theorem subset_insert_iff_of_not_mem (ha : a βˆ‰ s) : s βŠ† insert a t ↔ s βŠ† t := forallβ‚‚_congr $ Ξ» b hb, or_iff_right $ ne_of_mem_of_not_mem hb ha theorem ssubset_iff_insert {s t : set Ξ±} : s βŠ‚ t ↔ βˆƒ a βˆ‰ s, insert a s βŠ† t := begin simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset], simp only [exists_prop, and_comm] end theorem ssubset_insert {s : set Ξ±} {a : Ξ±} (h : a βˆ‰ s) : s βŠ‚ insert a s := ssubset_iff_insert.2 ⟨a, h, subset.rfl⟩ theorem insert_comm (a b : Ξ±) (s : set Ξ±) : insert a (insert b s) = insert b (insert a s) := ext $ Ξ» x, or.left_comm @[simp] lemma insert_idem (a : Ξ±) (s : set Ξ±) : insert a (insert a s) = insert a s := insert_eq_of_mem $ mem_insert _ _ theorem insert_union : insert a s βˆͺ t = insert a (s βˆͺ t) := ext $ Ξ» x, or.assoc @[simp] theorem union_insert : s βˆͺ insert a t = insert a (s βˆͺ t) := ext $ Ξ» x, or.left_comm @[simp] theorem insert_nonempty (a : Ξ±) (s : set Ξ±) : (insert a s).nonempty := ⟨a, mem_insert a s⟩ instance (a : Ξ±) (s : set Ξ±) : nonempty (insert a s : set Ξ±) := (insert_nonempty a s).to_subtype lemma insert_inter_distrib (a : Ξ±) (s t : set Ξ±) : insert a (s ∩ t) = insert a s ∩ insert a t := ext $ Ξ» y, or_and_distrib_left lemma insert_union_distrib (a : Ξ±) (s t : set Ξ±) : insert a (s βˆͺ t) = insert a s βˆͺ insert a t := ext $ Ξ» _, or_or_distrib_left _ _ _ lemma insert_inj (ha : a βˆ‰ s) : insert a s = insert b s ↔ a = b := ⟨λ h, eq_of_not_mem_of_mem_insert (h.subst $ mem_insert a s) ha, congr_arg _⟩ -- useful in proofs by induction theorem forall_of_forall_insert {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} (H : βˆ€ x, x ∈ insert a s β†’ P x) (x) (h : x ∈ s) : P x := H _ (or.inr h) theorem forall_insert_of_forall {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} (H : βˆ€ x, x ∈ s β†’ P x) (ha : P a) (x) (h : x ∈ insert a s) : P x := h.elim (Ξ» e, e.symm β–Έ ha) (H _) theorem bex_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆƒ x ∈ insert a s, P x) ↔ P a ∨ (βˆƒ x ∈ s, P x) := bex_or_left_distrib.trans $ or_congr_left' bex_eq_left theorem ball_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆ€ x ∈ insert a s, P x) ↔ P a ∧ (βˆ€x ∈ s, P x) := ball_or_left_distrib.trans $ and_congr_left' forall_eq /-! ### Lemmas about singletons -/ theorem singleton_def (a : Ξ±) : ({a} : set Ξ±) = insert a βˆ… := (insert_emptyc_eq _).symm @[simp] theorem mem_singleton_iff {a b : Ξ±} : a ∈ ({b} : set Ξ±) ↔ a = b := iff.rfl @[simp] lemma set_of_eq_eq_singleton {a : Ξ±} : {n | n = a} = {a} := rfl @[simp] lemma set_of_eq_eq_singleton' {a : Ξ±} : {x | a = x} = {a} := ext $ Ξ» x, eq_comm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : Ξ±) : a ∈ ({a} : set Ξ±) := @rfl _ _ theorem eq_of_mem_singleton {x y : Ξ±} (h : x ∈ ({y} : set Ξ±)) : x = y := h @[simp] theorem singleton_eq_singleton_iff {x y : Ξ±} : {x} = ({y} : set Ξ±) ↔ x = y := ext_iff.trans eq_iff_eq_cancel_left lemma singleton_injective : injective (singleton : Ξ± β†’ set Ξ±) := Ξ» _ _, singleton_eq_singleton_iff.mp theorem mem_singleton_of_eq {x y : Ξ±} (H : x = y) : x ∈ ({y} : set Ξ±) := H theorem insert_eq (x : Ξ±) (s : set Ξ±) : insert x s = ({x} : set Ξ±) βˆͺ s := rfl @[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : set Ξ±).nonempty := ⟨a, rfl⟩ @[simp] lemma singleton_ne_empty (a : Ξ±) : ({a} : set Ξ±) β‰  βˆ… := (singleton_nonempty _).ne_empty @[simp] lemma empty_ssubset_singleton : (βˆ… : set Ξ±) βŠ‚ {a} := (singleton_nonempty _).empty_ssubset @[simp] theorem singleton_subset_iff {a : Ξ±} {s : set Ξ±} : {a} βŠ† s ↔ a ∈ s := forall_eq lemma singleton_subset_singleton : ({a} : set Ξ±) βŠ† {b} ↔ a = b := by simp theorem set_compr_eq_eq_singleton {a : Ξ±} : {b | b = a} = {a} := rfl @[simp] theorem singleton_union : {a} βˆͺ s = insert a s := rfl @[simp] theorem union_singleton : s βˆͺ {a} = insert a s := union_comm _ _ @[simp] theorem singleton_inter_nonempty : ({a} ∩ s).nonempty ↔ a ∈ s := by simp only [set.nonempty, mem_inter_iff, mem_singleton_iff, exists_eq_left] @[simp] theorem inter_singleton_nonempty : (s ∩ {a}).nonempty ↔ a ∈ s := by rw [inter_comm, singleton_inter_nonempty] @[simp] theorem singleton_inter_eq_empty : {a} ∩ s = βˆ… ↔ a βˆ‰ s := not_nonempty_iff_eq_empty.symm.trans singleton_inter_nonempty.not @[simp] theorem inter_singleton_eq_empty : s ∩ {a} = βˆ… ↔ a βˆ‰ s := by rw [inter_comm, singleton_inter_eq_empty] lemma nmem_singleton_empty {s : set Ξ±} : s βˆ‰ ({βˆ…} : set (set Ξ±)) ↔ s.nonempty := nonempty_iff_ne_empty.symm instance unique_singleton (a : Ξ±) : unique β†₯({a} : set Ξ±) := ⟨⟨⟨a, mem_singleton a⟩⟩, Ξ» ⟨x, h⟩, subtype.eq h⟩ lemma eq_singleton_iff_unique_mem : s = {a} ↔ a ∈ s ∧ βˆ€ x ∈ s, x = a := subset.antisymm_iff.trans $ and.comm.trans $ and_congr_left' singleton_subset_iff lemma eq_singleton_iff_nonempty_unique_mem : s = {a} ↔ s.nonempty ∧ βˆ€ x ∈ s, x = a := eq_singleton_iff_unique_mem.trans $ and_congr_left $ Ξ» H, ⟨λ h', ⟨_, h'⟩, Ξ» ⟨x, h⟩, H x h β–Έ h⟩ -- while `simp` is capable of proving this, it is not capable of turning the LHS into the RHS. @[simp] lemma default_coe_singleton (x : Ξ±) : (default : ({x} : set Ξ±)) = ⟨x, rfl⟩ := rfl /-! ### Lemmas about pairs -/ @[simp] theorem pair_eq_singleton (a : Ξ±) : ({a, a} : set Ξ±) = {a} := union_self _ theorem pair_comm (a b : Ξ±) : ({a, b} : set Ξ±) = {b, a} := union_comm _ _ lemma pair_eq_pair_iff {x y z w : Ξ±} : ({x, y} : set Ξ±) = {z, w} ↔ x = z ∧ y = w ∨ x = w ∧ y = z := begin simp only [set.subset.antisymm_iff, set.insert_subset, set.mem_insert_iff, set.mem_singleton_iff, set.singleton_subset_iff], split, { tauto! }, { rintro (⟨rfl,rfl⟩|⟨rfl,rfl⟩); simp } end /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ section sep variables {p q : Ξ± β†’ Prop} {x : Ξ±} theorem mem_sep (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem sep_mem_eq : {x ∈ s | x ∈ t} = s ∩ t := rfl @[simp] theorem mem_sep_iff : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem sep_ext_iff : {x ∈ s | p x} = {x ∈ s | q x} ↔ βˆ€ x ∈ s, (p x ↔ q x) := by simp_rw [ext_iff, mem_sep_iff, and.congr_right_iff] theorem sep_eq_of_subset (h : s βŠ† t) : {x ∈ t | x ∈ s} = s := inter_eq_self_of_subset_right h @[simp] theorem sep_subset (s : set Ξ±) (p : Ξ± β†’ Prop) : {x ∈ s | p x} βŠ† s := Ξ» x, and.left @[simp] lemma sep_eq_self_iff_mem_true : {x ∈ s | p x} = s ↔ βˆ€ x ∈ s, p x := by simp_rw [ext_iff, mem_sep_iff, and_iff_left_iff_imp] @[simp] lemma sep_eq_empty_iff_mem_false : {x ∈ s | p x} = βˆ… ↔ βˆ€ x ∈ s, Β¬ p x := by simp_rw [ext_iff, mem_sep_iff, mem_empty_iff_false, iff_false, not_and] @[simp] lemma sep_true : {x ∈ s | true} = s := inter_univ s @[simp] lemma sep_false : {x ∈ s | false} = βˆ… := inter_empty s @[simp] lemma sep_empty (p : Ξ± β†’ Prop) : {x ∈ (βˆ… : set Ξ±) | p x} = βˆ… := empty_inter p @[simp] lemma sep_univ : {x ∈ (univ : set Ξ±) | p x} = {x | p x} := univ_inter p @[simp] lemma sep_union : {x ∈ s βˆͺ t | p x} = {x ∈ s | p x} βˆͺ {x ∈ t | p x} := union_inter_distrib_right @[simp] lemma sep_inter : {x ∈ s ∩ t | p x} = {x ∈ s | p x} ∩ {x ∈ t | p x} := inter_inter_distrib_right s t p @[simp] lemma sep_and : {x ∈ s | p x ∧ q x} = {x ∈ s | p x} ∩ {x ∈ s | q x} := inter_inter_distrib_left s p q @[simp] lemma sep_or : {x ∈ s | p x ∨ q x} = {x ∈ s | p x} βˆͺ {x ∈ s | q x} := inter_union_distrib_left @[simp] lemma sep_set_of : {x ∈ {y | p y} | q x} = {x | p x ∧ q x} := rfl end sep @[simp] lemma subset_singleton_iff {Ξ± : Type*} {s : set Ξ±} {x : Ξ±} : s βŠ† {x} ↔ βˆ€ y ∈ s, y = x := iff.rfl lemma subset_singleton_iff_eq {s : set Ξ±} {x : Ξ±} : s βŠ† {x} ↔ s = βˆ… ∨ s = {x} := begin obtain (rfl | hs) := s.eq_empty_or_nonempty, use ⟨λ _, or.inl rfl, Ξ» _, empty_subset _⟩, simp [eq_singleton_iff_nonempty_unique_mem, hs, hs.ne_empty], end lemma nonempty.subset_singleton_iff (h : s.nonempty) : s βŠ† {a} ↔ s = {a} := subset_singleton_iff_eq.trans $ or_iff_right h.ne_empty lemma ssubset_singleton_iff {s : set Ξ±} {x : Ξ±} : s βŠ‚ {x} ↔ s = βˆ… := begin rw [ssubset_iff_subset_ne, subset_singleton_iff_eq, or_and_distrib_right, and_not_self, or_false, and_iff_left_iff_imp], exact Ξ» h, ne_of_eq_of_ne h (singleton_ne_empty _).symm, end lemma eq_empty_of_ssubset_singleton {s : set Ξ±} {x : Ξ±} (hs : s βŠ‚ {x}) : s = βˆ… := ssubset_singleton_iff.1 hs /-! ### Disjointness -/ protected theorem disjoint_iff : disjoint s t ↔ s ∩ t βŠ† βˆ… := disjoint_iff_inf_le theorem disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = βˆ… := disjoint_iff lemma _root_.disjoint.inter_eq : disjoint s t β†’ s ∩ t = βˆ… := disjoint.eq_bot lemma disjoint_left : disjoint s t ↔ βˆ€ ⦃a⦄, a ∈ s β†’ a βˆ‰ t := disjoint_iff_inf_le.trans $ forall_congr $ Ξ» _, not_and lemma disjoint_right : disjoint s t ↔ βˆ€ ⦃a⦄, a ∈ t β†’ a βˆ‰ s := by rw [disjoint.comm, disjoint_left] lemma not_disjoint_iff : Β¬disjoint s t ↔ βˆƒ x, x ∈ s ∧ x ∈ t := set.disjoint_iff.not.trans $ not_forall.trans $ exists_congr $ Ξ» x, not_not lemma not_disjoint_iff_nonempty_inter : Β¬disjoint s t ↔ (s ∩ t).nonempty := not_disjoint_iff alias not_disjoint_iff_nonempty_inter ↔ _ nonempty.not_disjoint lemma disjoint_or_nonempty_inter (s t : set Ξ±) : disjoint s t ∨ (s ∩ t).nonempty := (em _).imp_right not_disjoint_iff_nonempty_inter.mp lemma disjoint_iff_forall_ne : disjoint s t ↔ βˆ€ (x ∈ s) (y ∈ t), x β‰  y := by simp only [ne.def, disjoint_left, @imp_not_comm _ (_ = _), forall_eq'] lemma _root_.disjoint.ne_of_mem (h : disjoint s t) {x y} (hx : x ∈ s) (hy : y ∈ t) : x β‰  y := disjoint_iff_forall_ne.mp h x hx y hy lemma disjoint_of_subset_left (hs : s₁ βŠ† sβ‚‚) (h : disjoint sβ‚‚ t) : disjoint s₁ t := h.mono_left hs lemma disjoint_of_subset_right (ht : t₁ βŠ† tβ‚‚) (h : disjoint s tβ‚‚) : disjoint s t₁ := h.mono_right ht lemma disjoint_of_subset (hs : s₁ βŠ† sβ‚‚) (ht : t₁ βŠ† tβ‚‚) (h : disjoint sβ‚‚ tβ‚‚) : disjoint s₁ t₁ := h.mono hs ht @[simp] lemma disjoint_union_left : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := disjoint_sup_left @[simp] lemma disjoint_union_right : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := disjoint_sup_right @[simp] lemma disjoint_empty (s : set Ξ±) : disjoint s βˆ… := disjoint_bot_right @[simp] lemma empty_disjoint (s : set Ξ±) : disjoint βˆ… s := disjoint_bot_left @[simp] lemma univ_disjoint : disjoint univ s ↔ s = βˆ… := top_disjoint @[simp] lemma disjoint_univ : disjoint s univ ↔ s = βˆ… := disjoint_top lemma disjoint_sdiff_left : disjoint (t \ s) s := disjoint_sdiff_self_left lemma disjoint_sdiff_right : disjoint s (t \ s) := disjoint_sdiff_self_right lemma diff_union_diff_cancel (hts : t βŠ† s) (hut : u βŠ† t) : s \ t βˆͺ t \ u = s \ u := sdiff_sup_sdiff_cancel hts hut lemma diff_diff_eq_sdiff_union (h : u βŠ† s) : s \ (t \ u) = s \ t βˆͺ u := sdiff_sdiff_eq_sdiff_sup h @[simp] lemma disjoint_singleton_left : disjoint {a} s ↔ a βˆ‰ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl @[simp] lemma disjoint_singleton_right : disjoint s {a} ↔ a βˆ‰ s := disjoint.comm.trans disjoint_singleton_left @[simp] lemma disjoint_singleton : disjoint ({a} : set Ξ±) {b} ↔ a β‰  b := by rw [disjoint_singleton_left, mem_singleton_iff] lemma subset_diff : s βŠ† t \ u ↔ s βŠ† t ∧ disjoint s u := le_iff_subset.symm.trans le_sdiff lemma inter_diff_distrib_left (s t u : set Ξ±) : s ∩ (t \ u) = (s ∩ t) \ (s ∩ u) := inf_sdiff_distrib_left _ _ _ lemma inter_diff_distrib_right (s t u : set Ξ±) : s \ t ∩ u = (s ∩ u) \ (t ∩ u) := inf_sdiff_distrib_right _ _ _ /-! ### Lemmas about complement -/ lemma compl_def (s : set Ξ±) : sᢜ = {x | x βˆ‰ s} := rfl 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_iff (s : set Ξ±) (x : Ξ±) : x ∈ sᢜ ↔ (x βˆ‰ s) := iff.rfl lemma not_mem_compl_iff {x : Ξ±} : x βˆ‰ sᢜ ↔ x ∈ s := not_not @[simp] theorem inter_compl_self (s : set Ξ±) : s ∩ sᢜ = βˆ… := inf_compl_eq_bot @[simp] theorem compl_inter_self (s : set Ξ±) : sᢜ ∩ s = βˆ… := compl_inf_eq_bot @[simp] theorem compl_empty : (βˆ… : set Ξ±)ᢜ = univ := compl_bot @[simp] theorem compl_union (s t : set Ξ±) : (s βˆͺ t)ᢜ = sᢜ ∩ tᢜ := compl_sup theorem compl_inter (s t : set Ξ±) : (s ∩ t)ᢜ = sᢜ βˆͺ tᢜ := compl_inf @[simp] theorem compl_univ : (univ : set Ξ±)ᢜ = βˆ… := compl_top @[simp] lemma compl_empty_iff {s : set Ξ±} : sᢜ = βˆ… ↔ s = univ := compl_eq_bot @[simp] lemma compl_univ_iff {s : set Ξ±} : sᢜ = univ ↔ s = βˆ… := compl_eq_top lemma compl_ne_univ : sᢜ β‰  univ ↔ s.nonempty := compl_univ_iff.not.trans nonempty_iff_ne_empty.symm lemma nonempty_compl : sᢜ.nonempty ↔ s β‰  univ := (ne_univ_iff_exists_not_mem s).symm lemma mem_compl_singleton_iff {a x : Ξ±} : x ∈ ({a} : set Ξ±)ᢜ ↔ x β‰  a := iff.rfl lemma compl_singleton_eq (a : Ξ±) : ({a} : set Ξ±)ᢜ = {x | x β‰  a} := rfl @[simp] lemma compl_ne_eq_singleton (a : Ξ±) : ({x | x β‰  a} : set Ξ±)ᢜ = {a} := compl_compl _ theorem union_eq_compl_compl_inter_compl (s t : set Ξ±) : s βˆͺ t = (sᢜ ∩ tᢜ)ᢜ := ext $ Ξ» x, or_iff_not_and_not theorem inter_eq_compl_compl_union_compl (s t : set Ξ±) : s ∩ t = (sᢜ βˆͺ tᢜ)ᢜ := ext $ Ξ» x, and_iff_not_or_not @[simp] theorem union_compl_self (s : set Ξ±) : s βˆͺ sᢜ = univ := eq_univ_iff_forall.2 $ Ξ» x, em _ @[simp] theorem compl_union_self (s : set Ξ±) : sᢜ βˆͺ s = univ := by rw [union_comm, union_compl_self] lemma compl_subset_comm : sᢜ βŠ† t ↔ tᢜ βŠ† s := @compl_le_iff_compl_le _ s _ _ lemma subset_compl_comm : s βŠ† tᢜ ↔ t βŠ† sᢜ := @le_compl_iff_le_compl _ _ _ t @[simp] lemma compl_subset_compl : sᢜ βŠ† tᢜ ↔ t βŠ† s := @compl_le_compl_iff_le (set Ξ±) _ _ _ lemma subset_compl_iff_disjoint_left : s βŠ† tᢜ ↔ disjoint t s := @le_compl_iff_disjoint_left (set Ξ±) _ _ _ lemma subset_compl_iff_disjoint_right : s βŠ† tᢜ ↔ disjoint s t := @le_compl_iff_disjoint_right (set Ξ±) _ _ _ lemma disjoint_compl_left_iff_subset : disjoint sᢜ t ↔ t βŠ† s := disjoint_compl_left_iff lemma disjoint_compl_right_iff_subset : disjoint s tᢜ ↔ s βŠ† t := disjoint_compl_right_iff alias subset_compl_iff_disjoint_right ↔ _ _root_.disjoint.subset_compl_right alias subset_compl_iff_disjoint_left ↔ _ _root_.disjoint.subset_compl_left alias disjoint_compl_left_iff_subset ↔ _ _root_.has_subset.subset.disjoint_compl_left alias disjoint_compl_right_iff_subset ↔ _ _root_.has_subset.subset.disjoint_compl_right theorem subset_union_compl_iff_inter_subset {s t u : set Ξ±} : s βŠ† t βˆͺ uᢜ ↔ s ∩ u βŠ† t := (@is_compl_compl _ u _).le_sup_right_iff_inf_left_le theorem compl_subset_iff_union {s t : set Ξ±} : sᢜ βŠ† t ↔ s βˆͺ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ Ξ» a, or_iff_not_imp_left @[simp] lemma subset_compl_singleton_iff {a : Ξ±} {s : set Ξ±} : s βŠ† {a}ᢜ ↔ a βˆ‰ s := subset_compl_comm.trans singleton_subset_iff theorem inter_subset (a b c : set Ξ±) : a ∩ b βŠ† c ↔ a βŠ† bᢜ βˆͺ c := forall_congr $ Ξ» x, and_imp.trans $ imp_congr_right $ Ξ» _, imp_iff_not_or lemma inter_compl_nonempty_iff {s t : set Ξ±} : (s ∩ tᢜ).nonempty ↔ Β¬ s βŠ† t := (not_subset.trans $ exists_congr $ by exact Ξ» x, by simp [mem_compl]).symm /-! ### Lemmas about 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⟩ lemma not_mem_diff_of_mem {s t : set Ξ±} {x : Ξ±} (hx : x ∈ t) : x βˆ‰ s \ t := Ξ» h, h.2 hx 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 diff_eq_compl_inter {s t : set Ξ±} : s \ t = tᢜ ∩ s := by rw [diff_eq, inter_comm] theorem nonempty_diff {s t : set Ξ±} : (s \ t).nonempty ↔ Β¬ (s βŠ† t) := inter_compl_nonempty_iff theorem diff_subset (s t : set Ξ±) : s \ t βŠ† s := show s \ t ≀ s, from sdiff_le theorem union_diff_cancel' {s t u : set Ξ±} (h₁ : s βŠ† t) (hβ‚‚ : t βŠ† u) : t βˆͺ (u \ s) = u := sup_sdiff_cancel' h₁ hβ‚‚ theorem union_diff_cancel {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ (t \ s) = t := sup_sdiff_cancel_right h theorem union_diff_cancel_left {s t : set Ξ±} (h : s ∩ t βŠ† βˆ…) : (s βˆͺ t) \ s = t := disjoint.sup_sdiff_cancel_left $ disjoint_iff_inf_le.2 h theorem union_diff_cancel_right {s t : set Ξ±} (h : s ∩ t βŠ† βˆ…) : (s βˆͺ t) \ t = s := disjoint.sup_sdiff_cancel_right $ disjoint_iff_inf_le.2 h @[simp] theorem union_diff_left {s t : set Ξ±} : (s βˆͺ t) \ s = t \ s := sup_sdiff_left_self @[simp] theorem union_diff_right {s t : set Ξ±} : (s βˆͺ t) \ t = s \ t := sup_sdiff_right_self theorem union_diff_distrib {s t u : set Ξ±} : (s βˆͺ t) \ u = s \ u βˆͺ t \ u := sup_sdiff theorem inter_diff_assoc (a b c : set Ξ±) : (a ∩ b) \ c = a ∩ (b \ c) := inf_sdiff_assoc @[simp] theorem inter_diff_self (a b : set Ξ±) : a ∩ (b \ a) = βˆ… := inf_sdiff_self_right @[simp] theorem inter_union_diff (s t : set Ξ±) : (s ∩ t) βˆͺ (s \ t) = s := sup_inf_sdiff s t @[simp] lemma diff_union_inter (s t : set Ξ±) : (s \ t) βˆͺ (s ∩ t) = s := by { rw union_comm, exact sup_inf_sdiff _ _ } @[simp] theorem inter_union_compl (s t : set Ξ±) : (s ∩ t) βˆͺ (s ∩ tᢜ) = s := inter_union_diff _ _ theorem diff_subset_diff {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} : s₁ βŠ† sβ‚‚ β†’ tβ‚‚ βŠ† t₁ β†’ s₁ \ t₁ βŠ† sβ‚‚ \ tβ‚‚ := show s₁ ≀ sβ‚‚ β†’ tβ‚‚ ≀ t₁ β†’ s₁ \ t₁ ≀ sβ‚‚ \ tβ‚‚, from sdiff_le_sdiff theorem diff_subset_diff_left {s₁ sβ‚‚ t : set Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ \ t βŠ† sβ‚‚ \ t := sdiff_le_sdiff_right β€Ήs₁ ≀ sβ‚‚β€Ί theorem diff_subset_diff_right {s t u : set Ξ±} (h : t βŠ† u) : s \ u βŠ† s \ t := sdiff_le_sdiff_left β€Ήt ≀ uβ€Ί theorem compl_eq_univ_diff (s : set Ξ±) : sᢜ = univ \ s := top_sdiff.symm @[simp] lemma empty_diff (s : set Ξ±) : (βˆ… \ s : set Ξ±) = βˆ… := bot_sdiff theorem diff_eq_empty {s t : set Ξ±} : s \ t = βˆ… ↔ s βŠ† t := sdiff_eq_bot_iff @[simp] theorem diff_empty {s : set Ξ±} : s \ βˆ… = s := sdiff_bot @[simp] lemma diff_univ (s : set Ξ±) : s \ univ = βˆ… := diff_eq_empty.2 (subset_univ s) theorem diff_diff {u : set Ξ±} : s \ t \ u = s \ (t βˆͺ u) := sdiff_sdiff_left -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set Ξ±} : (s \ t) \ u = (s \ u) \ t := sdiff_sdiff_comm lemma diff_subset_iff {s t u : set Ξ±} : s \ t βŠ† u ↔ s βŠ† t βˆͺ u := show s \ t ≀ u ↔ s ≀ t βˆͺ u, from sdiff_le_iff lemma subset_diff_union (s t : set Ξ±) : s βŠ† (s \ t) βˆͺ t := show s ≀ (s \ t) βˆͺ t, from le_sdiff_sup lemma diff_union_of_subset {s t : set Ξ±} (h : t βŠ† s) : (s \ t) βˆͺ t = s := subset.antisymm (union_subset (diff_subset _ _) h) (subset_diff_union _ _) @[simp] lemma diff_singleton_subset_iff {x : Ξ±} {s t : set Ξ±} : s \ {x} βŠ† t ↔ s βŠ† insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_diff_singleton {x : Ξ±} {s t : set Ξ±} (h : s βŠ† t) (hx : x βˆ‰ s) : s βŠ† t \ {x} := subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx lemma subset_insert_diff_singleton (x : Ξ±) (s : set Ξ±) : s βŠ† insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set Ξ±} : s \ t βŠ† u ↔ s \ u βŠ† t := show s \ t ≀ u ↔ s \ u ≀ t, from sdiff_le_comm lemma diff_inter {s t u : set Ξ±} : s \ (t ∩ u) = (s \ t) βˆͺ (s \ u) := sdiff_inf lemma diff_inter_diff {s t u : set Ξ±} : s \ t ∩ (s \ u) = s \ (t βˆͺ u) := sdiff_sup.symm lemma diff_compl : s \ tᢜ = s ∩ t := sdiff_compl lemma diff_diff_right {s t u : set Ξ±} : s \ (t \ u) = (s \ t) βˆͺ (s ∩ u) := sdiff_sdiff_right' @[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t := by { ext, split; simp [or_imp_distrib, h] {contextual := tt} } theorem insert_diff_of_not_mem (s) (h : a βˆ‰ t) : insert a s \ t = insert a (s \ t) := begin classical, ext x, by_cases h' : x ∈ t, { have : x β‰  a, { assume H, rw H at h', exact h h' }, simp [h, h', this] }, { simp [h, h'] } end lemma insert_diff_self_of_not_mem {a : Ξ±} {s : set Ξ±} (h : a βˆ‰ s) : insert a s \ {a} = s := by { ext, simp [and_iff_left_of_imp (Ξ» hx : x ∈ s, show x β‰  a, from Ξ» hxa, h $ hxa β–Έ hx)] } @[simp] lemma insert_diff_eq_singleton {a : Ξ±} {s : set Ξ±} (h : a βˆ‰ s) : insert a s \ s = {a} := begin ext, rw [set.mem_diff, set.mem_insert_iff, set.mem_singleton_iff, or_and_distrib_right, and_not_self, or_false, and_iff_left_iff_imp], rintro rfl, exact h, end lemma inter_insert_of_mem (h : a ∈ s) : s ∩ insert a t = insert a (s ∩ t) := by rw [insert_inter_distrib, insert_eq_of_mem h] lemma insert_inter_of_mem (h : a ∈ t) : insert a s ∩ t = insert a (s ∩ t) := by rw [insert_inter_distrib, insert_eq_of_mem h] lemma inter_insert_of_not_mem (h : a βˆ‰ s) : s ∩ insert a t = s ∩ t := ext $ Ξ» x, and_congr_right $ Ξ» hx, or_iff_right $ ne_of_mem_of_not_mem hx h lemma insert_inter_of_not_mem (h : a βˆ‰ t) : insert a s ∩ t = s ∩ t := ext $ Ξ» x, and_congr_left $ Ξ» hx, or_iff_right $ ne_of_mem_of_not_mem hx h @[simp] lemma union_diff_self {s t : set Ξ±} : s βˆͺ (t \ s) = s βˆͺ t := sup_sdiff_self _ _ @[simp] lemma diff_union_self {s t : set Ξ±} : (s \ t) βˆͺ t = s βˆͺ t := sdiff_sup_self _ _ @[simp] theorem diff_inter_self {a b : set Ξ±} : (b \ a) ∩ a = βˆ… := inf_sdiff_self_left @[simp] theorem diff_inter_self_eq_diff {s t : set Ξ±} : s \ (t ∩ s) = s \ t := sdiff_inf_self_right _ _ @[simp] theorem diff_self_inter {s t : set Ξ±} : s \ (s ∩ t) = s \ t := sdiff_inf_self_left _ _ @[simp] theorem diff_singleton_eq_self {a : Ξ±} {s : set Ξ±} (h : a βˆ‰ s) : s \ {a} = s := sdiff_eq_self_iff_disjoint.2 $ by simp [h] @[simp] lemma diff_singleton_ssubset {s : set Ξ±} {a : Ξ±} : s \ {a} βŠ‚ s ↔ a ∈ s := sdiff_le.lt_iff_ne.trans $ sdiff_eq_left.not.trans $ by simp @[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] lemma insert_diff_singleton_comm (hab : a β‰  b) (s : set Ξ±) : insert a (s \ {b}) = insert a s \ {b} := by simp_rw [←union_singleton, union_diff_distrib, diff_singleton_eq_self (mem_singleton_iff.not.2 hab.symm)] @[simp] lemma diff_self {s : set Ξ±} : s \ s = βˆ… := sdiff_self lemma diff_diff_right_self (s t : set Ξ±) : s \ (s \ t) = s ∩ t := sdiff_sdiff_right_self lemma diff_diff_cancel_left {s t : set Ξ±} (h : s βŠ† t) : t \ (t \ s) = s := sdiff_sdiff_eq_self h lemma mem_diff_singleton {x y : Ξ±} {s : set Ξ±} : x ∈ s \ {y} ↔ (x ∈ s ∧ x β‰  y) := iff.rfl lemma mem_diff_singleton_empty {t : set (set Ξ±)} : s ∈ t \ {βˆ…} ↔ s ∈ t ∧ s.nonempty := mem_diff_singleton.trans $ and_congr_right' nonempty_iff_ne_empty.symm lemma union_eq_diff_union_diff_union_inter (s t : set Ξ±) : s βˆͺ t = (s \ t) βˆͺ (t \ s) βˆͺ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf /-! ### Symmetric difference -/ lemma mem_symm_diff : a ∈ s βˆ† t ↔ a ∈ s ∧ a βˆ‰ t ∨ a ∈ t ∧ a βˆ‰ s := iff.rfl protected lemma symm_diff_def (s t : set Ξ±) : s βˆ† t = s \ t βˆͺ t \ s := rfl lemma symm_diff_subset_union : s βˆ† t βŠ† s βˆͺ t := @symm_diff_le_sup (set Ξ±) _ _ _ @[simp] lemma symm_diff_eq_empty : s βˆ† t = βˆ… ↔ s = t := symm_diff_eq_bot @[simp] lemma symm_diff_nonempty : (s βˆ† t).nonempty ↔ s β‰  t := nonempty_iff_ne_empty.trans symm_diff_eq_empty.not lemma inter_symm_diff_distrib_left (s t u : set Ξ±) : s ∩ t βˆ† u = (s ∩ t) βˆ† (s ∩ u) := inf_symm_diff_distrib_left _ _ _ lemma inter_symm_diff_distrib_right (s t u : set Ξ±) : s βˆ† t ∩ u = (s ∩ u) βˆ† (t ∩ u) := inf_symm_diff_distrib_right _ _ _ lemma subset_symm_diff_union_symm_diff_left (h : disjoint s t) : u βŠ† s βˆ† u βˆͺ t βˆ† u := h.le_symm_diff_sup_symm_diff_left lemma subset_symm_diff_union_symm_diff_right (h : disjoint t u) : s βŠ† s βˆ† t βˆͺ s βˆ† u := h.le_symm_diff_sup_symm_diff_right /-! ### Powerset -/ /-- `𝒫 s = set.powerset s` is the set of all subsets of `s`. -/ def powerset (s : set Ξ±) : set (set Ξ±) := {t | t βŠ† s} prefix `𝒫`:100 := powerset theorem mem_powerset {x s : set Ξ±} (h : x βŠ† s) : x ∈ 𝒫 s := h theorem subset_of_mem_powerset {x s : set Ξ±} (h : x ∈ 𝒫 s) : x βŠ† s := h @[simp] theorem mem_powerset_iff (x s : set Ξ±) : x ∈ 𝒫 s ↔ x βŠ† s := iff.rfl theorem powerset_inter (s t : set Ξ±) : 𝒫 (s ∩ t) = 𝒫 s ∩ 𝒫 t := ext $ Ξ» u, subset_inter_iff @[simp] theorem powerset_mono : 𝒫 s βŠ† 𝒫 t ↔ s βŠ† t := ⟨λ h, h (subset.refl s), Ξ» h u hu, subset.trans hu h⟩ theorem monotone_powerset : monotone (powerset : set Ξ± β†’ set (set Ξ±)) := Ξ» s t, powerset_mono.2 @[simp] theorem powerset_nonempty : (𝒫 s).nonempty := βŸ¨βˆ…, empty_subset s⟩ @[simp] theorem powerset_empty : 𝒫 (βˆ… : set Ξ±) = {βˆ…} := ext $ Ξ» s, subset_empty_iff @[simp] theorem powerset_univ : 𝒫 (univ : set Ξ±) = univ := eq_univ_of_forall subset_univ /-- The powerset of a singleton contains only `βˆ…` and the singleton itself. -/ theorem powerset_singleton (x : Ξ±) : 𝒫 ({x} : set Ξ±) = {βˆ…, {x}} := by { ext y, rw [mem_powerset_iff, subset_singleton_iff_eq, mem_insert_iff, mem_singleton_iff] } /-! ### Sets defined as an if-then-else -/ lemma mem_dite_univ_right (p : Prop) [decidable p] (t : p β†’ set Ξ±) (x : Ξ±) : (x ∈ if h : p then t h else univ) ↔ (βˆ€ h : p, x ∈ t h) := by split_ifs; simp [h] @[simp] lemma mem_ite_univ_right (p : Prop) [decidable p] (t : set Ξ±) (x : Ξ±) : x ∈ ite p t set.univ ↔ (p β†’ x ∈ t) := mem_dite_univ_right p (Ξ» _, t) x lemma mem_dite_univ_left (p : Prop) [decidable p] (t : Β¬ p β†’ set Ξ±) (x : Ξ±) : (x ∈ if h : p then univ else t h) ↔ (βˆ€ h : Β¬ p, x ∈ t h) := by split_ifs; simp [h] @[simp] lemma mem_ite_univ_left (p : Prop) [decidable p] (t : set Ξ±) (x : Ξ±) : x ∈ ite p set.univ t ↔ (Β¬ p β†’ x ∈ t) := mem_dite_univ_left p (Ξ» _, t) x lemma mem_dite_empty_right (p : Prop) [decidable p] (t : p β†’ set Ξ±) (x : Ξ±) : (x ∈ if h : p then t h else βˆ…) ↔ (βˆƒ h : p, x ∈ t h) := by split_ifs; simp [h] @[simp] lemma mem_ite_empty_right (p : Prop) [decidable p] (t : set Ξ±) (x : Ξ±) : x ∈ ite p t βˆ… ↔ p ∧ x ∈ t := by split_ifs; simp [h] lemma mem_dite_empty_left (p : Prop) [decidable p] (t : Β¬ p β†’ set Ξ±) (x : Ξ±) : (x ∈ if h : p then βˆ… else t h) ↔ (βˆƒ h : Β¬ p, x ∈ t h) := by split_ifs; simp [h] @[simp] lemma mem_ite_empty_left (p : Prop) [decidable p] (t : set Ξ±) (x : Ξ±) : x ∈ ite p βˆ… t ↔ Β¬ p ∧ x ∈ t := by split_ifs; simp [h] /-! ### If-then-else for sets -/ /-- `ite` for sets: `set.ite t s s' ∩ t = s ∩ t`, `set.ite t s s' ∩ tᢜ = s' ∩ tᢜ`. Defined as `s ∩ t βˆͺ s' \ t`. -/ protected def ite (t s s' : set Ξ±) : set Ξ± := s ∩ t βˆͺ s' \ t @[simp] lemma ite_inter_self (t s s' : set Ξ±) : t.ite s s' ∩ t = s ∩ t := by rw [set.ite, union_inter_distrib_right, diff_inter_self, inter_assoc, inter_self, union_empty] @[simp] lemma ite_compl (t s s' : set Ξ±) : tᢜ.ite s s' = t.ite s' s := by rw [set.ite, set.ite, diff_compl, union_comm, diff_eq] @[simp] lemma ite_inter_compl_self (t s s' : set Ξ±) : t.ite s s' ∩ tᢜ = s' ∩ tᢜ := by rw [← ite_compl, ite_inter_self] @[simp] lemma ite_diff_self (t s s' : set Ξ±) : t.ite s s' \ t = s' \ t := ite_inter_compl_self t s s' @[simp] lemma ite_same (t s : set Ξ±) : t.ite s s = s := inter_union_diff _ _ @[simp] lemma ite_left (s t : set Ξ±) : s.ite s t = s βˆͺ t := by simp [set.ite] @[simp] lemma ite_right (s t : set Ξ±) : s.ite t s = t ∩ s := by simp [set.ite] @[simp] lemma ite_empty (s s' : set Ξ±) : set.ite βˆ… s s' = s' := by simp [set.ite] @[simp] lemma ite_univ (s s' : set Ξ±) : set.ite univ s s' = s := by simp [set.ite] @[simp] lemma ite_empty_left (t s : set Ξ±) : t.ite βˆ… s = s \ t := by simp [set.ite] @[simp] lemma ite_empty_right (t s : set Ξ±) : t.ite s βˆ… = s ∩ t := by simp [set.ite] lemma ite_mono (t : set Ξ±) {s₁ s₁' sβ‚‚ sβ‚‚' : set Ξ±} (h : s₁ βŠ† sβ‚‚) (h' : s₁' βŠ† sβ‚‚') : t.ite s₁ s₁' βŠ† t.ite sβ‚‚ sβ‚‚' := union_subset_union (inter_subset_inter_left _ h) (inter_subset_inter_left _ h') lemma ite_subset_union (t s s' : set Ξ±) : t.ite s s' βŠ† s βˆͺ s' := union_subset_union (inter_subset_left _ _) (diff_subset _ _) lemma inter_subset_ite (t s s' : set Ξ±) : s ∩ s' βŠ† t.ite s s' := ite_same t (s ∩ s') β–Έ ite_mono _ (inter_subset_left _ _) (inter_subset_right _ _) lemma ite_inter_inter (t s₁ sβ‚‚ s₁' sβ‚‚' : set Ξ±) : t.ite (s₁ ∩ sβ‚‚) (s₁' ∩ sβ‚‚') = t.ite s₁ s₁' ∩ t.ite sβ‚‚ sβ‚‚' := by { ext x, simp only [set.ite, set.mem_inter_iff, set.mem_diff, set.mem_union], itauto } lemma ite_inter (t s₁ sβ‚‚ s : set Ξ±) : t.ite (s₁ ∩ s) (sβ‚‚ ∩ s) = t.ite s₁ sβ‚‚ ∩ s := by rw [ite_inter_inter, ite_same] lemma ite_inter_of_inter_eq (t : set Ξ±) {s₁ sβ‚‚ s : set Ξ±} (h : s₁ ∩ s = sβ‚‚ ∩ s) : t.ite s₁ sβ‚‚ ∩ s = s₁ ∩ s := by rw [← ite_inter, ← h, ite_same] lemma subset_ite {t s s' u : set Ξ±} : u βŠ† t.ite s s' ↔ u ∩ t βŠ† s ∧ u \ t βŠ† s' := begin simp only [subset_def, ← forall_and_distrib], refine forall_congr (Ξ» x, _), by_cases hx : x ∈ t; simp [*, set.ite] end /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton` if it has at most one element. -/ protected def subsingleton (s : set Ξ±) : Prop := βˆ€ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y lemma subsingleton.anti (ht : t.subsingleton) (hst : s βŠ† t) : s.subsingleton := Ξ» x hx y hy, ht (hst hx) (hst hy) lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:Ξ±} (hx : x ∈ s) : s = {x} := ext $ Ξ» y, ⟨λ hy, (hs hx hy) β–Έ mem_singleton _, Ξ» hy, (eq_of_mem_singleton hy).symm β–Έ hx⟩ @[simp] lemma subsingleton_empty : (βˆ… : set Ξ±).subsingleton := Ξ» x, false.elim @[simp] lemma subsingleton_singleton {a} : ({a} : set Ξ±).subsingleton := Ξ» x hx y hy, (eq_of_mem_singleton hx).symm β–Έ (eq_of_mem_singleton hy).symm β–Έ rfl lemma subsingleton_of_subset_singleton (h : s βŠ† {a}) : s.subsingleton := subsingleton_singleton.anti h lemma subsingleton_of_forall_eq (a : Ξ±) (h : βˆ€ b ∈ s, b = a) : s.subsingleton := Ξ» b hb c hc, (h _ hb).trans (h _ hc).symm lemma subsingleton_iff_singleton {x} (hx : x ∈ s) : s.subsingleton ↔ s = {x} := ⟨λ h, h.eq_singleton_of_mem hx, Ξ» h,h.symm β–Έ subsingleton_singleton⟩ lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) : s = βˆ… ∨ βˆƒ x, s = {x} := s.eq_empty_or_nonempty.elim or.inl (Ξ» ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩) lemma subsingleton.induction_on {p : set Ξ± β†’ Prop} (hs : s.subsingleton) (he : p βˆ…) (h₁ : βˆ€ x, p {x}) : p s := by { rcases hs.eq_empty_or_singleton with rfl|⟨x, rfl⟩, exacts [he, h₁ _] } lemma subsingleton_univ [subsingleton Ξ±] : (univ : set Ξ±).subsingleton := Ξ» x hx y hy, subsingleton.elim x y lemma subsingleton_of_univ_subsingleton (h : (univ : set Ξ±).subsingleton) : subsingleton Ξ± := ⟨λ a b, h (mem_univ a) (mem_univ b)⟩ @[simp] lemma subsingleton_univ_iff : (univ : set Ξ±).subsingleton ↔ subsingleton Ξ± := ⟨subsingleton_of_univ_subsingleton, Ξ» h, @subsingleton_univ _ h⟩ lemma subsingleton_of_subsingleton [subsingleton Ξ±] {s : set Ξ±} : set.subsingleton s := subsingleton_univ.anti (subset_univ s) lemma subsingleton_is_top (Ξ± : Type*) [partial_order Ξ±] : set.subsingleton {x : Ξ± | is_top x} := Ξ» x hx y hy, hx.is_max.eq_of_le (hy x) lemma subsingleton_is_bot (Ξ± : Type*) [partial_order Ξ±] : set.subsingleton {x : Ξ± | is_bot x} := Ξ» x hx y hy, hx.is_min.eq_of_ge (hy x) lemma exists_eq_singleton_iff_nonempty_subsingleton : (βˆƒ a : Ξ±, s = {a}) ↔ s.nonempty ∧ s.subsingleton := begin refine ⟨_, Ξ» h, _⟩, { rintros ⟨a, rfl⟩, exact ⟨singleton_nonempty a, subsingleton_singleton⟩ }, { exact h.2.eq_empty_or_singleton.resolve_left h.1.ne_empty }, end /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp, norm_cast] lemma subsingleton_coe (s : set Ξ±) : subsingleton s ↔ s.subsingleton := begin split, { refine Ξ» h, (Ξ» a ha b hb, _), exact set_coe.ext_iff.2 (@subsingleton.elim s h ⟨a, ha⟩ ⟨b, hb⟩) }, { exact Ξ» h, subsingleton.intro (Ξ» a b, set_coe.ext (h a.property b.property)) } end lemma subsingleton.coe_sort {s : set Ξ±} : s.subsingleton β†’ subsingleton s := s.subsingleton_coe.2 /-- The `coe_sort` of a set `s` in a subsingleton type is a subsingleton. For the corresponding result for `subtype`, see `subtype.subsingleton`. -/ instance subsingleton_coe_of_subsingleton [subsingleton Ξ±] {s : set Ξ±} : subsingleton s := by { rw [s.subsingleton_coe], exact subsingleton_of_subsingleton } /-! ### Nontrivial -/ /-- A set `s` is `nontrivial` if it has at least two distinct elements. -/ protected def nontrivial (s : set Ξ±) : Prop := βˆƒ x y ∈ s, x β‰  y lemma nontrivial_of_mem_mem_ne {x y} (hx : x ∈ s) (hy : y ∈ s) (hxy : x β‰  y) : s.nontrivial := ⟨x, hx, y, hy, hxy⟩ /-- Extract witnesses from s.nontrivial. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the classical.choice axiom. -/ protected noncomputable def nontrivial.some (hs : s.nontrivial) : Ξ± Γ— Ξ± := (hs.some, hs.some_spec.some_spec.some) protected lemma nontrivial.some_fst_mem (hs : s.nontrivial) : hs.some.fst ∈ s := hs.some_spec.some protected lemma nontrivial.some_snd_mem (hs : s.nontrivial) : hs.some.snd ∈ s := hs.some_spec.some_spec.some_spec.some protected lemma nontrivial.some_fst_ne_some_snd (hs : s.nontrivial) : hs.some.fst β‰  hs.some.snd := hs.some_spec.some_spec.some_spec.some_spec lemma nontrivial.mono (hs : s.nontrivial) (hst : s βŠ† t) : t.nontrivial := let ⟨x, hx, y, hy, hxy⟩ := hs in ⟨x, hst hx, y, hst hy, hxy⟩ lemma nontrivial_pair {x y} (hxy : x β‰  y) : ({x, y} : set Ξ±).nontrivial := ⟨x, mem_insert _ _, y, mem_insert_of_mem _ (mem_singleton _), hxy⟩ lemma nontrivial_of_pair_subset {x y} (hxy : x β‰  y) (h : {x, y} βŠ† s) : s.nontrivial := (nontrivial_pair hxy).mono h lemma nontrivial.pair_subset (hs : s.nontrivial) : βˆƒ x y (hab : x β‰  y), {x, y} βŠ† s := let ⟨x, hx, y, hy, hxy⟩ := hs in ⟨x, y, hxy, insert_subset.2 ⟨hx, (singleton_subset_iff.2 hy)⟩⟩ lemma nontrivial_iff_pair_subset : s.nontrivial ↔ βˆƒ x y (hxy : x β‰  y), {x, y} βŠ† s := ⟨nontrivial.pair_subset, Ξ» H, let ⟨x, y, hxy, h⟩ := H in nontrivial_of_pair_subset hxy h⟩ lemma nontrivial_of_exists_ne {x} (hx : x ∈ s) (h : βˆƒ y ∈ s, y β‰  x) : s.nontrivial := let ⟨y, hy, hyx⟩ := h in ⟨y, hy, x, hx, hyx⟩ lemma nontrivial.exists_ne (hs : s.nontrivial) (z) : βˆƒ x ∈ s, x β‰  z := begin by_contra H, push_neg at H, rcases hs with ⟨x, hx, y, hy, hxy⟩, rw [H x hx, H y hy] at hxy, exact hxy rfl end lemma nontrivial_iff_exists_ne {x} (hx : x ∈ s) : s.nontrivial ↔ βˆƒ y ∈ s, y β‰  x := ⟨λ H, H.exists_ne _, nontrivial_of_exists_ne hx⟩ lemma nontrivial_of_lt [preorder Ξ±] {x y} (hx : x ∈ s) (hy : y ∈ s) (hxy : x < y) : s.nontrivial := ⟨x, hx, y, hy, ne_of_lt hxy⟩ lemma nontrivial_of_exists_lt [preorder Ξ±] (H : βˆƒ x y ∈ s, x < y) : s.nontrivial := let ⟨x, hx, y, hy, hxy⟩ := H in nontrivial_of_lt hx hy hxy lemma nontrivial.exists_lt [linear_order Ξ±] (hs : s.nontrivial) : βˆƒ x y ∈ s, x < y := let ⟨x, hx, y, hy, hxy⟩ := hs in or.elim (lt_or_gt_of_ne hxy) (Ξ» H, ⟨x, hx, y, hy, H⟩) (Ξ» H, ⟨y, hy, x, hx, H⟩) lemma nontrivial_iff_exists_lt [linear_order Ξ±] : s.nontrivial ↔ βˆƒ x y ∈ s, x < y := ⟨nontrivial.exists_lt, nontrivial_of_exists_lt⟩ protected lemma nontrivial.nonempty (hs : s.nontrivial) : s.nonempty := let ⟨x, hx, _⟩ := hs in ⟨x, hx⟩ protected lemma nontrivial.ne_empty (hs : s.nontrivial) : s β‰  βˆ… := hs.nonempty.ne_empty lemma nontrivial.not_subset_empty (hs : s.nontrivial) : Β¬ s βŠ† βˆ… := hs.nonempty.not_subset_empty @[simp] lemma not_nontrivial_empty : Β¬ (βˆ… : set Ξ±).nontrivial := Ξ» h, h.ne_empty rfl @[simp] lemma not_nontrivial_singleton {x} : Β¬ ({x} : set Ξ±).nontrivial := Ξ» H, begin rw nontrivial_iff_exists_ne (mem_singleton x) at H, exact let ⟨y, hy, hya⟩ := H in hya (mem_singleton_iff.1 hy) end lemma nontrivial.ne_singleton {x} (hs : s.nontrivial) : s β‰  {x} := Ξ» H, by { rw H at hs, exact not_nontrivial_singleton hs } lemma nontrivial.not_subset_singleton {x} (hs : s.nontrivial) : Β¬ s βŠ† {x} := (not_congr subset_singleton_iff_eq).2 (not_or hs.ne_empty hs.ne_singleton) lemma nontrivial_univ [nontrivial Ξ±] : (univ : set Ξ±).nontrivial := let ⟨x, y, hxy⟩ := exists_pair_ne Ξ± in ⟨x, mem_univ _, y, mem_univ _, hxy⟩ lemma nontrivial_of_univ_nontrivial (h : (univ : set Ξ±).nontrivial) : nontrivial Ξ± := let ⟨x, _, y, _, hxy⟩ := h in ⟨⟨x, y, hxy⟩⟩ @[simp] lemma nontrivial_univ_iff : (univ : set Ξ±).nontrivial ↔ nontrivial Ξ± := ⟨nontrivial_of_univ_nontrivial, Ξ» h, @nontrivial_univ _ h⟩ lemma nontrivial_of_nontrivial (hs : s.nontrivial) : nontrivial Ξ± := let ⟨x, _, y, _, hxy⟩ := hs in ⟨⟨x, y, hxy⟩⟩ /-- `s`, coerced to a type, is a nontrivial type if and only if `s` is a nontrivial set. -/ @[simp, norm_cast] lemma nontrivial_coe_sort {s : set Ξ±} : nontrivial s ↔ s.nontrivial := by simp_rw [← nontrivial_univ_iff, set.nontrivial, mem_univ, exists_true_left, set_coe.exists, subtype.mk_eq_mk] alias nontrivial_coe_sort ↔ _ nontrivial.coe_sort /-- A type with a set `s` whose `coe_sort` is a nontrivial type is nontrivial. For the corresponding result for `subtype`, see `subtype.nontrivial_iff_exists_ne`. -/ lemma nontrivial_of_nontrivial_coe (hs : nontrivial s) : nontrivial Ξ± := nontrivial_of_nontrivial $ nontrivial_coe_sort.1 hs theorem nontrivial_mono {Ξ± : Type*} {s t : set Ξ±} (hst : s βŠ† t) (hs : nontrivial s) : nontrivial t := nontrivial.coe_sort $ (nontrivial_coe_sort.1 hs).mono hst @[simp] lemma not_subsingleton_iff : Β¬ s.subsingleton ↔ s.nontrivial := by simp_rw [set.subsingleton, set.nontrivial, not_forall] @[simp] lemma not_nontrivial_iff : Β¬ s.nontrivial ↔ s.subsingleton := iff.not_left not_subsingleton_iff.symm alias not_nontrivial_iff ↔ _ subsingleton.not_nontrivial alias not_subsingleton_iff ↔ _ nontrivial.not_subsingleton protected lemma subsingleton_or_nontrivial (s : set Ξ±) : s.subsingleton ∨ s.nontrivial := by simp [or_iff_not_imp_right] lemma eq_singleton_or_nontrivial (ha : a ∈ s) : s = {a} ∨ s.nontrivial := by { rw ←subsingleton_iff_singleton ha, exact s.subsingleton_or_nontrivial } lemma nontrivial_iff_ne_singleton (ha : a ∈ s) : s.nontrivial ↔ s β‰  {a} := ⟨nontrivial.ne_singleton, (eq_singleton_or_nontrivial ha).resolve_left⟩ lemma nonempty.exists_eq_singleton_or_nontrivial : s.nonempty β†’ (βˆƒ a, s = {a}) ∨ s.nontrivial := Ξ» ⟨a, ha⟩, (eq_singleton_or_nontrivial ha).imp_left $ exists.intro a theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) section preorder variables [preorder Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} lemma monotone_on_iff_monotone : monotone_on f s ↔ monotone (Ξ» a : s, f a) := by simp [monotone, monotone_on] lemma antitone_on_iff_antitone : antitone_on f s ↔ antitone (Ξ» a : s, f a) := by simp [antitone, antitone_on] lemma strict_mono_on_iff_strict_mono : strict_mono_on f s ↔ strict_mono (Ξ» a : s, f a) := by simp [strict_mono, strict_mono_on] lemma strict_anti_on_iff_strict_anti : strict_anti_on f s ↔ strict_anti (Ξ» a : s, f a) := by simp [strict_anti, strict_anti_on] variables (f) /-! ### Monotonicity on singletons -/ protected lemma subsingleton.monotone_on (h : s.subsingleton) : monotone_on f s := Ξ» a ha b hb _, (congr_arg _ (h ha hb)).le protected lemma subsingleton.antitone_on (h : s.subsingleton) : antitone_on f s := Ξ» a ha b hb _, (congr_arg _ (h hb ha)).le protected lemma subsingleton.strict_mono_on (h : s.subsingleton) : strict_mono_on f s := Ξ» a ha b hb hlt, (hlt.ne (h ha hb)).elim protected lemma subsingleton.strict_anti_on (h : s.subsingleton) : strict_anti_on f s := Ξ» a ha b hb hlt, (hlt.ne (h ha hb)).elim @[simp] lemma monotone_on_singleton : monotone_on f {a} := subsingleton_singleton.monotone_on f @[simp] lemma antitone_on_singleton : antitone_on f {a} := subsingleton_singleton.antitone_on f @[simp] lemma strict_mono_on_singleton : strict_mono_on f {a} := subsingleton_singleton.strict_mono_on f @[simp] lemma strict_anti_on_singleton : strict_anti_on f {a} := subsingleton_singleton.strict_anti_on f end preorder section linear_order variables [linear_order Ξ±] [linear_order Ξ²] {f : Ξ± β†’ Ξ²} /-- A function between linear orders which is neither monotone nor antitone makes a dent upright or downright. -/ lemma not_monotone_on_not_antitone_on_iff_exists_le_le : Β¬ monotone_on f s ∧ Β¬ antitone_on f s ↔ βˆƒ a b c ∈ s, a ≀ b ∧ b ≀ c ∧ (f a < f b ∧ f c < f b ∨ f b < f a ∧ f b < f c) := by simp [monotone_on_iff_monotone, antitone_on_iff_antitone, and_assoc, exists_and_distrib_left, not_monotone_not_antitone_iff_exists_le_le, @and.left_comm (_ ∈ s)] /-- A function between linear orders which is neither monotone nor antitone makes a dent upright or downright. -/ lemma not_monotone_on_not_antitone_on_iff_exists_lt_lt : Β¬ monotone_on f s ∧ Β¬ antitone_on f s ↔ βˆƒ a b c ∈ s, a < b ∧ b < c ∧ (f a < f b ∧ f c < f b ∨ f b < f a ∧ f b < f c) := by simp [monotone_on_iff_monotone, antitone_on_iff_antitone, and_assoc, exists_and_distrib_left, not_monotone_not_antitone_iff_exists_lt_lt, @and.left_comm (_ ∈ s)] end linear_order end set open set namespace function variables {ΞΉ : Sort*} {Ξ± : Type*} {Ξ² : Type*} {f : Ξ± β†’ Ξ²} lemma injective.nonempty_apply_iff {f : set Ξ± β†’ set Ξ²} (hf : injective f) (h2 : f βˆ… = βˆ…) {s : set Ξ±} : (f s).nonempty ↔ s.nonempty := by rw [nonempty_iff_ne_empty, ← h2, nonempty_iff_ne_empty, hf.ne_iff] end function open function namespace set /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `βŠ†` -/ section inclusion variables {Ξ± : Type*} {s t u : set Ξ±} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s βŠ† t` -/ def inclusion (h : s βŠ† t) : s β†’ t := Ξ» x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self (x : s) : inclusion subset.rfl x = x := by { cases x, refl } lemma inclusion_eq_id (h : s βŠ† s) : inclusion h = id := funext inclusion_self @[simp] lemma inclusion_mk {h : s βŠ† t} (a : Ξ±) (ha : a ∈ s) : inclusion h ⟨a, ha⟩ = ⟨a, h ha⟩ := rfl lemma inclusion_right (h : s βŠ† t) (x : t) (m : (x : Ξ±) ∈ s) : inclusion h ⟨x, m⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion (hst : s βŠ† t) (htu : t βŠ† u) (x : s) : inclusion htu (inclusion hst x) = inclusion (hst.trans htu) x := by { cases x, refl } @[simp] lemma inclusion_comp_inclusion {Ξ±} {s t u : set Ξ±} (hst : s βŠ† t) (htu : t βŠ† u) : inclusion htu ∘ inclusion hst = inclusion (hst.trans htu) := funext (inclusion_inclusion hst htu) @[simp] lemma coe_inclusion (h : s βŠ† t) (x : s) : (inclusion h x : Ξ±) = (x : Ξ±) := rfl lemma inclusion_injective (h : s βŠ† t) : injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext_iff_val.2 ∘ subtype.ext_iff_val.1 lemma eq_of_inclusion_surjective {s t : set Ξ±} {h : s βŠ† t} (h_surj : function.surjective (inclusion h)) : s = t := begin refine set.subset.antisymm h (Ξ» x hx, _), obtain ⟨y, hy⟩ := h_surj ⟨x, hx⟩, exact mem_of_eq_of_mem (congr_arg coe hy).symm y.prop, end end inclusion end set namespace subsingleton variables {Ξ± : Type*} [subsingleton Ξ±] lemma eq_univ_of_nonempty {s : set Ξ±} : s.nonempty β†’ s = univ := Ξ» ⟨x, hx⟩, eq_univ_of_forall $ Ξ» y, subsingleton.elim x y β–Έ hx @[elab_as_eliminator] lemma set_cases {p : set Ξ± β†’ Prop} (h0 : p βˆ…) (h1 : p univ) (s) : p s := s.eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ h0) $ Ξ» h, (eq_univ_of_nonempty h).symm β–Έ h1 lemma mem_iff_nonempty {Ξ± : Type*} [subsingleton Ξ±] {s : set Ξ±} {x : Ξ±} : x ∈ s ↔ s.nonempty := ⟨λ hx, ⟨x, hx⟩, Ξ» ⟨y, hy⟩, subsingleton.elim y x β–Έ hy⟩ end subsingleton /-! ### Decidability instances for sets -/ namespace set variables {Ξ± : Type u} (s t : set Ξ±) (a : Ξ±) instance decidable_sdiff [decidable (a ∈ s)] [decidable (a ∈ t)] : decidable (a ∈ s \ t) := (by apply_instance : decidable (a ∈ s ∧ a βˆ‰ t)) instance decidable_inter [decidable (a ∈ s)] [decidable (a ∈ t)] : decidable (a ∈ s ∩ t) := (by apply_instance : decidable (a ∈ s ∧ a ∈ t)) instance decidable_union [decidable (a ∈ s)] [decidable (a ∈ t)] : decidable (a ∈ s βˆͺ t) := (by apply_instance : decidable (a ∈ s ∨ a ∈ t)) instance decidable_compl [decidable (a ∈ s)] : decidable (a ∈ sᢜ) := (by apply_instance : decidable (a βˆ‰ s)) instance decidable_emptyset : decidable_pred (∈ (βˆ… : set Ξ±)) := Ξ» _, decidable.is_false (by simp) instance decidable_univ : decidable_pred (∈ (set.univ : set Ξ±)) := Ξ» _, decidable.is_true (by simp) instance decidable_set_of (p : Ξ± β†’ Prop) [decidable (p a)] : decidable (a ∈ {a | p a}) := by assumption end set /-! ### Monotone lemmas for sets -/ section monotone variables {Ξ± Ξ² : Type*} theorem monotone.inter [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : monotone f) (hg : monotone g) : monotone (Ξ» x, f x ∩ g x) := hf.inf hg theorem monotone_on.inter [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} {s : set Ξ²} (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (Ξ» x, f x ∩ g x) s := hf.inf hg theorem antitone.inter [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : antitone f) (hg : antitone g) : antitone (Ξ» x, f x ∩ g x) := hf.inf hg theorem antitone_on.inter [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} {s : set Ξ²} (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (Ξ» x, f x ∩ g x) s := hf.inf hg theorem monotone.union [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : monotone f) (hg : monotone g) : monotone (Ξ» x, f x βˆͺ g x) := hf.sup hg theorem monotone_on.union [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} {s : set Ξ²} (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (Ξ» x, f x βˆͺ g x) s := hf.sup hg theorem antitone.union [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : antitone f) (hg : antitone g) : antitone (Ξ» x, f x βˆͺ g x) := hf.sup hg theorem antitone_on.union [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} {s : set Ξ²} (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (Ξ» x, f x βˆͺ g x) s := hf.sup hg namespace set theorem monotone_set_of [preorder Ξ±] {p : Ξ± β†’ Ξ² β†’ Prop} (hp : βˆ€ b, monotone (Ξ» a, p a b)) : monotone (Ξ» a, {b | p a b}) := Ξ» a a' h b, hp b h theorem antitone_set_of [preorder Ξ±] {p : Ξ± β†’ Ξ² β†’ Prop} (hp : βˆ€ b, antitone (Ξ» a, p a b)) : antitone (Ξ» a, {b | p a b}) := Ξ» a a' h b, hp b h /-- Quantifying over a set is antitone in the set -/ lemma antitone_bforall {P : Ξ± β†’ Prop} : antitone (Ξ» s : set Ξ±, βˆ€ x ∈ s, P x) := Ξ» s t hst h x hx, h x $ hst hx end set end monotone /-! ### Disjoint sets -/ variables {Ξ± Ξ² : Type*} {s t u : set Ξ±} {f : Ξ± β†’ Ξ²} namespace disjoint theorem union_left (hs : disjoint s u) (ht : disjoint t u) : disjoint (s βˆͺ t) u := hs.sup_left ht theorem union_right (ht : disjoint s t) (hu : disjoint s u) : disjoint s (t βˆͺ u) := ht.sup_right hu lemma inter_left (u : set Ξ±) (h : disjoint s t) : disjoint (s ∩ u) t := h.inf_left u lemma inter_left' (u : set Ξ±) (h : disjoint s t) : disjoint (u ∩ s) t := h.inf_left' _ lemma inter_right (u : set Ξ±) (h : disjoint s t) : disjoint s (t ∩ u) := h.inf_right _ lemma inter_right' (u : set Ξ±) (h : disjoint s t) : disjoint s (u ∩ t) := h.inf_right' _ lemma subset_left_of_subset_union (h : s βŠ† t βˆͺ u) (hac : disjoint s u) : s βŠ† t := hac.left_le_of_le_sup_right h lemma subset_right_of_subset_union (h : s βŠ† t βˆͺ u) (hab : disjoint s t) : s βŠ† u := hab.left_le_of_le_sup_left h end disjoint
345c1c8f375eb6c1fcfe3689be463fdadbbedc58
9a0192b31a07f48502dacee8122e0b8beda7b110
/lista3.lean
a327007e92d06cedba14648f57e8f43c758e9964
[]
no_license
ana-/Lean
8a5b9f2e13685bd43efd72c6665401c0851157cb
4527da83de947fa1df368b2f9bcf322a4d14e121
refs/heads/master
1,593,588,619,552
1,565,196,720,000
1,565,196,720,000
201,090,102
0
0
null
null
null
null
UTF-8
Lean
false
false
1,274
lean
/- Lista 3 do EAD QuestΓ£o 2- Um crime Γ© cometido por uma e somente uma pessoa e hΓ‘ quatro suspeitos: Paulo, Rodrigo, Henrique e Felipe. Interrogados eles fazem as seguintes declaraΓ§Γ΅es: Felipe: Rodrigo mente quando diz que eu nΓ£o sou inocente. Paulo: Rodrigo nΓ£o Γ© inocente. Henrique: Eu sou inocente. Rodrigo: Felipe nΓ£o Γ© inocente. Represente em lΓ³gica proposicional as sentenΓ§as acima. Com base nestas sentenΓ§as lΓ³gicas, mostre que se o inspetor usar Tableaux irΓ‘ concluir que se o Henrique for culpado entΓ£o somente Felipe fala a verdade. Mostre o Tableaux que suporta a conclusΓ£o do inspetor. P : paulo F : felipe H : henrique R : rodrigo c: x Γ© culpado v: x fal2a a verdade constant P : U β†’ U constant R : U β†’ Prop constant H : U β†’ Prop constant F : U β†’ Prop -/ --resposta variable U : Type variable C : U variable V : U variables P R H F: U β†’ Prop #check ((P C ∨ R C ∨ H C ∨ F C ) ∧ (P C β†’ (Β¬ R C ∧ Β¬ H C ∧ Β¬ F C)) ∧ (R C β†’ (Β¬ P C ∧ Β¬ H C ∧ Β¬ F C)) ∧ (H C β†’ (Β¬ R C ∧ Β¬ P C ∧ Β¬ F C)) ∧ (F C β†’ (Β¬ R C ∧ Β¬ H C ∧ Β¬ P C)) ∧ (H V ↔ (F C β†’ Β¬ R V)) ∧ (P V ↔ R C ) ∧ (H V ↔ Β¬ H C) ∧ (R V ↔ F C)) β†’ (H C β†’ (F V ∧ Β¬ P V ∧ Β¬ H V ∧ Β¬ R V))
34fe36571e0d55daa3974c473e25c117e4ed86d0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/data/buffer.lean
4ff10660110e19d89182cd0ccd37935f191fd75e
[]
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
4,671
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 universes u w u_1 namespace Mathlib def buffer (Ξ± : Type u) := sigma fun (n : β„•) => array n Ξ± def mk_buffer {Ξ± : Type u} : buffer Ξ± := sigma.mk 0 (d_array.mk fun (i : fin 0) => fin.elim0 i) def array.to_buffer {Ξ± : Type u} {n : β„•} (a : array n Ξ±) : buffer Ξ± := sigma.mk n a namespace buffer def nil {Ξ± : Type u} : buffer Ξ± := mk_buffer def size {Ξ± : Type u} (b : buffer Ξ±) : β„• := sigma.fst b def to_array {Ξ± : Type u} (b : buffer Ξ±) : array (size b) Ξ± := sigma.snd b def push_back {Ξ± : Type u} : buffer Ξ± β†’ Ξ± β†’ buffer Ξ± := sorry def pop_back {Ξ± : Type u} : buffer Ξ± β†’ buffer Ξ± := sorry def read {Ξ± : Type u} (b : buffer Ξ±) : fin (size b) β†’ Ξ± := sorry def write {Ξ± : Type u} (b : buffer Ξ±) : fin (size b) β†’ Ξ± β†’ buffer Ξ± := sorry def read' {Ξ± : Type u} [Inhabited Ξ±] : buffer Ξ± β†’ β„• β†’ Ξ± := sorry def write' {Ξ± : Type u} : buffer Ξ± β†’ β„• β†’ Ξ± β†’ buffer Ξ± := sorry theorem read_eq_read' {Ξ± : Type u} [Inhabited Ξ±] (b : buffer Ξ±) (i : β„•) (h : i < size b) : read b { val := i, property := h } = read' b i := sorry theorem write_eq_write' {Ξ± : Type u} (b : buffer Ξ±) (i : β„•) (h : i < size b) (v : Ξ±) : write b { val := i, property := h } v = write' b i v := sorry def to_list {Ξ± : Type u} (b : buffer Ξ±) : List Ξ± := array.to_list (to_array b) protected def to_string (b : buffer char) : string := list.as_string (array.to_list (to_array b)) def append_list {Ξ± : Type u} : buffer Ξ± β†’ List Ξ± β†’ buffer Ξ± := sorry def append_string (b : buffer char) (s : string) : buffer char := append_list b (string.to_list s) theorem lt_aux_1 {a : β„•} {b : β„•} {c : β„•} (h : a + c < b) : a < b := lt_of_le_of_lt (nat.le_add_right a c) h theorem lt_aux_2 {n : β„•} (h : n > 0) : n - 1 < n := (fun (h₁ : 1 > 0) => nat.sub_lt h h₁) (of_as_true trivial) theorem lt_aux_3 {n : β„•} {i : β„•} (h : i + 1 < n) : n - bit0 1 - i < n := sorry def append_array {Ξ± : Type u} {n : β„•} (nz : n > 0) : buffer Ξ± β†’ array n Ξ± β†’ (i : β„•) β†’ i < n β†’ buffer Ξ± := sorry protected def append {Ξ± : Type u} : buffer Ξ± β†’ buffer Ξ± β†’ buffer Ξ± := sorry def iterate {Ξ± : Type u} {Ξ² : Type w} (b : buffer Ξ±) : Ξ² β†’ (fin (size b) β†’ Ξ± β†’ Ξ² β†’ Ξ²) β†’ Ξ² := sorry def foreach {Ξ± : Type u} (b : buffer Ξ±) : (fin (size b) β†’ Ξ± β†’ Ξ±) β†’ buffer Ξ± := sorry /-- Monadically map a function over the buffer. -/ def mmap {Ξ± : Type u} {Ξ² : Type w} {m : Type w β†’ Type u_1} [Monad m] (b : buffer Ξ±) (f : Ξ± β†’ m Ξ²) : m (buffer Ξ²) := do let b' ← array.mmap (sigma.snd b) f return (array.to_buffer b') /-- Map a function over the buffer. -/ def map {Ξ± : Type u} {Ξ² : Type w} : buffer Ξ± β†’ (Ξ± β†’ Ξ²) β†’ buffer Ξ² := sorry def foldl {Ξ± : Type u} {Ξ² : Type w} : buffer Ξ± β†’ Ξ² β†’ (Ξ± β†’ Ξ² β†’ Ξ²) β†’ Ξ² := sorry def rev_iterate {Ξ± : Type u} {Ξ² : Type w} (b : buffer Ξ±) : Ξ² β†’ (fin (size b) β†’ Ξ± β†’ Ξ² β†’ Ξ²) β†’ Ξ² := sorry def take {Ξ± : Type u} (b : buffer Ξ±) (n : β„•) : buffer Ξ± := dite (n ≀ size b) (fun (h : n ≀ size b) => sigma.mk n (array.take (to_array b) n h)) fun (h : Β¬n ≀ size b) => b def take_right {Ξ± : Type u} (b : buffer Ξ±) (n : β„•) : buffer Ξ± := dite (n ≀ size b) (fun (h : n ≀ size b) => sigma.mk n (array.take_right (to_array b) n h)) fun (h : Β¬n ≀ size b) => b def drop {Ξ± : Type u} (b : buffer Ξ±) (n : β„•) : buffer Ξ± := dite (n ≀ size b) (fun (h : n ≀ size b) => sigma.mk (size b - n) (array.drop (to_array b) n h)) fun (h : Β¬n ≀ size b) => b def reverse {Ξ± : Type u} (b : buffer Ξ±) : buffer Ξ± := sigma.mk (size b) (array.reverse (to_array b)) protected def mem {Ξ± : Type u} (v : Ξ±) (a : buffer Ξ±) := βˆƒ (i : fin (size a)), read a i = v protected instance has_mem {Ξ± : Type u} : has_mem Ξ± (buffer Ξ±) := has_mem.mk buffer.mem protected instance has_append {Ξ± : Type u} : Append (buffer Ξ±) := { append := buffer.append } protected instance has_repr {Ξ± : Type u} [has_repr Ξ±] : has_repr (buffer Ξ±) := has_repr.mk (repr ∘ to_list) end buffer def list.to_buffer {Ξ± : Type u} (l : List Ξ±) : buffer Ξ± := buffer.append_list mk_buffer l def char_buffer := buffer char /-- Convert a format object into a character buffer with the provided formatting options. -/ def string.to_char_buffer (s : string) : char_buffer := buffer.append_string buffer.nil s
d7fc17535ce035acdcd9ab0806992afb29248aef
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast11.lean
08b1da392d0cbdb0162daa7c8eb61816b8a5e1a2
[ "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
227
lean
import data.nat open algebra nat definition lemma1 (a b : nat) : a + b + 0 = b + a := by simp print lemma1 definition lemma2 (a b c : nat) : a + b + 0 + c + a + a + b = 0 + 0 + c + a + b + a + a + b := by simp print lemma2
7710a6669d21a8278665bef924728b209e3eaa27
e151e9053bfd6d71740066474fc500a087837323
/src/hott/init/meta/intro.lean
f7c04abd415998192e2343ca2e9196579f865814
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
650
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ open expr tactic local postfix `?`:9001 := optional namespace tactic.interactive open interactive interactive.types lean.parser lean /-- A version of `intro` which will never insert `id_locked` in the proof term. The `intro` tactic will add `id_locked` to the proof term if the goal is not a Pi or a let (but reduces to one). See issue lean#1260. -/ meta def hintro : parse ident_? β†’ tactic unit | none := intro_core `_ >> skip | (some h) := intro_core h >> skip end tactic.interactive
51cdb4c4a501888e9c778dfe49a3fd7c341a866b
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/for_mathlib/logic.lean
866543797b9fdba4b1b724f93e5b052fa9b75104
[ "Apache-2.0" ]
permissive
semorrison/lean-perfectoid-spaces
78c1572cedbfae9c3e460d8aaf91de38616904d8
bb4311dff45791170bcb1b6a983e2591bee88a19
refs/heads/master
1,588,841,765,494
1,554,805,620,000
1,554,805,620,000
180,353,546
0
1
null
1,554,809,880,000
1,554,809,880,000
null
UTF-8
Lean
false
false
246
lean
import logic.basic theorem forall_iff_forall_surj {Ξ± Ξ² : Type*} {f : Ξ± β†’ Ξ²} (h : function.surjective f) {P : Ξ² β†’ Prop} : (βˆ€ a, P (f a)) ↔ βˆ€ b, P b:= ⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, Ξ» hb a, hb $ f a⟩
81958e8500b16dd5d41ccb3808db9084d98ea5f1
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/algebra/subalgebra.lean
8c2714d9716cfdaf6f36ccc3cc5057cb57eed53e
[ "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
27,183
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.operations /-! # Subalgebras over Commutative Semiring In this file we define `subalgebra`s and the usual operations on them (`map`, `comap`). More lemmas about `adjoin` can be found in `ring_theory.adjoin`. -/ universes u v w open_locale tensor_product big_operators set_option old_structure_cmd true /-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/ structure subalgebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v := (algebra_map_mem' : βˆ€ r, algebra_map R A r ∈ carrier) (zero_mem' := (algebra_map R A).map_zero β–Έ algebra_map_mem' 0) (one_mem' := (algebra_map R A).map_one β–Έ algebra_map_mem' 1) /-- Reinterpret a `subalgebra` as a `subsemiring`. -/ add_decl_doc subalgebra.to_subsemiring namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] include R instance : set_like (subalgebra R A) A := ⟨subalgebra.carrier, Ξ» p q h, by cases p; cases q; congr'⟩ @[simp] lemma mem_carrier {s : subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : subalgebra R A} (h : βˆ€ x : A, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a submodule with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) : subalgebra R A := { carrier := s, add_mem' := hs.symm β–Έ S.add_mem', mul_mem' := hs.symm β–Έ S.mul_mem', algebra_map_mem' := hs.symm β–Έ S.algebra_map_mem' } variables (S : subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≀ S.to_subsemiring := Ξ» x ⟨r, hr⟩, hr β–Έ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) βŠ† S := Ξ» x ⟨r, hr⟩, hr β–Έ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≀ S := S.range_subset theorem one_mem : (1 : A) ∈ S := S.to_subsemiring.one_mem theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := S.to_subsemiring.mul_mem hx hy theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r β€’ x ∈ S := (algebra.smul_def r x).symm β–Έ S.mul_mem (S.algebra_map_mem r) hx theorem pow_mem {x : A} (hx : x ∈ S) (n : β„•) : x ^ n ∈ S := S.to_subsemiring.pow_mem hx n theorem zero_mem : (0 : A) ∈ S := S.to_subsemiring.zero_mem theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := S.to_subsemiring.add_mem hx hy theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_one_smul R x β–Έ S.smul_mem hx _ theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := by simpa only [sub_eq_add_neg] using S.add_mem hx (S.neg_mem hy) theorem nsmul_mem {x : A} (hx : x ∈ S) (n : β„•) : n β€’ x ∈ S := S.to_subsemiring.nsmul_mem hx n theorem gsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : βˆ€ (n : β„€), n β€’ x ∈ S | (n : β„•) := by { rw [gsmul_coe_nat], exact S.nsmul_mem hx n } | -[1+ n] := by { rw [gsmul_neg_succ_of_nat], exact S.neg_mem (S.nsmul_mem hx _) } theorem coe_nat_mem (n : β„•) : (n : A) ∈ S := S.to_subsemiring.coe_nat_mem n theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) (n : β„€) : (n : A) ∈ S := int.cases_on n (Ξ» i, S.coe_nat_mem i) (Ξ» i, S.neg_mem $ S.coe_nat_mem $ i + 1) theorem list_prod_mem {L : list A} (h : βˆ€ x ∈ L, x ∈ S) : L.prod ∈ S := S.to_subsemiring.list_prod_mem h theorem list_sum_mem {L : list A} (h : βˆ€ x ∈ L, x ∈ S) : L.sum ∈ S := S.to_subsemiring.list_sum_mem h theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {m : multiset A} (h : βˆ€ x ∈ m, x ∈ S) : m.prod ∈ S := S.to_subsemiring.multiset_prod_mem m h theorem multiset_sum_mem {m : multiset A} (h : βˆ€ x ∈ m, x ∈ S) : m.sum ∈ S := S.to_subsemiring.multiset_sum_mem m h theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {ΞΉ : Type w} {t : finset ΞΉ} {f : ΞΉ β†’ A} (h : βˆ€ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S := S.to_subsemiring.prod_mem h theorem sum_mem {ΞΉ : Type w} {t : finset ΞΉ} {f : ΞΉ β†’ A} (h : βˆ€ x ∈ t, f x ∈ S) : βˆ‘ x in t, f x ∈ S := S.to_subsemiring.sum_mem h instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_add_submonoid (S : set A) := { zero_mem := S.zero_mem, add_mem := Ξ» _ _, S.add_mem } instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_submonoid (S : set A) := { one_mem := S.one_mem, mul_mem := Ξ» _ _, S.mul_mem } /-- A subalgebra over a ring is also a `subring`. -/ def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : subring A := { neg_mem' := Ξ» _, S.neg_mem, .. S.to_subsemiring } instance {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S : set A) := { neg_mem := Ξ» _, S.neg_mem } instance : inhabited S := ⟨(0 : S.to_subsemiring)⟩ section /-! `subalgebra`s inherit structure from their `subsemiring` / `semiring` coercions. -/ instance to_semiring {R A} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : semiring S := S.to_subsemiring.to_semiring instance to_comm_semiring {R A} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : comm_semiring S := S.to_subsemiring.to_comm_semiring instance to_ring {R A} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : ring S := S.to_subring.to_ring instance to_comm_ring {R A} [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := S.to_subring.to_comm_ring instance to_ordered_semiring {R A} [comm_semiring R] [ordered_semiring A] [algebra R A] (S : subalgebra R A) : ordered_semiring S := S.to_subsemiring.to_ordered_semiring instance to_ordered_comm_semiring {R A} [comm_semiring R] [ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) : ordered_comm_semiring S := S.to_subsemiring.to_ordered_comm_semiring instance to_ordered_ring {R A} [comm_ring R] [ordered_ring A] [algebra R A] (S : subalgebra R A) : ordered_ring S := S.to_subring.to_ordered_ring instance to_ordered_comm_ring {R A} [comm_ring R] [ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : ordered_comm_ring S := S.to_subring.to_ordered_comm_ring instance to_linear_ordered_semiring {R A} [comm_semiring R] [linear_ordered_semiring A] [algebra R A] (S : subalgebra R A) : linear_ordered_semiring S := S.to_subsemiring.to_linear_ordered_semiring /-! There is no `linear_ordered_comm_semiring`. -/ instance to_linear_ordered_ring {R A} [comm_ring R] [linear_ordered_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_ring S := S.to_subring.to_linear_ordered_ring instance to_linear_ordered_comm_ring {R A} [comm_ring R] [linear_ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_comm_ring S := S.to_subring.to_linear_ordered_comm_ring end instance algebra : algebra R S := { smul := Ξ» (c:R) x, ⟨c β€’ x.1, S.smul_mem x.2 c⟩, commutes' := Ξ» c x, subtype.eq $ algebra.commutes _ _, smul_def' := Ξ» c x, subtype.eq $ algebra.smul_def _ _, .. (algebra_map R A).cod_srestrict S.to_subsemiring $ Ξ» x, S.range_le ⟨x, rfl⟩ } instance to_algebra {R A B : Type*} [comm_semiring R] [comm_semiring A] [semiring B] [algebra R A] [algebra A B] (Aβ‚€ : subalgebra R A) : algebra Aβ‚€ B := algebra.of_subsemiring Aβ‚€.to_subsemiring instance nontrivial [nontrivial A] : nontrivial S := S.to_subsemiring.nontrivial instance no_zero_smul_divisors_bot [no_zero_smul_divisors R A] : no_zero_smul_divisors R S := ⟨λ c x h, have c = 0 ∨ (x : A) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ @[simp, norm_cast] lemma coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : S) : A) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : S) : A) = 1 := rfl @[simp, norm_cast] lemma coe_neg {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl @[simp, norm_cast] lemma coe_sub {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl @[simp, norm_cast] lemma coe_smul (r : R) (x : S) : (↑(r β€’ x) : A) = r β€’ ↑x := rfl @[simp, norm_cast] lemma coe_algebra_map (r : R) : ↑(algebra_map R S r) = algebra_map R A r := rfl @[simp, norm_cast] lemma coe_pow (x : S) (n : β„•) : (↑(x^n) : A) = (↑x)^n := begin induction n with n ih, { simp, }, { simp [pow_succ, ih], }, end @[simp, norm_cast] lemma coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := (subtype.ext_iff.symm : (x : A) = (0 : S) ↔ x = 0) @[simp, norm_cast] lemma coe_eq_one {x : S} : (x : A) = 1 ↔ x = 1 := (subtype.ext_iff.symm : (x : A) = (1 : S) ↔ x = 1) -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := by refine_struct { to_fun := (coe : S β†’ A) }; intros; refl @[simp] lemma coe_val : (S.val : S β†’ A) = coe := rfl lemma val_apply (x : S) : S.val x = (x : A) := rfl /-- Convert a `subalgebra` to `submodule` -/ def to_submodule : submodule R A := { carrier := S, zero_mem' := (0:S).2, add_mem' := Ξ» x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul_mem' := Ξ» c x hx, (algebra.smul_def c x).symm β–Έ (⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance to_submodule.is_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S.to_submodule : set A) := S.is_subring @[simp] lemma mem_to_submodule {x} : x ∈ S.to_submodule ↔ x ∈ S := iff.rfl theorem to_submodule_injective : function.injective (to_submodule : subalgebra R A β†’ submodule R A) := Ξ» S T h, ext $ Ξ» x, by rw [← mem_to_submodule, ← mem_to_submodule, h] theorem to_submodule_inj {S U : subalgebra R A} : S.to_submodule = U.to_submodule ↔ S = U := to_submodule_injective.eq_iff /-- As submodules, subalgebras are idempotent. -/ @[simp] theorem mul_self : S.to_submodule * S.to_submodule = S.to_submodule := begin apply le_antisymm, { rw submodule.mul_le, intros y hy z hz, exact mul_mem S hy hz }, { intros x hx1, rw ← mul_one x, exact submodule.mul_mem_mul hx1 (one_mem S) } end /-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal, we define it as a `linear_equiv` to avoid type equalities. -/ def to_submodule_equiv (S : subalgebra R A) : S.to_submodule ≃ₗ[R] S := linear_equiv.of_eq _ _ rfl /-- Reinterpret an `S`-subalgebra as an `R`-subalgebra in `comap R S A`. -/ def comap {R : Type u} {S : Type v} {A : Type w} [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] (iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) := { algebra_map_mem' := Ξ» r, iSB.algebra_map_mem (algebra_map R S r), .. iSB } /-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`, then `T` is an `R`-subalgebra of `A`. -/ def under {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { algebra_map_mem' := Ξ» r, T.algebra_map_mem ⟨algebra_map R A r, S.algebra_map_mem r⟩, .. T } /-- Transport a subalgebra via an algebra homomorphism. -/ def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := Ξ» r, f.commutes r β–Έ set.mem_image_of_mem _ (S.algebra_map_mem r), .. S.to_subsemiring.map (f : A β†’+* B) } lemma map_mono {S₁ Sβ‚‚ : subalgebra R A} {f : A →ₐ[R] B} : S₁ ≀ Sβ‚‚ β†’ S₁.map f ≀ Sβ‚‚.map f := set.image_subset f lemma map_injective {S₁ Sβ‚‚ : subalgebra R A} (f : A →ₐ[R] B) (hf : function.injective f) (ih : S₁.map f = Sβ‚‚.map f) : S₁ = Sβ‚‚ := ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ set_like.ext_iff.mp ih lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map S f ↔ βˆƒ x ∈ S, f x = y := subsemiring.mem_map /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap' (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A := { algebra_map_mem' := Ξ» r, show f (algebra_map R A r) ∈ S, from (f.commutes r).symm β–Έ S.algebra_map_mem r, .. S.to_subsemiring.comap (f : A β†’+* B) } theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} : map S f ≀ U ↔ S ≀ comap' U f := set.image_subset_iff @[simp] lemma mem_comap (S : subalgebra R B) (f : A →ₐ[R] B) (x : A) : x ∈ S.comap' f ↔ f x ∈ S := iff.rfl @[simp, norm_cast] lemma coe_comap (S : subalgebra R B) (f : A →ₐ[R] B) : (S.comap' f : set A) = f ⁻¹' (S : set B) := by { ext, simp, } instance no_zero_divisors {R A : Type*} [comm_ring R] [semiring A] [no_zero_divisors A] [algebra R A] (S : subalgebra R A) : no_zero_divisors S := S.to_subsemiring.no_zero_divisors instance no_zero_smul_divisors_top {R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] [no_zero_divisors A] (S : subalgebra R A) : no_zero_smul_divisors S A := ⟨λ c x h, have (c : A) = 0 ∨ x = 0, from eq_zero_or_eq_zero_of_mul_eq_zero h, this.imp_left (@subtype.ext_iff _ _ c 0).mpr⟩ instance integral_domain {R A : Type*} [comm_ring R] [integral_domain A] [algebra R A] (S : subalgebra R A) : integral_domain S := @subring.domain A _ S _ end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] variables (Ο† : A →ₐ[R] B) /-- Range of an `alg_hom` as a subalgebra. -/ protected def range (Ο† : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := Ξ» r, ⟨algebra_map R A r, Ο†.commutes r⟩, .. Ο†.to_ring_hom.srange } @[simp] lemma mem_range (Ο† : A →ₐ[R] B) {y : B} : y ∈ Ο†.range ↔ βˆƒ x, Ο† x = y := ring_hom.mem_srange theorem mem_range_self (Ο† : A →ₐ[R] B) (x : A) : Ο† x ∈ Ο†.range := Ο†.mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (Ο† : A →ₐ[R] B) : (Ο†.range : set B) = set.range Ο† := by { ext, rw [set_like.mem_coe, mem_range], refl } /-- Restrict the codomain of an algebra homomorphism. -/ def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : βˆ€ x, f x ∈ S) : A →ₐ[R] S := { commutes' := Ξ» r, subtype.eq $ f.commutes r, .. ring_hom.cod_srestrict (f : A β†’+* B) S.to_subsemiring hf } @[simp] lemma val_comp_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : βˆ€ x, f x ∈ S) : S.val.comp (f.cod_restrict S hf) = f := alg_hom.ext $ Ξ» _, rfl @[simp] lemma coe_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : βˆ€ x, f x ∈ S) (x : A) : ↑(f.cod_restrict S hf x) = f x := rfl theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : βˆ€ x, f x ∈ S) : function.injective (f.cod_restrict S hf) ↔ function.injective f := ⟨λ H x y hxy, H $ subtype.eq hxy, Ξ» H x y hxy, H (congr_arg subtype.val hxy : _)⟩ /-- Restrict the codomain of a alg_hom `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : A →ₐ[R] B) : A →ₐ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (Ο• ψ : A →ₐ[R] B) : subalgebra R A := { carrier := {a | Ο• a = ψ a}, add_mem' := Ξ» x y hx hy, by { change Ο• x = ψ x at hx, change Ο• y = ψ y at hy, change Ο• (x + y) = ψ (x + y), rw [alg_hom.map_add, alg_hom.map_add, hx, hy] }, mul_mem' := Ξ» x y hx hy, by { change Ο• x = ψ x at hx, change Ο• y = ψ y at hy, change Ο• (x * y) = ψ (x * y), rw [alg_hom.map_mul, alg_hom.map_mul, hx, hy] }, algebra_map_mem' := Ξ» x, by { change Ο• (algebra_map R A x) = ψ (algebra_map R A x), rw [alg_hom.commutes, alg_hom.commutes] } } @[simp] lemma mem_equalizer (Ο• ψ : A →ₐ[R] B) (x : A) : x ∈ Ο•.equalizer ψ ↔ Ο• x = ψ x := iff.rfl end alg_hom namespace alg_equiv variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] /-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range. This is a computable alternative to `alg_equiv.of_injective`. -/ def of_left_inverse {g : B β†’ A} {f : A →ₐ[R] B} (h : function.left_inverse g f) : A ≃ₐ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.val, left_inv := h, right_inv := Ξ» x, subtype.ext $ let ⟨x', hx'⟩ := f.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : B β†’ A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : A) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : B β†’ A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Restrict an injective algebra homomorphism to an algebra isomorphism -/ noncomputable def of_injective (f : A →ₐ[R] B) (hf : function.injective f) : A ≃ₐ[R] f.range := of_left_inverse (classical.some_spec hf.has_left_inverse) @[simp] lemma of_injective_apply (f : A →ₐ[R] B) (hf : function.injective f) (x : A) : ↑(of_injective f hf x) = f x := rfl /-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/ noncomputable def of_injective_field {E F : Type*} [division_ring E] [semiring F] [nontrivial F] [algebra R E] [algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range := of_injective f f.to_ring_hom.injective end alg_equiv namespace algebra variables (R : Type u) {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : set A) : subalgebra R A := { algebra_map_mem' := Ξ» r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩, .. subsemiring.closure (set.range (algebra_map R A) βˆͺ s) } variables {R} protected lemma gc : galois_connection (adjoin R : set A β†’ subalgebra R A) coe := Ξ» s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H, Ξ» H, show subsemiring.closure (set.range (algebra_map R A) βˆͺ s) ≀ S.to_subsemiring, from subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A β†’ subalgebra R A) coe := { choice := Ξ» s hs, adjoin R s, gc := algebra.gc, le_l_u := Ξ» S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := Ξ» _ _, rfl } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi instance : inhabited (subalgebra R A) := ⟨βŠ₯⟩ theorem mem_bot {x : A} : x ∈ (βŠ₯ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := suffices (of_id R A).range = (βŠ₯ : subalgebra R A), by { rw [← this, ←set_like.mem_coe, alg_hom.coe_range], refl }, le_bot_iff.mp (Ξ» x hx, subalgebra.range_le _ ((of_id R A).coe_range β–Έ hx)) theorem to_submodule_bot : (βŠ₯ : subalgebra R A).to_submodule = R βˆ™ 1 := by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] } @[simp] theorem mem_top {x : A} : x ∈ (⊀ : subalgebra R A) := subsemiring.subset_closure $ or.inr trivial @[simp] theorem top_to_submodule : (⊀ : subalgebra R A).to_submodule = ⊀ := submodule.ext $ Ξ» x, iff_of_true mem_top trivial @[simp] theorem top_to_subsemiring : (⊀ : subalgebra R A).to_subsemiring = ⊀ := subsemiring.ext $ Ξ» x, iff_of_true mem_top trivial @[simp] theorem coe_bot : ((βŠ₯ : subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, algebra.mem_bot] theorem eq_top_iff {S : subalgebra R A} : S = ⊀ ↔ βˆ€ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, Ξ» h, by ext x; exact ⟨λ _, mem_top, Ξ» _, h x⟩⟩ @[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊀ : subalgebra R A) f = f.range := subalgebra.ext $ Ξ» x, ⟨λ ⟨y, _, hy⟩, ⟨y, hy⟩, Ξ» ⟨y, hy⟩, ⟨y, algebra.mem_top, hy⟩⟩ @[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (βŠ₯ : subalgebra R A) f = βŠ₯ := eq_bot_iff.2 $ Ξ» x ⟨y, hy, hfy⟩, let ⟨r, hr⟩ := mem_bot.1 hy in subalgebra.range_le _ ⟨r, by rwa [← f.commutes, hr]⟩ @[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap' (⊀ : subalgebra R B) f = ⊀ := eq_top_iff.2 $ Ξ» x, mem_top /-- `alg_hom` to `⊀ : subalgebra R A`. -/ def to_top : A →ₐ[R] (⊀ : subalgebra R A) := by refine_struct { to_fun := Ξ» x, (⟨x, mem_top⟩ : (⊀ : subalgebra R A)) }; intros; refl theorem surjective_algebra_map_iff : function.surjective (algebra_map R A) ↔ (⊀ : subalgebra R A) = βŠ₯ := ⟨λ h, eq_bot_iff.2 $ Ξ» y _, let ⟨x, hx⟩ := h y in hx β–Έ subalgebra.algebra_map_mem _ _, Ξ» h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩ theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A] [algebra R A] : function.bijective (algebra_map R A) ↔ (⊀ : subalgebra R A) = βŠ₯ := ⟨λ h, surjective_algebra_map_iff.1 h.2, Ξ» h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) : (βŠ₯ : subalgebra R A) ≃ₐ[R] R := alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _) ⟨λ x y hxy, h (congr_arg subtype.val hxy : _), Ξ» ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] : (βŠ₯ : subalgebra F R) ≃ₐ[F] F := bot_equiv_of_injective (ring_hom.injective _) /-- The top subalgebra is isomorphic to the field. -/ noncomputable def top_equiv : (⊀ : subalgebra R A) ≃ₐ[R] A := (alg_equiv.of_bijective to_top ⟨λ _ _, subtype.mk.inj, Ξ» x, ⟨x.val, by { ext, refl }⟩⟩ : A ≃ₐ[R] (⊀ : subalgebra R A)).symm end algebra namespace subalgebra open algebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] variables (S : subalgebra R A) -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_subsingleton [subsingleton A] : subsingleton (subalgebra R A) := ⟨λ B C, ext (Ξ» x, by { simp only [subsingleton.elim x 0, zero_mem] })⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_hom.subsingleton [subsingleton (subalgebra R A)] : subsingleton (A →ₐ[R] B) := ⟨λ f g, alg_hom.ext $ Ξ» a, have a ∈ (βŠ₯ : subalgebra R A) := subsingleton.elim (⊀ : subalgebra R A) βŠ₯ β–Έ mem_top, let ⟨x, hx⟩ := set.mem_range.mp (mem_bot.mp this) in hx β–Έ (f.commutes _).trans (g.commutes _).symm⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_equiv.subsingleton_left [subsingleton (subalgebra R A)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (A →ₐ[R] B) := alg_hom.subsingleton, exact ⟨λ f g, alg_equiv.ext (Ξ» x, alg_hom.ext_iff.mp (subsingleton.elim f.to_alg_hom g.to_alg_hom) x)⟩, end -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_equiv.subsingleton_right [subsingleton (subalgebra R B)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (B ≃ₐ[R] A) := alg_equiv.subsingleton_left, exact ⟨λ f g, eq.trans (alg_equiv.symm_symm _).symm (by rw [subsingleton.elim f.symm g.symm, alg_equiv.symm_symm])⟩ end lemma range_val : S.val.range = S := ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val instance : unique (subalgebra R R) := { uniq := begin intro S, refine le_antisymm (Ξ» r hr, _) bot_le, simp only [set.mem_range, mem_bot, id.map_eq_self, exists_apply_eq_apply, default], end .. algebra.subalgebra.inhabited } /-- Two subalgebras that are equal are also equivalent as algebras. This is the `subalgebra` version of `linear_equiv.of_eq` and `equiv.set.of_eq`. -/ @[simps apply] def equiv_of_eq (S T : subalgebra R A) (h : S = T) : S ≃ₐ[R] T := { to_fun := Ξ» x, ⟨x, h β–Έ x.2⟩, inv_fun := Ξ» x, ⟨x, h.symm β–Έ x.2⟩, map_mul' := Ξ» _ _, rfl, commutes' := Ξ» _, rfl, .. linear_equiv.of_eq _ _ (congr_arg to_submodule h) } @[simp] lemma equiv_of_eq_symm (S T : subalgebra R A) (h : S = T) : (equiv_of_eq S T h).symm = equiv_of_eq T S h.symm := rfl @[simp] lemma equiv_of_eq_rfl (S : subalgebra R A) : equiv_of_eq S S rfl = alg_equiv.refl := by { ext, refl } @[simp] lemma equiv_of_eq_trans (S T U : subalgebra R A) (hST : S = T) (hTU : T = U) : (equiv_of_eq S T hST).trans (equiv_of_eq T U hTU) = equiv_of_eq S U (trans hST hTU) := rfl end subalgebra section nat variables {R : Type*} [semiring R] /-- A subsemiring is a `β„•`-subalgebra. -/ def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra β„• R := { algebra_map_mem' := Ξ» i, S.coe_nat_mem i, .. S } @[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} : x ∈ subalgebra_of_subsemiring S ↔ x ∈ S := iff.rfl end nat section int variables {R : Type*} [ring R] /-- A subring is a `β„€`-subalgebra. -/ def subalgebra_of_subring (S : subring R) : subalgebra β„€ R := { algebra_map_mem' := Ξ» i, int.induction_on i S.zero_mem (Ξ» i ih, S.add_mem ih S.one_mem) (Ξ» i ih, show ((-i - 1 : β„€) : R) ∈ S, by { rw [int.cast_sub, int.cast_one], exact S.sub_mem ih S.one_mem }), .. S } /-- A subset closed under the ring operations is a `β„€`-subalgebra. -/ def subalgebra_of_is_subring (S : set R) [is_subring S] : subalgebra β„€ R := subalgebra_of_subring S.to_subring variables {S : Type*} [semiring S] @[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl @[simp] lemma mem_subalgebra_of_is_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_is_subring S ↔ x ∈ S := iff.rfl end int
ba6bc41d34b207af35797f742419d501f155df0d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/localization.lean
7495e71da36fce6c37b782db1759daa988b739a4
[ "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
112,297
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import data.equiv.ring import group_theory.monoid_localization import ring_theory.algebraic import ring_theory.ideal.local_ring import ring_theory.ideal.quotient import ring_theory.integral_closure import ring_theory.non_zero_divisors import group_theory.submonoid.inverses import tactic.ring_exp /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R β†’+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R Γ— M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. In the following, let `R, P` be commutative rings, `S, Q` be `R`- and `P`-algebras and `M, T` be submonoids of `R` and `P` respectively, e.g.: ``` variables (R S P Q : Type*) [comm_ring R] [comm_ring S] [comm_ring P] [comm_ring Q] variables [algebra R S] [algebra P Q] (M : submonoid R) (T : submonoid P) ``` ## Main definitions * `is_localization (M : submonoid R) (S : Type*)` is a typeclass expressing that `S` is a localization of `R` at `M`, i.e. the canonical map `algebra_map R S : R β†’+* S` is a localization map (satisfying the above properties). * `is_localization.mk' S` is a surjection sending `(x, y) : R Γ— M` to `f x * (f y)⁻¹` * `is_localization.lift` is the ring homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. * `is_localization.map S Q` is the ring homomorphism from `S` to `Q` which maps elements of `M` to elements of `T` * `is_localization.ring_equiv_of_ring_equiv`: if `R` and `P` are isomorphic by an isomorphism sending `M` to `T`, then `S` and `Q` are isomorphic * `is_localization.alg_equiv`: if `Q` is another localization of `R` at `M`, then `S` and `Q` are isomorphic as `R`-algebras * `is_localization.is_integer` is a predicate stating that `x : S` is in the image of `R` * `is_localization.away (x : R) S` expresses that `S` is a localization away from `x`, as an abbreviation of `is_localization (submonoid.powers x) S` * `is_localization.at_prime (I : ideal R) [is_prime I] (S : Type*)` expresses that `S` is a localization at (the complement of) a prime ideal `I`, as an abbreviation of `is_localization I.prime_compl S` * `is_fraction_ring R K` expresses that `K` is a field of fractions of `R`, as an abbreviation of `is_localization (non_zero_divisors R) K` ## Main results * `localization M S`, a construction of the localization as a quotient type, defined in `group_theory.monoid_localization`, has `comm_ring`, `algebra R` and `is_localization M` instances if `R` is a ring. `localization.away`, `localization.at_prime` and `fraction_ring` are abbreviations for `localization`s and have their corresponding `is_localization` instances * `is_localization.at_prime.local_ring`: a theorem (not an instance) stating a localization at the complement of a prime ideal is a local ring * `is_fraction_ring.field`: a definition (not an instance) stating the localization of an integral domain `R` at `R \ {0}` is a field * `rat.is_fraction_ring` is an instance stating `β„š` is the field of fractions of `β„€` ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A previous version of this file used a fully bundled type of ring localization maps, then used a type synonym `f.codomain` for `f : localization_map M S` to instantiate the `R`-algebra structure on `S`. This results in defining ad-hoc copies for everything already defined on `S`. By making `is_localization` a predicate on the `algebra_map R S`, we can ensure the localization map commutes nicely with other `algebra_map`s. To prove most lemmas about a localization map `algebra_map R S` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `is_localization.to_localization_map M S`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R β†’ M β†’ localization M` equals the surjection `localization_map.mk'` induced by the map `algebra_map : R β†’+* localization M`. The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] variables [algebra R S] {P : Type*} [comm_ring P] open function open_locale big_operators /-- The typeclass `is_localization (M : submodule R) S` where `S` is an `R`-algebra expresses that `S` is isomorphic to the localization of `R` at `M`. -/ class is_localization : Prop := (map_units [] : βˆ€ y : M, is_unit (algebra_map R S y)) (surj [] : βˆ€ z : S, βˆƒ x : R Γ— M, z * algebra_map R S x.2 = algebra_map R S x.1) (eq_iff_exists [] : βˆ€ {x y}, algebra_map R S x = algebra_map R S y ↔ βˆƒ c : M, x * c = y * c) variables {M S} namespace is_localization section is_localization variables [is_localization M S] section variables (M) lemma of_le (N : submonoid R) (h₁ : M ≀ N) (hβ‚‚ : βˆ€ r ∈ N, is_unit (algebra_map R S r)) : is_localization N S := { map_units := Ξ» r, hβ‚‚ r r.2, surj := Ξ» s, by { obtain ⟨⟨x, y, hy⟩, H⟩ := is_localization.surj M s, exact ⟨⟨x, y, h₁ hy⟩, H⟩ }, eq_iff_exists := Ξ» x y, begin split, { rw is_localization.eq_iff_exists M, rintro ⟨c, hc⟩, exact ⟨⟨c, h₁ c.2⟩, hc⟩ }, { rintro ⟨c, h⟩, simpa only [set_like.coe_mk, map_mul, (hβ‚‚ c c.2).mul_left_inj] using congr_arg (algebra_map R S) h } end } variables (S) /-- `is_localization.to_localization_map M S` shows `S` is the monoid localization of `R` at `M`. -/ @[simps] def to_localization_map : submonoid.localization_map M S := { to_fun := algebra_map R S, map_units' := is_localization.map_units _, surj' := is_localization.surj _, eq_iff_exists' := Ξ» _ _, is_localization.eq_iff_exists _ _, .. algebra_map R S } @[simp] lemma to_localization_map_to_map : (to_localization_map M S).to_map = (algebra_map R S : R β†’* S) := rfl lemma to_localization_map_to_map_apply (x) : (to_localization_map M S).to_map x = algebra_map R S x := rfl end section variables (R) -- TODO: define a subalgebra of `is_integer`s /-- Given `a : S`, `S` a localization of `R`, `is_integer R a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ (algebra_map R S).range end lemma is_integer_zero : is_integer R (0 : S) := subring.zero_mem _ lemma is_integer_one : is_integer R (1 : S) := subring.one_mem _ lemma is_integer_add {a b : S} (ha : is_integer R a) (hb : is_integer R b) : is_integer R (a + b) := subring.add_mem _ ha hb lemma is_integer_mul {a b : S} (ha : is_integer R a) (hb : is_integer R b) : is_integer R (a * b) := subring.mul_mem _ ha hb lemma is_integer_smul {a : R} {b : S} (hb : is_integer R b) : is_integer R (a β€’ b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, (algebra_map R S).map_mul, algebra.smul_def] end variables (M) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : βˆƒ (b : M), is_integer R (a * algebra_map R S b) := let ⟨⟨num, denom⟩, h⟩ := is_localization.surj _ a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : βˆƒ (b : M), is_integer R ((b : R) β€’ a) := by { simp_rw [algebra.smul_def, mul_comm _ a], apply exists_integer_multiple' } /-- Given a localization map `f : M β†’* N`, a section function sending `z : N` to some `(x, y) : M Γ— S` such that `f x * (f y)⁻¹ = z`. -/ noncomputable def sec (z : S) : R Γ— M := classical.some $ is_localization.surj _ z @[simp] lemma to_localization_map_sec : (to_localization_map M S).sec = sec M := rfl /-- Given `z : S`, `is_localization.sec M z` is defined to be a pair `(x, y) : R Γ— M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec (z : S) : z * algebra_map R S (is_localization.sec M z).2 = algebra_map R S (is_localization.sec M z).1 := classical.some_spec $ is_localization.surj _ z /-- Given `z : S`, `is_localization.sec M z` is defined to be a pair `(x, y) : R Γ— M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' (z : S) : algebra_map R S (is_localization.sec M z).1 = algebra_map R S (is_localization.sec M z).2 * z := by rw [mul_comm, sec_spec] open_locale big_operators /-- We can clear the denominators of a `finset`-indexed family of fractions. -/ lemma exist_integer_multiples {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ S) : βˆƒ (b : M), βˆ€ i ∈ s, is_localization.is_integer R ((b : R) β€’ f i) := begin haveI := classical.prop_decidable, refine ⟨∏ i in s, (sec M (f i)).2, Ξ» i hi, ⟨_, _⟩⟩, { exact (∏ j in s.erase i, (sec M (f j)).2) * (sec M (f i)).1 }, rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←(algebra_map R S).map_mul, ← algebra.smul_def], congr' 2, refine trans _ ((submonoid.subtype M).map_prod _ _).symm, rw [mul_comm, ←finset.prod_insert (s.not_mem_erase i), finset.insert_erase hi], refl end /-- We can clear the denominators of a `fintype`-indexed family of fractions. -/ lemma exist_integer_multiples_of_fintype {ΞΉ : Type*} [fintype ΞΉ] (f : ΞΉ β†’ S) : βˆƒ (b : M), βˆ€ i, is_localization.is_integer R ((b : R) β€’ f i) := begin obtain ⟨b, hb⟩ := exist_integer_multiples M finset.univ f, exact ⟨b, Ξ» i, hb i (finset.mem_univ _)⟩ end /-- We can clear the denominators of a finite set of fractions. -/ lemma exist_integer_multiples_of_finset (s : finset S) : βˆƒ (b : M), βˆ€ a ∈ s, is_integer R ((b : R) β€’ a) := exist_integer_multiples M s id /-- A choice of a common multiple of the denominators of a `finset`-indexed family of fractions. -/ noncomputable def common_denom {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ S) : M := (exist_integer_multiples M s f).some /-- The numerator of a fraction after clearing the denominators of a `finset`-indexed family of fractions. -/ noncomputable def integer_multiple {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ S) (i : s) : R := ((exist_integer_multiples M s f).some_spec i i.prop).some @[simp] lemma map_integer_multiple {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ S) (i : s) : algebra_map R S (integer_multiple M s f i) = common_denom M s f β€’ f i := ((exist_integer_multiples M s f).some_spec _ i.prop).some_spec /-- A choice of a common multiple of the denominators of a finite set of fractions. -/ noncomputable def common_denom_of_finset (s : finset S) : M := common_denom M s id /-- The finset of numerators after clearing the denominators of a finite set of fractions. -/ noncomputable def finset_integer_multiple [decidable_eq R] (s : finset S) : finset R := s.attach.image (Ξ» t, integer_multiple M s id t) open_locale pointwise lemma finset_integer_multiple_image [decidable_eq R] (s : finset S) : algebra_map R S '' (finset_integer_multiple M s) = common_denom_of_finset M s β€’ s := begin delta finset_integer_multiple common_denom, rw finset.coe_image, ext, split, { rintro ⟨_, ⟨x, -, rfl⟩, rfl⟩, rw map_integer_multiple, exact set.mem_image_of_mem _ x.prop }, { rintro ⟨x, hx, rfl⟩, exact ⟨_, ⟨⟨x, hx⟩, s.mem_attach _, rfl⟩, map_integer_multiple M s id _⟩ } end variables {R M} lemma map_right_cancel {x y} {c : M} (h : algebra_map R S (c * x) = algebra_map R S (c * y)) : algebra_map R S x = algebra_map R S y := (to_localization_map M S).map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : algebra_map R S (x * c) = algebra_map R S (y * c)) : algebra_map R S x = algebra_map R S y := (to_localization_map M S).map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * algebra_map R S y = algebra_map R S x) (hx : x = 0) : z = 0 := by { rw [hx, (algebra_map R S).map_zero] at h, exact (is_unit.mul_left_eq_zero (is_localization.map_units S y)).1 h} variables (M S) lemma map_eq_zero_iff (r : R) : algebra_map R S r = 0 ↔ βˆƒ m : M, r * m = 0 := begin split, intro h, { obtain ⟨m, hm⟩ := (is_localization.eq_iff_exists M S).mp ((algebra_map R S).map_zero.trans h.symm), exact ⟨m, by simpa using hm.symm⟩ }, { rintro ⟨m, hm⟩, rw [← (is_localization.map_units S m).mul_left_inj, zero_mul, ← ring_hom.map_mul, hm, ring_hom.map_zero] } end variables {M} /-- `is_localization.mk' S` is the surjection sending `(x, y) : R Γ— M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (x : R) (y : M) : S := (to_localization_map M S).mk' x y @[simp] lemma mk'_sec (z : S) : mk' S (is_localization.sec M z).1 (is_localization.sec M z).2 = z := (to_localization_map M S).mk'_sec _ lemma mk'_mul (x₁ xβ‚‚ : R) (y₁ yβ‚‚ : M) : mk' S (x₁ * xβ‚‚) (y₁ * yβ‚‚) = mk' S x₁ y₁ * mk' S xβ‚‚ yβ‚‚ := (to_localization_map M S).mk'_mul _ _ _ _ lemma mk'_one (x) : mk' S x (1 : M) = algebra_map R S x := (to_localization_map M S).mk'_one _ @[simp] lemma mk'_spec (x) (y : M) : mk' S x y * algebra_map R S y = algebra_map R S x := (to_localization_map M S).mk'_spec _ _ @[simp] lemma mk'_spec' (x) (y : M) : algebra_map R S y * mk' S x y = algebra_map R S x := (to_localization_map M S).mk'_spec' _ _ @[simp] lemma mk'_spec_mk (x) (y : R) (hy : y ∈ M) : mk' S x ⟨y, hy⟩ * algebra_map R S y = algebra_map R S x := mk'_spec S x ⟨y, hy⟩ @[simp] lemma mk'_spec'_mk (x) (y : R) (hy : y ∈ M) : algebra_map R S y * mk' S x ⟨y, hy⟩ = algebra_map R S x := mk'_spec' S x ⟨y, hy⟩ variables {S} theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = mk' S x y ↔ z * algebra_map R S y = algebra_map R S x := (to_localization_map M S).eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : mk' S x y = z ↔ algebra_map R S x = z * algebra_map R S y := (to_localization_map M S).mk'_eq_iff_eq_mul variables (M) lemma mk'_surjective (z : S) : βˆƒ x (y : M), mk' S x y = z := let ⟨r, hr⟩ := is_localization.surj _ z in ⟨r.1, r.2, (eq_mk'_iff_mul_eq.2 hr).symm⟩ variables {M} lemma mk'_eq_iff_eq {x₁ xβ‚‚} {y₁ yβ‚‚ : M} : mk' S x₁ y₁ = mk' S xβ‚‚ yβ‚‚ ↔ algebra_map R S (x₁ * yβ‚‚) = algebra_map R S (xβ‚‚ * y₁) := (to_localization_map M S).mk'_eq_iff_eq lemma mk'_mem_iff {x} {y : M} {I : ideal S} : mk' S x y ∈ I ↔ algebra_map R S x ∈ I := begin split; intro h, { rw [← mk'_spec S x y, mul_comm], exact I.mul_mem_left ((algebra_map R S) y) h }, { rw ← mk'_spec S x y at h, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (map_units S y), have := I.mul_mem_left b h, rwa [mul_comm, mul_assoc, hb, mul_one] at this } end protected lemma eq {a₁ b₁} {aβ‚‚ bβ‚‚ : M} : mk' S a₁ aβ‚‚ = mk' S b₁ bβ‚‚ ↔ βˆƒ c : M, a₁ * bβ‚‚ * c = b₁ * aβ‚‚ * c := (to_localization_map M S).eq lemma mk'_eq_zero_iff (x : R) (s : M) : mk' S x s = 0 ↔ βˆƒ (m : M), x * m = 0 := by rw [← (map_units S s).mul_left_inj, mk'_spec, zero_mul, map_eq_zero_iff M] section ext variables [algebra R P] [is_localization M P] lemma eq_iff_eq {x y} : algebra_map R S x = algebra_map R S y ↔ algebra_map R P x = algebra_map R P y := (to_localization_map M S).eq_iff_eq (to_localization_map M P) lemma mk'_eq_iff_mk'_eq {x₁ xβ‚‚} {y₁ yβ‚‚ : M} : mk' S x₁ y₁ = mk' S xβ‚‚ yβ‚‚ ↔ mk' P x₁ y₁ = mk' P xβ‚‚ yβ‚‚ := (to_localization_map M S).mk'_eq_iff_mk'_eq (to_localization_map M P) lemma mk'_eq_of_eq {a₁ b₁ : R} {aβ‚‚ bβ‚‚ : M} (H : b₁ * aβ‚‚ = a₁ * bβ‚‚) : mk' S a₁ aβ‚‚ = mk' S b₁ bβ‚‚ := (to_localization_map M S).mk'_eq_of_eq H variables (S) @[simp] lemma mk'_self {x : R} (hx : x ∈ M) : mk' S x ⟨x, hx⟩ = 1 := (to_localization_map M S).mk'_self _ hx @[simp] lemma mk'_self' {x : M} : mk' S (x : R) x = 1 := (to_localization_map M S).mk'_self' _ lemma mk'_self'' {x : M} : mk' S x.1 x = 1 := mk'_self' _ end ext lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : (algebra_map R S) x * mk' S y z = mk' S (x * y) z := (to_localization_map M S).mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : mk' S x y = (algebra_map R S) x * mk' S 1 y := ((to_localization_map M S).mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : mk' S (y * x : R) y = (algebra_map R S) x := (to_localization_map M S).mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : mk' S (x * y) y = (algebra_map R S) x := (to_localization_map M S).mk'_mul_cancel_right _ _ @[simp] lemma mk'_mul_mk'_eq_one (x y : M) : mk' S (x : R) y * mk' S (y : R) x = 1 := by rw [←mk'_mul, mul_comm]; exact mk'_self _ _ lemma mk'_mul_mk'_eq_one' (x : R) (y : M) (h : x ∈ M) : mk' S x y * mk' S (y : R) ⟨x, h⟩ = 1 := mk'_mul_mk'_eq_one ⟨x, h⟩ _ section variables (M) lemma is_unit_comp (j : S β†’+* P) (y : M) : is_unit (j.comp (algebra_map R S) y) := (to_localization_map M S).is_unit_comp j.to_monoid_hom _ end /-- Given a localization map `f : R β†’+* S` for a submonoid `M βŠ† R` and a map of `comm_ring`s `g : R β†’+* P` such that `g(M) βŠ† units P`, `f x = f y β†’ g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R β†’+* P} (hg : βˆ€ y : M, is_unit (g y)) {x y} (h : (algebra_map R S) x = (algebra_map R S) y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ xβ‚‚ : R) (y₁ yβ‚‚ : M) : mk' S (x₁ * yβ‚‚ + xβ‚‚ * y₁) (y₁ * yβ‚‚) = mk' S x₁ y₁ + mk' S xβ‚‚ yβ‚‚ := mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ ((algebra_map R S) _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←(algebra_map R S).map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [(algebra_map R S).map_add, submonoid.coe_mul, (algebra_map R S).map_mul], ring_exp, end /-- Given a localization map `f : R β†’+* S` for a submonoid `M βŠ† R` and a map of `comm_ring`s `g : R β†’+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R Γ— M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R β†’+* P} (hg : βˆ€ y : M, is_unit (g y)) : S β†’+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg) $ begin intros x y, rw [(to_localization_map M S).lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, (to_localization_map M S).lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, (to_localization_map M S).lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), simp only [← g.map_sub, ← g.map_mul, to_localization_map_sec], apply eq_of_eq hg, rw [(algebra_map R S).map_mul, sec_spec', mul_sub, (algebra_map R S).map_sub], simp only [ring_hom.map_mul, sec_spec'], ring, assumption end variables {g : R β†’+* P} (hg : βˆ€ y : M, is_unit (g y)) /-- Given a localization map `f : R β†’+* S` for a submonoid `M βŠ† R` and a map of `comm_ring`s `g : R β†’* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : lift hg (mk' S x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := (to_localization_map M S).lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : lift hg (mk' S x y) = v ↔ g x = g y * v := (to_localization_map M S).lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : lift hg ((algebra_map R S) x) = g x := (to_localization_map M S).lift_eq _ _ lemma lift_eq_iff {x y : R Γ— M} : lift hg (mk' S x.1 x.2) = lift hg (mk' S y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := (to_localization_map M S).lift_eq_iff _ @[simp] lemma lift_comp : (lift hg).comp (algebra_map R S) = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ (to_localization_map M S).lift_comp _ @[simp] lemma lift_of_comp (j : S β†’+* P) : lift (is_unit_comp M j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ (to_localization_map M S).lift_of_comp j.to_monoid_hom variables (M) /-- See note [partially-applied ext lemmas] -/ lemma monoid_hom_ext ⦃j k : S β†’* P⦄ (h : j.comp (algebra_map R S : R β†’* S) = k.comp (algebra_map R S)) : j = k := submonoid.localization_map.epic_of_localization_map (to_localization_map M S) $ monoid_hom.congr_fun h /-- See note [partially-applied ext lemmas] -/ lemma ring_hom_ext ⦃j k : S β†’+* P⦄ (h : j.comp (algebra_map R S) = k.comp (algebra_map R S)) : j = k := ring_hom.coe_monoid_hom_injective $ monoid_hom_ext M $ monoid_hom.ext $ ring_hom.congr_fun h /-- To show `j` and `k` agree on the whole localization, it suffices to show they agree on the image of the base ring, if they preserve `1` and `*`. -/ protected lemma ext (j k : S β†’ P) (hj1 : j 1 = 1) (hk1 : k 1 = 1) (hjm : βˆ€ a b, j (a * b) = j a * j b) (hkm : βˆ€ a b, k (a * b) = k a * k b) (h : βˆ€ a, j (algebra_map R S a) = k (algebra_map R S a)) : j = k := monoid_hom.mk.inj (monoid_hom_ext M $ monoid_hom.ext h : (⟨j, hj1, hjm⟩ : S β†’* P) = ⟨k, hk1, hkm⟩) variables {M} lemma lift_unique {j : S β†’+* P} (hj : βˆ€ x, j ((algebra_map R S) x) = g x) : lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : lift (map_units S : βˆ€ y : M, is_unit _) x = x := (to_localization_map M S).lift_id _ lemma lift_surjective_iff : surjective (lift hg : S β†’ P) ↔ βˆ€ v : P, βˆƒ x : R Γ— M, v * g x.2 = g x.1 := (to_localization_map M S).lift_surjective_iff hg lemma lift_injective_iff : injective (lift hg : S β†’ P) ↔ βˆ€ x y, algebra_map R S x = algebra_map R S y ↔ g x = g y := (to_localization_map M S).lift_injective_iff hg section map variables {T : submonoid P} {Q : Type*} [comm_ring Q] (hy : M ≀ T.comap g) variables [algebra P Q] [is_localization T Q] section variables (Q) /-- Map a homomorphism `g : R β†’+* P` to `S β†’+* Q`, where `S` and `Q` are localizations of `R` and `P` at `M` and `T` respectively, such that `g(M) βŠ† T`. We send `z : S` to `algebra_map P Q (g x) * (algebra_map P Q (g y))⁻¹`, where `(x, y) : R Γ— M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : R β†’+* P) (hy : M ≀ T.comap g) : S β†’+* Q := @lift R _ M _ _ _ _ _ _ ((algebra_map P Q).comp g) (Ξ» y, map_units _ ⟨g y, hy y.2⟩) end lemma map_eq (x) : map Q g hy ((algebra_map R S) x) = algebra_map P Q (g x) := lift_eq (Ξ» y, map_units _ ⟨g y, hy y.2⟩) x @[simp] lemma map_comp : (map Q g hy).comp (algebra_map R S) = (algebra_map P Q).comp g := lift_comp $ Ξ» y, map_units _ ⟨g y, hy y.2⟩ lemma map_mk' (x) (y : M) : map Q g hy (mk' S x y) = mk' Q (g x) ⟨g y, hy y.2⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom _ (Ξ» y, hy y.2) _ _ (to_localization_map T Q) _ _ @[simp] lemma map_id (z : S) (h : M ≀ M.comap (ring_hom.id R) := le_refl M) : map S (ring_hom.id _) h z = z := lift_id _ lemma map_unique (j : S β†’+* Q) (hj : βˆ€ x : R, j (algebra_map R S x) = algebra_map P Q (g x)) : map Q g hy = j := lift_unique (Ξ» y, map_units _ ⟨g y, hy y.2⟩) hj /-- If `comm_ring` homs `g : R β†’+* P, l : P β†’+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_comp_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] [algebra A W] [is_localization U W] {l : P β†’+* A} (hl : T ≀ U.comap l) : (map W l hl).comp (map Q g hy : S β†’+* _) = map W (l.comp g) (Ξ» x hx, hl (hy hx)) := ring_hom.ext $ Ξ» x, @submonoid.localization_map.map_map _ _ _ _ _ P _ (to_localization_map M S) g _ _ _ _ _ _ _ _ _ _ (to_localization_map U W) l _ x /-- If `comm_ring` homs `g : R β†’+* P, l : P β†’+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] [algebra A W] [is_localization U W] {l : P β†’+* A} (hl : T ≀ U.comap l) (x : S) : map W l hl (map Q g hy x) = map W (l.comp g) (Ξ» x hx, hl (hy hx)) x := by rw ←map_comp_map hy hl; refl section variables (S Q) /-- If `S`, `Q` are localizations of `R` and `P` at submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ @[simps] noncomputable def ring_equiv_of_ring_equiv (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := have H' : T.map h.symm.to_monoid_hom = M, by { rw [← M.map_id, ← H, submonoid.map_map], congr, ext, apply h.symm_apply_apply }, { to_fun := map Q (h : R β†’+* P) (M.le_comap_of_map_le (le_of_eq H)), inv_fun := map S (h.symm : P β†’+* R) (T.le_comap_of_map_le (le_of_eq H')), left_inv := Ξ» x, by { rw [map_map, map_unique _ (ring_hom.id _), ring_hom.id_apply], intro x, convert congr_arg (algebra_map R S) (h.symm_apply_apply x).symm }, right_inv := Ξ» x, by { rw [map_map, map_unique _ (ring_hom.id _), ring_hom.id_apply], intro x, convert congr_arg (algebra_map P Q) (h.apply_symm_apply x).symm }, .. map Q (h : R β†’+* P) _ } end lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (ring_equiv_of_ring_equiv S Q j H : S β†’+* Q) = map Q (j : R β†’+* P) (M.le_comap_of_map_le (le_of_eq H)) := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : ring_equiv_of_ring_equiv S Q j H ((algebra_map R S) x) = algebra_map P Q (j x) := map_eq _ _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x : R) (y : M) : ring_equiv_of_ring_equiv S Q j H (mk' S x y) = mk' Q (j x) ⟨j y, show j y ∈ T, from H β–Έ set.mem_image_of_mem j y.2⟩ := map_mk' _ _ _ end map section alg_equiv variables {Q : Type*} [comm_ring Q] [algebra R Q] [is_localization M Q] section variables (M S Q) /-- If `S`, `Q` are localizations of `R` at the submonoid `M` respectively, there is an isomorphism of localizations `S ≃ₐ[R] Q`. -/ @[simps] noncomputable def alg_equiv : S ≃ₐ[R] Q := { commutes' := ring_equiv_of_ring_equiv_eq _, .. ring_equiv_of_ring_equiv S Q (ring_equiv.refl R) M.map_id } end @[simp] lemma alg_equiv_mk' (x : R) (y : M) : alg_equiv M S Q (mk' S x y) = mk' Q x y:= map_mk' _ _ _ @[simp] lemma alg_equiv_symm_mk' (x : R) (y : M) : (alg_equiv M S Q).symm (mk' Q x y) = mk' S x y:= map_mk' _ _ _ end alg_equiv end is_localization section variables (M) lemma is_localization_of_alg_equiv [algebra R P] [is_localization M S] (h : S ≃ₐ[R] P) : is_localization M P := begin constructor, { intro y, convert (is_localization.map_units S y).map h.to_alg_hom.to_ring_hom.to_monoid_hom, exact (h.commutes y).symm }, { intro y, obtain ⟨⟨x, s⟩, e⟩ := is_localization.surj M (h.symm y), apply_fun h at e, simp only [h.map_mul, h.apply_symm_apply, h.commutes] at e, exact ⟨⟨x, s⟩, e⟩ }, { intros x y, rw [← h.symm.to_equiv.injective.eq_iff, ← is_localization.eq_iff_exists M S, ← h.symm.commutes, ← h.symm.commutes], refl } end lemma is_localization_iff_of_alg_equiv [algebra R P] (h : S ≃ₐ[R] P) : is_localization M S ↔ is_localization M P := ⟨λ _, by exactI is_localization_of_alg_equiv M h, Ξ» _, by exactI is_localization_of_alg_equiv M h.symm⟩ lemma is_localization_iff_of_ring_equiv (h : S ≃+* P) : is_localization M S ↔ @@is_localization _ M P _ (h.to_ring_hom.comp $ algebra_map R S).to_algebra := begin letI := (h.to_ring_hom.comp $ algebra_map R S).to_algebra, exact is_localization_iff_of_alg_equiv M { commutes' := Ξ» _, rfl, ..h }, end variable (S) lemma is_localization_of_base_ring_equiv [is_localization M S] (h : R ≃+* P) : @@is_localization _ (M.map h.to_monoid_hom) S _ ((algebra_map R S).comp h.symm.to_ring_hom).to_algebra := begin constructor, { rintros ⟨_, ⟨y, hy, rfl⟩⟩, convert is_localization.map_units S ⟨y, hy⟩, dsimp only [ring_hom.algebra_map_to_algebra, ring_hom.comp_apply], exact congr_arg _ (h.symm_apply_apply _) }, { intro y, obtain ⟨⟨x, s⟩, e⟩ := is_localization.surj M y, refine ⟨⟨h x, _, _, s.prop, rfl⟩, _⟩, dsimp only [ring_hom.algebra_map_to_algebra, ring_hom.comp_apply] at ⊒ e, convert e; exact h.symm_apply_apply _ }, { intros x y, rw [ring_hom.algebra_map_to_algebra, ring_hom.comp_apply, ring_hom.comp_apply, is_localization.eq_iff_exists M S], simp_rw ← h.to_equiv.apply_eq_iff_eq, change (βˆƒ (c : M), h (h.symm x * c) = h (h.symm y * c)) ↔ _, simp only [ring_equiv.apply_symm_apply, ring_equiv.map_mul], exact ⟨λ ⟨c, e⟩, ⟨⟨_, _, c.prop, rfl⟩, e⟩, Ξ» ⟨⟨_, c, h, eβ‚βŸ©, eβ‚‚βŸ©, ⟨⟨_, h⟩, e₁.symm β–Έ eβ‚‚βŸ©βŸ© } end lemma is_localization_iff_of_base_ring_equiv (h : R ≃+* P) : is_localization M S ↔ @@is_localization _ (M.map h.to_monoid_hom) S _ ((algebra_map R S).comp h.symm.to_ring_hom).to_algebra := begin refine ⟨λ _, by exactI is_localization_of_base_ring_equiv _ _ h, _⟩, letI := ((algebra_map R S).comp h.symm.to_ring_hom).to_algebra, intro H, convert @@is_localization_of_base_ring_equiv _ _ _ _ _ _ H h.symm, { erw [submonoid.map_equiv_eq_comap_symm, submonoid.comap_map_eq_of_injective], exact h.to_equiv.injective }, rw [ring_hom.algebra_map_to_algebra, ring_hom.comp_assoc], simp only [ring_hom.comp_id, ring_equiv.symm_symm, ring_equiv.symm_to_ring_hom_comp_to_ring_hom], apply algebra.algebra_ext, intro r, rw ring_hom.algebra_map_to_algebra end end section away variables (x : R) /-- Given `x : R`, the typeclass `is_localization.away x S` states that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ abbreviation away (S : Type*) [comm_ring S] [algebra R S] := is_localization (submonoid.powers x) S namespace away variables [is_localization.away x S] /-- Given `x : R` and a localization map `F : R β†’+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def inv_self : S := mk' S (1 : R) ⟨x, submonoid.mem_powers _⟩ variables {g : R β†’+* P} /-- Given `x : R`, a localization map `F : R β†’+* S` away from `x`, and a map of `comm_ring`s `g : R β†’+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : β„•` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def lift (hg : is_unit (g x)) : S β†’+* P := is_localization.lift $ Ξ» (y : submonoid.powers x), show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (pow_monoid_hom n) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : R) : lift x hg ((algebra_map R S) a) = g a := lift_eq _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (lift x hg).comp (algebra_map R S) = g := lift_comp _ /-- Given `x y : R` and localizations `S`, `P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ noncomputable def away_to_away_right (y : R) [algebra R P] [is_localization.away (x * y) P] : S β†’+* P := lift x $ show is_unit ((algebra_map R P) x), from is_unit_of_mul_eq_one ((algebra_map R P) x) (mk' P y ⟨x * y, submonoid.mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] variables (S) (Q : Type*) [comm_ring Q] [algebra P Q] /-- Given a map `f : R β†’+* S` and an element `r : R`, we may construct a map `Rα΅£ β†’+* Sα΅£`. -/ noncomputable def map (f : R β†’+* P) (r : R) [is_localization.away r S] [is_localization.away (f r) Q] : S β†’+* Q := is_localization.map Q f (show submonoid.powers r ≀ (submonoid.powers (f r)).comap f, by { rintros x ⟨n, rfl⟩, use n, simp }) end away end away section inv_submonoid variables (M S) /-- The submonoid of `S = M⁻¹R` consisting of `{ 1 / x | x ∈ M }`. -/ def inv_submonoid : submonoid S := (M.map (algebra_map R S : R β†’* S)).left_inv variable [is_localization M S] lemma submonoid_map_le_is_unit : M.map (algebra_map R S : R β†’* S) ≀ is_unit.submonoid S := by { rintros _ ⟨a, ha, rfl⟩, exact is_localization.map_units S ⟨_, ha⟩ } /-- There is an equivalence of monoids between the image of `M` and `inv_submonoid`. -/ noncomputable abbreviation equiv_inv_submonoid : M.map (algebra_map R S : R β†’* S) ≃* inv_submonoid M S := ((M.map (algebra_map R S : R β†’* S)).left_inv_equiv (submonoid_map_le_is_unit M S)).symm /-- There is a canonical map from `M` to `inv_submonoid` sending `x` to `1 / x`. -/ noncomputable def to_inv_submonoid : M β†’* inv_submonoid M S := (equiv_inv_submonoid M S).to_monoid_hom.comp ((algebra_map R S : R β†’* S).submonoid_map M) lemma to_inv_submonoid_surjective : function.surjective (to_inv_submonoid M S) := function.surjective.comp (equiv.surjective _) (monoid_hom.submonoid_map_surjective _ _) @[simp] lemma to_inv_submonoid_mul (m : M) : (to_inv_submonoid M S m : S) * (algebra_map R S m) = 1 := submonoid.left_inv_equiv_symm_mul _ _ _ @[simp] lemma mul_to_inv_submonoid (m : M) : (algebra_map R S m) * (to_inv_submonoid M S m : S) = 1 := submonoid.mul_left_inv_equiv_symm _ _ ⟨_, _⟩ @[simp] lemma smul_to_inv_submonoid (m : M) : m β€’ (to_inv_submonoid M S m : S) = 1 := by { convert mul_to_inv_submonoid M S m, rw ← algebra.smul_def, refl } variables {S} lemma surj' (z : S) : βˆƒ (r : R) (m : M), z = r β€’ to_inv_submonoid M S m := begin rcases is_localization.surj M z with ⟨⟨r, m⟩, e : z * _ = algebra_map R S r⟩, refine ⟨r, m, _⟩, rw [algebra.smul_def, ← e, mul_assoc], simp, end lemma to_inv_submonoid_eq_mk' (x : M) : (to_inv_submonoid M S x : S) = mk' S 1 x := by { rw ← (is_localization.map_units S x).mul_left_inj, simp } lemma mem_inv_submonoid_iff_exists_mk' (x : S) : x ∈ inv_submonoid M S ↔ βˆƒ m : M, mk' S 1 m = x := begin simp_rw ← to_inv_submonoid_eq_mk', exact ⟨λ h, ⟨_, congr_arg subtype.val (to_inv_submonoid_surjective M S ⟨x, h⟩).some_spec⟩, Ξ» h, h.some_spec β–Έ (to_inv_submonoid M S h.some).prop⟩ end variables (S) lemma span_inv_submonoid : submodule.span R (inv_submonoid M S : set S) = ⊀ := begin rw eq_top_iff, rintros x -, rcases is_localization.surj' M x with ⟨r, m, rfl⟩, exact submodule.smul_mem _ _ (submodule.subset_span (to_inv_submonoid M S m).prop), end lemma finite_type_of_monoid_fg [monoid.fg M] : algebra.finite_type R S := begin have := monoid.fg_of_surjective _ (to_inv_submonoid_surjective M S), rw monoid.fg_iff_submonoid_fg at this, rcases this with ⟨s, hs⟩, refine ⟨⟨s, _⟩⟩, rw eq_top_iff, rintro x -, change x ∈ ((algebra.adjoin R _ : subalgebra R S).to_submodule : set S), rw [algebra.adjoin_eq_span, hs, span_inv_submonoid], trivial end end inv_submonoid variables (M S) include M lemma non_zero_divisors_le_comap [is_localization M S] : non_zero_divisors R ≀ (non_zero_divisors S).comap (algebra_map R S) := begin rintros a ha b (e : b * algebra_map R S a = 0), obtain ⟨x, s, rfl⟩ := mk'_surjective M b, rw [← @mk'_one R _ M, ← mk'_mul, ← (algebra_map R S).map_zero, ← @mk'_one R _ M, is_localization.eq] at e, obtain ⟨c, e⟩ := e, rw [zero_mul, zero_mul, submonoid.coe_one, mul_one, mul_comm x a, mul_assoc, mul_comm] at e, rw mk'_eq_zero_iff, exact ⟨c, ha _ e⟩ end lemma map_non_zero_divisors_le [is_localization M S] : (non_zero_divisors R).map (algebra_map R S).to_monoid_hom ≀ non_zero_divisors S := submonoid.map_le_iff_le_comap.mpr (non_zero_divisors_le_comap M S) end is_localization namespace localization open is_localization /-! ### Constructing a localization at a given submonoid -/ variables {M} section instance [subsingleton R] : subsingleton (localization M) := ⟨λ a b, by { induction a, induction b, congr, refl, refl }⟩ /-- Addition in a ring localization is defined as `⟨a, b⟩ + ⟨c, d⟩ = ⟨b * c + d * a, b * d⟩`. Should not be confused with `add_localization.add`, which is defined as `⟨a, b⟩ + ⟨c, d⟩ = ⟨a + c, b + d⟩`. -/ @[irreducible] protected def add (z w : localization M) : localization M := localization.lift_onβ‚‚ z w (Ξ» a b c d, mk ((b : R) * c + d * a) (b * d)) $ Ξ» a a' b b' c c' d d' h1 h2, mk_eq_mk_iff.2 begin rw r_eq_r' at h1 h2 ⊒, cases h1 with tβ‚… htβ‚…, cases h2 with t₆ ht₆, use t₆ * tβ‚…, calc ((b : R) * c + d * a) * (b' * d') * (t₆ * tβ‚…) = (c * d' * t₆) * (b * b' * tβ‚…) + (a * b' * tβ‚…) * (d * d' * t₆) : by ring ... = (b' * c' + d' * a') * (b * d) * (t₆ * tβ‚…) : by rw [ht₆, htβ‚…]; ring end instance : has_add (localization M) := ⟨localization.add⟩ lemma add_mk (a b c d) : (mk a b : localization M) + mk c d = mk (b * c + d * a) (b * d) := by { unfold has_add.add localization.add, apply lift_onβ‚‚_mk } lemma add_mk_self (a b c) : (mk a b : localization M) + mk c b = mk (a + c) b := begin rw [add_mk, mk_eq_mk_iff, r_eq_r'], refine (r' M).symm ⟨1, _⟩, simp only [submonoid.coe_one, submonoid.coe_mul], ring end /-- Negation in a ring localization is defined as `-⟨a, b⟩ = ⟨-a, b⟩`. -/ @[irreducible] protected def neg (z : localization M) : localization M := localization.lift_on z (Ξ» a b, mk (-a) b) $ Ξ» a b c d h, mk_eq_mk_iff.2 begin rw r_eq_r' at h ⊒, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring_nf, end instance : has_neg (localization M) := ⟨localization.neg⟩ lemma neg_mk (a b) : -(mk a b : localization M) = mk (-a) b := by { unfold has_neg.neg localization.neg, apply lift_on_mk } /-- The zero element in a ring localization is defined as `⟨0, 1⟩`. Should not be confused with `add_localization.zero` which is `⟨0, 0⟩`. -/ @[irreducible] protected def zero : localization M := mk 0 1 instance : has_zero (localization M) := ⟨localization.zero⟩ lemma mk_zero (b) : (mk 0 b : localization M) = 0 := calc mk 0 b = mk 0 1 : mk_eq_mk_iff.mpr (r_of_eq (by simp)) ... = 0 : by unfold has_zero.zero localization.zero lemma lift_on_zero {p : Type*} (f : βˆ€ (a : R) (b : M), p) (H) : lift_on 0 f H = f 0 1 := by rw [← mk_zero 1, lift_on_mk] private meta def tac := `[ { intros, simp only [add_mk, localization.mk_mul, neg_mk, ← mk_zero 1], refine mk_eq_mk_iff.mpr (r_of_eq _), simp only [submonoid.coe_mul, prod.fst_mul, prod.snd_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), npow := localization.npow _, nsmul := (β€’), nsmul_zero' := Ξ» x, localization.induction_on x (Ξ» x, by simp only [smul_mk, zero_nsmul, mk_zero]), nsmul_succ' := Ξ» n x, localization.induction_on x (Ξ» x, by simp only [smul_mk, succ_nsmul, add_mk_self]), zsmul := (β€’), zsmul_zero' := Ξ» x, localization.induction_on x (Ξ» x, by simp only [smul_mk, zero_zsmul, mk_zero]), zsmul_succ' := Ξ» n x, localization.induction_on x (Ξ» x, by simp [smul_mk, add_mk_self, -mk_eq_monoid_of_mk', add_comm (n : β„€) 1, add_smul]), zsmul_neg' := Ξ» n x, localization.induction_on x (Ξ» x, by { rw [smul_mk, smul_mk, neg_mk, ← neg_smul], refl }), add_assoc := Ξ» m n k, localization.induction_on₃ m n k (by tac), zero_add := Ξ» y, localization.induction_on y (by tac), add_zero := Ξ» y, localization.induction_on y (by tac), neg := has_neg.neg, sub := Ξ» x y, x + -y, sub_eq_add_neg := Ξ» x y, rfl, add_left_neg := Ξ» y, by exact localization.induction_on y (by tac), add_comm := Ξ» y z, localization.induction_onβ‚‚ z y (by tac), left_distrib := Ξ» m n k, localization.induction_on₃ m n k (by tac), right_distrib := Ξ» m n k, localization.induction_on₃ m n k (by tac), ..localization.comm_monoid M } instance {S : Type*} [monoid S] [distrib_mul_action S R] [is_scalar_tower S R R] : distrib_mul_action S (localization M) := { smul_zero := Ξ» s, by simp only [←localization.mk_zero 1, localization.smul_mk, smul_zero], smul_add := Ξ» s x y, localization.induction_onβ‚‚ x y $ prod.rec $ by exact Ξ» r₁ x₁, prod.rec $ by exact Ξ» rβ‚‚ xβ‚‚, by simp only [localization.smul_mk, localization.add_mk, smul_add, mul_comm _ (s β€’ _), mul_comm _ r₁, mul_comm _ rβ‚‚, smul_mul_assoc] } instance {S : Type*} [semiring S] [mul_semiring_action S R] [is_scalar_tower S R R] : mul_semiring_action S (localization M) := { ..localization.mul_distrib_mul_action } instance {S : Type*} [semiring S] [module S R] [is_scalar_tower S R R] : module S (localization M) := { zero_smul := localization.ind $ prod.rec $ by { intros, simp only [localization.smul_mk, zero_smul, mk_zero] }, add_smul := Ξ» s₁ sβ‚‚, localization.ind $ prod.rec $ by { intros, simp only [localization.smul_mk, add_smul, add_mk_self] }, ..localization.distrib_mul_action } instance {S : Type*} [comm_semiring S] [algebra S R] : algebra S (localization M) := { to_ring_hom := ring_hom.comp { to_fun := (monoid_of M).to_map, map_zero' := by rw [← mk_zero (1 : M), mk_one_eq_monoid_of_mk], map_add' := Ξ» x y, by simp only [← mk_one_eq_monoid_of_mk, add_mk, submonoid.coe_one, one_mul, add_comm], .. localization.monoid_of M } (algebra_map S R), smul_def' := Ξ» s, localization.ind $ prod.rec $ begin intros r x, dsimp, simp only [←mk_one_eq_monoid_of_mk, mk_mul, localization.smul_mk, one_mul, algebra.smul_def], end, commutes' := Ξ» s, localization.ind $ prod.rec $ begin intros r x, dsimp, simp only [←mk_one_eq_monoid_of_mk, mk_mul, localization.smul_mk, one_mul, mul_one, algebra.commutes], end } instance : is_localization M (localization M) := { map_units := (localization.monoid_of M).map_units, surj := (localization.monoid_of M).surj, eq_iff_exists := Ξ» _ _, (localization.monoid_of M).eq_iff_exists } end @[simp] lemma to_localization_map_eq_monoid_of : to_localization_map M (localization M) = monoid_of M := rfl lemma monoid_of_eq_algebra_map (x) : (monoid_of M).to_map x = algebra_map R (localization M) x := rfl lemma mk_one_eq_algebra_map (x) : mk x 1 = algebra_map R (localization M) x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = is_localization.mk' (localization M) x y := by rw [mk_eq_monoid_of_mk'_apply, mk', to_localization_map_eq_monoid_of] @[simp] lemma mk_eq_mk' : (mk : R β†’ M β†’ localization M) = is_localization.mk' (localization M) := mk_eq_monoid_of_mk' variables [is_localization M S] section variables (M S) /-- The localization of `R` at `M` as a quotient type is isomorphic to any other localization. -/ @[simps] noncomputable def alg_equiv : localization M ≃ₐ[R] S := is_localization.alg_equiv M _ _ end @[simp] lemma alg_equiv_mk' (x : R) (y : M) : alg_equiv M S (mk' (localization M) x y) = mk' S x y := alg_equiv_mk' _ _ @[simp] lemma alg_equiv_symm_mk' (x : R) (y : M) : (alg_equiv M S).symm (mk' S x y) = mk' (localization M) x y := alg_equiv_symm_mk' _ _ lemma alg_equiv_mk (x y) : alg_equiv M S (mk x y) = mk' S x y := by rw [mk_eq_mk', alg_equiv_mk'] lemma alg_equiv_symm_mk (x : R) (y : M) : (alg_equiv M S).symm (mk' S x y) = mk x y := by rw [mk_eq_mk', alg_equiv_symm_mk'] /-- Given a map `f : R β†’+* S` and an element `r : R`, such that `f r` is invertible, we may construct a map `Rα΅£ β†’+* S`. -/ noncomputable abbreviation away_lift (f : R β†’+* P) (r : R) (hr : is_unit (f r)) : localization.away r β†’+* P := is_localization.away.lift r hr /-- Given a map `f : R β†’+* S` and an element `r : R`, we may construct a map `Rα΅£ β†’+* Sα΅£`. -/ noncomputable abbreviation away_map (f : R β†’+* P) (r : R) : localization.away r β†’+* localization.away (f r) := is_localization.away.map _ _ f r end localization variables {M} section at_prime variables (I : ideal R) [hp : I.is_prime] include hp namespace ideal /-- The complement of a prime ideal `I βŠ† R` is a submonoid of `R`. -/ def prime_compl : submonoid R := { carrier := (Iᢜ : set R), one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl, mul_mem' := Ξ» x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny } end ideal variables (S) /-- Given a prime ideal `P`, the typeclass `is_localization.at_prime S P` states that `S` is isomorphic to the localization of `R` at the complement of `P`. -/ protected abbreviation is_localization.at_prime := is_localization I.prime_compl S /-- Given a prime ideal `P`, `localization.at_prime S P` is a localization of `R` at the complement of `P`, as a quotient type. -/ protected abbreviation localization.at_prime := localization I.prime_compl namespace is_localization theorem at_prime.local_ring [is_localization.at_prime S I] : local_ring S := local_of_nonunits_ideal (Ξ» hze, begin rw [←(algebra_map R S).map_one, ←(algebra_map R S).map_zero] at hze, obtain ⟨t, ht⟩ := (eq_iff_exists I.prime_compl S).1 hze, exact ((show (t : R) βˆ‰ I, from t.2) (have htz : (t : R) = 0, by simpa using ht.symm, htz.symm β–Έ I.zero_mem)) end) (begin intros x hx y hy hu, cases is_unit_iff_exists_inv.1 hu with z hxyz, have : βˆ€ {r : R} {s : I.prime_compl}, mk' S r s ∈ nonunits S β†’ r ∈ I, from Ξ» (r : R) (s : I.prime_compl), not_imp_comm.1 (Ξ» nr, is_unit_iff_exists_inv.2 ⟨mk' S ↑s (⟨r, nr⟩ : I.prime_compl), mk'_mul_mk'_eq_one' _ _ nr⟩), rcases mk'_surjective I.prime_compl x with ⟨rx, sx, hrx⟩, rcases mk'_surjective I.prime_compl y with ⟨ry, sy, hry⟩, rcases mk'_surjective I.prime_compl z with ⟨rz, sz, hrz⟩, rw [←hrx, ←hry, ←hrz, ←mk'_add, ←mk'_mul, ←mk'_self S I.prime_compl.one_mem] at hxyz, rw ←hrx at hx, rw ←hry at hy, obtain ⟨t, ht⟩ := is_localization.eq.1 hxyz, simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht, rw [←sub_eq_zero, ←sub_mul] at ht, have hr := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right t.2, rw sub_eq_add_neg at hr, have := I.neg_mem_iff.1 ((ideal.add_mem_iff_right _ _).1 hr), { exact not_or (mt hp.mem_or_mem (not_or sx.2 sy.2)) sz.2 (hp.mem_or_mem this)}, { exact I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ (this hx)) (I.mul_mem_right _ (this hy)))} end) end is_localization namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ instance at_prime.local_ring : local_ring (localization I.prime_compl) := is_localization.at_prime.local_ring (localization I.prime_compl) I end localization end at_prime namespace is_localization variables [is_localization M S] section ideals variables (M) (S) include M /-- Explicit characterization of the ideal given by `ideal.map (algebra_map R S) I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ private def map_ideal (I : ideal R) : ideal S := { carrier := { z : S | βˆƒ x : I Γ— M, z * algebra_map R S x.2 = algebra_map R S x.1}, zero_mem' := ⟨⟨0, 1⟩, by simp⟩, add_mem' := begin rintros a b ⟨a', ha⟩ ⟨b', hb⟩, use ⟨a'.2 * b'.1 + b'.2 * a'.1, I.add_mem (I.mul_mem_left _ b'.1.2) (I.mul_mem_left _ a'.1.2)⟩, use a'.2 * b'.2, simp only [ring_hom.map_add, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], rw [add_mul, ← mul_assoc a, ha, mul_comm (algebra_map R S a'.2) (algebra_map R S b'.2), ← mul_assoc b, hb], ring end, smul_mem' := begin rintros c x ⟨x', hx⟩, obtain ⟨c', hc⟩ := is_localization.surj M c, use ⟨c'.1 * x'.1, I.mul_mem_left c'.1 x'.1.2⟩, use c'.2 * x'.2, simp only [←hx, ←hc, smul_eq_mul, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], ring end } theorem mem_map_algebra_map_iff {I : ideal R} {z} : z ∈ ideal.map (algebra_map R S) I ↔ βˆƒ x : I Γ— M, z * algebra_map R S x.2 = algebra_map R S x.1 := begin split, { change _ β†’ z ∈ map_ideal M S I, refine Ξ» h, ideal.mem_Inf.1 h (Ξ» z hz, _), obtain ⟨y, hy⟩ := hz, use ⟨⟨⟨y, hy.left⟩, 1⟩, by simp [hy.right]⟩ }, { rintros ⟨⟨a, s⟩, h⟩, rw [← ideal.unit_mul_mem_iff_mem _ (map_units S s), mul_comm], exact h.symm β–Έ ideal.mem_map_of_mem _ a.2 } end theorem map_comap (J : ideal S) : ideal.map (algebra_map R S) (ideal.comap (algebra_map R S) J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 le_rfl) $ Ξ» x hJ, begin obtain ⟨r, s, hx⟩ := mk'_surjective M x, rw ←hx at ⊒ hJ, exact ideal.mul_mem_right _ _ (ideal.mem_map_of_mem _ (show (algebra_map R S) r ∈ J, from mk'_spec S r s β–Έ J.mul_mem_right ((algebra_map R S) s) hJ)), end theorem comap_map_of_is_prime_disjoint (I : ideal R) (hI : I.is_prime) (hM : disjoint (M : set R) I) : ideal.comap (algebra_map R S) (ideal.map (algebra_map R S) I) = I := begin refine le_antisymm (Ξ» a ha, _) ideal.le_comap_map, rw [ideal.mem_comap, mem_map_algebra_map_iff M S] at ha, obtain ⟨⟨b, s⟩, h⟩ := ha, have : (algebra_map R S) (a * ↑s - b) = 0 := by simpa [sub_eq_zero] using h, rw [← (algebra_map R S).map_zero, eq_iff_exists M S] at this, obtain ⟨c, hc⟩ := this, have : a * s ∈ I, { rw zero_mul at hc, let this : (a * ↑s - ↑b) * ↑c ∈ I := hc.symm β–Έ I.zero_mem, cases hI.mem_or_mem this with h1 h2, { simpa using I.add_mem h1 b.2 }, { exfalso, refine hM ⟨c.2, h2⟩ } }, cases hI.mem_or_mem this with h1 h2, { exact h1 }, { exfalso, refine hM ⟨s.2, h2⟩ } end /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding : ideal S β†ͺo ideal R := { to_fun := Ξ» J, ideal.comap (algebra_map R S) J, inj' := function.left_inverse.injective (map_comap M S), map_rel_iff' := Ξ» J₁ Jβ‚‚, ⟨λ hJ, (map_comap M S) J₁ β–Έ (map_comap M S) Jβ‚‚ β–Έ ideal.map_mono hJ, ideal.comap_mono⟩ } /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ lemma is_prime_iff_is_prime_disjoint (J : ideal S) : J.is_prime ↔ (ideal.comap (algebra_map R S) J).is_prime ∧ disjoint (M : set R) ↑(ideal.comap (algebra_map R S) J) := begin split, { refine Ξ» h, ⟨⟨_, _⟩, Ξ» m hm, h.ne_top (ideal.eq_top_of_is_unit_mem _ hm.2 (map_units S ⟨m, hm.left⟩))⟩, { refine Ξ» hJ, h.ne_top _, rw [eq_top_iff, ← (order_embedding M S).le_iff_le], exact le_of_eq hJ.symm }, { intros x y hxy, rw [ideal.mem_comap, ring_hom.map_mul] at hxy, exact h.mem_or_mem hxy } }, { refine Ξ» h, ⟨λ hJ, h.left.ne_top (eq_top_iff.2 _), _⟩, { rwa [eq_top_iff, ← (order_embedding M S).le_iff_le] at hJ }, { intros x y hxy, obtain ⟨a, s, ha⟩ := mk'_surjective M x, obtain ⟨b, t, hb⟩ := mk'_surjective M y, have : mk' S (a * b) (s * t) ∈ J := by rwa [mk'_mul, ha, hb], rw [mk'_mem_iff, ← ideal.mem_comap] at this, replace this := h.left.mem_or_mem this, rw [ideal.mem_comap, ideal.mem_comap] at this, rwa [← ha, ← hb, mk'_mem_iff, mk'_mem_iff] } } end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ lemma is_prime_of_is_prime_disjoint (I : ideal R) (hp : I.is_prime) (hd : disjoint (M : set R) ↑I) : (ideal.map (algebra_map R S) I).is_prime := begin rw [is_prime_iff_is_prime_disjoint M S, comap_map_of_is_prime_disjoint M S I hp hd], exact ⟨hp, hd⟩ end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime : {p : ideal S // p.is_prime} ≃o {p : ideal R // p.is_prime ∧ disjoint (M : set R) ↑p} := { to_fun := Ξ» p, ⟨ideal.comap (algebra_map R S) p.1, (is_prime_iff_is_prime_disjoint M S p.1).1 p.2⟩, inv_fun := Ξ» p, ⟨ideal.map (algebra_map R S) p.1, is_prime_of_is_prime_disjoint M S p.1 p.2.1 p.2.2⟩, left_inv := Ξ» J, subtype.eq (map_comap M S J), right_inv := Ξ» I, subtype.eq (comap_map_of_is_prime_disjoint M S I.1 I.2.1 I.2.2), map_rel_iff' := Ξ» I I', ⟨λ h, (show I.val ≀ I'.val, from (map_comap M S I.val) β–Έ (map_comap M S I'.val) β–Έ (ideal.map_mono h)), Ξ» h x hx, h hx⟩ } /-- `quotient_map` applied to maximal ideals of a localization is `surjective`. The quotient by a maximal ideal is a field, so inverses to elements already exist, and the localization necessarily maps the equivalence class of the inverse in the localization -/ lemma surjective_quotient_map_of_maximal_of_localization {I : ideal S} [I.is_prime] {J : ideal R} {H : J ≀ I.comap (algebra_map R S)} (hI : (I.comap (algebra_map R S)).is_maximal) : function.surjective (I.quotient_map (algebra_map R S) H) := begin intro s, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective s, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M s, by_cases hM : (ideal.quotient.mk (I.comap (algebra_map R S))) m = 0, { have : I = ⊀, { rw ideal.eq_top_iff_one, rw [ideal.quotient.eq_zero_iff_mem, ideal.mem_comap] at hM, convert I.mul_mem_right (mk' S (1 : R) ⟨m, hm⟩) hM, rw [← mk'_eq_mul_mk'_one, mk'_self] }, exact ⟨0, eq_comm.1 (by simp [ideal.quotient.eq_zero_iff_mem, this])⟩ }, { rw ideal.quotient.maximal_ideal_iff_is_field_quotient at hI, obtain ⟨n, hn⟩ := hI.3 hM, obtain ⟨rn, rfl⟩ := ideal.quotient.mk_surjective n, refine ⟨(ideal.quotient.mk J) (r * rn), _⟩, -- The rest of the proof is essentially just algebraic manipulations to prove the equality rw ← ring_hom.map_mul at hn, replace hn := congr_arg (ideal.quotient_map I (algebra_map R S) le_rfl) hn, simp only [ring_hom.map_one, ideal.quotient_map_mk, ring_hom.map_mul] at hn, rw [ideal.quotient_map_mk, ← sub_eq_zero, ← ring_hom.map_sub, ideal.quotient.eq_zero_iff_mem, ← ideal.quotient.eq_zero_iff_mem, ring_hom.map_sub, sub_eq_zero, mk'_eq_mul_mk'_one], simp only [mul_eq_mul_left_iff, ring_hom.map_mul], exact or.inl (mul_left_cancelβ‚€ (Ξ» hn, hM (ideal.quotient.eq_zero_iff_mem.2 (ideal.mem_comap.2 (ideal.quotient.eq_zero_iff_mem.1 hn)))) (trans hn (by rw [← ring_hom.map_mul, ← mk'_eq_mul_mk'_one, mk'_self, ring_hom.map_one]))) } end end ideals section at_units variables (R) (S) (M) /-- The localization at a module of units is isomorphic to the ring -/ noncomputable def at_units (H : βˆ€ x : M, is_unit (x : R)) : R ≃ₐ[R] S := begin refine alg_equiv.of_bijective (algebra.of_id R S) ⟨_, _⟩, { intros x y hxy, obtain ⟨c, eq⟩ := (is_localization.eq_iff_exists M S).mp hxy, obtain ⟨u, hu⟩ := H c, rwa [← hu, units.mul_left_inj] at eq }, { intros y, obtain ⟨⟨x, s⟩, eq⟩ := is_localization.surj M y, obtain ⟨u, hu⟩ := H s, use x * u.inv, dsimp only [algebra.of_id, ring_hom.to_fun_eq_coe, alg_hom.coe_mk], rw [ring_hom.map_mul, ← eq, ← hu, mul_assoc, ← ring_hom.map_mul], simp } end /-- The localization away from a unit is isomorphic to the ring -/ noncomputable def at_unit (x : R) (e : is_unit x) [is_localization.away x S] : R ≃ₐ[R] S := begin apply at_units R (submonoid.powers x), rintros ⟨xn, n, hxn⟩, obtain ⟨u, hu⟩ := e, rw is_unit_iff_exists_inv, use u.inv ^ n, simp[← hxn, ← hu, ← mul_pow] end /-- The localization at one is isomorphic to the ring. -/ noncomputable def at_one [is_localization.away (1 : R) S] : R ≃ₐ[R] S := @at_unit R _ S _ _ (1 : R) is_unit_one _ end at_units section localization_localization variable (M) variables (N : submonoid S) (T : Type*) [comm_ring T] [algebra R T] section variables [algebra S T] [is_scalar_tower R S T] /-- Localizing wrt `M βŠ† R` and then wrt `N βŠ† S = M⁻¹R` is equal to the localization of `R` wrt this module. See `localization_localization_is_localization`. -/ -- This should only be defined when `S` is the localization `M⁻¹R`, hence the nolint. @[nolint unused_arguments] def localization_localization_submodule : submonoid R := (N βŠ” M.map (algebra_map R S)).comap (algebra_map R S) variables {M N} @[simp] lemma mem_localization_localization_submodule {x : R} : x ∈ localization_localization_submodule M N ↔ βˆƒ (y : N) (z : M), algebra_map R S x = y * algebra_map R S z := begin rw [localization_localization_submodule, submonoid.mem_comap, submonoid.mem_sup], split, { rintros ⟨y, hy, _, ⟨z, hz, rfl⟩, e⟩, exact ⟨⟨y, hy⟩, ⟨z, hz⟩ ,e.symm⟩ }, { rintros ⟨y, z, e⟩, exact ⟨y, y.prop, _, ⟨z, z.prop, rfl⟩, e.symm⟩ } end variables (M N) lemma localization_localization_map_units [is_localization N T] (y : localization_localization_submodule M N) : is_unit (algebra_map R T y) := begin obtain ⟨y', z, eq⟩ := mem_localization_localization_submodule.mp y.prop, rw [is_scalar_tower.algebra_map_apply R S T, eq, ring_hom.map_mul, is_unit.mul_iff], exact ⟨is_localization.map_units T y', (is_localization.map_units _ z).map (algebra_map S T : S β†’* T)⟩, end lemma localization_localization_surj [is_localization N T] (x : T) : βˆƒ (y : R Γ— localization_localization_submodule M N), x * (algebra_map R T y.2) = algebra_map R T y.1 := begin rcases is_localization.surj N x with ⟨⟨y, s⟩, eqβ‚βŸ©, -- x = y / s rcases is_localization.surj M y with ⟨⟨z, t⟩, eqβ‚‚βŸ©, -- y = z / t rcases is_localization.surj M (s : S) with ⟨⟨z', t'⟩, eqβ‚ƒβŸ©, -- s = z' / t' dsimp only at eq₁ eqβ‚‚ eq₃, use z * t', use z' * t, -- x = y / s = (z * t') / (z' * t) { rw mem_localization_localization_submodule, refine ⟨s, t * t', _⟩, rw [ring_hom.map_mul, ← eq₃, mul_assoc, ← ring_hom.map_mul, mul_comm t, submonoid.coe_mul] }, { simp only [subtype.coe_mk, ring_hom.map_mul, is_scalar_tower.algebra_map_apply R S T, ← eq₃, ← eqβ‚‚, ← eq₁], ring }, end lemma localization_localization_eq_iff_exists [is_localization N T] (x y : R) : algebra_map R T x = algebra_map R T y ↔ βˆƒ (c : localization_localization_submodule M N), x * c = y * c := begin rw [is_scalar_tower.algebra_map_apply R S T, is_scalar_tower.algebra_map_apply R S T, is_localization.eq_iff_exists N T], split, { rintros ⟨z, eqβ‚βŸ©, rcases is_localization.surj M (z : S) with ⟨⟨z', s⟩, eqβ‚‚βŸ©, dsimp only at eqβ‚‚, obtain ⟨c, eq₃ : x * z' * ↑ c = y * z' * ↑ c⟩ := (is_localization.eq_iff_exists M S).mp _, swap, { rw [ring_hom.map_mul, ring_hom.map_mul, ← eqβ‚‚, ← mul_assoc, ← mul_assoc, ← eq₁] }, use z' * c, { rw mem_localization_localization_submodule, refine ⟨z, s * c, _⟩, rw [ring_hom.map_mul, ← eqβ‚‚, mul_assoc, ← ring_hom.map_mul, submonoid.coe_mul] }, { simpa only [mul_assoc] using eq₃ } }, { rintro ⟨⟨c, hc⟩, eq₁ : x * c = y * c⟩, rw mem_localization_localization_submodule at hc, rcases hc with ⟨z₁, z, eqβ‚‚βŸ©, use z₁, refine (is_localization.map_units S z).mul_left_inj.mp _, rw [mul_assoc, mul_assoc, ← eqβ‚‚, ← ring_hom.map_mul, ← ring_hom.map_mul, eq₁] } end /-- Given submodules `M βŠ† R` and `N βŠ† S = M⁻¹R`, with `f : R β†’+* S` the localization map, we have `N ⁻¹ S = T = (f⁻¹ (N β€’ f(M))) ⁻¹ R`. I.e., the localization of a localization is a localization. -/ lemma localization_localization_is_localization [is_localization N T] : is_localization (localization_localization_submodule M N) T := { map_units := localization_localization_map_units M N T, surj := localization_localization_surj M N T, eq_iff_exists := localization_localization_eq_iff_exists M N T } include M /-- Given submodules `M βŠ† R` and `N βŠ† S = M⁻¹R`, with `f : R β†’+* S` the localization map, if `N` contains all the units of `S`, then `N ⁻¹ S = T = (f⁻¹ N) ⁻¹ R`. I.e., the localization of a localization is a localization. -/ lemma localization_localization_is_localization_of_has_all_units [is_localization N T] (H : βˆ€ (x : S), is_unit x β†’ x ∈ N) : is_localization (N.comap (algebra_map R S).to_monoid_hom) T := begin convert localization_localization_is_localization M N T, symmetry, rw sup_eq_left, rintros _ ⟨x, hx, rfl⟩, exact H _ (is_localization.map_units _ ⟨x, hx⟩), end /-- Given a submodule `M βŠ† R` and a prime ideal `p` of `S = M⁻¹R`, with `f : R β†’+* S` the localization map, then `T = Sβ‚š` is the localization of `R` at `f⁻¹(p)`. -/ lemma is_localization_is_localization_at_prime_is_localization (p : ideal S) [Hp : p.is_prime] [is_localization.at_prime T p] : is_localization.at_prime T (p.comap (algebra_map R S)) := begin apply localization_localization_is_localization_of_has_all_units M p.prime_compl T, intros x hx hx', exact (Hp.1 : Β¬ _) (p.eq_top_of_is_unit_mem hx' hx), end instance (p : ideal (localization M)) [p.is_prime] : algebra R (localization.at_prime p) := localization.algebra instance (p : ideal (localization M)) [p.is_prime] : is_scalar_tower R (localization M) (localization.at_prime p) := is_scalar_tower.of_algebra_map_eq' rfl instance localization_localization_at_prime_is_localization (p : ideal (localization M)) [p.is_prime] : is_localization.at_prime (localization.at_prime p) (p.comap (algebra_map R _)) := is_localization_is_localization_at_prime_is_localization M _ _ /-- Given a submodule `M βŠ† R` and a prime ideal `p` of `M⁻¹R`, with `f : R β†’+* S` the localization map, then `(M⁻¹R)β‚š` is isomorphic (as an `R`-algebra) to the localization of `R` at `f⁻¹(p)`. -/ noncomputable def localization_localization_at_prime_iso_localization (p : ideal (localization M)) [p.is_prime] : localization.at_prime (p.comap (algebra_map R _)) ≃ₐ[R] localization.at_prime p := is_localization.alg_equiv (p.comap (algebra_map R _)).prime_compl _ _ end variables (S) /-- Given submonoids `M ≀ N` of `R`, this is the canonical algebra structure of `M⁻¹S` acting on `N⁻¹S`. -/ noncomputable def localization_algebra_of_submonoid_le (M N : submonoid R) (h : M ≀ N) [is_localization M S] [is_localization N T] : algebra S T := (is_localization.lift (Ξ» y, (map_units T βŸ¨β†‘y, h y.prop⟩ : _)) : S β†’+* T).to_algebra /-- If `M ≀ N` are submonoids of `R`, then the natural map `M⁻¹S β†’+* N⁻¹S` commutes with the localization maps -/ lemma localization_is_scalar_tower_of_submonoid_le (M N : submonoid R) (h : M ≀ N) [is_localization M S] [is_localization N T] : @@is_scalar_tower R S T _ (localization_algebra_of_submonoid_le S T M N h).to_has_scalar _ := begin letI := localization_algebra_of_submonoid_le S T M N h, exact is_scalar_tower.of_algebra_map_eq' (is_localization.lift_comp _).symm end noncomputable instance (x : ideal R) [H : x.is_prime] [is_domain R] : algebra (localization.at_prime x) (localization (non_zero_divisors R)) := localization_algebra_of_submonoid_le _ _ x.prime_compl (non_zero_divisors R) (by { intros a ha, rw mem_non_zero_divisors_iff_ne_zero, exact Ξ» h, ha (h.symm β–Έ x.zero_mem) }) /-- If `M ≀ N` are submonoids of `R`, then `N⁻¹S` is also the localization of `M⁻¹S` at `N`. -/ lemma is_localization_of_submonoid_le (M N : submonoid R) (h : M ≀ N) [is_localization M S] [is_localization N T] [algebra S T] [is_scalar_tower R S T] : is_localization (N.map (algebra_map R S).to_monoid_hom) T := { map_units := begin rintro ⟨_, ⟨y, hy, rfl⟩⟩, convert is_localization.map_units T ⟨y, hy⟩, exact (is_scalar_tower.algebra_map_apply _ _ _ _).symm end, surj := Ξ» y, begin obtain ⟨⟨x, s⟩, e⟩ := is_localization.surj N y, refine ⟨⟨algebra_map _ _ x, _, _, s.prop, rfl⟩, _⟩, simpa [← is_scalar_tower.algebra_map_apply] using e end, eq_iff_exists := Ξ» x₁ xβ‚‚, begin obtain ⟨⟨y₁, sβ‚βŸ©, eβ‚βŸ© := is_localization.surj M x₁, obtain ⟨⟨yβ‚‚, sβ‚‚βŸ©, eβ‚‚βŸ© := is_localization.surj M xβ‚‚, refine iff.trans _ (set.exists_image_iff (algebra_map R S) N (Ξ» c, x₁ * c = xβ‚‚ * c)).symm, dsimp only at e₁ eβ‚‚ ⊒, suffices : algebra_map R T (y₁ * sβ‚‚) = algebra_map R T (yβ‚‚ * s₁) ↔ βˆƒ (a : N), algebra_map R S (a * (y₁ * sβ‚‚)) = algebra_map R S (a * (yβ‚‚ * s₁)), { have h₁ := (is_localization.map_units T ⟨_, h s₁.prop⟩).mul_left_inj, have hβ‚‚ := (is_localization.map_units T ⟨_, h sβ‚‚.prop⟩).mul_left_inj, simp only [is_scalar_tower.algebra_map_apply R S T, subtype.coe_mk] at h₁ hβ‚‚, simp only [is_scalar_tower.algebra_map_apply R S T, map_mul, ← e₁, ← eβ‚‚, ← mul_assoc, mul_right_comm _ (algebra_map R S sβ‚‚), mul_right_comm _ (algebra_map S T (algebra_map R S sβ‚‚)), (is_localization.map_units S s₁).mul_left_inj, (is_localization.map_units S sβ‚‚).mul_left_inj] at this, rw [hβ‚‚, h₁] at this, simpa only [mul_comm] using this }, simp_rw [is_localization.eq_iff_exists N T, is_localization.eq_iff_exists M S], split, { rintro ⟨a, e⟩, exact ⟨a, 1, by { convert e using 1; simp; ring }⟩ }, { rintro ⟨a, b, e⟩, exact ⟨a * (⟨_, h b.prop⟩ : N), by { convert e using 1; simp; ring }⟩ } end } /-- If `M ≀ N` are submonoids of `R` such that `βˆ€ x : N, βˆƒ m : R, m * x ∈ M`, then the localization at `N` is equal to the localizaton of `M`. -/ lemma is_localization_of_is_exists_mul_mem (M N : submonoid R) [is_localization M S] (h : M ≀ N) (h' : βˆ€ x : N, βˆƒ m : R, m * x ∈ M) : is_localization N S := { map_units := Ξ» y, begin obtain ⟨m, hm⟩ := h' y, have := is_localization.map_units S ⟨_, hm⟩, erw map_mul at this, exact (is_unit.mul_iff.mp this).2 end, surj := Ξ» z, by { obtain ⟨⟨y, s⟩, e⟩ := is_localization.surj M z, exact ⟨⟨y, _, h s.prop⟩, e⟩ }, eq_iff_exists := Ξ» x₁ xβ‚‚, begin rw is_localization.eq_iff_exists M, refine ⟨λ ⟨x, hx⟩, ⟨⟨_, h x.prop⟩, hx⟩, _⟩, rintros ⟨x, h⟩, obtain ⟨m, hm⟩ := h' x, refine ⟨⟨_, hm⟩, _⟩, simp [mul_comm m, ← mul_assoc, h] end } end localization_localization variables (S) /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `S = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule (I : ideal R) : submodule R S := submodule.map (algebra.linear_map R S) I lemma mem_coe_submodule (I : ideal R) {x : S} : x ∈ coe_submodule S I ↔ βˆƒ y : R, y ∈ I ∧ algebra_map R S y = x := iff.rfl lemma coe_submodule_mono {I J : ideal R} (h : I ≀ J) : coe_submodule S I ≀ coe_submodule S J := submodule.map_mono h @[simp] lemma coe_submodule_bot : coe_submodule S (βŠ₯ : ideal R) = βŠ₯ := by rw [coe_submodule, submodule.map_bot] @[simp] lemma coe_submodule_top : coe_submodule S (⊀ : ideal R) = 1 := by rw [coe_submodule, submodule.map_top, submodule.one_eq_range] @[simp] lemma coe_submodule_sup (I J : ideal R) : coe_submodule S (I βŠ” J) = coe_submodule S I βŠ” coe_submodule S J := submodule.map_sup _ _ _ @[simp] lemma coe_submodule_mul (I J : ideal R) : coe_submodule S (I * J) = coe_submodule S I * coe_submodule S J := submodule.map_mul _ _ (algebra.of_id R S) lemma coe_submodule_fg (hS : function.injective (algebra_map R S)) (I : ideal R) : submodule.fg (coe_submodule S I) ↔ submodule.fg I := ⟨submodule.fg_of_fg_map _ (linear_map.ker_eq_bot.mpr hS), submodule.fg.map _⟩ @[simp] lemma coe_submodule_span (s : set R) : coe_submodule S (ideal.span s) = submodule.span R ((algebra_map R S) '' s) := by { rw [is_localization.coe_submodule, ideal.span, submodule.map_span], refl } @[simp] lemma coe_submodule_span_singleton (x : R) : coe_submodule S (ideal.span {x}) = submodule.span R {(algebra_map R S) x} := by rw [coe_submodule_span, set.image_singleton] variables {g : R β†’+* P} variables {T : submonoid P} (hy : M ≀ T.comap g) {Q : Type*} [comm_ring Q] variables [algebra P Q] [is_localization T Q] lemma map_smul (x : S) (z : R) : map Q g hy (z β€’ x : S) = g z β€’ map Q g hy x := by rw [algebra.smul_def, algebra.smul_def, ring_hom.map_mul, map_eq] section include M lemma is_noetherian_ring (h : is_noetherian_ring R) : is_noetherian_ring S := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at h ⊒, exact order_embedding.well_founded ((is_localization.order_embedding M S).dual) h end end section integer_normalization open polynomial open_locale classical variables (M) {S} /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ noncomputable def coeff_integer_normalization (p : polynomial S) (i : β„•) : R := if hi : i ∈ p.support then classical.some (classical.some_spec (exist_integer_multiples_of_finset M (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) else 0 lemma coeff_integer_normalization_of_not_mem_support (p : polynomial S) (i : β„•) (h : coeff p i = 0) : coeff_integer_normalization M p i = 0 := by simp only [coeff_integer_normalization, h, mem_support_iff, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff] lemma coeff_integer_normalization_mem_support (p : polynomial S) (i : β„•) (h : coeff_integer_normalization M p i β‰  0) : i ∈ p.support := begin contrapose h, rw [ne.def, not_not, coeff_integer_normalization, dif_neg h] end /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ noncomputable def integer_normalization (p : polynomial S) : polynomial R := βˆ‘ i in p.support, monomial i (coeff_integer_normalization M p i) @[simp] lemma integer_normalization_coeff (p : polynomial S) (i : β„•) : (integer_normalization M p).coeff i = coeff_integer_normalization M p i := by simp [integer_normalization, coeff_monomial, coeff_integer_normalization_of_not_mem_support] {contextual := tt} lemma integer_normalization_spec (p : polynomial S) : βˆƒ (b : M), βˆ€ i, algebra_map R S ((integer_normalization M p).coeff i) = (b : R) β€’ p.coeff i := begin use classical.some (exist_integer_multiples_of_finset M (p.support.image p.coeff)), intro i, rw [integer_normalization_coeff, coeff_integer_normalization], split_ifs with hi, { exact classical.some_spec (classical.some_spec (exist_integer_multiples_of_finset M (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) }, { convert (smul_zero _).symm, { apply ring_hom.map_zero }, { exact not_mem_support_iff.mp hi } } end lemma integer_normalization_map_to_map (p : polynomial S) : βˆƒ (b : M), (integer_normalization M p).map (algebra_map R S) = (b : R) β€’ p := let ⟨b, hb⟩ := integer_normalization_spec M p in ⟨b, polynomial.ext (Ξ» i, by { rw [coeff_map, coeff_smul], exact hb i })⟩ variables {R' : Type*} [comm_ring R'] lemma integer_normalization_evalβ‚‚_eq_zero (g : S β†’+* R') (p : polynomial S) {x : R'} (hx : evalβ‚‚ g x p = 0) : evalβ‚‚ (g.comp (algebra_map R S)) x (integer_normalization M p) = 0 := let ⟨b, hb⟩ := integer_normalization_map_to_map M p in trans (evalβ‚‚_map (algebra_map R S) g x).symm (by rw [hb, ← is_scalar_tower.algebra_map_smul S (b : R) p, evalβ‚‚_smul, hx, mul_zero]) lemma integer_normalization_aeval_eq_zero [algebra R R'] [algebra S R'] [is_scalar_tower R S R'] (p : polynomial S) {x : R'} (hx : aeval x p = 0) : aeval x (integer_normalization M p) = 0 := by rw [aeval_def, is_scalar_tower.algebra_map_eq R S R', integer_normalization_evalβ‚‚_eq_zero _ _ _ hx] end integer_normalization variables {R M} (S) {K : Type*} lemma to_map_eq_zero_iff {x : R} (hM : M ≀ non_zero_divisors R) : algebra_map R S x = 0 ↔ x = 0 := begin rw ← (algebra_map R S).map_zero, split; intro h, { cases (eq_iff_exists M S).mp h with c hc, rw zero_mul at hc, exact hM c.2 x hc }, { rw h }, end protected lemma injective (hM : M ≀ non_zero_divisors R) : injective (algebra_map R S) := begin rw ring_hom.injective_iff (algebra_map R S), intros a ha, rwa to_map_eq_zero_iff S hM at ha end protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] (hM : M ≀ non_zero_divisors R) {x : R} (hx : x ∈ non_zero_divisors R) : algebra_map R S x β‰  0 := show (algebra_map R S).to_monoid_with_zero_hom x β‰  0, from map_ne_zero_of_mem_non_zero_divisors (algebra_map R S) (is_localization.injective S hM) hx variables (S Q M) /-- Injectivity of a map descends to the map induced on localizations. -/ lemma map_injective_of_injective (hg : function.injective g) [is_localization (M.map g : submonoid P) Q] (hM : (M.map g : submonoid P) ≀ non_zero_divisors P) : function.injective (map Q g M.le_comap_map : S β†’ Q) := begin rintros x y hxy, obtain ⟨a, b, rfl⟩ := mk'_surjective M x, obtain ⟨c, d, rfl⟩ := mk'_surjective M y, rw [map_mk' _ a b, map_mk' _ c d, mk'_eq_iff_eq] at hxy, refine mk'_eq_iff_eq.2 (congr_arg (algebra_map _ _) (hg _)), convert is_localization.injective _ hM hxy; simp, end variables {S Q M} @[mono] lemma coe_submodule_le_coe_submodule (h : M ≀ non_zero_divisors R) {I J : ideal R} : coe_submodule S I ≀ coe_submodule S J ↔ I ≀ J := submodule.map_le_map_iff_of_injective (is_localization.injective _ h) _ _ @[mono] lemma coe_submodule_strict_mono (h : M ≀ non_zero_divisors R) : strict_mono (coe_submodule S : ideal R β†’ submodule R S) := strict_mono_of_le_iff_le (Ξ» _ _, (coe_submodule_le_coe_submodule h).symm) variables (S) {Q M} lemma coe_submodule_injective (h : M ≀ non_zero_divisors R) : function.injective (coe_submodule S : ideal R β†’ submodule R S) := injective_of_le_imp_le _ (Ξ» _ _, (coe_submodule_le_coe_submodule h).mp) lemma coe_submodule_is_principal {I : ideal R} (h : M ≀ non_zero_divisors R) : (coe_submodule S I).is_principal ↔ I.is_principal := begin split; unfreezingI { rintros ⟨⟨x, hx⟩⟩ }, { have x_mem : x ∈ coe_submodule S I := hx.symm β–Έ submodule.mem_span_singleton_self x, obtain ⟨x, x_mem, rfl⟩ := (mem_coe_submodule _ _).mp x_mem, refine ⟨⟨x, coe_submodule_injective S h _⟩⟩, rw [ideal.submodule_span_eq, hx, coe_submodule_span_singleton] }, { refine ⟨⟨algebra_map R S x, _⟩⟩, rw [hx, ideal.submodule_span_eq, coe_submodule_span_singleton] } end variables {A : Type*} [comm_ring A] [is_domain A] /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. See note [reducible non-instances]. -/ @[reducible] theorem is_domain_of_le_non_zero_divisors [algebra A S] {M : submonoid A} [is_localization M S] (hM : M ≀ non_zero_divisors A) : is_domain S := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases surj M z with x hx, cases surj M w with y hy, have : z * w * algebra_map A S y.2 * algebra_map A S x.2 = algebra_map A S x.1 * algebra_map A S y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, rw [h, zero_mul, zero_mul, ← (algebra_map A S).map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero ((to_map_eq_zero_iff S hM).mp this.symm) with H H, { exact or.inl (eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (eq_zero_of_fst_eq_zero hy H) }, end, exists_pair_ne := ⟨(algebra_map A S) 0, (algebra_map A S) 1, Ξ» h, zero_ne_one (is_localization.injective S hM h)⟩, } /-- The localization at of an integral domain to a set of non-zero elements is an integral domain. See note [reducible non-instances]. -/ @[reducible] theorem is_domain_localization {M : submonoid A} (hM : M ≀ non_zero_divisors A) : is_domain (localization M) := is_domain_of_le_non_zero_divisors _ hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ instance is_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) : is_domain (localization.at_prime P) := is_domain_localization (le_non_zero_divisors_of_no_zero_divisors (not_not_intro P.zero_mem)) namespace at_prime variables (I : ideal R) [hI : I.is_prime] [is_localization.at_prime S I] include hI lemma is_unit_to_map_iff (x : R) : is_unit ((algebra_map R S) x) ↔ x ∈ I.prime_compl := ⟨λ h hx, (is_prime_of_is_prime_disjoint I.prime_compl S I hI disjoint_compl_left).ne_top $ (ideal.map (algebra_map R S) I).eq_top_of_is_unit_mem (ideal.mem_map_of_mem _ hx) h, Ξ» h, map_units S ⟨x, h⟩⟩ -- Can't use typeclasses to infer the `local_ring` instance, so use an `opt_param` instead -- (since `local_ring` is a `Prop`, there should be no unification issues.) lemma to_map_mem_maximal_iff (x : R) (h : _root_.local_ring S := local_ring S I) : algebra_map R S x ∈ local_ring.maximal_ideal S ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal S, mem_nonunits_iff, not_not] using is_unit_to_map_iff S I x lemma is_unit_mk'_iff (x : R) (y : I.prime_compl) : is_unit (mk' S x y) ↔ x ∈ I.prime_compl := ⟨λ h hx, mk'_mem_iff.mpr ((to_map_mem_maximal_iff S I x).mpr hx) h, Ξ» h, is_unit_iff_exists_inv.mpr ⟨mk' S ↑y ⟨x, h⟩, mk'_mul_mk'_eq_one ⟨x, h⟩ y⟩⟩ lemma mk'_mem_maximal_iff (x : R) (y : I.prime_compl) (h : _root_.local_ring S := local_ring S I) : mk' S x y ∈ local_ring.maximal_ideal S ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal S, mem_nonunits_iff, not_not] using is_unit_mk'_iff S I x y end at_prime end is_localization namespace localization open is_localization local attribute [instance] classical.prop_decidable variables (I : ideal R) [hI : I.is_prime] include hI variables {I} /-- The unique maximal ideal of the localization at `I.prime_compl` lies over the ideal `I`. -/ lemma at_prime.comap_maximal_ideal : ideal.comap (algebra_map R (localization.at_prime I)) (local_ring.maximal_ideal (localization I.prime_compl)) = I := ideal.ext $ Ξ» x, by simpa only [ideal.mem_comap] using at_prime.to_map_mem_maximal_iff _ I x /-- The image of `I` in the localization at `I.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ lemma at_prime.map_eq_maximal_ideal : ideal.map (algebra_map R (localization.at_prime I)) I = (local_ring.maximal_ideal (localization I.prime_compl)) := begin convert congr_arg (ideal.map _) at_prime.comap_maximal_ideal.symm, rw map_comap I.prime_compl end lemma le_comap_prime_compl_iff {J : ideal P} [hJ : J.is_prime] {f : R β†’+* P} : I.prime_compl ≀ J.prime_compl.comap f ↔ J.comap f ≀ I := ⟨λ h x hx, by { contrapose! hx, exact h hx }, Ξ» h x hx hfxJ, hx (h hfxJ)⟩ variables (I) /-- For a ring hom `f : R β†’+* S` and a prime ideal `J` in `S`, the induced ring hom from the localization of `R` at `J.comap f` to the localization of `S` at `J`. To make this definition more flexible, we allow any ideal `I` of `R` as input, together with a proof that `I = J.comap f`. This can be useful when `I` is not definitionally equal to `J.comap f`. -/ noncomputable def local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R β†’+* P) (hIJ : I = J.comap f) : localization.at_prime I β†’+* localization.at_prime J := is_localization.map (localization.at_prime J) f (le_comap_prime_compl_iff.mpr (ge_of_eq hIJ)) lemma local_ring_hom_to_map (J : ideal P) [hJ : J.is_prime] (f : R β†’+* P) (hIJ : I = J.comap f) (x : R) : local_ring_hom I J f hIJ (algebra_map _ _ x) = algebra_map _ _ (f x) := map_eq _ _ lemma local_ring_hom_mk' (J : ideal P) [hJ : J.is_prime] (f : R β†’+* P) (hIJ : I = J.comap f) (x : R) (y : I.prime_compl) : local_ring_hom I J f hIJ (is_localization.mk' _ x y) = is_localization.mk' (localization.at_prime J) (f x) (⟨f y, le_comap_prime_compl_iff.mpr (ge_of_eq hIJ) y.2⟩ : J.prime_compl) := map_mk' _ _ _ instance is_local_ring_hom_local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R β†’+* P) (hIJ : I = J.comap f) : is_local_ring_hom (local_ring_hom I J f hIJ) := is_local_ring_hom.mk $ Ξ» x hx, begin rcases is_localization.mk'_surjective I.prime_compl x with ⟨r, s, rfl⟩, rw local_ring_hom_mk' at hx, rw at_prime.is_unit_mk'_iff at hx ⊒, exact Ξ» hr, hx ((set_like.ext_iff.mp hIJ r).mp hr), end lemma local_ring_hom_unique (J : ideal P) [hJ : J.is_prime] (f : R β†’+* P) (hIJ : I = J.comap f) {j : localization.at_prime I β†’+* localization.at_prime J} (hj : βˆ€ x : R, j (algebra_map _ _ x) = algebra_map _ _ (f x)) : local_ring_hom I J f hIJ = j := map_unique _ _ hj @[simp] lemma local_ring_hom_id : local_ring_hom I I (ring_hom.id R) (ideal.comap_id I).symm = ring_hom.id _ := local_ring_hom_unique _ _ _ _ (Ξ» x, rfl) @[simp] lemma local_ring_hom_comp {S : Type*} [comm_ring S] (J : ideal S) [hJ : J.is_prime] (K : ideal P) [hK : K.is_prime] (f : R β†’+* S) (hIJ : I = J.comap f) (g : S β†’+* P) (hJK : J = K.comap g) : local_ring_hom I K (g.comp f) (by rw [hIJ, hJK, ideal.comap_comap f g]) = (local_ring_hom J K g hJK).comp (local_ring_hom I J f hIJ) := local_ring_hom_unique _ _ _ _ (Ξ» r, by simp only [function.comp_app, ring_hom.coe_comp, local_ring_hom_to_map]) end localization open is_localization /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ lemma localization_map_bijective_of_field {R Rβ‚˜ : Type*} [comm_ring R] [is_domain R] [comm_ring Rβ‚˜] {M : submonoid R} (hM : (0 : R) βˆ‰ M) (hR : is_field R) [algebra R Rβ‚˜] [is_localization M Rβ‚˜] : function.bijective (algebra_map R Rβ‚˜) := begin refine ⟨is_localization.injective _ (le_non_zero_divisors_of_no_zero_divisors hM), Ξ» x, _⟩, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M x, obtain ⟨n, hn⟩ := hR.mul_inv_cancel (Ξ» hm0, hM (hm0 β–Έ hm) : m β‰  0), exact ⟨r * n, by erw [eq_mk'_iff_mul_eq, ← ring_hom.map_mul, mul_assoc, mul_comm n, hn, mul_one]⟩ end variables (R) {A : Type*} [comm_ring A] [is_domain A] variables (K : Type*) /-- `is_fraction_ring R K` states `K` is the field of fractions of an integral domain `R`. -/ -- TODO: should this extend `algebra` instead of assuming it? abbreviation is_fraction_ring [comm_ring K] [algebra R K] := is_localization (non_zero_divisors R) K /-- The cast from `int` to `rat` as a `fraction_ring`. -/ instance rat.is_fraction_ring : is_fraction_ring β„€ β„š := { map_units := begin rintro ⟨x, hx⟩, rw mem_non_zero_divisors_iff_ne_zero at hx, simpa only [ring_hom.eq_int_cast, is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists := begin intros x y, rw [ring_hom.eq_int_cast, ring_hom.eq_int_cast, int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa mem_non_zero_divisors_iff_ne_zero at hc, end } namespace is_fraction_ring variables {R K} section comm_ring variables [comm_ring K] [algebra R K] [is_fraction_ring R K] [algebra A K] [is_fraction_ring A K] lemma to_map_eq_zero_iff {x : R} : algebra_map R K x = 0 ↔ x = 0 := to_map_eq_zero_iff _ (le_of_eq rfl) variables (R K) protected theorem injective : function.injective (algebra_map R K) := is_localization.injective _ (le_of_eq rfl) variables {R K} @[simp, mono] lemma coe_submodule_le_coe_submodule {I J : ideal R} : coe_submodule K I ≀ coe_submodule K J ↔ I ≀ J := is_localization.coe_submodule_le_coe_submodule le_rfl @[mono] lemma coe_submodule_strict_mono : strict_mono (coe_submodule K : ideal R β†’ submodule R K) := strict_mono_of_le_iff_le (Ξ» _ _, coe_submodule_le_coe_submodule.symm) @[priority 100] instance [no_zero_divisors K] : no_zero_smul_divisors R K := no_zero_smul_divisors.of_algebra_map_injective $ is_fraction_ring.injective R K variables (R K) lemma coe_submodule_injective : function.injective (coe_submodule K : ideal R β†’ submodule R K) := injective_of_le_imp_le _ (Ξ» _ _, (coe_submodule_le_coe_submodule).mp) @[simp] lemma coe_submodule_is_principal {I : ideal R} : (coe_submodule K I).is_principal ↔ I.is_principal := is_localization.coe_submodule_is_principal _ le_rfl variables {R K} protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] {x : R} (hx : x ∈ non_zero_divisors R) : algebra_map R K x β‰  0 := is_localization.to_map_ne_zero_of_mem_non_zero_divisors _ le_rfl hx variables (A) /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ protected theorem is_domain : is_domain K := is_domain_of_le_non_zero_divisors K (le_refl (non_zero_divisors A)) local attribute [instance] classical.dec_eq /-- The inverse of an element in the field of fractions of an integral domain. -/ @[irreducible] protected noncomputable def inv (z : K) : K := if h : z = 0 then 0 else mk' K ↑(sec (non_zero_divisors A) z).2 ⟨(sec _ z).1, mem_non_zero_divisors_iff_ne_zero.2 $ Ξ» h0, h $ eq_zero_of_fst_eq_zero (sec_spec (non_zero_divisors A) z) h0⟩ local attribute [semireducible] is_fraction_ring.inv protected lemma mul_inv_cancel (x : K) (hx : x β‰  0) : x * is_fraction_ring.inv A x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (map_units K ⟨(sec _ x).1, mem_non_zero_divisors_iff_ne_zero.2 $ Ξ» h0, hx $ eq_zero_of_fst_eq_zero (sec_spec (non_zero_divisors A) x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (mk'_sec _ x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. See note [reducible non-instances]. -/ @[reducible] noncomputable def to_field : field K := { inv := is_fraction_ring.inv A, mul_inv_cancel := is_fraction_ring.mul_inv_cancel A, inv_zero := dif_pos rfl, .. is_fraction_ring.is_domain A, .. show comm_ring K, by apply_instance } end comm_ring variables {B : Type*} [comm_ring B] [is_domain B] [field K] {L : Type*} [field L] [algebra A K] [is_fraction_ring A K] {g : A β†’+* L} lemma mk'_mk_eq_div {r s} (hs : s ∈ non_zero_divisors A) : mk' K r ⟨s, hs⟩ = algebra_map A K r / algebra_map A K s := mk'_eq_iff_eq_mul.2 $ (div_mul_cancel (algebra_map A K r) (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors hs)).symm @[simp] lemma mk'_eq_div {r} (s : non_zero_divisors A) : mk' K r s = algebra_map A K r / algebra_map A K s := mk'_mk_eq_div s.2 lemma div_surjective (z : K) : βˆƒ (x y : A) (hy : y ∈ non_zero_divisors A), algebra_map _ _ x / algebra_map _ _ y = z := let ⟨x, ⟨y, hy⟩, h⟩ := mk'_surjective (non_zero_divisors A) z in ⟨x, y, hy, by rwa mk'_eq_div at h⟩ lemma is_unit_map_of_injective (hg : function.injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ show g.to_monoid_with_zero_hom y β‰  0, from map_ne_zero_of_mem_non_zero_divisors g hg y.2 /-- Given an integral domain `A` with field of fractions `K`, and an injective ring hom `g : A β†’+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A Γ— (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K β†’+* L := lift $ Ξ» (y : non_zero_divisors A), is_unit_map_of_injective hg y /-- Given an integral domain `A` with field of fractions `K`, and an injective ring hom `g : A β†’+* L` where `L` is a field, the field hom induced from `K` to `L` maps `x` to `g x` for all `x : A`. -/ @[simp] lemma lift_algebra_map (hg : injective g) (x) : lift hg (algebra_map A K x) = g x := lift_eq _ _ /-- Given an integral domain `A` with field of fractions `K`, and an injective ring hom `g : A β†’+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ lemma lift_mk' (hg : injective g) (x) (y : non_zero_divisors A) : lift hg (mk' K x y) = g x / g y := by simp only [mk'_eq_div, ring_hom.map_div, lift_algebra_map] /-- Given integral domains `A, B` with fields of fractions `K`, `L` and an injective ring hom `j : A β†’+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A Γ— (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map {A B K L : Type*} [comm_ring A] [comm_ring B] [is_domain B] [comm_ring K] [algebra A K] [is_fraction_ring A K] [comm_ring L] [algebra B L] [is_fraction_ring B L] {j : A β†’+* B} (hj : injective j) : K β†’+* L := map L j (show non_zero_divisors A ≀ (non_zero_divisors B).comap j, from non_zero_divisors_le_comap_non_zero_divisors_of_injective j hj) /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A β†’+* K, g : B β†’+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv [algebra B L] [is_fraction_ring B L] (h : A ≃+* B) : K ≃+* L := ring_equiv_of_ring_equiv K L h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end lemma integer_normalization_eq_zero_iff {p : polynomial K} : integer_normalization (non_zero_divisors A) p = 0 ↔ p = 0 := begin refine (polynomial.ext_iff.trans (polynomial.ext_iff.trans _).symm), obtain ⟨⟨b, nonzero⟩, hb⟩ := integer_normalization_spec _ p, split; intros h i, { apply to_map_eq_zero_iff.mp, rw [hb i, h i], apply smul_zero, assumption }, { have hi := h i, rw [polynomial.coeff_zero, ← @to_map_eq_zero_iff A _ K, hb i, algebra.smul_def] at hi, apply or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hi), intro h, apply mem_non_zero_divisors_iff_ne_zero.mp nonzero, exact to_map_eq_zero_iff.mp h } end section variables (A K) (C : Type*) variables [comm_ring C] /-- An element of a ring is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma is_algebraic_iff [algebra A C] [algebra K C] [is_scalar_tower A K C] {x : C} : is_algebraic A x ↔ is_algebraic K x := begin split; rintros ⟨p, hp, px⟩, { refine ⟨p.map (algebra_map A K), Ξ» h, hp (polynomial.ext (Ξ» i, _)), _⟩, { have : algebra_map A K (p.coeff i) = 0 := trans (polynomial.coeff_map _ _).symm (by simp [h]), exact to_map_eq_zero_iff.mp this }, { rwa is_scalar_tower.aeval_apply _ K at px } }, { exact ⟨integer_normalization _ p, mt integer_normalization_eq_zero_iff.mp hp, integer_normalization_aeval_eq_zero _ p px⟩ }, end variables {A K C} /-- A ring is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma comap_is_algebraic_iff [algebra A C] [algebra K C] [is_scalar_tower A K C] : algebra.is_algebraic A C ↔ algebra.is_algebraic K C := ⟨λ h x, (is_algebraic_iff A K C).mp (h x), Ξ» h x, (is_algebraic_iff A K C).mpr (h x)⟩ end section num_denom variables (A) [unique_factorization_monoid A] lemma exists_reduced_fraction (x : K) : βˆƒ (a : A) (b : non_zero_divisors A), (βˆ€ {d}, d ∣ a β†’ d ∣ b β†’ is_unit d) ∧ mk' K a b = x := begin obtain ⟨⟨b, b_nonzero⟩, a, hab⟩ := exists_integer_multiple (non_zero_divisors A) x, obtain ⟨a', b', c', no_factor, rfl, rfl⟩ := unique_factorization_monoid.exists_reduced_factors' a b (mem_non_zero_divisors_iff_ne_zero.mp b_nonzero), obtain ⟨c'_nonzero, b'_nonzero⟩ := mul_mem_non_zero_divisors.mp b_nonzero, refine ⟨a', ⟨b', b'_nonzero⟩, @no_factor, _⟩, refine mul_left_cancelβ‚€ (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors b_nonzero) _, simp only [subtype.coe_mk, ring_hom.map_mul, algebra.smul_def] at *, erw [←hab, mul_assoc, mk'_spec' _ a' ⟨b', b'_nonzero⟩], end /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ noncomputable def num (x : K) : A := classical.some (exists_reduced_fraction A x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ noncomputable def denom (x : K) : non_zero_divisors A := classical.some (classical.some_spec (exists_reduced_fraction A x)) lemma num_denom_reduced (x : K) : βˆ€ {d}, d ∣ num A x β†’ d ∣ denom A x β†’ is_unit d := (classical.some_spec (classical.some_spec (exists_reduced_fraction A x))).1 @[simp] lemma mk'_num_denom (x : K) : mk' K (num A x) (denom A x) = x := (classical.some_spec (classical.some_spec (exists_reduced_fraction A x))).2 variables {A} lemma num_mul_denom_eq_num_iff_eq {x y : K} : x * algebra_map A K (denom A y) = algebra_map A K (num A y) ↔ x = y := ⟨λ h, by simpa only [mk'_num_denom] using eq_mk'_iff_mul_eq.mpr h, Ξ» h, eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom])⟩ lemma num_mul_denom_eq_num_iff_eq' {x y : K} : y * algebra_map A K (denom A x) = algebra_map A K (num A x) ↔ x = y := ⟨λ h, by simpa only [eq_comm, mk'_num_denom] using eq_mk'_iff_mul_eq.mpr h, Ξ» h, eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom])⟩ lemma num_mul_denom_eq_num_mul_denom_iff_eq {x y : K} : num A y * denom A x = num A x * denom A y ↔ x = y := ⟨λ h, by simpa only [mk'_num_denom] using mk'_eq_of_eq h, Ξ» h, by rw h⟩ lemma eq_zero_of_num_eq_zero {x : K} (h : num A x = 0) : x = 0 := num_mul_denom_eq_num_iff_eq'.mp (by rw [zero_mul, h, ring_hom.map_zero]) lemma is_integer_of_is_unit_denom {x : K} (h : is_unit (denom A x : A)) : is_integer A x := begin cases h with d hd, have d_ne_zero : algebra_map A K (denom A x) β‰  0 := is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors (denom A x).2, use ↑d⁻¹ * num A x, refine trans _ (mk'_num_denom A x), rw [ring_hom.map_mul, ring_hom.map_units_inv, hd], apply mul_left_cancelβ‚€ d_ne_zero, rw [←mul_assoc, mul_inv_cancel d_ne_zero, one_mul, mk'_spec'] end lemma is_unit_denom_of_num_eq_zero {x : K} (h : num A x = 0) : is_unit (denom A x : A) := num_denom_reduced A x (h.symm β–Έ dvd_zero _) dvd_rfl end num_denom variables (S) lemma is_fraction_ring_iff_of_base_ring_equiv (h : R ≃+* P) : is_fraction_ring R S ↔ @@is_fraction_ring P _ S _ ((algebra_map R S).comp h.symm.to_ring_hom).to_algebra := begin delta is_fraction_ring, convert is_localization_iff_of_base_ring_equiv _ _ h, ext x, erw submonoid.map_equiv_eq_comap_symm, simp only [mul_equiv.coe_to_monoid_hom, ring_equiv.to_mul_equiv_eq_coe, submonoid.mem_comap], split, { rintros hx z (hz : z * h.symm x = 0), rw ← h.map_eq_zero_iff, apply hx, simpa only [h.map_zero, h.apply_symm_apply, h.map_mul] using congr_arg h hz }, { rintros (hx : h.symm x ∈ _) z hz, rw ← h.symm.map_eq_zero_iff, apply hx, rw [← h.symm.map_mul, hz, h.symm.map_zero] } end variable (M) lemma is_fraction_ring_of_is_localization (S T : Type*) [comm_ring S] [comm_ring T] [algebra R S] [algebra R T] [algebra S T] [is_scalar_tower R S T] [is_localization M S] [is_fraction_ring R T] (hM : M ≀ non_zero_divisors R) : is_fraction_ring S T := begin have := is_localization_of_submonoid_le S T M (non_zero_divisors R) _, refine @@is_localization_of_is_exists_mul_mem _ _ _ _ _ _ this _ _, { exact map_non_zero_divisors_le M S }, { rintro ⟨x, hx⟩, obtain ⟨⟨y, s⟩, e⟩ := is_localization.surj M x, use algebra_map R S s, rw [mul_comm, subtype.coe_mk, e], refine set.mem_image_of_mem (algebra_map R S) _, intros z hz, apply is_localization.injective S hM, rw map_zero, apply hx, rw [← (map_units S s).mul_left_inj, mul_assoc, e, ← map_mul, hz, map_zero, zero_mul] }, { exact hM } end protected lemma nontrivial (R S : Type*) [comm_ring R] [nontrivial R] [comm_ring S] [algebra R S] [is_fraction_ring R S] : nontrivial S := begin apply nontrivial_of_ne, intro h, apply @zero_ne_one R, exact is_localization.injective S (le_of_eq rfl) (((algebra_map R S).map_zero.trans h).trans (algebra_map R S).map_one.symm), end lemma is_fraction_ring_of_is_domain_of_is_localization [is_domain R] (S T : Type*) [comm_ring S] [comm_ring T] [algebra R S] [algebra R T] [algebra S T] [is_scalar_tower R S T] [is_localization M S] [is_fraction_ring R T] : is_fraction_ring S T := begin haveI := is_fraction_ring.nontrivial R T, haveI := (algebra_map S T).domain_nontrivial, apply is_fraction_ring_of_is_localization M S T, intros x hx, rw mem_non_zero_divisors_iff_ne_zero, intro hx', apply @zero_ne_one S, rw [← (algebra_map R S).map_one, ← @mk'_one R _ M, @comm _ eq, mk'_eq_zero_iff], exact ⟨⟨_, hx⟩, (one_mul x).symm β–Έ hx'⟩, end end is_fraction_ring section algebra section is_integral variables {R S} {Rβ‚˜ Sβ‚˜ : Type*} [comm_ring Rβ‚˜] [comm_ring Sβ‚˜] variables [algebra R Rβ‚˜] [is_localization M Rβ‚˜] variables [algebra S Sβ‚˜] [is_localization (algebra.algebra_map_submonoid S M) Sβ‚˜] section variables (S M) /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R β†’ S`, a submonoid `R` of `M`, and a localization `Rβ‚˜` for `M`, let `Sβ‚˜` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rβ‚˜ β†’ Sβ‚˜`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ noncomputable def localization_algebra : algebra Rβ‚˜ Sβ‚˜ := (map Sβ‚˜ (algebra_map R S) (show _ ≀ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rβ‚˜ β†’+* Sβ‚˜).to_algebra end lemma algebra_map_mk' (r : R) (m : M) : (@algebra_map Rβ‚˜ Sβ‚˜ _ _ (localization_algebra M S)) (mk' Rβ‚˜ r m) = mk' Sβ‚˜ (algebra_map R S r) ⟨algebra_map R S m, algebra.mem_algebra_map_submonoid_of_mem m⟩ := map_mk' _ _ _ variables (Rβ‚˜ Sβ‚˜) /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization. -/ lemma localization_algebra_injective (hRS : function.injective (algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≀ non_zero_divisors S) : function.injective (@algebra_map Rβ‚˜ Sβ‚˜ _ _ (localization_algebra M S)) := is_localization.map_injective_of_injective M Rβ‚˜ Sβ‚˜ hRS hM variables {Rβ‚˜ Sβ‚˜} open polynomial lemma ring_hom.is_integral_elem_localization_at_leading_coeff {R S : Type*} [comm_ring R] [comm_ring S] (f : R β†’+* S) (x : S) (p : polynomial R) (hf : p.evalβ‚‚ f x = 0) (M : submonoid R) (hM : p.leading_coeff ∈ M) {Rβ‚˜ Sβ‚˜ : Type*} [comm_ring Rβ‚˜] [comm_ring Sβ‚˜] [algebra R Rβ‚˜] [is_localization M Rβ‚˜] [algebra S Sβ‚˜] [is_localization (M.map f : submonoid S) Sβ‚˜] : (map Sβ‚˜ f M.le_comap_map : Rβ‚˜ β†’+* _).is_integral_elem (algebra_map S Sβ‚˜ x) := begin by_cases triv : (1 : Rβ‚˜) = 0, { exact ⟨0, ⟨trans leading_coeff_zero triv.symm, evalβ‚‚_zero _ _⟩⟩ }, haveI : nontrivial Rβ‚˜ := nontrivial_of_ne 1 0 triv, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.mp (map_units Rβ‚˜ ⟨p.leading_coeff, hM⟩), refine ⟨(p.map (algebra_map R Rβ‚˜)) * C b, ⟨_, _⟩⟩, { refine monic_mul_C_of_leading_coeff_mul_eq_one _, rwa leading_coeff_map_of_leading_coeff_ne_zero (algebra_map R Rβ‚˜), refine Ξ» hfp, zero_ne_one (trans (zero_mul b).symm (hfp β–Έ hb) : (0 : Rβ‚˜) = 1) }, { refine evalβ‚‚_mul_eq_zero_of_left _ _ _ _, erw [evalβ‚‚_map, is_localization.map_comp, ← hom_evalβ‚‚ _ f (algebra_map S Sβ‚˜) x], exact trans (congr_arg (algebra_map S Sβ‚˜) hf) (ring_hom.map_zero _) } end /-- Given a particular witness to an element being algebraic over an algebra `R β†’ S`, We can localize to a submonoid containing the leading coefficient to make it integral. Explicitly, the map between the localizations will be an integral ring morphism -/ theorem is_integral_localization_at_leading_coeff {x : S} (p : polynomial R) (hp : aeval x p = 0) (hM : p.leading_coeff ∈ M) : (map Sβ‚˜ (algebra_map R S) (show _ ≀ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rβ‚˜ β†’+* _).is_integral_elem (algebra_map S Sβ‚˜ x) := (algebra_map R S).is_integral_elem_localization_at_leading_coeff x p hp M hM /-- If `R β†’ S` is an integral extension, `M` is a submonoid of `R`, `Rβ‚˜` is the localization of `R` at `M`, and `Sβ‚˜` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rβ‚˜ β†’ Sβ‚˜` is also an integral extension -/ theorem is_integral_localization (H : algebra.is_integral R S) : (map Sβ‚˜ (algebra_map R S) (show _ ≀ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rβ‚˜ β†’+* _).is_integral := begin intro x, obtain ⟨⟨s, ⟨u, hu⟩⟩, hx⟩ := surj (algebra.algebra_map_submonoid S M) x, obtain ⟨v, hv⟩ := hu, obtain ⟨v', hv'⟩ := is_unit_iff_exists_inv'.1 (map_units Rβ‚˜ ⟨v, hv.1⟩), refine @is_integral_of_is_integral_mul_unit Rβ‚˜ _ _ _ (localization_algebra M S) x (algebra_map S Sβ‚˜ u) v' _ _, { replace hv' := congr_arg (@algebra_map Rβ‚˜ Sβ‚˜ _ _ (localization_algebra M S)) hv', rw [ring_hom.map_mul, ring_hom.map_one, ← ring_hom.comp_apply _ (algebra_map R Rβ‚˜)] at hv', erw is_localization.map_comp at hv', exact hv.2 β–Έ hv' }, { obtain ⟨p, hp⟩ := H s, exact hx.symm β–Έ is_integral_localization_at_leading_coeff p hp.2 (hp.1.symm β–Έ M.one_mem) } end lemma is_integral_localization' {R S : Type*} [comm_ring R] [comm_ring S] {f : R β†’+* S} (hf : f.is_integral) (M : submonoid R) : (map (localization (M.map (f : R β†’* S))) f M.le_comap_map : localization M β†’+* _).is_integral := @is_integral_localization R _ M S _ f.to_algebra _ _ _ _ _ _ _ _ hf end is_integral namespace is_integral_closure variables (A) {L : Type*} [field K] [field L] [algebra A K] [algebra A L] [is_fraction_ring A K] variables (C : Type*) [comm_ring C] [is_domain C] [algebra C L] [is_integral_closure C A L] variables [algebra A C] [is_scalar_tower A C L] open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure `C` of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_algebraic (alg : is_algebraic A L) (inj : βˆ€ x, algebra_map A L x = 0 β†’ x = 0) : is_fraction_ring C L := { map_units := Ξ» ⟨y, hy⟩, is_unit.mk0 _ (show algebra_map C L y β‰  0, from Ξ» h, mem_non_zero_divisors_iff_ne_zero.mp hy ((algebra_map C L).injective_iff.mp (algebra_map_injective C A L) _ h)), surj := Ξ» z, let ⟨x, y, hy, hxy⟩ := exists_integral_multiple (alg z) inj in ⟨⟨mk' C (x : L) x.2, algebra_map _ _ y, mem_non_zero_divisors_iff_ne_zero.mpr (Ξ» h, hy (inj _ (by rw [is_scalar_tower.algebra_map_apply A C L, h, ring_hom.map_zero])))⟩, by rw [set_like.coe_mk, algebra_map_mk', ← is_scalar_tower.algebra_map_apply A C L, hxy]⟩, eq_iff_exists := Ξ» x y, ⟨λ h, ⟨1, by simpa using algebra_map_injective C A L h⟩, Ξ» ⟨c, hc⟩, congr_arg (algebra_map _ L) (mul_right_cancelβ‚€ (mem_non_zero_divisors_iff_ne_zero.mp c.2) hc)⟩ } variables (K L) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure `C` of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_finite_extension [algebra K L] [is_scalar_tower A K L] [finite_dimensional K L] : is_fraction_ring C L := is_fraction_ring_of_algebraic A C (is_fraction_ring.comap_is_algebraic_iff.mpr (is_algebraic_of_finite K L)) (Ξ» x hx, is_fraction_ring.to_map_eq_zero_iff.mp ((algebra_map K L).map_eq_zero.mp $ (is_scalar_tower.algebra_map_apply _ _ _ _).symm.trans hx)) end is_integral_closure namespace integral_closure variables {L : Type*} [field K] [field L] [algebra A K] [is_fraction_ring A K] open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_algebraic [algebra A L] (alg : is_algebraic A L) (inj : βˆ€ x, algebra_map A L x = 0 β†’ x = 0) : is_fraction_ring (integral_closure A L) L := is_integral_closure.is_fraction_ring_of_algebraic A (integral_closure A L) alg inj variables (K L) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_finite_extension [algebra A L] [algebra K L] [is_scalar_tower A K L] [finite_dimensional K L] : is_fraction_ring (integral_closure A L) L := is_integral_closure.is_fraction_ring_of_finite_extension A K L (integral_closure A L) end integral_closure end algebra variables (R A) /-- The fraction ring of a commutative ring `R` as a quotient type. We instantiate this definition as generally as possible, and assume that the commutative ring `R` is an integral domain only when this is needed for proving. -/ @[reducible] def fraction_ring := localization (non_zero_divisors R) namespace fraction_ring instance [subsingleton R] : subsingleton (fraction_ring R) := localization.subsingleton instance [nontrivial R] : nontrivial (fraction_ring R) := ⟨⟨(algebra_map R _) 0, (algebra_map _ _) 1, Ξ» H, zero_ne_one (is_localization.injective _ le_rfl H)⟩⟩ variables {A} noncomputable instance : field (fraction_ring A) := { add := (+), mul := (*), neg := has_neg.neg, sub := has_sub.sub, one := 1, zero := 0, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul, npow := localization.npow _, .. localization.comm_ring, .. is_fraction_ring.to_field A } @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = (algebra_map _ _ r / algebra_map A _ s : fraction_ring A) := by rw [localization.mk_eq_mk', is_fraction_ring.mk'_eq_div] noncomputable instance [is_domain R] [field K] [algebra R K] [no_zero_smul_divisors R K] : algebra (fraction_ring R) K := ring_hom.to_algebra (is_fraction_ring.lift (no_zero_smul_divisors.algebra_map_injective R _)) instance [is_domain R] [field K] [algebra R K] [no_zero_smul_divisors R K] : is_scalar_tower R (fraction_ring R) K := is_scalar_tower.of_algebra_map_eq (Ξ» x, (is_fraction_ring.lift_algebra_map _ x).symm) variables (A) /-- Given an integral domain `A` and a localization map to a field of fractions `f : A β†’+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def alg_equiv (K : Type*) [field K] [algebra A K] [is_fraction_ring A K] : fraction_ring A ≃ₐ[A] K := localization.alg_equiv (non_zero_divisors A) K instance [algebra R A] [no_zero_smul_divisors R A] : no_zero_smul_divisors R (fraction_ring A) := no_zero_smul_divisors.of_algebra_map_injective begin rw [is_scalar_tower.algebra_map_eq R A], exact function.injective.comp (no_zero_smul_divisors.algebra_map_injective _ _) (no_zero_smul_divisors.algebra_map_injective _ _) end end fraction_ring namespace is_fraction_ring variables (R S K) /-- `S` is algebraic over `R` iff a fraction ring of `S` is algebraic over `R` -/ lemma is_algebraic_iff' [field K] [is_domain R] [is_domain S] [algebra R K] [algebra S K] [no_zero_smul_divisors R K] [is_fraction_ring S K] [is_scalar_tower R S K] : algebra.is_algebraic R S ↔ algebra.is_algebraic R K := begin simp only [algebra.is_algebraic], split, { intros h x, rw [is_fraction_ring.is_algebraic_iff R (fraction_ring R) K, is_algebraic_iff_is_integral], obtain ⟨(a : S), b, ha, rfl⟩ := @div_surjective S _ _ _ _ _ _ x, obtain ⟨f, hf₁, hfβ‚‚βŸ© := h b, rw [div_eq_mul_inv], refine is_integral_mul _ _, { rw [← is_algebraic_iff_is_integral], refine _root_.is_algebraic_of_larger_base_of_injective (no_zero_smul_divisors.algebra_map_injective R (fraction_ring R)) _, exact is_algebraic_algebra_map_of_is_algebraic (h a) }, { rw [← is_algebraic_iff_is_integral], use (f.map (algebra_map R (fraction_ring R))).reverse, split, { rwa [ne.def, polynomial.reverse_eq_zero, ← polynomial.degree_eq_bot, polynomial.degree_map_eq_of_injective (no_zero_smul_divisors.algebra_map_injective R (fraction_ring R)), polynomial.degree_eq_bot]}, { haveI : invertible (algebra_map S K b), from is_unit.invertible (is_unit_of_mem_non_zero_divisors (mem_non_zero_divisors_iff_ne_zero.2 (Ξ» h, non_zero_divisors.ne_zero ha ((ring_hom.injective_iff (algebra_map S K)).1 (no_zero_smul_divisors.algebra_map_injective _ _) b h)))), rw [polynomial.aeval_def, ← inv_of_eq_inv, polynomial.evalβ‚‚_reverse_eq_zero_iff, polynomial.evalβ‚‚_map, ← is_scalar_tower.algebra_map_eq, ← polynomial.aeval_def, ← is_scalar_tower.algebra_map_aeval, hfβ‚‚, ring_hom.map_zero] } } }, { intros h x, obtain ⟨f, hf₁, hfβ‚‚βŸ© := h (algebra_map S K x), use [f, hf₁], rw [← is_scalar_tower.algebra_map_aeval] at hfβ‚‚, exact (algebra_map S K).injective_iff.1 (no_zero_smul_divisors.algebra_map_injective _ _) _ hfβ‚‚ } end end is_fraction_ring
c8ff6f4c645211a839b984a6381b0c5a9fd1351e
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp6.lean
313cf358ed5a0fa3cf7ad05600b2a4578579fe3a
[ "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
153
lean
variables a b : Nat rewrite_set simple add_rewrite Nat::add_comm eq_id : simple (* local t = parse_lean("a + b = b + a") print(simplify(t, 'simple')) *)
24ed611acfd7b85ba351fe74d57eac7cea7f902f
7afc29faca4febb6e5005c20aa4aa5c3df5cf35c
/src/height.lean
134ef45337d4e7f5243e6884fa40ce61b4463925
[ "MIT" ]
permissive
Piwry/Proof-of-Surreal
ad2883027e275050b43a578c5513ae3fe350515b
6b92baf2382ac23dd0d700f5c958aa910ad4b754
refs/heads/master
1,670,521,185,736
1,599,657,591,000
1,599,657,591,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,442
lean
import data.list tactic defs single @[simp] lemma height_ge1 : βˆ€ t : bintree, height t β‰₯ 1 := begin intros t, destruct t, repeat { try {intros t H}, try {intros H}, rewrite H, repeat { unfold height }, omega } end @[simp] lemma height_ne0 : βˆ€ t : bintree, Β¬(height t = 0) := begin intros t contra, have ht : height t β‰₯ 1 := by simp, rewrite contra at ht, cases ht end @[simp] lemma height_nle0 : βˆ€ t : bintree, Β¬(height t ≀ 0) := begin intros t contra, have ht : height t = 0, omega, eapply height_ne0, assumption end meta def pos_height : tactic unit := do tactic.exfalso, tactic.try ( do tactic.eapplyc `height_ne0, tactic.assumption ), tactic.try ( do tactic.eapplyc `height_nle0, tactic.assumption ) lemma height_le1_single : βˆ€ t : bintree, height t ≀ 1 β†’ t = ● := begin intros t h1, have h2 : height t β‰₯ 1, simp, have ht : height t = 1, omega, cases t, trivial, repeat { unfold height at ht, have ht' : height t = 0, omega, pos_height, }, begin unfold height at ht, have ht' : max (height t_a) (height t_a_1) = 0, omega, have ht'' : height t_a ≀ max (height t_a) (height t_a_1), apply le_max_left, rewrite ht' at ht'', pos_height, end end lemma grow_height : βˆ€ t' t : bintree, (t ↣ t') β†’ height t ≀ height t' := begin intros t', induction t', begin -- single intros, unfold height, rewrite (single_grow _ a), unfold height end, begin -- left intros t h1, cases t, begin unfold height, apply ge.le, omega, end, begin unfold height, have h2 : (t ↣ t'_a), cases h1, repeat {assumption}, simp, apply t'_ih, assumption end, repeat {cases h1}, end, begin -- right intros t h1, cases t, begin unfold height, apply ge.le, omega, end, cases h1, begin unfold height, have h2 : (t ↣ t'_a), cases h1, repeat {assumption}, simp, apply t'_ih, assumption end, cases h1 end, begin -- full intros t h1, cases t, begin unfold height, omega end, cases h1, cases h1, begin unfold height, cases h1, have h3 : height t_a ≀ height t'_a, apply t'_ih_a, assumption, have h3' : height t_a_1 ≀ height t'_a_1, apply t'_ih_a_1, assumption, have h4 : max (height t_a) (height t_a_1) ≀ max (height t'_a) (height t'_a_1), apply max_le_max, repeat {assumption}, omega end end end lemma iso_height : βˆ€ t t' : bintree, (t = t') β†’ height t = height t' := begin intros, rewrite a end lemma height_neq_niso : βˆ€ t t' : bintree, height t β‰  height t' β†’ t β‰  t' := begin intros, intro contra, apply a, rewrite contra end @[simp] def height_bound : list bintree β†’ β„• | [] := 1 | (t :: l) := max (height t) (height_bound l) lemma height_bound_correct : βˆ€ l t, t ∈ l β†’ height t ≀ height_bound l := begin intros l t h, induction l, begin cases h end, begin unfold height_bound, cases h, begin rewrite h, apply le_max_left end, begin eapply le_trans, apply l_ih, assumption, apply le_max_right end end end lemma height_bound_ge1 : βˆ€ l, height_bound l β‰₯ 1 := begin intros l, induction l, unfold height_bound, omega, unfold height_bound, eapply ge_trans, begin apply has_le.le.ge, apply le_max_right end, begin assumption end end
c3400153fd25782bd745af86146e56686faa2902
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/run/structInst3.lean
a54ab755d1be879031a479e73015928c8ce4bf14
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
463
lean
universes u namespace Ex1 structure A (Ξ± : Type u) := (x : Ξ±) (f : Ξ± β†’ Ξ± := Ξ» x => x) structure B (Ξ± : Type u) extends A Ξ± := (y : Ξ± := f (f x)) (g : Ξ± β†’ Ξ± β†’ Ξ± := Ξ» x y => f x) structure C (Ξ± : Type u) extends B Ξ± := (z : Ξ± := g x y) (x := f z) end Ex1 new_frontend open Ex1 def c1 : C Nat := { x := 1 } #check { c1 with z := 2 } def c2 : C (Nat Γ— Nat) := { z := (1, 1) } #check { c2 with x.fst := 2 } #check { c2 with x.1 := 3 }
665f867f9ba3257853d59ada431d731f939c6dfd
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/order/rel_classes.lean
e7b871d694bbb6a9216b5800f442884165646da9
[ "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
16,850
lean
/- Copyright (c) 2020 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Yury G. Kudryashov -/ import data.set.basic /-! # Unbundled relation classes In this file we prove some properties of `is_*` classes defined in `init.algebra.classes`. The main difference between these classes and the usual order classes (`preorder` etc) is that usual classes extend `has_le` and/or `has_lt` while these classes take a relation as an explicit argument. -/ universes u v variables {Ξ± : Type u} {Ξ² : Type v} {r : Ξ± β†’ Ξ± β†’ Prop} {s : Ξ² β†’ Ξ² β†’ Prop} open function theorem is_refl.swap (r) [is_refl Ξ± r] : is_refl Ξ± (swap r) := ⟨refl_of r⟩ theorem is_irrefl.swap (r) [is_irrefl Ξ± r] : is_irrefl Ξ± (swap r) := ⟨irrefl_of r⟩ theorem is_trans.swap (r) [is_trans Ξ± r] : is_trans Ξ± (swap r) := ⟨λ a b c h₁ hβ‚‚, trans_of r hβ‚‚ hβ‚βŸ© theorem is_antisymm.swap (r) [is_antisymm Ξ± r] : is_antisymm Ξ± (swap r) := ⟨λ a b h₁ hβ‚‚, antisymm hβ‚‚ hβ‚βŸ© theorem is_asymm.swap (r) [is_asymm Ξ± r] : is_asymm Ξ± (swap r) := ⟨λ a b h₁ hβ‚‚, asymm_of r hβ‚‚ hβ‚βŸ© theorem is_total.swap (r) [is_total Ξ± r] : is_total Ξ± (swap r) := ⟨λ a b, (total_of r a b).swap⟩ theorem is_trichotomous.swap (r) [is_trichotomous Ξ± r] : is_trichotomous Ξ± (swap r) := ⟨λ a b, by simpa [swap, or.comm, or.left_comm] using trichotomous_of r a b⟩ theorem is_preorder.swap (r) [is_preorder Ξ± r] : is_preorder Ξ± (swap r) := {..@is_refl.swap Ξ± r _, ..@is_trans.swap Ξ± r _} theorem is_strict_order.swap (r) [is_strict_order Ξ± r] : is_strict_order Ξ± (swap r) := {..@is_irrefl.swap Ξ± r _, ..@is_trans.swap Ξ± r _} theorem is_partial_order.swap (r) [is_partial_order Ξ± r] : is_partial_order Ξ± (swap r) := {..@is_preorder.swap Ξ± r _, ..@is_antisymm.swap Ξ± r _} theorem is_total_preorder.swap (r) [is_total_preorder Ξ± r] : is_total_preorder Ξ± (swap r) := {..@is_preorder.swap Ξ± r _, ..@is_total.swap Ξ± r _} theorem is_linear_order.swap (r) [is_linear_order Ξ± r] : is_linear_order Ξ± (swap r) := {..@is_partial_order.swap Ξ± r _, ..@is_total.swap Ξ± r _} protected theorem is_asymm.is_antisymm (r) [is_asymm Ξ± r] : is_antisymm Ξ± r := ⟨λ x y h₁ hβ‚‚, (asymm h₁ hβ‚‚).elim⟩ protected theorem is_asymm.is_irrefl [is_asymm Ξ± r] : is_irrefl Ξ± r := ⟨λ a h, asymm h h⟩ /- Convert algebraic structure style to explicit relation style typeclasses -/ instance [preorder Ξ±] : is_refl Ξ± (≀) := ⟨le_refl⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_refl Ξ± (β‰₯) := is_refl.swap _ instance [preorder Ξ±] : is_trans Ξ± (≀) := ⟨@le_trans _ _⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_trans Ξ± (β‰₯) := is_trans.swap _ instance [preorder Ξ±] : is_preorder Ξ± (≀) := {} @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_preorder Ξ± (β‰₯) := {} instance [preorder Ξ±] : is_irrefl Ξ± (<) := ⟨lt_irrefl⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_irrefl Ξ± (>) := is_irrefl.swap _ instance [preorder Ξ±] : is_trans Ξ± (<) := ⟨@lt_trans _ _⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_trans Ξ± (>) := is_trans.swap _ instance [preorder Ξ±] : is_asymm Ξ± (<) := ⟨@lt_asymm _ _⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_asymm Ξ± (>) := is_asymm.swap _ instance [preorder Ξ±] : is_antisymm Ξ± (<) := is_asymm.is_antisymm _ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_antisymm Ξ± (>) := is_asymm.is_antisymm _ instance [preorder Ξ±] : is_strict_order Ξ± (<) := {} @[nolint ge_or_gt] -- see Note [nolint_ge] instance [preorder Ξ±] : is_strict_order Ξ± (>) := {} instance preorder.is_total_preorder [preorder Ξ±] [is_total Ξ± (≀)] : is_total_preorder Ξ± (≀) := {} instance [partial_order Ξ±] : is_antisymm Ξ± (≀) := ⟨@le_antisymm _ _⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [partial_order Ξ±] : is_antisymm Ξ± (β‰₯) := is_antisymm.swap _ instance [partial_order Ξ±] : is_partial_order Ξ± (≀) := {} @[nolint ge_or_gt] -- see Note [nolint_ge] instance [partial_order Ξ±] : is_partial_order Ξ± (β‰₯) := {} instance [linear_order Ξ±] : is_total Ξ± (≀) := ⟨le_total⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [linear_order Ξ±] : is_total Ξ± (β‰₯) := is_total.swap _ instance linear_order.is_total_preorder [linear_order Ξ±] : is_total_preorder Ξ± (≀) := by apply_instance @[nolint ge_or_gt] -- see Note [nolint_ge] instance [linear_order Ξ±] : is_total_preorder Ξ± (β‰₯) := {} instance [linear_order Ξ±] : is_linear_order Ξ± (≀) := {} @[nolint ge_or_gt] -- see Note [nolint_ge] instance [linear_order Ξ±] : is_linear_order Ξ± (β‰₯) := {} instance [linear_order Ξ±] : is_trichotomous Ξ± (<) := ⟨lt_trichotomy⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] instance [linear_order Ξ±] : is_trichotomous Ξ± (>) := is_trichotomous.swap _ lemma trans_trichotomous_left [is_trans Ξ± r] [is_trichotomous Ξ± r] {a b c : Ξ±} : Β¬r b a β†’ r b c β†’ r a c := begin intros h₁ hβ‚‚, rcases trichotomous_of r a b with h₃|h₃|h₃, exact trans h₃ hβ‚‚, rw h₃, exact hβ‚‚, exfalso, exact h₁ h₃ end lemma trans_trichotomous_right [is_trans Ξ± r] [is_trichotomous Ξ± r] {a b c : Ξ±} : r a b β†’ Β¬r c b β†’ r a c := begin intros h₁ hβ‚‚, rcases trichotomous_of r b c with h₃|h₃|h₃, exact trans h₁ h₃, rw ←h₃, exact h₁, exfalso, exact hβ‚‚ h₃ end /-- Construct a partial order from a `is_strict_order` relation -/ def partial_order_of_SO (r) [is_strict_order Ξ± r] : partial_order Ξ± := { le := Ξ» x y, x = y ∨ r x y, lt := r, le_refl := Ξ» x, or.inl rfl, le_trans := Ξ» x y z h₁ hβ‚‚, match y, z, h₁, hβ‚‚ with | _, _, or.inl rfl, hβ‚‚ := hβ‚‚ | _, _, h₁, or.inl rfl := h₁ | _, _, or.inr h₁, or.inr hβ‚‚ := or.inr (trans h₁ hβ‚‚) end, le_antisymm := Ξ» x y h₁ hβ‚‚, match y, h₁, hβ‚‚ with | _, or.inl rfl, hβ‚‚ := rfl | _, h₁, or.inl rfl := rfl | _, or.inr h₁, or.inr hβ‚‚ := (asymm h₁ hβ‚‚).elim end, lt_iff_le_not_le := Ξ» x y, ⟨λ h, ⟨or.inr h, not_or (Ξ» e, by rw e at h; exact irrefl _ h) (asymm h)⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁.resolve_left (Ξ» e, hβ‚‚ $ e β–Έ or.inl rfl)⟩ } section prio set_option default_priority 100 -- see Note [default priority] /-- This is basically the same as `is_strict_total_order`, but that definition is in Type (probably by mistake) and also has redundant assumptions. -/ @[algebra] class is_strict_total_order' (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) extends is_trichotomous Ξ± lt, is_strict_order Ξ± lt : Prop. end prio /-- Construct a linear order from a `is_strict_total_order'` relation -/ def linear_order_of_STO' (r) [is_strict_total_order' Ξ± r] : linear_order Ξ± := { le_total := Ξ» x y, match y, trichotomous_of r x y with | y, or.inl h := or.inl (or.inr h) | _, or.inr (or.inl rfl) := or.inl (or.inl rfl) | _, or.inr (or.inr h) := or.inr (or.inr h) end, ..partial_order_of_SO r } /-- Construct a decidable linear order from a `is_strict_total_order'` relation -/ def decidable_linear_order_of_STO' (r) [is_strict_total_order' Ξ± r] [decidable_rel r] : decidable_linear_order Ξ± := by letI LO := linear_order_of_STO' r; exact { decidable_le := Ξ» x y, decidable_of_iff (Β¬ r y x) (@not_lt _ _ y x), ..LO } theorem is_strict_total_order'.swap (r) [is_strict_total_order' Ξ± r] : is_strict_total_order' Ξ± (swap r) := {..is_trichotomous.swap r, ..is_strict_order.swap r} instance [linear_order Ξ±] : is_strict_total_order' Ξ± (<) := {} /-- A connected order is one satisfying the condition `a < c β†’ a < b ∨ b < c`. This is recognizable as an intuitionistic substitute for `a ≀ b ∨ b ≀ a` on the constructive reals, and is also known as negative transitivity, since the contrapositive asserts transitivity of the relation `Β¬ a < b`. -/ @[algebra] class is_order_connected (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) : Prop := (conn : βˆ€ a b c, lt a c β†’ lt a b ∨ lt b c) theorem is_order_connected.neg_trans {r : Ξ± β†’ Ξ± β†’ Prop} [is_order_connected Ξ± r] {a b c} (h₁ : Β¬ r a b) (hβ‚‚ : Β¬ r b c) : Β¬ r a c := mt (is_order_connected.conn a b c) $ by simp [h₁, hβ‚‚] theorem is_strict_weak_order_of_is_order_connected [is_asymm Ξ± r] [is_order_connected Ξ± r] : is_strict_weak_order Ξ± r := { trans := Ξ» a b c h₁ hβ‚‚, (is_order_connected.conn _ c _ h₁).resolve_right (asymm hβ‚‚), incomp_trans := Ξ» a b c ⟨h₁, hβ‚‚βŸ© ⟨h₃, hβ‚„βŸ©, ⟨is_order_connected.neg_trans h₁ h₃, is_order_connected.neg_trans hβ‚„ hβ‚‚βŸ©, ..@is_asymm.is_irrefl Ξ± r _ } @[priority 100] -- see Note [lower instance priority] instance is_order_connected_of_is_strict_total_order' [is_strict_total_order' Ξ± r] : is_order_connected Ξ± r := ⟨λ a b c h, (trichotomous _ _).imp_right (Ξ» o, o.elim (Ξ» e, e β–Έ h) (Ξ» h', trans h' h))⟩ @[priority 100] -- see Note [lower instance priority] instance is_strict_total_order_of_is_strict_total_order' [is_strict_total_order' Ξ± r] : is_strict_total_order Ξ± r := {..is_strict_weak_order_of_is_order_connected} instance [linear_order Ξ±] : is_strict_total_order Ξ± (<) := by apply_instance instance [linear_order Ξ±] : is_order_connected Ξ± (<) := by apply_instance instance [linear_order Ξ±] : is_incomp_trans Ξ± (<) := by apply_instance instance [linear_order Ξ±] : is_strict_weak_order Ξ± (<) := by apply_instance /-- An extensional relation is one in which an element is determined by its set of predecessors. It is named for the `x ∈ y` relation in set theory, whose extensionality is one of the first axioms of ZFC. -/ @[algebra] class is_extensional (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (ext : βˆ€ a b, (βˆ€ x, r x a ↔ r x b) β†’ a = b) @[priority 100] -- see Note [lower instance priority] instance is_extensional_of_is_strict_total_order' [is_strict_total_order' Ξ± r] : is_extensional Ξ± r := ⟨λ a b H, ((@trichotomous _ r _ a b) .resolve_left $ mt (H _).2 (irrefl a)) .resolve_right $ mt (H _).1 (irrefl b)⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- A well order is a well-founded linear order. -/ @[algebra] class is_well_order (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_strict_total_order' Ξ± r : Prop := (wf : well_founded r) end prio @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_strict_total_order {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_strict_total_order Ξ± r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_extensional {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_extensional Ξ± r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trichotomous {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_trichotomous Ξ± r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trans {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_trans Ξ± r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_irrefl {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_irrefl Ξ± r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_asymm {Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : is_asymm Ξ± r := by apply_instance /-- Construct a decidable linear order from a well-founded linear order. -/ noncomputable def is_well_order.decidable_linear_order (r : Ξ± β†’ Ξ± β†’ Prop) [is_well_order Ξ± r] : decidable_linear_order Ξ± := by { haveI := linear_order_of_STO' r, exact classical.DLO Ξ± } instance empty_relation.is_well_order [subsingleton Ξ±] : is_well_order Ξ± empty_relation := { trichotomous := Ξ» a b, or.inr $ or.inl $ subsingleton.elim _ _, irrefl := Ξ» a, id, trans := Ξ» a b c, false.elim, wf := ⟨λ a, ⟨_, Ξ» y, false.elim⟩⟩ } instance nat.lt.is_well_order : is_well_order β„• (<) := ⟨nat.lt_wf⟩ instance sum.lex.is_well_order [is_well_order Ξ± r] [is_well_order Ξ² s] : is_well_order (Ξ± βŠ• Ξ²) (sum.lex r s) := { trichotomous := Ξ» a b, by cases a; cases b; simp; apply trichotomous, irrefl := Ξ» a, by cases a; simp; apply irrefl, trans := Ξ» a b c, by cases a; cases b; simp; cases c; simp; apply trans, wf := sum.lex_wf is_well_order.wf is_well_order.wf } instance prod.lex.is_well_order [is_well_order Ξ± r] [is_well_order Ξ² s] : is_well_order (Ξ± Γ— Ξ²) (prod.lex r s) := { trichotomous := Ξ» ⟨a₁, aβ‚‚βŸ© ⟨b₁, bβ‚‚βŸ©, match @trichotomous _ r _ a₁ b₁ with | or.inl h₁ := or.inl $ prod.lex.left _ _ h₁ | or.inr (or.inr h₁) := or.inr $ or.inr $ prod.lex.left _ _ h₁ | or.inr (or.inl e) := e β–Έ match @trichotomous _ s _ aβ‚‚ bβ‚‚ with | or.inl h := or.inl $ prod.lex.right _ h | or.inr (or.inr h) := or.inr $ or.inr $ prod.lex.right _ h | or.inr (or.inl e) := e β–Έ or.inr $ or.inl rfl end end, irrefl := Ξ» ⟨a₁, aβ‚‚βŸ© h, by cases h with _ _ _ _ h _ _ _ h; [exact irrefl _ h, exact irrefl _ h], trans := Ξ» a b c h₁ hβ‚‚, begin cases h₁ with a₁ aβ‚‚ b₁ bβ‚‚ ab a₁ b₁ bβ‚‚ ab; cases hβ‚‚ with _ _ c₁ cβ‚‚ bc _ _ cβ‚‚ bc, { exact prod.lex.left _ _ (trans ab bc) }, { exact prod.lex.left _ _ ab }, { exact prod.lex.left _ _ bc }, { exact prod.lex.right _ (trans ab bc) } end, wf := prod.lex_wf is_well_order.wf is_well_order.wf } /-- An unbounded or cofinal set -/ def unbounded (r : Ξ± β†’ Ξ± β†’ Prop) (s : set Ξ±) : Prop := βˆ€ a, βˆƒ b ∈ s, Β¬ r b a /-- A bounded or final set -/ def bounded (r : Ξ± β†’ Ξ± β†’ Prop) (s : set Ξ±) : Prop := βˆƒa, βˆ€ b ∈ s, r b a @[simp] lemma not_bounded_iff {r : Ξ± β†’ Ξ± β†’ Prop} (s : set Ξ±) : Β¬bounded r s ↔ unbounded r s := begin classical, simp only [bounded, unbounded, not_forall, not_exists, exists_prop, not_and, not_not] end @[simp] lemma not_unbounded_iff {r : Ξ± β†’ Ξ± β†’ Prop} (s : set Ξ±) : Β¬unbounded r s ↔ bounded r s := by { classical, rw [not_iff_comm, not_bounded_iff] } namespace well_founded /-- If `r` is a well-founded relation, then any nonempty set has a minimal element with respect to `r`. -/ theorem has_min {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (s : set Ξ±) : s.nonempty β†’ βˆƒ a ∈ s, βˆ€ x ∈ s, Β¬ r x a | ⟨a, ha⟩ := (acc.rec_on (H.apply a) $ Ξ» x _ IH, classical.not_imp_not.1 $ Ξ» hne hx, hne $ ⟨x, hx, Ξ» y hy hyx, hne $ IH y hyx hy⟩) ha /-- A minimal element of a nonempty set in a well-founded order -/ noncomputable def min {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : p.nonempty) : Ξ± := classical.some (H.has_min p h) theorem min_mem {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : p.nonempty) : H.min p h ∈ p := let ⟨h, _⟩ := classical.some_spec (H.has_min p h) in h theorem not_lt_min {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : p.nonempty) {x} (xp : x ∈ p) : Β¬ r x (H.min p h) := let ⟨_, h'⟩ := classical.some_spec (H.has_min p h) in h' _ xp open set /-- The supremum of a bounded, well-founded order -/ protected noncomputable def sup {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) (s : set Ξ±) (h : bounded r s) : Ξ± := wf.min { x | βˆ€a ∈ s, r a x } h protected lemma lt_sup {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) {s : set Ξ±} (h : bounded r s) {x} (hx : x ∈ s) : r x (wf.sup s h) := min_mem wf { x | βˆ€a ∈ s, r a x } h x hx section open_locale classical /-- A successor of an element `x` in a well-founded order is a minimal element `y` such that `x < y` if one exists. Otherwise it is `x` itself. -/ protected noncomputable def succ {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) (x : Ξ±) : Ξ± := if h : βˆƒy, r x y then wf.min { y | r x y } h else x protected lemma lt_succ {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) {x : Ξ±} (h : βˆƒy, r x y) : r x (wf.succ x) := by { rw [well_founded.succ, dif_pos h], apply min_mem } end protected lemma lt_succ_iff {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} [wo : is_well_order Ξ± r] {x : Ξ±} (h : βˆƒy, r x y) (y : Ξ±) : r y (wo.wf.succ x) ↔ r y x ∨ y = x := begin split, { intro h', have : Β¬r x y, { intro hy, rw [well_founded.succ, dif_pos] at h', exact wo.wf.not_lt_min _ h hy h' }, rcases trichotomous_of r x y with hy | hy | hy, exfalso, exact this hy, right, exact hy.symm, left, exact hy }, rintro (hy | rfl), exact trans hy (wo.wf.lt_succ h), exact wo.wf.lt_succ h end end well_founded
a6208e2cfd1485c7d85baee3311b95c605381d97
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/limits/shapes/kernels.lean
e42686a2f39202ede24ece822a35cd41f9b30683
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
27,036
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.zero /-! # Kernels and cokernels In a category with zero morphisms, the kernel of a morphism `f : X ⟢ Y` is the equalizer of `f` and `0 : X ⟢ Y`. (Similarly the cokernel is the coequalizer.) The basic definitions are * `kernel : (X ⟢ Y) β†’ C` * `kernel.ΞΉ : kernel f ⟢ X` * `kernel.condition : kernel.ΞΉ f ≫ f = 0` and * `kernel.lift (k : W ⟢ X) (h : k ≫ f = 0) : W ⟢ kernel f` (as well as the dual versions) ## Main statements Besides the definition and lifts, we prove * `kernel.ΞΉ_zero_is_iso`: a kernel map of a zero morphism is an isomorphism * `kernel.eq_zero_of_epi_kernel`: if `kernel.ΞΉ f` is an epimorphism, then `f = 0` * `kernel.of_mono`: the kernel of a monomorphism is the zero object * `kernel.lift_mono`: the lift of a monomorphism `k : W ⟢ X` such that `k ≫ f = 0` is still a monomorphism * `kernel.is_limit_cone_zero_cone`: if our category has a zero object, then the map from the zero obect is a kernel map of any monomorphism * `kernel.ΞΉ_of_zero`: `kernel.ΞΉ (0 : X ⟢ Y)` is an isomorphism and the corresponding dual statements. ## Future work * TODO: connect this with existing working in the group theory and ring theory libraries. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ universes v u u' open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables [has_zero_morphisms C] /-- A morphism `f` has a kernel if the functor `parallel_pair f 0` has a limit. -/ abbreviation has_kernel {X Y : C} (f : X ⟢ Y) : Type (max u v) := has_limit (parallel_pair f 0) /-- A morphism `f` has a cokernel if the functor `parallel_pair f 0` has a colimit. -/ abbreviation has_cokernel {X Y : C} (f : X ⟢ Y) : Type (max u v) := has_colimit (parallel_pair f 0) variables {X Y : C} (f : X ⟢ Y) section /-- A kernel fork is just a fork where the second morphism is a zero morphism. -/ abbreviation kernel_fork := fork f 0 variables {f} @[simp, reassoc] lemma kernel_fork.condition (s : kernel_fork f) : fork.ΞΉ s ≫ f = 0 := by erw [fork.condition, has_zero_morphisms.comp_zero] @[simp] lemma kernel_fork.app_one (s : kernel_fork f) : s.Ο€.app one = 0 := by rw [←fork.app_zero_left, kernel_fork.condition] /-- A morphism `ΞΉ` satisfying `ΞΉ ≫ f = 0` determines a kernel fork over `f`. -/ abbreviation kernel_fork.of_ΞΉ {Z : C} (ΞΉ : Z ⟢ X) (w : ΞΉ ≫ f = 0) : kernel_fork f := fork.of_ΞΉ ΞΉ $ by rw [w, has_zero_morphisms.comp_zero] @[simp] lemma kernel_fork.ΞΉ_of_ΞΉ {X Y P : C} (f : X ⟢ Y) (ΞΉ : P ⟢ X) (w : ΞΉ ≫ f = 0) : fork.ΞΉ (kernel_fork.of_ΞΉ ΞΉ w) = ΞΉ := rfl section local attribute [tidy] tactic.case_bash /-- Every kernel fork `s` is isomorphic (actually, equal) to `fork.of_ΞΉ (fork.ΞΉ s) _`. -/ def iso_of_ΞΉ (s : fork f 0) : s β‰… fork.of_ΞΉ (fork.ΞΉ s) (fork.condition s) := cones.ext (iso.refl _) $ by tidy /-- If `ΞΉ = ΞΉ'`, then `fork.of_ΞΉ ΞΉ _` and `fork.of_ΞΉ ΞΉ' _` are isomorphic. -/ def of_ΞΉ_congr {P : C} {ΞΉ ΞΉ' : P ⟢ X} {w : ΞΉ ≫ f = 0} (h : ΞΉ = ΞΉ') : kernel_fork.of_ΞΉ ΞΉ w β‰… kernel_fork.of_ΞΉ ΞΉ' (by rw [←h, w]) := cones.ext (iso.refl _) $ by tidy /-- If `F` is an equivalence, then applying `F` to a diagram indexing a (co)kernel of `f` yields the diagram indexing the (co)kernel of `F.map f`. -/ def comp_nat_iso {D : Type u'} [category.{v} D] [has_zero_morphisms D] (F : C β₯€ D) [is_equivalence F] : parallel_pair f 0 β‹™ F β‰… parallel_pair (F.map f) 0 := nat_iso.of_components (Ξ» j, match j with | zero := iso.refl _ | one := iso.refl _ end) $ by tidy end /-- If `s` is a limit kernel fork and `k : W ⟢ X` satisfies ``k ≫ f = 0`, then there is some `l : W ⟢ s.X` such that `l ≫ fork.ΞΉ s = k`. -/ def kernel_fork.is_limit.lift' {s : kernel_fork f} (hs : is_limit s) {W : C} (k : W ⟢ X) (h : k ≫ f = 0) : {l : W ⟢ s.X // l ≫ fork.ΞΉ s = k} := ⟨hs.lift $ kernel_fork.of_ΞΉ _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a kernel fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def is_limit_aux (t : kernel_fork f) (lift : Ξ  (s : kernel_fork f), s.X ⟢ t.X) (fac : βˆ€ (s : kernel_fork f), lift s ≫ t.ΞΉ = s.ΞΉ) (uniq : βˆ€ (s : kernel_fork f) (m : s.X ⟢ t.X) (w : m ≫ t.ΞΉ = s.ΞΉ), m = lift s) : is_limit t := { lift := lift, fac' := Ξ» s j, by { cases j, { exact fac s, }, { simp, }, }, uniq' := Ξ» s m w, uniq s m (w limits.walking_parallel_pair.zero), } /-- This is a more convenient formulation to show that a `kernel_fork` constructed using `kernel_fork.of_ΞΉ` is a limit cone. -/ def is_limit.of_ΞΉ {W : C} (g : W ⟢ X) (eq : g ≫ f = 0) (lift : Ξ  {W' : C} (g' : W' ⟢ X) (eq' : g' ≫ f = 0), W' ⟢ W) (fac : βˆ€ {W' : C} (g' : W' ⟢ X) (eq' : g' ≫ f = 0), lift g' eq' ≫ g = g') (uniq : βˆ€ {W' : C} (g' : W' ⟢ X) (eq' : g' ≫ f = 0) (m : W' ⟢ W) (w : m ≫ g = g'), m = lift g' eq') : is_limit (kernel_fork.of_ΞΉ g eq) := is_limit_aux _ (Ξ» s, lift s.ΞΉ s.condition) (Ξ» s, fac s.ΞΉ s.condition) (Ξ» s, uniq s.ΞΉ s.condition) end section variables [has_kernel f] /-- The kernel of a morphism, expressed as the equalizer with the 0 morphism. -/ abbreviation kernel : C := equalizer f 0 /-- The map from `kernel f` into the source of `f`. -/ abbreviation kernel.ΞΉ : kernel f ⟢ X := equalizer.ΞΉ f 0 @[simp, reassoc] lemma kernel.condition : kernel.ΞΉ f ≫ f = 0 := kernel_fork.condition _ /-- Given any morphism `k : W ⟢ X` satisfying `k ≫ f = 0`, `k` factors through `kernel.ΞΉ f` via `kernel.lift : W ⟢ kernel f`. -/ abbreviation kernel.lift {W : C} (k : W ⟢ X) (h : k ≫ f = 0) : W ⟢ kernel f := limit.lift (parallel_pair f 0) (kernel_fork.of_ΞΉ k h) @[simp, reassoc] lemma kernel.lift_ΞΉ {W : C} (k : W ⟢ X) (h : k ≫ f = 0) : kernel.lift f k h ≫ kernel.ΞΉ f = k := limit.lift_Ο€ _ _ @[simp] lemma kernel.lift_zero {W : C} {h} : kernel.lift f (0 : W ⟢ X) h = 0 := by { ext, simp, } instance kernel.lift_mono {W : C} (k : W ⟢ X) (h : k ≫ f = 0) [mono k] : mono (kernel.lift f k h) := ⟨λ Z g g' w, begin replace w := w =≫ kernel.ΞΉ f, simp only [category.assoc, kernel.lift_ΞΉ] at w, exact (cancel_mono k).1 w, end⟩ /-- Any morphism `k : W ⟢ X` satisfying `k ≫ f = 0` induces a morphism `l : W ⟢ kernel f` such that `l ≫ kernel.ΞΉ f = k`. -/ def kernel.lift' {W : C} (k : W ⟢ X) (h : k ≫ f = 0) : {l : W ⟢ kernel f // l ≫ kernel.ΞΉ f = k} := ⟨kernel.lift f k h, kernel.lift_ΞΉ _ _ _⟩ /-- Every kernel of the zero morphism is an isomorphism -/ instance kernel.ΞΉ_zero_is_iso [has_kernel (0 : X ⟢ Y)] : is_iso (kernel.ΞΉ (0 : X ⟢ Y)) := equalizer.ΞΉ_of_self _ lemma eq_zero_of_epi_kernel [epi (kernel.ΞΉ f)] : f = 0 := (cancel_epi (kernel.ΞΉ f)).1 (by simp) /-- The kernel of a zero morphism is isomorphic to the source. -/ def kernel_zero_iso_source [has_kernel (0 : X ⟢ Y)] : kernel (0 : X ⟢ Y) β‰… X := equalizer.iso_source_of_self 0 @[simp] lemma kernel_zero_iso_source_hom [has_kernel (0 : X ⟢ Y)] : kernel_zero_iso_source.hom = kernel.ΞΉ (0 : X ⟢ Y) := rfl @[simp] lemma kernel_zero_iso_source_inv [has_kernel (0 : X ⟢ Y)] : kernel_zero_iso_source.inv = kernel.lift (0 : X ⟢ Y) (πŸ™ X) (by simp) := rfl /-- If two morphisms are known to be equal, then their kernels are isomorphic. -/ def kernel_iso_of_eq {f g : X ⟢ Y} [has_kernel f] [has_kernel g] (h : f = g) : kernel f β‰… kernel g := has_limit.iso_of_nat_iso (by simp[h]) @[simp] lemma kernel_iso_of_eq_refl {h : f = f} : kernel_iso_of_eq h = iso.refl (kernel f) := by { ext, simp [kernel_iso_of_eq], } @[simp] lemma kernel_iso_of_eq_trans {f g h : X ⟢ Y} [has_kernel f] [has_kernel g] [has_kernel h] (w₁ : f = g) (wβ‚‚ : g = h) : kernel_iso_of_eq w₁ β‰ͺ≫ kernel_iso_of_eq wβ‚‚ = kernel_iso_of_eq (w₁.trans wβ‚‚) := by { unfreezingI { induction w₁, induction wβ‚‚, }, ext, simp [kernel_iso_of_eq], } variables {f} lemma kernel_not_epi_of_nonzero (w : f β‰  0) : Β¬epi (kernel.ΞΉ f) := Ξ» I, by exactI w (eq_zero_of_epi_kernel f) lemma kernel_not_iso_of_nonzero (w : f β‰  0) : (is_iso (kernel.ΞΉ f)) β†’ false := Ξ» I, kernel_not_epi_of_nonzero w $ by { resetI, apply_instance } /-- When `g` is an isomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `f`. -/ @[simps] def kernel_comp_is_iso {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [has_kernel f] [is_iso g] : kernel (f ≫ g) β‰… kernel f := { hom := kernel.lift _ (kernel.ΞΉ _) (by { rw [←cancel_mono g], simp, }), inv := kernel.lift _ (kernel.ΞΉ _) (by simp), } lemma kernel_comp_is_iso_hom_comp_kernel_ΞΉ {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [has_kernel f] [is_iso g] : (kernel_comp_is_iso f g).hom ≫ kernel.ΞΉ f = kernel.ΞΉ (f ≫ g) := by simp lemma kernel_comp_is_iso_inv_comp_kernel_ΞΉ {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [has_kernel f] [is_iso g] : (kernel_comp_is_iso f g).inv ≫ kernel.ΞΉ (f ≫ g) = kernel.ΞΉ f := by simp /-- When `f` is an isomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `g`. -/ @[simps] def kernel_is_iso_comp {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [is_iso f] [has_kernel g] : kernel (f ≫ g) β‰… kernel g := { hom := kernel.lift _ (kernel.ΞΉ _ ≫ f) (by simp), inv := kernel.lift _ (kernel.ΞΉ _ ≫ inv f) (by simp), } lemma kernel_is_iso_comp_hom_comp_kernel_ΞΉ {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [is_iso f] [has_kernel g] : (kernel_is_iso_comp f g).hom ≫ kernel.ΞΉ g = kernel.ΞΉ (f ≫ g) ≫ f := by simp lemma kernel_is_iso_comp_inv_comp_kernel_ΞΉ {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_kernel (f ≫ g)] [is_iso f] [has_kernel g] : (kernel_is_iso_comp f g).inv ≫ kernel.ΞΉ (f ≫ g) = kernel.ΞΉ g ≫ (inv f) := by simp end section has_zero_object variables [has_zero_object C] local attribute [instance] has_zero_object.has_zero /-- The morphism from the zero object determines a cone on a kernel diagram -/ def kernel.zero_cone : cone (parallel_pair f 0) := { X := 0, Ο€ := { app := Ξ» j, 0 }} /-- The map from the zero object is a kernel of a monomorphism -/ def kernel.is_limit_cone_zero_cone [mono f] : is_limit (kernel.zero_cone f) := fork.is_limit.mk _ (Ξ» s, 0) (Ξ» s, by { erw has_zero_morphisms.zero_comp, convert (zero_of_comp_mono f _).symm, exact kernel_fork.condition _ }) (Ξ» _ _ _, zero_of_to_zero _) /-- The kernel of a monomorphism is isomorphic to the zero object -/ def kernel.of_mono [has_kernel f] [mono f] : kernel f β‰… 0 := functor.map_iso (cones.forget _) $ is_limit.unique_up_to_iso (limit.is_limit (parallel_pair f 0)) (kernel.is_limit_cone_zero_cone f) /-- The kernel morphism of a monomorphism is a zero morphism -/ lemma kernel.ΞΉ_of_mono [has_kernel f] [mono f] : kernel.ΞΉ f = 0 := zero_of_source_iso_zero _ (kernel.of_mono f) end has_zero_object section transport /-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then any kernel of `f` is a kernel of `l`.-/ def is_kernel.of_comp_iso {Z : C} (l : X ⟢ Z) (i : Z β‰… Y) (h : l ≫ i.hom = f) {s : kernel_fork f} (hs : is_limit s) : is_limit (kernel_fork.of_ΞΉ (fork.ΞΉ s) $ show fork.ΞΉ s ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) := fork.is_limit.mk _ (Ξ» s, hs.lift $ kernel_fork.of_ΞΉ (fork.ΞΉ s) $ by simp [←h]) (Ξ» s, by simp) (Ξ» s m h, by { apply fork.is_limit.hom_ext hs, simpa using h walking_parallel_pair.zero }) /-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then the kernel of `f` is a kernel of `l`.-/ def kernel.of_comp_iso [has_kernel f] {Z : C} (l : X ⟢ Z) (i : Z β‰… Y) (h : l ≫ i.hom = f) : is_limit (kernel_fork.of_ΞΉ (kernel.ΞΉ f) $ show kernel.ΞΉ f ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) := is_kernel.of_comp_iso f l i h $ limit.is_limit _ /-- If `s` is any limit kernel cone over `f` and if `i` is an isomorphism such that `i.hom ≫ s.ΞΉ = l`, then `l` is a kernel of `f`. -/ def is_kernel.iso_kernel {Z : C} (l : Z ⟢ X) {s : kernel_fork f} (hs : is_limit s) (i : Z β‰… s.X) (h : i.hom ≫ fork.ΞΉ s = l) : is_limit (kernel_fork.of_ΞΉ l $ show l ≫ f = 0, by simp [←h]) := is_limit.of_iso_limit hs $ cones.ext i.symm $ Ξ» j, by { cases j, { exact (iso.eq_inv_comp i).2 h }, { simp } } /-- If `i` is an isomorphism such that `i.hom ≫ kernel.ΞΉ f = l`, then `l` is a kernel of `f`. -/ def kernel.iso_kernel [has_kernel f] {Z : C} (l : Z ⟢ X) (i : Z β‰… kernel f) (h : i.hom ≫ kernel.ΞΉ f = l) : is_limit (kernel_fork.of_ΞΉ l $ by simp [←h]) := is_kernel.iso_kernel f l (limit.is_limit _) i h end transport section variables (X Y) /-- The kernel morphism of a zero morphism is an isomorphism -/ def kernel.ΞΉ_of_zero [has_kernel (0 : X ⟢ Y)] : is_iso (kernel.ΞΉ (0 : X ⟢ Y)) := equalizer.ΞΉ_of_self _ end section /-- A cokernel cofork is just a cofork where the second morphism is a zero morphism. -/ abbreviation cokernel_cofork := cofork f 0 variables {f} @[simp, reassoc] lemma cokernel_cofork.condition (s : cokernel_cofork f) : f ≫ cofork.Ο€ s = 0 := by rw [cofork.condition, has_zero_morphisms.zero_comp] @[simp] lemma cokernel_cofork.app_zero (s : cokernel_cofork f) : s.ΞΉ.app zero = 0 := by rw [←cofork.left_app_one, cokernel_cofork.condition] /-- A morphism `Ο€` satisfying `f ≫ Ο€ = 0` determines a cokernel cofork on `f`. -/ abbreviation cokernel_cofork.of_Ο€ {Z : C} (Ο€ : Y ⟢ Z) (w : f ≫ Ο€ = 0) : cokernel_cofork f := cofork.of_Ο€ Ο€ $ by rw [w, has_zero_morphisms.zero_comp] @[simp] lemma cokernel_cofork.Ο€_of_Ο€ {X Y P : C} (f : X ⟢ Y) (Ο€ : Y ⟢ P) (w : f ≫ Ο€ = 0) : cofork.Ο€ (cokernel_cofork.of_Ο€ Ο€ w) = Ο€ := rfl /-- Every cokernel cofork `s` is isomorphic (actually, equal) to `cofork.of_Ο€ (cofork.Ο€ s) _`. -/ def iso_of_Ο€ (s : cofork f 0) : s β‰… cofork.of_Ο€ (cofork.Ο€ s) (cofork.condition s) := cocones.ext (iso.refl _) $ Ξ» j, by cases j; tidy /-- If `Ο€ = Ο€'`, then `cokernel_cofork.of_Ο€ Ο€ _` and `cokernel_cofork.of_Ο€ Ο€' _` are isomorphic. -/ def of_Ο€_congr {P : C} {Ο€ Ο€' : Y ⟢ P} {w : f ≫ Ο€ = 0} (h : Ο€ = Ο€') : cokernel_cofork.of_Ο€ Ο€ w β‰… cokernel_cofork.of_Ο€ Ο€' (by rw [←h, w]) := cocones.ext (iso.refl _) $ Ξ» j, by cases j; tidy /-- If `s` is a colimit cokernel cofork, then every `k : Y ⟢ W` satisfying `f ≫ k = 0` induces `l : s.X ⟢ W` such that `cofork.Ο€ s ≫ l = k`. -/ def cokernel_cofork.is_colimit.desc' {s : cokernel_cofork f} (hs : is_colimit s) {W : C} (k : Y ⟢ W) (h : f ≫ k = 0) : {l : s.X ⟢ W // cofork.Ο€ s ≫ l = k} := ⟨hs.desc $ cokernel_cofork.of_Ο€ _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a cokernel cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def is_colimit_aux (t : cokernel_cofork f) (desc : Ξ  (s : cokernel_cofork f), t.X ⟢ s.X) (fac : βˆ€ (s : cokernel_cofork f), t.Ο€ ≫ desc s = s.Ο€) (uniq : βˆ€ (s : cokernel_cofork f) (m : t.X ⟢ s.X) (w : t.Ο€ ≫ m = s.Ο€), m = desc s) : is_colimit t := { desc := desc, fac' := Ξ» s j, by { cases j, { simp, }, { exact fac s, }, }, uniq' := Ξ» s m w, uniq s m (w limits.walking_parallel_pair.one), } /-- This is a more convenient formulation to show that a `cokernel_cofork` constructed using `cokernel_cofork.of_Ο€` is a limit cone. -/ def is_colimit.of_Ο€ {Z : C} (g : Y ⟢ Z) (eq : f ≫ g = 0) (desc : Ξ  {Z' : C} (g' : Y ⟢ Z') (eq' : f ≫ g' = 0), Z ⟢ Z') (fac : βˆ€ {Z' : C} (g' : Y ⟢ Z') (eq' : f ≫ g' = 0), g ≫ desc g' eq' = g') (uniq : βˆ€ {Z' : C} (g' : Y ⟢ Z') (eq' : f ≫ g' = 0) (m : Z ⟢ Z') (w : g ≫ m = g'), m = desc g' eq') : is_colimit (cokernel_cofork.of_Ο€ g eq) := is_colimit_aux _ (Ξ» s, desc s.Ο€ s.condition) (Ξ» s, fac s.Ο€ s.condition) (Ξ» s, uniq s.Ο€ s.condition) end section variables [has_cokernel f] /-- The cokernel of a morphism, expressed as the coequalizer with the 0 morphism. -/ abbreviation cokernel : C := coequalizer f 0 /-- The map from the target of `f` to `cokernel f`. -/ abbreviation cokernel.Ο€ : Y ⟢ cokernel f := coequalizer.Ο€ f 0 @[simp, reassoc] lemma cokernel.condition : f ≫ cokernel.Ο€ f = 0 := cokernel_cofork.condition _ /-- Given any morphism `k : Y ⟢ W` such that `f ≫ k = 0`, `k` factors through `cokernel.Ο€ f` via `cokernel.desc : cokernel f ⟢ W`. -/ abbreviation cokernel.desc {W : C} (k : Y ⟢ W) (h : f ≫ k = 0) : cokernel f ⟢ W := colimit.desc (parallel_pair f 0) (cokernel_cofork.of_Ο€ k h) @[simp, reassoc] lemma cokernel.Ο€_desc {W : C} (k : Y ⟢ W) (h : f ≫ k = 0) : cokernel.Ο€ f ≫ cokernel.desc f k h = k := colimit.ΞΉ_desc _ _ @[simp] lemma cokernel.desc_zero {W : C} {h} : cokernel.desc f (0 : Y ⟢ W) h = 0 := by { ext, simp, } instance cokernel.desc_epi {W : C} (k : Y ⟢ W) (h : f ≫ k = 0) [epi k] : epi (cokernel.desc f k h) := ⟨λ Z g g' w, begin replace w := cokernel.Ο€ f ≫= w, simp only [cokernel.Ο€_desc_assoc] at w, exact (cancel_epi k).1 w, end⟩ /-- Any morphism `k : Y ⟢ W` satisfying `f ≫ k = 0` induces `l : cokernel f ⟢ W` such that `cokernel.Ο€ f ≫ l = k`. -/ def cokernel.desc' {W : C} (k : Y ⟢ W) (h : f ≫ k = 0) : {l : cokernel f ⟢ W // cokernel.Ο€ f ≫ l = k} := ⟨cokernel.desc f k h, cokernel.Ο€_desc _ _ _⟩ /-- The cokernel of the zero morphism is an isomorphism -/ instance cokernel.Ο€_zero_is_iso [has_colimit (parallel_pair (0 : X ⟢ Y) 0)] : is_iso (cokernel.Ο€ (0 : X ⟢ Y)) := coequalizer.Ο€_of_self _ lemma eq_zero_of_mono_cokernel [mono (cokernel.Ο€ f)] : f = 0 := (cancel_mono (cokernel.Ο€ f)).1 (by simp) /-- The cokernel of a zero morphism is isomorphic to the target. -/ def cokernel_zero_iso_target [has_cokernel (0 : X ⟢ Y)] : cokernel (0 : X ⟢ Y) β‰… Y := coequalizer.iso_target_of_self 0 @[simp] lemma cokernel_zero_iso_target_hom [has_cokernel (0 : X ⟢ Y)] : cokernel_zero_iso_target.hom = cokernel.desc (0 : X ⟢ Y) (πŸ™ Y) (by simp) := rfl @[simp] lemma cokernel_zero_iso_target_inv [has_cokernel (0 : X ⟢ Y)] : cokernel_zero_iso_target.inv = cokernel.Ο€ (0 : X ⟢ Y) := rfl /-- If two morphisms are known to be equal, then their cokernels are isomorphic. -/ def cokernel_iso_of_eq {f g : X ⟢ Y} [has_cokernel f] [has_cokernel g] (h : f = g) : cokernel f β‰… cokernel g := has_colimit.iso_of_nat_iso (by simp[h]) @[simp] lemma cokernel_iso_of_eq_refl {h : f = f} : cokernel_iso_of_eq h = iso.refl (cokernel f) := by { ext, simp [cokernel_iso_of_eq], } @[simp] lemma cokernel_iso_of_eq_trans {f g h : X ⟢ Y} [has_cokernel f] [has_cokernel g] [has_cokernel h] (w₁ : f = g) (wβ‚‚ : g = h) : cokernel_iso_of_eq w₁ β‰ͺ≫ cokernel_iso_of_eq wβ‚‚ = cokernel_iso_of_eq (w₁.trans wβ‚‚) := by { unfreezingI { induction w₁, induction wβ‚‚, }, ext, simp [cokernel_iso_of_eq], } variables {f} lemma cokernel_not_mono_of_nonzero (w : f β‰  0) : Β¬mono (cokernel.Ο€ f) := Ξ» I, by exactI w (eq_zero_of_mono_cokernel f) lemma cokernel_not_iso_of_nonzero (w : f β‰  0) : (is_iso (cokernel.Ο€ f)) β†’ false := Ξ» I, cokernel_not_mono_of_nonzero w $ by { resetI, apply_instance } /-- When `g` is an isomorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `f`. -/ @[simps] def cokernel_comp_is_iso {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] : cokernel (f ≫ g) β‰… cokernel f := { hom := cokernel.desc _ (inv g ≫ cokernel.Ο€ f) (by simp), inv := cokernel.desc _ (g ≫ cokernel.Ο€ (f ≫ g)) (by rw [←category.assoc, cokernel.condition]), } lemma cokernel_Ο€_comp_cokernel_comp_is_iso_hom {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] : cokernel.Ο€ (f ≫ g) ≫ (cokernel_comp_is_iso f g).hom = inv g ≫ cokernel.Ο€ f := by simp lemma cokernel_Ο€_comp_cokernel_comp_is_iso_inv {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] : cokernel.Ο€ f ≫ (cokernel_comp_is_iso f g).inv = g ≫ cokernel.Ο€ (f ≫ g) := by simp /-- When `f` is an isomorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `g`. -/ @[simps] def cokernel_is_iso_comp {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] : cokernel (f ≫ g) β‰… cokernel g := { hom := cokernel.desc _ (cokernel.Ο€ g) (by simp), inv := cokernel.desc _ (cokernel.Ο€ (f ≫ g)) (by { rw [←cancel_epi f, ←category.assoc], simp, }), } lemma cokernel_Ο€_comp_cokernel_is_iso_comp_hom {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] : cokernel.Ο€ (f ≫ g) ≫ (cokernel_is_iso_comp f g).hom = cokernel.Ο€ g := by simp lemma cokernel_Ο€_comp_cokernel_is_iso_comp_inv {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] : cokernel.Ο€ g ≫ (cokernel_is_iso_comp f g).inv = cokernel.Ο€ (f ≫ g) := by simp end section has_zero_object variables [has_zero_object C] local attribute [instance] has_zero_object.has_zero /-- The morphism to the zero object determines a cocone on a cokernel diagram -/ def cokernel.zero_cocone : cocone (parallel_pair f 0) := { X := 0, ΞΉ := { app := Ξ» j, 0 } } /-- The morphism to the zero object is a cokernel of an epimorphism -/ def cokernel.is_colimit_cocone_zero_cocone [epi f] : is_colimit (cokernel.zero_cocone f) := cofork.is_colimit.mk _ (Ξ» s, 0) (Ξ» s, by { erw has_zero_morphisms.zero_comp, convert (zero_of_epi_comp f _).symm, exact cokernel_cofork.condition _ }) (Ξ» _ _ _, zero_of_from_zero _) /-- The cokernel of an epimorphism is isomorphic to the zero object -/ def cokernel.of_epi [has_cokernel f] [epi f] : cokernel f β‰… 0 := functor.map_iso (cocones.forget _) $ is_colimit.unique_up_to_iso (colimit.is_colimit (parallel_pair f 0)) (cokernel.is_colimit_cocone_zero_cocone f) /-- The cokernel morphism of an epimorphism is a zero morphism -/ lemma cokernel.Ο€_of_epi [has_cokernel f] [epi f] : cokernel.Ο€ f = 0 := zero_of_target_iso_zero _ (cokernel.of_epi f) end has_zero_object section has_image /-- The cokernel of the image inclusion of a morphism `f` is isomorphic to the cokernel of `f`. (This result requires that the factorisation through the image is an epimorphism. This holds in any category with equalizers.) -/ @[simps] def cokernel_image_ΞΉ {X Y : C} (f : X ⟢ Y) [has_image f] [has_cokernel (image.ΞΉ f)] [has_cokernel f] [epi (factor_thru_image f)] : cokernel (image.ΞΉ f) β‰… cokernel f := { hom := cokernel.desc _ (cokernel.Ο€ f) begin have w := cokernel.condition f, conv at w { to_lhs, congr, rw ←image.fac f, }, rw [←has_zero_morphisms.comp_zero (limits.factor_thru_image f), category.assoc, cancel_epi] at w, exact w, end, inv := cokernel.desc _ (cokernel.Ο€ _) begin conv { to_lhs, congr, rw ←image.fac f, }, rw [category.assoc, cokernel.condition, has_zero_morphisms.comp_zero], end, } end has_image section variables (X Y) /-- The cokernel of a zero morphism is an isomorphism -/ def cokernel.Ο€_of_zero [has_cokernel (0 : X ⟢ Y)] : is_iso (cokernel.Ο€ (0 : X ⟢ Y)) := coequalizer.Ο€_of_self _ end section has_zero_object variables [has_zero_object C] local attribute [instance] has_zero_object.has_zero /-- The kernel of the cokernel of an epimorphism is an isomorphism -/ instance kernel.of_cokernel_of_epi [has_cokernel f] [has_kernel (cokernel.Ο€ f)] [epi f] : is_iso (kernel.ΞΉ (cokernel.Ο€ f)) := equalizer.ΞΉ_of_eq $ cokernel.Ο€_of_epi f /-- The cokernel of the kernel of a monomorphism is an isomorphism -/ instance cokernel.of_kernel_of_mono [has_kernel f] [has_cokernel (kernel.ΞΉ f)] [mono f] : is_iso (cokernel.Ο€ (kernel.ΞΉ f)) := coequalizer.Ο€_of_eq $ kernel.ΞΉ_of_mono f end has_zero_object section transport /-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then any cokernel of `f` is a cokernel of `l`. -/ def is_cokernel.of_iso_comp {Z : C} (l : Z ⟢ Y) (i : X β‰… Z) (h : i.hom ≫ l = f) {s : cokernel_cofork f} (hs : is_colimit s) : is_colimit (cokernel_cofork.of_Ο€ (cofork.Ο€ s) $ show l ≫ cofork.Ο€ s = 0, by simp [i.eq_inv_comp.2 h]) := cofork.is_colimit.mk _ (Ξ» s, hs.desc $ cokernel_cofork.of_Ο€ (cofork.Ο€ s) $ by simp [←h]) (Ξ» s, by simp) (Ξ» s m h, by { apply cofork.is_colimit.hom_ext hs, simpa using h walking_parallel_pair.one }) /-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then the cokernel of `f` is a cokernel of `l`. -/ def cokernel.of_iso_comp [has_cokernel f] {Z : C} (l : Z ⟢ Y) (i : X β‰… Z) (h : i.hom ≫ l = f) : is_colimit (cokernel_cofork.of_Ο€ (cokernel.Ο€ f) $ show l ≫ cokernel.Ο€ f = 0, by simp [i.eq_inv_comp.2 h]) := is_cokernel.of_iso_comp f l i h $ colimit.is_colimit _ /-- If `s` is any colimit cokernel cocone over `f` and `i` is an isomorphism such that `s.Ο€ ≫ i.hom = l`, then `l` is a cokernel of `f`. -/ def is_cokernel.cokernel_iso {Z : C} (l : Y ⟢ Z) {s : cokernel_cofork f} (hs : is_colimit s) (i : s.X β‰… Z) (h : cofork.Ο€ s ≫ i.hom = l) : is_colimit (cokernel_cofork.of_Ο€ l $ show f ≫ l = 0, by simp [←h]) := is_colimit.of_iso_colimit hs $ cocones.ext i $ Ξ» j, by { cases j, { simp }, { exact h } } /-- If `i` is an isomorphism such that `cokernel.Ο€ f ≫ i.hom = l`, then `l` is a cokernel of `f`. -/ def cokernel.cokernel_iso [has_cokernel f] {Z : C} (l : Y ⟢ Z) (i : cokernel f β‰… Z) (h : cokernel.Ο€ f ≫ i.hom = l) : is_colimit (cokernel_cofork.of_Ο€ l $ by simp [←h]) := is_cokernel.cokernel_iso f l (colimit.is_colimit _) i h end transport end category_theory.limits namespace category_theory.limits variables (C : Type u) [category.{v} C] variables [has_zero_morphisms C] /-- `has_kernels` represents a choice of kernel for every morphism -/ class has_kernels := (has_limit : Ξ  {X Y : C} (f : X ⟢ Y), has_kernel f) /-- `has_cokernels` represents a choice of cokernel for every morphism -/ class has_cokernels := (has_colimit : Ξ  {X Y : C} (f : X ⟢ Y), has_cokernel f) attribute [instance, priority 100] has_kernels.has_limit has_cokernels.has_colimit @[priority 100] instance has_kernels_of_has_equalizers [has_equalizers C] : has_kernels C := { has_limit := infer_instance } @[priority 100] instance has_cokernels_of_has_coequalizers [has_coequalizers C] : has_cokernels C := { has_colimit := infer_instance } end category_theory.limits
dc99dfd4a6e3add6677c2223bbdacea0d15e3bbc
42610cc2e5db9c90269470365e6056df0122eaa0
/hott/types/pointed.hlean
28a98cec1d89e1f3eec3ec44bb874af9db3ffbc7
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
28,372
hlean
/- Copyright (c) 2014-2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Floris van Doorn Ported from Coq HoTT The basic definitions are in init.pointed -/ import .equiv .nat.basic open is_trunc eq prod sigma nat equiv option is_equiv bool unit algebra sigma.ops sum namespace pointed variables {A B : Type} definition pointed_loop [instance] [constructor] (a : A) : pointed (a = a) := pointed.mk idp definition pointed_fun_closed [constructor] (f : A β†’ B) [H : pointed A] : pointed B := pointed.mk (f pt) definition ploop_space [reducible] [constructor] (A : Type*) : Type* := pointed.mk' (point A = point A) definition iterated_ploop_space [reducible] : β„• β†’ Type* β†’ Type* | iterated_ploop_space 0 X := X | iterated_ploop_space (n+1) X := ploop_space (iterated_ploop_space n X) notation `Ξ©` := ploop_space notation `Ξ©[`:95 n:0 `] `:0 A:95 := iterated_ploop_space n A definition is_trunc_loop [instance] [priority 1100] (A : Type*) (n : β„•β‚‹β‚‚) [H : is_trunc (n.+1) A] : is_trunc n (Ξ© A) := !is_trunc_eq definition iterated_ploop_space_zero [unfold_full] (A : Type*) : Ξ©[0] A = A := rfl definition iterated_ploop_space_succ [unfold_full] (k : β„•) (A : Type*) : Ξ©[succ k] A = Ξ© Ξ©[k] A := rfl definition rfln [constructor] [reducible] {n : β„•} {A : Type*} : Ξ©[n] A := pt definition refln [constructor] [reducible] (n : β„•) (A : Type*) : Ξ©[n] A := pt definition refln_eq_refl [unfold_full] (A : Type*) (n : β„•) : rfln = rfl :> Ξ©[succ n] A := rfl definition iterated_loop_space [unfold 3] (A : Type) [H : pointed A] (n : β„•) : Type := Ξ©[n] (pointed.mk' A) definition loop_mul {k : β„•} {A : Type*} (mul : A β†’ A β†’ A) : Ξ©[k] A β†’ Ξ©[k] A β†’ Ξ©[k] A := begin cases k with k, exact mul, exact concat end definition pType_eq {A B : Type*} (f : A ≃ B) (p : f pt = pt) : A = B := begin cases A with A a, cases B with B b, esimp at *, fapply apd011 @pType.mk, { apply ua f}, { rewrite [cast_ua,p]}, end definition pType_eq_elim {A B : Type*} (p : A = B :> Type*) : Ξ£(p : carrier A = carrier B :> Type), Point A =[p] Point B := by induction p; exact ⟨idp, idpo⟩ protected definition pType.sigma_char.{u} : pType.{u} ≃ Ξ£(X : Type.{u}), X := begin fapply equiv.MK, { intro x, induction x with X x, exact ⟨X, x⟩}, { intro x, induction x with X x, exact pointed.MK X x}, { intro x, induction x with X x, reflexivity}, { intro x, induction x with X x, reflexivity}, end definition add_point [constructor] (A : Type) : Type* := pointed.Mk (none : option A) postfix `β‚Š`:(max+1) := add_point -- the inclusion A β†’ Aβ‚Š is called "some", the extra point "pt" or "none" ("@none A") end pointed namespace pointed /- truncated pointed types -/ definition ptrunctype_eq {n : β„•β‚‹β‚‚} {A B : n-Type*} (p : A = B :> Type) (q : Point A =[p] Point B) : A = B := begin induction A with A HA a, induction B with B HB b, esimp at *, induction q, esimp, refine ap010 (ptrunctype.mk A) _ a, exact !is_prop.elim end definition ptrunctype_eq_of_pType_eq {n : β„•β‚‹β‚‚} {A B : n-Type*} (p : A = B :> Type*) : A = B := begin cases pType_eq_elim p with q r, exact ptrunctype_eq q r end definition is_trunc_ptrunctype [instance] {n : β„•β‚‹β‚‚} (A : n-Type*) : is_trunc n A := trunctype.struct A /- properties of iterated loop space -/ variable (A : Type*) definition loop_space_succ_eq_in (n : β„•) : Ξ©[succ n] A = Ξ©[n] (Ξ© A) := begin induction n with n IH, { reflexivity}, { exact ap ploop_space IH} end definition loop_space_add (n m : β„•) : Ξ©[n] (Ξ©[m] A) = Ξ©[m+n] (A) := begin induction n with n IH, { reflexivity}, { exact ap ploop_space IH} end definition loop_space_succ_eq_out (n : β„•) : Ξ©[succ n] A = Ξ©(Ξ©[n] A) := idp variable {A} /- the equality [loop_space_succ_eq_in] preserves concatenation -/ theorem loop_space_succ_eq_in_concat {n : β„•} (p q : Ξ©[succ (succ n)] A) : transport carrier (ap ploop_space (loop_space_succ_eq_in A n)) (p ⬝ q) = transport carrier (ap ploop_space (loop_space_succ_eq_in A n)) p ⬝ transport carrier (ap ploop_space (loop_space_succ_eq_in A n)) q := begin rewrite [-+tr_compose, ↑function.compose], rewrite [+@transport_eq_FlFr_D _ _ _ _ Point Point, +con.assoc], apply whisker_left, rewrite [-+con.assoc], apply whisker_right, rewrite [con_inv_cancel_right, β–Έ*, -ap_con] end definition loop_space_loop_irrel (p : point A = point A) : Ξ©(pointed.Mk p) = Ξ©[2] A := begin intros, fapply pType_eq, { esimp, transitivity _, apply eq_equiv_fn_eq_of_equiv (equiv_eq_closed_right _ p⁻¹), esimp, apply eq_equiv_eq_closed, apply con.right_inv, apply con.right_inv}, { esimp, apply con.left_inv} end definition iterated_loop_space_loop_irrel (n : β„•) (p : point A = point A) : Ξ©[succ n](pointed.Mk p) = Ξ©[succ (succ n)] A :> pType := calc Ξ©[succ n](pointed.Mk p) = Ξ©[n](Ξ© (pointed.Mk p)) : loop_space_succ_eq_in ... = Ξ©[n] (Ξ©[2] A) : loop_space_loop_irrel ... = Ξ©[2+n] A : loop_space_add ... = Ξ©[n+2] A : by rewrite [algebra.add.comm] end pointed open pointed namespace pointed variables {A B C D : Type*} {f g h : A β†’* B} /- categorical properties of pointed maps -/ definition pmap_of_map [constructor] {A B : Type} (f : A β†’ B) (a : A) : pointed.MK A a β†’* pointed.MK B (f a) := pmap.mk f idp definition pid [constructor] [refl] (A : Type*) : A β†’* A := pmap.mk id idp definition pcompose [constructor] [trans] (g : B β†’* C) (f : A β†’* B) : A β†’* C := pmap.mk (Ξ»a, g (f a)) (ap g (respect_pt f) ⬝ respect_pt g) infixr ` ∘* `:60 := pcompose definition passoc (h : C β†’* D) (g : B β†’* C) (f : A β†’* B) : (h ∘* g) ∘* f ~* h ∘* (g ∘* f) := begin fconstructor, intro a, reflexivity, cases A, cases B, cases C, cases D, cases f with f pf, cases g with g pg, cases h with h ph, esimp at *, induction pf, induction pg, induction ph, reflexivity end definition pid_comp [constructor] (f : A β†’* B) : pid B ∘* f ~* f := begin fconstructor, { intro a, reflexivity}, { reflexivity} end definition comp_pid [constructor] (f : A β†’* B) : f ∘* pid A ~* f := begin fconstructor, { intro a, reflexivity}, { reflexivity} end /- equivalences and equalities -/ definition pmap_eq (r : Ξ a, f a = g a) (s : respect_pt f = (r pt) ⬝ respect_pt g) : f = g := begin cases f with f p, cases g with g q, esimp at *, fapply apo011 pmap.mk, { exact eq_of_homotopy r}, { apply concato_eq, apply pathover_eq_Fl, apply inv_con_eq_of_eq_con, rewrite [ap_eq_ap10,↑ap10,apd10_eq_of_homotopy,s]} end definition pmap_equiv_left (A : Type) (B : Type*) : Aβ‚Š β†’* B ≃ (A β†’ B) := begin fapply equiv.MK, { intro f a, cases f with f p, exact f (some a)}, { intro f, fconstructor, intro a, cases a, exact pt, exact f a, reflexivity}, { intro f, reflexivity}, { intro f, cases f with f p, esimp, fapply pmap_eq, { intro a, cases a; all_goals (esimp at *), exact p⁻¹}, { esimp, exact !con.left_inv⁻¹}}, end definition pmap_equiv_right (A : Type*) (B : Type) : (Ξ£(b : B), A β†’* (pointed.Mk b)) ≃ (A β†’ B) := begin fapply equiv.MK, { intro u a, exact pmap.to_fun u.2 a}, { intro f, refine ⟨f pt, _⟩, fapply pmap.mk, intro a, esimp, exact f a, reflexivity}, { intro f, reflexivity}, { intro u, cases u with b f, cases f with f p, esimp at *, induction p, reflexivity} end definition pmap_bool_equiv (B : Type*) : (pbool β†’* B) ≃ B := begin fapply equiv.MK, { intro f, cases f with f p, exact f tt}, { intro b, fconstructor, intro u, cases u, exact pt, exact b, reflexivity}, { intro b, reflexivity}, { intro f, cases f with f p, esimp, fapply pmap_eq, { intro a, cases a; all_goals (esimp at *), exact p⁻¹}, { esimp, exact !con.left_inv⁻¹}}, end -- The constant pointed map between any two types definition pconst [constructor] (A B : Type*) : A β†’* B := pmap.mk (Ξ» a, Point B) idp -- the pointed type of pointed maps definition ppmap [constructor] (A B : Type*) : Type* := pType.mk (A β†’* B) (pconst A B) /- instances of pointed maps -/ definition pcast [constructor] {A B : Type*} (p : A = B) : A β†’* B := pmap.mk (cast (ap pType.carrier p)) (by induction p; reflexivity) definition pinverse [constructor] {X : Type*} : Ξ© X β†’* Ξ© X := pmap.mk eq.inverse idp definition ap1 [constructor] (f : A β†’* B) : Ξ© A β†’* Ξ© B := begin fconstructor, { intro p, exact !respect_pt⁻¹ ⬝ ap f p ⬝ !respect_pt}, { esimp, apply con.left_inv} end definition apn (n : β„•) (f : A β†’* B) : Ξ©[n] A β†’* Ξ©[n] B := begin induction n with n IH, { exact f}, { esimp [iterated_ploop_space], exact ap1 IH} end prefix `Ξ©β†’`:(max+5) := ap1 notation `Ξ©β†’[`:95 n:0 `] `:0 f:95 := apn n f /- categorical properties of pointed homotopies -/ protected definition phomotopy.refl [constructor] [refl] (f : A β†’* B) : f ~* f := begin fconstructor, { intro a, exact idp}, { apply idp_con} end protected definition phomotopy.rfl [constructor] {f : A β†’* B} : f ~* f := phomotopy.refl f protected definition phomotopy.trans [constructor] [trans] (p : f ~* g) (q : g ~* h) : f ~* h := phomotopy.mk (Ξ»a, p a ⬝ q a) abstract begin induction f, induction g, induction p with p p', induction q with q q', esimp at *, induction p', induction q', esimp, apply con.assoc end end protected definition phomotopy.symm [constructor] [symm] (p : f ~* g) : g ~* f := phomotopy.mk (Ξ»a, (p a)⁻¹) abstract begin induction f, induction p with p p', esimp at *, induction p', esimp, apply inv_con_cancel_left end end infix ` ⬝* `:75 := phomotopy.trans postfix `⁻¹*`:(max+1) := phomotopy.symm /- properties about the given pointed maps -/ definition is_equiv_ap1 (f : A β†’* B) [is_equiv f] : is_equiv (ap1 f) := begin induction B with B b, induction f with f pf, esimp at *, cases pf, esimp, apply is_equiv.homotopy_closed (ap f), intro p, exact !idp_con⁻¹ end definition is_equiv_apn (n : β„•) (f : A β†’* B) [H : is_equiv f] : is_equiv (apn n f) := begin induction n with n IH, { exact H}, { exact is_equiv_ap1 (apn n f)} end definition is_equiv_pcast [instance] {A B : Type*} (p : A = B) : is_equiv (pcast p) := !is_equiv_cast definition ap1_id [constructor] {A : Type*} : ap1 (pid A) ~* pid (Ξ© A) := begin fapply phomotopy.mk, { intro p, esimp, refine !idp_con ⬝ !ap_id}, { reflexivity} end definition ap1_pinverse {A : Type*} : ap1 (@pinverse A) ~* @pinverse (Ξ© A) := begin fapply phomotopy.mk, { intro p, esimp, refine !idp_con ⬝ _, exact !inv_eq_inv2⁻¹ }, { reflexivity} end definition ap1_compose (g : B β†’* C) (f : A β†’* B) : ap1 (g ∘* f) ~* ap1 g ∘* ap1 f := begin induction B, induction C, induction g with g pg, induction f with f pf, esimp at *, induction pg, induction pf, fconstructor, { intro p, esimp, apply whisker_left, exact ap_compose g f p ⬝ ap (ap g) !idp_con⁻¹}, { reflexivity} end definition ap1_compose_pinverse (f : A β†’* B) : ap1 f ∘* pinverse ~* pinverse ∘* ap1 f := begin fconstructor, { intro p, esimp, refine !con.assoc ⬝ _ ⬝ !con_inv⁻¹, apply whisker_left, refine whisker_right !ap_inv _ ⬝ _ ⬝ !con_inv⁻¹, apply whisker_left, exact !inv_inv⁻¹}, { induction B with B b, induction f with f pf, esimp at *, induction pf, reflexivity}, end theorem ap1_con (f : A β†’* B) (p q : Ξ© A) : ap1 f (p ⬝ q) = ap1 f p ⬝ ap1 f q := begin rewrite [β–Έ*,ap_con, +con.assoc, con_inv_cancel_left], repeat apply whisker_left end theorem ap1_inv (f : A β†’* B) (p : Ξ© A) : ap1 f p⁻¹ = (ap1 f p)⁻¹ := begin rewrite [β–Έ*,ap_inv, +con_inv, inv_inv, +con.assoc], repeat apply whisker_left end definition pcast_ap_loop_space {A B : Type*} (p : A = B) : pcast (ap ploop_space p) ~* Ξ©β†’ (pcast p) := begin induction p, exact !ap1_id⁻¹* end definition pinverse_con [constructor] {X : Type*} (p q : Ξ© X) : pinverse (p ⬝ q) = pinverse q ⬝ pinverse p := !con_inv definition pinverse_inv [constructor] {X : Type*} (p : Ξ© X) : pinverse p⁻¹ = (pinverse p)⁻¹ := idp definition pcast_idp [constructor] {A : Type*} : pcast (idpath A) ~* pid A := by reflexivity definition apn_zero [unfold_full] (f : A β†’* B) : Ξ©β†’[0] f = f := idp definition apn_succ [unfold_full] (n : β„•) (f : A β†’* B) : Ξ©β†’[n + 1] f = Ξ©β†’ (Ξ©β†’[n] f) := idp /- more on pointed homotopies -/ definition phomotopy_of_eq [constructor] {A B : Type*} {f g : A β†’* B} (p : f = g) : f ~* g := phomotopy.mk (ap010 pmap.to_fun p) begin induction p, apply idp_con end definition pconcat_eq [constructor] {A B : Type*} {f g h : A β†’* B} (p : f ~* g) (q : g = h) : f ~* h := p ⬝* phomotopy_of_eq q definition eq_pconcat [constructor] {A B : Type*} {f g h : A β†’* B} (p : f = g) (q : g ~* h) : f ~* h := phomotopy_of_eq p ⬝* q infix ` ⬝*p `:75 := pconcat_eq infix ` ⬝p* `:75 := eq_pconcat definition pwhisker_left [constructor] (h : B β†’* C) (p : f ~* g) : h ∘* f ~* h ∘* g := phomotopy.mk (Ξ»a, ap h (p a)) abstract begin induction A, induction B, induction C, induction f with f pf, induction g with g pg, induction h with h ph, induction p with p p', esimp at *, induction ph, induction pg, induction p', reflexivity end end definition pwhisker_right [constructor] (h : C β†’* A) (p : f ~* g) : f ∘* h ~* g ∘* h := phomotopy.mk (Ξ»a, p (h a)) abstract begin induction A, induction B, induction C, induction f with f pf, induction g with g pg, induction h with h ph, induction p with p p', esimp at *, induction ph, induction pg, induction p', esimp, exact !idp_con⁻¹ end end definition pconcat2 [constructor] {A B C : Type*} {h i : B β†’* C} {f g : A β†’* B} (q : h ~* i) (p : f ~* g) : h ∘* f ~* i ∘* g := pwhisker_left _ p ⬝* pwhisker_right _ q definition eq_of_phomotopy (p : f ~* g) : f = g := begin fapply pmap_eq, { intro a, exact p a}, { exact !to_homotopy_pt⁻¹} end /- In general we need function extensionality for pap, but for particular F we can do it without function extensionality. -/ definition pap (F : (A β†’* B) β†’ (C β†’* D)) {f g : A β†’* B} (p : f ~* g) : F f ~* F g := phomotopy.mk (ap010 F (eq_of_phomotopy p)) begin cases eq_of_phomotopy p, apply idp_con end definition ap1_phomotopy {f g : A β†’* B} (p : f ~* g) : ap1 f ~* ap1 g := begin induction p with p q, induction f with f pf, induction g with g pg, induction B with B b, esimp at *, induction q, induction pg, fapply phomotopy.mk, { intro l, esimp, refine _ ⬝ !idp_con⁻¹, refine !con.assoc ⬝ _, apply inv_con_eq_of_eq_con, apply ap_con_eq_con_ap}, { unfold [ap_con_eq_con_ap], generalize p (Point A), generalize g (Point A), intro b q, induction q, reflexivity} end definition apn_phomotopy {f g : A β†’* B} (n : β„•) (p : f ~* g) : apn n f ~* apn n g := begin induction n with n IH, { exact p}, { exact ap1_phomotopy IH} end definition apn_compose (n : β„•) (g : B β†’* C) (f : A β†’* B) : apn n (g ∘* f) ~* apn n g ∘* apn n f := begin induction n with n IH, { reflexivity}, { refine ap1_phomotopy IH ⬝* _, apply ap1_compose} end definition apn_pid [constructor] {A : Type*} (n : β„•) : apn n (pid A) ~* pid (Ξ©[n] A) := begin induction n with n IH, { reflexivity}, { exact ap1_phomotopy IH ⬝* ap1_id} end theorem apn_con (n : β„•) (f : A β†’* B) (p q : Ξ©[n+1] A) : apn (n+1) f (p ⬝ q) = apn (n+1) f p ⬝ apn (n+1) f q := by rewrite [+apn_succ, ap1_con] theorem apn_inv (n : β„•) (f : A β†’* B) (p : Ξ©[n+1] A) : apn (n+1) f p⁻¹ = (apn (n+1) f p)⁻¹ := by rewrite [+apn_succ, ap1_inv] definition pinverse_pinverse (A : Type*) : pinverse ∘* pinverse ~* pid (Ξ© A) := begin fapply phomotopy.mk, { apply inv_inv}, { reflexivity} end definition pcast_loop_space [constructor] {A B : Type*} (p : A = B) : pcast (ap Ξ© p) ~* ap1 (pcast p) := begin fapply phomotopy.mk, { intro a, induction p, esimp, exact (!idp_con ⬝ !ap_id)⁻¹}, { induction p, reflexivity} end definition apn_succ_phomotopy_in (n : β„•) (f : A β†’* B) : pcast (loop_space_succ_eq_in B n) ∘* Ξ©β†’[n + 1] f ~* Ξ©β†’[n] (Ξ©β†’ f) ∘* pcast (loop_space_succ_eq_in A n) := begin induction n with n IH, { reflexivity}, { refine pwhisker_right _ (pcast_loop_space (loop_space_succ_eq_in B n)) ⬝* _, refine _ ⬝* pwhisker_left _ (pcast_loop_space (loop_space_succ_eq_in A n))⁻¹*, refine (ap1_compose _ (Ξ©β†’[n + 1] f))⁻¹* ⬝* _ ⬝* ap1_compose (Ξ©β†’[n] (Ξ©β†’ f)) _, exact ap1_phomotopy IH} end definition ap1_pmap_of_map [constructor] {A B : Type} (f : A β†’ B) (a : A) : ap1 (pmap_of_map f a) ~* pmap_of_map (ap f) (idpath a) := begin fapply phomotopy.mk, { intro a, esimp, apply idp_con}, { reflexivity} end definition pmap_of_eq_pt [constructor] {A : Type} {a a' : A} (p : a = a') : pointed.MK A a β†’* pointed.MK A a' := pmap.mk id p /- pointed equivalences -/ definition pequiv_of_pmap [constructor] (f : A β†’* B) (H : is_equiv f) : A ≃* B := pequiv.mk f _ (respect_pt f) definition pequiv_of_equiv [constructor] (f : A ≃ B) (H : f pt = pt) : A ≃* B := pequiv.mk f _ H protected definition pequiv.MK [constructor] (f : A β†’* B) (g : B β†’ A) (gf : Ξ a, g (f a) = a) (fg : Ξ b, f (g b) = b) : A ≃* B := pequiv.mk f (adjointify f g fg gf) (respect_pt f) definition equiv_of_pequiv [constructor] (f : A ≃* B) : A ≃ B := equiv.mk f _ definition to_pinv [constructor] (f : A ≃* B) : B β†’* A := pmap.mk f⁻¹ ((ap f⁻¹ (respect_pt f))⁻¹ ⬝ left_inv f pt) definition to_pmap_pequiv_of_pmap {A B : Type*} (f : A β†’* B) (H : is_equiv f) : pequiv.to_pmap (pequiv_of_pmap f H) = f := by cases f; reflexivity /- A version of pequiv.MK with stronger conditions. The advantage of defining a pointed equivalence with this definition is that there is a pointed homotopy between the inverse of the resulting equivalence and the given pointed map g. This is not the case when using `pequiv.MK` (if g is a pointed map), that will only give an ordinary homotopy. -/ protected definition pequiv.MK2 [constructor] (f : A β†’* B) (g : B β†’* A) (gf : g ∘* f ~* !pid) (fg : f ∘* g ~* !pid) : A ≃* B := pequiv.MK f g gf fg definition to_pmap_pequiv_MK2 [constructor] (f : A β†’* B) (g : B β†’* A) (gf : g ∘* f ~* !pid) (fg : f ∘* g ~* !pid) : pequiv.MK2 f g gf fg ~* f := phomotopy.mk (Ξ»b, idp) !idp_con definition to_pinv_pequiv_MK2 [constructor] (f : A β†’* B) (g : B β†’* A) (gf : g ∘* f ~* !pid) (fg : f ∘* g ~* !pid) : to_pinv (pequiv.MK2 f g gf fg) ~* g := phomotopy.mk (Ξ»b, idp) abstract [irreducible] begin esimp, unfold [adjointify_left_inv'], note H := to_homotopy_pt gf, note H2 := to_homotopy_pt fg, note H3 := eq_top_of_square (natural_square_tr (to_homotopy fg) (respect_pt f)), rewrite [β–Έ* at *, H, H3, H2, ap_id, - +con.assoc, ap_compose' f g, con_inv, - ap_inv, - +ap_con g], apply whisker_right, apply ap02 g, rewrite [ap_con, - + con.assoc, +ap_inv, +inv_con_cancel_right, con.left_inv], end end definition pua {A B : Type*} (f : A ≃* B) : A = B := pType_eq (equiv_of_pequiv f) !respect_pt protected definition pequiv.refl [refl] [constructor] (A : Type*) : A ≃* A := pequiv_of_pmap !pid !is_equiv_id protected definition pequiv.rfl [constructor] : A ≃* A := pequiv.refl A protected definition pequiv.symm [symm] (f : A ≃* B) : B ≃* A := pequiv_of_pmap (to_pinv f) !is_equiv_inv protected definition pequiv.trans [trans] (f : A ≃* B) (g : B ≃* C) : A ≃* C := pequiv_of_pmap (g ∘* f) !is_equiv_compose postfix `⁻¹ᡉ*`:(max + 1) := pequiv.symm infix ` ⬝e* `:75 := pequiv.trans definition to_pmap_pequiv_trans {A B C : Type*} (f : A ≃* B) (g : B ≃* C) : pequiv.to_pmap (f ⬝e* g) = g ∘* f := !to_pmap_pequiv_of_pmap definition pequiv_change_fun [constructor] (f : A ≃* B) (f' : A β†’* B) (Heq : f ~ f') : A ≃* B := pequiv_of_pmap f' (is_equiv.homotopy_closed f Heq) definition pequiv_change_inv [constructor] (f : A ≃* B) (f' : B β†’* A) (Heq : to_pinv f ~ f') : A ≃* B := pequiv.MK f f' (to_left_inv (equiv_change_inv f Heq)) (to_right_inv (equiv_change_inv f Heq)) definition pequiv_rect' (f : A ≃* B) (P : A β†’ B β†’ Type) (g : Ξ b, P (f⁻¹ b) b) (a : A) : P a (f a) := left_inv f a β–Έ g (f a) definition pequiv_of_eq [constructor] {A B : Type*} (p : A = B) : A ≃* B := pequiv_of_pmap (pcast p) !is_equiv_tr definition peconcat_eq {A B C : Type*} (p : A ≃* B) (q : B = C) : A ≃* C := p ⬝e* pequiv_of_eq q definition eq_peconcat {A B C : Type*} (p : A = B) (q : B ≃* C) : A ≃* C := pequiv_of_eq p ⬝e* q definition eq_of_pequiv {A B : Type*} (p : A ≃* B) : A = B := pType_eq (equiv_of_pequiv p) !respect_pt definition peap {A B : Type*} (F : Type* β†’ Type*) (p : A ≃* B) : F A ≃* F B := pequiv_of_pmap (pcast (ap F (eq_of_pequiv p))) begin cases eq_of_pequiv p, apply is_equiv_id end definition pequiv_eq {p q : A ≃* B} (H : p = q :> (A β†’* B)) : p = q := begin cases p with f Hf, cases q with g Hg, esimp at *, exact apd011 pequiv_of_pmap H !is_prop.elim end infix ` ⬝e*p `:75 := peconcat_eq infix ` ⬝pe* `:75 := eq_peconcat local attribute pequiv.symm [constructor] definition pleft_inv (f : A ≃* B) : f⁻¹ᡉ* ∘* f ~* pid A := phomotopy.mk (left_inv f) abstract begin esimp, symmetry, apply con_inv_cancel_left end end definition pright_inv (f : A ≃* B) : f ∘* f⁻¹ᡉ* ~* pid B := phomotopy.mk (right_inv f) abstract begin induction f with f H p, esimp, rewrite [ap_con, +ap_inv, -adj f, -ap_compose], note q := natural_square (right_inv f) p, rewrite [ap_id at q], apply eq_bot_of_square, exact transpose q end end definition pcancel_left (f : B ≃* C) {g h : A β†’* B} (p : f ∘* g ~* f ∘* h) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_left f⁻¹ᡉ* p ⬝* _: refine !passoc⁻¹* ⬝* _: refine pwhisker_right _ (pleft_inv f) ⬝* _: apply pid_comp end definition pcancel_right (f : A ≃* B) {g h : B β†’* C} (p : g ∘* f ~* h ∘* f) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_right f⁻¹ᡉ* p ⬝* _: refine !passoc ⬝* _: refine pwhisker_left _ (pright_inv f) ⬝* _: apply comp_pid end definition phomotopy_pinv_right_of_phomotopy {f : A ≃* B} {g : B β†’* C} {h : A β†’* C} (p : g ∘* f ~* h) : g ~* h ∘* f⁻¹ᡉ* := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine !passoc ⬝* _, refine pwhisker_left _ (pright_inv f) ⬝* _, apply comp_pid end definition phomotopy_of_pinv_right_phomotopy {f : B ≃* A} {g : B β†’* C} {h : A β†’* C} (p : g ∘* f⁻¹ᡉ* ~* h) : g ~* h ∘* f := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine !passoc ⬝* _, refine pwhisker_left _ (pleft_inv f) ⬝* _, apply comp_pid end definition pinv_right_phomotopy_of_phomotopy {f : A ≃* B} {g : B β†’* C} {h : A β†’* C} (p : h ~* g ∘* f) : h ∘* f⁻¹ᡉ* ~* g := (phomotopy_pinv_right_of_phomotopy p⁻¹*)⁻¹* definition phomotopy_of_phomotopy_pinv_right {f : B ≃* A} {g : B β†’* C} {h : A β†’* C} (p : h ~* g ∘* f⁻¹ᡉ*) : h ∘* f ~* g := (phomotopy_of_pinv_right_phomotopy p⁻¹*)⁻¹* definition phomotopy_pinv_left_of_phomotopy {f : B ≃* C} {g : A β†’* B} {h : A β†’* C} (p : f ∘* g ~* h) : g ~* f⁻¹ᡉ* ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ (pleft_inv f) ⬝* _, apply pid_comp end definition phomotopy_of_pinv_left_phomotopy {f : C ≃* B} {g : A β†’* B} {h : A β†’* C} (p : f⁻¹ᡉ* ∘* g ~* h) : g ~* f ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ (pright_inv f) ⬝* _, apply pid_comp end definition pinv_left_phomotopy_of_phomotopy {f : B ≃* C} {g : A β†’* B} {h : A β†’* C} (p : h ~* f ∘* g) : f⁻¹ᡉ* ∘* h ~* g := (phomotopy_pinv_left_of_phomotopy p⁻¹*)⁻¹* definition phomotopy_of_phomotopy_pinv_left {f : C ≃* B} {g : A β†’* B} {h : A β†’* C} (p : h ~* f⁻¹ᡉ* ∘* g) : f ∘* h ~* g := (phomotopy_of_pinv_left_phomotopy p⁻¹*)⁻¹* /- pointed equivalences between particular pointed types -/ definition loopn_pequiv_loopn [constructor] (n : β„•) (f : A ≃* B) : Ξ©[n] A ≃* Ξ©[n] B := pequiv.MK2 (apn n f) (apn n f⁻¹ᡉ*) abstract begin induction n with n IH, { apply pleft_inv}, { replace nat.succ n with n + 1, rewrite [+apn_succ], refine !ap1_compose⁻¹* ⬝* _, refine ap1_phomotopy IH ⬝* _, apply ap1_id} end end abstract begin induction n with n IH, { apply pright_inv}, { replace nat.succ n with n + 1, rewrite [+apn_succ], refine !ap1_compose⁻¹* ⬝* _, refine ap1_phomotopy IH ⬝* _, apply ap1_id} end end definition loop_pequiv_loop [constructor] (f : A ≃* B) : Ξ© A ≃* Ξ© B := loopn_pequiv_loopn 1 f definition to_pmap_loopn_pequiv_loopn [constructor] (n : β„•) (f : A ≃* B) : loopn_pequiv_loopn n f ~* apn n f := !to_pmap_pequiv_MK2 definition to_pinv_loopn_pequiv_loopn [constructor] (n : β„•) (f : A ≃* B) : (loopn_pequiv_loopn n f)⁻¹ᡉ* ~* apn n f⁻¹ᡉ* := !to_pinv_pequiv_MK2 definition loopn_pequiv_loopn_con (n : β„•) (f : A ≃* B) (p q : Ξ©[n+1] A) : loopn_pequiv_loopn (n+1) f (p ⬝ q) = loopn_pequiv_loopn (n+1) f p ⬝ loopn_pequiv_loopn (n+1) f q := ap1_con (loopn_pequiv_loopn n f) p q definition loop_pequiv_loop_con {A B : Type*} (f : A ≃* B) (p q : Ξ© A) : loop_pequiv_loop f (p ⬝ q) = loop_pequiv_loop f p ⬝ loop_pequiv_loop f q := loopn_pequiv_loopn_con 0 f p q definition loopn_pequiv_loopn_rfl (n : β„•) (A : Type*) : loopn_pequiv_loopn n (pequiv.refl A) = pequiv.refl (Ξ©[n] A) := begin apply pequiv_eq, apply eq_of_phomotopy, exact !to_pmap_loopn_pequiv_loopn ⬝* apn_pid n, end definition loop_pequiv_loop_rfl (A : Type*) : loop_pequiv_loop (pequiv.refl A) = pequiv.refl (Ξ© A) := loopn_pequiv_loopn_rfl 1 A definition pmap_functor [constructor] {A A' B B' : Type*} (f : A' β†’* A) (g : B β†’* B') : ppmap A B β†’* ppmap A' B' := pmap.mk (Ξ»h, g ∘* h ∘* f) abstract begin fapply pmap_eq, { esimp, intro a, exact respect_pt g}, { rewrite [β–Έ*, ap_constant], apply idp_con} end end definition pequiv_pinverse (A : Type*) : Ξ© A ≃* Ξ© A := pequiv_of_pmap pinverse !is_equiv_eq_inverse definition pequiv_of_eq_pt [constructor] {A : Type} {a a' : A} (p : a = a') : pointed.MK A a ≃* pointed.MK A a' := pequiv_of_pmap (pmap_of_eq_pt p) !is_equiv_id /- -- TODO definition pmap_pequiv_pmap {A A' B B' : Type*} (f : A ≃* A') (g : B ≃* B') : ppmap A B ≃* ppmap A' B' := pequiv.MK (pmap_functor f⁻¹ᡉ* g) (pmap_functor f g⁻¹ᡉ*) abstract begin intro a, esimp, apply pmap_eq, { esimp, }, { } end end abstract begin end end -/ end pointed
57099fcc91a6f6076f556ff1d96575ab91453a83
9dc8cecdf3c4634764a18254e94d43da07142918
/src/tactic/zify.lean
e92e06714a34f01801131599984a0232c61d8fb6
[ "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
5,432
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.int.cast -- used by clients import data.int.char_zero import tactic.norm_cast /-! # A tactic to shift `β„•` goals to `β„€` It is often easier to work in `β„€`, where subtraction is well behaved, than in `β„•` where it isn't. `zify` is a tactic that casts goals and hypotheses about natural numbers to ones about integers. It makes use of `push_cast`, part of the `norm_cast` family, to simplify these goals. ## Implementation notes `zify` is extensible, using the attribute `@[zify]` to label lemmas used for moving propositions from `β„•` to `β„€`. `zify` lemmas should have the form `βˆ€ a₁ ... aβ‚™ : β„•, Pz (a₁ : β„€) ... (aβ‚™ : β„€) ↔ Pn a₁ ... aβ‚™`. For example, `int.coe_nat_le_coe_nat_iff : βˆ€ (m n : β„•), ↑m ≀ ↑n ↔ m ≀ n` is a `zify` lemma. `zify` is very nearly just `simp only with zify push_cast`. There are a few minor differences: * `zify` lemmas are used in the opposite order of the standard simp form. E.g. we will rewrite with `int.coe_nat_le_coe_nat_iff` from right to left. * `zify` should fail if no `zify` lemma applies (i.e. it was unable to shift any proposition to β„€). However, once this succeeds, it does not necessarily need to rewrite with any `push_cast` rules. -/ open tactic namespace zify /-- The `zify` attribute is used by the `zify` tactic. It applies to lemmas that shift propositions between `nat` and `int`. `zify` lemmas should have the form `βˆ€ a₁ ... aβ‚™ : β„•, Pz (a₁ : β„€) ... (aβ‚™ : β„€) ↔ Pn a₁ ... aβ‚™`. For example, `int.coe_nat_le_coe_nat_iff : βˆ€ (m n : β„•), ↑m ≀ ↑n ↔ m ≀ n` is a `zify` lemma. -/ @[user_attribute] meta def zify_attr : user_attribute simp_lemmas unit := { name := `zify, descr := "Used to tag lemmas for use in the `zify` tactic", cache_cfg := { mk_cache := Ξ» ns, mmap (Ξ» n, do c ← mk_const n, return (c, tt)) ns >>= simp_lemmas.mk.append_with_symm, dependencies := [] } } /-- Given an expression `e`, `lift_to_z e` looks for subterms of `e` that are propositions "about" natural numbers and change them to propositions about integers. Returns an expression `e'` and a proof that `e = e'`. Includes `ge_iff_le` and `gt_iff_lt` in the simp set. These can't be tagged with `zify` as we want to use them in the "forward", not "backward", direction. -/ meta def lift_to_z (e : expr) : tactic (expr Γ— expr) := do sl ← zify_attr.get_cache, sl ← sl.add_simp `ge_iff_le, sl ← sl.add_simp `gt_iff_lt, (e', prf, _) ← simplify sl [] e, return (e', prf) attribute [zify] int.coe_nat_le_coe_nat_iff int.coe_nat_lt_coe_nat_iff int.coe_nat_eq_coe_nat_iff end zify @[zify] lemma int.coe_nat_ne_coe_nat_iff (a b : β„•) : (a : β„€) β‰  b ↔ a β‰  b := by simp /-- `zify extra_lems e` is used to shift propositions in `e` from `β„•` to `β„€`. This is often useful since `β„€` has well-behaved subtraction. The list of extra lemmas is used in the `push_cast` step. Returns an expression `e'` and a proof that `e = e'`.-/ meta def tactic.zify (extra_lems : list simp_arg_type) : expr β†’ tactic (expr Γ— expr) := Ξ» z, do (z1, p1) ← zify.lift_to_z z <|> fail "failed to find an applicable zify lemma", (z2, p2) ← norm_cast.derive_push_cast extra_lems z1, prod.mk z2 <$> mk_eq_trans p1 p2 /-- A variant of `tactic.zify` that takes `h`, a proof of a proposition about natural numbers, and returns a proof of the zified version of that propositon. -/ meta def tactic.zify_proof (extra_lems : list simp_arg_type) (h : expr) : tactic expr := do (_, pf) ← infer_type h >>= tactic.zify extra_lems, mk_eq_mp pf h section setup_tactic_parser /-- The `zify` tactic is used to shift propositions from `β„•` to `β„€`. This is often useful since `β„€` has well-behaved subtraction. ```lean example (a b c x y z : β„•) (h : Β¬ x*y*z < 0) : c < a + 3*b := begin zify, zify at h, /- h : ¬↑x * ↑y * ↑z < 0 ⊒ ↑c < ↑a + 3 * ↑b -/ end ``` `zify` can be given extra lemmas to use in simplification. This is especially useful in the presence of nat subtraction: passing `≀` arguments will allow `push_cast` to do more work. ``` example (a b c : β„•) (h : a - b < c) (hab : b ≀ a) : false := begin zify [hab] at h, /- h : ↑a - ↑b < ↑c -/ end ``` `zify` makes use of the `@[zify]` attribute to move propositions, and the `push_cast` tactic to simplify the `β„€`-valued expressions. `zify` is in some sense dual to the `lift` tactic. `lift (z : β„€) to β„•` will change the type of an integer `z` (in the supertype) to `β„•` (the subtype), given a proof that `z β‰₯ 0`; propositions concerning `z` will still be over `β„€`. `zify` changes propositions about `β„•` (the subtype) to propositions about `β„€` (the supertype), without changing the type of any variable. -/ meta def tactic.interactive.zify (sl : parse simp_arg_list) (l : parse location) : tactic unit := do locs ← l.get_locals, replace_at (tactic.zify sl) locs l.include_goal >>= guardb end add_tactic_doc { name := "zify", category := doc_category.attr, decl_names := [`zify.zify_attr], tags := ["coercions", "transport"] } add_tactic_doc { name := "zify", category := doc_category.tactic, decl_names := [`tactic.interactive.zify], tags := ["coercions", "transport"] }
6ef6d2e24cae6a80d4c9ddc09cadf865da0ba847
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Lean/Compiler/Specialize.lean
e5b4a9fc09de1e93087ccce66a9f3a90af750a74
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,454
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Attributes import Lean.Compiler.Util namespace Lean namespace Compiler inductive SpecializeAttributeKind | specialize | nospecialize namespace SpecializeAttributeKind instance : Inhabited SpecializeAttributeKind := ⟨SpecializeAttributeKind.specialize⟩ protected def beq : SpecializeAttributeKind β†’ SpecializeAttributeKind β†’ Bool | specialize, specialize => true | nospecialize, nospecialize => true | _, _ => false instance : HasBeq SpecializeAttributeKind := ⟨SpecializeAttributeKind.beq⟩ end SpecializeAttributeKind def mkSpecializeAttrs : IO (EnumAttributes SpecializeAttributeKind) := registerEnumAttributes `specializeAttrs [(`specialize, "mark definition to always be inlined", SpecializeAttributeKind.specialize), (`nospecialize, "mark definition to never be inlined", SpecializeAttributeKind.nospecialize) ] /- TODO: fix the following hack. We need to use the following hack because the equation compiler generates auxiliary definitions that are compiled before we even finish the elaboration of the current command. So, if the current command is a `@[specialize] def foo ...`, we must set the attribute `[specialize]` before we start elaboration, otherwise when we compile the auxiliary definitions we will not be able to test whether `@[specialize]` has been set or not. In the new equation compiler we should pass all attributes and allow it to apply them to auxiliary definitions. In the current implementation, we workaround this issue by using functions such as `hasSpecializeAttrAux`. -/ (fun declName _ => pure ()) AttributeApplicationTime.beforeElaboration @[init mkSpecializeAttrs] constant specializeAttrs : EnumAttributes SpecializeAttributeKind := arbitrary _ private partial def hasSpecializeAttrAux (env : Environment) (kind : SpecializeAttributeKind) : Name β†’ Bool | n => match specializeAttrs.getValue env n with | some k => kind == k | none => if n.isInternal then hasSpecializeAttrAux n.getPrefix else false @[export lean_has_specialize_attribute] def hasSpecializeAttribute (env : Environment) (n : Name) : Bool := hasSpecializeAttrAux env SpecializeAttributeKind.specialize n @[export lean_has_nospecialize_attribute] def hasNospecializeAttribute (env : Environment) (n : Name) : Bool := hasSpecializeAttrAux env SpecializeAttributeKind.nospecialize n inductive SpecArgKind | fixed | fixedNeutral -- computationally neutral | fixedHO -- higher order | fixedInst -- type class instance | other structure SpecInfo := (mutualDecls : List Name) (argKinds : SpecArgKind) structure SpecState := (specInfo : SMap Name SpecInfo := {}) (cache : SMap Expr Name := {}) inductive SpecEntry | info (name : Name) (info : SpecInfo) | cache (key : Expr) (fn : Name) namespace SpecState instance : Inhabited SpecState := ⟨{}⟩ def addEntry (s : SpecState) (e : SpecEntry) : SpecState := match e with | SpecEntry.info name info => { s with specInfo := s.specInfo.insert name info } | SpecEntry.cache key fn => { s with cache := s.cache.insert key fn } def switch : SpecState β†’ SpecState | ⟨m₁, mβ‚‚βŸ© => ⟨m₁.switch, mβ‚‚.switch⟩ end SpecState def mkSpecExtension : IO (SimplePersistentEnvExtension SpecEntry SpecState) := registerSimplePersistentEnvExtension { name := `specialize, addEntryFn := SpecState.addEntry, addImportedFn := fun es => (mkStateFromImportedEntries SpecState.addEntry {} es).switch } @[init mkSpecExtension] constant specExtension : SimplePersistentEnvExtension SpecEntry SpecState := arbitrary _ @[export lean_add_specialization_info] def addSpecializationInfo (env : Environment) (fn : Name) (info : SpecInfo) : Environment := specExtension.addEntry env (SpecEntry.info fn info) @[export lean_get_specialization_info] def getSpecializationInfo (env : Environment) (fn : Name) : Option SpecInfo := (specExtension.getState env).specInfo.find? fn @[export lean_cache_specialization] def cacheSpecialization (env : Environment) (e : Expr) (fn : Name) : Environment := specExtension.addEntry env (SpecEntry.cache e fn) @[export lean_get_cached_specialization] def getCachedSpecialization (env : Environment) (e : Expr) : Option Name := (specExtension.getState env).cache.find? e end Compiler end Lean
2fb3d0aacf826e4091620065b154976549cd429c
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/tactic10.lean
a9c45d834e625dee983ad2e59f15cf6c45103af8
[ "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
521
lean
(* import("tactic.lua") *) definition f(a : Bool) : Bool := not a. definition g(a b : Bool) : Bool := a \/ b. theorem T1 (a b : Bool) : (g a b) β†’ (f b) β†’ a := _. unfold_all disj_hyp exact absurd done. (* simple_tac = Repeat(unfold_tac()) .. Repeat(OrElse(conj_hyp_tac(), assumption_tac(), absurd_tac(), conj_hyp_tac(), disj_hyp_tac())) *) definition h(a b : Bool) : Bool := g a b. theorem T2 (a b : Bool) : (h a b) β†’ (f b) β†’ a := _. simple_tac done. print environment 1.
82650ca56c9fd355961681cd6b85a5d9a098c35e
bb31430994044506fa42fd667e2d556327e18dfe
/src/topology/algebra/module/finite_dimension.lean
fece5d836a471e5919a0e292cc5bc90242a20df0
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
20,296
lean
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel, Anatole Dedecker -/ import analysis.locally_convex.balanced_core_hull import topology.algebra.module.determinant /-! # Finite dimensional topological vector spaces over complete fields Let `π•œ` be a complete nontrivially normed field, and `E` a topological vector space (TVS) over `π•œ` (i.e we have `[add_comm_group E] [module π•œ E] [topological_space E] [topological_add_group E]` and `[has_continuous_smul π•œ E]`). If `E` is finite dimensional and Hausdorff, then all linear maps from `E` to any other TVS are continuous. When `E` is a normed space, this gets us the equivalence of norms in finite dimension. ## Main results : * `linear_map.continuous_iff_is_closed_ker` : a linear form is continuous if and only if its kernel is closed. * `linear_map.continuous_of_finite_dimensional` : a linear map on a finite-dimensional Hausdorff space over a complete field is continuous. ## TODO Generalize more of `analysis/normed_space/finite_dimension` to general TVSs. ## Implementation detail The main result from which everything follows is the fact that, if `ΞΎ : ΞΉ β†’ E` is a finite basis, then `ΞΎ.equiv_fun : E β†’β‚— (ΞΉ β†’ π•œ)` is continuous. However, for technical reasons, it is easier to prove this when `ΞΉ` and `E` live ine the same universe. So we start by doing that as a private lemma, then we deduce `linear_map.continuous_of_finite_dimensional` from it, and then the general result follows as `continuous_equiv_fun_basis`. -/ universes u v w x noncomputable theory open set finite_dimensional topological_space filter open_locale big_operators section semiring variables {ΞΉ π•œ F : Type*} [finite ΞΉ] [semiring π•œ] [topological_space π•œ] [add_comm_monoid F] [module π•œ F] [topological_space F] [has_continuous_add F] [has_continuous_smul π•œ F] /-- A linear map on `ΞΉ β†’ π•œ` (where `ΞΉ` is finite) is continuous -/ lemma linear_map.continuous_on_pi (f : (ΞΉ β†’ π•œ) β†’β‚—[π•œ] F) : continuous f := begin casesI nonempty_fintype ΞΉ, classical, -- for the proof, write `f` in the standard basis, and use that each coordinate is a continuous -- function. have : (f : (ΞΉ β†’ π•œ) β†’ F) = (Ξ»x, βˆ‘ i : ΞΉ, x i β€’ (f (Ξ» j, if i = j then 1 else 0))), by { ext x, exact f.pi_apply_eq_sum_univ x }, rw this, refine continuous_finset_sum _ (Ξ»i hi, _), exact (continuous_apply i).smul continuous_const end end semiring section field variables {π•œ E F : Type*} [field π•œ] [topological_space π•œ] [add_comm_group E] [module π•œ E] [topological_space E] [add_comm_group F] [module π•œ F] [topological_space F] [topological_add_group F] [has_continuous_smul π•œ F] /-- The space of continuous linear maps between finite-dimensional spaces is finite-dimensional. -/ instance [finite_dimensional π•œ E] [finite_dimensional π•œ F] : finite_dimensional π•œ (E β†’L[π•œ] F) := finite_dimensional.of_injective (continuous_linear_map.coe_lm π•œ : (E β†’L[π•œ] F) β†’β‚—[π•œ] (E β†’β‚—[π•œ] F)) continuous_linear_map.coe_injective end field section normed_field variables {π•œ : Type u} [hnorm : nontrivially_normed_field π•œ] {E : Type v} [add_comm_group E] [module π•œ E] [topological_space E] [topological_add_group E] [has_continuous_smul π•œ E] {F : Type w} [add_comm_group F] [module π•œ F] [topological_space F] [topological_add_group F] [has_continuous_smul π•œ F] {F' : Type x} [add_comm_group F'] [module π•œ F'] [topological_space F'] [topological_add_group F'] [has_continuous_smul π•œ F'] include hnorm /-- If `π•œ` is a nontrivially normed field, any T2 topology on `π•œ` which makes it a topological vector space over itself (with the norm topology) is *equal* to the norm topology. -/ lemma unique_topology_of_t2 {t : topological_space π•œ} (h₁ : @topological_add_group π•œ t _) (hβ‚‚ : @has_continuous_smul π•œ π•œ _ hnorm.to_uniform_space.to_topological_space t) (h₃ : @t2_space π•œ t) : t = hnorm.to_uniform_space.to_topological_space := begin -- Let `𝓣₀` denote the topology on `π•œ` induced by the norm, and `𝓣` be any T2 vector -- topology on `π•œ`. To show that `𝓣₀ = 𝓣`, it suffices to show that they have the same -- neighborhoods of 0. refine topological_add_group.ext h₁ infer_instance (le_antisymm _ _), { -- To show `𝓣 ≀ 𝓣₀`, we have to show that closed balls are `𝓣`-neighborhoods of 0. rw metric.nhds_basis_closed_ball.ge_iff, -- Let `Ξ΅ > 0`. Since `π•œ` is nontrivially normed, we have `0 < β€–ΞΎβ‚€β€– < Ξ΅` for some `ΞΎβ‚€ : π•œ`. intros Ξ΅ hΞ΅, rcases normed_field.exists_norm_lt π•œ hΞ΅ with βŸ¨ΞΎβ‚€, hΞΎβ‚€, hΞΎβ‚€Ξ΅βŸ©, -- Since `ΞΎβ‚€ β‰  0` and `𝓣` is T2, we know that `{ΞΎβ‚€}ᢜ` is a `𝓣`-neighborhood of 0. have : {ΞΎβ‚€}ᢜ ∈ @nhds π•œ t 0 := is_open.mem_nhds is_open_compl_singleton (ne.symm $ norm_ne_zero_iff.mp hΞΎβ‚€.ne.symm), -- Thus, its balanced core `𝓑` is too. Let's show that the closed ball of radius `Ξ΅` contains -- `𝓑`, which will imply that the closed ball is indeed a `𝓣`-neighborhood of 0. have : balanced_core π•œ {ΞΎβ‚€}ᢜ ∈ @nhds π•œ t 0 := balanced_core_mem_nhds_zero this, refine mem_of_superset this (Ξ» ΞΎ hΞΎ, _), -- Let `ΞΎ ∈ 𝓑`. We want to show `β€–ΞΎβ€– < Ξ΅`. If `ΞΎ = 0`, this is trivial. by_cases hΞΎ0 : ΞΎ = 0, { rw hΞΎ0, exact metric.mem_closed_ball_self hΞ΅.le }, { rw [mem_closed_ball_zero_iff], -- Now suppose `ΞΎ β‰  0`. By contradiction, let's assume `Ξ΅ < β€–ΞΎβ€–`, and show that -- `ΞΎβ‚€ ∈ 𝓑 βŠ† {ΞΎβ‚€}ᢜ`, which is a contradiction. by_contra' h, suffices : (ΞΎβ‚€ * ξ⁻¹) β€’ ΞΎ ∈ balanced_core π•œ {ΞΎβ‚€}ᢜ, { rw [smul_eq_mul π•œ, mul_assoc, inv_mul_cancel hΞΎ0, mul_one] at this, exact not_mem_compl_iff.mpr (mem_singleton ΞΎβ‚€) ((balanced_core_subset _) this) }, -- For that, we use that `𝓑` is balanced : since `β€–ΞΎβ‚€β€– < Ξ΅ < β€–ΞΎβ€–`, we have `β€–ΞΎβ‚€ / ΞΎβ€– ≀ 1`, -- hence `ΞΎβ‚€ = (ΞΎβ‚€ / ΞΎ) β€’ ΞΎ ∈ 𝓑` because `ΞΎ ∈ 𝓑`. refine (balanced_core_balanced _).smul_mem _ hΞΎ, rw [norm_mul, norm_inv, mul_inv_le_iff (norm_pos_iff.mpr hΞΎ0), mul_one], exact (hΞΎβ‚€Ξ΅.trans h).le } }, { -- Finally, to show `𝓣₀ ≀ 𝓣`, we simply argue that `id = (Ξ» x, x β€’ 1)` is continuous from -- `(π•œ, 𝓣₀)` to `(π•œ, 𝓣)` because `(β€’) : (π•œ, 𝓣₀) Γ— (π•œ, 𝓣) β†’ (π•œ, 𝓣)` is continuous. calc (@nhds π•œ hnorm.to_uniform_space.to_topological_space 0) = map id (@nhds π•œ hnorm.to_uniform_space.to_topological_space 0) : map_id.symm ... = map (Ξ» x, id x β€’ 1) (@nhds π•œ hnorm.to_uniform_space.to_topological_space 0) : by conv_rhs {congr, funext, rw [smul_eq_mul, mul_one]}; refl ... ≀ (@nhds π•œ t ((0 : π•œ) β€’ 1)) : @tendsto.smul_const _ _ _ hnorm.to_uniform_space.to_topological_space t _ _ _ _ _ tendsto_id (1 : π•œ) ... = (@nhds π•œ t 0) : by rw zero_smul } end /-- Any linear form on a topological vector space over a nontrivially normed field is continuous if its kernel is closed. -/ lemma linear_map.continuous_of_is_closed_ker (l : E β†’β‚—[π•œ] π•œ) (hl : is_closed (l.ker : set E)) : continuous l := begin -- `l` is either constant or surjective. If it is constant, the result is trivial. by_cases H : finrank π•œ l.range = 0, { rw [finrank_eq_zero, linear_map.range_eq_bot] at H, rw H, exact continuous_zero }, { -- In the case where `l` is surjective, we factor it as `Ο† : (E β§Έ l.ker) ≃ₗ[π•œ] π•œ`. Note that -- `E β§Έ l.ker` is T2 since `l.ker` is closed. have : finrank π•œ l.range = 1, from le_antisymm (finrank_self π•œ β–Έ l.range.finrank_le) (zero_lt_iff.mpr H), have hi : function.injective (l.ker.liftq l (le_refl _)), { rw [← linear_map.ker_eq_bot], exact submodule.ker_liftq_eq_bot _ _ _ (le_refl _) }, have hs : function.surjective (l.ker.liftq l (le_refl _)), { rw [← linear_map.range_eq_top, submodule.range_liftq], exact eq_top_of_finrank_eq ((finrank_self π•œ).symm β–Έ this) }, let Ο† : (E β§Έ l.ker) ≃ₗ[π•œ] π•œ := linear_equiv.of_bijective (l.ker.liftq l (le_refl _)) ⟨hi, hs⟩, have hlΟ† : (l : E β†’ π•œ) = Ο† ∘ l.ker.mkq, by ext; refl, -- Since the quotient map `E β†’β‚—[π•œ] (E β§Έ l.ker)` is continuous, the continuity of `l` will follow -- form the continuity of `Ο†`. suffices : continuous Ο†.to_equiv, { rw hlΟ†, exact this.comp continuous_quot_mk }, -- The pullback by `Ο†.symm` of the quotient topology is a T2 topology on `π•œ`, because `Ο†.symm` -- is injective. Since `Ο†.symm` is linear, it is also a vector space topology. -- Hence, we know that it is equal to the topology induced by the norm. have : induced Ο†.to_equiv.symm infer_instance = hnorm.to_uniform_space.to_topological_space, { refine unique_topology_of_t2 (topological_add_group_induced Ο†.symm.to_linear_map) (has_continuous_smul_induced Ο†.symm.to_linear_map) _, rw t2_space_iff, exact Ξ» x y hxy, @separated_by_continuous _ _ (induced _ _) _ _ _ continuous_induced_dom _ _ (Ο†.to_equiv.symm.injective.ne hxy) }, -- Finally, the pullback by `Ο†.symm` is exactly the pushforward by `Ο†`, so we have to prove -- that `Ο†` is continuous when `π•œ` is endowed with the pushforward by `Ο†` of the quotient -- topology, which is trivial by definition of the pushforward. rw [this.symm, equiv.induced_symm], exact continuous_coinduced_rng } end /-- Any linear form on a topological vector space over a nontrivially normed field is continuous if and only if its kernel is closed. -/ lemma linear_map.continuous_iff_is_closed_ker (l : E β†’β‚—[π•œ] π•œ) : continuous l ↔ is_closed (l.ker : set E) := ⟨λ h, is_closed_singleton.preimage h, l.continuous_of_is_closed_ker⟩ /-- Over a nontrivially normed field, any linear form which is nonzero on a nonempty open set is automatically continuous. -/ lemma linear_map.continuous_of_nonzero_on_open (l : E β†’β‚—[π•œ] π•œ) (s : set E) (hs₁ : is_open s) (hsβ‚‚ : s.nonempty) (hs₃ : βˆ€ x ∈ s, l x β‰  0) : continuous l := begin refine l.continuous_of_is_closed_ker (l.is_closed_or_dense_ker.resolve_right $ Ξ» hl, _), rcases hsβ‚‚ with ⟨x, hx⟩, have : x ∈ interior (l.ker : set E)ᢜ, { rw mem_interior_iff_mem_nhds, exact mem_of_superset (hs₁.mem_nhds hx) hs₃ }, rwa hl.interior_compl at this end variables [complete_space π•œ] /-- This version imposes `ΞΉ` and `E` to live in the same universe, so you should instead use `continuous_equiv_fun_basis` which gives the same result without universe restrictions. -/ private lemma continuous_equiv_fun_basis_aux [ht2 : t2_space E] {ΞΉ : Type v} [fintype ΞΉ] (ΞΎ : basis ΞΉ π•œ E) : continuous ΞΎ.equiv_fun := begin letI : uniform_space E := topological_add_group.to_uniform_space E, letI : uniform_add_group E := topological_add_comm_group_is_uniform, letI : separated_space E := separated_iff_t2.mpr ht2, unfreezingI { induction hn : fintype.card ΞΉ with n IH generalizing ΞΉ E }, { rw fintype.card_eq_zero_iff at hn, exact continuous_of_const (Ξ» x y, funext hn.elim) }, { haveI : finite_dimensional π•œ E := of_fintype_basis ΞΎ, -- first step: thanks to the induction hypothesis, any n-dimensional subspace is equivalent -- to a standard space of dimension n, hence it is complete and therefore closed. have H₁ : βˆ€s : submodule π•œ E, finrank π•œ s = n β†’ is_closed (s : set E), { assume s s_dim, letI : uniform_add_group s := s.to_add_subgroup.uniform_add_group, let b := basis.of_vector_space π•œ s, have U : uniform_embedding b.equiv_fun.symm.to_equiv, { have : fintype.card (basis.of_vector_space_index π•œ s) = n, by { rw ← s_dim, exact (finrank_eq_card_basis b).symm }, have : continuous b.equiv_fun := IH b this, exact b.equiv_fun.symm.uniform_embedding b.equiv_fun.symm.to_linear_map.continuous_on_pi this }, have : is_complete (s : set E), from complete_space_coe_iff_is_complete.1 ((complete_space_congr U).1 (by apply_instance)), exact this.is_closed }, -- second step: any linear form is continuous, as its kernel is closed by the first step have Hβ‚‚ : βˆ€f : E β†’β‚—[π•œ] π•œ, continuous f, { assume f, by_cases H : finrank π•œ f.range = 0, { rw [finrank_eq_zero, linear_map.range_eq_bot] at H, rw H, exact continuous_zero }, { have : finrank π•œ f.ker = n, { have Z := f.finrank_range_add_finrank_ker, rw [finrank_eq_card_basis ΞΎ, hn] at Z, have : finrank π•œ f.range = 1, from le_antisymm (finrank_self π•œ β–Έ f.range.finrank_le) (zero_lt_iff.mpr H), rw [this, add_comm, nat.add_one] at Z, exact nat.succ.inj Z }, have : is_closed (f.ker : set E), from H₁ _ this, exact linear_map.continuous_of_is_closed_ker f this } }, rw continuous_pi_iff, intros i, change continuous (ΞΎ.coord i), exact Hβ‚‚ (ΞΎ.coord i) }, end /-- Any linear map on a finite dimensional space over a complete field is continuous. -/ theorem linear_map.continuous_of_finite_dimensional [t2_space E] [finite_dimensional π•œ E] (f : E β†’β‚—[π•œ] F') : continuous f := begin -- for the proof, go to a model vector space `b β†’ π•œ` thanks to `continuous_equiv_fun_basis`, and -- argue that all linear maps there are continuous. let b := basis.of_vector_space π•œ E, have A : continuous b.equiv_fun := continuous_equiv_fun_basis_aux b, have B : continuous (f.comp (b.equiv_fun.symm : (basis.of_vector_space_index π•œ E β†’ π•œ) β†’β‚—[π•œ] E)) := linear_map.continuous_on_pi _, have : continuous ((f.comp (b.equiv_fun.symm : (basis.of_vector_space_index π•œ E β†’ π•œ) β†’β‚—[π•œ] E)) ∘ b.equiv_fun) := B.comp A, convert this, ext x, dsimp, rw [basis.equiv_fun_symm_apply, basis.sum_repr] end instance linear_map.continuous_linear_map_class_of_finite_dimensional [t2_space E] [finite_dimensional π•œ E] : continuous_linear_map_class (E β†’β‚—[π•œ] F') π•œ E F' := { map_continuous := Ξ» f, f.continuous_of_finite_dimensional, ..linear_map.semilinear_map_class } /-- In finite dimensions over a non-discrete complete normed field, the canonical identification (in terms of a basis) with `π•œ^n` (endowed with the product topology) is continuous. This is the key fact wich makes all linear maps from a T2 finite dimensional TVS over such a field continuous (see `linear_map.continuous_of_finite_dimensional`), which in turn implies that all norms are equivalent in finite dimensions. -/ theorem continuous_equiv_fun_basis [t2_space E] {ΞΉ : Type*} [fintype ΞΉ] (ΞΎ : basis ΞΉ π•œ E) : continuous ΞΎ.equiv_fun := begin haveI : finite_dimensional π•œ E := of_fintype_basis ΞΎ, exact ΞΎ.equiv_fun.to_linear_map.continuous_of_finite_dimensional end namespace linear_map variables [t2_space E] [finite_dimensional π•œ E] /-- The continuous linear map induced by a linear map on a finite dimensional space -/ def to_continuous_linear_map : (E β†’β‚—[π•œ] F') ≃ₗ[π•œ] E β†’L[π•œ] F' := { to_fun := Ξ» f, ⟨f, f.continuous_of_finite_dimensional⟩, inv_fun := coe, map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl, left_inv := Ξ» f, rfl, right_inv := Ξ» f, continuous_linear_map.coe_injective rfl } @[simp] lemma coe_to_continuous_linear_map' (f : E β†’β‚—[π•œ] F') : ⇑f.to_continuous_linear_map = f := rfl @[simp] lemma coe_to_continuous_linear_map (f : E β†’β‚—[π•œ] F') : (f.to_continuous_linear_map : E β†’β‚—[π•œ] F') = f := rfl @[simp] lemma coe_to_continuous_linear_map_symm : ⇑(to_continuous_linear_map : (E β†’β‚—[π•œ] F') ≃ₗ[π•œ] E β†’L[π•œ] F').symm = coe := rfl @[simp] lemma det_to_continuous_linear_map (f : E β†’β‚—[π•œ] E) : f.to_continuous_linear_map.det = f.det := rfl @[simp] lemma ker_to_continuous_linear_map (f : E β†’β‚—[π•œ] F') : ker f.to_continuous_linear_map = ker f := rfl @[simp] lemma range_to_continuous_linear_map (f : E β†’β‚—[π•œ] F') : range f.to_continuous_linear_map = range f := rfl /-- A surjective linear map `f` with finite dimensional codomain is an open map. -/ lemma is_open_map_of_finite_dimensional (f : F β†’β‚—[π•œ] E) (hf : function.surjective f) : is_open_map f := begin rcases f.exists_right_inverse_of_surjective (linear_map.range_eq_top.2 hf) with ⟨g, hg⟩, refine is_open_map.of_sections (Ξ» x, ⟨λ y, g (y - f x) + x, _, _, Ξ» y, _⟩), { exact ((g.continuous_of_finite_dimensional.comp $ continuous_id.sub continuous_const).add continuous_const).continuous_at }, { rw [sub_self, map_zero, zero_add] }, { simp only [map_sub, map_add, ← comp_apply f g, hg, id_apply, sub_add_cancel] } end instance can_lift_continuous_linear_map : can_lift (E β†’β‚—[π•œ] F) (E β†’L[π•œ] F) coe (Ξ» _, true) := ⟨λ f _, ⟨f.to_continuous_linear_map, rfl⟩⟩ end linear_map namespace linear_equiv variables [t2_space E] [t2_space F] [finite_dimensional π•œ E] /-- The continuous linear equivalence induced by a linear equivalence on a finite dimensional space. -/ def to_continuous_linear_equiv (e : E ≃ₗ[π•œ] F) : E ≃L[π•œ] F := { continuous_to_fun := e.to_linear_map.continuous_of_finite_dimensional, continuous_inv_fun := begin haveI : finite_dimensional π•œ F := e.finite_dimensional, exact e.symm.to_linear_map.continuous_of_finite_dimensional end, ..e } @[simp] lemma coe_to_continuous_linear_equiv (e : E ≃ₗ[π•œ] F) : (e.to_continuous_linear_equiv : E β†’β‚—[π•œ] F) = e := rfl @[simp] lemma coe_to_continuous_linear_equiv' (e : E ≃ₗ[π•œ] F) : (e.to_continuous_linear_equiv : E β†’ F) = e := rfl @[simp] lemma coe_to_continuous_linear_equiv_symm (e : E ≃ₗ[π•œ] F) : (e.to_continuous_linear_equiv.symm : F β†’β‚—[π•œ] E) = e.symm := rfl @[simp] lemma coe_to_continuous_linear_equiv_symm' (e : E ≃ₗ[π•œ] F) : (e.to_continuous_linear_equiv.symm : F β†’ E) = e.symm := rfl @[simp] lemma to_linear_equiv_to_continuous_linear_equiv (e : E ≃ₗ[π•œ] F) : e.to_continuous_linear_equiv.to_linear_equiv = e := by { ext x, refl } @[simp] lemma to_linear_equiv_to_continuous_linear_equiv_symm (e : E ≃ₗ[π•œ] F) : e.to_continuous_linear_equiv.symm.to_linear_equiv = e.symm := by { ext x, refl } instance can_lift_continuous_linear_equiv : can_lift (E ≃ₗ[π•œ] F) (E ≃L[π•œ] F) continuous_linear_equiv.to_linear_equiv (Ξ» _, true) := ⟨λ f _, ⟨_, f.to_linear_equiv_to_continuous_linear_equiv⟩⟩ end linear_equiv namespace continuous_linear_map variables [t2_space E] [finite_dimensional π•œ E] /-- Builds a continuous linear equivalence from a continuous linear map on a finite-dimensional vector space whose determinant is nonzero. -/ def to_continuous_linear_equiv_of_det_ne_zero (f : E β†’L[π•œ] E) (hf : f.det β‰  0) : E ≃L[π•œ] E := ((f : E β†’β‚—[π•œ] E).equiv_of_det_ne_zero hf).to_continuous_linear_equiv @[simp] lemma coe_to_continuous_linear_equiv_of_det_ne_zero (f : E β†’L[π•œ] E) (hf : f.det β‰  0) : (f.to_continuous_linear_equiv_of_det_ne_zero hf : E β†’L[π•œ] E) = f := by { ext x, refl } @[simp] lemma to_continuous_linear_equiv_of_det_ne_zero_apply (f : E β†’L[π•œ] E) (hf : f.det β‰  0) (x : E) : f.to_continuous_linear_equiv_of_det_ne_zero hf x = f x := rfl lemma _root_.matrix.to_lin_fin_two_prod_to_continuous_linear_map (a b c d : π•œ) : (matrix.to_lin (basis.fin_two_prod π•œ) (basis.fin_two_prod π•œ) !![a, b; c, d]).to_continuous_linear_map = (a β€’ continuous_linear_map.fst π•œ π•œ π•œ + b β€’ continuous_linear_map.snd π•œ π•œ π•œ).prod (c β€’ continuous_linear_map.fst π•œ π•œ π•œ + d β€’ continuous_linear_map.snd π•œ π•œ π•œ) := continuous_linear_map.ext $ matrix.to_lin_fin_two_prod_apply _ _ _ _ end continuous_linear_map end normed_field
4b33a0f457e2395ad129d6593bbec96ff03a1b7d
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/analytic/linear.lean
e0c124303f8446b65494e61d2e8c5706d10ddd31
[ "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
4,277
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.analytic.basic /-! # Linear functions are analytic In this file we prove that a `continuous_linear_map` defines an analytic function with the formal power series `f x = f a + f (x - a)`. -/ variables {π•œ : Type*} [nontrivially_normed_field π•œ] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] {F : Type*} [normed_add_comm_group F] [normed_space π•œ F] {G : Type*} [normed_add_comm_group G] [normed_space π•œ G] open_locale topology classical big_operators nnreal ennreal open set filter asymptotics noncomputable theory namespace continuous_linear_map /-- Formal power series of a continuous linear map `f : E β†’L[π•œ] F` at `x : E`: `f y = f x + f (y - x)`. -/ @[simp] def fpower_series (f : E β†’L[π•œ] F) (x : E) : formal_multilinear_series π•œ E F | 0 := continuous_multilinear_map.curry0 π•œ _ (f x) | 1 := (continuous_multilinear_curry_fin1 π•œ E F).symm f | _ := 0 @[simp] lemma fpower_series_apply_add_two (f : E β†’L[π•œ] F) (x : E) (n : β„•) : f.fpower_series x (n + 2) = 0 := rfl @[simp] lemma fpower_series_radius (f : E β†’L[π•œ] F) (x : E) : (f.fpower_series x).radius = ∞ := (f.fpower_series x).radius_eq_top_of_forall_image_add_eq_zero 2 $ Ξ» n, rfl protected theorem has_fpower_series_on_ball (f : E β†’L[π•œ] F) (x : E) : has_fpower_series_on_ball f (f.fpower_series x) x ∞ := { r_le := by simp, r_pos := ennreal.coe_lt_top, has_sum := Ξ» y _, (has_sum_nat_add_iff' 2).1 $ by simp [finset.sum_range_succ, ← sub_sub, has_sum_zero] } protected theorem has_fpower_series_at (f : E β†’L[π•œ] F) (x : E) : has_fpower_series_at f (f.fpower_series x) x := ⟨∞, f.has_fpower_series_on_ball x⟩ protected theorem analytic_at (f : E β†’L[π•œ] F) (x : E) : analytic_at π•œ f x := (f.has_fpower_series_at x).analytic_at /-- Reinterpret a bilinear map `f : E β†’L[π•œ] F β†’L[π•œ] G` as a multilinear map `(E Γ— F) [Γ—2]β†’L[π•œ] G`. This multilinear map is the second term in the formal multilinear series expansion of `uncurry f`. It is given by `f.uncurry_bilinear ![(x, y), (x', y')] = f x y'`. -/ def uncurry_bilinear (f : E β†’L[π•œ] F β†’L[π•œ] G) : (E Γ— F) [Γ—2]β†’L[π•œ] G := @continuous_linear_map.uncurry_left π•œ 1 (Ξ» _, E Γ— F) G _ _ _ _ _ $ (↑(continuous_multilinear_curry_fin1 π•œ (E Γ— F) G).symm : (E Γ— F β†’L[π•œ] G) β†’L[π•œ] _).comp $ f.bilinear_comp (fst _ _ _) (snd _ _ _) @[simp] lemma uncurry_bilinear_apply (f : E β†’L[π•œ] F β†’L[π•œ] G) (m : fin 2 β†’ E Γ— F) : f.uncurry_bilinear m = f (m 0).1 (m 1).2 := rfl /-- Formal multilinear series expansion of a bilinear function `f : E β†’L[π•œ] F β†’L[π•œ] G`. -/ @[simp] def fpower_series_bilinear (f : E β†’L[π•œ] F β†’L[π•œ] G) (x : E Γ— F) : formal_multilinear_series π•œ (E Γ— F) G | 0 := continuous_multilinear_map.curry0 π•œ _ (f x.1 x.2) | 1 := (continuous_multilinear_curry_fin1 π•œ (E Γ— F) G).symm (f.derivβ‚‚ x) | 2 := f.uncurry_bilinear | _ := 0 @[simp] lemma fpower_series_bilinear_radius (f : E β†’L[π•œ] F β†’L[π•œ] G) (x : E Γ— F) : (f.fpower_series_bilinear x).radius = ∞ := (f.fpower_series_bilinear x).radius_eq_top_of_forall_image_add_eq_zero 3 $ Ξ» n, rfl protected theorem has_fpower_series_on_ball_bilinear (f : E β†’L[π•œ] F β†’L[π•œ] G) (x : E Γ— F) : has_fpower_series_on_ball (Ξ» x : E Γ— F, f x.1 x.2) (f.fpower_series_bilinear x) x ∞ := { r_le := by simp, r_pos := ennreal.coe_lt_top, has_sum := Ξ» y _, (has_sum_nat_add_iff' 3).1 $ begin simp only [finset.sum_range_succ, finset.sum_range_one, prod.fst_add, prod.snd_add, f.map_add_add], dsimp, simp only [add_comm, sub_self, has_sum_zero] end } protected theorem has_fpower_series_at_bilinear (f : E β†’L[π•œ] F β†’L[π•œ] G) (x : E Γ— F) : has_fpower_series_at (Ξ» x : E Γ— F, f x.1 x.2) (f.fpower_series_bilinear x) x := ⟨∞, f.has_fpower_series_on_ball_bilinear x⟩ protected theorem analytic_at_bilinear (f : E β†’L[π•œ] F β†’L[π•œ] G) (x : E Γ— F) : analytic_at π•œ (Ξ» x : E Γ— F, f x.1 x.2) x := (f.has_fpower_series_at_bilinear x).analytic_at end continuous_linear_map
63a8e72a43babeb0b59e1d5723414aba86adce3d
618003631150032a5676f229d13a079ac875ff77
/src/computability/tm_to_partrec.lean
5657d261a0bd8eb7215584187d130f2c4266eb72
[ "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
64,312
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import computability.halting import computability.turing_machine import data.num.lemmas /-! # Modelling partial recursive functions using Turing machines This file defines a simplified basis for partial recursive functions, and a `turing.TM2` model Turing machine for evaluating these functions. This amounts to a constructive proof that every `partrec` function can be evaluated by a Turing machine. ## Main definitions * `to_partrec.code`: a simplified basis for partial recursive functions, valued in `list β„• β†’. list β„•`. * `to_partrec.code.eval`: semantics for a `to_partrec.code` program * `partrec_to_TM2.tr`: A TM2 turing machine which can evaluate `code` programs -/ open function (update) open relation namespace turing /-! ## A simplified basis for partrec This section constructs the type `code`, which is a data type of programs with `list β„•` input and output, with enough expressivity to write any partial recursive function. The primitives are: * `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`. * `succ` returns the successor of the head of the input, defaulting to zero if there is no head: * `succ [] = [1]` * `succ (n :: v) = [n + 1]` * `tail` returns the tail of the input * `tail [] = []` * `tail (n :: v) = v` * `cons f fs` calls `f` and `fs` on the input and conses the results: * `cons f fs v = (f v).head :: fs v` * `comp f g` calls `f` on the output of `g`: * `comp f g v = f (g v)` * `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or a successor (similar to `nat.cases_on`). * `case f g [] = f []` * `case f g (0 :: v) = f v` * `case f g (n+1 :: v) = g (n :: v)` * `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f` again or finish: * `fix f v = []` if `f v = []` * `fix f v = w` if `f v = 0 :: w` * `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded) This basis is convenient because it is closer to the Turing machine model - the key operations are splitting and merging of lists of unknown length, while the messy `n`-ary composition operation from the traditional basis for partial recursive functions is absent - but it retains a compositional semantics. The first step in transitioning to Turing machines is to make a sequential evaluator for this basis, which we take up in the next section. -/ namespace to_partrec /-- The type of codes for primitive recursive functions. Unlike `nat.partrec.code`, this uses a set of operations on `list β„•`. See `code.eval` for a description of the behavior of the primitives. -/ @[derive inhabited] inductive code | zero' | succ | tail | cons : code β†’ code β†’ code | comp : code β†’ code β†’ code | case : code β†’ code β†’ code | fix : code β†’ code /-- The semantics of the `code` primitives, as partial functions `list β„• β†’. list β„•`. By convention we functions that return a single result return a singleton `[n]`, or in some cases `n :: v` where `v` will be ignored by a subsequent function. * `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`. * `succ` returns the successor of the head of the input, defaulting to zero if there is no head: * `succ [] = [1]` * `succ (n :: v) = [n + 1]` * `tail` returns the tail of the input * `tail [] = []` * `tail (n :: v) = v` * `cons f fs` calls `f` and `fs` on the input and conses the results: * `cons f fs v = (f v).head :: fs v` * `comp f g` calls `f` on the output of `g`: * `comp f g v = f (g v)` * `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or a successor (similar to `nat.cases_on`). * `case f g [] = f []` * `case f g (0 :: v) = f v` * `case f g (n+1 :: v) = g (n :: v)` * `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f` again or finish: * `fix f v = []` if `f v = []` * `fix f v = w` if `f v = 0 :: w` * `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded) -/ @[simp] def code.eval : code β†’ list β„• β†’. list β„• | code.zero' := Ξ» v, pure (0 :: v) | code.succ := Ξ» v, pure [v.head.succ] | code.tail := Ξ» v, pure v.tail | (code.cons f fs) := Ξ» v, do n ← code.eval f v, ns ← code.eval fs v, pure (n.head :: ns) | (code.comp f g) := Ξ» v, g.eval v >>= f.eval | (code.case f g) := Ξ» v, v.head.elim (f.eval v.tail) (Ξ» y _, g.eval (y :: v.tail)) | (code.fix f) := pfun.fix $ Ξ» v, (f.eval v).map $ Ξ» v, if v.head = 0 then sum.inl v.tail else sum.inr v.tail namespace code /-- `nil` is the constant nil function: `nil v = []`. -/ def nil : code := tail.comp succ @[simp] theorem nil_eval (v) : nil.eval v = pure [] := by simp [nil] /-- `id` is the identity function: `id v = v`. -/ def id : code := tail.comp zero' @[simp] theorem id_eval (v) : id.eval v = pure v := by simp [id] /-- `head` gets the head of the input list: `head [] = [0]`, `head (n :: v) = [n]`. -/ def head : code := cons id nil @[simp] theorem head_eval (v) : head.eval v = pure [v.head] := by simp [head] /-- `zero` is the constant zero function: `zero v = [0]`. -/ def zero : code := cons zero' nil @[simp] theorem zero_eval (v) : zero.eval v = pure [0] := by simp [zero] /-- `pred` returns the predecessor of the head of the input: `pred [] = [0]`, `pred (0 :: v) = [0]`, `pred (n+1 :: v) = [n]`. -/ def pred : code := case zero head @[simp] theorem pred_eval (v) : pred.eval v = pure [v.head.pred] := by simp [pred]; cases v.head; simp /-- `rfind f` performs the function of the `rfind` primitive of partial recursive functions. `rfind f v` returns the smallest `n` such that `(f (n :: v)).head = 0`. It is implemented as: rfind f v = pred (fix (Ξ» (n::v), f (n::v) :: n+1 :: v) (0 :: v)) The idea is that the initial state is `0 :: v`, and the `fix` keeps `n :: v` as its internal state; it calls `f (n :: v)` as the exit test and `n+1 :: v` as the next state. At the end we get `n+1 :: v` where `n` is the desired output, and `pred (n+1 :: v) = [n]` returns the result. -/ def rfind (f : code) : code := comp pred $ comp (fix $ cons f $ cons succ tail) zero' /-- `prec f g` implements the `prec` (primitive recursion) operation of partial recursive functions. `prec f g` evaluates as: * `prec f g [] = [f []]` * `prec f g (0 :: v) = [f v]` * `prec f g (n+1 :: v) = [g (n :: prec f g (n :: v) :: v)]` It is implemented as: G (a :: b :: IH :: v) = (b :: a+1 :: b-1 :: g (a :: IH :: v) :: v) F (0 :: f_v :: v) = (f_v :: v) F (n+1 :: f_v :: v) = (fix G (0 :: n :: f_v :: v)).tail.tail prec f g (a :: v) = [(F (a :: f v :: v)).head] Because `fix` always evaluates its body at least once, we must special case the `0` case to avoid calling `g` more times than necessary (which could be bad if `g` diverges). If the input is `0 :: v`, then `F (0 :: f v :: v) = (f v :: v)` so we return `[f v]`. If the input is `n+1 :: v`, we evaluate the function from the bottom up, with initial state `0 :: n :: f v :: v`. The first number counts up, providing arguments for the applications to `g`, while the second number counts down, providing the exit condition (this is the initial `b` in the return value of `G`, which is stripped by `fix`). After the `fix` is complete, the final state is `n :: 0 :: res :: v` where `res` is the desired result, and the rest reduces this to `[res]`. -/ def prec (f g : code) : code := let G := cons tail $ cons succ $ cons (comp pred tail) $ cons (comp g $ cons id $ comp tail tail) $ comp tail $ comp tail tail in let F := case id $ comp (comp (comp tail tail) (fix G)) zero' in cons (comp F (cons head $ cons (comp f tail) tail)) nil local attribute [-simp] roption.bind_eq_bind roption.map_eq_map roption.pure_eq_some theorem exists_code.comp {m n} {f : vector β„• n β†’. β„•} {g : fin n β†’ vector β„• m β†’. β„•} (hf : βˆƒ c : code, βˆ€ v : vector β„• n, c.eval v.1 = pure <$> f v) (hg : βˆ€ i, βˆƒ c : code, βˆ€ v : vector β„• m, c.eval v.1 = pure <$> g i v) : βˆƒ c : code, βˆ€ v : vector β„• m, c.eval v.1 = pure <$> (vector.m_of_fn (Ξ» i, g i v) >>= f) := begin suffices : βˆƒ c : code, βˆ€ v : vector β„• m, c.eval v.1 = subtype.val <$> vector.m_of_fn (Ξ» i, g i v), { obtain ⟨cf, hf⟩ := hf, obtain ⟨cg, hg⟩ := this, exact ⟨cf.comp cg, Ξ» v, by simp [hg, hf, map_bind, seq_bind_eq, (∘)]; refl⟩ }, clear hf f, induction n with n IH, { exact ⟨nil, Ξ» v, by simp [vector.m_of_fn]; refl⟩ }, { obtain ⟨cg, hgβ‚βŸ© := hg 0, obtain ⟨cl, hl⟩ := IH (Ξ» i, hg i.succ), exact ⟨cons cg cl, Ξ» v, by simp [vector.m_of_fn, hg₁, map_bind, seq_bind_eq, bind_assoc, (∘), hl]; refl⟩ }, end theorem exists_code {n} {f : vector β„• n β†’. β„•} (hf : nat.partrec' f) : βˆƒ c : code, βˆ€ v : vector β„• n, c.eval v.1 = pure <$> f v := begin induction hf with n f hf, induction hf, case prim zero { exact ⟨zero', Ξ» ⟨[], _⟩, rfl⟩ }, case prim succ { exact ⟨succ, Ξ» ⟨[v], _⟩, rfl⟩ }, case prim nth : n i { refine fin.succ_rec (Ξ» n, _) (Ξ» n i IH, _) i, { exact ⟨head, Ξ» ⟨list.cons a as, _⟩, by simp; refl⟩ }, { obtain ⟨c, h⟩ := IH, exact ⟨c.comp tail, Ξ» v, by simpa [← vector.nth_tail] using h v.tail⟩ } }, case prim comp : m n f g hf hg IHf IHg { simpa [roption.bind_eq_bind] using exists_code.comp IHf IHg }, case prim prec : n f g hf hg IHf IHg { obtain ⟨cf, hf⟩ := IHf, obtain ⟨cg, hg⟩ := IHg, simp only [roption.map_eq_map, roption.map_some, pfun.coe_val] at hf hg, refine ⟨prec cf cg, Ξ» v, _⟩, rw ← v.cons_head_tail, specialize hf v.tail, replace hg := Ξ» a b, hg (a :: b :: v.tail), simp only [vector.cons_val, vector.tail_val] at hf hg, simp only [roption.map_eq_map, roption.map_some, vector.cons_val, vector.cons_tail, vector.cons_head, pfun.coe_val, vector.tail_val], simp only [← roption.pure_eq_some] at hf hg ⊒, induction v.head with n IH; simp [prec, hf, bind_assoc, ← roption.map_eq_map, ← bind_pure_comp_eq_map, show βˆ€ x, pure x = [x], from Ξ» _, rfl], suffices : βˆ€ a b, a + b = n β†’ (n.succ :: 0 :: g (n :: nat.elim (f v.tail) (Ξ» y IH, g (y::IH::v.tail)) n :: v.tail) :: v.val.tail : list β„•) ∈ pfun.fix (Ξ» v : list β„•, do x ← cg.eval (v.head :: v.tail.tail), pure $ if v.tail.head = 0 then sum.inl (v.head.succ :: v.tail.head.pred :: x.head :: v.tail.tail.tail : list β„•) else sum.inr (v.head.succ :: v.tail.head.pred :: x.head :: v.tail.tail.tail)) (a :: b :: nat.elim (f v.tail) (Ξ» y IH, g (y::IH::v.tail)) a :: v.val.tail), { rw (_ : pfun.fix _ _ = pure _), swap, exact roption.eq_some_iff.2 (this 0 n (zero_add n)), simp only [list.head, pure_bind, list.tail_cons] }, intros a b e, induction b with b IH generalizing a e, { refine pfun.mem_fix_iff.2 (or.inl $ roption.eq_some_iff.1 _), simp only [hg, ← e, pure_bind, list.tail_cons], refl }, { refine pfun.mem_fix_iff.2 (or.inr ⟨_, _, IH (a+1) (by rwa add_right_comm)⟩), simp only [hg, eval, pure_bind, nat.elim_succ, list.tail], exact roption.mem_some_iff.2 rfl } }, case comp : m n f g hf hg IHf IHg { exact exists_code.comp IHf IHg }, case rfind : n f hf IHf { obtain ⟨cf, hf⟩ := IHf, refine ⟨rfind cf, Ξ» v, _⟩, replace hf := Ξ» a, hf (a :: v), simp only [roption.map_eq_map, roption.map_some, vector.cons_val, pfun.coe_val, show βˆ€ x, pure x = [x], from Ξ» _, rfl] at hf ⊒, refine roption.ext (Ξ» x, _), simp only [rfind, roption.bind_eq_bind, roption.pure_eq_some, roption.map_eq_map, roption.bind_some, exists_prop, eval, list.head, pred_eval, roption.map_some, bool.ff_eq_to_bool_iff, roption.mem_bind_iff, list.length, roption.mem_map_iff, nat.mem_rfind, list.tail, bool.tt_eq_to_bool_iff, roption.mem_some_iff, roption.map_bind], split, { rintro ⟨v', h1, rfl⟩, suffices : βˆ€ (v₁ : list β„•), v' ∈ pfun.fix (Ξ» v, (cf.eval v).bind $ Ξ» y, roption.some $ if y.head = 0 then sum.inl (v.head.succ :: v.tail) else sum.inr (v.head.succ :: v.tail)) v₁ β†’ βˆ€ n, v₁ = n :: v.val β†’ (βˆ€ m < n, Β¬f (m :: v) = 0) β†’ (βˆƒ (a : β„•), (f (a :: v) = 0 ∧ βˆ€ {m : β„•}, m < a β†’ Β¬f (m :: v) = 0) ∧ [a] = [v'.head.pred]), { exact this _ h1 0 rfl (by rintro _ ⟨⟩) }, clear h1, intros vβ‚€ h1, refine pfun.fix_induction h1 (Ξ» v₁ h2 IH, _), clear h1, rintro n rfl hm, have := pfun.mem_fix_iff.1 h2, simp only [hf, roption.bind_some] at this, split_ifs at this, { simp only [list.head, exists_false, or_false, roption.mem_some_iff, list.tail_cons, false_and] at this, subst this, exact ⟨_, ⟨h, hm⟩, rfl⟩ }, { simp only [list.head, exists_eq_left, roption.mem_some_iff, list.tail_cons, false_or] at this, refine IH _ this (by simp [hf, h]) _ rfl (Ξ» m h', _), obtain h|rfl := nat.lt_succ_iff_lt_or_eq.1 h', exacts [hm _ h, h] } }, { rintro ⟨n, ⟨hn, hm⟩, rfl⟩, refine ⟨n.succ :: v.1, _, rfl⟩, have : (n.succ :: v.1 : list β„•) ∈ pfun.fix (Ξ» v, (cf.eval v).bind $ Ξ» y, roption.some $ if y.head = 0 then sum.inl (v.head.succ :: v.tail) else sum.inr (v.head.succ :: v.tail)) (n :: v.val) := pfun.mem_fix_iff.2 (or.inl (by simp [hf, hn])), generalize_hyp : (n.succ :: v.1 : list β„•) = w at this ⊒, clear hn, induction n with n IH, {exact this}, refine IH (Ξ» m h', hm (nat.lt_succ_of_lt h')) (pfun.mem_fix_iff.2 (or.inr ⟨_, _, this⟩)), simp only [hf, hm n.lt_succ_self, roption.bind_some, list.head, eq_self_iff_true, if_false, roption.mem_some_iff, and_self, list.tail_cons] } } end end code /-! ## From compositional semantics to sequential semantics Our initial sequential model is designed to be as similar as possible to the compositional semantics in terms of its primitives, but it is a sequential semantics, meaning that rather than defining an `eval c : list β„• β†’. list β„•` function for each program, defined by recursion on programs, we have a type `cfg` with a step function `step : cfg β†’ option cfg` that provides a deterministic evaluation order. In order to do this, we introduce the notion of a *continuation*, which can be viewed as a `code` with a hole in it where evaluation is currently taking place. Continuations can be assigned a `list β„• β†’. list β„•` semantics as well, with the interpretation being that given a `list β„•` result returned from the code in the hole, the remainder of the program will evaluate to a `list β„•` final value. The continuations are: * `halt`: the empty continuation: the hole is the whole program, whatever is returned is the final result. In our notation this is just `_`. * `cons₁ fs v k`: evaluating the first part of a `cons`, that is `k (_ :: fs v)`, where `k` is the outer continuation. * `consβ‚‚ ns k`: evaluating the second part of a `cons`: `k (ns.head :: _)`. (Technically we don't need to hold on to all of `ns` here since we are already committed to taking the head, but this is more regular.) * `comp f k`: evaluating the first part of a composition: `k (f _)`. * `fix f k`: waiting for the result of `f` in a `fix f` expression: `k (if _.head = 0 then _.tail else fix f (_.tail))` The type `cfg` of evaluation states is: * `ret k v`: we have received a result, and are now evaluating the continuation `k` with result `v`; that is, `k v` where `k` is ready to evaluate. * `halt v`: we are done and the result is `v`. The main theorem of this section is that for each code `c`, the state `step_normal c halt v` steps to `v'` in finitely many steps if and only if `code.eval c v = some v'`. -/ /-- The type of continuations, built up during evaluation of a `code` expression. -/ @[derive inhabited] inductive cont | halt | cons₁ : code β†’ list β„• β†’ cont β†’ cont | consβ‚‚ : list β„• β†’ cont β†’ cont | comp : code β†’ cont β†’ cont | fix : code β†’ cont β†’ cont /-- The semantics of a continuation. -/ def cont.eval : cont β†’ list β„• β†’. list β„• | cont.halt := pure | (cont.cons₁ fs as k) := Ξ» v, do ns ← code.eval fs as, cont.eval k (v.head :: ns) | (cont.consβ‚‚ ns k) := Ξ» v, cont.eval k (ns.head :: v) | (cont.comp f k) := Ξ» v, code.eval f v >>= cont.eval k | (cont.fix f k) := Ξ» v, if v.head = 0 then k.eval v.tail else f.fix.eval v.tail >>= k.eval /-- The semantics of a continuation. -/ @[derive inhabited] inductive cfg | halt : list β„• β†’ cfg | ret : cont β†’ list β„• β†’ cfg /-- Evaluating `c : code` in a continuation `k : cont` and input `v : list β„•`. This goes by recursion on `c`, building an augmented continuation and a value to pass to it. * `zero' v = 0 :: v` evaluates immediately, so we return it to the parent continuation * `succ v = [v.head.succ]` evaluates immediately, so we return it to the parent continuation * `tail v = v.tail` evaluates immediately, so we return it to the parent continuation * `cons f fs v = (f v).head :: fs v` requires two sub-evaluations, so we evaluate `f v` in the continuation `k (_.head :: fs v)` (called `cont.cons₁ fs v k`) * `comp f g v = f (g v)` requires two sub-evaluations, so we evaluate `g v` in the continuation `k (f _)` (called `cont.comp f k`) * `case f g v = v.head.cases_on (f v.tail) (Ξ» n, g (n :: v.tail))` has the information needed to evaluate the case statement, so we do that and transition to either `f v` or `g (n :: v.tail)`. * `fix f v = let v' := f v in if v'.head = 0 then k v'.tail else fix f v'.tail` needs to first evaluate `f v`, so we do that and leave the rest for the continuation (called `cont.fix f k`) -/ def step_normal : code β†’ cont β†’ list β„• β†’ cfg | code.zero' k v := cfg.ret k (0 :: v) | code.succ k v := cfg.ret k [v.head.succ] | code.tail k v := cfg.ret k v.tail | (code.cons f fs) k v := step_normal f (cont.cons₁ fs v k) v | (code.comp f g) k v := step_normal g (cont.comp f k) v | (code.case f g) k v := v.head.elim (step_normal f k v.tail) (Ξ» y _, step_normal g k (y :: v.tail)) | (code.fix f) k v := step_normal f (cont.fix f k) v /-- Evaluating a continuation `k : cont` on input `v : list β„•`. This is the second part of evaluation, when we receive results from continuations built by `step_normal`. * `cont.halt v = v`, so we are done and transition to the `cfg.halt v` state * `cont.cons₁ fs as k v = k (v.head :: fs as)`, so we evaluate `fs as` now with the continuation `k (v.head :: _)` (called `consβ‚‚ v k`). * `cont.consβ‚‚ ns k v = k (ns.head :: v)`, where we now have everything we need to evaluate `ns.head :: v`, so we return it to `k`. * `cont.comp f k v = k (f v)`, so we call `f v` with `k` as the continuation. * `cont.fix f k v = k (if v.head = 0 then k v.tail else fix f v.tail)`, where `v` is a value, so we evaluate the if statement and either call `k` with `v.tail`, or call `fix f v` with `k` as the continuation (which immediately calls `f` with `cont.fix f k` as the continuation). -/ def step_ret : cont β†’ list β„• β†’ cfg | cont.halt v := cfg.halt v | (cont.cons₁ fs as k) v := step_normal fs (cont.consβ‚‚ v k) as | (cont.consβ‚‚ ns k) v := step_ret k (ns.head :: v) | (cont.comp f k) v := step_normal f k v | (cont.fix f k) v := if v.head = 0 then step_ret k v.tail else step_normal f (cont.fix f k) v.tail /-- If we are not done (in `cfg.halt` state), then we must be still stuck on a continuation, so this main loop calls `step_ret` with the new continuation. The overall `step` function transitions from one `cfg` to another, only halting at the `cfg.halt` state. -/ def step : cfg β†’ option cfg | (cfg.halt _) := none | (cfg.ret k v) := some (step_ret k v) /-- In order to extract a compositional semantics from the sequential execution behavior of configurations, we observe that continuations have a monoid structure, with `cont.halt` as the unit and `cont.then` as the multiplication. `cont.then k₁ kβ‚‚` runs `k₁` until it halts, and then takes the result of `k₁` and passes it to `kβ‚‚`. We will not prove it is associative (although it is), but we are instead interested in the associativity law `kβ‚‚ (eval c k₁) = eval c (k₁.then kβ‚‚)`. This holds at both the sequential and compositional levels, and allows us to express running a machine without the ambient continuation and relate it to the original machine's evaluation steps. In the literature this is usually where one uses Turing machines embedded inside other Turing machines, but this approach allows us to avoid changing the ambient type `cfg` in the middle of the recursion. -/ def cont.then : cont β†’ cont β†’ cont | cont.halt k' := k' | (cont.cons₁ fs as k) k' := cont.cons₁ fs as (k.then k') | (cont.consβ‚‚ ns k) k' := cont.consβ‚‚ ns (k.then k') | (cont.comp f k) k' := cont.comp f (k.then k') | (cont.fix f k) k' := cont.fix f (k.then k') theorem cont.then_eval {k k' : cont} {v} : (k.then k').eval v = k.eval v >>= k'.eval := begin induction k generalizing v; simp only [cont.eval, cont.then, bind_assoc, pure_bind, *], { simp only [← k_ih] }, { split_ifs; [refl, simp only [← k_ih, bind_assoc]] } end /-- The `then k` function is a "configuration homomorphism". Its operation on states is to append `k` to the continuation of a `cfg.ret` state, and to run `k` on `v` if we are in the `cfg.halt v` state. -/ def cfg.then : cfg β†’ cont β†’ cfg | (cfg.halt v) k' := step_ret k' v | (cfg.ret k v) k' := cfg.ret (k.then k') v /-- The `step_normal` function respects the `then k'` homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state. -/ theorem step_normal_then (c) (k k' : cont) (v) : step_normal c (k.then k') v = (step_normal c k v).then k' := begin induction c generalizing k v; simp only [cont.then, step_normal, cfg.then, *] {constructor_eq := ff}, { rw [← c_ih_a, cont.then] }, { rw [← c_ih_a_1, cont.then] }, { cases v.head; simp only [nat.elim] }, { rw [← c_ih, cont.then] }, end /-- The `step_ret` function respects the `then k'` homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state. -/ theorem step_ret_then {k k' : cont} {v} : step_ret (k.then k') v = (step_ret k v).then k' := begin induction k generalizing v; simp only [cont.then, step_ret, cfg.then, *], { rw ← step_normal_then, refl }, { rw ← step_normal_then }, { split_ifs, {rw ← k_ih}, {rw ← step_normal_then, refl} }, end /-- This is a temporary definition, because we will prove in `code_is_ok` that it always holds. It asserts that `c` is semantically correct; that is, for any `k` and `v`, `eval (step_normal c k v) = eval (cfg.ret k (code.eval c v))`, as an equality of partial values (so one diverges iff the other does). In particular, we can let `k = cont.halt`, and then this asserts that `step_normal c cont.halt v` evaluates to `cfg.halt (code.eval c v)`. -/ def code.ok (c : code) := βˆ€ k v, eval step (step_normal c k v) = code.eval c v >>= Ξ» v, eval step (cfg.ret k v) theorem code.ok.zero {c} (h : code.ok c) {v} : eval step (step_normal c cont.halt v) = cfg.halt <$> code.eval c v := begin rw [h, ← bind_pure_comp_eq_map], congr, funext v, exact roption.eq_some_iff.2 (mem_eval.2 ⟨refl_trans_gen.single rfl, rfl⟩), end theorem step_normal.is_ret (c k v) : βˆƒ k' v', step_normal c k v = cfg.ret k' v' := begin induction c generalizing k v, iterate 3 { exact ⟨_, _, rfl⟩ }, case cons : f fs IHf IHfs { apply IHf }, case comp : f g IHf IHg { apply IHg }, case case : f g IHf IHg { rw step_normal, cases v.head; simp only [nat.elim]; [apply IHf, apply IHg] }, case fix : f IHf { apply IHf }, end theorem cont_eval_fix {f k v} (fok : code.ok f) : eval step (step_normal f (cont.fix f k) v) = f.fix.eval v >>= Ξ» v, eval step (cfg.ret k v) := begin refine roption.ext (Ξ» x, _), simp only [roption.bind_eq_bind, roption.mem_bind_iff], split, { suffices : βˆ€ c, x ∈ eval step c β†’ βˆ€ v c', c = cfg.then c' (cont.fix f k) β†’ reaches step (step_normal f cont.halt v) c' β†’ βˆƒ v₁ ∈ f.eval v, βˆƒ vβ‚‚ ∈ (if list.head v₁ = 0 then pure v₁.tail else f.fix.eval v₁.tail), x ∈ eval step (cfg.ret k vβ‚‚), { intro h, obtain ⟨v₁, hv₁, vβ‚‚, hvβ‚‚, hβ‚ƒβŸ© := this _ h _ _ (step_normal_then _ cont.halt _ _) refl_trans_gen.refl, refine ⟨vβ‚‚, pfun.mem_fix_iff.2 _, hβ‚ƒβŸ©, simp only [roption.eq_some_iff.2 hv₁, roption.map_some], split_ifs at hvβ‚‚ ⊒, { rw roption.mem_some_iff.1 hvβ‚‚, exact or.inl (roption.mem_some _) }, { exact or.inr ⟨_, roption.mem_some _, hvβ‚‚βŸ© } }, refine Ξ» c he, eval_induction he (Ξ» y h IH, _), rintro v (⟨v'⟩ | ⟨k',v'⟩) rfl hr; rw cfg.then at h IH, { have := mem_eval.2 ⟨hr, rfl⟩, rw [fok, roption.bind_eq_bind, roption.mem_bind_iff] at this, obtain ⟨v'', h₁, hβ‚‚βŸ© := this, rw reaches_eval at hβ‚‚, swap, exact refl_trans_gen.single rfl, cases roption.mem_unique hβ‚‚ (mem_eval.2 ⟨refl_trans_gen.refl, rfl⟩), refine ⟨v', h₁, _⟩, rw [step_ret] at h, revert h, by_cases he : v'.head = 0; simp only [exists_prop, if_pos, if_false, he]; intro h, { refine ⟨_, roption.mem_some _, _⟩, rw reaches_eval, exact h, exact refl_trans_gen.single rfl }, { obtain ⟨kβ‚€, vβ‚€, eβ‚€βŸ© := step_normal.is_ret f cont.halt v'.tail, have e₁ := step_normal_then f cont.halt (cont.fix f k) v'.tail, rw [eβ‚€, cont.then, cfg.then] at e₁, obtain ⟨v₁, hv₁, vβ‚‚, hvβ‚‚, hβ‚ƒβŸ© := IH (step_ret (kβ‚€.then (cont.fix f k)) vβ‚€) _ _ v'.tail _ step_ret_then _, { refine ⟨_, pfun.mem_fix_iff.2 _, hβ‚ƒβŸ©, simp only [roption.eq_some_iff.2 hv₁, roption.map_some, roption.mem_some_iff], split_ifs at hvβ‚‚ ⊒; [exact or.inl (roption.mem_some_iff.1 hvβ‚‚), exact or.inr ⟨_, rfl, hvβ‚‚βŸ©] }, { rwa [← @reaches_eval _ _ (cfg.ret (kβ‚€.then (cont.fix f k)) vβ‚€), ← e₁], exact refl_trans_gen.single rfl }, { rw [step_ret, if_neg he, e₁], refl }, { apply refl_trans_gen.single, rw eβ‚€, exact rfl } } }, { rw reaches_eval at h, swap, exact refl_trans_gen.single rfl, exact IH _ h rfl _ _ step_ret_then (refl_trans_gen.tail hr rfl) } }, { rintro ⟨v', he, hr⟩, rw reaches_eval at hr, swap, exact refl_trans_gen.single rfl, refine pfun.fix_induction he (Ξ» v (he : v' ∈ f.fix.eval v) IH, _), rw [fok, roption.bind_eq_bind, roption.mem_bind_iff], obtain he | ⟨v'', he₁', heβ‚‚'⟩ := pfun.mem_fix_iff.1 he, { obtain ⟨v', he₁, heβ‚‚βŸ© := (roption.mem_map_iff _).1 he, split_ifs at heβ‚‚; cases heβ‚‚, refine ⟨_, he₁, _⟩, rw reaches_eval, swap, exact refl_trans_gen.single rfl, rwa [step_ret, if_pos h] }, { obtain ⟨v₁, he₁, heβ‚‚βŸ© := (roption.mem_map_iff _).1 he₁', split_ifs at heβ‚‚; cases heβ‚‚, clear heβ‚‚ he₁', change _ ∈ f.fix.eval _ at heβ‚‚', refine ⟨_, he₁, _⟩, rw reaches_eval, swap, exact refl_trans_gen.single rfl, rwa [step_ret, if_neg h], exact IH v₁.tail heβ‚‚' ((roption.mem_map_iff _).2 ⟨_, he₁, if_neg h⟩) } } end theorem code_is_ok (c) : code.ok c := begin induction c; intros k v; rw step_normal, iterate 3 { simp only [code.eval, pure_bind] }, case cons : f fs IHf IHfs { rw [code.eval, IHf], simp only [bind_assoc, cont.eval, pure_bind], congr, funext v, rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IHfs], congr, funext v', refine eq.trans _ (eq.symm _); try {exact reaches_eval (refl_trans_gen.single rfl)} }, case comp : f g IHf IHg { rw [code.eval, IHg], simp only [bind_assoc, cont.eval, pure_bind], congr, funext v, rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IHf] }, case case : f g IHf IHg { simp only [code.eval], cases v.head; simp only [nat.elim, code.eval]; [apply IHf, apply IHg] }, case fix : f IHf { rw cont_eval_fix IHf }, end theorem step_normal_eval (c v) : eval step (step_normal c cont.halt v) = cfg.halt <$> c.eval v := (code_is_ok c).zero theorem step_ret_eval {k v} : eval step (step_ret k v) = cfg.halt <$> k.eval v := begin induction k generalizing v, case halt : { simp only [mem_eval, cont.eval, map_pure], exact roption.eq_some_iff.2 (mem_eval.2 ⟨refl_trans_gen.refl, rfl⟩) }, case cons₁ : fs as k IH { rw [cont.eval, step_ret, code_is_ok], simp only [← bind_pure_comp_eq_map, bind_assoc], congr, funext v', rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IH, bind_pure_comp_eq_map] }, case consβ‚‚ : ns k IH { rw [cont.eval, step_ret], exact IH }, case comp : f k IH { rw [cont.eval, step_ret, code_is_ok], simp only [← bind_pure_comp_eq_map, bind_assoc], congr, funext v', rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [IH, bind_pure_comp_eq_map] }, case fix : f k IH { rw [cont.eval, step_ret], simp only [bind_pure_comp_eq_map], split_ifs, { exact IH }, simp only [← bind_pure_comp_eq_map, bind_assoc, cont_eval_fix (code_is_ok _)], congr, funext, rw [bind_pure_comp_eq_map, ← IH], exact reaches_eval (refl_trans_gen.single rfl) }, end end to_partrec /-! ## Simulating sequentialized partial recursive functions in TM2 At this point we have a sequential model of partial recursive functions: the `cfg` type and `step : cfg β†’ option cfg` function from the previous section. The key feature of this model is that it does a finite amount of computation (in fact, an amount which is statically bounded by the size of the program) between each step, and no individual step can diverge (unlike the compositional semantics, where every sub-part of the computation is potentially divergent). So we can utilize the same techniques as in the other TM simulations in `computability.turing_machine` to prove that each step corresponds to a finite number of steps in a lower level model. (We don't prove it here, but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.) The target model is `turing.TM2`, which has a fixed finite set of stacks, a bit of local storage, with programs selected from a potentially infinite (but finitely accessible) set of program positions, or labels `Ξ›`, each of which executes a finite sequence of basic stack commands. For this program we will need four stacks, each on an alphabet `Ξ“'` like so: inductive Ξ“' | Cons | cons | bit0 | bit1 We represent a number as a bit sequence, lists of numbers by putting `cons` after each element, and lists of lists of natural numbers by putting `Cons` after each list. For example: 0 ~> [] 1 ~> [bit1] 6 ~> [bit0, bit1, bit1] [1, 2] ~> [bit1, cons, bit0, bit1, cons] [[], [1, 2]] ~> [Cons, bit1, cons, bit0, bit1, cons, Cons] The four stacks are `main`, `rev`, `aux`, `stack`. In normal mode, `main` contains the input to the current program (a `list β„•`) and `stack` contains data (a `list (list β„•)`) associated to the current continuation, and in `ret` mode `main` contains the value that is being passed to the continuation and `stack` contains the data for the continuation. The `rev` and `aux` stacks are usually empty; `rev` is used to store reversed data when e.g. moving a value from one stack to another, while `aux` is used as a temporary for a `main`/`stack` swap that happens during `cons₁` evaluation. The only local store we need is `option Ξ“'`, which stores the result of the last pop operation. (Most of our working data are natural numbers, which are too large to fit in the local store.) The continuations from the previous section are data-carrying, containing all the values that have been computed and are awaiting other arguments. In order to have only a finite number of continuations appear in the program so that they can be used in machine states, we separate the data part (anything with type `list β„•`) from the `cont` type, producing a `cont'` type that lacks this information. The data is kept on the `stack` stack. Because we want to have subroutines for e.g. moving an entire stack to another place, we use an infinite inductive type `Ξ›'` so that we can execute a program and then return to do something else without having to define too many different kinds of intermediate states. (We must nevertheless prove that only finitely many labels are accessible.) The labels are: * `move p k₁ kβ‚‚ q`: move elements from stack `k₁` to `kβ‚‚` while `p` holds of the value being moved. The last element, that fails `p`, is placed in neither stack but left in the local store. At the end of the operation, `kβ‚‚` will have the elements of `k₁` in reverse order. Then do `q`. * `clear p k q`: delete elements from stack `k` until `p` is true. Like `move`, the last element is left in the local storage. Then do `q`. * `copy q`: Move all elements from `rev` to both `main` and `stack` (in reverse order), then do `q`. That is, it takes `(a, b, c, d)` to `(b.reverse ++ a, [], c, b.reverse ++ d)`. * `push k f q`: push `f s`, where `s` is the local store, to stack `k`, then do `q`. This is a duplicate of the `push` instruction that is part of the TM2 model, but by having a subroutine just for this purpose we can build up programs to execute inside a `goto` statement, where we have the flexibility to be general recursive. * `read (f : option Ξ“' β†’ Ξ›')`: go to state `f s` where `s` is the local store. Again this is only here for convenience. * `succ q`: perform a successor operation. Assuming `[n]` is encoded on `main` before, `[n+1]` will be on main after. This implements successor for binary natural numbers. * `pred q₁ qβ‚‚`: perform a predecessor operation or `case` statement. If `[]` is encoded on `main` before, then we transition to `q₁` with `[]` on main; if `(0 :: v)` is on `main` before then `v` will be on `main` after and we transition to `q₁`; and if `(n+1 :: v)` is on `main` before then `n :: v` will be on `main` after and we transition to `qβ‚‚`. * `ret k`: call continuation `k`. Each continuation has its own interpretation of the data in `stack` and sets up the data for the next continuation. * `ret (cons₁ fs k)`: `v :: k_data` on `stack` and `ns` on `main`, and the next step expects `v` on `main` and `ns :: k_data` on `stack`. So we have to do a little dance here with six reverse-moves using the `aux` stack to perform a three-point swap, each of which involves two reversals. * `ret (consβ‚‚ k)`: `ns :: k_data` is on `stack` and `v` is on `main`, and we have to put `ns.head :: v` on `main` and `k_data` on `stack`. This is done using the `head` subroutine. * `ret (fix f k)`: This stores no data, so we just check if `main` starts with `0` and if so, remove it and call `k`, otherwise `clear` the first value and call `f`. * `ret halt`: the stack is empty, and `main` has the output. Do nothing and halt. In addition to these basic states, we define some additional subroutines that are used in the above: * `push'`, `peek'`, `pop'` are special versions of the builtins that use the local store to supply inputs and outputs. * `unrev`: special case `move ff rev main` to move everything from `rev` back to `main`. Used as a cleanup operation in several functions. * `move_excl p k₁ kβ‚‚ q`: same as `move` but pushes the last value read back onto the source stack. * `moveβ‚‚ p k₁ kβ‚‚ q`: double `move`, so that the result comes out in the right order at the target stack. Implemented as `move_excl p k rev; move ff rev kβ‚‚`. Assumes that neither `k₁` nor `kβ‚‚` is `rev` and `rev` is initially empty. * `head k q`: get the first natural number from stack `k` and reverse-move it to `rev`, then clear the rest of the list at `k` and then `unrev` to reverse-move the head value to `main`. This is used with `k = main` to implement regular `head`, i.e. if `v` is on `main` before then `[v.head]` will be on `main` after; and also with `k = stack` for the `cons` operation, which has `v` on `main` and `ns :: k_data` on `stack`, and results in `k_data` on `stack` and `ns.head :: v` on `main`. * `tr_normal` is the main entry point, defining states that perform a given `code` computation. It mostly just dispatches to functions written above. The main theorem of this section is `tr_eval`, which asserts that for each that for each code `c`, the state `init c v` steps to `halt v'` in finitely many steps if and only if `code.eval c v = some v'`. -/ namespace partrec_to_TM2 section open to_partrec /-- The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `β„•` values as lists of binary digits, `cons` is used to separate `list β„•` values, and `Cons` is used to separate `list (list β„•)` values. See the section documentation. -/ @[derive [decidable_eq, inhabited]] inductive Ξ“' | Cons | cons | bit0 | bit1 /-- The four stacks used by the program. `main` is used to store the input value in `tr_normal` mode and the output value in `Ξ›'.ret` mode, while `stack` is used to keep all the data for the continuations. `rev` is used to store reversed lists when transferring values between stacks, and `aux` is only used once in `cons₁`. See the section documentation. -/ @[derive [decidable_eq, inhabited]] inductive K' | main | rev | aux | stack open K' /-- Continuations as in `to_partrec.cont` but with the data removed. This is done because we want the set of all continuations in the program to be finite (so that it can ultimately be encoded into the finite state machine of a Turing machine), but a continuation can handle a potentially infinite number of data values during execution. -/ @[derive inhabited] inductive cont' | halt | cons₁ : code β†’ cont' β†’ cont' | consβ‚‚ : cont' β†’ cont' | comp : code β†’ cont' β†’ cont' | fix : code β†’ cont' β†’ cont' /-- The set of program positions. We make extensive use of inductive types here to let us describe "subroutines"; for example `clear p k q` is a program that clears stack `k`, then does `q` where `q` is another label. In order to prevent this from resulting in an infinite number of distinct accessible states, we are careful to be non-recursive (although loops are okay). See the section documentation for a description of all the programs. -/ inductive Ξ›' | move (p : Ξ“' β†’ bool) (k₁ kβ‚‚ : K') (q : Ξ›') | clear (p : Ξ“' β†’ bool) (k : K') (q : Ξ›') | copy (q : Ξ›') | push (k : K') (s : option Ξ“' β†’ option Ξ“') (q : Ξ›') | read (f : option Ξ“' β†’ Ξ›') | succ (q : Ξ›') | pred (q₁ qβ‚‚ : Ξ›') | ret (k : cont') instance : inhabited Ξ›' := βŸ¨Ξ›'.ret cont'.halt⟩ /-- The type of TM2 statements used by this machine. -/ @[derive inhabited] def stmt' := TM2.stmt (Ξ» _:K', Ξ“') Ξ›' (option Ξ“') /-- The type of TM2 configurations used by this machine. -/ @[derive inhabited] def cfg' := TM2.cfg (Ξ» _:K', Ξ“') Ξ›' (option Ξ“') open TM2.stmt /-- A predicate that detects the end of a natural number, either `Ξ“'.cons` or `Ξ“'.Cons` (or implicitly the end of the list), for use in predicate-taking functions like `move` and `clear`. -/ def nat_end : Ξ“' β†’ bool | Ξ“'.Cons := tt | Ξ“'.cons := tt | _ := ff /-- Pop a value from the stack and place the result in local store. -/ @[simp] def pop' (k : K') : stmt' β†’ stmt' := pop k (Ξ» x v, v) /-- Peek a value from the stack and place the result in local store. -/ @[simp] def peek' (k : K') : stmt' β†’ stmt' := peek k (Ξ» x v, v) /-- Push the value in the local store to the given stack. -/ @[simp] def push' (k : K') : stmt' β†’ stmt' := push k (Ξ» x, x.iget) /-- Move everything from the `rev` stack to the `main` stack (reversed). -/ def unrev := Ξ›'.move (Ξ» _, ff) rev main /-- Move elements from `k₁` to `kβ‚‚` while `p` holds, with the last element being left on `k₁`. -/ def move_excl (p k₁ kβ‚‚ q) := Ξ›'.move p k₁ kβ‚‚ $ Ξ›'.push k₁ id q /-- Move elements from `k₁` to `kβ‚‚` without reversion, by performing a double move via the `rev` stack. -/ def moveβ‚‚ (p k₁ kβ‚‚ q) := move_excl p k₁ rev $ Ξ›'.move (Ξ» _, ff) rev kβ‚‚ q /-- Assuming `tr_list v` is on the front of stack `k`, remove it, and push `v.head` onto `main`. See the section documentation. -/ def head (k : K') (q : Ξ›') : Ξ›' := Ξ›'.move nat_end k rev $ Ξ›'.push rev (Ξ» _, some Ξ“'.cons) $ Ξ›'.read $ Ξ» s, (if s = some Ξ“'.Cons then id else Ξ›'.clear (Ξ» x, x = Ξ“'.Cons) k) $ unrev q /-- The program that evaluates code `c` with continuation `k`. This expects an initial state where `tr_list v` is on `main`, `tr_cont_stack k` is on `stack`, and `aux` and `rev` are empty. See the section documentation for details. -/ @[simp] def tr_normal : code β†’ cont' β†’ Ξ›' | code.zero' k := Ξ›'.push main (Ξ» _, some Ξ“'.cons) $ Ξ›'.ret k | code.succ k := head main $ Ξ›'.succ $ Ξ›'.ret k | code.tail k := Ξ›'.clear nat_end main $ Ξ›'.ret k | (code.cons f fs) k := Ξ›'.push stack (Ξ» _, some Ξ“'.Cons) $ Ξ›'.move (Ξ» _, ff) main rev $ Ξ›'.copy $ tr_normal f (cont'.cons₁ fs k) | (code.comp f g) k := tr_normal g (cont'.comp f k) | (code.case f g) k := Ξ›'.pred (tr_normal f k) (tr_normal g k) | (code.fix f) k := tr_normal f (cont'.fix f k) /-- The main program. See the section documentation for details. -/ @[simp] def tr : Ξ›' β†’ stmt' | (Ξ›'.move p k₁ kβ‚‚ q) := pop' k₁ $ branch (Ξ» s, s.elim tt p) ( goto $ Ξ» _, q ) ( push' kβ‚‚ $ goto $ Ξ» _, Ξ›'.move p k₁ kβ‚‚ q ) | (Ξ›'.push k f q) := branch (Ξ» s, (f s).is_some) ( push k (Ξ» s, (f s).iget) $ goto $ Ξ» _, q ) ( goto $ Ξ» _, q ) | (Ξ›'.read q) := goto q | (Ξ›'.clear p k q) := pop' k $ branch (Ξ» s, s.elim tt p) ( goto $ Ξ» _, q ) ( goto $ Ξ» _, Ξ›'.clear p k q ) | (Ξ›'.copy q) := pop' rev $ branch option.is_some ( push' main $ push' stack $ goto $ Ξ» _, Ξ›'.copy q ) ( goto $ Ξ» _, q ) | (Ξ›'.succ q) := pop' main $ branch (Ξ» s, s = some Ξ“'.bit1) ( push rev (Ξ» _, Ξ“'.bit0) $ goto $ Ξ» _, Ξ›'.succ q ) $ branch (Ξ» s, s = some Ξ“'.cons) ( push main (Ξ» _, Ξ“'.cons) $ push main (Ξ» _, Ξ“'.bit1) $ goto $ Ξ» _, unrev q ) ( push main (Ξ» _, Ξ“'.bit1) $ goto $ Ξ» _, unrev q ) | (Ξ›'.pred q₁ qβ‚‚) := pop' main $ branch (Ξ» s, s = some Ξ“'.bit0) ( push rev (Ξ» _, Ξ“'.bit1) $ goto $ Ξ» _, Ξ›'.pred q₁ qβ‚‚ ) $ branch (Ξ» s, nat_end s.iget) ( goto $ Ξ» _, q₁ ) ( peek' main $ branch (Ξ» s, nat_end s.iget) ( goto $ Ξ» _, unrev qβ‚‚ ) ( push rev (Ξ» _, Ξ“'.bit0) $ goto $ Ξ» _, unrev qβ‚‚ ) ) | (Ξ›'.ret (cont'.cons₁ fs k)) := goto $ Ξ» _, moveβ‚‚ (Ξ» _, ff) main aux $ moveβ‚‚ (Ξ» s, s = Ξ“'.Cons) stack main $ moveβ‚‚ (Ξ» _, ff) aux stack $ tr_normal fs (cont'.consβ‚‚ k) | (Ξ›'.ret (cont'.consβ‚‚ k)) := goto $ Ξ» _, head stack $ Ξ›'.ret k | (Ξ›'.ret (cont'.comp f k)) := goto $ Ξ» _, tr_normal f k | (Ξ›'.ret (cont'.fix f k)) := pop' main $ goto $ Ξ» s, cond (nat_end s.iget) (Ξ›'.ret k) $ Ξ›'.clear nat_end main $ tr_normal f (cont'.fix f k) | (Ξ›'.ret cont'.halt) := load (Ξ» _, none) $ halt /-- Translating a `cont` continuation to a `cont'` continuation simply entails dropping all the data. This data is instead encoded in `tr_cont_stack` in the configuration. -/ def tr_cont : cont β†’ cont' | cont.halt := cont'.halt | (cont.cons₁ c _ k) := cont'.cons₁ c (tr_cont k) | (cont.consβ‚‚ _ k) := cont'.consβ‚‚ (tr_cont k) | (cont.comp c k) := cont'.comp c (tr_cont k) | (cont.fix c k) := cont'.fix c (tr_cont k) /-- We use `pos_num` to define the translation of binary natural numbers. A natural number is represented as a little-endian list of `bit0` and `bit1` elements: 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] In particular, this representation guarantees no trailing `bit0`'s at the end of the list. -/ def tr_pos_num : pos_num β†’ list Ξ“' | pos_num.one := [Ξ“'.bit1] | (pos_num.bit0 n) := Ξ“'.bit0 :: tr_pos_num n | (pos_num.bit1 n) := Ξ“'.bit1 :: tr_pos_num n /-- We use `num` to define the translation of binary natural numbers. Positive numbers are translated using `tr_pos_num`, and `tr_num 0 = []`. So there are never any trailing `bit0`'s in a translated `num`. 0 = [] 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] -/ def tr_num : num β†’ list Ξ“' | num.zero := [] | (num.pos n) := tr_pos_num n /-- Because we use binary encoding, we define `tr_nat` in terms of `tr_num`, using `num`, which are binary natural numbers. (We could also use `nat.binary_rec_on`, but `num` and `pos_num` make for easy inductions.) -/ def tr_nat (n : β„•) : list Ξ“' := tr_num n @[simp] theorem tr_nat_zero : tr_nat 0 = [] := rfl /-- Lists are translated with a `cons` after each encoded number. For example: [] = [] [0] = [cons] [1] = [bit1, cons] [6, 0] = [bit0, bit1, bit1, cons, cons] -/ @[simp] def tr_list : list β„• β†’ list Ξ“' | [] := [] | (n :: ns) := tr_nat n ++ Ξ“'.cons :: tr_list ns /-- Lists of lists are translated with a `Cons` after each encoded list. For example: [] = [] [[]] = [Cons] [[], []] = [Cons, Cons] [[0]] = [cons, Cons] [[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, Cons, cons, Cons] -/ @[simp] def tr_llist : list (list β„•) β†’ list Ξ“' | [] := [] | (l :: ls) := tr_list l ++ Ξ“'.Cons :: tr_llist ls /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `tr_llist`. -/ @[simp] def cont_stack : cont β†’ list (list β„•) | cont.halt := [] | (cont.cons₁ _ ns k) := ns :: cont_stack k | (cont.consβ‚‚ ns k) := ns :: cont_stack k | (cont.comp _ k) := cont_stack k | (cont.fix _ k) := cont_stack k /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `tr_llist`. -/ def tr_cont_stack (k : cont) := tr_llist (cont_stack k) /-- This is the nondependent eliminator for `K'`, but we use it specifically here in order to represent the stack data as four lists rather than as a function `K' β†’ list Ξ“'`, because this makes rewrites easier. The theorems `K'.elim_update_main` et. al. show how such a function is updated after an `update` to one of the components. -/ @[simp] def K'.elim (a b c d : list Ξ“') : K' β†’ list Ξ“' | K'.main := a | K'.rev := b | K'.aux := c | K'.stack := d @[simp] theorem K'.elim_update_main {a b c d a'} : update (K'.elim a b c d) main a' = K'.elim a' b c d := by funext x; cases x; refl @[simp] theorem K'.elim_update_rev {a b c d b'} : update (K'.elim a b c d) rev b' = K'.elim a b' c d := by funext x; cases x; refl @[simp] theorem K'.elim_update_aux {a b c d c'} : update (K'.elim a b c d) aux c' = K'.elim a b c' d := by funext x; cases x; refl @[simp] theorem K'.elim_update_stack {a b c d d'} : update (K'.elim a b c d) stack d' = K'.elim a b c d' := by funext x; cases x; refl /-- The halting state corresponding to a `list β„•` output value. -/ def halt (v : list β„•) : cfg' := ⟨none, none, K'.elim (tr_list v) [] [] []⟩ /-- The `cfg` states map to `cfg'` states almost one to one, except that in normal operation the local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly clear it in the halt state so that there is exactly one configuration corresponding to output `v`. -/ def tr_cfg : cfg β†’ cfg' β†’ Prop | (cfg.ret k v) c' := βˆƒ s, c' = ⟨some (Ξ›'.ret (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ | (cfg.halt v) c' := c' = halt v /-- This could be a general list definition, but it is also somewhat specialized to this application. `split_at_pred p L` will search `L` for the first element satisfying `p`. If it is found, say `L = l₁ ++ a :: lβ‚‚` where `a` satisfies `p` but `l₁` does not, then it returns `(l₁, some a, lβ‚‚)`. Otherwise, if there is no such element, it returns `(L, none, [])`. -/ def split_at_pred {Ξ±} (p : Ξ± β†’ bool) : list Ξ± β†’ list Ξ± Γ— option Ξ± Γ— list Ξ± | [] := ([], none, []) | (a :: as) := cond (p a) ([], some a, as) $ let ⟨l₁, o, lβ‚‚βŸ© := split_at_pred as in ⟨a :: l₁, o, lβ‚‚βŸ© theorem split_at_pred_eq {Ξ±} (p : Ξ± β†’ bool) : βˆ€ L l₁ o lβ‚‚, (βˆ€ x ∈ l₁, p x = ff) β†’ option.elim o (L = l₁ ∧ lβ‚‚ = []) (Ξ» a, p a = tt ∧ L = l₁ ++ a :: lβ‚‚) β†’ split_at_pred p L = (l₁, o, lβ‚‚) | [] _ none _ _ ⟨rfl, rfl⟩ := rfl | [] l₁ (some o) lβ‚‚ h₁ ⟨hβ‚‚, hβ‚ƒβŸ© := by simp at h₃; contradiction | (a :: L) l₁ o lβ‚‚ h₁ hβ‚‚ := begin rw [split_at_pred], have IH := split_at_pred_eq L, cases o, { cases l₁ with a' l₁; rcases hβ‚‚ with ⟨⟨⟩, rfl⟩, rw [h₁ a (or.inl rfl), cond, IH L none [] _ ⟨rfl, rfl⟩], refl, exact Ξ» x h, h₁ x (or.inr h) }, { cases l₁ with a' l₁; rcases hβ‚‚ with ⟨hβ‚‚, ⟨⟩⟩, {rw [hβ‚‚, cond]}, rw [h₁ a (or.inl rfl), cond, IH l₁ (some o) lβ‚‚ _ ⟨hβ‚‚, _⟩]; try {refl}, exact Ξ» x h, h₁ x (or.inr h) }, end theorem split_at_pred_ff {Ξ±} (L : list Ξ±) : split_at_pred (Ξ» _, ff) L = (L, none, []) := split_at_pred_eq _ _ _ _ _ (Ξ» _ _, rfl) ⟨rfl, rfl⟩ theorem move_ok {p k₁ kβ‚‚ q s L₁ o Lβ‚‚} {S : K' β†’ list Ξ“'} (h₁ : k₁ β‰  kβ‚‚) (e : split_at_pred p (S k₁) = (L₁, o, Lβ‚‚)) : reaches₁ (TM2.step tr) ⟨some (Ξ›'.move p k₁ kβ‚‚ q), s, S⟩ ⟨some q, o, update (update S k₁ Lβ‚‚) kβ‚‚ (L₁.reverse_core (S kβ‚‚))⟩ := begin induction L₁ with a L₁ IH generalizing S s, { rw [(_ : [].reverse_core _ = _), function.update_eq_self], swap, { rw function.update_noteq h₁.symm, refl }, refine trans_gen.head' rfl _, simp, cases S k₁ with a Sk, {cases e, refl}, simp [split_at_pred] at e ⊒, cases p a; simp at e ⊒, { revert e, rcases split_at_pred p Sk with ⟨_, _, _⟩, rintro ⟨⟩ }, { simp only [e] } }, { refine trans_gen.head rfl _, simp, cases e₁ : S k₁ with a' Sk; rw [e₁, split_at_pred] at e, {cases e}, cases eβ‚‚ : p a'; simp only [eβ‚‚, cond] at e, swap, {cases e}, rcases e₃ : split_at_pred p Sk with ⟨_, _, _⟩, rw [e₃, split_at_pred] at e, cases e, simp [eβ‚‚], convert @IH (update (update S k₁ Sk) kβ‚‚ (a :: S kβ‚‚)) _ _ using 2; simp [function.update_noteq, h₁, h₁.symm, e₃, list.reverse_core], simp [function.update_comm h₁.symm] } end theorem unrev_ok {q s} {S : K' β†’ list Ξ“'} : reaches₁ (TM2.step tr) ⟨some (unrev q), s, S⟩ ⟨some q, none, update (update S rev []) main (list.reverse_core (S rev) (S main))⟩ := move_ok dec_trivial $ split_at_pred_ff _ theorem moveβ‚‚_ok {p k₁ kβ‚‚ q s L₁ o Lβ‚‚} {S : K' β†’ list Ξ“'} (h₁ : k₁ β‰  rev ∧ kβ‚‚ β‰  rev ∧ k₁ β‰  kβ‚‚) (hβ‚‚ : S rev = []) (e : split_at_pred p (S k₁) = (L₁, o, Lβ‚‚)) : reaches₁ (TM2.step tr) ⟨some (moveβ‚‚ p k₁ kβ‚‚ q), s, S⟩ ⟨some q, none, update (update S k₁ (o.elim id list.cons Lβ‚‚)) kβ‚‚ (L₁ ++ S kβ‚‚)⟩ := begin refine (move_ok h₁.1 e).trans (trans_gen.head rfl _), cases o; simp only [option.elim, tr, id.def], { convert move_ok h₁.2.1.symm (split_at_pred_ff _) using 2, simp only [function.update_comm h₁.1, function.update_idem], rw show update S rev [] = S, by rw [← hβ‚‚, function.update_eq_self], simp only [function.update_noteq h₁.2.2.symm, function.update_noteq h₁.2.1, function.update_noteq h₁.1.symm, list.reverse_core_eq, hβ‚‚, function.update_same, list.append_nil, list.reverse_reverse] }, { convert move_ok h₁.2.1.symm (split_at_pred_ff _) using 2, simp only [hβ‚‚, function.update_comm h₁.1, list.reverse_core_eq, function.update_same, list.append_nil, function.update_idem], rw show update S rev [] = S, by rw [← hβ‚‚, function.update_eq_self], simp only [function.update_noteq h₁.1.symm, function.update_noteq h₁.2.2.symm, function.update_noteq h₁.2.1, function.update_same, list.reverse_reverse] }, end theorem clear_ok {p k q s L₁ o Lβ‚‚} {S : K' β†’ list Ξ“'} (e : split_at_pred p (S k) = (L₁, o, Lβ‚‚)) : reaches₁ (TM2.step tr) ⟨some (Ξ›'.clear p k q), s, S⟩ ⟨some q, o, update S k Lβ‚‚βŸ© := begin induction L₁ with a L₁ IH generalizing S s, { refine trans_gen.head' rfl _, simp, cases S k with a Sk, {cases e, refl}, simp [split_at_pred] at e ⊒, cases p a; simp at e ⊒, { revert e, rcases split_at_pred p Sk with ⟨_, _, _⟩, rintro ⟨⟩ }, { simp only [e] } }, { refine trans_gen.head rfl _, simp, cases e₁ : S k with a' Sk; rw [e₁, split_at_pred] at e, {cases e}, cases eβ‚‚ : p a'; simp only [eβ‚‚, cond] at e, swap, {cases e}, rcases e₃ : split_at_pred p Sk with ⟨_, _, _⟩, rw [e₃, split_at_pred] at e, cases e, simp [eβ‚‚], convert @IH (update S k Sk) _ _ using 2; simp [e₃] } end theorem copy_ok (q s a b c d) : reaches₁ (TM2.step tr) ⟨some (Ξ›'.copy q), s, K'.elim a b c d⟩ ⟨some q, none, K'.elim (list.reverse_core b a) [] c (list.reverse_core b d)⟩ := begin induction b with x b IH generalizing a d s, { refine trans_gen.single _, simp, refl }, refine trans_gen.head rfl _, simp, exact IH _ _ _, end theorem tr_pos_num_nat_end : βˆ€ n (x ∈ tr_pos_num n), nat_end x = ff | pos_num.one _ (or.inl rfl) := rfl | (pos_num.bit0 n) _ (or.inl rfl) := rfl | (pos_num.bit0 n) _ (or.inr h) := tr_pos_num_nat_end n _ h | (pos_num.bit1 n) _ (or.inl rfl) := rfl | (pos_num.bit1 n) _ (or.inr h) := tr_pos_num_nat_end n _ h theorem tr_num_nat_end : βˆ€ n (x ∈ tr_num n), nat_end x = ff | (num.pos n) x h := tr_pos_num_nat_end n x h theorem tr_nat_nat_end (n) : βˆ€ x ∈ tr_nat n, nat_end x = ff := tr_num_nat_end _ theorem tr_list_ne_Cons : βˆ€ l (x ∈ tr_list l), x β‰  Ξ“'.Cons | (a :: l) x h := begin simp [tr_list] at h, obtain h | rfl | h := h, { rintro rfl, cases tr_nat_nat_end _ _ h }, { rintro ⟨⟩ }, { exact tr_list_ne_Cons l _ h } end theorem head_main_ok {q s L} {c d : list Ξ“'} : reaches₁ (TM2.step tr) ⟨some (head main q), s, K'.elim (tr_list L) [] c d⟩ ⟨some q, none, K'.elim (tr_list [L.head]) [] c d⟩ := begin let o : option Ξ“' := list.cases_on L none (Ξ» _ _, some Ξ“'.cons), refine (move_ok dec_trivial (split_at_pred_eq _ _ (tr_nat L.head) o (tr_list L.tail) (tr_nat_nat_end _) _)).trans (trans_gen.head rfl (trans_gen.head rfl _)), { cases L; exact ⟨rfl, rfl⟩ }, simp [show o β‰  some Ξ“'.Cons, by cases L; rintro ⟨⟩], refine (clear_ok (split_at_pred_eq _ _ _ none [] _ ⟨rfl, rfl⟩)).trans _, { exact Ξ» x h, (to_bool_ff (tr_list_ne_Cons _ _ h)) }, convert unrev_ok, simp [list.reverse_core_eq], end theorem head_stack_ok {q s L₁ Lβ‚‚ L₃} : reaches₁ (TM2.step tr) ⟨some (head stack q), s, K'.elim (tr_list L₁) [] [] (tr_list Lβ‚‚ ++ Ξ“'.Cons :: L₃)⟩ ⟨some q, none, K'.elim (tr_list (Lβ‚‚.head :: L₁)) [] [] Lβ‚ƒβŸ© := begin cases Lβ‚‚ with a Lβ‚‚, { refine trans_gen.trans (move_ok dec_trivial (split_at_pred_eq _ _ [] (some Ξ“'.Cons) L₃ (by rintro _ ⟨⟩) ⟨rfl, rfl⟩)) (trans_gen.head rfl (trans_gen.head rfl _)), convert unrev_ok, simp, refl }, { refine trans_gen.trans (move_ok dec_trivial (split_at_pred_eq _ _ (tr_nat a) (some Ξ“'.cons) (tr_list Lβ‚‚ ++ Ξ“'.Cons :: L₃) (tr_nat_nat_end _) ⟨rfl, by simp⟩)) (trans_gen.head rfl (trans_gen.head rfl _)), simp, refine trans_gen.trans (clear_ok (split_at_pred_eq _ _ (tr_list Lβ‚‚) (some Ξ“'.Cons) L₃ (Ξ» x h, (to_bool_ff (tr_list_ne_Cons _ _ h))) ⟨rfl, by simp⟩)) _, convert unrev_ok, simp [list.reverse_core_eq] }, end theorem succ_ok {q s n} {c d : list Ξ“'} : reaches₁ (TM2.step tr) ⟨some (Ξ›'.succ q), s, K'.elim (tr_list [n]) [] c d⟩ ⟨some q, none, K'.elim (tr_list [n.succ]) [] c d⟩ := begin simp [tr_nat, num.add_one], cases (n:num), { refine trans_gen.head rfl _, simp, rw if_neg, swap, rintro ⟨⟩, rw if_pos, swap, refl, convert unrev_ok, simp, refl }, simp [num.succ, tr_num, num.succ'], suffices : βˆ€ l₁, βˆƒ l₁' lβ‚‚' s', list.reverse_core l₁ (tr_pos_num a.succ) = list.reverse_core l₁' lβ‚‚' ∧ reaches₁ (TM2.step tr) ⟨some q.succ, s, K'.elim (tr_pos_num a ++ [Ξ“'.cons]) l₁ c d⟩ ⟨some (unrev q), s', K'.elim (lβ‚‚' ++ [Ξ“'.cons]) l₁' c d⟩, { obtain ⟨l₁', lβ‚‚', s', e, h⟩ := this [], simp [list.reverse_core] at e, refine h.trans _, convert unrev_ok using 2, simp [e, list.reverse_core_eq] }, induction a with m IH m IH generalizing s; intro l₁, { refine βŸ¨Ξ“'.bit0 :: l₁, [Ξ“'.bit1], some Ξ“'.cons, rfl, trans_gen.head rfl (trans_gen.single _)⟩, simp [tr_pos_num] }, { obtain ⟨l₁', lβ‚‚', s', e, h⟩ := IH (Ξ“'.bit0 :: l₁), refine ⟨l₁', lβ‚‚', s', e, trans_gen.head _ h⟩, swap, simp [pos_num.succ, tr_pos_num] }, { refine ⟨l₁, _, some Ξ“'.bit0, rfl, trans_gen.single _⟩, simp, refl }, end theorem pred_ok (q₁ qβ‚‚ s v) (c d : list Ξ“') : βˆƒ s', reaches₁ (TM2.step tr) ⟨some (Ξ›'.pred q₁ qβ‚‚), s, K'.elim (tr_list v) [] c d⟩ (v.head.elim ⟨some q₁, s', K'.elim (tr_list v.tail) [] c d⟩ (Ξ» n _, ⟨some qβ‚‚, s', K'.elim (tr_list (n :: v.tail)) [] c d⟩)) := begin rcases v with _|⟨_|n, v⟩, { refine ⟨none, trans_gen.single _⟩, simp, refl }, { refine ⟨some Ξ“'.cons, trans_gen.single _⟩, simp, refl }, refine ⟨none, _⟩, simp [tr_nat, num.add_one, num.succ, tr_num], cases (n:num), { simp [tr_pos_num, tr_num, show num.zero.succ' = pos_num.one, from rfl], refine trans_gen.head rfl _, convert unrev_ok, simp, refl }, simp [tr_num, num.succ'], suffices : βˆ€ l₁, βˆƒ l₁' lβ‚‚' s', list.reverse_core l₁ (tr_pos_num a) = list.reverse_core l₁' lβ‚‚' ∧ reaches₁ (TM2.step tr) ⟨some (q₁.pred qβ‚‚), s, K'.elim (tr_pos_num a.succ ++ Ξ“'.cons :: tr_list v) l₁ c d⟩ ⟨some (unrev qβ‚‚), s', K'.elim (lβ‚‚' ++ Ξ“'.cons :: tr_list v) l₁' c d⟩, { obtain ⟨l₁', lβ‚‚', s', e, h⟩ := this [], simp [list.reverse_core] at e, refine h.trans _, convert unrev_ok using 2, simp [e, list.reverse_core_eq] }, induction a with m IH m IH generalizing s; intro l₁, { refine βŸ¨Ξ“'.bit1 :: l₁, [], some Ξ“'.cons, rfl, trans_gen.head rfl (trans_gen.single _)⟩, simp [tr_pos_num, show pos_num.one.succ = pos_num.one.bit0, from rfl], refl }, { obtain ⟨l₁', lβ‚‚', s', e, h⟩ := IH (some Ξ“'.bit0) (Ξ“'.bit1 :: l₁), refine ⟨l₁', lβ‚‚', s', e, trans_gen.head _ h⟩, simp, refl }, { obtain ⟨a, l, e, h⟩ : βˆƒ a l, tr_pos_num m = a :: l ∧ nat_end a = ff, { cases m; refine ⟨_, _, rfl, rfl⟩ }, refine βŸ¨Ξ“'.bit0 :: l₁, _, some a, rfl, trans_gen.single _⟩, simp [tr_pos_num, pos_num.succ, e, h, nat_end, show some Ξ“'.bit1 β‰  some Ξ“'.bit0, from dec_trivial] }, end theorem tr_normal_respects (c k v s) : βˆƒ bβ‚‚, tr_cfg (step_normal c k v) bβ‚‚ ∧ reaches₁ (TM2.step tr) ⟨some (tr_normal c (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ bβ‚‚ := begin induction c generalizing k v s, case zero' : { refine ⟨_, ⟨s, rfl⟩, trans_gen.single _⟩, simp }, case succ : { refine ⟨_, ⟨none, rfl⟩, head_main_ok.trans succ_ok⟩ }, case tail : { let o : option Ξ“' := list.cases_on v none (Ξ» _ _, some Ξ“'.cons), refine ⟨_, ⟨o, rfl⟩, _⟩, convert clear_ok _, simp, swap, refine split_at_pred_eq _ _ (tr_nat v.head) _ _ (tr_nat_nat_end _) _, cases v; exact ⟨rfl, rfl⟩ }, case cons : f fs IHf IHfs { obtain ⟨c, h₁, hβ‚‚βŸ© := IHf (cont.cons₁ fs v k) v none, refine ⟨c, h₁, trans_gen.head rfl $ (move_ok dec_trivial (split_at_pred_ff _)).trans _⟩, simp [step_normal], refine (copy_ok _ none [] (tr_list v).reverse _ _).trans _, convert hβ‚‚ using 2, simp [list.reverse_core_eq, tr_cont_stack] }, case comp : f g IHf IHg { exact IHg (cont.comp f k) v s }, case case : f g IHf IHg { rw step_normal, obtain ⟨s', h⟩ := pred_ok _ _ s v _ _, cases v.head with n, { obtain ⟨c, h₁, hβ‚‚βŸ© := IHf k _ s', exact ⟨_, h₁, h.trans hβ‚‚βŸ© }, { obtain ⟨c, h₁, hβ‚‚βŸ© := IHg k _ s', exact ⟨_, h₁, h.trans hβ‚‚βŸ© } }, case fix : f IH { apply IH } end theorem tr_ret_respects (k v s) : βˆƒ bβ‚‚, tr_cfg (step_ret k v) bβ‚‚ ∧ reaches₁ (TM2.step tr) ⟨some (Ξ›'.ret (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ bβ‚‚ := begin induction k generalizing v s, case halt : { exact ⟨_, rfl, trans_gen.single rfl⟩ }, case cons₁ : fs as k IH { obtain ⟨s', h₁, hβ‚‚βŸ© := tr_normal_respects fs (cont.consβ‚‚ v k) as none, refine ⟨s', h₁, trans_gen.head rfl _⟩, simp, refine (moveβ‚‚_ok dec_trivial _ (split_at_pred_ff _)).trans _, {refl}, simp, refine (moveβ‚‚_ok dec_trivial _ _).trans _, swap 4, {refl}, swap 4, {exact (split_at_pred_eq _ _ _ (some Ξ“'.Cons) _ (Ξ» x h, to_bool_ff (tr_list_ne_Cons _ _ h)) ⟨rfl, rfl⟩)}, refine (moveβ‚‚_ok dec_trivial _ (split_at_pred_ff _)).trans _, {refl}, simp, exact hβ‚‚ }, case consβ‚‚ : ns k IH { obtain ⟨c, h₁, hβ‚‚βŸ© := IH (ns.head :: v) none, exact ⟨c, h₁, trans_gen.head rfl $ head_stack_ok.trans hβ‚‚βŸ© }, case comp : f k IH { obtain ⟨s', h₁, hβ‚‚βŸ© := tr_normal_respects f k v s, exact ⟨_, h₁, trans_gen.head rfl hβ‚‚βŸ© }, case fix : f k IH { rw [step_ret], have : if v.head = 0 then nat_end (tr_list v).head'.iget = tt ∧ (tr_list v).tail = tr_list v.tail else nat_end (tr_list v).head'.iget = ff ∧ (tr_list v).tail = (tr_nat v.head).tail ++ Ξ“'.cons :: tr_list v.tail, { cases v with n, {exact ⟨rfl, rfl⟩}, cases n, {exact ⟨rfl, rfl⟩}, rw [tr_list, list.head, tr_nat, nat.cast_succ, num.add_one, num.succ, list.tail], cases (n:num).succ'; exact ⟨rfl, rfl⟩ }, by_cases v.head = 0; simp [h] at this ⊒, { obtain ⟨c, h₁, hβ‚‚βŸ© := IH v.tail (tr_list v).head', refine ⟨c, h₁, trans_gen.head rfl _⟩, simp [tr_cont, tr_cont_stack, this], exact hβ‚‚ }, { obtain ⟨s', h₁, hβ‚‚βŸ© := tr_normal_respects f (cont.fix f k) v.tail (some Ξ“'.cons), refine ⟨_, h₁, trans_gen.head rfl $ trans_gen.trans _ hβ‚‚βŸ©, swap 3, simp [tr_cont, this.1], convert clear_ok (split_at_pred_eq _ _ (tr_nat v.head).tail (some Ξ“'.cons) _ _ _) using 2, { simp }, { exact Ξ» x h, tr_nat_nat_end _ _ (list.tail_subset _ h) }, { exact ⟨rfl, this.2⟩ } } }, end theorem tr_respects : respects step (TM2.step tr) tr_cfg | (cfg.ret k v) _ ⟨s, rfl⟩ := tr_ret_respects _ _ _ | (cfg.halt v) _ rfl := rfl /-- The initial state, evaluating function `c` on input `v`. -/ def init (c : code) (v : list β„•) : cfg' := ⟨some (tr_normal c cont'.halt), none, K'.elim (tr_list v) [] [] []⟩ theorem tr_init (c v) : βˆƒ b, tr_cfg (step_normal c cont.halt v) b ∧ reaches₁ (TM2.step tr) (init c v) b := tr_normal_respects _ _ _ _ theorem tr_eval (c v) : eval (TM2.step tr) (init c v) = halt <$> code.eval c v := begin obtain ⟨i, h₁, hβ‚‚βŸ© := tr_init c v, refine roption.ext (Ξ» x, _), rw [reaches_eval hβ‚‚.to_refl], simp, refine ⟨λ h, _, _⟩, { obtain ⟨c, hc₁, hcβ‚‚βŸ© := tr_eval_rev tr_respects h₁ h, simp [step_normal_eval] at hcβ‚‚, obtain ⟨v', hv, rfl⟩ := hcβ‚‚, exact ⟨_, hv, hc₁.symm⟩ }, { rintro ⟨v', hv, rfl⟩, have := tr_eval tr_respects h₁, simp [step_normal_eval] at this, obtain ⟨_, ⟨⟩, h⟩ := this _ hv rfl, exact h } end end end partrec_to_TM2 end turing
266accc46d45213f151fe047895c4c8351f6e70b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/field_theory/separable_auto.lean
b231d62fca2e061139aedf23c2bf5561a2c3774a
[]
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
17,294
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.polynomial.big_operators import Mathlib.field_theory.minpoly import Mathlib.field_theory.splitting_field import Mathlib.field_theory.tower import Mathlib.algebra.squarefree import Mathlib.PostPort universes u u_1 v u_2 u_3 namespace Mathlib /-! # Separable polynomials We define a polynomial to be separable if it is coprime with its derivative. We prove basic properties about separable polynomials here. ## Main definitions * `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative. * `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a commutative semiring `R` by a factor of p, so `expand R p (βˆ‘ aβ‚™ xⁿ)` is `βˆ‘ aβ‚™ xⁿᡖ`. * `polynomial.contract p f`: the opposite of `expand`, so it sends `βˆ‘ aβ‚™ xⁿᡖ` to `βˆ‘ aβ‚™ xⁿ`. -/ namespace polynomial /-- A polynomial is separable iff it is coprime with its derivative. -/ def separable {R : Type u} [comm_semiring R] (f : polynomial R) := is_coprime f (coe_fn derivative f) theorem separable_def {R : Type u} [comm_semiring R] (f : polynomial R) : separable f ↔ is_coprime f (coe_fn derivative f) := iff.rfl theorem separable_def' {R : Type u} [comm_semiring R] (f : polynomial R) : separable f ↔ βˆƒ (a : polynomial R), βˆƒ (b : polynomial R), a * f + b * coe_fn derivative f = 1 := iff.rfl theorem separable_one {R : Type u} [comm_semiring R] : separable 1 := is_coprime_one_left theorem separable_X_add_C {R : Type u} [comm_semiring R] (a : R) : separable (X + coe_fn C a) := sorry theorem separable_X {R : Type u} [comm_semiring R] : separable X := eq.mpr (id (Eq._oldrec (Eq.refl (separable X)) (propext (separable_def X)))) (eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime X (coe_fn derivative X))) derivative_X)) is_coprime_one_right) theorem separable_C {R : Type u} [comm_semiring R] (r : R) : separable (coe_fn C r) ↔ is_unit r := sorry theorem separable.of_mul_left {R : Type u} [comm_semiring R] {f : polynomial R} {g : polynomial R} (h : separable (f * g)) : separable f := sorry theorem separable.of_mul_right {R : Type u} [comm_semiring R] {f : polynomial R} {g : polynomial R} (h : separable (f * g)) : separable g := separable.of_mul_left (eq.mp (Eq._oldrec (Eq.refl (separable (f * g))) (mul_comm f g)) h) theorem separable.of_dvd {R : Type u} [comm_semiring R] {f : polynomial R} {g : polynomial R} (hf : separable f) (hfg : g ∣ f) : separable g := Exists.dcases_on hfg fun (f' : polynomial R) (hfg_h : f = g * f') => Eq._oldrec (fun (hf : separable (g * f')) => separable.of_mul_left hf) (Eq.symm hfg_h) hf theorem separable_gcd_left {F : Type u_1} [field F] {f : polynomial F} (hf : separable f) (g : polynomial F) : separable (euclidean_domain.gcd f g) := separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g) theorem separable_gcd_right {F : Type u_1} [field F] {g : polynomial F} (f : polynomial F) (hg : separable g) : separable (euclidean_domain.gcd f g) := separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g) theorem separable.is_coprime {R : Type u} [comm_semiring R] {f : polynomial R} {g : polynomial R} (h : separable (f * g)) : is_coprime f g := sorry theorem separable.of_pow' {R : Type u} [comm_semiring R] {f : polynomial R} {n : β„•} (h : separable (f ^ n)) : is_unit f ∨ separable f ∧ n = 1 ∨ n = 0 := sorry theorem separable.of_pow {R : Type u} [comm_semiring R] {f : polynomial R} (hf : Β¬is_unit f) {n : β„•} (hn : n β‰  0) (hfs : separable (f ^ n)) : separable f ∧ n = 1 := or.resolve_right (or.resolve_left (separable.of_pow' hfs) hf) hn theorem separable.map {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] {p : polynomial R} (h : separable p) {f : R β†’+* S} : separable (map f p) := sorry /-- Expand the polynomial by a factor of p, so `βˆ‘ aβ‚™ xⁿ` becomes `βˆ‘ aβ‚™ xⁿᡖ`. -/ def expand (R : Type u) [comm_semiring R] (p : β„•) : alg_hom R (polynomial R) (polynomial R) := alg_hom.mk (ring_hom.to_fun (evalβ‚‚_ring_hom C (X ^ p))) sorry sorry sorry sorry sorry theorem coe_expand (R : Type u) [comm_semiring R] (p : β„•) : ⇑(expand R p) = evalβ‚‚ C (X ^ p) := rfl theorem expand_eq_sum {R : Type u} [comm_semiring R] (p : β„•) {f : polynomial R} : coe_fn (expand R p) f = finsupp.sum f fun (e : β„•) (a : R) => coe_fn C a * (X ^ p) ^ e := id (Eq.refl (finsupp.sum f fun (e : β„•) (a : R) => coe_fn C a * (X ^ p) ^ e)) @[simp] theorem expand_C {R : Type u} [comm_semiring R] (p : β„•) (r : R) : coe_fn (expand R p) (coe_fn C r) = coe_fn C r := evalβ‚‚_C C (X ^ p) @[simp] theorem expand_X {R : Type u} [comm_semiring R] (p : β„•) : coe_fn (expand R p) X = X ^ p := evalβ‚‚_X C (X ^ p) @[simp] theorem expand_monomial {R : Type u} [comm_semiring R] (p : β„•) (q : β„•) (r : R) : coe_fn (expand R p) (coe_fn (monomial q) r) = coe_fn (monomial (q * p)) r := sorry theorem expand_expand {R : Type u} [comm_semiring R] (p : β„•) (q : β„•) (f : polynomial R) : coe_fn (expand R p) (coe_fn (expand R q) f) = coe_fn (expand R (p * q)) f := sorry theorem expand_mul {R : Type u} [comm_semiring R] (p : β„•) (q : β„•) (f : polynomial R) : coe_fn (expand R (p * q)) f = coe_fn (expand R p) (coe_fn (expand R q) f) := Eq.symm (expand_expand p q f) @[simp] theorem expand_one {R : Type u} [comm_semiring R] (f : polynomial R) : coe_fn (expand R 1) f = f := sorry theorem expand_pow {R : Type u} [comm_semiring R] (p : β„•) (q : β„•) (f : polynomial R) : coe_fn (expand R (p ^ q)) f = nat.iterate (⇑(expand R p)) q f := sorry theorem derivative_expand {R : Type u} [comm_semiring R] (p : β„•) (f : polynomial R) : coe_fn derivative (coe_fn (expand R p) f) = coe_fn (expand R p) (coe_fn derivative f) * (↑p * X ^ (p - 1)) := sorry theorem coeff_expand {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) (f : polynomial R) (n : β„•) : coeff (coe_fn (expand R p) f) n = ite (p ∣ n) (coeff f (n / p)) 0 := sorry @[simp] theorem coeff_expand_mul {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) (f : polynomial R) (n : β„•) : coeff (coe_fn (expand R p) f) (n * p) = coeff f n := sorry @[simp] theorem coeff_expand_mul' {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) (f : polynomial R) (n : β„•) : coeff (coe_fn (expand R p) f) (p * n) = coeff f n := eq.mpr (id (Eq._oldrec (Eq.refl (coeff (coe_fn (expand R p) f) (p * n) = coeff f n)) (mul_comm p n))) (eq.mpr (id (Eq._oldrec (Eq.refl (coeff (coe_fn (expand R p) f) (n * p) = coeff f n)) (coeff_expand_mul hp f n))) (Eq.refl (coeff f n))) theorem expand_eq_map_domain {R : Type u} [comm_semiring R] (p : β„•) (f : polynomial R) : coe_fn (expand R p) f = finsupp.map_domain (fun (_x : β„•) => _x * p) f := sorry theorem expand_inj {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) {f : polynomial R} {g : polynomial R} : coe_fn (expand R p) f = coe_fn (expand R p) g ↔ f = g := sorry theorem expand_eq_zero {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) {f : polynomial R} : coe_fn (expand R p) f = 0 ↔ f = 0 := sorry theorem expand_eq_C {R : Type u} [comm_semiring R] {p : β„•} (hp : 0 < p) {f : polynomial R} {r : R} : coe_fn (expand R p) f = coe_fn C r ↔ f = coe_fn C r := sorry theorem nat_degree_expand {R : Type u} [comm_semiring R] (p : β„•) (f : polynomial R) : nat_degree (coe_fn (expand R p) f) = nat_degree f * p := sorry theorem map_expand {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] {p : β„•} (hp : 0 < p) {f : R β†’+* S} {q : polynomial R} : map f (coe_fn (expand R p) q) = coe_fn (expand S p) (map f q) := sorry theorem separable_X_sub_C {R : Type u} [comm_ring R] {x : R} : separable (X - coe_fn C x) := sorry theorem separable.mul {R : Type u} [comm_ring R] {f : polynomial R} {g : polynomial R} (hf : separable f) (hg : separable g) (h : is_coprime f g) : separable (f * g) := sorry theorem separable_prod' {R : Type u} [comm_ring R] {ΞΉ : Type u_1} {f : ΞΉ β†’ polynomial R} {s : finset ΞΉ} : (βˆ€ (x : ΞΉ), x ∈ s β†’ βˆ€ (y : ΞΉ), y ∈ s β†’ x β‰  y β†’ is_coprime (f x) (f y)) β†’ (βˆ€ (x : ΞΉ), x ∈ s β†’ separable (f x)) β†’ separable (finset.prod s fun (x : ΞΉ) => f x) := sorry theorem separable_prod {R : Type u} [comm_ring R] {ΞΉ : Type u_1} [fintype ΞΉ] {f : ΞΉ β†’ polynomial R} (h1 : pairwise (is_coprime on f)) (h2 : βˆ€ (x : ΞΉ), separable (f x)) : separable (finset.prod finset.univ fun (x : ΞΉ) => f x) := separable_prod' (fun (x : ΞΉ) (hx : x ∈ finset.univ) (y : ΞΉ) (hy : y ∈ finset.univ) (hxy : x β‰  y) => h1 x y hxy) fun (x : ΞΉ) (hx : x ∈ finset.univ) => h2 x theorem separable.inj_of_prod_X_sub_C {R : Type u} [comm_ring R] [nontrivial R] {ΞΉ : Type u_1} {f : ΞΉ β†’ R} {s : finset ΞΉ} (hfs : separable (finset.prod s fun (i : ΞΉ) => X - coe_fn C (f i))) {x : ΞΉ} {y : ΞΉ} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y := sorry theorem separable.injective_of_prod_X_sub_C {R : Type u} [comm_ring R] [nontrivial R] {ΞΉ : Type u_1} [fintype ΞΉ] {f : ΞΉ β†’ R} (hfs : separable (finset.prod finset.univ fun (i : ΞΉ) => X - coe_fn C (f i))) : function.injective f := fun (x y : ΞΉ) (hfxy : f x = f y) => separable.inj_of_prod_X_sub_C hfs (finset.mem_univ x) (finset.mem_univ y) hfxy theorem is_unit_of_self_mul_dvd_separable {R : Type u} [comm_ring R] {p : polynomial R} {q : polynomial R} (hp : separable p) (hq : q * q ∣ p) : is_unit q := sorry theorem is_local_ring_hom_expand (R : Type u) [integral_domain R] {p : β„•} (hp : 0 < p) : is_local_ring_hom ↑(expand R p) := sorry theorem separable_iff_derivative_ne_zero {F : Type u} [field F] {f : polynomial F} (hf : irreducible f) : separable f ↔ coe_fn derivative f β‰  0 := sorry theorem separable_map {F : Type u} [field F] {K : Type v} [field K] (f : F β†’+* K) {p : polynomial F} : separable (map f p) ↔ separable p := sorry /-- The opposite of `expand`: sends `βˆ‘ aβ‚™ xⁿᡖ` to `βˆ‘ aβ‚™ xⁿ`. -/ def contract {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] (f : polynomial F) : polynomial F := finsupp.mk (finset.preimage (finsupp.support f) (fun (_x : β„•) => _x * p) sorry) (fun (n : β„•) => coeff f (n * p)) sorry theorem coeff_contract {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] (f : polynomial F) (n : β„•) : coeff (contract p f) n = coeff f (n * p) := rfl theorem of_irreducible_expand {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] {f : polynomial F} (hf : irreducible (coe_fn (expand F p) f)) : irreducible f := of_irreducible_map (↑(expand F p)) hf theorem of_irreducible_expand_pow {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] {f : polynomial F} {n : β„•} : irreducible (coe_fn (expand F (p ^ n)) f) β†’ irreducible f := sorry theorem expand_char {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] (f : polynomial F) : map (frobenius F p) (coe_fn (expand F p) f) = f ^ p := sorry theorem map_expand_pow_char {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] (f : polynomial F) (n : β„•) : map (frobenius F p ^ n) (coe_fn (expand F (p ^ n)) f) = f ^ p ^ n := sorry theorem expand_contract {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] {f : polynomial F} (hf : coe_fn derivative f = 0) : coe_fn (expand F p) (contract p f) = f := sorry theorem separable_or {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] {f : polynomial F} (hf : irreducible f) : separable f ∨ Β¬separable f ∧ βˆƒ (g : polynomial F), irreducible g ∧ coe_fn (expand F p) g = f := sorry theorem exists_separable_of_irreducible {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] {f : polynomial F} (hf : irreducible f) (hf0 : f β‰  0) : βˆƒ (n : β„•), βˆƒ (g : polynomial F), separable g ∧ coe_fn (expand F (p ^ n)) g = f := sorry theorem is_unit_or_eq_zero_of_separable_expand {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] {f : polynomial F} (n : β„•) (hf : separable (coe_fn (expand F (p ^ n)) f)) : is_unit f ∨ n = 0 := sorry theorem unique_separable_of_irreducible {F : Type u} [field F] (p : β„•) [hp : fact (nat.prime p)] [HF : char_p F p] {f : polynomial F} (hf : irreducible f) (hf0 : f β‰  0) (n₁ : β„•) (g₁ : polynomial F) (hg₁ : separable g₁) (hgf₁ : coe_fn (expand F (p ^ n₁)) g₁ = f) (nβ‚‚ : β„•) (gβ‚‚ : polynomial F) (hgβ‚‚ : separable gβ‚‚) (hgfβ‚‚ : coe_fn (expand F (p ^ nβ‚‚)) gβ‚‚ = f) : n₁ = nβ‚‚ ∧ g₁ = gβ‚‚ := sorry theorem separable_prod_X_sub_C_iff' {F : Type u} [field F] {ΞΉ : Type u_1} {f : ΞΉ β†’ F} {s : finset ΞΉ} : separable (finset.prod s fun (i : ΞΉ) => X - coe_fn C (f i)) ↔ βˆ€ (x : ΞΉ), x ∈ s β†’ βˆ€ (y : ΞΉ), y ∈ s β†’ f x = f y β†’ x = y := sorry theorem separable_prod_X_sub_C_iff {F : Type u} [field F] {ΞΉ : Type u_1} [fintype ΞΉ] {f : ΞΉ β†’ F} : separable (finset.prod finset.univ fun (i : ΞΉ) => X - coe_fn C (f i)) ↔ function.injective f := sorry theorem not_unit_X_sub_C {F : Type u} [field F] (a : F) : Β¬is_unit (X - coe_fn C a) := sorry theorem nodup_of_separable_prod {F : Type u} [field F] {s : multiset F} (hs : separable (multiset.prod (multiset.map (fun (a : F) => X - coe_fn C a) s))) : multiset.nodup s := sorry theorem multiplicity_le_one_of_separable {F : Type u} [field F] {p : polynomial F} {q : polynomial F} (hq : Β¬is_unit q) (hsep : separable p) : multiplicity q p ≀ 1 := sorry theorem separable.squarefree {F : Type u} [field F] {p : polynomial F} (hsep : separable p) : squarefree p := sorry /--If `n β‰  0` in `F`, then ` X ^ n - a` is separable for any `a β‰  0`. -/ theorem separable_X_pow_sub_C {F : Type u} [field F] {n : β„•} (a : F) (hn : ↑n β‰  0) (ha : a β‰  0) : separable (X ^ n - coe_fn C a) := sorry /--If `n β‰  0` in `F`, then ` X ^ n - a` is squarefree for any `a β‰  0`. -/ theorem squarefree_X_pow_sub_C {F : Type u} [field F] {n : β„•} (a : F) (hn : ↑n β‰  0) (ha : a β‰  0) : squarefree (X ^ n - coe_fn C a) := separable.squarefree (separable_X_pow_sub_C a hn ha) theorem root_multiplicity_le_one_of_separable {F : Type u} [field F] {p : polynomial F} (hp : p β‰  0) (hsep : separable p) (x : F) : root_multiplicity x p ≀ 1 := sorry theorem count_roots_le_one {F : Type u} [field F] {p : polynomial F} (hsep : separable p) (x : F) : multiset.count x (roots p) ≀ 1 := sorry theorem nodup_roots {F : Type u} [field F] {p : polynomial F} (hsep : separable p) : multiset.nodup (roots p) := iff.mpr multiset.nodup_iff_count_le_one (count_roots_le_one hsep) theorem eq_X_sub_C_of_separable_of_root_eq {F : Type u} [field F] {K : Type v} [field K] {i : F β†’+* K} {x : F} {h : polynomial F} (h_ne_zero : h β‰  0) (h_sep : separable h) (h_root : eval x h = 0) (h_splits : splits i h) (h_roots : βˆ€ (y : K), y ∈ roots (map i h) β†’ y = coe_fn i x) : h = coe_fn C (leading_coeff h) * (X - coe_fn C x) := sorry end polynomial theorem irreducible.separable {F : Type u} [field F] [char_zero F] {f : polynomial F} (hf : irreducible f) : polynomial.separable f := sorry -- TODO: refactor to allow transcendental extensions? -- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions /-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff the minimal polynomial of every `x : K` is separable. -/ def is_separable (F : Type u_1) (K : Type u_2) [field F] [field K] [algebra F K] := βˆ€ (x : K), is_integral F x ∧ polynomial.separable (minpoly F x) protected instance is_separable_self (F : Type u_1) [field F] : is_separable F F := fun (x : F) => { left := is_integral_algebra_map, right := eq.mpr (id (Eq._oldrec (Eq.refl (polynomial.separable (minpoly F x))) (minpoly.eq_X_sub_C' x))) polynomial.separable_X_sub_C } theorem is_separable_tower_top_of_is_separable (F : Type u_1) (K : Type u_2) (E : Type u_3) [field F] [field K] [field E] [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] [h : is_separable F E] : is_separable K E := sorry theorem is_separable_tower_bot_of_is_separable (F : Type u_1) (K : Type u_2) (E : Type u_3) [field F] [field K] [field E] [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] [h : is_separable F E] : is_separable F K := sorry theorem is_separable.of_alg_hom (F : Type u_1) {E : Type u_3} [field F] [field E] [algebra F E] (E' : Type u_2) [field E'] [algebra F E'] (f : alg_hom F E E') [is_separable F E'] : is_separable F E := let _inst : algebra E E' := ring_hom.to_algebra (alg_hom.to_ring_hom f); is_separable_tower_bot_of_is_separable F E E' end Mathlib
58d4e07b12b308bf6f1e34eebbbae95047936197
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/category/NonemptyFinLinOrd.lean
a9187c8796b332ec3728bb75171f053eda58bf95
[ "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
2,298
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.fin.basic import data.fintype.sort import order.category.LinearOrder /-! # Nonempty finite linear orders Nonempty finite linear orders form the index category for simplicial objects. -/ universes u v open category_theory /-- A typeclass for nonempty finite linear orders. -/ class nonempty_fin_lin_ord (Ξ± : Type*) extends fintype Ξ±, linear_order Ξ± := (nonempty : nonempty Ξ± . tactic.apply_instance) attribute [instance] nonempty_fin_lin_ord.nonempty @[priority 100] instance nonempty_fin_lin_ord.order_bot (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : order_bot Ξ± := { bot := finset.min' finset.univ ⟨classical.arbitrary Ξ±, by simp⟩, bot_le := Ξ» a, finset.min'_le _ a (by simp) } @[priority 100] instance nonempty_fin_lin_ord.order_top (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : order_top Ξ± := { top := finset.max' finset.univ ⟨classical.arbitrary Ξ±, by simp⟩, le_top := Ξ» a, finset.le_max' _ a (by simp) } instance punit.nonempty_fin_lin_ord : nonempty_fin_lin_ord punit := { .. punit.linear_ordered_cancel_add_comm_monoid, .. punit.fintype } instance fin.nonempty_fin_lin_ord (n : β„•) : nonempty_fin_lin_ord (fin (n+1)) := { .. fin.fintype _, .. fin.linear_order } instance ulift.nonempty_fin_lin_ord (Ξ± : Type u) [nonempty_fin_lin_ord Ξ±] : nonempty_fin_lin_ord (ulift.{v} Ξ±) := { nonempty := ⟨ulift.up βŠ₯⟩, .. linear_order.lift equiv.ulift (equiv.injective _), .. ulift.fintype _ } /-- The category of nonempty finite linear orders. -/ def NonemptyFinLinOrd := bundled nonempty_fin_lin_ord namespace NonemptyFinLinOrd instance : bundled_hom.parent_projection @nonempty_fin_lin_ord.to_linear_order := ⟨⟩ attribute [derive [large_category, concrete_category]] NonemptyFinLinOrd instance : has_coe_to_sort NonemptyFinLinOrd Type* := bundled.has_coe_to_sort /-- Construct a bundled NonemptyFinLinOrd from the underlying type and typeclass. -/ def of (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : NonemptyFinLinOrd := bundled.of Ξ± instance : inhabited NonemptyFinLinOrd := ⟨of punit⟩ instance (Ξ± : NonemptyFinLinOrd) : nonempty_fin_lin_ord Ξ± := Ξ±.str end NonemptyFinLinOrd
062297247b9963a9fcb37e4c34c899b4d67d27d1
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/data/padics/padic_numbers.lean
8be6f79ecb63d27bf1922675e397ff7c7e7ca842
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
32,563
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.real.cau_seq_completion import data.padics.padic_norm algebra.archimedean analysis.normed_space.basic import tactic.norm_cast /-! # p-adic numbers This file defines the p-adic numbers (rationals) β„š_p as the completion of β„š with respect to the p-adic norm. We show that the p-adic norm on β„š extends to β„š_p, that β„š is embedded in β„š_p, and that β„š_p is Cauchy complete. ## Important definitions * `padic` : the type of p-adic numbers * `padic_norm_e` : the rational valued p-adic norm on β„š_p ## Notation We introduce the notation β„š_[p] for the p-adic numbers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking (prime p) as a type class argument. We use the same concrete Cauchy sequence construction that is used to construct ℝ. β„š_p inherits a field structure from this construction. The extension of the norm on β„š to β„š_p is *not* analogous to extending the absolute value to ℝ, and hence the proof that β„š_p is complete is different from the proof that ℝ is complete. A small special-purpose simplification tactic, `padic_index_simp`, is used to manipulate sequence indices in the proof that the norm extends. `padic_norm_e` is the rational-valued p-adic norm on β„š_p. To instantiate β„š_p as a normed field, we must cast this into a ℝ-valued norm. The ℝ-valued norm, using notation βˆ₯ βˆ₯ from normed spaces, is the canonical representation of this norm. Coercions from β„š to β„š_p are set up to work with the `norm_cast` tactic. ## References * [F. Q. GouΓͺva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation, cauchy, completion, p-adic completion -/ noncomputable theory open_locale classical open nat multiplicity padic_norm cau_seq cau_seq.completion metric /-- The type of Cauchy sequences of rationals with respect to the p-adic norm. -/ @[reducible] def padic_seq (p : β„•) [p.prime] := cau_seq _ (padic_norm p) namespace padic_seq section variables {p : β„•} [nat.prime p] /-- The p-adic norm of the entries of a nonzero Cauchy sequence of rationals is eventually constant. -/ lemma stationary {f : cau_seq β„š (padic_norm p)} (hf : Β¬ f β‰ˆ 0) : βˆƒ N, βˆ€ m n, N ≀ m β†’ N ≀ n β†’ padic_norm p (f n) = padic_norm p (f m) := have βˆƒ Ξ΅ > 0, βˆƒ N1, βˆ€ j β‰₯ N1, Ξ΅ ≀ padic_norm p (f j), from cau_seq.abv_pos_of_not_lim_zero $ not_lim_zero_of_not_congr_zero hf, let ⟨Ρ, hΞ΅, N1, hN1⟩ := this, ⟨N2, hN2⟩ := cau_seq.cauchyβ‚‚ f hΞ΅ in ⟨ max N1 N2, Ξ» n m hn hm, have padic_norm p (f n - f m) < Ξ΅, from hN2 _ _ (max_le_iff.1 hn).2 (max_le_iff.1 hm).2, have padic_norm p (f n - f m) < padic_norm p (f n), from lt_of_lt_of_le this $ hN1 _ (max_le_iff.1 hn).1, have padic_norm p (f n - f m) < max (padic_norm p (f n)) (padic_norm p (f m)), from lt_max_iff.2 (or.inl this), begin by_contradiction hne, rw ←padic_norm.neg p (f m) at hne, have hnam := add_eq_max_of_ne p hne, rw [padic_norm.neg, max_comm] at hnam, rw [←hnam, sub_eq_add_neg, add_comm] at this, apply _root_.lt_irrefl _ this end ⟩ /-- For all n β‰₯ stationary_point f hf, the p-adic norm of f n is the same. -/ def stationary_point {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) : β„• := classical.some $ stationary hf lemma stationary_point_spec {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) : βˆ€ {m n}, m β‰₯ stationary_point hf β†’ n β‰₯ stationary_point hf β†’ padic_norm p (f n) = padic_norm p (f m) := classical.some_spec $ stationary hf /-- Since the norm of the entries of a Cauchy sequence is eventually stationary, we can lift the norm to sequences. -/ def norm (f : padic_seq p) : β„š := if hf : f β‰ˆ 0 then 0 else padic_norm p (f (stationary_point hf)) lemma norm_zero_iff (f : padic_seq p) : f.norm = 0 ↔ f β‰ˆ 0 := begin constructor, { intro h, by_contradiction hf, unfold norm at h, split_ifs at h, apply hf, intros Ξ΅ hΞ΅, existsi stationary_point hf, intros j hj, have heq := stationary_point_spec hf (le_refl _) hj, simpa [h, heq] }, { intro h, simp [norm, h] } end end section embedding open cau_seq variables {p : β„•} [nat.prime p] lemma equiv_zero_of_val_eq_of_equiv_zero {f g : padic_seq p} (h : βˆ€ k, padic_norm p (f k) = padic_norm p (g k)) (hf : f β‰ˆ 0) : g β‰ˆ 0 := Ξ» Ξ΅ hΞ΅, let ⟨i, hi⟩ := hf _ hΞ΅ in ⟨i, Ξ» j hj, by simpa [h] using hi _ hj⟩ lemma norm_nonzero_of_not_equiv_zero {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) : f.norm β‰  0 := hf ∘ f.norm_zero_iff.1 lemma norm_eq_norm_app_of_nonzero {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) : βˆƒ k, f.norm = padic_norm p k ∧ k β‰  0 := have heq : f.norm = padic_norm p (f $ stationary_point hf), by simp [norm, hf], ⟨f $ stationary_point hf, heq, Ξ» h, norm_nonzero_of_not_equiv_zero hf (by simpa [h] using heq)⟩ lemma not_lim_zero_const_of_nonzero {q : β„š} (hq : q β‰  0) : Β¬ lim_zero (const (padic_norm p) q) := Ξ» h', hq $ const_lim_zero.1 h' lemma not_equiv_zero_const_of_nonzero {q : β„š} (hq : q β‰  0) : Β¬ (const (padic_norm p) q) β‰ˆ 0 := Ξ» h : lim_zero (const (padic_norm p) q - 0), not_lim_zero_const_of_nonzero hq $ by simpa using h lemma norm_nonneg (f : padic_seq p) : f.norm β‰₯ 0 := if hf : f β‰ˆ 0 then by simp [hf, norm] else by simp [norm, hf, padic_norm.nonneg] /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left_left {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) (v2 v3 : β„•) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max (stationary_point hf) (max v2 v3))) := let i := max (stationary_point hf) (max v2 v3) in begin apply stationary_point_spec hf, { apply le_max_left }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) (v1 v3 : β„•) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max (stationary_point hf) v3))) := let i := max v1 (max (stationary_point hf) v3) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_left _ v3 }, { apply le_max_right } }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_right {f : padic_seq p} (hf : Β¬ f β‰ˆ 0) (v1 v2 : β„•) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max v2 (stationary_point hf)))) := let i := max v1 (max v2 (stationary_point hf)) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_right v2 }, { apply le_max_right } }, { apply le_refl } end end embedding end padic_seq section open padic_seq private meta def index_simp_core (hh hf hg : expr) (at_ : interactive.loc := interactive.loc.ns [none]) : tactic unit := do [v1, v2, v3] ← [hh, hf, hg].mmap (Ξ» n, tactic.mk_app ``stationary_point [n] <|> return n), e1 ← tactic.mk_app ``lift_index_left_left [hh, v2, v3] <|> return `(true), e2 ← tactic.mk_app ``lift_index_left [hf, v1, v3] <|> return `(true), e3 ← tactic.mk_app ``lift_index_right [hg, v1, v2] <|> return `(true), sl ← [e1, e2, e3].mfoldl (Ξ» s e, simp_lemmas.add s e) simp_lemmas.mk, when at_.include_goal (tactic.simp_target sl), hs ← at_.get_locals, hs.mmap' (tactic.simp_hyp sl []) /-- This is a special-purpose tactic that lifts padic_norm (f (stationary_point f)) to padic_norm (f (max _ _ _)). -/ meta def tactic.interactive.padic_index_simp (l : interactive.parse interactive.types.pexpr_list) (at_ : interactive.parse interactive.types.location) : tactic unit := do [h, f, g] ← l.mmap tactic.i_to_expr, index_simp_core h f g at_ end namespace padic_seq section embedding open cau_seq variables {p : β„•} [hp : nat.prime p] include hp lemma norm_mul (f g : padic_seq p) : (f * g).norm = f.norm * g.norm := if hf : f β‰ˆ 0 then have hg : f * g β‰ˆ 0, from mul_equiv_zero' _ hf, by simp [hf, hg, norm] else if hg : g β‰ˆ 0 then have hf : f * g β‰ˆ 0, from mul_equiv_zero _ hg, by simp [hf, hg, norm] else have hfg : Β¬ f * g β‰ˆ 0, by apply mul_not_equiv_zero; assumption, begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.mul end lemma eq_zero_iff_equiv_zero (f : padic_seq p) : mk f = 0 ↔ f β‰ˆ 0 := mk_eq lemma ne_zero_iff_nequiv_zero (f : padic_seq p) : mk f β‰  0 ↔ Β¬ f β‰ˆ 0 := not_iff_not.2 (eq_zero_iff_equiv_zero _) lemma norm_const (q : β„š) : norm (const (padic_norm p) q) = padic_norm p q := if hq : q = 0 then have (const (padic_norm p) q) β‰ˆ 0, by simp [hq]; apply setoid.refl (const (padic_norm p) 0), by subst hq; simp [norm, this] else have Β¬ (const (padic_norm p) q) β‰ˆ 0, from not_equiv_zero_const_of_nonzero hq, by simp [norm, this] lemma norm_image (a : padic_seq p) (ha : Β¬ a β‰ˆ 0) : (βˆƒ (n : β„€), a.norm = ↑p ^ (-n)) := let ⟨k, hk, hk'⟩ := norm_eq_norm_app_of_nonzero ha in by simpa [hk] using padic_norm.image p hk' lemma norm_one : norm (1 : padic_seq p) = 1 := have h1 : Β¬ (1 : padic_seq p) β‰ˆ 0, from one_not_equiv_zero _, by simp [h1, norm, hp.one_lt] private lemma norm_eq_of_equiv_aux {f g : padic_seq p} (hf : Β¬ f β‰ˆ 0) (hg : Β¬ g β‰ˆ 0) (hfg : f β‰ˆ g) (h : padic_norm p (f (stationary_point hf)) β‰  padic_norm p (g (stationary_point hg))) (hgt : padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg))) : false := begin have hpn : padic_norm p (f (stationary_point hf)) - padic_norm p (g (stationary_point hg)) > 0, from sub_pos_of_lt hgt, cases hfg _ hpn with N hN, let i := max N (max (stationary_point hf) (stationary_point hg)), have hi : i β‰₯ N, from le_max_left _ _, have hN' := hN _ hi, padic_index_simp [N, hf, hg] at hN' h hgt, have hpne : padic_norm p (f i) β‰  padic_norm p (-(g i)), by rwa [ ←padic_norm.neg p (g i)] at h, let hpnem := add_eq_max_of_ne p hpne, have hpeq : padic_norm p ((f - g) i) = max (padic_norm p (f i)) (padic_norm p (g i)), { rwa padic_norm.neg at hpnem }, rw [hpeq, max_eq_left_of_lt hgt] at hN', have : padic_norm p (f i) < padic_norm p (f i), { apply lt_of_lt_of_le hN', apply sub_le_self, apply padic_norm.nonneg }, exact lt_irrefl _ this end private lemma norm_eq_of_equiv {f g : padic_seq p} (hf : Β¬ f β‰ˆ 0) (hg : Β¬ g β‰ˆ 0) (hfg : f β‰ˆ g) : padic_norm p (f (stationary_point hf)) = padic_norm p (g (stationary_point hg)) := begin by_contradiction h, cases (decidable.em (padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg)))) with hgt hngt, { exact norm_eq_of_equiv_aux hf hg hfg h hgt }, { apply norm_eq_of_equiv_aux hg hf (setoid.symm hfg) (ne.symm h), apply lt_of_le_of_ne, apply le_of_not_gt hngt, apply h } end theorem norm_equiv {f g : padic_seq p} (hfg : f β‰ˆ g) : f.norm = g.norm := if hf : f β‰ˆ 0 then have hg : g β‰ˆ 0, from setoid.trans (setoid.symm hfg) hf, by simp [norm, hf, hg] else have hg : Β¬ g β‰ˆ 0, from hf ∘ setoid.trans hfg, by unfold norm; split_ifs; exact norm_eq_of_equiv hf hg hfg private lemma norm_nonarchimedean_aux {f g : padic_seq p} (hfg : Β¬ f + g β‰ˆ 0) (hf : Β¬ f β‰ˆ 0) (hg : Β¬ g β‰ˆ 0) : (f + g).norm ≀ max (f.norm) (g.norm) := begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.nonarchimedean end theorem norm_nonarchimedean (f g : padic_seq p) : (f + g).norm ≀ max (f.norm) (g.norm) := if hfg : f + g β‰ˆ 0 then have 0 ≀ max (f.norm) (g.norm), from le_max_left_of_le (norm_nonneg _), by simpa [hfg, norm] else if hf : f β‰ˆ 0 then have hfg' : f + g β‰ˆ g, { change lim_zero (f - 0) at hf, show lim_zero (f + g - g), by simpa using hf }, have hcfg : (f + g).norm = g.norm, from norm_equiv hfg', have hcl : f.norm = 0, from (norm_zero_iff f).2 hf, have max (f.norm) (g.norm) = g.norm, by rw hcl; exact max_eq_right (norm_nonneg _), by rw [this, hcfg] else if hg : g β‰ˆ 0 then have hfg' : f + g β‰ˆ f, { change lim_zero (g - 0) at hg, show lim_zero (f + g - f), by simpa [add_sub_cancel'] using hg }, have hcfg : (f + g).norm = f.norm, from norm_equiv hfg', have hcl : g.norm = 0, from (norm_zero_iff g).2 hg, have max (f.norm) (g.norm) = f.norm, by rw hcl; exact max_eq_left (norm_nonneg _), by rw [this, hcfg] else norm_nonarchimedean_aux hfg hf hg lemma norm_eq {f g : padic_seq p} (h : βˆ€ k, padic_norm p (f k) = padic_norm p (g k)) : f.norm = g.norm := if hf : f β‰ˆ 0 then have hg : g β‰ˆ 0, from equiv_zero_of_val_eq_of_equiv_zero h hf, by simp [hf, hg, norm] else have hg : Β¬ g β‰ˆ 0, from Ξ» hg, hf $ equiv_zero_of_val_eq_of_equiv_zero (by simp [h]) hg, begin simp [hg, hf, norm], let i := max (stationary_point hf) (stationary_point hg), have hpf : padic_norm p (f (stationary_point hf)) = padic_norm p (f i), { apply stationary_point_spec, apply le_max_left, apply le_refl }, have hpg : padic_norm p (g (stationary_point hg)) = padic_norm p (g i), { apply stationary_point_spec, apply le_max_right, apply le_refl }, rw [hpf, hpg, h] end lemma norm_neg (a : padic_seq p) : (-a).norm = a.norm := norm_eq $ by simp lemma norm_eq_of_add_equiv_zero {f g : padic_seq p} (h : f + g β‰ˆ 0) : f.norm = g.norm := have lim_zero (f + g - 0), from h, have f β‰ˆ -g, from show lim_zero (f - (-g)), by simpa, have f.norm = (-g).norm, from norm_equiv this, by simpa [norm_neg] using this lemma add_eq_max_of_ne {f g : padic_seq p} (hfgne : f.norm β‰  g.norm) : (f + g).norm = max f.norm g.norm := have hfg : Β¬f + g β‰ˆ 0, from mt norm_eq_of_add_equiv_zero hfgne, if hf : f β‰ˆ 0 then have lim_zero (f - 0), from hf, have f + g β‰ˆ g, from show lim_zero ((f + g) - g), by simpa, have h1 : (f+g).norm = g.norm, from norm_equiv this, have h2 : f.norm = 0, from (norm_zero_iff _).2 hf, by rw [h1, h2]; rw max_eq_right (norm_nonneg _) else if hg : g β‰ˆ 0 then have lim_zero (g - 0), from hg, have f + g β‰ˆ f, from show lim_zero ((f + g) - f), by rw [add_sub_cancel']; simpa, have h1 : (f+g).norm = f.norm, from norm_equiv this, have h2 : g.norm = 0, from (norm_zero_iff _).2 hg, by rw [h1, h2]; rw max_eq_left (norm_nonneg _) else begin unfold norm at ⊒ hfgne, split_ifs at ⊒ hfgne, padic_index_simp [hfg, hf, hg] at ⊒ hfgne, apply padic_norm.add_eq_max_of_ne, simpa [hf, hg, norm] using hfgne end end embedding end padic_seq /-- The p-adic numbers `Q_[p]` are the Cauchy completion of `β„š` with respect to the p-adic norm. -/ def padic (p : β„•) [nat.prime p] := @cau_seq.completion.Cauchy _ _ _ _ (padic_norm p) _ notation `β„š_[` p `]` := padic p namespace padic section completion variables {p : β„•} [nat.prime p] /-- The discrete field structure on β„š_p is inherited from the Cauchy completion construction. -/ instance field : field (β„š_[p]) := cau_seq.completion.field instance : inhabited β„š_[p] := ⟨0⟩ -- short circuits instance : has_zero β„š_[p] := by apply_instance instance : has_one β„š_[p] := by apply_instance instance : has_add β„š_[p] := by apply_instance instance : has_mul β„š_[p] := by apply_instance instance : has_sub β„š_[p] := by apply_instance instance : has_neg β„š_[p] := by apply_instance instance : has_div β„š_[p] := by apply_instance instance : add_comm_group β„š_[p] := by apply_instance instance : comm_ring β„š_[p] := by apply_instance /-- Builds the equivalence class of a Cauchy sequence of rationals. -/ def mk : padic_seq p β†’ β„š_[p] := quotient.mk end completion section completion variables (p : β„•) [nat.prime p] lemma mk_eq {f g : padic_seq p} : mk f = mk g ↔ f β‰ˆ g := quotient.eq /-- Embeds the rational numbers in the p-adic numbers. -/ def of_rat : β„š β†’ β„š_[p] := cau_seq.completion.of_rat @[simp] lemma of_rat_add : βˆ€ (x y : β„š), of_rat p (x + y) = of_rat p x + of_rat p y := cau_seq.completion.of_rat_add @[simp] lemma of_rat_neg : βˆ€ (x : β„š), of_rat p (-x) = -of_rat p x := cau_seq.completion.of_rat_neg @[simp] lemma of_rat_mul : βˆ€ (x y : β„š), of_rat p (x * y) = of_rat p x * of_rat p y := cau_seq.completion.of_rat_mul @[simp] lemma of_rat_sub : βˆ€ (x y : β„š), of_rat p (x - y) = of_rat p x - of_rat p y := cau_seq.completion.of_rat_sub @[simp] lemma of_rat_div : βˆ€ (x y : β„š), of_rat p (x / y) = of_rat p x / of_rat p y := cau_seq.completion.of_rat_div @[simp] lemma of_rat_one : of_rat p 1 = 1 := rfl @[simp] lemma of_rat_zero : of_rat p 0 = 0 := rfl @[simp] lemma cast_eq_of_rat_of_nat (n : β„•) : (↑n : β„š_[p]) = of_rat p n := begin induction n with n ih, { refl }, { simpa using ih } end -- without short circuits, this needs an increase of class.instance_max_depth @[simp] lemma cast_eq_of_rat_of_int (n : β„€) : ↑n = of_rat p n := by induction n; simp lemma cast_eq_of_rat : βˆ€ (q : β„š), (↑q : β„š_[p]) = of_rat p q | ⟨n, d, h1, h2⟩ := show ↑n / ↑d = _, from have (⟨n, d, h1, h2⟩ : β„š) = rat.mk n d, from rat.num_denom', by simp [this, rat.mk_eq_div, of_rat_div] @[norm_cast] lemma coe_add : βˆ€ {x y : β„š}, (↑(x + y) : β„š_[p]) = ↑x + ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_neg : βˆ€ {x : β„š}, (↑(-x) : β„š_[p]) = -↑x := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_mul : βˆ€ {x y : β„š}, (↑(x * y) : β„š_[p]) = ↑x * ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_sub : βˆ€ {x y : β„š}, (↑(x - y) : β„š_[p]) = ↑x - ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_div : βˆ€ {x y : β„š}, (↑(x / y) : β„š_[p]) = ↑x / ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_one : (↑1 : β„š_[p]) = 1 := rfl @[norm_cast] lemma coe_zero : (↑0 : β„š_[p]) = 0 := rfl lemma const_equiv {q r : β„š} : const (padic_norm p) q β‰ˆ const (padic_norm p) r ↔ q = r := ⟨ Ξ» heq : lim_zero (const (padic_norm p) (q - r)), eq_of_sub_eq_zero $ const_lim_zero.1 heq, Ξ» heq, by rw heq; apply setoid.refl _ ⟩ lemma of_rat_eq {q r : β„š} : of_rat p q = of_rat p r ↔ q = r := ⟨(const_equiv p).1 ∘ quotient.eq.1, Ξ» h, by rw h⟩ @[norm_cast] lemma coe_inj {q r : β„š} : (↑q : β„š_[p]) = ↑r ↔ q = r := by simp [cast_eq_of_rat, of_rat_eq] instance : char_zero β„š_[p] := ⟨λ m n, by { rw ← rat.cast_coe_nat, norm_cast, exact id }⟩ end completion end padic /-- The rational-valued p-adic norm on β„š_p is lifted from the norm on Cauchy sequences. The canonical form of this function is the normed space instance, with notation `βˆ₯ βˆ₯`. -/ def padic_norm_e {p : β„•} [hp : nat.prime p] : β„š_[p] β†’ β„š := quotient.lift padic_seq.norm $ @padic_seq.norm_equiv _ _ namespace padic_norm_e section embedding open padic_seq variables {p : β„•} [nat.prime p] lemma defn (f : padic_seq p) {Ξ΅ : β„š} (hΞ΅ : Ξ΅ > 0) : βˆƒ N, βˆ€ i β‰₯ N, padic_norm_e (⟦f⟧ - f i) < Ξ΅ := begin simp only [padic.cast_eq_of_rat], change βˆƒ N, βˆ€ i β‰₯ N, (f - const _ (f i)).norm < Ξ΅, by_contradiction h, cases cauchyβ‚‚ f hΞ΅ with N hN, have : βˆ€ N, βˆƒ i β‰₯ N, (f - const _ (f i)).norm β‰₯ Ξ΅, by simpa [not_forall] using h, rcases this N with ⟨i, hi, hge⟩, have hne : Β¬ (f - const (padic_norm p) (f i)) β‰ˆ 0, { intro h, unfold padic_seq.norm at hge; split_ifs at hge, exact not_lt_of_ge hge hΞ΅ }, unfold padic_seq.norm at hge; split_ifs at hge, apply not_le_of_gt _ hge, cases decidable.em ((stationary_point hne) β‰₯ N) with hgen hngen, { apply hN; assumption }, { have := stationary_point_spec hne (le_refl _) (le_of_not_le hngen), rw ←this, apply hN, apply le_refl, assumption } end protected lemma nonneg (q : β„š_[p]) : padic_norm_e q β‰₯ 0 := quotient.induction_on q $ norm_nonneg lemma zero_def : (0 : β„š_[p]) = ⟦0⟧ := rfl lemma zero_iff (q : β„š_[p]) : padic_norm_e q = 0 ↔ q = 0 := quotient.induction_on q $ by simpa only [zero_def, quotient.eq] using norm_zero_iff @[simp] protected lemma zero : padic_norm_e (0 : β„š_[p]) = 0 := (zero_iff _).2 rfl /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`βˆ₯ βˆ₯`). -/ @[simp] protected lemma one' : padic_norm_e (1 : β„š_[p]) = 1 := norm_one @[simp] protected lemma neg (q : β„š_[p]) : padic_norm_e (-q) = padic_norm_e q := quotient.induction_on q $ norm_neg /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`βˆ₯ βˆ₯`). -/ theorem nonarchimedean' (q r : β„š_[p]) : padic_norm_e (q + r) ≀ max (padic_norm_e q) (padic_norm_e r) := quotient.induction_onβ‚‚ q r $ norm_nonarchimedean /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`βˆ₯ βˆ₯`). -/ theorem add_eq_max_of_ne' {q r : β„š_[p]} : padic_norm_e q β‰  padic_norm_e r β†’ padic_norm_e (q + r) = max (padic_norm_e q) (padic_norm_e r) := quotient.induction_onβ‚‚ q r $ Ξ» _ _, padic_seq.add_eq_max_of_ne lemma triangle_ineq (x y z : β„š_[p]) : padic_norm_e (x - z) ≀ padic_norm_e (x - y) + padic_norm_e (y - z) := calc padic_norm_e (x - z) = padic_norm_e ((x - y) + (y - z)) : by rw sub_add_sub_cancel ... ≀ max (padic_norm_e (x - y)) (padic_norm_e (y - z)) : padic_norm_e.nonarchimedean' _ _ ... ≀ padic_norm_e (x - y) + padic_norm_e (y - z) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma add (q r : β„š_[p]) : padic_norm_e (q + r) ≀ (padic_norm_e q) + (padic_norm_e r) := calc padic_norm_e (q + r) ≀ max (padic_norm_e q) (padic_norm_e r) : nonarchimedean' _ _ ... ≀ (padic_norm_e q) + (padic_norm_e r) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma mul' (q r : β„š_[p]) : padic_norm_e (q * r) = (padic_norm_e q) * (padic_norm_e r) := quotient.induction_onβ‚‚ q r $ norm_mul instance : is_absolute_value (@padic_norm_e p _) := { abv_nonneg := padic_norm_e.nonneg, abv_eq_zero := zero_iff, abv_add := padic_norm_e.add, abv_mul := padic_norm_e.mul' } @[simp] lemma eq_padic_norm' (q : β„š) : padic_norm_e (padic.of_rat p q) = padic_norm p q := norm_const _ protected theorem image' {q : β„š_[p]} : q β‰  0 β†’ βˆƒ n : β„€, padic_norm_e q = p ^ (-n) := quotient.induction_on q $ Ξ» f hf, have Β¬ f β‰ˆ 0, from (ne_zero_iff_nequiv_zero f).1 hf, norm_image f this lemma sub_rev (q r : β„š_[p]) : padic_norm_e (q - r) = padic_norm_e (r - q) := by rw ←(padic_norm_e.neg); simp end embedding end padic_norm_e namespace padic section complete open padic_seq padic theorem rat_dense' {p : β„•} [nat.prime p] (q : β„š_[p]) {Ξ΅ : β„š} (hΞ΅ : Ξ΅ > 0) : βˆƒ r : β„š, padic_norm_e (q - r) < Ξ΅ := quotient.induction_on q $ Ξ» q', have βˆƒ N, βˆ€ m n β‰₯ N, padic_norm p (q' m - q' n) < Ξ΅, from cauchyβ‚‚ _ hΞ΅, let ⟨N, hN⟩ := this in ⟨q' N, begin simp only [padic.cast_eq_of_rat], change padic_seq.norm (q' - const _ (q' N)) < Ξ΅, cases decidable.em ((q' - const (padic_norm p) (q' N)) β‰ˆ 0) with heq hne', { simpa only [heq, padic_seq.norm, dif_pos] }, { simp only [padic_seq.norm, dif_neg hne'], change padic_norm p (q' _ - q' _) < Ξ΅, have := stationary_point_spec hne', cases decidable.em (N β‰₯ stationary_point hne') with hle hle, { have := eq.symm (this (le_refl _) hle), simp at this, simpa [this] }, { apply hN, apply le_of_lt, apply lt_of_not_ge, apply hle, apply le_refl }} end⟩ variables {p : β„•} [nat.prime p] (f : cau_seq _ (@padic_norm_e p _)) open classical private lemma div_nat_pos (n : β„•) : (1 / ((n + 1): β„š)) > 0 := div_pos zero_lt_one (by exact_mod_cast succ_pos _) def lim_seq : β„• β†’ β„š := Ξ» n, classical.some (rat_dense' (f n) (div_nat_pos n)) lemma exi_rat_seq_conv {Ξ΅ : β„š} (hΞ΅ : 0 < Ξ΅) : βˆƒ N, βˆ€ i β‰₯ N, padic_norm_e (f i - ((lim_seq f) i : β„š_[p])) < Ξ΅ := begin refine (exists_nat_gt (1/Ξ΅)).imp (Ξ» N hN i hi, _), have h := classical.some_spec (rat_dense' (f i) (div_nat_pos i)), refine lt_of_lt_of_le h (div_le_of_le_mul (by exact_mod_cast succ_pos _) _), rw right_distrib, apply le_add_of_le_of_nonneg, { exact le_mul_of_div_le hΞ΅ (le_trans (le_of_lt hN) (by exact_mod_cast hi)) }, { apply le_of_lt, simpa } end lemma exi_rat_seq_conv_cauchy : is_cau_seq (padic_norm p) (lim_seq f) := assume Ξ΅ hΞ΅, have hΞ΅3 : Ξ΅ / 3 > 0, from div_pos hΞ΅ (by norm_num), let ⟨N, hN⟩ := exi_rat_seq_conv f hΞ΅3, ⟨N2, hN2⟩ := f.cauchyβ‚‚ hΞ΅3 in begin existsi max N N2, intros j hj, suffices : padic_norm_e ((↑(lim_seq f j) - f (max N N2)) + (f (max N N2) - lim_seq f (max N N2))) < Ξ΅, { ring at this ⊒, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat], exact_mod_cast this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : (3 : β„š) β‰  0, by norm_num, have : Ξ΅ = Ξ΅ / 3 + Ξ΅ / 3 + Ξ΅ / 3, { apply eq_of_mul_eq_mul_left this, simp [left_distrib, mul_div_cancel' _ this ], ring }, rw this, apply add_lt_add, { suffices : padic_norm_e ((↑(lim_seq f j) - f j) + (f j - f (max N N2))) < Ξ΅ / 3 + Ξ΅ / 3, by simpa [sub_eq_add_neg], apply lt_of_le_of_lt, { apply padic_norm_e.add }, { apply add_lt_add, { rw [padic_norm_e.sub_rev], apply_mod_cast hN, exact le_of_max_le_left hj }, { apply hN2, exact le_of_max_le_right hj, apply le_max_right }}}, { apply_mod_cast hN, apply le_max_left }}} end private def lim' : padic_seq p := ⟨_, exi_rat_seq_conv_cauchy f⟩ private def lim : β„š_[p] := ⟦lim' f⟧ theorem complete' : βˆƒ q : β„š_[p], βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ i β‰₯ N, padic_norm_e (q - f i) < Ξ΅ := ⟨ lim f, Ξ» Ξ΅ hΞ΅, let ⟨N, hN⟩ := exi_rat_seq_conv f (show Ξ΅ / 2 > 0, from div_pos hΞ΅ (by norm_num)), ⟨N2, hN2⟩ := padic_norm_e.defn (lim' f) (show Ξ΅ / 2 > 0, from div_pos hΞ΅ (by norm_num)) in begin existsi max N N2, intros i hi, suffices : padic_norm_e ((lim f - lim' f i) + (lim' f i - f i)) < Ξ΅, { ring at this; exact this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : Ξ΅ = Ξ΅ / 2 + Ξ΅ / 2, by rw ←(add_self_div_two Ξ΅); simp, rw this, apply add_lt_add, { apply hN2, exact le_of_max_le_right hi }, { rw_mod_cast [padic_norm_e.sub_rev], apply hN, exact le_of_max_le_left hi }}} end ⟩ end complete section normed_space variables (p : β„•) [nat.prime p] instance : has_dist β„š_[p] := ⟨λ x y, padic_norm_e (x - y)⟩ instance : metric_space β„š_[p] := { dist_self := by simp [dist], dist_comm := Ξ» x y, by unfold dist; rw ←padic_norm_e.neg (x - y); simp, dist_triangle := begin intros, unfold dist, exact_mod_cast padic_norm_e.triangle_ineq _ _ _, end, eq_of_dist_eq_zero := begin unfold dist, intros _ _ h, apply eq_of_sub_eq_zero, apply (padic_norm_e.zero_iff _).1, exact_mod_cast h end } instance : has_norm β„š_[p] := ⟨λ x, padic_norm_e x⟩ instance : normed_field β„š_[p] := { dist_eq := Ξ» _ _, rfl, norm_mul' := by simp [has_norm.norm, padic_norm_e.mul'] } instance : is_absolute_value (Ξ» a : β„š_[p], βˆ₯aβˆ₯) := { abv_nonneg := norm_nonneg, abv_eq_zero := Ξ» _, norm_eq_zero, abv_add := norm_add_le, abv_mul := by simp [has_norm.norm, padic_norm_e.mul'] } theorem rat_dense {p : β„•} {hp : p.prime} (q : β„š_[p]) {Ξ΅ : ℝ} (hΞ΅ : Ξ΅ > 0) : βˆƒ r : β„š, βˆ₯q - rβˆ₯ < Ξ΅ := let ⟨Ρ', hΞ΅'l, hΞ΅'r⟩ := exists_rat_btwn hΞ΅, ⟨r, hr⟩ := rat_dense' q (by simpa using hΞ΅'l) in ⟨r, lt.trans (by simpa [has_norm.norm] using hr) hΞ΅'r⟩ end normed_space end padic namespace padic_norm_e section normed_space variables {p : β„•} [hp : p.prime] include hp @[simp] protected lemma mul (q r : β„š_[p]) : βˆ₯q * rβˆ₯ = βˆ₯qβˆ₯ * βˆ₯rβˆ₯ := by simp [has_norm.norm, padic_norm_e.mul'] protected lemma is_norm (q : β„š_[p]) : ↑(padic_norm_e q) = βˆ₯qβˆ₯ := rfl theorem nonarchimedean (q r : β„š_[p]) : βˆ₯q + rβˆ₯ ≀ max (βˆ₯qβˆ₯) (βˆ₯rβˆ₯) := begin unfold has_norm.norm, exact_mod_cast nonarchimedean' _ _ end theorem add_eq_max_of_ne {q r : β„š_[p]} (h : βˆ₯qβˆ₯ β‰  βˆ₯rβˆ₯) : βˆ₯q+rβˆ₯ = max (βˆ₯qβˆ₯) (βˆ₯rβˆ₯) := begin unfold has_norm.norm, apply_mod_cast add_eq_max_of_ne', intro h', apply h, unfold has_norm.norm, exact_mod_cast h' end @[simp] lemma eq_padic_norm (q : β„š) : βˆ₯(↑q : β„š_[p])βˆ₯ = padic_norm p q := begin unfold has_norm.norm, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat] end instance : nondiscrete_normed_field β„š_[p] := { non_trivial := ⟨padic.of_rat p (p⁻¹), begin have h0 : p β‰  0 := ne_of_gt (hp.pos), have h1 : 1 < p := hp.one_lt, rw [← padic.cast_eq_of_rat, eq_padic_norm], simp only [padic_norm, inv_eq_zero], simp only [if_neg] {discharger := `[exact_mod_cast h0]}, norm_cast, simp only [padic_val_rat.inv] {discharger := `[exact_mod_cast h0]}, rw [neg_neg, padic_val_rat.padic_val_rat_self h1], erw _root_.pow_one, exact_mod_cast h1, end⟩ } protected theorem image {q : β„š_[p]} : q β‰  0 β†’ βˆƒ n : β„€, βˆ₯qβˆ₯ = ↑((↑p : β„š) ^ (-n)) := quotient.induction_on q $ Ξ» f hf, have Β¬ f β‰ˆ 0, from (padic_seq.ne_zero_iff_nequiv_zero f).1 hf, let ⟨n, hn⟩ := padic_seq.norm_image f this in ⟨n, congr_arg coe hn⟩ protected lemma is_rat (q : β„š_[p]) : βˆƒ q' : β„š, βˆ₯qβˆ₯ = ↑q' := if h : q = 0 then ⟨0, by simp [h]⟩ else let ⟨n, hn⟩ := padic_norm_e.image h in ⟨_, hn⟩ def rat_norm (q : β„š_[p]) : β„š := classical.some (padic_norm_e.is_rat q) lemma eq_rat_norm (q : β„š_[p]) : βˆ₯qβˆ₯ = rat_norm q := classical.some_spec (padic_norm_e.is_rat q) theorem norm_rat_le_one : βˆ€ {q : β„š} (hq : Β¬ p ∣ q.denom), βˆ₯(q : β„š_[p])βˆ₯ ≀ 1 | ⟨n, d, hn, hd⟩ := Ξ» hq : Β¬ p ∣ d, if hnz : n = 0 then have (⟨n, d, hn, hd⟩ : β„š) = 0, from rat.zero_iff_num_zero.mpr hnz, by norm_num [this] else begin have hnz' : {rat . num := n, denom := d, pos := hn, cop := hd} β‰  0, from mt rat.zero_iff_num_zero.1 hnz, rw [padic_norm_e.eq_padic_norm], norm_cast, rw [padic_norm.eq_fpow_of_nonzero p hnz', padic_val_rat_def p hnz'], have h : (multiplicity p d).get _ = 0, by simp [multiplicity_eq_zero_of_not_dvd, hq], simp only [], norm_cast, rw_mod_cast [h, sub_zero], apply fpow_le_one_of_nonpos, { exact_mod_cast le_of_lt hp.one_lt, }, { apply neg_nonpos_of_nonneg, norm_cast, simp, } end lemma eq_of_norm_add_lt_right {p : β„•} {hp : p.prime} {z1 z2 : β„š_[p]} (h : βˆ₯z1 + z2βˆ₯ < βˆ₯z2βˆ₯) : βˆ₯z1βˆ₯ = βˆ₯z2βˆ₯ := by_contradiction $ Ξ» hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_right) h lemma eq_of_norm_add_lt_left {p : β„•} {hp : p.prime} {z1 z2 : β„š_[p]} (h : βˆ₯z1 + z2βˆ₯ < βˆ₯z1βˆ₯) : βˆ₯z1βˆ₯ = βˆ₯z2βˆ₯ := by_contradiction $ Ξ» hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_left) h end normed_space end padic_norm_e namespace padic variables {p : β„•} [nat.prime p] set_option eqn_compiler.zeta true instance complete : cau_seq.is_complete β„š_[p] norm := begin split, intro f, have cau_seq_norm_e : is_cau_seq padic_norm_e f, { intros Ξ΅ hΞ΅, let h := is_cau f Ξ΅ (by exact_mod_cast hΞ΅), unfold norm at h, apply_mod_cast h }, cases padic.complete' ⟨f, cau_seq_norm_e⟩ with q hq, existsi q, intros Ξ΅ hΞ΅, cases exists_rat_btwn hΞ΅ with Ξ΅' hΞ΅', norm_cast at hΞ΅', cases hq Ξ΅' hΞ΅'.1 with N hN, existsi N, intros i hi, let h := hN i hi, unfold norm, rw_mod_cast [cau_seq.sub_apply, padic_norm_e.sub_rev], refine lt.trans _ hΞ΅'.2, exact_mod_cast hN i hi end lemma padic_norm_e_lim_le {f : cau_seq β„š_[p] norm} {a : ℝ} (ha : a > 0) (hf : βˆ€ i, βˆ₯f iβˆ₯ ≀ a) : βˆ₯f.limβˆ₯ ≀ a := let ⟨N, hN⟩ := setoid.symm (cau_seq.equiv_lim f) _ ha in calc βˆ₯f.limβˆ₯ = βˆ₯f.lim - f N + f Nβˆ₯ : by simp ... ≀ max (βˆ₯f.lim - f Nβˆ₯) (βˆ₯f Nβˆ₯) : padic_norm_e.nonarchimedean _ _ ... ≀ a : max_le (le_of_lt (hN _ (le_refl _))) (hf _) end padic
21f998ee795d380c50a78f776f87df58d8695ed1
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/mvar_fvar.lean
0f7a2dc08e9b2cbdfb038bd01ac9a377143652b2
[ "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
275
lean
import Lean open Lean #eval (mkFVar `a).hasFVar #eval (mkApp (mkConst `foo) (mkFVar `a)).hasFVar #eval (mkApp (mkConst `foo) (mkConst `a)).hasFVar #eval (mkMVar `a).hasMVar #eval (mkApp (mkConst `foo) (mkMVar `a)).hasMVar #eval (mkApp (mkConst `foo) (mkConst `a)).hasMVar
44d606278900424e69734374ee4240dc50c68cc3
ad3e8f15221a986da27c99f371922c0b3f5792b6
/src/week05/solutions/e01_matroids.lean
cf128164eaacb9ecb4494c65caa49786b18f5ac0
[]
no_license
VArtem/lean-itmo
a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1
dc44cd06f9f5b984d051831b3aaa7364e64c2dc4
refs/heads/main
1,683,761,214,467
1,622,821,295,000
1,622,821,295,000
357,236,048
12
0
null
null
null
null
UTF-8
Lean
false
false
14,629
lean
import data.finset import data.fintype.basic import tactic import week05.solutions.e00_intro open finset structure matroid (Ξ± : Type*) [fintype Ξ±] [decidable_eq Ξ±] := (ind : set (finset Ξ±)) (ind_empty : ind βˆ…) (ind_subset : βˆ€ ⦃A B⦄, A βŠ† B β†’ ind B β†’ ind A) (ind_exchange : βˆ€ (A B : finset Ξ±), ind A β†’ ind B β†’ A.card < B.card β†’ (βˆƒ (c ∈ B), (c βˆ‰ A) ∧ ind (insert c A))) namespace matroid attribute [simp] matroid.ind_empty variables {Ξ± : Type} [fintype Ξ±] [decidable_eq Ξ±] {A B X : finset Ξ±} {m : matroid Ξ±} -- ΠŸΠΎΠΏΡ€ΠΎΠ±ΡƒΠΉΡ‚Π΅ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ Π² term mode lemma dep_superset {A B} : A βŠ† B β†’ Β¬m.ind A β†’ Β¬m.ind B := Ξ» h hA hB, hA (m.ind_subset h hB) -- Π‘Π°Π·ΠΎΠΉ мноТСства A называСтся нСзависимоС мноТСство B, Ρ‡Ρ‚ΠΎ B βŠ† A, ΠΈ для любого x ∈ A \ B, B βˆͺ {x} зависимо -- x ∈ A \ B - Π½Π΅ΡƒΠ΄ΠΎΠ±Π½ΠΎΠ΅ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅, ΠΏΠΎΡ‚ΠΎΠΌΡƒ Ρ‡Ρ‚ΠΎ Π²Π΅Π·Π΄Π΅ придСтся ΠΏΠ΅Ρ€Π΅ΠΏΠΈΡΡ‹Π²Π°Ρ‚ΡŒ mem_sdiff -- Аналогично, для вставки x Π² B Π΅ΡΡ‚ΡŒ `insert x B` def base_of (m : matroid Ξ±) (X A : finset Ξ±) := A βŠ† X ∧ m.ind A ∧ βˆ€ x ∈ X, x βˆ‰ A β†’ Β¬m.ind (insert x A) lemma base_of_ind : m.base_of X A β†’ m.ind A := Ξ» h, h.2.1 lemma base_of_subset : m.base_of X A β†’ A βŠ† X := Ξ» h, h.1 lemma base_of_dep_of_insert {x} : m.base_of X A β†’ x ∈ X β†’ x βˆ‰ A β†’ Β¬m.ind (insert x A) := Ξ» h hX hA, h.2.2 x hX hA -- Π‘Π°Π·Π° ΠΌΠ°Ρ‚Ρ€ΠΎΠΈΠ΄Π° - Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π½ΠΎ, Π½ΠΎ Π±Π΅Π· ограничСния Π½Π° подмноТСство def base (m : matroid Ξ±) (B : finset Ξ±) := m.ind B ∧ βˆ€ x βˆ‰ B, Β¬m.ind (insert x B) lemma base_ind : m.base A β†’ m.ind A := Ξ» h, h.1 @[simp] lemma base_of_univ_iff_base {A : finset Ξ±} : m.base_of univ A ↔ m.base A := ⟨λ ⟨_, h_ind, h_ins⟩, ⟨h_ind, Ξ» x hx, h_ins x (mem_univ _) hx⟩, Ξ» ⟨h_ind, h_ins⟩, ⟨subset_univ A, h_ind, Ξ» x _ hx, h_ins x hx⟩⟩ -- БСкция с Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²Π°ΠΌΠΈ ΠΏΡ€ΠΎ `base_of` section base_of -- A нСзависимо Ρ‚ΠΎΠ³Π΄Π° ΠΈ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‚ΠΎΠ³Π΄Π°, ΠΊΠΎΠ³Π΄Π° A - Π±Π°Π·Π° A lemma ind_iff_base_of_self : m.ind A ↔ m.base_of A A := begin refine ⟨λ Aind, _, base_of_ind⟩, refine ⟨subset.refl _, Aind, Ξ» x hx hnx, absurd hx hnx⟩, end -- ВсС Π±Π°Π·Ρ‹ мноТСства Ρ€Π°Π²Π½ΠΎΠΌΠΎΡ‰Π½Ρ‹ -- ВспомнитС/ΠΏΠΎΠ΄ΡƒΠΌΠ°ΠΉΡ‚Π΅ ΠΏΡ€ΠΎ матСматичСскоС Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²ΠΎ lemma base_of_card_eq (hA : m.base_of X A) (hB : m.base_of X B) : A.card = B.card := begin by_contradiction hne, wlog h : A.card < B.card := lt_or_gt_of_ne hne using [A B, B A], obtain ⟨w, wB, wA, winsert⟩ := m.ind_exchange _ _ (base_of_ind hA) (base_of_ind hB) h, refine base_of_dep_of_insert hA (base_of_subset hB wB) wA winsert, end -- Π›ΡŽΠ±ΠΎΠ΅ нСзависимоС подмноТСство X ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡ‚Ρ€ΠΎΠΈΡ‚ΡŒ Π΄ΠΎ Π±Π°Π·Ρ‹ X -- `by_contradiction` Π΄ΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ Ρ†Π΅Π»ΡŒ ΠΎΡ‚ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠ³ΠΎ -- `push_neg` ΠΏΡ€ΠΎΡ‚Π°Π»ΠΊΠΈΠ²Π°Π΅Ρ‚ отрицания максимально Π²Π½ΡƒΡ‚Ρ€ΡŒ утвСрТдСния ΠΈΠ»ΠΈ Ρ†Π΅Π»ΠΈ -- Π’Π°ΠΊΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠΌΠΎΡ‡ΡŒ `nat.le_induction` #check @nat.le_induction lemma exists_base_of_superset_of_ind : A βŠ† X β†’ m.ind A β†’ βˆƒ B, A βŠ† B ∧ m.base_of X B := begin intros hAX hA, by_contradiction, push_neg at h, suffices ind_unbounded : βˆ€ k β‰₯ A.card, βˆƒ B, A βŠ† B ∧ B βŠ† X ∧ m.ind B ∧ B.card = k, { replace hAX := card_le_of_subset hAX, rcases ind_unbounded (X.card + 1) (by linarith) with ⟨B, -, hBX, -, Bcard⟩, replace hBX := card_le_of_subset hBX, linarith, }, apply nat.le_induction, { use [A, subset.refl A, hAX, hA], }, { rintro n Acard ⟨B, hAB, hBX, Bind, rfl⟩, specialize h B hAB, rw base_of at h, push_neg at h, rcases h hBX Bind with ⟨w, wX, wB, w_insert⟩, use [insert w B], use [subset.trans hAB (subset_insert _ _)], use [insert_subset.2 ⟨wX, hBX⟩], use [w_insert, card_insert_of_not_mem wB], } end -- Π£ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ подмноТСства X сущСствуСт Π±Π°Π·Π° theorem exists_base_of (m : matroid Ξ±) (X : finset Ξ±): βˆƒ A, m.base_of X A := begin obtain ⟨B, _, Bbase⟩ := exists_base_of_superset_of_ind (empty_subset X) m.ind_empty, exact ⟨B, Bbase⟩, end -- ΠœΠΎΡ‰Π½ΠΎΡΡ‚ΡŒ нСзависимого подмноТСства A βŠ† X Π½Π΅ большС мощности Π±Π°Π·Ρ‹ X lemma ind_card_le_base_of_card : A βŠ† X β†’ m.ind A β†’ m.base_of X B β†’ A.card ≀ B.card := begin intros hAX Aind Bbase, obtain ⟨T, hAT, Tbase⟩ := exists_base_of_superset_of_ind hAX Aind, rw base_of_card_eq Bbase Tbase, exact card_le_of_subset hAT, end end base_of -- Π’Π΅ΠΎΡ€Π΅ΠΌΠ° ΠΎ Π±Π°Π·Π°Ρ… ΠΌΠ°Ρ‚Ρ€ΠΎΠΈΠ΄Π° section base lemma base_eq_card (hA : m.base A) (hB : m.base B) : A.card = B.card := by {rw [←base_of_univ_iff_base] at *, exact base_of_card_eq hA hB} lemma ind_subset_base (hA : m.ind A) : βˆƒ B, A βŠ† B ∧ m.base B := begin obtain ⟨B, hAB, h_base⟩ := exists_base_of_superset_of_ind (subset_univ A) hA, rw base_of_univ_iff_base at h_base, use [B, hAB, h_base], end theorem exists_base : βˆƒ A, m.base A := begin obtain ⟨B, _, Bbase⟩ := ind_subset_base m.ind_empty, exact ⟨B, Bbase⟩, end theorem base_not_subset {A B} : m.base A β†’ m.base B β†’ A βŠ† B β†’ A = B := begin rintro Abase Bbase h_subset, exact eq_of_subset_of_card_le h_subset (ge_of_eq $ base_eq_card Abase Bbase), end lemma ind_and_card_eq_card_base_iff_base {A B} (Abase : m.base A) : (m.ind B ∧ A.card = B.card) ↔ m.base B := begin split, { rintro ⟨Bind, Acard⟩, use Bind, intros x xB h_insert_ind, obtain ⟨C, Bsubset, Cbase⟩ := ind_subset_base h_insert_ind, replace Bsubset := card_le_of_subset Bsubset, rw [base_eq_card Abase Cbase] at Acard, rw card_insert_of_not_mem xB at Bsubset, linarith, }, { intro Bbase, exact ⟨base_ind Bbase, base_eq_card Abase Bbase⟩, } end -- ΠŸΠΎΡ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅ ΠΏΡ€ΠΎ Ρ‚Π°ΠΊΡ‚ΠΈΠΊΡƒ `finish`: https://leanprover-community.github.io/mathlib_docs/tactics.html#finish%20/%20clarify%20/%20safe theorem base_exchange {A B} : m.base A β†’ m.base B β†’ βˆ€ x ∈ A \ B, βˆƒ b ∈ (B \ A), m.base (insert b (A.erase x)) := begin intros Abase Bbase x xA, rcases m.ind_exchange (A.erase x) B _ Bbase.1 _ with ⟨c, cB, cA, cinsert⟩, { use c, rw mem_sdiff at ⊒ xA, use [cB], { finish, }, { apply (ind_and_card_eq_card_base_iff_base Abase).1, use cinsert, rw [card_insert_of_not_mem cA, card_erase_of_mem' xA.1], }, }, { exact m.ind_subset (erase_subset _ _) (base_ind Abase), }, { rw mem_sdiff at xA, rw [← base_eq_card Abase Bbase, card_erase_of_mem xA.1], exact nat.pred_lt (card_ne_zero_of_mem xA.1), }, end end base -- Π¦ΠΈΠΊΠ» ΠΌΠ°Ρ‚Ρ€ΠΎΠΈΠ΄Π° это минимальноС ΠΏΠΎ Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡŽ зависимоС мноТСство def circuit (m : matroid Ξ±) (A : finset Ξ±) := Β¬ m.ind A ∧ βˆ€ x ∈ A, m.ind (A.erase x) section circuit theorem not_circuit : Β¬ m.circuit βˆ… := Ξ» ⟨empty_dep, _⟩, empty_dep m.ind_empty lemma circuit_ssubset : m.circuit A β†’ B βŠ‚ A β†’ m.ind B := begin rintro ⟨Anind, Aerase⟩ Bsubset, rw ssubset_iff at Bsubset, rcases Bsubset with ⟨x, xB, x_insert⟩, rw insert_subset at x_insert, cases x_insert with xA h_subset, apply m.ind_subset _ (Aerase x xA), rw ← erase_eq_of_not_mem xB, exact erase_subset_erase x h_subset, end theorem circuit_not_subset {A B} : m.circuit A β†’ m.circuit B β†’ A βŠ† B β†’ A = B := begin rintro Acirc Bcirc h_subset, cases @lt_or_eq_of_le _ _ A B h_subset, { exfalso, exact Acirc.1 (circuit_ssubset Bcirc h), }, { exact h, } end -- Π§Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ эту Π»Π΅ΠΌΠΌΡƒ ΠΎΡ‚ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠ³ΠΎ, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ ΡƒΡ‚Π²Π΅Ρ€ΠΆΠ΄Π΅Π½ΠΈΠ΅ Π²ΠΈΠ΄Π° -- "Если A зависимо ΠΈ Ρƒ Π½Π΅Π³ΠΎ Π½Π΅Ρ‚ подмноТСств-Ρ†ΠΈΠΊΠ»ΠΎΠ², -- Ρ‚ΠΎ для всСх k ΠΎΡ‚ 0 Π΄ΠΎ |A| сущСствуСт зависимоС мноТСство Ρ€Π°Π·ΠΌΠ΅Ρ€Π° k" -- Для этого я Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΈΠ½Π΄ΡƒΠΊΡ†ΠΈΡŽ ΠΏΠΎ ΡƒΠ±Ρ‹Π²Π°Π½ΠΈΡŽ k -- Π’ mathlib это nat.decreasing_induction lemma ind_iff_no_circuit_subset (A) : m.ind A ↔ βˆ€ C βŠ† A, Β¬m.circuit C := begin split, { intros Aind C hCA Ccirc, refine dep_superset hCA (Ccirc.1) Aind, }, { intros h, by_contradiction Aind, suffices subsets_dep : βˆ€ k ≀ A.card, βˆƒ B βŠ† A, B.card = k ∧ Β¬ m.ind B, { obtain ⟨B, Bsub, Bcard, Bdep⟩ := subsets_dep 0 (zero_le _), apply Bdep, convert m.ind_empty, rwa card_eq_zero at Bcard, }, intros k k_le_card, refine nat.decreasing_induction _ k_le_card _, { rintro n ⟨B, hBA, Bcard, Bdep⟩, specialize h B hBA, rw circuit at h, push_neg at h, obtain ⟨x, xAB, h_dep_erase⟩ := h Bdep, refine ⟨B.erase x, subset.trans (erase_subset _ _) hBA, _, h_dep_erase⟩, rw ← card_erase_of_mem' xAB at Bcard, exact add_right_cancel Bcard, }, { use [A, subset.refl _, rfl], } } end lemma dep_iff_contains_circuit (A) : Β¬ m.ind A ↔ βˆƒ C βŠ† A, m.circuit C := begin convert (not_congr $ ind_iff_no_circuit_subset A), simp, end end circuit -- Π•ΡΡ‚ΡŒ нСсколько способов ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Ρ€Π°Π½Π³ΠΎΠ²ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ Π² ΠΌΠ°Ρ‚Ρ€ΠΎΠΈΠ΄Π΅ -- Π― Π²Ρ‹Π±Ρ€Π°Π» ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ: возьмСм всС нСзависимыС подмноТСства A ΠΈ возьмСм ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ ΠΌΠΎΡ‰Π½ΠΎΡΡ‚ΡŒ срСди Π½ΠΈΡ… -- Для Ρ‚Π°ΠΊΠΎΠ³ΠΎ опрСдСлСния Π½ΡƒΠΆΠ½ΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡ‚ΡŒ Π² ΠΌΠ°Ρ‚Ρ€ΠΎΠΈΠ΄Π΅ Π±Ρ‹Π»Π° Ρ€Π°Π·Ρ€Π΅ΡˆΠΈΠΌΠΎΠΉ variable [decidable_pred m.ind] def rank (m : matroid Ξ±) [decidable_pred m.ind] (A : finset Ξ±) : β„• := sup (filter m.ind (powerset A)) card section rank @[simp] lemma rank_def : rank m A = sup (filter m.ind (powerset A)) card := rfl @[simp] lemma rank_empty : rank m βˆ… = 0 := begin rw rank, simp only [card_empty, filter_true_of_mem, ind_empty, forall_eq, powerset_empty, sup_singleton, mem_singleton], end -- Если B - Π±Π°Π·Π° A, Ρ‚ΠΎ Ρ€Π°Π½Π³ А Ρ€Π°Π²Π΅Π½ мощности B -- Π›Π΅ΠΌΠΌΡ‹ ΠΏΡ€ΠΎ `sup` здСсь ΠΏΠΎΠ»Π΅Π·Π½Ρ‹: `finset.sup_le` ΠΈ `le_sup` lemma rank_eq_base_of_card (Bbase : m.base_of A B) : rank m A = B.card := begin rw rank_def, apply nat.le_antisymm, { apply finset.sup_le, intros C hC, rw [mem_filter, mem_powerset] at hC, exact ind_card_le_base_of_card hC.1 hC.2 Bbase, }, { apply le_sup, rw [mem_filter, mem_powerset], exact ⟨base_of_subset Bbase, base_of_ind Bbase⟩, } end lemma rank_exists_base_of (m : matroid Ξ±) [decidable_pred m.ind] (A : finset Ξ±) : βˆƒ (B : finset Ξ±), m.rank A = B.card ∧ m.base_of A B := begin obtain ⟨B, Bbase⟩ := exists_base_of m A, refine ⟨B, rank_eq_base_of_card Bbase, Bbase⟩, end theorem rank_le_card : rank m A ≀ A.card := begin obtain ⟨bA, bAcard, bAbase⟩ := rank_exists_base_of m A, rw bAcard, exact card_le_of_subset bAbase.1, end @[simp] lemma rank_le_of_subset (hAB : A βŠ† B) : rank m A ≀ rank m B := begin obtain ⟨bA, bAbase⟩ := exists_base_of m A, obtain ⟨bB, bBbase⟩ := exists_base_of m B, rw rank_eq_base_of_card bAbase, rw rank_eq_base_of_card bBbase, refine ind_card_le_base_of_card (subset.trans bAbase.1 hAB) bAbase.2.1 bBbase, end @[simp] lemma rank_ind : rank m A = A.card ↔ m.ind A := begin split, { intro h_card, obtain ⟨B, Bcard, Bbase⟩ := rank_exists_base_of m A, rw h_card at Bcard, suffices h_eq : A = B, { subst h_eq, exact Bbase.2.1, }, symmetry, exact eq_of_subset_of_card_le Bbase.1 (le_of_eq Bcard), }, { intro Aind, rw [ind_iff_base_of_self] at Aind, rw rank_eq_base_of_card Aind, } end lemma not_mem_sdiff_iff {x : Ξ±} {A B : finset Ξ±} : x βˆ‰ A \ B ↔ x βˆ‰ A ∨ x ∈ B := begin rw not_iff_comm, push_neg, simp only [mem_sdiff, iff_self], end lemma card_sdiff_β„€ {A B : finset Ξ±} (hAB : A βŠ† B) : ((B \ A).card : β„€) = (B.card : β„€) - A.card := begin suffices h : (B \ A).card + A.card = B.card, { linarith, }, rw [card_sdiff hAB, nat.sub_add_cancel (card_le_of_subset hAB)], end -- Π‘Π»ΠΎΠΆΠ½Ρ‹ΠΉ Ρ„ΠΈΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ босс! -- http://neerc.ifmo.ru/wiki/index.php?title=%D0%A0%D0%B0%D0%BD%D0%B3%D0%BE%D0%B2%D0%B0%D1%8F_%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8F,_%D0%BF%D0%BE%D0%BB%D1%83%D0%BC%D0%BE%D0%B4%D1%83%D0%BB%D1%8F%D1%80%D0%BD%D0%BE%D1%81%D1%82%D1%8C theorem rank_submodular : m.rank (A ∩ B) + m.rank (A βˆͺ B) ≀ m.rank A + m.rank B := begin obtain ⟨bInter, bInter_base⟩ := exists_base_of m (A ∩ B), have rankInter := rank_eq_base_of_card bInter_base, obtain ⟨bA, bA_sub, bA_base⟩ := exists_base_of_superset_of_ind (subset.trans bInter_base.1 (inter_subset_left _ _)) (bInter_base.2.1), have rankA := rank_eq_base_of_card bA_base, obtain ⟨bUnion, bUnion_sub, bUnion_base⟩ := exists_base_of_superset_of_ind (subset.trans bA_base.1 (subset_union_left A B)) bA_base.2.1, have rankUnion := rank_eq_base_of_card bUnion_base, obtain ⟨bB, bB_base⟩ := exists_base_of m B, have rankB := rank_eq_base_of_card bB_base, have indB_sub : (bUnion \ (bA \ bInter)) βŠ† B := by { intros x hx, rw [mem_sdiff, not_mem_sdiff_iff] at hx, rcases hx with ⟨xBunion, xA | xInter⟩, { have xAB := bUnion_base.1 xBunion, cases (mem_union.1 xAB), { exfalso, have h_insert_dep := bA_base.2.2 _ h xA, refine h_insert_dep (m.ind_subset _ (bUnion_base.2.1)), rw ← insert_eq_of_mem xBunion, refine insert_subset_insert x bUnion_sub, }, { exact h, } }, { exact mem_of_mem_inter_right (bInter_base.1 xInter), }, }, have indB_ind : m.ind (bUnion \ (bA \ bInter)) := m.ind_subset (sdiff_subset _ _) bUnion_base.2.1, have indB_card_le := ind_card_le_base_of_card indB_sub indB_ind bB_base, have indB_card : (bUnion \ (bA \ bInter)).card + bA.card = bUnion.card + bInter.card := by { zify, rw card_sdiff_β„€ (subset.trans (sdiff_subset bA bInter) bUnion_sub), rw card_sdiff_β„€ bA_sub, ring, }, rw [rankInter, rankA, rankUnion, rankB], linarith, end end rank end matroid
014208cb96a653d15f6f56cab50a14701be9f62e
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/3-5.lean
1b8085565b5d34dbebd20a440430f08ac7cff467
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
195
lean
open classical variable p: Prop #check em p theorem dne {p: Prop} (h: Β¬ Β¬ p) : p := (em p).elim (assume hp: p, hp) (assume hnp: Β¬ p, absurd hnp h) #check by_cases
ed3354917836e6f26d711abf9abb94a58e0f2684
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/sum/order.lean
fa415e8e8a7f4c698d62ce79a44e224e45f6688b
[ "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,237
lean
/- Copyright (c) 2021 YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies -/ import order.hom.basic /-! # Orders on a sum type This file defines the disjoint sum and the linear (aka lexicographic) sum of two orders and provides relation instances for `sum.lift_rel` and `sum.lex`. We declare the disjoint sum of orders as the default set of instances. The linear order goes on a type synonym. ## Main declarations * `sum.has_le`, `sum.has_lt`: Disjoint sum of orders. * `sum.lex.has_le`, `sum.lex.has_lt`: Lexicographic/linear sum of orders. ## Notation * `Ξ± βŠ•β‚— Ξ²`: The linear sum of `Ξ±` and `Ξ²`. -/ variables {Ξ± Ξ² Ξ³ Ξ΄ : Type*} namespace sum /-! ### Unbundled relation classes -/ section lift_rel variables (r : Ξ± β†’ Ξ± β†’ Prop) (s : Ξ² β†’ Ξ² β†’ Prop) @[refl] lemma lift_rel.refl [is_refl Ξ± r] [is_refl Ξ² s] : βˆ€ x, lift_rel r s x x | (inl a) := lift_rel.inl (refl _) | (inr a) := lift_rel.inr (refl _) instance [is_refl Ξ± r] [is_refl Ξ² s] : is_refl (Ξ± βŠ• Ξ²) (lift_rel r s) := ⟨lift_rel.refl _ _⟩ instance [is_irrefl Ξ± r] [is_irrefl Ξ² s] : is_irrefl (Ξ± βŠ• Ξ²) (lift_rel r s) := ⟨by { rintro _ (⟨a, _, h⟩ | ⟨a, _, h⟩); exact irrefl _ h }⟩ @[trans] lemma lift_rel.trans [is_trans Ξ± r] [is_trans Ξ² s] : βˆ€ {a b c}, lift_rel r s a b β†’ lift_rel r s b c β†’ lift_rel r s a c | _ _ _ (lift_rel.inl hab) (lift_rel.inl hbc) := lift_rel.inl $ trans hab hbc | _ _ _ (lift_rel.inr hab) (lift_rel.inr hbc) := lift_rel.inr $ trans hab hbc instance [is_trans Ξ± r] [is_trans Ξ² s] : is_trans (Ξ± βŠ• Ξ²) (lift_rel r s) := ⟨λ _ _ _, lift_rel.trans _ _⟩ instance [is_antisymm Ξ± r] [is_antisymm Ξ² s] : is_antisymm (Ξ± βŠ• Ξ²) (lift_rel r s) := ⟨by { rintro _ _ (⟨a, b, hab⟩ | ⟨a, b, hab⟩) (⟨_, _, hba⟩ | ⟨_, _, hba⟩); rw antisymm hab hba }⟩ end lift_rel section lex variables (r : Ξ± β†’ Ξ± β†’ Prop) (s : Ξ² β†’ Ξ² β†’ Prop) instance [is_refl Ξ± r] [is_refl Ξ² s] : is_refl (Ξ± βŠ• Ξ²) (lex r s) := ⟨by { rintro (a | a), exacts [lex.inl (refl _), lex.inr (refl _)] }⟩ instance [is_irrefl Ξ± r] [is_irrefl Ξ² s] : is_irrefl (Ξ± βŠ• Ξ²) (lex r s) := ⟨by { rintro _ (⟨a, _, h⟩ | ⟨a, _, h⟩); exact irrefl _ h }⟩ instance [is_trans Ξ± r] [is_trans Ξ² s] : is_trans (Ξ± βŠ• Ξ²) (lex r s) := ⟨by { rintro _ _ _ (⟨a, b, hab⟩ | ⟨a, b, hab⟩) (⟨_, c, hbc⟩ | ⟨_, c, hbc⟩), exacts [lex.inl (trans hab hbc), lex.sep _ _, lex.inr (trans hab hbc), lex.sep _ _] }⟩ instance [is_antisymm Ξ± r] [is_antisymm Ξ² s] : is_antisymm (Ξ± βŠ• Ξ²) (lex r s) := ⟨by { rintro _ _ (⟨a, b, hab⟩ | ⟨a, b, hab⟩) (⟨_, _, hba⟩ | ⟨_, _, hba⟩); rw antisymm hab hba }⟩ instance [is_total Ξ± r] [is_total Ξ² s] : is_total (Ξ± βŠ• Ξ²) (lex r s) := ⟨λ a b, match a, b with | inl a, inl b := (total_of r a b).imp lex.inl lex.inl | inl a, inr b := or.inl (lex.sep _ _) | inr a, inl b := or.inr (lex.sep _ _) | inr a, inr b := (total_of s a b).imp lex.inr lex.inr end⟩ instance [is_trichotomous Ξ± r] [is_trichotomous Ξ² s] : is_trichotomous (Ξ± βŠ• Ξ²) (lex r s) := ⟨λ a b, match a, b with | inl a, inl b := (trichotomous_of r a b).imp3 lex.inl (congr_arg _) lex.inl | inl a, inr b := or.inl (lex.sep _ _) | inr a, inl b := or.inr (or.inr $ lex.sep _ _) | inr a, inr b := (trichotomous_of s a b).imp3 lex.inr (congr_arg _) lex.inr end⟩ instance [is_well_order Ξ± r] [is_well_order Ξ² s] : is_well_order (Ξ± βŠ• Ξ²) (sum.lex r s) := { wf := sum.lex_wf is_well_founded.wf is_well_founded.wf } end lex /-! ### Disjoint sum of two orders -/ section disjoint instance [has_le Ξ±] [has_le Ξ²] : has_le (Ξ± βŠ• Ξ²) := ⟨lift_rel (≀) (≀)⟩ instance [has_lt Ξ±] [has_lt Ξ²] : has_lt (Ξ± βŠ• Ξ²) := ⟨lift_rel (<) (<)⟩ lemma le_def [has_le Ξ±] [has_le Ξ²] {a b : Ξ± βŠ• Ξ²} : a ≀ b ↔ lift_rel (≀) (≀) a b := iff.rfl lemma lt_def [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ± βŠ• Ξ²} : a < b ↔ lift_rel (<) (<) a b := iff.rfl @[simp] lemma inl_le_inl_iff [has_le Ξ±] [has_le Ξ²] {a b : Ξ±} : (inl a : Ξ± βŠ• Ξ²) ≀ inl b ↔ a ≀ b := lift_rel_inl_inl @[simp] lemma inr_le_inr_iff [has_le Ξ±] [has_le Ξ²] {a b : Ξ²} : (inr a : Ξ± βŠ• Ξ²) ≀ inr b ↔ a ≀ b := lift_rel_inr_inr @[simp] lemma inl_lt_inl_iff [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ±} : (inl a : Ξ± βŠ• Ξ²) < inl b ↔ a < b := lift_rel_inl_inl @[simp] lemma inr_lt_inr_iff [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ²} : (inr a : Ξ± βŠ• Ξ²) < inr b ↔ a < b := lift_rel_inr_inr @[simp] lemma not_inl_le_inr [has_le Ξ±] [has_le Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ inl b ≀ inr a := not_lift_rel_inl_inr @[simp] lemma not_inl_lt_inr [has_lt Ξ±] [has_lt Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ inl b < inr a := not_lift_rel_inl_inr @[simp] lemma not_inr_le_inl [has_le Ξ±] [has_le Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ inr b ≀ inl a := not_lift_rel_inr_inl @[simp] lemma not_inr_lt_inl [has_lt Ξ±] [has_lt Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ inr b < inl a := not_lift_rel_inr_inl section preorder variables [preorder Ξ±] [preorder Ξ²] instance : preorder (Ξ± βŠ• Ξ²) := { le_refl := Ξ» _, refl _, le_trans := Ξ» _ _ _, trans, lt_iff_le_not_le := Ξ» a b, begin refine ⟨λ hab, ⟨hab.mono (Ξ» _ _, le_of_lt) (Ξ» _ _, le_of_lt), _⟩, _⟩, { rintro (⟨b, a, hba⟩ | ⟨b, a, hba⟩), { exact hba.not_lt (inl_lt_inl_iff.1 hab) }, { exact hba.not_lt (inr_lt_inr_iff.1 hab) } }, { rintro ⟨⟨a, b, hab⟩ | ⟨a, b, hab⟩, hba⟩, { exact lift_rel.inl (hab.lt_of_not_le $ Ξ» h, hba $ lift_rel.inl h) }, { exact lift_rel.inr (hab.lt_of_not_le $ Ξ» h, hba $ lift_rel.inr h) } } end, .. sum.has_le, .. sum.has_lt } lemma inl_mono : monotone (inl : Ξ± β†’ Ξ± βŠ• Ξ²) := Ξ» a b, lift_rel.inl lemma inr_mono : monotone (inr : Ξ² β†’ Ξ± βŠ• Ξ²) := Ξ» a b, lift_rel.inr lemma inl_strict_mono : strict_mono (inl : Ξ± β†’ Ξ± βŠ• Ξ²) := Ξ» a b, lift_rel.inl lemma inr_strict_mono : strict_mono (inr : Ξ² β†’ Ξ± βŠ• Ξ²) := Ξ» a b, lift_rel.inr end preorder instance [partial_order Ξ±] [partial_order Ξ²] : partial_order (Ξ± βŠ• Ξ²) := { le_antisymm := Ξ» _ _, antisymm, .. sum.preorder } instance no_min_order [has_lt Ξ±] [has_lt Ξ²] [no_min_order Ξ±] [no_min_order Ξ²] : no_min_order (Ξ± βŠ• Ξ²) := ⟨λ a, match a with | inl a := let ⟨b, h⟩ := exists_lt a in ⟨inl b, inl_lt_inl_iff.2 h⟩ | inr a := let ⟨b, h⟩ := exists_lt a in ⟨inr b, inr_lt_inr_iff.2 h⟩ end⟩ instance no_max_order [has_lt Ξ±] [has_lt Ξ²] [no_max_order Ξ±] [no_max_order Ξ²] : no_max_order (Ξ± βŠ• Ξ²) := ⟨λ a, match a with | inl a := let ⟨b, h⟩ := exists_gt a in ⟨inl b, inl_lt_inl_iff.2 h⟩ | inr a := let ⟨b, h⟩ := exists_gt a in ⟨inr b, inr_lt_inr_iff.2 h⟩ end⟩ @[simp] lemma no_min_order_iff [has_lt Ξ±] [has_lt Ξ²] : no_min_order (Ξ± βŠ• Ξ²) ↔ no_min_order Ξ± ∧ no_min_order Ξ² := ⟨λ _, by exactI ⟨⟨λ a, begin obtain ⟨b | b, h⟩ := exists_lt (inl a : Ξ± βŠ• Ξ²), { exact ⟨b, inl_lt_inl_iff.1 h⟩ }, { exact (not_inr_lt_inl h).elim } end⟩, ⟨λ a, begin obtain ⟨b | b, h⟩ := exists_lt (inr a : Ξ± βŠ• Ξ²), { exact (not_inl_lt_inr h).elim }, { exact ⟨b, inr_lt_inr_iff.1 h⟩ } end⟩⟩, Ξ» h, @sum.no_min_order _ _ _ _ h.1 h.2⟩ @[simp] lemma no_max_order_iff [has_lt Ξ±] [has_lt Ξ²] : no_max_order (Ξ± βŠ• Ξ²) ↔ no_max_order Ξ± ∧ no_max_order Ξ² := ⟨λ _, by exactI ⟨⟨λ a, begin obtain ⟨b | b, h⟩ := exists_gt (inl a : Ξ± βŠ• Ξ²), { exact ⟨b, inl_lt_inl_iff.1 h⟩ }, { exact (not_inl_lt_inr h).elim } end⟩, ⟨λ a, begin obtain ⟨b | b, h⟩ := exists_gt (inr a : Ξ± βŠ• Ξ²), { exact (not_inr_lt_inl h).elim }, { exact ⟨b, inr_lt_inr_iff.1 h⟩ } end⟩⟩, Ξ» h, @sum.no_max_order _ _ _ _ h.1 h.2⟩ instance densely_ordered [has_lt Ξ±] [has_lt Ξ²] [densely_ordered Ξ±] [densely_ordered Ξ²] : densely_ordered (Ξ± βŠ• Ξ²) := ⟨λ a b h, match a, b, h with | inl a, inl b, lift_rel.inl h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inl c), lift_rel.inl ha, lift_rel.inl hb⟩ | inr a, inr b, lift_rel.inr h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inr c), lift_rel.inr ha, lift_rel.inr hb⟩ end⟩ @[simp] lemma densely_ordered_iff [has_lt Ξ±] [has_lt Ξ²] : densely_ordered (Ξ± βŠ• Ξ²) ↔ densely_ordered Ξ± ∧ densely_ordered Ξ² := ⟨λ _, by exactI ⟨⟨λ a b h, begin obtain ⟨c | c, ha, hb⟩ := @exists_between (Ξ± βŠ• Ξ²) _ _ _ _ (inl_lt_inl_iff.2 h), { exact ⟨c, inl_lt_inl_iff.1 ha, inl_lt_inl_iff.1 hb⟩ }, { exact (not_inl_lt_inr ha).elim } end⟩, ⟨λ a b h, begin obtain ⟨c | c, ha, hb⟩ := @exists_between (Ξ± βŠ• Ξ²) _ _ _ _ (inr_lt_inr_iff.2 h), { exact (not_inl_lt_inr hb).elim }, { exact ⟨c, inr_lt_inr_iff.1 ha, inr_lt_inr_iff.1 hb⟩ } end⟩⟩, Ξ» h, @sum.densely_ordered _ _ _ _ h.1 h.2⟩ @[simp] lemma swap_le_swap_iff [has_le Ξ±] [has_le Ξ²] {a b : Ξ± βŠ• Ξ²} : a.swap ≀ b.swap ↔ a ≀ b := lift_rel_swap_iff @[simp] lemma swap_lt_swap_iff [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ± βŠ• Ξ²} : a.swap < b.swap ↔ a < b := lift_rel_swap_iff end disjoint /-! ### Linear sum of two orders -/ namespace lex notation Ξ± ` βŠ•β‚— `:30 Ξ²:29 := _root_.lex (Ξ± βŠ• Ξ²) --TODO: Can we make `inlβ‚—`, `inrβ‚—` `local notation`? /-- Lexicographical `sum.inl`. Only used for pattern matching. -/ @[pattern] abbreviation _root_.sum.inlβ‚— (x : Ξ±) : Ξ± βŠ•β‚— Ξ² := to_lex (sum.inl x) /-- Lexicographical `sum.inr`. Only used for pattern matching. -/ @[pattern] abbreviation _root_.sum.inrβ‚— (x : Ξ²) : Ξ± βŠ•β‚— Ξ² := to_lex (sum.inr x) /-- The linear/lexicographical `≀` on a sum. -/ instance has_le [has_le Ξ±] [has_le Ξ²] : has_le (Ξ± βŠ•β‚— Ξ²) := ⟨lex (≀) (≀)⟩ /-- The linear/lexicographical `<` on a sum. -/ instance has_lt [has_lt Ξ±] [has_lt Ξ²] : has_lt (Ξ± βŠ•β‚— Ξ²) := ⟨lex (<) (<)⟩ @[simp] lemma to_lex_le_to_lex [has_le Ξ±] [has_le Ξ²] {a b : Ξ± βŠ• Ξ²} : to_lex a ≀ to_lex b ↔ lex (≀) (≀) a b := iff.rfl @[simp] lemma to_lex_lt_to_lex [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ± βŠ• Ξ²} : to_lex a < to_lex b ↔ lex (<) (<) a b := iff.rfl lemma le_def [has_le Ξ±] [has_le Ξ²] {a b : Ξ± βŠ•β‚— Ξ²} : a ≀ b ↔ lex (≀) (≀) (of_lex a) (of_lex b) := iff.rfl lemma lt_def [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ± βŠ•β‚— Ξ²} : a < b ↔ lex (<) (<) (of_lex a) (of_lex b) := iff.rfl @[simp] lemma inl_le_inl_iff [has_le Ξ±] [has_le Ξ²] {a b : Ξ±} : to_lex (inl a : Ξ± βŠ• Ξ²) ≀ to_lex (inl b) ↔ a ≀ b := lex_inl_inl @[simp] lemma inr_le_inr_iff [has_le Ξ±] [has_le Ξ²] {a b : Ξ²} : to_lex (inr a : Ξ± βŠ• Ξ²) ≀ to_lex (inr b) ↔ a ≀ b := lex_inr_inr @[simp] lemma inl_lt_inl_iff [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ±} : to_lex (inl a : Ξ± βŠ• Ξ²) < to_lex (inl b) ↔ a < b := lex_inl_inl @[simp] lemma inr_lt_inr_iff [has_lt Ξ±] [has_lt Ξ²] {a b : Ξ²} : to_lex (inr a : Ξ± βŠ•β‚— Ξ²) < to_lex (inr b) ↔ a < b := lex_inr_inr @[simp] lemma inl_le_inr [has_le Ξ±] [has_le Ξ²] (a : Ξ±) (b : Ξ²) : to_lex (inl a) ≀ to_lex (inr b) := lex.sep _ _ @[simp] lemma inl_lt_inr [has_lt Ξ±] [has_lt Ξ²] (a : Ξ±) (b : Ξ²) : to_lex (inl a) < to_lex (inr b) := lex.sep _ _ @[simp] lemma not_inr_le_inl [has_le Ξ±] [has_le Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ to_lex (inr b) ≀ to_lex (inl a) := lex_inr_inl @[simp] lemma not_inr_lt_inl [has_lt Ξ±] [has_lt Ξ²] {a : Ξ±} {b : Ξ²} : Β¬ to_lex (inr b) < to_lex (inl a) := lex_inr_inl section preorder variables [preorder Ξ±] [preorder Ξ²] instance preorder : preorder (Ξ± βŠ•β‚— Ξ²) := { le_refl := refl_of (lex (≀) (≀)), le_trans := Ξ» _ _ _, trans_of (lex (≀) (≀)), lt_iff_le_not_le := Ξ» a b, begin refine ⟨λ hab, ⟨hab.mono (Ξ» _ _, le_of_lt) (Ξ» _ _, le_of_lt), _⟩, _⟩, { rintro (⟨b, a, hba⟩ | ⟨b, a, hba⟩ | ⟨b, a⟩), { exact hba.not_lt (inl_lt_inl_iff.1 hab) }, { exact hba.not_lt (inr_lt_inr_iff.1 hab) }, { exact not_inr_lt_inl hab } }, { rintro ⟨⟨a, b, hab⟩ | ⟨a, b, hab⟩ | ⟨a, b⟩, hba⟩, { exact lex.inl (hab.lt_of_not_le $ Ξ» h, hba $ lex.inl h) }, { exact lex.inr (hab.lt_of_not_le $ Ξ» h, hba $ lex.inr h) }, { exact lex.sep _ _} } end, .. lex.has_le, .. lex.has_lt } lemma to_lex_mono : monotone (@to_lex (Ξ± βŠ• Ξ²)) := Ξ» a b h, h.lex lemma to_lex_strict_mono : strict_mono (@to_lex (Ξ± βŠ• Ξ²)) := Ξ» a b h, h.lex lemma inl_mono : monotone (to_lex ∘ inl : Ξ± β†’ Ξ± βŠ•β‚— Ξ²) := to_lex_mono.comp inl_mono lemma inr_mono : monotone (to_lex ∘ inr : Ξ² β†’ Ξ± βŠ•β‚— Ξ²) := to_lex_mono.comp inr_mono lemma inl_strict_mono : strict_mono (to_lex ∘ inl : Ξ± β†’ Ξ± βŠ•β‚— Ξ²) := to_lex_strict_mono.comp inl_strict_mono lemma inr_strict_mono : strict_mono (to_lex ∘ inr : Ξ² β†’ Ξ± βŠ•β‚— Ξ²) := to_lex_strict_mono.comp inr_strict_mono end preorder instance partial_order [partial_order Ξ±] [partial_order Ξ²] : partial_order (Ξ± βŠ•β‚— Ξ²) := { le_antisymm := Ξ» _ _, antisymm_of (lex (≀) (≀)), .. lex.preorder } instance linear_order [linear_order Ξ±] [linear_order Ξ²] : linear_order (Ξ± βŠ•β‚— Ξ²) := { le_total := total_of (lex (≀) (≀)), decidable_le := lex.decidable_rel, decidable_eq := sum.decidable_eq _ _, .. lex.partial_order } /-- The lexicographical bottom of a sum is the bottom of the left component. -/ instance order_bot [has_le Ξ±] [order_bot Ξ±] [has_le Ξ²] : order_bot (Ξ± βŠ•β‚— Ξ²) := { bot := inl βŠ₯, bot_le := begin rintro (a | b), { exact lex.inl bot_le }, { exact lex.sep _ _ } end } @[simp] lemma inl_bot [has_le Ξ±] [order_bot Ξ±] [has_le Ξ²]: to_lex (inl βŠ₯ : Ξ± βŠ• Ξ²) = βŠ₯ := rfl /-- The lexicographical top of a sum is the top of the right component. -/ instance order_top [has_le Ξ±] [has_le Ξ²] [order_top Ξ²] : order_top (Ξ± βŠ•β‚— Ξ²) := { top := inr ⊀, le_top := begin rintro (a | b), { exact lex.sep _ _ }, { exact lex.inr le_top } end } @[simp] lemma inr_top [has_le Ξ±] [has_le Ξ²] [order_top Ξ²] : to_lex (inr ⊀ : Ξ± βŠ• Ξ²) = ⊀ := rfl instance bounded_order [has_le Ξ±] [has_le Ξ²] [order_bot Ξ±] [order_top Ξ²] : bounded_order (Ξ± βŠ•β‚— Ξ²) := { .. lex.order_bot, .. lex.order_top } instance no_min_order [has_lt Ξ±] [has_lt Ξ²] [no_min_order Ξ±] [no_min_order Ξ²] : no_min_order (Ξ± βŠ•β‚— Ξ²) := ⟨λ a, match a with | inl a := let ⟨b, h⟩ := exists_lt a in ⟨to_lex (inl b), inl_lt_inl_iff.2 h⟩ | inr a := let ⟨b, h⟩ := exists_lt a in ⟨to_lex (inr b), inr_lt_inr_iff.2 h⟩ end⟩ instance no_max_order [has_lt Ξ±] [has_lt Ξ²] [no_max_order Ξ±] [no_max_order Ξ²] : no_max_order (Ξ± βŠ•β‚— Ξ²) := ⟨λ a, match a with | inl a := let ⟨b, h⟩ := exists_gt a in ⟨to_lex (inl b), inl_lt_inl_iff.2 h⟩ | inr a := let ⟨b, h⟩ := exists_gt a in ⟨to_lex (inr b), inr_lt_inr_iff.2 h⟩ end⟩ instance no_min_order_of_nonempty [has_lt Ξ±] [has_lt Ξ²] [no_min_order Ξ±] [nonempty Ξ±] : no_min_order (Ξ± βŠ•β‚— Ξ²) := ⟨λ a, match a with | inl a := let ⟨b, h⟩ := exists_lt a in ⟨to_lex (inl b), inl_lt_inl_iff.2 h⟩ | inr a := ⟨to_lex (inl $ classical.arbitrary Ξ±), inl_lt_inr _ _⟩ end⟩ instance no_max_order_of_nonempty [has_lt Ξ±] [has_lt Ξ²] [no_max_order Ξ²] [nonempty Ξ²] : no_max_order (Ξ± βŠ•β‚— Ξ²) := ⟨λ a, match a with | inl a := ⟨to_lex (inr $ classical.arbitrary Ξ²), inl_lt_inr _ _⟩ | inr a := let ⟨b, h⟩ := exists_gt a in ⟨to_lex (inr b), inr_lt_inr_iff.2 h⟩ end⟩ instance densely_ordered_of_no_max_order [has_lt Ξ±] [has_lt Ξ²] [densely_ordered Ξ±] [densely_ordered Ξ²] [no_max_order Ξ±] : densely_ordered (Ξ± βŠ•β‚— Ξ²) := ⟨λ a b h, match a, b, h with | inl a, inl b, lex.inl h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inl c), inl_lt_inl_iff.2 ha, inl_lt_inl_iff.2 hb⟩ | inl a, inr b, lex.sep _ _ := let ⟨c, h⟩ := exists_gt a in ⟨to_lex (inl c), inl_lt_inl_iff.2 h, inl_lt_inr _ _⟩ | inr a, inr b, lex.inr h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inr c), inr_lt_inr_iff.2 ha, inr_lt_inr_iff.2 hb⟩ end⟩ instance densely_ordered_of_no_min_order [has_lt Ξ±] [has_lt Ξ²] [densely_ordered Ξ±] [densely_ordered Ξ²] [no_min_order Ξ²] : densely_ordered (Ξ± βŠ•β‚— Ξ²) := ⟨λ a b h, match a, b, h with | inl a, inl b, lex.inl h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inl c), inl_lt_inl_iff.2 ha, inl_lt_inl_iff.2 hb⟩ | inl a, inr b, lex.sep _ _ := let ⟨c, h⟩ := exists_lt b in ⟨to_lex (inr c), inl_lt_inr _ _, inr_lt_inr_iff.2 h⟩ | inr a, inr b, lex.inr h := let ⟨c, ha, hb⟩ := exists_between h in ⟨to_lex (inr c), inr_lt_inr_iff.2 ha, inr_lt_inr_iff.2 hb⟩ end⟩ end lex end sum /-! ### Order isomorphisms -/ open order_dual sum namespace order_iso variables [has_le Ξ±] [has_le Ξ²] [has_le Ξ³] (a : Ξ±) (b : Ξ²) (c : Ξ³) /-- `equiv.sum_comm` promoted to an order isomorphism. -/ @[simps apply] def sum_comm (Ξ± Ξ² : Type*) [has_le Ξ±] [has_le Ξ²] : Ξ± βŠ• Ξ² ≃o Ξ² βŠ• Ξ± := { map_rel_iff' := Ξ» a b, swap_le_swap_iff, ..equiv.sum_comm Ξ± Ξ² } @[simp] lemma sum_comm_symm (Ξ± Ξ² : Type*) [has_le Ξ±] [has_le Ξ²] : (order_iso.sum_comm Ξ± Ξ²).symm = order_iso.sum_comm Ξ² Ξ± := rfl /-- `equiv.sum_assoc` promoted to an order isomorphism. -/ def sum_assoc (Ξ± Ξ² Ξ³ : Type*) [has_le Ξ±] [has_le Ξ²] [has_le Ξ³] : (Ξ± βŠ• Ξ²) βŠ• Ξ³ ≃o Ξ± βŠ• Ξ² βŠ• Ξ³ := { map_rel_iff' := by { rintro ((a | a) | a) ((b | b) | b); simp }, ..equiv.sum_assoc Ξ± Ξ² Ξ³ } @[simp] lemma sum_assoc_apply_inl_inl : sum_assoc Ξ± Ξ² Ξ³ (inl (inl a)) = inl a := rfl @[simp] lemma sum_assoc_apply_inl_inr : sum_assoc Ξ± Ξ² Ξ³ (inl (inr b)) = inr (inl b) := rfl @[simp] lemma sum_assoc_apply_inr : sum_assoc Ξ± Ξ² Ξ³ (inr c) = inr (inr c) := rfl @[simp] lemma sum_assoc_symm_apply_inl : (sum_assoc Ξ± Ξ² Ξ³).symm (inl a) = inl (inl a) := rfl @[simp] lemma sum_assoc_symm_apply_inr_inl : (sum_assoc Ξ± Ξ² Ξ³).symm (inr (inl b)) = inl (inr b) := rfl @[simp] lemma sum_assoc_symm_apply_inr_inr : (sum_assoc Ξ± Ξ² Ξ³).symm (inr (inr c)) = inr c := rfl /-- `order_dual` is distributive over `βŠ•` up to an order isomorphism. -/ def sum_dual_distrib (Ξ± Ξ² : Type*) [has_le Ξ±] [has_le Ξ²] : (Ξ± βŠ• Ξ²)α΅’α΅ˆ ≃o Ξ±α΅’α΅ˆ βŠ• Ξ²α΅’α΅ˆ := { map_rel_iff' := begin rintro (a | a) (b | b), { change inl (to_dual a) ≀ inl (to_dual b) ↔ to_dual (inl a) ≀ to_dual (inl b), simp only [to_dual_le_to_dual, inl_le_inl_iff] }, { exact iff_of_false not_inl_le_inr not_inr_le_inl }, { exact iff_of_false not_inr_le_inl not_inl_le_inr }, { change inr (to_dual a) ≀ inr (to_dual b) ↔ to_dual (inr a) ≀ to_dual (inr b), simp only [to_dual_le_to_dual, inr_le_inr_iff] } end, ..equiv.refl _ } @[simp] lemma sum_dual_distrib_inl : sum_dual_distrib Ξ± Ξ² (to_dual (inl a)) = inl (to_dual a) := rfl @[simp] lemma sum_dual_distrib_inr : sum_dual_distrib Ξ± Ξ² (to_dual (inr b)) = inr (to_dual b) := rfl @[simp] lemma sum_dual_distrib_symm_inl : (sum_dual_distrib Ξ± Ξ²).symm (inl (to_dual a)) = to_dual (inl a) := rfl @[simp] lemma sum_dual_distrib_symm_inr : (sum_dual_distrib Ξ± Ξ²).symm (inr (to_dual b)) = to_dual (inr b) := rfl /-- `equiv.sum_assoc` promoted to an order isomorphism. -/ def sum_lex_assoc (Ξ± Ξ² Ξ³ : Type*) [has_le Ξ±] [has_le Ξ²] [has_le Ξ³] : (Ξ± βŠ•β‚— Ξ²) βŠ•β‚— Ξ³ ≃o Ξ± βŠ•β‚— Ξ² βŠ•β‚— Ξ³ := { map_rel_iff' := Ξ» a b, ⟨λ h, match a, b, h with | inlβ‚— (inlβ‚— a), inlβ‚— (inlβ‚— b), lex.inl h := lex.inl $ lex.inl h | inlβ‚— (inlβ‚— a), inlβ‚— (inrβ‚— b), lex.sep _ _ := lex.inl $ lex.sep _ _ | inlβ‚— (inlβ‚— a), inrβ‚— b, lex.sep _ _ := lex.sep _ _ | inlβ‚— (inrβ‚— a), inlβ‚— (inrβ‚— b), lex.inr (lex.inl h) := lex.inl $ lex.inr h | inlβ‚— (inrβ‚— a), inrβ‚— b, lex.inr (lex.sep _ _) := lex.sep _ _ | inrβ‚— a, inrβ‚— b, lex.inr (lex.inr h) := lex.inr h end, Ξ» h, match a, b, h with | inlβ‚— (inlβ‚— a), inlβ‚— (inlβ‚— b), lex.inl (lex.inl h) := lex.inl h | inlβ‚— (inlβ‚— a), inlβ‚— (inrβ‚— b), lex.inl (lex.sep _ _) := lex.sep _ _ | inlβ‚— (inlβ‚— a), inrβ‚— b, lex.sep _ _ := lex.sep _ _ | inlβ‚— (inrβ‚— a), inlβ‚— (inrβ‚— b), lex.inl (lex.inr h) := lex.inr $ lex.inl h | inlβ‚— (inrβ‚— a), inrβ‚— b, lex.sep _ _ := lex.inr $ lex.sep _ _ | inrβ‚— a, inrβ‚— b, lex.inr h := lex.inr $ lex.inr h end⟩, ..equiv.sum_assoc Ξ± Ξ² Ξ³ } @[simp] lemma sum_lex_assoc_apply_inl_inl : sum_lex_assoc Ξ± Ξ² Ξ³ (to_lex $ inl $ to_lex $ inl a) = to_lex (inl a) := rfl @[simp] lemma sum_lex_assoc_apply_inl_inr : sum_lex_assoc Ξ± Ξ² Ξ³ (to_lex $ inl $ to_lex $ inr b) = to_lex (inr $ to_lex $ inl b) := rfl @[simp] lemma sum_lex_assoc_apply_inr : sum_lex_assoc Ξ± Ξ² Ξ³ (to_lex $ inr c) = to_lex (inr $ to_lex $ inr c) := rfl @[simp] lemma sum_lex_assoc_symm_apply_inl : (sum_lex_assoc Ξ± Ξ² Ξ³).symm (inl a) = inl (inl a) := rfl @[simp] lemma sum_lex_assoc_symm_apply_inr_inl : (sum_lex_assoc Ξ± Ξ² Ξ³).symm (inr (inl b)) = inl (inr b) := rfl @[simp] lemma sum_lex_assoc_symm_apply_inr_inr : (sum_lex_assoc Ξ± Ξ² Ξ³).symm (inr (inr c)) = inr c := rfl /-- `order_dual` is antidistributive over `βŠ•β‚—` up to an order isomorphism. -/ def sum_lex_dual_antidistrib (Ξ± Ξ² : Type*) [has_le Ξ±] [has_le Ξ²] : (Ξ± βŠ•β‚— Ξ²)α΅’α΅ˆ ≃o Ξ²α΅’α΅ˆ βŠ•β‚— Ξ±α΅’α΅ˆ := { map_rel_iff' := begin rintro (a | a) (b | b), simp, { change to_lex (inr $ to_dual a) ≀ to_lex (inr $ to_dual b) ↔ to_dual (to_lex $ inl a) ≀ to_dual (to_lex $ inl b), simp only [to_dual_le_to_dual, lex.inl_le_inl_iff, lex.inr_le_inr_iff] }, { exact iff_of_false lex.not_inr_le_inl lex.not_inr_le_inl }, { exact iff_of_true (lex.inl_le_inr _ _) (lex.inl_le_inr _ _) }, { change to_lex (inl $ to_dual a) ≀ to_lex (inl $ to_dual b) ↔ to_dual (to_lex $ inr a) ≀ to_dual (to_lex $ inr b), simp only [to_dual_le_to_dual, lex.inl_le_inl_iff, lex.inr_le_inr_iff] } end, ..equiv.sum_comm Ξ± Ξ² } @[simp] lemma sum_lex_dual_antidistrib_inl : sum_lex_dual_antidistrib Ξ± Ξ² (to_dual (inl a)) = inr (to_dual a) := rfl @[simp] lemma sum_lex_dual_antidistrib_inr : sum_lex_dual_antidistrib Ξ± Ξ² (to_dual (inr b)) = inl (to_dual b) := rfl @[simp] lemma sum_lex_dual_antidistrib_symm_inl : (sum_lex_dual_antidistrib Ξ± Ξ²).symm (inl (to_dual b)) = to_dual (inr b) := rfl @[simp] lemma sum_lex_dual_antidistrib_symm_inr : (sum_lex_dual_antidistrib Ξ± Ξ²).symm (inr (to_dual a)) = to_dual (inl a) := rfl end order_iso variable [has_le Ξ±] namespace with_bot /-- `with_bot Ξ±` is order-isomorphic to `punit βŠ•β‚— Ξ±`, by sending `βŠ₯` to `punit.star` and `↑a` to `a`. -/ def order_iso_punit_sum_lex : with_bot Ξ± ≃o punit βŠ•β‚— Ξ± := ⟨(equiv.option_equiv_sum_punit Ξ±).trans $ (equiv.sum_comm _ _).trans to_lex, by rintro (a | _) (b | _); simp; exact not_coe_le_bot _⟩ @[simp] lemma order_iso_punit_sum_lex_bot : @order_iso_punit_sum_lex Ξ± _ βŠ₯ = to_lex (inl punit.star) := rfl @[simp] lemma order_iso_punit_sum_lex_coe (a : Ξ±) : order_iso_punit_sum_lex (↑a) = to_lex (inr a) := rfl @[simp] lemma order_iso_punit_sum_lex_symm_inl (x : punit) : (@order_iso_punit_sum_lex Ξ± _).symm (to_lex $ inl x) = βŠ₯ := rfl @[simp] lemma order_iso_punit_sum_lex_symm_inr (a : Ξ±) : order_iso_punit_sum_lex.symm (to_lex $ inr a) = a := rfl end with_bot namespace with_top /-- `with_top Ξ±` is order-isomorphic to `Ξ± βŠ•β‚— punit`, by sending `⊀` to `punit.star` and `↑a` to `a`. -/ def order_iso_sum_lex_punit : with_top Ξ± ≃o Ξ± βŠ•β‚— punit := ⟨(equiv.option_equiv_sum_punit Ξ±).trans to_lex, by rintro (a | _) (b | _); simp; exact not_top_le_coe _⟩ @[simp] lemma order_iso_sum_lex_punit_top : @order_iso_sum_lex_punit Ξ± _ ⊀ = to_lex (inr punit.star) := rfl @[simp] lemma order_iso_sum_lex_punit_coe (a : Ξ±) : order_iso_sum_lex_punit (↑a) = to_lex (inl a) := rfl @[simp] lemma order_iso_sum_lex_punit_symm_inr (x : punit) : (@order_iso_sum_lex_punit Ξ± _).symm (to_lex $ inr x) = ⊀ := rfl @[simp] lemma order_iso_sum_lex_punit_symm_inl (a : Ξ±) : order_iso_sum_lex_punit.symm (to_lex $ inl a) = a := rfl end with_top
58c826af0d58c248a7daee15075bca28b89cb4e9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/manifold/algebra/lie_group.lean
7866fc867b9a0f1065f3206f9e46d6e0cd533adf
[ "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
6,113
lean
/- Copyright Β© 2020 NicolΓ² Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: NicolΓ² Cavalleri -/ import geometry.manifold.algebra.monoid /-! # Lie groups A Lie group is a group that is also a smooth manifold, in which the group operations of multiplication and inversion are smooth maps. Smoothness of the group multiplication means that multiplication is a smooth mapping of the product manifold `G` Γ— `G` into `G`. Note that, since a manifold here is not second-countable and Hausdorff a Lie group here is not guaranteed to be second-countable (even though it can be proved it is Hausdorff). Note also that Lie groups here are not necessarily finite dimensional. ## Main definitions and statements * `lie_add_group I G` : a Lie additive group where `G` is a manifold on the model with corners `I`. * `lie_group I G` : a Lie multiplicative group where `G` is a manifold on the model with corners `I`. * `normed_space_lie_add_group` : a normed vector space over a nontrivially normed field is an additive Lie group. ## Implementation notes A priori, a Lie group here is a manifold with corners. The definition of Lie group cannot require `I : model_with_corners π•œ E E` with the same space as the model space and as the model vector space, as one might hope, beause in the product situation, the model space is `model_prod E E'` and the model vector space is `E Γ— E'`, which are not the same, so the definition does not apply. Hence the definition should be more general, allowing `I : model_with_corners π•œ E H`. -/ noncomputable theory open_locale manifold /-- A Lie (additive) group is a group and a smooth manifold at the same time in which the addition and negation operations are smooth. -/ -- See note [Design choices about smooth algebraic structures] @[ancestor has_smooth_add] class lie_add_group {π•œ : Type*} [nontrivially_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] (I : model_with_corners π•œ E H) (G : Type*) [add_group G] [topological_space G] [charted_space H G] extends has_smooth_add I G : Prop := (smooth_neg : smooth I I (Ξ» a:G, -a)) /-- A Lie group is a group and a smooth manifold at the same time in which the multiplication and inverse operations are smooth. -/ -- See note [Design choices about smooth algebraic structures] @[ancestor has_smooth_mul, to_additive] class lie_group {π•œ : Type*} [nontrivially_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] (I : model_with_corners π•œ E H) (G : Type*) [group G] [topological_space G] [charted_space H G] extends has_smooth_mul I G : Prop := (smooth_inv : smooth I I (Ξ» a:G, a⁻¹)) section lie_group variables {π•œ : Type*} [nontrivially_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] {I : model_with_corners π•œ E H} {F : Type*} [normed_add_comm_group F] [normed_space π•œ F] {J : model_with_corners π•œ F F} {G : Type*} [topological_space G] [charted_space H G] [group G] [lie_group I G] {E' : Type*} [normed_add_comm_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M : Type*} [topological_space M] [charted_space H' M] {E'' : Type*} [normed_add_comm_group E''] [normed_space π•œ E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners π•œ E'' H''} {M' : Type*} [topological_space M'] [charted_space H'' M'] section variable (I) @[to_additive] lemma smooth_inv : smooth I I (Ξ» x : G, x⁻¹) := lie_group.smooth_inv /-- A Lie group is a topological group. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ @[to_additive "An additive Lie group is an additive topological group. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]."] lemma topological_group_of_lie_group : topological_group G := { continuous_inv := (smooth_inv I).continuous, .. has_continuous_mul_of_smooth I } end @[to_additive] lemma smooth.inv {f : M β†’ G} (hf : smooth I' I f) : smooth I' I (Ξ»x, (f x)⁻¹) := (smooth_inv I).comp hf @[to_additive] lemma smooth_on.inv {f : M β†’ G} {s : set M} (hf : smooth_on I' I f s) : smooth_on I' I (Ξ»x, (f x)⁻¹) s := (smooth_inv I).comp_smooth_on hf @[to_additive] lemma smooth.div {f g : M β†’ G} (hf : smooth I' I f) (hg : smooth I' I g) : smooth I' I (f / g) := by { rw div_eq_mul_inv, exact ((smooth_mul I).comp (hf.prod_mk hg.inv) : _), } @[to_additive] lemma smooth_on.div {f g : M β†’ G} {s : set M} (hf : smooth_on I' I f s) (hg : smooth_on I' I g s) : smooth_on I' I (f / g) s := by { rw div_eq_mul_inv, exact ((smooth_mul I).comp_smooth_on (hf.prod_mk hg.inv) : _), } end lie_group section prod_lie_group /- Instance of product group -/ @[to_additive] instance {π•œ : Type*} [nontrivially_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] {I : model_with_corners π•œ E H} {G : Type*} [topological_space G] [charted_space H G] [group G] [lie_group I G] {E' : Type*} [normed_add_comm_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {G' : Type*} [topological_space G'] [charted_space H' G'] [group G'] [lie_group I' G'] : lie_group (I.prod I') (GΓ—G') := { smooth_inv := smooth_fst.inv.prod_mk smooth_snd.inv, ..has_smooth_mul.prod _ _ _ _ } end prod_lie_group /-! ### Normed spaces are Lie groups -/ instance normed_space_lie_add_group {π•œ : Type*} [nontrivially_normed_field π•œ] {E : Type*} [normed_add_comm_group E] [normed_space π•œ E] : lie_add_group (π“˜(π•œ, E)) E := { smooth_add := smooth_iff.2 ⟨continuous_add, Ξ» x y, cont_diff_add.cont_diff_on⟩, smooth_neg := smooth_iff.2 ⟨continuous_neg, Ξ» x y, cont_diff_neg.cont_diff_on⟩, .. model_space_smooth }
97cf17eae67f121bcae7f6eb0ad9a898b0a38ed3
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/function/simple_func_dense_lp.lean
93bf695847081f29e547c10a9b30aebc9252b61a
[ "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
41,507
lean
/- Copyright (c) 2022 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth -/ import measure_theory.function.l1_space import measure_theory.function.simple_func_dense /-! # Density of simple functions Show that each `Lα΅–` Borel measurable function can be approximated in `Lα΅–` norm by a sequence of simple functions. ## Main definitions * `measure_theory.Lp.simple_func`, the type of `Lp` simple functions * `coe_to_Lp`, the embedding of `Lp.simple_func E p ΞΌ` into `Lp E p ΞΌ` ## Main results * `tendsto_approx_on_univ_Lp` (Lα΅– convergence): If `E` is a `normed_add_comm_group` and `f` is measurable and `mem_β„’p` (for `p < ∞`), then the simple functions `simple_func.approx_on f hf s 0 hβ‚€ n` may be considered as elements of `Lp E p ΞΌ`, and they tend in Lα΅– to `f`. * `Lp.simple_func.dense_embedding`: the embedding `coe_to_Lp` of the `Lp` simple functions into `Lp` is dense. * `Lp.simple_func.induction`, `Lp.induction`, `mem_β„’p.induction`, `integrable.induction`: to prove a predicate for all elements of one of these classes of functions, it suffices to check that it behaves correctly on simple functions. ## TODO For `E` finite-dimensional, simple functions `Ξ± β†’β‚› E` are dense in L^∞ -- prove this. ## Notations * `Ξ± β†’β‚› Ξ²` (local notation): the type of simple functions `Ξ± β†’ Ξ²`. * `Ξ± →₁ₛ[ΞΌ] E`: the type of `L1` simple functions `Ξ± β†’ Ξ²`. -/ noncomputable theory open set function filter topological_space ennreal emetric finset open_locale classical topological_space ennreal measure_theory big_operators variables {Ξ± Ξ² ΞΉ E F π•œ : Type*} namespace measure_theory local infixr ` β†’β‚› `:25 := simple_func namespace simple_func /-! ### Lp approximation by simple functions -/ section Lp variables [measurable_space Ξ²] [measurable_space E] [normed_add_comm_group E] [normed_add_comm_group F] {q : ℝ} {p : ℝβ‰₯0∞} lemma nnnorm_approx_on_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š ≀ βˆ₯f x - yβ‚€βˆ₯β‚Š := begin have := edist_approx_on_le hf hβ‚€ x n, rw edist_comm yβ‚€ at this, simp only [edist_nndist, nndist_eq_nnnorm] at this, exact_mod_cast this end lemma norm_approx_on_yβ‚€_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - yβ‚€βˆ₯ ≀ βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯ := begin have := edist_approx_on_y0_le hf hβ‚€ x n, repeat { rw [edist_comm yβ‚€, edist_eq_coe_nnnorm_sub] at this }, exact_mod_cast this, end lemma norm_approx_on_zero_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} (hβ‚€ : (0 : E) ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s 0 hβ‚€ n xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ := begin have := edist_approx_on_y0_le hf hβ‚€ x n, simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this, exact_mod_cast this, end lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (hΞΌ : βˆ€α΅ x βˆ‚ΞΌ, f x ∈ closure s) (hi : snorm (Ξ» x, f x - yβ‚€) p ΞΌ < ∞) : tendsto (Ξ» n, snorm (approx_on f hf s yβ‚€ hβ‚€ n - f) p ΞΌ) at_top (𝓝 0) := begin by_cases hp_zero : p = 0, { simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds }, have hp : 0 < p.to_real := to_real_pos hp_zero hp_ne_top, suffices : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š ^ p.to_real βˆ‚ΞΌ) at_top (𝓝 0), { simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top], convert continuous_rpow_const.continuous_at.tendsto.comp this; simp [_root_.inv_pos.mpr hp] }, -- We simply check the conditions of the Dominated Convergence Theorem: -- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable have hF_meas : βˆ€ n, measurable (Ξ» x, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real), { simpa only [← edist_eq_coe_nnnorm_sub] using Ξ» n, (approx_on f hf s yβ‚€ hβ‚€ n).measurable_bind (Ξ» y x, (edist y (f x)) ^ p.to_real) (Ξ» y, (measurable_edist_right.comp hf).pow_const p.to_real) }, -- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly -- bounded, at any given point, by `Ξ» x, βˆ₯f x - yβ‚€βˆ₯ ^ p.to_real` have h_bound : βˆ€ n, (Ξ» x, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real) ≀ᡐ[ΞΌ] (Ξ» x, βˆ₯f x - yβ‚€βˆ₯β‚Š ^ p.to_real), { exact Ξ» n, eventually_of_forall (Ξ» x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf hβ‚€ x n)) to_real_nonneg) }, -- (3) The bounding function `Ξ» x, βˆ₯f x - yβ‚€βˆ₯ ^ p.to_real` has finite integral have h_fin : ∫⁻ (a : Ξ²), βˆ₯f a - yβ‚€βˆ₯β‚Š ^ p.to_real βˆ‚ΞΌ β‰  ⊀, from (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi).ne, -- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise -- to zero have h_lim : βˆ€α΅ (a : Ξ²) βˆ‚ΞΌ, tendsto (Ξ» n, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n a - f aβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real) at_top (𝓝 0), { filter_upwards [hΞΌ] with a ha, have : tendsto (Ξ» n, (approx_on f hf s yβ‚€ hβ‚€ n) a - f a) at_top (𝓝 (f a - f a)), { exact (tendsto_approx_on hf hβ‚€ ha).sub tendsto_const_nhds }, convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm), simp [zero_rpow_of_pos hp] }, -- Then we apply the Dominated Convergence Theorem simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim, end lemma mem_β„’p_approx_on [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_β„’p f p ΞΌ) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hiβ‚€ : mem_β„’p (Ξ» x, yβ‚€) p ΞΌ) (n : β„•) : mem_β„’p (approx_on f fmeas s yβ‚€ hβ‚€ n) p ΞΌ := begin refine ⟨(approx_on f fmeas s yβ‚€ hβ‚€ n).ae_strongly_measurable, _⟩, suffices : snorm (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ < ⊀, { have : mem_β„’p (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ := ⟨(approx_on f fmeas s yβ‚€ hβ‚€ n - const Ξ² yβ‚€).ae_strongly_measurable, this⟩, convert snorm_add_lt_top this hiβ‚€, ext x, simp }, have hf' : mem_β„’p (Ξ» x, βˆ₯f x - yβ‚€βˆ₯) p ΞΌ, { have h_meas : measurable (Ξ» x, βˆ₯f x - yβ‚€βˆ₯), { simp only [← dist_eq_norm], exact (continuous_id.dist continuous_const).measurable.comp fmeas }, refine ⟨h_meas.ae_measurable.ae_strongly_measurable, _⟩, rw snorm_norm, convert snorm_add_lt_top hf hiβ‚€.neg, ext x, simp [sub_eq_add_neg] }, have : βˆ€α΅ x βˆ‚ΞΌ, βˆ₯approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€βˆ₯ ≀ βˆ₯(βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯)βˆ₯, { refine eventually_of_forall _, intros x, convert norm_approx_on_yβ‚€_le fmeas hβ‚€ x n, rw [real.norm_eq_abs, abs_of_nonneg], exact add_nonneg (norm_nonneg _) (norm_nonneg _) }, calc snorm (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ ≀ snorm (Ξ» x, βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯) p ΞΌ : snorm_mono_ae this ... < ⊀ : snorm_add_lt_top hf' hf', end lemma tendsto_approx_on_range_Lp_snorm [borel_space E] {f : Ξ² β†’ E} (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (fmeas : measurable f) [separable_space (range f βˆͺ {0} : set E)] (hf : snorm f p ΞΌ < ∞) : tendsto (Ξ» n, snorm (approx_on f fmeas (range f βˆͺ {0}) 0 (by simp) n - f) p ΞΌ) at_top (𝓝 0) := begin refine tendsto_approx_on_Lp_snorm fmeas _ hp_ne_top _ _, { apply eventually_of_forall, assume x, apply subset_closure, simp }, { simpa using hf } end lemma mem_β„’p_approx_on_range [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) [separable_space (range f βˆͺ {0} : set E)] (hf : mem_β„’p f p ΞΌ) (n : β„•) : mem_β„’p (approx_on f fmeas (range f βˆͺ {0}) 0 (by simp) n) p ΞΌ := mem_β„’p_approx_on fmeas hf (by simp) zero_mem_β„’p n lemma tendsto_approx_on_range_Lp [borel_space E] {f : Ξ² β†’ E} [hp : fact (1 ≀ p)] (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (fmeas : measurable f) [separable_space (range f βˆͺ {0} : set E)] (hf : mem_β„’p f p ΞΌ) : tendsto (Ξ» n, (mem_β„’p_approx_on_range fmeas hf n).to_Lp (approx_on f fmeas (range f βˆͺ {0}) 0 (by simp) n)) at_top (𝓝 (hf.to_Lp f)) := by simpa only [Lp.tendsto_Lp_iff_tendsto_β„’p''] using tendsto_approx_on_range_Lp_snorm hp_ne_top fmeas hf.2 end Lp /-! ### L1 approximation by simple functions -/ section integrable variables [measurable_space Ξ²] variables [measurable_space E] [normed_add_comm_group E] lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] {ΞΌ : measure Ξ²} (hΞΌ : βˆ€α΅ x βˆ‚ΞΌ, f x ∈ closure s) (hi : has_finite_integral (Ξ» x, f x - yβ‚€) ΞΌ) : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š βˆ‚ΞΌ) at_top (𝓝 0) := by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf hβ‚€ one_ne_top hΞΌ (by simpa [snorm_one_eq_lintegral_nnnorm] using hi) lemma integrable_approx_on [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hiβ‚€ : integrable (Ξ» x, yβ‚€) ΞΌ) (n : β„•) : integrable (approx_on f fmeas s yβ‚€ hβ‚€ n) ΞΌ := begin rw ← mem_β„’p_one_iff_integrable at hf hiβ‚€ ⊒, exact mem_β„’p_approx_on fmeas hf hβ‚€ hiβ‚€ n, end lemma tendsto_approx_on_range_L1_nnnorm [opens_measurable_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} [separable_space (range f βˆͺ {0} : set E)] (fmeas : measurable f) (hf : integrable f ΞΌ) : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f fmeas (range f βˆͺ {0}) 0 (by simp) n x - f xβˆ₯β‚Š βˆ‚ΞΌ) at_top (𝓝 0) := begin apply tendsto_approx_on_L1_nnnorm fmeas, { apply eventually_of_forall, assume x, apply subset_closure, simp }, { simpa using hf.2 } end lemma integrable_approx_on_range [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) [separable_space (range f βˆͺ {0} : set E)] (hf : integrable f ΞΌ) (n : β„•) : integrable (approx_on f fmeas (range f βˆͺ {0}) 0 (by simp) n) ΞΌ := integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n end integrable section simple_func_properties variables [measurable_space Ξ±] variables [normed_add_comm_group E] [normed_add_comm_group F] variables {ΞΌ : measure Ξ±} {p : ℝβ‰₯0∞} /-! ### Properties of simple functions in `Lp` spaces A simple function `f : Ξ± β†’β‚› E` into a normed group `E` verifies, for a measure `ΞΌ`: - `mem_β„’p f 0 ΞΌ` and `mem_β„’p f ∞ ΞΌ`, since `f` is a.e.-measurable and bounded, - for `0 < p < ∞`, `mem_β„’p f p ΞΌ ↔ integrable f ΞΌ ↔ f.fin_meas_supp ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞`. -/ lemma exists_forall_norm_le (f : Ξ± β†’β‚› F) : βˆƒ C, βˆ€ x, βˆ₯f xβˆ₯ ≀ C := exists_forall_le (f.map (Ξ» x, βˆ₯xβˆ₯)) lemma mem_β„’p_zero (f : Ξ± β†’β‚› E) (ΞΌ : measure Ξ±) : mem_β„’p f 0 ΞΌ := mem_β„’p_zero_iff_ae_strongly_measurable.mpr f.ae_strongly_measurable lemma mem_β„’p_top (f : Ξ± β†’β‚› E) (ΞΌ : measure Ξ±) : mem_β„’p f ∞ ΞΌ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_β„’p_top_of_bound f.ae_strongly_measurable C $ eventually_of_forall hfC protected lemma snorm'_eq {p : ℝ} (f : Ξ± β†’β‚› F) (ΞΌ : measure Ξ±) : snorm' f p ΞΌ = (βˆ‘ y in f.range, (βˆ₯yβˆ₯β‚Š : ℝβ‰₯0∞) ^ p * ΞΌ (f ⁻¹' {y})) ^ (1/p) := have h_map : (Ξ» a, (βˆ₯f aβˆ₯β‚Š : ℝβ‰₯0∞) ^ p) = f.map (Ξ» a : F, (βˆ₯aβˆ₯β‚Š : ℝβ‰₯0∞) ^ p), by simp, by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral] lemma measure_preimage_lt_top_of_mem_β„’p (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (y : E) (hy_ne : y β‰  0) : ΞΌ (f ⁻¹' {y}) < ∞ := begin have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos hp_pos hp_ne_top, have hf_snorm := mem_β„’p.snorm_lt_top hf, rw [snorm_eq_snorm' hp_pos hp_ne_top, f.snorm'_eq, ← @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]), @ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]), ennreal.sum_lt_top_iff] at hf_snorm, by_cases hyf : y ∈ f.range, swap, { suffices h_empty : f ⁻¹' {y} = βˆ…, by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, }, ext1 x, rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false], refine Ξ» hxy, hyf _, rw [mem_range, set.mem_range], exact ⟨x, hxy⟩, }, specialize hf_snorm y hyf, rw ennreal.mul_lt_top_iff at hf_snorm, cases hf_snorm, { exact hf_snorm.2, }, cases hf_snorm, { refine absurd _ hy_ne, simpa [hp_pos_real] using hf_snorm, }, { simp [hf_snorm], }, end lemma mem_β„’p_of_finite_measure_preimage (p : ℝβ‰₯0∞) {f : Ξ± β†’β‚› E} (hf : βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞) : mem_β„’p f p ΞΌ := begin by_cases hp0 : p = 0, { rw [hp0, mem_β„’p_zero_iff_ae_strongly_measurable], exact f.ae_strongly_measurable, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_β„’p_top f ΞΌ, }, refine ⟨f.ae_strongly_measurable, _⟩, rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq], refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (Ξ» y hy, _)).ne, by_cases hy0 : y = 0, { simp [hy0, ennreal.to_real_pos hp0 hp_top], }, { refine ennreal.mul_lt_top _ (hf y hy0).ne, exact (ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top).ne }, end lemma mem_β„’p_iff {f : Ξ± β†’β‚› E} (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞ := ⟨λ h, measure_preimage_lt_top_of_mem_β„’p hp_pos hp_ne_top f h, Ξ» h, mem_β„’p_of_finite_measure_preimage p h⟩ lemma integrable_iff {f : Ξ± β†’β‚› E} : integrable f ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞ := mem_β„’p_one_iff_integrable.symm.trans $ mem_β„’p_iff ennreal.zero_lt_one.ne' ennreal.coe_ne_top lemma mem_β„’p_iff_integrable {f : Ξ± β†’β‚› E} (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ integrable f ΞΌ := (mem_β„’p_iff hp_pos hp_ne_top).trans integrable_iff.symm lemma mem_β„’p_iff_fin_meas_supp {f : Ξ± β†’β‚› E} (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ f.fin_meas_supp ΞΌ := (mem_β„’p_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm lemma integrable_iff_fin_meas_supp {f : Ξ± β†’β‚› E} : integrable f ΞΌ ↔ f.fin_meas_supp ΞΌ := integrable_iff.trans fin_meas_supp_iff.symm lemma fin_meas_supp.integrable {f : Ξ± β†’β‚› E} (h : f.fin_meas_supp ΞΌ) : integrable f ΞΌ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair {f : Ξ± β†’β‚› E} {g : Ξ± β†’β‚› F} : integrable f ΞΌ β†’ integrable g ΞΌ β†’ integrable (pair f g) ΞΌ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair lemma mem_β„’p_of_is_finite_measure (f : Ξ± β†’β‚› E) (p : ℝβ‰₯0∞) (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ] : mem_β„’p f p ΞΌ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_β„’p.of_bound f.ae_strongly_measurable C $ eventually_of_forall hfC lemma integrable_of_is_finite_measure [is_finite_measure ΞΌ] (f : Ξ± β†’β‚› E) : integrable f ΞΌ := mem_β„’p_one_iff_integrable.mp (f.mem_β„’p_of_is_finite_measure 1 ΞΌ) lemma measure_preimage_lt_top_of_integrable (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) {x : E} (hx : x β‰  0) : ΞΌ (f ⁻¹' {x}) < ∞ := integrable_iff.mp hf x hx lemma measure_support_lt_top [has_zero Ξ²] (f : Ξ± β†’β‚› Ξ²) (hf : βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞) : ΞΌ (support f) < ∞ := begin rw support_eq, refine (measure_bUnion_finset_le _ _).trans_lt (ennreal.sum_lt_top_iff.mpr (Ξ» y hy, _)), rw finset.mem_filter at hy, exact hf y hy.2, end lemma measure_support_lt_top_of_mem_β„’p (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (hp_ne_zero : p β‰  0) (hp_ne_top : p β‰  ∞) : ΞΌ (support f) < ∞ := f.measure_support_lt_top ((mem_β„’p_iff hp_ne_zero hp_ne_top).mp hf) lemma measure_support_lt_top_of_integrable (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : ΞΌ (support f) < ∞ := f.measure_support_lt_top (integrable_iff.mp hf) lemma measure_lt_top_of_mem_β„’p_indicator (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) {c : E} (hc : c β‰  0) {s : set Ξ±} (hs : measurable_set s) (hcs : mem_β„’p ((const Ξ± c).piecewise s hs (const Ξ± 0)) p ΞΌ) : ΞΌ s < ⊀ := begin have : function.support (const Ξ± c) = set.univ := function.support_const hc, simpa only [mem_β„’p_iff_fin_meas_supp hp_pos hp_ne_top, fin_meas_supp_iff_support, support_indicator, set.inter_univ, this] using hcs end end simple_func_properties end simple_func /-! Construction of the space of `Lp` simple functions, and its dense embedding into `Lp`. -/ namespace Lp open ae_eq_fun variables [measurable_space Ξ±] [normed_add_comm_group E] [normed_add_comm_group F] (p : ℝβ‰₯0∞) (ΞΌ : measure Ξ±) variables (E) /-- `Lp.simple_func` is a subspace of Lp consisting of equivalence classes of an integrable simple function. -/ def simple_func : add_subgroup (Lp E p ΞΌ) := { carrier := {f : Lp E p ΞΌ | βˆƒ (s : Ξ± β†’β‚› E), (ae_eq_fun.mk s s.ae_strongly_measurable : Ξ± β†’β‚˜[ΞΌ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := Ξ» f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [←hs, ←ht, ae_eq_fun.mk_add_mk, add_subgroup.coe_add, ae_eq_fun.mk_eq_mk, simple_func.coe_add]⟩, neg_mem' := Ξ» f ⟨s, hs⟩, ⟨-s, by simp only [←hs, ae_eq_fun.neg_mk, simple_func.coe_neg, ae_eq_fun.mk_eq_mk, add_subgroup.coe_neg]⟩ } variables {E p ΞΌ} namespace simple_func section instances /-! Simple functions in Lp space form a `normed_space`. -/ @[norm_cast] lemma coe_coe (f : Lp.simple_func E p ΞΌ) : ⇑(f : Lp E p ΞΌ) = f := rfl protected lemma eq' {f g : Lp.simple_func E p ΞΌ} : (f : Ξ± β†’β‚˜[ΞΌ] E) = (g : Ξ± β†’β‚˜[ΞΌ] E) β†’ f = g := subtype.eq ∘ subtype.eq /-! Implementation note: If `Lp.simple_func E p ΞΌ` were defined as a `π•œ`-submodule of `Lp E p ΞΌ`, then the next few lemmas, putting a normed `π•œ`-group structure on `Lp.simple_func E p ΞΌ`, would be unnecessary. But instead, `Lp.simple_func E p ΞΌ` is defined as an `add_subgroup` of `Lp E p ΞΌ`, which does not permit this (but has the advantage of working when `E` itself is a normed group, i.e. has no scalar action). -/ variables [normed_field π•œ] [normed_space π•œ E] /-- If `E` is a normed space, `Lp.simple_func E p ΞΌ` is a `has_smul`. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def has_smul : has_smul π•œ (Lp.simple_func E p ΞΌ) := ⟨λ k f, ⟨k β€’ f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k β€’ s, apply eq.trans (ae_eq_fun.smul_mk k s s.ae_strongly_measurable).symm _, rw hs, refl, end ⟩⟩ local attribute [instance] simple_func.has_smul @[simp, norm_cast] lemma coe_smul (c : π•œ) (f : Lp.simple_func E p ΞΌ) : ((c β€’ f : Lp.simple_func E p ΞΌ) : Lp E p ΞΌ) = c β€’ (f : Lp E p ΞΌ) := rfl /-- If `E` is a normed space, `Lp.simple_func E p ΞΌ` is a module. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def module : module π•œ (Lp.simple_func E p ΞΌ) := { one_smul := Ξ»f, by { ext1, exact one_smul _ _ }, mul_smul := Ξ»x y f, by { ext1, exact mul_smul _ _ _ }, smul_add := Ξ»x f g, by { ext1, exact smul_add _ _ _ }, smul_zero := Ξ»x, by { ext1, exact smul_zero _ }, add_smul := Ξ»x y f, by { ext1, exact add_smul _ _ _ }, zero_smul := Ξ»f, by { ext1, exact zero_smul _ _ } } local attribute [instance] simple_func.module /-- If `E` is a normed space, `Lp.simple_func E p ΞΌ` is a normed space. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def normed_space [fact (1 ≀ p)] : normed_space π•œ (Lp.simple_func E p ΞΌ) := ⟨ Ξ»c f, by { rw [add_subgroup.coe_norm, add_subgroup.coe_norm, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.module simple_func.normed_space section to_Lp /-- Construct the equivalence class `[f]` of a simple function `f` satisfying `mem_β„’p`. -/ @[reducible] def to_Lp (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) : (Lp.simple_func E p ΞΌ) := ⟨hf.to_Lp f, ⟨f, rfl⟩⟩ lemma to_Lp_eq_to_Lp (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) : (to_Lp f hf : Lp E p ΞΌ) = hf.to_Lp f := rfl lemma to_Lp_eq_mk (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) : (to_Lp f hf : Ξ± β†’β‚˜[ΞΌ] E) = ae_eq_fun.mk f f.ae_strongly_measurable := rfl lemma to_Lp_zero : to_Lp (0 : Ξ± β†’β‚› E) zero_mem_β„’p = (0 : Lp.simple_func E p ΞΌ) := rfl lemma to_Lp_add (f g : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (hg : mem_β„’p g p ΞΌ) : to_Lp (f + g) (hf.add hg) = to_Lp f hf + to_Lp g hg := rfl lemma to_Lp_neg (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) : to_Lp (-f) hf.neg = -to_Lp f hf := rfl lemma to_Lp_sub (f g : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (hg : mem_β„’p g p ΞΌ) : to_Lp (f - g) (hf.sub hg) = to_Lp f hf - to_Lp g hg := by { simp only [sub_eq_add_neg, ← to_Lp_neg, ← to_Lp_add], refl } variables [normed_field π•œ] [normed_space π•œ E] lemma to_Lp_smul (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (c : π•œ) : to_Lp (c β€’ f) (hf.const_smul c) = c β€’ to_Lp f hf := rfl lemma norm_to_Lp [fact (1 ≀ p)] (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) : βˆ₯to_Lp f hfβˆ₯ = ennreal.to_real (snorm f p ΞΌ) := norm_to_Lp f hf end to_Lp section to_simple_func /-- Find a representative of a `Lp.simple_func`. -/ def to_simple_func (f : Lp.simple_func E p ΞΌ) : Ξ± β†’β‚› E := classical.some f.2 /-- `(to_simple_func f)` is measurable. -/ @[measurability] protected lemma measurable [measurable_space E] (f : Lp.simple_func E p ΞΌ) : measurable (to_simple_func f) := (to_simple_func f).measurable protected lemma strongly_measurable (f : Lp.simple_func E p ΞΌ) : strongly_measurable (to_simple_func f) := (to_simple_func f).strongly_measurable @[measurability] protected lemma ae_measurable [measurable_space E] (f : Lp.simple_func E p ΞΌ) : ae_measurable (to_simple_func f) ΞΌ := (simple_func.measurable f).ae_measurable protected lemma ae_strongly_measurable (f : Lp.simple_func E p ΞΌ) : ae_strongly_measurable (to_simple_func f) ΞΌ := (simple_func.strongly_measurable f).ae_strongly_measurable lemma to_simple_func_eq_to_fun (f : Lp.simple_func E p ΞΌ) : to_simple_func f =ᡐ[ΞΌ] f := show ⇑(to_simple_func f) =ᡐ[ΞΌ] ⇑(f : Ξ± β†’β‚˜[ΞΌ] E), begin convert (ae_eq_fun.coe_fn_mk (to_simple_func f) (to_simple_func f).ae_strongly_measurable).symm using 2, exact (classical.some_spec f.2).symm, end /-- `to_simple_func f` satisfies the predicate `mem_β„’p`. -/ protected lemma mem_β„’p (f : Lp.simple_func E p ΞΌ) : mem_β„’p (to_simple_func f) p ΞΌ := mem_β„’p.ae_eq (to_simple_func_eq_to_fun f).symm $ mem_Lp_iff_mem_β„’p.mp (f : Lp E p ΞΌ).2 lemma to_Lp_to_simple_func (f : Lp.simple_func E p ΞΌ) : to_Lp (to_simple_func f) (simple_func.mem_β„’p f) = f := simple_func.eq' (classical.some_spec f.2) lemma to_simple_func_to_Lp (f : Ξ± β†’β‚› E) (hfi : mem_β„’p f p ΞΌ) : to_simple_func (to_Lp f hfi) =ᡐ[ΞΌ] f := by { rw ← ae_eq_fun.mk_eq_mk, exact classical.some_spec (to_Lp f hfi).2 } variables (E ΞΌ) lemma zero_to_simple_func : to_simple_func (0 : Lp.simple_func E p ΞΌ) =ᡐ[ΞΌ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : Lp.simple_func E p ΞΌ), Lp.coe_fn_zero E 1 ΞΌ] with _ h₁ _, rwa h₁, end variables {E ΞΌ} lemma add_to_simple_func (f g : Lp.simple_func E p ΞΌ) : to_simple_func (f + g) =ᡐ[ΞΌ] to_simple_func f + to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Lp E p ΞΌ) g] with _, simp only [← coe_coe, add_subgroup.coe_add, pi.add_apply], iterate 4 { assume h, rw h, }, end lemma neg_to_simple_func (f : Lp.simple_func E p ΞΌ) : to_simple_func (-f) =ᡐ[ΞΌ] - to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, Lp.coe_fn_neg (f : Lp E p ΞΌ)] with _, simp only [pi.neg_apply, add_subgroup.coe_neg, ← coe_coe], repeat { assume h, rw h, }, end lemma sub_to_simple_func (f g : Lp.simple_func E p ΞΌ) : to_simple_func (f - g) =ᡐ[ΞΌ] to_simple_func f - to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Lp E p ΞΌ) g] with _, simp only [add_subgroup.coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h, }, end variables [normed_field π•œ] [normed_space π•œ E] lemma smul_to_simple_func (k : π•œ) (f : Lp.simple_func E p ΞΌ) : to_simple_func (k β€’ f) =ᡐ[ΞΌ] k β€’ to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (k β€’ f), to_simple_func_eq_to_fun f, Lp.coe_fn_smul k (f : Lp E p ΞΌ)] with _, simp only [pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h, }, end lemma norm_to_simple_func [fact (1 ≀ p)] (f : Lp.simple_func E p ΞΌ) : βˆ₯fβˆ₯ = ennreal.to_real (snorm (to_simple_func f) p ΞΌ) := by simpa [to_Lp_to_simple_func] using norm_to_Lp (to_simple_func f) (simple_func.mem_β„’p f) end to_simple_func section induction variables (p) /-- The characteristic function of a finite-measure measurable set `s`, as an `Lp` simple function. -/ def indicator_const {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : E) : Lp.simple_func E p ΞΌ := to_Lp ((simple_func.const _ c).piecewise s hs (simple_func.const _ 0)) (mem_β„’p_indicator_const p hs c (or.inr hΞΌs)) variables {p} @[simp] lemma coe_indicator_const {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : E) : (↑(indicator_const p hs hΞΌs c) : Lp E p ΞΌ) = indicator_const_Lp p hs hΞΌs c := rfl lemma to_simple_func_indicator_const {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : E) : to_simple_func (indicator_const p hs hΞΌs c) =ᡐ[ΞΌ] (simple_func.const _ c).piecewise s hs (simple_func.const _ 0) := Lp.simple_func.to_simple_func_to_Lp _ _ /-- To prove something for an arbitrary `Lp` simple function, with `0 < p < ∞`, it suffices to show that the property holds for (multiples of) characteristic functions of finite-measure measurable sets and is closed under addition (of functions with disjoint support). -/ @[elab_as_eliminator] protected lemma induction (hp_pos : p β‰  0) (hp_ne_top : p β‰  ∞) {P : Lp.simple_func E p ΞΌ β†’ Prop} (h_ind : βˆ€ (c : E) {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s < ∞), P (Lp.simple_func.indicator_const p hs hΞΌs.ne c)) (h_add : βˆ€ ⦃f g : Ξ± β†’β‚› E⦄, βˆ€ hf : mem_β„’p f p ΞΌ, βˆ€ hg : mem_β„’p g p ΞΌ, disjoint (support f) (support g) β†’ P (Lp.simple_func.to_Lp f hf) β†’ P (Lp.simple_func.to_Lp g hg) β†’ P (Lp.simple_func.to_Lp f hf + Lp.simple_func.to_Lp g hg)) (f : Lp.simple_func E p ΞΌ) : P f := begin suffices : βˆ€ f : Ξ± β†’β‚› E, βˆ€ hf : mem_β„’p f p ΞΌ, P (to_Lp f hf), { rw ← to_Lp_to_simple_func f, apply this }, clear f, refine simple_func.induction _ _, { intros c s hs hf, by_cases hc : c = 0, { convert h_ind 0 measurable_set.empty (by simp) using 1, ext1, simp [hc] }, exact h_ind c hs (simple_func.measure_lt_top_of_mem_β„’p_indicator hp_pos hp_ne_top hc hs hf) }, { intros f g hfg hf hg hfg', obtain ⟨hf', hg'⟩ : mem_β„’p f p ΞΌ ∧ mem_β„’p g p ΞΌ, { exact (mem_β„’p_add_of_disjoint hfg f.strongly_measurable g.strongly_measurable).mp hfg' }, exact h_add hf' hg' hfg (hf hf') (hg hg') }, end end induction section coe_to_Lp variables [fact (1 ≀ p)] protected lemma uniform_continuous : uniform_continuous (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding (hp_ne_top : p β‰  ∞) : dense_embedding (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := begin borelize E, apply simple_func.uniform_embedding.dense_embedding, assume f, rw mem_closure_iff_seq_limit, have hfi' : mem_β„’p f p ΞΌ := Lp.mem_β„’p f, haveI : separable_space (range f βˆͺ {0} : set E) := (Lp.strongly_measurable f).separable_space_range_union_singleton, refine ⟨λ n, ↑(to_Lp (simple_func.approx_on f (Lp.strongly_measurable f).measurable (range f βˆͺ {0}) 0 (by simp) n) (simple_func.mem_β„’p_approx_on_range (Lp.strongly_measurable f).measurable hfi' n)), Ξ» n, mem_range_self _, _⟩, convert simple_func.tendsto_approx_on_range_Lp hp_ne_top (Lp.strongly_measurable f).measurable hfi', rw to_Lp_coe_fn f (Lp.mem_β„’p f) end protected lemma dense_inducing (hp_ne_top : p β‰  ∞) : dense_inducing (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := (simple_func.dense_embedding hp_ne_top).to_dense_inducing protected lemma dense_range (hp_ne_top : p β‰  ∞) : dense_range (coe : (Lp.simple_func E p ΞΌ) β†’ (Lp E p ΞΌ)) := (simple_func.dense_inducing hp_ne_top).dense variables [normed_field π•œ] [normed_space π•œ E] variables (Ξ± E π•œ) /-- The embedding of Lp simple functions into Lp functions, as a continuous linear map. -/ def coe_to_Lp : (Lp.simple_func E p ΞΌ) β†’L[π•œ] (Lp E p ΞΌ) := { map_smul' := Ξ»k f, rfl, cont := Lp.simple_func.uniform_continuous.continuous, .. add_subgroup.subtype (Lp.simple_func E p ΞΌ) } variables {Ξ± E π•œ} end coe_to_Lp section order variables {G : Type*} [normed_lattice_add_comm_group G] lemma coe_fn_le (f g : Lp.simple_func G p ΞΌ) : f ≀ᡐ[ΞΌ] g ↔ f ≀ g := by rw [← subtype.coe_le_coe, ← Lp.coe_fn_le, coe_fn_coe_base', coe_fn_coe_base' g] instance : covariant_class (Lp.simple_func G p ΞΌ) (Lp.simple_func G p ΞΌ) (+) (≀) := begin refine ⟨λ f g₁ gβ‚‚ hg₁₂, _⟩, rw ← Lp.simple_func.coe_fn_le at hg₁₂ ⊒, have h_add_1 : ⇑(f + g₁) =ᡐ[ΞΌ] f + g₁, from Lp.coe_fn_add _ _, have h_add_2 : ⇑(f + gβ‚‚) =ᡐ[ΞΌ] f + gβ‚‚, from Lp.coe_fn_add _ _, filter_upwards [h_add_1, h_add_2, hg₁₂] with _ h1 h2 h3, rw [h1, h2, pi.add_apply, pi.add_apply], exact add_le_add le_rfl h3, end variables (p ΞΌ G) lemma coe_fn_zero : (0 : Lp.simple_func G p ΞΌ) =ᡐ[ΞΌ] (0 : Ξ± β†’ G) := Lp.coe_fn_zero _ _ _ variables{p ΞΌ G} lemma coe_fn_nonneg (f : Lp.simple_func G p ΞΌ) : 0 ≀ᡐ[ΞΌ] f ↔ 0 ≀ f := begin rw ← Lp.simple_func.coe_fn_le, have h0 : (0 : Lp.simple_func G p ΞΌ) =ᡐ[ΞΌ] (0 : Ξ± β†’ G), from Lp.simple_func.coe_fn_zero p ΞΌ G, split; intro h; filter_upwards [h, h0] with _ _ h2, { rwa h2, }, { rwa ← h2, }, end lemma exists_simple_func_nonneg_ae_eq {f : Lp.simple_func G p ΞΌ} (hf : 0 ≀ f) : βˆƒ f' : Ξ± β†’β‚› G, 0 ≀ f' ∧ f =ᡐ[ΞΌ] f' := begin rw ← Lp.simple_func.coe_fn_nonneg at hf, have hf_ae : 0 ≀ᡐ[ΞΌ] (simple_func.to_simple_func f), by { filter_upwards [to_simple_func_eq_to_fun f, hf] with _ h1 _, rwa h1 }, let s := (to_measurable ΞΌ {x | Β¬ 0 ≀ simple_func.to_simple_func f x})ᢜ, have hs_zero : ΞΌ sᢜ = 0, by { rw [compl_compl, measure_to_measurable], rwa [eventually_le, ae_iff] at hf_ae, }, have hfs_nonneg : βˆ€ x ∈ s, 0 ≀ simple_func.to_simple_func f x, { intros x hxs, rw mem_compl_iff at hxs, have hx' : x βˆ‰ {a : Ξ± | Β¬0 ≀ simple_func.to_simple_func f a}, from Ξ» h, hxs (subset_to_measurable ΞΌ _ h), rwa [set.nmem_set_of_eq, not_not] at hx', }, let f' := simple_func.piecewise s (measurable_set_to_measurable ΞΌ _).compl (simple_func.to_simple_func f) (simple_func.const Ξ± (0 : G)), refine ⟨f', Ξ» x, _, _⟩, { rw simple_func.piecewise_apply, by_cases hxs : x ∈ s, { simp only [hxs, hfs_nonneg x hxs, if_true, pi.zero_apply, simple_func.coe_zero], }, { simp only [hxs, simple_func.const_zero, if_false], }, }, { rw simple_func.coe_piecewise, have : s =ᡐ[ΞΌ] univ, { rw ae_eq_set, simp only [true_and, measure_empty, eq_self_iff_true, diff_univ, ← compl_eq_univ_diff], exact hs_zero, }, refine eventually_eq.trans (to_simple_func_eq_to_fun f).symm _, refine eventually_eq.trans _ (piecewise_ae_eq_of_ae_eq_set this.symm), simp only [simple_func.const_zero, indicator_univ, piecewise_eq_indicator, simple_func.coe_zero], }, end variables (p ΞΌ G) /-- Coercion from nonnegative simple functions of Lp to nonnegative functions of Lp. -/ def coe_simple_func_nonneg_to_Lp_nonneg : {g : Lp.simple_func G p ΞΌ // 0 ≀ g} β†’ {g : Lp G p ΞΌ // 0 ≀ g} := Ξ» g, ⟨g, g.2⟩ lemma dense_range_coe_simple_func_nonneg_to_Lp_nonneg [hp : fact (1 ≀ p)] (hp_ne_top : p β‰  ∞) : dense_range (coe_simple_func_nonneg_to_Lp_nonneg p ΞΌ G) := begin borelize G, assume g, rw mem_closure_iff_seq_limit, have hg_mem_β„’p : mem_β„’p g p ΞΌ := Lp.mem_β„’p g, have zero_mem : (0 : G) ∈ (range g βˆͺ {0} : set G) ∩ {y | 0 ≀ y}, by simp only [union_singleton, mem_inter_eq, mem_insert_iff, eq_self_iff_true, true_or, mem_set_of_eq, le_refl, and_self], haveI : separable_space (((range g βˆͺ {0}) ∩ {y | 0 ≀ y}) : set G), { apply is_separable.separable_space, apply is_separable.mono _ (set.inter_subset_left _ _), exact (Lp.strongly_measurable (g : Lp G p ΞΌ)).is_separable_range.union (finite_singleton _).is_separable }, have g_meas : measurable g := (Lp.strongly_measurable (g : Lp G p ΞΌ)).measurable, let x := Ξ» n, simple_func.approx_on g g_meas ((range g βˆͺ {0}) ∩ {y | 0 ≀ y}) 0 zero_mem n, have hx_nonneg : βˆ€ n, 0 ≀ x n, { assume n a, change x n a ∈ {y : G | 0 ≀ y}, have A : (range g βˆͺ {0} : set G) ∩ {y | 0 ≀ y} βŠ† {y | 0 ≀ y} := inter_subset_right _ _, apply A, exact simple_func.approx_on_mem g_meas _ n a }, have hx_mem_β„’p : βˆ€ n, mem_β„’p (x n) p ΞΌ, from simple_func.mem_β„’p_approx_on _ hg_mem_β„’p _ ⟨ae_strongly_measurable_const, by simp⟩, have h_to_Lp := Ξ» n, mem_β„’p.coe_fn_to_Lp (hx_mem_β„’p n), have hx_nonneg_Lp : βˆ€ n, 0 ≀ to_Lp (x n) (hx_mem_β„’p n), { intro n, rw [← Lp.simple_func.coe_fn_le, coe_fn_coe_base' (simple_func.to_Lp (x n) _), Lp.simple_func.to_Lp_eq_to_Lp], have h0 := Lp.simple_func.coe_fn_zero p ΞΌ G, filter_upwards [Lp.simple_func.coe_fn_zero p ΞΌ G, h_to_Lp n] with a ha0 ha_to_Lp, rw [ha0, ha_to_Lp], exact hx_nonneg n a, }, have hx_tendsto : tendsto (Ξ» (n : β„•), snorm (x n - g) p ΞΌ) at_top (𝓝 0), { apply simple_func.tendsto_approx_on_Lp_snorm g_meas zero_mem hp_ne_top, { have hg_nonneg : 0 ≀ᡐ[ΞΌ] g, from (Lp.coe_fn_nonneg _).mpr g.2, refine hg_nonneg.mono (Ξ» a ha, subset_closure _), simpa using ha, }, { simp_rw sub_zero, exact hg_mem_β„’p.snorm_lt_top, }, }, refine ⟨λ n, (coe_simple_func_nonneg_to_Lp_nonneg p ΞΌ G) ⟨to_Lp (x n) (hx_mem_β„’p n), hx_nonneg_Lp n⟩, Ξ» n, mem_range_self _, _⟩, suffices : tendsto (Ξ» (n : β„•), ↑(to_Lp (x n) (hx_mem_β„’p n))) at_top (𝓝 (g : Lp G p ΞΌ)), { rw tendsto_iff_dist_tendsto_zero at this ⊒, simp_rw subtype.dist_eq, convert this, }, rw Lp.tendsto_Lp_iff_tendsto_β„’p', convert hx_tendsto, refine funext (Ξ» n, snorm_congr_ae (eventually_eq.sub _ _)), { rw Lp.simple_func.to_Lp_eq_to_Lp, exact h_to_Lp n, }, { rw ← coe_fn_coe_base, }, end variables {p ΞΌ G} end order end simple_func end Lp variables [measurable_space Ξ±] [normed_add_comm_group E] {f : Ξ± β†’ E} {p : ℝβ‰₯0∞} {ΞΌ : measure Ξ±} /-- To prove something for an arbitrary `Lp` function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in `Lp` for which the property holds is closed. -/ @[elab_as_eliminator] lemma Lp.induction [_i : fact (1 ≀ p)] (hp_ne_top : p β‰  ∞) (P : Lp E p ΞΌ β†’ Prop) (h_ind : βˆ€ (c : E) {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s < ∞), P (Lp.simple_func.indicator_const p hs hΞΌs.ne c)) (h_add : βˆ€ ⦃f g⦄, βˆ€ hf : mem_β„’p f p ΞΌ, βˆ€ hg : mem_β„’p g p ΞΌ, disjoint (support f) (support g) β†’ P (hf.to_Lp f) β†’ P (hg.to_Lp g) β†’ P ((hf.to_Lp f) + (hg.to_Lp g))) (h_closed : is_closed {f : Lp E p ΞΌ | P f}) : βˆ€ f : Lp E p ΞΌ, P f := begin refine Ξ» f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed _, refine Lp.simple_func.induction (lt_of_lt_of_le ennreal.zero_lt_one _i.elim).ne' hp_ne_top _ _, { exact Ξ» c s, h_ind c }, { exact Ξ» f g hf hg, h_add hf hg }, end /-- To prove something for an arbitrary `mem_β„’p` function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `Lα΅–` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma mem_β„’p.induction [_i : fact (1 ≀ p)] (hp_ne_top : p β‰  ∞) (P : (Ξ± β†’ E) β†’ Prop) (h_ind : βˆ€ (c : E) ⦃s⦄, measurable_set s β†’ ΞΌ s < ∞ β†’ P (s.indicator (Ξ» _, c))) (h_add : βˆ€ ⦃f g : Ξ± β†’ E⦄, disjoint (support f) (support g) β†’ mem_β„’p f p ΞΌ β†’ mem_β„’p g p ΞΌ β†’ P f β†’ P g β†’ P (f + g)) (h_closed : is_closed {f : Lp E p ΞΌ | P f} ) (h_ae : βˆ€ ⦃f g⦄, f =ᡐ[ΞΌ] g β†’ mem_β„’p f p ΞΌ β†’ P f β†’ P g) : βˆ€ ⦃f : Ξ± β†’ E⦄ (hf : mem_β„’p f p ΞΌ), P f := begin have : βˆ€ (f : simple_func Ξ± E), mem_β„’p f p ΞΌ β†’ P f, { refine simple_func.induction _ _, { intros c s hs h, by_cases hc : c = 0, { subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, ext, simp [const] }, have hp_pos : p β‰  0 := (lt_of_lt_of_le ennreal.zero_lt_one _i.elim).ne', exact h_ind c hs (simple_func.measure_lt_top_of_mem_β„’p_indicator hp_pos hp_ne_top hc hs h) }, { intros f g hfg hf hg int_fg, rw [simple_func.coe_add, mem_β„’p_add_of_disjoint hfg f.strongly_measurable g.strongly_measurable] at int_fg, refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } }, have : βˆ€ (f : Lp.simple_func E p ΞΌ), P f, { intro f, exact h_ae (Lp.simple_func.to_simple_func_eq_to_fun f) (Lp.simple_func.mem_β„’p f) (this (Lp.simple_func.to_simple_func f) (Lp.simple_func.mem_β„’p f)) }, have : βˆ€ (f : Lp E p ΞΌ), P f := Ξ» f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed this, exact Ξ» f hf, h_ae hf.coe_fn_to_Lp (Lp.mem_β„’p _) (this (hf.to_Lp f)), end section integrable notation Ξ± ` →₁ₛ[`:25 ΞΌ `] ` E := @measure_theory.Lp.simple_func Ξ± E _ _ 1 ΞΌ lemma L1.simple_func.to_Lp_one_eq_to_L1 (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : (Lp.simple_func.to_Lp f (mem_β„’p_one_iff_integrable.2 hf) : Ξ± →₁[ΞΌ] E) = hf.to_L1 f := rfl protected lemma L1.simple_func.integrable (f : Ξ± →₁ₛ[ΞΌ] E) : integrable (Lp.simple_func.to_simple_func f) ΞΌ := by { rw ← mem_β„’p_one_iff_integrable, exact (Lp.simple_func.mem_β„’p f) } /-- To prove something for an arbitrary integrable function in a normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `LΒΉ` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma integrable.induction (P : (Ξ± β†’ E) β†’ Prop) (h_ind : βˆ€ (c : E) ⦃s⦄, measurable_set s β†’ ΞΌ s < ∞ β†’ P (s.indicator (Ξ» _, c))) (h_add : βˆ€ ⦃f g : Ξ± β†’ E⦄, disjoint (support f) (support g) β†’ integrable f ΞΌ β†’ integrable g ΞΌ β†’ P f β†’ P g β†’ P (f + g)) (h_closed : is_closed {f : Ξ± →₁[ΞΌ] E | P f} ) (h_ae : βˆ€ ⦃f g⦄, f =ᡐ[ΞΌ] g β†’ integrable f ΞΌ β†’ P f β†’ P g) : βˆ€ ⦃f : Ξ± β†’ E⦄ (hf : integrable f ΞΌ), P f := begin simp only [← mem_β„’p_one_iff_integrable] at *, exact mem_β„’p.induction one_ne_top P h_ind h_add h_closed h_ae end end integrable end measure_theory
8f75fb9127c54e0b23f4f7942ef1bc16626abe9a
4727251e0cd73359b15b664c3170e5d754078599
/src/data/pi/algebra.lean
367eb56bcbbca68c9436ecc7cf6298fd70374279
[ "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
9,049
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Eric Wieser -/ import tactic.split_ifs import tactic.simpa import tactic.congr import algebra.group.to_additive import data.prod import logic.unique /-! # Instances and theorems on pi types This file provides basic definitions and notation instances for Pi types. Instances of more sophisticated classes are defined in `pi.lean` files elsewhere. -/ open function universes u v₁ vβ‚‚ v₃ variable {I : Type u} -- The indexing type variables {Ξ± Ξ² Ξ³ : Type*} -- The families of types already equipped with instances variables {f : I β†’ Type v₁} {g : I β†’ Type vβ‚‚} {h : I β†’ Type v₃} variables (x y : Ξ  i, f i) (i : I) namespace pi /-! `1`, `0`, `+`, `*`, `-`, `⁻¹`, and `/` are defined pointwise. -/ @[to_additive] instance has_one [βˆ€ i, has_one $ f i] : has_one (Ξ  i : I, f i) := ⟨λ _, 1⟩ @[simp, to_additive] lemma one_apply [βˆ€ i, has_one $ f i] : (1 : Ξ  i, f i) i = 1 := rfl @[to_additive] lemma one_def [Ξ  i, has_one $ f i] : (1 : Ξ  i, f i) = Ξ» i, 1 := rfl @[simp, to_additive] lemma const_one [has_one Ξ²] : const Ξ± (1 : Ξ²) = 1 := rfl @[simp, to_additive] lemma one_comp [has_one Ξ³] (x : Ξ± β†’ Ξ²) : (1 : Ξ² β†’ Ξ³) ∘ x = 1 := rfl @[simp, to_additive] lemma comp_one [has_one Ξ²] (x : Ξ² β†’ Ξ³) : x ∘ 1 = const Ξ± (x 1) := rfl @[to_additive] instance has_mul [βˆ€ i, has_mul $ f i] : has_mul (Ξ  i : I, f i) := ⟨λ f g i, f i * g i⟩ @[simp, to_additive] lemma mul_apply [βˆ€ i, has_mul $ f i] : (x * y) i = x i * y i := rfl @[to_additive] lemma mul_def [Ξ  i, has_mul $ f i] : x * y = Ξ» i, x i * y i := rfl @[simp, to_additive] lemma const_mul [has_mul Ξ²] (a b : Ξ²) : const Ξ± a * const Ξ± b = const Ξ± (a * b) := rfl @[to_additive] lemma mul_comp [has_mul Ξ³] (x y : Ξ² β†’ Ξ³) (z : Ξ± β†’ Ξ²) : (x * y) ∘ z = x ∘ z * y ∘ z := rfl @[simp] lemma bit0_apply [Ξ  i, has_add $ f i] : (bit0 x) i = bit0 (x i) := rfl @[simp] lemma bit1_apply [Ξ  i, has_add $ f i] [Ξ  i, has_one $ f i] : (bit1 x) i = bit1 (x i) := rfl @[to_additive] instance has_inv [βˆ€ i, has_inv $ f i] : has_inv (Ξ  i : I, f i) := ⟨λ f i, (f i)⁻¹⟩ @[simp, to_additive] lemma inv_apply [βˆ€ i, has_inv $ f i] : x⁻¹ i = (x i)⁻¹ := rfl @[to_additive] lemma inv_def [Ξ  i, has_inv $ f i] : x⁻¹ = Ξ» i, (x i)⁻¹ := rfl @[to_additive] lemma const_inv [has_inv Ξ²] (a : Ξ²) : (const Ξ± a)⁻¹ = const Ξ± a⁻¹ := rfl @[to_additive] lemma inv_comp [has_inv Ξ³] (x : Ξ² β†’ Ξ³) (y : Ξ± β†’ Ξ²) : x⁻¹ ∘ y = (x ∘ y)⁻¹ := rfl @[to_additive] instance has_div [Ξ  i, has_div $ f i] : has_div (Ξ  i : I, f i) := ⟨λ f g i, f i / g i⟩ @[simp, to_additive] lemma div_apply [Ξ  i, has_div $ f i] : (x / y) i = x i / y i := rfl @[to_additive] lemma div_def [Ξ  i, has_div $ f i] : x / y = Ξ» i, x i / y i := rfl @[to_additive] lemma div_comp [has_div Ξ³] (x y : Ξ² β†’ Ξ³) (z : Ξ± β†’ Ξ²) : (x / y) ∘ z = x ∘ z / y ∘ z := rfl @[simp, to_additive] lemma const_div [has_div Ξ²] (a b : Ξ²) : const Ξ± a / const Ξ± b = const Ξ± (a / b) := rfl section variables [decidable_eq I] variables [Ξ  i, has_one (f i)] [Ξ  i, has_one (g i)] [Ξ  i, has_one (h i)] /-- The function supported at `i`, with value `x` there, and `1` elsewhere. -/ @[to_additive pi.single "The function supported at `i`, with value `x` there, and `0` elsewhere." ] def mul_single (i : I) (x : f i) : Ξ  i, f i := function.update 1 i x @[simp, to_additive] lemma mul_single_eq_same (i : I) (x : f i) : mul_single i x i = x := function.update_same i x _ @[simp, to_additive] lemma mul_single_eq_of_ne {i i' : I} (h : i' β‰  i) (x : f i) : mul_single i x i' = 1 := function.update_noteq h x _ /-- Abbreviation for `mul_single_eq_of_ne h.symm`, for ease of use by `simp`. -/ @[simp, to_additive "Abbreviation for `single_eq_of_ne h.symm`, for ease of use by `simp`."] lemma mul_single_eq_of_ne' {i i' : I} (h : i β‰  i') (x : f i) : mul_single i x i' = 1 := mul_single_eq_of_ne h.symm x @[simp, to_additive] lemma mul_single_one (i : I) : mul_single i (1 : f i) = 1 := function.update_eq_self _ _ /-- On non-dependent functions, `pi.mul_single` can be expressed as an `ite` -/ @[to_additive "On non-dependent functions, `pi.single` can be expressed as an `ite`"] lemma mul_single_apply {Ξ² : Sort*} [has_one Ξ²] (i : I) (x : Ξ²) (i' : I) : mul_single i x i' = if i' = i then x else 1 := function.update_apply 1 i x i' /-- On non-dependent functions, `pi.mul_single` is symmetric in the two indices. -/ @[to_additive "On non-dependent functions, `pi.single` is symmetric in the two indices."] lemma mul_single_comm {Ξ² : Sort*} [has_one Ξ²] (i : I) (x : Ξ²) (i' : I) : mul_single i x i' = mul_single i' x i := by simp [mul_single_apply, eq_comm] @[to_additive] lemma apply_mul_single (f' : Ξ  i, f i β†’ g i) (hf' : βˆ€ i, f' i 1 = 1) (i : I) (x : f i) (j : I): f' j (mul_single i x j) = mul_single i (f' i x) j := by simpa only [pi.one_apply, hf', mul_single] using function.apply_update f' 1 i x j @[to_additive apply_singleβ‚‚] lemma apply_mul_singleβ‚‚ (f' : Ξ  i, f i β†’ g i β†’ h i) (hf' : βˆ€ i, f' i 1 1 = 1) (i : I) (x : f i) (y : g i) (j : I): f' j (mul_single i x j) (mul_single i y j) = mul_single i (f' i x y) j := begin by_cases h : j = i, { subst h, simp only [mul_single_eq_same] }, { simp only [mul_single_eq_of_ne h, hf'] }, end @[to_additive] lemma mul_single_op {g : I β†’ Type*} [Ξ  i, has_one (g i)] (op : Ξ  i, f i β†’ g i) (h : βˆ€ i, op i 1 = 1) (i : I) (x : f i) : mul_single i (op i x) = Ξ» j, op j (mul_single i x j) := eq.symm $ funext $ apply_mul_single op h i x @[to_additive] lemma mul_single_opβ‚‚ {g₁ gβ‚‚ : I β†’ Type*} [Ξ  i, has_one (g₁ i)] [Ξ  i, has_one (gβ‚‚ i)] (op : Ξ  i, g₁ i β†’ gβ‚‚ i β†’ f i) (h : βˆ€ i, op i 1 1 = 1) (i : I) (x₁ : g₁ i) (xβ‚‚ : gβ‚‚ i) : mul_single i (op i x₁ xβ‚‚) = Ξ» j, op j (mul_single i x₁ j) (mul_single i xβ‚‚ j) := eq.symm $ funext $ apply_mul_singleβ‚‚ op h i x₁ xβ‚‚ variables (f) @[to_additive] lemma mul_single_injective (i : I) : function.injective (mul_single i : f i β†’ Ξ  i, f i) := function.update_injective _ i @[simp, to_additive] lemma mul_single_inj (i : I) {x y : f i} : mul_single i x = mul_single i y ↔ x = y := (pi.mul_single_injective _ _).eq_iff end /-- The mapping into a product type built from maps into each component. -/ @[simp] protected def prod (f' : Ξ  i, f i) (g' : Ξ  i, g i) (i : I) : f i Γ— g i := (f' i, g' i) @[simp] lemma prod_fst_snd : pi.prod (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) = id := funext $ Ξ» _, prod.mk.eta @[simp] lemma prod_snd_fst : pi.prod (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) = prod.swap := rfl end pi namespace function section extend @[to_additive] lemma extend_one [has_one Ξ³] (f : Ξ± β†’ Ξ²) : function.extend f (1 : Ξ± β†’ Ξ³) (1 : Ξ² β†’ Ξ³) = 1 := funext $ Ξ» _, by apply if_t_t _ _ @[to_additive] lemma extend_mul [has_mul Ξ³] (f : Ξ± β†’ Ξ²) (g₁ gβ‚‚ : Ξ± β†’ Ξ³) (e₁ eβ‚‚ : Ξ² β†’ Ξ³) : function.extend f (g₁ * gβ‚‚) (e₁ * eβ‚‚) = function.extend f g₁ e₁ * function.extend f gβ‚‚ eβ‚‚ := funext $ Ξ» _, by convert (apply_dite2 (*) _ _ _ _ _).symm @[to_additive] lemma extend_inv [has_inv Ξ³] (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ³) (e : Ξ² β†’ Ξ³) : function.extend f (g⁻¹) (e⁻¹) = (function.extend f g e)⁻¹ := funext $ Ξ» _, by convert (apply_dite has_inv.inv _ _ _).symm @[to_additive] lemma extend_div [has_div Ξ³] (f : Ξ± β†’ Ξ²) (g₁ gβ‚‚ : Ξ± β†’ Ξ³) (e₁ eβ‚‚ : Ξ² β†’ Ξ³) : function.extend f (g₁ / gβ‚‚) (e₁ / eβ‚‚) = function.extend f g₁ e₁ / function.extend f gβ‚‚ eβ‚‚ := funext $ Ξ» _, by convert (apply_dite2 (/) _ _ _ _ _).symm end extend lemma surjective_pi_map {F : Ξ  i, f i β†’ g i} (hF : βˆ€ i, surjective (F i)) : surjective (Ξ» x : Ξ  i, f i, Ξ» i, F i (x i)) := Ξ» y, ⟨λ i, (hF i (y i)).some, funext $ Ξ» i, (hF i (y i)).some_spec⟩ lemma injective_pi_map {F : Ξ  i, f i β†’ g i} (hF : βˆ€ i, injective (F i)) : injective (Ξ» x : Ξ  i, f i, Ξ» i, F i (x i)) := Ξ» x y h, funext $ Ξ» i, hF i $ (congr_fun h i : _) lemma bijective_pi_map {F : Ξ  i, f i β†’ g i} (hF : βˆ€ i, bijective (F i)) : bijective (Ξ» x : Ξ  i, f i, Ξ» i, F i (x i)) := ⟨injective_pi_map (Ξ» i, (hF i).injective), surjective_pi_map (Ξ» i, (hF i).surjective)⟩ end function /-- If the one function is surjective, the codomain is trivial. -/ @[to_additive "If the zero function is surjective, the codomain is trivial."] def unique_of_surjective_one (Ξ± : Type*) {Ξ² : Type*} [has_one Ξ²] (h : function.surjective (1 : Ξ± β†’ Ξ²)) : unique Ξ² := h.unique_of_surjective_const Ξ± (1 : Ξ²) @[to_additive subsingleton.pi_single_eq] lemma subsingleton.pi_mul_single_eq {Ξ± : Type*} [decidable_eq I] [subsingleton I] [has_one Ξ±] (i : I) (x : Ξ±) : pi.mul_single i x = Ξ» _, x := funext $ Ξ» j, by rw [subsingleton.elim j i, pi.mul_single_eq_same]
19d441405d3f29be064726cf305bd9f48352be7a
1dd482be3f611941db7801003235dc84147ec60a
/src/data/finset.lean
880287d86b0bb5805afc1e3a65648923030529d7
[ "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
78,118
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, Minchao Wu, Mario Carneiro Finite sets. -/ import logic.embedding order.boolean_algebra algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice 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. -/ def to_set (s : finset Ξ±) : set Ξ± := {x | x ∈ s} instance : has_lift (finset Ξ±) (set Ξ±) := ⟨to_set⟩ @[simp] 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 /- extensionality -/ theorem ext {s₁ sβ‚‚ : finset Ξ±} : s₁ = sβ‚‚ ↔ βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚ := val_inj.symm.trans $ nodup_ext s₁.2 sβ‚‚.2 @[extensionality] theorem ext' {s₁ sβ‚‚ : finset Ξ±} : (βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚) β†’ s₁ = sβ‚‚ := ext.2 @[simp] theorem coe_inj {s₁ sβ‚‚ : finset Ξ±} : (↑s₁ : set Ξ±) = ↑sβ‚‚ ↔ s₁ = sβ‚‚ := (set.ext_iff _ _).trans ext.symm /- 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 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.2 $ Ξ» 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_iff_subset_not_subset, 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 /- empty -/ 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 @[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 exists_mem_of_ne_empty {s : finset Ξ±} (h : s β‰  βˆ…) : βˆƒ a : Ξ±, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) @[simp] lemma coe_empty : ↑(βˆ… : finset Ξ±) = (βˆ… : set Ξ±) := rfl /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : Ξ±) : finset Ξ± := ⟨_, nodup_singleton a⟩ local prefix `ΞΉ`:90 := singleton @[simp] theorem singleton_val (a : Ξ±) : (ΞΉ a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ ΞΉ a ↔ b = a := mem_singleton theorem not_mem_singleton {a b : Ξ±} : a βˆ‰ ΞΉ b ↔ a β‰  b := not_iff_not_of_iff mem_singleton theorem mem_singleton_self (a : Ξ±) : a ∈ ΞΉ a := or.inl rfl theorem singleton_inj {a b : Ξ±} : ΞΉ a = ΞΉ b ↔ a = b := ⟨λ h, mem_singleton.1 (h β–Έ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : Ξ±) : ΞΉ a β‰  βˆ… := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : Ξ±) : ↑(ΞΉ a) = ({a} : set Ξ±) := rfl /- 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⟩⟩ @[simp] theorem has_insert_eq_insert (a : Ξ±) (s : finset Ξ±) : has_insert.insert a s = insert a s := rfl 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] @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h theorem insert.comm (a b : Ξ±) (s : finset Ξ±) : insert a (insert b s) = insert b (insert a s) := ext.2 $ Ξ» 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.2 $ Ξ» 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) 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 [h : decidable_eq Ξ±] (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₁, hβ‚‚βŸ©, have βˆƒa ∈ t, a βˆ‰ s, by simpa only [finset.subset_iff, classical.not_forall] using hβ‚‚, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, hβ‚βŸ©βŸ©) (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 @[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 @[simp] theorem singleton_eq_singleton (a : Ξ±) : _root_.singleton a = ΞΉ a := rfl @[simp] theorem insert_empty_eq_singleton (a : Ξ±) : {a} = ΞΉ a := rfl @[simp] theorem insert_singleton_self_eq (a : Ξ±) : ({a, a} : finset Ξ±) = ΞΉ a := insert_eq_of_mem $ mem_singleton_self _ /- 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 _ @[simp] theorem union_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ βˆͺ sβ‚‚ = sβ‚‚ βˆͺ s₁ := ext.2 $ Ξ» 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.2 $ Ξ» x, by simp only [mem_union, or_assoc] instance : is_associative (finset Ξ±) (βˆͺ) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset Ξ±) : s βˆͺ s = s := ext.2 $ Ξ» _, 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.2 $ Ξ» _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := ext.2 $ Ξ» x, by simp only [mem_union, or_assoc, or_comm (x ∈ sβ‚‚)] @[simp] theorem union_self (s : finset Ξ±) : s βˆͺ s = s := union_idempotent s @[simp] theorem union_empty (s : finset Ξ±) : s βˆͺ βˆ… = s := ext.2 $ Ξ» x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset Ξ±) : βˆ… βˆͺ s = s := ext.2 $ Ξ» 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] /- 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 inter_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ = sβ‚‚ ∩ s₁ := ext.2 $ Ξ» _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = s₁ ∩ (sβ‚‚ ∩ s₃) := ext.2 $ Ξ» _, by simp only [mem_inter, and_assoc] @[simp] theorem inter_left_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := ext.2 $ Ξ» _, by simp only [mem_inter, and.left_comm] @[simp] theorem inter_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := ext.2 $ Ξ» _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset Ξ±) : s ∩ s = s := ext.2 $ Ξ» _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset Ξ±) : s ∩ βˆ… = βˆ… := ext.2 $ Ξ» _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset Ξ±) : βˆ… ∩ s = βˆ… := ext.2 $ Ξ» _, mem_inter.trans $ false_and _ @[simp] theorem insert_inter_of_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a ∈ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := ext.2 $ Ξ» 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.2 $ Ξ» 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] /- 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.lattice } instance {Ξ± : Type*} [decidable_eq Ξ±] : semilattice_sup_bot (finset Ξ±) := { ..finset.lattice.semilattice_inf_bot, ..finset.lattice.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.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 /- 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.2 $ 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.2 $ 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 @[simp] theorem sdiff_union_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : (sβ‚‚ \ s₁) βˆͺ s₁ = sβ‚‚ := ext.2 $ Ξ» 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) @[simp] theorem union_sdiff_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ βˆͺ (sβ‚‚ \ s₁) = sβ‚‚ := (union_comm _ _).trans (sdiff_union_of_subset h) @[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 _ _) 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β‚‚) @[simp] lemma coe_sdiff (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ \ sβ‚‚) = (↑s₁ \ ↑sβ‚‚ : set Ξ±) := set.ext $ Ξ» _, mem_sdiff 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 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_filter (s : finset Ξ±) : (s.filter p).filter q = s.filter (Ξ»a, p a ∧ q a) := ext.2 $ 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.2 $ 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 variable [decidable_eq Ξ±] theorem filter_union (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).filter p = s₁.filter p βˆͺ sβ‚‚.filter p := ext.2 $ Ξ» _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_or (s : finset Ξ±) : s.filter (Ξ» a, p a ∨ q a) = s.filter p βˆͺ s.filter q := ext.2 $ Ξ» _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and (s : finset Ξ±) : s.filter (Ξ» a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ Ξ» _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not (s : finset Ξ±) : s.filter (Ξ» a, Β¬ p a) = s \ s.filter p := ext.2 $ 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.2 $ Ξ» _, by simp only [mem_sdiff, mem_filter] theorem filter_union_filter_neg_eq (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] @[simp] lemma coe_filter (s : finset Ξ±) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set Ξ±) := set.ext $ Ξ» _, mem_filter 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 range_subset {n m} : range n βŠ† range m ↔ n ≀ m := range_subset theorem exists_nat_subset_range (s : finset β„•) : βˆƒn : β„•, s βŠ† range n := finset.induction_on s ⟨0, empty_subset _⟩ $ Ξ» a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa only [mem_range] using le_max_left (a+1) n, subset.trans hn (by simpa only [range_subset] using le_max_right (a+1) n)⟩⟩ 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 namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option Ξ±) : finset Ξ± := match o with | none := βˆ… | some a := finset.singleton a end @[simp] theorem to_finset_none : none.to_finset = (βˆ… : finset Ξ±) := rfl @[simp] theorem to_finset_some {a : Ξ±} : (some a).to_finset = finset.singleton 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 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 section map open function 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 : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (βˆ… : 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 @[simp] theorem mem_map_of_mem (f : Ξ± β†ͺ Ξ²) {a} {s : finset Ξ±} : a ∈ s β†’ f a ∈ s.map f := (mem_map' _).2 theorem map_to_finset [decidable_eq Ξ±] [decidable_eq Ξ²] {s : multiset Ξ±} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ Ξ» _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ Ξ» _, 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] 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.2 $ Ξ» 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.2 $ Ξ» _, 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.2 $ Ξ» 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 : Ξ±) : (singleton a).map f = singleton (f a) := ext.2 $ Ξ» _, 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, insert_empty_eq_singleton, 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 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] @[simp] 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 $ by simp only [exists_prop]; refl theorem image_to_finset [decidable_eq Ξ±] {s : multiset Ξ±} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ Ξ» _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] @[simp] 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.2 $ Ξ» _, 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_filter {p : Ξ² β†’ Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ Ξ» 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.2 $ Ξ» _, 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.2 $ 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 [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (a : Ξ±) : (singleton a).image f = singleton (f a) := ext.2 $ Ξ» 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, insert_empty_eq_singleton, 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.2 $ Ξ» ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (assume h : x = a, Ξ» _, mem_insert.2 $ or.inl $ subtype.eq h) (assume 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 [decidable_eq Ξ²] {s : finset Ξ±} (h : s β‰  βˆ…) (b : Ξ²) : s.image (Ξ»a, b) = singleton b := ext.2 $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, exists_mem_of_ne_empty h, true_and, mem_singleton, eq_comm] 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] 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 β‰  βˆ… := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_eq_one {s : finset Ξ±} : s.card = 1 ↔ βˆƒ a, s = finset.singleton a := by cases s; simp [multiset.card_eq_one, finset.singleton, finset.card] @[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 (singleton a) = 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 @[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 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 lemma card_eq_of_bijective [decidable_eq Ξ±] {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 := 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, 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 lemma card_eq_succ [decidable_eq Ξ±] {s : finset Ξ±} {a : Ξ±} {n : β„•} : s.card = n + 1 ↔ (βˆƒa t, a βˆ‰ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have card s > 0, from eq.symm β–Έ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ 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 [decidable_eq Ξ±] {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 [decidable_eq Ξ±] [decidable_eq Ξ²] {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 := 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 lemma card_le_of_inj_on [decidable_eq Ξ±] {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β‚‚) @[elab_as_eliminator] lemma 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.2 $ Ξ» 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, by by_cases a ∈ s; simp * {contextual := tt}) 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 end card 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.2 $ Ξ» x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext.2 $ Ξ» x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind [decidable_eq Ξ±] {a : Ξ±} : (singleton a).bind t = t a := show (insert a βˆ… : finset Ξ±).bind t = t a, from bind_insert.trans $ union_empty _ 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.2 $ Ξ» 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_singleton {f : Ξ± β†’ Ξ²} : s.bind (Ξ»a, {f a}) = s.image f := ext.2 $ Ξ» x, by simp only [mem_bind, mem_image, insert_empty_eq_singleton, 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, simp, split, { rintros ⟨a, ⟨b', _, _⟩, hb, _⟩, exact hb }, { rintros hb, exact ⟨g b, ⟨b, hb, rfl⟩, hb, rfl⟩ } end end bind 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.2 $ Ξ» ⟨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 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.2 $ Ξ» ⟨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 section pi variables {Ξ΄ : Ξ± β†’ Type*} [decidable_eq Ξ±] 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 _ _ _ def pi.empty (Ξ² : Ξ± β†’ Sort*) [decidable_eq Ξ±] (a : Ξ±) (h : a ∈ (βˆ… : finset Ξ±)) : Ξ² a := multiset.pi.empty Ξ² a h 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), by 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 end pi section powerset 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] 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) end powerset 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 : (singleton a).fold op b f = f a * b := rfl @[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 := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp only [map_cons, fold_cons_left, fold] end fold 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 [decidable_eq Ξ²] {b : Ξ²} : ({b} : finset Ξ²).sup f = f b := calc _ = f b βŠ” (βˆ…:finset Ξ²).sup f : sup_insert ... = f b : sup_bot_eq 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 lemma sup_mono_fun {g : Ξ² β†’ Ξ±} : (βˆ€b∈s, f b ≀ g b) β†’ s.sup f ≀ s.sup g := by letI := classical.dec_eq Ξ²; from finset.induction_on s (Ξ» _, le_refl _) (Ξ» a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le_sup H.1 (ih H.2)) lemma le_sup {b : Ξ²} (hb : b ∈ s) : f b ≀ s.sup f := by letI := classical.dec_eq Ξ²; from calc f b ≀ f b βŠ” s.sup f : le_sup_left ... = (insert b s).sup f : sup_insert.symm ... = s.sup f : by rw [insert_eq_of_mem hb] lemma sup_le {a : Ξ±} : (βˆ€b ∈ s, f b ≀ a) β†’ s.sup f ≀ a := by letI := classical.dec_eq Ξ²; from finset.induction_on s (Ξ» _, bot_le) (Ξ» n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le H.1 (ih H.2)) lemma sup_le_iff {a : Ξ±} : s.sup f ≀ a ↔ (βˆ€b ∈ s, f b ≀ a) := iff.intro (assume h b hb, le_trans (le_sup hb) h) sup_le lemma sup_mono (h : s₁ βŠ† sβ‚‚) : s₁.sup f ≀ sβ‚‚.sup f := sup_le $ assume b hb, le_sup (h hb) lemma sup_lt [is_total Ξ± (≀)] {a : Ξ±} : (βŠ₯ < a) β†’ (βˆ€b ∈ s, f b < a) β†’ s.sup f < a := by letI := classical.dec_eq Ξ²; from finset.induction_on s (by simp) (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}) 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) 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 @[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 [decidable_eq Ξ²] {b : Ξ²} : ({b} : finset Ξ²).inf f = f b := calc _ = f b βŠ“ (βˆ…:finset Ξ²).inf f : inf_insert ... = f b : inf_top_eq 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_mono_fun {g : Ξ² β†’ Ξ±} : (βˆ€b∈s, f b ≀ g b) β†’ s.inf f ≀ s.inf g := by letI := classical.dec_eq Ξ²; from finset.induction_on s (Ξ» _, le_refl _) (Ξ» a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact inf_le_inf H.1 (ih H.2)) lemma inf_le {b : Ξ²} (hb : b ∈ s) : s.inf f ≀ f b := by letI := classical.dec_eq Ξ²; from calc f b β‰₯ f b βŠ“ s.inf f : inf_le_left ... = (insert b s).inf f : inf_insert.symm ... = s.inf f : by rw [insert_eq_of_mem hb] lemma le_inf {a : Ξ±} : (βˆ€b ∈ s, a ≀ f b) β†’ a ≀ s.inf f := by letI := classical.dec_eq Ξ²; from finset.induction_on s (Ξ» _, le_top) (Ξ» n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact le_inf H.1 (ih H.2)) lemma le_inf_iff {a : Ξ±} : a ≀ s.inf f ↔ (βˆ€b ∈ s, a ≀ f b) := iff.intro (assume h b hb, le_trans h (inf_le hb)) le_inf 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 Ξ±] 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 := max_insert @[simp] theorem max_singleton' {a : Ξ±} : finset.max (singleton a) = some a := max_singleton 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_ne_empty {s : finset Ξ±} (h : s β‰  βˆ…) : βˆƒ a, a ∈ s.max := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in max_of_mem ha theorem max_eq_none {s : finset Ξ±} : s.max = none ↔ s = βˆ… := ⟨λ h, by_contradiction $ Ξ» hs, let ⟨a, ha⟩ := max_of_ne_empty hs 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 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 := 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_ne_empty {s : finset Ξ±} (h : s β‰  βˆ…) : βˆƒ a, a ∈ s.min := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in min_of_mem ha theorem min_eq_none {s : finset Ξ±} : s.min = none ↔ s = βˆ… := ⟨λ h, by_contradiction $ Ξ» hs, let ⟨a, ha⟩ := min_of_ne_empty hs 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 le_min_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 end max_min 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 _ end sort 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 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] @[simp] theorem card_disjoint_union {s t : finset Ξ±} : disjoint s t β†’ card (s βˆͺ t) = card s + card t := finset.induction_on s (Ξ» _, by simp only [empty_union, card_empty, zero_add]) $ Ξ» a s ha ih H, have h1 : a βˆ‰ s βˆͺ t, from Ξ» h, or.elim (mem_union.1 h) ha (disjoint_insert_left.1 H).1, by rw [insert_union, card_insert_of_not_mem h1, card_insert_of_not_mem ha, ih (disjoint_insert_left.1 H).2, add_right_comm] end disjoint theorem sort_sorted_lt [decidable_linear_order Ξ±] (s : finset Ξ±) : list.sorted (<) (sort (≀) s) := (sort_sorted _ _).impβ‚‚ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr Ξ±] : has_repr (finset Ξ±) := ⟨λ s, repr s.1⟩ 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 _ _ _ section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : finset Ξ±) def choose_x (hp : (βˆƒ! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp 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 {Ξ±} [decidable_eq Ξ±] : 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 section decidable_linear_order variables {Ξ±} [decidable_linear_order Ξ±] def min' (S : finset Ξ±) (H : S β‰  βˆ…) : Ξ± := @option.get _ S.min $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] def max' (S : finset Ξ±) (H : S β‰  βˆ…) : Ξ± := @option.get _ S.max $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (S : finset Ξ±) (H : S β‰  βˆ…) theorem min'_mem : S.min' H ∈ S := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ S) : S.min' H ≀ x := le_min_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 end decidable_linear_order /- Ico (a closed openinterval) -/ 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 map_add (n m k : β„•) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] 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 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] 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 eq_cons {n m : β„•} (h : n < m) : Ico n m = insert n (Ico (n + 1) m) := by rw [← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] theorem pred_singleton {m : β„•} (h : m > 0) : 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_ge_of_le_bot {n m l : β„•} (hln : l ≀ n) : (Ico n m).filter (Ξ» x, x β‰₯ l) = Ico n m := eq_of_veq $ multiset.Ico.filter_ge_of_le_bot hln lemma filter_ge_of_top_le {n m l : β„•} (hml : m ≀ l) : (Ico n m).filter (Ξ» x, x β‰₯ l) = βˆ… := eq_of_veq $ multiset.Ico.filter_ge_of_top_le hml lemma filter_ge_of_ge {n m l : β„•} (hnl : n ≀ l) : (Ico n m).filter (Ξ» x, x β‰₯ l) = Ico l m := eq_of_veq $ multiset.Ico.filter_ge_of_ge hnl @[simp] lemma filter_ge (n m l : β„•) : (Ico n m).filter (Ξ» x, x β‰₯ l) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_ge 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] end Ico end finset 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 namespace lattice variables {ΞΉ : Sort*} [complete_lattice Ξ±] [decidable_eq ΞΉ] lemma supr_eq_supr_finset (s : ΞΉ β†’ Ξ±) : (⨆i, s i) = (⨆t:finset (plift ΞΉ), ⨆i∈t, s (plift.down i)) := 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 _ _) lemma infi_eq_infi_finset (s : ΞΉ β†’ Ξ±) : (β¨…i, s i) = (β¨…t:finset (plift ΞΉ), β¨…i∈t, s (plift.down i)) := 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 lattice namespace set variables {ΞΉ : Sort*} [decidable_eq ΞΉ] lemma Union_eq_Union_finset (s : ΞΉ β†’ set Ξ±) : (⋃i, s i) = (⋃t:finset (plift ΞΉ), ⋃i∈t, s (plift.down i)) := lattice.supr_eq_supr_finset s lemma Inter_eq_Inter_finset (s : ΞΉ β†’ set Ξ±) : (β‹‚i, s i) = (β‹‚t:finset (plift ΞΉ), β‹‚i∈t, s (plift.down i)) := lattice.infi_eq_infi_finset s end set
12e4f45739a311f288a382c954c7137ff046517b
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Init/Data/Nat/Log2.lean
2148bd433cb7143f22a52e04a88c9b8b90ff6824
[ "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
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
816
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ prelude import Init.NotationExtra namespace Nat private theorem log2_terminates : βˆ€ n, n β‰₯ 2 β†’ n / 2 < n | 2, _ => by decide | 3, _ => by decide | n+4, _ => by rw [div_eq, if_pos] refine succ_lt_succ (Nat.lt_trans ?_ (lt_succ_self _)) exact log2_terminates (n+2) (succ_lt_succ (zero_lt_succ _)) exact ⟨by decide, succ_lt_succ (zero_lt_succ _)⟩ /-- Computes `⌊max 0 (logβ‚‚ n)βŒ‹`. `log2 0 = log2 1 = 0`, `log2 2 = 1`, ..., `log2 (2^i) = i`, etc. -/ @[extern "lean_nat_log2"] def log2 (n : @& Nat) : Nat := if h : n β‰₯ 2 then log2 (n / 2) + 1 else 0 termination_by measure id decreasing_by exact log2_terminates _ h
e40179ff127ceb1e7a18b17bf12f9b7599323873
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/prod_auto.lean
92a58768188e12ffb9ab1abf511dcd43486e4d4d
[]
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
7,934
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.basic import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u_5 u_6 namespace Mathlib /-! # Extra facts about `prod` This file defines `prod.swap : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ±` and proves various simple lemmas about `prod`. -/ @[simp] theorem prod_map {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) (p : Ξ± Γ— Ξ²) : prod.map f g p = (f (prod.fst p), g (prod.snd p)) := rfl namespace prod @[simp] theorem forall {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆ€ (x : Ξ± Γ— Ξ²), p x) ↔ βˆ€ (a : Ξ±) (b : Ξ²), p (a, b) := sorry @[simp] theorem exists {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆƒ (x : Ξ± Γ— Ξ²), p x) ↔ βˆƒ (a : Ξ±), βˆƒ (b : Ξ²), p (a, b) := sorry theorem forall' {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± β†’ Ξ² β†’ Prop} : (βˆ€ (x : Ξ± Γ— Ξ²), p (fst x) (snd x)) ↔ βˆ€ (a : Ξ±) (b : Ξ²), p a b := forall theorem exists' {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± β†’ Ξ² β†’ Prop} : (βˆƒ (x : Ξ± Γ— Ξ²), p (fst x) (snd x)) ↔ βˆƒ (a : Ξ±), βˆƒ (b : Ξ²), p a b := exists @[simp] theorem map_mk {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) (a : Ξ±) (b : Ξ²) : map f g (a, b) = (f a, g b) := rfl theorem map_fst {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) (p : Ξ± Γ— Ξ²) : fst (map f g p) = f (fst p) := rfl theorem map_snd {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) (p : Ξ± Γ— Ξ²) : snd (map f g p) = g (snd p) := rfl theorem map_fst' {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) : fst ∘ map f g = f ∘ fst := funext (map_fst f g) theorem map_snd' {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) : snd ∘ map f g = g ∘ snd := funext (map_snd f g) /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions. -/ theorem map_comp_map {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} {Ξ΅ : Type u_5} {ΞΆ : Type u_6} (f : Ξ± β†’ Ξ²) (f' : Ξ³ β†’ Ξ΄) (g : Ξ² β†’ Ξ΅) (g' : Ξ΄ β†’ ΞΆ) : map g g' ∘ map f f' = map (g ∘ f) (g' ∘ f') := rfl /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions, fully applied. -/ theorem map_map {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} {Ξ΅ : Type u_5} {ΞΆ : Type u_6} (f : Ξ± β†’ Ξ²) (f' : Ξ³ β†’ Ξ΄) (g : Ξ² β†’ Ξ΅) (g' : Ξ΄ β†’ ΞΆ) (x : Ξ± Γ— Ξ³) : map g g' (map f f' x) = map (g ∘ f) (g' ∘ f') x := rfl @[simp] theorem mk.inj_iff {Ξ± : Type u_1} {Ξ² : Type u_2} {a₁ : Ξ±} {aβ‚‚ : Ξ±} {b₁ : Ξ²} {bβ‚‚ : Ξ²} : (a₁, b₁) = (aβ‚‚, bβ‚‚) ↔ a₁ = aβ‚‚ ∧ b₁ = bβ‚‚ := sorry theorem mk.inj_left {Ξ± : Type u_1} {Ξ² : Type u_2} (a : Ξ±) : function.injective (Prod.mk a) := sorry theorem mk.inj_right {Ξ± : Type u_1} {Ξ² : Type u_2} (b : Ξ²) : function.injective fun (a : Ξ±) => (a, b) := sorry theorem ext_iff {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {q : Ξ± Γ— Ξ²} : p = q ↔ fst p = fst q ∧ snd p = snd q := sorry theorem ext {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {q : Ξ± Γ— Ξ²} (h₁ : fst p = fst q) (hβ‚‚ : snd p = snd q) : p = q := iff.mpr ext_iff { left := h₁, right := hβ‚‚ } theorem map_def {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} : map f g = fun (p : Ξ± Γ— Ξ²) => (f (fst p), g (snd p)) := funext fun (p : Ξ± Γ— Ξ²) => ext (map_fst f g p) (map_snd f g p) theorem id_prod {Ξ± : Type u_1} : (fun (p : Ξ± Γ— Ξ±) => (fst p, snd p)) = id := sorry theorem fst_surjective {Ξ± : Type u_1} {Ξ² : Type u_2} [h : Nonempty Ξ²] : function.surjective fst := fun (x : Ξ±) => nonempty.elim h fun (y : Ξ²) => Exists.intro (x, y) rfl theorem snd_surjective {Ξ± : Type u_1} {Ξ² : Type u_2} [h : Nonempty Ξ±] : function.surjective snd := fun (y : Ξ²) => nonempty.elim h fun (x : Ξ±) => Exists.intro (x, y) rfl theorem fst_injective {Ξ± : Type u_1} {Ξ² : Type u_2} [subsingleton Ξ²] : function.injective fst := fun (x y : Ξ± Γ— Ξ²) (h : fst x = fst y) => ext h (subsingleton.elim (snd x) (snd y)) theorem snd_injective {Ξ± : Type u_1} {Ξ² : Type u_2} [subsingleton Ξ±] : function.injective snd := fun (x y : Ξ± Γ— Ξ²) (h : snd x = snd y) => ext (subsingleton.elim (fst x) (fst y)) h /-- Swap the factors of a product. `swap (a, b) = (b, a)` -/ def swap {Ξ± : Type u_1} {Ξ² : Type u_2} : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ± := fun (p : Ξ± Γ— Ξ²) => (snd p, fst p) @[simp] theorem swap_swap {Ξ± : Type u_1} {Ξ² : Type u_2} (x : Ξ± Γ— Ξ²) : swap (swap x) = x := cases_on x fun (x_fst : Ξ±) (x_snd : Ξ²) => idRhs (swap (swap (x_fst, x_snd)) = swap (swap (x_fst, x_snd))) rfl @[simp] theorem fst_swap {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} : fst (swap p) = snd p := rfl @[simp] theorem snd_swap {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} : snd (swap p) = fst p := rfl @[simp] theorem swap_prod_mk {Ξ± : Type u_1} {Ξ² : Type u_2} {a : Ξ±} {b : Ξ²} : swap (a, b) = (b, a) := rfl @[simp] theorem swap_swap_eq {Ξ± : Type u_1} {Ξ² : Type u_2} : swap ∘ swap = id := funext swap_swap @[simp] theorem swap_left_inverse {Ξ± : Type u_1} {Ξ² : Type u_2} : function.left_inverse swap swap := swap_swap @[simp] theorem swap_right_inverse {Ξ± : Type u_1} {Ξ² : Type u_2} : function.right_inverse swap swap := swap_swap theorem swap_injective {Ξ± : Type u_1} {Ξ² : Type u_2} : function.injective swap := function.left_inverse.injective swap_left_inverse theorem swap_surjective {Ξ± : Type u_1} {Ξ² : Type u_2} : function.surjective swap := function.left_inverse.surjective swap_left_inverse theorem swap_bijective {Ξ± : Type u_1} {Ξ² : Type u_2} : function.bijective swap := { left := swap_injective, right := swap_surjective } @[simp] theorem swap_inj {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {q : Ξ± Γ— Ξ²} : swap p = swap q ↔ p = q := function.injective.eq_iff swap_injective theorem eq_iff_fst_eq_snd_eq {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {q : Ξ± Γ— Ξ²} : p = q ↔ fst p = fst q ∧ snd p = snd q := sorry theorem fst_eq_iff {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {x : Ξ±} : fst p = x ↔ p = (x, snd p) := sorry theorem snd_eq_iff {Ξ± : Type u_1} {Ξ² : Type u_2} {p : Ξ± Γ— Ξ²} {x : Ξ²} : snd p = x ↔ p = (fst p, x) := sorry theorem lex_def {Ξ± : Type u_1} {Ξ² : Type u_2} (r : Ξ± β†’ Ξ± β†’ Prop) (s : Ξ² β†’ Ξ² β†’ Prop) {p : Ξ± Γ— Ξ²} {q : Ξ± Γ— Ξ²} : lex r s p q ↔ r (fst p) (fst q) ∨ fst p = fst q ∧ s (snd p) (snd q) := sorry protected instance lex.decidable {Ξ± : Type u_1} {Ξ² : Type u_2} [DecidableEq Ξ±] (r : Ξ± β†’ Ξ± β†’ Prop) (s : Ξ² β†’ Ξ² β†’ Prop) [DecidableRel r] [DecidableRel s] : DecidableRel (lex r s) := fun (p q : Ξ± Γ— Ξ²) => decidable_of_decidable_of_iff or.decidable sorry end prod theorem function.injective.prod_map {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} (hf : function.injective f) (hg : function.injective g) : function.injective (prod.map f g) := fun (x y : Ξ± Γ— Ξ²) (h : prod.map f g x = prod.map f g y) => prod.ext (hf (and.left (iff.mp prod.ext_iff h))) (hg (and.right (iff.mp prod.ext_iff h))) theorem function.surjective.prod_map {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} {Ξ΄ : Type u_4} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} (hf : function.surjective f) (hg : function.surjective g) : function.surjective (prod.map f g) := sorry end Mathlib
e1b04afedda3c338b28249a0382d4d685d4618e4
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/test/qpf.lean
f3a358092fb2e89dc79dba3dd31eaea9d311d94c
[ "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
6,161
lean
import data.qpf.univariate.basic import control.bifunctor universes u variables {F : Type u β†’ Type u} [functor F] namespace qpf section box variables (F) /-- apply a functor to a set of values. taken from [Basil FΓΌrer, Andreas Lochbihler, Joshua Schneider, Dmitriy Traytel *Quotients of Bounded Natural Functors*][fuerer-lochbihler-schneider-traytel2020] henceforth referred to as the QBNF paper -/ def box {Ξ±} (A : set Ξ±) : set (F Ξ±) := { x | βˆ€ Ξ² (f g : Ξ± β†’ Ξ²), (βˆ€ a ∈ A, f a = g a) β†’ f <$> x = g <$> x } variables {F} /-- Alternate notion of support set based on `box`. Taken from the QBNF paper -/ def supp' {Ξ±} (x : F Ξ±) : set Ξ± := β‹‚ A ∈ { A : set Ξ± | x ∈ box F A}, A /-- Alternate notion of predicate lifting based on `box`. Taken from the QBNF paper -/ def liftp' {Ξ±} (x : F Ξ±) (p : Ξ± β†’ Prop) : Prop := βˆ€ a ∈ supp' x, p a end box end qpf namespace ex /-- polynomial functor isomorph to `Ξ± Γ— _` for some `Ξ±` -/ def prod.pfunctor (Ξ± : Type) : pfunctor := ⟨ Ξ±, Ξ» _, unit ⟩ instance {Ξ±} : qpf (prod Ξ±) := { P := prod.pfunctor Ξ±, abs := Ξ» Ξ² ⟨a,f⟩, (a, f ()), repr := Ξ» Ξ² ⟨x,y⟩, ⟨x, Ξ» _, y⟩, abs_repr := Ξ» Ξ² ⟨x,y⟩, rfl, abs_map := Ξ» Ξ² Ξ³ f ⟨a,g⟩, rfl } /-- example relation for products -/ def foo.R (Ξ± : Type) (x y : bool Γ— Ξ±) : Prop := x.1 = y.1 ∧ (x.1 β†’ x.2 = y.2) lemma equivalence_foo.R (Ξ±) : equivalence (foo.R Ξ±) := begin refine ⟨_,_,_⟩, { intro, exact ⟨rfl,Ξ» _, rfl⟩ }, { intros x y h, refine ⟨h.1.symm, Ξ» _, (h.2 _).symm⟩, rwa h.1 }, { rintros x y z ⟨ha,ha'⟩ ⟨hb,hb'⟩, refine ⟨ha.trans hb, Ξ» hh, _⟩, refine (ha' hh).trans (hb' _), rwa ← ha } end /-- example of a qpf -/ def foo (Ξ± : Type) := quot $ foo.R Ξ± instance {Ξ±} [inhabited Ξ±] : inhabited (foo Ξ±) := ⟨ quot.mk _ (default _) ⟩ /-- functor operation of `foo` -/ def foo.map {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (x : foo Ξ±) : foo Ξ² := quot.lift_on x (Ξ» x : bool Γ— Ξ±, quot.mk (foo.R Ξ²) $ f <$> x) (Ξ» ⟨aβ‚€,aβ‚βŸ© ⟨bβ‚€,bβ‚βŸ© h, quot.sound ⟨h.1,Ξ» h', show f a₁ = f b₁, from congr_arg f (h.2 h')⟩) instance : functor foo := { map := @foo.map } @[simp] lemma foo.map_mk {Ξ± Ξ² : Type} (f : Ξ± β†’ Ξ²) (x : bool Γ— Ξ±) : (f <$> quot.mk _ x : foo Ξ²) = quot.mk _ (f <$> x) := by simp [(<$>),foo.map] noncomputable instance qpf.foo : qpf foo := @qpf.quotient_qpf (prod bool) _ ex.prod.qpf foo _ (Ξ» Ξ±, quot.mk _) (Ξ» Ξ±, quot.out) (by simp) (by intros; simp) /-- constructor for `foo` -/ def foo.mk {Ξ±} (b : bool) (x : Ξ±) : foo Ξ± := quot.mk _ (b, x) @[simp] lemma foo.map_mk' {Ξ± Ξ² : Type} (f : Ξ± β†’ Ξ²) (b : bool) (x : Ξ±) : f <$> foo.mk b x = foo.mk b (f x) := by simp only [foo.mk, foo.map_mk]; refl @[simp] lemma foo.map_tt {Ξ± : Type} (x y : Ξ±) : foo.mk tt x = foo.mk tt y ↔ x = y := by simp [foo.mk]; split; intro h; [replace h := quot.exact _ h, rw h]; rw (equivalence_foo.R _).eqv_gen_iff at h; exact h.2 rfl /-- consequence of original definition of `supp`. If there exists more than one value of type `Ξ±`, then the support of `foo.mk ff x` is empty -/ lemma supp_mk_ffβ‚€ {Ξ±} (x y : Ξ±) (h : Β¬ x = y) : functor.supp (foo.mk ff x) = {} := begin dsimp [functor.supp], ext z, simp, -- split; intro h, classical, by_cases x = z, { use (Ξ» a, Β¬ z = a), subst z, dsimp [functor.liftp], simp, refine ⟨foo.mk ff ⟨y,h⟩,_⟩, simp, apply quot.sound, simp [foo.R] }, { use (Ξ» a, x = a), dsimp [functor.liftp], simp [h], use foo.mk ff ⟨x,rfl⟩, simp } end /-- consequence of original definition of `supp`. If there exists only one value of type `Ξ±`, then the support of `foo.mk ff x` contains that value -/ lemma supp_mk_ff₁ {Ξ±} (x : Ξ±) (h : βˆ€ z, x = z) : functor.supp (foo.mk ff x) = {x} := begin dsimp [functor.supp], ext y, simp, split; intro h', { apply @h' (= x), dsimp [functor.liftp], use foo.mk ff ⟨x,rfl⟩, refl }, { introv hp, simp [functor.liftp] at hp, rcases hp with ⟨⟨z,z',hz⟩,hp⟩, simp at hp, convert hz, rw [h'], apply h }, end /-- Such a QPF is not uniform -/ lemma foo_not_uniform : Β¬ @qpf.is_uniform foo _ qpf.foo := begin simp only [qpf.is_uniform, foo, qpf.foo, set.image_univ, not_forall, not_imp], existsi [bool,ff,ff,Ξ» a : unit, tt,Ξ» a : unit, ff], split, { apply quot.sound, simp [foo.R, qpf.abs, prod.qpf._match_1] }, { simp! only [set.range, set.ext_iff], simp only [not_exists, false_iff, bool.forall_bool, eq_self_iff_true, exists_false, not_true, and_self, set.mem_set_of_eq, iff_false], exact Ξ» h, h () } end /-- intuitive consequence of original definition of `supp`. -/ lemma supp_mk_tt {Ξ±} (x : Ξ±) : functor.supp (foo.mk tt x) = {x} := begin dsimp [functor.supp], ext y, simp, split; intro h', { apply @h' (= x), dsimp [functor.liftp], use foo.mk tt ⟨x,rfl⟩, refl }, { introv hp, simp [functor.liftp] at hp, rcases hp with ⟨⟨z,z',hz⟩,hp⟩, simp at hp, replace hp := quot.exact _ hp, rw (equivalence_foo.R _).eqv_gen_iff at hp, rcases hp with ⟨⟨⟩,hp⟩, subst y, replace hp := hp rfl, cases hp, exact hz } end /-- simple consequence of the definition of `supp` from the QBNF paper -/ lemma supp_mk_ff' {Ξ±} (x : Ξ±) : qpf.supp' (foo.mk ff x) = {} := begin dsimp [qpf.supp'], ext, simp, dsimp [qpf.box], use βˆ…, simp [foo.mk], intros, apply quot.sound, dsimp [foo.R], split, refl, rintro ⟨ ⟩ end /-- simple consequence of the definition of `supp` from the QBNF paper -/ lemma supp_mk_tt' {Ξ±} (x : Ξ±) : qpf.supp' (foo.mk tt x) = {x} := begin dsimp [qpf.supp'], ext, simp, dsimp [qpf.box], split; intro h, { specialize h {x} _, { clear h, introv hfg, simp, rw hfg, simp }, { simp at h, assumption }, }, { introv hfg, subst x_1, classical, let f : Ξ± β†’ Ξ± βŠ• bool := Ξ» x, if x ∈ i then sum.inl x else sum.inr tt, let g : Ξ± β†’ Ξ± βŠ• bool := Ξ» x, if x ∈ i then sum.inl x else sum.inr ff, specialize hfg _ f g _, { intros, simp [*,f,g,if_pos] }, { simp [f,g] at hfg, split_ifs at hfg, assumption, cases hfg } } end end ex
19a65c7bf9896f3ca3367f11ca960a116d0ff449
8b9f17008684d796c8022dab552e42f0cb6fb347
/hott/algebra/precategory/constructions.hlean
67f76c39abf693963a941615ee46ea0b00364ba3
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,929
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.precategory.constructions Authors: Floris van Doorn, Jakob von Raumer This file contains basic constructions on precategories, including common precategories -/ import .nat_trans import types.prod types.sigma types.pi open eq prod eq eq.ops equiv is_trunc namespace category namespace opposite definition opposite [reducible] {ob : Type} (C : precategory ob) : precategory ob := precategory.mk (Ξ» a b, hom b a) (Ξ» a b, !homH) (Ξ» a b c f g, g ∘ f) (Ξ» a, id) (Ξ» a b c d f g h, !assoc⁻¹) (Ξ» a b f, !id_right) (Ξ» a b f, !id_left) definition Opposite [reducible] (C : Precategory) : Precategory := precategory.Mk (opposite C) infixr `∘op`:60 := @comp _ (opposite _) _ _ _ variables {C : Precategory} {a b c : C} set_option apply.class_instance false -- disable class instance resolution in the apply tactic definition compose_op {f : hom a b} {g : hom b c} : f ∘op g = g ∘ f := idp -- TODO: Decide whether just to use funext for this theorem or -- take the trick they use in Coq-HoTT, and introduce a further -- axiom in the definition of precategories that provides thee -- symmetric associativity proof. definition opposite_opposite' {ob : Type} (C : precategory ob) : opposite (opposite C) = C := begin apply (precategory.rec_on C), intros [hom', homH', comp', ID', assoc', id_left', id_right'], apply (ap (Ξ»assoc'', precategory.mk hom' @homH' comp' ID' assoc'' id_left' id_right')), repeat (apply eq_of_homotopy ; intros ), apply ap, apply (@is_hset.elim), apply !homH', end definition opposite_opposite : Opposite (Opposite C) = C := (ap (Precategory.mk C) (opposite_opposite' C)) ⬝ !Precategory.eta end opposite -- Note: Discrete precategory doesn't really make sense in HoTT, -- We'll define a discrete _category_ later. /-section open decidable unit empty variables {A : Type} [H : decidable_eq A] include H definition set_hom (a b : A) := decidable.rec_on (H a b) (Ξ»h, unit) (Ξ»h, empty) theorem set_hom_subsingleton [instance] (a b : A) : subsingleton (set_hom a b) := _ definition set_compose {a b c : A} (g : set_hom b c) (f : set_hom a b) : set_hom a c := decidable.rec_on (H b c) (Ξ» Hbc g, decidable.rec_on (H a b) (Ξ» Hab f, rec_on_true (trans Hab Hbc) ⋆) (Ξ»h f, empty.rec _ f) f) (Ξ»h (g : empty), empty.rec _ g) g omit H definition discrete_precategory (A : Type) [H : decidable_eq A] : precategory A := mk (Ξ»a b, set_hom a b) (Ξ» a b c g f, set_compose g f) (Ξ» a, decidable.rec_on_true rfl ⋆) (Ξ» a b c d h g f, @subsingleton.elim (set_hom a d) _ _ _) (Ξ» a b f, @subsingleton.elim (set_hom a b) _ _ _) (Ξ» a b f, @subsingleton.elim (set_hom a b) _ _ _) definition Discrete_category (A : Type) [H : decidable_eq A] := Mk (discrete_category A) end section open unit bool definition category_one := discrete_category unit definition Category_one := Mk category_one definition category_two := discrete_category bool definition Category_two := Mk category_two end-/ namespace product section open prod is_trunc definition precategory_prod [reducible] {obC obD : Type} (C : precategory obC) (D : precategory obD) : precategory (obC Γ— obD) := precategory.mk (Ξ» a b, hom (pr1 a) (pr1 b) Γ— hom (pr2 a) (pr2 b)) (Ξ» a b, !is_trunc_prod) (Ξ» a b c g f, (pr1 g ∘ pr1 f , pr2 g ∘ pr2 f)) (Ξ» a, (id, id)) (Ξ» a b c d h g f, pair_eq !assoc !assoc ) (Ξ» a b f, prod_eq !id_left !id_left ) (Ξ» a b f, prod_eq !id_right !id_right) definition Precategory_prod [reducible] (C D : Precategory) : Precategory := precategory.Mk (precategory_prod C D) end end product namespace ops --notation 1 := Category_one --notation 2 := Category_two postfix `α΅’α΅–`:max := opposite.Opposite infixr `Γ—c`:30 := product.Precategory_prod --instance [persistent] type_category category_one -- category_two product.prod_category end ops open ops namespace opposite open ops functor definition opposite_functor [reducible] {C D : Precategory} (F : C β‡’ D) : Cα΅’α΅– β‡’ Dα΅’α΅– := begin apply (@functor.mk (Cα΅’α΅–) (Dα΅’α΅–)), intro a, apply (respect_id F), intros, apply (@respect_comp C D) end end opposite namespace product section open ops functor definition prod_functor [reducible] {C C' D D' : Precategory} (F : C β‡’ D) (G : C' β‡’ D') : C Γ—c C' β‡’ D Γ—c D' := functor.mk (Ξ» a, pair (F (pr1 a)) (G (pr2 a))) (Ξ» a b f, pair (F (pr1 f)) (G (pr2 f))) (Ξ» a, pair_eq !respect_id !respect_id) (Ξ» a b c g f, pair_eq !respect_comp !respect_comp) end end product definition precategory_hset [reducible] : precategory hset := precategory.mk (Ξ»x y : hset, x β†’ y) _ (Ξ»x y z g f a, g (f a)) (Ξ»x a, a) (Ξ»x y z w h g f, eq_of_homotopy (Ξ»a, idp)) (Ξ»x y f, eq_of_homotopy (Ξ»a, idp)) (Ξ»x y f, eq_of_homotopy (Ξ»a, idp)) definition Precategory_hset [reducible] : Precategory := Precategory.mk hset precategory_hset section open iso functor nat_trans definition precategory_functor [instance] [reducible] (D C : Precategory) : precategory (functor C D) := precategory.mk (Ξ»a b, nat_trans a b) (Ξ» a b, @is_hset_nat_trans C D a b) (Ξ» a b c g f, nat_trans.compose g f) (Ξ» a, nat_trans.id) (Ξ» a b c d h g f, !nat_trans.assoc) (Ξ» a b f, !nat_trans.id_left) (Ξ» a b f, !nat_trans.id_right) definition Precategory_functor [reducible] (D C : Precategory) : Precategory := precategory.Mk (precategory_functor D C) end namespace ops infixr `^c`:35 := Precategory_functor end ops section open iso functor nat_trans /- we prove that if a natural transformation is pointwise an to_fun, then it is an to_fun -/ variables {C D : Precategory} {F G : C β‡’ D} (Ξ· : F ⟹ G) [iso : Ξ (a : C), is_iso (Ξ· a)] include iso definition nat_trans_inverse : G ⟹ F := nat_trans.mk (Ξ»c, (Ξ· c)⁻¹) (Ξ»c d f, begin apply comp_inverse_eq_of_eq_comp, apply concat, rotate_left 1, apply assoc, apply eq_inverse_comp_of_comp_eq, apply inverse, apply naturality, end) definition nat_trans_left_inverse : nat_trans_inverse Ξ· ∘n Ξ· = nat_trans.id := begin fapply (apD011 nat_trans.mk), apply eq_of_homotopy, intro c, apply left_inverse, apply eq_of_homotopy, intros, apply eq_of_homotopy, intros, apply eq_of_homotopy, intros, apply is_hset.elim end definition nat_trans_right_inverse : Ξ· ∘n nat_trans_inverse Ξ· = nat_trans.id := begin fapply (apD011 nat_trans.mk), apply eq_of_homotopy, intro c, apply right_inverse, apply eq_of_homotopy, intros, apply eq_of_homotopy, intros, apply eq_of_homotopy, intros, apply is_hset.elim end definition is_iso_nat_trans : is_iso Ξ· := is_iso.mk (nat_trans_left_inverse Ξ·) (nat_trans_right_inverse Ξ·) omit iso -- local attribute is_iso_nat_trans [instance] -- definition functor_iso_functor (H : Ξ (a : C), F a β‰… G a) : F β‰… G := -- is this true? -- iso.mk _ end section open iso functor category.ops nat_trans iso.iso /- and conversely, if a natural transformation is an iso, it is componentwise an iso -/ variables {C D : Precategory} {F G : D ^c C} (Ξ· : hom F G) [isoΞ· : is_iso Ξ·] (c : C) include isoΞ· definition componentwise_is_iso : is_iso (Ξ· c) := @is_iso.mk _ _ _ _ _ (natural_map η⁻¹ c) (ap010 natural_map ( left_inverse Ξ·) c) (ap010 natural_map (right_inverse Ξ·) c) local attribute componentwise_is_iso [instance] definition natural_map_inverse : natural_map η⁻¹ c = (Ξ· c)⁻¹ := idp definition naturality_iso {c c' : C} (f : c ⟢ c') : G f = Ξ· c' ∘ F f ∘ (Ξ· c)⁻¹ := calc G f = (G f ∘ Ξ· c) ∘ (Ξ· c)⁻¹ : comp_inverse_cancel_right ... = (Ξ· c' ∘ F f) ∘ (Ξ· c)⁻¹ : {naturality Ξ· f} ... = Ξ· c' ∘ F f ∘ (Ξ· c)⁻¹ : assoc definition naturality_iso' {c c' : C} (f : c ⟢ c') : (Ξ· c')⁻¹ ∘ G f ∘ Ξ· c = F f := calc (Ξ· c')⁻¹ ∘ G f ∘ Ξ· c = (Ξ· c')⁻¹ ∘ Ξ· c' ∘ F f : {naturality Ξ· f} ... = F f : inverse_comp_cancel_left omit isoΞ· definition componentwise_iso (Ξ· : F β‰… G) (c : C) : F c β‰… G c := @iso.mk _ _ _ _ (natural_map (to_hom Ξ·) c) (@componentwise_is_iso _ _ _ _ (to_hom Ξ·) (struct Ξ·) c) definition componentwise_iso_id (c : C) : componentwise_iso (iso.refl F) c = iso.refl (F c) := iso.eq_mk (idpath id) definition componentwise_iso_iso_of_eq (p : F = G) (c : C) : componentwise_iso (iso_of_eq p) c = iso_of_eq (ap010 to_fun_ob p c) := eq.rec_on p !componentwise_iso_id definition natural_map_hom_of_eq (p : F = G) (c : C) : natural_map (hom_of_eq p) c = hom_of_eq (ap010 to_fun_ob p c) := eq.rec_on p idp definition natural_map_inv_of_eq (p : F = G) (c : C) : natural_map (inv_of_eq p) c = hom_of_eq (ap010 to_fun_ob p c)⁻¹ := eq.rec_on p idp end namespace ops infixr `Γ—f`:30 := product.prod_functor infixr `α΅’α΅–αΆ `:(max+1) := opposite.opposite_functor end ops end category
8fd8182a23a56c984070d4ce261d032b315fe865
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/monoidal/functor.lean
748ac9776ba92dab8f6af966e0b715cd3cbf2c1b
[ "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
14,068
lean
/- Copyright (c) 2018 Michael Jendrusch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Jendrusch, Scott Morrison, Bhavik Mehta -/ import category_theory.monoidal.category import category_theory.adjunction.basic /-! # (Lax) monoidal functors A lax monoidal functor `F` between monoidal categories `C` and `D` is a functor between the underlying categories equipped with morphisms * `Ξ΅ : πŸ™_ D ⟢ F.obj (πŸ™_ C)` (called the unit morphism) * `ΞΌ X Y : (F.obj X) βŠ— (F.obj Y) ⟢ F.obj (X βŠ— Y)` (called the tensorator, or strength). satisfying various axioms. A monoidal functor is a lax monoidal functor for which `Ξ΅` and `ΞΌ` are isomorphisms. We show that the composition of (lax) monoidal functors gives a (lax) monoidal functor. See also `category_theory.monoidal.functorial` for a typeclass decorating an object-level function with the additional data of a monoidal functor. This is useful when stating that a pre-existing functor is monoidal. See `category_theory.monoidal.natural_transformation` for monoidal natural transformations. We show in `category_theory.monoidal.Mon_` that lax monoidal functors take monoid objects to monoid objects. ## Future work * Oplax monoidal functors. ## References See https://stacks.math.columbia.edu/tag/0FFL. -/ open category_theory universes v₁ vβ‚‚ v₃ u₁ uβ‚‚ u₃ open category_theory.category open category_theory.functor namespace category_theory section open monoidal_category variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] (D : Type uβ‚‚) [category.{vβ‚‚} D] [monoidal_category.{vβ‚‚} D] /-- A lax monoidal functor is a functor `F : C β₯€ D` between monoidal categories, equipped with morphisms `Ξ΅ : πŸ™ _D ⟢ F.obj (πŸ™_ C)` and `ΞΌ X Y : F.obj X βŠ— F.obj Y ⟢ F.obj (X βŠ— Y)`, satisfying the appropriate coherences. -/ -- The direction of `left_unitality` and `right_unitality` as simp lemmas may look strange: -- remember the rule of thumb that component indices of natural transformations -- "weigh more" than structural maps. -- (However by this argument `associativity` is currently stated backwards!) structure lax_monoidal_functor extends C β₯€ D := -- unit morphism (Ξ΅ : πŸ™_ D ⟢ obj (πŸ™_ C)) -- tensorator (ΞΌ : Ξ  X Y : C, (obj X) βŠ— (obj Y) ⟢ obj (X βŠ— Y)) (ΞΌ_natural' : βˆ€ {X Y X' Y' : C} (f : X ⟢ Y) (g : X' ⟢ Y'), ((map f) βŠ— (map g)) ≫ ΞΌ Y Y' = ΞΌ X X' ≫ map (f βŠ— g) . obviously) -- associativity of the tensorator (associativity' : βˆ€ (X Y Z : C), (ΞΌ X Y βŠ— πŸ™ (obj Z)) ≫ ΞΌ (X βŠ— Y) Z ≫ map (Ξ±_ X Y Z).hom = (Ξ±_ (obj X) (obj Y) (obj Z)).hom ≫ (πŸ™ (obj X) βŠ— ΞΌ Y Z) ≫ ΞΌ X (Y βŠ— Z) . obviously) -- unitality (left_unitality' : βˆ€ X : C, (Ξ»_ (obj X)).hom = (Ξ΅ βŠ— πŸ™ (obj X)) ≫ ΞΌ (πŸ™_ C) X ≫ map (Ξ»_ X).hom . obviously) (right_unitality' : βˆ€ X : C, (ρ_ (obj X)).hom = (πŸ™ (obj X) βŠ— Ξ΅) ≫ ΞΌ X (πŸ™_ C) ≫ map (ρ_ X).hom . obviously) restate_axiom lax_monoidal_functor.ΞΌ_natural' attribute [simp, reassoc] lax_monoidal_functor.ΞΌ_natural restate_axiom lax_monoidal_functor.left_unitality' attribute [simp] lax_monoidal_functor.left_unitality restate_axiom lax_monoidal_functor.right_unitality' attribute [simp] lax_monoidal_functor.right_unitality restate_axiom lax_monoidal_functor.associativity' attribute [simp, reassoc] lax_monoidal_functor.associativity -- When `rewrite_search` lands, add @[search] attributes to -- lax_monoidal_functor.ΞΌ_natural lax_monoidal_functor.left_unitality -- lax_monoidal_functor.right_unitality lax_monoidal_functor.associativity section variables {C D} @[simp, reassoc] lemma lax_monoidal_functor.left_unitality_inv (F : lax_monoidal_functor C D) (X : C) : (Ξ»_ (F.obj X)).inv ≫ (F.Ξ΅ βŠ— πŸ™ (F.obj X)) ≫ F.ΞΌ (πŸ™_ C) X = F.map (Ξ»_ X).inv := begin rw [iso.inv_comp_eq, F.left_unitality, category.assoc, category.assoc, ←F.to_functor.map_comp, iso.hom_inv_id, F.to_functor.map_id, comp_id], end @[simp, reassoc] lemma lax_monoidal_functor.right_unitality_inv (F : lax_monoidal_functor C D) (X : C) : (ρ_ (F.obj X)).inv ≫ (πŸ™ (F.obj X) βŠ— F.Ξ΅) ≫ F.ΞΌ X (πŸ™_ C) = F.map (ρ_ X).inv := begin rw [iso.inv_comp_eq, F.right_unitality, category.assoc, category.assoc, ←F.to_functor.map_comp, iso.hom_inv_id, F.to_functor.map_id, comp_id], end @[simp, reassoc] lemma lax_monoidal_functor.associativity_inv (F : lax_monoidal_functor C D) (X Y Z : C) : (πŸ™ (F.obj X) βŠ— F.ΞΌ Y Z) ≫ F.ΞΌ X (Y βŠ— Z) ≫ F.map (Ξ±_ X Y Z).inv = (Ξ±_ (F.obj X) (F.obj Y) (F.obj Z)).inv ≫ (F.ΞΌ X Y βŠ— πŸ™ (F.obj Z)) ≫ F.ΞΌ (X βŠ— Y) Z := begin rw [iso.eq_inv_comp, ←F.associativity_assoc, ←F.to_functor.map_comp, iso.hom_inv_id, F.to_functor.map_id, comp_id], end end /-- A monoidal functor is a lax monoidal functor for which the tensorator and unitor as isomorphisms. See https://stacks.math.columbia.edu/tag/0FFL. -/ structure monoidal_functor extends lax_monoidal_functor.{v₁ vβ‚‚} C D := (Ξ΅_is_iso : is_iso Ξ΅ . tactic.apply_instance) (ΞΌ_is_iso : Ξ  X Y : C, is_iso (ΞΌ X Y) . tactic.apply_instance) attribute [instance] monoidal_functor.Ξ΅_is_iso monoidal_functor.ΞΌ_is_iso variables {C D} /-- The unit morphism of a (strong) monoidal functor as an isomorphism. -/ noncomputable def monoidal_functor.Ξ΅_iso (F : monoidal_functor.{v₁ vβ‚‚} C D) : tensor_unit D β‰… F.obj (tensor_unit C) := as_iso F.Ξ΅ /-- The tensorator of a (strong) monoidal functor as an isomorphism. -/ noncomputable def monoidal_functor.ΞΌ_iso (F : monoidal_functor.{v₁ vβ‚‚} C D) (X Y : C) : (F.obj X) βŠ— (F.obj Y) β‰… F.obj (X βŠ— Y) := as_iso (F.ΞΌ X Y) end open monoidal_category namespace lax_monoidal_functor variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] /-- The identity lax monoidal functor. -/ @[simps] def id : lax_monoidal_functor.{v₁ v₁} C C := { Ξ΅ := πŸ™ _, ΞΌ := Ξ» X Y, πŸ™ _, .. 𝟭 C } instance : inhabited (lax_monoidal_functor C C) := ⟨id C⟩ end lax_monoidal_functor namespace monoidal_functor section variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C] variables {D : Type uβ‚‚} [category.{vβ‚‚} D] [monoidal_category.{vβ‚‚} D] lemma map_tensor (F : monoidal_functor.{v₁ vβ‚‚} C D) {X Y X' Y' : C} (f : X ⟢ Y) (g : X' ⟢ Y') : F.map (f βŠ— g) = inv (F.ΞΌ X X') ≫ ((F.map f) βŠ— (F.map g)) ≫ F.ΞΌ Y Y' := by simp lemma map_left_unitor (F : monoidal_functor.{v₁ vβ‚‚} C D) (X : C) : F.map (Ξ»_ X).hom = inv (F.ΞΌ (πŸ™_ C) X) ≫ (inv F.Ξ΅ βŠ— πŸ™ (F.obj X)) ≫ (Ξ»_ (F.obj X)).hom := begin simp only [lax_monoidal_functor.left_unitality], slice_rhs 2 3 { rw ←comp_tensor_id, simp, }, simp, end lemma map_right_unitor (F : monoidal_functor.{v₁ vβ‚‚} C D) (X : C) : F.map (ρ_ X).hom = inv (F.ΞΌ X (πŸ™_ C)) ≫ (πŸ™ (F.obj X) βŠ— inv F.Ξ΅) ≫ (ρ_ (F.obj X)).hom := begin simp only [lax_monoidal_functor.right_unitality], slice_rhs 2 3 { rw ←id_tensor_comp, simp, }, simp, end /-- The tensorator as a natural isomorphism. -/ noncomputable def ΞΌ_nat_iso (F : monoidal_functor.{v₁ vβ‚‚} C D) : (functor.prod F.to_functor F.to_functor) β‹™ (tensor D) β‰… (tensor C) β‹™ F.to_functor := nat_iso.of_components (by { intros, apply F.ΞΌ_iso }) (by { intros, apply F.to_lax_monoidal_functor.ΞΌ_natural }) end section variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] /-- The identity monoidal functor. -/ @[simps] def id : monoidal_functor.{v₁ v₁} C C := { Ξ΅ := πŸ™ _, ΞΌ := Ξ» X Y, πŸ™ _, .. 𝟭 C } instance : inhabited (monoidal_functor C C) := ⟨id C⟩ end end monoidal_functor variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C] variables {D : Type uβ‚‚} [category.{vβ‚‚} D] [monoidal_category.{vβ‚‚} D] variables {E : Type u₃} [category.{v₃} E] [monoidal_category.{v₃} E] namespace lax_monoidal_functor variables (F : lax_monoidal_functor.{v₁ vβ‚‚} C D) (G : lax_monoidal_functor.{vβ‚‚ v₃} D E) -- The proofs here are horrendous; rewrite_search helps a lot. /-- The composition of two lax monoidal functors is again lax monoidal. -/ @[simps] def comp : lax_monoidal_functor.{v₁ v₃} C E := { Ξ΅ := G.Ξ΅ ≫ (G.map F.Ξ΅), ΞΌ := Ξ» X Y, G.ΞΌ (F.obj X) (F.obj Y) ≫ G.map (F.ΞΌ X Y), ΞΌ_natural' := Ξ» _ _ _ _ f g, begin simp only [functor.comp_map, assoc], rw [←category.assoc, lax_monoidal_functor.ΞΌ_natural, category.assoc, ←map_comp, ←map_comp, ←lax_monoidal_functor.ΞΌ_natural] end, associativity' := Ξ» X Y Z, begin dsimp, rw id_tensor_comp, slice_rhs 3 4 { rw [← G.to_functor.map_id, G.ΞΌ_natural], }, slice_rhs 1 3 { rw ←G.associativity, }, rw comp_tensor_id, slice_lhs 2 3 { rw [← G.to_functor.map_id, G.ΞΌ_natural], }, rw [category.assoc, category.assoc, category.assoc, category.assoc, category.assoc, ←G.to_functor.map_comp, ←G.to_functor.map_comp, ←G.to_functor.map_comp, ←G.to_functor.map_comp, F.associativity], end, left_unitality' := Ξ» X, begin dsimp, rw [G.left_unitality, comp_tensor_id, category.assoc, category.assoc], apply congr_arg, rw [F.left_unitality, map_comp, ←nat_trans.id_app, ←category.assoc, ←lax_monoidal_functor.ΞΌ_natural, nat_trans.id_app, map_id, ←category.assoc, map_comp], end, right_unitality' := Ξ» X, begin dsimp, rw [G.right_unitality, id_tensor_comp, category.assoc, category.assoc], apply congr_arg, rw [F.right_unitality, map_comp, ←nat_trans.id_app, ←category.assoc, ←lax_monoidal_functor.ΞΌ_natural, nat_trans.id_app, map_id, ←category.assoc, map_comp], end, .. (F.to_functor) β‹™ (G.to_functor) }. infixr ` βŠ—β‹™ `:80 := comp end lax_monoidal_functor namespace monoidal_functor variables (F : monoidal_functor.{v₁ vβ‚‚} C D) (G : monoidal_functor.{vβ‚‚ v₃} D E) /-- The composition of two monoidal functors is again monoidal. -/ @[simps] def comp : monoidal_functor.{v₁ v₃} C E := { Ξ΅_is_iso := by { dsimp, apply_instance }, ΞΌ_is_iso := by { dsimp, apply_instance }, .. (F.to_lax_monoidal_functor).comp (G.to_lax_monoidal_functor) }. infixr ` βŠ—β‹™ `:80 := comp -- We overload notation; potentially dangerous, but it seems to work. end monoidal_functor /-- If we have a right adjoint functor `G` to a monoidal functor `F`, then `G` has a lax monoidal structure as well. -/ @[simps] noncomputable def monoidal_adjoint (F : monoidal_functor C D) {G : D β₯€ C} (h : F.to_functor ⊣ G) : lax_monoidal_functor D C := { to_functor := G, Ξ΅ := h.hom_equiv _ _ (inv F.Ξ΅), ΞΌ := Ξ» X Y, h.hom_equiv _ (X βŠ— Y) (inv (F.ΞΌ (G.obj X) (G.obj Y)) ≫ (h.counit.app X βŠ— h.counit.app Y)), ΞΌ_natural' := Ξ» X Y X' Y' f g, begin rw [←h.hom_equiv_naturality_left, ←h.hom_equiv_naturality_right, equiv.apply_eq_iff_eq, assoc, is_iso.eq_inv_comp, ←F.to_lax_monoidal_functor.ΞΌ_natural_assoc, is_iso.hom_inv_id_assoc, ←tensor_comp, adjunction.counit_naturality, adjunction.counit_naturality, tensor_comp], end, associativity' := Ξ» X Y Z, begin rw [←h.hom_equiv_naturality_right, ←h.hom_equiv_naturality_left, ←h.hom_equiv_naturality_left, ←h.hom_equiv_naturality_left, equiv.apply_eq_iff_eq, ← cancel_epi (F.to_lax_monoidal_functor.ΞΌ (G.obj X βŠ— G.obj Y) (G.obj Z)), ← cancel_epi (F.to_lax_monoidal_functor.ΞΌ (G.obj X) (G.obj Y) βŠ— πŸ™ (F.obj (G.obj Z))), F.to_lax_monoidal_functor.associativity_assoc (G.obj X) (G.obj Y) (G.obj Z), ←F.to_lax_monoidal_functor.ΞΌ_natural_assoc, assoc, is_iso.hom_inv_id_assoc, ←F.to_lax_monoidal_functor.ΞΌ_natural_assoc, is_iso.hom_inv_id_assoc, ←tensor_comp, ←tensor_comp, id_comp, functor.map_id, functor.map_id, id_comp, ←tensor_comp_assoc, ←tensor_comp_assoc, id_comp, id_comp, h.hom_equiv_unit, h.hom_equiv_unit, functor.map_comp, assoc, assoc, h.counit_naturality, h.left_triangle_components_assoc, is_iso.hom_inv_id_assoc, functor.map_comp, assoc, h.counit_naturality, h.left_triangle_components_assoc, is_iso.hom_inv_id_assoc], exact associator_naturality (h.counit.app X) (h.counit.app Y) (h.counit.app Z), end, left_unitality' := Ξ» X, begin rw [←h.hom_equiv_naturality_right, ←h.hom_equiv_naturality_left, ←equiv.symm_apply_eq, h.hom_equiv_counit, F.map_left_unitor, h.hom_equiv_unit, assoc, assoc, assoc, F.map_tensor, assoc, assoc, is_iso.hom_inv_id_assoc, ←tensor_comp_assoc, functor.map_id, id_comp, functor.map_comp, assoc, h.counit_naturality, h.left_triangle_components_assoc, ←left_unitor_naturality, ←tensor_comp_assoc, id_comp, comp_id], end, right_unitality' := Ξ» X, begin rw [←h.hom_equiv_naturality_right, ←h.hom_equiv_naturality_left, ←equiv.symm_apply_eq, h.hom_equiv_counit, F.map_right_unitor, assoc, assoc, ←right_unitor_naturality, ←tensor_comp_assoc, comp_id, id_comp, h.hom_equiv_unit, F.map_tensor, assoc, assoc, assoc, is_iso.hom_inv_id_assoc, functor.map_comp, functor.map_id, ←tensor_comp_assoc, assoc, h.counit_naturality, h.left_triangle_components_assoc, id_comp], end }. /-- If a monoidal functor `F` is an equivalence of categories then its inverse is also monoidal. -/ noncomputable def monoidal_inverse (F : monoidal_functor C D) [is_equivalence F.to_functor] : monoidal_functor D C := { to_lax_monoidal_functor := monoidal_adjoint F (as_equivalence _).to_adjunction, Ξ΅_is_iso := by { dsimp [equivalence.to_adjunction], apply_instance }, ΞΌ_is_iso := Ξ» X Y, by { dsimp [equivalence.to_adjunction], apply_instance } } @[simp] lemma monoidal_inverse_to_functor (F : monoidal_functor C D) [is_equivalence F.to_functor] : (monoidal_inverse F).to_functor = F.to_functor.inv := rfl end category_theory
c3e1cfbfbb1b39f28f5fd7808bb21d125d4c8e1f
f618aea02cb4104ad34ecf3b9713065cc0d06103
/src/field_theory/perfect_closure.lean
41dcfb2cd31b60ee262410ca856a5be77ff691bc
[ "Apache-2.0" ]
permissive
joehendrix/mathlib
84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5
c15eab34ad754f9ecd738525cb8b5a870e834ddc
refs/heads/master
1,589,606,591,630
1,555,946,393,000
1,555,946,393,000
182,813,854
0
0
null
1,555,946,309,000
1,555,946,308,000
null
UTF-8
Lean
false
false
17,289
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau The perfect closure of a field. -/ import algebra.char_p local attribute [instance, priority 0] nat.cast_coe local attribute [instance, priority 0] int.cast_coe universes u v /-- A perfect field is a field of characteristic p that has p-th root. -/ class perfect_field (Ξ± : Type u) [field Ξ±] (p : β„•) [char_p Ξ± p] : Type u := (pth_root : Ξ± β†’ Ξ±) (frobenius_pth_root : βˆ€ x, frobenius Ξ± p (pth_root x) = x) theorem frobenius_pth_root (Ξ± : Type u) [field Ξ±] (p : β„•) [char_p Ξ± p] [perfect_field Ξ± p] (x : Ξ±) : frobenius Ξ± p (perfect_field.pth_root p x) = x := perfect_field.frobenius_pth_root p x theorem pth_root_frobenius (Ξ± : Type u) [field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] [perfect_field Ξ± p] (x : Ξ±) : perfect_field.pth_root p (frobenius Ξ± p x) = x := frobenius_inj Ξ± p _ _ (by rw frobenius_pth_root) instance pth_root.is_ring_hom (Ξ± : Type u) [field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] [perfect_field Ξ± p] : is_ring_hom (@perfect_field.pth_root Ξ± _ p _ _) := { map_one := frobenius_inj Ξ± p _ _ (by rw [frobenius_pth_root, frobenius_one]), map_mul := Ξ» x y, frobenius_inj Ξ± p _ _ (by simp only [frobenius_pth_root, frobenius_mul]), map_add := Ξ» x y, frobenius_inj Ξ± p _ _ (by simp only [frobenius_pth_root, frobenius_add]) } theorem is_ring_hom.pth_root {Ξ± : Type u} [field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] [perfect_field Ξ± p] {Ξ² : Type v} [field Ξ²] [char_p Ξ² p] [perfect_field Ξ² p] (f : Ξ± β†’ Ξ²) [is_ring_hom f] {x : Ξ±} : f (perfect_field.pth_root p x) = perfect_field.pth_root p (f x) := frobenius_inj Ξ² p _ _ (by rw [← is_monoid_hom.map_frobenius f, frobenius_pth_root, frobenius_pth_root]) inductive perfect_closure.r (Ξ± : Type u) [monoid Ξ±] (p : β„•) : (β„• Γ— Ξ±) β†’ (β„• Γ— Ξ±) β†’ Prop | intro : βˆ€ n x, perfect_closure.r (n, x) (n+1, frobenius Ξ± p x) run_cmd tactic.mk_iff_of_inductive_prop `perfect_closure.r `perfect_closure.r_iff /-- The perfect closure is the smallest extension that makes frobenius surjective. -/ def perfect_closure (Ξ± : Type u) [monoid Ξ±] (p : β„•) : Type u := quot (perfect_closure.r Ξ± p) namespace perfect_closure variables (Ξ± : Type u) private lemma mul_aux_left [comm_monoid Ξ±] (p : β„•) (x1 x2 y : β„• Γ— Ξ±) (H : r Ξ± p x1 x2) : quot.mk (r Ξ± p) (x1.1 + y.1, ((frobenius Ξ± p)^[y.1] x1.2) * ((frobenius Ξ± p)^[x1.1] y.2)) = quot.mk (r Ξ± p) (x2.1 + y.1, ((frobenius Ξ± p)^[y.1] x2.2) * ((frobenius Ξ± p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro _ n x := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_mul, nat.succ_add]; apply r.intro end private lemma mul_aux_right [comm_monoid Ξ±] (p : β„•) (x y1 y2 : β„• Γ— Ξ±) (H : r Ξ± p y1 y2) : quot.mk (r Ξ± p) (x.1 + y1.1, ((frobenius Ξ± p)^[y1.1] x.2) * ((frobenius Ξ± p)^[x.1] y1.2)) = quot.mk (r Ξ± p) (x.1 + y2.1, ((frobenius Ξ± p)^[y2.1] x.2) * ((frobenius Ξ± p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro _ n y := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_mul]; apply r.intro end instance [comm_monoid Ξ±] (p : β„•) : has_mul (perfect_closure Ξ± p) := ⟨quot.lift (Ξ» x:β„•Γ—Ξ±, quot.lift (Ξ» y:β„•Γ—Ξ±, quot.mk (r Ξ± p) (x.1 + y.1, ((frobenius Ξ± p)^[y.1] x.2) * ((frobenius Ξ± p)^[x.1] y.2))) (mul_aux_right Ξ± p x)) (Ξ» x1 x2 (H : r Ξ± p x1 x2), funext $ Ξ» e, quot.induction_on e $ Ξ» y, mul_aux_left Ξ± p x1 x2 y H)⟩ instance [comm_monoid Ξ±] (p : β„•) : comm_monoid (perfect_closure Ξ± p) := { mul_assoc := Ξ» e f g, quot.induction_on e $ Ξ» ⟨m, x⟩, quot.induction_on f $ Ξ» ⟨n, y⟩, quot.induction_on g $ Ξ» ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, mul_assoc, nat.iterateβ‚‚ (frobenius_mul _ _), (nat.iterate_add _ _ _ _).symm, add_comm, add_left_comm], one := quot.mk _ (0, 1), one_mul := Ξ» e, quot.induction_on e (Ξ» ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterateβ‚€ (frobenius_one _ _), nat.iterate_zero, one_mul, zero_add]), mul_one := Ξ» e, quot.induction_on e (Ξ» ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterateβ‚€ (frobenius_one _ _), nat.iterate_zero, mul_one, add_zero]), mul_comm := Ξ» e f, quot.induction_on e (Ξ» ⟨m, x⟩, quot.induction_on f (Ξ» ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm, mul_comm])), .. (infer_instance : has_mul (perfect_closure Ξ± p)) } private lemma add_aux_left [comm_ring Ξ±] (p : β„•) (hp : nat.prime p) [char_p Ξ± p] (x1 x2 y : β„• Γ— Ξ±) (H : r Ξ± p x1 x2) : quot.mk (r Ξ± p) (x1.1 + y.1, ((frobenius Ξ± p)^[y.1] x1.2) + ((frobenius Ξ± p)^[x1.1] y.2)) = quot.mk (r Ξ± p) (x2.1 + y.1, ((frobenius Ξ± p)^[y.1] x2.2) + ((frobenius Ξ± p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro _ n x := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_add, nat.succ_add]; apply r.intro end private lemma add_aux_right [comm_ring Ξ±] (p : β„•) (hp : nat.prime p) [char_p Ξ± p] (x y1 y2 : β„• Γ— Ξ±) (H : r Ξ± p y1 y2) : quot.mk (r Ξ± p) (x.1 + y1.1, ((frobenius Ξ± p)^[y1.1] x.2) + ((frobenius Ξ± p)^[x.1] y1.2)) = quot.mk (r Ξ± p) (x.1 + y2.1, ((frobenius Ξ± p)^[y2.1] x.2) + ((frobenius Ξ± p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro _ n y := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_add]; apply r.intro end instance [comm_ring Ξ±] (p : β„•) [hp : nat.prime p] [char_p Ξ± p] : has_add (perfect_closure Ξ± p) := ⟨quot.lift (Ξ» x:β„•Γ—Ξ±, quot.lift (Ξ» y:β„•Γ—Ξ±, quot.mk (r Ξ± p) (x.1 + y.1, ((frobenius Ξ± p)^[y.1] x.2) + ((frobenius Ξ± p)^[x.1] y.2))) (add_aux_right Ξ± p hp x)) (Ξ» x1 x2 (H : r Ξ± p x1 x2), funext $ Ξ» e, quot.induction_on e $ Ξ» y, add_aux_left Ξ± p hp x1 x2 y H)⟩ instance [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : has_neg (perfect_closure Ξ± p) := ⟨quot.lift (Ξ» x:β„•Γ—Ξ±, quot.mk (r Ξ± p) (x.1, -x.2)) (Ξ» x y (H : r Ξ± p x y), match x, y, H with | _, _, r.intro _ n x := quot.sound $ by rw ← frobenius_neg; apply r.intro end)⟩ theorem mk_zero [comm_ring Ξ±] (p : β„•) [nat.prime p] (n : β„•) : quot.mk (r Ξ± p) (n, 0) = quot.mk (r Ξ± p) (0, 0) := by induction n with n ih; [refl, rw ← ih]; symmetry; apply quot.sound; have := r.intro p n (0:Ξ±); rwa [frobenius_zero Ξ± p] at this theorem r.sound [monoid Ξ±] (p m n : β„•) (x y : Ξ±) (H : frobenius Ξ± p^[m] x = y) : quot.mk (r Ξ± p) (n, x) = quot.mk (r Ξ± p) (m + n, y) := by subst H; induction m with m ih; [simp only [zero_add, nat.iterate_zero], rw [ih, nat.succ_add, nat.iterate_succ']]; apply quot.sound; apply r.intro instance [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : comm_ring (perfect_closure Ξ± p) := { add_assoc := Ξ» e f g, quot.induction_on e $ Ξ» ⟨m, x⟩, quot.induction_on f $ Ξ» ⟨n, y⟩, quot.induction_on g $ Ξ» ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, nat.iterateβ‚‚ (frobenius_add Ξ± p), (nat.iterate_add _ _ _ _).symm, add_comm, add_left_comm], zero := quot.mk _ (0, 0), zero_add := Ξ» e, quot.induction_on e (Ξ» ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterateβ‚€ (frobenius_zero Ξ± p), nat.iterate_zero, zero_add]), add_zero := Ξ» e, quot.induction_on e (Ξ» ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterateβ‚€ (frobenius_zero Ξ± p), nat.iterate_zero, add_zero]), add_left_neg := Ξ» e, quot.induction_on e (Ξ» ⟨n, x⟩, show quot.mk _ _ = _, by simp only [nat.iterate₁ (frobenius_neg Ξ± p), add_left_neg, mk_zero]; refl), add_comm := Ξ» e f, quot.induction_on e (Ξ» ⟨m, x⟩, quot.induction_on f (Ξ» ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm])), left_distrib := Ξ» e f g, quot.induction_on e $ Ξ» ⟨m, x⟩, quot.induction_on f $ Ξ» ⟨n, y⟩, quot.induction_on g $ Ξ» ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm, add_left_comm]; apply r.sound; simp only [nat.iterateβ‚‚ (frobenius_mul Ξ± p), nat.iterateβ‚‚ (frobenius_add Ξ± p), (nat.iterate_add _ _ _ _).symm, mul_add, add_comm, add_left_comm], right_distrib := Ξ» e f g, quot.induction_on e $ Ξ» ⟨m, x⟩, quot.induction_on f $ Ξ» ⟨n, y⟩, quot.induction_on g $ Ξ» ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm _ s, add_left_comm _ s]; apply r.sound; simp only [nat.iterateβ‚‚ (frobenius_mul Ξ± p), nat.iterateβ‚‚ (frobenius_add Ξ± p), (nat.iterate_add _ _ _ _).symm, add_mul, add_comm, add_left_comm], .. (infer_instance : has_add (perfect_closure Ξ± p)), .. (infer_instance : has_neg (perfect_closure Ξ± p)), .. (infer_instance : comm_monoid (perfect_closure Ξ± p)) } instance [discrete_field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : has_inv (perfect_closure Ξ± p) := ⟨quot.lift (Ξ» x:β„•Γ—Ξ±, quot.mk (r Ξ± p) (x.1, x.2⁻¹)) (Ξ» x y (H : r Ξ± p x y), match x, y, H with | _, _, r.intro _ n x := quot.sound $ by simp only [frobenius]; rw inv_pow'; apply r.intro end)⟩ theorem eq_iff' [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (x y : β„• Γ— Ξ±) : quot.mk (r Ξ± p) x = quot.mk (r Ξ± p) y ↔ βˆƒ z, (frobenius Ξ± p^[y.1 + z] x.2) = (frobenius Ξ± p^[x.1 + z] y.2) := begin split, { intro H, replace H := quot.exact _ H, induction H, case eqv_gen.rel : x y H { cases H with n x, exact ⟨0, rfl⟩ }, case eqv_gen.refl : H { exact ⟨0, rfl⟩ }, case eqv_gen.symm : x y H ih { cases ih with w ih, exact ⟨w, ih.symm⟩ }, case eqv_gen.trans : x y z H1 H2 ih1 ih2 { cases ih1 with z1 ih1, cases ih2 with z2 ih2, existsi z2+(y.1+z1), rw [← add_assoc, nat.iterate_add, ih1], rw [← nat.iterate_add, add_comm, nat.iterate_add, ih2], rw [← nat.iterate_add], simp only [add_comm, add_left_comm] } }, intro H, cases x with m x, cases y with n y, cases H with z H, dsimp only at H, rw [r.sound Ξ± p (n+z) m x _ rfl, r.sound Ξ± p (m+z) n y _ rfl, H], rw [add_assoc, add_comm, add_comm z] end theorem eq_iff [integral_domain Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (x y : β„• Γ— Ξ±) : quot.mk (r Ξ± p) x = quot.mk (r Ξ± p) y ↔ (frobenius Ξ± p^[y.1] x.2) = (frobenius Ξ± p^[x.1] y.2) := (eq_iff' Ξ± p x y).trans ⟨λ ⟨z, H⟩, nat.iterate_inj (frobenius_inj Ξ± p) z _ _ $ by simpa only [add_comm, nat.iterate_add] using H, Ξ» H, ⟨0, H⟩⟩ instance [discrete_field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : discrete_field (perfect_closure Ξ± p) := { zero_ne_one := Ξ» H, zero_ne_one ((eq_iff _ _ _ _).1 H), mul_inv_cancel := Ξ» e, quot.induction_on e $ Ξ» ⟨m, x⟩ H, have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2 (by simp only [nat.iterateβ‚€ (frobenius_one _ _), nat.iterateβ‚€ (frobenius_zero Ξ± p), nat.iterate_zero, (nat.iterateβ‚‚ (frobenius_mul Ξ± p)).symm] at this ⊒; rw [mul_inv_cancel this, nat.iterateβ‚€ (frobenius_one _ _)]), inv_mul_cancel := Ξ» e, quot.induction_on e $ Ξ» ⟨m, x⟩ H, have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2 (by simp only [nat.iterateβ‚€ (frobenius_one _ _), nat.iterateβ‚€ (frobenius_zero Ξ± p), nat.iterate_zero, (nat.iterateβ‚‚ (frobenius_mul Ξ± p)).symm] at this ⊒; rw [inv_mul_cancel this, nat.iterateβ‚€ (frobenius_one _ _)]), has_decidable_eq := Ξ» e f, quot.rec_on_subsingleton e $ Ξ» ⟨m, x⟩, quot.rec_on_subsingleton f $ Ξ» ⟨n, y⟩, decidable_of_iff' _ (eq_iff Ξ± p _ _), inv_zero := congr_arg (quot.mk (r Ξ± p)) (by rw [inv_zero]), .. (infer_instance : has_inv (perfect_closure Ξ± p)), .. (infer_instance : comm_ring (perfect_closure Ξ± p)) } theorem frobenius_mk [comm_monoid Ξ±] (p : β„•) (x : β„• Γ— Ξ±) : frobenius (perfect_closure Ξ± p) p (quot.mk (r Ξ± p) x) = quot.mk _ (x.1, x.2^p) := begin unfold frobenius, cases x with n x, dsimp only, suffices : βˆ€ p':β„•, (quot.mk (r Ξ± p) (n, x) ^ p' : perfect_closure Ξ± p) = quot.mk (r Ξ± p) (n, x ^ p'), { apply this }, intro p, induction p with p ih, case nat.zero { apply r.sound, rw [nat.iterateβ‚€ (frobenius_one _ _), pow_zero] }, case nat.succ { rw [pow_succ, ih], symmetry, apply r.sound, simp only [pow_succ, nat.iterateβ‚‚ (frobenius_mul _ _)] } end def frobenius_equiv [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : perfect_closure Ξ± p ≃ perfect_closure Ξ± p := { to_fun := frobenius (perfect_closure Ξ± p) p, inv_fun := Ξ» e, quot.lift_on e (Ξ» x, quot.mk (r Ξ± p) (x.1 + 1, x.2)) (Ξ» x y H, match x, y, H with | _, _, r.intro _ n x := quot.sound (r.intro _ _ _) end), left_inv := Ξ» e, quot.induction_on e (Ξ» ⟨m, x⟩, by rw frobenius_mk; symmetry; apply quot.sound; apply r.intro), right_inv := Ξ» e, quot.induction_on e (Ξ» ⟨m, x⟩, by rw frobenius_mk; symmetry; apply quot.sound; apply r.intro) } theorem frobenius_equiv_apply [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] {x : perfect_closure Ξ± p} : frobenius_equiv Ξ± p x = frobenius _ p x := rfl theorem nat_cast [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (n x : β„•) : (x : perfect_closure Ξ± p) = quot.mk (r Ξ± p) (n, x) := begin induction n with n ih, { induction x with x ih, {refl}, rw [nat.cast_succ, nat.cast_succ, ih], refl }, rw ih, apply quot.sound, conv {congr, skip, skip, rw ← frobenius_nat_cast Ξ± p x}, apply r.intro end theorem int_cast [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (x : β„€) : (x : perfect_closure Ξ± p) = quot.mk (r Ξ± p) (0, x) := by induction x; simp only [int.cast_of_nat, int.cast_neg_succ_of_nat, nat_cast Ξ± p 0]; refl theorem nat_cast_eq_iff [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (x y : β„•) : (x : perfect_closure Ξ± p) = y ↔ (x : Ξ±) = y := begin split; intro H, { rw [nat_cast Ξ± p 0, nat_cast Ξ± p 0, eq_iff'] at H, cases H with z H, simpa only [zero_add, nat.iterateβ‚€ (frobenius_nat_cast Ξ± p _)] using H }, rw [nat_cast Ξ± p 0, nat_cast Ξ± p 0, H] end instance [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : char_p (perfect_closure Ξ± p) p := begin constructor, intro x, rw ← char_p.cast_eq_zero_iff Ξ±, rw [← nat.cast_zero, nat_cast_eq_iff, nat.cast_zero] end instance [discrete_field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : perfect_field (perfect_closure Ξ± p) p := { pth_root := (frobenius_equiv Ξ± p).symm, frobenius_pth_root := (frobenius_equiv Ξ± p).apply_symm_apply } def of [monoid Ξ±] (p : β„•) (x : Ξ±) : perfect_closure Ξ± p := quot.mk _ (0, x) instance [comm_ring Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] : is_ring_hom (of Ξ± p) := { map_one := rfl, map_mul := Ξ» x y, rfl, map_add := Ξ» x y, rfl } theorem eq_pth_root [discrete_field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (m : β„•) (x : Ξ±) : quot.mk (r Ξ± p) (m, x) = (perfect_field.pth_root p^[m] (of Ξ± p x) : perfect_closure Ξ± p) := begin unfold of, induction m with m ih, {refl}, rw [nat.iterate_succ', ← ih]; refl end def UMP [discrete_field Ξ±] (p : β„•) [nat.prime p] [char_p Ξ± p] (Ξ² : Type v) [discrete_field Ξ²] [char_p Ξ² p] [perfect_field Ξ² p] : { f : Ξ± β†’ Ξ² // is_ring_hom f } ≃ { f : perfect_closure Ξ± p β†’ Ξ² // is_ring_hom f } := { to_fun := Ξ» f, ⟨λ e, quot.lift_on e (Ξ» x, perfect_field.pth_root p^[x.1] (f.1 x.2)) (Ξ» x y H, match x, y, H with | _, _, r.intro _ n x := by letI := f.2; simp only [is_monoid_hom.map_frobenius f.1, nat.iterate_succ, pth_root_frobenius] end), show f.1 1 = 1, from f.2.1, Ξ» j k, quot.induction_on j $ Ξ» ⟨m, x⟩, quot.induction_on k $ Ξ» ⟨n, y⟩, show (perfect_field.pth_root p^[_] _) = (perfect_field.pth_root p^[_] _) * (perfect_field.pth_root p^[_] _), by letI := f.2; simp only [is_ring_hom.map_mul f.1, (nat.iterate₁ (Ξ» x, (is_monoid_hom.map_frobenius f.1 p x).symm)).symm, @nat.iterateβ‚‚ Ξ² _ (*) (Ξ» x y, is_ring_hom.map_mul (perfect_field.pth_root p))]; rw [nat.iterate_add, nat.iterate_cancel (pth_root_frobenius Ξ² p), add_comm, nat.iterate_add, nat.iterate_cancel (pth_root_frobenius Ξ² p)], Ξ» j k, quot.induction_on j $ Ξ» ⟨m, x⟩, quot.induction_on k $ Ξ» ⟨n, y⟩, show (perfect_field.pth_root p^[_] _) = (perfect_field.pth_root p^[_] _) + (perfect_field.pth_root p^[_] _), by letI := f.2; simp only [is_ring_hom.map_add f.1, (nat.iterate₁ (Ξ» x, (is_monoid_hom.map_frobenius f.1 p x).symm)).symm, @nat.iterateβ‚‚ Ξ² _ (+) (Ξ» x y, is_ring_hom.map_add (perfect_field.pth_root p))]; rw [nat.iterate_add, nat.iterate_cancel (pth_root_frobenius Ξ² p), add_comm m, nat.iterate_add, nat.iterate_cancel (pth_root_frobenius Ξ² p)]⟩, inv_fun := Ξ» f, ⟨f.1 ∘ of Ξ± p, @@is_ring_hom.comp _ _ _ _ _ _ f.2⟩, left_inv := Ξ» ⟨f, hf⟩, subtype.eq rfl, right_inv := Ξ» ⟨f, hf⟩, subtype.eq $ funext $ Ξ» i, quot.induction_on i $ Ξ» ⟨m, x⟩, show perfect_field.pth_root p^[m] (f _) = f _, by resetI; rw [eq_pth_root, @nat.iterate₁ _ _ _ _ f (Ξ» x:perfect_closure Ξ± p, (is_ring_hom.pth_root p f).symm)] } end perfect_closure
7b569996cf1295d315994f0aa27dcb279ee7fba1
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/init/function.lean
1110039d03b497dfe5531af75c00c5f615f03d6f
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
5,635
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Haitao Zhang General operations on functions. -/ prelude import init.prod init.funext init.logic namespace function variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type} definition compose [reducible] [unfold_full] (f : B β†’ C) (g : A β†’ B) : A β†’ C := Ξ»x, f (g x) definition compose_right [reducible] [unfold_full] (f : B β†’ B β†’ B) (g : A β†’ B) : B β†’ A β†’ B := Ξ» b a, f b (g a) definition compose_left [reducible] [unfold_full] (f : B β†’ B β†’ B) (g : A β†’ B) : A β†’ B β†’ B := Ξ» a b, f (g a) b definition on_fun [reducible] [unfold_full] (f : B β†’ B β†’ C) (g : A β†’ B) : A β†’ A β†’ C := Ξ»x y, f (g x) (g y) definition combine [reducible] [unfold_full] (f : A β†’ B β†’ C) (op : C β†’ D β†’ E) (g : A β†’ B β†’ D) : A β†’ B β†’ E := Ξ»x y, op (f x y) (g x y) definition const [reducible] [unfold_full] (B : Type) (a : A) : B β†’ A := Ξ»x, a definition dcompose [reducible] [unfold_full] {B : A β†’ Type} {C : Ξ  {x : A}, B x β†’ Type} (f : Ξ  {x : A} (y : B x), C y) (g : Ξ x, B x) : Ξ x, C (g x) := Ξ»x, f (g x) definition swap [reducible] [unfold_full] {C : A β†’ B β†’ Type} (f : Ξ x y, C x y) : Ξ y x, C x y := Ξ»y x, f x y definition app [reducible] {B : A β†’ Type} (f : Ξ x, B x) (x : A) : B x := f x definition curry [reducible] [unfold_full] : (A Γ— B β†’ C) β†’ A β†’ B β†’ C := Ξ» f a b, f (a, b) definition uncurry [reducible] [unfold 5] : (A β†’ B β†’ C) β†’ (A Γ— B β†’ C) := Ξ» f p, match p with (a, b) := f a b end theorem curry_uncurry (f : A β†’ B β†’ C) : curry (uncurry f) = f := rfl theorem uncurry_curry (f : A Γ— B β†’ C) : uncurry (curry f) = f := funext (Ξ» p, match p with (a, b) := rfl end) infixr ` ∘ ` := compose infixr ` ∘' `:60 := dcompose infixl ` on `:1 := on_fun infixr ` $ `:1 := app notation f ` -[` op `]- ` g := combine f op g lemma left_id (f : A β†’ B) : id ∘ f = f := rfl lemma right_id (f : A β†’ B) : f ∘ id = f := rfl theorem compose.assoc (f : C β†’ D) (g : B β†’ C) (h : A β†’ B) : (f ∘ g) ∘ h = f ∘ (g ∘ h) := rfl theorem compose.left_id (f : A β†’ B) : id ∘ f = f := rfl theorem compose.right_id (f : A β†’ B) : f ∘ id = f := rfl theorem compose_const_right (f : B β†’ C) (b : B) : f ∘ (const A b) = const A (f b) := rfl definition injective [reducible] (f : A β†’ B) : Prop := βˆ€ ⦃a₁ a₂⦄, f a₁ = f aβ‚‚ β†’ a₁ = aβ‚‚ theorem injective_compose {g : B β†’ C} {f : A β†’ B} (Hg : injective g) (Hf : injective f) : injective (g ∘ f) := take a₁ aβ‚‚, assume Heq, Hf (Hg Heq) definition surjective [reducible] (f : A β†’ B) : Prop := βˆ€ b, βˆƒ a, f a = b theorem surjective_compose {g : B β†’ C} {f : A β†’ B} (Hg : surjective g) (Hf : surjective f) : surjective (g ∘ f) := take c, obtain b (Hb : g b = c), from Hg c, obtain a (Ha : f a = b), from Hf b, exists.intro a (eq.trans (congr_arg g Ha) Hb) definition bijective (f : A β†’ B) := injective f ∧ surjective f theorem bijective_compose {g : B β†’ C} {f : A β†’ B} (Hg : bijective g) (Hf : bijective f) : bijective (g ∘ f) := obtain Hginj Hgsurj, from Hg, obtain Hfinj Hfsurj, from Hf, and.intro (injective_compose Hginj Hfinj) (surjective_compose Hgsurj Hfsurj) -- g is a left inverse to f definition left_inverse (g : B β†’ A) (f : A β†’ B) : Prop := βˆ€x, g (f x) = x definition id_of_left_inverse {g : B β†’ A} {f : A β†’ B} : left_inverse g f β†’ g ∘ f = id := assume h, funext h definition has_left_inverse (f : A β†’ B) : Prop := βˆƒ finv : B β†’ A, left_inverse finv f -- g is a right inverse to f definition right_inverse (g : B β†’ A) (f : A β†’ B) : Prop := left_inverse f g definition id_of_right_inverse {g : B β†’ A} {f : A β†’ B} : right_inverse g f β†’ f ∘ g = id := assume h, funext h definition has_right_inverse (f : A β†’ B) : Prop := βˆƒ finv : B β†’ A, right_inverse finv f theorem injective_of_left_inverse {g : B β†’ A} {f : A β†’ B} : left_inverse g f β†’ injective f := assume h, take a b, assume faeqfb, calc a = g (f a) : by rewrite h ... = g (f b) : faeqfb ... = b : by rewrite h theorem injective_of_has_left_inverse {f : A β†’ B} : has_left_inverse f β†’ injective f := assume h, obtain (finv : B β†’ A) (inv : left_inverse finv f), from h, injective_of_left_inverse inv theorem right_inverse_of_injective_of_left_inverse {f : A β†’ B} {g : B β†’ A} (injf : injective f) (lfg : left_inverse f g) : right_inverse f g := take x, have H : f (g (f x)) = f x, from lfg (f x), injf H theorem surjective_of_has_right_inverse {f : A β†’ B} : has_right_inverse f β†’ surjective f := assume h, take b, obtain (finv : B β†’ A) (inv : right_inverse finv f), from h, let a : A := finv b in have h : f a = b, from calc f a = (f ∘ finv) b : rfl ... = id b : by rewrite inv ... = b : rfl, exists.intro a h theorem left_inverse_of_surjective_of_right_inverse {f : A β†’ B} {g : B β†’ A} (surjf : surjective f) (rfg : right_inverse f g) : left_inverse f g := take y, obtain x (Hx : f x = y), from surjf y, calc f (g y) = f (g (f x)) : Hx ... = f x : rfg ... = y : Hx theorem injective_id : injective (@id A) := take a₁ aβ‚‚ H, H theorem surjective_id : surjective (@id A) := take a, exists.intro a rfl theorem bijective_id : bijective (@id A) := and.intro injective_id surjective_id end function -- copy reducible annotations to top-level export [reducible] [unfold] function
b989b0d663df1b806cf3340e73299a1907223c7a
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/data/sum.lean
3a4647bd42eb10df5dd3cb25f0d10d090eca805f
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,155
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury G. Kudryashov -/ /-! # More theorems about the sum type -/ universes u v w x variables {Ξ± : Type u} {Ξ±' : Type w} {Ξ² : Type v} {Ξ²' : Type x} open sum /-- Check if a sum is `inl` and if so, retrieve its contents. -/ @[simp] def sum.get_left {Ξ± Ξ²} : Ξ± βŠ• Ξ² β†’ option Ξ± | (inl a) := some a | (inr _) := none /-- Check if a sum is `inr` and if so, retrieve its contents. -/ @[simp] def sum.get_right {Ξ± Ξ²} : Ξ± βŠ• Ξ² β†’ option Ξ² | (inr b) := some b | (inl _) := none /-- Check if a sum is `inl`. -/ @[simp] def sum.is_left {Ξ± Ξ²} : Ξ± βŠ• Ξ² β†’ bool | (inl _) := tt | (inr _) := ff /-- Check if a sum is `inr`. -/ @[simp] def sum.is_right {Ξ± Ξ²} : Ξ± βŠ• Ξ² β†’ bool | (inl _) := ff | (inr _) := tt attribute [derive decidable_eq] sum @[simp] theorem sum.forall {p : Ξ± βŠ• Ξ² β†’ Prop} : (βˆ€ x, p x) ↔ (βˆ€ a, p (inl a)) ∧ (βˆ€ b, p (inr b)) := ⟨λ h, ⟨λ a, h _, Ξ» b, h _⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, sum.rec h₁ hβ‚‚βŸ© @[simp] theorem sum.exists {p : Ξ± βŠ• Ξ² β†’ Prop} : (βˆƒ x, p x) ↔ (βˆƒ a, p (inl a)) ∨ βˆƒ b, p (inr b) := ⟨λ h, match h with | ⟨inl a, h⟩ := or.inl ⟨a, h⟩ | ⟨inr b, h⟩ := or.inr ⟨b, h⟩ end, Ξ» h, match h with | or.inl ⟨a, h⟩ := ⟨inl a, h⟩ | or.inr ⟨b, h⟩ := ⟨inr b, h⟩ end⟩ namespace sum /-- Map `Ξ± βŠ• Ξ²` to `Ξ±' βŠ• Ξ²'` sending `Ξ±` to `Ξ±'` and `Ξ²` to `Ξ²'`. -/ protected def map (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') : Ξ± βŠ• Ξ² β†’ Ξ±' βŠ• Ξ²' | (sum.inl x) := sum.inl (f x) | (sum.inr x) := sum.inr (g x) @[simp] lemma map_inl (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') (x : Ξ±) : (inl x).map f g = inl (f x) := rfl @[simp] lemma map_inr (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') (x : Ξ²) : (inr x).map f g = inr (g x) := rfl @[simp] lemma map_map {Ξ±'' Ξ²''} (f' : Ξ±' β†’ Ξ±'') (g' : Ξ²' β†’ Ξ²'') (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') : βˆ€ x : Ξ± βŠ• Ξ², (x.map f g).map f' g' = x.map (f' ∘ f) (g' ∘ g) | (inl a) := rfl | (inr b) := rfl @[simp] lemma map_comp_map {Ξ±'' Ξ²''} (f' : Ξ±' β†’ Ξ±'') (g' : Ξ²' β†’ Ξ²'') (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') : (sum.map f' g') ∘ (sum.map f g) = sum.map (f' ∘ f) (g' ∘ g) := funext $ map_map f' g' f g @[simp] lemma map_id_id (Ξ± Ξ²) : sum.map (@id Ξ±) (@id Ξ²) = id := funext $ Ξ» x, sum.rec_on x (Ξ» _, rfl) (Ξ» _, rfl) theorem inl.inj_iff {a b} : (inl a : Ξ± βŠ• Ξ²) = inl b ↔ a = b := ⟨inl.inj, congr_arg _⟩ theorem inr.inj_iff {a b} : (inr a : Ξ± βŠ• Ξ²) = inr b ↔ a = b := ⟨inr.inj, congr_arg _⟩ theorem inl_ne_inr {a : Ξ±} {b : Ξ²} : inl a β‰  inr b. theorem inr_ne_inl {a : Ξ±} {b : Ξ²} : inr b β‰  inl a. /-- Define a function on `Ξ± βŠ• Ξ²` by giving separate definitions on `Ξ±` and `Ξ²`. -/ protected def elim {Ξ± Ξ² Ξ³ : Sort*} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) : Ξ± βŠ• Ξ² β†’ Ξ³ := Ξ» x, sum.rec_on x f g @[simp] lemma elim_inl {Ξ± Ξ² Ξ³ : Sort*} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) (x : Ξ±) : sum.elim f g (inl x) = f x := rfl @[simp] lemma elim_inr {Ξ± Ξ² Ξ³ : Sort*} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) (x : Ξ²) : sum.elim f g (inr x) = g x := rfl lemma elim_injective {Ξ± Ξ² Ξ³ : Sort*} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : function.injective f) (hg : function.injective g) (hfg : βˆ€ a b, f a β‰  g b) : function.injective (sum.elim f g) := Ξ» x y, sum.rec_on x (sum.rec_on y (Ξ» x y hxy, by rw hf hxy) (Ξ» x y hxy, false.elim $ hfg _ _ hxy)) (sum.rec_on y (Ξ» x y hxy, false.elim $ hfg x y hxy.symm) (Ξ» x y hxy, by rw hg hxy)) section variables (ra : Ξ± β†’ Ξ± β†’ Prop) (rb : Ξ² β†’ Ξ² β†’ Prop) /-- Lexicographic order for sum. Sort all the `inl a` before the `inr b`, otherwise use the respective order on `Ξ±` or `Ξ²`. -/ inductive lex : Ξ± βŠ• Ξ² β†’ Ξ± βŠ• Ξ² β†’ Prop | inl {a₁ aβ‚‚} (h : ra a₁ aβ‚‚) : lex (inl a₁) (inl aβ‚‚) | inr {b₁ bβ‚‚} (h : rb b₁ bβ‚‚) : lex (inr b₁) (inr bβ‚‚) | sep (a b) : lex (inl a) (inr b) variables {ra rb} @[simp] theorem lex_inl_inl {a₁ aβ‚‚} : lex ra rb (inl a₁) (inl aβ‚‚) ↔ ra a₁ aβ‚‚ := ⟨λ h, by cases h; assumption, lex.inl⟩ @[simp] theorem lex_inr_inr {b₁ bβ‚‚} : lex ra rb (inr b₁) (inr bβ‚‚) ↔ rb b₁ bβ‚‚ := ⟨λ h, by cases h; assumption, lex.inr⟩ @[simp] theorem lex_inr_inl {b a} : Β¬ lex ra rb (inr b) (inl a) := Ξ» h, by cases h attribute [simp] lex.sep theorem lex_acc_inl {a} (aca : acc ra a) : acc (lex ra rb) (inl a) := begin induction aca with a H IH, constructor, intros y h, cases h with a' _ h', exact IH _ h' end theorem lex_acc_inr (aca : βˆ€ a, acc (lex ra rb) (inl a)) {b} (acb : acc rb b) : acc (lex ra rb) (inr b) := begin induction acb with b H IH, constructor, intros y h, cases h with _ _ _ b' _ h' a, { exact IH _ h' }, { exact aca _ } end theorem lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := have aca : βˆ€ a, acc (lex ra rb) (inl a), from Ξ» a, lex_acc_inl (ha.apply a), ⟨λ x, sum.rec_on x aca (Ξ» b, lex_acc_inr aca (hb.apply b))⟩ end /-- Swap the factors of a sum type -/ @[simp] def swap : Ξ± βŠ• Ξ² β†’ Ξ² βŠ• Ξ± | (inl a) := inr a | (inr b) := inl b @[simp] lemma swap_swap (x : Ξ± βŠ• Ξ²) : swap (swap x) = x := by cases x; refl @[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 end sum namespace function open sum lemma injective.sum_map {f : Ξ± β†’ Ξ²} {g : Ξ±' β†’ Ξ²'} (hf : injective f) (hg : injective g) : injective (sum.map f g) | (inl x) (inl y) h := congr_arg inl $ hf $ inl.inj h | (inr x) (inr y) h := congr_arg inr $ hg $ inr.inj h lemma surjective.sum_map {f : Ξ± β†’ Ξ²} {g : Ξ±' β†’ Ξ²'} (hf : surjective f) (hg : surjective g) : surjective (sum.map f g) | (inl y) := let ⟨x, hx⟩ := hf y in ⟨inl x, congr_arg inl hx⟩ | (inr y) := let ⟨x, hx⟩ := hg y in ⟨inr x, congr_arg inr hx⟩ end function
f55deb223e30288db8b19a6342e85a3ee05ad8dc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/1021.lean
055522620b089ce3bfb9c0382470105bb95c1a1e
[ "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
108
lean
import Lean open Lean Meta #eval show MetaM _ from do findDeclarationRanges? `Lean.Elab.Term.expandAssert
be00e6df180d440032b06b265edd3bb461ee30bc
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/wlog_auto.lean
407cd736987b1b122cfeb085135d29d2f1b1a119
[]
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,471
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 Without loss of generality tactic. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.perm import Mathlib.PostPort namespace Mathlib namespace tactic namespace interactive /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊒ p xs_0 ∨ β‹― ∨ p xs_n` The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊒ g xs_0` The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊒ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `pβ‚€ ∨ (p₁ ∨ pβ‚‚)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. Example: On a state `(n m : β„•) ⊒ p n m` the tactic `wlog h : n ≀ m using [n m, m n]` produces the following states: `(n m : β„•) ⊒ n ≀ m ∨ m ≀ n` `(n m : β„•) (h : n ≀ m) ⊒ p n m` `(n m : β„•) (h : m ≀ n) (this : p n m) ⊒ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. (1) `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ β‹― ∨ ps xsn`, the main goal `(case : p xs0) ⊒ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊒ g xsi`. (2) `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ β‹― ∨ p xsi`, it is also used to compute the variable permutations. (3) `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ β‹― ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. (4) `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≀, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ end Mathlib
a92853e2bf9cccd52607ab7c832c66ee66a08953
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/field_theory/separable.lean
4a09fb62f4fac72dc65244afc27d3eb7dc3a1941
[ "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
21,044
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.squarefree import data.polynomial.expand import data.polynomial.splits import field_theory.minpoly import ring_theory.power_basis /-! # Separable polynomials We define a polynomial to be separable if it is coprime with its derivative. We prove basic properties about separable polynomials here. ## Main definitions * `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative. -/ universes u v w open_locale classical big_operators polynomial open finset namespace polynomial section comm_semiring variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] /-- A polynomial is separable iff it is coprime with its derivative. -/ def separable (f : R[X]) : Prop := is_coprime f f.derivative lemma separable_def (f : R[X]) : f.separable ↔ is_coprime f f.derivative := iff.rfl lemma separable_def' (f : R[X]) : f.separable ↔ βˆƒ a b : R[X], a * f + b * f.derivative = 1 := iff.rfl lemma not_separable_zero [nontrivial R] : Β¬ separable (0 : R[X]) := begin rintro ⟨x, y, h⟩, simpa only [derivative_zero, mul_zero, add_zero, zero_ne_one] using h, end lemma separable_one : (1 : R[X]).separable := is_coprime_one_left @[nontriviality] lemma separable_of_subsingleton [subsingleton R] (f : R[X]) : f.separable := by simp [separable] lemma separable_X_add_C (a : R) : (X + C a).separable := by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero], exact is_coprime_one_right } lemma separable_X : (X : R[X]).separable := by { rw [separable_def, derivative_X], exact is_coprime_one_right } lemma separable_C (r : R) : (C r).separable ↔ is_unit r := by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C] lemma separable.of_mul_left {f g : R[X]} (h : (f * g).separable) : f.separable := begin have := h.of_mul_left_left, rw derivative_mul at this, exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this) end lemma separable.of_mul_right {f g : R[X]} (h : (f * g).separable) : g.separable := by { rw mul_comm at h, exact h.of_mul_left } lemma separable.of_dvd {f g : R[X]} (hf : f.separable) (hfg : g ∣ f) : g.separable := by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf } lemma separable_gcd_left {F : Type*} [field F] {f : F[X]} (hf : f.separable) (g : F[X]) : (euclidean_domain.gcd f g).separable := separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g) lemma separable_gcd_right {F : Type*} [field F] {g : F[X]} (f : F[X]) (hg : g.separable) : (euclidean_domain.gcd f g).separable := separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g) lemma separable.is_coprime {f g : R[X]} (h : (f * g).separable) : is_coprime f g := begin have := h.of_mul_left_left, rw derivative_mul at this, exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this) end theorem separable.of_pow' {f : R[X]} : βˆ€ {n : β„•} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0 | 0 := Ξ» h, or.inr $ or.inr rfl | 1 := Ξ» h, or.inr $ or.inl ⟨pow_one f β–Έ h, rfl⟩ | (n+2) := Ξ» h, by { rw [pow_succ, pow_succ] at h, exact or.inl (is_coprime_self.1 h.is_coprime.of_mul_right_left) } theorem separable.of_pow {f : R[X]} (hf : Β¬is_unit f) {n : β„•} (hn : n β‰  0) (hfs : (f ^ n).separable) : f.separable ∧ n = 1 := (hfs.of_pow'.resolve_left hf).resolve_right hn theorem separable.map {p : R[X]} (h : p.separable) {f : R β†’+* S} : (p.map f).separable := let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f, by rw [derivative_map, ← polynomial.map_mul, ← polynomial.map_mul, ← polynomial.map_add, H, polynomial.map_one]⟩ variables (p q : β„•) lemma is_unit_of_self_mul_dvd_separable {p q : R[X]} (hp : p.separable) (hq : q * q ∣ p) : is_unit q := begin obtain ⟨p, rfl⟩ := hq, apply is_coprime_self.mp, have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)), { simp only [← mul_assoc, mul_add], convert hp, rw [derivative_mul, derivative_mul], ring }, exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this) end lemma multiplicity_le_one_of_separable {p q : R[X]} (hq : Β¬ is_unit q) (hsep : separable p) : multiplicity q p ≀ 1 := begin contrapose! hq, apply is_unit_of_self_mul_dvd_separable hsep, rw ← sq, apply multiplicity.pow_dvd_of_le_multiplicity, simpa only [nat.cast_one, nat.cast_bit0] using part_enat.add_one_le_of_lt hq end lemma separable.squarefree {p : R[X]} (hsep : separable p) : squarefree p := begin rw multiplicity.squarefree_iff_multiplicity_le_one p, intro f, by_cases hunit : is_unit f, { exact or.inr hunit }, exact or.inl (multiplicity_le_one_of_separable hunit hsep) end end comm_semiring section comm_ring variables {R : Type u} [comm_ring R] lemma separable_X_sub_C {x : R} : separable (X - C x) := by simpa only [sub_eq_add_neg, C_neg] using separable_X_add_C (-x) lemma separable.mul {f g : R[X]} (hf : f.separable) (hg : g.separable) (h : is_coprime f g) : (f * g).separable := by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left ((h.symm.mul_right hg).mul_add_right_right _) } lemma separable_prod' {ΞΉ : Sort*} {f : ΞΉ β†’ R[X]} {s : finset ΞΉ} : (βˆ€x∈s, βˆ€y∈s, x β‰  y β†’ is_coprime (f x) (f y)) β†’ (βˆ€x∈s, (f x).separable) β†’ (∏ x in s, f x).separable := finset.induction_on s (Ξ» _ _, separable_one) $ Ξ» a s has ih h1 h2, begin simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has, exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ Ξ» i his, h1.1.2 i his $ ne.symm $ ne_of_mem_of_not_mem his has) end lemma separable_prod {ΞΉ : Sort*} [fintype ΞΉ] {f : ΞΉ β†’ R[X]} (h1 : pairwise (is_coprime on f)) (h2 : βˆ€ x, (f x).separable) : (∏ x, f x).separable := separable_prod' (Ξ» x hx y hy hxy, h1 hxy) (Ξ» x hx, h2 x) lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ΞΉ : Sort*} {f : ΞΉ β†’ R} {s : finset ΞΉ} (hfs : (∏ i in s, (X - C (f i))).separable) {x y : ΞΉ} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y := begin by_contra hxy, rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy), prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← sq] at hfs, cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C _) two_ne_zero).2 end lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ΞΉ : Sort*} [fintype ΞΉ] {f : ΞΉ β†’ R} (hfs : (∏ i, (X - C (f i))).separable) : function.injective f := Ξ» x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy lemma nodup_of_separable_prod [nontrivial R] {s : multiset R} (hs : separable (multiset.map (Ξ» a, X - C a) s).prod) : s.nodup := begin rw multiset.nodup_iff_ne_cons_cons, rintros a t rfl, refine not_is_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _), simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _) end /--If `is_unit n` in a `comm_ring R`, then `X ^ n - u` is separable for any unit `u`. -/ lemma separable_X_pow_sub_C_unit {n : β„•} (u : RΛ£) (hn : is_unit (n : R)) : separable (X ^ n - C (u : R)) := begin nontriviality R, rcases n.eq_zero_or_pos with rfl | hpos, { simpa using hn }, apply (separable_def' (X ^ n - C (u : R))).2, obtain ⟨n', hn'⟩ := hn.exists_left_inv, refine ⟨-C ↑u⁻¹, C ↑u⁻¹ * C n' * X, _⟩, rw [derivative_sub, derivative_C, sub_zero, derivative_pow X n, derivative_X, mul_one], calc - C ↑u⁻¹ * (X ^ n - C ↑u) + C ↑u⁻¹ * C n' * X * (↑n * X ^ (n - 1)) = C (↑u⁻¹ * ↑ u) - C ↑u⁻¹ * X^n + C ↑ u ⁻¹ * C (n' * ↑n) * (X * X ^ (n - 1)) : by { simp only [C.map_mul, C_eq_nat_cast], ring } ... = 1 : by simp only [units.inv_mul, hn', C.map_one, mul_one, ← pow_succ, nat.sub_add_cancel (show 1 ≀ n, from hpos), sub_add_cancel] end lemma root_multiplicity_le_one_of_separable [nontrivial R] {p : R[X]} (hsep : separable p) (x : R) : root_multiplicity x p ≀ 1 := begin by_cases hp : p = 0, { simp [hp], }, rw [root_multiplicity_eq_multiplicity, dif_neg hp, ← part_enat.coe_le_coe, part_enat.coe_get, nat.cast_one], exact multiplicity_le_one_of_separable (not_is_unit_X_sub_C _) hsep end end comm_ring section is_domain variables {R : Type u} [comm_ring R] [is_domain R] lemma count_roots_le_one {p : R[X]} (hsep : separable p) (x : R) : p.roots.count x ≀ 1 := begin rw count_roots p, exact root_multiplicity_le_one_of_separable hsep x end lemma nodup_roots {p : R[X]} (hsep : separable p) : p.roots.nodup := multiset.nodup_iff_count_le_one.mpr (count_roots_le_one hsep) end is_domain section field variables {F : Type u} [field F] {K : Type v} [field K] theorem separable_iff_derivative_ne_zero {f : F[X]} (hf : irreducible f) : f.separable ↔ f.derivative β‰  0 := ⟨λ h1 h2, hf.not_unit $ is_coprime_zero_right.1 $ h2 β–Έ h1, Ξ» h, euclidean_domain.is_coprime_of_dvd (mt and.right h) $ Ξ» g hg1 hg2 ⟨p, hg3⟩ hg4, let ⟨u, hu⟩ := (hf.is_unit_or_is_unit hg3).resolve_left hg1 in have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd }, not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt $ mt derivative_of_nat_degree_zero h⟩ theorem separable_map (f : F β†’+* K) {p : F[X]} : (p.map f).separable ↔ p.separable := by simp_rw [separable_def, derivative_map, is_coprime_map] lemma separable_prod_X_sub_C_iff' {ΞΉ : Sort*} {f : ΞΉ β†’ F} {s : finset ΞΉ} : (∏ i in s, (X - C (f i))).separable ↔ (βˆ€ (x ∈ s) (y ∈ s), f x = f y β†’ x = y) := ⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy, Ξ» H, by { rw ← prod_attach, exact separable_prod' (Ξ» x hx y hy hxy, @pairwise_coprime_X_sub_C _ _ { x // x ∈ s } (Ξ» x, f x) (Ξ» x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy) (Ξ» _ _, separable_X_sub_C) }⟩ lemma separable_prod_X_sub_C_iff {ΞΉ : Sort*} [fintype ΞΉ] {f : ΞΉ β†’ F} : (∏ i, (X - C (f i))).separable ↔ function.injective f := separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff, function.injective] section char_p variables (p : β„•) [HF : char_p F p] include HF theorem separable_or {f : F[X]} (hf : irreducible f) : f.separable ∨ Β¬f.separable ∧ βˆƒ g : F[X], irreducible g ∧ expand F p g = f := if H : f.derivative = 0 then begin unfreezingI { rcases p.eq_zero_or_pos with rfl | hp }, { haveI := char_p.char_p_to_char_zero F, have := nat_degree_eq_zero_of_derivative_eq_zero H, have := (nat_degree_pos_iff_degree_pos.mpr $ degree_pos_of_irreducible hf).ne', contradiction }, haveI := is_local_ring_hom_expand F hp, exact or.inr ⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H], contract p f, of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H hp.ne' at hf), expand_contract p H hp.ne'⟩ end else or.inl $ (separable_iff_derivative_ne_zero hf).2 H theorem exists_separable_of_irreducible {f : F[X]} (hf : irreducible f) (hp : p β‰  0) : βˆƒ (n : β„•) (g : F[X]), g.separable ∧ expand F (p ^ n) g = f := begin replace hp : p.prime := (char_p.char_is_prime_or_zero F p).resolve_right hp, unfreezingI { induction hn : f.nat_degree using nat.strong_induction_on with N ih generalizing f }, rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩, { refine ⟨0, f, h, _⟩, rw [pow_zero, expand_one] }, { cases N with N, { rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn, rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1, have hf0 : f β‰  0 := hf.ne_zero, rw [h1, C_0] at hn, exact absurd hn hf0 }, have hg1 : g.nat_degree * p = N.succ, { rwa [← nat_degree_expand, hgf] }, have hg2 : g.nat_degree β‰  0, { intro this, rw [this, zero_mul] at hg1, cases hg1 }, have hg3 : g.nat_degree < N.succ, { rw [← mul_one g.nat_degree, ← hg1], exact nat.mul_lt_mul_of_pos_left hp.one_lt hg2.bot_lt }, rcases ih _ hg3 hg rfl with ⟨n, g, hg4, rfl⟩, refine ⟨n+1, g, hg4, _⟩, rw [← hgf, expand_expand, pow_succ] } end theorem is_unit_or_eq_zero_of_separable_expand {f : F[X]} (n : β„•) (hp : 0 < p) (hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 := begin rw or_iff_not_imp_right, rintro hn : n β‰  0, have hf2 : (expand F (p ^ n) f).derivative = 0, { rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero, zero_pow hn.bot_lt, zero_mul, mul_zero] }, rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf, rcases hf with ⟨r, hr, hrf⟩, rw [eq_comm, expand_eq_C (pow_pos hp _)] at hrf, rwa [hrf, is_unit_C] end theorem unique_separable_of_irreducible {f : F[X]} (hf : irreducible f) (hp : 0 < p) (n₁ : β„•) (g₁ : F[X]) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f) (nβ‚‚ : β„•) (gβ‚‚ : F[X]) (hgβ‚‚ : gβ‚‚.separable) (hgfβ‚‚ : expand F (p ^ nβ‚‚) gβ‚‚ = f) : n₁ = nβ‚‚ ∧ g₁ = gβ‚‚ := begin revert g₁ gβ‚‚, wlog hn : n₁ ≀ nβ‚‚ := le_total n₁ nβ‚‚ using [n₁ nβ‚‚, nβ‚‚ n₁], have hf0 : f β‰  0 := hf.ne_zero, unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩, rw [← hgf₁, pow_add, expand_mul, expand_inj (pow_pos hp n₁)] at hgfβ‚‚, subst hgfβ‚‚, subst hgf₁, rcases is_unit_or_eq_zero_of_separable_expand p k hp hg₁ with h | rfl, { rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩, simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 }, { rw [add_zero, pow_zero, expand_one], split; refl } }, obtain ⟨hn, hg⟩ := this gβ‚‚ g₁ hgβ‚‚ hgfβ‚‚ hg₁ hgf₁, exact ⟨hn.symm, hg.symm⟩ end end char_p /--If `n β‰  0` in `F`, then ` X ^ n - a` is separable for any `a β‰  0`. -/ lemma separable_X_pow_sub_C {n : β„•} (a : F) (hn : (n : F) β‰  0) (ha : a β‰  0) : separable (X ^ n - C a) := separable_X_pow_sub_C_unit (units.mk0 a ha) (is_unit.mk0 n hn) -- this can possibly be strengthened to making `separable_X_pow_sub_C_unit` a -- bi-implication, but it is nontrivial! /-- In a field `F`, `X ^ n - 1` is separable iff `↑n β‰  0`. -/ lemma X_pow_sub_one_separable_iff {n : β„•} : (X ^ n - 1 : F[X]).separable ↔ (n : F) β‰  0 := begin refine ⟨_, Ξ» h, separable_X_pow_sub_C_unit 1 (is_unit.mk0 ↑n h)⟩, rw [separable_def', derivative_sub, derivative_X_pow, derivative_one, sub_zero], -- Suppose `(n : F) = 0`, then the derivative is `0`, so `X ^ n - 1` is a unit, contradiction. rintro (h : is_coprime _ _) hn', rw [← C_eq_nat_cast, hn', C.map_zero, zero_mul, is_coprime_zero_right] at h, have := not_is_unit_X_pow_sub_one F n, contradiction end section splits lemma card_root_set_eq_nat_degree [algebra F K] {p : F[X]} (hsep : p.separable) (hsplit : splits (algebra_map F K) p) : fintype.card (p.root_set K) = p.nat_degree := begin simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe], rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots hsplit], exact nodup_roots hsep.map, end variable {i : F β†’+* K} lemma eq_X_sub_C_of_separable_of_root_eq {x : F} {h : F[X]} (h_sep : h.separable) (h_root : h.eval x = 0) (h_splits : splits i h) (h_roots : βˆ€ y ∈ (h.map i).roots, y = i x) : h = (C (leading_coeff h)) * (X - C x) := begin have h_ne_zero : h β‰  0 := by { rintro rfl, exact not_separable_zero h_sep }, apply polynomial.eq_X_sub_C_of_splits_of_single_root i h_splits, apply finset.mk.inj, { change _ = {i x}, rw finset.eq_singleton_iff_unique_mem, split, { apply finset.mem_mk.mpr, rw mem_roots (show h.map i β‰  0, by exact map_ne_zero h_ne_zero), rw [is_root.def,←evalβ‚‚_eq_eval_map,evalβ‚‚_hom,h_root], exact ring_hom.map_zero i }, { exact h_roots } }, { exact nodup_roots (separable.map h_sep) }, end lemma exists_finset_of_splits (i : F β†’+* K) {f : F[X]} (sep : separable f) (sp : splits i f) : βˆƒ (s : finset K), f.map i = C (i f.leading_coeff) * (s.prod (Ξ» a : K, X - C a)) := begin obtain ⟨s, h⟩ := (splits_iff_exists_multiset _).1 sp, use s.to_finset, rw [h, finset.prod_eq_multiset_prod, ←multiset.to_finset_eq], apply nodup_of_separable_prod, apply separable.of_mul_right, rw ←h, exact sep.map, end end splits theorem _root_.irreducible.separable [char_zero F] {f : F[X]} (hf : irreducible f) : f.separable := begin rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq], { rintro ⟨⟩ }, rw [pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff], refine Ξ» hf1, hf.not_unit _, rw [hf1, is_unit_C, is_unit_iff_ne_zero], intro hf2, rw [hf2, C_0] at hf1, exact absurd hf1 hf.ne_zero end end field end polynomial open polynomial section comm_ring variables (F K : Type*) [comm_ring F] [ring K] [algebra F K] -- TODO: refactor to allow transcendental extensions? -- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions -- Note that right now a Galois extension (class `is_galois`) is defined to be an extension which -- is separable and normal, so if the definition of separable changes here at some point -- to allow non-algebraic extensions, then the definition of `is_galois` must also be changed. /-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff the minimal polynomial of every `x : K` is separable. We define this for general (commutative) rings and only assume `F` and `K` are fields if this is needed for a proof. -/ class is_separable : Prop := (is_integral' (x : K) : is_integral F x) (separable' (x : K) : (minpoly F x).separable) variables (F) {K} theorem is_separable.is_integral [is_separable F K] : βˆ€ x : K, is_integral F x := is_separable.is_integral' theorem is_separable.separable [is_separable F K] : βˆ€ x : K, (minpoly F x).separable := is_separable.separable' variables {F K} theorem is_separable_iff : is_separable F K ↔ βˆ€ x : K, is_integral F x ∧ (minpoly F x).separable := ⟨λ h x, ⟨@@is_separable.is_integral F _ _ _ h x, @@is_separable.separable F _ _ _ h x⟩, Ξ» h, ⟨λ x, (h x).1, Ξ» x, (h x).2⟩⟩ end comm_ring instance is_separable_self (F : Type*) [field F] : is_separable F F := ⟨λ x, is_integral_algebra_map, Ξ» x, by { rw minpoly.eq_X_sub_C', exact separable_X_sub_C }⟩ /-- A finite field extension in characteristic 0 is separable. -/ @[priority 100] -- See note [lower instance priority] instance is_separable.of_finite (F K : Type*) [field F] [field K] [algebra F K] [finite_dimensional F K] [char_zero F] : is_separable F K := have βˆ€ (x : K), is_integral F x, from Ξ» x, algebra.is_integral_of_finite _ _ _, ⟨this, Ξ» x, (minpoly.irreducible (this x)).separable⟩ section is_separable_tower variables (F K E : Type*) [field F] [field K] [field E] [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] lemma is_separable_tower_top_of_is_separable [is_separable F E] : is_separable K E := ⟨λ x, is_integral_of_is_scalar_tower (is_separable.is_integral F x), Ξ» x, (is_separable.separable F x).map.of_dvd (minpoly.dvd_map_of_is_scalar_tower _ _ _)⟩ lemma is_separable_tower_bot_of_is_separable [h : is_separable F E] : is_separable F K := is_separable_iff.2 $ Ξ» x, begin refine (is_separable_iff.1 h (algebra_map K E x)).imp is_integral_tower_bot_of_is_integral_field (Ξ» hs, _), obtain ⟨q, hq⟩ := minpoly.dvd F x ((aeval_algebra_map_eq_zero_iff _ _ _).mp (minpoly.aeval F ((algebra_map K E) x))), rw hq at hs, exact hs.of_mul_left end variables {E} lemma is_separable.of_alg_hom (E' : Type*) [field E'] [algebra F E'] (f : E →ₐ[F] E') [is_separable F E'] : is_separable F E := begin letI : algebra E E' := ring_hom.to_algebra f.to_ring_hom, haveI : is_scalar_tower F E E' := is_scalar_tower.of_algebra_map_eq (Ξ» x, (f.commutes x).symm), exact is_separable_tower_bot_of_is_separable F E E', end end is_separable_tower section card_alg_hom variables {R S T : Type*} [comm_ring S] variables {K L F : Type*} [field K] [field L] [field F] variables [algebra K S] [algebra K L] lemma alg_hom.card_of_power_basis (pb : power_basis K S) (h_sep : (minpoly K pb.gen).separable) (h_splits : (minpoly K pb.gen).splits (algebra_map K L)) : @fintype.card (S →ₐ[K] L) (power_basis.alg_hom.fintype pb) = pb.dim := begin let s := ((minpoly K pb.gen).map (algebra_map K L)).roots.to_finset, have H := Ξ» x, multiset.mem_to_finset, rw [fintype.card_congr pb.lift_equiv', fintype.card_of_subtype s H, ← pb.nat_degree_minpoly, nat_degree_eq_card_roots h_splits, multiset.to_finset_card_of_nodup], exact nodup_roots ((separable_map (algebra_map K L)).mpr h_sep) end end card_alg_hom
4c45501e894f454950fc6f3917199ba0b41aee9b
947fa6c38e48771ae886239b4edce6db6e18d0fb
/archive/100-theorems-list/73_ascending_descending_sequences.lean
41e92aff0d128067a309900dd5c94922fcfa28bd
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
8,063
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 /-- **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`. 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_on f ↑t) ∨ (βˆƒ (t : finset (fin n)), s < t.card ∧ strict_anti_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 = i ∧ strict_mono_on f ↑t), let dec_sequences_ending_in : fin n β†’ finset (finset (fin n)) := Ξ» i, univ.powerset.filter (Ξ» t, finset.max t = i ∧ strict_anti_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_on], have dec_i : βˆ€ i, {i} ∈ dec_sequences_ending_in i := Ξ» i, by simp [strict_anti_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 1 { have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 2 { 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 1 { apply ne_of_lt _ q₁, have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 2 { 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 : t.max = i, 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`. { convert max_insert, rw [ht₁.2.1, max_eq_left], apply with_bot.coe_le_coe.mpr (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_on, strict_anti_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β€Ί β€Ήt.max = iβ€Ί) }, { 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β€Ί β€Ήt.max = iβ€Ί) with _ | rfl, { apply le_of_lt (ht₁.2.2 β€Ήx ∈ tβ€Ί (mem_of_max β€Ήt.max = iβ€Ί) β€Ή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β€Ί β€Ήt.max = iβ€Ί) } } }, -- 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 Γ—Λ’ (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 tsub_lt_iff_right z.1, apply nat.lt_succ_of_le q.1 }, { refine ⟨(ab i).2 - 1, _, nat.succ_pred_eq_of_pos z.2⟩, rw tsub_lt_iff_right 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
d4c549fedd7e88eabf31c0229bc778f2d5097691
b2fe74b11b57d362c13326bc5651244f111fa6f4
/src/analysis/mean_inequalities.lean
892eccfde7d33daf735e00a54bb4f08899b00aa3
[ "Apache-2.0" ]
permissive
midfield/mathlib
c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7
775edc615ecec631d65b6180dbcc7bc26c3abc26
refs/heads/master
1,675,330,551,921
1,608,304,514,000
1,608,304,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
47,315
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, SΓ©bastien GouΓ«zel, RΓ©my Degenne -/ import analysis.convex.specific_functions import analysis.special_functions.pow import data.real.conjugate_exponents import tactic.nth_rewrite import measure_theory.integration /-! # Mean value inequalities In this file we prove several inequalities, including AM-GM inequality, Young's inequality, HΓΆlder inequality, and Minkowski inequality. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≀ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `real` namespace, and a version for `nnreal`-valued functions is in the `nnreal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### Generalized mean inequality The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$ and $p ≀ q$ we have $$ \sqrt[p]{\sum_{i\in s} w_i z_i^p} ≀ \sqrt[q]{\sum_{i\in s} w_i z_i^q}. $$ Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents (`fpow_arith_mean_le_arith_mean_fpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and `arith_mean_le_rpow_mean`). In the first two cases we prove $$ \left(\sum_{i\in s} w_i z_i\right)^n ≀ \sum_{i\in s} w_i z_i^n $$ in order to avoid using real exponents. For real exponents we prove both this and standard versions. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≀ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It can be used to prove HΓΆlder's inequality (see below) but we use a different proof. ### HΓΆlder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≀ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `real`, `nnreal` and `ennreal`. There are at least two short proofs of this inequality. In one proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. We use a different proof deducing this inequality from the generalized mean inequality for well-chosen vectors and weights. HΓΆlder's inequality for the Lebesgue integral of ennreal and nnreal functions: we prove `∫ (f * g) βˆ‚ΞΌ ≀ (∫ f^p βˆ‚ΞΌ) ^ (1/p) * (∫ g^q βˆ‚ΞΌ) ^ (1/q)` for `p`, `q` conjugate real exponents and `Ξ±β†’(e)nnreal` functions in two cases, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ennreal functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : nnreal functions. ### Minkowski's inequality The inequality says that for `p β‰₯ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `real`, `nnreal` and `ennreal`. We deduce this inequality from HΓΆlder's inequality. Namely, HΓΆlder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. Minkowski's inequality for the Lebesgue integral of measurable functions with `ennreal` values: we prove `(∫ (f + g)^p βˆ‚ΞΌ) ^ (1/p) ≀ (∫ f^p βˆ‚ΞΌ) ^ (1/p) + (∫ g^p βˆ‚ΞΌ) ^ (1/p)` for `1 ≀ p`. ## TODO - each inequality `A ≀ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `strict_convex_on` functions. - generalized mean inequality with any `p ≀ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. - prove integral versions of these inequalities. -/ universes u v open finset open_locale classical nnreal big_operators noncomputable theory variables {ΞΉ : Type u} (s : finset ΞΉ) namespace real /-- AM-GM inequality: the geometric mean is less than or equal to the arithmetic mean, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) : (∏ i in s, (z i) ^ (w i)) ≀ βˆ‘ i in s, w i * z i := begin -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : βˆƒ i ∈ s, z i = 0 ∧ w i β‰  0, { rcases A with ⟨i, his, hzi, hwi⟩, rw [prod_eq_zero his], { exact sum_nonneg (Ξ» j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow hwi } }, -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. { simp only [not_exists, not_and, ne.def, not_not] at A, have := convex_on_exp.map_sum_le hw hw' (Ξ» i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { exact rpow_def_of_pos hz _ } }, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { rw [exp_log hz] } } } end theorem pow_arith_mean_le_arith_mean_pow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) (n : β„•) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := (convex_on_pow n).map_sum_le hw hw' hz theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) {n : β„•} (hn : even n) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := (convex_on_pow_of_even hn).map_sum_le hw hw' (Ξ» _ _, trivial) theorem fpow_arith_mean_le_arith_mean_fpow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 < z i) (m : β„€) : (βˆ‘ i in s, w i * z i) ^ m ≀ βˆ‘ i in s, (w i * z i ^ m) := (convex_on_fpow m).map_sum_le hw hw' hz theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := (convex_on_rpow hp).map_sum_le hw hw' hz theorem arith_mean_le_rpow_mean (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) {p : ℝ} (hp : 1 ≀ p) : βˆ‘ i in s, w i * z i ≀ (βˆ‘ i in s, (w i * z i ^ p)) ^ (1 / p) := begin have : 0 < p := lt_of_lt_of_le zero_lt_one hp, rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one], exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp, all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg], intros i hi, apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] }, end end real namespace nnreal /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for `nnreal`-valued functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) : (∏ i in s, (z i) ^ (w i:ℝ)) ≀ βˆ‘ i in s, w i * z i := by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (Ξ» i _, (w i).coe_nonneg) (by assumption_mod_cast) (Ξ» i _, (z i).coe_nonneg) /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for two `nnreal` numbers. -/ theorem geom_mean_le_arith_mean2_weighted (w₁ wβ‚‚ p₁ pβ‚‚ : ℝβ‰₯0) : w₁ + wβ‚‚ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin_zero_elim) theorem geom_mean_le_arith_mean3_weighted (w₁ wβ‚‚ w₃ p₁ pβ‚‚ p₃ : ℝβ‰₯0) : w₁ + wβ‚‚ + w₃ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) * p₃ ^ (w₃:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 3)) (fin.cons w₁ $ fin.cons wβ‚‚ $ fin.cons w₃ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin.cons p₃ fin_zero_elim) theorem geom_mean_le_arith_mean4_weighted (w₁ wβ‚‚ w₃ wβ‚„ p₁ pβ‚‚ p₃ pβ‚„ : ℝβ‰₯0) : w₁ + wβ‚‚ + w₃ + wβ‚„ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) * p₃ ^ (w₃:ℝ)* pβ‚„ ^ (wβ‚„:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ + wβ‚„ * pβ‚„ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 4)) (fin.cons w₁ $ fin.cons wβ‚‚ $ fin.cons w₃ $ fin.cons wβ‚„ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin.cons p₃ $ fin.cons pβ‚„ fin_zero_elim) /-- Weighted generalized mean inequality, version sums over finite sets, with `ℝβ‰₯0`-valued functions and natural exponent. -/ theorem pow_arith_mean_le_arith_mean_pow (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) (n : β„•) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) n /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝβ‰₯0`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) hp /-- Weighted generalized mean inequality, version for two elements of `ℝβ‰₯0` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ wβ‚‚ z₁ zβ‚‚ : ℝβ‰₯0) (hw' : w₁ + wβ‚‚ = 1) {p : ℝ} (hp : 1 ≀ p) : (w₁ * z₁ + wβ‚‚ * zβ‚‚) ^ p ≀ w₁ * z₁ ^ p + wβ‚‚ * zβ‚‚ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons z₁ $ fin.cons zβ‚‚ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝβ‰₯0`-valued functions and real exponents. -/ theorem arith_mean_le_rpow_mean (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : βˆ‘ i in s, w i * z i ≀ (βˆ‘ i in s, (w i * z i ^ p)) ^ (1 / p) := by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) hp end nnreal namespace ennreal /-- Weighted generalized mean inequality, version for sums over finite sets, with `ennreal`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ennreal) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp, have hp_nonneg : 0 ≀ p, from le_of_lt hp_pos, have hp_not_nonpos : Β¬ p ≀ 0, by simp [hp_pos], have hp_not_neg : Β¬ p < 0, by simp [hp_nonneg], have h_top_iff_rpow_top : βˆ€ (i : ΞΉ) (hi : i ∈ s), w i * z i = ⊀ ↔ w i * (z i) ^ p = ⊀, by simp [hp_pos, hp_nonneg, hp_not_nonpos, hp_not_neg], refine le_of_top_imp_top_of_to_nnreal_le _ _, { -- first, prove `(βˆ‘ i in s, w i * z i) ^ p = ⊀ β†’ βˆ‘ i in s, (w i * z i ^ p) = ⊀` rw [rpow_eq_top_iff, sum_eq_top_iff, sum_eq_top_iff], intro h, simp only [and_false, hp_not_neg, false_or] at h, rcases h.left with ⟨a, H, ha⟩, use [a, H], rwa ←h_top_iff_rpow_top a H, }, { -- second, suppose both `(βˆ‘ i in s, w i * z i) ^ p β‰  ⊀` and `βˆ‘ i in s, (w i * z i ^ p) β‰  ⊀`, -- and prove `((βˆ‘ i in s, w i * z i) ^ p).to_nnreal ≀ (βˆ‘ i in s, (w i * z i ^ p)).to_nnreal`, -- by using `nnreal.rpow_arith_mean_le_arith_mean_rpow`. intros h_top_rpow_sum _, -- show hypotheses needed to put the `.to_nnreal` inside the sums. have h_top : βˆ€ (a : ΞΉ), a ∈ s β†’ w a * z a < ⊀, { have h_top_sum : βˆ‘ (i : ΞΉ) in s, w i * z i < ⊀, { by_contra h, rw [lt_top_iff_ne_top, not_not] at h, rw [h, top_rpow_of_pos hp_pos] at h_top_rpow_sum, exact h_top_rpow_sum rfl, }, rwa sum_lt_top_iff at h_top_sum, }, have h_top_rpow : βˆ€ (a : ΞΉ), a ∈ s β†’ w a * z a ^ p < ⊀, { intros i hi, specialize h_top i hi, rw lt_top_iff_ne_top at h_top ⊒, rwa [ne.def, ←h_top_iff_rpow_top i hi], }, -- put the `.to_nnreal` inside the sums. simp_rw [to_nnreal_sum h_top_rpow, ←to_nnreal_rpow, to_nnreal_sum h_top, to_nnreal_mul, ←to_nnreal_rpow], -- use corresponding nnreal result refine nnreal.rpow_arith_mean_le_arith_mean_rpow s (Ξ» i, (w i).to_nnreal) (Ξ» i, (z i).to_nnreal) _ hp, -- verify the hypothesis `βˆ‘ i in s, (w i).to_nnreal = 1`, using `βˆ‘ i in s, w i = 1` . have h_sum_nnreal : (βˆ‘ i in s, w i) = ↑(βˆ‘ i in s, (w i).to_nnreal), { have hw_top : βˆ‘ i in s, w i < ⊀, by { rw hw', exact one_lt_top, }, rw ←to_nnreal_sum, { rw coe_to_nnreal, rwa ←lt_top_iff_ne_top, }, { rwa sum_lt_top_iff at hw_top, }, }, rwa [←coe_eq_coe, ←h_sum_nnreal], }, end /-- Weighted generalized mean inequality, version for two elements of `ennreal` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ wβ‚‚ z₁ zβ‚‚ : ennreal) (hw' : w₁ + wβ‚‚ = 1) {p : ℝ} (hp : 1 ≀ p) : (w₁ * z₁ + wβ‚‚ * zβ‚‚) ^ p ≀ w₁ * z₁ ^ p + wβ‚‚ * zβ‚‚ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons z₁ $ fin.cons zβ‚‚ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end end ennreal namespace real theorem geom_mean_le_arith_mean2_weighted {w₁ wβ‚‚ p₁ pβ‚‚ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hw : w₁ + wβ‚‚ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := nnreal.geom_mean_le_arith_mean2_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean3_weighted {w₁ wβ‚‚ w₃ p₁ pβ‚‚ p₃ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hw₃ : 0 ≀ w₃) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hp₃ : 0 ≀ p₃) (hw : w₁ + wβ‚‚ + w₃ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ * p₃ ^ w₃ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ := nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨w₃, hwβ‚ƒβŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© ⟨p₃, hpβ‚ƒβŸ© $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean4_weighted {w₁ wβ‚‚ w₃ wβ‚„ p₁ pβ‚‚ p₃ pβ‚„ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hw₃ : 0 ≀ w₃) (hwβ‚„ : 0 ≀ wβ‚„) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hp₃ : 0 ≀ p₃) (hpβ‚„ : 0 ≀ pβ‚„) (hw : w₁ + wβ‚‚ + w₃ + wβ‚„ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ * p₃ ^ w₃ * pβ‚„ ^ wβ‚„ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ + wβ‚„ * pβ‚„ := nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨w₃, hwβ‚ƒβŸ© ⟨wβ‚„, hwβ‚„βŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© ⟨p₃, hpβ‚ƒβŸ© ⟨pβ‚„, hpβ‚„βŸ© $ nnreal.coe_eq.1 $ by assumption /-- Young's inequality, a version for nonnegative real numbers. -/ theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≀ a) (hb : 0 ≀ b) (hpq : p.is_conjugate_exponent q) : a * b ≀ a^p / p + b^q / q := by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul] using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg (rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj /-- Young's inequality, a version for arbitrary real numbers. -/ theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ (abs a)^p / p + (abs b)^q / q := calc a * b ≀ abs (a * b) : le_abs_self (a * b) ... = abs a * abs b : abs_mul a b ... ≀ (abs a)^p / p + (abs b)^q / q : real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq end real namespace nnreal /-- Young's inequality, `ℝβ‰₯0` version. We use `{p q : ℝβ‰₯0}` in order to avoid constructing witnesses of `0 ≀ p` and `0 ≀ q` for the denominators. -/ theorem young_inequality (a b : ℝβ‰₯0) {p q : ℝβ‰₯0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) : a * b ≀ a^(p:ℝ) / p + b^(q:ℝ) / q := real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩ /-- Young's inequality, `ℝβ‰₯0` version with real conjugate exponents. -/ theorem young_inequality_real (a b : ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ a ^ p / nnreal.of_real p + b ^ q / nnreal.of_real q := begin nth_rewrite 0 ←coe_of_real p hpq.nonneg, nth_rewrite 0 ←coe_of_real q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, end /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝβ‰₯0`-valued functions. -/ theorem inner_le_Lp_mul_Lq (f g : ΞΉ β†’ ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) * (βˆ‘ i in s, (g i) ^ q) ^ (1 / q) := begin -- Let `G=βˆ₯gβˆ₯_q` be the `L_q`-norm of `g`. set G := (βˆ‘ i in s, (g i) ^ q) ^ (1 / q), have hGq : G ^ q = βˆ‘ i in s, (g i) ^ q, { rw [← rpow_mul, one_div_mul_cancel hpq.symm.ne_zero, rpow_one], }, -- First consider the trivial case `βˆ₯gβˆ₯_q=0` by_cases hG : G = 0, { rw [hG, sum_eq_zero, mul_zero], intros i hi, simp only [rpow_eq_zero_iff, sum_eq_zero_iff] at hG, simp [(hG.1 i hi).1] }, { -- Move power from right to left rw [← div_le_iff hG, sum_div], -- Now the inequality follows from the weighted generalized mean inequality -- with weights `w_i` and numbers `z_i` given by the following formulas. set w : ΞΉ β†’ ℝβ‰₯0 := Ξ» i, (g i) ^ q / G ^ q, set z : ΞΉ β†’ ℝβ‰₯0 := Ξ» i, f i * (G / g i) ^ (q / p), -- Show that the sum of weights equals one have A : βˆ‘ i in s, w i = 1, { rw [← sum_div, hGq, div_self], simpa [rpow_eq_zero_iff, hpq.symm.ne_zero] using hG }, -- LHS of the goal equals LHS of the weighted generalized mean inequality calc (βˆ‘ i in s, f i * g i / G) = (βˆ‘ i in s, w i * z i) : begin refine sum_congr rfl (Ξ» i hi, _), have : q - q / p = 1, by field_simp [hpq.ne_zero, hpq.symm.mul_eq_add], dsimp only [w, z], rw [← div_rpow, mul_left_comm, mul_div_assoc, ← @inv_div _ _ _ G, inv_rpow, ← div_eq_mul_inv, ← rpow_sub']; simp [this] end -- Apply the generalized mean inequality ... ≀ (βˆ‘ i in s, w i * (z i) ^ p) ^ (1 / p) : nnreal.arith_mean_le_rpow_mean s w z A (le_of_lt hpq.one_lt) -- Simplify the right hand side. Terms with `g i β‰  0` are equal to `(f i) ^ p`, -- the others are zeros. ... ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) : begin refine rpow_le_rpow (sum_le_sum (Ξ» i hi, _)) hpq.one_div_nonneg, dsimp only [w, z], rw [mul_rpow, mul_left_comm, ← rpow_mul _ _ p, div_mul_cancel _ hpq.ne_zero, div_rpow, div_mul_div, mul_comm (G ^ q), mul_div_mul_right], { nth_rewrite 1 [← mul_one ((f i) ^ p)], exact canonically_ordered_semiring.mul_le_mul (le_refl _) (div_self_le _) }, { simpa [hpq.symm.ne_zero] using hG } end } end /-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product `βˆ‘ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/ theorem is_greatest_Lp (f : ΞΉ β†’ ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : is_greatest ((Ξ» g : ΞΉ β†’ ℝβ‰₯0, βˆ‘ i in s, f i * g i) '' {g | βˆ‘ i in s, (g i)^q ≀ 1}) ((βˆ‘ i in s, (f i)^p) ^ (1 / p)) := begin split, { use Ξ» i, ((f i) ^ p / f i / (βˆ‘ i in s, (f i) ^ p) ^ (1 / q)), by_cases hf : βˆ‘ i in s, (f i)^p = 0, { simp [hf, hpq.ne_zero, hpq.symm.ne_zero] }, { have A : p + q - q β‰  0, by simp [hpq.ne_zero], have B : βˆ€ y : ℝβ‰₯0, y * y^p / y = y^p, { refine Ξ» y, mul_div_cancel_left_of_imp (Ξ» h, _), simpa [h, hpq.ne_zero] }, simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul, div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add, ← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B], rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one], simpa [hpq.symm.ne_zero] using hf } }, { rintros _ ⟨g, hg, rfl⟩, apply le_trans (inner_le_Lp_mul_Lq s f g hpq), simpa only [mul_one] using canonically_ordered_semiring.mul_le_mul (le_refl _) (nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) } end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/ theorem Lp_add_le (f g : ΞΉ β†’ ℝβ‰₯0) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, (f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (g i) ^ p) ^ (1 / p) := begin -- The result is trivial when `p = 1`, so we can assume `1 < p`. rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp, have := is_greatest_Lp s (f + g) hpq, simp only [pi.add_apply, add_mul, sum_add_distrib] at this, rcases this.1 with βŸ¨Ο†, hΟ†, H⟩, rw ← H, exact add_le_add ((is_greatest_Lp s f hpq).2 βŸ¨Ο†, hΟ†, rfl⟩) ((is_greatest_Lp s g hpq).2 βŸ¨Ο†, hΟ†, rfl⟩) end end nnreal namespace real variables (f g : ΞΉ β†’ ℝ) {p q : ℝ} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (abs $ f i)^p) ^ (1 / p) * (βˆ‘ i in s, (abs $ g i)^q) ^ (1 / q) := begin have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (Ξ» i, ⟨_, abs_nonneg (f i)⟩) (Ξ» i, ⟨_, abs_nonneg (g i)⟩) hpq), push_cast at this, refine le_trans (sum_le_sum $ Ξ» i hi, _) this, simp only [← abs_mul, le_abs_self] end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/ theorem Lp_add_le (hp : 1 ≀ p) : (βˆ‘ i in s, (abs $ f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (abs $ f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (abs $ g i) ^ p) ^ (1 / p) := begin have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (Ξ» i, ⟨_, abs_nonneg (f i)⟩) (Ξ» i, ⟨_, abs_nonneg (g i)⟩) hp), push_cast at this, refine le_trans (rpow_le_rpow _ (sum_le_sum $ Ξ» i hi, _) _) this; simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add, rpow_le_rpow] end variables {f g} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued nonnegative functions. -/ theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q) (hf : βˆ€ i ∈ s, 0 ≀ f i) (hg : βˆ€ i ∈ s, 0 ≀ g i) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (f i)^p) ^ (1 / p) * (βˆ‘ i in s, (g i)^q) ^ (1 / q) := by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi] /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued nonnegative functions. -/ theorem Lp_add_le_of_nonneg (hp : 1 ≀ p) (hf : βˆ€ i ∈ s, 0 ≀ f i) (hg : βˆ€ i ∈ s, 0 ≀ g i) : (βˆ‘ i in s, (f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (g i) ^ p) ^ (1 / p) := by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1]; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg] end real namespace ennreal /-- Young's inequality, `ennreal` version with real conjugate exponents. -/ theorem young_inequality (a b : ennreal) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ a ^ p / ennreal.of_real p + b ^ q / ennreal.of_real q := begin by_cases h : a = ⊀ ∨ b = ⊀, { refine le_trans le_top (le_of_eq _), repeat {rw ennreal.div_def}, cases h; rw h; simp [h, hpq.pos, hpq.symm.pos], }, push_neg at h, -- if a β‰  ⊀ and b β‰  ⊀, use the nnreal version: nnreal.young_inequality_real rw [←coe_to_nnreal h.left, ←coe_to_nnreal h.right, ←coe_mul, coe_rpow_of_nonneg _ hpq.nonneg, coe_rpow_of_nonneg _ hpq.symm.nonneg, ennreal.of_real, ennreal.of_real, ←@coe_div (nnreal.of_real p) _ (by simp [hpq.pos]), ←@coe_div (nnreal.of_real q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end variables (f g : ΞΉ β†’ ennreal) {p q : ℝ} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ennreal`-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) : (βˆ‘ i in s, f i * g i) ≀ (βˆ‘ i in s, (f i)^p) ^ (1/p) * (βˆ‘ i in s, (g i)^q) ^ (1/q) := begin by_cases H : (βˆ‘ i in s, (f i)^p) ^ (1/p) = 0 ∨ (βˆ‘ i in s, (g i)^q) ^ (1/q) = 0, { replace H : (βˆ€ i ∈ s, f i = 0) ∨ (βˆ€ i ∈ s, g i = 0), by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos, sum_eq_zero_iff_of_nonneg] using H, have : βˆ€ i ∈ s, f i * g i = 0 := Ξ» i hi, by cases H; simp [H i hi], have : (βˆ‘ i in s, f i * g i) = (βˆ‘ i in s, 0) := sum_congr rfl this, simp [this] }, push_neg at H, by_cases H' : (βˆ‘ i in s, (f i)^p) ^ (1/p) = ⊀ ∨ (βˆ‘ i in s, (g i)^q) ^ (1/q) = ⊀, { cases H'; simp [H', -one_div, H] }, replace H' : (βˆ€ i ∈ s, f i β‰  ⊀) ∧ (βˆ€ i ∈ s, g i β‰  ⊀), by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (Ξ» i, ennreal.to_nnreal (f i)) (Ξ» i, ennreal.to_nnreal (g i)) _ _ hpq), simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos), le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this, convert this using 1; [skip, congr' 2]; [skip, skip, simp, skip, simp]; { apply finset.sum_congr rfl (Ξ» i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul, with_top.coe_mul.symm] }, end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ennreal` valued nonnegative functions. -/ theorem Lp_add_le (hp : 1 ≀ p) : (βˆ‘ i in s, (f i + g i) ^ p)^(1/p) ≀ (βˆ‘ i in s, (f i)^p) ^ (1/p) + (βˆ‘ i in s, (g i)^p) ^ (1/p) := begin by_cases H' : (βˆ‘ i in s, (f i)^p) ^ (1/p) = ⊀ ∨ (βˆ‘ i in s, (g i)^p) ^ (1/p) = ⊀, { cases H'; simp [H', -one_div] }, have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, replace H' : (βˆ€ i ∈ s, f i β‰  ⊀) ∧ (βˆ€ i ∈ s, g i β‰  ⊀), by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (Ξ» i, ennreal.to_nnreal (f i)) (Ξ» i, ennreal.to_nnreal (g i)) _ hp), push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this, convert this using 2; [skip, congr' 1, congr' 1]; { apply finset.sum_congr rfl (Ξ» i hi, _), simp [H'.1 i hi, H'.2 i hi] } end end ennreal section lintegral /-! ### HΓΆlder's inequality for the Lebesgue integral of ennreal and nnreal functions We prove `∫ (f * g) βˆ‚ΞΌ ≀ (∫ f^p βˆ‚ΞΌ) ^ (1/p) * (∫ g^q βˆ‚ΞΌ) ^ (1/q)` for `p`, `q` conjugate real exponents and `Ξ±β†’(e)nnreal` functions in several cases, the first two being useful only to prove the more general results: * `ennreal.lintegral_mul_le_one_of_lintegral_rpow_eq_one` : ennreal functions for which the integrals on the right are equal to 1, * `ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top` : ennreal functions for which the integrals on the right are neither ⊀ nor 0, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ennreal functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : nnreal functions. -/ open measure_theory variables {Ξ± : Type*} [measurable_space Ξ±] {ΞΌ : measure Ξ±} namespace ennreal lemma lintegral_mul_le_one_of_lintegral_rpow_eq_one {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hg : measurable g) (hf_norm : ∫⁻ a, (f a)^p βˆ‚ΞΌ = 1) (hg_norm : ∫⁻ a, (g a)^q βˆ‚ΞΌ = 1) : ∫⁻ a, (f * g) a βˆ‚ΞΌ ≀ 1 := begin calc ∫⁻ (a : Ξ±), ((f * g) a) βˆ‚ΞΌ ≀ ∫⁻ (a : Ξ±), ((f a)^p / ennreal.of_real p + (g a)^q / ennreal.of_real q) βˆ‚ΞΌ : lintegral_mono (Ξ» a, young_inequality (f a) (g a) hpq) ... = 1 : begin simp_rw [div_def], rw lintegral_add, { rw [lintegral_mul_const _ hf.ennreal_rpow_const, lintegral_mul_const _ hg.ennreal_rpow_const, hf_norm, hg_norm, ←ennreal.div_def, ←ennreal.div_def, hpq.inv_add_inv_conj_ennreal], }, { exact hf.ennreal_rpow_const.ennreal_mul measurable_const, }, { exact hg.ennreal_rpow_const.ennreal_mul measurable_const, }, end end /-- Function multiplied by the inverse of its p-seminorm `(∫⁻ f^p βˆ‚ΞΌ) ^ 1/p`-/ def fun_mul_inv_snorm (f : Ξ± β†’ ennreal) (p : ℝ) (ΞΌ : measure Ξ±) : Ξ± β†’ ennreal := Ξ» a, (f a) * ((∫⁻ c, (f c) ^ p βˆ‚ΞΌ) ^ (1 / p))⁻¹ lemma fun_eq_fun_mul_inv_snorm_mul_snorm {p : ℝ} (f : Ξ± β†’ ennreal) (hf_nonzero : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ β‰  0) (hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ β‰  ⊀) {a : Ξ±} : f a = (fun_mul_inv_snorm f p ΞΌ a) * (∫⁻ c, (f c)^p βˆ‚ΞΌ)^(1/p) := by simp [fun_mul_inv_snorm, mul_assoc, inv_mul_cancel, hf_nonzero, hf_top] lemma fun_mul_inv_snorm_rpow {p : ℝ} (hp0 : 0 < p) {f : Ξ± β†’ ennreal} {a : Ξ±} : (fun_mul_inv_snorm f p ΞΌ a) ^ p = (f a)^p * (∫⁻ c, (f c) ^ p βˆ‚ΞΌ)⁻¹ := begin rw [fun_mul_inv_snorm, mul_rpow_of_nonneg _ _ (le_of_lt hp0)], suffices h_inv_rpow : ((∫⁻ (c : Ξ±), f c ^ p βˆ‚ΞΌ) ^ (1 / p))⁻¹ ^ p = (∫⁻ (c : Ξ±), f c ^ p βˆ‚ΞΌ)⁻¹, by rw h_inv_rpow, rw [inv_rpow_of_pos hp0, ←rpow_mul, div_eq_mul_inv, one_mul, _root_.inv_mul_cancel (ne_of_lt hp0).symm, rpow_one], end lemma lintegral_rpow_fun_mul_inv_snorm_eq_one {p : ℝ} (hp0_lt : 0 < p) {f : Ξ± β†’ ennreal} (hf : measurable f) (hf_nonzero : ∫⁻ a, (f a)^p βˆ‚ΞΌ β‰  0) (hf_top : ∫⁻ a, (f a)^p βˆ‚ΞΌ β‰  ⊀) : ∫⁻ c, (fun_mul_inv_snorm f p ΞΌ c)^p βˆ‚ΞΌ = 1 := begin simp_rw fun_mul_inv_snorm_rpow hp0_lt, rw [lintegral_mul_const _ hf.ennreal_rpow_const, mul_inv_cancel hf_nonzero hf_top], end /-- HΓΆlder's inequality in case of finite non-zero integrals -/ lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hg : measurable g) (hf_nontop : ∫⁻ a, (f a)^p βˆ‚ΞΌ β‰  ⊀) (hg_nontop : ∫⁻ a, (g a)^q βˆ‚ΞΌ β‰  ⊀) (hf_nonzero : ∫⁻ a, (f a)^p βˆ‚ΞΌ β‰  0) (hg_nonzero : ∫⁻ a, (g a)^q βˆ‚ΞΌ β‰  0) : ∫⁻ a, (f * g) a βˆ‚ΞΌ ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ)^(1/p) * (∫⁻ a, (g a)^q βˆ‚ΞΌ)^(1/q) := begin let npf := (∫⁻ (c : Ξ±), (f c) ^ p βˆ‚ΞΌ) ^ (1/p), let nqg := (∫⁻ (c : Ξ±), (g c) ^ q βˆ‚ΞΌ) ^ (1/q), calc ∫⁻ (a : Ξ±), (f * g) a βˆ‚ΞΌ = ∫⁻ (a : Ξ±), ((fun_mul_inv_snorm f p ΞΌ * fun_mul_inv_snorm g q ΞΌ) a) * (npf * nqg) βˆ‚ΞΌ : begin refine lintegral_congr (Ξ» a, _), rw [pi.mul_apply, fun_eq_fun_mul_inv_snorm_mul_snorm f hf_nonzero hf_nontop, fun_eq_fun_mul_inv_snorm_mul_snorm g hg_nonzero hg_nontop, pi.mul_apply], ring, end ... ≀ npf * nqg : begin rw lintegral_mul_const' (npf * nqg) _ (by simp [hf_nontop, hg_nontop, hf_nonzero, hg_nonzero]), nth_rewrite 1 ←one_mul (npf * nqg), refine mul_le_mul _ (le_refl (npf * nqg)), have hf1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.pos hf hf_nonzero hf_nontop, have hg1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.symm.pos hg hg_nonzero hg_nontop, exact lintegral_mul_le_one_of_lintegral_rpow_eq_one hpq (hf.ennreal_mul measurable_const) (hg.ennreal_mul measurable_const) hf1 hg1, end end lemma ae_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f : Ξ± β†’ ennreal} (hf : measurable f) (hf_zero : ∫⁻ a, (f a)^p βˆ‚ΞΌ = 0) : f =ᡐ[ΞΌ] 0 := begin rw lintegral_eq_zero_iff hf.ennreal_rpow_const at hf_zero, refine filter.eventually.mp hf_zero (filter.eventually_of_forall (Ξ» x, _)), dsimp only, rw [pi.zero_apply, rpow_eq_zero_iff], intro hx, cases hx, { exact hx.left, }, { exfalso, linarith, }, end lemma lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hf_zero : ∫⁻ a, (f a)^p βˆ‚ΞΌ = 0) : ∫⁻ a, (f * g) a βˆ‚ΞΌ = 0 := begin rw ←@lintegral_zero_fun Ξ± _ ΞΌ, refine lintegral_congr_ae _, suffices h_mul_zero : f * g =ᡐ[ΞΌ] 0 * g , by rwa zero_mul at h_mul_zero, have hf_eq_zero : f =ᡐ[ΞΌ] 0, from ae_eq_zero_of_lintegral_rpow_eq_zero hp0_lt hf hf_zero, exact filter.eventually_eq.mul hf_eq_zero (ae_eq_refl g), end lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top {p q : ℝ} (hp0_lt : 0 < p) (hq0 : 0 ≀ q) {f g : Ξ± β†’ ennreal} (hf_top : ∫⁻ a, (f a)^p βˆ‚ΞΌ = ⊀) (hg_nonzero : ∫⁻ a, (g a)^q βˆ‚ΞΌ β‰  0) : ∫⁻ a, (f * g) a βˆ‚ΞΌ ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) * (∫⁻ a, (g a)^q βˆ‚ΞΌ) ^ (1/q) := begin refine le_trans le_top (le_of_eq _), have hp0_inv_lt : 0 < 1/p, by simp [hp0_lt], rw [hf_top, ennreal.top_rpow_of_pos hp0_inv_lt], simp [hq0, hg_nonzero], end /-- HΓΆlder's inequality for functions `Ξ± β†’ ennreal`. The integral of the product of two functions is bounded by the product of their `β„’p` and `β„’q` seminorms when `p` and `q` are conjugate exponents. -/ theorem lintegral_mul_le_Lp_mul_Lq (ΞΌ : measure Ξ±) {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hg : measurable g) : ∫⁻ a, (f * g) a βˆ‚ΞΌ ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) * (∫⁻ a, (g a)^q βˆ‚ΞΌ) ^ (1/q) := begin by_cases hf_zero : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ = 0, { refine le_trans (le_of_eq _) (zero_le _), exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.pos hf hf_zero, }, by_cases hg_zero : ∫⁻ a, (g a) ^ q βˆ‚ΞΌ = 0, { refine le_trans (le_of_eq _) (zero_le _), rw mul_comm, exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.symm.pos hg hg_zero, }, by_cases hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ = ⊀, { exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.pos hpq.symm.nonneg hf_top hg_zero, }, by_cases hg_top : ∫⁻ a, (g a) ^ q βˆ‚ΞΌ = ⊀, { rw [mul_comm, mul_comm ((∫⁻ (a : Ξ±), (f a) ^ p βˆ‚ΞΌ) ^ (1 / p))], exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.symm.pos hpq.nonneg hg_top hf_zero, }, -- non-⊀ non-zero case exact ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top hpq hf hg hf_top hg_top hf_zero hg_zero, end lemma lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top {p : ℝ} {f g : Ξ± β†’ ennreal} (hf : measurable f) (hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ < ⊀) (hg : measurable g) (hg_top : ∫⁻ a, (g a) ^ p βˆ‚ΞΌ < ⊀) (hp1 : 1 ≀ p) : ∫⁻ a, ((f + g) a) ^ p βˆ‚ΞΌ < ⊀ := begin have hp0_lt : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, have hp0 : 0 ≀ p, from le_of_lt hp0_lt, calc ∫⁻ (a : Ξ±), (f a + g a) ^ p βˆ‚ΞΌ ≀ ∫⁻ a, ((2:ennreal)^(p-1) * (f a) ^ p + (2:ennreal)^(p-1) * (g a) ^ p) βˆ‚ ΞΌ : begin refine lintegral_mono (Ξ» a, _), dsimp only, have h_zero_lt_half_rpow : (0 : ennreal) < (1 / 2) ^ p, { rw [←ennreal.zero_rpow_of_pos hp0_lt], exact ennreal.rpow_lt_rpow (by simp [zero_lt_one]) hp0_lt, }, have h_rw : (1 / 2) ^ p * (2:ennreal) ^ (p - 1) = 1 / 2, { rw [sub_eq_add_neg, ennreal.rpow_add _ _ ennreal.two_ne_zero ennreal.coe_ne_top, ←mul_assoc, ←ennreal.mul_rpow_of_nonneg _ _ hp0, ennreal.div_def, one_mul, ennreal.inv_mul_cancel ennreal.two_ne_zero ennreal.coe_ne_top, ennreal.one_rpow, one_mul, ennreal.rpow_neg_one], }, rw ←ennreal.mul_le_mul_left (ne_of_lt h_zero_lt_half_rpow).symm _, { rw [mul_add, ← mul_assoc, ← mul_assoc, h_rw, ←ennreal.mul_rpow_of_nonneg _ _ hp0, mul_add], refine ennreal.rpow_arith_mean_le_arith_mean2_rpow (1/2 : ennreal) (1/2 : ennreal) (f a) (g a) _ hp1, rw [ennreal.div_add_div_same, one_add_one_eq_two, ennreal.div_self ennreal.two_ne_zero ennreal.coe_ne_top], }, { rw ←ennreal.lt_top_iff_ne_top, refine ennreal.rpow_lt_top_of_nonneg hp0 _, rw [ennreal.div_def, one_mul, ennreal.inv_ne_top], exact ennreal.two_ne_zero, }, end ... < ⊀ : begin rw [lintegral_add, lintegral_const_mul _ hf.ennreal_rpow_const, lintegral_const_mul _ hg.ennreal_rpow_const, ennreal.add_lt_top], { have h_two : (2 : ennreal) ^ (p - 1) < ⊀, from ennreal.rpow_lt_top_of_nonneg (by simp [hp1]) ennreal.coe_ne_top, repeat {rw ennreal.mul_lt_top_iff}, simp [hf_top, hg_top, h_two], }, { exact (ennreal.continuous_const_mul (by simp)).measurable.comp hf.ennreal_rpow_const, }, { exact (ennreal.continuous_const_mul (by simp)).measurable.comp hg.ennreal_rpow_const }, end end lemma lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hg : measurable g) (hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ β‰  ⊀) : ∫⁻ a, (f a) * (g a) ^ (p - 1) βˆ‚ΞΌ ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) * (∫⁻ a, (g a)^p βˆ‚ΞΌ) ^ (1/q) := begin refine le_trans (ennreal.lintegral_mul_le_Lp_mul_Lq ΞΌ hpq hf hg.ennreal_rpow_const) _, by_cases hf_zero_rpow : (∫⁻ (a : Ξ±), (f a) ^ p βˆ‚ΞΌ) ^ (1 / p) = 0, { rw [hf_zero_rpow, zero_mul], exact zero_le _, }, have hf_top_rpow : (∫⁻ (a : Ξ±), (f a) ^ p βˆ‚ΞΌ) ^ (1 / p) β‰  ⊀, { by_contra h, push_neg at h, refine hf_top _, have hp_not_neg : Β¬ p < 0, by simp [hpq.nonneg], simpa [hpq.pos, hp_not_neg] using h, }, refine (ennreal.mul_le_mul_left hf_zero_rpow hf_top_rpow).mpr (le_of_eq _), congr, ext1 a, rw [←ennreal.rpow_mul, hpq.sub_one_mul_conj], end lemma lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ β‰  ⊀) (hg : measurable g) (hg_top : ∫⁻ a, (g a) ^ p βˆ‚ΞΌ β‰  ⊀) : ∫⁻ a, ((f + g) a)^p βˆ‚ ΞΌ ≀ ((∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ a, (g a)^p βˆ‚ΞΌ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p βˆ‚ΞΌ) ^ (1/q) := begin calc ∫⁻ a, ((f+g) a) ^ p βˆ‚ΞΌ ≀ ∫⁻ a, ((f + g) a) * ((f + g) a) ^ (p - 1) βˆ‚ΞΌ : begin refine lintegral_mono (Ξ» a, _), dsimp only, by_cases h_zero : (f + g) a = 0, { rw [h_zero, ennreal.zero_rpow_of_pos hpq.pos], exact zero_le _, }, by_cases h_top : (f + g) a = ⊀, { rw [h_top, ennreal.top_rpow_of_pos hpq.sub_one_pos, ennreal.top_mul_top], exact le_top, }, refine le_of_eq _, nth_rewrite 1 ←ennreal.rpow_one ((f + g) a), rw [←ennreal.rpow_add _ _ h_zero h_top, add_sub_cancel'_right], end ... = ∫⁻ (a : Ξ±), f a * (f + g) a ^ (p - 1) βˆ‚ΞΌ + ∫⁻ (a : Ξ±), g a * (f + g) a ^ (p - 1) βˆ‚ΞΌ : begin have h_add_m : measurable (Ξ» (a : Ξ±), ((f + g) a) ^ (p-1)), from (hf.add hg).ennreal_rpow_const, have h_add_apply : ∫⁻ (a : Ξ±), (f + g) a * (f + g) a ^ (p - 1) βˆ‚ΞΌ = ∫⁻ (a : Ξ±), (f a + g a) * (f + g) a ^ (p - 1) βˆ‚ΞΌ, from rfl, simp_rw [h_add_apply, add_mul], rw lintegral_add (hf.ennreal_mul h_add_m) (hg.ennreal_mul h_add_m), end ... ≀ ((∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ a, (g a)^p βˆ‚ΞΌ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p βˆ‚ΞΌ) ^ (1/q) : begin rw add_mul, exact add_le_add (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hf (hf.add hg) hf_top) (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hg (hf.add hg) hg_top), end end private lemma lintegral_Lp_add_le_aux {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ennreal} (hf : measurable f) (hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ β‰  ⊀) (hg : measurable g) (hg_top : ∫⁻ a, (g a) ^ p βˆ‚ΞΌ β‰  ⊀) (h_add_zero : ∫⁻ a, ((f+g) a) ^ p βˆ‚ ΞΌ β‰  0) (h_add_top : ∫⁻ a, ((f+g) a) ^ p βˆ‚ ΞΌ β‰  ⊀) : (∫⁻ a, ((f + g) a)^p βˆ‚ ΞΌ) ^ (1/p) ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ a, (g a)^p βˆ‚ΞΌ) ^ (1/p) := begin have hp_not_nonpos : Β¬ p ≀ 0, by simp [hpq.pos], have htop_rpow : (∫⁻ a, ((f+g) a) ^ p βˆ‚ΞΌ)^(1/p) β‰  ⊀, { by_contra h, push_neg at h, exact h_add_top (@ennreal.rpow_eq_top_of_nonneg _ (1/p) (by simp [hpq.nonneg]) h), }, have h0_rpow : (∫⁻ a, ((f+g) a) ^ p βˆ‚ ΞΌ) ^ (1/p) β‰  0, by simp [h_add_zero, h_add_top, hpq.nonneg, hp_not_nonpos, -pi.add_apply], suffices h : 1 ≀ (∫⁻ (a : Ξ±), ((f+g) a)^p βˆ‚ΞΌ) ^ -(1/p) * ((∫⁻ (a : Ξ±), (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ (a : Ξ±), (g a)^p βˆ‚ΞΌ) ^ (1/p)), by rwa [←mul_le_mul_left h0_rpow htop_rpow, ←mul_assoc, ←rpow_add _ _ h_add_zero h_add_top, ←sub_eq_add_neg, _root_.sub_self, rpow_zero, one_mul, mul_one] at h, have h : ∫⁻ (a : Ξ±), ((f+g) a)^p βˆ‚ΞΌ ≀ ((∫⁻ (a : Ξ±), (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ (a : Ξ±), (g a)^p βˆ‚ΞΌ) ^ (1/p)) * (∫⁻ (a : Ξ±), ((f+g) a)^p βˆ‚ΞΌ) ^ (1/q), from lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add hpq hf hf_top hg hg_top, have h_one_div_q : 1/q = 1 - 1/p, by { nth_rewrite 1 ←hpq.inv_add_inv_conj, ring, }, simp_rw [h_one_div_q, sub_eq_add_neg 1 (1/p), ennreal.rpow_add _ _ h_add_zero h_add_top, rpow_one] at h, nth_rewrite 1 mul_comm at h, nth_rewrite 0 ←one_mul (∫⁻ (a : Ξ±), ((f+g) a) ^ p βˆ‚ΞΌ) at h, rwa [←mul_assoc, ennreal.mul_le_mul_right h_add_zero h_add_top, mul_comm] at h, end /-- Minkowski's inequality for functions `Ξ± β†’ ennreal`: the `β„’p` seminorm of the sum of two functions is bounded by the sum of their `β„’p` seminorms. -/ theorem lintegral_Lp_add_le {p : ℝ} {f g : Ξ± β†’ ennreal} (hf : measurable f) (hg : measurable g) (hp1 : 1 ≀ p) : (∫⁻ a, ((f + g) a)^p βˆ‚ ΞΌ) ^ (1/p) ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ) ^ (1/p) + (∫⁻ a, (g a)^p βˆ‚ΞΌ) ^ (1/p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, by_cases hf_top : ∫⁻ a, (f a) ^ p βˆ‚ΞΌ = ⊀, { simp [hf_top, hp_pos], }, by_cases hg_top : ∫⁻ a, (g a) ^ p βˆ‚ΞΌ = ⊀, { simp [hg_top, hp_pos], }, by_cases h1 : p = 1, { refine le_of_eq _, simp_rw [h1, one_div_one, ennreal.rpow_one], exact lintegral_add hf hg, }, have hp1_lt : 1 < p, by { refine lt_of_le_of_ne hp1 _, symmetry, exact h1, }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp1_lt, by_cases h0 : ∫⁻ a, ((f+g) a) ^ p βˆ‚ ΞΌ = 0, { rw [h0, @ennreal.zero_rpow_of_pos (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1])], exact zero_le _, }, have htop : ∫⁻ a, ((f+g) a) ^ p βˆ‚ ΞΌ β‰  ⊀, { rw ←ne.def at hf_top hg_top, rw ←ennreal.lt_top_iff_ne_top at hf_top hg_top ⊒, exact lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top hf hf_top hg hg_top hp1, }, exact lintegral_Lp_add_le_aux hpq hf hf_top hg hg_top h0 htop, end end ennreal /-- HΓΆlder's inequality for functions `Ξ± β†’ ℝβ‰₯0`. The integral of the product of two functions is bounded by the product of their `β„’p` and `β„’q` seminorms when `p` and `q` are conjugate exponents. -/ theorem nnreal.lintegral_mul_le_Lp_mul_Lq {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : Ξ± β†’ ℝβ‰₯0} (hf : measurable f) (hg : measurable g) : ∫⁻ a, (f * g) a βˆ‚ΞΌ ≀ (∫⁻ a, (f a)^p βˆ‚ΞΌ)^(1/p) * (∫⁻ a, (g a)^q βˆ‚ΞΌ)^(1/q) := begin simp_rw [pi.mul_apply, ennreal.coe_mul], exact ennreal.lintegral_mul_le_Lp_mul_Lq ΞΌ hpq hf.ennreal_coe hg.ennreal_coe, end end lintegral
7df24fc41e858568046835f7a47511a8e8880852
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/find.lean
057dd4b3da4a4829b253787d4015ea144f4f36f9
[ "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
48
lean
import find. find "^.ongr" find "foo" find "(ab"
f6b94d8dc9116dc4c24b8124f4d49971625e5d6c
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/linear_algebra/prod.lean
d833f33a69a4fb619ee163366505e04927b78c5a
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,510
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, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic import order.partial_sups /-! ### Products of modules This file defines constructors for linear maps whose domains or codomains are products. It contains theorems relating these to each other, as well as to `submodule.prod`, `submodule.map`, `submodule.comap`, `linear_map.range`, and `linear_map.ker`. ## Main definitions - products in the domain: - `linear_map.fst` - `linear_map.snd` - `linear_map.coprod` - `linear_map.prod_ext` - products in the codomain: - `linear_map.inl` - `linear_map.inr` - `linear_map.prod` - products in both domain and codomain: - `linear_map.prod_map` - `linear_equiv.prod_map` - `linear_equiv.skew_prod` -/ universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {Mβ‚‚ : Type w} {Vβ‚‚ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {Mβ‚„ : Type z} {ΞΉ : Type x} section prod namespace linear_map variables (S : Type*) [semiring R] [semiring S] variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] variables [module R M] [module R Mβ‚‚] [module R M₃] [module R Mβ‚„] variables (f : M β†’β‚—[R] Mβ‚‚) section variables (R M Mβ‚‚) /-- The first projection of a product is a linear map. -/ def fst : M Γ— Mβ‚‚ β†’β‚—[R] M := { to_fun := prod.fst, map_add' := Ξ» x y, rfl, map_smul' := Ξ» x y, rfl } /-- The second projection of a product is a linear map. -/ def snd : M Γ— Mβ‚‚ β†’β‚—[R] Mβ‚‚ := { to_fun := prod.snd, map_add' := Ξ» x y, rfl, map_smul' := Ξ» x y, rfl } end @[simp] theorem fst_apply (x : M Γ— Mβ‚‚) : fst R M Mβ‚‚ x = x.1 := rfl @[simp] theorem snd_apply (x : M Γ— Mβ‚‚) : snd R M Mβ‚‚ x = x.2 := rfl theorem fst_surjective : function.surjective (fst R M Mβ‚‚) := Ξ» x, ⟨(x, 0), rfl⟩ theorem snd_surjective : function.surjective (snd R M Mβ‚‚) := Ξ» x, ⟨(0, x), rfl⟩ /-- The prod of two linear maps is a linear map. -/ @[simps] def prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : (M β†’β‚—[R] Mβ‚‚ Γ— M₃) := { to_fun := Ξ» x, (f x, g x), map_add' := Ξ» x y, by simp only [prod.mk_add_mk, map_add], map_smul' := Ξ» c x, by simp only [prod.smul_mk, map_smul, ring_hom.id_apply] } @[simp] theorem fst_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : (fst R Mβ‚‚ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : (snd R Mβ‚‚ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M Mβ‚‚) (snd R M Mβ‚‚) = linear_map.id := by ext; refl /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def prod_equiv [module S Mβ‚‚] [module S M₃] [smul_comm_class R S Mβ‚‚] [smul_comm_class R S M₃] : ((M β†’β‚—[R] Mβ‚‚) Γ— (M β†’β‚—[R] M₃)) ≃ₗ[S] (M β†’β‚—[R] Mβ‚‚ Γ— M₃) := { to_fun := Ξ» f, f.1.prod f.2, inv_fun := Ξ» f, ((fst _ _ _).comp f, (snd _ _ _).comp f), left_inv := Ξ» f, by ext; refl, right_inv := Ξ» f, by ext; refl, map_add' := Ξ» a b, rfl, map_smul' := Ξ» r a, rfl } section variables (R M Mβ‚‚) /-- The left injection into a product is a linear map. -/ def inl : M β†’β‚—[R] M Γ— Mβ‚‚ := prod linear_map.id 0 /-- The right injection into a product is a linear map. -/ def inr : Mβ‚‚ β†’β‚—[R] M Γ— Mβ‚‚ := prod 0 linear_map.id theorem range_inl : range (inl R M Mβ‚‚) = ker (snd R M Mβ‚‚) := begin ext x, simp only [mem_ker, mem_range], split, { rintros ⟨y, rfl⟩, refl }, { intro h, exact ⟨x.fst, prod.ext rfl h.symm⟩ } end theorem ker_snd : ker (snd R M Mβ‚‚) = range (inl R M Mβ‚‚) := eq.symm $ range_inl R M Mβ‚‚ theorem range_inr : range (inr R M Mβ‚‚) = ker (fst R M Mβ‚‚) := begin ext x, simp only [mem_ker, mem_range], split, { rintros ⟨y, rfl⟩, refl }, { intro h, exact ⟨x.snd, prod.ext h.symm rfl⟩ } end theorem ker_fst : ker (fst R M Mβ‚‚) = range (inr R M Mβ‚‚) := eq.symm $ range_inr R M Mβ‚‚ end @[simp] theorem coe_inl : (inl R M Mβ‚‚ : M β†’ M Γ— Mβ‚‚) = Ξ» x, (x, 0) := rfl theorem inl_apply (x : M) : inl R M Mβ‚‚ x = (x, 0) := rfl @[simp] theorem coe_inr : (inr R M Mβ‚‚ : Mβ‚‚ β†’ M Γ— Mβ‚‚) = prod.mk 0 := rfl theorem inr_apply (x : Mβ‚‚) : inr R M Mβ‚‚ x = (0, x) := rfl theorem inl_eq_prod : inl R M Mβ‚‚ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M Mβ‚‚ = prod 0 linear_map.id := rfl theorem inl_injective : function.injective (inl R M Mβ‚‚) := Ξ» _, by simp theorem inr_injective : function.injective (inr R M Mβ‚‚) := Ξ» _, by simp /-- The coprod function `Ξ» x : M Γ— Mβ‚‚, f.1 x.1 + f.2 x.2` is a linear map. -/ def coprod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : M Γ— Mβ‚‚ β†’β‚—[R] M₃ := f.comp (fst _ _ _) + g.comp (snd _ _ _) @[simp] theorem coprod_apply (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (x : M Γ— Mβ‚‚) : coprod f g x = f x.1 + g x.2 := rfl @[simp] theorem coprod_inl (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (coprod f g).comp (inl R M Mβ‚‚) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (coprod f g).comp (inr R M Mβ‚‚) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M Mβ‚‚) (inr R M Mβ‚‚) = linear_map.id := by ext; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem comp_coprod (f : M₃ β†’β‚—[R] Mβ‚„) (g₁ : M β†’β‚—[R] M₃) (gβ‚‚ : Mβ‚‚ β†’β‚—[R] M₃) : f.comp (g₁.coprod gβ‚‚) = (f.comp g₁).coprod (f.comp gβ‚‚) := ext $ Ξ» x, f.map_add (g₁ x.1) (gβ‚‚ x.2) theorem fst_eq_coprod : fst R M Mβ‚‚ = coprod linear_map.id 0 := by ext; simp theorem snd_eq_coprod : snd R M Mβ‚‚ = coprod 0 linear_map.id := by ext; simp @[simp] theorem coprod_comp_prod (f : Mβ‚‚ β†’β‚—[R] Mβ‚„) (g : M₃ β†’β‚—[R] Mβ‚„) (f' : M β†’β‚—[R] Mβ‚‚) (g' : M β†’β‚—[R] M₃) : (f.coprod g).comp (f'.prod g') = f.comp f' + g.comp g' := rfl @[simp] lemma coprod_map_prod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (S : submodule R M) (S' : submodule R Mβ‚‚) : (submodule.prod S S').map (linear_map.coprod f g) = S.map f βŠ” S'.map g := set_like.coe_injective $ begin simp only [linear_map.coprod_apply, submodule.coe_sup, submodule.map_coe], rw [←set.image2_add, set.image2_image_left, set.image2_image_right], exact set.image_prod (Ξ» m mβ‚‚, f m + g mβ‚‚), end /-- Taking the product of two maps with the same codomain is equivalent to taking the product of their domains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def coprod_equiv [module S M₃] [smul_comm_class R S M₃] : ((M β†’β‚—[R] M₃) Γ— (Mβ‚‚ β†’β‚—[R] M₃)) ≃ₗ[S] (M Γ— Mβ‚‚ β†’β‚—[R] M₃) := { to_fun := Ξ» f, f.1.coprod f.2, inv_fun := Ξ» f, (f.comp (inl _ _ _), f.comp (inr _ _ _)), left_inv := Ξ» f, by simp only [prod.mk.eta, coprod_inl, coprod_inr], right_inv := Ξ» f, by simp only [←comp_coprod, comp_id, coprod_inl_inr], map_add' := Ξ» a b, by { ext, simp only [prod.snd_add, add_apply, coprod_apply, prod.fst_add], ac_refl }, map_smul' := Ξ» r a, by { dsimp, ext, simp only [smul_add, smul_apply, prod.smul_snd, prod.smul_fst, coprod_apply] } } theorem prod_ext_iff {f g : M Γ— Mβ‚‚ β†’β‚—[R] M₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := (coprod_equiv β„•).symm.injective.eq_iff.symm.trans prod.ext_iff /-- Split equality of linear maps from a product into linear maps over each component, to allow `ext` to apply lemmas specific to `M β†’β‚— M₃` and `Mβ‚‚ β†’β‚— M₃`. See note [partially-applied ext lemmas]. -/ @[ext] theorem prod_ext {f g : M Γ— Mβ‚‚ β†’β‚—[R] M₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ /-- `prod.map` of two linear maps. -/ def prod_map (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] Mβ‚„) : (M Γ— Mβ‚‚) β†’β‚—[R] (M₃ Γ— Mβ‚„) := (f.comp (fst R M Mβ‚‚)).prod (g.comp (snd R M Mβ‚‚)) @[simp] theorem prod_map_apply (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] Mβ‚„) (x) : f.prod_map g x = (f x.1, g x.2) := rfl lemma prod_map_comap_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M₃ β†’β‚—[R] Mβ‚„) (S : submodule R Mβ‚‚) (S' : submodule R Mβ‚„) : (submodule.prod S S').comap (linear_map.prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ lemma ker_prod_map (f : M β†’β‚—[R] Mβ‚‚) (g : M₃ β†’β‚—[R] Mβ‚„) : (linear_map.prod_map f g).ker = submodule.prod f.ker g.ker := begin dsimp only [ker], rw [←prod_map_comap_prod, submodule.prod_bot], end section map_mul variables {A : Type*} [non_unital_non_assoc_semiring A] [module R A] variables {B : Type*} [non_unital_non_assoc_semiring B] [module R B] lemma inl_map_mul (a₁ aβ‚‚ : A) : linear_map.inl R A B (a₁ * aβ‚‚) = linear_map.inl R A B a₁ * linear_map.inl R A B aβ‚‚ := prod.ext rfl (by simp) lemma inr_map_mul (b₁ bβ‚‚ : B) : linear_map.inr R A B (b₁ * bβ‚‚) = linear_map.inr R A B b₁ * linear_map.inr R A B bβ‚‚ := prod.ext (by simp) rfl end map_mul end linear_map end prod namespace linear_map open submodule variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] [module R M] [module R Mβ‚‚] [module R M₃] [module R Mβ‚„] lemma range_coprod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (f.coprod g).range = f.range βŠ” g.range := submodule.ext $ Ξ» x, by simp [mem_sup] lemma is_compl_range_inl_inr : is_compl (inl R M Mβ‚‚).range (inr R M Mβ‚‚).range := begin split, { rintros ⟨_, _⟩ ⟨⟨x, hx⟩, ⟨y, hy⟩⟩, simp only [prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊒, exact ⟨hy.1.symm, hx.2.symm⟩ }, { rintros ⟨x, y⟩ -, simp only [mem_sup, mem_range, exists_prop], refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, _⟩, simp } end lemma sup_range_inl_inr : (inl R M Mβ‚‚).range βŠ” (inr R M Mβ‚‚).range = ⊀ := is_compl_range_inl_inr.sup_eq_top lemma disjoint_inl_inr : disjoint (inl R M Mβ‚‚).range (inr R M Mβ‚‚).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm Mβ‚‚ 0] {contextual := tt}; intros; refl theorem map_coprod_prod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (p : submodule R M) (q : submodule R Mβ‚‚) : map (coprod 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 set_like.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_prod_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) (p : submodule R Mβ‚‚) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p βŠ“ comap g q := submodule.ext $ Ξ» x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R Mβ‚‚) : p.prod q = p.comap (linear_map.fst R M Mβ‚‚) βŠ“ q.comap (linear_map.snd R M Mβ‚‚) := submodule.ext $ Ξ» x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R Mβ‚‚) : p.prod q = p.map (linear_map.inl R M Mβ‚‚) βŠ” q.map (linear_map.inr R M Mβ‚‚) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set Mβ‚‚} : span R (inl R M Mβ‚‚ '' s βˆͺ inr R M Mβ‚‚ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image] @[simp] lemma ker_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : ker (prod f g) = ker f βŠ“ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl lemma range_prod_le (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : range (prod f g) ≀ (range f).prod (range g) := begin simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end lemma ker_prod_ker_le_ker_coprod {Mβ‚‚ : Type*} [add_comm_group Mβ‚‚] [module R Mβ‚‚] {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (ker f).prod (ker g) ≀ ker (f.coprod g) := by { rintros ⟨y, z⟩, simp {contextual := tt} } lemma ker_coprod_of_disjoint_range {Mβ‚‚ : Type*} [add_comm_group Mβ‚‚] [module R Mβ‚‚] {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (hd : disjoint f.range g.range) : ker (f.coprod g) = (ker f).prod (ker g) := begin apply le_antisymm _ (ker_prod_ker_le_ker_coprod f g), rintros ⟨y, z⟩ h, simp only [mem_ker, mem_prod, coprod_apply] at h ⊒, have : f y ∈ f.range βŠ“ g.range, { simp only [true_and, mem_range, mem_inf, exists_apply_eq_apply], use -z, rwa [eq_comm, map_neg, ← sub_eq_zero, sub_neg_eq_add] }, rw [hd.eq_bot, mem_bot] at this, rw [this] at h, simpa [this] using h, end end linear_map namespace submodule open linear_map variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] variables [module R M] [module R Mβ‚‚] lemma sup_eq_range (p q : submodule R M) : p βŠ” q = (p.subtype.coprod q.subtype).range := submodule.ext $ Ξ» x, by simp [submodule.mem_sup, set_like.exists] variables (p : submodule R M) (q : submodule R Mβ‚‚) @[simp] theorem map_inl : p.map (inl R M Mβ‚‚) = prod p βŠ₯ := by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] } @[simp] theorem map_inr : q.map (inr R M Mβ‚‚) = prod βŠ₯ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M Mβ‚‚) = prod p ⊀ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M Mβ‚‚) = prod ⊀ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M Mβ‚‚) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M Mβ‚‚) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M Mβ‚‚) = p := by ext x; simp [(⟨0, zero_mem _⟩ : βˆƒ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M Mβ‚‚) = q := by ext x; simp [(⟨0, zero_mem _⟩ : βˆƒ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M Mβ‚‚).ker = βŠ₯ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M Mβ‚‚).ker = βŠ₯ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M Mβ‚‚).range = ⊀ := by rw [range_eq_map, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M Mβ‚‚).range = ⊀ := by rw [range_eq_map, ← prod_top, prod_map_snd] variables (R M Mβ‚‚) /-- `M` as a submodule of `M Γ— N`. -/ def fst : submodule R (M Γ— Mβ‚‚) := (βŠ₯ : submodule R Mβ‚‚).comap (linear_map.snd R M Mβ‚‚) /-- `M` as a submodule of `M Γ— N` is isomorphic to `M`. -/ @[simps] def fst_equiv : submodule.fst R M Mβ‚‚ ≃ₗ[R] M := { to_fun := Ξ» x, x.1.1, inv_fun := Ξ» m, ⟨⟨m, 0⟩, by tidy⟩, map_add' := by simp, map_smul' := by simp, left_inv := by tidy, right_inv := by tidy, } lemma fst_map_fst : (submodule.fst R M Mβ‚‚).map (linear_map.fst R M Mβ‚‚) = ⊀ := by tidy lemma fst_map_snd : (submodule.fst R M Mβ‚‚).map (linear_map.snd R M Mβ‚‚) = βŠ₯ := by { tidy, exact 0, } /-- `N` as a submodule of `M Γ— N`. -/ def snd : submodule R (M Γ— Mβ‚‚) := (βŠ₯ : submodule R M).comap (linear_map.fst R M Mβ‚‚) /-- `N` as a submodule of `M Γ— N` is isomorphic to `N`. -/ @[simps] def snd_equiv : submodule.snd R M Mβ‚‚ ≃ₗ[R] Mβ‚‚ := { to_fun := Ξ» x, x.1.2, inv_fun := Ξ» n, ⟨⟨0, n⟩, by tidy⟩, map_add' := by simp, map_smul' := by simp, left_inv := by tidy, right_inv := by tidy, } lemma snd_map_fst : (submodule.snd R M Mβ‚‚).map (linear_map.fst R M Mβ‚‚) = βŠ₯ := by { tidy, exact 0, } lemma snd_map_snd : (submodule.snd R M Mβ‚‚).map (linear_map.snd R M Mβ‚‚) = ⊀ := by tidy lemma fst_sup_snd : submodule.fst R M Mβ‚‚ βŠ” submodule.snd R M Mβ‚‚ = ⊀ := begin rw eq_top_iff, rintro ⟨m, n⟩ -, rw [show (m, n) = (m, 0) + (0, n), by simp], apply submodule.add_mem (submodule.fst R M Mβ‚‚ βŠ” submodule.snd R M Mβ‚‚), { exact submodule.mem_sup_left (submodule.mem_comap.mpr (by simp)), }, { exact submodule.mem_sup_right (submodule.mem_comap.mpr (by simp)), }, end lemma fst_inf_snd : submodule.fst R M Mβ‚‚ βŠ“ submodule.snd R M Mβ‚‚ = βŠ₯ := by tidy end submodule namespace linear_equiv section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] variables {module_M : module R M} {module_Mβ‚‚ : module R Mβ‚‚} variables {module_M₃ : module R M₃} {module_Mβ‚„ : module R Mβ‚„} variables (e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₃ ≃ₗ[R] Mβ‚„) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M Γ— M₃) ≃ₗ[R] (Mβ‚‚ Γ— Mβ‚„) := { map_add' := Ξ» x y, prod.ext (e₁.map_add _ _) (eβ‚‚.map_add _ _), map_smul' := Ξ» c x, prod.ext (e₁.map_smulβ‚›β‚— c _) (eβ‚‚.map_smulβ‚›β‚— c _), .. equiv.prod_congr e₁.to_equiv eβ‚‚.to_equiv } lemma prod_symm : (e₁.prod eβ‚‚).symm = e₁.symm.prod eβ‚‚.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod eβ‚‚ p = (e₁ p.1, eβ‚‚ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod eβ‚‚ : (M Γ— M₃) β†’β‚—[R] (Mβ‚‚ Γ— Mβ‚„)) = (e₁ : M β†’β‚—[R] Mβ‚‚).prod_map (eβ‚‚ : M₃ β†’β‚—[R] Mβ‚„) := rfl end section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_group Mβ‚„] variables {module_M : module R M} {module_Mβ‚‚ : module R Mβ‚‚} variables {module_M₃ : module R M₃} {module_Mβ‚„ : module R Mβ‚„} variables (e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₃ ≃ₗ[R] Mβ‚„) /-- Equivalence given by a block lower diagonal matrix. `e₁` and `eβ‚‚` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M β†’β‚—[R] Mβ‚„) : (M Γ— M₃) ≃ₗ[R] Mβ‚‚ Γ— Mβ‚„ := { inv_fun := Ξ» p : Mβ‚‚ Γ— Mβ‚„, (e₁.symm p.1, eβ‚‚.symm (p.2 - f (e₁.symm p.1))), left_inv := Ξ» p, by simp, right_inv := Ξ» p, by simp, .. ((e₁ : M β†’β‚—[R] Mβ‚‚).comp (linear_map.fst R M M₃)).prod ((eβ‚‚ : M₃ β†’β‚—[R] Mβ‚„).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M β†’β‚—[R] Mβ‚„) (x) : e₁.skew_prod eβ‚‚ f x = (e₁ x.1, eβ‚‚ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M β†’β‚—[R] Mβ‚„) (x) : (e₁.skew_prod eβ‚‚ f).symm x = (e₁.symm x.1, eβ‚‚.symm (x.2 - f (e₁.symm x.1))) := rfl end end linear_equiv namespace linear_map open submodule variables [ring R] variables [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [module R M] [module R Mβ‚‚] [module R M₃] /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M β†’β‚—[R] Mβ‚‚} {g : M β†’β‚—[R] M₃} (h : ker f βŠ” ker g = ⊀) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f βŠ” ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end linear_map namespace linear_map /-! ## Tunnels and tailings Some preliminary work for establishing the strong rank condition for noetherian rings. Given a morphism `f : M Γ— N β†’β‚—[R] M` which is `i : injective f`, we can find an infinite decreasing `tunnel f i n` of copies of `M` inside `M`, and sitting beside these, an infinite sequence of copies of `N`. We picturesquely name these as `tailing f i n` for each individual copy of `N`, and `tailings f i n` for the supremum of the first `n+1` copies: they are the pieces left behind, sitting inside the tunnel. By construction, each `tailing f i (n+1)` is disjoint from `tailings f i n`; later, when we assume `M` is noetherian, this implies that `N` must be trivial, and establishes the strong rank condition for any left-noetherian ring. -/ section tunnel -- (This doesn't work over a semiring: we need to use that `submodule R M` is a modular lattice, -- which requires cancellation.) variables [ring R] variables {N : Type*} [add_comm_group M] [module R M] [add_comm_group N] [module R N] open function /-- An auxiliary construction for `tunnel`. The composition of `f`, followed by the isomorphism back to `K`, followed by the inclusion of this submodule back into `M`. -/ def tunnel_aux (f : M Γ— N β†’β‚—[R] M) (KΟ† : Ξ£ K : submodule R M, K ≃ₗ[R] M) : M Γ— N β†’β‚—[R] M := (KΟ†.1.subtype.comp KΟ†.2.symm.to_linear_map).comp f lemma tunnel_aux_injective (f : M Γ— N β†’β‚—[R] M) (i : injective f) (KΟ† : Ξ£ K : submodule R M, K ≃ₗ[R] M) : injective (tunnel_aux f KΟ†) := (subtype.val_injective.comp KΟ†.2.symm.injective).comp i noncomputable theory /-- Auxiliary definition for `tunnel`. -/ -- Even though we have `noncomputable theory`, -- we get an error without another `noncomputable` here. noncomputable def tunnel' (f : M Γ— N β†’β‚—[R] M) (i : injective f) : β„• β†’ Ξ£ (K : submodule R M), K ≃ₗ[R] M | 0 := ⟨⊀, linear_equiv.of_top ⊀ rfl⟩ | (n+1) := ⟨(submodule.fst R M N).map (tunnel_aux f (tunnel' n)), ((submodule.fst R M N).equiv_map_of_injective _ (tunnel_aux_injective f i (tunnel' n))).symm.trans (submodule.fst_equiv R M N)⟩ /-- Give an injective map `f : M Γ— N β†’β‚—[R] M` we can find a nested sequence of submodules all isomorphic to `M`. -/ def tunnel (f : M Γ— N β†’β‚—[R] M) (i : injective f) : β„• β†’β‚˜ order_dual (submodule R M) := ⟨λ n, (tunnel' f i n).1, monotone_nat_of_le_succ (Ξ» n, begin dsimp [tunnel', tunnel_aux], rw [submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end)⟩ /-- Give an injective map `f : M Γ— N β†’β‚—[R] M` we can find a sequence of submodules all isomorphic to `N`. -/ def tailing (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : submodule R M := (submodule.snd R M N).map (tunnel_aux f (tunnel' f i n)) /-- Each `tailing f i n` is a copy of `N`. -/ def tailing_linear_equiv (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : tailing f i n ≃ₗ[R] N := ((submodule.snd R M N).equiv_map_of_injective _ (tunnel_aux_injective f i (tunnel' f i n))).symm.trans (submodule.snd_equiv R M N) lemma tailing_le_tunnel (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : tailing f i n ≀ tunnel f i n := begin dsimp [tailing, tunnel_aux], rw [submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end lemma tailing_disjoint_tunnel_succ (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : disjoint (tailing f i n) (tunnel f i (n+1)) := begin rw disjoint_iff, dsimp [tailing, tunnel, tunnel'], rw [submodule.map_inf_eq_map_inf_comap, submodule.comap_map_eq_of_injective (tunnel_aux_injective _ i _), inf_comm, submodule.fst_inf_snd, submodule.map_bot], end lemma tailing_sup_tunnel_succ_le_tunnel (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : tailing f i n βŠ” tunnel f i (n+1) ≀ tunnel f i n := begin dsimp [tailing, tunnel, tunnel', tunnel_aux], rw [←submodule.map_sup, sup_comm, submodule.fst_sup_snd, submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end /-- The supremum of all the copies of `N` found inside the tunnel. -/ def tailings (f : M Γ— N β†’β‚—[R] M) (i : injective f) : β„• β†’ submodule R M := partial_sups (tailing f i) @[simp] lemma tailings_zero (f : M Γ— N β†’β‚—[R] M) (i : injective f) : tailings f i 0 = tailing f i 0 := by simp [tailings] @[simp] lemma tailings_succ (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : tailings f i (n+1) = tailings f i n βŠ” tailing f i (n+1) := by simp [tailings] lemma tailings_disjoint_tunnel (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : disjoint (tailings f i n) (tunnel f i (n+1)) := begin induction n with n ih, { simp only [tailings_zero], apply tailing_disjoint_tunnel_succ, }, { simp only [tailings_succ], refine disjoint.disjoint_sup_left_of_disjoint_sup_right _ _, apply tailing_disjoint_tunnel_succ, apply disjoint.mono_right _ ih, apply tailing_sup_tunnel_succ_le_tunnel, }, end lemma tailings_disjoint_tailing (f : M Γ— N β†’β‚—[R] M) (i : injective f) (n : β„•) : disjoint (tailings f i n) (tailing f i (n+1)) := disjoint.mono_right (tailing_le_tunnel f i _) (tailings_disjoint_tunnel f i _) end tunnel end linear_map
5fcfc4c0bf6e8b3076980259b7f6b43ef5a31e80
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/seclvl.lean
feabe3d4a847af2e8e6cfc7db3079f9f076b39bd
[ "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
317
lean
import logic section variables (A B C : Type) definition foo := A β†’ B check foo A B check foo B C check foo A A end constants A B C : Type check foo A B check foo B C check foo A A section variables (A B C : Type) definition foo2 := A β†’ B check foo2 A B check foo2 B C check foo2 A A end
86ec78b4b0772d7a5da45f10503eb803e2fbbba1
097294e9b80f0d9893ac160b9c7219aa135b51b9
/instructor/identifiers/2020-01-23-notes-001.lean
49ba7d2edeab87121251c972208e118d13dc4fcd
[]
no_license
AbigailCastro17/CS2102-Discrete-Math
cf296251be9418ce90206f5e66bde9163e21abf9
d741e4d2d6a9b2e0c8380e51706218b8f608cee4
refs/heads/main
1,682,891,087,358
1,621,401,341,000
1,621,401,341,000
368,749,959
0
0
null
null
null
null
UTF-8
Lean
false
false
681
lean
def x := 1 + 2 #print x #eval x /- Identifiers, bindings, types -/ def b' : bool := (tt : bool) def b'' := tt -- type inference def s' : string := "I love logic!" def n' : β„• := 1 /- Functions and their application -/ #eval band tt tt #eval tt && tt #eval nat.add 3 4 #eval string.append "Hello, " "Logic!" #eval "Hello, " ++ "Logic!" #check band /- Types -/ /- Every term has exactly one type A type defines a set of terms These sets are disjoint -/ #check 0 #check "Hello" #check tt #check prod.mk 2 "Hi!" #check true -- a proposition #check bool #check string #check β„• #check Type 0 #check Type 1 #check Prop #check Sort 0 #check Sort 1 #check Sort 2
6d576fed00dc5eb6fcbd2a1da86ac01b0cc758de
968e2f50b755d3048175f176376eff7139e9df70
/examples/pred_logic/unnamed_308.lean
694f8674be2ea4f7788f1e3913158c57df0a1f0b
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
107
lean
variables (U : Type*) (P : U β†’ Prop) (u : U) -- BEGIN example (h : βˆ€ x, P x) : P u := by apply h -- END
73f67c9dcf3a3402d62be6702471698c9863bf31
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/sheaves/sheaf.lean
46ce853717d8601fc81f7453b0be2ca4877e7e85
[ "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
5,316
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 topology.sheaves.presheaf import category_theory.sites.sheaf import category_theory.sites.spaces /-! # Sheaves We define sheaves on a topological space, with values in an arbitrary category. A presheaf on a topological space `X` is a sheaf presicely when it is a sheaf under the grothendieck topology on `opens X`, which expands out to say: For each open cover `{ Uα΅’ }` of `U`, and a family of compatible functions `A ⟢ F(Uα΅’)` for an `A : X`, there exists an unique gluing `A ⟢ F(U)` compatible with the restriction. See the docstring of `Top.presheaf.is_sheaf` for an explanation on the design descisions and a list of equivalent conditions. We provide the instance `category (sheaf C X)` as the full subcategory of presheaves, and the fully faithful functor `sheaf.forget : sheaf C X β₯€ presheaf C X`. -/ universes w v u noncomputable theory open category_theory open category_theory.limits open topological_space open opposite open topological_space.opens namespace Top variables {C : Type u} [category.{v} C] variables {X : Top.{w}} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ opens X) namespace presheaf /-- The sheaf condition has several different equivalent formulations. The official definition chosen here is in terms of grothendieck topologies so that the results on sites could be applied here easily, and this condition does not require additional constraints on the value category. The equivalent formulations of the sheaf condition on `presheaf C X` are as follows : 1. `Top.presheaf.is_sheaf`: (the official definition) It is a sheaf with respect to the grothendieck topology on `opens X`, which is to say: For each open cover `{ Uα΅’ }` of `U`, and a family of compatible functions `A ⟢ F(Uα΅’)` for an `A : X`, there exists an unique gluing `A ⟢ F(U)` compatible with the restriction. 2. `Top.presheaf.is_sheaf_equalizer_products`: (requires `C` to have all products) For each open cover `{ Uα΅’ }` of `U`, `F(U) ⟢ ∏ F(Uα΅’)` is the equalizer of the two morphisms `∏ F(Uα΅’) ⟢ ∏ F(Uα΅’ ∩ Uβ±Ό)`. See `Top.presheaf.is_sheaf_iff_is_sheaf_equalizer_products`. 3. `Top.presheaf.is_sheaf_opens_le_cover`: For each open cover `{ Uα΅’ }` of `U`, `F(U)` is the limit of the diagram consisting of arrows `F(V₁) ⟢ F(Vβ‚‚)` for every pair of open sets `V₁ βŠ‡ Vβ‚‚` that are contained in some `Uα΅’`. See `Top.presheaf.is_sheaf_iff_is_sheaf_opens_le_cover`. 4. `Top.presheaf.is_sheaf_pairwise_intersections`: For each open cover `{ Uα΅’ }` of `U`, `F(U)` is the limit of the diagram consisting of arrows from `F(Uα΅’)` and `F(Uβ±Ό)` to `F(Uα΅’ ∩ Uβ±Ό)` for each pair `(i, j)`. See `Top.presheaf.is_sheaf_iff_is_sheaf_pairwise_intersections`. The following requires `C` to be concrete and complete, and `forget C` to reflect isomorphisms and preserve limits. This applies to most "algebraic" categories, e.g. groups, abelian groups and rings. 5. `Top.presheaf.is_sheaf_unique_gluing`: (requires `C` to be concrete and complete; `forget C` to reflect isomorphisms and preserve limits) For each open cover `{ Uα΅’ }` of `U`, and a compatible family of elements `x : F(Uα΅’)`, there exists a unique gluing `x : F(U)` that restricts to the given elements. See `Top.presheaf.is_sheaf_iff_is_sheaf_unique_gluing`. 6. The underlying sheaf of types is a sheaf. See `Top.presheaf.is_sheaf_iff_is_sheaf_comp` and `category_theory.presheaf.is_sheaf_iff_is_sheaf_forget`. -/ def is_sheaf (F : presheaf.{w v u} C X) : Prop := presheaf.is_sheaf (opens.grothendieck_topology X) F /-- The presheaf valued in `unit` over any topological space is a sheaf. -/ lemma is_sheaf_unit (F : presheaf (category_theory.discrete unit) X) : F.is_sheaf := Ξ» x U S hS x hx, ⟨eq_to_hom (subsingleton.elim _ _), by tidy, by tidy⟩ lemma is_sheaf_iso_iff {F G : presheaf C X} (Ξ± : F β‰… G) : F.is_sheaf ↔ G.is_sheaf := presheaf.is_sheaf_of_iso_iff Ξ± /-- Transfer the sheaf condition across an isomorphism of presheaves. -/ lemma is_sheaf_of_iso {F G : presheaf C X} (Ξ± : F β‰… G) (h : F.is_sheaf) : G.is_sheaf := (is_sheaf_iso_iff Ξ±).1 h end presheaf variables (C X) /-- A `sheaf C X` is a presheaf of objects from `C` over a (bundled) topological space `X`, satisfying the sheaf condition. -/ @[derive category] def sheaf : Type (max u v w) := Sheaf (opens.grothendieck_topology X) C variables {C X} /-- The underlying presheaf of a sheaf -/ abbreviation sheaf.presheaf (F : X.sheaf C) : Top.presheaf C X := F.1 variables (C X) -- Let's construct a trivial example, to keep the inhabited linter happy. instance sheaf_inhabited : inhabited (sheaf (category_theory.discrete punit) X) := ⟨⟨functor.star _, presheaf.is_sheaf_unit _⟩⟩ namespace sheaf /-- The forgetful functor from sheaves to presheaves. -/ @[derive [full, faithful]] def forget : Top.sheaf C X β₯€ Top.presheaf C X := Sheaf_to_presheaf _ _ -- Note: These can be proved by simp. lemma id_app (F : sheaf C X) (t) : (πŸ™ F : F ⟢ F).1.app t = πŸ™ _ := rfl lemma comp_app {F G H : sheaf C X} (f : F ⟢ G) (g : G ⟢ H) (t) : (f ≫ g).1.app t = f.1.app t ≫ g.1.app t := rfl end sheaf end Top
7ebf0e939f999282529ac837e57287e9ebe170f5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/def8.lean
f0998699f5c7bdc07d849e8b4ff608008cefef50
[ "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
936
lean
def g : List Nat β†’ List Nat β†’ Nat | [], y::ys => y | [], ys => 0 | x1::x2::xs, ys => g xs ys | x::xs, y::ys => g xs ys + y | x::xs, [] => g xs [] universe u v inductive Imf {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) : Ξ² β†’ Type (max u v) | mk : (a : Ξ±) β†’ Imf f (f a) def h {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} : {b : Ξ²} β†’ Imf f b β†’ Ξ± | _, Imf.mk a => a theorem ex1 {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (a : Ξ±) : h (Imf.mk (f := f) a) = a := rfl def v₁ : Imf Nat.succ 1 := Imf.mk 0 def vβ‚‚ : Imf (fun x => 1 + x) 1 := Imf.mk 0 theorem ex2 : h v₁ = 0 := rfl theorem ex3 : h vβ‚‚ = 0 := rfl theorem ex4 {Ξ±} : {a b : Ξ±} β†’ a = b β†’ b = a | _, _, rfl => rfl theorem ex5 {Ξ±} : {a b : Ξ±} β†’ a = b β†’ b = a | a, .(a), rfl => rfl theorem ex6 {Ξ±} : {a b : Ξ±} β†’ a = b β†’ b = a | a, .(a), Eq.refl .(a) => rfl theorem ex7 {Ξ±} : {a b : Ξ±} β†’ a = b β†’ b = a | .(a), a, Eq.refl .(a) => rfl
9112b75a22dd0c7580e4797fc667b00eb0d0c5f4
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/group_theory/submonoid/membership.lean
68a205b93926c093cc948fa2c7f85fea653fcc88
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,624
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.operations import algebra.big_operators.basic import algebra.free_monoid import algebra.pointwise /-! # Submonoids: membership criteria In this file we prove various facts about membership in a submonoid: * `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs to a multiplicative submonoid, then so does their product; * `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs to an additive submonoid, then so does their sum; * `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and `n` is a natural number, then `x^n` (resp., `n β€’ x`) belongs to `S`; * `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`, `coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union. * `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set of products; * `closure_singleton_eq`, `mem_closure_singleton`: the multiplicative (resp., additive) closure of `{x}` consists of powers (resp., natural multiples) of `x`. ## Tags submonoid, submonoids -/ open_locale big_operators pointwise variables {M : Type*} variables {A : Type*} namespace submonoid section assoc variables [monoid M] (S : submonoid M) @[simp, norm_cast, to_additive coe_nsmul] theorem coe_pow (x : S) (n : β„•) : ↑(x ^ n) = (x ^ n : M) := S.subtype.map_pow x n @[simp, norm_cast, to_additive] theorem coe_list_prod (l : list S) : (l.prod : M) = (l.map coe).prod := S.subtype.map_list_prod l @[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] (S : submonoid M) (m : multiset S) : (m.prod : M) = (m.map coe).prod := S.subtype.map_multiset_prod m @[simp, norm_cast, to_additive] theorem coe_finset_prod {ΞΉ M} [comm_monoid M] (S : submonoid M) (f : ΞΉ β†’ S) (s : finset ΞΉ) : ↑(∏ i in s, f i) = (∏ i in s, f i : M) := S.subtype.map_prod f s /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem {l : list M} (hl : βˆ€ x ∈ l, x ∈ S) : l.prod ∈ S := by { lift l to list S using hl, rw ← coe_list_prod, exact l.prod.coe_prop } /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) (hm : βˆ€ a ∈ m, a ∈ S) : m.prod ∈ S := by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop } /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M) {ΞΉ : Type*} {t : finset ΞΉ} {f : ΞΉ β†’ M} (h : βˆ€c ∈ t, f c ∈ S) : ∏ c in t, f c ∈ S := S.multiset_prod_mem (t.1.map f) $ Ξ» x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix β–Έ h i hi @[to_additive nsmul_mem] lemma pow_mem {x : M} (hx : x ∈ S) (n : β„•) : x ^ n ∈ S := by simpa only [coe_pow] using ((⟨x, hx⟩ : S) ^ n).coe_prop end assoc section non_assoc variables [mul_one_class M] (S : submonoid M) open set @[to_additive] lemma mem_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] {S : ΞΉ β†’ submonoid M} (hS : directed (≀) S) {x : M} : x ∈ (⨆ i, S i) ↔ βˆƒ i, x ∈ S i := begin refine ⟨_, Ξ» ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, suffices : x ∈ closure (⋃ i, (S i : set M)) β†’ βˆƒ i, x ∈ S i, by simpa only [closure_Union, closure_eq (S _)] using this, refine (Ξ» hx, closure_induction hx (Ξ» _, mem_Union.1) _ _), { exact hΞΉ.elim (Ξ» i, ⟨i, (S i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hS i j with ⟨k, hki, hkj⟩, exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ } end @[to_additive] lemma coe_supr_of_directed {ΞΉ} [nonempty ΞΉ] {S : ΞΉ β†’ submonoid M} (hS : directed (≀) S) : ((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) := set.ext $ Ξ» x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≀) S) {x : M} : x ∈ Sup S ↔ βˆƒ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end @[to_additive] lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≀) S) : (↑(Sup S) : set M) = ⋃ s ∈ S, ↑s := set.ext $ Ξ» x, by simp [mem_Sup_of_directed_on Sne hS] @[to_additive] lemma mem_sup_left {S T : submonoid M} : βˆ€ {x : M}, x ∈ S β†’ x ∈ S βŠ” T := show S ≀ S βŠ” T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : submonoid M} : βˆ€ {x : M}, x ∈ T β†’ x ∈ S βŠ” T := show T ≀ S βŠ” T, from le_sup_right @[to_additive] lemma mem_supr_of_mem {ΞΉ : Type*} {S : ΞΉ β†’ submonoid M} (i : ΞΉ) : βˆ€ {x : M}, x ∈ S i β†’ x ∈ supr S := show S i ≀ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (submonoid M)} {s : submonoid M} (hs : s ∈ S) : βˆ€ {x : M}, x ∈ s β†’ x ∈ Sup S := show s ≀ Sup S, from le_Sup hs end non_assoc end submonoid namespace free_monoid variables {Ξ± : Type*} open submonoid @[to_additive] theorem closure_range_of : closure (set.range $ @of Ξ±) = ⊀ := eq_top_iff.2 $ Ξ» x hx, free_monoid.rec_on x (one_mem _) $ Ξ» x xs hxs, mul_mem _ (subset_closure $ set.mem_range_self _) hxs end free_monoid namespace submonoid variables [monoid M] open monoid_hom lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, pow_one x⟩) $ Ξ» x ⟨n, hn⟩, hn β–Έ pow_mem _ (subset_closure $ set.mem_singleton _) _ /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ βˆƒ n:β„•, x^n=y := by rw [closure_singleton_eq, mem_mrange]; refl lemma mem_closure_singleton_self {y : M} : y ∈ closure ({y} : set M) := mem_closure_singleton.2 ⟨1, pow_one y⟩ lemma closure_singleton_one : closure ({1} : set M) = βŠ₯ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[to_additive] lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s β†’ M)).mrange := by rw [mrange_eq_map, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp, free_monoid.lift_comp_of, subtype.range_coe] @[to_additive] lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) : βˆƒ (l : list M) (hl : βˆ€ y ∈ l, y ∈ s), l.prod = x := begin rw [closure_eq_mrange, mem_mrange] at hx, rcases hx with ⟨l, hx⟩, exact ⟨list.map coe l, Ξ» y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy β–Έ z.2, hx⟩ end /-- The submonoid generated by an element. -/ def powers (n : M) : submonoid M := submonoid.copy (powers_hom M n).mrange (set.range ((^) n : β„• β†’ M)) $ set.ext (Ξ» n, exists_congr $ Ξ» i, by simp; refl) @[simp] lemma mem_powers (n : M) : n ∈ powers n := ⟨1, pow_one _⟩ lemma mem_powers_iff (x z : M) : x ∈ powers z ↔ βˆƒ n : β„•, z ^ n = x := iff.rfl lemma powers_eq_closure (n : M) : powers n = closure {n} := by { ext, exact mem_closure_singleton.symm } lemma powers_subset {n : M} {P : submonoid M} (h : n ∈ P) : powers n ≀ P := Ξ» x hx, match x, hx with _, ⟨i, rfl⟩ := P.pow_mem h i end /-- Exponentiation map from natural numbers to powers. -/ def pow (n : M) (m : β„•) : powers n := ⟨n ^ m, m, rfl⟩ /-- Logarithms from powers to natural numbers. -/ def log [decidable_eq M] {n : M} (p : powers n) : β„• := nat.find $ (mem_powers_iff p.val n).mp p.prop @[simp] theorem pow_log_eq_self [decidable_eq M] {n : M} (p : powers n) : pow n (log p) = p := subtype.ext $ nat.find_spec p.prop lemma pow_right_injective_iff_pow_injective {n : M} : function.injective (Ξ» m : β„•, n ^ m) ↔ function.injective (pow n) := subtype.coe_injective.of_comp_iff (pow n) theorem log_pow_eq_self [decidable_eq M] {n : M} (h : function.injective (Ξ» m : β„•, n ^ m)) (m : β„•) : log (pow n m) = m := pow_right_injective_iff_pow_injective.mp h $ pow_log_eq_self _ theorem log_pow_int_eq_self {x : β„€} (h : 1 < x.nat_abs) (m : β„•) : log (pow x m) = m := log_pow_eq_self (int.pow_right_injective h) _ end submonoid namespace submonoid variables {N : Type*} [comm_monoid N] open monoid_hom @[to_additive] lemma sup_eq_range (s t : submonoid N) : s βŠ” t = (s.subtype.coprod t.subtype).mrange := by rw [mrange_eq_map, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl, map_mrange, coprod_comp_inr, range_subtype, range_subtype] @[to_additive] lemma mem_sup {s t : submonoid N} {x : N} : x ∈ s βŠ” t ↔ βˆƒ (y ∈ s) (z ∈ t), y * z = x := by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, set_like.exists, coe_subtype, subtype.coe_mk] end submonoid namespace add_submonoid variables [add_monoid A] open set lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, one_nsmul x⟩) $ Ξ» x ⟨n, hn⟩, hn β–Έ nsmul_mem _ (subset_closure $ set.mem_singleton _) _ /-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ βˆƒ n:β„•, n β€’ x = y := by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl lemma closure_singleton_zero : closure ({0} : set A) = βŠ₯ := by simp [eq_bot_iff_forall, mem_closure_singleton, nsmul_zero] /-- The additive submonoid generated by an element. -/ def multiples (x : A) : add_submonoid A := add_submonoid.copy (multiples_hom A x).mrange (set.range (Ξ» i, nsmul i x : β„• β†’ A)) $ set.ext (Ξ» n, exists_congr $ Ξ» i, by simp; refl) @[simp] lemma mem_multiples (x : A) : x ∈ multiples x := ⟨1, one_nsmul _⟩ lemma mem_multiples_iff (x z : A) : x ∈ multiples z ↔ βˆƒ n : β„•, n β€’ z = x := iff.rfl lemma multiples_eq_closure (x : A) : multiples x = closure {x} := by { ext, exact mem_closure_singleton.symm } lemma multiples_subset {x : A} {P : add_submonoid A} (h : x ∈ P) : multiples x ≀ P := Ξ» x hx, match x, hx with _, ⟨i, rfl⟩ := P.nsmul_mem h i end attribute [to_additive add_submonoid.multiples] submonoid.powers attribute [to_additive add_submonoid.mem_multiples] submonoid.mem_powers attribute [to_additive add_submonoid.mem_multiples_iff] submonoid.mem_powers_iff attribute [to_additive add_submonoid.multiples_eq_closure] submonoid.powers_eq_closure attribute [to_additive add_submonoid.multiples_subset] submonoid.powers_subset end add_submonoid /-! Lemmas about additive closures of `submonoid`. -/ namespace submonoid variables {R : Type*} [non_assoc_semiring R] (S : submonoid R) {a b : R} /-- The product of an element of the additive closure of a multiplicative submonoid `M` and an element of `M` is contained in the additive closure of `M`. -/ lemma mul_right_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ S) : a * b ∈ add_submonoid.closure (S : set R) := begin revert b, refine add_submonoid.closure_induction ha _ _ _; clear ha a, { exact Ξ» r hr b hb, add_submonoid.mem_closure.mpr (Ξ» y hy, hy (S.mul_mem hr hb)) }, { exact Ξ» b hb, by simp only [zero_mul, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw add_mul, exact Ξ» r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of two elements of the additive closure of a submonoid `M` is an element of the additive closure of `M`. -/ lemma mul_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := begin revert a, refine add_submonoid.closure_induction hb _ _ _; clear hb b, { exact Ξ» r hr b hb, S.mul_right_mem_add_closure hb hr }, { exact Ξ» b hb, by simp only [mul_zero, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw mul_add, exact Ξ» r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of an element of `S` and an element of the additive closure of a multiplicative submonoid `S` is contained in the additive closure of `S`. -/ lemma mul_left_mem_add_closure (ha : a ∈ S) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := S.mul_mem_add_closure (add_submonoid.mem_closure.mpr (Ξ» sT hT, hT ha)) hb @[to_additive] lemma mem_closure_inv {G : Type*} [group G] (S : set G) (x : G) : x ∈ submonoid.closure S⁻¹ ↔ x⁻¹ ∈ submonoid.closure S := begin suffices : βˆ€ (S : set G) (x : G), x ∈ submonoid.closure S⁻¹ β†’ x⁻¹ ∈ submonoid.closure S, { refine ⟨this S x, _⟩, have := this S⁻¹ x⁻¹, rw [inv_inv, set.inv_inv] at this, exact this, }, intros S x hx, refine submonoid.closure_induction hx (Ξ» x hx, _) _ (Ξ» x y hx hy, _), { exact submonoid.subset_closure (set.mem_inv.mp hx), }, { rw one_inv, exact submonoid.one_mem _ }, { rw mul_inv_rev x y, exact submonoid.mul_mem _ hy hx }, end end submonoid section mul_add lemma of_mul_image_powers_eq_multiples_of_mul [monoid M] {x : M} : additive.of_mul '' ((submonoid.powers x) : set M) = add_submonoid.multiples (additive.of_mul x) := begin ext, split, { rintros ⟨y, ⟨n, hy1⟩, hy2⟩, use n, simpa [← of_mul_pow, hy1] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_pow } end lemma of_add_image_multiples_eq_powers_of_add [add_monoid A] {x : A} : multiplicative.of_add '' ((add_submonoid.multiples x) : set A) = submonoid.powers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_powers_eq_multiples_of_mul, end end mul_add
a05eba68112adf5df959dcecbdc362f67355a87e
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Data/Lsp/Capabilities.lean
396675ffe0b19c71d29768361088cfcc2deeb20d
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
931
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Lean.Data.JsonRpc import Lean.Data.Lsp.TextSync /-! Minimal LSP servers/clients do not have to implement a lot of functionality. Most useful additional behaviour is instead opted into via capabilities. -/ namespace Lean namespace Lsp open Json -- TODO: right now we ignore the client's capabilities inductive ClientCapabilities | mk instance : FromJson ClientCapabilities := ⟨fun j => ClientCapabilities.mk⟩ -- TODO largely unimplemented structure ServerCapabilities := (textDocumentSync? : Option TextDocumentSyncOptions := none) (hoverProvider : Bool := false) instance : ToJson ServerCapabilities := ⟨fun o => mkObj $ opt "textDocumentSync" o.textDocumentSync? ++ [⟨"hoverProvider", o.hoverProvider⟩]⟩ end Lsp end Lean
1d41d050eeec5bf4319faa121a678596734ed2a0
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/witt_vector/identities.lean
2347d9edc4d82fb9f05cdd0312073cc2e6d7c9c5
[ "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
1,901
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import ring_theory.witt_vector.frobenius import ring_theory.witt_vector.verschiebung import ring_theory.witt_vector.mul_p /-! ## Identities between operations on the ring of Witt vectors In this file we derive common identities between the Frobenius and Verschiebung operators. ## Main declarations * `frobenius_verschiebung`: the composition of Frobenius and Verschiebung is multiplication by `p` * `verschiebung_mul_frobenius`: the β€œprojection formula”: `V(x * F y) = V x * y` ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ namespace witt_vector variables {p : β„•} {R : Type*} [fact p.prime] [comm_ring R] local notation `π•Ž` := witt_vector p -- type as `\bbW` noncomputable theory /-- The composition of Frobenius and Verschiebung is multiplication by `p`. -/ lemma frobenius_verschiebung (x : π•Ž R) : frobenius (verschiebung x) = x * p := by { ghost_calc x, ghost_simp [mul_comm] } /-- Verschiebung is the same as multiplication by `p` on the ring of Witt vectors of `zmod p`. -/ lemma verschiebung_zmod (x : π•Ž (zmod p)) : verschiebung x = x * p := by rw [← frobenius_verschiebung, frobenius_zmodp] lemma coeff_p_pow [char_p R p] (i : β„•) : (p ^ i : π•Ž R).coeff i = 1 := begin induction i with i h, { simp only [one_coeff_zero, ne.def, pow_zero] }, { rw [pow_succ', ← frobenius_verschiebung, coeff_frobenius_char_p, verschiebung_coeff_succ, h, one_pow], } end /-- The β€œprojection formula” for Frobenius and Verschiebung. -/ lemma verschiebung_mul_frobenius (x y : π•Ž R) : verschiebung (x * frobenius y) = verschiebung x * y := by { ghost_calc x y, rintro ⟨⟩; ghost_simp [mul_assoc] } end witt_vector
f70ac0cd855b9a987c4c23b34f5468cf16c6914a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/filter/n_ary.lean
4c06a1c9c27134581343fccc6775681158dbb151
[ "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
18,121
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 order.filter.prod /-! # N-ary maps of filter This file defines the binary and ternary maps of filters. This is mostly useful to define pointwise operations on filters. ## Main declarations * `filter.mapβ‚‚`: Binary map of filters. * `filter.map₃`: Ternary map of filters. ## Notes This file is very similar to the n-ary section of `data.set.basic`, to `data.finset.n_ary` and to `data.option.n_ary`. Please keep them in sync. -/ open function set open_locale filter namespace filter variables {Ξ± Ξ±' Ξ² Ξ²' Ξ³ Ξ³' Ξ΄ Ξ΄' Ξ΅ Ξ΅' : Type*} {m : Ξ± β†’ Ξ² β†’ Ξ³} {f f₁ fβ‚‚ : filter Ξ±} {g g₁ gβ‚‚ : filter Ξ²} {h h₁ hβ‚‚ : filter Ξ³} {s s₁ sβ‚‚ : set Ξ±} {t t₁ tβ‚‚ : set Ξ²} {u : set Ξ³} {v : set Ξ΄} {a : Ξ±} {b : Ξ²} {c : Ξ³} /-- The image of a binary function `m : Ξ± β†’ Ξ² β†’ Ξ³` as a function `filter Ξ± β†’ filter Ξ² β†’ filter Ξ³`. Mathematically this should be thought of as the image of the corresponding function `Ξ± Γ— Ξ² β†’ Ξ³`. -/ def mapβ‚‚ (m : Ξ± β†’ Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : filter Ξ³ := { sets := {s | βˆƒ u v, u ∈ f ∧ v ∈ g ∧ image2 m u v βŠ† s}, univ_sets := ⟨univ, univ, univ_sets _, univ_sets _, subset_univ _⟩, sets_of_superset := Ξ» s t hs hst, Existsβ‚‚.imp (Ξ» u v, and.imp_right $ and.imp_right $ Ξ» h, subset.trans h hst) hs, inter_sets := Ξ» s t, begin simp only [exists_prop, mem_set_of_eq, subset_inter_iff], rintro ⟨s₁, sβ‚‚, hs₁, hsβ‚‚, hs⟩ ⟨t₁, tβ‚‚, ht₁, htβ‚‚, ht⟩, exact ⟨s₁ ∩ t₁, sβ‚‚ ∩ tβ‚‚, inter_sets f hs₁ ht₁, inter_sets g hsβ‚‚ htβ‚‚, (image2_subset (inter_subset_left _ _) $ inter_subset_left _ _).trans hs, (image2_subset (inter_subset_right _ _) $ inter_subset_right _ _).trans ht⟩, end } @[simp] lemma mem_mapβ‚‚_iff : u ∈ mapβ‚‚ m f g ↔ βˆƒ s t, s ∈ f ∧ t ∈ g ∧ image2 m s t βŠ† u := iff.rfl lemma image2_mem_mapβ‚‚ (hs : s ∈ f) (ht : t ∈ g) : image2 m s t ∈ mapβ‚‚ m f g := ⟨_, _, hs, ht, subset.rfl⟩ lemma map_prod_eq_mapβ‚‚ (m : Ξ± β†’ Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : filter.map (Ξ» p : Ξ± Γ— Ξ², m p.1 p.2) (f Γ—αΆ  g) = mapβ‚‚ m f g := begin ext s, split, { intro hmem, rw filter.mem_map_iff_exists_image at hmem, obtain ⟨s', hs', hsub⟩ := hmem, rw filter.mem_prod_iff at hs', obtain ⟨t, ht, t', ht', hsub'⟩ := hs', refine ⟨t, t', ht, ht', _⟩, rw ← set.image_prod, exact subset_trans (set.image_subset (Ξ» (p : Ξ± Γ— Ξ²), m p.fst p.snd) hsub') hsub }, { intro hmem, rw mem_mapβ‚‚_iff at hmem, obtain ⟨t, t', ht, ht', hsub⟩ := hmem, rw ← set.image_prod at hsub, rw filter.mem_map_iff_exists_image, exact ⟨t Γ—Λ’ t', filter.prod_mem_prod ht ht', hsub⟩ }, end lemma map_prod_eq_mapβ‚‚' (m : Ξ± Γ— Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : filter.map m (f Γ—αΆ  g) = mapβ‚‚ (Ξ» a b, m (a, b)) f g := by { refine eq.trans _ (map_prod_eq_mapβ‚‚ (curry m) f g), ext, simp } @[simp] lemma mapβ‚‚_mk_eq_prod (f : filter Ξ±) (g : filter Ξ²) : mapβ‚‚ prod.mk f g = f Γ—αΆ  g := by ext; simp [mem_prod_iff] -- lemma image2_mem_mapβ‚‚_iff (hm : injective2 m) : image2 m s t ∈ mapβ‚‚ m f g ↔ s ∈ f ∧ t ∈ g := -- ⟨by { rintro ⟨u, v, hu, hv, h⟩, rw image2_subset_image2_iff hm at h, -- exact ⟨mem_of_superset hu h.1, mem_of_superset hv h.2⟩ }, Ξ» h, image2_mem_mapβ‚‚ h.1 h.2⟩ lemma mapβ‚‚_mono (hf : f₁ ≀ fβ‚‚) (hg : g₁ ≀ gβ‚‚) : mapβ‚‚ m f₁ g₁ ≀ mapβ‚‚ m fβ‚‚ gβ‚‚ := Ξ» _ ⟨s, t, hs, ht, hst⟩, ⟨s, t, hf hs, hg ht, hst⟩ lemma mapβ‚‚_mono_left (h : g₁ ≀ gβ‚‚) : mapβ‚‚ m f g₁ ≀ mapβ‚‚ m f gβ‚‚ := mapβ‚‚_mono subset.rfl h lemma mapβ‚‚_mono_right (h : f₁ ≀ fβ‚‚) : mapβ‚‚ m f₁ g ≀ mapβ‚‚ m fβ‚‚ g := mapβ‚‚_mono h subset.rfl @[simp] lemma le_mapβ‚‚_iff {h : filter Ξ³} : h ≀ mapβ‚‚ m f g ↔ βˆ€ ⦃s⦄, s ∈ f β†’ βˆ€ ⦃t⦄, t ∈ g β†’ image2 m s t ∈ h := ⟨λ H s hs t ht, H $ image2_mem_mapβ‚‚ hs ht, Ξ» H u ⟨s, t, hs, ht, hu⟩, mem_of_superset (H hs ht) hu⟩ @[simp] lemma mapβ‚‚_bot_left : mapβ‚‚ m βŠ₯ g = βŠ₯ := empty_mem_iff_bot.1 βŸ¨βˆ…, univ, trivial, univ_mem, (image2_empty_left).subset⟩ @[simp] lemma mapβ‚‚_bot_right : mapβ‚‚ m f βŠ₯ = βŠ₯ := empty_mem_iff_bot.1 ⟨univ, βˆ…, univ_mem, trivial, (image2_empty_right).subset⟩ @[simp] lemma mapβ‚‚_eq_bot_iff : mapβ‚‚ m f g = βŠ₯ ↔ f = βŠ₯ ∨ g = βŠ₯ := begin simp only [←empty_mem_iff_bot, mem_mapβ‚‚_iff, subset_empty_iff, image2_eq_empty_iff], split, { rintro ⟨s, t, hs, ht, rfl | rfl⟩, { exact or.inl hs }, { exact or.inr ht } }, { rintro (h | h), { exact ⟨_, _, h, univ_mem, or.inl rfl⟩ }, { exact ⟨_, _, univ_mem, h, or.inr rfl⟩ } } end @[simp] lemma mapβ‚‚_ne_bot_iff : (mapβ‚‚ m f g).ne_bot ↔ f.ne_bot ∧ g.ne_bot := by { simp_rw ne_bot_iff, exact mapβ‚‚_eq_bot_iff.not.trans not_or_distrib } lemma ne_bot.mapβ‚‚ (hf : f.ne_bot) (hg : g.ne_bot) : (mapβ‚‚ m f g).ne_bot := mapβ‚‚_ne_bot_iff.2 ⟨hf, hg⟩ lemma ne_bot.of_mapβ‚‚_left (h : (mapβ‚‚ m f g).ne_bot) : f.ne_bot := (mapβ‚‚_ne_bot_iff.1 h).1 lemma ne_bot.of_mapβ‚‚_right (h : (mapβ‚‚ m f g).ne_bot) : g.ne_bot := (mapβ‚‚_ne_bot_iff.1 h).2 lemma mapβ‚‚_sup_left : mapβ‚‚ m (f₁ βŠ” fβ‚‚) g = mapβ‚‚ m f₁ g βŠ” mapβ‚‚ m fβ‚‚ g := begin ext u, split, { rintro ⟨s, t, ⟨h₁, hβ‚‚βŸ©, ht, hu⟩, exact ⟨mem_of_superset (image2_mem_mapβ‚‚ h₁ ht) hu, mem_of_superset (image2_mem_mapβ‚‚ hβ‚‚ ht) hu⟩ }, { rintro ⟨⟨s₁, t₁, hs₁, ht₁, huβ‚βŸ©, sβ‚‚, tβ‚‚, hsβ‚‚, htβ‚‚, huβ‚‚βŸ©, refine ⟨s₁ βˆͺ sβ‚‚, t₁ ∩ tβ‚‚, union_mem_sup hs₁ hsβ‚‚, inter_mem ht₁ htβ‚‚, _⟩, rw image2_union_left, exact union_subset ((image2_subset_left $ inter_subset_left _ _).trans hu₁) ((image2_subset_left $ inter_subset_right _ _).trans huβ‚‚) } end lemma mapβ‚‚_sup_right : mapβ‚‚ m f (g₁ βŠ” gβ‚‚) = mapβ‚‚ m f g₁ βŠ” mapβ‚‚ m f gβ‚‚ := begin ext u, split, { rintro ⟨s, t, hs, ⟨h₁, hβ‚‚βŸ©, hu⟩, exact ⟨mem_of_superset (image2_mem_mapβ‚‚ hs h₁) hu, mem_of_superset (image2_mem_mapβ‚‚ hs hβ‚‚) hu⟩ }, { rintro ⟨⟨s₁, t₁, hs₁, ht₁, huβ‚βŸ©, sβ‚‚, tβ‚‚, hsβ‚‚, htβ‚‚, huβ‚‚βŸ©, refine ⟨s₁ ∩ sβ‚‚, t₁ βˆͺ tβ‚‚, inter_mem hs₁ hsβ‚‚, union_mem_sup ht₁ htβ‚‚, _⟩, rw image2_union_right, exact union_subset ((image2_subset_right $ inter_subset_left _ _).trans hu₁) ((image2_subset_right $ inter_subset_right _ _).trans huβ‚‚) } end lemma mapβ‚‚_inf_subset_left : mapβ‚‚ m (f₁ βŠ“ fβ‚‚) g ≀ mapβ‚‚ m f₁ g βŠ“ mapβ‚‚ m fβ‚‚ g := le_inf (mapβ‚‚_mono_right inf_le_left) (mapβ‚‚_mono_right inf_le_right) lemma mapβ‚‚_inf_subset_right : mapβ‚‚ m f (g₁ βŠ“ gβ‚‚) ≀ mapβ‚‚ m f g₁ βŠ“ mapβ‚‚ m f gβ‚‚ := le_inf (mapβ‚‚_mono_left inf_le_left) (mapβ‚‚_mono_left inf_le_right) @[simp] lemma mapβ‚‚_pure_left : mapβ‚‚ m (pure a) g = g.map (Ξ» b, m a b) := filter.ext $ Ξ» u, ⟨λ ⟨s, t, hs, ht, hu⟩, mem_of_superset (image_mem_map ht) ((image_subset_image2_right $ mem_pure.1 hs).trans hu), Ξ» h, ⟨{a}, _, singleton_mem_pure, h, by rw [image2_singleton_left, image_subset_iff]⟩⟩ @[simp] lemma mapβ‚‚_pure_right : mapβ‚‚ m f (pure b) = f.map (Ξ» a, m a b) := filter.ext $ Ξ» u, ⟨λ ⟨s, t, hs, ht, hu⟩, mem_of_superset (image_mem_map hs) ((image_subset_image2_left $ mem_pure.1 ht).trans hu), Ξ» h, ⟨_, {b}, h, singleton_mem_pure, by rw [image2_singleton_right, image_subset_iff]⟩⟩ lemma mapβ‚‚_pure : mapβ‚‚ m (pure a) (pure b) = pure (m a b) := by rw [mapβ‚‚_pure_right, map_pure] lemma mapβ‚‚_swap (m : Ξ± β†’ Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : mapβ‚‚ m f g = mapβ‚‚ (Ξ» a b, m b a) g f := by { ext u, split; rintro ⟨s, t, hs, ht, hu⟩; refine ⟨t, s, ht, hs, by rwa image2_swap⟩ } @[simp] lemma mapβ‚‚_left (h : g.ne_bot) : mapβ‚‚ (Ξ» x y, x) f g = f := begin ext u, refine ⟨_, Ξ» hu, ⟨_, _, hu, univ_mem, (image2_left $ h.nonempty_of_mem univ_mem).subset⟩⟩, rintro ⟨s, t, hs, ht, hu⟩, rw image2_left (h.nonempty_of_mem ht) at hu, exact mem_of_superset hs hu, end @[simp] lemma mapβ‚‚_right (h : f.ne_bot) : mapβ‚‚ (Ξ» x y, y) f g = g := by rw [mapβ‚‚_swap, mapβ‚‚_left h] /-- The image of a ternary function `m : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄` as a function `filter Ξ± β†’ filter Ξ² β†’ filter Ξ³ β†’ filter Ξ΄`. Mathematically this should be thought of as the image of the corresponding function `Ξ± Γ— Ξ² Γ— Ξ³ β†’ Ξ΄`. -/ def map₃ (m : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄) (f : filter Ξ±) (g : filter Ξ²) (h : filter Ξ³) : filter Ξ΄ := { sets := {s | βˆƒ u v w, u ∈ f ∧ v ∈ g ∧ w ∈ h ∧ image3 m u v w βŠ† s}, univ_sets := ⟨univ, univ, univ, univ_sets _, univ_sets _, univ_sets _, subset_univ _⟩, sets_of_superset := Ξ» s t hs hst, Exists₃.imp (Ξ» u v w, and.imp_right $ and.imp_right $ and.imp_right $ Ξ» h, subset.trans h hst) hs, inter_sets := Ξ» s t, begin simp only [exists_prop, mem_set_of_eq, subset_inter_iff], rintro ⟨s₁, sβ‚‚, s₃, hs₁, hsβ‚‚, hs₃, hs⟩ ⟨t₁, tβ‚‚, t₃, ht₁, htβ‚‚, ht₃, ht⟩, exact ⟨s₁ ∩ t₁, sβ‚‚ ∩ tβ‚‚, s₃ ∩ t₃, inter_mem hs₁ ht₁, inter_mem hsβ‚‚ htβ‚‚, inter_mem hs₃ ht₃, (image3_mono (inter_subset_left _ _) (inter_subset_left _ _) $ inter_subset_left _ _).trans hs, (image3_mono (inter_subset_right _ _) (inter_subset_right _ _) $ inter_subset_right _ _).trans ht⟩, end } lemma mapβ‚‚_mapβ‚‚_left (m : Ξ΄ β†’ Ξ³ β†’ Ξ΅) (n : Ξ± β†’ Ξ² β†’ Ξ΄) : mapβ‚‚ m (mapβ‚‚ n f g) h = map₃ (Ξ» a b c, m (n a b) c) f g h := begin ext w, split, { rintro ⟨s, t, ⟨u, v, hu, hv, hs⟩, ht, hw⟩, refine ⟨u, v, t, hu, hv, ht, _⟩, rw ←image2_image2_left, exact (image2_subset_right hs).trans hw }, { rintro ⟨s, t, u, hs, ht, hu, hw⟩, exact ⟨_, u, image2_mem_mapβ‚‚ hs ht, hu, by rwa image2_image2_left⟩ } end lemma mapβ‚‚_mapβ‚‚_right (m : Ξ± β†’ Ξ΄ β†’ Ξ΅) (n : Ξ² β†’ Ξ³ β†’ Ξ΄) : mapβ‚‚ m f (mapβ‚‚ n g h) = map₃ (Ξ» a b c, m a (n b c)) f g h := begin ext w, split, { rintro ⟨s, t, hs, ⟨u, v, hu, hv, ht⟩, hw⟩, refine ⟨s, u, v, hs, hu, hv, _⟩, rw ←image2_image2_right, exact (image2_subset_left ht).trans hw }, { rintro ⟨s, t, u, hs, ht, hu, hw⟩, exact ⟨s, _, hs, image2_mem_mapβ‚‚ ht hu, by rwa image2_image2_right⟩ } end lemma map_mapβ‚‚ (m : Ξ± β†’ Ξ² β†’ Ξ³) (n : Ξ³ β†’ Ξ΄) : (mapβ‚‚ m f g).map n = mapβ‚‚ (Ξ» a b, n (m a b)) f g := filter.ext $ Ξ» u, existsβ‚‚_congr $ Ξ» s t, by rw [←image_subset_iff, image_image2] lemma mapβ‚‚_map_left (m : Ξ³ β†’ Ξ² β†’ Ξ΄) (n : Ξ± β†’ Ξ³) : mapβ‚‚ m (f.map n) g = mapβ‚‚ (Ξ» a b, m (n a) b) f g := begin ext u, split, { rintro ⟨s, t, hs, ht, hu⟩, refine ⟨_, t, hs, ht, _⟩, rw ←image2_image_left, exact (image2_subset_right $ image_preimage_subset _ _).trans hu }, { rintro ⟨s, t, hs, ht, hu⟩, exact ⟨_, t, image_mem_map hs, ht, by rwa image2_image_left⟩ } end lemma mapβ‚‚_map_right (m : Ξ± β†’ Ξ³ β†’ Ξ΄) (n : Ξ² β†’ Ξ³) : mapβ‚‚ m f (g.map n) = mapβ‚‚ (Ξ» a b, m a (n b)) f g := by rw [mapβ‚‚_swap, mapβ‚‚_map_left, mapβ‚‚_swap] @[simp] lemma mapβ‚‚_curry (m : Ξ± Γ— Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : mapβ‚‚ (curry m) f g = (f Γ—αΆ  g).map m := by { classical, rw [←mapβ‚‚_mk_eq_prod, map_mapβ‚‚, curry] } @[simp] lemma map_uncurry_prod (m : Ξ± β†’ Ξ² β†’ Ξ³) (f : filter Ξ±) (g : filter Ξ²) : (f Γ—αΆ  g).map (uncurry m) = mapβ‚‚ m f g := by rw [←mapβ‚‚_curry, curry_uncurry] /-! ### Algebraic replacement rules A collection of lemmas to transfer associativity, commutativity, distributivity, ... of operations to the associativity, commutativity, distributivity, ... of `filter.mapβ‚‚` of those operations. The proof pattern is `mapβ‚‚_lemma operation_lemma`. For example, `mapβ‚‚_comm mul_comm` proves that `mapβ‚‚ (*) f g = mapβ‚‚ (*) g f` in a `comm_semigroup`. -/ lemma mapβ‚‚_assoc {m : Ξ΄ β†’ Ξ³ β†’ Ξ΅} {n : Ξ± β†’ Ξ² β†’ Ξ΄} {m' : Ξ± β†’ Ξ΅' β†’ Ξ΅} {n' : Ξ² β†’ Ξ³ β†’ Ξ΅'} {h : filter Ξ³} (h_assoc : βˆ€ a b c, m (n a b) c = m' a (n' b c)) : mapβ‚‚ m (mapβ‚‚ n f g) h = mapβ‚‚ m' f (mapβ‚‚ n' g h) := by simp only [mapβ‚‚_mapβ‚‚_left, mapβ‚‚_mapβ‚‚_right, h_assoc] lemma mapβ‚‚_comm {n : Ξ² β†’ Ξ± β†’ Ξ³} (h_comm : βˆ€ a b, m a b = n b a) : mapβ‚‚ m f g = mapβ‚‚ n g f := (mapβ‚‚_swap _ _ _).trans $ by simp_rw h_comm lemma mapβ‚‚_left_comm {m : Ξ± β†’ Ξ΄ β†’ Ξ΅} {n : Ξ² β†’ Ξ³ β†’ Ξ΄} {m' : Ξ± β†’ Ξ³ β†’ Ξ΄'} {n' : Ξ² β†’ Ξ΄' β†’ Ξ΅} (h_left_comm : βˆ€ a b c, m a (n b c) = n' b (m' a c)) : mapβ‚‚ m f (mapβ‚‚ n g h) = mapβ‚‚ n' g (mapβ‚‚ m' f h) := by { rw [mapβ‚‚_swap m', mapβ‚‚_swap m], exact mapβ‚‚_assoc (Ξ» _ _ _, h_left_comm _ _ _) } lemma mapβ‚‚_right_comm {m : Ξ΄ β†’ Ξ³ β†’ Ξ΅} {n : Ξ± β†’ Ξ² β†’ Ξ΄} {m' : Ξ± β†’ Ξ³ β†’ Ξ΄'} {n' : Ξ΄' β†’ Ξ² β†’ Ξ΅} (h_right_comm : βˆ€ a b c, m (n a b) c = n' (m' a c) b) : mapβ‚‚ m (mapβ‚‚ n f g) h = mapβ‚‚ n' (mapβ‚‚ m' f h) g := by { rw [mapβ‚‚_swap n, mapβ‚‚_swap n'], exact mapβ‚‚_assoc (Ξ» _ _ _, h_right_comm _ _ _) } lemma map_mapβ‚‚_distrib {n : Ξ³ β†’ Ξ΄} {m' : Ξ±' β†’ Ξ²' β†’ Ξ΄} {n₁ : Ξ± β†’ Ξ±'} {nβ‚‚ : Ξ² β†’ Ξ²'} (h_distrib : βˆ€ a b, n (m a b) = m' (n₁ a) (nβ‚‚ b)) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' (f.map n₁) (g.map nβ‚‚) := by simp_rw [map_mapβ‚‚, mapβ‚‚_map_left, mapβ‚‚_map_right, h_distrib] /-- Symmetric statement to `filter.mapβ‚‚_map_left_comm`. -/ lemma map_mapβ‚‚_distrib_left {n : Ξ³ β†’ Ξ΄} {m' : Ξ±' β†’ Ξ² β†’ Ξ΄} {n' : Ξ± β†’ Ξ±'} (h_distrib : βˆ€ a b, n (m a b) = m' (n' a) b) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' (f.map n') g := map_mapβ‚‚_distrib h_distrib /-- Symmetric statement to `filter.map_mapβ‚‚_right_comm`. -/ lemma map_mapβ‚‚_distrib_right {n : Ξ³ β†’ Ξ΄} {m' : Ξ± β†’ Ξ²' β†’ Ξ΄} {n' : Ξ² β†’ Ξ²'} (h_distrib : βˆ€ a b, n (m a b) = m' a (n' b)) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' f (g.map n') := map_mapβ‚‚_distrib h_distrib /-- Symmetric statement to `filter.map_mapβ‚‚_distrib_left`. -/ lemma mapβ‚‚_map_left_comm {m : Ξ±' β†’ Ξ² β†’ Ξ³} {n : Ξ± β†’ Ξ±'} {m' : Ξ± β†’ Ξ² β†’ Ξ΄} {n' : Ξ΄ β†’ Ξ³} (h_left_comm : βˆ€ a b, m (n a) b = n' (m' a b)) : mapβ‚‚ m (f.map n) g = (mapβ‚‚ m' f g).map n' := (map_mapβ‚‚_distrib_left $ Ξ» a b, (h_left_comm a b).symm).symm /-- Symmetric statement to `filter.map_mapβ‚‚_distrib_right`. -/ lemma map_mapβ‚‚_right_comm {m : Ξ± β†’ Ξ²' β†’ Ξ³} {n : Ξ² β†’ Ξ²'} {m' : Ξ± β†’ Ξ² β†’ Ξ΄} {n' : Ξ΄ β†’ Ξ³} (h_right_comm : βˆ€ a b, m a (n b) = n' (m' a b)) : mapβ‚‚ m f (g.map n) = (mapβ‚‚ m' f g).map n' := (map_mapβ‚‚_distrib_right $ Ξ» a b, (h_right_comm a b).symm).symm /-- The other direction does not hold because of the `f`-`f` cross terms on the RHS. -/ lemma mapβ‚‚_distrib_le_left {m : Ξ± β†’ Ξ΄ β†’ Ξ΅} {n : Ξ² β†’ Ξ³ β†’ Ξ΄} {m₁ : Ξ± β†’ Ξ² β†’ Ξ²'} {mβ‚‚ : Ξ± β†’ Ξ³ β†’ Ξ³'} {n' : Ξ²' β†’ Ξ³' β†’ Ξ΅} (h_distrib : βˆ€ a b c, m a (n b c) = n' (m₁ a b) (mβ‚‚ a c)) : mapβ‚‚ m f (mapβ‚‚ n g h) ≀ mapβ‚‚ n' (mapβ‚‚ m₁ f g) (mapβ‚‚ mβ‚‚ f h) := begin rintro s ⟨t₁, tβ‚‚, ⟨u₁, v, hu₁, hv, htβ‚βŸ©, ⟨uβ‚‚, w, huβ‚‚, hw, htβ‚‚βŸ©, hs⟩, refine ⟨u₁ ∩ uβ‚‚, _, inter_mem hu₁ huβ‚‚, image2_mem_mapβ‚‚ hv hw, _⟩, refine (image2_distrib_subset_left h_distrib).trans ((image2_subset _ _).trans hs), { exact (image2_subset_right $ inter_subset_left _ _).trans ht₁ }, { exact (image2_subset_right $ inter_subset_right _ _).trans htβ‚‚ } end /-- The other direction does not hold because of the `h`-`h` cross terms on the RHS. -/ lemma mapβ‚‚_distrib_le_right {m : Ξ΄ β†’ Ξ³ β†’ Ξ΅} {n : Ξ± β†’ Ξ² β†’ Ξ΄} {m₁ : Ξ± β†’ Ξ³ β†’ Ξ±'} {mβ‚‚ : Ξ² β†’ Ξ³ β†’ Ξ²'} {n' : Ξ±' β†’ Ξ²' β†’ Ξ΅} (h_distrib : βˆ€ a b c, m (n a b) c = n' (m₁ a c) (mβ‚‚ b c)) : mapβ‚‚ m (mapβ‚‚ n f g) h ≀ mapβ‚‚ n' (mapβ‚‚ m₁ f h) (mapβ‚‚ mβ‚‚ g h) := begin rintro s ⟨t₁, tβ‚‚, ⟨u, w₁, hu, hw₁, htβ‚βŸ©, ⟨v, wβ‚‚, hv, hwβ‚‚, htβ‚‚βŸ©, hs⟩, refine ⟨_, w₁ ∩ wβ‚‚, image2_mem_mapβ‚‚ hu hv, inter_mem hw₁ hwβ‚‚, _⟩, refine (image2_distrib_subset_right h_distrib).trans ((image2_subset _ _).trans hs), { exact (image2_subset_left $ inter_subset_left _ _).trans ht₁ }, { exact (image2_subset_left $ inter_subset_right _ _).trans htβ‚‚ } end lemma map_mapβ‚‚_antidistrib {n : Ξ³ β†’ Ξ΄} {m' : Ξ²' β†’ Ξ±' β†’ Ξ΄} {n₁ : Ξ² β†’ Ξ²'} {nβ‚‚ : Ξ± β†’ Ξ±'} (h_antidistrib : βˆ€ a b, n (m a b) = m' (n₁ b) (nβ‚‚ a)) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' (g.map n₁) (f.map nβ‚‚) := by { rw mapβ‚‚_swap m, exact map_mapβ‚‚_distrib (Ξ» _ _, h_antidistrib _ _) } /-- Symmetric statement to `filter.mapβ‚‚_map_left_anticomm`. -/ lemma map_mapβ‚‚_antidistrib_left {n : Ξ³ β†’ Ξ΄} {m' : Ξ²' β†’ Ξ± β†’ Ξ΄} {n' : Ξ² β†’ Ξ²'} (h_antidistrib : βˆ€ a b, n (m a b) = m' (n' b) a) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' (g.map n') f := map_mapβ‚‚_antidistrib h_antidistrib /-- Symmetric statement to `filter.map_mapβ‚‚_right_anticomm`. -/ lemma map_mapβ‚‚_antidistrib_right {n : Ξ³ β†’ Ξ΄} {m' : Ξ² β†’ Ξ±' β†’ Ξ΄} {n' : Ξ± β†’ Ξ±'} (h_antidistrib : βˆ€ a b, n (m a b) = m' b (n' a)) : (mapβ‚‚ m f g).map n = mapβ‚‚ m' g (f.map n') := map_mapβ‚‚_antidistrib h_antidistrib /-- Symmetric statement to `filter.map_mapβ‚‚_antidistrib_left`. -/ lemma mapβ‚‚_map_left_anticomm {m : Ξ±' β†’ Ξ² β†’ Ξ³} {n : Ξ± β†’ Ξ±'} {m' : Ξ² β†’ Ξ± β†’ Ξ΄} {n' : Ξ΄ β†’ Ξ³} (h_left_anticomm : βˆ€ a b, m (n a) b = n' (m' b a)) : mapβ‚‚ m (f.map n) g = (mapβ‚‚ m' g f).map n' := (map_mapβ‚‚_antidistrib_left $ Ξ» a b, (h_left_anticomm b a).symm).symm /-- Symmetric statement to `filter.map_mapβ‚‚_antidistrib_right`. -/ lemma map_mapβ‚‚_right_anticomm {m : Ξ± β†’ Ξ²' β†’ Ξ³} {n : Ξ² β†’ Ξ²'} {m' : Ξ² β†’ Ξ± β†’ Ξ΄} {n' : Ξ΄ β†’ Ξ³} (h_right_anticomm : βˆ€ a b, m a (n b) = n' (m' b a)) : mapβ‚‚ m f (g.map n) = (mapβ‚‚ m' g f).map n' := (map_mapβ‚‚_antidistrib_right $ Ξ» a b, (h_right_anticomm b a).symm).symm end filter
21adfc14a8bf998c136ea0d0c81592f8e5ba44ff
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/measure_theory/simple_func_dense.lean
e56cce3f712ea686c692a375adb39a2bf5d4524c
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
14,920
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.l1_space /-! # Density of simple functions Show that each Borel measurable function can be approximated, both pointwise and in `LΒΉ` norm, by a sequence of simple functions. -/ noncomputable theory open set filter topological_space open_locale classical topological_space universes u v variables {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Type*} namespace measure_theory open ennreal nat metric variables [measure_space Ξ±] [normed_group Ξ²] [second_countable_topology Ξ²] [measurable_space Ξ²] [borel_space Ξ²] local infixr ` β†’β‚› `:25 := simple_func -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces. lemma simple_func_sequence_tendsto {f : Ξ± β†’ Ξ²} (hf : measurable f) : βˆƒ (F : β„• β†’ (Ξ± β†’β‚› Ξ²)), βˆ€ x : Ξ±, tendsto (Ξ» n, F n x) at_top (𝓝 (f x)) ∧ βˆ€ n, βˆ₯F n xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ := -- enumerate a countable dense subset {e k} of Ξ² let ⟨D, ⟨D_countable, D_dense⟩⟩ := @separable_space.exists_countable_closure_eq_univ Ξ² _ _ in let e := enumerate_countable D_countable 0 in let E := range e in have E_dense : closure E = univ := dense_of_subset_dense (subset_range_enumerate D_countable 0) D_dense, let A' (N k : β„•) : set Ξ± := f ⁻¹' (metric.ball (e k) (1 / (N+1 : ℝ)) \ metric.ball 0 (1 / (N+1 : ℝ))) in let A N := disjointed (A' N) in have is_measurable_A' : βˆ€ {N k}, is_measurable (A' N k) := Ξ» N k, hf.preimage $ is_measurable.inter is_measurable_ball $ is_measurable.compl is_measurable_ball, have is_measurable_A : βˆ€ {N k}, is_measurable (A N k) := Ξ» N, is_measurable.disjointed $ Ξ» k, is_measurable_A', have A_subset_A' : βˆ€ {N k x}, x ∈ A N k β†’ x ∈ A' N k := Ξ» N k, inter_subset_left _ _, have dist_ek_fx' : βˆ€ {x N k}, x ∈ A' N k β†’ (dist (e k) (f x) < 1 / (N+1 : ℝ)) := Ξ» x N k, by { rw [dist_comm], simpa using (Ξ» a b, a) }, have dist_ek_fx : βˆ€ {x N k}, x ∈ A N k β†’ (dist (e k) (f x) < 1 / (N+1 : ℝ)) := Ξ» x N k h, dist_ek_fx' (A_subset_A' h), have norm_fx' : βˆ€ {x N k}, x ∈ A' N k β†’ (1 / (N+1 : ℝ)) ≀ βˆ₯f xβˆ₯ := Ξ» x N k, by simp [ball_0_eq], have norm_fx : βˆ€ {x N k}, x ∈ A N k β†’ (1 / (N+1 : ℝ)) ≀ βˆ₯f xβˆ₯ := Ξ» x N k h, norm_fx' (A_subset_A' h), -- construct the desired sequence of simple functions let M N x := nat.find_greatest (Ξ» M, x ∈ ⋃ k ≀ N, (A M k)) N in let k N x := nat.find_greatest (Ξ» k, x ∈ A (M N x) k) N in let F N x := if x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k then e (k N x) else 0 in -- prove properties of the construction above have k_unique : βˆ€ {M k k' x}, x ∈ A M k ∧ x ∈ A M k' β†’ k = k' := Ξ» M k k' x h, begin by_contradiction k_ne_k', have NE : (A M k ∩ A M k').nonempty, from ⟨x, h⟩, have E : A M k ∩ A M k' = βˆ… := disjoint_disjointed' k k' k_ne_k', exact NE.ne_empty E, end, have x_mem_Union_k : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ ⋃ k ≀ N, A (M N x) k := Ξ» N x h, @nat.find_greatest_spec (Ξ» M, x ∈ ⋃ k ≀ N, (A M k)) _ N ( let ⟨M, hM⟩ := mem_Union.1 (h) in let ⟨hM₁, hMβ‚‚βŸ© := mem_Union.1 hM in ⟨M, ⟨hM₁, hMβ‚‚βŸ©βŸ©), have x_mem_A : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ A (M N x) (k N x) := Ξ» N x h, @nat.find_greatest_spec (Ξ» k, x ∈ A (M N x) k) _ N ( let ⟨k, hk⟩ := mem_Union.1 (x_mem_Union_k h) in let ⟨hk₁, hkβ‚‚βŸ© := mem_Union.1 hk in ⟨k, ⟨hk₁, hkβ‚‚βŸ©βŸ©), have x_mem_A' : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ A' (M N x) (k N x) := Ξ» N x h, mem_of_subset_of_mem (inter_subset_left _ _) (x_mem_A h), -- prove that for all N, (F N) has finite range have F_finite : βˆ€ {N}, finite (range (F N)) := begin assume N, apply finite_range_ite, { rw range_comp, apply finite_image, exact finite_range_find_greatest }, { exact finite_range_const } end, -- prove that for all N, (F N) is a measurable function have F_measurable : βˆ€ {N}, measurable (F N) := begin assume N, refine measurable.if _ _ measurable_const, -- show `is_measurable {a : Ξ± | a ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k}` { rw set_of_mem_eq, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, -- show `measurable (Ξ» (x : Ξ±), e (k N x))` apply measurable.comp measurable_from_nat, apply measurable_find_greatest, assume k' k'_le_N, by_cases k'_eq_0 : k' = 0, -- if k' = 0 have : {x | k N x = 0} = (-⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k) βˆͺ (⋃ (m ≀ N), A m 0 - ⋃ m' (hmm' : m < m') (hm'N : m' ≀ N) (k ≀ N), A m' k), { ext, split, { rw [mem_set_of_eq, mem_union_eq, or_iff_not_imp_left, mem_compl_eq, not_not_mem], assume k_eq_0 x_mem, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_0, exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, { exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ } }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], by_cases x_mem : (x βˆ‰ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k), { intro, apply find_greatest_eq_zero, assume k k_le_N hx, have : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { simp only [mem_Union], use [M N x, nat.find_greatest_le, k, k_le_N, hx] }, contradiction }, { rw not_not_mem at x_mem, assume h, cases h, contradiction, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, { have : x ∈ ⋃ k ≀ N, A m k, { exact mem_bUnion (nat.zero_le N) hx }, contradiction }, { exact ⟨m, m_le_N, mem_bUnion (nat.zero_le _) hx⟩ } }, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, rw ← k'_eq_0, exact k_unique ⟨x_mem_A x_mem, by { rw [k'_eq_0, M_eq_m], exact hx }⟩ } } }, -- end of `have` rw [k'_eq_0, this], apply is_measurable.union, { apply is_measurable.compl, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, { simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] }, -- if k' β‰  0 have : {x | k N x = k'} = ⋃(m ≀ N), A m k' - ⋃m' (hmm' : m < m') (hm'N : m' ≀ N) (k ≀ N), A m' k, { ext, split, { rw [mem_set_of_eq], assume k_eq_k', have x_mem : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { have := find_greatest_of_ne_zero k_eq_k' k'_eq_0, simp only [mem_Union], use [M N x, nat.find_greatest_le, k', k'_le_N, this] }, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_k', exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], assume h, have x_mem : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, hm, hx, _⟩, simp only [mem_Union], use [m, hm, k', k'_le_N, hx] }, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, have : x ∈ ⋃ k ≀ N, A m k := mem_bUnion k'_le_N hx, contradiction, { simp only [mem_Union], use [m, m_le_N, k', k'_le_N, hx] }}, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, exact k_unique ⟨x_mem_A x_mem, by { rw M_eq_m, exact hx }⟩ } }, -- end of `have` rw this, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] end, -- start of proof ⟨λ N, ⟨F N, Ξ» x, measurable.preimage F_measurable is_measurable_singleton, F_finite⟩, -- The pointwise convergence part of the theorem Ξ» x, ⟨metric.tendsto_at_top.2 $ Ξ» Ξ΅ hΞ΅, classical.by_cases --first case : f x = 0 ( assume fx_eq_0 : f x = 0, have x_not_mem_A' : βˆ€ {M k}, x βˆ‰ A' M k := Ξ» M k, begin simp only [mem_preimage, fx_eq_0, metric.mem_ball, one_div_eq_inv, norm_zero, not_and, not_lt, add_comm, not_le, dist_zero_right, mem_diff], assume h, rw add_comm, exact inv_pos_of_nat end, have x_not_mem_A : βˆ€ {M k}, x βˆ‰ A M k := by { assume M k h, have := disjointed_subset h, exact absurd this x_not_mem_A' }, have F_eq_0 : βˆ€ {N}, F N x = 0 := Ξ» N, by simp [F, if_neg, mem_Union, x_not_mem_A], -- end of `have` ⟨0, Ξ» n hn, show dist (F n x) (f x) < Ξ΅, by {rw [fx_eq_0, F_eq_0, dist_self], exact hΞ΅}⟩ ) --second case : f x β‰  0 ( assume fx_ne_0 : f x β‰  0, let ⟨Nβ‚€, hN⟩ := exists_nat_one_div_lt (lt_min (norm_pos_iff.2 fx_ne_0) hΞ΅) in have norm_fx_gt : _ := (lt_min_iff.1 hN).1, have Ξ΅_gt : _ := (lt_min_iff.1 hN).2, have x_mem_Union_k_Nβ‚€ : x ∈ ⋃ k, A Nβ‚€ k := let ⟨k, hk⟩ := mem_closure_range_iff_nat.1 (by { rw E_dense, exact mem_univ (f x) }) Nβ‚€ in begin rw [Union_disjointed, mem_Union], use k, rw [mem_preimage], simp, rw [← one_div_eq_inv], exact ⟨hk, le_of_lt norm_fx_gt⟩ end, let ⟨kβ‚€, x_mem_A⟩ := mem_Union.1 x_mem_Union_k_Nβ‚€ in let n := max Nβ‚€ kβ‚€ in have x_mem_Union_Union : βˆ€ {N} (hN : n ≀ N), x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k := assume N hN, mem_Union.2 ⟨Nβ‚€, mem_Union.2 ⟨le_trans (le_max_left _ _) hN, mem_Union.2 ⟨kβ‚€, mem_Union.2 ⟨le_trans (le_max_right _ _) hN, x_mem_A⟩⟩⟩⟩, have FN_eq : βˆ€ {N} (hN : n ≀ N), F N x = e (k N x) := assume N hN, if_pos $ x_mem_Union_Union hN, -- start of proof ⟨n, assume N hN, have Nβ‚€_le_N : Nβ‚€ ≀ N := le_trans (le_max_left _ _) hN, have kβ‚€_le_N : kβ‚€ ≀ N := le_trans (le_max_right _ _) hN, show dist (F N x) (f x) < Ξ΅, from calc dist (F N x) (f x) = dist (e (k N x)) (f x) : by rw FN_eq hN ... < 1 / ((M N x : ℝ) + 1) : begin have := x_mem_A' (x_mem_Union_Union hN), rw [mem_preimage, mem_diff, metric.mem_ball, dist_comm] at this, exact this.1 end ... ≀ 1 / ((Nβ‚€ : ℝ) + 1) : @one_div_le_one_div_of_le _ _ ((Nβ‚€ : ℝ) + 1) ((M N x : ℝ) + 1) (nat.cast_add_one_pos Nβ‚€) (add_le_add_right (nat.cast_le.2 (nat.le_find_greatest Nβ‚€_le_N (mem_bUnion kβ‚€_le_N x_mem_A))) 1) ... < Ξ΅ : Ξ΅_gt ⟩ ), -- second part of the theorem assume N, show βˆ₯F N xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯, from classical.by_cases ( assume h : x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k, calc βˆ₯F N xβˆ₯ = dist (F N x) 0 : by simp ... = dist (e (k N x)) 0 : begin simp only [F], rw if_pos h end ... ≀ dist (e (k N x)) (f x) + dist (f x) 0 : dist_triangle _ _ _ ... = dist (e (k N x)) (f x) + βˆ₯f xβˆ₯ : by simp ... ≀ 1 / ((M N x : ℝ) + 1) + βˆ₯f xβˆ₯ : le_of_lt $ add_lt_add_right (dist_ek_fx (x_mem_A h)) _ ... ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ : add_le_add_right (norm_fx (x_mem_A h) ) _) ( assume h : x βˆ‰ ⋃ M ≀ N, ⋃ k ≀ N, A M k, have F_eq_0 : F N x = 0 := if_neg h, by { simp only [F_eq_0, norm_zero], exact add_nonneg (norm_nonneg _) (norm_nonneg _) } )⟩⟩ lemma simple_func_sequence_tendsto' {f : Ξ± β†’ Ξ²} (hfm : measurable f) (hfi : integrable f) : βˆƒ (F : β„• β†’ (Ξ± β†’β‚› Ξ²)), (βˆ€n, integrable (F n)) ∧ tendsto (Ξ» n, ∫⁻ x, nndist (F n x) (f x)) at_top (𝓝 0) := let ⟨F, hF⟩ := simple_func_sequence_tendsto hfm in let G : β„• β†’ Ξ± β†’ ennreal := Ξ»n x, nndist (F n x) (f x) in let g : Ξ± β†’ ennreal := Ξ»x, nnnorm (f x) + nnnorm (f x) + nnnorm (f x) in have hF_meas : βˆ€ n, measurable (G n) := Ξ» n, measurable.comp measurable_coe $ (F n).measurable.nndist hfm, have hg_meas : measurable g := measurable.comp measurable_coe $ measurable.add (measurable.add hfm.nnnorm hfm.nnnorm) hfm.nnnorm, have h_bound : βˆ€ n, βˆ€β‚˜ x, G n x ≀ g x := Ξ» n, all_ae_of_all $ Ξ» x, coe_le_coe.2 $ calc nndist (F n x) (f x) ≀ nndist (F n x) 0 + nndist 0 (f x) : nndist_triangle _ _ _ ... = nnnorm (F n x) + nnnorm (f x) : by simp [nndist_eq_nnnorm] ... ≀ nnnorm (f x) + nnnorm (f x) + nnnorm (f x) : by { simp [nnreal.coe_le_coe.symm, (hF x).2, add_comm] }, have h_finite : lintegral g < ⊀ := calc (∫⁻ x, nnnorm (f x) + nnnorm (f x) + nnnorm (f x)) = (∫⁻ x, nnnorm (f x)) + (∫⁻ x, nnnorm (f x)) + (∫⁻ x, nnnorm (f x)) : by { rw [lintegral_add, lintegral_nnnorm_add], exacts [hfm, hfm, hfm.ennnorm.add hfm.ennnorm, hfm.ennnorm] } ... < ⊀ : by { simp only [and_self, add_lt_top], exact hfi}, have h_lim : βˆ€β‚˜ x, tendsto (Ξ» n, G n x) at_top (𝓝 0) := all_ae_of_all $ Ξ» x, begin apply (@tendsto_coe β„• at_top (Ξ» n, nndist (F n x) (f x)) 0).2, apply (@nnreal.tendsto_coe β„• at_top (Ξ» n, nndist (F n x) (f x)) 0).1, apply tendsto_iff_dist_tendsto_zero.1 (hF x).1 end, begin use F, split, { assume n, exact calc (∫⁻ a, nnnorm (F n a)) ≀ ∫⁻ a, nnnorm (f a) + nnnorm (f a) : lintegral_mono (by { assume a, simp only [coe_add.symm, coe_le_coe], exact (hF a).2 n }) ... = (∫⁻ a, nnnorm (f a)) + (∫⁻ a, nnnorm (f a)) : lintegral_nnnorm_add hfm hfm ... < ⊀ : by simp only [add_lt_top, and_self]; exact hfi }, convert @tendsto_lintegral_of_dominated_convergence _ _ G (Ξ» a, 0) g hF_meas h_bound h_finite h_lim, simp only [lintegral_zero] end end measure_theory
c0228a74275c43b3f16a5602a70f1710f212279a
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/padics/ring_homs.lean
c5e60e21d58708ea74f23e97a2471562e9e045b0
[ "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
24,407
lean
/- Copyright (c) 2020 Johan Commelin, Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import data.zmod.basic import number_theory.padics.padic_integers /-! # Relating `β„€_[p]` to `zmod (p ^ n)` In this file we establish connections between the `p`-adic integers $\mathbb{Z}_p$ and the integers modulo powers of `p`, $\mathbb{Z}/p^n\mathbb{Z}$. ## Main declarations We show that $\mathbb{Z}_p$ has a ring hom to $\mathbb{Z}/p^n\mathbb{Z}$ for each `n`. The case for `n = 1` is handled separately, since it is used in the general construction and we may want to use it without the `^1` getting in the way. * `padic_int.to_zmod`: ring hom to `zmod p` * `padic_int.to_zmod_pow`: ring hom to `zmod (p^n)` * `padic_int.ker_to_zmod` / `padic_int.ker_to_zmod_pow`: the kernels of these maps are the ideals generated by `p^n` We also establish the universal property of $\mathbb{Z}_p$ as a projective limit. Given a family of compatible ring homs $f_k : R \to \mathbb{Z}/p^n\mathbb{Z}$, there is a unique limit $R \to \mathbb{Z}_p$. * `padic_int.lift`: the limit function * `padic_int.lift_spec` / `padic_int.lift_unique`: the universal property ## Implementation notes The ring hom constructions go through an auxiliary constructor `padic_int.to_zmod_hom`, which removes some boilerplate code. -/ noncomputable theory open_locale classical open nat local_ring padic namespace padic_int variables {p : β„•} [hp_prime : fact (p.prime)] include hp_prime section ring_homs /-! ### Ring homomorphisms to `zmod p` and `zmod (p ^ n)` -/ variables (p) (r : β„š) omit hp_prime /-- `mod_part p r` is an integer that satisfies `βˆ₯(r - mod_part p r : β„š_[p])βˆ₯ < 1` when `βˆ₯(r : β„š_[p])βˆ₯ ≀ 1`, see `padic_int.norm_sub_mod_part`. It is the unique non-negative integer that is `< p` with this property. (Note that this definition assumes `r : β„š`. See `padic_int.zmod_repr` for a version that takes values in `β„•` and works for arbitrary `x : β„€_[p]`.) -/ def mod_part : β„€ := (r.num * gcd_a r.denom p) % p include hp_prime variable {p} lemma mod_part_lt_p : mod_part p r < p := begin convert int.mod_lt _ _, { simp }, { exact_mod_cast hp_prime.1.ne_zero } end lemma mod_part_nonneg : 0 ≀ mod_part p r := int.mod_nonneg _ $ by exact_mod_cast hp_prime.1.ne_zero lemma is_unit_denom (r : β„š) (h : βˆ₯(r : β„š_[p])βˆ₯ ≀ 1) : is_unit (r.denom : β„€_[p]) := begin rw is_unit_iff, apply le_antisymm (r.denom : β„€_[p]).2, rw [← not_lt, val_eq_coe, coe_coe], intro norm_denom_lt, have hr : βˆ₯(r * r.denom : β„š_[p])βˆ₯ = βˆ₯(r.num : β„š_[p])βˆ₯, { rw_mod_cast @rat.mul_denom_eq_num r, refl, }, rw padic_norm_e.mul at hr, have key : βˆ₯(r.num : β„š_[p])βˆ₯ < 1, { calc _ = _ : hr.symm ... < 1 * 1 : mul_lt_mul' h norm_denom_lt (norm_nonneg _) zero_lt_one ... = 1 : mul_one 1 }, have : ↑p ∣ r.num ∧ (p : β„€) ∣ r.denom, { simp only [← norm_int_lt_one_iff_dvd, ← padic_norm_e_of_padic_int], norm_cast, exact ⟨key, norm_denom_lt⟩ }, apply hp_prime.1.not_dvd_one, rwa [← r.cop.gcd_eq_one, nat.dvd_gcd_iff, ← int.coe_nat_dvd_left, ← int.coe_nat_dvd], end lemma norm_sub_mod_part_aux (r : β„š) (h : βˆ₯(r : β„š_[p])βˆ₯ ≀ 1) : ↑p ∣ r.num - r.num * r.denom.gcd_a p % p * ↑(r.denom) := begin rw ← zmod.int_coe_zmod_eq_zero_iff_dvd, simp only [int.cast_coe_nat, zmod.nat_cast_mod, int.cast_mul, int.cast_sub], have := congr_arg (coe : β„€ β†’ zmod p) (gcd_eq_gcd_ab r.denom p), simp only [int.cast_coe_nat, add_zero, int.cast_add, zmod.nat_cast_self, int.cast_mul, zero_mul] at this, push_cast, rw [mul_right_comm, mul_assoc, ←this], suffices rdcp : r.denom.coprime p, { rw rdcp.gcd_eq_one, simp only [mul_one, cast_one, sub_self], }, apply coprime.symm, apply (coprime_or_dvd_of_prime hp_prime.1 _).resolve_right, rw [← int.coe_nat_dvd, ← norm_int_lt_one_iff_dvd, not_lt], apply ge_of_eq, rw ← is_unit_iff, exact is_unit_denom r h, end lemma norm_sub_mod_part (h : βˆ₯(r : β„š_[p])βˆ₯ ≀ 1) : βˆ₯(⟨r,h⟩ - mod_part p r : β„€_[p])βˆ₯ < 1 := begin let n := mod_part p r, rw [norm_lt_one_iff_dvd, ← (is_unit_denom r h).dvd_mul_right], suffices : ↑p ∣ r.num - n * r.denom, { convert (int.cast_ring_hom β„€_[p]).map_dvd this, simp only [sub_mul, int.cast_coe_nat, ring_hom.eq_int_cast, int.cast_mul, sub_left_inj, int.cast_sub], apply subtype.coe_injective, simp only [coe_mul, subtype.coe_mk, coe_coe], rw_mod_cast @rat.mul_denom_eq_num r, refl }, exact norm_sub_mod_part_aux r h end lemma exists_mem_range_of_norm_rat_le_one (h : βˆ₯(r : β„š_[p])βˆ₯ ≀ 1) : βˆƒ n : β„€, 0 ≀ n ∧ n < p ∧ βˆ₯(⟨r,h⟩ - n : β„€_[p])βˆ₯ < 1 := ⟨mod_part p r, mod_part_nonneg _, mod_part_lt_p _, norm_sub_mod_part _ h⟩ lemma zmod_congr_of_sub_mem_span_aux (n : β„•) (x : β„€_[p]) (a b : β„€) (ha : x - a ∈ (ideal.span {p ^ n} : ideal β„€_[p])) (hb : x - b ∈ (ideal.span {p ^ n} : ideal β„€_[p])) : (a : zmod (p ^ n)) = b := begin rw [ideal.mem_span_singleton] at ha hb, rw [← sub_eq_zero, ← int.cast_sub, zmod.int_coe_zmod_eq_zero_iff_dvd, int.coe_nat_pow], rw [← dvd_neg, neg_sub] at ha, have := dvd_add ha hb, rwa [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, ← sub_eq_add_neg, ← int.cast_sub, pow_p_dvd_int_iff] at this, end lemma zmod_congr_of_sub_mem_span (n : β„•) (x : β„€_[p]) (a b : β„•) (ha : x - a ∈ (ideal.span {p ^ n} : ideal β„€_[p])) (hb : x - b ∈ (ideal.span {p ^ n} : ideal β„€_[p])) : (a : zmod (p ^ n)) = b := zmod_congr_of_sub_mem_span_aux n x a b ha hb lemma zmod_congr_of_sub_mem_max_ideal (x : β„€_[p]) (m n : β„•) (hm : x - m ∈ maximal_ideal β„€_[p]) (hn : x - n ∈ maximal_ideal β„€_[p]) : (m : zmod p) = n := begin rw maximal_ideal_eq_span_p at hm hn, have := zmod_congr_of_sub_mem_span_aux 1 x m n, simp only [pow_one] at this, specialize this hm hn, apply_fun zmod.cast_hom (show p ∣ p ^ 1, by rw pow_one) (zmod p) at this, simpa only [ring_hom.map_int_cast], end variable (x : β„€_[p]) lemma exists_mem_range : βˆƒ n : β„•, n < p ∧ (x - n ∈ maximal_ideal β„€_[p]) := begin simp only [maximal_ideal_eq_span_p, ideal.mem_span_singleton, ← norm_lt_one_iff_dvd], obtain ⟨r, hr⟩ := rat_dense (x : β„š_[p]) zero_lt_one, have H : βˆ₯(r : β„š_[p])βˆ₯ ≀ 1, { rw norm_sub_rev at hr, calc _ = βˆ₯(r : β„š_[p]) - x + xβˆ₯ : by ring_nf ... ≀ _ : padic_norm_e.nonarchimedean _ _ ... ≀ _ : max_le (le_of_lt hr) x.2 }, obtain ⟨n, hzn, hnp, hn⟩ := exists_mem_range_of_norm_rat_le_one r H, lift n to β„• using hzn, use n, split, {exact_mod_cast hnp}, simp only [norm_def, coe_sub, subtype.coe_mk, coe_coe] at hn ⊒, rw show (x - n : β„š_[p]) = (x - r) + (r - n), by ring, apply lt_of_le_of_lt (padic_norm_e.nonarchimedean _ _), apply max_lt hr, simpa using hn end /-- `zmod_repr x` is the unique natural number smaller than `p` satisfying `βˆ₯(x - zmod_repr x : β„€_[p])βˆ₯ < 1`. -/ def zmod_repr : β„• := classical.some (exists_mem_range x) lemma zmod_repr_spec : zmod_repr x < p ∧ (x - zmod_repr x ∈ maximal_ideal β„€_[p]) := classical.some_spec (exists_mem_range x) lemma zmod_repr_lt_p : zmod_repr x < p := (zmod_repr_spec _).1 lemma sub_zmod_repr_mem : (x - zmod_repr x ∈ maximal_ideal β„€_[p]) := (zmod_repr_spec _).2 /-- `to_zmod_hom` is an auxiliary constructor for creating ring homs from `β„€_[p]` to `zmod v`. -/ def to_zmod_hom (v : β„•) (f : β„€_[p] β†’ β„•) (f_spec : βˆ€ x, x - f x ∈ (ideal.span {v} : ideal β„€_[p])) (f_congr : βˆ€ (x : β„€_[p]) (a b : β„•), x - a ∈ (ideal.span {v} : ideal β„€_[p]) β†’ x - b ∈ (ideal.span {v} : ideal β„€_[p]) β†’ (a : zmod v) = b) : β„€_[p] β†’+* zmod v := { to_fun := Ξ» x, f x, map_zero' := begin rw [f_congr (0 : β„€_[p]) _ 0, cast_zero], { exact f_spec _ }, { simp only [sub_zero, cast_zero, submodule.zero_mem], } end, map_one' := begin rw [f_congr (1 : β„€_[p]) _ 1, cast_one], { exact f_spec _ }, { simp only [sub_self, cast_one, submodule.zero_mem], } end, map_add' := begin intros x y, rw [f_congr (x + y) _ (f x + f y), cast_add], { exact f_spec _ }, { convert ideal.add_mem _ (f_spec x) (f_spec y), rw cast_add, ring, } end, map_mul' := begin intros x y, rw [f_congr (x * y) _ (f x * f y), cast_mul], { exact f_spec _ }, { let I : ideal β„€_[p] := ideal.span {v}, convert I.add_mem (I.mul_mem_left x (f_spec y)) (I.mul_mem_right (f y) (f_spec x)), rw cast_mul, ring, } end, } /-- `to_zmod` is a ring hom from `β„€_[p]` to `zmod p`, with the equality `to_zmod x = (zmod_repr x : zmod p)`. -/ def to_zmod : β„€_[p] β†’+* zmod p := to_zmod_hom p zmod_repr (by { rw ←maximal_ideal_eq_span_p, exact sub_zmod_repr_mem }) (by { rw ←maximal_ideal_eq_span_p, exact zmod_congr_of_sub_mem_max_ideal } ) /-- `z - (to_zmod z : β„€_[p])` is contained in the maximal ideal of `β„€_[p]`, for every `z : β„€_[p]`. The coercion from `zmod p` to `β„€_[p]` is `zmod.has_coe_t`, which coerces `zmod p` into artibrary rings. This is unfortunate, but a consequence of the fact that we allow `zmod p` to coerce to rings of arbitrary characteristic, instead of only rings of characteristic `p`. This coercion is only a ring homomorphism if it coerces into a ring whose characteristic divides `p`. While this is not the case here we can still make use of the coercion. -/ lemma to_zmod_spec (z : β„€_[p]) : z - (to_zmod z : β„€_[p]) ∈ maximal_ideal β„€_[p] := begin convert sub_zmod_repr_mem z using 2, dsimp [to_zmod, to_zmod_hom], unfreezingI { rcases (exists_eq_add_of_lt (hp_prime.1.pos)) with ⟨p', rfl⟩ }, change ↑(zmod.val _) = _, simp only [zmod.val_nat_cast, add_zero, add_def, nat.cast_inj, zero_add], apply mod_eq_of_lt, simpa only [zero_add] using zmod_repr_lt_p z, end lemma ker_to_zmod : (to_zmod : β„€_[p] β†’+* zmod p).ker = maximal_ideal β„€_[p] := begin ext x, rw ring_hom.mem_ker, split, { intro h, simpa only [h, zmod.cast_zero, sub_zero] using to_zmod_spec x, }, { intro h, rw ← sub_zero x at h, dsimp [to_zmod, to_zmod_hom], convert zmod_congr_of_sub_mem_max_ideal x _ 0 _ h, apply sub_zmod_repr_mem, } end /-- `appr n x` gives a value `v : β„•` such that `x` and `↑v : β„€_p` are congruent mod `p^n`. See `appr_spec`. -/ noncomputable def appr : β„€_[p] β†’ β„• β†’ β„• | x 0 := 0 | x (n+1) := let y := x - appr x n in if hy : y = 0 then appr x n else let u := unit_coeff hy in appr x n + p ^ n * (to_zmod ((u : β„€_[p]) * (p ^ (y.valuation - n).nat_abs))).val lemma appr_lt (x : β„€_[p]) (n : β„•) : x.appr n < p ^ n := begin induction n with n ih generalizing x, { simp only [appr, succ_pos', pow_zero], }, simp only [appr, map_nat_cast, zmod.nat_cast_self, ring_hom.map_pow, int.nat_abs, ring_hom.map_mul], have hp : p ^ n < p ^ (n + 1), { apply pow_lt_pow hp_prime.1.one_lt (lt_add_one n) }, split_ifs with h, { apply lt_trans (ih _) hp, }, { calc _ < p ^ n + p ^ n * (p - 1) : _ ... = p ^ (n + 1) : _, { apply add_lt_add_of_lt_of_le (ih _), apply nat.mul_le_mul_left, apply le_pred_of_lt, apply zmod.val_lt }, { rw [mul_tsub, mul_one, ← pow_succ'], apply add_tsub_cancel_of_le (le_of_lt hp) } } end lemma appr_mono (x : β„€_[p]) : monotone x.appr := begin apply monotone_nat_of_le_succ, intro n, dsimp [appr], split_ifs, { refl, }, apply nat.le_add_right, end lemma dvd_appr_sub_appr (x : β„€_[p]) (m n : β„•) (h : m ≀ n) : p ^ m ∣ x.appr n - x.appr m := begin obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le h, clear h, induction k with k ih, { simp only [add_zero, tsub_self, dvd_zero], }, rw [nat.succ_eq_add_one, ← add_assoc], dsimp [appr], split_ifs with h, { exact ih }, rw [add_comm, add_tsub_assoc_of_le (appr_mono _ (nat.le_add_right m k))], apply dvd_add _ ih, apply dvd_mul_of_dvd_left, apply pow_dvd_pow _ (nat.le_add_right m k), end lemma appr_spec (n : β„•) : βˆ€ (x : β„€_[p]), x - appr x n ∈ (ideal.span {p^n} : ideal β„€_[p]) := begin simp only [ideal.mem_span_singleton], induction n with n ih, { simp only [is_unit_one, is_unit.dvd, pow_zero, forall_true_iff], }, intro x, dsimp only [appr], split_ifs with h, { rw h, apply dvd_zero }, push_cast, rw sub_add_eq_sub_sub, obtain ⟨c, hc⟩ := ih x, simp only [map_nat_cast, zmod.nat_cast_self, ring_hom.map_pow, ring_hom.map_mul, zmod.nat_cast_val], have hc' : c β‰  0, { rintro rfl, simp only [mul_zero] at hc, contradiction }, conv_rhs { congr, simp only [hc], }, rw show (x - ↑(appr x n)).valuation = (↑p ^ n * c).valuation, { rw hc }, rw [valuation_p_pow_mul _ _ hc', add_sub_cancel', pow_succ', ← mul_sub], apply mul_dvd_mul_left, obtain hc0 | hc0 := c.valuation.nat_abs.eq_zero_or_pos, { simp only [hc0, mul_one, pow_zero], rw [mul_comm, unit_coeff_spec h] at hc, suffices : c = unit_coeff h, { rw [← this, ← ideal.mem_span_singleton, ← maximal_ideal_eq_span_p], apply to_zmod_spec }, obtain ⟨c, rfl⟩ : is_unit c, -- TODO: write a can_lift instance for units { rw int.nat_abs_eq_zero at hc0, rw [is_unit_iff, norm_eq_pow_val hc', hc0, neg_zero, zpow_zero], }, rw discrete_valuation_ring.unit_mul_pow_congr_unit _ _ _ _ _ hc, exact irreducible_p }, { rw zero_pow hc0, simp only [sub_zero, zmod.cast_zero, mul_zero], rw unit_coeff_spec hc', exact (dvd_pow_self (p : β„€_[p]) hc0.ne').mul_left _, }, end attribute [irreducible] appr /-- A ring hom from `β„€_[p]` to `zmod (p^n)`, with underlying function `padic_int.appr n`. -/ def to_zmod_pow (n : β„•) : β„€_[p] β†’+* zmod (p ^ n) := to_zmod_hom (p^n) (Ξ» x, appr x n) (by { intros, convert appr_spec n _ using 1, simp }) (by { intros x a b ha hb, apply zmod_congr_of_sub_mem_span n x a b, { simpa using ha }, { simpa using hb } }) lemma ker_to_zmod_pow (n : β„•) : (to_zmod_pow n : β„€_[p] β†’+* zmod (p ^ n)).ker = ideal.span {p ^ n} := begin ext x, rw ring_hom.mem_ker, split, { intro h, suffices : x.appr n = 0, { convert appr_spec n x, simp only [this, sub_zero, cast_zero], }, dsimp [to_zmod_pow, to_zmod_hom] at h, rw zmod.nat_coe_zmod_eq_zero_iff_dvd at h, apply eq_zero_of_dvd_of_lt h (appr_lt _ _), }, { intro h, rw ← sub_zero x at h, dsimp [to_zmod_pow, to_zmod_hom], rw [zmod_congr_of_sub_mem_span n x _ 0 _ h, cast_zero], apply appr_spec, } end @[simp] lemma zmod_cast_comp_to_zmod_pow (m n : β„•) (h : m ≀ n) : (zmod.cast_hom (pow_dvd_pow p h) (zmod (p ^ m))).comp (to_zmod_pow n) = to_zmod_pow m := begin apply zmod.ring_hom_eq_of_ker_eq, ext x, rw [ring_hom.mem_ker, ring_hom.mem_ker], simp only [function.comp_app, zmod.cast_hom_apply, ring_hom.coe_comp], simp only [to_zmod_pow, to_zmod_hom, ring_hom.coe_mk], rw [zmod.cast_nat_cast (pow_dvd_pow p h), zmod_congr_of_sub_mem_span m (x.appr n) (x.appr n) (x.appr m)], { rw [sub_self], apply ideal.zero_mem _, }, { rw ideal.mem_span_singleton, rcases dvd_appr_sub_appr x m n h with ⟨c, hc⟩, use c, rw [← nat.cast_sub (appr_mono _ h), hc, nat.cast_mul, nat.cast_pow], }, { apply_instance } end @[simp] lemma cast_to_zmod_pow (m n : β„•) (h : m ≀ n) (x : β„€_[p]) : ↑(to_zmod_pow n x) = to_zmod_pow m x := by { rw ← zmod_cast_comp_to_zmod_pow _ _ h, refl } lemma dense_range_nat_cast : dense_range (nat.cast : β„• β†’ β„€_[p]) := begin intro x, rw metric.mem_closure_range_iff, intros Ξ΅ hΞ΅, obtain ⟨n, hn⟩ := exists_pow_neg_lt p hΞ΅, use (x.appr n), rw dist_eq_norm, apply lt_of_le_of_lt _ hn, rw norm_le_pow_iff_mem_span_pow, apply appr_spec, end lemma dense_range_int_cast : dense_range (int.cast : β„€ β†’ β„€_[p]) := begin intro x, apply dense_range_nat_cast.induction_on x, { exact is_closed_closure, }, { intro a, change (a.cast : β„€_[p]) with (a : β„€).cast, apply subset_closure, exact set.mem_range_self _ } end end ring_homs section lift /-! ### Universal property as projective limit -/ open cau_seq padic_seq variables {R : Type*} [non_assoc_semiring R] (f : Ξ  k : β„•, R β†’+* zmod (p^k)) (f_compat : βˆ€ k1 k2 (hk : k1 ≀ k2), (zmod.cast_hom (pow_dvd_pow p hk) _).comp (f k2) = f k1) omit hp_prime /-- Given a family of ring homs `f : Ξ  n : β„•, R β†’+* zmod (p ^ n)`, `nth_hom f r` is an integer-valued sequence whose `n`th value is the unique integer `k` such that `0 ≀ k < p ^ n` and `f n r = (k : zmod (p ^ n))`. -/ def nth_hom (r : R) : β„• β†’ β„€ := Ξ» n, (f n r : zmod (p^n)).val @[simp] lemma nth_hom_zero : nth_hom f 0 = 0 := by simp [nth_hom]; refl variable {f} include hp_prime include f_compat lemma pow_dvd_nth_hom_sub (r : R) (i j : β„•) (h : i ≀ j) : ↑p ^ i ∣ nth_hom f r j - nth_hom f r i := begin specialize f_compat i j h, rw [← int.coe_nat_pow, ← zmod.int_coe_zmod_eq_zero_iff_dvd, int.cast_sub], dsimp [nth_hom], rw [← f_compat, ring_hom.comp_apply], have : fact (p ^ i > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [zmod.cast_id, zmod.cast_hom_apply, sub_self, zmod.nat_cast_val], }, end lemma is_cau_seq_nth_hom (r : R): is_cau_seq (padic_norm p) (Ξ» n, nth_hom f r n) := begin intros Ξ΅ hΞ΅, obtain ⟨k, hk⟩ : βˆƒ k : β„•, (p ^ - (↑(k : β„•) : β„€) : β„š) < Ξ΅ := exists_pow_neg_lt_rat p hΞ΅, use k, intros j hj, refine lt_of_le_of_lt _ hk, norm_cast, rw ← padic_norm.dvd_iff_norm_le, exact_mod_cast pow_dvd_nth_hom_sub f_compat r k j hj end /-- `nth_hom_seq f_compat r` bundles `padic_int.nth_hom f r` as a Cauchy sequence of rationals with respect to the `p`-adic norm. The `n`th value of the sequence is `((f n r).val : β„š)`. -/ def nth_hom_seq (r : R) : padic_seq p := ⟨λ n, nth_hom f r n, is_cau_seq_nth_hom f_compat r⟩ lemma nth_hom_seq_one : nth_hom_seq f_compat 1 β‰ˆ 1 := begin intros Ξ΅ hΞ΅, change _ < _ at hΞ΅, use 1, intros j hj, haveI : fact (1 < p^j) := ⟨nat.one_lt_pow _ _ (by linarith) hp_prime.1.one_lt⟩, simp [nth_hom_seq, nth_hom, zmod.val_one, hΞ΅], end lemma nth_hom_seq_add (r s : R) : nth_hom_seq f_compat (r + s) β‰ˆ nth_hom_seq f_compat r + nth_hom_seq f_compat s := begin intros Ξ΅ hΞ΅, obtain ⟨n, hn⟩ := exists_pow_neg_lt_rat p hΞ΅, use n, intros j hj, dsimp [nth_hom_seq], apply lt_of_le_of_lt _ hn, rw [← int.cast_add, ← int.cast_sub, ← padic_norm.dvd_iff_norm_le, ← zmod.int_coe_zmod_eq_zero_iff_dvd], dsimp [nth_hom], have : fact (p ^ n > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [int.cast_coe_nat, int.cast_add, ring_hom.map_add, int.cast_sub, zmod.nat_cast_val] }, rw [zmod.cast_add (show p ^ n ∣ p ^ j, from _), sub_self], { apply_instance }, { apply pow_dvd_pow, linarith only [hj] }, end lemma nth_hom_seq_mul (r s : R) : nth_hom_seq f_compat (r * s) β‰ˆ nth_hom_seq f_compat r * nth_hom_seq f_compat s := begin intros Ξ΅ hΞ΅, obtain ⟨n, hn⟩ := exists_pow_neg_lt_rat p hΞ΅, use n, intros j hj, dsimp [nth_hom_seq], apply lt_of_le_of_lt _ hn, rw [← int.cast_mul, ← int.cast_sub, ← padic_norm.dvd_iff_norm_le, ← zmod.int_coe_zmod_eq_zero_iff_dvd], dsimp [nth_hom], have : fact (p ^ n > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [int.cast_coe_nat, int.cast_mul, int.cast_sub, ring_hom.map_mul, zmod.nat_cast_val] }, rw [zmod.cast_mul (show p ^ n ∣ p ^ j, from _), sub_self], { apply_instance }, { apply pow_dvd_pow, linarith only [hj] }, end /-- `lim_nth_hom f_compat r` is the limit of a sequence `f` of compatible ring homs `R β†’+* zmod (p^k)`. This is itself a ring hom: see `padic_int.lift`. -/ def lim_nth_hom (r : R) : β„€_[p] := of_int_seq (nth_hom f r) (is_cau_seq_nth_hom f_compat r) lemma lim_nth_hom_spec (r : R) : βˆ€ Ξ΅ : ℝ, 0 < Ξ΅ β†’ βˆƒ N : β„•, βˆ€ n β‰₯ N, βˆ₯lim_nth_hom f_compat r - nth_hom f r nβˆ₯ < Ξ΅ := begin intros Ξ΅ hΞ΅, obtain ⟨Ρ', hΞ΅'0, hΞ΅'⟩ : βˆƒ v : β„š, (0 : ℝ) < v ∧ ↑v < Ξ΅ := exists_rat_btwn hΞ΅, norm_cast at hΞ΅'0, obtain ⟨N, hN⟩ := padic_norm_e.defn (nth_hom_seq f_compat r) hΞ΅'0, use N, intros n hn, apply lt_trans _ hΞ΅', change ↑(padic_norm_e _) < _, norm_cast, convert hN _ hn, simp [nth_hom, lim_nth_hom, nth_hom_seq, of_int_seq], end lemma lim_nth_hom_zero : lim_nth_hom f_compat 0 = 0 := by simp [lim_nth_hom]; refl lemma lim_nth_hom_one : lim_nth_hom f_compat 1 = 1 := subtype.ext $ quot.sound $ nth_hom_seq_one _ lemma lim_nth_hom_add (r s : R) : lim_nth_hom f_compat (r + s) = lim_nth_hom f_compat r + lim_nth_hom f_compat s := subtype.ext $ quot.sound $ nth_hom_seq_add _ _ _ lemma lim_nth_hom_mul (r s : R) : lim_nth_hom f_compat (r * s) = lim_nth_hom f_compat r * lim_nth_hom f_compat s := subtype.ext $ quot.sound $ nth_hom_seq_mul _ _ _ -- TODO: generalize this to arbitrary complete discrete valuation rings /-- `lift f_compat` is the limit of a sequence `f` of compatible ring homs `R β†’+* zmod (p^k)`, with the equality `lift f_compat r = padic_int.lim_nth_hom f_compat r`. -/ def lift : R β†’+* β„€_[p] := { to_fun := lim_nth_hom f_compat, map_one' := lim_nth_hom_one f_compat, map_mul' := lim_nth_hom_mul f_compat, map_zero' := lim_nth_hom_zero f_compat, map_add' := lim_nth_hom_add f_compat } omit f_compat lemma lift_sub_val_mem_span (r : R) (n : β„•) : (lift f_compat r - (f n r).val) ∈ (ideal.span {↑p ^ n} : ideal β„€_[p]) := begin obtain ⟨k, hk⟩ := lim_nth_hom_spec f_compat r _ (show (0 : ℝ) < p ^ (-n : β„€), from nat.zpow_pos_of_pos hp_prime.1.pos _), have := le_of_lt (hk (max n k) (le_max_right _ _)), rw norm_le_pow_iff_mem_span_pow at this, dsimp [lift], rw sub_eq_sub_add_sub (lim_nth_hom f_compat r) _ ↑(nth_hom f r (max n k)), apply ideal.add_mem _ _ this, rw [ideal.mem_span_singleton], simpa only [ring_hom.eq_int_cast, ring_hom.map_pow, int.cast_sub] using (int.cast_ring_hom β„€_[p]).map_dvd (pow_dvd_nth_hom_sub f_compat r n (max n k) (le_max_left _ _)), end /-- One part of the universal property of `β„€_[p]` as a projective limit. See also `padic_int.lift_unique`. -/ lemma lift_spec (n : β„•) : (to_zmod_pow n).comp (lift f_compat) = f n := begin ext r, haveI : fact (0 < p ^ n) := ⟨pow_pos hp_prime.1.pos n⟩, rw [ring_hom.comp_apply, ← zmod.nat_cast_zmod_val (f n r), ← map_nat_cast $ to_zmod_pow n, ← sub_eq_zero, ← ring_hom.map_sub, ← ring_hom.mem_ker, ker_to_zmod_pow], apply lift_sub_val_mem_span, end /-- One part of the universal property of `β„€_[p]` as a projective limit. See also `padic_int.lift_spec`. -/ lemma lift_unique (g : R β†’+* β„€_[p]) (hg : βˆ€ n, (to_zmod_pow n).comp g = f n) : lift f_compat = g := begin ext1 r, apply eq_of_forall_dist_le, intros Ξ΅ hΞ΅, obtain ⟨n, hn⟩ := exists_pow_neg_lt p hΞ΅, apply le_trans _ (le_of_lt hn), rw [dist_eq_norm, norm_le_pow_iff_mem_span_pow, ← ker_to_zmod_pow, ring_hom.mem_ker, ring_hom.map_sub, ← ring_hom.comp_apply, ← ring_hom.comp_apply, lift_spec, hg, sub_self], end @[simp] lemma lift_self (z : β„€_[p]) : @lift p _ β„€_[p] _ to_zmod_pow zmod_cast_comp_to_zmod_pow z = z := begin show _ = ring_hom.id _ z, rw @lift_unique p _ β„€_[p] _ _ zmod_cast_comp_to_zmod_pow (ring_hom.id β„€_[p]), intro, rw ring_hom.comp_id, end end lift lemma ext_of_to_zmod_pow {x y : β„€_[p]} : (βˆ€ n, to_zmod_pow n x = to_zmod_pow n y) ↔ x = y := begin split, { intro h, rw [← lift_self x, ← lift_self y], simp [lift, lim_nth_hom, nth_hom, h] }, { rintro rfl _, refl } end lemma to_zmod_pow_eq_iff_ext {R : Type*} [non_assoc_semiring R] {g g' : R β†’+* β„€_[p]} : (βˆ€ n, (to_zmod_pow n).comp g = (to_zmod_pow n).comp g') ↔ g = g' := begin split, { intro hg, ext x : 1, apply ext_of_to_zmod_pow.mp, intro n, show (to_zmod_pow n).comp g x = (to_zmod_pow n).comp g' x, rw hg n }, { rintro rfl _, refl } end end padic_int
d7cca1c4f8b6a58f4f5e64e17550d5a89b5403ee
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/tactic/library_search.lean
d59a66fddebd556bc1295bd2a88a314dff1690c5
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
4,653
lean
-- Copyright (c) 2019 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import tactic.basic import data.list.defs /- A basic `library_search` tactic. -/ namespace tactic open native namespace library_search meta def head_symbol : expr β†’ name | (expr.pi _ _ _ t) := head_symbol t | (expr.app f _) := head_symbol f | (expr.const n _) := -- TODO this is a hack; if you suspect more cases here would help, please report them match n with | `gt := `has_lt.lt | `ge := `has_le.le | _ := n end | _ := `_ inductive head_symbol_match | ex | mp | mpr | both open head_symbol_match def head_symbol_match.to_string : head_symbol_match β†’ string | ex := "exact" | mp := "iff.mp" | mpr := "iff.mpr" | both := "iff.mp and iff.mpr" meta def unfold_head_symbol : name β†’ list name | `false := [`not, `false] | n := [n] meta def match_head_symbol (hs : name) : expr β†’ option head_symbol_match | (expr.pi _ _ _ t) := match_head_symbol t | `(%%a ↔ %%b) := if `iff = hs then some ex else match (match_head_symbol a, match_head_symbol b) with | (some ex, some ex) := some both | (some ex, _) := some mpr | (_, some ex) := some mp | _ := none end | (expr.app f _) := match_head_symbol f | (expr.const n _) := if list.mem hs (unfold_head_symbol n) then some ex else none | _ := none meta structure decl_data := (d : declaration) (n : name) (m : head_symbol_match) (l : β„•) -- cached length of name -- We used to check here for private declarations, or declarations with certain suffixes. -- It turns out `apply` is so fast, it's better to just try them all. meta def process_declaration (hs : name) (d : declaration) : option decl_data := let n := d.to_name in if Β¬ d.is_trusted ∨ n.is_internal then none else (Ξ» m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type /-- Retrieve all library definitions with a given head symbol. -/ meta def library_defs (hs : name) : tactic (list decl_data) := do env ← get_env, return $ env.decl_filter_map (process_declaration hs) meta def apply_and_solve (e : expr) := apply e >> (done <|> solve_by_elim { all_goals := tt }) meta def apply_declaration (d : decl_data) : tactic unit := do e ← mk_const d.n, let t := d.d.type, match d.m with | ex := apply_and_solve e | mp := do l ← iff_mp_core e t, apply_and_solve l | mpr := do l ← iff_mpr_core e t, apply_and_solve l | both := do l ← iff_mp_core e t, apply_and_solve l <|> do l ← iff_mpr_core e t, apply_and_solve l end end library_search open library_search open library_search.head_symbol_match declare_trace silence_library_search -- Turn off `exact ...` trace message declare_trace library_search -- Trace a list of all relevant lemmas meta def library_search : tactic string := do [g] ← get_goals | fail "`library_search` should be called with exactly one goal", t ← infer_type g, -- Collect all definitions with the correct head symbol defs ← library_defs (head_symbol t), -- Sort by length; people like short proofs let defs := defs.qsort(Ξ» d₁ dβ‚‚, d₁.l ≀ dβ‚‚.l), when (is_trace_enabled_for `library_search) $ (do trace format!"Found {defs.length} relevant lemmas:", trace $ defs.map (Ξ» ⟨d, n, m, l⟩, (n, m.to_string))), -- Try `apply` followed by `solve_by_elim`, for each definition. defs.mfirst apply_declaration, -- If something worked, prepare a string to print. p ← instantiate_mvars g >>= head_beta >>= pp, let r := format!"exact {p}", when (Β¬ is_trace_enabled_for `silence_library_search) $ tactic.trace r, return $ to_string r namespace interactive /-- `library_search` attempts to apply every definition in the library whose head symbol matches the goal, and then discharge any new goals using `solve_by_elim`. If it succeeds, it prints a trace message `exact ...` which can replace the invocation of `library_search`. -/ meta def library_search := tactic.library_search end interactive @[hole_command] meta def library_search_hole_cmd : hole_command := { name := "library_search", descr := "Use `library_search` to complete the goal.", action := Ξ» _, do script ← library_search, -- Is there a better API for dropping the 'exact ' prefix on this string? return [((script.mk_iterator.remove 6).to_string, "by library_search")] } end tactic
f07de0c29b27ff6e4bb792d736b3f49a95c1195c
f00cc9c04d77f9621aa57d1406d35c522c3ff82c
/library/init/meta/interactive.lean
ab798efe33bf0a63704c5c2babe6a42de5478bcf
[ "Apache-2.0" ]
permissive
shonfeder/lean
444c66a74676d74fb3ef682d88cd0f5c1bf928a5
24d5a1592d80cefe86552d96410c51bb07e6d411
refs/heads/master
1,619,338,440,905
1,512,842,340,000
1,512,842,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
58,593
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.tactic init.meta.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.category.combinators import init.meta.interactive_base init.meta.derive init.meta.match_tactic open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /- allows metavars -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt /- allow metavars and no subgoals -/ meta def i_to_expr_no_subgoals (q : pexpr) : tactic expr := to_expr q tt ff /- doesn't allows metavars -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff /- Auxiliary version of i_to_expr for apply-like tactics. This is a workaround for comment https://github.com/leanprover/lean/issues/1342#issuecomment-307912291 at issue #1342. In interactive mode, given a tactic apply f we want the apply tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. Before we added `i_to_expr_for_apply`, the tactic apply le_antisymm would first elaborate `le_antisymm`, and create @le_antisymm ?m_1 ?m_2 ?m_3 ?m_4 The type class resolution problem ?m_2 : weak_order ?m_1 by the elaborator since ?m_1 is not assigned yet, and the problem is discarded. Then, we would invoke `apply_core`, which would create two new metavariables for the explicit arguments, and try to unify the resulting type with the current target. After the unification, the metavariables ?m_1, ?m_3 and ?m_4 are assigned, but we lost the information about the pending type class resolution problem. With `i_to_expr_for_apply`, `le_antisymm` is elaborate into `@le_antisymm`, the apply_core tactic creates all metavariables, and solves the ones that can be solved by type class resolution. Another possible fix: we modify the elaborator to return pending type class resolution problems, and store them in the tactic_state. -/ meta def i_to_expr_for_apply (q : pexpr) : tactic expr := let aux (n : name) : tactic expr := do p ← resolve_name n, match p with | (expr.const c []) := do r ← mk_const c, save_type_info r q, return r | _ := i_to_expr p end in match q with | (expr.const c []) := aux c | (expr.local_const c _ _ _) := aux c | _ := i_to_expr q end namespace interactive open interactive interactive.types expr /-- itactic: parse a nested "interactive" tactic. That is, parse `{` tactic `}` -/ meta def itactic : Type := tactic unit /-- If the current goal is a Pi/forall `βˆ€ x : t, u` (resp. `let x := t in u`) then `intro` puts `x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`. If the goal is an arrow `t β†’ u`, then it puts `h : t` in the local context and the new goal target is `u`. If the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the tactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter case, the tactic fails. -/ meta def intro : parse ident_? β†’ tactic unit | none := intro1 >> skip | (some h) := tactic.intro h >> skip /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h₁ ... hβ‚™` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident_* β†’ tactic unit | [] := tactic.intros >> skip | hs := intro_lst hs >> skip /-- The tactic `introv` allows the user to automatically introduce the variables of a theorem and explicitly name the hypotheses involved. The given names are used to name non-dependent hypotheses. Examples: ``` example : βˆ€ a b : nat, a = b β†’ b = a := begin introv h, exact h.symm end ``` The state after `introv h` is ``` a b : β„•, h : a = b ⊒ b = a ``` ``` example : βˆ€ a b : nat, a = b β†’ βˆ€ c, b = c β†’ a = c := begin introv h₁ hβ‚‚, exact h₁.trans hβ‚‚ end ``` The state after `introv h₁ hβ‚‚` is ``` a b : β„•, h₁ : a = b, c : β„•, hβ‚‚ : b = c ⊒ a = c ``` -/ meta def introv (ns : parse ident_*) : tactic unit := tactic.introv ns >> return () /-- The tactic `rename h₁ hβ‚‚` renames hypothesis `h₁` to `hβ‚‚` in the current local context. -/ meta def rename : parse ident β†’ parse ident β†’ tactic unit := tactic.rename /-- The `apply` tactic tries to match the current goal against the conclusion of the type of term. The argument term should be a term well-formed in the local context of the main goal. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.apply /-- Similar to the `apply` tactic, but does not reorder goals. -/ meta def fapply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.fapply /-- Similar to the `apply` tactic, but only creates subgoals for non-dependent premises that have not been fixed by type inference or type class resolution. -/ meta def eapply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.eapply /-- Similar to the `apply` tactic, but allows the user to provide a `apply_cfg` configuration object. -/ meta def apply_with (q : parse parser.pexpr) (cfg : apply_cfg) : tactic unit := do e ← i_to_expr_for_apply q, tactic.apply e cfg /-- This tactic tries to close the main goal `... ⊒ t` by generating a term of type `t` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic behaves like `exact`, but with a big difference: the user can put underscores `_` in the expression as placeholders for holes that need to be filled, and `refine` will generate as many subgoals as there are holes. Note that some holes may be implicit. The type of each hole must either be synthesized by the system or declared by an explicit type ascription like `(_ : nat β†’ Prop)`. -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q /-- This tactic looks in the local context for a hypothesis whose type is equal to the goal target. If it finds one, it uses it to prove the goal, and otherwise it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- Try to apply `assumption` to all goals. -/ meta def assumption' : tactic unit := tactic.any_goals tactic.assumption private 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 /-- `change u` replaces the target `t` of the main goal to `u` provided that `t` is well formed with respect to the local context of the main goal and `t` and `u` are definitionally equal. `change u at h` will change a local hypothesis to `u`. `change t with u at h1 h2 ...` will replace `t` with `u` in all the supplied hypotheses (or `*`), or in the goal if no `at` clause is specified, provided that `t` and `u` are definitionally equal. -/ meta def change (q : parse texpr) : parse (tk "with" *> texpr)? β†’ parse location β†’ tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do u ← mk_meta_univ, ty ← mk_meta_var (sort u), eq ← i_to_expr ``(%%q : %%ty), ew ← i_to_expr ``(%%w : %%ty), let repl := Ξ»e : expr, e.replace (Ξ» a n, if a = eq then some ew else none), l.try_apply (Ξ»h, do e ← infer_type h, change_core (repl e) (some h)) (do g ← target, change_core (repl g) none) /-- This tactic provides an exact proof term to solve the main goal. If `t` is the goal and `p` is a term of type `u` then `exact p` succeeds if and only if `t` and `u` can be unified. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact /-- Like `exact`, but takes a list of terms and checks that all goals are discharged after the tactic. -/ meta def exacts : parse pexpr_list_or_texpr β†’ tactic unit | [] := done | (t :: ts) := exact t >> exacts ts /-- A synonym for `exact` that allows writing `have/suffices/show ..., from ...` in tactic mode. -/ meta def Β«fromΒ» := exact /-- `revert h₁ ... hβ‚™` applies to any goal with hypotheses `h₁` ... `hβ‚™`. It moves the hypotheses and their dependencies to the target of the goal. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := do hs ← mmap tactic.get_local ids, revert_lst hs, skip private meta def resolve_name' (n : name) : tactic expr := do { p ← resolve_name n, match p with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := i_to_expr p end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that no unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ meta def to_expr' (p : pexpr) : tactic expr := match p with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | _ := i_to_expr p end @[derive has_reflect] meta structure rw_rule := (pos : pos) (symm : bool) (rule : pexpr) meta def get_rule_eqn_lemmas (r : rw_rule) : tactic (list name) := let aux (n : name) : tactic (list name) := do { p ← resolve_name n, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := get_eqn_lemmas_for tt n | _ := return [] end } <|> return [] in match r.rule with | const n _ := aux n | local_const n _ _ _ := aux n | _ := return [] end private meta def rw_goal (cfg : rewrite_cfg) (rs : list rw_rule) : tactic unit := rs.mmap' $ Ξ» r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.mfirst $ Ξ» n, do e ← mk_const n, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.empty) private meta def uses_hyp (e : expr) (h : expr) : bool := e.fold ff $ Ξ» t _ r, r || to_bool (t = h) private meta def rw_hyp (cfg : rewrite_cfg) : list rw_rule β†’ expr β†’ tactic unit | [] hyp := skip | (r::rs) hyp := do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, when (not (uses_hyp e hyp)) $ rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.mfirst $ Ξ» n, do e ← mk_const n, rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.empty) meta def rw_rule_p (ep : parser pexpr) : parser rw_rule := rw_rule.mk <$> cur_pos <*> (option.is_some <$> (with_desc "←" (tk "←" <|> tk "<-"))?) <*> ep @[derive has_reflect] meta structure rw_rules_t := (rules : list rw_rule) (end_pos : option pos) -- accepts the same content as `pexpr_list_or_texpr`, but with correct goal info pos annotations meta def rw_rules : parser rw_rules_t := (tk "[" *> rw_rules_t.mk <$> sep_by (skip_info (tk ",")) (set_goal_info_pos $ rw_rule_p (parser.pexpr 0)) <*> (some <$> cur_pos <* set_goal_info_pos (tk "]"))) <|> rw_rules_t.mk <$> (list.ret <$> rw_rule_p texpr) <*> return none private meta def rw_core (rs : parse rw_rules) (loca : parse location) (cfg : rewrite_cfg) : tactic unit := match loca with | loc.wildcard := loca.try_apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) | _ := loca.apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) end >> try (reflexivity reducible) >> (returnopt rs.end_pos >>= save_info <|> skip) /-- `rewrite e` applies identity `e` as a rewrite rule to the target of the main goal. If `e` is preceded by left arrow (`←` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined constant, then the equational lemmas associated with `e` are used. This provides a convenient way to unfold `e`. `rewrite [e₁, ..., eβ‚™]` applies the given rules sequentially. `rewrite e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses in the local context. In the latter case, a turnstile `⊒` or `|-` can also be used, to signify the target of the goal. -/ meta def rewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rw_core q l cfg /-- An abbreviation for `rewrite`. -/ meta def rw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rw_core q l cfg /-- `rewrite` followed by `assumption`. -/ meta def rwa (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rewrite q l cfg >> try assumption /-- A variant of `rewrite` that uses the unifier more aggressively, unfolding semireducible definitions. -/ meta def erewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := rw_core q l cfg /-- An abbreviation for `erewrite`. -/ meta def erw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := rw_core q l cfg private meta def get_type_name (e : expr) : tactic name := do e_type ← infer_type e >>= whnf, (const I ls) ← return $ get_app_fn e_type, return I private meta def generalize_arg_p_aux : pexpr β†’ parser (pexpr Γ— name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def generalize_arg_p : parser (pexpr Γ— name) := with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux /-- `generalize : e = x` replaces all occurrences of `e` in the target with a new hypothesis `x` of the same type. `generalize h : e = x` in addition registers the hypothesis `h : e = x`. -/ meta def generalize (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) : tactic unit := do let (p, x) := p, e ← i_to_expr p, some h ← pure h | tactic.generalize e x >> intro1 >> skip, tgt ← target, -- if generalizing fails, fall back to not replacing anything tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize e x >> target), to_expr ``(Ξ  x, %%e = x β†’ %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(Ξ  x, %%e = x β†’ %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h meta def cases_arg_p : parser (option name Γ— pexpr) := with_desc "(id :)? expr" $ do t ← texpr, match t with | (local_const x _ _ _) := (tk ":" *> do t ← texpr, pure (some x, t)) <|> pure (none, t) | _ := pure (none, t) end precedence `generalizing` : 0 /-- Assuming `x` is a variable in the local context with an inductive type, `induction x` applies induction on `x` to the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor and an inductive hypothesis is added for each recursive argument to the constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the inductive hypothesis incorporates that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `induction n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypotheses `h : P (nat.succ a)` and `ih₁ : P a β†’ Q a` and target `Q (nat.succ a)`. Here the names `a` and `ih₁` ire chosen automatically. `induction e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then performs induction on the resulting variable. `induction e with y₁ ... yβ‚™`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yβ‚™` should be used for the arguments to the constructors and inductive hypotheses, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. Note that for long sequences of names, the `case` tactic provides a more convenient naming mechanism. `induction e using r` allows the user to specify the principle of induction that should be used. Here `r` should be a theorem whose result type must be of the form `C t`, where `C` is a bound variable and `t` is a (possibly empty) sequence of bound variables `induction e generalizing z₁ ... zβ‚™`, where `z₁ ... zβ‚™` are variables in the local context, generalizes over `z₁ ... zβ‚™` before applying the induction but then introduces them in each goal. In other words, the net effect is that each inductive hypothesis is generalized. `induction h : t` will introduce an equality of the form `h : t = C x y`, asserting that the input term is equal to the current constructor case, to the context. -/ meta def induction (hp : parse cases_arg_p) (rec_name : parse using_ident) (ids : parse with_ident_list) (revert : parse $ (tk "generalizing" *> ident*)?) : tactic unit := do -- process `h : t` case e ← match hp with | (some h, p) := do x ← mk_fresh_name, generalize h () (p, x), get_local x | (none, p) := i_to_expr p end, -- generalize major premise e ← if e.is_local_constant then pure e else tactic.generalize e >> intro1, -- generalize major premise args (e, newvars, locals) ← do { none ← pure rec_name | pure (e, [], []), t ← infer_type e, t ← whnf_ginductive t, const n _ ← pure t.get_app_fn | pure (e, [], []), env ← get_env, tt ← pure $ env.is_inductive n | pure (e, [], []), let (locals, nonlocals) := (t.get_app_args.drop $ env.inductive_num_params n).partition (Ξ» arg : expr, arg.is_local_constant), _ :: _ ← pure nonlocals | pure (e, [], []), n ← tactic.revert e, newvars ← nonlocals.mmap $ Ξ» arg, do { n ← revert_kdeps arg, tactic.generalize arg, h ← intro1, intron n, -- now try to clear hypotheses that may have been abstracted away let locals := arg.fold [] (Ξ» e _ acc, if e.is_local_constant then e::acc else acc), locals.mmap' (try ∘ clear), pure h }, intron (n-1), e ← intro1, pure (e, newvars, locals) }, -- revert `generalizing` params n ← mmap tactic.get_local (revert.get_or_else []) >>= revert_lst, tactic.induction e ids rec_name, all_goals $ do { intron n, clear_lst (newvars.map local_pp_name), (e::locals).mmap' (try ∘ clear) } private meta def find_case (goals : list expr) (ty : name) (idx : nat) (num_indices : nat) : option expr β†’ expr β†’ option (expr Γ— expr) | case e := if e.has_meta_var then match e with | (mvar _ _ _) := do case ← case, guard $ e ∈ goals, pure (case, e) | (app _ _) := let idx := match e.get_app_fn with | const (name.mk_string rec ty') _ := guard (ty' = ty) >> match mk_simple_name rec with | `drec := some idx | `rec := some idx -- indices + major premise | `dcases_on := some (idx + num_indices + 1) | `cases_on := some (idx + num_indices + 1) | _ := none end | _ := none end in match idx with | none := list.foldl (<|>) (find_case case e.get_app_fn) $ e.get_app_args.map (find_case case) | some idx := let args := e.get_app_args in do arg ← args.nth idx, args.enum.foldl (Ξ» acc ⟨i, arg⟩, match acc with | some _ := acc | _ := if i β‰  idx then find_case none arg else none end) -- start recursion with likely case (find_case (some arg) arg) end | (lam _ _ _ e) := find_case case e | (macro n args) := list.foldl (<|>) none $ args.map (find_case case) | _ := none end else none private meta def rename_lams : expr β†’ list name β†’ tactic unit | (lam n _ _ e) (n'::ns) := (rename n n' >> rename_lams e ns) <|> rename_lams e (n'::ns) | _ _ := skip /-- Focuses on the `induction`/`cases` subgoal corresponding to the given introduction rule, optionally renaming introduced locals. ``` example (n : β„•) : n = n := begin induction n, case nat.zero { reflexivity }, case nat.succ a ih { reflexivity } end ``` -/ meta def case (ctor : parse ident) (ids : parse ident_*) (tac : itactic) : tactic unit := do r ← result, env ← get_env, ctor ← resolve_constant ctor <|> fail ("'" ++ to_string ctor ++ "' is not a constructor"), ty ← (env.inductive_type_of ctor).to_monad <|> fail ("'" ++ to_string ctor ++ "' is not a constructor"), let ctors := env.constructors_of ty, let idx := env.inductive_num_params ty + /- motive -/ 1 + list.index_of ctor ctors, gs ← get_goals, (case, g) ← (find_case gs ty idx (env.inductive_num_indices ty) none r ).to_monad <|> fail "could not find open goal of given case", set_goals $ g :: gs.filter (β‰  g), rename_lams case ids, solve1 tac /-- Apply `t` to main goal, and revert any new hypothesis in the generated goals. This tactic is useful for writing robust proof scripts that are not sensitive to the name generation strategy used by `t`. ``` example (n : β„•) : n = n := begin guard_names { induction n }, { reflexivity }, { intros n' ih, reflexivity } end ``` -/ meta def guard_names : itactic β†’ tactic unit := tactic.guard_names /-- Assuming `x` is a variable in the local context with an inductive type, `destruct x` splits the main goal, producing one goal for each constructor of the inductive type, in which `x` is assumed to be a general instance of that constructor. In contrast to `cases`, the local context is unchanged, i.e. no elements are reverted or introduced. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `destruct n` produces one goal with target `n = 0 β†’ Q n`, and one goal with target `βˆ€ (a : β„•), (Ξ» (w : β„•), n = w β†’ Q n) (nat.succ a)`. Here the name `a` is chosen automatically. -/ meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct /-- Assuming `x` is a variable in the local context with an inductive type, `cases x` splits the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the case split affects that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `cases n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypothesis `h : P (nat.succ a)` and target `Q (nat.succ a)`. Here the name `a` is chosen automatically. `cases e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then cases on the resulting variable. `cases e with y₁ ... yβ‚™`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yβ‚™` should be used for the arguments to the constructors, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. `cases h : e`, where `e` is a variable or an expression, performs cases on `e` as above, but also adds a hypothesis `h : e = ...` to each hypothesis, where `...` is the constructor instance for that particular case. -/ meta def cases : parse cases_arg_p β†’ parse with_ident_list β†’ tactic unit | (none, p) ids := do e ← i_to_expr p, tactic.cases e ids | (some h, p) ids := do x ← mk_fresh_name, generalize h () (p, x), hx ← get_local x, tactic.cases hx ids private meta def find_matching_hyp (ps : list pattern) : tactic expr := any_hyp $ Ξ» h, do type ← infer_type h, ps.mfirst $ Ξ» p, do match_pattern p type, return h /-- `cases_matching p` applies the `cases` tactic to a hypothesis `h : type` if `type` matches the pattern `p`. `cases_matching [p_1, ..., p_n]` applies the `cases` tactic to a hypothesis `h : type` if `type` matches one of the given patterns. `cases_matching* p` more efficient and compact version of `focus1 { repeat { cases_matching p } }`. It is more efficient because the pattern is compiled once. Example: The following tactic destructs all conjunctions and disjunctions in the current goal. ``` cases_matching* [_ ∨ _, _ ∧ _] ``` -/ meta def cases_matching (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := do ps ← ps.mmap pexpr_to_pattern, if rec.is_none then find_matching_hyp ps >>= tactic.cases else tactic.focus1 $ tactic.repeat $ find_matching_hyp ps >>= tactic.cases /-- Shorthand for `cases_matching` -/ meta def casesm (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := cases_matching rec ps private meta def try_cases_for_types (type_names : list name) (at_most_one : bool) : tactic unit := any_hyp $ Ξ» h, do I ← expr.get_app_fn <$> (infer_type h >>= head_beta), guard I.is_constant, guard (I.const_name ∈ type_names), tactic.focus1 (tactic.cases h >> if at_most_one then do n ← num_goals, guard (n <= 1) else skip) /-- `cases_type I` applies the `cases` tactic to a hypothesis `h : (I ...)` `cases_type I_1 ... I_n` applies the `cases` tactic to a hypothesis `h : (I_1 ...)` or ... or `h : (I_n ...)` `cases_type* I` is shorthand for `focus1 { repeat { cases_type I } }` `cases_type! I` only applies `cases` if the number of resulting subgoals is <= 1. Example: The following tactic destructs all conjunctions and disjunctions in the current goal. ``` cases_type* or and ``` -/ meta def cases_type (one : parse $ (tk "!")?) (rec : parse $ (tk "*")?) (type_names : parse ident*) : tactic unit := do type_names ← type_names.mmap resolve_constant, if rec.is_none then try_cases_for_types type_names (bnot one.is_none) else tactic.focus1 $ tactic.repeat $ try_cases_for_types type_names (bnot one.is_none) /-- Tries to solve the current goal using a canonical proof of `true`, or the `reflexivity` tactic, or the `contradiction` tactic. -/ meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using `sorry`. -/ meta def admit : tactic unit := tactic.admit /-- The contradiction tactic attempts to find in the current local context a hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction /-- `iterate { t }` repeatedly applies tactic `t` until `t` fails. `iterate { t }` always succeeds. `iterate n { t }` applies `t` `n` times. -/ meta def iterate (n : parse small_nat?) (t : itactic) : tactic unit := match n with | none := tactic.iterate t | some n := iterate_exactly n t end /-- `repeat { t }` applies `t` to each goal. If the application succeeds, the tactic is applied recursively to all the generated subgoals until it eventually fails. The recursion stops in a subgoal when the tactic has failed to make progress. The tactic `repeat { t }` never fails. -/ meta def repeat : itactic β†’ tactic unit := tactic.repeat /-- `try { t }` tries to apply tactic `t`, but succeeds whether or not `t` succeeds. -/ meta def try : itactic β†’ tactic unit := tactic.try /-- A do-nothing tactic that always succeeds. -/ meta def skip : tactic unit := tactic.skip /-- `solve1 { t }` applies the tactic `t` to the main goal and fails if it is not solved. -/ meta def solve1 : itactic β†’ tactic unit := tactic.solve1 /-- `abstract id { t }` tries to use tactic `t` to solve the main goal. If it succeeds, it abstracts the goal as an independent definition or theorem with name `id`. If `id` is omitted, a name is generated automatically. -/ meta def abstract (id : parse ident?) (tac : itactic) : tactic unit := tactic.abstract tac id /-- `all_goals { t }` applies the tactic `t` to every goal, and succeeds if each application succeeds. -/ meta def all_goals : itactic β†’ tactic unit := tactic.all_goals /-- `any_goals { t }` applies the tactic `t` to every goal, and succeeds if at least one application succeeds. -/ meta def any_goals : itactic β†’ tactic unit := tactic.any_goals /-- `focus { t }` temporarily hides all goals other than the first, applies `t`, and then restores the other goals. It fails if there are no goals. -/ meta def focus (tac : itactic) : tactic unit := tactic.focus1 tac private meta def assume_core (n : name) (ty : pexpr) := do t ← target, when (not $ t.is_pi ∨ t.is_let) whnf_target, t ← target, when (not $ t.is_pi ∨ t.is_let) $ fail "assume tactic failed, Pi/let expression expected", ty ← i_to_expr ty, unify ty t.binding_domain, intro_core n >> skip /-- Assuming the target of the goal is a Pi or a let, `assume h : t` unifies the type of the binder with `t` and introduces it with name `h`, just like `intro h`. If `h` is absent, the tactic uses the name `this`. If `t` is omitted, it will be inferred. `assume (h₁ : t₁) ... (hβ‚™ : tβ‚™)` introduces multiple hypotheses. Any of the types may be omitted, but the names must be present. -/ meta def Β«assumeΒ» : parse (sum.inl <$> (tk ":" *> texpr) <|> sum.inr <$> parse_binders tac_rbp) β†’ tactic unit | (sum.inl ty) := assume_core `this ty | (sum.inr binders) := binders.mmap' $ Ξ» b, assume_core b.local_pp_name b.local_type /-- `have h : t := p` adds the hypothesis `h : t` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `have h : t` adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def Β«haveΒ» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (qβ‚‚ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, qβ‚‚ with | some e, some p := do t ← i_to_expr e, v ← i_to_expr ``(%%p : %%t), tactic.assertv h t v | none, some p := do p ← i_to_expr p, tactic.note h none p | some e, none := i_to_expr e >>= tactic.assert h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.assert h e end >> skip /-- `let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def Β«letΒ» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (qβ‚‚ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, qβ‚‚ with | some e, some p := do t ← i_to_expr e, v ← i_to_expr ``(%%p : %%t), tactic.definev h t v | none, some p := do p ← i_to_expr p, tactic.pose h none p | some e, none := i_to_expr e >>= tactic.define h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.define h e end >> skip /-- `suffices h : t` is the same as `have h : t, tactic.swap`. In other words, it adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. -/ meta def Β«sufficesΒ» (h : parse ident?) (t : parse (tk ":" *> texpr)?) : tactic unit := Β«haveΒ» h t none >> tactic.swap /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {Ξ± : Type} [has_to_tactic_format Ξ±] (a : Ξ±) : tactic unit := tactic.trace a /-- `existsi e` will instantiate an existential quantifier in the target with `e` and leave the instantiated body as the new target. More generally, it applies to any inductive type with one constructor and at least two arguments, applying the constructor with `e` as the first argument and leaving the remaining arguments as goals. `existsi [e₁, ..., eβ‚™]` iteratively does the same for each expression in the list. -/ meta def existsi : parse pexpr_list_or_texpr β†’ tactic unit | [] := return () | (p::ps) := i_to_expr p >>= tactic.existsi >> existsi ps /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := tactic.constructor /-- Similar to `constructor`, but only non-dependent premises are added as new goals. -/ meta def econstructor : tactic unit := tactic.econstructor /-- Applies the first constructor when the type of the target is an inductive data type with two constructors. -/ meta def left : tactic unit := tactic.left /-- Applies the second constructor when the type of the target is an inductive data type with two constructors. -/ meta def right : tactic unit := tactic.right /-- Applies the constructor when the type of the target is an inductive data type with one constructor. -/ meta def split : tactic unit := tactic.split private meta def constructor_matching_aux (ps : list pattern) : tactic unit := do t ← target, ps.mfirst (Ξ» p, match_pattern p t), constructor meta def constructor_matching (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := do ps ← ps.mmap pexpr_to_pattern, if rec.is_none then constructor_matching_aux ps else tactic.focus1 $ tactic.repeat $ constructor_matching_aux ps /-- Replaces the target of the main goal by `false`. -/ meta def exfalso : tactic unit := tactic.exfalso /-- The `injection` tactic is based on the fact that constructors of inductive data types are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c tβ‚‚)` are two terms that are equal then `t₁` and `tβ‚‚` are equal too. If `q` is a proof of a statement of conclusion `t₁ = tβ‚‚`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `tβ‚‚` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `tβ‚‚` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds two new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ hβ‚‚` uses the names `h₁` and `hβ‚‚` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs, try assumption /-- `injections with h₁ ... hβ‚™` iteratively applies `injection` to hypotheses using the names `h₁ ... hβ‚™`. -/ meta def injections (hs : parse with_ident_list) : tactic unit := do tactic.injections_with hs, try assumption end interactive meta structure simp_config_ext extends simp_config := (discharger : tactic unit := failed) section mk_simp_set open expr interactive.types @[derive has_reflect] meta inductive simp_arg_type : Type | all_hyps : simp_arg_type | except : name β†’ simp_arg_type | expr : pexpr β†’ simp_arg_type meta def simp_arg : parser simp_arg_type := (tk "*" *> return simp_arg_type.all_hyps) <|> (tk "-" *> simp_arg_type.except <$> ident) <|> (simp_arg_type.expr <$> texpr) meta def simp_arg_list : parser (list simp_arg_type) := (tk "*" *> return [simp_arg_type.all_hyps]) <|> list_of simp_arg <|> return [] private meta def resolve_exception_ids (all_hyps : bool) : list name β†’ list name β†’ list name β†’ tactic (list name Γ— list name) | [] gex hex := return (gex.reverse, hex.reverse) | (id::ids) gex hex := do p ← resolve_name id, let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := resolve_exception_ids ids (n::gex) hex | local_const n _ _ _ := when (not all_hyps) (fail $ sformat! "invalid local exception {id}, '*' was not used") >> resolve_exception_ids ids gex (n::hex) | _ := fail $ sformat! "invalid exception {id}, unknown identifier" end /- Return (hs, gex, hex, all) -/ meta def decode_simp_arg_list (hs : list simp_arg_type) : tactic $ list pexpr Γ— list name Γ— list name Γ— bool := do let (hs, ex, all) := hs.foldl (Ξ» r h, match r, h with | (es, ex, all), simp_arg_type.all_hyps := (es, ex, tt) | (es, ex, all), simp_arg_type.except id := (es, id::ex, all) | (es, ex, all), simp_arg_type.expr e := (e::es, ex, all) end) ([], [], ff), (gex, hex) ← resolve_exception_ids all ex [] [], return (hs.reverse, gex, hex, all) private meta def add_simps : simp_lemmas β†’ list name β†’ tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s.add_simp n, add_simps s' ns private meta def report_invalid_simp_lemma {Ξ± : Type} (n : name): tactic Ξ± := fail format!"invalid simplification lemma '{n}' (use command 'set_option trace.simp_lemmas true' for more details)" private meta def check_no_overload (p : pexpr) : tactic unit := when p.is_choice_macro $ match p with | macro _ ps := fail $ to_fmt "ambiguous overload, possible interpretations" ++ format.join (ps.map (Ξ» p, (to_fmt p).indent 4)) | _ := failed end private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (u : list name) (n : name) (ref : pexpr) : tactic (simp_lemmas Γ— list name) := do p ← resolve_name n, check_no_overload p, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := (do b ← is_valid_simp_lemma_cnst n, guard b, save_const_type_info n ref, s ← s.add_simp n, return (s, u)) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns.length > 0), save_const_type_info n ref, s ← add_simps s eqns, return (s, u)) <|> (do env ← get_env, guard (env.is_projection n).is_some, return (s, n::u)) <|> report_invalid_simp_lemma n | _ := (do e ← i_to_expr_no_subgoals p, b ← is_valid_simp_lemma e, guard b, try (save_type_info e ref), s ← s.add e, return (s, u)) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (u : list name) (p : pexpr) : tactic (simp_lemmas Γ— list name) := match p with | (const c []) := simp_lemmas.resolve_and_add s u c p | (local_const c _ _ _) := simp_lemmas.resolve_and_add s u c p | _ := do new_e ← i_to_expr_no_subgoals p, s ← s.add new_e, return (s, u) end private meta def simp_lemmas.append_pexprs : simp_lemmas β†’ list name β†’ list pexpr β†’ tactic (simp_lemmas Γ— list name) | s u [] := return (s, u) | s u (l::ls) := do (s, u) ← simp_lemmas.add_pexpr s u l, simp_lemmas.append_pexprs s u ls meta def mk_simp_set_core (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) (at_star : bool) : tactic (bool Γ— simp_lemmas Γ— list name) := do (hs, gex, hex, all_hyps) ← decode_simp_arg_list hs, when (all_hyps ∧ at_star ∧ not hex.empty) $ fail "A tactic of the form `simp [*, -h] at *` is currently not supported", s ← join_user_simp_lemmas no_dflt attr_names, (s, u) ← simp_lemmas.append_pexprs s [] hs, s ← if not at_star ∧ all_hyps then do ctx ← collect_ctx_simps, let ctx := ctx.filter (Ξ» h, h.local_uniq_name βˆ‰ hex), -- remove local exceptions s.append ctx else return s, -- add equational lemmas, if any gex ← gex.mmap (Ξ» n, list.cons n <$> get_eqn_lemmas_for tt n), return (all_hyps, simp_lemmas.erase s $ gex.join, u) meta def mk_simp_set (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) : tactic (simp_lemmas Γ— list name) := prod.snd <$> (mk_simp_set_core no_dflt attr_names hs ff) end mk_simp_set namespace interactive open interactive interactive.types expr meta def simp_core_aux (cfg : simp_config) (discharger : tactic unit) (s : simp_lemmas) (u : list name) (hs : list expr) (tgt : bool) : tactic unit := do to_remove ← hs.mfilter $ Ξ» h, do { h_type ← infer_type h, (do (new_h_type, pr) ← simplify s u h_type cfg `eq discharger, assert h.local_pp_name new_h_type, mk_eq_mp pr h >>= tactic.exact >> return tt) <|> (return ff) }, goal_simplified ← if tgt then (simp_target s u cfg discharger >> return tt) <|> (return ff) else return ff, guard (cfg.fail_if_unchanged = ff ∨ to_remove.length > 0 ∨ goal_simplified) <|> fail "simplify tactic failed to simplify", to_remove.mmap' (Ξ» h, try (clear h)) meta def simp_core (cfg : simp_config) (discharger : tactic unit) (no_dflt : bool) (hs : list simp_arg_type) (attr_names : list name) (locat : loc) : tactic unit := match locat with | loc.wildcard := do (all_hyps, s, u) ← mk_simp_set_core no_dflt attr_names hs tt, if all_hyps then tactic.simp_all s u cfg discharger else do hyps ← non_dep_prop_hyps, simp_core_aux cfg discharger s u hyps tt | _ := do (s, u) ← mk_simp_set no_dflt attr_names hs, ns ← locat.get_locals, simp_core_aux cfg discharger s u ns locat.include_goal end >> try tactic.triv >> try (tactic.reflexivity reducible) /-- The `simp` tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. `simp [h₁ hβ‚‚ ... hβ‚™]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `hα΅’`'s, where the `hα΅’`'s are expressions. If an `hα΅’` is a defined constant `f`, then the equational lemmas associated with `f` are used. This provides a convenient way to unfold `f`. `simp [*]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and all hypotheses. `simp *` is a shorthand for `simp [*]`. `simp only [h₁ hβ‚‚ ... hβ‚™]` is like `simp [h₁ hβ‚‚ ... hβ‚™]` but does not use `[simp]` lemmas `simp [-id_1, ... -id_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `idα΅’`. `simp at h₁ hβ‚‚ ... hβ‚™` simplifies the non-dependent hypotheses `h₁ : T₁` ... `hβ‚™ : Tβ‚™`. The tactic fails if the target or another hypothesis depends on one of them. The token `⊒` or `|-` can be added to the list to include the target. `simp at *` simplifies all the hypotheses and the target. `simp * at *` simplifies target and all (non-dependent propositional) hypotheses using the other hypotheses. `simp with attr₁ ... attrβ‚™` simplifies the main goal target using the lemmas tagged with any of the attributes `[attr₁]`, ..., `[attrβ‚™]` or `[simp]`. -/ meta def simp (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : simp_config_ext := {}) : tactic unit := simp_core cfg.to_simp_config cfg.discharger no_dflt hs attr_names locat /-- Just construct the simp set and trace it. Used for debugging. -/ meta def trace_simp_set (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) : tactic unit := do (s, _) ← mk_simp_set no_dflt attr_names hs, s.pp >>= trace /-- `simp_intros h₁ hβ‚‚ ... hβ‚™` is similar to `intros h₁ hβ‚‚ ... hβ‚™` except that each hypothesis is simplified as it is introduced, and each introduced hypothesis is used to simplify later ones and the final target. As with `simp`, a list of simplification lemmas can be provided. The modifiers `only` and `with` behave as with `simp`. -/ meta def simp_intros (ids : parse ident_*) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (cfg : simp_intros_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names hs, when (Β¬u.empty) (fail (sformat! "simp_intros tactic does not support {u}")), tactic.simp_intros s u ids cfg, try triv >> try (reflexivity reducible) private meta def to_simp_arg_list (es : list pexpr) : list simp_arg_type := es.map simp_arg_type.expr /-- `dsimp` is similar to `simp`, except that it only uses definitional equalities. -/ meta def dsimp (no_dflt : parse only_flag) (es : parse simp_arg_list) (attr_names : parse with_ident_list) (l : parse location) (cfg : dsimp_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names es, match l with | loc.wildcard := do ls ← local_context, n ← revert_lst ls, dsimp_target s u cfg, intron n | _ := l.apply (Ξ» h, dsimp_hyp h s u cfg) (dsimp_target s u cfg) end /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation, that is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a symmetric relation, that is, a relation which has a symmetry lemma tagged with the attribute `[symm]`. It replaces the target with `u ~ t`. -/ meta def symmetry : tactic unit := tactic.symmetry /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a transitive relation, that is, a relation which has a transitivity lemma tagged with the attribute `[trans]`. `transitivity s` replaces the goal with the two subgoals `t ~ s` and `s ~ u`. If `s` is omitted, then a metavariable is used instead. -/ meta def transitivity (q : parse texpr?) : tactic unit := tactic.transitivity >> match q with | none := skip | some q := do (r, lhs, rhs) ← target_lhs_rhs, i_to_expr q >>= unify rhs end /-- Proves a goal with target `s = t` when `s` and `t` are equal up to the associativity and commutativity of their binary operations. -/ meta def ac_reflexivity : tactic unit := tactic.ac_refl /-- An abbreviation for `ac_reflexivity`. -/ meta def ac_refl : tactic unit := tactic.ac_refl /-- Tries to prove the main goal using congruence closure. -/ meta def cc : tactic unit := tactic.cc /-- Given hypothesis `h : x = t` or `h : t = x`, where `x` is a local constant, `subst h` substitutes `x` by `t` everywhere in the main goal and then clears `h`. -/ meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) /-- `clear h₁ ... hβ‚™` tries to clear each hypothesis `hα΅’` from the local context. -/ meta def clear : parse ident* β†’ tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name β†’ list name β†’ tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name β†’ tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) /-- Similar to `unfold`, but only uses definitional equalities. -/ meta def dunfold (cs : parse ident*) (l : parse location) (cfg : dunfold_config := {}) : tactic unit := match l with | (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, dunfold_target new_cs cfg, intron n | _ := do new_cs ← to_qualified_names cs, l.apply (Ξ» h, dunfold_hyp cs h cfg) (dunfold_target new_cs cfg) end private meta def delta_hyps : list name β†’ list name β†’ tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_hyp cs >> delta_hyps cs hs /-- Similar to `dunfold`, but performs a raw delta reduction, rather than using an equation associated with the defined constants. -/ meta def delta : parse ident* β†’ parse location β†’ tactic unit | cs (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, delta_target new_cs, intron n | cs l := do new_cs ← to_qualified_names cs, l.apply (delta_hyp new_cs) (delta_target new_cs) private meta def unfold_projs_hyps (cfg : unfold_proj_config := {}) (hs : list name) : tactic bool := hs.mfoldl (Ξ» r h, do h ← get_local h, (unfold_projs_hyp h cfg >> return tt) <|> return r) ff /-- This tactic unfolds all structure projections. -/ meta def unfold_projs (l : parse location) (cfg : unfold_proj_config := {}) : tactic unit := match l with | loc.wildcard := do ls ← local_context, b₁ ← unfold_projs_hyps cfg (ls.map expr.local_pp_name), bβ‚‚ ← (tactic.unfold_projs_target cfg >> return tt) <|> return ff, when (not b₁ ∧ not bβ‚‚) (fail "unfold_projs failed to simplify") | _ := l.try_apply (Ξ» h, unfold_projs_hyp h cfg) (tactic.unfold_projs_target cfg) <|> fail "unfold_projs failed to simplify" end end interactive meta def ids_to_simp_arg_list (tac_name : name) (cs : list name) : tactic (list simp_arg_type) := cs.mmap $ Ξ» c, do n ← resolve_name c, hs ← get_eqn_lemmas_for ff n.const_name, env ← get_env, let p := env.is_projection n.const_name, when (hs.empty ∧ p.is_none) (fail (sformat! "{tac_name} tactic failed, {c} does not have equational lemmas nor is a projection")), return $ simp_arg_type.expr (expr.const c []) structure unfold_config extends simp_config := (zeta := ff) (proj := ff) (eta := ff) (canonize_instances := ff) namespace interactive open interactive interactive.types expr /-- Given defined constants `e₁ ... eβ‚™`, `unfold e₁ ... eβ‚™` iteratively unfolds all occurrences in the target of the main goal, using equational lemmas associated with the definitions. As with `simp`, the `at` modifier can be used to specify locations for the unfolding. -/ meta def unfold (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {}) : tactic unit := do es ← ids_to_simp_arg_list "unfold" cs, let no_dflt := tt, simp_core cfg.to_simp_config failed no_dflt es [] locat /-- Similar to `unfold`, but does not iterate the unfolding. -/ meta def unfold1 (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {single_pass := tt}) : tactic unit := unfold cs locat cfg /-- If the target of the main goal is an `opt_param`, assigns the default value. -/ meta def apply_opt_param : tactic unit := tactic.apply_opt_param /-- If the target of the main goal is an `auto_param`, executes the associated tactic. -/ meta def apply_auto_param : tactic unit := tactic.apply_auto_param /-- Fails if the given tactic succeeds. -/ meta def fail_if_success (tac : itactic) : tactic unit := tactic.fail_if_success tac /-- Succeeds if the given tactic fails. -/ meta def success_if_fail (tac : itactic) : tactic unit := tactic.success_if_fail tac meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (alpha_eqv t e) /-- `guard_target t` fails if the target of the main goal is not `t`. We use this tactic for writing tests. -/ meta def guard_target (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq t p /-- `guard_hyp h := t` fails if the hypothesis `h` does not have type `t`. We use this tactic for writing tests. -/ meta def guard_hyp (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit := do h ← get_local n >>= infer_type, guard_expr_eq h p /-- `match_target t` fails if target does not match pattern `t`. -/ meta def match_target (t : parse texpr) (m := reducible) : tactic unit := tactic.match_target t m >> skip /-- `by_cases p with h` splits the main goal into two cases, assuming `h : p` in the first branch, and `h : Β¬ p` in the second branch. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `local attribute classical.prop_decidable [instance]`. -/ meta def by_cases (q : parse texpr) (n : parse (tk "with" *> ident)?): tactic unit := do p ← tactic.to_expr_strict q, tactic.by_cases p (n.get_or_else `h) /-- Apply function extensionality and introduce new hypotheses. The tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible to ``` |- ((fun x, ...) = (fun x, ...)) ``` The variant `funext h₁ ... hβ‚™` applies `funext` `n` times, and uses the given identifiers to name the new hypotheses. -/ meta def funext : parse ident_* β†’ tactic unit | [] := tactic.funext >> skip | hs := funext_lst hs >> skip /-- If the target of the main goal is a proposition `p`, `by_contradiction h` reduces the goal to proving `false` using the additional hypothesis `h : Β¬ p`. If `h` is omitted, a name is generated automatically. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `local attribute classical.prop_decidable [instance]`. -/ meta def by_contradiction (n : parse ident?) : tactic unit := tactic.by_contradiction n >> return () /-- An abbreviation for `by_contradiction`. -/ meta def by_contra (n : parse ident?) : tactic unit := tactic.by_contradiction n >> return () /-- Type check the given expression, and trace its type. -/ meta def type_check (p : parse texpr) : tactic unit := do e ← to_expr p, tactic.type_check e, infer_type e >>= trace /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := tactic.done private meta def show_aux (p : pexpr) : list expr β†’ list expr β†’ tactic unit | [] r := fail "show tactic failed" | (g::gs) r := do do {set_goals [g], g_ty ← target, ty ← i_to_expr p, unify g_ty ty, set_goals (g :: r.reverse ++ gs), tactic.change ty} <|> show_aux gs (g::r) /-- `show t` finds the first goal whose target unifies with `t`. It makes that the main goal, performs the unification, and replaces the target with the unified version of `t`. -/ meta def Β«showΒ» (q : parse texpr) : tactic unit := do gs ← get_goals, show_aux q gs [] /-- The tactic `specialize h a₁ ... aβ‚™` works on local hypothesis `h`. The premises of this hypothesis, either universal quantifications or non-dependent implications, are instantiated by concrete terms coming either from arguments `a₁` ... `aβ‚™`. The tactic adds a new hypothesis with the same name `h := h a₁ ... aβ‚™` and tries to clear the previous one. -/ meta def specialize (p : parse texpr) : tactic unit := do e ← i_to_expr p, let h := expr.get_app_fn e, if h.is_local_constant then tactic.note h.local_pp_name none e >> try (tactic.clear h) else tactic.fail "specialize requires a term of the form `h x_1 .. x_n` where `h` appears in the local context" end interactive end tactic section add_interactive open tactic /- See add_interactive -/ private meta def add_interactive_aux (new_namespace : name) : list name β†’ command | [] := return () | (n::ns) := do env ← get_env, d_name ← resolve_constant n, (declaration.defn _ ls ty val hints trusted) ← env.get d_name, (name.mk_string h _) ← return d_name, let new_name := `tactic.interactive <.> h, add_decl (declaration.defn new_name ls ty (expr.const d_name (ls.map level.param)) hints trusted), add_interactive_aux ns /-- Copy a list of meta definitions in the current namespace to tactic.interactive. This command is useful when we want to update tactic.interactive without closing the current namespace. -/ meta def add_interactive (ns : list name) (p : name := `tactic.interactive) : command := add_interactive_aux p ns meta def has_dup : tactic bool := do ctx ← local_context, let p : name_set Γ— bool := ctx.foldl (Ξ» ⟨s, r⟩ h, if r then (s, r) else if s.contains h.local_pp_name then (s, tt) else (s.insert h.local_pp_name, ff)) (mk_name_set, ff), return p.2 /-- Renames hypotheses with the same name. -/ meta def dedup : tactic unit := mwhen has_dup $ do ctx ← local_context, n ← revert_lst ctx, intron n end add_interactive
069fb1aa0da86d8ecd5e781fc6550a21c40d2ad9
d5ff69c5608a867046609101d89910f1257aaf8c
/src/2020/problem_sheets/sheet2.lean
aabfe9e9a44c0f3df7d53e3a05650178b53212b3
[]
no_license
jiaminglimjm/M40001_lean
c299ff574a22d3a636a2b9720dc9b5e853a3bab0
c8bd8922f37f3e10e2d448f226798ebd0a2af232
refs/heads/master
1,672,312,761,737
1,603,095,035,000
1,603,095,035,000
304,831,347
0
0
null
1,603,094,893,000
1,602,922,986,000
null
UTF-8
Lean
false
false
3,097
lean
/- Math40001 : Introduction to university mathematics. Problem Sheet 2, October 2020. This is a Lean file. It can be read with the Lean theorem prover. You can work on this file online via the link at https://github.com/ImperialCollegeLondon/M40001_lean/blob/master/README.md or you can install Lean and its maths library following the instructions at https://leanprover-community.github.io/get_started.html and then just clone the project onto your own computer with `leanproject get ImperialCollegeLondon/M40001_lean`. There are advantages to installing Lean on your own computer (for example it's faster), but it's more hassle than just using it online. In the below, delete "sorry" and replace it with some tactics which prove the result. -/ import data.real.basic -- need real numbers for Q5 -- Q1 prove that ∩ is symmetric lemma question1 (Ξ± : Type) (X Y : set Ξ±) : X ∩ Y = Y ∩ X := begin sorry end -- question 2 defs def A := {x : ℝ | x ^ 2 < 3} def B := {x : ℝ | βˆƒ n : β„€, x = n ∧ x ^ 2 < 3} def C := {x : ℝ | x ^ 3 < 3} -- Change _true to _false and put a Β¬ in front -- of the goal if you think it's false. -- e.g. if you think 2c is false then don't -- try to prove it's true, try proving -- lemma question2c_false : Β¬ (A βŠ† C) := ... -- Some of these are tricky for Lean beginners. lemma question2a_true : (1 : ℝ)/2 ∈ A ∩ B := begin sorry end lemma question2b_true : (1 : ℝ)/2 ∈ A βˆͺ B := begin sorry end lemma question2c_true : A βŠ† C := begin sorry end lemma question2d_true : B βŠ† C := begin sorry end lemma question2e_true : C βŠ† A βˆͺ B := begin sorry end lemma question2f_true : (A ∩ B) βˆͺ C = (A βˆͺ B) ∩ C := begin sorry end -- Q3 set-up variables (X Y : Type) variable (P : X β†’ Prop) variable (Q : X β†’ Prop) variable (R : X β†’ Y β†’ Prop) -- for Q3 you're going to have to change the right hand -- side of the ↔ in the statement -- of the lemma to the answer you think is correct. lemma question3a : Β¬ (βˆ€ x : X, P x ∧ Β¬ Q x) ↔ true := -- change `true`! begin sorry end lemma question3b : Β¬ (βˆƒ x : X, (Β¬ P x) ∧ Q x) ↔ true := -- change `true`! begin sorry end lemma question3c : Β¬ (βˆ€ x : X, βˆƒ y : Y, R x y) ↔ true := -- change `true`! begin sorry end example (f : ℝ β†’ ℝ) (x : ℝ) : Β¬ (βˆ€ Ξ΅ : ℝ, Ξ΅ > 0 β†’ βˆƒ Ξ΄ : ℝ, Ξ΄ > 0 β†’ βˆ€ y : ℝ, abs (y - x) < Ξ΄ β†’ abs (f y -f x) < Ξ΅ ) ↔ -- change next line to what you think the answer is true := begin sorry end -- change _true to _false in 4a, 4b if you think the opposite is true -- and stick a `Β¬` in front of it lemma question4a_true : βˆ€ x : ℝ, βˆƒ y : ℝ, x + y = 2 := begin sorry end lemma question4b_true : βˆƒ y : ℝ, βˆ€ x : ℝ, x + y = 2 := begin sorry end -- similarly for Q5 -- change _true to _false and add in a negation if you -- want to prove that the proposition in the question is false. lemma question5a_true : βˆƒ x ∈ (βˆ… : set β„•), 2 + 2 = 5 := begin sorry end lemma question5b_true : βˆ€ x ∈ (βˆ… : set β„•), 2 + 2 = 5 := begin sorry end
05f309ce1e1b37f67a4b449765d38468b7e1b077
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/pnat/xgcd.lean
f58589a851047d2d12f54c5ed18a0ceb7958cebe
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,637
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland -/ import tactic.ring import data.pnat.prime /-! # Euclidean algorithm for β„• This file sets up a version of the Euclidean algorithm that only works with natural numbers. Given `0 < a, b`, it computes the unique `(w, x, y, z, d)` such that the following identities hold: * `a = (w + x) d` * `b = (y + z) d` * `w * z = x * y + 1` `d` is then the gcd of `a` and `b`, and `a' := a / d = w + x` and `b' := b / d = y + z` are coprime. This story is closely related to the structure of SLβ‚‚(β„•) (as a free monoid on two generators) and the theory of continued fractions. ## Main declarations * `xgcd_type`: Helper type in defining the gcd. Encapsulates `(wp, x, y, zp, ap, bp)`. where `wp` `zp`, `ap`, `bp` are the variables getting changed through the algorithm. * `is_special`: States `wp * zp = x * y + 1` * `is_reduced`: States `ap = a ∧ bp = b` ## Notes See `nat.xgcd` for a very similar algorithm allowing values in `β„€`. -/ open nat namespace pnat /-- A term of xgcd_type is a system of six naturals. They should be thought of as representing the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] together with the vector [a, b] = [ap + 1, bp + 1]. -/ @[derive inhabited] structure xgcd_type := (wp x y zp ap bp : β„•) namespace xgcd_type variable (u : xgcd_type) instance : has_sizeof xgcd_type := ⟨λ u, u.bp⟩ /-- The has_repr instance converts terms to strings in a way that reflects the matrix/vector interpretation as above. -/ instance : has_repr xgcd_type := ⟨λ u, "[[[" ++ (repr (u.wp + 1)) ++ ", " ++ (repr u.x) ++ "], [" ++ (repr u.y) ++ ", " ++ (repr (u.zp + 1)) ++ "]], [" ++ (repr (u.ap + 1)) ++ ", " ++ (repr (u.bp + 1)) ++ "]]"⟩ def mk' (w : β„•+) (x : β„•) (y : β„•) (z : β„•+) (a : β„•+) (b : β„•+) : xgcd_type := mk w.val.pred x y z.val.pred a.val.pred b.val.pred def w : β„•+ := succ_pnat u.wp def z : β„•+ := succ_pnat u.zp def a : β„•+ := succ_pnat u.ap def b : β„•+ := succ_pnat u.bp def r : β„• := (u.ap + 1) % (u.bp + 1) def q : β„• := (u.ap + 1) / (u.bp + 1) def qp : β„• := u.q - 1 /-- The map v gives the product of the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] and the vector [a, b] = [ap + 1, bp + 1]. The map vp gives [sp, tp] such that v = [sp + 1, tp + 1]. -/ def vp : β„• Γ— β„• := ⟨ u.wp + u.x + u.ap + u.wp * u.ap + u.x * u.bp, u.y + u.zp + u.bp + u.y * u.ap + u.zp * u.bp ⟩ def v : β„• Γ— β„• := ⟨u.w * u.a + u.x * u.b, u.y * u.a + u.z * u.b⟩ def succβ‚‚ (t : β„• Γ— β„•) : β„• Γ— β„• := ⟨t.1.succ, t.2.succ⟩ theorem v_eq_succ_vp : u.v = succβ‚‚ u.vp := by { ext; dsimp [v, vp, w, z, a, b, succβ‚‚]; repeat { rw [nat.succ_eq_add_one] }; ring } /-- is_special holds if the matrix has determinant one. -/ def is_special : Prop := u.wp + u.zp + u.wp * u.zp = u.x * u.y def is_special' : Prop := u.w * u.z = succ_pnat (u.x * u.y) theorem is_special_iff : u.is_special ↔ u.is_special' := begin dsimp [is_special, is_special'], split; intro h, { apply eq, dsimp [w, z, succ_pnat], rw [← h], repeat { rw [nat.succ_eq_add_one] }, ring }, { apply nat.succ.inj, replace h := congr_arg (coe : β„•+ β†’ β„•) h, rw [mul_coe, w, z] at h, repeat { rw [succ_pnat_coe, nat.succ_eq_add_one] at h }, repeat { rw [nat.succ_eq_add_one] }, rw [← h], ring } end /-- is_reduced holds if the two entries in the vector are the same. The reduction algorithm will produce a system with this property, whose product vector is the same as for the original system. -/ def is_reduced : Prop := u.ap = u.bp def is_reduced' : Prop := u.a = u.b theorem is_reduced_iff : u.is_reduced ↔ u.is_reduced' := ⟨ congr_arg succ_pnat, succ_pnat_inj ⟩ def flip : xgcd_type := { wp := u.zp, x := u.y, y := u.x, zp := u.wp, ap := u.bp, bp := u.ap } @[simp] theorem flip_w : (flip u).w = u.z := rfl @[simp] theorem flip_x : (flip u).x = u.y := rfl @[simp] theorem flip_y : (flip u).y = u.x := rfl @[simp] theorem flip_z : (flip u).z = u.w := rfl @[simp] theorem flip_a : (flip u).a = u.b := rfl @[simp] theorem flip_b : (flip u).b = u.a := rfl theorem flip_is_reduced : (flip u).is_reduced ↔ u.is_reduced := by { dsimp [is_reduced, flip], split; intro h; exact h.symm } theorem flip_is_special : (flip u).is_special ↔ u.is_special := by { dsimp [is_special, flip], rw[mul_comm u.x, mul_comm u.zp, add_comm u.zp] } theorem flip_v : (flip u).v = (u.v).swap := by { dsimp [v], ext, { simp only, ring }, { simp only, ring } } /-- Properties of division with remainder for a / b. -/ theorem rq_eq : u.r + (u.bp + 1) * u.q = u.ap + 1 := nat.mod_add_div (u.ap + 1) (u.bp + 1) theorem qp_eq (hr : u.r = 0) : u.q = u.qp + 1 := begin by_cases hq : u.q = 0, { let h := u.rq_eq, rw [hr, hq, mul_zero, add_zero] at h, cases h }, { exact (nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hq)).symm } end /-- The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying is_reduced. The gcd can be read off from this final system. -/ def start (a b : β„•+) : xgcd_type := ⟨0, 0, 0, 0, a - 1, b - 1⟩ theorem start_is_special (a b : β„•+) : (start a b).is_special := by { dsimp [start, is_special], refl } theorem start_v (a b : β„•+) : (start a b).v = ⟨a, b⟩ := begin dsimp [start, v, xgcd_type.a, xgcd_type.b, w, z], rw [one_mul, one_mul, zero_mul, zero_mul, zero_add, add_zero], rw [← nat.pred_eq_sub_one, ← nat.pred_eq_sub_one], rw [nat.succ_pred_eq_of_pos a.pos, nat.succ_pred_eq_of_pos b.pos] end def finish : xgcd_type := xgcd_type.mk u.wp ((u.wp + 1) * u.qp + u.x) u.y (u.y * u.qp + u.zp) u.bp u.bp theorem finish_is_reduced : u.finish.is_reduced := by { dsimp [is_reduced], refl } theorem finish_is_special (hs : u.is_special) : u.finish.is_special := begin dsimp [is_special, finish] at hs ⊒, rw [add_mul _ _ u.y, add_comm _ (u.x * u.y), ← hs], ring end theorem finish_v (hr : u.r = 0) : u.finish.v = u.v := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, rw [hr, zero_add] at ha, ext, { change (u.wp + 1) * u.b + ((u.wp + 1) * u.qp + u.x) * u.b = u.w * u.a + u.x * u.b, have : u.wp + 1 = u.w := rfl, rw [this, ← ha, u.qp_eq hr], ring }, { change u.y * u.b + (u.y * u.qp + u.z) * u.b = u.y * u.a + u.z * u.b, rw [← ha, u.qp_eq hr], ring } end /-- This is the main reduction step, which is used when u.r β‰  0, or equivalently b does not divide a. -/ def step : xgcd_type := xgcd_type.mk (u.y * u.q + u.zp) u.y ((u.wp + 1) * u.q + u.x) u.wp u.bp (u.r - 1) /-- We will apply the above step recursively. The following result is used to ensure that the process terminates. -/ theorem step_wf (hr : u.r β‰  0) : sizeof u.step < sizeof u := begin change u.r - 1 < u.bp, have hβ‚€ : (u.r - 1) + 1 = u.r := nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hr), have h₁ : u.r < u.bp + 1 := nat.mod_lt (u.ap + 1) u.bp.succ_pos, rw[← hβ‚€] at h₁, exact lt_of_succ_lt_succ h₁, end theorem step_is_special (hs : u.is_special) : u.step.is_special := begin dsimp [is_special, step] at hs ⊒, rw [mul_add, mul_comm u.y u.x, ← hs], ring end /-- The reduction step does not change the product vector. -/ theorem step_v (hr : u.r β‰  0) : u.step.v = (u.v).swap := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, let hr : (u.r - 1) + 1 = u.r := (add_comm _ 1).trans (nat.add_sub_of_le (nat.pos_of_ne_zero hr)), ext, { change ((u.y * u.q + u.z) * u.b + u.y * (u.r - 1 + 1) : β„•) = u.y * u.a + u.z * u.b, rw [← ha, hr], ring }, { change ((u.w * u.q + u.x) * u.b + u.w * (u.r - 1 + 1) : β„•) = u.w * u.a + u.x * u.b, rw [← ha, hr], ring } end /-- We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below. -/ def reduce : xgcd_type β†’ xgcd_type | u := dite (u.r = 0) (Ξ» h, u.finish) (Ξ» h, have sizeof u.step < sizeof u, from u.step_wf h, flip (reduce u.step)) theorem reduce_a {u : xgcd_type} (h : u.r = 0) : u.reduce = u.finish := by { rw [reduce], simp only, rw [if_pos h] } theorem reduce_b {u : xgcd_type} (h : u.r β‰  0) : u.reduce = u.step.reduce.flip := by { rw [reduce], simp only, rw [if_neg h, step] } theorem reduce_reduced : βˆ€ (u : xgcd_type), u.reduce.is_reduced | u := dite (u.r = 0) (Ξ» h, by { rw [reduce_a h], exact u.finish_is_reduced }) (Ξ» h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h, flip_is_reduced], apply reduce_reduced }) theorem reduce_reduced' (u : xgcd_type) : u.reduce.is_reduced' := (is_reduced_iff _).mp u.reduce_reduced theorem reduce_special : βˆ€ (u : xgcd_type), u.is_special β†’ u.reduce.is_special | u := dite (u.r = 0) (Ξ» h hs, by { rw [reduce_a h], exact u.finish_is_special hs }) (Ξ» h hs, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h], exact (flip_is_special _).mpr (reduce_special _ (u.step_is_special hs)) }) theorem reduce_special' (u : xgcd_type) (hs : u.is_special) : u.reduce.is_special' := (is_special_iff _).mp (u.reduce_special hs) theorem reduce_v : βˆ€ (u : xgcd_type), u.reduce.v = u.v | u := dite (u.r = 0) (Ξ» h, by {rw[reduce_a h, finish_v u h]}) (Ξ» h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw[reduce_b h, flip_v, reduce_v (step u), step_v u h, prod.swap_swap] }) end xgcd_type section gcd variables (a b : β„•+) def xgcd : xgcd_type := (xgcd_type.start a b).reduce def gcd_d : β„•+ := (xgcd a b).a def gcd_w : β„•+ := (xgcd a b).w def gcd_x : β„• := (xgcd a b).x def gcd_y : β„• := (xgcd a b).y def gcd_z : β„•+ := (xgcd a b).z def gcd_a' : β„•+ := succ_pnat ((xgcd a b).wp + (xgcd a b).x) def gcd_b' : β„•+ := succ_pnat ((xgcd a b).y + (xgcd a b).zp) theorem gcd_a'_coe : ((gcd_a' a b) : β„•) = (gcd_w a b) + (gcd_x a b) := by { dsimp [gcd_a', gcd_x, gcd_w, xgcd_type.w], rw [nat.succ_eq_add_one, nat.succ_eq_add_one, add_right_comm] } theorem gcd_b'_coe : ((gcd_b' a b) : β„•) = (gcd_y a b) + (gcd_z a b) := by { dsimp [gcd_b', gcd_y, gcd_z, xgcd_type.z], rw [nat.succ_eq_add_one, nat.succ_eq_add_one, add_assoc] } theorem gcd_props : let d := gcd_d a b, w := gcd_w a b, x := gcd_x a b, y := gcd_y a b, z := gcd_z a b, a' := gcd_a' a b, b' := gcd_b' a b in (w * z = succ_pnat (x * y) ∧ (a = a' * d) ∧ (b = b' * d) ∧ z * a' = succ_pnat (x * b') ∧ w * b' = succ_pnat (y * a') ∧ (z * a : β„•) = x * b + d ∧ (w * b : β„•) = y * a + d ) := begin intros, let u := (xgcd_type.start a b), let ur := u.reduce, have ha : d = ur.a := rfl, have hb : d = ur.b := u.reduce_reduced', have ha' : (a' : β„•) = w + x := gcd_a'_coe a b, have hb' : (b' : β„•) = y + z := gcd_b'_coe a b, have hdet : w * z = succ_pnat (x * y) := u.reduce_special' rfl, split, exact hdet, have hdet' : ((w * z) : β„•) = x * y + 1 := by { rw [← mul_coe, hdet, succ_pnat_coe] }, have huv : u.v = ⟨a, b⟩ := (xgcd_type.start_v a b), let hv : prod.mk (w * d + x * ur.b : β„•) (y * d + z * ur.b : β„•) = ⟨a, b⟩ := u.reduce_v.trans (xgcd_type.start_v a b), rw [← hb, ← add_mul, ← add_mul, ← ha', ← hb'] at hv, have ha'' : (a : β„•) = a' * d := (congr_arg prod.fst hv).symm, have hb'' : (b : β„•) = b' * d := (congr_arg prod.snd hv).symm, split, exact eq ha'', split, exact eq hb'', have hza' : (z * a' : β„•) = x * b' + 1, by { rw [ha', hb', mul_add, mul_add, mul_comm (z : β„•), hdet'], ring }, have hwb' : (w * b' : β„•) = y * a' + 1, by { rw [ha', hb', mul_add, mul_add, hdet'], ring }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hza'] }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hwb'] }, rw [ha'', hb''], repeat { rw [← mul_assoc] }, rw [hza', hwb'], split; ring, end theorem gcd_eq : gcd_d a b = gcd a b := begin rcases gcd_props a b with ⟨hβ‚€, h₁, hβ‚‚, h₃, hβ‚„, hβ‚…, hβ‚†βŸ©, apply dvd_antisymm, { apply dvd_gcd, exact dvd.intro (gcd_a' a b) (h₁.trans (mul_comm _ _)).symm, exact dvd.intro (gcd_b' a b) (hβ‚‚.trans (mul_comm _ _)).symm}, { have h₇ : (gcd a b : β„•) ∣ (gcd_z a b) * a := (nat.gcd_dvd_left a b).trans (dvd_mul_left _ _), have hβ‚ˆ : (gcd a b : β„•) ∣ (gcd_x a b) * b := (nat.gcd_dvd_right a b).trans (dvd_mul_left _ _), rw[hβ‚…] at h₇, rw dvd_iff, exact (nat.dvd_add_iff_right hβ‚ˆ).mpr h₇,} end theorem gcd_det_eq : (gcd_w a b) * (gcd_z a b) = succ_pnat ((gcd_x a b) * (gcd_y a b)) := (gcd_props a b).1 theorem gcd_a_eq : a = (gcd_a' a b) * (gcd a b) := (gcd_eq a b) β–Έ (gcd_props a b).2.1 theorem gcd_b_eq : b = (gcd_b' a b) * (gcd a b) := (gcd_eq a b) β–Έ (gcd_props a b).2.2.1 theorem gcd_rel_left' : (gcd_z a b) * (gcd_a' a b) = succ_pnat ((gcd_x a b) * (gcd_b' a b)) := (gcd_props a b).2.2.2.1 theorem gcd_rel_right' : (gcd_w a b) * (gcd_b' a b) = succ_pnat ((gcd_y a b) * (gcd_a' a b)) := (gcd_props a b).2.2.2.2.1 theorem gcd_rel_left : ((gcd_z a b) * a : β„•) = (gcd_x a b) * b + (gcd a b) := (gcd_eq a b) β–Έ (gcd_props a b).2.2.2.2.2.1 theorem gcd_rel_right : ((gcd_w a b) * b : β„•) = (gcd_y a b) * a + (gcd a b) := (gcd_eq a b) β–Έ (gcd_props a b).2.2.2.2.2.2 end gcd end pnat
412e0b9859edbbbae127f248a9c84a4f963d5534
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/group/units_auto.lean
8efb60fa5b5994c7c1267c8f6371ed14d9d4ac03
[]
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
15,104
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group.basic import Mathlib.logic.nontrivial import Mathlib.PostPort universes u l u_1 namespace Mathlib /-! # Units (i.e., invertible elements) of a multiplicative monoid -/ /-- 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 Ξ±] where 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 Ξ±] where val : Ξ± neg : Ξ± val_neg : val + neg = 0 neg_val : neg + val = 0 namespace units protected instance Mathlib.add_units.has_coe {Ξ± : Type u} [add_monoid Ξ±] : has_coe (add_units Ξ±) Ξ± := has_coe.mk add_units.val @[simp] theorem Mathlib.add_units.coe_mk {Ξ± : Type u} [add_monoid Ξ±] (a : Ξ±) (b : Ξ±) (h₁ : a + b = 0) (hβ‚‚ : b + a = 0) : ↑(add_units.mk a b h₁ hβ‚‚) = a := rfl theorem ext {Ξ± : Type u} [monoid Ξ±] : function.injective coe := sorry theorem Mathlib.add_units.eq_iff {Ξ± : Type u} [add_monoid Ξ±] {a : add_units Ξ±} {b : add_units Ξ±} : ↑a = ↑b ↔ a = b := function.injective.eq_iff add_units.ext theorem Mathlib.add_units.ext_iff {Ξ± : Type u} [add_monoid Ξ±] {a : add_units Ξ±} {b : add_units Ξ±} : a = b ↔ ↑a = ↑b := iff.symm add_units.eq_iff protected instance Mathlib.add_units.decidable_eq {Ξ± : Type u} [add_monoid Ξ±] [DecidableEq Ξ±] : DecidableEq (add_units Ξ±) := fun (a b : add_units Ξ±) => decidable_of_iff' (↑a = ↑b) add_units.ext_iff @[simp] theorem mk_coe {Ξ± : Type u} [monoid Ξ±] (u : units Ξ±) (y : Ξ±) (h₁ : ↑u * y = 1) (hβ‚‚ : y * ↑u = 1) : mk (↑u) y h₁ hβ‚‚ = u := ext rfl /-- Units of a monoid form a group. -/ protected instance group {Ξ± : Type u} [monoid Ξ±] : group (units Ξ±) := group.mk (fun (u₁ uβ‚‚ : units Ξ±) => mk (val u₁ * val uβ‚‚) (inv uβ‚‚ * inv u₁) sorry sorry) sorry (mk 1 1 sorry sorry) sorry sorry (fun (u : units Ξ±) => mk (inv u) (val u) (inv_val u) (val_inv u)) (div_inv_monoid.div._default (fun (u₁ uβ‚‚ : units Ξ±) => mk (val u₁ * val uβ‚‚) (inv uβ‚‚ * inv u₁) sorry sorry) sorry (mk 1 1 sorry sorry) sorry sorry fun (u : units Ξ±) => mk (inv u) (val u) (inv_val u) (val_inv u)) sorry @[simp] theorem Mathlib.add_units.coe_add {Ξ± : Type u} [add_monoid Ξ±] (a : add_units Ξ±) (b : add_units Ξ±) : ↑(a + b) = ↑a + ↑b := rfl @[simp] theorem coe_one {Ξ± : Type u} [monoid Ξ±] : ↑1 = 1 := rfl @[simp] theorem Mathlib.add_units.coe_eq_zero {Ξ± : Type u} [add_monoid Ξ±] {a : add_units Ξ±} : ↑a = 0 ↔ a = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (↑a = 0 ↔ a = 0)) (Eq.symm add_units.coe_zero))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑a = ↑0 ↔ a = 0)) (propext add_units.eq_iff))) (iff.refl (a = 0))) @[simp] theorem inv_mk {Ξ± : Type u} [monoid Ξ±] (x : Ξ±) (y : Ξ±) (h₁ : x * y = 1) (hβ‚‚ : y * x = 1) : mk x y h₁ h₂⁻¹ = mk y x hβ‚‚ h₁ := rfl theorem val_coe {Ξ± : Type u} [monoid Ξ±] (a : units Ξ±) : ↑a = val a := rfl theorem coe_inv {Ξ± : Type u} [monoid Ξ±] (a : units Ξ±) : ↑(a⁻¹) = inv a := rfl @[simp] theorem inv_mul {Ξ± : Type u} [monoid Ξ±] (a : units Ξ±) : ↑(a⁻¹) * ↑a = 1 := inv_val a @[simp] theorem Mathlib.add_units.add_neg {Ξ± : Type u} [add_monoid Ξ±] (a : add_units Ξ±) : ↑a + ↑(-a) = 0 := add_units.val_neg a theorem inv_mul_of_eq {Ξ± : Type u} [monoid Ξ±] {u : units Ξ±} {a : Ξ±} (h : ↑u = a) : ↑(u⁻¹) * a = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑(u⁻¹) * a = 1)) (Eq.symm h))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(u⁻¹) * ↑u = 1)) (inv_mul u))) (Eq.refl 1)) theorem Mathlib.add_units.add_neg_of_eq {Ξ± : Type u} [add_monoid Ξ±] {u : add_units Ξ±} {a : Ξ±} (h : ↑u = a) : a + ↑(-u) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (a + ↑(-u) = 0)) (Eq.symm h))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑u + ↑(-u) = 0)) (add_units.add_neg u))) (Eq.refl 0)) @[simp] theorem mul_inv_cancel_left {Ξ± : Type u} [monoid Ξ±] (a : units Ξ±) (b : Ξ±) : ↑a * (↑(a⁻¹) * b) = b := eq.mpr (id (Eq._oldrec (Eq.refl (↑a * (↑(a⁻¹) * b) = b)) (Eq.symm (mul_assoc (↑a) (↑(a⁻¹)) b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑a * ↑(a⁻¹) * b = b)) (mul_inv a))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * b = b)) (one_mul b))) (Eq.refl b))) @[simp] theorem Mathlib.add_units.neg_add_cancel_left {Ξ± : Type u} [add_monoid Ξ±] (a : add_units Ξ±) (b : Ξ±) : ↑(-a) + (↑a + b) = b := eq.mpr (id (Eq._oldrec (Eq.refl (↑(-a) + (↑a + b) = b)) (Eq.symm (add_assoc (↑(-a)) (↑a) b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(-a) + ↑a + b = b)) (add_units.neg_add a))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 + b = b)) (zero_add b))) (Eq.refl b))) @[simp] theorem mul_inv_cancel_right {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) (b : units Ξ±) : a * ↑b * ↑(b⁻¹) = a := eq.mpr (id (Eq._oldrec (Eq.refl (a * ↑b * ↑(b⁻¹) = a)) (mul_assoc a ↑b ↑(b⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (↑b * ↑(b⁻¹)) = a)) (mul_inv b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = a)) (mul_one a))) (Eq.refl a))) @[simp] theorem Mathlib.add_units.neg_add_cancel_right {Ξ± : Type u} [add_monoid Ξ±] (a : Ξ±) (b : add_units Ξ±) : a + ↑(-b) + ↑b = a := eq.mpr (id (Eq._oldrec (Eq.refl (a + ↑(-b) + ↑b = a)) (add_assoc a ↑(-b) ↑b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + (↑(-b) + ↑b) = a)) (add_units.neg_add b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + 0 = a)) (add_zero a))) (Eq.refl a))) protected instance Mathlib.add_units.inhabited {Ξ± : Type u} [add_monoid Ξ±] : Inhabited (add_units Ξ±) := { default := 0 } protected instance comm_group {Ξ± : Type u_1} [comm_monoid Ξ±] : comm_group (units Ξ±) := comm_group.mk group.mul sorry group.one sorry sorry group.inv group.div sorry sorry protected instance Mathlib.add_units.has_repr {Ξ± : Type u} [add_monoid Ξ±] [has_repr Ξ±] : has_repr (add_units Ξ±) := has_repr.mk (repr ∘ add_units.val) @[simp] theorem Mathlib.add_units.add_right_inj {Ξ± : Type u} [add_monoid Ξ±] (a : add_units Ξ±) {b : Ξ±} {c : Ξ±} : ↑a + b = ↑a + c ↔ b = c := sorry @[simp] theorem Mathlib.add_units.add_left_inj {Ξ± : Type u} [add_monoid Ξ±] (a : add_units Ξ±) {b : Ξ±} {c : Ξ±} : b + ↑a = c + ↑a ↔ b = c := sorry theorem Mathlib.add_units.eq_add_neg_iff_add_eq {Ξ± : Type u} [add_monoid Ξ±] {c : add_units Ξ±} {a : Ξ±} {b : Ξ±} : a = b + ↑(-c) ↔ a + ↑c = b := sorry theorem eq_inv_mul_iff_mul_eq {Ξ± : Type u} [monoid Ξ±] (b : units Ξ±) {a : Ξ±} {c : Ξ±} : a = ↑(b⁻¹) * c ↔ ↑b * a = c := sorry theorem inv_mul_eq_iff_eq_mul {Ξ± : Type u} [monoid Ξ±] (a : units Ξ±) {b : Ξ±} {c : Ξ±} : ↑(a⁻¹) * b = c ↔ b = ↑a * c := sorry theorem mul_inv_eq_iff_eq_mul {Ξ± : Type u} [monoid Ξ±] (b : units Ξ±) {a : Ξ±} {c : Ξ±} : a * ↑(b⁻¹) = c ↔ a = c * ↑b := sorry theorem inv_eq_of_mul_eq_one {Ξ± : Type u} [monoid Ξ±] {u : units Ξ±} {a : Ξ±} (h : ↑u * a = 1) : ↑(u⁻¹) = a := sorry theorem inv_unique {Ξ± : Type u} [monoid Ξ±] {u₁ : units Ξ±} {uβ‚‚ : units Ξ±} (h : ↑u₁ = ↑uβ‚‚) : ↑(u₁⁻¹) = ↑(u₂⁻¹) := (fun (this : ↑u₁ * ↑(u₂⁻¹) = 1) => inv_eq_of_mul_eq_one this) (eq.mpr (id (Eq._oldrec (Eq.refl (↑u₁ * ↑(u₂⁻¹) = 1)) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (↑uβ‚‚ * ↑(u₂⁻¹) = 1)) (mul_inv uβ‚‚))) (Eq.refl 1))) end units /-- For `a, b` in a `comm_monoid` such that `a * b = 1`, makes a unit out of `a`. -/ def add_units.mk_of_add_eq_zero {Ξ± : Type u} [add_comm_monoid Ξ±] (a : Ξ±) (b : Ξ±) (hab : a + b = 0) : add_units Ξ± := add_units.mk a b hab sorry @[simp] theorem units.coe_mk_of_mul_eq_one {Ξ± : Type u} [comm_monoid Ξ±] {a : Ξ±} {b : Ξ±} (h : a * b = 1) : ↑(units.mk_of_mul_eq_one a b h) = a := rfl /-- 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 {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) (u : units Ξ±) : Ξ± := a * ↑(u⁻¹) infixl:70 " /β‚š " => Mathlib.divp @[simp] theorem divp_self {Ξ± : Type u} [monoid Ξ±] (u : units Ξ±) : ↑u /β‚š u = 1 := units.mul_inv u @[simp] theorem divp_one {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) : a /β‚š 1 = a := mul_one a theorem divp_assoc {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) (b : Ξ±) (u : units Ξ±) : a * b /β‚š u = a * (b /β‚š u) := mul_assoc a b ↑(u⁻¹) @[simp] theorem divp_inv {Ξ± : Type u} [monoid Ξ±] {a : Ξ±} (u : units Ξ±) : a /β‚š (u⁻¹) = a * ↑u := rfl @[simp] theorem divp_mul_cancel {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) (u : units Ξ±) : a /β‚š u * ↑u = a := Eq.trans (mul_assoc a ↑(u⁻¹) ↑u) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (↑(u⁻¹) * ↑u) = a)) (units.inv_mul u))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = a)) (mul_one a))) (Eq.refl a))) @[simp] theorem mul_divp_cancel {Ξ± : Type u} [monoid Ξ±] (a : Ξ±) (u : units Ξ±) : a * ↑u /β‚š u = a := Eq.trans (mul_assoc a ↑u ↑(u⁻¹)) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (↑u * ↑(u⁻¹)) = a)) (units.mul_inv u))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = a)) (mul_one a))) (Eq.refl a))) @[simp] theorem divp_left_inj {Ξ± : Type u} [monoid Ξ±] (u : units Ξ±) {a : Ξ±} {b : Ξ±} : a /β‚š u = b /β‚š u ↔ a = b := units.mul_left_inj (u⁻¹) theorem divp_divp_eq_divp_mul {Ξ± : Type u} [monoid Ξ±] (x : Ξ±) (u₁ : units Ξ±) (uβ‚‚ : units Ξ±) : x /β‚š u₁ /β‚š uβ‚‚ = x /β‚š (uβ‚‚ * u₁) := sorry theorem divp_eq_iff_mul_eq {Ξ± : Type u} [monoid Ξ±] {x : Ξ±} {u : units Ξ±} {y : Ξ±} : x /β‚š u = y ↔ y * ↑u = x := iff.trans (iff.symm (units.mul_left_inj u)) (eq.mpr (id (Eq._oldrec (Eq.refl (x /β‚š u * ↑u = y * ↑u ↔ y * ↑u = x)) (divp_mul_cancel x u))) { mp := Eq.symm, mpr := Eq.symm }) theorem divp_eq_one_iff_eq {Ξ± : Type u} [monoid Ξ±] {a : Ξ±} {u : units Ξ±} : a /β‚š u = 1 ↔ a = ↑u := iff.trans (iff.symm (units.mul_left_inj u)) (eq.mpr (id (Eq._oldrec (Eq.refl (a /β‚š u * ↑u = 1 * ↑u ↔ a = ↑u)) (divp_mul_cancel a u))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = 1 * ↑u ↔ a = ↑u)) (one_mul ↑u))) (iff.refl (a = ↑u)))) @[simp] theorem one_divp {Ξ± : Type u} [monoid Ξ±] (u : units Ξ±) : 1 /β‚š u = ↑(u⁻¹) := one_mul ↑(u⁻¹) theorem divp_eq_divp_iff {Ξ± : Type u} [comm_monoid Ξ±] {x : Ξ±} {y : Ξ±} {ux : units Ξ±} {uy : units Ξ±} : x /β‚š ux = y /β‚š uy ↔ x * ↑uy = y * ↑ux := sorry theorem divp_mul_divp {Ξ± : Type u} [comm_monoid Ξ±] (x : Ξ±) (y : Ξ±) (ux : units Ξ±) (uy : units Ξ±) : x /β‚š ux * (y /β‚š uy) = x * y /β‚š (ux * uy) := sorry /-! # `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`. -/ /-- 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`. -/ def is_unit {M : Type u_1} [monoid M] (a : M) := βˆƒ (u : units M), ↑u = a theorem is_unit_of_subsingleton {M : Type u_1} [monoid M] [subsingleton M] (a : M) : is_unit a := Exists.intro (units.mk a a (subsingleton.elim (a * a) 1) (subsingleton.elim (a * a) 1)) rfl @[simp] theorem is_unit_unit {M : Type u_1} [monoid M] (u : units M) : is_unit ↑u := Exists.intro u rfl @[simp] theorem is_add_unit_zero {M : Type u_1} [add_monoid M] : is_add_unit 0 := Exists.intro 0 rfl theorem is_add_unit_of_add_eq_zero {M : Type u_1} [add_comm_monoid M] (a : M) (b : M) (h : a + b = 0) : is_add_unit a := Exists.intro (add_units.mk_of_add_eq_zero a b h) rfl theorem is_add_unit_iff_exists_neg {M : Type u_1} [add_comm_monoid M] {a : M} : is_add_unit a ↔ βˆƒ (b : M), a + b = 0 := sorry theorem is_add_unit_iff_exists_neg' {M : Type u_1} [add_comm_monoid M] {a : M} : is_add_unit a ↔ βˆƒ (b : M), b + a = 0 := sorry /-- Multiplication by a `u : units M` doesn't affect `is_unit`. -/ @[simp] theorem units.is_unit_mul_units {M : Type u_1} [monoid M] (a : M) (u : units M) : is_unit (a * ↑u) ↔ is_unit a := sorry theorem is_unit.mul {M : Type u_1} [monoid M] {x : M} {y : M} : is_unit x β†’ is_unit y β†’ is_unit (x * y) := sorry theorem is_add_unit_of_add_is_add_unit_left {M : Type u_1} [add_comm_monoid M] {x : M} {y : M} (hu : is_add_unit (x + y)) : is_add_unit x := sorry theorem is_unit_of_mul_is_unit_right {M : Type u_1} [comm_monoid M] {x : M} {y : M} (hu : is_unit (x * y)) : is_unit y := is_unit_of_mul_is_unit_left (eq.mpr (id (Eq._oldrec (Eq.refl (is_unit (y * x))) (mul_comm y x))) hu) theorem is_unit.mul_right_inj {M : Type u_1} [monoid M] {a : M} {b : M} {c : M} (ha : is_unit a) : a * b = a * c ↔ b = c := sorry theorem is_unit.mul_left_inj {M : Type u_1} [monoid M] {a : M} {b : M} {c : M} (ha : is_unit a) : b * a = c * a ↔ b = c := sorry /-- The element of the group of units, corresponding to an element of a monoid which is a unit. -/ def is_unit.unit {M : Type u_1} [monoid M] {a : M} (h : is_unit a) : units M := classical.some h theorem is_unit.unit_spec {M : Type u_1} [monoid M] {a : M} (h : is_unit a) : ↑(is_unit.unit h) = a := classical.some_spec h /-- Constructs a `group` structure on a `monoid` consisting only of units. -/ def group_of_is_unit {M : Type u_1} [hM : monoid M] (h : βˆ€ (a : M), is_unit a) : group M := group.mk monoid.mul monoid.mul_assoc monoid.one monoid.one_mul monoid.mul_one (fun (a : M) => ↑(is_unit.unit (h a)⁻¹)) (div_inv_monoid.div._default monoid.mul monoid.mul_assoc monoid.one monoid.one_mul monoid.mul_one fun (a : M) => ↑(is_unit.unit (h a)⁻¹)) sorry /-- Constructs a `comm_group` structure on a `comm_monoid` consisting only of units. -/ def comm_group_of_is_unit {M : Type u_1} [hM : comm_monoid M] (h : βˆ€ (a : M), is_unit a) : comm_group M := comm_group.mk comm_monoid.mul comm_monoid.mul_assoc comm_monoid.one comm_monoid.one_mul comm_monoid.mul_one (fun (a : M) => ↑(is_unit.unit (h a)⁻¹)) (group.div._default comm_monoid.mul comm_monoid.mul_assoc comm_monoid.one comm_monoid.one_mul comm_monoid.mul_one fun (a : M) => ↑(is_unit.unit (h a)⁻¹)) sorry comm_monoid.mul_comm end Mathlib
bfe79bbe9b506af5da37c4da8b7a666df0cbf8bf
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/measurable_space.lean
60e479ef9cf97e7a9e63eb1af664962b960247be
[ "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
68,527
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 data.prod.tprod import group_theory.coset import logic.equiv.fin import measure_theory.measurable_space_def import order.filter.small_sets import order.liminf_limsup import measure_theory.tactic /-! # Measurable spaces and measurable functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides properties of measurable spaces and the functions and isomorphisms between them. The definition of a measurable space is in `measure_theory.measurable_space_def`. A measurable space is a set equipped with a Οƒ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. Οƒ-algebras on a fixed set `Ξ±` form a complete lattice. Here we order Οƒ-algebras by writing `m₁ ≀ mβ‚‚` if every set which is `m₁`-measurable is also `mβ‚‚`-measurable (that is, `m₁` is a subset of `mβ‚‚`). In particular, any collection of subsets of `Ξ±` generates a smallest Οƒ-algebra which contains all of them. A function `f : Ξ± β†’ Ξ²` induces a Galois connection between the lattices of Οƒ-algebras on `Ξ±` and `Ξ²`. A measurable equivalence between measurable spaces is an equivalence which respects the Οƒ-algebras, that is, for which both directions of the equivalence are measurable functions. We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`. ## Notation * We write `Ξ± ≃ᡐ Ξ²` for measurable equivalences between the measurable spaces `Ξ±` and `Ξ²`. This should not be confused with `β‰ƒβ‚˜` which is used for diffeomorphisms between manifolds. ## Implementation notes Measurability of a function `f : Ξ± β†’ Ξ²` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, Οƒ-algebra, measurable function, measurable equivalence, dynkin system, Ο€-Ξ» theorem, Ο€-system -/ open set encodable function equiv open_locale filter measure_theory variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΄' : Type*} {ΞΉ : Sort*} {s t u : set Ξ±} namespace measurable_space section functors variables {m m₁ mβ‚‚ : measurable_space Ξ±} {m' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} /-- The forward image of a measurable space under a function. `map f m` contains the sets `s : set Ξ²` whose preimage under `f` is measurable. -/ protected def map (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ±) : measurable_space Ξ² := { measurable_set' := Ξ» s, measurable_set[m] $ f ⁻¹' s, measurable_set_empty := m.measurable_set_empty, measurable_set_compl := assume s hs, m.measurable_set_compl _ hs, measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }} @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measurable space under a function. `comap f m` contains the sets `s : set Ξ±` such that `s` is the `f`-preimage of a measurable set in `Ξ²`. -/ protected def comap (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ²) : measurable_space Ξ± := { measurable_set' := Ξ» s, βˆƒs', measurable_set[m] s' ∧ f ⁻¹' s' = s, measurable_set_empty := βŸ¨βˆ…, m.measurable_set_empty, rfl⟩, measurable_set_compl := assume s ⟨s', h₁, hβ‚‚βŸ©, ⟨s'ᢜ, m.measurable_set_compl _ h₁, hβ‚‚ β–Έ rfl⟩, measurable_set_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in βŸ¨β‹ƒ i, s' i, m.measurable_set_Union _ (Ξ» i, (hs' i).left), by simp [hs'] ⟩ } lemma comap_eq_generate_from (m : measurable_space Ξ²) (f : Ξ± β†’ Ξ²) : m.comap f = generate_from {t | βˆƒ s, measurable_set s ∧ f ⁻¹' s = t} := by convert generate_from_measurable_set.symm @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h β–Έ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ²} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht β–Έ hu β–Έ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : Ξ± β†’ Ξ²} : m'.comap f ≀ m ↔ m' ≀ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq β–Έ h _ ht⟩ lemma gc_comap_map (f : Ξ± β†’ Ξ²) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≀ mβ‚‚) : m₁.map f ≀ mβ‚‚.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≀ mβ‚‚) : m₁.comap g ≀ mβ‚‚.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (βŠ₯ : measurable_space Ξ±).comap g = βŠ₯ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ βŠ” mβ‚‚).comap g = m₁.comap g βŠ” mβ‚‚.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ΞΉ β†’ measurable_space Ξ±} : (⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊀ : measurable_space Ξ±).map f = ⊀ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ βŠ“ mβ‚‚).map f = m₁.map f βŠ“ mβ‚‚.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ΞΉ β†’ measurable_space Ξ±} : (β¨…i, m i).map f = (β¨…i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≀ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≀ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma comap_generate_from {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq β–Έ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space lemma measurable_iff_le_map {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚ ≀ m₁.map f := iff.rfl alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map lemma measurable_iff_comap_le {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚.comap f ≀ m₁ := comap_le_iff_le_map.symm alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le lemma comap_measurable {m : measurable_space Ξ²} (f : Ξ± β†’ Ξ²) : measurable[m.comap f] f := Ξ» s hs, ⟨s, hs, rfl⟩ lemma measurable.mono {ma ma' : measurable_space Ξ±} {mb mb' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} (hf : @measurable Ξ± Ξ² ma mb f) (ha : ma ≀ ma') (hb : mb' ≀ mb) : @measurable Ξ± Ξ² ma' mb' f := Ξ» t ht, ha _ $ hf $ hb _ ht @[measurability] lemma measurable_from_top [measurable_space Ξ²] {f : Ξ± β†’ Ξ²} : measurable[⊀] f := Ξ» s hs, trivial lemma measurable_generate_from [measurable_space Ξ±] {s : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (h : βˆ€ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := measurable.of_le_map $ generate_from_le h variables {f g : Ξ± β†’ Ξ²} section typeclass_measurable_space variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] @[nontriviality, measurability] lemma subsingleton.measurable [subsingleton Ξ±] : measurable f := Ξ» s hs, @subsingleton.measurable_set Ξ± _ _ _ @[nontriviality, measurability] lemma measurable_of_subsingleton_codomain [subsingleton Ξ²] (f : Ξ± β†’ Ξ²) : measurable f := Ξ» s hs, subsingleton.set_cases measurable_set.empty measurable_set.univ s @[to_additive] lemma measurable_one [has_one Ξ±] : measurable (1 : Ξ² β†’ Ξ±) := @measurable_const _ _ _ _ 1 lemma measurable_of_empty [is_empty Ξ±] (f : Ξ± β†’ Ξ²) : measurable f := subsingleton.measurable lemma measurable_of_empty_codomain [is_empty Ξ²] (f : Ξ± β†’ Ξ²) : measurable f := by { haveI := function.is_empty f, exact measurable_of_empty f } /-- A version of `measurable_const` that assumes `f x = f y` for all `x, y`. This version works for functions between empty types. -/ lemma measurable_const' {f : Ξ² β†’ Ξ±} (hf : βˆ€ x y, f x = f y) : measurable f := begin casesI is_empty_or_nonempty Ξ², { exact measurable_of_empty f }, { convert measurable_const, exact funext (Ξ» x, hf x h.some) } end @[measurability] lemma measurable_nat_cast [has_nat_cast Ξ±] (n : β„•) : measurable (n : Ξ² β†’ Ξ±) := @measurable_const Ξ± _ _ _ n @[measurability] lemma measurable_int_cast [has_int_cast Ξ±] (n : β„€) : measurable (n : Ξ² β†’ Ξ±) := @measurable_const Ξ± _ _ _ n lemma measurable_of_finite [finite Ξ±] [measurable_singleton_class Ξ±] (f : Ξ± β†’ Ξ²) : measurable f := Ξ» s hs, (f ⁻¹' s).to_finite.measurable_set lemma measurable_of_countable [countable Ξ±] [measurable_singleton_class Ξ±] (f : Ξ± β†’ Ξ²) : measurable f := Ξ» s hs, (f ⁻¹' s).to_countable.measurable_set end typeclass_measurable_space variables {m : measurable_space Ξ±} include m @[measurability] lemma measurable.iterate {f : Ξ± β†’ Ξ±} (hf : measurable f) : βˆ€ n, measurable (f^[n]) | 0 := measurable_id | (n+1) := (measurable.iterate n).comp hf variables {mΞ² : measurable_space Ξ²} include mΞ² @[measurability] lemma measurable_set_preimage {t : set Ξ²} (hf : measurable f) (ht : measurable_set t) : measurable_set (f ⁻¹' t) := hf ht @[measurability] lemma measurable.piecewise {_ : decidable_pred (∈ s)} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : measurable (piecewise s f g) := begin intros t ht, rw piecewise_preimage, exact hs.ite (hf ht) (hg ht) end /-- this is slightly different from `measurable.piecewise`. It can be used to show `measurable (ite (x=0) 0 1)` by `exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`, but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/ lemma measurable.ite {p : Ξ± β†’ Prop} {_ : decidable_pred p} (hp : measurable_set {a : Ξ± | p a}) (hf : measurable f) (hg : measurable g) : measurable (Ξ» x, ite (p x) (f x) (g x)) := measurable.piecewise hp hf hg @[measurability] lemma measurable.indicator [has_zero Ξ²] (hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) := hf.piecewise hs measurable_const @[measurability, to_additive] lemma measurable_set_mul_support [has_one Ξ²] [measurable_singleton_class Ξ²] (hf : measurable f) : measurable_set (mul_support f) := hf (measurable_set_singleton 1).compl /-- If a function coincides with a measurable function outside of a countable set, it is measurable. -/ lemma measurable.measurable_of_countable_ne [measurable_singleton_class Ξ±] (hf : measurable f) (h : set.countable {x | f x β‰  g x}) : measurable g := begin assume t ht, have : g ⁻¹' t = (g ⁻¹' t ∩ {x | f x = g x}ᢜ) βˆͺ (g ⁻¹' t ∩ {x | f x = g x}), by simp [← inter_union_distrib_left], rw this, apply measurable_set.union (h.mono (inter_subset_right _ _)).measurable_set, have : g ⁻¹' t ∩ {x : Ξ± | f x = g x} = f ⁻¹' t ∩ {x : Ξ± | f x = g x}, by { ext x, simp {contextual := tt} }, rw this, exact (hf ht).inter h.measurable_set.of_compl, end end measurable_functions section constructions instance : measurable_space empty := ⊀ instance : measurable_space punit := ⊀ -- this also works for `unit` instance : measurable_space bool := ⊀ instance : measurable_space β„• := ⊀ instance : measurable_space β„€ := ⊀ instance : measurable_space β„š := ⊀ instance : measurable_singleton_class empty := ⟨λ _, trivial⟩ instance : measurable_singleton_class punit := ⟨λ _, trivial⟩ instance : measurable_singleton_class bool := ⟨λ _, trivial⟩ instance : measurable_singleton_class β„• := ⟨λ _, trivial⟩ instance : measurable_singleton_class β„€ := ⟨λ _, trivial⟩ instance : measurable_singleton_class β„š := ⟨λ _, trivial⟩ lemma measurable_to_countable [measurable_space Ξ±] [countable Ξ±] [measurable_space Ξ²] {f : Ξ² β†’ Ξ±} (h : βˆ€ y, measurable_set (f ⁻¹' {f y})) : measurable f := begin assume s hs, rw [← bUnion_preimage_singleton], refine measurable_set.Union (Ξ» y, measurable_set.Union $ Ξ» hy, _), by_cases hyf : y ∈ range f, { rcases hyf with ⟨y, rfl⟩, apply h }, { simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] } end lemma measurable_to_countable' [measurable_space Ξ±] [countable Ξ±] [measurable_space Ξ²] {f : Ξ² β†’ Ξ±} (h : βˆ€ x, measurable_set (f ⁻¹' {x})) : measurable f := measurable_to_countable (Ξ» y, h (f y)) @[measurability] lemma measurable_unit [measurable_space Ξ±] (f : unit β†’ Ξ±) : measurable f := measurable_from_top section nat variables [measurable_space Ξ±] @[measurability] lemma measurable_from_nat {f : β„• β†’ Ξ±} : measurable f := measurable_from_top lemma measurable_to_nat {f : Ξ± β†’ β„•} : (βˆ€ y, measurable_set (f ⁻¹' {f y})) β†’ measurable f := measurable_to_countable lemma measurable_to_bool {f : Ξ± β†’ bool} (h : measurable_set (f⁻¹' {tt})) : measurable f := begin apply measurable_to_countable', rintros (-|-), { convert h.compl, rw [← preimage_compl, bool.compl_singleton, bool.bnot_tt] }, exact h, end lemma measurable_find_greatest' {p : Ξ± β†’ β„• β†’ Prop} [βˆ€ x, decidable_pred (p x)] {N : β„•} (hN : βˆ€ k ≀ N, measurable_set {x | nat.find_greatest (p x) N = k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := measurable_to_nat $ Ξ» x, hN _ N.find_greatest_le lemma measurable_find_greatest {p : Ξ± β†’ β„• β†’ Prop} [βˆ€ x, decidable_pred (p x)] {N} (hN : βˆ€ k ≀ N, measurable_set {x | p x k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := begin refine measurable_find_greatest' (Ξ» k hk, _), simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter, measurable_set.compl, hN]; try { intros } } end lemma measurable_find {p : Ξ± β†’ β„• β†’ Prop} [βˆ€ x, decidable_pred (p x)] (hp : βˆ€ x, βˆƒ N, p x N) (hm : βˆ€ k, measurable_set {x | p x k}) : measurable (Ξ» x, nat.find (hp x)) := begin refine measurable_to_nat (Ξ» x, _), rw [preimage_find_eq_disjointed], exact measurable_set.disjointed hm _ end end nat section quotient variables [measurable_space Ξ±] [measurable_space Ξ²] instance {Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} [m : measurable_space Ξ±] : measurable_space (quot r) := m.map (quot.mk r) instance {Ξ±} {s : setoid Ξ±} [m : measurable_space Ξ±] : measurable_space (quotient s) := m.map quotient.mk' @[to_additive] instance _root_.quotient_group.measurable_space {G} [group G] [measurable_space G] (S : subgroup G) : measurable_space (G β§Έ S) := quotient.measurable_space lemma measurable_set_quotient {s : setoid Ξ±} {t : set (quotient s)} : measurable_set t ↔ measurable_set (quotient.mk' ⁻¹' t) := iff.rfl lemma measurable_from_quotient {s : setoid Ξ±} {f : quotient s β†’ Ξ²} : measurable f ↔ measurable (f ∘ quotient.mk') := iff.rfl @[measurability] lemma measurable_quotient_mk [s : setoid Ξ±] : measurable (quotient.mk : Ξ± β†’ quotient s) := Ξ» s, id @[measurability] lemma measurable_quotient_mk' {s : setoid Ξ±} : measurable (quotient.mk' : Ξ± β†’ quotient s) := Ξ» s, id @[measurability] lemma measurable_quot_mk {r : Ξ± β†’ Ξ± β†’ Prop} : measurable (quot.mk r) := Ξ» s, id @[to_additive] lemma quotient_group.measurable_coe {G} [group G] [measurable_space G] {S : subgroup G} : measurable (coe : G β†’ G β§Έ S) := measurable_quotient_mk' attribute [measurability] quotient_group.measurable_coe quotient_add_group.measurable_coe @[to_additive] lemma quotient_group.measurable_from_quotient {G} [group G] [measurable_space G] {S : subgroup G} {f : G β§Έ S β†’ Ξ±} : measurable f ↔ measurable (f ∘ (coe : G β†’ G β§Έ S)) := measurable_from_quotient end quotient section subtype instance {Ξ±} {p : Ξ± β†’ Prop} [m : measurable_space Ξ±] : measurable_space (subtype p) := m.comap (coe : _ β†’ Ξ±) section variables [measurable_space Ξ±] @[measurability] lemma measurable_subtype_coe {p : Ξ± β†’ Prop} : measurable (coe : subtype p β†’ Ξ±) := measurable_space.le_map_comap instance {p : Ξ± β†’ Prop} [measurable_singleton_class Ξ±] : measurable_singleton_class (subtype p) := { measurable_set_singleton := Ξ» x, begin have : measurable_set {(x : Ξ±)} := measurable_set_singleton _, convert @measurable_subtype_coe Ξ± _ p _ this, ext y, simp [subtype.ext_iff], end } end variables {m : measurable_space Ξ±} {mΞ² : measurable_space Ξ²} include m lemma measurable_set.subtype_image {s : set Ξ±} {t : set s} (hs : measurable_set s) : measurable_set t β†’ measurable_set ((coe : s β†’ Ξ±) '' t) | ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, subtype.image_preimage_coe], exact hu.inter hs end include mΞ² @[measurability] lemma measurable.subtype_coe {p : Ξ² β†’ Prop} {f : Ξ± β†’ subtype p} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a : Ξ²)) := measurable_subtype_coe.comp hf @[measurability] lemma measurable.subtype_mk {p : Ξ² β†’ Prop} {f : Ξ± β†’ Ξ²} (hf : measurable f) {h : βˆ€ x, p (f x)} : measurable (Ξ» x, (⟨f x, h x⟩ : subtype p)) := Ξ» t ⟨s, hs⟩, hs.2 β–Έ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1] lemma measurable_of_measurable_union_cover {f : Ξ± β†’ Ξ²} (s t : set Ξ±) (hs : measurable_set s) (ht : measurable_set t) (h : univ βŠ† s βˆͺ t) (hc : measurable (Ξ» a : s, f a)) (hd : measurable (Ξ» a : t, f a)) : measurable f := begin intros u hu, convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)), change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) βˆͺ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], end lemma measurable_of_restrict_of_restrict_compl {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hs : measurable_set s) (h₁ : measurable (s.restrict f)) (hβ‚‚ : measurable (sᢜ.restrict f)) : measurable f := measurable_of_measurable_union_cover s sᢜ hs hs.compl (union_compl_self s).ge h₁ hβ‚‚ lemma measurable.dite [βˆ€ x, decidable (x ∈ s)] {f : s β†’ Ξ²} (hf : measurable f) {g : sᢜ β†’ Ξ²} (hg : measurable g) (hs : measurable_set s) : measurable (Ξ» x, if hx : x ∈ s then f ⟨x, hx⟩ else g ⟨x, hx⟩) := measurable_of_restrict_of_restrict_compl hs (by simpa) (by simpa) lemma measurable_of_measurable_on_compl_finite [measurable_singleton_class Ξ±] {f : Ξ± β†’ Ξ²} (s : set Ξ±) (hs : s.finite) (hf : measurable (sᢜ.restrict f)) : measurable f := begin letI : fintype s := finite.fintype hs, exact measurable_of_restrict_of_restrict_compl hs.measurable_set (measurable_of_finite _) hf end lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class Ξ±] {f : Ξ± β†’ Ξ²} (a : Ξ±) (hf : measurable ({x | x β‰  a}.restrict f)) : measurable f := measurable_of_measurable_on_compl_finite {a} (finite_singleton a) hf end subtype section prod /-- A `measurable_space` structure on the product of two measurable spaces. -/ def measurable_space.prod {Ξ± Ξ²} (m₁ : measurable_space Ξ±) (mβ‚‚ : measurable_space Ξ²) : measurable_space (Ξ± Γ— Ξ²) := m₁.comap prod.fst βŠ” mβ‚‚.comap prod.snd instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± Γ— Ξ²) := m₁.prod mβ‚‚ @[measurability] lemma measurable_fst {ma : measurable_space Ξ±} {mb : measurable_space Ξ²} : measurable (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) := measurable.of_comap_le le_sup_left @[measurability] lemma measurable_snd {ma : measurable_space Ξ±} {mb : measurable_space Ξ²} : measurable (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) := measurable.of_comap_le le_sup_right variables {m : measurable_space Ξ±} {mΞ² : measurable_space Ξ²} {mΞ³ : measurable_space Ξ³} include m mΞ² mΞ³ lemma measurable.fst {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).1) := measurable_fst.comp hf lemma measurable.snd {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).2) := measurable_snd.comp hf @[measurability] lemma measurable.prod {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf₁ : measurable (Ξ» a, (f a).1)) (hfβ‚‚ : measurable (Ξ» a, (f a).2)) : measurable f := measurable.of_le_map $ sup_le (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ }) (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hfβ‚‚ }) lemma measurable.prod_mk {Ξ² Ξ³} {mΞ² : measurable_space Ξ²} {mΞ³ : measurable_space Ξ³} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a : Ξ±, (f a, g a)) := measurable.prod hf hg lemma measurable.prod_map [measurable_space Ξ΄] {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ΄} (hf : measurable f) (hg : measurable g) : measurable (prod.map f g) := (hf.comp measurable_fst).prod_mk (hg.comp measurable_snd) omit mΞ³ lemma measurable_prod_mk_left {x : Ξ±} : measurable (@prod.mk _ Ξ² x) := measurable_const.prod_mk measurable_id lemma measurable_prod_mk_right {y : Ξ²} : measurable (Ξ» x : Ξ±, (x, y)) := measurable_id.prod_mk measurable_const include mΞ³ lemma measurable.of_uncurry_left {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {x : Ξ±} : measurable (f x) := hf.comp measurable_prod_mk_left lemma measurable.of_uncurry_right {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {y : Ξ²} : measurable (Ξ» x, f x y) := hf.comp measurable_prod_mk_right lemma measurable_prod {f : Ξ± β†’ Ξ² Γ— Ξ³} : measurable f ↔ measurable (Ξ» a, (f a).1) ∧ measurable (Ξ» a, (f a).2) := ⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, Ξ» h, measurable.prod h.1 h.2⟩ omit mΞ³ @[measurability] lemma measurable_swap : measurable (prod.swap : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ±) := measurable.prod measurable_snd measurable_fst lemma measurable_swap_iff {mΞ³ : measurable_space Ξ³} {f : Ξ± Γ— Ξ² β†’ Ξ³} : measurable (f ∘ prod.swap) ↔ measurable f := ⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, Ξ» hf, hf.comp measurable_swap⟩ @[measurability] lemma measurable_set.prod {s : set Ξ±} {t : set Ξ²} (hs : measurable_set s) (ht : measurable_set t) : measurable_set (s Γ—Λ’ t) := measurable_set.inter (measurable_fst hs) (measurable_snd ht) lemma measurable_set_prod_of_nonempty {s : set Ξ±} {t : set Ξ²} (h : (s Γ—Λ’ t).nonempty) : measurable_set (s Γ—Λ’ t) ↔ measurable_set s ∧ measurable_set t := begin rcases h with ⟨⟨x, y⟩, hx, hy⟩, refine ⟨λ hst, _, Ξ» h, h.1.prod h.2⟩, have : measurable_set ((Ξ» x, (x, y)) ⁻¹' s Γ—Λ’ t) := measurable_prod_mk_right hst, have : measurable_set (prod.mk x ⁻¹' s Γ—Λ’ t) := measurable_prod_mk_left hst, simp * at * end lemma measurable_set_prod {s : set Ξ±} {t : set Ξ²} : measurable_set (s Γ—Λ’ t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = βˆ… ∨ t = βˆ… := begin cases (s Γ—Λ’ t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.mp h] }, { simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] } end lemma measurable_set_swap_iff {s : set (Ξ± Γ— Ξ²)} : measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s := ⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, Ξ» hs, measurable_swap hs⟩ instance [measurable_singleton_class Ξ±] [measurable_singleton_class Ξ²] : measurable_singleton_class (Ξ± Γ— Ξ²) := ⟨λ ⟨a, b⟩, @singleton_prod_singleton _ _ a b β–Έ (measurable_set_singleton a).prod (measurable_set_singleton b)⟩ lemma measurable_from_prod_countable [countable Ξ²] [measurable_singleton_class Ξ²] {mΞ³ : measurable_space Ξ³} {f : Ξ± Γ— Ξ² β†’ Ξ³} (hf : βˆ€ y, measurable (Ξ» x, f (x, y))) : measurable f := begin intros s hs, have : f ⁻¹' s = ⋃ y, ((Ξ» x, f (x, y)) ⁻¹' s) Γ—Λ’ ({y} : set Ξ²), { ext1 ⟨x, y⟩, simp [and_assoc, and.left_comm] }, rw this, exact measurable_set.Union (Ξ» y, (hf y hs).prod (measurable_set_singleton y)) end /-- A piecewise function on countably many pieces is measurable if all the data is measurable. -/ @[measurability] lemma measurable.find {m : measurable_space Ξ±} {f : β„• β†’ Ξ± β†’ Ξ²} {p : β„• β†’ Ξ± β†’ Prop} [βˆ€ n, decidable_pred (p n)] (hf : βˆ€ n, measurable (f n)) (hp : βˆ€ n, measurable_set {x | p n x}) (h : βˆ€ x, βˆƒ n, p n x) : measurable (Ξ» x, f (nat.find (h x)) x) := begin have : measurable (Ξ» (p : Ξ± Γ— β„•), f p.2 p.1) := measurable_from_prod_countable (Ξ» n, hf n), exact this.comp (measurable.prod_mk measurable_id (measurable_find h hp)), end /-- Given countably many disjoint measurable sets `t n` and countably many measurable functions `g n`, one can construct a measurable function that coincides with `g n` on `t n`. -/ lemma exists_measurable_piecewise_nat {m : measurable_space Ξ±} (t : β„• β†’ set Ξ²) (t_meas : βˆ€ n, measurable_set (t n)) (t_disj : pairwise (disjoint on t)) (g : β„• β†’ Ξ² β†’ Ξ±) (hg : βˆ€ n, measurable (g n)) : βˆƒ f : Ξ² β†’ Ξ±, measurable f ∧ (βˆ€ n x, x ∈ t n β†’ f x = g n x) := begin classical, let p : β„• β†’ Ξ² β†’ Prop := Ξ» n x, x ∈ t n βˆͺ (⋃ k, t k)ᢜ, have M : βˆ€ n, measurable_set {x | p n x} := Ξ» n, (t_meas n).union (measurable_set.compl (measurable_set.Union t_meas)), have P : βˆ€ x, βˆƒ n, p n x, { assume x, by_cases H : βˆ€ (i : β„•), x βˆ‰ t i, { exact ⟨0, or.inr (by simpa only [mem_Inter, compl_Union] using H)⟩ }, { simp only [not_forall, not_not_mem] at H, rcases H with ⟨n, hn⟩, exact ⟨n, or.inl hn⟩ } }, refine ⟨λ x, g (nat.find (P x)) x, measurable.find hg M P, _⟩, assume n x hx, have : x ∈ t (nat.find (P x)), { have B : x ∈ t (nat.find (P x)) βˆͺ (⋃ k, t k)ᢜ := nat.find_spec (P x), have B' : (βˆ€ (i : β„•), x βˆ‰ t i) ↔ false, { simp only [iff_false, not_forall, not_not_mem], exact ⟨n, hx⟩ }, simpa only [B', mem_union, mem_Inter, or_false, compl_Union, mem_compl_iff] using B }, congr, by_contra h, exact (t_disj (ne.symm h)).le_bot ⟨hx, this⟩ end end prod section pi variables {Ο€ : Ξ΄ β†’ Type*} [measurable_space Ξ±] instance measurable_space.pi [m : Ξ  a, measurable_space (Ο€ a)] : measurable_space (Ξ  a, Ο€ a) := ⨆ a, (m a).comap (Ξ» b, b a) variables [Ξ  a, measurable_space (Ο€ a)] [measurable_space Ξ³] lemma measurable_pi_iff {g : Ξ± β†’ Ξ  a, Ο€ a} : measurable g ↔ βˆ€ a, measurable (Ξ» x, g x a) := by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr, measurable_space.comap_comp, function.comp, supr_le_iff] @[measurability] lemma measurable_pi_apply (a : Ξ΄) : measurable (Ξ» f : Ξ  a, Ο€ a, f a) := measurable.of_comap_le $ le_supr _ a @[measurability] lemma measurable.eval {a : Ξ΄} {g : Ξ± β†’ Ξ  a, Ο€ a} (hg : measurable g) : measurable (Ξ» x, g x a) := (measurable_pi_apply a).comp hg @[measurability] lemma measurable_pi_lambda (f : Ξ± β†’ Ξ  a, Ο€ a) (hf : βˆ€ a, measurable (Ξ» c, f c a)) : measurable f := measurable_pi_iff.mpr hf /-- The function `update f a : Ο€ a β†’ Ξ  a, Ο€ a` is always measurable. This doesn't require `f` to be measurable. This should not be confused with the statement that `update f a x` is measurable. -/ @[measurability] lemma measurable_update (f : Ξ  (a : Ξ΄), Ο€ a) {a : Ξ΄} [decidable_eq Ξ΄] : measurable (update f a) := begin apply measurable_pi_lambda, intro x, by_cases hx : x = a, { cases hx, convert measurable_id, ext, simp }, simp_rw [update_noteq hx], apply measurable_const, end /- Even though we cannot use projection notation, we still keep a dot to be consistent with similar lemmas, like `measurable_set.prod`. -/ @[measurability] lemma measurable_set.pi {s : set Ξ΄} {t : Ξ  i : Ξ΄, set (Ο€ i)} (hs : s.countable) (ht : βˆ€ i ∈ s, measurable_set (t i)) : measurable_set (s.pi t) := by { rw [pi_def], exact measurable_set.bInter hs (Ξ» i hi, measurable_pi_apply _ (ht i hi)) } lemma measurable_set.univ_pi [countable Ξ΄] {t : Ξ  i : Ξ΄, set (Ο€ i)} (ht : βˆ€ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi (to_countable _) (Ξ» i _, ht i) lemma measurable_set_pi_of_nonempty {s : set Ξ΄} {t : Ξ  i, set (Ο€ i)} (hs : s.countable) (h : (pi s t).nonempty) : measurable_set (pi s t) ↔ βˆ€ i ∈ s, measurable_set (t i) := begin classical, rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩, convert measurable_update f hst, rw [update_preimage_pi hi], exact Ξ» j hj _, hf j hj end lemma measurable_set_pi {s : set Ξ΄} {t : Ξ  i, set (Ο€ i)} (hs : s.countable) : measurable_set (pi s t) ↔ (βˆ€ i ∈ s, measurable_set (t i)) ∨ pi s t = βˆ… := begin cases (pi s t).eq_empty_or_nonempty with h h, { simp [h] }, { simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] } end instance [countable Ξ΄] [Ξ  a, measurable_singleton_class (Ο€ a)] : measurable_singleton_class (Ξ  a, Ο€ a) := ⟨λ f, univ_pi_singleton f β–Έ measurable_set.univ_pi (Ξ» t, measurable_set_singleton (f t))⟩ variable (Ο€) @[measurability] lemma measurable_pi_equiv_pi_subtype_prod_symm (p : Ξ΄ β†’ Prop) [decidable_pred p] : measurable (equiv.pi_equiv_pi_subtype_prod p Ο€).symm := begin apply measurable_pi_iff.2 (Ξ» j, _), by_cases hj : p j, { simp only [hj, dif_pos, equiv.pi_equiv_pi_subtype_prod_symm_apply], have : measurable (Ξ» (f : (Ξ  (i : {x // p x}), Ο€ ↑i)), f ⟨j, hj⟩) := measurable_pi_apply ⟨j, hj⟩, exact measurable.comp this measurable_fst }, { simp only [hj, equiv.pi_equiv_pi_subtype_prod_symm_apply, dif_neg, not_false_iff], have : measurable (Ξ» (f : (Ξ  (i : {x // Β¬ p x}), Ο€ ↑i)), f ⟨j, hj⟩) := measurable_pi_apply ⟨j, hj⟩, exact measurable.comp this measurable_snd } end @[measurability] lemma measurable_pi_equiv_pi_subtype_prod (p : Ξ΄ β†’ Prop) [decidable_pred p] : measurable (equiv.pi_equiv_pi_subtype_prod p Ο€) := begin refine measurable_prod.2 _, split; { apply measurable_pi_iff.2 (Ξ» j, _), simp only [pi_equiv_pi_subtype_prod_apply, measurable_pi_apply] } end end pi instance tprod.measurable_space (Ο€ : Ξ΄ β†’ Type*) [βˆ€ x, measurable_space (Ο€ x)] : βˆ€ (l : list Ξ΄), measurable_space (list.tprod Ο€ l) | [] := punit.measurable_space | (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is) section tprod open list variables {Ο€ : Ξ΄ β†’ Type*} [βˆ€ x, measurable_space (Ο€ x)] lemma measurable_tprod_mk (l : list Ξ΄) : measurable (@tprod.mk Ξ΄ Ο€ l) := begin induction l with i l ih, { exact measurable_const }, { exact (measurable_pi_apply i).prod_mk ih } end lemma measurable_tprod_elim [decidable_eq Ξ΄] : βˆ€ {l : list Ξ΄} {i : Ξ΄} (hi : i ∈ l), measurable (Ξ» (v : tprod Ο€ l), v.elim hi) | (i :: is) j hj := begin by_cases hji : j = i, { subst hji, simp [measurable_fst] }, { rw [funext $ tprod.elim_of_ne _ hji], exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd } end lemma measurable_tprod_elim' [decidable_eq Ξ΄] {l : list Ξ΄} (h : βˆ€ i, i ∈ l) : measurable (tprod.elim' h : tprod Ο€ l β†’ Ξ  i, Ο€ i) := measurable_pi_lambda _ (Ξ» i, measurable_tprod_elim (h i)) lemma measurable_set.tprod (l : list Ξ΄) {s : βˆ€ i, set (Ο€ i)} (hs : βˆ€ i, measurable_set (s i)) : measurable_set (set.tprod l s) := by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih } end tprod instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± βŠ• Ξ²) := m₁.map sum.inl βŠ“ mβ‚‚.map sum.inr section sum @[measurability] lemma measurable_inl [measurable_space Ξ±] [measurable_space Ξ²] : measurable (@sum.inl Ξ± Ξ²) := measurable.of_le_map inf_le_left @[measurability] lemma measurable_inr [measurable_space Ξ±] [measurable_space Ξ²] : measurable (@sum.inr Ξ± Ξ²) := measurable.of_le_map inf_le_right variables {m : measurable_space Ξ±} {mΞ² : measurable_space Ξ²} include m mΞ² lemma measurable_sum {mΞ³ : measurable_space Ξ³} {f : Ξ± βŠ• Ξ² β†’ Ξ³} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable.of_comap_le $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) @[measurability] lemma measurable.sum_elim {mΞ³ : measurable_space Ξ³} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (sum.elim f g) := measurable_sum hf hg lemma measurable_set.inl_image {s : set Ξ±} (hs : measurable_set s) : measurable_set (sum.inl '' s : set (Ξ± βŠ• Ξ²)) := ⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (Ξ» a b, sum.inl.inj) }, have sum.inr ⁻¹' (sum.inl '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩ lemma measurable_set_inr_image {s : set Ξ²} (hs : measurable_set s) : measurable_set (sum.inr '' s : set (Ξ± βŠ• Ξ²)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty }, show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact Ξ» a b, sum.inr.inj }⟩ omit m lemma measurable_set_range_inl [measurable_space Ξ±] : measurable_set (range sum.inl : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact measurable_set.univ.inl_image } lemma measurable_set_range_inr [measurable_space Ξ±] : measurable_set (range sum.inr : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ } end sum instance {Ξ±} {Ξ² : Ξ± β†’ Type*} [m : Ξ a, measurable_space (Ξ² a)] : measurable_space (sigma Ξ²) := β¨…a, (m a).map (sigma.mk a) end constructions /-- A map `f : Ξ± β†’ Ξ²` is called a *measurable embedding* if it is injective, measurable, and sends measurable sets to measurable sets. The latter assumption can be replaced with β€œ`f` has measurable inverse `g : range f β†’ Ξ±`”, see `measurable_embedding.measurable_range_splitting`, `measurable_embedding.of_measurable_inverse_range`, and `measurable_embedding.of_measurable_inverse`. One more interpretation: `f` is a measurable embedding if it defines a measurable equivalence to its range and the range is a measurable set. One implication is formalized as `measurable_embedding.equiv_range`; the other one follows from `measurable_equiv.measurable_embedding`, `measurable_embedding.subtype_coe`, and `measurable_embedding.comp`. -/ @[protect_proj] structure measurable_embedding {Ξ± Ξ² : Type*} [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β†’ Ξ²) : Prop := (injective : injective f) (measurable : measurable f) (measurable_set_image' : βˆ€ ⦃s⦄, measurable_set s β†’ measurable_set (f '' s)) namespace measurable_embedding variables {mΞ± : measurable_space Ξ±} [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} include mΞ± lemma measurable_set_image (hf : measurable_embedding f) {s : set Ξ±} : measurable_set (f '' s) ↔ measurable_set s := ⟨λ h, by simpa only [hf.injective.preimage_image] using hf.measurable h, Ξ» h, hf.measurable_set_image' h⟩ lemma id : measurable_embedding (id : Ξ± β†’ Ξ±) := ⟨injective_id, measurable_id, Ξ» s hs, by rwa image_id⟩ lemma comp (hg : measurable_embedding g) (hf : measurable_embedding f) : measurable_embedding (g ∘ f) := ⟨hg.injective.comp hf.injective, hg.measurable.comp hf.measurable, Ξ» s hs, by rwa [← image_image, hg.measurable_set_image, hf.measurable_set_image]⟩ lemma subtype_coe {s : set Ξ±} (hs : measurable_set s) : measurable_embedding (coe : s β†’ Ξ±) := { injective := subtype.coe_injective, measurable := measurable_subtype_coe, measurable_set_image' := Ξ» _, measurable_set.subtype_image hs } lemma measurable_set_range (hf : measurable_embedding f) : measurable_set (range f) := by { rw ← image_univ, exact hf.measurable_set_image' measurable_set.univ } lemma measurable_set_preimage (hf : measurable_embedding f) {s : set Ξ²} : measurable_set (f ⁻¹' s) ↔ measurable_set (s ∩ range f) := by rw [← image_preimage_eq_inter_range, hf.measurable_set_image] lemma measurable_range_splitting (hf : measurable_embedding f) : measurable (range_splitting f) := Ξ» s hs, by rwa [preimage_range_splitting hf.injective, ← (subtype_coe hf.measurable_set_range).measurable_set_image, ← image_comp, coe_comp_range_factorization, hf.measurable_set_image] lemma measurable_extend (hf : measurable_embedding f) {g : Ξ± β†’ Ξ³} {g' : Ξ² β†’ Ξ³} (hg : measurable g) (hg' : measurable g') : measurable (extend f g g') := begin refine measurable_of_restrict_of_restrict_compl hf.measurable_set_range _ _, { rw restrict_extend_range, simpa only [range_splitting] using hg.comp hf.measurable_range_splitting }, { rw restrict_extend_compl_range, exact hg'.comp measurable_subtype_coe } end lemma exists_measurable_extend (hf : measurable_embedding f) {g : Ξ± β†’ Ξ³} (hg : measurable g) (hne : Ξ² β†’ nonempty Ξ³) : βˆƒ g' : Ξ² β†’ Ξ³, measurable g' ∧ g' ∘ f = g := ⟨extend f g (Ξ» x, classical.choice (hne x)), hf.measurable_extend hg (measurable_const' $ Ξ» _ _, rfl), funext $ Ξ» x, hf.injective.extend_apply _ _ _⟩ lemma measurable_comp_iff (hg : measurable_embedding g) : measurable (g ∘ f) ↔ measurable f := begin refine ⟨λ H, _, hg.measurable.comp⟩, suffices : measurable ((range_splitting g ∘ range_factorization g) ∘ f), by rwa [(right_inverse_range_splitting hg.injective).comp_eq_id] at this, exact hg.measurable_range_splitting.comp H.subtype_mk end end measurable_embedding lemma measurable_set.exists_measurable_proj {m : measurable_space Ξ±} {s : set Ξ±} (hs : measurable_set s) (hne : s.nonempty) : βˆƒ f : Ξ± β†’ s, measurable f ∧ βˆ€ x : s, f x = x := let ⟨f, hfm, hf⟩ := (measurable_embedding.subtype_coe hs).exists_measurable_extend measurable_id (Ξ» _, hne.to_subtype) in ⟨f, hfm, congr_fun hf⟩ /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (Ξ± Ξ² : Type*) [measurable_space Ξ±] [measurable_space Ξ²] extends Ξ± ≃ Ξ² := (measurable_to_fun : measurable to_equiv) (measurable_inv_fun : measurable to_equiv.symm) infix ` ≃ᡐ `:25 := measurable_equiv namespace measurable_equiv variables (Ξ± Ξ²) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] instance : has_coe_to_fun (Ξ± ≃ᡐ Ξ²) (Ξ» _, Ξ± β†’ Ξ²) := ⟨λ e, e.to_fun⟩ variables {Ξ± Ξ²} @[simp] lemma coe_to_equiv (e : Ξ± ≃ᡐ Ξ²) : (e.to_equiv : Ξ± β†’ Ξ²) = e := rfl @[measurability] protected lemma measurable (e : Ξ± ≃ᡐ Ξ²) : measurable (e : Ξ± β†’ Ξ²) := e.measurable_to_fun @[simp] lemma coe_mk (e : Ξ± ≃ Ξ²) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : Ξ± ≃ᡐ Ξ²) : Ξ± β†’ Ξ²) = e := rfl /-- Any measurable space is equivalent to itself. -/ def refl (Ξ± : Type*) [measurable_space Ξ±] : Ξ± ≃ᡐ Ξ± := { to_equiv := equiv.refl Ξ±, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } instance : inhabited (Ξ± ≃ᡐ Ξ±) := ⟨refl α⟩ /-- The composition of equivalences between measurable spaces. -/ def trans (ab : Ξ± ≃ᡐ Ξ²) (bc : Ξ² ≃ᡐ Ξ³) : Ξ± ≃ᡐ Ξ³ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } /-- The inverse of an equivalence between measurable spaces. -/ def symm (ab : Ξ± ≃ᡐ Ξ²) : Ξ² ≃ᡐ Ξ± := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } @[simp] lemma coe_to_equiv_symm (e : Ξ± ≃ᡐ Ξ²) : (e.to_equiv.symm : Ξ² β†’ Ξ±) = e.symm := rfl /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : Ξ± ≃ᡐ Ξ²) : Ξ± β†’ Ξ² := h /-- See Note [custom simps projection] -/ def simps.symm_apply (h : Ξ± ≃ᡐ Ξ²) : Ξ² β†’ Ξ± := h.symm initialize_simps_projections measurable_equiv (to_equiv_to_fun β†’ apply, to_equiv_inv_fun β†’ symm_apply) lemma to_equiv_injective : injective (to_equiv : (Ξ± ≃ᡐ Ξ²) β†’ (Ξ± ≃ Ξ²)) := by { rintro ⟨e₁, _, _⟩ ⟨eβ‚‚, _, _⟩ (rfl : e₁ = eβ‚‚), refl } @[ext] lemma ext {e₁ eβ‚‚ : Ξ± ≃ᡐ Ξ²} (h : (e₁ : Ξ± β†’ Ξ²) = eβ‚‚) : e₁ = eβ‚‚ := to_equiv_injective $ equiv.coe_fn_injective h @[simp] lemma symm_mk (e : Ξ± ≃ Ξ²) (h1 : measurable e) (h2 : measurable e.symm) : (⟨e, h1, h2⟩ : Ξ± ≃ᡐ Ξ²).symm = ⟨e.symm, h2, h1⟩ := rfl attribute [simps apply to_equiv] trans refl @[simp] lemma symm_refl (Ξ± : Type*) [measurable_space Ξ±] : (refl Ξ±).symm = refl Ξ± := rfl @[simp] theorem symm_comp_self (e : Ξ± ≃ᡐ Ξ²) : e.symm ∘ e = id := funext e.left_inv @[simp] theorem self_comp_symm (e : Ξ± ≃ᡐ Ξ²) : e ∘ e.symm = id := funext e.right_inv @[simp] theorem apply_symm_apply (e : Ξ± ≃ᡐ Ξ²) (y : Ξ²) : e (e.symm y) = y := e.right_inv y @[simp] theorem symm_apply_apply (e : Ξ± ≃ᡐ Ξ²) (x : Ξ±) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_trans_self (e : Ξ± ≃ᡐ Ξ²) : e.symm.trans e = refl Ξ² := ext e.self_comp_symm @[simp] theorem self_trans_symm (e : Ξ± ≃ᡐ Ξ²) : e.trans e.symm = refl Ξ± := ext e.symm_comp_self protected theorem surjective (e : Ξ± ≃ᡐ Ξ²) : surjective e := e.to_equiv.surjective protected theorem bijective (e : Ξ± ≃ᡐ Ξ²) : bijective e := e.to_equiv.bijective protected theorem injective (e : Ξ± ≃ᡐ Ξ²) : injective e := e.to_equiv.injective @[simp] theorem symm_preimage_preimage (e : Ξ± ≃ᡐ Ξ²) (s : set Ξ²) : e.symm ⁻¹' (e ⁻¹' s) = s := e.to_equiv.symm_preimage_preimage s theorem image_eq_preimage (e : Ξ± ≃ᡐ Ξ²) (s : set Ξ±) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s @[simp] theorem measurable_set_preimage (e : Ξ± ≃ᡐ Ξ²) {s : set Ξ²} : measurable_set (e ⁻¹' s) ↔ measurable_set s := ⟨λ h, by simpa only [symm_preimage_preimage] using e.symm.measurable h, Ξ» h, e.measurable h⟩ @[simp] theorem measurable_set_image (e : Ξ± ≃ᡐ Ξ²) {s : set Ξ±} : measurable_set (e '' s) ↔ measurable_set s := by rw [image_eq_preimage, measurable_set_preimage] /-- A measurable equivalence is a measurable embedding. -/ protected lemma measurable_embedding (e : Ξ± ≃ᡐ Ξ²) : measurable_embedding e := { injective := e.injective, measurable := e.measurable, measurable_set_image' := Ξ» s, e.measurable_set_image.2 } /-- Equal measurable spaces are equivalent. -/ protected def cast {Ξ± Ξ²} [i₁ : measurable_space Ξ±] [iβ‚‚ : measurable_space Ξ²] (h : Ξ± = Ξ²) (hi : i₁ == iβ‚‚) : Ξ± ≃ᡐ Ξ² := { to_equiv := equiv.cast h, measurable_to_fun := by { substI h, substI hi, exact measurable_id }, measurable_inv_fun := by { substI h, substI hi, exact measurable_id }} protected lemma measurable_comp_iff {f : Ξ² β†’ Ξ³} (e : Ξ± ≃ᡐ Ξ²) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [coe_to_equiv, symm_trans_self] at this) (Ξ» h, h.comp e.measurable) /-- Any two types with unique elements are measurably equivalent. -/ def of_unique_of_unique (Ξ± Ξ² : Type*) [measurable_space Ξ±] [measurable_space Ξ²] [unique Ξ±] [unique Ξ²] : Ξ± ≃ᡐ Ξ² := { to_equiv := equiv_of_unique Ξ± Ξ², measurable_to_fun := subsingleton.measurable, measurable_inv_fun := subsingleton.measurable } /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr (ab : Ξ± ≃ᡐ Ξ²) (cd : Ξ³ ≃ᡐ Ξ΄) : Ξ± Γ— Ξ³ ≃ᡐ Ξ² Γ— Ξ΄ := { to_equiv := prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk (cd.measurable_to_fun.comp measurable_id.snd), measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk (cd.measurable_inv_fun.comp measurable_id.snd) } /-- Products of measurable spaces are symmetric. -/ def prod_comm : Ξ± Γ— Ξ² ≃ᡐ Ξ² Γ— Ξ± := { to_equiv := prod_comm Ξ± Ξ², measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst, measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst } /-- Products of measurable spaces are associative. -/ def prod_assoc : (Ξ± Γ— Ξ²) Γ— Ξ³ ≃ᡐ Ξ± Γ— (Ξ² Γ— Ξ³) := { to_equiv := prod_assoc Ξ± Ξ² Ξ³, measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd, measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd } /-- Sums of measurable spaces are symmetric. -/ def sum_congr (ab : Ξ± ≃ᡐ Ξ²) (cd : Ξ³ ≃ᡐ Ξ΄) : Ξ± βŠ• Ξ³ ≃ᡐ Ξ² βŠ• Ξ΄ := { to_equiv := sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `s Γ—Λ’ t ≃ (s Γ— t)` as measurable spaces. -/ def set.prod (s : set Ξ±) (t : set Ξ²) : β†₯(s Γ—Λ’ t) ≃ᡐ s Γ— t := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk measurable_id.snd.subtype_coe } /-- `univ Ξ± ≃ Ξ±` as measurable spaces. -/ def set.univ (Ξ± : Type*) [measurable_space Ξ±] : (univ : set Ξ±) ≃ᡐ Ξ± := { to_equiv := equiv.set.univ Ξ±, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton (a : Ξ±) : ({a} : set Ξ±) ≃ᡐ unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- `Ξ±` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inl : (range sum.inl : set (Ξ± βŠ• Ξ²)) ≃ᡐ Ξ± := { to_fun := Ξ» ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» a, ⟨sum.inl a, a, rfl⟩, left_inv := by { rintro ⟨ab, a, rfl⟩, refl }, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, hs.inl_image, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `Ξ²` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inr : (range sum.inr : set (Ξ± βŠ• Ξ²)) ≃ᡐ Ξ² := { to_fun := Ξ» ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» b, ⟨sum.inr b, b, rfl⟩, left_inv := by { rintro ⟨ab, b, rfl⟩, refl }, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, measurable_set_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : (Ξ± βŠ• Ξ²) Γ— Ξ³ ≃ᡐ (Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³) := { to_equiv := sum_prod_distrib Ξ± Ξ² Ξ³, measurable_to_fun := begin refine measurable_of_measurable_union_cover (range sum.inl Γ—Λ’ (univ : set Ξ³)) (range sum.inr Γ—Λ’ (univ : set Ξ³)) (measurable_set_range_inl.prod measurable_set.univ) (measurable_set_range_inr.prod measurable_set.univ) (by { rintro ⟨a|b, c⟩; simp [set.prod_eq] }) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_comp_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_comp_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_comp_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_comp_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp measurable_fst).prod_mk measurable_snd) ((measurable_inr.comp measurable_fst).prod_mk measurable_snd) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ᡐ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (Ξ± Ξ² Ξ³ Ξ΄) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] : (Ξ± βŠ• Ξ²) Γ— (Ξ³ βŠ• Ξ΄) ≃ᡐ ((Ξ± Γ— Ξ³) βŠ• (Ξ± Γ— Ξ΄)) βŠ• ((Ξ² Γ— Ξ³) βŠ• (Ξ² Γ— Ξ΄)) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) variables {Ο€ Ο€' : Ξ΄' β†’ Type*} [βˆ€ x, measurable_space (Ο€ x)] [βˆ€ x, measurable_space (Ο€' x)] /-- A family of measurable equivalences `Ξ  a, β₁ a ≃ᡐ Ξ²β‚‚ a` generates a measurable equivalence between `Ξ  a, β₁ a` and `Ξ  a, Ξ²β‚‚ a`. -/ def Pi_congr_right (e : Ξ  a, Ο€ a ≃ᡐ Ο€' a) : (Ξ  a, Ο€ a) ≃ᡐ (Ξ  a, Ο€' a) := { to_equiv := Pi_congr_right (Ξ» a, (e a).to_equiv), measurable_to_fun := measurable_pi_lambda _ (Ξ» i, (e i).measurable_to_fun.comp (measurable_pi_apply i)), measurable_inv_fun := measurable_pi_lambda _ (Ξ» i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) } /-- Pi-types are measurably equivalent to iterated products. -/ @[simps {fully_applied := ff}] def pi_measurable_equiv_tprod [decidable_eq Ξ΄'] {l : list Ξ΄'} (hnd : l.nodup) (h : βˆ€ i, i ∈ l) : (Ξ  i, Ο€ i) ≃ᡐ list.tprod Ο€ l := { to_equiv := list.tprod.pi_equiv_tprod hnd h, measurable_to_fun := measurable_tprod_mk l, measurable_inv_fun := measurable_tprod_elim' h } /-- If `Ξ±` has a unique term, then the type of function `Ξ± β†’ Ξ²` is measurably equivalent to `Ξ²`. -/ @[simps {fully_applied := ff}] def fun_unique (Ξ± Ξ² : Type*) [unique Ξ±] [measurable_space Ξ²] : (Ξ± β†’ Ξ²) ≃ᡐ Ξ² := { to_equiv := equiv.fun_unique Ξ± Ξ², measurable_to_fun := measurable_pi_apply _, measurable_inv_fun := measurable_pi_iff.2 $ Ξ» b, measurable_id } /-- The space `Ξ  i : fin 2, Ξ± i` is measurably equivalent to `Ξ± 0 Γ— Ξ± 1`. -/ @[simps {fully_applied := ff}] def pi_fin_two (Ξ± : fin 2 β†’ Type*) [βˆ€ i, measurable_space (Ξ± i)] : (Ξ  i, Ξ± i) ≃ᡐ Ξ± 0 Γ— Ξ± 1 := { to_equiv := pi_fin_two_equiv Ξ±, measurable_to_fun := measurable.prod (measurable_pi_apply _) (measurable_pi_apply _), measurable_inv_fun := measurable_pi_iff.2 $ fin.forall_fin_two.2 ⟨measurable_fst, measurable_snd⟩ } /-- The space `fin 2 β†’ Ξ±` is measurably equivalent to `Ξ± Γ— Ξ±`. -/ @[simps {fully_applied := ff}] def fin_two_arrow : (fin 2 β†’ Ξ±) ≃ᡐ Ξ± Γ— Ξ± := pi_fin_two (Ξ» _, Ξ±) /-- Measurable equivalence between `Ξ  j : fin (n + 1), Ξ± j` and `Ξ± i Γ— Ξ  j : fin n, Ξ± (fin.succ_above i j)`. -/ @[simps {fully_applied := ff}] def pi_fin_succ_above_equiv {n : β„•} (Ξ± : fin (n + 1) β†’ Type*) [Ξ  i, measurable_space (Ξ± i)] (i : fin (n + 1)) : (Ξ  j, Ξ± j) ≃ᡐ Ξ± i Γ— (Ξ  j, Ξ± (i.succ_above j)) := { to_equiv := pi_fin_succ_above_equiv Ξ± i, measurable_to_fun := (measurable_pi_apply i).prod_mk $ measurable_pi_iff.2 $ Ξ» j, measurable_pi_apply _, measurable_inv_fun := by simp [measurable_pi_iff, i.forall_iff_succ_above, measurable_fst, (measurable_pi_apply _).comp measurable_snd] } variable (Ο€) /-- Measurable equivalence between (dependent) functions on a type and pairs of functions on `{i // p i}` and `{i // Β¬p i}`. See also `equiv.pi_equiv_pi_subtype_prod`. -/ @[simps {fully_applied := ff}] def pi_equiv_pi_subtype_prod (p : Ξ΄' β†’ Prop) [decidable_pred p] : (Ξ  i, Ο€ i) ≃ᡐ ((Ξ  i : subtype p, Ο€ i) Γ— (Ξ  i : {i // Β¬p i}, Ο€ i)) := { to_equiv := pi_equiv_pi_subtype_prod p Ο€, measurable_to_fun := measurable_pi_equiv_pi_subtype_prod Ο€ p, measurable_inv_fun := measurable_pi_equiv_pi_subtype_prod_symm Ο€ p } /-- If `s` is a measurable set in a measurable space, that space is equivalent to the sum of `s` and `sᢜ`.-/ def sum_compl {s : set Ξ±} [decidable_pred s] (hs : measurable_set s) : s βŠ• (sᢜ : set Ξ±) ≃ᡐ Ξ± := { to_equiv := sum_compl s, measurable_to_fun := by {apply measurable.sum_elim; exact measurable_subtype_coe}, measurable_inv_fun := measurable.dite measurable_inl measurable_inr hs } end measurable_equiv namespace measurable_embedding variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is a measurable embedding -/ noncomputable def equiv_image (s : set Ξ±) (hf : measurable_embedding f) : s ≃ᡐ (f '' s) := { to_equiv := equiv.set.image f s hf.injective, measurable_to_fun := (hf.measurable.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := begin rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf.injective], exact measurable_subtype_coe (hf.measurable_set_image' hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is a measurable embedding -/ noncomputable def equiv_range (hf : measurable_embedding f) : Ξ± ≃ᡐ (range f) := (measurable_equiv.set.univ _).symm.trans $ (hf.equiv_image univ).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) lemma of_measurable_inverse_on_range {g : range f β†’ Ξ±} (hf₁ : measurable f) (hfβ‚‚ : measurable_set (range f)) (hg : measurable g) (H : left_inverse g (range_factorization f)) : measurable_embedding f := begin set e : Ξ± ≃ᡐ range f := ⟨⟨range_factorization f, g, H, H.right_inverse_of_surjective surjective_onto_range⟩, hf₁.subtype_mk, hg⟩, exact (measurable_embedding.subtype_coe hfβ‚‚).comp e.measurable_embedding end lemma of_measurable_inverse (hf₁ : measurable f) (hfβ‚‚ : measurable_set (range f)) (hg : measurable g) (H : left_inverse g f) : measurable_embedding f := of_measurable_inverse_on_range hf₁ hfβ‚‚ (hg.comp measurable_subtype_coe) H open_locale classical /-- The **`measurable SchrΓΆder-Bernstein Theorem**: Given measurable embeddings `Ξ± β†’ Ξ²` and `Ξ² β†’ Ξ±`, we can find a measurable equivalence `Ξ± ≃ᡐ Ξ²`.-/ noncomputable def schroeder_bernstein {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} (hf : measurable_embedding f)(hg : measurable_embedding g) : Ξ± ≃ᡐ Ξ² := begin let F : set Ξ± β†’ set Ξ± := Ξ» A, (g '' (f '' A)ᢜ)ᢜ, -- We follow the proof of the usual SB theorem in mathlib, -- the crux of which is finding a fixed point of this F. -- However, we must find this fixed point manually instead of invoking Knaster-Tarski -- in order to make sure it is measurable. suffices : Ξ£' A : set Ξ±, measurable_set A ∧ F A = A, { rcases this with ⟨A, Ameas, Afp⟩, let B := f '' A, have Bmeas : measurable_set B := hf.measurable_set_image' Ameas, refine (measurable_equiv.sum_compl Ameas).symm.trans (measurable_equiv.trans _ (measurable_equiv.sum_compl Bmeas)), apply measurable_equiv.sum_congr (hf.equiv_image _), have : Aᢜ = g '' Bᢜ, { apply compl_injective, rw ← Afp, simp, }, rw this, exact (hg.equiv_image _).symm, }, have Fmono : βˆ€ {A B}, A βŠ† B β†’ F A βŠ† F B := Ξ» A B hAB, compl_subset_compl.mpr $ set.image_subset _ $ compl_subset_compl.mpr $ set.image_subset _ hAB, let X : β„• β†’ set Ξ± := Ξ» n, F^[n] univ, refine ⟨Inter X, _, _⟩, { apply measurable_set.Inter, intros n, induction n with n ih, { exact measurable_set.univ }, rw [function.iterate_succ', function.comp_apply], exact (hg.measurable_set_image' (hf.measurable_set_image' ih).compl).compl, }, apply subset_antisymm, { apply subset_Inter, intros n, cases n, { exact subset_univ _ }, rw [function.iterate_succ', function.comp_apply], exact Fmono (Inter_subset _ _ ), }, rintros x hx ⟨y, hy, rfl⟩, rw mem_Inter at hx, apply hy, rw (inj_on_of_injective hf.injective _).image_Inter_eq, swap, { apply_instance }, rw mem_Inter, intro n, specialize hx n.succ, rw [function.iterate_succ', function.comp_apply] at hx, by_contradiction h, apply hx, exact ⟨y, h, rfl⟩, end end measurable_embedding section countably_generated namespace measurable_space variable (Ξ±) /-- We say a measurable space is countably generated if can be generated by a countable set of sets.-/ class countably_generated [m : measurable_space Ξ±] : Prop := (is_countably_generated : βˆƒ b : set (set Ξ±), b.countable ∧ m = generate_from b) open_locale classical /-- If a measurable space is countably generated, it admits a measurable injection into the Cantor space `β„• β†’ bool` (equipped with the product sigma algebra). -/ theorem measurable_injection_nat_bool_of_countably_generated [measurable_space Ξ±] [h : countably_generated Ξ±] [measurable_singleton_class Ξ±] : βˆƒ f : Ξ± β†’ (β„• β†’ bool), measurable f ∧ function.injective f := begin obtain ⟨b, bct, hb⟩ := h.is_countably_generated, obtain ⟨e, he⟩ := set.countable.exists_eq_range (bct.insert βˆ…) (insert_nonempty _ _), rw [← generate_from_insert_empty, he] at hb, refine ⟨λ x n, to_bool (x ∈ e n), _, _⟩, { rw measurable_pi_iff, intro n, apply measurable_to_bool, simp only [preimage, mem_singleton_iff, to_bool_iff, set_of_mem_eq], rw hb, apply measurable_set_generate_from, use n, }, intros x y hxy, have : βˆ€ s : set Ξ±, measurable_set s β†’ (x ∈ s ↔ y ∈ s) := Ξ» s, by { rw hb, apply generate_from_induction, { rintros - ⟨n, rfl⟩, rw ← bool.to_bool_eq, rw funext_iff at hxy, exact hxy n }, { tauto }, { intro t, tauto }, intros t ht, simp_rw [mem_Union, ht], }, specialize this {y} measurable_set_eq, simpa only [mem_singleton, iff_true], end end measurable_space end countably_generated namespace filter variables [measurable_space Ξ±] /-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/ class is_measurably_generated (f : filter Ξ±) : Prop := (exists_measurable_subset : βˆ€ ⦃s⦄, s ∈ f β†’ βˆƒ t ∈ f, measurable_set t ∧ t βŠ† s) instance is_measurably_generated_bot : is_measurably_generated (βŠ₯ : filter Ξ±) := ⟨λ _ _, βŸ¨βˆ…, mem_bot, measurable_set.empty, empty_subset _⟩⟩ instance is_measurably_generated_top : is_measurably_generated (⊀ : filter Ξ±) := ⟨λ s hs, ⟨univ, univ_mem, measurable_set.univ, Ξ» x _, hs x⟩⟩ lemma eventually.exists_measurable_mem {f : filter Ξ±} [is_measurably_generated f] {p : Ξ± β†’ Prop} (h : βˆ€αΆ  x in f, p x) : βˆƒ s ∈ f, measurable_set s ∧ βˆ€ x ∈ s, p x := is_measurably_generated.exists_measurable_subset h lemma eventually.exists_measurable_mem_of_small_sets {f : filter Ξ±} [is_measurably_generated f] {p : set Ξ± β†’ Prop} (h : βˆ€αΆ  s in f.small_sets, p s) : βˆƒ s ∈ f, measurable_set s ∧ p s := let ⟨s, hsf, hs⟩ := eventually_small_sets.1 h, ⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf in ⟨t, htf, htm, hs t hts⟩ instance inf_is_measurably_generated (f g : filter Ξ±) [is_measurably_generated f] [is_measurably_generated g] : is_measurably_generated (f βŠ“ g) := begin refine ⟨_⟩, rintros t ⟨sf, hsf, sg, hsg, rfl⟩, rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩, rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩, refine ⟨s'f ∩ s'g, inter_mem_inf hs'f hs'g, hmf.inter hmg, _⟩, exact inter_subset_inter hs'sf hs'sg end lemma principal_is_measurably_generated_iff {s : set Ξ±} : is_measurably_generated (π“Ÿ s) ↔ measurable_set s := begin refine ⟨_, Ξ» hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩, rintros ⟨hs⟩, rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩, have : t = s := subset.antisymm hts ht, rwa ← this end alias principal_is_measurably_generated_iff ↔ _ _root_.measurable_set.principal_is_measurably_generated instance infi_is_measurably_generated {f : ΞΉ β†’ filter Ξ±} [βˆ€ i, is_measurably_generated (f i)] : is_measurably_generated (β¨… i, f i) := begin refine ⟨λ s hs, _⟩, rw [← equiv.plift.surjective.infi_comp, mem_infi] at hs, rcases hs with ⟨t, ht, ⟨V, hVf, rfl⟩⟩, choose U hUf hU using Ξ» i, is_measurably_generated.exists_measurable_subset (hVf i), refine βŸ¨β‹‚ i : t, U i, _, _, _⟩, { rw [← equiv.plift.surjective.infi_comp, mem_infi], refine ⟨t, ht, U, hUf, rfl⟩ }, { haveI := ht.countable.to_encodable, exact measurable_set.Inter (Ξ» i, (hU i).1) }, { exact Inter_mono (Ξ» i, (hU i).2) } end end filter /-- We say that a collection of sets is countably spanning if a countable subset spans the whole type. This is a useful condition in various parts of measure theory. For example, it is a needed condition to show that the product of two collections generate the product sigma algebra, see `generate_from_prod_eq`. -/ def is_countably_spanning (C : set (set Ξ±)) : Prop := βˆƒ (s : β„• β†’ set Ξ±), (βˆ€ n, s n ∈ C) ∧ (⋃ n, s n) = univ lemma is_countably_spanning_measurable_set [measurable_space Ξ±] : is_countably_spanning {s : set Ξ± | measurable_set s} := ⟨λ _, univ, Ξ» _, measurable_set.univ, Union_const _⟩ namespace measurable_set /-! ### Typeclasses on `subtype measurable_set` -/ variables [measurable_space Ξ±] instance : has_mem Ξ± (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ a s, a ∈ (s : set Ξ±)⟩ @[simp] lemma mem_coe (a : Ξ±) (s : subtype (measurable_set : set Ξ± β†’ Prop)) : a ∈ (s : set Ξ±) ↔ a ∈ s := iff.rfl instance : has_emptyc (subtype (measurable_set : set Ξ± β†’ Prop)) := βŸ¨βŸ¨βˆ…, measurable_set.empty⟩⟩ @[simp] lemma coe_empty : ↑(βˆ… : subtype (measurable_set : set Ξ± β†’ Prop)) = (βˆ… : set Ξ±) := rfl instance [measurable_singleton_class Ξ±] : has_insert Ξ± (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ a s, ⟨has_insert.insert a s, s.prop.insert a⟩⟩ @[simp] lemma coe_insert [measurable_singleton_class Ξ±] (a : Ξ±) (s : subtype (measurable_set : set Ξ± β†’ Prop)) : ↑(has_insert.insert a s) = (has_insert.insert a s : set Ξ±) := rfl instance : has_compl (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ x, ⟨xᢜ, x.prop.compl⟩⟩ @[simp] lemma coe_compl (s : subtype (measurable_set : set Ξ± β†’ Prop)) : ↑(sᢜ) = (sᢜ : set Ξ±) := rfl instance : has_union (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ x y, ⟨x βˆͺ y, x.prop.union y.prop⟩⟩ @[simp] lemma coe_union (s t : subtype (measurable_set : set Ξ± β†’ Prop)) : ↑(s βˆͺ t) = (s βˆͺ t : set Ξ±) := rfl instance : has_inter (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ x y, ⟨x ∩ y, x.prop.inter y.prop⟩⟩ @[simp] lemma coe_inter (s t : subtype (measurable_set : set Ξ± β†’ Prop)) : ↑(s ∩ t) = (s ∩ t : set Ξ±) := rfl instance : has_sdiff (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨λ x y, ⟨x \ y, x.prop.diff y.prop⟩⟩ @[simp] lemma coe_sdiff (s t : subtype (measurable_set : set Ξ± β†’ Prop)) : ↑(s \ t) = (s \ t : set Ξ±) := rfl instance : has_bot (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨⟨βŠ₯, measurable_set.empty⟩⟩ @[simp] lemma coe_bot : ↑(βŠ₯ : subtype (measurable_set : set Ξ± β†’ Prop)) = (βŠ₯ : set Ξ±) := rfl instance : has_top (subtype (measurable_set : set Ξ± β†’ Prop)) := ⟨⟨⊀, measurable_set.univ⟩⟩ @[simp] lemma coe_top : ↑(⊀ : subtype (measurable_set : set Ξ± β†’ Prop)) = (⊀ : set Ξ±) := rfl instance : partial_order (subtype (measurable_set : set Ξ± β†’ Prop)) := partial_order.lift _ subtype.coe_injective instance : distrib_lattice (subtype (measurable_set : set Ξ± β†’ Prop)) := { sup := (βˆͺ), le_sup_left := Ξ» a b, show (a : set Ξ±) ≀ a βŠ” b, from le_sup_left, le_sup_right := Ξ» a b, show (b : set Ξ±) ≀ a βŠ” b, from le_sup_right, sup_le := Ξ» a b c ha hb, show (a βŠ” b : set Ξ±) ≀ c, from sup_le ha hb, inf := (∩), inf_le_left := Ξ» a b, show (a βŠ“ b : set Ξ±) ≀ a, from inf_le_left, inf_le_right := Ξ» a b, show (a βŠ“ b : set Ξ±) ≀ b, from inf_le_right, le_inf := Ξ» a b c ha hb, show (a : set Ξ±) ≀ b βŠ“ c, from le_inf ha hb, le_sup_inf := Ξ» x y z, show ((x βŠ” y) βŠ“ (x βŠ” z) : set Ξ±) ≀ x βŠ” y βŠ“ z, from le_sup_inf, .. measurable_set.subtype.partial_order } instance : bounded_order (subtype (measurable_set : set Ξ± β†’ Prop)) := { top := ⊀, le_top := Ξ» a, show (a : set Ξ±) ≀ ⊀, from le_top, bot := βŠ₯, bot_le := Ξ» a, show (βŠ₯ : set Ξ±) ≀ a, from bot_le } instance : boolean_algebra (subtype (measurable_set : set Ξ± β†’ Prop)) := { sdiff := (\), compl := has_compl.compl, inf_compl_le_bot := Ξ» a, boolean_algebra.inf_compl_le_bot (a : set Ξ±), top_le_sup_compl := Ξ» a, boolean_algebra.top_le_sup_compl (a : set Ξ±), sdiff_eq := Ξ» a b, subtype.eq $ sdiff_eq, .. measurable_set.subtype.bounded_order, .. measurable_set.subtype.distrib_lattice } @[measurability] lemma measurable_set_blimsup {s : β„• β†’ set Ξ±} {p : β„• β†’ Prop} (h : βˆ€ n, p n β†’ measurable_set (s n)) : measurable_set $ filter.blimsup s filter.at_top p := begin simp only [filter.blimsup_eq_infi_bsupr_of_nat, supr_eq_Union, infi_eq_Inter], exact measurable_set.Inter (Ξ» n, measurable_set.Union (Ξ» m, measurable_set.Union $ Ξ» hm, h m hm.1)), end @[measurability] lemma measurable_set_bliminf {s : β„• β†’ set Ξ±} {p : β„• β†’ Prop} (h : βˆ€ n, p n β†’ measurable_set (s n)) : measurable_set $ filter.bliminf s filter.at_top p := begin simp only [filter.bliminf_eq_supr_binfi_of_nat, infi_eq_Inter, supr_eq_Union], exact measurable_set.Union (Ξ» n, measurable_set.Inter (Ξ» m, measurable_set.Inter $ Ξ» hm, h m hm.1)), end @[measurability] lemma measurable_set_limsup {s : β„• β†’ set Ξ±} (hs : βˆ€ n, measurable_set $ s n) : measurable_set $ filter.limsup s filter.at_top := begin convert measurable_set_blimsup (Ξ» n h, hs n : βˆ€ n, true β†’ measurable_set (s n)), simp, end @[measurability] lemma measurable_set_liminf {s : β„• β†’ set Ξ±} (hs : βˆ€ n, measurable_set $ s n) : measurable_set $ filter.liminf s filter.at_top := begin convert measurable_set_bliminf (Ξ» n h, hs n : βˆ€ n, true β†’ measurable_set (s n)), simp, end end measurable_set
682b1cc32d458a9389d0a68130b6d999f0cc9df2
4727251e0cd73359b15b664c3170e5d754078599
/src/data/polynomial/algebra_map.lean
e91fee5d30a3ae933478f7ca888bfad3038a4c20
[ "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
13,840
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes HΓΆlzl, Scott Morrison, Jens Wagemaker -/ import ring_theory.adjoin.basic import data.polynomial.eval /-! # Theory of univariate polynomials We show that `polynomial A` is an R-algebra when `A` is an R-algebra. We promote `evalβ‚‚` to an algebra hom in `aeval`. -/ noncomputable theory open finset open_locale big_operators polynomial namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {A' B' : Type*} {a b : R} {n : β„•} variables [comm_semiring A'] [semiring B'] section comm_semiring variables [comm_semiring R] {p q r : R[X]} variables [semiring A] [algebra R A] /-- Note that this instance also provides `algebra R R[X]`. -/ instance algebra_of_algebra : algebra R (polynomial A) := { smul_def' := Ξ» r p, to_finsupp_injective $ begin dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply], rw [to_finsupp_smul, to_finsupp_mul, to_finsupp_C], exact algebra.smul_def' _ _, end, commutes' := Ξ» r p, to_finsupp_injective $ begin dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply], simp_rw [to_finsupp_mul, to_finsupp_C], convert algebra.commutes' r p.to_finsupp, end, to_ring_hom := C.comp (algebra_map R A) } lemma algebra_map_apply (r : R) : algebra_map R (polynomial A) r = C (algebra_map R A r) := rfl @[simp] lemma to_finsupp_algebra_map (r : R) : (algebra_map R (polynomial A) r).to_finsupp = algebra_map R _ r := show to_finsupp (C (algebra_map _ _ r)) = _, by { rw to_finsupp_C, refl } lemma of_finsupp_algebra_map (r : R) : (⟨algebra_map R _ r⟩ : A[X]) = algebra_map R (polynomial A) r := to_finsupp_injective (to_finsupp_algebra_map _).symm /-- When we have `[comm_semiring R]`, the function `C` is the same as `algebra_map R R[X]`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebra_map` is not available.) -/ lemma C_eq_algebra_map (r : R) : C r = algebra_map R R[X] r := rfl variables {R} /-- Extensionality lemma for algebra maps out of `polynomial A'` over a smaller base ring than `A'` -/ @[ext] lemma alg_hom_ext' [algebra R A'] [algebra R B'] {f g : A'[X] →ₐ[R] B'} (h₁ : f.comp (is_scalar_tower.to_alg_hom R A' (polynomial A')) = g.comp (is_scalar_tower.to_alg_hom R A' (polynomial A'))) (hβ‚‚ : f X = g X) : f = g := alg_hom.coe_ring_hom_injective (polynomial.ring_hom_ext' (congr_arg alg_hom.to_ring_hom h₁) hβ‚‚) variable (R) /-- Algebra isomorphism between `polynomial R` and `add_monoid_algebra R β„•`. This is just an implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/ @[simps] def to_finsupp_iso_alg : R[X] ≃ₐ[R] add_monoid_algebra R β„• := { commutes' := Ξ» r, begin dsimp, exact to_finsupp_algebra_map _, end, ..to_finsupp_iso R } variable {R} instance [nontrivial A] : nontrivial (subalgebra R (polynomial A)) := ⟨⟨βŠ₯, ⊀, begin rw [ne.def, set_like.ext_iff, not_forall], refine ⟨X, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top, algebra_map_apply, not_forall], intro x, rw [ext_iff, not_forall], refine ⟨1, _⟩, simp [coeff_C], end⟩⟩ @[simp] lemma alg_hom_evalβ‚‚_algebra_map {R A B : Type*} [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] (p : R[X]) (f : A →ₐ[R] B) (a : A) : f (evalβ‚‚ (algebra_map R A) a p) = evalβ‚‚ (algebra_map R B) (f a) p := begin dsimp [evalβ‚‚, sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast, alg_hom.commutes], end @[simp] lemma evalβ‚‚_algebra_map_X {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] (p : R[X]) (f : R[X] →ₐ[R] A) : evalβ‚‚ (algebra_map R A) (f X) p = f p := begin conv_rhs { rw [←polynomial.sum_C_mul_X_eq p], }, dsimp [evalβ‚‚, sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast], simp [polynomial.C_eq_algebra_map], end -- these used to be about `algebra_map β„€ R`, but now the simp-normal form is `int.cast_ring_hom R`. @[simp] lemma ring_hom_evalβ‚‚_cast_int_ring_hom {R S : Type*} [ring R] [ring S] (p : β„€[X]) (f : R β†’+* S) (r : R) : f (evalβ‚‚ (int.cast_ring_hom R) r p) = evalβ‚‚ (int.cast_ring_hom S) (f r) p := alg_hom_evalβ‚‚_algebra_map p f.to_int_alg_hom r @[simp] lemma evalβ‚‚_int_cast_ring_hom_X {R : Type*} [ring R] (p : β„€[X]) (f : β„€[X] β†’+* R) : evalβ‚‚ (int.cast_ring_hom R) (f X) p = f p := evalβ‚‚_algebra_map_X p f.to_int_alg_hom end comm_semiring section aeval variables [comm_semiring R] {p q : R[X]} variables [semiring A] [algebra R A] variables {B : Type*} [semiring B] [algebra R B] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. This is a stronger variant of the linear map `polynomial.leval`. -/ def aeval : R[X] →ₐ[R] A := { commutes' := Ξ» r, evalβ‚‚_C _ _, ..evalβ‚‚_ring_hom' (algebra_map R A) x (Ξ» a, algebra.commutes _ _) } variables {R A} @[simp] lemma adjoin_X : algebra.adjoin R ({X} : set R[X]) = ⊀ := begin refine top_unique (Ξ» p hp, _), set S := algebra.adjoin R ({X} : set R[X]), rw [← sum_monomial_eq p], simp only [monomial_eq_smul_X, sum], exact S.sum_mem (Ξ» n hn, S.smul_mem (S.pow_mem (algebra.subset_adjoin rfl) _) _) end @[ext] lemma alg_hom_ext {f g : R[X] →ₐ[R] A} (h : f X = g X) : f = g := alg_hom.ext_of_adjoin_eq_top adjoin_X $ Ξ» p hp, (set.mem_singleton_iff.1 hp).symm β–Έ h theorem aeval_def (p : R[X]) : aeval x p = evalβ‚‚ (algebra_map R A) x p := rfl @[simp] lemma aeval_zero : aeval x (0 : R[X]) = 0 := alg_hom.map_zero (aeval x) @[simp] lemma aeval_X : aeval x (X : R[X]) = x := evalβ‚‚_X _ x @[simp] lemma aeval_C (r : R) : aeval x (C r) = algebra_map R A r := evalβ‚‚_C _ x @[simp] lemma aeval_monomial {n : β„•} {r : R} : aeval x (monomial n r) = (algebra_map _ _ r) * x^n := evalβ‚‚_monomial _ _ @[simp] lemma aeval_X_pow {n : β„•} : aeval x ((X : R[X])^n) = x^n := evalβ‚‚_X_pow _ _ @[simp] lemma aeval_add : aeval x (p + q) = aeval x p + aeval x q := alg_hom.map_add _ _ _ @[simp] lemma aeval_one : aeval x (1 : R[X]) = 1 := alg_hom.map_one _ @[simp] lemma aeval_bit0 : aeval x (bit0 p) = bit0 (aeval x p) := alg_hom.map_bit0 _ _ @[simp] lemma aeval_bit1 : aeval x (bit1 p) = bit1 (aeval x p) := alg_hom.map_bit1 _ _ @[simp] lemma aeval_nat_cast (n : β„•) : aeval x (n : R[X]) = n := map_nat_cast _ _ lemma aeval_mul : aeval x (p * q) = aeval x p * aeval x q := alg_hom.map_mul _ _ _ lemma aeval_comp {A : Type*} [comm_semiring A] [algebra R A] (x : A) : aeval x (p.comp q) = (aeval (aeval x q) p) := evalβ‚‚_comp (algebra_map R A) @[simp] lemma aeval_map {A : Type*} [comm_semiring A] [algebra R A] [algebra A B] [is_scalar_tower R A B] (b : B) (p : R[X]) : aeval b (p.map (algebra_map R A)) = aeval b p := by rw [aeval_def, evalβ‚‚_map, ←is_scalar_tower.algebra_map_eq, ←aeval_def] theorem aeval_alg_hom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) := alg_hom_ext $ by simp only [aeval_X, alg_hom.comp_apply] @[simp] theorem aeval_X_left : aeval (X : R[X]) = alg_hom.id R R[X] := alg_hom_ext $ aeval_X X theorem eval_unique (Ο† : R[X] →ₐ[R] A) (p) : Ο† p = evalβ‚‚ (algebra_map R A) (Ο† X) p := by rw [← aeval_def, aeval_alg_hom, aeval_X_left, alg_hom.comp_id] theorem aeval_alg_hom_apply (f : A →ₐ[R] B) (x : A) (p : R[X]) : aeval (f x) p = f (aeval x p) := alg_hom.ext_iff.1 (aeval_alg_hom f x) p theorem aeval_alg_equiv (f : A ≃ₐ[R] B) (x : A) : aeval (f x) = (f : A →ₐ[R] B).comp (aeval x) := aeval_alg_hom (f : A →ₐ[R] B) x theorem aeval_alg_equiv_apply (f : A ≃ₐ[R] B) (x : A) (p : R[X]) : aeval (f x) p = f (aeval x p) := aeval_alg_hom_apply (f : A →ₐ[R] B) x p lemma aeval_algebra_map_apply (x : R) (p : R[X]) : aeval (algebra_map R A x) p = algebra_map R A (p.eval x) := aeval_alg_hom_apply (algebra.of_id R A) x p @[simp] lemma coe_aeval_eq_eval (r : R) : (aeval r : R[X] β†’ R) = eval r := rfl @[simp] lemma aeval_fn_apply {X : Type*} (g : R[X]) (f : X β†’ R) (x : X) : ((aeval f) g) x = aeval (f x) g := (aeval_alg_hom_apply (pi.eval_alg_hom _ _ x) f g).symm @[norm_cast] lemma aeval_subalgebra_coe (g : R[X]) {A : Type*} [semiring A] [algebra R A] (s : subalgebra R A) (f : s) : (aeval f g : A) = aeval (f : A) g := (aeval_alg_hom_apply s.val f g).symm lemma coeff_zero_eq_aeval_zero (p : R[X]) : p.coeff 0 = aeval 0 p := by simp [coeff_zero_eq_eval_zero] lemma coeff_zero_eq_aeval_zero' (p : R[X]) : algebra_map R A (p.coeff 0) = aeval (0 : A) p := by simp [aeval_def] variable (R) theorem _root_.algebra.adjoin_singleton_eq_range_aeval (x : A) : algebra.adjoin R {x} = (polynomial.aeval x).range := by rw [← algebra.map_top, ← adjoin_X, alg_hom.map_adjoin, set.image_singleton, aeval_X] variable {R} section comm_semiring variables [comm_semiring S] {f : R β†’+* S} lemma aeval_eq_sum_range [algebra R S] {p : R[X]} (x : S) : aeval x p = βˆ‘ i in finset.range (p.nat_degree + 1), p.coeff i β€’ x ^ i := by { simp_rw algebra.smul_def, exact evalβ‚‚_eq_sum_range (algebra_map R S) x } lemma aeval_eq_sum_range' [algebra R S] {p : R[X]} {n : β„•} (hn : p.nat_degree < n) (x : S) : aeval x p = βˆ‘ i in finset.range n, p.coeff i β€’ x ^ i := by { simp_rw algebra.smul_def, exact evalβ‚‚_eq_sum_range' (algebra_map R S) hn x } lemma is_root_of_evalβ‚‚_map_eq_zero (hf : function.injective f) {r : R} : evalβ‚‚ f (f r) p = 0 β†’ p.is_root r := begin intro h, apply hf, rw [←evalβ‚‚_hom, h, f.map_zero], end lemma is_root_of_aeval_algebra_map_eq_zero [algebra R S] {p : R[X]} (inj : function.injective (algebra_map R S)) {r : R} (hr : aeval (algebra_map R S r) p = 0) : p.is_root r := is_root_of_evalβ‚‚_map_eq_zero inj hr section aeval_tower variables [algebra S R] [algebra S A'] [algebra S B'] /-- Version of `aeval` for defining algebra homs out of `polynomial R` over a smaller base ring than `R`. -/ def aeval_tower (f : R →ₐ[S] A') (x : A') : R[X] →ₐ[S] A' := { commutes' := Ξ» r, by simp [algebra_map_apply], ..evalβ‚‚_ring_hom ↑f x } variables (g : R →ₐ[S] A') (y : A') @[simp] lemma aeval_tower_X : aeval_tower g y X = y := evalβ‚‚_X _ _ @[simp] lemma aeval_tower_C (x : R) : aeval_tower g y (C x) = g x := evalβ‚‚_C _ _ @[simp] lemma aeval_tower_comp_C : ((aeval_tower g y : R[X] β†’+* A').comp C) = g := ring_hom.ext $ aeval_tower_C _ _ @[simp] lemma aeval_tower_algebra_map (x : R) : aeval_tower g y (algebra_map R R[X] x) = g x := evalβ‚‚_C _ _ @[simp] lemma aeval_tower_comp_algebra_map : (aeval_tower g y : R[X] β†’+* A').comp (algebra_map R R[X]) = g := aeval_tower_comp_C _ _ lemma aeval_tower_to_alg_hom (x : R) : aeval_tower g y (is_scalar_tower.to_alg_hom S R R[X] x) = g x := aeval_tower_algebra_map _ _ _ @[simp] lemma aeval_tower_comp_to_alg_hom : (aeval_tower g y).comp (is_scalar_tower.to_alg_hom S R R[X]) = g := alg_hom.coe_ring_hom_injective $ aeval_tower_comp_algebra_map _ _ @[simp] lemma aeval_tower_id : aeval_tower (alg_hom.id S S) = aeval := by { ext, simp only [eval_X, aeval_tower_X, coe_aeval_eq_eval], } @[simp] lemma aeval_tower_of_id : aeval_tower (algebra.of_id S A') = aeval := by { ext, simp only [aeval_X, aeval_tower_X], } end aeval_tower end comm_semiring section comm_ring variables [comm_ring S] {f : R β†’+* S} lemma dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : S[X]} (i : β„•) (dvd_eval : p ∣ f.eval z) (dvd_terms : βˆ€ (j β‰  i), p ∣ f.coeff j * z ^ j) : p ∣ f.coeff i * z ^ i := begin by_cases hi : i ∈ f.support, { rw [eval, evalβ‚‚, sum] at dvd_eval, rw [←finset.insert_erase hi, finset.sum_insert (finset.not_mem_erase _ _)] at dvd_eval, refine (dvd_add_left _).mp dvd_eval, apply finset.dvd_sum, intros j hj, exact dvd_terms j (finset.ne_of_mem_erase hj) }, { convert dvd_zero p, rw not_mem_support_iff at hi, simp [hi] } end lemma dvd_term_of_is_root_of_dvd_terms {r p : S} {f : S[X]} (i : β„•) (hr : f.is_root r) (h : βˆ€ (j β‰  i), p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (eq.symm hr β–Έ dvd_zero p) h end comm_ring end aeval section ring variables [ring R] /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ lemma eval_mul_X_sub_C {p : R[X]} (r : R) : (p * (X - C r)).eval r = 0 := begin simp only [eval, evalβ‚‚, ring_hom.id_apply], have bound := calc (p * (X - C r)).nat_degree ≀ p.nat_degree + (X - C r).nat_degree : nat_degree_mul_le ... ≀ p.nat_degree + 1 : add_le_add_left nat_degree_X_sub_C_le _ ... < p.nat_degree + 2 : lt_add_one _, rw sum_over_range' _ _ (p.nat_degree + 2) bound, swap, { simp, }, rw sum_range_succ', conv_lhs { congr, apply_congr, skip, rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ], }, simp [sum_range_sub', coeff_monomial], end theorem not_is_unit_X_sub_C [nontrivial R] (r : R) : Β¬ is_unit (X - C r) := Ξ» ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by erw [← eval_mul_X_sub_C, hgf, eval_one] end ring lemma aeval_endomorphism {M : Type*} [comm_ring R] [add_comm_group M] [module R M] (f : M β†’β‚—[R] M) (v : M) (p : R[X]) : aeval f p v = p.sum (Ξ» n b, b β€’ (f ^ n) v) := begin rw [aeval_def, evalβ‚‚], exact (linear_map.applyβ‚— v).map_sum, end end polynomial
a992fb386c73e565d2a1b0f3d231ea6b5e6fd7c2
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/pnat.lean
e04b522cb61b13e005f7c2918d14cd028923e352
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
2,424
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import tactic.basic /-- `β„•+` is the type of positive natural numbers. It is defined as a subtype, and the VM representation of `β„•+` is the same as `β„•` because the proof is not stored. -/ def pnat := {n : β„• // n > 0} notation `β„•+` := pnat instance coe_pnat_nat : has_coe β„•+ β„• := ⟨subtype.val⟩ namespace nat /-- Convert a natural number to a positive natural number. The positivity assumption is inferred by `dec_trivial`. -/ def to_pnat (n : β„•) (h : n > 0 . tactic.exact_dec_trivial) : β„•+ := ⟨n, h⟩ /-- Write a successor as an element of `β„•+`. -/ def succ_pnat (n : β„•) : β„•+ := ⟨succ n, succ_pos n⟩ @[simp] theorem succ_pnat_coe (n : β„•) : (succ_pnat n : β„•) = succ n := rfl /-- Convert a natural number to a pnat. `n+1` is mapped to itself, and `0` becomes `1`. -/ def to_pnat' (n : β„•) : β„•+ := succ_pnat (pred n) end nat namespace pnat open nat @[simp] theorem pos (n : β„•+) : (n : β„•) > 0 := n.2 theorem eq {m n : β„•+} : (m : β„•) = n β†’ m = n := subtype.eq @[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : β„•+) : β„•) = n := rfl instance : has_add β„•+ := ⟨λ m n, ⟨m + n, add_pos m.2 n.2⟩⟩ @[simp] theorem add_coe (m n : β„•+) : ((m + n : β„•+) : β„•) = m + n := rfl @[simp] theorem ne_zero (n : β„•+) : (n : β„•) β‰  0 := ne_of_gt n.2 @[simp] theorem to_pnat'_coe {n : β„•} : n > 0 β†’ (n.to_pnat' : β„•) = n := succ_pred_eq_of_pos @[simp] theorem coe_to_pnat' (n : β„•+) : (n : β„•).to_pnat' = n := eq (to_pnat'_coe n.pos) instance : comm_monoid β„•+ := { mul := Ξ» m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩, mul_assoc := Ξ» a b c, subtype.eq (mul_assoc _ _ _), one := succ_pnat 0, one_mul := Ξ» a, subtype.eq (one_mul _), mul_one := Ξ» a, subtype.eq (mul_one _), mul_comm := Ξ» a b, subtype.eq (mul_comm _ _) } @[simp] theorem one_coe : ((1 : β„•+) : β„•) = 1 := rfl @[simp] theorem mul_coe (m n : β„•+) : ((m * n : β„•+) : β„•) = m * n := rfl /-- The power of a pnat and a nat is a pnat. -/ def pow (m : β„•+) (n : β„•) : β„•+ := ⟨m ^ n, nat.pos_pow_of_pos _ m.pos⟩ instance : has_pow β„•+ β„• := ⟨pow⟩ @[simp] theorem pow_coe (m : β„•+) (n : β„•) : (↑(m ^ n) : β„•) = m ^ n := rfl instance : has_repr β„•+ := ⟨λ n, repr n.1⟩ end pnat
b352787f96494602a0c4e8a3ffa5f0991c3612bd
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/algebra/continued_fractions/computation/approximations.lean
9bb14add6fb38dca5b3ac34df2f7dae255da61a7
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,862
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.computation.correctness_terminating import data.nat.fib import tactic.solve_by_elim /-! # Approximations for Continued Fraction Computations (`generalized_continued_fraction.of`) ## Summary This file contains useful approximations for the values involved in the continued fractions computation `generalized_continued_fraction.of`. In particular, we derive the so-called *determinant formula* for `generalized_continued_fraction.of`: `Aβ‚™ * Bβ‚™β‚Šβ‚ - Bβ‚™ * Aβ‚™β‚Šβ‚ = (-1)^(n + 1)`. Moreover, we derive some upper bounds for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (generalized_continued_fraction.of v).convergents n|`. The derived bounds will show us that the error term indeed gets smaller. As a corollary, we will be able to show that `(generalized_continued_fraction.of v).convergents` converges to `v` in `algebra.continued_fractions.computation.approximation_corollaries`. ## Main Theorems - `generalized_continued_fraction.of_part_num_eq_one`: shows that all partial numerators `aα΅’` are equal to one. - `generalized_continued_fraction.exists_int_eq_of_part_denom`: shows that all partial denominators `bα΅’` correspond to an integer. - `generalized_continued_fraction.one_le_of_nth_part_denom`: shows that `1 ≀ bα΅’`. - `generalized_continued_fraction.succ_nth_fib_le_of_nth_denom`: shows that the `n`th denominator `Bβ‚™` is greater than or equal to the `n + 1`th fibonacci number `nat.fib (n + 1)`. - `generalized_continued_fraction.le_of_succ_nth_denom`: shows that `bβ‚™ * Bβ‚™ ≀ Bβ‚™β‚Šβ‚`, where `bβ‚™` is the `n`th partial denominator of the continued fraction. - `generalized_continued_fraction.abs_sub_convergents_le`: shows that `|v - Aβ‚™ / Bβ‚™| ≀ 1 / (Bβ‚™ * Bβ‚™β‚Šβ‚)`, where `Aβ‚™` is the nth partial numerator. ## References - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] - https://en.wikipedia.org/wiki/Generalized_continued_fraction#The_determinant_formula -/ namespace generalized_continued_fraction open generalized_continued_fraction as gcf variables {K : Type*} {v : K} {n : β„•} [linear_ordered_field K] [floor_ring K] namespace int_fract_pair /-! We begin with some lemmas about the stream of `int_fract_pair`s, which presumably are not of great interest for the end user. -/ /-- Shows that the fractional parts of the stream are in `[0,1)`. -/ lemma nth_stream_fr_nonneg_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≀ ifp_n.fr ∧ ifp_n.fr < 1 := begin cases n, case nat.zero { have : int_fract_pair.of v = ifp_n, by injection nth_stream_eq, simp [fract_lt_one, fract_nonneg, int_fract_pair.of, this.symm] }, case nat.succ { rcases (succ_nth_stream_eq_some_iff.elim_left nth_stream_eq) with ⟨_, _, _, ifp_of_eq_ifp_n⟩, simp [fract_lt_one, fract_nonneg, int_fract_pair.of, ifp_of_eq_ifp_n.symm] } end /-- Shows that the fractional parts of the stream are nonnegative. -/ lemma nth_stream_fr_nonneg {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : 0 ≀ ifp_n.fr := (nth_stream_fr_nonneg_lt_one nth_stream_eq).left /-- Shows that the fractional parts of the stream are smaller than one. -/ lemma nth_stream_fr_lt_one {ifp_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) : ifp_n.fr < 1 := (nth_stream_fr_nonneg_lt_one nth_stream_eq).right /-- Shows that the integer parts of the stream are at least one. -/ lemma one_le_succ_nth_stream_b {ifp_succ_n : int_fract_pair K} (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : 1 ≀ ifp_succ_n.b := begin obtain ⟨ifp_n, nth_stream_eq, stream_nth_fr_ne_zero, ⟨-⟩⟩ : βˆƒ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr β‰  0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq, suffices : 1 ≀ ifp_n.fr⁻¹, { rw_mod_cast [le_floor], assumption }, suffices : ifp_n.fr ≀ 1, { have h : 0 < ifp_n.fr, from lt_of_le_of_ne (nth_stream_fr_nonneg nth_stream_eq) stream_nth_fr_ne_zero.symm, apply one_le_inv h this }, simp only [le_of_lt (nth_stream_fr_lt_one nth_stream_eq)] end /-- Shows that the `n + 1`th integer part `bβ‚™β‚Šβ‚` of the stream is smaller or equal than the inverse of the `n`th fractional part `frβ‚™` of the stream. This result is straight-forward as `bβ‚™β‚Šβ‚` is defined as the floor of `1 / frβ‚™` -/ lemma succ_nth_stream_b_le_nth_stream_fr_inv {ifp_n ifp_succ_n : int_fract_pair K} (nth_stream_eq : int_fract_pair.stream v n = some ifp_n) (succ_nth_stream_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : (ifp_succ_n.b : K) ≀ ifp_n.fr⁻¹ := begin suffices : (⌊ifp_n.frβ»ΒΉβŒ‹ : K) ≀ ifp_n.fr⁻¹, { cases ifp_n with _ ifp_n_fr, have : ifp_n_fr β‰  0, { intro h, simpa [h, int_fract_pair.stream, nth_stream_eq] using succ_nth_stream_eq }, have : int_fract_pair.of ifp_n_fr⁻¹ = ifp_succ_n, { simpa [this, int_fract_pair.stream, nth_stream_eq, option.coe_def] using succ_nth_stream_eq }, rwa ←this }, exact (floor_le ifp_n.fr⁻¹) end end int_fract_pair /-! Next we translate above results about the stream of `int_fract_pair`s to the computed continued fraction `generalized_continued_fraction.of`. -/ /-- Shows that the integer parts of the continued fraction are at least one. -/ lemma of_one_le_nth_part_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : 1 ≀ b := begin obtain ⟨gp_n, nth_s_eq, ⟨-⟩⟩ : βˆƒ gp_n, (gcf.of v).s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, obtain ⟨ifp_n, succ_nth_stream_eq, ifp_n_b_eq_gp_n_b⟩ : βˆƒ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, rw [←ifp_n_b_eq_gp_n_b], exact_mod_cast (int_fract_pair.one_le_succ_nth_stream_b succ_nth_stream_eq) end /-- Shows that the partial numerators `aα΅’` of the continued fraction are equal to one and the partial denominators `bα΅’` correspond to integers. -/ lemma of_part_num_eq_one_and_exists_int_part_denom_eq {gp : gcf.pair K} (nth_s_eq : (gcf.of v).s.nth n = some gp) : gp.a = 1 ∧ βˆƒ (z : β„€), gp.b = (z : K) := begin obtain ⟨ifp, stream_succ_nth_eq, -⟩ : βˆƒ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ _, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some nth_s_eq, have : gp = ⟨1, ifp.b⟩, by { have : (gcf.of v).s.nth n = some ⟨1, ifp.b⟩, from nth_of_eq_some_of_succ_nth_int_fract_pair_stream stream_succ_nth_eq, have : some gp = some ⟨1, ifp.b⟩, by rwa nth_s_eq at this, injection this }, finish end /-- Shows that the partial numerators `aα΅’` are equal to one. -/ lemma of_part_num_eq_one {a : K} (nth_part_num_eq : (gcf.of v).partial_numerators.nth n = some a) : a = 1 := begin obtain ⟨gp, nth_s_eq, gp_a_eq_a_n⟩ : βˆƒ gp, (gcf.of v).s.nth n = some gp ∧ gp.a = a, from exists_s_a_of_part_num nth_part_num_eq, have : gp.a = 1, from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).left, rwa gp_a_eq_a_n at this end /-- Shows that the partial denominators `bα΅’` correspond to an integer. -/ lemma exists_int_eq_of_part_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : βˆƒ (z : β„€), b = (z : K) := begin obtain ⟨gp, nth_s_eq, gp_b_eq_b_n⟩ : βˆƒ gp, (gcf.of v).s.nth n = some gp ∧ gp.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, have : βˆƒ (z : β„€), gp.b = (z : K), from (of_part_num_eq_one_and_exists_int_part_denom_eq nth_s_eq).right, rwa gp_b_eq_b_n at this end /-! One of our next goals is to show that `bβ‚™ * Bβ‚™ ≀ Bβ‚™β‚Šβ‚`. For this, we first show that the partial denominators `Bβ‚™` are bounded from below by the fibonacci sequence `nat.fib`. This then implies that `0 ≀ Bβ‚™` and hence `Bβ‚™β‚Šβ‚‚ = bβ‚™β‚Šβ‚ * Bβ‚™β‚Šβ‚ + Bβ‚™ β‰₯ bβ‚™β‚Šβ‚ * Bβ‚™β‚Šβ‚ + 0 = bβ‚™β‚Šβ‚ * Bβ‚™β‚Šβ‚`. -/ -- open `nat` as we will make use of fibonacci numbers. open nat lemma fib_le_of_continuants_aux_b : (n ≀ 1 ∨ Β¬(gcf.of v).terminated_at (n - 2)) β†’ (fib n : K) ≀ ((gcf.of v).continuants_aux n).b := nat.strong_induction_on n begin clear n, assume n IH hyp, rcases n with _|_|n, { simp [fib_succ_succ, continuants_aux] }, -- case n = 0 { simp [fib_succ_succ, continuants_aux] }, -- case n = 1 { let g := gcf.of v, -- case 2 ≀ n have : Β¬(n + 2 ≀ 1), by linarith, have not_terminated_at_n : Β¬g.terminated_at n, from or.resolve_left hyp this, obtain ⟨gp, s_ppred_nth_eq⟩ : βˆƒ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.mp not_terminated_at_n, set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, -- use the recurrence of continuants_aux suffices : (fib n : K) + fib (n + 1) ≀ gp.a * ppconts.b + gp.b * pconts.b, by simpa [fib_succ_succ, add_comm, (continuants_aux_recurrence s_ppred_nth_eq ppconts_eq pconts_eq)], -- make use of the fact that gp.a = 1 suffices : (fib n : K) + fib (n + 1) ≀ ppconts.b + gp.b * pconts.b, by simpa [(of_part_num_eq_one $ part_num_eq_s_a s_ppred_nth_eq)], have not_terminated_at_pred_n : Β¬g.terminated_at (n - 1), from mt (terminated_stable $ nat.sub_le n 1) not_terminated_at_n, have not_terminated_at_ppred_n : Β¬terminated_at g (n - 2), from mt (terminated_stable (n - 1).pred_le) not_terminated_at_pred_n, -- use the IH to get the inequalities for `pconts` and `ppconts` have : (fib (n + 1) : K) ≀ pconts.b, from IH _ (nat.lt.base $ n + 1) (or.inr not_terminated_at_pred_n), have ppred_nth_fib_le_ppconts_B : (fib n : K) ≀ ppconts.b, from IH n (lt_trans (nat.lt.base n) $ nat.lt.base $ n + 1) (or.inr not_terminated_at_ppred_n), suffices : (fib (n + 1) : K) ≀ gp.b * pconts.b, solve_by_elim [add_le_add ppred_nth_fib_le_ppconts_B], -- finally use the fact that 1 ≀ gp.b to solve the goal suffices : 1 * (fib (n + 1) : K) ≀ gp.b * pconts.b, by rwa [one_mul] at this, have one_le_gp_b : (1 : K) ≀ gp.b, from of_one_le_nth_part_denom (part_denom_eq_s_b s_ppred_nth_eq), have : (0 : K) ≀ fib (n + 1), by exact_mod_cast (fib (n + 1)).zero_le, have : (0 : K) ≀ gp.b, from le_trans zero_le_one one_le_gp_b, mono } end /-- Shows that the `n`th denominator is greater than or equal to the `n + 1`th fibonacci number, that is `nat.fib (n + 1) ≀ Bβ‚™`. -/ lemma succ_nth_fib_le_of_nth_denom (hyp: n = 0 ∨ Β¬(gcf.of v).terminated_at (n - 1)) : (fib (n + 1) : K) ≀ (gcf.of v).denominators n := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], have : (n + 1) ≀ 1 ∨ Β¬(gcf.of v).terminated_at (n - 1), by { cases n, case nat.zero : { exact (or.inl $ le_refl 1) }, case nat.succ : { exact or.inr (or.resolve_left hyp n.succ_ne_zero) } }, exact (fib_le_of_continuants_aux_b this) end /-! As a simple consequence, we can now derive that all denominators are nonnegative. -/ lemma zero_le_of_continuants_aux_b : 0 ≀ ((gcf.of v).continuants_aux n).b := begin let g := gcf.of v, induction n with n IH, case nat.zero: { refl }, case nat.succ: { cases (decidable.em $ g.terminated_at (n - 1)) with terminated not_terminated, { cases n, -- terminating case { simp [zero_le_one] }, { have : g.continuants_aux (n + 2) = g.continuants_aux (n + 1), from continuants_aux_stable_step_of_terminated terminated, simp only [this, IH] } }, { calc -- non-terminating case (0 : K) ≀ fib (n + 1) : by exact_mod_cast (n + 1).fib.zero_le ... ≀ ((gcf.of v).continuants_aux (n + 1)).b : fib_le_of_continuants_aux_b (or.inr not_terminated) } } end /-- Shows that all denominators are nonnegative. -/ lemma zero_le_of_denom : 0 ≀ (gcf.of v).denominators n := by { rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact zero_le_of_continuants_aux_b } lemma le_of_succ_succ_nth_continuants_aux_b {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : b * ((gcf.of v).continuants_aux $ n + 1).b ≀ ((gcf.of v).continuants_aux $ n + 2).b := begin set g := gcf.of v with g_eq, obtain ⟨gp_n, nth_s_eq, gpnb_eq_b⟩ : βˆƒ gp_n, g.s.nth n = some gp_n ∧ gp_n.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, let conts := g.continuants_aux (n + 2), set pconts := g.continuants_aux (n + 1) with pconts_eq, set ppconts := g.continuants_aux n with ppconts_eq, -- use the recurrence of continuants_aux and the fact that gp_n.a = 1 suffices : gp_n.b * pconts.b ≀ ppconts.b + gp_n.b * pconts.b, by { have : gp_n.a = 1, from of_part_num_eq_one (part_num_eq_s_a nth_s_eq), finish [gcf.continuants_aux_recurrence nth_s_eq ppconts_eq pconts_eq] }, have : 0 ≀ ppconts.b, from zero_le_of_continuants_aux_b, solve_by_elim [le_add_of_nonneg_of_le, le_refl] end /-- Shows that `bβ‚™ * Bβ‚™ ≀ Bβ‚™β‚Šβ‚`, where `bβ‚™` is the `n`th partial denominator and `Bβ‚™β‚Šβ‚` and `Bβ‚™` are the `n + 1`th and `n`th denominator of the continued fraction. -/ theorem le_of_succ_nth_denom {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : b * (gcf.of v).denominators n ≀ (gcf.of v).denominators (n + 1) := begin rw [denom_eq_conts_b, nth_cont_eq_succ_nth_cont_aux], exact (le_of_succ_succ_nth_continuants_aux_b nth_part_denom_eq) end /-- Shows that the sequence of denominators is monotonically increasing, that is `Bβ‚™ ≀ Bβ‚™β‚Šβ‚`. -/ theorem of_denom_mono : (gcf.of v).denominators n ≀ (gcf.of v).denominators (n + 1) := begin let g := gcf.of v, cases (decidable.em $ g.partial_denominators.terminated_at n) with terminated not_terminated, { have : g.partial_denominators.nth n = none, by rwa seq.terminated_at at terminated, have : g.terminated_at n, from terminated_at_iff_part_denom_none.elim_right (by rwa seq.terminated_at at terminated), have : g.denominators (n + 1) = g.denominators n, from denominators_stable_of_terminated n.le_succ this, rw this }, { obtain ⟨b, nth_part_denom_eq⟩ : βˆƒ b, g.partial_denominators.nth n = some b, from option.ne_none_iff_exists'.mp not_terminated, have : 1 ≀ b, from of_one_le_nth_part_denom nth_part_denom_eq, calc g.denominators n ≀ b * g.denominators n : by simpa using (mul_le_mul_of_nonneg_right this zero_le_of_denom) ... ≀ g.denominators (n + 1) : le_of_succ_nth_denom nth_part_denom_eq } end section determinant /-! ### Determinant Formula Next we prove the so-called *determinant formula* for `generalized_continued_fraction.of`: `Aβ‚™ * Bβ‚™β‚Šβ‚ - Bβ‚™ * Aβ‚™β‚Šβ‚ = (-1)^(n + 1)`. -/ lemma determinant_aux (hyp: n = 0 ∨ Β¬(gcf.of v).terminated_at (n - 1)) : ((gcf.of v).continuants_aux n).a * ((gcf.of v).continuants_aux (n + 1)).b - ((gcf.of v).continuants_aux n).b * ((gcf.of v).continuants_aux (n + 1)).a = (-1)^n := begin induction n with n IH, case nat.zero { simp [continuants_aux] }, case nat.succ { -- set up some shorthand notation let g := gcf.of v, let conts := continuants_aux g (n + 2), set pred_conts := continuants_aux g (n + 1) with pred_conts_eq, set ppred_conts := continuants_aux g n with ppred_conts_eq, let pA := pred_conts.a, let pB := pred_conts.b, let ppA := ppred_conts.a, let ppB := ppred_conts.b, -- let's change the goal to something more readable change pA * conts.b - pB * conts.a = (-1)^(n + 1), have not_terminated_at_n : Β¬terminated_at g n, from or.resolve_left hyp n.succ_ne_zero, obtain ⟨gp, s_nth_eq⟩ : βˆƒ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.elim_left not_terminated_at_n, -- unfold the recurrence relation for `conts` once and simplify to derive the following suffices : pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = (-1)^(n + 1), by { simp only [conts, (continuants_aux_recurrence s_nth_eq ppred_conts_eq pred_conts_eq)], have gp_a_eq_one : gp.a = 1, from of_part_num_eq_one (part_num_eq_s_a s_nth_eq), rw [gp_a_eq_one, this.symm], ring }, suffices : pA * ppB - pB * ppA = (-1)^(n + 1), calc pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = pA * ppB + pA * gp.b * pB - pB * ppA - pB * gp.b * pA : by ring ... = pA * ppB - pB * ppA : by ring ... = (-1)^(n + 1) : by assumption, suffices : ppA * pB - ppB * pA = (-1)^n, by { have pow_succ_n : (-1 : K)^(n + 1) = (-1) * (-1)^n, from pow_succ (-1) n, rw [pow_succ_n, ←this], ring }, exact (IH $ or.inr $ mt (terminated_stable $ n.sub_le 1) not_terminated_at_n) } end /-- The determinant formula `Aβ‚™ * Bβ‚™β‚Šβ‚ - Bβ‚™ * Aβ‚™β‚Šβ‚ = (-1)^(n + 1)` -/ lemma determinant (not_terminated_at_n : Β¬(gcf.of v).terminated_at n) : (gcf.of v).numerators n * (gcf.of v).denominators (n + 1) - (gcf.of v).denominators n * (gcf.of v).numerators (n + 1) = (-1)^(n + 1) := (determinant_aux $ or.inr $ not_terminated_at_n) end determinant section error_term /-! ### Approximation of Error Term Next we derive some approximations for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (generalized_continued_fraction.of v).convergents n|`. -/ /-- This lemma follows from the finite correctness proof, the determinant equality, and by simplifying the difference. -/ lemma sub_convergents_eq {ifp : int_fract_pair K} (stream_nth_eq : int_fract_pair.stream v n = some ifp) : let g := gcf.of v in let B := (g.continuants_aux (n + 1)).b in let pB := (g.continuants_aux n).b in v - g.convergents n = if ifp.fr = 0 then 0 else (-1)^n / (B * (ifp.fr⁻¹ * B + pB)) := begin -- set up some shorthand notation let g := gcf.of v, let conts := g.continuants_aux (n + 1), let pred_conts := g.continuants_aux n, have g_finite_correctness : v = gcf.comp_exact_value pred_conts conts ifp.fr, from comp_exact_value_correctness_of_stream_eq_some stream_nth_eq, cases decidable.em (ifp.fr = 0) with ifp_fr_eq_zero ifp_fr_ne_zero, { suffices : v - g.convergents n = 0, by simpa [ifp_fr_eq_zero], replace g_finite_correctness : v = g.convergents n, by simpa [gcf.comp_exact_value, ifp_fr_eq_zero] using g_finite_correctness, exact (sub_eq_zero.elim_right g_finite_correctness) }, { -- more shorthand notation let A := conts.a, let B := conts.b, let pA := pred_conts.a, let pB := pred_conts.b, -- first, let's simplify the goal as `ifp.fr β‰  0` suffices : v - A / B = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)), by simpa [ifp_fr_ne_zero], -- now we can unfold `g.comp_exact_value` to derive the following equality for `v` replace g_finite_correctness : v = (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B), by simpa [gcf.comp_exact_value, ifp_fr_ne_zero, next_continuants, next_numerator, next_denominator, add_comm] using g_finite_correctness, -- let's rewrite this equality for `v` in our goal suffices : (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) - A / B = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)), by rwa g_finite_correctness, -- To continue, we need use the determinant equality. So let's derive the needed hypothesis. have n_eq_zero_or_not_terminated_at_pred_n : n = 0 ∨ Β¬g.terminated_at (n - 1), by { cases n with n', { simp }, { have : int_fract_pair.stream v (n' + 1) β‰  none, by simp [stream_nth_eq], have : Β¬g.terminated_at n', from (not_iff_not_of_iff of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none) .elim_right this, exact (or.inr this) } }, have determinant_eq : pA * B - pB * A = (-1)^n, from determinant_aux n_eq_zero_or_not_terminated_at_pred_n, -- now all we got to do is to rewrite this equality in our goal and re-arrange terms; -- however, for this, we first have to derive quite a few tedious inequalities. have pB_ineq : (fib n : K) ≀ pB, by { have : n ≀ 1 ∨ Β¬g.terminated_at (n - 2), by { cases n_eq_zero_or_not_terminated_at_pred_n with n_eq_zero not_terminated_at_pred_n, { simp [n_eq_zero] }, { exact (or.inr $ mt (terminated_stable (n - 1).pred_le) not_terminated_at_pred_n) } }, exact (fib_le_of_continuants_aux_b this) }, have B_ineq : (fib (n + 1) : K) ≀ B, by { have : n + 1 ≀ 1 ∨ Β¬g.terminated_at (n + 1 - 2), by { cases n_eq_zero_or_not_terminated_at_pred_n with n_eq_zero not_terminated_at_pred_n, { simp [n_eq_zero, le_refl] }, { exact (or.inr not_terminated_at_pred_n) } }, exact (fib_le_of_continuants_aux_b this) }, have zero_lt_B : 0 < B, { have : 1 ≀ B, from le_trans (by exact_mod_cast fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm)) B_ineq, exact (lt_of_lt_of_le zero_lt_one this) }, have zero_ne_B : 0 β‰  B, from ne_of_lt zero_lt_B, have : 0 β‰  pB + ifp.fr⁻¹ * B, by { have : (0 : K) ≀ fib n, by exact_mod_cast (fib n).zero_le, -- 0 ≀ fib n ≀ pB have zero_le_pB : 0 ≀ pB, from le_trans this pB_ineq, have : 0 < ifp.fr⁻¹, by { suffices : 0 < ifp.fr, by rwa inv_pos, have : 0 ≀ ifp.fr, from int_fract_pair.nth_stream_fr_nonneg stream_nth_eq, change ifp.fr β‰  0 at ifp_fr_ne_zero, exact lt_of_le_of_ne this ifp_fr_ne_zero.symm }, have : 0 < ifp.fr⁻¹ * B, from mul_pos this zero_lt_B, have : 0 < pB + ifp.fr⁻¹ * B, from add_pos_of_nonneg_of_pos zero_le_pB this, exact (ne_of_lt this) }, -- finally, let's do the rewriting calc (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) - A / B = ((pA + ifp.fr⁻¹ * A) * B - (pB + ifp.fr⁻¹ * B) * A) / ((pB + ifp.fr⁻¹ * B) * B) : by rw (div_sub_div _ _ this.symm zero_ne_B.symm) ... = (pA * B + ifp.fr⁻¹ * A * B - (pB * A + ifp.fr⁻¹ * B * A)) / _ : by repeat { rw [add_mul] } ... = (pA * B - pB * A) / ((pB + ifp.fr⁻¹ * B) * B) : by ring ... = (-1)^n / ((pB + ifp.fr⁻¹ * B) * B) : by rw determinant_eq ... = (-1)^n / (B * (ifp.fr⁻¹ * B + pB)) : by ac_refl } end local notation `|` x `|` := abs x /-- Shows that `|v - Aβ‚™ / Bβ‚™| ≀ 1 / (Bβ‚™ * Bβ‚™β‚Šβ‚)` -/ theorem abs_sub_convergents_le (not_terminated_at_n : Β¬(gcf.of v).terminated_at n) : |v - (gcf.of v).convergents n| ≀ 1 / (((gcf.of v).denominators n) * ((gcf.of v).denominators $ n + 1)) := begin -- shorthand notation let g := gcf.of v, let nextConts := g.continuants_aux (n + 2), set conts := continuants_aux g (n + 1) with conts_eq, set pred_conts := continuants_aux g n with pred_conts_eq, -- change the goal to something more readable change |v - convergents g n| ≀ 1 / (conts.b * nextConts.b), obtain ⟨gp, s_nth_eq⟩ : βˆƒ gp, g.s.nth n = some gp, from option.ne_none_iff_exists'.elim_left not_terminated_at_n, have gp_a_eq_one : gp.a = 1, from of_part_num_eq_one (part_num_eq_s_a s_nth_eq), -- unfold the recurrence relation for `nextConts.b` have nextConts_b_eq : nextConts.b = pred_conts.b + gp.b * conts.b, by simp [nextConts, (continuants_aux_recurrence s_nth_eq pred_conts_eq conts_eq), gp_a_eq_one, pred_conts_eq.symm, conts_eq.symm, add_comm], let denom := conts.b * (pred_conts.b + gp.b * conts.b), suffices : |v - g.convergents n| ≀ 1 / denom, by { rw [nextConts_b_eq], congr' 1 }, obtain ⟨ifp_succ_n, succ_nth_stream_eq, ifp_succ_n_b_eq_gp_b⟩ : βˆƒ ifp_succ_n, int_fract_pair.stream v (n + 1) = some ifp_succ_n ∧ (ifp_succ_n.b : K) = gp.b, from int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some s_nth_eq, obtain ⟨ifp_n, stream_nth_eq, stream_nth_fr_ne_zero, if_of_eq_ifp_succ_n⟩ : βˆƒ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr β‰  0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from int_fract_pair.succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq, let denom' := conts.b * (pred_conts.b + ifp_n.fr⁻¹ * conts.b), -- now we can use `sub_convergents_eq` to simplify our goal suffices : |(-1)^n / denom'| ≀ 1 / denom, by { have : v - g.convergents n = (-1)^n / denom', by { -- apply `sub_convergens_eq` and simplify the result have tmp, from sub_convergents_eq stream_nth_eq, delta at tmp, simp only [stream_nth_fr_ne_zero, conts_eq.symm, pred_conts_eq.symm] at tmp, rw tmp, simp only [denom'], ring, ac_refl }, rwa this }, -- derive some tedious inequalities that we need to rewrite our goal have nextConts_b_ineq : (fib (n + 2) : K) ≀ (pred_conts.b + gp.b * conts.b), by { have : (fib (n + 2) : K) ≀ nextConts.b, from fib_le_of_continuants_aux_b (or.inr not_terminated_at_n), rwa [nextConts_b_eq] at this }, have conts_b_ineq : (fib (n + 1) : K) ≀ conts.b, by { have : Β¬g.terminated_at (n - 1), from mt (terminated_stable n.pred_le) not_terminated_at_n, exact (fib_le_of_continuants_aux_b $ or.inr this) }, have zero_lt_conts_b : 0 < conts.b, by { have : (0 : K) < fib (n + 1), by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm)), exact (lt_of_lt_of_le this conts_b_ineq) }, -- `denom'` is positive, so we can remove `|⬝|` from our goal suffices : 1 / denom' ≀ 1 / denom, by { have : |(-1)^n / denom'| = 1 / denom', by { suffices : 1 / |denom'| = 1 / denom', by rwa [abs_div, (abs_neg_one_pow n)], have : 0 < denom', by { have : 0 ≀ pred_conts.b, by { have : (fib n : K) ≀ pred_conts.b, by { have : Β¬g.terminated_at (n - 2), from mt (terminated_stable (n.sub_le 2)) not_terminated_at_n, exact (fib_le_of_continuants_aux_b $ or.inr this) }, exact le_trans (by exact_mod_cast (fib n).zero_le) this }, have : 0 < ifp_n.fr⁻¹, by { have zero_le_ifp_n_fract : 0 ≀ ifp_n.fr, from int_fract_pair.nth_stream_fr_nonneg stream_nth_eq, exact inv_pos.elim_right (lt_of_le_of_ne zero_le_ifp_n_fract stream_nth_fr_ne_zero.symm) }, any_goals { repeat { apply mul_pos <|> apply add_pos_of_nonneg_of_pos } }; assumption }, rwa (abs_of_pos this) }, rwa this }, suffices : 0 < denom ∧ denom ≀ denom', from div_le_div_of_le_left zero_le_one this.left this.right, split, { have : 0 < pred_conts.b + gp.b * conts.b, from lt_of_lt_of_le (by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.succ.zero_le n.succ.succ_ne_zero.symm))) nextConts_b_ineq, solve_by_elim [mul_pos] }, { -- we can cancel multiplication by `conts.b` and addition with `pred_conts.b` suffices : gp.b * conts.b ≀ ifp_n.fr⁻¹ * conts.b, from ((mul_le_mul_left zero_lt_conts_b).elim_right $ (add_le_add_iff_left pred_conts.b).elim_right this), suffices : (ifp_succ_n.b : K) * conts.b ≀ ifp_n.fr⁻¹ * conts.b, by rwa [←ifp_succ_n_b_eq_gp_b], have : (ifp_succ_n.b : K) ≀ ifp_n.fr⁻¹, from int_fract_pair.succ_nth_stream_b_le_nth_stream_fr_inv stream_nth_eq succ_nth_stream_eq, have : 0 ≀ conts.b, from le_of_lt zero_lt_conts_b, mono } end /-- Shows that `|v - Aβ‚™ / Bβ‚™| ≀ 1 / (bβ‚™ * Bβ‚™ * Bβ‚™)`. This bound is worse than the one shown in `gcf.abs_sub_convergents_le`, but sometimes it is easier to apply and sufficient for one's use case. -/ lemma abs_sub_convergents_le' {b : K} (nth_part_denom_eq : (gcf.of v).partial_denominators.nth n = some b) : |v - (gcf.of v).convergents n| ≀ 1 / (b * ((gcf.of v).denominators n) * ((gcf.of v).denominators n)) := begin let g := gcf.of v, let B := g.denominators n, let nB := g.denominators (n + 1), have not_terminated_at_n : Β¬g.terminated_at n, by { have : g.partial_denominators.nth n β‰  none, by simp [nth_part_denom_eq], exact (not_iff_not_of_iff terminated_at_iff_part_denom_none).elim_right this }, suffices : 1 / (B * nB) ≀ (1 : K) / (b * B * B), by { have : |v - g.convergents n| ≀ 1 / (B * nB), from abs_sub_convergents_le not_terminated_at_n, transitivity; assumption }, -- derive some inequalities needed to show the claim have zero_lt_B : 0 < B, by { have : (fib (n + 1) : K) ≀ B, from succ_nth_fib_le_of_nth_denom (or.inr $ mt (terminated_stable n.pred_le) not_terminated_at_n), exact (lt_of_lt_of_le (by exact_mod_cast (fib_pos (lt_of_le_of_ne n.succ.zero_le n.succ_ne_zero.symm))) this) }, have denoms_ineq : b * B * B ≀ B * nB, by { have : b * B ≀ nB, from le_of_succ_nth_denom nth_part_denom_eq, rwa [(mul_comm B nB), (mul_le_mul_right zero_lt_B)] }, have : (0 : K) < b * B * B, by { have : 0 < b, from lt_of_lt_of_le zero_lt_one (of_one_le_nth_part_denom nth_part_denom_eq), any_goals { repeat { apply mul_pos } }; assumption }, exact (div_le_div_of_le_left zero_le_one this denoms_ineq) end end error_term end generalized_continued_fraction
524d15bc5610df413b8fffccf2ceb5361b78c198
bb31430994044506fa42fd667e2d556327e18dfe
/src/combinatorics/simple_graph/subgraph.lean
e1a67790ddd9b413fea1e3709beec8e58685ad1b
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
34,531
lean
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller, Alena Gusakov -/ import combinatorics.simple_graph.basic /-! # Subgraphs of a simple graph A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the endpoints of each edge are present in the vertex subset. The edge subset is formalized as a sub-relation of the adjacency relation of the simple graph. ## Main definitions * `subgraph G` is the type of subgraphs of a `G : simple_graph` * `subgraph.neighbor_set`, `subgraph.incidence_set`, and `subgraph.degree` are like their `simple_graph` counterparts, but they refer to vertices from `G` to avoid subtype coercions. * `subgraph.coe` is the coercion from a `G' : subgraph G` to a `simple_graph G'.verts`. (This cannot be a `has_coe` instance since the destination type depends on `G'`.) * `subgraph.is_spanning` for whether a subgraph is a spanning subgraph and `subgraph.is_induced` for whether a subgraph is an induced subgraph. * Instances for `lattice (subgraph G)` and `bounded_order (subgraph G)`. * `simple_graph.to_subgraph`: If a `simple_graph` is a subgraph of another, then you can turn it into a member of the larger graph's `simple_graph.subgraph` type. * Graph homomorphisms from a subgraph to a graph (`subgraph.map_top`) and between subgraphs (`subgraph.map`). ## Implementation notes * Recall that subgraphs are not determined by their vertex sets, so `set_like` does not apply to this kind of subobject. ## Todo * Images of graph homomorphisms as subgraphs. -/ universes u v namespace simple_graph /-- A subgraph of a `simple_graph` is a subset of vertices along with a restriction of the adjacency relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice. Thinking of `V β†’ V β†’ Prop` as `set (V Γ— V)`, a set of darts (i.e., half-edges), then `subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/ @[ext] structure subgraph {V : Type u} (G : simple_graph V) := (verts : set V) (adj : V β†’ V β†’ Prop) (adj_sub : βˆ€ {v w : V}, adj v w β†’ G.adj v w) (edge_vert : βˆ€ {v w : V}, adj v w β†’ v ∈ verts) (symm : symmetric adj . obviously) variables {V : Type u} {W : Type v} /-- The one-vertex subgraph. -/ @[simps] protected def singleton_subgraph (G : simple_graph V) (v : V) : G.subgraph := { verts := {v}, adj := βŠ₯, adj_sub := by simp [-set.bot_eq_empty], edge_vert := by simp [-set.bot_eq_empty] } /-- The one-edge subgraph. -/ @[simps] def subgraph_of_adj (G : simple_graph V) {v w : V} (hvw : G.adj v w) : G.subgraph := { verts := {v, w}, adj := Ξ» a b, ⟦(v, w)⟧ = ⟦(a, b)⟧, adj_sub := Ξ» a b h, by { rw [← G.mem_edge_set, ← h], exact hvw }, edge_vert := Ξ» a b h, by { apply_fun (Ξ» e, a ∈ e) at h, simpa using h } } namespace subgraph variables {G : simple_graph V} protected lemma loopless (G' : subgraph G) : irreflexive G'.adj := Ξ» v h, G.loopless v (G'.adj_sub h) lemma adj_comm (G' : subgraph G) (v w : V) : G'.adj v w ↔ G'.adj w v := ⟨λ x, G'.symm x, Ξ» x, G'.symm x⟩ @[symm] lemma adj_symm (G' : subgraph G) {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h protected lemma adj.symm {G' : subgraph G} {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h protected lemma adj.adj_sub {H : G.subgraph} {u v : V} (h : H.adj u v) : G.adj u v := H.adj_sub h protected lemma adj.fst_mem {H : G.subgraph} {u v : V} (h : H.adj u v) : u ∈ H.verts := H.edge_vert h protected lemma adj.snd_mem {H : G.subgraph} {u v : V} (h : H.adj u v) : v ∈ H.verts := h.symm.fst_mem protected lemma adj.ne {H : G.subgraph} {u v : V} (h : H.adj u v) : u β‰  v := h.adj_sub.ne /-- Coercion from `G' : subgraph G` to a `simple_graph β†₯G'.verts`. -/ @[simps] protected def coe (G' : subgraph G) : simple_graph G'.verts := { adj := Ξ» v w, G'.adj v w, symm := Ξ» v w h, G'.symm h, loopless := Ξ» v h, loopless G v (G'.adj_sub h) } @[simp] lemma coe_adj_sub (G' : subgraph G) (u v : G'.verts) (h : G'.coe.adj u v) : G.adj u v := G'.adj_sub h /- Given `h : H.adj u v`, then `h.coe : H.coe.adj ⟨u, _⟩ ⟨v, _⟩`. -/ protected lemma adj.coe {H : G.subgraph} {u v : V} (h : H.adj u v) : H.coe.adj ⟨u, H.edge_vert h⟩ ⟨v, H.edge_vert h.symm⟩ := h /-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. -/ def is_spanning (G' : subgraph G) : Prop := βˆ€ (v : V), v ∈ G'.verts lemma is_spanning_iff {G' : subgraph G} : G'.is_spanning ↔ G'.verts = set.univ := set.eq_univ_iff_forall.symm /-- Coercion from `subgraph G` to `simple_graph V`. If `G'` is a spanning subgraph, then `G'.spanning_coe` yields an isomorphic graph. In general, this adds in all vertices from `V` as isolated vertices. -/ @[simps] protected def spanning_coe (G' : subgraph G) : simple_graph V := { adj := G'.adj, symm := G'.symm, loopless := Ξ» v hv, G.loopless v (G'.adj_sub hv) } @[simp] lemma adj.of_spanning_coe {G' : subgraph G} {u v : G'.verts} (h : G'.spanning_coe.adj u v) : G.adj u v := G'.adj_sub h /-- `spanning_coe` is equivalent to `coe` for a subgraph that `is_spanning`. -/ @[simps] def spanning_coe_equiv_coe_of_spanning (G' : subgraph G) (h : G'.is_spanning) : G'.spanning_coe ≃g G'.coe := { to_fun := Ξ» v, ⟨v, h v⟩, inv_fun := Ξ» v, v, left_inv := Ξ» v, rfl, right_inv := Ξ» ⟨v, hv⟩, rfl, map_rel_iff' := Ξ» v w, iff.rfl } /-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if they are adjacent in `G`. -/ def is_induced (G' : subgraph G) : Prop := βˆ€ {v w : V}, v ∈ G'.verts β†’ w ∈ G'.verts β†’ G.adj v w β†’ G'.adj v w /-- `H.support` is the set of vertices that form edges in the subgraph `H`. -/ def support (H : subgraph G) : set V := rel.dom H.adj lemma mem_support (H : subgraph G) {v : V} : v ∈ H.support ↔ βˆƒ w, H.adj v w := iff.rfl lemma support_subset_verts (H : subgraph G) : H.support βŠ† H.verts := Ξ» v ⟨w, h⟩, H.edge_vert h /-- `G'.neighbor_set v` is the set of vertices adjacent to `v` in `G'`. -/ def neighbor_set (G' : subgraph G) (v : V) : set V := set_of (G'.adj v) lemma neighbor_set_subset (G' : subgraph G) (v : V) : G'.neighbor_set v βŠ† G.neighbor_set v := Ξ» w h, G'.adj_sub h lemma neighbor_set_subset_verts (G' : subgraph G) (v : V) : G'.neighbor_set v βŠ† G'.verts := Ξ» _ h, G'.edge_vert (adj_symm G' h) @[simp] lemma mem_neighbor_set (G' : subgraph G) (v w : V) : w ∈ G'.neighbor_set v ↔ G'.adj v w := iff.rfl /-- A subgraph as a graph has equivalent neighbor sets. -/ def coe_neighbor_set_equiv {G' : subgraph G} (v : G'.verts) : G'.coe.neighbor_set v ≃ G'.neighbor_set v := { to_fun := Ξ» w, ⟨w, by { obtain ⟨w', hw'⟩ := w, simpa using hw' }⟩, inv_fun := Ξ» w, ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, by simpa using w.2⟩, left_inv := Ξ» w, by simp, right_inv := Ξ» w, by simp } /-- The edge set of `G'` consists of a subset of edges of `G`. -/ def edge_set (G' : subgraph G) : set (sym2 V) := sym2.from_rel G'.symm lemma edge_set_subset (G' : subgraph G) : G'.edge_set βŠ† G.edge_set := Ξ» e, quotient.ind (Ξ» e h, G'.adj_sub h) e @[simp] lemma mem_edge_set {G' : subgraph G} {v w : V} : ⟦(v, w)⟧ ∈ G'.edge_set ↔ G'.adj v w := iff.rfl lemma mem_verts_if_mem_edge {G' : subgraph G} {e : sym2 V} {v : V} (he : e ∈ G'.edge_set) (hv : v ∈ e) : v ∈ G'.verts := begin refine quotient.ind (Ξ» e he hv, _) e he hv, cases e with v w, simp only [mem_edge_set] at he, cases sym2.mem_iff.mp hv with h h; subst h, { exact G'.edge_vert he, }, { exact G'.edge_vert (G'.symm he), }, end /-- The `incidence_set` is the set of edges incident to a given vertex. -/ def incidence_set (G' : subgraph G) (v : V) : set (sym2 V) := {e ∈ G'.edge_set | v ∈ e} lemma incidence_set_subset_incidence_set (G' : subgraph G) (v : V) : G'.incidence_set v βŠ† G.incidence_set v := Ξ» e h, ⟨G'.edge_set_subset h.1, h.2⟩ lemma incidence_set_subset (G' : subgraph G) (v : V) : G'.incidence_set v βŠ† G'.edge_set := Ξ» _ h, h.1 /-- Give a vertex as an element of the subgraph's vertex type. -/ @[reducible] def vert (G' : subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩ /-- Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities. See Note [range copy pattern]. -/ def copy (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V β†’ V β†’ Prop) (hadj : adj' = G'.adj) : subgraph G := { verts := V'', adj := adj', adj_sub := Ξ» _ _, hadj.symm β–Έ G'.adj_sub, edge_vert := Ξ» _ _, hV.symm β–Έ hadj.symm β–Έ G'.edge_vert, symm := hadj.symm β–Έ G'.symm } lemma copy_eq (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V β†’ V β†’ Prop) (hadj : adj' = G'.adj) : G'.copy V'' hV adj' hadj = G' := subgraph.ext _ _ hV hadj /-- The union of two subgraphs. -/ def union (x y : subgraph G) : subgraph G := { verts := x.verts βˆͺ y.verts, adj := x.adj βŠ” y.adj, adj_sub := Ξ» v w h, or.cases_on h (Ξ» h, x.adj_sub h) (Ξ» h, y.adj_sub h), edge_vert := Ξ» v w h, or.cases_on h (Ξ» h, or.inl (x.edge_vert h)) (Ξ» h, or.inr (y.edge_vert h)), symm := Ξ» v w h, by rwa [pi.sup_apply, pi.sup_apply, x.adj_comm, y.adj_comm] } /-- The intersection of two subgraphs. -/ def inter (x y : subgraph G) : subgraph G := { verts := x.verts ∩ y.verts, adj := x.adj βŠ“ y.adj, adj_sub := Ξ» v w h, x.adj_sub h.1, edge_vert := Ξ» v w h, ⟨x.edge_vert h.1, y.edge_vert h.2⟩, symm := Ξ» v w h, by rwa [pi.inf_apply, pi.inf_apply, x.adj_comm, y.adj_comm] } /-- The `top` subgraph is `G` as a subgraph of itself. -/ def top : subgraph G := { verts := set.univ, adj := G.adj, adj_sub := Ξ» v w h, h, edge_vert := Ξ» v w h, set.mem_univ v, symm := G.symm } /-- The `bot` subgraph is the subgraph with no vertices or edges. -/ def bot : subgraph G := { verts := βˆ…, adj := βŠ₯, adj_sub := Ξ» v w h, false.rec _ h, edge_vert := Ξ» v w h, false.rec _ h, symm := Ξ» u v h, h } /-- The relation that one subgraph is a subgraph of another. -/ def is_subgraph (x y : subgraph G) : Prop := x.verts βŠ† y.verts ∧ βˆ€ ⦃v w : V⦄, x.adj v w β†’ y.adj v w instance : lattice (subgraph G) := { le := is_subgraph, sup := union, inf := inter, le_refl := Ξ» x, ⟨rfl.subset, Ξ» _ _ h, h⟩, le_trans := Ξ» x y z hxy hyz, ⟨hxy.1.trans hyz.1, Ξ» _ _ h, hyz.2 (hxy.2 h)⟩, le_antisymm := begin intros x y hxy hyx, ext1 v, exact set.subset.antisymm hxy.1 hyx.1, ext v w, exact iff.intro (Ξ» h, hxy.2 h) (Ξ» h, hyx.2 h), end, sup_le := Ξ» x y z hxy hyz, ⟨set.union_subset hxy.1 hyz.1, (Ξ» v w h, h.cases_on (Ξ» h, hxy.2 h) (Ξ» h, hyz.2 h))⟩, le_sup_left := Ξ» x y, ⟨set.subset_union_left x.verts y.verts, (Ξ» v w h, or.inl h)⟩, le_sup_right := Ξ» x y, ⟨set.subset_union_right x.verts y.verts, (Ξ» v w h, or.inr h)⟩, le_inf := Ξ» x y z hxy hyz, ⟨set.subset_inter hxy.1 hyz.1, (Ξ» v w h, ⟨hxy.2 h, hyz.2 h⟩)⟩, inf_le_left := Ξ» x y, ⟨set.inter_subset_left x.verts y.verts, (Ξ» v w h, h.1)⟩, inf_le_right := Ξ» x y, ⟨set.inter_subset_right x.verts y.verts, (Ξ» v w h, h.2)⟩ } instance : bounded_order (subgraph G) := { top := top, bot := bot, le_top := Ξ» x, ⟨set.subset_univ _, (Ξ» v w h, x.adj_sub h)⟩, bot_le := Ξ» x, ⟨set.empty_subset _, (Ξ» v w h, false.rec _ h)⟩ } @[simps] instance subgraph_inhabited : inhabited (subgraph G) := ⟨βŠ₯⟩ -- TODO simp lemmas for the other lattice operations on subgraphs @[simp] lemma top_verts : (⊀ : subgraph G).verts = set.univ := rfl @[simp] lemma top_adj_iff {v w : V} : (⊀ : subgraph G).adj v w ↔ G.adj v w := iff.rfl @[simp] lemma bot_verts : (βŠ₯ : subgraph G).verts = βˆ… := rfl @[simp] lemma not_bot_adj {v w : V} : Β¬(βŠ₯ : subgraph G).adj v w := not_false @[simp] lemma inf_adj {H₁ Hβ‚‚ : subgraph G} {v w : V} : (H₁ βŠ“ Hβ‚‚).adj v w ↔ H₁.adj v w ∧ Hβ‚‚.adj v w := iff.rfl @[simp] lemma sup_adj {H₁ Hβ‚‚ : subgraph G} {v w : V} : (H₁ βŠ” Hβ‚‚).adj v w ↔ H₁.adj v w ∨ Hβ‚‚.adj v w := iff.rfl @[simp] lemma verts_sup {H H' : G.subgraph} : (H βŠ” H').verts = H.verts βˆͺ H'.verts := rfl @[simp] lemma verts_inf {H H' : G.subgraph} : (H βŠ“ H').verts = H.verts ∩ H'.verts := rfl lemma neighbor_set_sup {H H' : G.subgraph} (v : V) : (H βŠ” H').neighbor_set v = H.neighbor_set v βˆͺ H'.neighbor_set v := by { ext w, simp } lemma neighbor_set_inf {H H' : G.subgraph} (v : V) : (H βŠ“ H').neighbor_set v = H.neighbor_set v ∩ H'.neighbor_set v := by { ext w, simp } @[simp] lemma edge_set_top : (⊀ : subgraph G).edge_set = G.edge_set := rfl @[simp] lemma edge_set_bot : (βŠ₯ : subgraph G).edge_set = βˆ… := set.ext $ sym2.ind (by simp) @[simp] lemma edge_set_inf {H₁ Hβ‚‚ : subgraph G} : (H₁ βŠ“ Hβ‚‚).edge_set = H₁.edge_set ∩ Hβ‚‚.edge_set := set.ext $ sym2.ind (by simp) @[simp] lemma edge_set_sup {H₁ Hβ‚‚ : subgraph G} : (H₁ βŠ” Hβ‚‚).edge_set = H₁.edge_set βˆͺ Hβ‚‚.edge_set := set.ext $ sym2.ind (by simp) @[simp] lemma spanning_coe_top : (⊀ : subgraph G).spanning_coe = G := by { ext, refl } @[simp] lemma spanning_coe_bot : (βŠ₯ : subgraph G).spanning_coe = βŠ₯ := rfl /-- Turn a subgraph of a `simple_graph` into a member of its subgraph type. -/ @[simps] def _root_.simple_graph.to_subgraph (H : simple_graph V) (h : H ≀ G) : G.subgraph := { verts := set.univ, adj := H.adj, adj_sub := h, edge_vert := Ξ» v w h, set.mem_univ v, symm := H.symm } lemma support_mono {H H' : subgraph G} (h : H ≀ H') : H.support βŠ† H'.support := rel.dom_mono h.2 lemma _root_.simple_graph.to_subgraph.is_spanning (H : simple_graph V) (h : H ≀ G) : (H.to_subgraph h).is_spanning := set.mem_univ lemma spanning_coe_le_of_le {H H' : subgraph G} (h : H ≀ H') : H.spanning_coe ≀ H'.spanning_coe := h.2 /-- The top of the `subgraph G` lattice is equivalent to the graph itself. -/ def top_equiv : (⊀ : subgraph G).coe ≃g G := { to_fun := Ξ» v, ↑v, inv_fun := Ξ» v, ⟨v, trivial⟩, left_inv := Ξ» ⟨v, _⟩, rfl, right_inv := Ξ» v, rfl, map_rel_iff' := Ξ» a b, iff.rfl } /-- The bottom of the `subgraph G` lattice is equivalent to the empty graph on the empty vertex type. -/ def bot_equiv : (βŠ₯ : subgraph G).coe ≃g (βŠ₯ : simple_graph empty) := { to_fun := Ξ» v, v.property.elim, inv_fun := Ξ» v, v.elim, left_inv := Ξ» ⟨_, h⟩, h.elim, right_inv := Ξ» v, v.elim, map_rel_iff' := Ξ» a b, iff.rfl } lemma edge_set_mono {H₁ Hβ‚‚ : subgraph G} (h : H₁ ≀ Hβ‚‚) : H₁.edge_set ≀ Hβ‚‚.edge_set := Ξ» e, sym2.ind h.2 e lemma _root_.disjoint.edge_set {H₁ Hβ‚‚ : subgraph G} (h : disjoint H₁ Hβ‚‚) : disjoint H₁.edge_set Hβ‚‚.edge_set := disjoint_iff_inf_le.mpr $ by simpa using edge_set_mono h.le_bot /-- Graph homomorphisms induce a covariant function on subgraphs. -/ @[simps] protected def map {G' : simple_graph W} (f : G β†’g G') (H : G.subgraph) : G'.subgraph := { verts := f '' H.verts, adj := relation.map H.adj f f, adj_sub := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact f.map_rel (H.adj_sub h) }, edge_vert := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact set.mem_image_of_mem _ (H.edge_vert h) }, symm := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact ⟨v, u, H.symm h, rfl, rfl⟩ } } lemma map_monotone {G' : simple_graph W} (f : G β†’g G') : monotone (subgraph.map f) := begin intros H H' h, split, { intro, simp only [map_verts, set.mem_image, forall_exists_index, and_imp], rintro v hv rfl, exact ⟨_, h.1 hv, rfl⟩ }, { rintros _ _ ⟨u, v, ha, rfl, rfl⟩, exact ⟨_, _, h.2 ha, rfl, rfl⟩ } end lemma map_sup {G : simple_graph V} {G' : simple_graph W} (f : G β†’g G') {H H' : G.subgraph} : (H βŠ” H').map f = H.map f βŠ” H'.map f := begin ext1, { simp only [set.image_union, map_verts, verts_sup]}, { ext, simp only [relation.map, map_adj, sup_adj], split, { rintro ⟨a, b, h|h, rfl, rfl⟩, { exact or.inl ⟨_, _, h, rfl, rfl⟩ }, { exact or.inr ⟨_, _, h, rfl, rfl⟩ } }, { rintro (⟨a, b, h, rfl, rfl⟩|⟨a, b, h, rfl, rfl⟩), { exact ⟨_, _, or.inl h, rfl, rfl⟩ }, { exact ⟨_, _, or.inr h, rfl, rfl⟩ } } }, end /-- Graph homomorphisms induce a contravariant function on subgraphs. -/ @[simps] protected def comap {G' : simple_graph W} (f : G β†’g G') (H : G'.subgraph) : G.subgraph := { verts := f ⁻¹' H.verts, adj := Ξ» u v, G.adj u v ∧ H.adj (f u) (f v), adj_sub := by { rintros v w ⟨ga, ha⟩, exact ga }, edge_vert := by { rintros v w ⟨ga, ha⟩, simp [H.edge_vert ha] } } lemma comap_monotone {G' : simple_graph W} (f : G β†’g G') : monotone (subgraph.comap f) := begin intros H H' h, split, { intro, simp only [comap_verts, set.mem_preimage], apply h.1, }, { intros v w, simp only [comap_adj, and_imp, true_and] { contextual := tt }, intro, apply h.2, } end lemma map_le_iff_le_comap {G' : simple_graph W} (f : G β†’g G') (H : G.subgraph) (H' : G'.subgraph) : H.map f ≀ H' ↔ H ≀ H'.comap f := begin refine ⟨λ h, ⟨λ v hv, _, Ξ» v w hvw, _⟩, Ξ» h, ⟨λ v, _, Ξ» v w, _⟩⟩, { simp only [comap_verts, set.mem_preimage], exact h.1 ⟨v, hv, rfl⟩, }, { simp only [H.adj_sub hvw, comap_adj, true_and], exact h.2 ⟨v, w, hvw, rfl, rfl⟩, }, { simp only [map_verts, set.mem_image, forall_exists_index, and_imp], rintro w hw rfl, exact h.1 hw, }, { simp only [relation.map, map_adj, forall_exists_index, and_imp], rintros u u' hu rfl rfl, have := h.2 hu, simp only [comap_adj] at this, exact this.2, } end /-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of the subgraphs as graphs. -/ @[simps] def inclusion {x y : subgraph G} (h : x ≀ y) : x.coe β†’g y.coe := { to_fun := Ξ» v, βŸ¨β†‘v, and.left h v.property⟩, map_rel' := Ξ» v w hvw, h.2 hvw } lemma inclusion.injective {x y : subgraph G} (h : x ≀ y) : function.injective (inclusion h) := Ξ» v w h, by { simp only [inclusion, rel_hom.coe_fn_mk, subtype.mk_eq_mk] at h, exact subtype.ext h } /-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/ @[simps] protected def hom (x : subgraph G) : x.coe β†’g G := { to_fun := Ξ» v, v, map_rel' := Ξ» v w hvw, x.adj_sub hvw } lemma hom.injective {x : subgraph G} : function.injective x.hom := Ξ» v w h, subtype.ext h /-- There is an induced injective homomorphism of a subgraph of `G` as a spanning subgraph into `G`. -/ @[simps] def spanning_hom (x : subgraph G) : x.spanning_coe β†’g G := { to_fun := id, map_rel' := Ξ» v w hvw, x.adj_sub hvw } lemma spanning_hom.injective {x : subgraph G} : function.injective x.spanning_hom := Ξ» v w h, h lemma neighbor_set_subset_of_subgraph {x y : subgraph G} (h : x ≀ y) (v : V) : x.neighbor_set v βŠ† y.neighbor_set v := Ξ» w h', h.2 h' instance neighbor_set.decidable_pred (G' : subgraph G) [h : decidable_rel G'.adj] (v : V) : decidable_pred (∈ G'.neighbor_set v) := h v /-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/ instance finite_at {G' : subgraph G} (v : G'.verts) [decidable_rel G'.adj] [fintype (G.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G.neighbor_set v) (G'.neighbor_set_subset v) /-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph. This is not an instance because `G''` cannot be inferred. -/ def finite_at_of_subgraph {G' G'' : subgraph G} [decidable_rel G'.adj] (h : G' ≀ G'') (v : G'.verts) [hf : fintype (G''.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G''.neighbor_set v) (neighbor_set_subset_of_subgraph h v) instance (G' : subgraph G) [fintype G'.verts] (v : V) [decidable_pred (∈ G'.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset G'.verts (neighbor_set_subset_verts G' v) instance coe_finite_at {G' : subgraph G} (v : G'.verts) [fintype (G'.neighbor_set v)] : fintype (G'.coe.neighbor_set v) := fintype.of_equiv _ (coe_neighbor_set_equiv v).symm lemma is_spanning.card_verts [fintype V] {G' : subgraph G} [fintype G'.verts] (h : G'.is_spanning) : G'.verts.to_finset.card = fintype.card V := by { rw is_spanning_iff at h, simpa [h] } /-- The degree of a vertex in a subgraph. It's zero for vertices outside the subgraph. -/ def degree (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] : β„• := fintype.card (G'.neighbor_set v) lemma finset_card_neighbor_set_eq_degree {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] : (G'.neighbor_set v).to_finset.card = G'.degree v := by rw [degree, set.to_finset_card] lemma degree_le (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] [fintype (G.neighbor_set v)] : G'.degree v ≀ G.degree v := begin rw ←card_neighbor_set_eq_degree, exact set.card_le_of_subset (G'.neighbor_set_subset v), end lemma degree_le' (G' G'' : subgraph G) (h : G' ≀ G'') (v : V) [fintype (G'.neighbor_set v)] [fintype (G''.neighbor_set v)] : G'.degree v ≀ G''.degree v := set.card_le_of_subset (neighbor_set_subset_of_subgraph h v) @[simp] lemma coe_degree (G' : subgraph G) (v : G'.verts) [fintype (G'.coe.neighbor_set v)] [fintype (G'.neighbor_set v)] : G'.coe.degree v = G'.degree v := begin rw ←card_neighbor_set_eq_degree, exact fintype.card_congr (coe_neighbor_set_equiv v), end @[simp] lemma degree_spanning_coe {G' : G.subgraph} (v : V) [fintype (G'.neighbor_set v)] [fintype (G'.spanning_coe.neighbor_set v)] : G'.spanning_coe.degree v = G'.degree v := by { rw [← card_neighbor_set_eq_degree, subgraph.degree], congr } lemma degree_eq_one_iff_unique_adj {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] : G'.degree v = 1 ↔ βˆƒ! (w : V), G'.adj v w := begin rw [← finset_card_neighbor_set_eq_degree, finset.card_eq_one, finset.singleton_iff_unique_mem], simp only [set.mem_to_finset, mem_neighbor_set], end end subgraph section mk_properties /-! ### Properties of `singleton_subgraph` and `subgraph_of_adj` -/ variables {G : simple_graph V} {G' : simple_graph W} instance nonempty_singleton_subgraph_verts (v : V) : nonempty (G.singleton_subgraph v).verts := ⟨⟨v, set.mem_singleton v⟩⟩ @[simp] lemma singleton_subgraph_le_iff (v : V) (H : G.subgraph) : G.singleton_subgraph v ≀ H ↔ v ∈ H.verts := begin refine ⟨λ h, h.1 (set.mem_singleton v), _⟩, intro h, split, { simp [h] }, { simp [-set.bot_eq_empty] } end @[simp] lemma map_singleton_subgraph (f : G β†’g G') {v : V} : subgraph.map f (G.singleton_subgraph v) = G'.singleton_subgraph (f v) := by ext; simp only [relation.map, subgraph.map_adj, singleton_subgraph_adj, pi.bot_apply, exists_and_distrib_left, and_iff_left_iff_imp, is_empty.forall_iff, subgraph.map_verts, singleton_subgraph_verts, set.image_singleton] @[simp] lemma neighbor_set_singleton_subgraph (v w : V) : (G.singleton_subgraph v).neighbor_set w = βˆ… := by { ext u, refl } @[simp] lemma edge_set_singleton_subgraph (v : V) : (G.singleton_subgraph v).edge_set = βˆ… := sym2.from_rel_bot lemma eq_singleton_subgraph_iff_verts_eq (H : G.subgraph) {v : V} : H = G.singleton_subgraph v ↔ H.verts = {v} := begin refine ⟨λ h, by simp [h], Ξ» h, _⟩, ext, { rw [h, singleton_subgraph_verts] }, { simp only [Prop.bot_eq_false, singleton_subgraph_adj, pi.bot_apply, iff_false], intro ha, have ha1 := ha.fst_mem, have ha2 := ha.snd_mem, rw [h, set.mem_singleton_iff] at ha1 ha2, subst_vars, exact ha.ne rfl }, end instance nonempty_subgraph_of_adj_verts {v w : V} (hvw : G.adj v w) : nonempty (G.subgraph_of_adj hvw).verts := ⟨⟨v, by simp⟩⟩ @[simp] lemma edge_set_subgraph_of_adj {v w : V} (hvw : G.adj v w) : (G.subgraph_of_adj hvw).edge_set = {⟦(v, w)⟧} := begin ext e, refine e.ind _, simp only [eq_comm, set.mem_singleton_iff, subgraph.mem_edge_set, subgraph_of_adj_adj, iff_self, forall_2_true_iff], end lemma subgraph_of_adj_symm {v w : V} (hvw : G.adj v w) : G.subgraph_of_adj hvw.symm = G.subgraph_of_adj hvw := by ext; simp [or_comm, and_comm] @[simp] lemma map_subgraph_of_adj (f : G β†’g G') {v w : V} (hvw : G.adj v w) : subgraph.map f (G.subgraph_of_adj hvw) = G'.subgraph_of_adj (f.map_adj hvw) := begin ext, { simp only [subgraph.map_verts, subgraph_of_adj_verts, set.mem_image, set.mem_insert_iff, set.mem_singleton_iff], split, { rintro ⟨u, rfl|rfl, rfl⟩; simp }, { rintro (rfl|rfl), { use v, simp }, { use w, simp } } }, { simp only [relation.map, subgraph.map_adj, subgraph_of_adj_adj, quotient.eq, sym2.rel_iff], split, { rintro ⟨a, b, (⟨rfl,rfl⟩|⟨rfl,rfl⟩), rfl, rfl⟩; simp }, { rintro (⟨rfl,rfl⟩|⟨rfl,rfl⟩), { use [v, w], simp }, { use [w, v], simp } } } end lemma neighbor_set_subgraph_of_adj_subset {u v w : V} (hvw : G.adj v w) : (G.subgraph_of_adj hvw).neighbor_set u βŠ† {v, w} := (G.subgraph_of_adj hvw).neighbor_set_subset_verts _ @[simp] lemma neighbor_set_fst_subgraph_of_adj {v w : V} (hvw : G.adj v w) : (G.subgraph_of_adj hvw).neighbor_set v = {w} := begin ext u, suffices : w = u ↔ u = w, by simpa [hvw.ne.symm] using this, rw eq_comm, end @[simp] lemma neighbor_set_snd_subgraph_of_adj {v w : V} (hvw : G.adj v w) : (G.subgraph_of_adj hvw).neighbor_set w = {v} := begin rw subgraph_of_adj_symm hvw.symm, exact neighbor_set_fst_subgraph_of_adj hvw.symm, end @[simp] lemma neighbor_set_subgraph_of_adj_of_ne_of_ne {u v w : V} (hvw : G.adj v w) (hv : u β‰  v) (hw : u β‰  w) : (G.subgraph_of_adj hvw).neighbor_set u = βˆ… := by { ext, simp [hv.symm, hw.symm] } lemma neighbor_set_subgraph_of_adj [decidable_eq V] {u v w : V} (hvw : G.adj v w) : (G.subgraph_of_adj hvw).neighbor_set u = (if u = v then {w} else βˆ…) βˆͺ (if u = w then {v} else βˆ…) := by split_ifs; subst_vars; simp [*] lemma singleton_subgraph_fst_le_subgraph_of_adj {u v : V} {h : G.adj u v} : G.singleton_subgraph u ≀ G.subgraph_of_adj h := by split; simp [-set.bot_eq_empty] lemma singleton_subgraph_snd_le_subgraph_of_adj {u v : V} {h : G.adj u v} : G.singleton_subgraph v ≀ G.subgraph_of_adj h := by split; simp [-set.bot_eq_empty] end mk_properties namespace subgraph variables {G : simple_graph V} /-! ### Subgraphs of subgraphs -/ /-- Given a subgraph of a subgraph of `G`, construct a subgraph of `G`. -/ @[reducible] protected def coe_subgraph {G' : G.subgraph} : G'.coe.subgraph β†’ G.subgraph := subgraph.map G'.hom /-- Given a subgraph of `G`, restrict it to being a subgraph of another subgraph `G'` by taking the portion of `G` that intersects `G'`. -/ @[reducible] protected def restrict {G' : G.subgraph} : G.subgraph β†’ G'.coe.subgraph := subgraph.comap G'.hom lemma restrict_coe_subgraph {G' : G.subgraph} (G'' : G'.coe.subgraph) : G''.coe_subgraph.restrict = G'' := begin ext, { simp }, { simp only [relation.map, comap_adj, coe_adj, subtype.coe_prop, hom_apply, map_adj, set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right_right, subtype.coe_eta, exists_true_left, exists_eq_right, and_iff_right_iff_imp], apply G''.adj_sub, } end lemma coe_subgraph_injective (G' : G.subgraph) : function.injective (subgraph.coe_subgraph : G'.coe.subgraph β†’ G.subgraph) := function.left_inverse.injective restrict_coe_subgraph /-! ### Edge deletion -/ /-- Given a subgraph `G'` and a set of vertex pairs, remove all of the corresponding edges from its edge set, if present. See also: `simple_graph.delete_edges`. -/ def delete_edges (G' : G.subgraph) (s : set (sym2 V)) : G.subgraph := { verts := G'.verts, adj := G'.adj \ sym2.to_rel s, adj_sub := Ξ» a b h', G'.adj_sub h'.1, edge_vert := Ξ» a b h', G'.edge_vert h'.1, symm := Ξ» a b, by simp [G'.adj_comm, sym2.eq_swap] } section delete_edges variables {G' : G.subgraph} (s : set (sym2 V)) @[simp] lemma delete_edges_verts : (G'.delete_edges s).verts = G'.verts := rfl @[simp] lemma delete_edges_adj (v w : V) : (G'.delete_edges s).adj v w ↔ G'.adj v w ∧ Β¬ ⟦(v, w)⟧ ∈ s := iff.rfl @[simp] lemma delete_edges_delete_edges (s s' : set (sym2 V)) : (G'.delete_edges s).delete_edges s' = G'.delete_edges (s βˆͺ s') := by ext; simp [and_assoc, not_or_distrib] @[simp] lemma delete_edges_empty_eq : G'.delete_edges βˆ… = G' := by ext; simp @[simp] lemma delete_edges_spanning_coe_eq : G'.spanning_coe.delete_edges s = (G'.delete_edges s).spanning_coe := by { ext, simp } lemma delete_edges_coe_eq (s : set (sym2 G'.verts)) : G'.coe.delete_edges s = (G'.delete_edges (sym2.map coe '' s)).coe := begin ext ⟨v, hv⟩ ⟨w, hw⟩, simp only [simple_graph.delete_edges_adj, coe_adj, subtype.coe_mk, delete_edges_adj, set.mem_image, not_exists, not_and, and.congr_right_iff], intro h, split, { intros hs, refine sym2.ind _, rintro ⟨v', hv'⟩ ⟨w', hw'⟩, simp only [sym2.map_pair_eq, subtype.coe_mk, quotient.eq], contrapose!, rintro (_ | _); simpa [sym2.eq_swap], }, { intros h' hs, exact h' _ hs rfl, }, end lemma coe_delete_edges_eq (s : set (sym2 V)) : (G'.delete_edges s).coe = G'.coe.delete_edges (sym2.map coe ⁻¹' s) := by { ext ⟨v, hv⟩ ⟨w, hw⟩, simp } lemma delete_edges_le : G'.delete_edges s ≀ G' := by split; simp { contextual := tt } lemma delete_edges_le_of_le {s s' : set (sym2 V)} (h : s βŠ† s') : G'.delete_edges s' ≀ G'.delete_edges s := begin split; simp only [delete_edges_verts, delete_edges_adj, true_and, and_imp] {contextual := tt}, exact Ξ» v w hvw hs' hs, hs' (h hs), end @[simp] lemma delete_edges_inter_edge_set_left_eq : G'.delete_edges (G'.edge_set ∩ s) = G'.delete_edges s := by ext; simp [imp_false] { contextual := tt } @[simp] lemma delete_edges_inter_edge_set_right_eq : G'.delete_edges (s ∩ G'.edge_set) = G'.delete_edges s := by ext; simp [imp_false] { contextual := tt } lemma coe_delete_edges_le : (G'.delete_edges s).coe ≀ (G'.coe : simple_graph G'.verts) := Ξ» v w, by simp { contextual := tt } lemma spanning_coe_delete_edges_le (G' : G.subgraph) (s : set (sym2 V)) : (G'.delete_edges s).spanning_coe ≀ G'.spanning_coe := spanning_coe_le_of_le (delete_edges_le s) end delete_edges /-! ### Induced subgraphs -/ /- Given a subgraph, we can change its vertex set while removing any invalid edges, which gives induced subgraphs. See also `simple_graph.induce` for the `simple_graph` version, which, unlike for subgraphs, results in a graph with a different vertex type. -/ /-- The induced subgraph of a subgraph. The expectation is that `s βŠ† G'.verts` for the usual notion of an induced subgraph, but, in general, `s` is taken to be the new vertex set and edges are induced from the subgraph `G'`. -/ @[simps] def induce (G' : G.subgraph) (s : set V) : G.subgraph := { verts := s, adj := Ξ» u v, u ∈ s ∧ v ∈ s ∧ G'.adj u v, adj_sub := Ξ» u v, by { rintro ⟨-, -, ha⟩, exact G'.adj_sub ha }, edge_vert := Ξ» u v, by { rintro ⟨h, -, -⟩, exact h } } lemma _root_.simple_graph.induce_eq_coe_induce_top (s : set V) : G.induce s = ((⊀ : G.subgraph).induce s).coe := by { ext v w, simp } section induce variables {G' G'' : G.subgraph} {s s' : set V} lemma induce_mono (hg : G' ≀ G'') (hs : s βŠ† s') : G'.induce s ≀ G''.induce s' := begin split, { simp [hs], }, { simp only [induce_adj, true_and, and_imp] { contextual := tt }, intros v w hv hw ha, exact ⟨hs hv, hs hw, hg.2 ha⟩, }, end @[mono] lemma induce_mono_left (hg : G' ≀ G'') : G'.induce s ≀ G''.induce s := induce_mono hg (by refl) @[mono] lemma induce_mono_right (hs : s βŠ† s') : G'.induce s ≀ G'.induce s' := induce_mono (by refl) hs @[simp] lemma induce_empty : G'.induce βˆ… = βŠ₯ := by ext; simp @[simp] lemma induce_self_verts : G'.induce G'.verts = G' := begin ext, { simp }, { split; simp only [induce_adj, implies_true_iff, and_true] {contextual := tt}, exact Ξ» ha, ⟨G'.edge_vert ha, G'.edge_vert ha.symm⟩ } end lemma singleton_subgraph_eq_induce {v : V} : G.singleton_subgraph v = (⊀ : G.subgraph).induce {v} := by ext; simp [-set.bot_eq_empty, Prop.bot_eq_false] { contextual := tt } lemma subgraph_of_adj_eq_induce {v w : V} (hvw : G.adj v w) : G.subgraph_of_adj hvw = (⊀ : G.subgraph).induce {v, w} := begin ext, { simp }, { split, { intro h, simp only [subgraph_of_adj_adj, quotient.eq, sym2.rel_iff] at h, obtain ⟨rfl, rfl⟩|⟨rfl, rfl⟩ := h; simp [hvw, hvw.symm], }, { intro h, simp only [induce_adj, set.mem_insert_iff, set.mem_singleton_iff, top_adj_iff] at h, obtain ⟨rfl|rfl, rfl|rfl, ha⟩ := h; exact (ha.ne rfl).elim <|> simp } } end end induce /-- Given a subgraph and a set of vertices, delete all the vertices from the subgraph, if present. Any edges indicent to the deleted vertices are deleted as well. -/ @[reducible] def delete_verts (G' : G.subgraph) (s : set V) : G.subgraph := G'.induce (G'.verts \ s) section delete_verts variables {G' : G.subgraph} {s : set V} lemma delete_verts_verts : (G'.delete_verts s).verts = G'.verts \ s := rfl lemma delete_verts_adj {u v : V} : (G'.delete_verts s).adj u v ↔ u ∈ G'.verts ∧ Β¬ u ∈ s ∧ v ∈ G'.verts ∧ Β¬ v ∈ s ∧ G'.adj u v := by simp [and_assoc] @[simp] lemma delete_verts_delete_verts (s s' : set V) : (G'.delete_verts s).delete_verts s' = G'.delete_verts (s βˆͺ s') := by ext; simp [not_or_distrib, and_assoc] { contextual := tt } @[simp] lemma delete_verts_empty : G'.delete_verts βˆ… = G' := by simp [delete_verts] lemma delete_verts_le : G'.delete_verts s ≀ G' := by split; simp [set.diff_subset] @[mono] lemma delete_verts_mono {G' G'' : G.subgraph} (h : G' ≀ G'') : G'.delete_verts s ≀ G''.delete_verts s := induce_mono h (set.diff_subset_diff_left h.1) @[mono] lemma delete_verts_anti {s s' : set V} (h : s βŠ† s') : G'.delete_verts s' ≀ G'.delete_verts s := induce_mono (le_refl _) (set.diff_subset_diff_right h) @[simp] lemma delete_verts_inter_verts_left_eq : G'.delete_verts (G'.verts ∩ s) = G'.delete_verts s := by ext; simp [imp_false] { contextual := tt } @[simp] lemma delete_verts_inter_verts_set_right_eq : G'.delete_verts (s ∩ G'.verts) = G'.delete_verts s := by ext; simp [imp_false] { contextual := tt } end delete_verts end subgraph end simple_graph
1335a26c9fa571af0d82c2731ab75c044f0789a4
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/measure/vector_measure.lean
af04f6d6049671c340f8ba00066f21843ccdf6fc
[ "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
49,770
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.measure.measure_space import analysis.complex.basic /-! # Vector valued measures This file defines vector valued measures, which are Οƒ-additive functions from a set to a add monoid `M` such that it maps the empty set and non-measurable sets to zero. In the case that `M = ℝ`, we called the vector measure a signed measure and write `signed_measure Ξ±`. Similarly, when `M = β„‚`, we call the measure a complex measure and write `complex_measure Ξ±`. ## Main definitions * `measure_theory.vector_measure` is a vector valued, Οƒ-additive function that maps the empty and non-measurable set to zero. * `measure_theory.vector_measure.map` is the pushforward of a vector measure along a function. * `measure_theory.vector_measure.restrict` is the restriction of a vector measure on some set. ## Notation * `v ≀[i] w` means that the vector measure `v` restricted on the set `i` is less than or equal to the vector measure `w` restricted on `i`, i.e. `v.restrict i ≀ w.restrict i`. ## Implementation notes We require all non-measurable sets to be mapped to zero in order for the extensionality lemma to only compare the underlying functions for measurable sets. We use `has_sum` instead of `tsum` in the definition of vector measures in comparison to `measure` since this provides summablity. ## Tags vector measure, signed measure, complex measure -/ noncomputable theory open_locale classical big_operators nnreal ennreal measure_theory namespace measure_theory variables {Ξ± Ξ² : Type*} {m : measurable_space Ξ±} /-- A vector measure on a measurable space `Ξ±` is a Οƒ-additive `M`-valued function (for some `M` an add monoid) such that the empty set and non-measurable sets are mapped to zero. -/ structure vector_measure (Ξ± : Type*) [measurable_space Ξ±] (M : Type*) [add_comm_monoid M] [topological_space M] := (measure_of' : set Ξ± β†’ M) (empty' : measure_of' βˆ… = 0) (not_measurable' ⦃i : set α⦄ : Β¬ measurable_set i β†’ measure_of' i = 0) (m_Union' ⦃f : β„• β†’ set α⦄ : (βˆ€ i, measurable_set (f i)) β†’ pairwise (disjoint on f) β†’ has_sum (Ξ» i, measure_of' (f i)) (measure_of' (⋃ i, f i))) /-- A `signed_measure` is a `ℝ`-vector measure. -/ abbreviation signed_measure (Ξ± : Type*) [measurable_space Ξ±] := vector_measure Ξ± ℝ /-- A `complex_measure` is a `β„‚`-vector_measure. -/ abbreviation complex_measure (Ξ± : Type*) [measurable_space Ξ±] := vector_measure Ξ± β„‚ open set measure_theory namespace vector_measure section variables {M : Type*} [add_comm_monoid M] [topological_space M] include m instance : has_coe_to_fun (vector_measure Ξ± M) (Ξ» _, set Ξ± β†’ M) := ⟨vector_measure.measure_of'⟩ initialize_simps_projections vector_measure (measure_of' β†’ apply) @[simp] lemma measure_of_eq_coe (v : vector_measure Ξ± M) : v.measure_of' = v := rfl @[simp] lemma empty (v : vector_measure Ξ± M) : v βˆ… = 0 := v.empty' lemma not_measurable (v : vector_measure Ξ± M) {i : set Ξ±} (hi : Β¬ measurable_set i) : v i = 0 := v.not_measurable' hi lemma m_Union (v : vector_measure Ξ± M) {f : β„• β†’ set Ξ±} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) : has_sum (Ξ» i, v (f i)) (v (⋃ i, f i)) := v.m_Union' hf₁ hfβ‚‚ lemma of_disjoint_Union_nat [t2_space M] (v : vector_measure Ξ± M) {f : β„• β†’ set Ξ±} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) : v (⋃ i, f i) = βˆ‘' i, v (f i) := (v.m_Union hf₁ hfβ‚‚).tsum_eq.symm lemma coe_injective : @function.injective (vector_measure Ξ± M) (set Ξ± β†’ M) coe_fn := Ξ» v w h, by { cases v, cases w, congr' } lemma ext_iff' (v w : vector_measure Ξ± M) : v = w ↔ βˆ€ i : set Ξ±, v i = w i := by rw [← coe_injective.eq_iff, function.funext_iff] lemma ext_iff (v w : vector_measure Ξ± M) : v = w ↔ βˆ€ i : set Ξ±, measurable_set i β†’ v i = w i := begin split, { rintro rfl _ _, refl }, { rw ext_iff', intros h i, by_cases hi : measurable_set i, { exact h i hi }, { simp_rw [not_measurable _ hi] } } end @[ext] lemma ext {s t : vector_measure Ξ± M} (h : βˆ€ i : set Ξ±, measurable_set i β†’ s i = t i) : s = t := (ext_iff s t).2 h variables [t2_space M] {v : vector_measure Ξ± M} {f : β„• β†’ set Ξ±} lemma has_sum_of_disjoint_Union [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) : has_sum (Ξ» i, v (f i)) (v (⋃ i, f i)) := begin set g := Ξ» i : β„•, ⋃ (b : Ξ²) (H : b ∈ encodable.decodeβ‚‚ Ξ² i), f b with hg, have hg₁ : βˆ€ i, measurable_set (g i), { exact Ξ» _, measurable_set.Union (Ξ» b, measurable_set.Union_Prop $ Ξ» _, hf₁ b) }, have hgβ‚‚ : pairwise (disjoint on g), { exact encodable.Union_decodeβ‚‚_disjoint_on hfβ‚‚ }, have := v.of_disjoint_Union_nat hg₁ hgβ‚‚, rw [hg, encodable.Union_decodeβ‚‚] at this, have hg₃ : (Ξ» (i : Ξ²), v (f i)) = (Ξ» i, v (g (encodable.encode i))), { ext, rw hg, simp only, congr, ext y, simp only [exists_prop, mem_Union, option.mem_def], split, { intro hy, refine ⟨x, (encodable.decodeβ‚‚_is_partial_inv _ _).2 rfl, hy⟩ }, { rintro ⟨b, hb₁, hbβ‚‚βŸ©, rw (encodable.decodeβ‚‚_is_partial_inv _ _) at hb₁, rwa ← encodable.encode_injective hb₁ } }, rw [summable.has_sum_iff, this, ← tsum_Union_decodeβ‚‚], { exact v.empty }, { rw hg₃, change summable ((Ξ» i, v (g i)) ∘ encodable.encode), rw function.injective.summable_iff encodable.encode_injective, { exact (v.m_Union hg₁ hgβ‚‚).summable }, { intros x hx, convert v.empty, simp only [Union_eq_empty, option.mem_def, not_exists, mem_range] at ⊒ hx, intros i hi, exact false.elim ((hx i) ((encodable.decodeβ‚‚_is_partial_inv _ _).1 hi)) } } end lemma of_disjoint_Union [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) : v (⋃ i, f i) = βˆ‘' i, v (f i) := (has_sum_of_disjoint_Union hf₁ hfβ‚‚).tsum_eq.symm lemma of_union {A B : set Ξ±} (h : disjoint A B) (hA : measurable_set A) (hB : measurable_set B) : v (A βˆͺ B) = v A + v B := begin rw [union_eq_Union, of_disjoint_Union, tsum_fintype, fintype.sum_bool, cond, cond], exacts [Ξ» b, bool.cases_on b hB hA, pairwise_disjoint_on_bool.2 h] end lemma of_add_of_diff {A B : set Ξ±} (hA : measurable_set A) (hB : measurable_set B) (h : A βŠ† B) : v A + v (B \ A) = v B := begin rw [← of_union disjoint_diff hA (hB.diff hA), union_diff_cancel h], apply_instance, end lemma of_diff {M : Type*} [add_comm_group M] [topological_space M] [t2_space M] {v : vector_measure Ξ± M} {A B : set Ξ±} (hA : measurable_set A) (hB : measurable_set B) (h : A βŠ† B) : v (B \ A) = v B - (v A) := begin rw [← of_add_of_diff hA hB h, add_sub_cancel'], apply_instance, end lemma of_diff_of_diff_eq_zero {A B : set Ξ±} (hA : measurable_set A) (hB : measurable_set B) (h' : v (B \ A) = 0) : v (A \ B) + v B = v A := begin symmetry, calc v A = v (A \ B βˆͺ A ∩ B) : by simp only [set.diff_union_inter] ... = v (A \ B) + v (A ∩ B) : by { rw of_union, { rw disjoint.comm, exact set.disjoint_of_subset_left (A.inter_subset_right B) set.disjoint_diff }, { exact hA.diff hB }, { exact hA.inter hB } } ... = v (A \ B) + v (A ∩ B βˆͺ B \ A) : by { rw [of_union, h', add_zero], { exact set.disjoint_of_subset_left (A.inter_subset_left B) set.disjoint_diff }, { exact hA.inter hB }, { exact hB.diff hA } } ... = v (A \ B) + v B : by { rw [set.union_comm, set.inter_comm, set.diff_union_inter] } end lemma of_Union_nonneg {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] {v : vector_measure Ξ± M} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) (hf₃ : βˆ€ i, 0 ≀ v (f i)) : 0 ≀ v (⋃ i, f i) := (v.of_disjoint_Union_nat hf₁ hfβ‚‚).symm β–Έ tsum_nonneg hf₃ lemma of_Union_nonpos {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] {v : vector_measure Ξ± M} (hf₁ : βˆ€ i, measurable_set (f i)) (hfβ‚‚ : pairwise (disjoint on f)) (hf₃ : βˆ€ i, v (f i) ≀ 0) : v (⋃ i, f i) ≀ 0 := (v.of_disjoint_Union_nat hf₁ hfβ‚‚).symm β–Έ tsum_nonpos hf₃ lemma of_nonneg_disjoint_union_eq_zero {s : signed_measure Ξ±} {A B : set Ξ±} (h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B) (hAβ‚‚ : 0 ≀ s A) (hBβ‚‚ : 0 ≀ s B) (hAB : s (A βˆͺ B) = 0) : s A = 0 := begin rw of_union h hA₁ hB₁ at hAB, linarith, apply_instance, end lemma of_nonpos_disjoint_union_eq_zero {s : signed_measure Ξ±} {A B : set Ξ±} (h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B) (hAβ‚‚ : s A ≀ 0) (hBβ‚‚ : s B ≀ 0) (hAB : s (A βˆͺ B) = 0) : s A = 0 := begin rw of_union h hA₁ hB₁ at hAB, linarith, apply_instance, end end section add_comm_monoid variables {M : Type*} [add_comm_monoid M] [topological_space M] include m instance : has_zero (vector_measure Ξ± M) := ⟨⟨0, rfl, Ξ» _ _, rfl, Ξ» _ _ _, has_sum_zero⟩⟩ instance : inhabited (vector_measure Ξ± M) := ⟨0⟩ @[simp] lemma coe_zero : ⇑(0 : vector_measure Ξ± M) = 0 := rfl lemma zero_apply (i : set Ξ±) : (0 : vector_measure Ξ± M) i = 0 := rfl variables [has_continuous_add M] /-- The sum of two vector measure is a vector measure. -/ def add (v w : vector_measure Ξ± M) : vector_measure Ξ± M := { measure_of' := v + w, empty' := by simp, not_measurable' := Ξ» _ hi, by simp [v.not_measurable hi, w.not_measurable hi], m_Union' := Ξ» f hf₁ hfβ‚‚, has_sum.add (v.m_Union hf₁ hfβ‚‚) (w.m_Union hf₁ hfβ‚‚) } instance : has_add (vector_measure Ξ± M) := ⟨add⟩ @[simp] lemma coe_add (v w : vector_measure Ξ± M) : ⇑(v + w) = v + w := rfl lemma add_apply (v w : vector_measure Ξ± M) (i : set Ξ±) :(v + w) i = v i + w i := rfl instance : add_comm_monoid (vector_measure Ξ± M) := function.injective.add_comm_monoid _ coe_injective coe_zero coe_add /-- `coe_fn` is an `add_monoid_hom`. -/ @[simps] def coe_fn_add_monoid_hom : vector_measure Ξ± M β†’+ (set Ξ± β†’ M) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } end add_comm_monoid section add_comm_group variables {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M] include m /-- The negative of a vector measure is a vector measure. -/ def neg (v : vector_measure Ξ± M) : vector_measure Ξ± M := { measure_of' := -v, empty' := by simp, not_measurable' := Ξ» _ hi, by simp [v.not_measurable hi], m_Union' := Ξ» f hf₁ hfβ‚‚, has_sum.neg $ v.m_Union hf₁ hfβ‚‚ } instance : has_neg (vector_measure Ξ± M) := ⟨neg⟩ @[simp] lemma coe_neg (v : vector_measure Ξ± M) : ⇑(-v) = - v := rfl lemma neg_apply (v : vector_measure Ξ± M) (i : set Ξ±) :(-v) i = - v i := rfl /-- The difference of two vector measure is a vector measure. -/ def sub (v w : vector_measure Ξ± M) : vector_measure Ξ± M := { measure_of' := v - w, empty' := by simp, not_measurable' := Ξ» _ hi, by simp [v.not_measurable hi, w.not_measurable hi], m_Union' := Ξ» f hf₁ hfβ‚‚, has_sum.sub (v.m_Union hf₁ hfβ‚‚) (w.m_Union hf₁ hfβ‚‚) } instance : has_sub (vector_measure Ξ± M) := ⟨sub⟩ @[simp] lemma coe_sub (v w : vector_measure Ξ± M) : ⇑(v - w) = v - w := rfl lemma sub_apply (v w : vector_measure Ξ± M) (i : set Ξ±) : (v - w) i = v i - w i := rfl instance : add_comm_group (vector_measure Ξ± M) := function.injective.add_comm_group _ coe_injective coe_zero coe_add coe_neg coe_sub end add_comm_group section distrib_mul_action variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [distrib_mul_action R M] variables [topological_space R] [has_continuous_smul R M] include m /-- Given a real number `r` and a signed measure `s`, `smul r s` is the signed measure corresponding to the function `r β€’ s`. -/ def smul (r : R) (v : vector_measure Ξ± M) : vector_measure Ξ± M := { measure_of' := r β€’ v, empty' := by rw [pi.smul_apply, empty, smul_zero], not_measurable' := Ξ» _ hi, by rw [pi.smul_apply, v.not_measurable hi, smul_zero], m_Union' := Ξ» _ hf₁ hfβ‚‚, has_sum.const_smul (v.m_Union hf₁ hfβ‚‚) } instance : has_scalar R (vector_measure Ξ± M) := ⟨smul⟩ @[simp] lemma coe_smul (r : R) (v : vector_measure Ξ± M) : ⇑(r β€’ v) = r β€’ v := rfl lemma smul_apply (r : R) (v : vector_measure Ξ± M) (i : set Ξ±) : (r β€’ v) i = r β€’ v i := rfl instance [has_continuous_add M] : distrib_mul_action R (vector_measure Ξ± M) := function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul end distrib_mul_action section module variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [module R M] variables [topological_space R] [has_continuous_smul R M] include m instance [has_continuous_add M] : module R (vector_measure Ξ± M) := function.injective.module R coe_fn_add_monoid_hom coe_injective coe_smul end module end vector_measure namespace measure include m /-- A finite measure coerced into a real function is a signed measure. -/ @[simps] def to_signed_measure (ΞΌ : measure Ξ±) [hΞΌ : is_finite_measure ΞΌ] : signed_measure Ξ± := { measure_of' := Ξ» i : set Ξ±, if measurable_set i then (ΞΌ.measure_of i).to_real else 0, empty' := by simp [ΞΌ.empty], not_measurable' := Ξ» _ hi, if_neg hi, m_Union' := begin intros _ hf₁ hfβ‚‚, rw [ΞΌ.m_Union hf₁ hfβ‚‚, ennreal.tsum_to_real_eq, if_pos (measurable_set.Union hf₁), summable.has_sum_iff], { congr, ext n, rw if_pos (hf₁ n) }, { refine @summable_of_nonneg_of_le _ (ennreal.to_real ∘ ΞΌ ∘ f) _ _ _ _, { intro, split_ifs, exacts [ennreal.to_real_nonneg, le_rfl] }, { intro, split_ifs, exacts [le_rfl, ennreal.to_real_nonneg] }, exact summable_measure_to_real hf₁ hfβ‚‚ }, { intros a ha, apply ne_of_lt hΞΌ.measure_univ_lt_top, rw [eq_top_iff, ← ha, outer_measure.measure_of_eq_coe, coe_to_outer_measure], exact measure_mono (set.subset_univ _) } end } lemma to_signed_measure_apply_measurable {ΞΌ : measure Ξ±} [is_finite_measure ΞΌ] {i : set Ξ±} (hi : measurable_set i) : ΞΌ.to_signed_measure i = (ΞΌ i).to_real := if_pos hi -- Without this lemma, `singular_part_neg` in `measure_theory.decomposition.lebesgue` is -- extremely slow lemma to_signed_measure_congr {ΞΌ Ξ½ : measure Ξ±} [is_finite_measure ΞΌ] [is_finite_measure Ξ½] (h : ΞΌ = Ξ½) : ΞΌ.to_signed_measure = Ξ½.to_signed_measure := by { congr, exact h } lemma to_signed_measure_eq_to_signed_measure_iff {ΞΌ Ξ½ : measure Ξ±} [is_finite_measure ΞΌ] [is_finite_measure Ξ½] : ΞΌ.to_signed_measure = Ξ½.to_signed_measure ↔ ΞΌ = Ξ½ := begin refine ⟨λ h, _, Ξ» h, _⟩, { ext1 i hi, have : ΞΌ.to_signed_measure i = Ξ½.to_signed_measure i, { rw h }, rwa [to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi, ennreal.to_real_eq_to_real] at this; { exact measure_ne_top _ _ } }, { congr, assumption } end @[simp] lemma to_signed_measure_zero : (0 : measure Ξ±).to_signed_measure = 0 := by { ext i hi, simp } @[simp] lemma to_signed_measure_add (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] [is_finite_measure Ξ½] : (ΞΌ + Ξ½).to_signed_measure = ΞΌ.to_signed_measure + Ξ½.to_signed_measure := begin ext i hi, rw [to_signed_measure_apply_measurable hi, add_apply, ennreal.to_real_add (ne_of_lt (measure_lt_top _ _ )) (ne_of_lt (measure_lt_top _ _)), vector_measure.add_apply, to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi], all_goals { apply_instance } end @[simp] lemma to_signed_measure_smul (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ] (r : ℝβ‰₯0) : (r β€’ ΞΌ).to_signed_measure = r β€’ ΞΌ.to_signed_measure := begin ext i hi, rw [to_signed_measure_apply_measurable hi, vector_measure.smul_apply, to_signed_measure_apply_measurable hi, coe_nnreal_smul, pi.smul_apply, ennreal.to_real_smul], end /-- A measure is a vector measure over `ℝβ‰₯0∞`. -/ @[simps] def to_ennreal_vector_measure (ΞΌ : measure Ξ±) : vector_measure Ξ± ℝβ‰₯0∞ := { measure_of' := Ξ» i : set Ξ±, if measurable_set i then ΞΌ i else 0, empty' := by simp [ΞΌ.empty], not_measurable' := Ξ» _ hi, if_neg hi, m_Union' := Ξ» _ hf₁ hfβ‚‚, begin rw summable.has_sum_iff ennreal.summable, { rw [if_pos (measurable_set.Union hf₁), measure_theory.measure_Union hfβ‚‚ hf₁], exact tsum_congr (Ξ» n, if_pos (hf₁ n)) }, end } lemma to_ennreal_vector_measure_apply_measurable {ΞΌ : measure Ξ±} {i : set Ξ±} (hi : measurable_set i) : ΞΌ.to_ennreal_vector_measure i = ΞΌ i := if_pos hi @[simp] lemma to_ennreal_vector_measure_zero : (0 : measure Ξ±).to_ennreal_vector_measure = 0 := by { ext i hi, simp } @[simp] lemma to_ennreal_vector_measure_add (ΞΌ Ξ½ : measure Ξ±) : (ΞΌ + Ξ½).to_ennreal_vector_measure = ΞΌ.to_ennreal_vector_measure + Ξ½.to_ennreal_vector_measure := begin refine measure_theory.vector_measure.ext (Ξ» i hi, _), rw [to_ennreal_vector_measure_apply_measurable hi, add_apply, vector_measure.add_apply, to_ennreal_vector_measure_apply_measurable hi, to_ennreal_vector_measure_apply_measurable hi] end lemma to_signed_measure_sub_apply {ΞΌ Ξ½ : measure Ξ±} [is_finite_measure ΞΌ] [is_finite_measure Ξ½] {i : set Ξ±} (hi : measurable_set i) : (ΞΌ.to_signed_measure - Ξ½.to_signed_measure) i = (ΞΌ i).to_real - (Ξ½ i).to_real := begin rw [vector_measure.sub_apply, to_signed_measure_apply_measurable hi, measure.to_signed_measure_apply_measurable hi, sub_eq_add_neg] end end measure namespace vector_measure open measure section /-- A vector measure over `ℝβ‰₯0∞` is a measure. -/ def ennreal_to_measure {m : measurable_space Ξ±} (v : vector_measure Ξ± ℝβ‰₯0∞) : measure Ξ± := of_measurable (Ξ» s _, v s) v.empty (Ξ» f hf₁ hfβ‚‚, v.of_disjoint_Union_nat hf₁ hfβ‚‚) lemma ennreal_to_measure_apply {m : measurable_space Ξ±} {v : vector_measure Ξ± ℝβ‰₯0∞} {s : set Ξ±} (hs : measurable_set s) : ennreal_to_measure v s = v s := by rw [ennreal_to_measure, of_measurable_apply _ hs] /-- The equiv between `vector_measure Ξ± ℝβ‰₯0∞` and `measure Ξ±` formed by `measure_theory.vector_measure.ennreal_to_measure` and `measure_theory.measure.to_ennreal_vector_measure`. -/ @[simps] def equiv_measure [measurable_space Ξ±] : vector_measure Ξ± ℝβ‰₯0∞ ≃ measure Ξ± := { to_fun := ennreal_to_measure, inv_fun := to_ennreal_vector_measure, left_inv := Ξ» _, ext (Ξ» s hs, by rw [to_ennreal_vector_measure_apply_measurable hs, ennreal_to_measure_apply hs]), right_inv := Ξ» _, measure.ext (Ξ» s hs, by rw [ennreal_to_measure_apply hs, to_ennreal_vector_measure_apply_measurable hs]) } end section variables [measurable_space Ξ±] [measurable_space Ξ²] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables (v : vector_measure Ξ± M) /-- The pushforward of a vector measure along a function. -/ def map (v : vector_measure Ξ± M) (f : Ξ± β†’ Ξ²) : vector_measure Ξ² M := if hf : measurable f then { measure_of' := Ξ» s, if measurable_set s then v (f ⁻¹' s) else 0, empty' := by simp, not_measurable' := Ξ» i hi, if_neg hi, m_Union' := begin intros g hg₁ hgβ‚‚, convert v.m_Union (Ξ» i, hf (hg₁ i)) (Ξ» i j hij x hx, hgβ‚‚ i j hij hx), { ext i, rw if_pos (hg₁ i) }, { rw [preimage_Union, if_pos (measurable_set.Union hg₁)] } end } else 0 lemma map_not_measurable {f : Ξ± β†’ Ξ²} (hf : Β¬ measurable f) : v.map f = 0 := dif_neg hf lemma map_apply {f : Ξ± β†’ Ξ²} (hf : measurable f) {s : set Ξ²} (hs : measurable_set s) : v.map f s = v (f ⁻¹' s) := by { rw [map, dif_pos hf], exact if_pos hs } @[simp] lemma map_id : v.map id = v := ext (Ξ» i hi, by rw [map_apply v measurable_id hi, preimage_id]) @[simp] lemma map_zero (f : Ξ± β†’ Ξ²) : (0 : vector_measure Ξ± M).map f = 0 := begin by_cases hf : measurable f, { ext i hi, rw [map_apply _ hf hi, zero_apply, zero_apply] }, { exact dif_neg hf } end section variables {N : Type*} [add_comm_monoid N] [topological_space N] /-- Given a vector measure `v` on `M` and a continuous add_monoid_hom `f : M β†’ N`, `f ∘ v` is a vector measure on `N`. -/ def map_range (v : vector_measure Ξ± M) (f : M β†’+ N) (hf : continuous f) : vector_measure Ξ± N := { measure_of' := Ξ» s, f (v s), empty' := by rw [empty, add_monoid_hom.map_zero], not_measurable' := Ξ» i hi, by rw [not_measurable v hi, add_monoid_hom.map_zero], m_Union' := Ξ» g hg₁ hgβ‚‚, has_sum.map (v.m_Union hg₁ hgβ‚‚) f hf } @[simp] lemma map_range_apply {f : M β†’+ N} (hf : continuous f) {s : set Ξ±} : v.map_range f hf s = f (v s) := rfl @[simp] lemma map_range_id : v.map_range (add_monoid_hom.id M) continuous_id = v := by { ext, refl } @[simp] lemma map_range_zero {f : M β†’+ N} (hf : continuous f) : map_range (0 : vector_measure Ξ± M) f hf = 0 := by { ext, simp } section has_continuous_add variables [has_continuous_add M] [has_continuous_add N] @[simp] lemma map_range_add {v w : vector_measure Ξ± M} {f : M β†’+ N} (hf : continuous f) : (v + w).map_range f hf = v.map_range f hf + w.map_range f hf := by { ext, simp } /-- Given a continuous add_monoid_hom `f : M β†’ N`, `map_range_hom` is the add_monoid_hom mapping the vector measure `v` on `M` to the vector measure `f ∘ v` on `N`. -/ def map_range_hom (f : M β†’+ N) (hf : continuous f) : vector_measure Ξ± M β†’+ vector_measure Ξ± N := { to_fun := Ξ» v, v.map_range f hf, map_zero' := map_range_zero hf, map_add' := Ξ» _ _, map_range_add hf } end has_continuous_add section module variables {R : Type*} [semiring R] [module R M] [module R N] variables [topological_space R] [has_continuous_add M] [has_continuous_add N] [has_continuous_smul R M] [has_continuous_smul R N] /-- Given a continuous linear map `f : M β†’ N`, `map_rangeβ‚—` is the linear map mapping the vector measure `v` on `M` to the vector measure `f ∘ v` on `N`. -/ def map_rangeβ‚— (f : M β†’β‚—[R] N) (hf : continuous f) : vector_measure Ξ± M β†’β‚—[R] vector_measure Ξ± N := { to_fun := Ξ» v, v.map_range f.to_add_monoid_hom hf, map_add' := Ξ» _ _, map_range_add hf, map_smul' := by { intros, ext, simp } } end module end /-- The restriction of a vector measure on some set. -/ def restrict (v : vector_measure Ξ± M) (i : set Ξ±) : vector_measure Ξ± M := if hi : measurable_set i then { measure_of' := Ξ» s, if measurable_set s then v (s ∩ i) else 0, empty' := by simp, not_measurable' := Ξ» i hi, if_neg hi, m_Union' := begin intros f hf₁ hfβ‚‚, convert v.m_Union (Ξ» n, (hf₁ n).inter hi) (hfβ‚‚.mono $ Ξ» i j, disjoint.mono inf_le_left inf_le_left), { ext n, rw if_pos (hf₁ n) }, { rw [Union_inter, if_pos (measurable_set.Union hf₁)] } end } else 0 lemma restrict_not_measurable {i : set Ξ±} (hi : Β¬ measurable_set i) : v.restrict i = 0 := dif_neg hi lemma restrict_apply {i : set Ξ±} (hi : measurable_set i) {j : set Ξ±} (hj : measurable_set j) : v.restrict i j = v (j ∩ i) := by { rw [restrict, dif_pos hi], exact if_pos hj } lemma restrict_eq_self {i : set Ξ±} (hi : measurable_set i) {j : set Ξ±} (hj : measurable_set j) (hij : j βŠ† i) : v.restrict i j = v j := by rw [restrict_apply v hi hj, inter_eq_left_iff_subset.2 hij] @[simp] lemma restrict_empty : v.restrict βˆ… = 0 := ext (Ξ» i hi, by rw [restrict_apply v measurable_set.empty hi, inter_empty, v.empty, zero_apply]) @[simp] lemma restrict_univ : v.restrict univ = v := ext (Ξ» i hi, by rw [restrict_apply v measurable_set.univ hi, inter_univ]) @[simp] lemma restrict_zero {i : set Ξ±} : (0 : vector_measure Ξ± M).restrict i = 0 := begin by_cases hi : measurable_set i, { ext j hj, rw [restrict_apply 0 hi hj], refl }, { exact dif_neg hi } end section has_continuous_add variables [has_continuous_add M] lemma map_add (v w : vector_measure Ξ± M) (f : Ξ± β†’ Ξ²) : (v + w).map f = v.map f + w.map f := begin by_cases hf : measurable f, { ext i hi, simp [map_apply _ hf hi] }, { simp [map, dif_neg hf] } end /-- `vector_measure.map` as an additive monoid homomorphism. -/ @[simps] def map_gm (f : Ξ± β†’ Ξ²) : vector_measure Ξ± M β†’+ vector_measure Ξ² M := { to_fun := Ξ» v, v.map f, map_zero' := map_zero f, map_add' := Ξ» _ _, map_add _ _ f } lemma restrict_add (v w : vector_measure Ξ± M) (i : set Ξ±) : (v + w).restrict i = v.restrict i + w.restrict i := begin by_cases hi : measurable_set i, { ext j hj, simp [restrict_apply _ hi hj] }, { simp [restrict_not_measurable _ hi] } end /--`vector_measure.restrict` as an additive monoid homomorphism. -/ @[simps] def restrict_gm (i : set Ξ±) : vector_measure Ξ± M β†’+ vector_measure Ξ± M := { to_fun := Ξ» v, v.restrict i, map_zero' := restrict_zero, map_add' := Ξ» _ _, restrict_add _ _ i } end has_continuous_add end section variables [measurable_space Ξ²] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [distrib_mul_action R M] variables [topological_space R] [has_continuous_smul R M] include m @[simp] lemma map_smul {v : vector_measure Ξ± M} {f : Ξ± β†’ Ξ²} (c : R) : (c β€’ v).map f = c β€’ v.map f := begin by_cases hf : measurable f, { ext i hi, simp [map_apply _ hf hi] }, { simp only [map, dif_neg hf], -- `smul_zero` does not work since we do not require `has_continuous_add` ext i hi, simp } end @[simp] lemma restrict_smul {v :vector_measure Ξ± M} {i : set Ξ±} (c : R) : (c β€’ v).restrict i = c β€’ v.restrict i := begin by_cases hi : measurable_set i, { ext j hj, simp [restrict_apply _ hi hj] }, { simp only [restrict_not_measurable _ hi], -- `smul_zero` does not work since we do not require `has_continuous_add` ext j hj, simp } end end section variables [measurable_space Ξ²] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [module R M] variables [topological_space R] [has_continuous_smul R M] [has_continuous_add M] include m /-- `vector_measure.map` as a linear map. -/ @[simps] def mapβ‚— (f : Ξ± β†’ Ξ²) : vector_measure Ξ± M β†’β‚—[R] vector_measure Ξ² M := { to_fun := Ξ» v, v.map f, map_add' := Ξ» _ _, map_add _ _ f, map_smul' := Ξ» _ _, map_smul _ } /-- `vector_measure.restrict` as an additive monoid homomorphism. -/ @[simps] def restrictβ‚— (i : set Ξ±) : vector_measure Ξ± M β†’β‚—[R] vector_measure Ξ± M := { to_fun := Ξ» v, v.restrict i, map_add' := Ξ» _ _, restrict_add _ _ i, map_smul' := Ξ» _ _, restrict_smul _ } end section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] include m /-- Vector measures over a partially ordered monoid is partially ordered. This definition is consistent with `measure.partial_order`. -/ instance : partial_order (vector_measure Ξ± M) := { le := Ξ» v w, βˆ€ i, measurable_set i β†’ v i ≀ w i, le_refl := Ξ» v i hi, le_rfl, le_trans := Ξ» u v w h₁ hβ‚‚ i hi, le_trans (h₁ i hi) (hβ‚‚ i hi), le_antisymm := Ξ» v w h₁ hβ‚‚, ext (Ξ» i hi, le_antisymm (h₁ i hi) (hβ‚‚ i hi)) } variables {u v w : vector_measure Ξ± M} lemma le_iff : v ≀ w ↔ βˆ€ i, measurable_set i β†’ v i ≀ w i := iff.rfl lemma le_iff' : v ≀ w ↔ βˆ€ i, v i ≀ w i := begin refine ⟨λ h i, _, Ξ» h i hi, h i⟩, by_cases hi : measurable_set i, { exact h i hi }, { rw [v.not_measurable hi, w.not_measurable hi] } end end localized "notation v ` ≀[`:50 i:50 `] `:0 w:50 := measure_theory.vector_measure.restrict v i ≀ measure_theory.vector_measure.restrict w i" in measure_theory section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] variables (v w : vector_measure Ξ± M) lemma restrict_le_restrict_iff {i : set Ξ±} (hi : measurable_set i) : v ≀[i] w ↔ βˆ€ ⦃j⦄, measurable_set j β†’ j βŠ† i β†’ v j ≀ w j := ⟨λ h j hj₁ hjβ‚‚, (restrict_eq_self v hi hj₁ hjβ‚‚) β–Έ (restrict_eq_self w hi hj₁ hjβ‚‚) β–Έ h j hj₁, Ξ» h, le_iff.1 (Ξ» j hj, (restrict_apply v hi hj).symm β–Έ (restrict_apply w hi hj).symm β–Έ h (hj.inter hi) (set.inter_subset_right j i))⟩ lemma subset_le_of_restrict_le_restrict {i : set Ξ±} (hi : measurable_set i) (hiβ‚‚ : v ≀[i] w) {j : set Ξ±} (hj : j βŠ† i) : v j ≀ w j := begin by_cases hj₁ : measurable_set j, { exact (restrict_le_restrict_iff _ _ hi).1 hiβ‚‚ hj₁ hj }, { rw [v.not_measurable hj₁, w.not_measurable hj₁] }, end lemma restrict_le_restrict_of_subset_le {i : set Ξ±} (h : βˆ€ ⦃j⦄, measurable_set j β†’ j βŠ† i β†’ v j ≀ w j) : v ≀[i] w := begin by_cases hi : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi).2 h }, { rw [restrict_not_measurable v hi, restrict_not_measurable w hi], exact le_rfl }, end lemma restrict_le_restrict_subset {i j : set Ξ±} (hi₁ : measurable_set i) (hiβ‚‚ : v ≀[i] w) (hij : j βŠ† i) : v ≀[j] w := restrict_le_restrict_of_subset_le v w (Ξ» k hk₁ hkβ‚‚, subset_le_of_restrict_le_restrict v w hi₁ hiβ‚‚ (set.subset.trans hkβ‚‚ hij)) lemma le_restrict_empty : v ≀[βˆ…] w := begin intros j hj, rw [restrict_empty, restrict_empty] end lemma le_restrict_univ_iff_le : v ≀[univ] w ↔ v ≀ w := begin split, { intros h s hs, have := h s hs, rwa [restrict_apply _ measurable_set.univ hs, inter_univ, restrict_apply _ measurable_set.univ hs, inter_univ] at this }, { intros h s hs, rw [restrict_apply _ measurable_set.univ hs, inter_univ, restrict_apply _ measurable_set.univ hs, inter_univ], exact h s hs } end end section variables {M : Type*} [topological_space M] [ordered_add_comm_group M] [topological_add_group M] variables (v w : vector_measure Ξ± M) lemma neg_le_neg {i : set Ξ±} (hi : measurable_set i) (h : v ≀[i] w) : -w ≀[i] -v := begin intros j hj₁, rw [restrict_apply _ hi hj₁, restrict_apply _ hi hj₁, neg_apply, neg_apply], refine neg_le_neg _, rw [← restrict_apply _ hi hj₁, ← restrict_apply _ hi hj₁], exact h j hj₁, end @[simp] lemma neg_le_neg_iff {i : set Ξ±} (hi : measurable_set i) : -w ≀[i] -v ↔ v ≀[i] w := ⟨λ h, neg_neg v β–Έ neg_neg w β–Έ neg_le_neg _ _ hi h, Ξ» h, neg_le_neg _ _ hi h⟩ end section variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] variables (v w : vector_measure Ξ± M) {i j : set Ξ±} lemma restrict_le_restrict_Union {f : β„• β†’ set Ξ±} (hf₁ : βˆ€ n, measurable_set (f n)) (hfβ‚‚ : βˆ€ n, v ≀[f n] w) : v ≀[⋃ n, f n] w := begin refine restrict_le_restrict_of_subset_le v w (Ξ» a ha₁ haβ‚‚, _), have ha₃ : (⋃ n, a ∩ disjointed f n) = a, { rwa [← inter_Union, Union_disjointed, inter_eq_left_iff_subset] }, have haβ‚„ : pairwise (disjoint on (Ξ» n, a ∩ disjointed f n)), { exact (disjoint_disjointed _).mono (Ξ» i j, disjoint.mono inf_le_right inf_le_right) }, rw [← ha₃, v.of_disjoint_Union_nat _ haβ‚„, w.of_disjoint_Union_nat _ haβ‚„], refine tsum_le_tsum (Ξ» n, (restrict_le_restrict_iff v w (hf₁ n)).1 (hfβ‚‚ n) _ _) _ _, { exact (ha₁.inter (measurable_set.disjointed hf₁ n)) }, { exact set.subset.trans (set.inter_subset_right _ _) (disjointed_subset _ _) }, { refine (v.m_Union (Ξ» n, _) _).summable, { exact ha₁.inter (measurable_set.disjointed hf₁ n) }, { exact (disjoint_disjointed _).mono (Ξ» i j, disjoint.mono inf_le_right inf_le_right) } }, { refine (w.m_Union (Ξ» n, _) _).summable, { exact ha₁.inter (measurable_set.disjointed hf₁ n) }, { exact (disjoint_disjointed _).mono (Ξ» i j, disjoint.mono inf_le_right inf_le_right) } }, { intro n, exact (ha₁.inter (measurable_set.disjointed hf₁ n)) }, { exact Ξ» n, ha₁.inter (measurable_set.disjointed hf₁ n) } end lemma restrict_le_restrict_encodable_Union [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hf₁ : βˆ€ b, measurable_set (f b)) (hfβ‚‚ : βˆ€ b, v ≀[f b] w) : v ≀[⋃ b, f b] w := begin rw ← encodable.Union_decodeβ‚‚, refine restrict_le_restrict_Union v w _ _, { intro n, measurability }, { intro n, cases encodable.decodeβ‚‚ Ξ² n with b, { simp }, { simp [hfβ‚‚ b] } } end lemma restrict_le_restrict_union (hi₁ : measurable_set i) (hiβ‚‚ : v ≀[i] w) (hj₁ : measurable_set j) (hjβ‚‚ : v ≀[j] w) : v ≀[i βˆͺ j] w := begin rw union_eq_Union, refine restrict_le_restrict_encodable_Union v w _ _, { measurability }, { rintro (_ | _); simpa } end end section variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M] variables (v w : vector_measure Ξ± M) {i j : set Ξ±} lemma nonneg_of_zero_le_restrict (hiβ‚‚ : 0 ≀[i] v) : 0 ≀ v i := begin by_cases hi₁ : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi₁).1 hiβ‚‚ hi₁ set.subset.rfl }, { rw v.not_measurable hi₁ }, end lemma nonpos_of_restrict_le_zero (hiβ‚‚ : v ≀[i] 0) : v i ≀ 0 := begin by_cases hi₁ : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi₁).1 hiβ‚‚ hi₁ set.subset.rfl }, { rw v.not_measurable hi₁ } end lemma zero_le_restrict_not_measurable (hi : Β¬ measurable_set i) : 0 ≀[i] v := begin rw [restrict_zero, restrict_not_measurable _ hi], exact le_rfl, end lemma restrict_le_zero_of_not_measurable (hi : Β¬ measurable_set i) : v ≀[i] 0 := begin rw [restrict_zero, restrict_not_measurable _ hi], exact le_rfl, end lemma measurable_of_not_zero_le_restrict (hi : Β¬ 0 ≀[i] v) : measurable_set i := not.imp_symm (zero_le_restrict_not_measurable _) hi lemma measurable_of_not_restrict_le_zero (hi : Β¬ v ≀[i] 0) : measurable_set i := not.imp_symm (restrict_le_zero_of_not_measurable _) hi lemma zero_le_restrict_subset (hi₁ : measurable_set i) (hij : j βŠ† i) (hiβ‚‚ : 0 ≀[i] v): 0 ≀[j] v := restrict_le_restrict_of_subset_le _ _ (Ξ» k hk₁ hkβ‚‚, (restrict_le_restrict_iff _ _ hi₁).1 hiβ‚‚ hk₁ (set.subset.trans hkβ‚‚ hij)) lemma restrict_le_zero_subset (hi₁ : measurable_set i) (hij : j βŠ† i) (hiβ‚‚ : v ≀[i] 0): v ≀[j] 0 := restrict_le_restrict_of_subset_le _ _ (Ξ» k hk₁ hkβ‚‚, (restrict_le_restrict_iff _ _ hi₁).1 hiβ‚‚ hk₁ (set.subset.trans hkβ‚‚ hij)) end section variables {M : Type*} [topological_space M] [linear_ordered_add_comm_monoid M] variables (v w : vector_measure Ξ± M) {i j : set Ξ±} include m lemma exists_pos_measure_of_not_restrict_le_zero (hi : Β¬ v ≀[i] 0) : βˆƒ j : set Ξ±, measurable_set j ∧ j βŠ† i ∧ 0 < v j := begin have hi₁ : measurable_set i := measurable_of_not_restrict_le_zero _ hi, rw [restrict_le_restrict_iff _ _ hi₁] at hi, push_neg at hi, obtain ⟨j, hj₁, hjβ‚‚, hj⟩ := hi, exact ⟨j, hj₁, hjβ‚‚, hj⟩, end end section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] [covariant_class M M (+) (≀)] [has_continuous_add M] include m instance covariant_add_le : covariant_class (vector_measure Ξ± M) (vector_measure Ξ± M) (+) (≀) := ⟨λ u v w h i hi, add_le_add_left (h i hi) _⟩ end section variables {L M N : Type*} variables [add_comm_monoid L] [topological_space L] [add_comm_monoid M] [topological_space M] [add_comm_monoid N] [topological_space N] include m /-- A vector measure `v` is absolutely continuous with respect to a measure `ΞΌ` if for all sets `s`, `ΞΌ s = 0`, we have `v s = 0`. -/ def absolutely_continuous (v : vector_measure Ξ± M) (w : vector_measure Ξ± N) := βˆ€ ⦃s : set α⦄, w s = 0 β†’ v s = 0 localized "infix ` β‰ͺα΅₯ `:50 := measure_theory.vector_measure.absolutely_continuous" in measure_theory open_locale measure_theory namespace absolutely_continuous variables {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} lemma mk (h : βˆ€ ⦃s : set α⦄, measurable_set s β†’ w s = 0 β†’ v s = 0) : v β‰ͺα΅₯ w := begin intros s hs, by_cases hmeas : measurable_set s, { exact h hmeas hs }, { exact not_measurable v hmeas } end lemma eq {w : vector_measure Ξ± M} (h : v = w) : v β‰ͺα΅₯ w := Ξ» s hs, h.symm β–Έ hs @[refl] lemma refl (v : vector_measure Ξ± M) : v β‰ͺα΅₯ v := eq rfl @[trans] lemma trans {u : vector_measure Ξ± L} (huv : u β‰ͺα΅₯ v) (hvw : v β‰ͺα΅₯ w) : u β‰ͺα΅₯ w := Ξ» _ hs, huv $ hvw hs lemma zero (v : vector_measure Ξ± N) : (0 : vector_measure Ξ± M) β‰ͺα΅₯ v := Ξ» s _, vector_measure.zero_apply s lemma neg_left {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} (h : v β‰ͺα΅₯ w) : -v β‰ͺα΅₯ w := Ξ» s hs, by { rw [neg_apply, h hs, neg_zero] } lemma neg_right {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} (h : v β‰ͺα΅₯ w) : v β‰ͺα΅₯ -w := begin intros s hs, rw [neg_apply, neg_eq_zero] at hs, exact h hs end lemma add [has_continuous_add M] {v₁ vβ‚‚ : vector_measure Ξ± M} {w : vector_measure Ξ± N} (hv₁ : v₁ β‰ͺα΅₯ w) (hvβ‚‚ : vβ‚‚ β‰ͺα΅₯ w) : v₁ + vβ‚‚ β‰ͺα΅₯ w := Ξ» s hs, by { rw [add_apply, hv₁ hs, hvβ‚‚ hs, zero_add] } lemma sub {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M] {v₁ vβ‚‚ : vector_measure Ξ± M} {w : vector_measure Ξ± N} (hv₁ : v₁ β‰ͺα΅₯ w) (hvβ‚‚ : vβ‚‚ β‰ͺα΅₯ w) : v₁ - vβ‚‚ β‰ͺα΅₯ w := Ξ» s hs, by { rw [sub_apply, hv₁ hs, hvβ‚‚ hs, zero_sub, neg_zero] } lemma smul {R : Type*} [semiring R] [distrib_mul_action R M] [topological_space R] [has_continuous_smul R M] {r : R} {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} (h : v β‰ͺα΅₯ w) : r β€’ v β‰ͺα΅₯ w := Ξ» s hs, by { rw [smul_apply, h hs, smul_zero] } lemma map [measure_space Ξ²] (h : v β‰ͺα΅₯ w) (f : Ξ± β†’ Ξ²) : v.map f β‰ͺα΅₯ w.map f := begin by_cases hf : measurable f, { refine mk (Ξ» s hs hws, _), rw map_apply _ hf hs at hws ⊒, exact h hws }, { intros s hs, rw [map_not_measurable v hf, zero_apply] } end lemma ennreal_to_measure {ΞΌ : vector_measure Ξ± ℝβ‰₯0∞} : (βˆ€ ⦃s : set α⦄, ΞΌ.ennreal_to_measure s = 0 β†’ v s = 0) ↔ v β‰ͺα΅₯ ΞΌ := begin split; intro h, { refine mk (Ξ» s hmeas hs, h _), rw [← hs, ennreal_to_measure_apply hmeas] }, { intros s hs, by_cases hmeas : measurable_set s, { rw ennreal_to_measure_apply hmeas at hs, exact h hs }, { exact not_measurable v hmeas } }, end end absolutely_continuous /-- Two vector measures `v` and `w` are said to be mutually singular if there exists a measurable set `s`, such that for all `t βŠ† s`, `v t = 0` and for all `t βŠ† sᢜ`, `w t = 0`. We note that we do not require the measurability of `t` in the definition since this makes it easier to use. This is equivalent to the definition which requires measurability. To prove `mutually_singular` with the measurability condition, use `measure_theory.vector_measure.mutually_singular.mk`. -/ def mutually_singular (v : vector_measure Ξ± M) (w : vector_measure Ξ± N) : Prop := βˆƒ (s : set Ξ±), measurable_set s ∧ (βˆ€ t βŠ† s, v t = 0) ∧ (βˆ€ t βŠ† sᢜ, w t = 0) localized "infix ` βŠ₯α΅₯ `:60 := measure_theory.vector_measure.mutually_singular" in measure_theory namespace mutually_singular variables {v v₁ vβ‚‚ : vector_measure Ξ± M} {w w₁ wβ‚‚ : vector_measure Ξ± N} lemma mk (s : set Ξ±) (hs : measurable_set s) (h₁ : βˆ€ t βŠ† s, measurable_set t β†’ v t = 0) (hβ‚‚ : βˆ€ t βŠ† sᢜ, measurable_set t β†’ w t = 0) : v βŠ₯α΅₯ w := begin refine ⟨s, hs, Ξ» t hst, _, Ξ» t hst, _⟩; by_cases ht : measurable_set t, { exact h₁ t hst ht }, { exact not_measurable v ht }, { exact hβ‚‚ t hst ht }, { exact not_measurable w ht } end lemma symm (h : v βŠ₯α΅₯ w) : w βŠ₯α΅₯ v := let ⟨s, hmeas, hs₁, hsβ‚‚βŸ© := h in ⟨sᢜ, hmeas.compl, hsβ‚‚, Ξ» t ht, hs₁ _ (compl_compl s β–Έ ht : t βŠ† s)⟩ lemma zero_right : v βŠ₯α΅₯ (0 : vector_measure Ξ± N) := βŸ¨βˆ…, measurable_set.empty, Ξ» t ht, (subset_empty_iff.1 ht).symm β–Έ v.empty, Ξ» _ _, zero_apply _⟩ lemma zero_left : (0 : vector_measure Ξ± M) βŠ₯α΅₯ w := zero_right.symm lemma add_left [t2_space N] [has_continuous_add M] (h₁ : v₁ βŠ₯α΅₯ w) (hβ‚‚ : vβ‚‚ βŠ₯α΅₯ w) : v₁ + vβ‚‚ βŠ₯α΅₯ w := begin obtain ⟨u, hmu, hu₁, huβ‚‚βŸ© := h₁, obtain ⟨v, hmv, hv₁, hvβ‚‚βŸ© := hβ‚‚, refine mk (u ∩ v) (hmu.inter hmv) (Ξ» t ht hmt, _) (Ξ» t ht hmt, _), { rw [add_apply, hu₁ _ (subset_inter_iff.1 ht).1, hv₁ _ (subset_inter_iff.1 ht).2, zero_add] }, { rw compl_inter at ht, rw [(_ : t = (uᢜ ∩ t) βˆͺ (vᢜ \ uᢜ ∩ t)), of_union _ (hmu.compl.inter hmt) ((hmv.compl.diff hmu.compl).inter hmt), huβ‚‚, hvβ‚‚, add_zero], { exact subset.trans (inter_subset_left _ _) (diff_subset _ _) }, { exact inter_subset_left _ _ }, { apply_instance }, { exact disjoint.mono (inter_subset_left _ _) (inter_subset_left _ _) disjoint_diff }, { apply subset.antisymm; intros x hx, { by_cases hxu' : x ∈ uᢜ, { exact or.inl ⟨hxu', hx⟩ }, rcases ht hx with (hxu | hxv), exacts [false.elim (hxu' hxu), or.inr ⟨⟨hxv, hxu'⟩, hx⟩] }, { rcases hx; exact hx.2 } } }, end lemma add_right [t2_space M] [has_continuous_add N] (h₁ : v βŠ₯α΅₯ w₁) (hβ‚‚ : v βŠ₯α΅₯ wβ‚‚) : v βŠ₯α΅₯ w₁ + wβ‚‚ := (add_left h₁.symm hβ‚‚.symm).symm lemma smul_right {R : Type*} [semiring R] [distrib_mul_action R N] [topological_space R] [has_continuous_smul R N] (r : R) (h : v βŠ₯α΅₯ w) : v βŠ₯α΅₯ r β€’ w := let ⟨s, hmeas, hs₁, hsβ‚‚βŸ© := h in ⟨s, hmeas, hs₁, Ξ» t ht, by simp only [coe_smul, pi.smul_apply, hsβ‚‚ t ht, smul_zero]⟩ lemma smul_left {R : Type*} [semiring R] [distrib_mul_action R M] [topological_space R] [has_continuous_smul R M] (r : R) (h : v βŠ₯α΅₯ w) : r β€’ v βŠ₯α΅₯ w := (smul_right r h.symm).symm lemma neg_left {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} (h : v βŠ₯α΅₯ w) : -v βŠ₯α΅₯ w := begin obtain ⟨u, hmu, hu₁, huβ‚‚βŸ© := h, refine ⟨u, hmu, Ξ» s hs, _, huβ‚‚βŸ©, rw [neg_apply v s, neg_eq_zero], exact hu₁ s hs end lemma neg_right {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} (h : v βŠ₯α΅₯ w) : v βŠ₯α΅₯ -w := h.symm.neg_left.symm @[simp] lemma neg_left_iff {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} : -v βŠ₯α΅₯ w ↔ v βŠ₯α΅₯ w := ⟨λ h, neg_neg v β–Έ h.neg_left, neg_left⟩ @[simp] lemma neg_right_iff {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N] {v : vector_measure Ξ± M} {w : vector_measure Ξ± N} : v βŠ₯α΅₯ -w ↔ v βŠ₯α΅₯ w := ⟨λ h, neg_neg w β–Έ h.neg_right, neg_right⟩ end mutually_singular section trim omit m /-- Restriction of a vector measure onto a sub-Οƒ-algebra. -/ @[simps] def trim {m n : measurable_space Ξ±} (v : vector_measure Ξ± M) (hle : m ≀ n) : @vector_measure Ξ± m M _ _ := { measure_of' := Ξ» i, if measurable_set[m] i then v i else 0, empty' := by rw [if_pos measurable_set.empty, v.empty], not_measurable' := Ξ» i hi, by rw if_neg hi, m_Union' := Ξ» f hf₁ hfβ‚‚, begin have hf₁' : βˆ€ k, measurable_set[n] (f k) := Ξ» k, hle _ (hf₁ k), convert v.m_Union hf₁' hfβ‚‚, { ext n, rw if_pos (hf₁ n) }, { rw if_pos (@measurable_set.Union _ _ m _ _ hf₁) } end } variables {n : measurable_space Ξ±} {v : vector_measure Ξ± M} lemma trim_eq_self : v.trim le_rfl = v := begin ext1 i hi, exact if_pos hi, end @[simp] lemma zero_trim (hle : m ≀ n) : (0 : vector_measure Ξ± M).trim hle = 0 := begin ext1 i hi, exact if_pos hi, end lemma trim_measurable_set_eq (hle : m ≀ n) {i : set Ξ±} (hi : measurable_set[m] i) : v.trim hle i = v i := if_pos hi lemma restrict_trim (hle : m ≀ n) {i : set Ξ±} (hi : measurable_set[m] i) : @vector_measure.restrict Ξ± m M _ _ (v.trim hle) i = (v.restrict i).trim hle := begin ext j hj, rw [restrict_apply, trim_measurable_set_eq hle hj, restrict_apply, trim_measurable_set_eq], all_goals { measurability } end end trim end end vector_measure namespace signed_measure open vector_measure open_locale measure_theory include m /-- The underlying function for `signed_measure.to_measure_of_zero_le`. -/ def to_measure_of_zero_le' (s : signed_measure Ξ±) (i : set Ξ±) (hi : 0 ≀[i] s) (j : set Ξ±) (hj : measurable_set j) : ℝβ‰₯0∞ := @coe ℝβ‰₯0 ℝβ‰₯0∞ _ ⟨s.restrict i j, le_trans (by simp) (hi j hj)⟩ /-- Given a signed measure `s` and a positive measurable set `i`, `to_measure_of_zero_le` provides the measure, mapping measurable sets `j` to `s (i ∩ j)`. -/ def to_measure_of_zero_le (s : signed_measure Ξ±) (i : set Ξ±) (hi₁ : measurable_set i) (hiβ‚‚ : 0 ≀[i] s) : measure Ξ± := measure.of_measurable (s.to_measure_of_zero_le' i hiβ‚‚) (by { simp_rw [to_measure_of_zero_le', s.restrict_apply hi₁ measurable_set.empty, set.empty_inter i, s.empty], refl }) begin intros f hf₁ hfβ‚‚, have h₁ : βˆ€ n, measurable_set (i ∩ f n) := Ξ» n, hi₁.inter (hf₁ n), have hβ‚‚ : pairwise (disjoint on Ξ» (n : β„•), i ∩ f n), { rintro n m hnm x ⟨⟨_, hxβ‚βŸ©, _, hxβ‚‚βŸ©, exact hfβ‚‚ n m hnm ⟨hx₁, hxβ‚‚βŸ© }, simp only [to_measure_of_zero_le', s.restrict_apply hi₁ (measurable_set.Union hf₁), set.inter_comm, set.inter_Union, s.of_disjoint_Union_nat h₁ hβ‚‚, ennreal.some_eq_coe, id.def], have h : βˆ€ n, 0 ≀ s (i ∩ f n) := Ξ» n, s.nonneg_of_zero_le_restrict (s.zero_le_restrict_subset hi₁ (inter_subset_left _ _) hiβ‚‚), rw [nnreal.coe_tsum_of_nonneg h, ennreal.coe_tsum], { refine tsum_congr (Ξ» n, _), simp_rw [s.restrict_apply hi₁ (hf₁ n), set.inter_comm] }, { exact (nnreal.summable_coe_of_nonneg h).2 (s.m_Union h₁ hβ‚‚).summable } end variables (s : signed_measure Ξ±) {i j : set Ξ±} lemma to_measure_of_zero_le_apply (hi : 0 ≀[i] s) (hi₁ : measurable_set i) (hj₁ : measurable_set j) : s.to_measure_of_zero_le i hi₁ hi j = @coe ℝβ‰₯0 ℝβ‰₯0∞ _ ⟨s (i ∩ j), nonneg_of_zero_le_restrict s (zero_le_restrict_subset s hi₁ (set.inter_subset_left _ _) hi)⟩ := by simp_rw [to_measure_of_zero_le, measure.of_measurable_apply _ hj₁, to_measure_of_zero_le', s.restrict_apply hi₁ hj₁, set.inter_comm] /-- Given a signed measure `s` and a negative measurable set `i`, `to_measure_of_le_zero` provides the measure, mapping measurable sets `j` to `-s (i ∩ j)`. -/ def to_measure_of_le_zero (s : signed_measure Ξ±) (i : set Ξ±) (hi₁ : measurable_set i) (hiβ‚‚ : s ≀[i] 0) : measure Ξ± := to_measure_of_zero_le (-s) i hi₁ $ (@neg_zero (vector_measure Ξ± ℝ) _) β–Έ neg_le_neg _ _ hi₁ hiβ‚‚ lemma to_measure_of_le_zero_apply (hi : s ≀[i] 0) (hi₁ : measurable_set i) (hj₁ : measurable_set j) : s.to_measure_of_le_zero i hi₁ hi j = @coe ℝβ‰₯0 ℝβ‰₯0∞ _ ⟨-s (i ∩ j), neg_apply s (i ∩ j) β–Έ nonneg_of_zero_le_restrict _ (zero_le_restrict_subset _ hi₁ (set.inter_subset_left _ _) ((@neg_zero (vector_measure Ξ± ℝ) _) β–Έ neg_le_neg _ _ hi₁ hi))⟩ := begin erw [to_measure_of_zero_le_apply], { simp }, { assumption }, end /-- `signed_measure.to_measure_of_zero_le` is a finite measure. -/ instance to_measure_of_zero_le_finite (hi : 0 ≀[i] s) (hi₁ : measurable_set i) : is_finite_measure (s.to_measure_of_zero_le i hi₁ hi) := { measure_univ_lt_top := begin rw [to_measure_of_zero_le_apply s hi hi₁ measurable_set.univ], exact ennreal.coe_lt_top, end } /-- `signed_measure.to_measure_of_le_zero` is a finite measure. -/ instance to_measure_of_le_zero_finite (hi : s ≀[i] 0) (hi₁ : measurable_set i) : is_finite_measure (s.to_measure_of_le_zero i hi₁ hi) := { measure_univ_lt_top := begin rw [to_measure_of_le_zero_apply s hi hi₁ measurable_set.univ], exact ennreal.coe_lt_top, end } lemma to_measure_of_zero_le_to_signed_measure (hs : 0 ≀[univ] s) : (s.to_measure_of_zero_le univ measurable_set.univ hs).to_signed_measure = s := begin ext i hi, simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_zero_le_apply _ _ _ hi], end lemma to_measure_of_le_zero_to_signed_measure (hs : s ≀[univ] 0) : (s.to_measure_of_le_zero univ measurable_set.univ hs).to_signed_measure = -s := begin ext i hi, simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_le_zero_apply _ _ _ hi], end end signed_measure namespace measure open vector_measure variables (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ] lemma zero_le_to_signed_measure : 0 ≀ ΞΌ.to_signed_measure := begin rw ← le_restrict_univ_iff_le, refine restrict_le_restrict_of_subset_le _ _ (Ξ» j hj₁ _, _), simp only [measure.to_signed_measure_apply_measurable hj₁, coe_zero, pi.zero_apply, ennreal.to_real_nonneg, vector_measure.coe_zero] end lemma to_signed_measure_to_measure_of_zero_le : ΞΌ.to_signed_measure.to_measure_of_zero_le univ measurable_set.univ ((le_restrict_univ_iff_le _ _).2 (zero_le_to_signed_measure ΞΌ)) = ΞΌ := begin refine measure.ext (Ξ» i hi, _), lift ΞΌ i to ℝβ‰₯0 using (measure_lt_top _ _).ne with m hm, simp [signed_measure.to_measure_of_zero_le_apply _ _ _ hi, measure.to_signed_measure_apply_measurable hi, ← hm], end end measure end measure_theory
9bba5a965b7d937a1fa6154e797b8a90325a3f19
2d787ac9a5ab28a7d164cd13cee8a3cdc1e4680a
/src/my_exercises/07_first_negations.lean
5f3d4f77acc2de30b299c0701946640f8ee5041c
[ "Apache-2.0" ]
permissive
paulzfm/lean3-tutorials
8c113ec2c9494d401c8a877f094db93e5a631fd6
f41baf1c62c251976ec8dd9ccae9e85ec4c4d336
refs/heads/master
1,679,267,488,532
1,614,782,461,000
1,614,782,461,000
344,157,026
0
0
null
null
null
null
UTF-8
Lean
false
false
8,402
lean
import tuto_lib import data.int.parity /- Negations, proof by contradiction and contraposition. This file introduces the logical rules and tactics related to negation: exfalso, by_contradiction, contrapose, by_cases and push_neg. There is a special statement denoted by `false` which, by definition, has no proof. So `false` implies everything. Indeed `false β†’ P` means any proof of `false` could be turned into a proof of P. This fact is known by its latin name "ex falso quod libet" (from false follows whatever you want). Hence Lean's tactic to invoke this is called `exfalso`. -/ example : false β†’ 0 = 1 := begin intro h, exfalso, exact h, end /- The preceding example suggests that this definition of `false` isn't very useful. But actually it allows us to define the negation of a statement P as "P implies false" that we can read as "if P were true, we would get a contradiction". Lean denotes this by `Β¬ P`. One can prove that (Β¬ P) ↔ (P ↔ false). But in practice we directly use the definition of `Β¬ P`. -/ example {x : ℝ} : Β¬ x < x := begin intro hyp, rw lt_iff_le_and_ne at hyp, cases hyp with hyp_inf hyp_non, clear hyp_inf, -- we won't use that one, so let's discard it change x = x β†’ false at hyp_non, -- Lean doesn't need this psychological line apply hyp_non, refl, end open int -- 0045 example (n : β„€) (h_even : even n) (h_not_even : Β¬ even n) : 0 = 1 := begin exfalso, apply h_not_even, exact h_even, end -- 0046 example (P Q : Prop) (h₁ : P ∨ Q) (hβ‚‚ : Β¬ (P ∧ Q)) : Β¬ P ↔ Q := begin split, { intros notP, cases h₁ with hP hQ, { exfalso, apply notP, exact hP, }, { exact hQ, } }, { intros hQ hP, apply hβ‚‚, exact ⟨hP, hQ⟩, } end /- The definition of negation easily implies that, for every statement P, P β†’ Β¬ Β¬ P The excluded middle axiom, which asserts P ∨ Β¬ P allows us to prove the converse implication. Together those two implications form the principle of double negation elimination. not_not {P : Prop} : (Β¬ Β¬ P) ↔ P The implication `Β¬ Β¬ P β†’ P` is the basis for proofs by contradiction: in order to prove P, it suffices to prove ¬¬ P, ie `Β¬ P β†’ false`. Of course there is no need to keep explaining all this. The tactic `by_contradiction Hyp` will transform any goal P into `false` and add Hyp : Β¬ P to the local context. Let's return to a proof from the 5th file: uniqueness of limits for a sequence. This cannot be proved without using some version of the excluded middle axiom. We used it secretely in eq_of_abs_sub_le_all (x y : ℝ) : (βˆ€ Ξ΅ > 0, |x - y| ≀ Ξ΅) β†’ x = y (we'll prove a variation on this lemma below). In the proof below, we also take the opportunity to introduce the `let` tactic which creates a local definition. If needed, it can be unfolded using `dsimp` which takes a list of definitions to unfold. For instance after using `let Nβ‚€ := max N N'`, you could write `dsimp [Nβ‚€] at h` to replace `Nβ‚€` by its definition in some local assumption `h`. -/ example (u : β„• β†’ ℝ) (l l' : ℝ) : seq_limit u l β†’ seq_limit u l' β†’ l = l' := begin intros hl hl', by_contradiction H, change l β‰  l' at H, -- Lean does not need this line have ineg : |l-l'| > 0, exact abs_pos.mpr (sub_ne_zero_of_ne H), -- details about that line are not important cases hl ( |l-l'|/4 ) (by linarith) with N hN, cases hl' ( |l-l'|/4 ) (by linarith) with N' hN', let Nβ‚€ := max N N', specialize hN Nβ‚€ (le_max_left _ _), specialize hN' Nβ‚€ (le_max_right _ _), have clef : |l-l'| < |l-l'|, calc |l - l'| = |(l-u Nβ‚€) + (u Nβ‚€ -l')| : by ring ... ≀ |l - u Nβ‚€| + |u Nβ‚€ - l'| : by apply abs_add ... = |u Nβ‚€ - l| + |u Nβ‚€ - l'| : by rw abs_sub ... < |l-l'| : by linarith, linarith, -- linarith can also find simple numerical contradictions end /- Another incarnation of the excluded middle axiom is the principle of contraposition: in order to prove P β‡’ Q, it suffices to prove non Q β‡’ non P. -/ -- Using a proof by contradiction, let's prove the contraposition principle -- 0047 example (P Q : Prop) (h : Β¬ Q β†’ Β¬ P) : P β†’ Q := begin intros hP, by_contradiction hnot, specialize h hnot, apply h, exact hP, end /- Again Lean doesn't need this principle explained to it. We can use the `contrapose` tactic. -/ example (P Q : Prop) (h : Β¬ Q β†’ Β¬ P) : P β†’ Q := begin contrapose, exact h, end /- In the next exercise, we'll use odd n : βˆƒ k, n = 2*k + 1 int.odd_iff_not_even {n : β„€} : odd n ↔ Β¬ even n -/ -- 0048 example (n : β„€) : even (n^2) ↔ even n := begin split, { contrapose, intro h, rw ← int.odd_iff_not_even at *, cases h with k hk, use (2*k^2+2*k), rw hk, ring, }, { rintros ⟨k, rfl⟩, use (2*k^2), ring, } end /- As a last step on our law of the excluded middle tour, let's notice that, especially in pure logic exercises, it can sometimes be useful to use the excluded middle axiom in its original form: classical.em : βˆ€ P, P ∨ Β¬ P Instead of applying this lemma and then using the `cases` tactic, we have the shortcut by_cases h : P, combining both steps to create two proof branches: one assuming h : P, and the other assuming h : Β¬ P For instance, let's prove a reformulation of this implication relation, which is sometimes used as a definition in other logical foundations, especially those based on truth tables (hence very strongly using excluded middle from the very beginning). -/ variables (P Q : Prop) example : (P β†’ Q) ↔ (Β¬ P ∨ Q) := begin split, { intro h, by_cases hP : P, { right, exact h hP }, { left, exact hP } }, { intros h hP, cases h with hnP hQ, { exfalso, exact hnP hP }, { exact hQ } }, end -- 0049 example : Β¬ (P ∧ Q) ↔ Β¬ P ∨ Β¬ Q := begin split, { intro h, by_cases hP : P, { right, intros hQ, apply h, exact ⟨hP, hQ⟩, }, { left, exact hP, } }, { rintros h ⟨hP, hQ⟩, cases h with h h, { apply h, exact hP, }, { apply h, exact hQ, } } end /- It is crucial to understand negation of quantifiers. Let's do it by hand for a little while. In the first exercise, only the definition of negation is needed. -/ -- 0050 example (n : β„€) : Β¬ (βˆƒ k, n = 2*k) ↔ βˆ€ k, n β‰  2*k := begin split, { intros h k hn, apply h, use k, exact hn, }, { rintros h ⟨k, rfl⟩, apply h k, refl, } end /- Contrary to negation of the existential quantifier, negation of the universal quantifier requires excluded middle for the first implication. In order to prove this, we can use either * a double proof by contradiction * a contraposition, not_not : (Β¬ Β¬ P) ↔ P) and a proof by contradiction. -/ def even_fun (f : ℝ β†’ ℝ) := βˆ€ x, f (-x) = f x -- 0051 example (f : ℝ β†’ ℝ) : Β¬ even_fun f ↔ βˆƒ x, f (-x) β‰  f x := begin split, { contrapose, intro h, rw not_not, intro x, by_contradiction H, apply h, use x, }, { rintro ⟨x, hx⟩ h, apply hx, apply h, } end /- Of course we can't keep repeating the above proofs, especially the second one. So we use the `push_neg` tactic. -/ example : Β¬ even_fun (Ξ» x, 2*x) := begin unfold even_fun, -- Here unfolding is important because push_neg won't do it. push_neg, use 42, linarith, end -- 0052 example (f : ℝ β†’ ℝ) : Β¬ even_fun f ↔ βˆƒ x, f (-x) β‰  f x := begin unfold even_fun, push_neg, end def bounded_above (f : ℝ β†’ ℝ) := βˆƒ M, βˆ€ x, f x ≀ M example : Β¬ bounded_above (Ξ» x, x) := begin unfold bounded_above, push_neg, intro M, use M + 1, linarith, end -- Let's contrapose -- 0053 example (x : ℝ) : (βˆ€ Ξ΅ > 0, x ≀ Ξ΅) β†’ x ≀ 0 := begin contrapose, push_neg, intro h, use x/2, split; linarith, end /- The "contrapose, push_neg" combo is so common that we can abreviate it to `contrapose!` Let's use this trick, together with: eq_or_lt_of_le : a ≀ b β†’ a = b ∨ a < b -/ -- 0054 example (f : ℝ β†’ ℝ) : (βˆ€ x y, x < y β†’ f x < f y) ↔ (βˆ€ x y, (x ≀ y ↔ f x ≀ f y)) := begin split, { intros h x y, split, { intros hle, cases eq_or_lt_of_le hle with heq hlt, { rw heq, }, { linarith [h _ _ hlt], } }, contrapose!, apply h, }, { intros h x y, contrapose!, rw ← h, intro hle, exact hle, } end
94d46ac23b3f2d5077a5924409cfa004f7729d21
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/topology/uniform_space/basic.lean
54bb214d53294812a87176e1ad5153348f025e21
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
71,039
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 -/ import order.filter.lift import topology.separation /-! # Uniform spaces Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * uniform continuity (in this file) * completeness (in `cauchy.lean`) * extension of uniform continuous functions to complete spaces (in `uniform_embedding.lean`) * totally bounded sets (in `cauchy.lean`) * totally bounded complete sets are compact (in `cauchy.lean`) A uniform structure on a type `X` is a filter `𝓀 X` on `X Γ— X` satisfying some conditions which makes it reasonable to say that `βˆ€αΆ  (p : X Γ— X) in 𝓀 X, ...` means "for all p.1 and p.2 in X close enough, ...". Elements of this filter are called entourages of `X`. The two main examples are: * If `X` is a metric space, `V ∈ 𝓀 X ↔ βˆƒ Ξ΅ > 0, { p | dist p.1 p.2 < Ξ΅ } βŠ† V` * If `G` is an additive topological group, `V ∈ 𝓀 G ↔ βˆƒ U ∈ 𝓝 (0 : G), {p | p.2 - p.1 ∈ U} βŠ† V` Those examples are generalizations in two different directions of the elementary example where `X = ℝ` and `V ∈ 𝓀 ℝ ↔ βˆƒ Ξ΅ > 0, { p | |p.2 - p.1| < Ξ΅ } βŠ† V` which features both the topological group structure on `ℝ` and its metric space structure. Each uniform structure on `X` induces a topology on `X` characterized by > `nhds_eq_comap_uniformity : βˆ€ {x : X}, 𝓝 x = comap (prod.mk x) (𝓀 X)` where `prod.mk x : X β†’ X Γ— X := (Ξ» y, (x, y))` is the partial evaluation of the product constructor. The dictionary with metric spaces includes: * an upper bound for `dist x y` translates into `(x, y) ∈ V` for some `V ∈ 𝓀 X` * a ball `ball x r` roughly corresponds to `uniform_space.ball x V := {y | (x, y) ∈ V}` for some `V ∈ 𝓀 X`, but the later is more general (it includes in particular both open and closed balls for suitable `V`). In particular we have: `is_open_iff_ball_subset {s : set X} : is_open s ↔ βˆ€ x ∈ s, βˆƒ V ∈ 𝓀 X, ball x V βŠ† s` The triangle inequality is abstracted to a statement involving the composition of relations in `X`. First note that the triangle inequality in a metric space is equivalent to `βˆ€ (x y z : X) (r r' : ℝ), dist x y ≀ r β†’ dist y z ≀ r' β†’ dist x z ≀ r + r'`. Then, for any `V` and `W` with type `set (X Γ— X)`, the composition `V β—‹ W : set (X Γ— X)` is defined as `{ p : X Γ— X | βˆƒ z, (p.1, z) ∈ V ∧ (z, p.2) ∈ W }`. In the metric space case, if `V = { p | dist p.1 p.2 ≀ r }` and `W = { p | dist p.1 p.2 ≀ r' }` then the triangle inequality, as reformulated above, says `V β—‹ W` is contained in `{p | dist p.1 p.2 ≀ r + r'}` which is the entourage associated to the radius `r + r'`. In general we have `mem_ball_comp (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V β—‹ W)`. Note that this discussion does not depend on any axiom imposed on the uniformity filter, it is simply captured by the definition of composition. The uniform space axioms ask the filter `𝓀 X` to satisfy the following: * every `V ∈ 𝓀 X` contains the diagonal `id_rel = { p | p.1 = p.2 }`. This abstracts the fact that `dist x x ≀ r` for every non-negative radius `r` in the metric space case and also that `x - x` belongs to every neighborhood of zero in the topological group case. * `V ∈ 𝓀 X β†’ prod.swap '' V ∈ 𝓀 X`. This is tightly related the fact that `dist x y = dist y x` in a metric space, and to continuity of negation in the topological group case. * `βˆ€ V ∈ 𝓀 X, βˆƒ W ∈ 𝓀 X, W β—‹ W βŠ† V`. In the metric space case, it corresponds to cutting the radius of a ball in half and applying the triangle inequality. In the topological group case, it comes from continuity of addition at `(0, 0)`. These three axioms are stated more abstractly in the definition below, in terms of operations on filters, without directly manipulating entourages. ##Β Main definitions * `uniform_space X` is a uniform space structure on a type `X` * `uniform_continuous f` is a predicate saying a function `f : Ξ± β†’ Ξ²` between uniform spaces is uniformly continuous : `βˆ€ r ∈ 𝓀 Ξ², βˆ€αΆ  (x : Ξ± Γ— Ξ±) in 𝓀 Ξ±, (f x.1, f x.2) ∈ r` In this file we also define a complete lattice structure on the type `uniform_space X` of uniform structures on `X`, as well as the pullback (`uniform_space.comap`) of uniform structures coming from the pullback of filters. Like distance functions, uniform structures cannot be pushed forward in general. ## Notations Localized in `uniformity`, we have the notation `𝓀 X` for the uniformity on a uniform space `X`, and `β—‹` for composition of relations, seen as terms with type `set (X Γ— X)`. ## Implementation notes There is already a theory of relations in `data/rel.lean` where the main definition is `def rel (Ξ± Ξ² : Type*) := Ξ± β†’ Ξ² β†’ Prop`. The relations used in the current file involve only one type, but this is not the reason why we don't reuse `data/rel.lean`. We use `set (Ξ± Γ— Ξ±)` instead of `rel Ξ± Ξ±` because we really need sets to use the filter library, and elements of filters on `Ξ± Γ— Ξ±` have type `set (Ξ± Γ— Ξ±)`. The structure `uniform_space X` bundles a uniform structure on `X`, a topology on `X` and an assumption saying those are compatible. This may not seem mathematically reasonable at first, but is in fact an instance of the forgetful inheritance pattern. See Note [forgetful inheritance] below. ## References The formalization uses the books: * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] But it makes a more systematic use of the filter library. -/ open set filter classical open_locale classical topological_space filter set_option eqn_compiler.zeta true universes u /-! ### Relations, seen as `set (Ξ± Γ— Ξ±)` -/ variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} {ΞΉ : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {Ξ± : Type*} := {p : Ξ± Γ— Ξ± | p.1 = p.2} @[simp] theorem mem_id_rel {a b : Ξ±} : (a, b) ∈ @id_rel Ξ± ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (Ξ± Γ— Ξ±)} : id_rel βŠ† s ↔ βˆ€ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (Ξ» a, by simp) /-- The composition of relations -/ def comp_rel {Ξ± : Type u} (r₁ rβ‚‚ : set (Ξ±Γ—Ξ±)) := {p : Ξ± Γ— Ξ± | βˆƒz:Ξ±, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ rβ‚‚} localized "infix ` β—‹ `:55 := comp_rel" in uniformity @[simp] theorem mem_comp_rel {r₁ rβ‚‚ : set (Ξ±Γ—Ξ±)} {x y : Ξ±} : (x, y) ∈ r₁ β—‹ rβ‚‚ ↔ βˆƒ z, (x, z) ∈ r₁ ∧ (z, y) ∈ rβ‚‚ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel Ξ± := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder Ξ²] {f g : Ξ² β†’ set (Ξ±Γ—Ξ±)} (hf : monotone f) (hg : monotone g) : monotone (Ξ»x, (f x) β—‹ (g x)) := assume a b h p ⟨z, h₁, hβ‚‚βŸ©, ⟨z, hf h h₁, hg h hβ‚‚βŸ© @[mono] lemma comp_rel_mono {f g h k: set (Ξ±Γ—Ξ±)} (h₁ : f βŠ† h) (hβ‚‚ : g βŠ† k) : f β—‹ g βŠ† h β—‹ k := Ξ» ⟨x, y⟩ ⟨z, h, h'⟩, ⟨z, h₁ h, hβ‚‚ h'⟩ lemma prod_mk_mem_comp_rel {a b c : Ξ±} {s t : set (Ξ±Γ—Ξ±)} (h₁ : (a, c) ∈ s) (hβ‚‚ : (c, b) ∈ t) : (a, b) ∈ s β—‹ t := ⟨c, h₁, hβ‚‚βŸ© @[simp] lemma id_comp_rel {r : set (Ξ±Γ—Ξ±)} : id_rel β—‹ r = r := set.ext $ assume ⟨a, b⟩, by simp lemma comp_rel_assoc {r s t : set (Ξ±Γ—Ξ±)} : (r β—‹ s) β—‹ t = r β—‹ (s β—‹ t) := by ext p; cases p; simp only [mem_comp_rel]; tauto lemma subset_comp_self {Ξ± : Type*} {s : set (Ξ± Γ— Ξ±)} (h : id_rel βŠ† s) : s βŠ† s β—‹ s := Ξ» ⟨x, y⟩ xy_in, ⟨x, h (by rw mem_id_rel), xy_in⟩ /-- The relation is invariant under swapping factors. -/ def symmetric_rel (V : set (Ξ± Γ— Ξ±)) : Prop := prod.swap ⁻¹' V = V /-- The maximal symmetric relation contained in a given relation. -/ def symmetrize_rel (V : set (Ξ± Γ— Ξ±)) : set (Ξ± Γ— Ξ±) := V ∩ prod.swap ⁻¹' V lemma symmetric_symmetrize_rel (V : set (Ξ± Γ— Ξ±)) : symmetric_rel (symmetrize_rel V) := by simp [symmetric_rel, symmetrize_rel, preimage_inter, inter_comm, ← preimage_comp] lemma symmetrize_rel_subset_self (V : set (Ξ± Γ— Ξ±)) : symmetrize_rel V βŠ† V := sep_subset _ _ @[mono] lemma symmetrize_mono {V W: set (Ξ± Γ— Ξ±)} (h : V βŠ† W) : symmetrize_rel V βŠ† symmetrize_rel W := inter_subset_inter h $ preimage_mono h lemma symmetric_rel_inter {U V : set (Ξ± Γ— Ξ±)} (hU : symmetric_rel U) (hV : symmetric_rel V) : symmetric_rel (U ∩ V) := begin unfold symmetric_rel at *, rw [preimage_inter, hU, hV], end /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (Ξ± : Type u) := (uniformity : filter (Ξ± Γ— Ξ±)) (refl : π“Ÿ id_rel ≀ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (Ξ»s, s β—‹ s) ≀ uniformity) /-- An alternative constructor for `uniform_space.core`. This version unfolds various `filter`-related definitions. -/ def uniform_space.core.mk' {Ξ± : Type u} (U : filter (Ξ± Γ— Ξ±)) (refl : βˆ€ (r ∈ U) x, (x, x) ∈ r) (symm : βˆ€ r ∈ U, prod.swap ⁻¹' r ∈ U) (comp : βˆ€ r ∈ U, βˆƒ t ∈ U, t β—‹ t βŠ† r) : uniform_space.core Ξ± := ⟨U, Ξ» r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {Ξ± : Type u} (u : uniform_space.core Ξ±) : topological_space Ξ± := { is_open := Ξ»s, βˆ€x∈s, { p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s } ∈ u.uniformity, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : βˆ€{u₁ uβ‚‚ : uniform_space.core Ξ±}, u₁.uniformity = uβ‚‚.uniformity β†’ u₁ = uβ‚‚ | ⟨u₁, _, _, _⟩ ⟨uβ‚‚, _, _, _⟩ h := by { congr, exact h } /-- Suppose that one can put two mathematical structures on a type, a rich one `R` and a poor one `P`, and that one can deduce the poor structure from the rich structure through a map `F` (called a forgetful functor) (think `R = metric_space` and `P = topological_space`). A possible implementation would be to have a type class `rich` containing a field `R`, a type class `poor` containing a field `P`, and an instance from `rich` to `poor`. However, this creates diamond problems, and a better approach is to let `rich` extend `poor` and have a field saying that `F R = P`. To illustrate this, consider the pair `metric_space` / `topological_space`. Consider the topology on a product of two metric spaces. With the first approach, it could be obtained by going first from each metric space to its topology, and then taking the product topology. But it could also be obtained by considering the product metric space (with its sup distance) and then the topology coming from this distance. These would be the same topology, but not definitionally, which means that from the point of view of Lean's kernel, there would be two different `topological_space` instances on the product. This is not compatible with the way instances are designed and used: there should be at most one instance of a kind on each type. This approach has created an instance diamond that does not commute definitionally. The second approach solves this issue. Now, a metric space contains both a distance, a topology, and a proof that the topology coincides with the one coming from the distance. When one defines the product of two metric spaces, one uses the sup distance and the product topology, and one has to give the proof that the sup distance induces the product topology. Following both sides of the instance diamond then gives rise (definitionally) to the product topology on the product space. Another approach would be to have the rich type class take the poor type class as an instance parameter. It would solve the diamond problem, but it would lead to a blow up of the number of type classes one would need to declare to work with complicated classes, say a real inner product space, and would create exponential complexity when working with products of such complicated spaces, that are avoided by bundling things carefully as above. Note that this description of this specific case of the product of metric spaces is oversimplified compared to mathlib, as there is an intermediate typeclass between `metric_space` and `topological_space` called `uniform_space`. The above scheme is used at both levels, embedding a topology in the uniform space structure, and a uniform structure in the metric space structure. Note also that, when `P` is a proposition, there is no such issue as any two proofs of `P` are definitionally equivalent in Lean. To avoid boilerplate, there are some designs that can automatically fill the poor fields when creating a rich structure if one doesn't want to do something special about them. For instance, in the definition of metric spaces, default tactics fill the uniform space fields if they are not given explicitly. One can also have a helper function creating the rich structure from a structure with less fields, where the helper function fills the remaining fields. See for instance `uniform_space.of_core` or `real_inner_product.of_core`. For more details on this question, called the forgetful inheritance pattern, see [Competing inheritance paths in dependent type theory: a case study in functional analysis](https://hal.inria.fr/hal-02463336). -/ library_note "forgetful inheritance" /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `Ξ± Γ— Ξ±` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (Ξ± : Type u) extends topological_space Ξ±, uniform_space.core Ξ± := (is_open_uniformity : βˆ€s, is_open s ↔ (βˆ€x∈s, { p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s } ∈ uniformity)) /-- Alternative constructor for `uniform_space Ξ±` when a topology is already given. -/ @[pattern] def uniform_space.mk' {Ξ±} (t : topological_space Ξ±) (c : uniform_space.core Ξ±) (is_open_uniformity : βˆ€s:set Ξ±, t.is_open s ↔ (βˆ€x∈s, { p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s } ∈ c.uniformity)) : uniform_space Ξ± := ⟨c, is_open_uniformity⟩ /-- Construct a `uniform_space` from a `uniform_space.core`. -/ def uniform_space.of_core {Ξ± : Type u} (u : uniform_space.core Ξ±) : uniform_space Ξ± := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.rfl } /-- Construct a `uniform_space` from a `u : uniform_space.core` and a `topological_space` structure that is equal to `u.to_topological_space`. -/ def uniform_space.of_core_eq {Ξ± : Type u} (u : uniform_space.core Ξ±) (t : topological_space Ξ±) (h : t = u.to_topological_space) : uniform_space Ξ± := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm β–Έ iff.rfl } lemma uniform_space.to_core_to_topological_space (u : uniform_space Ξ±) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] @[ext] lemma uniform_space_eq : βˆ€{u₁ uβ‚‚ : uniform_space Ξ±}, u₁.uniformity = uβ‚‚.uniformity β†’ u₁ = uβ‚‚ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' tβ‚‚ uβ‚‚ oβ‚‚) h := have u₁ = uβ‚‚, from uniform_space.core_eq h, have t₁ = tβ‚‚, from topological_space_eq $ funext $ assume s, by rw [o₁, oβ‚‚]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space Ξ±) (t : topological_space Ξ±) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space Ξ±] /-- The uniformity is a filter on Ξ± Γ— Ξ± (inferred from an ambient uniform space structure on Ξ±). -/ def uniformity (Ξ± : Type u) [uniform_space Ξ±] : filter (Ξ± Γ— Ξ±) := (@uniform_space.to_core Ξ± _).uniformity localized "notation `𝓀` := uniformity" in uniformity lemma is_open_uniformity {s : set Ξ±} : is_open s ↔ (βˆ€x∈s, { p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s } ∈ 𝓀 Ξ±) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : π“Ÿ id_rel ≀ 𝓀 Ξ± := (@uniform_space.to_core Ξ± _).refl lemma refl_mem_uniformity {x : Ξ±} {s : set (Ξ± Γ— Ξ±)} (h : s ∈ 𝓀 Ξ±) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap Ξ± Ξ±) (𝓀 _) ≀ (𝓀 _) := (@uniform_space.to_core Ξ± _).symm lemma comp_le_uniformity : (𝓀 Ξ±).lift' (Ξ»s:set (Ξ±Γ—Ξ±), s β—‹ s) ≀ 𝓀 Ξ± := (@uniform_space.to_core Ξ± _).comp lemma tendsto_swap_uniformity : tendsto (@prod.swap Ξ± Ξ±) (𝓀 Ξ±) (𝓀 Ξ±) := symm_le_uniformity lemma comp_mem_uniformity_sets {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : βˆƒ t ∈ 𝓀 Ξ±, t β—‹ t βŠ† s := have s ∈ (𝓀 Ξ±).lift' (Ξ»t:set (Ξ±Γ—Ξ±), t β—‹ t), from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this /-- Relation `Ξ» f g, tendsto (Ξ» x, (f x, g x)) l (𝓀 Ξ±)` is transitive. -/ lemma filter.tendsto.uniformity_trans {l : filter Ξ²} {f₁ fβ‚‚ f₃ : Ξ² β†’ Ξ±} (h₁₂ : tendsto (Ξ» x, (f₁ x, fβ‚‚ x)) l (𝓀 Ξ±)) (h₂₃ : tendsto (Ξ» x, (fβ‚‚ x, f₃ x)) l (𝓀 Ξ±)) : tendsto (Ξ» x, (f₁ x, f₃ x)) l (𝓀 Ξ±) := begin refine le_trans (le_lift' $ Ξ» s hs, mem_map.2 _) comp_le_uniformity, filter_upwards [h₁₂ hs, h₂₃ hs], exact Ξ» x hx₁₂ hx₂₃, ⟨_, hx₁₂, hxβ‚‚β‚ƒβŸ© end /-- Relation `Ξ» f g, tendsto (Ξ» x, (f x, g x)) l (𝓀 Ξ±)` is symmetric -/ lemma filter.tendsto.uniformity_symm {l : filter Ξ²} {f : Ξ² β†’ Ξ± Γ— Ξ±} (h : tendsto f l (𝓀 Ξ±)) : tendsto (Ξ» x, ((f x).2, (f x).1)) l (𝓀 Ξ±) := tendsto_swap_uniformity.comp h /-- Relation `Ξ» f g, tendsto (Ξ» x, (f x, g x)) l (𝓀 Ξ±)` is reflexive. -/ lemma tendsto_diag_uniformity (f : Ξ² β†’ Ξ±) (l : filter Ξ²) : tendsto (Ξ» x, (f x, f x)) l (𝓀 Ξ±) := assume s hs, mem_map.2 $ univ_mem_sets' $ Ξ» x, refl_mem_uniformity hs lemma tendsto_const_uniformity {a : Ξ±} {f : filter Ξ²} : tendsto (Ξ» _, (a, a)) f (𝓀 Ξ±) := tendsto_diag_uniformity (Ξ» _, a) f lemma symm_of_uniformity {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : βˆƒ t ∈ 𝓀 Ξ±, (βˆ€a b, (a, b) ∈ t β†’ (b, a) ∈ t) ∧ t βŠ† s := have preimage prod.swap s ∈ 𝓀 Ξ±, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, Ξ» a b ⟨h₁, hβ‚‚βŸ©, ⟨hβ‚‚, hβ‚βŸ©, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : βˆƒ t ∈ 𝓀 Ξ±, (βˆ€{a b}, (a, b) ∈ t β†’ (b, a) ∈ t) ∧ t β—‹ t βŠ† s := let ⟨t, ht₁, htβ‚‚βŸ© := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'β‚‚βŸ© := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'β‚‚) htβ‚‚βŸ© lemma uniformity_le_symm : 𝓀 Ξ± ≀ (@prod.swap Ξ± Ξ±) <$> 𝓀 Ξ± := by rw [map_swap_eq_comap_swap]; from map_le_iff_le_comap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : 𝓀 Ξ± = (@prod.swap Ξ± Ξ±) <$> 𝓀 Ξ± := le_antisymm uniformity_le_symm symm_le_uniformity lemma symmetrize_mem_uniformity {V : set (Ξ± Γ— Ξ±)} (h : V ∈ 𝓀 Ξ±) : symmetrize_rel V ∈ 𝓀 Ξ± := begin apply (𝓀 Ξ±).inter_sets h, rw [← image_swap_eq_preimage_swap, uniformity_eq_symm], exact image_mem_map h, end theorem uniformity_lift_le_swap {g : set (Ξ±Γ—Ξ±) β†’ filter Ξ²} {f : filter Ξ²} (hg : monotone g) (h : (𝓀 Ξ±).lift (Ξ»s, g (preimage prod.swap s)) ≀ f) : (𝓀 Ξ±).lift g ≀ f := calc (𝓀 Ξ±).lift g ≀ (filter.map (@prod.swap Ξ± Ξ±) $ 𝓀 Ξ±).lift g : lift_mono uniformity_le_symm (le_refl _) ... ≀ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (Ξ±Γ—Ξ±) β†’ filter Ξ²} (h : monotone f) : (𝓀 Ξ±).lift (Ξ»s, f (s β—‹ s)) ≀ (𝓀 Ξ±).lift f := calc (𝓀 Ξ±).lift (Ξ»s, f (s β—‹ s)) = ((𝓀 Ξ±).lift' (Ξ»s:set (Ξ±Γ—Ξ±), s β—‹ s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≀ (𝓀 Ξ±).lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : (𝓀 Ξ±).lift' (Ξ»s:set (Ξ±Γ—Ξ±), s β—‹ (s β—‹ s)) ≀ (𝓀 Ξ±) := calc (𝓀 Ξ±).lift' (Ξ»d, d β—‹ (d β—‹ d)) = (𝓀 Ξ±).lift (Ξ»s, (𝓀 Ξ±).lift' (Ξ»t:set(Ξ±Γ—Ξ±), s β—‹ (t β—‹ t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≀ (𝓀 Ξ±).lift (Ξ»s, (𝓀 Ξ±).lift' (Ξ»t:set(Ξ±Γ—Ξ±), s β—‹ t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp Ξ± _ _ (π“Ÿ ∘ (β—‹) s) $ monotone_principal.comp (monotone_comp_rel monotone_const monotone_id) ... = (𝓀 Ξ±).lift' (Ξ»s:set(Ξ±Γ—Ξ±), s β—‹ s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≀ (𝓀 Ξ±) : comp_le_uniformity lemma comp_symm_mem_uniformity_sets {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : βˆƒ t ∈ 𝓀 Ξ±, symmetric_rel t ∧ t β—‹ t βŠ† s := begin obtain ⟨w, w_in, w_sub⟩ : βˆƒ w ∈ 𝓀 Ξ±, w β—‹ w βŠ† s := comp_mem_uniformity_sets hs, use [symmetrize_rel w, symmetrize_mem_uniformity w_in, symmetric_symmetrize_rel w], have : symmetrize_rel w βŠ† w := symmetrize_rel_subset_self w, calc symmetrize_rel w β—‹ symmetrize_rel w βŠ† w β—‹ w : by mono ... βŠ† s : w_sub, end lemma subset_comp_self_of_mem_uniformity {s : set (Ξ± Γ— Ξ±)} (h : s ∈ 𝓀 Ξ±) : s βŠ† s β—‹ s := subset_comp_self (refl_le_uniformity h) lemma comp_comp_symm_mem_uniformity_sets {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : βˆƒ t ∈ 𝓀 Ξ±, symmetric_rel t ∧ t β—‹ t β—‹ t βŠ† s := begin rcases comp_symm_mem_uniformity_sets hs with ⟨w, w_in, w_symm, w_sub⟩, rcases comp_symm_mem_uniformity_sets w_in with ⟨t, t_in, t_symm, t_sub⟩, use [t, t_in, t_symm], have : t βŠ† t β—‹ t := subset_comp_self_of_mem_uniformity t_in, calc t β—‹ t β—‹ t βŠ† w β—‹ t : by mono ... βŠ† w β—‹ (t β—‹ t) : by mono ... βŠ† w β—‹ w : by mono ... βŠ† s : w_sub, end /-! ###Β Balls in uniform spaces -/ /-- The ball around `(x : Ξ²)` with respect to `(V : set (Ξ² Γ— Ξ²))`. Intended to be used for `V ∈ 𝓀 Ξ²`, but this is not needed for the definition. Recovers the notions of metric space ball when `V = {p | dist p.1 p.2 < r }`. -/ def uniform_space.ball (x : Ξ²) (V : set (Ξ² Γ— Ξ²)) : set Ξ² := (prod.mk x) ⁻¹' V open uniform_space (ball) lemma uniform_space.mem_ball_self (x : Ξ±) {V : set (Ξ± Γ— Ξ±)} (hV : V ∈ 𝓀 Ξ±) : x ∈ ball x V := refl_mem_uniformity hV /-- The triangle inequality for `uniform_space.ball` -/ lemma mem_ball_comp {V W : set (Ξ² Γ— Ξ²)} {x y z} (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V β—‹ W) := prod_mk_mem_comp_rel h h' lemma ball_subset_of_comp_subset {V W : set (Ξ² Γ— Ξ²)} {x y} (h : x ∈ ball y W) (h' : W β—‹ W βŠ† V) : ball x W βŠ† ball y V := Ξ» z z_in, h' (mem_ball_comp h z_in) lemma ball_mono {V W : set (Ξ² Γ— Ξ²)} (h : V βŠ† W) (x : Ξ²) : ball x V βŠ† ball x W := by tauto lemma mem_ball_symmetry {V : set (Ξ² Γ— Ξ²)} (hV : symmetric_rel V) {x y} : x ∈ ball y V ↔ y ∈ ball x V := show (x, y) ∈ prod.swap ⁻¹' V ↔ (x, y) ∈ V, by { unfold symmetric_rel at hV, rw hV } lemma ball_eq_of_symmetry {V : set (Ξ² Γ— Ξ²)} (hV : symmetric_rel V) {x} : ball x V = {y | (y, x) ∈ V} := by { ext y, rw mem_ball_symmetry hV, exact iff.rfl } lemma mem_comp_of_mem_ball {V W : set (Ξ² Γ— Ξ²)} {x y z : Ξ²} (hV : symmetric_rel V) (hx : x ∈ ball z V) (hy : y ∈ ball z W) : (x, y) ∈ V β—‹ W := begin rw mem_ball_symmetry hV at hx, exact ⟨z, hx, hy⟩ end lemma uniform_space.is_open_ball (x : Ξ±) {V : set (Ξ± Γ— Ξ±)} (hV : is_open V) : is_open (ball x V) := hV.preimage $ continuous_const.prod_mk continuous_id lemma mem_comp_comp {V W M : set (Ξ² Γ— Ξ²)} (hW' : symmetric_rel W) {p : Ξ² Γ— Ξ²} : p ∈ V β—‹ M β—‹ W ↔ ((ball p.1 V).prod (ball p.2 W) ∩ M).nonempty := begin cases p with x y, split, { rintros ⟨z, ⟨w, hpw, hwz⟩, hzy⟩, exact ⟨(w, z), ⟨hpw, by rwa mem_ball_symmetry hW'⟩, hwz⟩, }, { rintro ⟨⟨w, z⟩, ⟨w_in, z_in⟩, hwz⟩, rwa mem_ball_symmetry hW' at z_in, use [z, w] ; tauto }, end /-! ### Neighborhoods in uniform spaces -/ lemma mem_nhds_uniformity_iff_right {x : Ξ±} {s : set Ξ±} : s ∈ 𝓝 x ↔ {p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s} ∈ 𝓀 Ξ± := ⟨ begin simp only [mem_nhds_sets_iff, is_open_uniformity, and_imp, exists_imp_distrib], exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : Ξ± Γ— Ξ± | p.1 = x β†’ p.2 ∈ s} ∈ 𝓀 Ξ±}, assume x' hx', refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'), by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b), have hp : (x', b) ∈ t, from hax' β–Έ hp', have (b, b') ∈ t, from hab β–Έ hp'', have (x', b') ∈ t β—‹ t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma mem_nhds_uniformity_iff_left {x : Ξ±} {s : set Ξ±} : s ∈ 𝓝 x ↔ {p : Ξ± Γ— Ξ± | p.2 = x β†’ p.1 ∈ s} ∈ 𝓀 Ξ± := by { rw [uniformity_eq_symm, mem_nhds_uniformity_iff_right], refl } lemma nhds_eq_comap_uniformity_aux {Ξ± : Type u} {x : Ξ±} {s : set Ξ±} {F : filter (Ξ± Γ— Ξ±)} : {p : Ξ± Γ— Ξ± | p.fst = x β†’ p.snd ∈ s} ∈ F ↔ s ∈ comap (prod.mk x) F := by rw mem_comap_sets ; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, F.sets_of_superset h $ assume ⟨p₁, pβ‚‚βŸ© hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_comap_uniformity {x : Ξ±} : 𝓝 x = (𝓀 Ξ±).comap (prod.mk x) := by { ext s, rw [mem_nhds_uniformity_iff_right], exact nhds_eq_comap_uniformity_aux } lemma is_open_iff_ball_subset {s : set Ξ±} : is_open s ↔ βˆ€ x ∈ s, βˆƒ V ∈ 𝓀 Ξ±, ball x V βŠ† s := begin simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity], exact iff.rfl, end lemma nhds_basis_uniformity' {p : Ξ² β†’ Prop} {s : Ξ² β†’ set (Ξ± Γ— Ξ±)} (h : (𝓀 Ξ±).has_basis p s) {x : Ξ±} : (𝓝 x).has_basis p (Ξ» i, ball x (s i)) := by { rw [nhds_eq_comap_uniformity], exact h.comap (prod.mk x) } lemma nhds_basis_uniformity {p : Ξ² β†’ Prop} {s : Ξ² β†’ set (Ξ± Γ— Ξ±)} (h : (𝓀 Ξ±).has_basis p s) {x : Ξ±} : (𝓝 x).has_basis p (Ξ» i, {y | (y, x) ∈ s i}) := begin replace h := h.comap prod.swap, rw [← map_swap_eq_comap_swap, ← uniformity_eq_symm] at h, exact nhds_basis_uniformity' h end lemma uniform_space.mem_nhds_iff {x : Ξ±} {s : set Ξ±} : s ∈ 𝓝 x ↔ βˆƒ V ∈ 𝓀 Ξ±, ball x V βŠ† s := begin rw [nhds_eq_comap_uniformity, mem_comap_sets], exact iff.rfl, end lemma uniform_space.ball_mem_nhds (x : Ξ±) ⦃V : set (Ξ± Γ— Ξ±)⦄ (V_in : V ∈ 𝓀 Ξ±) : ball x V ∈ 𝓝 x := begin rw uniform_space.mem_nhds_iff, exact ⟨V, V_in, subset.refl _⟩ end lemma uniform_space.mem_nhds_iff_symm {x : Ξ±} {s : set Ξ±} : s ∈ 𝓝 x ↔ βˆƒ V ∈ 𝓀 Ξ±, symmetric_rel V ∧ ball x V βŠ† s := begin rw uniform_space.mem_nhds_iff, split, { rintros ⟨V, V_in, V_sub⟩, use [symmetrize_rel V, symmetrize_mem_uniformity V_in, symmetric_symmetrize_rel V], exact subset.trans (ball_mono (symmetrize_rel_subset_self V) x) V_sub }, { rintros ⟨V, V_in, V_symm, V_sub⟩, exact ⟨V, V_in, V_sub⟩ } end lemma uniform_space.has_basis_nhds (x : Ξ±) : has_basis (𝓝 x) (Ξ» s : set (Ξ± Γ— Ξ±), s ∈ 𝓀 Ξ± ∧ symmetric_rel s) (Ξ» s, ball x s) := ⟨λ t, by simp [uniform_space.mem_nhds_iff_symm, and_assoc]⟩ open uniform_space lemma uniform_space.has_basis_nhds_prod (x y : Ξ±) : has_basis (𝓝 (x, y)) (Ξ» s, s ∈ 𝓀 Ξ± ∧ symmetric_rel s) $ Ξ» s, (ball x s).prod (ball y s) := begin rw nhds_prod_eq, apply (has_basis_nhds x).prod' (has_basis_nhds y), rintro U V ⟨U_in, U_symm⟩ ⟨V_in, V_symm⟩, exact ⟨U ∩ V, ⟨(𝓀 Ξ±).inter_sets U_in V_in, symmetric_rel_inter U_symm V_symm⟩, ball_mono (inter_subset_left U V) x, ball_mono (inter_subset_right U V) y⟩, end lemma nhds_eq_uniformity {x : Ξ±} : 𝓝 x = (𝓀 Ξ±).lift' (ball x) := (nhds_basis_uniformity' (𝓀 Ξ±).basis_sets).eq_binfi lemma mem_nhds_left (x : Ξ±) {s : set (Ξ±Γ—Ξ±)} (h : s ∈ 𝓀 Ξ±) : {y : Ξ± | (x, y) ∈ s} ∈ 𝓝 x := ball_mem_nhds x h lemma mem_nhds_right (y : Ξ±) {s : set (Ξ±Γ—Ξ±)} (h : s ∈ 𝓀 Ξ±) : {x : Ξ± | (x, y) ∈ s} ∈ 𝓝 y := mem_nhds_left _ (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : Ξ±} : tendsto (Ξ»a', (a', a)) (𝓝 a) (𝓀 Ξ±) := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : Ξ±} : tendsto (Ξ»a', (a, a')) (𝓝 a) (𝓀 Ξ±) := assume s, mem_nhds_left a lemma lift_nhds_left {x : Ξ±} {g : set Ξ± β†’ filter Ξ²} (hg : monotone g) : (𝓝 x).lift g = (𝓀 Ξ±).lift (Ξ»s:set (Ξ±Γ—Ξ±), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage ) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : Ξ±} {g : set Ξ± β†’ filter Ξ²} (hg : monotone g) : (𝓝 x).lift g = (𝓀 Ξ±).lift (Ξ»s:set (Ξ±Γ—Ξ±), g {y | (y, x) ∈ s}) := calc (𝓝 x).lift g = (𝓀 Ξ±).lift (Ξ»s:set (Ξ±Γ—Ξ±), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap Ξ± Ξ±) <$> (𝓀 Ξ±)).lift (Ξ»s:set (Ξ±Γ—Ξ±), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = (𝓀 Ξ±).lift (Ξ»s:set (Ξ±Γ—Ξ±), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ hg.comp monotone_preimage ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : Ξ±} : 𝓝 a Γ—αΆ  𝓝 b = (𝓀 Ξ±).lift (Ξ»s:set (Ξ±Γ—Ξ±), (𝓀 Ξ±).lift' (Ξ»t:set (Ξ±Γ—Ξ±), set.prod {y : Ξ± | (y, a) ∈ s} {y : Ξ± | (b, y) ∈ t})) := begin rw [prod_def], show (𝓝 a).lift (Ξ»s:set Ξ±, (𝓝 b).lift (Ξ»t:set Ξ±, π“Ÿ (set.prod s t))) = _, rw [lift_nhds_right], apply congr_arg, funext s, rw [lift_nhds_left], refl, exact monotone_principal.comp (monotone_prod monotone_const monotone_id), exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : Ξ±} : 𝓝 (a, b) = (𝓀 Ξ±).lift' (Ξ»s:set (Ξ±Γ—Ξ±), set.prod {y : Ξ± | (y, a) ∈ s} {y : Ξ± | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (Ξ±Γ—Ξ±)} (s : set (Ξ±Γ—Ξ±)) (hd : d ∈ 𝓀 Ξ±) : βˆƒ(t : set (Ξ±Γ—Ξ±)), is_open t ∧ s βŠ† t ∧ t βŠ† {p | βˆƒx y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:Ξ±Γ—Ξ± | βˆƒx y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have βˆ€p ∈ s, βˆƒt βŠ† cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ 𝓝 (x, y), begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have βˆƒt:(Ξ (p:Ξ±Γ—Ξ±) (h:p ∈ s), set (Ξ±Γ—Ξ±)), βˆ€p, βˆ€h:p ∈ s, t p h βŠ† cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:Ξ±Γ—Ξ±, ⋃ h : p ∈ s, t p h : set (Ξ±Γ—Ξ±)), is_open_Union $ assume (p:Ξ±Γ—Ξ±), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end /-- Entourages are neighborhoods of the diagonal. -/ lemma nhds_le_uniformity (x : Ξ±) : 𝓝 (x, x) ≀ 𝓀 Ξ± := begin intros V V_in, rcases comp_symm_mem_uniformity_sets V_in with ⟨w, w_in, w_symm, w_sub⟩, have : (ball x w).prod (ball x w) ∈ 𝓝 (x, x), { rw nhds_prod_eq, exact prod_mem_prod (ball_mem_nhds x w_in) (ball_mem_nhds x w_in) }, apply mem_sets_of_superset this, rintros ⟨u, v⟩ ⟨u_in, v_in⟩, exact w_sub (mem_comp_of_mem_ball w_symm u_in v_in) end /-- Entourages are neighborhoods of the diagonal. -/ lemma supr_nhds_le_uniformity : (⨆ x : Ξ±, 𝓝 (x, x)) ≀ 𝓀 Ξ± := supr_le nhds_le_uniformity /-! ### Closure and interior in uniform spaces -/ lemma closure_eq_uniformity (s : set $ Ξ± Γ— Ξ±) : closure s = β‹‚ V ∈ {V | V ∈ 𝓀 Ξ± ∧ symmetric_rel V}, V β—‹ s β—‹ V := begin ext ⟨x, y⟩, simp_rw [mem_closure_iff_nhds_basis (uniform_space.has_basis_nhds_prod x y), mem_Inter, mem_set_of_eq], apply forall_congr, intro V, apply forall_congr, rintros ⟨V_in, V_symm⟩, simp_rw [mem_comp_comp V_symm, inter_comm, exists_prop], exact iff.rfl, end lemma uniformity_has_basis_closed : has_basis (𝓀 Ξ±) (Ξ» V : set (Ξ± Γ— Ξ±), V ∈ 𝓀 Ξ± ∧ is_closed V) id := begin rw filter.has_basis_self, intro t, split, { intro h, rcases comp_comp_symm_mem_uniformity_sets h with ⟨w, w_in, w_symm, r⟩, refine ⟨closure w, _, is_closed_closure, _⟩, apply mem_sets_of_superset w_in subset_closure, refine subset.trans _ r, rw closure_eq_uniformity, apply Inter_subset_of_subset, apply Inter_subset, exact ⟨w_in, w_symm⟩ }, { rintros ⟨r, r_in, r_closed, r_sub⟩, exact mem_sets_of_superset r_in r_sub, } end /-- Closed entourages form a basis of the uniformity filter. -/ lemma uniformity_has_basis_closure : has_basis (𝓀 Ξ±) (Ξ» V : set (Ξ± Γ— Ξ±), V ∈ 𝓀 Ξ±) closure := ⟨begin intro t, rw uniformity_has_basis_closed.mem_iff, split, { rintros ⟨r, ⟨r_in, r_closed⟩, r_sub⟩, use [r, r_in], convert r_sub, rw r_closed.closure_eq, refl }, { rintros ⟨r, r_in, r_sub⟩, exact ⟨closure r, ⟨mem_sets_of_superset r_in subset_closure, is_closed_closure⟩, r_sub⟩ } end⟩ lemma closure_eq_inter_uniformity {t : set (Ξ±Γ—Ξ±)} : closure t = (β‹‚ d ∈ 𝓀 Ξ±, d β—‹ (t β—‹ d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (𝓝 (a, b) βŠ“ π“Ÿ t β‰  βŠ₯) : mem_closure_iff_cluster_pt ... ↔ (((@prod.swap Ξ± Ξ±) <$> 𝓀 Ξ±).lift' (Ξ» (s : set (Ξ± Γ— Ξ±)), set.prod {x : Ξ± | (x, a) ∈ s} {y : Ξ± | (b, y) ∈ s}) βŠ“ π“Ÿ t β‰  βŠ₯) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap Ξ± Ξ±) (𝓀 Ξ±)).lift' (Ξ» (s : set (Ξ± Γ— Ξ±)), set.prod {x : Ξ± | (x, a) ∈ s} {y : Ξ± | (b, y) ∈ s}) βŠ“ π“Ÿ t β‰  βŠ₯) : by refl ... ↔ ((𝓀 Ξ±).lift' (Ξ» (s : set (Ξ± Γ— Ξ±)), set.prod {y : Ξ± | (a, y) ∈ s} {x : Ξ± | (x, b) ∈ s}) βŠ“ π“Ÿ t β‰  βŠ₯) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (βˆ€s ∈ 𝓀 Ξ±, (set.prod {y : Ξ± | (a, y) ∈ s} {x : Ξ± | (x, b) ∈ s} ∩ t).nonempty) : begin rw [lift'_inf_principal_eq, ← ne_bot, lift'_ne_bot_iff], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (βˆ€ s ∈ 𝓀 Ξ±, (a, b) ∈ s β—‹ (t β—‹ s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : 𝓀 Ξ± = (𝓀 Ξ±).lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure) (calc (𝓀 Ξ±).lift' closure ≀ (𝓀 Ξ±).lift' (Ξ»d, d β—‹ (d β—‹ d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≀ (𝓀 Ξ±) : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : 𝓀 Ξ± = (𝓀 Ξ±).lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s βŠ† interior d, from calc s βŠ† t : hst ... βŠ† interior d : (subset_interior_iff_subset_of_open ht).mpr $ Ξ» x (hx : x ∈ t), let ⟨x, y, h₁, hβ‚‚, hβ‚ƒβŸ© := ht_comp hx in hs_comp ⟨x, h₁, y, hβ‚‚, hβ‚ƒβŸ©, have interior d ∈ 𝓀 Ξ±, by filter_upwards [hs] this, by simp [this]) (assume s hs, ((𝓀 Ξ±).lift' interior).sets_of_superset (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (Ξ± Γ— Ξ±)} (hs : s ∈ 𝓀 Ξ±) : interior s ∈ 𝓀 Ξ± := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed {s : set (Ξ±Γ—Ξ±)} (h : s ∈ 𝓀 Ξ±) : βˆƒt ∈ 𝓀 Ξ±, is_closed t ∧ t βŠ† s := let ⟨t, ⟨ht_mem, htc⟩, hts⟩ := uniformity_has_basis_closed.mem_iff.1 h in ⟨t, ht_mem, htc, hts⟩ /-- The uniform neighborhoods of all points of a dense set cover the whole space. -/ lemma dense.bUnion_uniformity_ball {s : set Ξ±} {U : set (Ξ± Γ— Ξ±)} (hs : dense s) (hU : U ∈ 𝓀 Ξ±) : (⋃ x ∈ s, ball x U) = univ := begin refine bUnion_eq_univ_iff.2 (Ξ» y, _), rcases hs.inter_nhds_nonempty (mem_nhds_right y hU) with ⟨x, hxs, hxy : (x, y) ∈ U⟩, exact ⟨x, hxs, hxy⟩ end /-! ### Uniformity bases -/ /-- Open elements of `𝓀 Ξ±` form a basis of `𝓀 Ξ±`. -/ lemma uniformity_has_basis_open : has_basis (𝓀 Ξ±) (Ξ» V : set (Ξ± Γ— Ξ±), V ∈ 𝓀 Ξ± ∧ is_open V) id := has_basis_self.2 $ Ξ» s, ⟨λ hs, ⟨interior s, interior_mem_uniformity hs, is_open_interior, interior_subset⟩, Ξ» ⟨t, htU, hto, hts⟩, mem_sets_of_superset htU hts⟩ lemma filter.has_basis.mem_uniformity_iff {p : Ξ² β†’ Prop} {s : Ξ² β†’ set (Ξ±Γ—Ξ±)} (h : (𝓀 Ξ±).has_basis p s) {t : set (Ξ± Γ— Ξ±)} : t ∈ 𝓀 Ξ± ↔ βˆƒ i (hi : p i), βˆ€ a b, (a, b) ∈ s i β†’ (a, b) ∈ t := h.mem_iff.trans $ by simp only [prod.forall, subset_def] /-- Symmetric entourages form a basis of `𝓀 Ξ±` -/ lemma uniform_space.has_basis_symmetric : (𝓀 Ξ±).has_basis (Ξ» s : set (Ξ± Γ— Ξ±), s ∈ 𝓀 Ξ± ∧ symmetric_rel s) id := has_basis_self.2 $ Ξ» t, ⟨λ t_in, ⟨symmetrize_rel t, symmetrize_mem_uniformity t_in, symmetric_symmetrize_rel t, symmetrize_rel_subset_self t⟩, Ξ» ⟨s, s_in, _, hst⟩, mem_sets_of_superset s_in hst⟩ /-- Open elements `s : set (Ξ± Γ— Ξ±)` of `𝓀 Ξ±` such that `(x, y) ∈ s ↔ (y, x) ∈ s` form a basis of `𝓀 Ξ±`. -/ lemma uniformity_has_basis_open_symmetric : has_basis (𝓀 Ξ±) (Ξ» V : set (Ξ± Γ— Ξ±), V ∈ 𝓀 Ξ± ∧ is_open V ∧ symmetric_rel V) id := begin simp only [← and_assoc], refine uniformity_has_basis_open.restrict (Ξ» s hs, ⟨symmetrize_rel s, _⟩), exact ⟨⟨symmetrize_mem_uniformity hs.1, is_open_inter hs.2 (hs.2.preimage continuous_swap)⟩, symmetric_symmetrize_rel s, symmetrize_rel_subset_self s⟩ end lemma uniform_space.has_seq_basis (h : is_countably_generated $ 𝓀 Ξ±) : βˆƒ V : β„• β†’ set (Ξ± Γ— Ξ±), has_antimono_basis (𝓀 Ξ±) (Ξ» _, true) V ∧ βˆ€ n, symmetric_rel (V n) := let ⟨U, hsym, hbasis⟩ := h.exists_antimono_subbasis uniform_space.has_basis_symmetric in ⟨U, hbasis, Ξ» n, (hsym n).2⟩ /-! ### Uniform continuity -/ /-- A function `f : Ξ± β†’ Ξ²` is *uniformly continuous* if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `Ξ±`. -/ def uniform_continuous [uniform_space Ξ²] (f : Ξ± β†’ Ξ²) := tendsto (Ξ»x:Ξ±Γ—Ξ±, (f x.1, f x.2)) (𝓀 Ξ±) (𝓀 Ξ²) /-- A function `f : Ξ± β†’ Ξ²` is *uniformly continuous* on `s : set Ξ±` if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal while remaining in `s.prod s`. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `s`.-/ def uniform_continuous_on [uniform_space Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) : Prop := tendsto (Ξ» x : Ξ± Γ— Ξ±, (f x.1, f x.2)) (𝓀 Ξ± βŠ“ principal (s.prod s)) (𝓀 Ξ²) theorem uniform_continuous_def [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_continuous f ↔ βˆ€ r ∈ 𝓀 Ξ², { x : Ξ± Γ— Ξ± | (f x.1, f x.2) ∈ r} ∈ 𝓀 Ξ± := iff.rfl theorem uniform_continuous_iff_eventually [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_continuous f ↔ βˆ€ r ∈ 𝓀 Ξ², βˆ€αΆ  (x : Ξ± Γ— Ξ±) in 𝓀 Ξ±, (f x.1, f x.2) ∈ r := iff.rfl lemma uniform_continuous_of_const [uniform_space Ξ²] {c : Ξ± β†’ Ξ²} (h : βˆ€a b, c a = c b) : uniform_continuous c := have (Ξ» (x : Ξ± Γ— Ξ±), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b, le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity lemma uniform_continuous_id : uniform_continuous (@id Ξ±) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space Ξ²] {b : Ξ²} : uniform_continuous (Ξ»a:Ξ±, b) := uniform_continuous_of_const $ Ξ» _ _, rfl lemma uniform_continuous.comp [uniform_space Ξ²] [uniform_space Ξ³] {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) := hg.comp hf lemma filter.has_basis.uniform_continuous_iff [uniform_space Ξ²] {p : Ξ³ β†’ Prop} {s : Ξ³ β†’ set (Ξ±Γ—Ξ±)} (ha : (𝓀 Ξ±).has_basis p s) {q : Ξ΄ β†’ Prop} {t : Ξ΄ β†’ set (Ξ²Γ—Ξ²)} (hb : (𝓀 Ξ²).has_basis q t) {f : Ξ± β†’ Ξ²} : uniform_continuous f ↔ βˆ€ i (hi : q i), βˆƒ j (hj : p j), βˆ€ x y, (x, y) ∈ s j β†’ (f x, f y) ∈ t i := (ha.tendsto_iff hb).trans $ by simp only [prod.forall] lemma filter.has_basis.uniform_continuous_on_iff [uniform_space Ξ²] {p : Ξ³ β†’ Prop} {s : Ξ³ β†’ set (Ξ±Γ—Ξ±)} (ha : (𝓀 Ξ±).has_basis p s) {q : Ξ΄ β†’ Prop} {t : Ξ΄ β†’ set (Ξ²Γ—Ξ²)} (hb : (𝓀 Ξ²).has_basis q t) {f : Ξ± β†’ Ξ²} {S : set Ξ±} : uniform_continuous_on f S ↔ βˆ€ i (hi : q i), βˆƒ j (hj : p j), βˆ€ x y ∈ S, (x, y) ∈ s j β†’ (f x, f y) ∈ t i := ((ha.inf_principal (S.prod S)).tendsto_iff hb).trans $ by finish [prod.forall] end uniform_space open_locale uniformity section constructions instance : partial_order (uniform_space Ξ±) := { le := Ξ»t s, t.uniformity ≀ s.uniformity, le_antisymm := assume t s h₁ hβ‚‚, uniform_space_eq $ le_antisymm h₁ hβ‚‚, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ hβ‚‚, le_trans h₁ hβ‚‚ } instance : has_Inf (uniform_space Ξ±) := ⟨assume s, uniform_space.of_core { uniformity := (β¨…u∈s, @uniformity Ξ± u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma Inf_le {tt : set (uniform_space Ξ±)} {t : uniform_space Ξ±} (h : t ∈ tt) : Inf tt ≀ t := show (β¨…u∈tt, @uniformity Ξ± u) ≀ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma le_Inf {tt : set (uniform_space Ξ±)} {t : uniform_space Ξ±} (h : βˆ€t'∈tt, t ≀ t') : t ≀ Inf tt := show t.uniformity ≀ (β¨…u∈tt, @uniformity Ξ± u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_top (uniform_space Ξ±) := ⟨uniform_space.of_core { uniformity := ⊀, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_bot (uniform_space Ξ±) := ⟨{ to_topological_space := βŠ₯, uniformity := π“Ÿ id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩ instance : complete_lattice (uniform_space Ξ±) := { sup := Ξ»a b, Inf {x | a ≀ x ∧ b ≀ x}, le_sup_left := Ξ» a b, le_Inf (Ξ» _ ⟨h, _⟩, h), le_sup_right := Ξ» a b, le_Inf (Ξ» _ ⟨_, h⟩, h), sup_le := Ξ» a b c h₁ hβ‚‚, Inf_le ⟨h₁, hβ‚‚βŸ©, inf := Ξ» a b, Inf {a, b}, le_inf := Ξ» a b c h₁ hβ‚‚, le_Inf (Ξ» u h, by { cases h, exact h.symm β–Έ h₁, exact (mem_singleton_iff.1 h).symm β–Έ hβ‚‚ }), inf_le_left := Ξ» a b, Inf_le (by simp), inf_le_right := Ξ» a b, Inf_le (by simp), top := ⊀, le_top := Ξ» a, show a.uniformity ≀ ⊀, from le_top, bot := βŠ₯, bot_le := Ξ» u, u.refl, Sup := Ξ» tt, Inf {t | βˆ€ t' ∈ tt, t' ≀ t}, le_Sup := Ξ» s u h, le_Inf (Ξ» u' h', h' u h), Sup_le := Ξ» s u h, Inf_le h, Inf := Inf, le_Inf := Ξ» s a hs, le_Inf hs, Inf_le := Ξ» s a ha, Inf_le ha, ..uniform_space.partial_order } lemma infi_uniformity {ΞΉ : Sort*} {u : ΞΉ β†’ uniform_space Ξ±} : (infi u).uniformity = (β¨…i, (u i).uniformity) := show (β¨…a (h : βˆƒi:ΞΉ, u i = a), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha β–Έ infi_le _ _) lemma inf_uniformity {u v : uniform_space Ξ±} : (u βŠ“ v).uniformity = u.uniformity βŠ“ v.uniformity := have (u βŠ“ v) = (β¨…i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq], calc (u βŠ“ v).uniformity = ((β¨…i (h : i = u ∨ i = v), i) : uniform_space Ξ±).uniformity : by rw [this] ... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space Ξ±) := ⟨βŠ₯⟩ instance inhabited_uniform_space_core : inhabited (uniform_space.core Ξ±) := ⟨@uniform_space.to_core _ (default _)⟩ /-- Given `f : Ξ± β†’ Ξ²` and a uniformity `u` on `Ξ²`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `Ξ± Γ— Ξ± β†’ Ξ² Γ— Ξ²`. -/ def uniform_space.comap (f : Ξ± β†’ Ξ²) (u : uniform_space Ξ²) : uniform_space Ξ± := { uniformity := u.uniformity.comap (Ξ»p:Ξ±Γ—Ξ±, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h β–Έ rfl) (comap_mono u.refl), symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap, comp := le_trans begin rw [comap_lift'_eq, comap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, aβ‚‚βŸ© ⟨x, h₁, hβ‚‚βŸ©, ⟨f x, h₁, hβ‚‚βŸ©), repeat { exact monotone_comp_rel monotone_id monotone_id } end (comap_mono u.comp), is_open_uniformity := Ξ» s, begin change (@is_open Ξ± (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff_right, filter.comap, and_comm], refine ball_congr (Ξ» x hx, ⟨_, _⟩), { rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩, rintro ⟨x₁, xβ‚‚βŸ© h rfl, exact hts (h rfl) }, { rintro ⟨t, ht, hts⟩, exact ⟨{y | (f x, y) ∈ t}, Ξ» y hy, @hts (x, y) hy rfl, mem_nhds_uniformity_iff_right.1 $ mem_nhds_left _ ht⟩ } end } lemma uniformity_comap [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} (h : β€Ήuniform_space Ξ±β€Ί = uniform_space.comap f β€Ήuniform_space Ξ²β€Ί) : 𝓀 Ξ± = comap (prod.map f f) (𝓀 Ξ²) := by { rw h, refl } lemma uniform_space_comap_id {Ξ± : Type*} : uniform_space.comap (id : Ξ± β†’ Ξ±) = id := by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id] lemma uniform_space.comap_comap {Ξ± Ξ² Ξ³} [uΞ³ : uniform_space Ξ³] {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} : uniform_space.comap (g ∘ f) uΞ³ = uniform_space.comap f (uniform_space.comap g uΞ³) := by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap lemma uniform_continuous_iff {Ξ± Ξ²} [uΞ± : uniform_space Ξ±] [uΞ² : uniform_space Ξ²] {f : Ξ± β†’ Ξ²} : uniform_continuous f ↔ uΞ± ≀ uΞ².comap f := filter.map_le_iff_le_comap lemma uniform_continuous_comap {f : Ξ± β†’ Ξ²} [u : uniform_space Ξ²] : @uniform_continuous Ξ± Ξ² (uniform_space.comap f u) u f := tendsto_comap theorem to_topological_space_comap {f : Ξ± β†’ Ξ²} {u : uniform_space Ξ²} : @uniform_space.to_topological_space _ (uniform_space.comap f u) = topological_space.induced f (@uniform_space.to_topological_space Ξ² u) := rfl lemma uniform_continuous_comap' {f : Ξ³ β†’ Ξ²} {g : Ξ± β†’ Ξ³} [v : uniform_space Ξ²] [u : uniform_space Ξ±] (h : uniform_continuous (f ∘ g)) : @uniform_continuous Ξ± Ξ³ u (uniform_space.comap f v) g := tendsto_comap_iff.2 h lemma to_topological_space_mono {u₁ uβ‚‚ : uniform_space Ξ±} (h : u₁ ≀ uβ‚‚) : @uniform_space.to_topological_space _ u₁ ≀ @uniform_space.to_topological_space _ uβ‚‚ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity Ξ± u₁ a, @nhds_eq_uniformity Ξ± uβ‚‚ a]; exact (lift'_mono h $ le_refl _) lemma uniform_continuous.continuous [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : uniform_continuous f) : continuous f := continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf lemma to_topological_space_bot : @uniform_space.to_topological_space Ξ± βŠ₯ = βŠ₯ := rfl lemma to_topological_space_top : @uniform_space.to_topological_space Ξ± ⊀ = ⊀ := top_unique $ assume s hs, s.eq_empty_or_nonempty.elim (assume : s = βˆ…, this.symm β–Έ @is_open_empty _ ⊀) (assume ⟨x, hx⟩, have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl, this.symm β–Έ @is_open_univ _ ⊀) lemma to_topological_space_infi {ΞΉ : Sort*} {u : ΞΉ β†’ uniform_space Ξ±} : (infi u).to_topological_space = β¨…i, (u i).to_topological_space := begin by_cases h : nonempty ΞΉ, { resetI, refine (eq_of_nhds_eq_nhds $ assume a, _), rw [nhds_infi, nhds_eq_uniformity], change (infi u).uniformity.lift' (preimage $ prod.mk a) = _, rw [infi_uniformity, lift'_infi], { simp only [nhds_eq_uniformity], refl }, { exact assume a b, rfl } }, { rw [infi_of_empty h, infi_of_empty h, to_topological_space_top] } end lemma to_topological_space_Inf {s : set (uniform_space Ξ±)} : (Inf s).to_topological_space = (β¨…i∈s, @uniform_space.to_topological_space Ξ± i) := begin rw [Inf_eq_infi], simp only [← to_topological_space_infi], end lemma to_topological_space_inf {u v : uniform_space Ξ±} : (u βŠ“ v).to_topological_space = u.to_topological_space βŠ“ v.to_topological_space := by rw [to_topological_space_Inf, infi_pair] instance : uniform_space empty := βŠ₯ instance : uniform_space unit := βŠ₯ instance : uniform_space bool := βŠ₯ instance : uniform_space β„• := βŠ₯ instance : uniform_space β„€ := βŠ₯ instance {p : Ξ± β†’ Prop} [t : uniform_space Ξ±] : uniform_space (subtype p) := uniform_space.comap subtype.val t lemma uniformity_subtype {p : Ξ± β†’ Prop} [t : uniform_space Ξ±] : 𝓀 (subtype p) = comap (Ξ»q:subtype p Γ— subtype p, (q.1.1, q.2.1)) (𝓀 Ξ±) := rfl lemma uniform_continuous_subtype_val {p : Ξ± β†’ Prop} [uniform_space Ξ±] : uniform_continuous (subtype.val : {a : Ξ± // p a} β†’ Ξ±) := uniform_continuous_comap lemma uniform_continuous_subtype_mk {p : Ξ± β†’ Prop} [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ² β†’ Ξ±} (hf : uniform_continuous f) (h : βˆ€x, p (f x)) : uniform_continuous (Ξ»x, ⟨f x, h x⟩ : Ξ² β†’ subtype p) := uniform_continuous_comap' hf lemma uniform_continuous_on_iff_restrict [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} : uniform_continuous_on f s ↔ uniform_continuous (s.restrict f) := begin unfold uniform_continuous_on set.restrict uniform_continuous tendsto, rw [show (Ξ» x : s Γ— s, (f x.1, f x.2)) = prod.map f f ∘ coe, by ext x; cases x; refl, uniformity_comap rfl, show prod.map subtype.val subtype.val = (coe : s Γ— s β†’ Ξ± Γ— Ξ±), by ext x; cases x; refl], conv in (map _ (comap _ _)) { rw ← filter.map_map }, rw subtype_coe_map_comap_prod, refl, end lemma tendsto_of_uniform_continuous_subtype [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : uniform_continuous (Ξ»x:s, f x.val)) (ha : s ∈ 𝓝 a) : tendsto f (𝓝 a) (𝓝 (f a)) := by rw [(@map_nhds_subtype_coe_eq Ξ± _ s a (mem_of_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _) lemma uniform_continuous_on.continuous_on [uniform_space Ξ±] [uniform_space Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : uniform_continuous_on f s) : continuous_on f s := begin rw uniform_continuous_on_iff_restrict at h, rw continuous_on_iff_continuous_restrict, exact h.continuous end section prod /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ instance [u₁ : uniform_space Ξ±] [uβ‚‚ : uniform_space Ξ²] : uniform_space (Ξ± Γ— Ξ²) := uniform_space.of_core_eq (u₁.comap prod.fst βŠ“ uβ‚‚.comap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.comap prod.fst βŠ“ uβ‚‚.comap prod.snd).to_topological_space : by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) theorem uniformity_prod [uniform_space Ξ±] [uniform_space Ξ²] : 𝓀 (Ξ± Γ— Ξ²) = (𝓀 Ξ±).comap (Ξ»p:(Ξ± Γ— Ξ²) Γ— Ξ± Γ— Ξ², (p.1.1, p.2.1)) βŠ“ (𝓀 Ξ²).comap (Ξ»p:(Ξ± Γ— Ξ²) Γ— Ξ± Γ— Ξ², (p.1.2, p.2.2)) := inf_uniformity lemma uniformity_prod_eq_prod [uniform_space Ξ±] [uniform_space Ξ²] : 𝓀 (Ξ±Γ—Ξ²) = map (Ξ»p:(Ξ±Γ—Ξ±)Γ—(Ξ²Γ—Ξ²), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (𝓀 Ξ± Γ—αΆ  𝓀 Ξ²) := have map (Ξ»p:(Ξ±Γ—Ξ±)Γ—(Ξ²Γ—Ξ²), ((p.1.1, p.2.1), (p.1.2, p.2.2))) = comap (Ξ»p:(Ξ±Γ—Ξ²)Γ—(Ξ±Γ—Ξ²), ((p.1.1, p.2.1), (p.1.2, p.2.2))), from funext $ assume f, map_eq_comap_of_inverse (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl), by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap, comap_comap] lemma mem_map_sets_iff' {Ξ± : Type*} {Ξ² : Type*} {f : filter Ξ±} {m : Ξ± β†’ Ξ²} {t : set Ξ²} : t ∈ (map m f).sets ↔ (βˆƒs∈f, m '' s βŠ† t) := mem_map_sets_iff lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space Ξ±] {s:set (Ξ±Γ—Ξ±)} {f : Ξ± β†’ Ξ± β†’ Ξ±} (hf : uniform_continuous (Ξ»p:Ξ±Γ—Ξ±, f p.1 p.2)) (hs : s ∈ 𝓀 Ξ±) : βˆƒuβˆˆπ“€ Ξ±, βˆ€a b c, (a, b) ∈ u β†’ (f a c, f b c) ∈ s := begin rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf, rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf, rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht, refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩, exact hab, exact refl_mem_uniformity hv, refl end lemma mem_uniform_prod [t₁ : uniform_space Ξ±] [tβ‚‚ : uniform_space Ξ²] {a : set (Ξ± Γ— Ξ±)} {b : set (Ξ² Γ— Ξ²)} (ha : a ∈ 𝓀 Ξ±) (hb : b ∈ 𝓀 Ξ²) : {p:(Ξ±Γ—Ξ²)Γ—(Ξ±Γ—Ξ²) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (Ξ± Γ— Ξ²) _) := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb) lemma tendsto_prod_uniformity_fst [uniform_space Ξ±] [uniform_space Ξ²] : tendsto (Ξ»p:(Ξ±Γ—Ξ²)Γ—(Ξ±Γ—Ξ²), (p.1.1, p.2.1)) (𝓀 (Ξ± Γ— Ξ²)) (𝓀 Ξ±) := le_trans (map_mono (@inf_le_left (uniform_space (Ξ±Γ—Ξ²)) _ _ _)) map_comap_le lemma tendsto_prod_uniformity_snd [uniform_space Ξ±] [uniform_space Ξ²] : tendsto (Ξ»p:(Ξ±Γ—Ξ²)Γ—(Ξ±Γ—Ξ²), (p.1.2, p.2.2)) (𝓀 (Ξ± Γ— Ξ²)) (𝓀 Ξ²) := le_trans (map_mono (@inf_le_right (uniform_space (Ξ±Γ—Ξ²)) _ _ _)) map_comap_le lemma uniform_continuous_fst [uniform_space Ξ±] [uniform_space Ξ²] : uniform_continuous (Ξ»p:Ξ±Γ—Ξ², p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space Ξ±] [uniform_space Ξ²] : uniform_continuous (Ξ»p:Ξ±Γ—Ξ², p.2) := tendsto_prod_uniformity_snd variables [uniform_space Ξ±] [uniform_space Ξ²] [uniform_space Ξ³] lemma uniform_continuous.prod_mk {f₁ : Ξ± β†’ Ξ²} {fβ‚‚ : Ξ± β†’ Ξ³} (h₁ : uniform_continuous f₁) (hβ‚‚ : uniform_continuous fβ‚‚) : uniform_continuous (Ξ»a, (f₁ a, fβ‚‚ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 hβ‚‚βŸ© lemma uniform_continuous.prod_mk_left {f : Ξ± Γ— Ξ² β†’ Ξ³} (h : uniform_continuous f) (b) : uniform_continuous (Ξ» a, f (a,b)) := h.comp (uniform_continuous_id.prod_mk uniform_continuous_const) lemma uniform_continuous.prod_mk_right {f : Ξ± Γ— Ξ² β†’ Ξ³} (h : uniform_continuous f) (a) : uniform_continuous (Ξ» b, f (a,b)) := h.comp (uniform_continuous_const.prod_mk uniform_continuous_id) lemma uniform_continuous.prod_map [uniform_space Ξ΄] {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (prod.map f g) := (hf.comp uniform_continuous_fst).prod_mk (hg.comp uniform_continuous_snd) lemma to_topological_space_prod {Ξ±} {Ξ²} [u : uniform_space Ξ±] [v : uniform_space Ξ²] : @uniform_space.to_topological_space (Ξ± Γ— Ξ²) prod.uniform_space = @prod.topological_space Ξ± Ξ² u.to_topological_space v.to_topological_space := rfl end prod section open uniform_space function variables {Ξ΄' : Type*} [uniform_space Ξ±] [uniform_space Ξ²] [uniform_space Ξ³] [uniform_space Ξ΄] [uniform_space Ξ΄'] local notation f `βˆ˜β‚‚` g := function.bicompr f g /-- Uniform continuity for functions of two variables. -/ def uniform_continuousβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) := uniform_continuous (uncurry f) lemma uniform_continuousβ‚‚_def (f : Ξ± β†’ Ξ² β†’ Ξ³) : uniform_continuousβ‚‚ f ↔ uniform_continuous (uncurry f) := iff.rfl lemma uniform_continuousβ‚‚.uniform_continuous {f : Ξ± β†’ Ξ² β†’ Ξ³} (h : uniform_continuousβ‚‚ f) : uniform_continuous (uncurry f) := h lemma uniform_continuousβ‚‚_curry (f : Ξ± Γ— Ξ² β†’ Ξ³) : uniform_continuousβ‚‚ (function.curry f) ↔ uniform_continuous f := by rw [uniform_continuousβ‚‚, uncurry_curry] lemma uniform_continuousβ‚‚.comp {f : Ξ± β†’ Ξ² β†’ Ξ³} {g : Ξ³ β†’ Ξ΄} (hg : uniform_continuous g) (hf : uniform_continuousβ‚‚ f) : uniform_continuousβ‚‚ (g βˆ˜β‚‚ f) := hg.comp hf lemma uniform_continuousβ‚‚.bicompl {f : Ξ± β†’ Ξ² β†’ Ξ³} {ga : Ξ΄ β†’ Ξ±} {gb : Ξ΄' β†’ Ξ²} (hf : uniform_continuousβ‚‚ f) (hga : uniform_continuous ga) (hgb : uniform_continuous gb) : uniform_continuousβ‚‚ (bicompl f ga gb) := hf.uniform_continuous.comp (hga.prod_map hgb) end lemma to_topological_space_subtype [u : uniform_space Ξ±] {p : Ξ± β†’ Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space Ξ± p u.to_topological_space := rfl section sum variables [uniform_space Ξ±] [uniform_space Ξ²] open sum /-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained by taking independently an entourage of the diagonal in the first part, and an entourage of the diagonal in the second part. -/ def uniform_space.core.sum : uniform_space.core (Ξ± βŠ• Ξ²) := uniform_space.core.mk' (map (Ξ» p : Ξ± Γ— Ξ±, (inl p.1, inl p.2)) (𝓀 Ξ±) βŠ” map (Ξ» p : Ξ² Γ— Ξ², (inr p.1, inr p.2)) (𝓀 Ξ²)) (Ξ» r ⟨H₁, Hβ‚‚βŸ© x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity Hβ‚‚]) (Ξ» r ⟨H₁, Hβ‚‚βŸ©, ⟨symm_le_uniformity H₁, symm_le_uniformity Hβ‚‚βŸ©) (Ξ» r ⟨HrΞ±, Hrβ⟩, begin rcases comp_mem_uniformity_sets HrΞ± with ⟨tΞ±, htΞ±, Htα⟩, rcases comp_mem_uniformity_sets HrΞ² with ⟨tΞ², htΞ², Htβ⟩, refine ⟨_, ⟨mem_map_sets_iff.2 ⟨tΞ±, htΞ±, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨tΞ², htΞ², subset_union_right _ _⟩⟩, _⟩, rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩, ⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩, { have A : (a, c) ∈ tΞ± β—‹ tΞ± := ⟨b, hab, hbc⟩, exact HtΞ± A }, { have A : (a, c) ∈ tΞ² β—‹ tΞ² := ⟨b, hab, hbc⟩, exact HtΞ² A } end) /-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage of the diagonal. -/ lemma union_mem_uniformity_sum {a : set (Ξ± Γ— Ξ±)} (ha : a ∈ 𝓀 Ξ±) {b : set (Ξ² Γ— Ξ²)} (hb : b ∈ 𝓀 Ξ²) : ((Ξ» p : (Ξ± Γ— Ξ±), (inl p.1, inl p.2)) '' a βˆͺ (Ξ» p : (Ξ² Γ— Ξ²), (inr p.1, inr p.2)) '' b) ∈ (@uniform_space.core.sum Ξ± Ξ² _ _).uniformity := ⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩ /- To prove that the topology defined by the uniform structure on the disjoint union coincides with the disjoint union topology, we need two lemmas saying that open sets can be characterized by the uniform structure -/ lemma uniformity_sum_of_open_aux {s : set (Ξ± βŠ• Ξ²)} (hs : is_open s) {x : Ξ± βŠ• Ξ²} (xs : x ∈ s) : { p : ((Ξ± βŠ• Ξ²) Γ— (Ξ± βŠ• Ξ²)) | p.1 = x β†’ p.2 ∈ s } ∈ (@uniform_space.core.sum Ξ± Ξ² _ _).uniformity := begin cases x, { refine mem_sets_of_superset (union_mem_uniformity_sum (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.1 xs)) univ_mem_sets) (union_subset _ _); rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, { refine mem_sets_of_superset (union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.2 xs))) (union_subset _ _); rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, end lemma open_of_uniformity_sum_aux {s : set (Ξ± βŠ• Ξ²)} (hs : βˆ€x ∈ s, { p : ((Ξ± βŠ• Ξ²) Γ— (Ξ± βŠ• Ξ²)) | p.1 = x β†’ p.2 ∈ s } ∈ (@uniform_space.core.sum Ξ± Ξ² _ _).uniformity) : is_open s := begin split, { refine (@is_open_iff_mem_nhds Ξ± _ _).2 (Ξ» a ha, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }, { refine (@is_open_iff_mem_nhds Ξ² _ _).2 (Ξ» b hb, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl } end /- We can now define the uniform structure on the disjoint union -/ instance sum.uniform_space : uniform_space (Ξ± βŠ• Ξ²) := { to_core := uniform_space.core.sum, is_open_uniformity := Ξ» s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ } lemma sum.uniformity : 𝓀 (Ξ± βŠ• Ξ²) = map (Ξ» p : Ξ± Γ— Ξ±, (inl p.1, inl p.2)) (𝓀 Ξ±) βŠ” map (Ξ» p : Ξ² Γ— Ξ², (inr p.1, inr p.2)) (𝓀 Ξ²) := rfl end sum end constructions -- For a version of the Lebesgue number lemma assuming only a sequentially compact space, -- see topology/sequences.lean /-- Let `c : ΞΉ β†’ set Ξ±` be an open cover of a compact set `s`. Then there exists an entourage `n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `c i`. -/ lemma lebesgue_number_lemma {Ξ± : Type u} [uniform_space Ξ±] {s : set Ξ±} {ΞΉ} {c : ΞΉ β†’ set Ξ±} (hs : is_compact s) (hc₁ : βˆ€ i, is_open (c i)) (hcβ‚‚ : s βŠ† ⋃ i, c i) : βˆƒ n ∈ 𝓀 Ξ±, βˆ€ x ∈ s, βˆƒ i, {y | (x, y) ∈ n} βŠ† c i := begin let u := Ξ» n, {x | βˆƒ i (m ∈ 𝓀 Ξ±), {y | (x, y) ∈ m β—‹ n} βŠ† c i}, have hu₁ : βˆ€ n ∈ 𝓀 Ξ±, is_open (u n), { refine Ξ» n hn, is_open_uniformity.2 _, rintro x ⟨i, m, hm, h⟩, rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩, apply (𝓀 Ξ±).sets_of_superset hm', rintros ⟨x, y⟩ hp rfl, refine ⟨i, m', hm', Ξ» z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩, dsimp at hz ⊒, rw comp_rel_assoc, exact ⟨y, hp, hz⟩ }, have huβ‚‚ : s βŠ† ⋃ n ∈ 𝓀 Ξ±, u n, { intros x hx, rcases mem_Union.1 (hcβ‚‚ hx) with ⟨i, h⟩, rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩, exact mem_bUnion hm' ⟨i, _, hm', Ξ» y hy, mm' hy rfl⟩ }, rcases hs.elim_finite_subcover_image hu₁ huβ‚‚ with ⟨b, bu, b_fin, b_cover⟩, refine ⟨_, (bInter_mem_sets b_fin).2 bu, Ξ» x hx, _⟩, rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩, refine ⟨i, Ξ» y hy, h _⟩, exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy) end /-- Let `c : set (set Ξ±)` be an open cover of a compact set `s`. Then there exists an entourage `n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `t ∈ c`. -/ lemma lebesgue_number_lemma_sUnion {Ξ± : Type u} [uniform_space Ξ±] {s : set Ξ±} {c : set (set Ξ±)} (hs : is_compact s) (hc₁ : βˆ€ t ∈ c, is_open t) (hcβ‚‚ : s βŠ† ⋃₀ c) : βˆƒ n ∈ 𝓀 Ξ±, βˆ€ x ∈ s, βˆƒ t ∈ c, βˆ€ y, (x, y) ∈ n β†’ y ∈ t := by rw sUnion_eq_Union at hcβ‚‚; simpa using lebesgue_number_lemma hs (by simpa) hcβ‚‚ /-! ### Expressing continuity properties in uniform spaces We reformulate the various continuity properties of functions taking values in a uniform space in terms of the uniformity in the target. Since the same lemmas (essentially with the same names) also exist for metric spaces and emetric spaces (reformulating things in terms of the distance or the edistance in the target), we put them in a namespace `uniform` here. In the metric and emetric space setting, there are also similar lemmas where one assumes that both the source and the target are metric spaces, reformulating things in terms of the distance on both sides. These lemmas are generally written without primes, and the versions where only the target is a metric space is primed. We follow the same convention here, thus giving lemmas with primes. -/ namespace uniform variables [uniform_space Ξ±] theorem tendsto_nhds_right {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} : tendsto u f (𝓝 a) ↔ tendsto (Ξ» x, (a, u x)) f (𝓀 Ξ±) := ⟨λ H, tendsto_left_nhds_uniformity.comp H, Ξ» H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_right.1 hs)⟩ theorem tendsto_nhds_left {f : filter Ξ²} {u : Ξ² β†’ Ξ±} {a : Ξ±} : tendsto u f (𝓝 a) ↔ tendsto (Ξ» x, (u x, a)) f (𝓀 Ξ±) := ⟨λ H, tendsto_right_nhds_uniformity.comp H, Ξ» H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_left.1 hs)⟩ theorem continuous_at_iff'_right [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {b : Ξ²} : continuous_at f b ↔ tendsto (Ξ» x, (f b, f x)) (𝓝 b) (𝓀 Ξ±) := by rw [continuous_at, tendsto_nhds_right] theorem continuous_at_iff'_left [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {b : Ξ²} : continuous_at f b ↔ tendsto (Ξ» x, (f x, f b)) (𝓝 b) (𝓀 Ξ±) := by rw [continuous_at, tendsto_nhds_left] theorem continuous_at_iff_prod [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {b : Ξ²} : continuous_at f b ↔ tendsto (Ξ» x : Ξ² Γ— Ξ², (f x.1, f x.2)) (𝓝 (b, b)) (𝓀 Ξ±) := ⟨λ H, le_trans (H.prod_map' H) (nhds_le_uniformity _), Ξ» H, continuous_at_iff'_left.2 $ H.comp $ tendsto_id.prod_mk_nhds tendsto_const_nhds⟩ theorem continuous_within_at_iff'_right [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {b : Ξ²} {s : set Ξ²} : continuous_within_at f s b ↔ tendsto (Ξ» x, (f b, f x)) (𝓝[s] b) (𝓀 Ξ±) := by rw [continuous_within_at, tendsto_nhds_right] theorem continuous_within_at_iff'_left [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {b : Ξ²} {s : set Ξ²} : continuous_within_at f s b ↔ tendsto (Ξ» x, (f x, f b)) (𝓝[s] b) (𝓀 Ξ±) := by rw [continuous_within_at, tendsto_nhds_left] theorem continuous_on_iff'_right [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {s : set Ξ²} : continuous_on f s ↔ βˆ€ b ∈ s, tendsto (Ξ» x, (f b, f x)) (𝓝[s] b) (𝓀 Ξ±) := by simp [continuous_on, continuous_within_at_iff'_right] theorem continuous_on_iff'_left [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {s : set Ξ²} : continuous_on f s ↔ βˆ€ b ∈ s, tendsto (Ξ» x, (f x, f b)) (𝓝[s] b) (𝓀 Ξ±) := by simp [continuous_on, continuous_within_at_iff'_left] theorem continuous_iff'_right [topological_space Ξ²] {f : Ξ² β†’ Ξ±} : continuous f ↔ βˆ€ b, tendsto (Ξ» x, (f b, f x)) (𝓝 b) (𝓀 Ξ±) := continuous_iff_continuous_at.trans $ forall_congr $ Ξ» b, tendsto_nhds_right theorem continuous_iff'_left [topological_space Ξ²] {f : Ξ² β†’ Ξ±} : continuous f ↔ βˆ€ b, tendsto (Ξ» x, (f x, f b)) (𝓝 b) (𝓀 Ξ±) := continuous_iff_continuous_at.trans $ forall_congr $ Ξ» b, tendsto_nhds_left end uniform lemma filter.tendsto.congr_uniformity {Ξ± Ξ²} [uniform_space Ξ²] {f g : Ξ± β†’ Ξ²} {l : filter Ξ±} {b : Ξ²} (hf : tendsto f l (𝓝 b)) (hg : tendsto (Ξ» x, (f x, g x)) l (𝓀 Ξ²)) : tendsto g l (𝓝 b) := uniform.tendsto_nhds_right.2 $ (uniform.tendsto_nhds_right.1 hf).uniformity_trans hg lemma uniform.tendsto_congr {Ξ± Ξ²} [uniform_space Ξ²] {f g : Ξ± β†’ Ξ²} {l : filter Ξ±} {b : Ξ²} (hfg : tendsto (Ξ» x, (f x, g x)) l (𝓀 Ξ²)) : tendsto f l (𝓝 b) ↔ tendsto g l (𝓝 b) := ⟨λ h, h.congr_uniformity hfg, Ξ» h, h.congr_uniformity hfg.uniformity_symm⟩
90c30e7dfc5c3b1729fb9fe1896efbde9712c0d6
367134ba5a65885e863bdc4507601606690974c1
/archive/100-theorems-list/83_friendship_graphs.lean
36fcc870f2daaa7177f1892142659d4fcd8dd438
[ "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
13,831
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson, Jalex Stark, Kyle Miller. -/ import combinatorics.simple_graph.adj_matrix import linear_algebra.char_poly.coeff import data.int.modeq import data.zmod.basic import tactic.interval_cases /-! # The Friendship Theorem ## Definitions and Statement - A `friendship` graph is one in which any two distinct vertices have exactly one neighbor in common - A `politician`, at least in the context of this problem, is a vertex in a graph which is adjacent to every other vertex. - The friendship theorem (ErdΕ‘s, RΓ©nyi, SΓ³s 1966) states that every finite friendship graph has a politician. ## Proof outline The proof revolves around the theory of adjacency matrices, although some steps could equivalently be phrased in terms of counting walks. - Assume `G` is a finite friendship graph. - First we show that any two nonadjacent vertices have the same degree - Assume for contradiction that `G` does not have a politician. - Conclude from the last two points that `G` is `d`-regular for some `d : β„•`. - Show that `G` has `d ^ 2 - d + 1` vertices. - By casework, show that if `d = 0, 1, 2`, then `G` has a politician. - If `3 ≀ d`, let `p` be a prime factor of `d - 1`. - If `A` is the adjacency matrix of `G` with entries in `β„€/pβ„€`, we show that `A ^ p` has trace `1`. - This gives a contradiction, as `A` has trace `0`, and thus `A ^ p` has trace `0`. ## References - [P. ErdΕ‘s, A. RΓ©nyi, V. SΓ³s, *On A Problem of Graph Theory*][erdosrenyisos] - [C. Huneke, *The Friendship Theorem*][huneke2002] -/ open_locale classical big_operators noncomputable theory open finset simple_graph matrix universes u v variables {V : Type u} {R : Type v} [semiring R] section friendship_def variables (G : simple_graph V) /-- This property of a graph is the hypothesis of the friendship theorem: every pair of nonadjacent vertices has exactly one common friend, a vertex to which both are adjacent. -/ def friendship [fintype V] : Prop := βˆ€ ⦃v w : V⦄, v β‰  w β†’ fintype.card (G.common_neighbors v w) = 1 /-- A politician is a vertex that is adjacent to all other vertices. -/ def exists_politician : Prop := βˆƒ (v : V), βˆ€ (w : V), v β‰  w β†’ G.adj v w end friendship_def variables [fintype V] {G : simple_graph V} {d : β„•} (hG : friendship G) include hG namespace friendship variables (R) /-- One characterization of a friendship graph is that there is exactly one walk of length 2 between distinct vertices. These walks are counted in off-diagonal entries of the square of the adjacency matrix, so for a friendship graph, those entries are all 1. -/ theorem adj_matrix_sq_of_ne {v w : V} (hvw : v β‰  w) : ((G.adj_matrix R) ^ 2) v w = 1 := begin rw [pow_two, ← nat.cast_one, ← hG hvw], simp [common_neighbors, neighbor_finset_eq_filter, finset.filter_filter, finset.filter_inter, and_comm], end /-- This calculation amounts to counting the number of length 3 walks between nonadjacent vertices. We use it to show that nonadjacent vertices have equal degrees. -/ lemma adj_matrix_pow_three_of_not_adj {v w : V} (non_adj : Β¬ G.adj v w) : ((G.adj_matrix R) ^ 3) v w = degree G v := begin rw [pow_succ, mul_eq_mul, adj_matrix_mul_apply, degree, card_eq_sum_ones, sum_nat_cast], apply sum_congr rfl, intros x hx, rw [adj_matrix_sq_of_ne _ hG, nat.cast_one], rintro ⟨rfl⟩, rw mem_neighbor_finset at hx, exact non_adj hx, end variable {R} /-- As `v` and `w` not being adjacent implies `degree G v = ((G.adj_matrix R) ^ 3) v w` and `degree G w = ((G.adj_matrix R) ^ 3) v w`, the degrees are equal if `((G.adj_matrix R) ^ 3) v w = ((G.adj_matrix R) ^ 3) w v` This is true as the adjacency matrix is symmetric. -/ lemma degree_eq_of_not_adj {v w : V} (hvw : Β¬ G.adj v w) : degree G v = degree G w := begin rw [← nat.cast_id (G.degree v), ← nat.cast_id (G.degree w), ← adj_matrix_pow_three_of_not_adj β„• hG hvw, ← adj_matrix_pow_three_of_not_adj β„• hG (Ξ» h, hvw (G.sym h))], conv_lhs {rw ← transpose_adj_matrix}, simp only [pow_succ, pow_two, mul_eq_mul, ← transpose_mul, transpose_apply], simp only [← mul_eq_mul, mul_assoc], end /-- Let `A` be the adjacency matrix of a graph `G`. If `G` is a friendship graph, then all of the off-diagonal entries of `A^2` are 1. If `G` is `d`-regular, then all of the diagonal entries of `A^2` are `d`. Putting these together determines `A^2` exactly for a `d`-regular friendship graph. -/ theorem adj_matrix_sq_of_regular (hd : G.is_regular_of_degree d) : ((G.adj_matrix R) ^ 2) = Ξ» v w, if v = w then d else 1 := begin ext v w, by_cases h : v = w, { rw [h, pow_two, mul_eq_mul, adj_matrix_mul_self_apply_self, hd], simp, }, { rw [adj_matrix_sq_of_ne R hG h, if_neg h], }, end lemma adj_matrix_sq_mod_p_of_regular {p : β„•} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) ^ 2 = Ξ» _ _, 1 := by simp [adj_matrix_sq_of_regular hG hd, dmod] section nonempty variable [nonempty V] /-- If `G` is a friendship graph without a politician (a vertex adjacent to all others), then it is regular. We have shown that nonadjacent vertices of a friendship graph have the same degree, and if there isn't a politician, we can show this for adjacent vertices by finding a vertex neither is adjacent to, and then using transitivity. -/ theorem is_regular_of_not_exists_politician (hG' : Β¬exists_politician G) : βˆƒ (d : β„•), G.is_regular_of_degree d := begin have v := classical.arbitrary V, use G.degree v, intro x, by_cases hvx : G.adj v x, swap, { exact (degree_eq_of_not_adj hG hvx).symm, }, dunfold exists_politician at hG', push_neg at hG', rcases hG' v with ⟨w, hvw', hvw⟩, rcases hG' x with ⟨y, hxy', hxy⟩, by_cases hxw : G.adj x w, swap, { rw degree_eq_of_not_adj hG hvw, exact degree_eq_of_not_adj hG hxw }, rw degree_eq_of_not_adj hG hxy, by_cases hvy : G.adj v y, swap, { exact (degree_eq_of_not_adj hG hvy).symm }, rw degree_eq_of_not_adj hG hvw, apply degree_eq_of_not_adj hG, intro hcontra, rcases finset.card_eq_one.mp (hG hvw') with ⟨⟨a, ha⟩, h⟩, have key : βˆ€ {x}, x ∈ G.common_neighbors v w β†’ x = a, { intros x hx, have h' := mem_univ (subtype.mk x hx), rw [h, mem_singleton] at h', injection h', }, apply hxy', rw [key ((mem_common_neighbors G).mpr ⟨hvx, G.sym hxw⟩), key ((mem_common_neighbors G).mpr ⟨hvy, G.sym hcontra⟩)], end /-- Let `A` be the adjacency matrix of a `d`-regular friendship graph, and let `v` be a vector all of whose components are `1`. Then `v` is an eigenvector of `A ^ 2`, and we can compute the eigenvalue to be `d * d`, or as `d + (fintype.card V - 1)`, so those quantities must be equal. This essentially means that the graph has `d ^ 2 - d + 1` vertices. -/ lemma card_of_regular (hd : G.is_regular_of_degree d) : d + (fintype.card V - 1) = d * d := begin have v := classical.arbitrary V, transitivity ((G.adj_matrix β„•) ^ 2).mul_vec (Ξ» _, 1) v, { rw [adj_matrix_sq_of_regular hG hd, mul_vec, dot_product, ← insert_erase (mem_univ v)], simp only [sum_insert, mul_one, if_true, nat.cast_id, eq_self_iff_true, mem_erase, not_true, ne.def, not_false_iff, add_right_inj, false_and], rw [finset.sum_const_nat, card_erase_of_mem (mem_univ v), mul_one], { refl }, intros x hx, simp [(ne_of_mem_erase hx).symm], }, { rw [pow_two, mul_eq_mul, ← mul_vec_mul_vec], simp [adj_matrix_mul_vec_const_apply_of_regular hd, neighbor_finset, card_neighbor_set_eq_degree, hd v], } end /-- The size of a `d`-regular friendship graph is `1 mod (d-1)`, and thus `1 mod p` for a factor `p ∣ d-1`. -/ lemma card_mod_p_of_regular {p : β„•} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (fintype.card V : zmod p) = 1 := begin have hpos : 0 < fintype.card V := fintype.card_pos_iff.mpr infer_instance, rw [← nat.succ_pred_eq_of_pos hpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], have h := congr_arg (Ξ» n, (↑n : zmod p)) (card_of_regular hG hd), revert h, simp [dmod], end end nonempty omit hG lemma adj_matrix_sq_mul_const_one_of_regular (hd : G.is_regular_of_degree d) : (G.adj_matrix R) * (Ξ» _ _, 1) = Ξ» _ _, d := by { ext x, simp [← hd x, degree] } lemma adj_matrix_mul_const_one_mod_p_of_regular {p : β„•} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) * (Ξ» _ _, 1) = Ξ» _ _, 1 := by rw [adj_matrix_sq_mul_const_one_of_regular hd, dmod] include hG /-- Modulo a factor of `d-1`, the square and all higher powers of the adjacency matrix of a `d`-regular friendship graph reduce to the matrix whose entries are all 1. -/ lemma adj_matrix_pow_mod_p_of_regular {p : β„•} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) {k : β„•} (hk : 2 ≀ k) : (G.adj_matrix (zmod p)) ^ k = Ξ» _ _, 1 := begin iterate 2 {cases k with k, { exfalso, linarith, }, }, induction k with k hind, { exact adj_matrix_sq_mod_p_of_regular hG dmod hd, }, have h2 : 2 ≀ k.succ.succ := by omega, rw [pow_succ, hind h2], exact adj_matrix_mul_const_one_mod_p_of_regular dmod hd, end variable [nonempty V] /-- This is the main proof. Assuming that `3 ≀ d`, we take `p` to be a prime factor of `d-1`. Then the `p`th power of the adjacency matrix of a `d`-regular friendship graph must have trace 1 mod `p`, but we can also show that the trace must be the `p`th power of the trace of the original adjacency matrix, which is 0, a contradiction. -/ lemma false_of_three_le_degree (hd : G.is_regular_of_degree d) (h : 3 ≀ d) : false := begin -- get a prime factor of d - 1 let p : β„• := (d - 1).min_fac, have p_dvd_d_pred := (zmod.nat_coe_zmod_eq_zero_iff_dvd _ _).mpr (d - 1).min_fac_dvd, have dpos : 0 < d := by linarith, have d_cast : ↑(d - 1) = (d : β„€) - 1 := by norm_cast, haveI : fact p.prime := nat.min_fac_prime (by linarith), have hp2 : 2 ≀ p, { apply nat.prime.two_le, assumption }, have dmod : (d : zmod p) = 1, { rw [← nat.succ_pred_eq_of_pos dpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], exact p_dvd_d_pred, }, have Vmod := card_mod_p_of_regular hG dmod hd, -- now we reduce to a trace calculation have := zmod.trace_pow_card (G.adj_matrix (zmod p)), contrapose! this, clear this, -- the trace is 0 mod p when computed one way rw [trace_adj_matrix, zero_pow], swap, { apply nat.prime.pos, assumption, }, -- but the trace is 1 mod p when computed the other way rw adj_matrix_pow_mod_p_of_regular hG dmod hd hp2, dunfold fintype.card at Vmod, simp only [matrix.trace, diag_apply, mul_one, nsmul_eq_mul, linear_map.coe_mk, sum_const], rw [Vmod, ← nat.cast_one, zmod.nat_coe_zmod_eq_zero_iff_dvd, nat.dvd_one, nat.min_fac_eq_one_iff], linarith, end /-- If `d ≀ 1`, a `d`-regular friendship graph has at most one vertex, which is trivially a politician. -/ lemma exists_politician_of_degree_le_one (hd : G.is_regular_of_degree d) (hd1 : d ≀ 1) : exists_politician G := begin have sq : d * d = d := by { interval_cases d; norm_num }, have h := card_of_regular hG hd, rw sq at h, have : fintype.card V ≀ 1, { cases fintype.card V with n, { exact zero_le _, }, { have : n = 0, { rw [nat.succ_sub_succ_eq_sub, nat.sub_zero] at h, linarith }, subst n, } }, use classical.arbitrary V, intros w h, exfalso, apply h, apply fintype.card_le_one_iff.mp this, end /-- If `d = 2`, a `d`-regular friendship graph has 3 vertices, so it must be complete graph, and all the vertices are politicians. -/ lemma neighbor_finset_eq_of_degree_eq_two (hd : G.is_regular_of_degree 2) (v : V) : G.neighbor_finset v = finset.univ.erase v := begin apply finset.eq_of_subset_of_card_le, { rw finset.subset_iff, intro x, rw [mem_neighbor_finset, finset.mem_erase], exact Ξ» h, ⟨(G.ne_of_adj h).symm, finset.mem_univ _⟩ }, convert_to 2 ≀ _, { convert_to _ = fintype.card V - 1, { have hfr:= card_of_regular hG hd, linarith }, { exact finset.card_erase_of_mem (finset.mem_univ _), }, }, { dsimp [is_regular_of_degree, degree] at hd, rw hd, } end lemma exists_politician_of_degree_eq_two (hd : G.is_regular_of_degree 2) : exists_politician G := begin have v := classical.arbitrary V, use v, intros w hvw, rw [← mem_neighbor_finset, neighbor_finset_eq_of_degree_eq_two hG hd v, finset.mem_erase], exact ⟨hvw.symm, finset.mem_univ _⟩, end lemma exists_politician_of_degree_le_two (hd : G.is_regular_of_degree d) (h : d ≀ 2) : exists_politician G := begin interval_cases d, iterate 2 { apply exists_politician_of_degree_le_one hG hd, norm_num }, { exact exists_politician_of_degree_eq_two hG hd }, end end friendship /-- We wish to show that a friendship graph has a politician (a vertex adjacent to all others). We proceed by contradiction, and assume the graph has no politician. We have already proven that a friendship graph with no politician is `d`-regular for some `d`, and now we do casework on `d`. If the degree is at most 2, we observe by casework that it has a politician anyway. If the degree is at least 3, the graph cannot exist. -/ theorem friendship_theorem [nonempty V] : exists_politician G := begin by_contradiction npG, rcases hG.is_regular_of_not_exists_politician npG with ⟨d, dreg⟩, have h : d ≀ 2 ∨ 3 ≀ d := by omega, cases h with dle2 dge3, { exact npG (hG.exists_politician_of_degree_le_two dreg dle2) }, { exact hG.false_of_three_le_degree dreg dge3 }, end
d46889f34deaf585f9dd6a7e393b8aa1c59a05e7
b561a44b48979a98df50ade0789a21c79ee31288
/src/Lean/Elab/Inductive.lean
13b80894dc96cc9b5130a0cd97d5ce77e4f91974
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,028
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.ReplaceLevel import Lean.Util.ReplaceExpr import Lean.Util.CollectLevelParams import Lean.Meta.Constructions import Lean.Meta.CollectFVars import Lean.Meta.SizeOf import Lean.Meta.Injective import Lean.Meta.IndPredBelow import Lean.Elab.Command import Lean.Elab.DefView import Lean.Elab.DeclUtil import Lean.Elab.Deriving.Basic namespace Lean.Elab.Command open Meta builtin_initialize registerTraceClass `Elab.inductive def checkValidInductiveModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in inductive declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in inductive declaration" unless modifiers.attrs.size == 0 || (modifiers.attrs.size == 1 && modifiers.attrs[0].name == `class) do throwError "invalid use of attributes in inductive declaration" def checkValidCtorModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in constructor declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in constructor declaration" if modifiers.isUnsafe then throwError "invalid use of 'unsafe' in constructor declaration" if modifiers.attrs.size != 0 then throwError "invalid use of attributes in constructor declaration" structure CtorView where ref : Syntax modifiers : Modifiers inferMod : Bool -- true if `{}` is used in the constructor declaration declName : Name binders : Syntax type? : Option Syntax deriving Inhabited structure InductiveView where ref : Syntax modifiers : Modifiers shortDeclName : Name declName : Name levelNames : List Name binders : Syntax type? : Option Syntax ctors : Array CtorView derivingClasses : Array DerivingClassView deriving Inhabited structure ElabHeaderResult where view : InductiveView lctx : LocalContext localInsts : LocalInstances params : Array Expr type : Expr deriving Inhabited private partial def elabHeaderAux (views : Array InductiveView) (i : Nat) (acc : Array ElabHeaderResult) : TermElabM (Array ElabHeaderResult) := do if h : i < views.size then let view := views.get ⟨i, h⟩ let acc ← Term.withAutoBoundImplicit <| Term.elabBinders view.binders.getArgs fun params => do match view.type? with | none => let u ← mkFreshLevelMVar let type := mkSort u Term.synthesizeSyntheticMVarsNoPostponing let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } | some typeStx => let type ← Term.elabType typeStx unless (← isTypeFormerType type) do throwErrorAt typeStx "invalid inductive type, resultant type is not a sort" Term.synthesizeSyntheticMVarsNoPostponing let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } elabHeaderAux views (i+1) acc else pure acc private def checkNumParams (rs : Array ElabHeaderResult) : TermElabM Nat := do let numParams := rs[0].params.size for r in rs do unless r.params.size == numParams do throwErrorAt r.view.ref "invalid inductive type, number of parameters mismatch in mutually inductive datatypes" pure numParams private def checkUnsafe (rs : Array ElabHeaderResult) : TermElabM Unit := do let isUnsafe := rs[0].view.modifiers.isUnsafe for r in rs do unless r.view.modifiers.isUnsafe == isUnsafe do throwErrorAt r.view.ref "invalid inductive type, cannot mix unsafe and safe declarations in a mutually inductive datatypes" private def checkLevelNames (views : Array InductiveView) : TermElabM Unit := do if views.size > 1 then let levelNames := views[0].levelNames for view in views do unless view.levelNames == levelNames do throwErrorAt view.ref "invalid inductive type, universe parameters mismatch in mutually inductive datatypes" private def mkTypeFor (r : ElabHeaderResult) : TermElabM Expr := do withLCtx r.lctx r.localInsts do mkForallFVars r.params r.type private def throwUnexpectedInductiveType {Ξ±} : TermElabM Ξ± := throwError "unexpected inductive resulting type" private def eqvFirstTypeResult (firstType type : Expr) : MetaM Bool := forallTelescopeReducing firstType fun _ firstTypeResult => isDefEq firstTypeResult type -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkParamsAndResultType (type firstType : Expr) (numParams : Nat) : TermElabM Unit := do try forallTelescopeCompatible type firstType numParams fun _ type firstType => forallTelescopeReducing type fun _ type => forallTelescopeReducing firstType fun _ firstType => do let type ← whnfD type match type with | Expr.sort .. => unless (← isDefEq firstType type) do throwError "resulting universe mismatch, given{indentExpr type}\nexpected type{indentExpr firstType}" | _ => throwError "unexpected inductive resulting type" catch | Exception.error ref msg => throw (Exception.error ref m!"invalid mutually inductive types, {msg}") | ex => throw ex -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private def checkHeader (r : ElabHeaderResult) (numParams : Nat) (firstType? : Option Expr) : TermElabM Expr := do let type ← mkTypeFor r match firstType? with | none => pure type | some firstType => withRef r.view.ref $ checkParamsAndResultType type firstType numParams pure firstType -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkHeaders (rs : Array ElabHeaderResult) (numParams : Nat) (i : Nat) (firstType? : Option Expr) : TermElabM Unit := do if i < rs.size then let type ← checkHeader rs[i] numParams firstType? checkHeaders rs numParams (i+1) type private def elabHeader (views : Array InductiveView) : TermElabM (Array ElabHeaderResult) := do let rs ← elabHeaderAux views 0 #[] if rs.size > 1 then checkUnsafe rs let numParams ← checkNumParams rs checkHeaders rs numParams 0 none return rs /- Create a local declaration for each inductive type in `rs`, and execute `x params indFVars`, where `params` are the inductive type parameters and `indFVars` are the new local declarations. We use the local context/instances and parameters of rs[0]. Note that this method is executed after we executed `checkHeaders` and established all parameters are compatible. -/ private partial def withInductiveLocalDecls {Ξ±} (rs : Array ElabHeaderResult) (x : Array Expr β†’ Array Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := do let namesAndTypes ← rs.mapM fun r => do let type ← mkTypeFor r pure (r.view.shortDeclName, type) let r0 := rs[0] let params := r0.params withLCtx r0.lctx r0.localInsts $ withRef r0.view.ref do let rec loop (i : Nat) (indFVars : Array Expr) := do if h : i < namesAndTypes.size then let (id, type) := namesAndTypes.get ⟨i, h⟩ withLocalDeclD id type fun indFVar => loop (i+1) (indFVars.push indFVar) else x params indFVars loop 0 #[] private def isInductiveFamily (numParams : Nat) (indFVar : Expr) : TermElabM Bool := do let indFVarType ← inferType indFVar forallTelescopeReducing indFVarType fun xs _ => return xs.size > numParams /- Elaborate constructor types. Remark: we check whether the resulting type is correct, and the parameter occurrences are consistent, but we currently do not check for: - Positivity (it is a rare failure, and the kernel already checks for it). - Universe constraints (the kernel checks for it). -/ private def elabCtors (indFVars : Array Expr) (indFVar : Expr) (params : Array Expr) (r : ElabHeaderResult) : TermElabM (List Constructor) := withRef r.view.ref do let indFamily ← isInductiveFamily params.size indFVar r.view.ctors.toList.mapM fun ctorView => Term.withAutoBoundImplicit <| Term.elabBinders ctorView.binders.getArgs fun ctorParams => withRef ctorView.ref do let rec elabCtorType (k : Expr β†’ TermElabM Constructor) : TermElabM Constructor := do match ctorView.type? with | none => if indFamily then throwError "constructor resulting type must be specified in inductive family declaration" k <| mkAppN indFVar params | some ctorType => let type ← Term.elabType ctorType Term.synthesizeSyntheticMVars (mayPostpone := true) let type ← instantiateMVars type let type ← checkParamOccs type forallTelescopeReducing type fun _ resultingType => do unless resultingType.getAppFn == indFVar do throwError "unexpected constructor resulting type{indentExpr resultingType}" unless (← isType resultingType) do throwError "unexpected constructor resulting type, type expected{indentExpr resultingType}" k type elabCtorType fun type => do Term.synthesizeSyntheticMVarsNoPostponing let ctorParams ← Term.addAutoBoundImplicits ctorParams let type ← mkForallFVars ctorParams type let type ← mkForallFVars params type return { name := ctorView.declName, type := type } where checkParamOccs (ctorType : Expr) : MetaM Expr := let visit (e : Expr) : MetaM TransformStep := do let f := e.getAppFn if indFVars.contains f then let mut args := e.getAppArgs unless args.size β‰₯ params.size do throwError "unexpected inductive type occurrence{indentExpr e}" for i in [:params.size] do let param := params[i] let arg := args[i] unless (← isDefEq param arg) do throwError "inductive datatype parameter mismatch{indentExpr arg}\nexpected{indentExpr param}" args := args.set! i param return TransformStep.done (mkAppN f args) else return TransformStep.visit e transform ctorType (pre := visit) /- Convert universe metavariables occurring in the `indTypes` into new parameters. Remark: if the resulting inductive datatype has universe metavariables, we will fix it later using `inferResultingUniverse`. -/ private def levelMVarToParamAux (indTypes : List InductiveType) : StateRefT Nat TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← Term.levelMVarToParam' indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← Term.levelMVarToParam' ctor.type pure { ctor with type := ctorType } pure { indType with ctors := ctors, type := type } private def levelMVarToParam (indTypes : List InductiveType) : TermElabM (List InductiveType) := (levelMVarToParamAux indTypes).run' 1 private def getResultingUniverse : List InductiveType β†’ TermElabM Level | [] => throwError "unexpected empty inductive declaration" | indType :: _ => forallTelescopeReducing indType.type fun _ r => do let r ← whnfD r match r with | Expr.sort u _ => pure u | _ => throwError "unexpected inductive type resulting type{indentExpr r}" def tmpIndParam := mkLevelParam `_tmp_ind_univ_param /-- Return true if `u` is of the form `?m + k`. Return false if `u` does not contain universe metavariables. Throw exception otherwise. -/ def shouldInferResultUniverse (u : Level) : TermElabM Bool := do let u ← instantiateLevelMVars u if u.hasMVar then match u.getLevelOffset with | Level.mvar mvarId _ => do Term.assignLevelMVar mvarId tmpIndParam pure true | _ => throwError "cannot infer resulting universe level of inductive datatype, given level contains metavariables {mkSort u}, provide universe explicitly" else pure false /- Auxiliary function for `updateResultingUniverse` `accLevelAtCtor u r rOffset us` add `u` components to `us` if they are not already there and it is different from the resulting universe level `r+rOffset`. If `u` is a `max`, then its components are recursively processed. If `u` is a `succ` and `rOffset > 0`, we process the `u`s child using `rOffset-1`. This method is used to infer the resulting universe level of an inductive datatype. -/ def accLevelAtCtor : Level β†’ Level β†’ Nat β†’ Array Level β†’ TermElabM (Array Level) | Level.max u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.imax u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.zero _, _, _, us => pure us | Level.succ u _, r, rOffset+1, us => accLevelAtCtor u r rOffset us | u, r, rOffset, us => if rOffset == 0 && u == r then pure us else if r.occurs u then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if rOffset > 0 then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if us.contains u then pure us else pure (us.push u) /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorTypeAux (r : Level) (rOffset : Nat) : Nat β†’ Expr β†’ Array Level β†’ TermElabM (Array Level) | 0, Expr.forallE n d b c, us => do let u ← getLevel d let u ← instantiateLevelMVars u let us ← accLevelAtCtor u r rOffset us withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset 0 e us | i+1, Expr.forallE n d b c, us => do withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset i e us | _, _, us => pure us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorType (r : Level) (rOffset : Nat) (ctorType : Expr) (numParams : Nat) (us : Array Level) : TermElabM (Array Level) := collectUniversesFromCtorTypeAux r rOffset numParams ctorType us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniverses (r : Level) (rOffset : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (Array Level) := do let mut us := #[] for indType in indTypes do for ctor in indType.ctors do us ← collectUniversesFromCtorType r rOffset ctor.type numParams us return us def mkResultUniverse (us : Array Level) (rOffset : Nat) : Level := if us.isEmpty && rOffset == 0 then levelOne else let r := Level.mkNaryMax us.toList if rOffset == 0 && !r.isZero && !r.isNeverZero then (mkLevelMax r levelOne).normalize else r.normalize private def updateResultingUniverse (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := do let r ← getResultingUniverse indTypes let rOffset : Nat := r.getOffset let r : Level := r.getLevelOffset unless r.isParam do throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" let us ← collectUniverses r rOffset numParams indTypes trace[Elab.inductive] "updateResultingUniverse us: {us}, r: {r}, rOffset: {rOffset}" let rNew := mkResultUniverse us rOffset let updateLevel (e : Expr) : Expr := e.replaceLevel fun u => if u == tmpIndParam then some rNew else none return indTypes.map fun indType => let type := updateLevel indType.type; let ctors := indType.ctors.map fun ctor => { ctor with type := updateLevel ctor.type }; { indType with type := type, ctors := ctors } register_builtin_option bootstrap.inductiveCheckResultingUniverse : Bool := { defValue := true, group := "bootstrap", descr := "by default the `inductive/structure commands report an error if the resulting universe is not zero, but may be zero for some universe parameters. Reason: unless this type is a subsingleton, it is hardly what the user wants since it can only eliminate into `Prop`. In the `Init` package, we define subsingletons, and we use this option to disable the check. This option may be deleted in the future after we improve the validator" } def checkResultingUniverse (u : Level) : TermElabM Unit := do if bootstrap.inductiveCheckResultingUniverse.get (← getOptions) then let u ← instantiateLevelMVars u if !u.isZero && !u.isNeverZero then throwError "invalid universe polymorphic type, the resultant universe is not Prop (i.e., 0), but it may be Prop for some parameter values (solution: use 'u+1' or 'max 1 u'{indentD u}" private def checkResultingUniverses (indTypes : List InductiveType) : TermElabM Unit := do checkResultingUniverse (← getResultingUniverse indTypes) private def collectUsed (indTypes : List InductiveType) : StateRefT CollectFVars.State MetaM Unit := do indTypes.forM fun indType => do Meta.collectUsedFVars indType.type indType.ctors.forM fun ctor => Meta.collectUsedFVars ctor.type private def removeUnused (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (LocalContext Γ— LocalInstances Γ— Array Expr) := do let (_, used) ← (collectUsed indTypes).run {} Meta.removeUnused vars used private def withUsed {Ξ±} (vars : Array Expr) (indTypes : List InductiveType) (k : Array Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := do let (lctx, localInsts, vars) ← removeUnused vars indTypes withLCtx lctx localInsts $ k vars private def updateParams (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← mkForallFVars vars indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← mkForallFVars vars ctor.type pure { ctor with type := ctorType } pure { indType with type := type, ctors := ctors } private def collectLevelParamsInInductive (indTypes : List InductiveType) : Array Name := do let mut usedParams : CollectLevelParams.State := {} for indType in indTypes do usedParams := collectLevelParams usedParams indType.type for ctor in indType.ctors do usedParams := collectLevelParams usedParams ctor.type return usedParams.params private def mkIndFVar2Const (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) : ExprMap Expr := do let levelParams := levelNames.map mkLevelParam; let mut m : ExprMap Expr := {} for i in [:views.size] do let view := views[i] let indFVar := indFVars[i] m := m.insert indFVar (mkConst view.declName levelParams) return m /- Remark: `numVars <= numParams`. `numVars` is the number of context `variables` used in the inductive declaration, and `numParams` is `numVars` + number of explicit parameters provided in the declaration. -/ private def replaceIndFVarsWithConsts (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) (numVars : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := let indFVar2Const := mkIndFVar2Const views indFVars levelNames indTypes.mapM fun indType => do let ctors ← indType.ctors.mapM fun ctor => do let type ← forallBoundedTelescope ctor.type numParams fun params type => do let type := type.replace fun e => if !e.isFVar then none else match indFVar2Const.find? e with | none => none | some c => mkAppN c (params.extract 0 numVars) mkForallFVars params type pure { ctor with type := type } pure { indType with ctors := ctors } abbrev Ctor2InferMod := Std.HashMap Name Bool private def mkCtor2InferMod (views : Array InductiveView) : Ctor2InferMod := do let mut m := {} for view in views do for ctorView in view.ctors do m := m.insert ctorView.declName ctorView.inferMod return m private def applyInferMod (views : Array InductiveView) (numParams : Nat) (indTypes : List InductiveType) : List InductiveType := let ctor2InferMod := mkCtor2InferMod views indTypes.map fun indType => let ctors := indType.ctors.map fun ctor => let inferMod := ctor2InferMod.find! ctor.name -- true if `{}` was used let ctorType := ctor.type.inferImplicit numParams !inferMod { ctor with type := ctorType } { indType with ctors := ctors } private def mkAuxConstructions (views : Array InductiveView) : TermElabM Unit := do let env ← getEnv let hasEq := env.contains ``Eq let hasHEq := env.contains ``HEq let hasUnit := env.contains ``PUnit let hasProd := env.contains ``Prod for view in views do let n := view.declName mkRecOn n if hasUnit then mkCasesOn n if hasUnit && hasEq && hasHEq then mkNoConfusion n if hasUnit && hasProd then mkBelow n if hasUnit && hasProd then mkIBelow n for view in views do let n := view.declName; if hasUnit && hasProd then mkBRecOn n if hasUnit && hasProd then mkBInductionOn n private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) : TermElabM Unit := do let view0 := views[0] let scopeLevelNames ← Term.getLevelNames checkLevelNames views let allUserLevelNames := view0.levelNames let isUnsafe := view0.modifiers.isUnsafe withRef view0.ref <| Term.withLevelNames allUserLevelNames do let rs ← elabHeader views withInductiveLocalDecls rs fun params indFVars => do let numExplicitParams := params.size let mut indTypesArray := #[] for i in [:views.size] do let indFVar := indFVars[i] let r := rs[i] let type ← mkForallFVars params r.type let ctors ← elabCtors indFVars indFVar params r indTypesArray := indTypesArray.push { name := r.view.declName, type := type, ctors := ctors : InductiveType } let indTypes := indTypesArray.toList Term.synthesizeSyntheticMVarsNoPostponing let u ← getResultingUniverse indTypes let inferLevel ← shouldInferResultUniverse u withUsed vars indTypes fun vars => do let numVars := vars.size let numParams := numVars + numExplicitParams let indTypes ← updateParams vars indTypes let indTypes ← levelMVarToParam indTypes let indTypes ← if inferLevel then updateResultingUniverse numParams indTypes else checkResultingUniverses indTypes; pure indTypes let usedLevelNames := collectLevelParamsInInductive indTypes match sortDeclLevelParams scopeLevelNames allUserLevelNames usedLevelNames with | Except.error msg => throwError msg | Except.ok levelParams => do let indTypes ← replaceIndFVarsWithConsts views indFVars levelParams numVars numParams indTypes let indTypes := applyInferMod views numParams indTypes trace[Meta.debug] "levelParams: {levelParams}" let decl := Declaration.inductDecl levelParams numParams indTypes isUnsafe Term.ensureNoUnassignedMVars decl addDecl decl mkAuxConstructions views -- We need to invoke `applyAttributes` because `class` is implemented as an attribute. for view in views do Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking private def applyDerivingHandlers (views : Array InductiveView) : CommandElabM Unit := do let mut processed : NameSet := {} for view in views do for classView in view.derivingClasses do let className := classView.className unless processed.contains className do processed := processed.insert className let mut declNames := #[] for view in views do if view.derivingClasses.any fun classView => classView.className == className then declNames := declNames.push view.declName classView.applyHandlers declNames def elabInductiveViews (views : Array InductiveView) : CommandElabM Unit := do let view0 := views[0] let ref := view0.ref runTermElabM view0.declName fun vars => withRef ref do mkInductiveDecl vars views mkSizeOfInstances view0.declName Lean.Meta.IndPredBelow.mkBelow view0.declName for view in views do mkInjectiveTheorems view.declName applyDerivingHandlers views end Lean.Elab.Command
efb905f34dc79b15533d1b15836734bf42037967
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/free_module/basic.lean
15a86b4839cb24d9ed9582f8e3b92a2f155fdb55
[ "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,319
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import linear_algebra.direct_sum.finsupp import logic.small import linear_algebra.std_basis import linear_algebra.finsupp_vector_space /-! # Free modules We introduce a class `module.free R M`, for `R` a `semiring` and `M` an `R`-module and we provide several basic instances for this class. Use `finsupp.total_id_surjective` to prove that any module is the quotient of a free module. ## Main definition * `module.free R M` : the class of free `R`-modules. -/ universes u v w z variables {ΞΉ : Type*} (R : Type u) (M : Type v) (N : Type z) open_locale tensor_product direct_sum big_operators section basic variables [semiring R] [add_comm_monoid M] [module R M] /-- `module.free R M` is the statement that the `R`-module `M` is free.-/ class module.free : Prop := (exists_basis [] : nonempty (Ξ£ (I : Type v), basis I R M)) /- If `M` fits in universe `w`, then freeness is equivalent to existence of a basis in that universe. Note that if `M` does not fit in `w`, the reverse direction of this implication is still true as `module.free.of_basis`. -/ lemma module.free_def [small.{w} M] : module.free R M ↔ βˆƒ (I : Type w), nonempty (basis I R M) := ⟨ Ξ» h, ⟨shrink (set.range h.exists_basis.some.2), ⟨(basis.reindex_range h.exists_basis.some.2).reindex (equiv_shrink _)⟩⟩, Ξ» h, ⟨(nonempty_sigma.2 h).map $ Ξ» ⟨i, b⟩, ⟨set.range b, b.reindex_range⟩⟩⟩ lemma module.free_iff_set : module.free R M ↔ βˆƒ (S : set M), nonempty (basis S R M) := ⟨λ h, ⟨set.range h.exists_basis.some.2, ⟨basis.reindex_range h.exists_basis.some.2⟩⟩, Ξ» ⟨S, hS⟩, ⟨nonempty_sigma.2 ⟨S, hS⟩⟩⟩ variables {R M} lemma module.free.of_basis {ΞΉ : Type w} (b : basis ΞΉ R M) : module.free R M := (module.free_def R M).2 ⟨set.range b, ⟨b.reindex_range⟩⟩ end basic namespace module.free section semiring variables (R M) [semiring R] [add_comm_monoid M] [module R M] [module.free R M] variables [add_comm_monoid N] [module R N] /-- If `module.free R M` then `choose_basis_index R M` is the `ΞΉ` which indexes the basis `ΞΉ β†’ M`. -/ @[nolint has_nonempty_instance] def choose_basis_index := (exists_basis R M).some.1 /-- If `module.free R M` then `choose_basis : ΞΉ β†’ M` is the basis. Here `ΞΉ = choose_basis_index R M`. -/ noncomputable def choose_basis : basis (choose_basis_index R M) R M := (exists_basis R M).some.2 /-- The isomorphism `M ≃ₗ[R] (choose_basis_index R M β†’β‚€ R)`. -/ noncomputable def repr : M ≃ₗ[R] (choose_basis_index R M β†’β‚€ R) := (choose_basis R M).repr /-- The universal property of free modules: giving a functon `(choose_basis_index R M) β†’ N`, for `N` an `R`-module, is the same as giving an `R`-linear map `M β†’β‚—[R] N`. This definition is parameterized over an extra `semiring S`, such that `smul_comm_class R S M'` holds. If `R` is commutative, you can set `S := R`; if `R` is not commutative, you can recover an `add_equiv` by setting `S := β„•`. See library note [bundled maps over different rings]. -/ noncomputable def constr {S : Type z} [semiring S] [module S N] [smul_comm_class R S N] : ((choose_basis_index R M) β†’ N) ≃ₗ[S] M β†’β‚—[R] N := basis.constr (choose_basis R M) S @[priority 100] instance no_zero_smul_divisors [no_zero_divisors R] : no_zero_smul_divisors R M := let ⟨⟨_, b⟩⟩ := exists_basis R M in b.no_zero_smul_divisors variables {R M N} lemma of_equiv (e : M ≃ₗ[R] N) : module.free R N := of_basis $ (choose_basis R M).map e /-- A variation of `of_equiv`: the assumption `module.free R P` here is explicit rather than an instance. -/ lemma of_equiv' {P : Type v} [add_comm_monoid P] [module R P] (h : module.free R P) (e : P ≃ₗ[R] N) : module.free R N := of_equiv e variables (R M N) /-- The module structure provided by `semiring.to_module` is free. -/ instance self : module.free R R := of_basis (basis.singleton unit R) instance prod [module.free R N] : module.free R (M Γ— N) := of_basis $ (choose_basis R M).prod (choose_basis R N) /-- The product of finitely many free modules is free. -/ instance pi (M : ΞΉ β†’ Type*) [finite ΞΉ] [Ξ  (i : ΞΉ), add_comm_monoid (M i)] [Ξ  (i : ΞΉ), module R (M i)] [Ξ  (i : ΞΉ), module.free R (M i)] : module.free R (Ξ  i, M i) := let ⟨_⟩ := nonempty_fintype ΞΉ in by exactI (of_basis $ pi.basis $ Ξ» i, choose_basis R (M i)) /-- The module of finite matrices is free. -/ instance matrix {m n : Type*} [finite m] [finite n] : module.free R (matrix m n M) := module.free.pi R _ variables (ΞΉ) /-- The product of finitely many free modules is free (non-dependent version to help with typeclass search). -/ instance function [finite ΞΉ] : module.free R (ΞΉ β†’ M) := free.pi _ _ instance finsupp : module.free R (ΞΉ β†’β‚€ M) := of_basis (finsupp.basis $ Ξ» i, choose_basis R M) variables {ΞΉ} @[priority 100] instance of_subsingleton [subsingleton N] : module.free R N := of_basis (basis.empty N : basis pempty R N) @[priority 100] instance of_subsingleton' [subsingleton R] : module.free R N := by letI := module.subsingleton R N; exact module.free.of_subsingleton R N instance dfinsupp {ΞΉ : Type*} (M : ΞΉ β†’ Type*) [Ξ  (i : ΞΉ), add_comm_monoid (M i)] [Ξ  (i : ΞΉ), module R (M i)] [Ξ  (i : ΞΉ), module.free R (M i)] : module.free R (Ξ β‚€ i, M i) := of_basis $ dfinsupp.basis $ Ξ» i, choose_basis R (M i) instance direct_sum {ΞΉ : Type*} (M : ΞΉ β†’ Type*) [Ξ  (i : ΞΉ), add_comm_monoid (M i)] [Ξ  (i : ΞΉ), module R (M i)] [Ξ  (i : ΞΉ), module.free R (M i)] : module.free R (⨁ i, M i) := module.free.dfinsupp R M end semiring section comm_ring variables [comm_ring R] [add_comm_group M] [module R M] [module.free R M] variables [add_comm_group N] [module R N] [module.free R N] instance tensor : module.free R (M βŠ—[R] N) := of_equiv' (of_equiv' (free.finsupp _ R _) (finsupp_tensor_finsupp' R _ _).symm) (tensor_product.congr (choose_basis R M).repr (choose_basis R N).repr).symm end comm_ring section division_ring variables [division_ring R] [add_comm_group M] [module R M] @[priority 100] instance of_division_ring : module.free R M := of_basis (basis.of_vector_space R M) end division_ring end module.free
2d369c9f33249cba06b25643deb95c2bcc5497fd
6afa22d5eee6e9a56b6a2f1210eca8f7a1067466
/library/init/meta/widget/interactive_expr.lean
65d289dd26a0cc7ff7d487c82edde45a90dd3051
[ "Apache-2.0" ]
permissive
giordano/lean
72a1fabfeb2f1ccfd38673e2719a719cd6ffbb40
56f8877f1efa22215aca0b82f1c0ce2ff975b9c3
refs/heads/master
1,663,091,511,168
1,590,688,082,000
1,590,688,082,000
268,183,678
0
0
Apache-2.0
1,590,885,425,000
1,590,885,424,000
null
UTF-8
Lean
false
false
7,074
lean
/- Copyright (c) E.W.Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: E.W.Ayers -/ prelude import init.meta.widget.basic import init.meta.widget.tactic_component import init.meta.tagged_format import init.data.punit meta def subexpr := (expr Γ— expr.address) namespace widget open tagged_format open html attr namespace interactive_expression /-- eformat but without any of the formatting stuff like highlighting, groups etc. -/ meta inductive sf : Type | tag_expr : expr.address β†’ expr β†’ sf β†’ sf | compose : sf β†’ sf β†’ sf | of_string : string β†’ sf private meta def to_simple : eformat β†’ sf | (tag ⟨ea,e⟩ m) := sf.tag_expr ea e $ to_simple m | (group m) := to_simple m | (nest i m) := to_simple m | (highlight i m) := to_simple m | (of_format f) := sf.of_string $ format.to_string f | (compose x y) := sf.compose (to_simple x) (to_simple y) private meta def sf.flatten : sf β†’ sf | (sf.tag_expr e ea m) := (sf.tag_expr e ea $ sf.flatten m) | (sf.compose x y) := match (sf.flatten x), (sf.flatten y) with | (sf.of_string sx), (sf.of_string sy) := sf.of_string (sx ++ sy) | (sf.of_string sx), (sf.compose (sf.of_string sy) z) := sf.compose (sf.of_string (sx ++ sy)) z | (sf.compose x (sf.of_string sy)), (sf.of_string sz) := sf.compose x (sf.of_string (sy ++ sz)) | (sf.compose x (sf.of_string sy1)), (sf.compose (sf.of_string sy2) z) := sf.compose x (sf.compose (sf.of_string (sy1 ++ sy2)) z) | x, y := sf.compose x y end | (sf.of_string s) := sf.of_string s meta inductive action (Ξ³ : Type) | on_mouse_enter : subexpr β†’ action | on_mouse_leave_all : action | on_click : subexpr β†’ action | on_tooltip_action : Ξ³ β†’ action | on_close_tooltip : action meta def view {Ξ³} (tooltip_component : tc subexpr (action Ξ³)) (click_address : option expr.address) (select_address : option expr.address) : subexpr β†’ sf β†’ tactic (list (html (action Ξ³))) | ⟨ce, current_address⟩ (sf.tag_expr ea e m) := do let new_address := current_address ++ ea, let select_attrs : list (attr (action Ξ³)) := if some new_address = select_address then [className "highlight"] else [], click_attrs : list (attr (action Ξ³)) ← if some new_address = click_address then do content ← tc.to_html tooltip_component (e, new_address), pure [tooltip $ h "div" [] [ h "button" [cn "fr pointer ba br3", on_click (Ξ» _, action.on_close_tooltip)] ["x"], content ]] else pure [], let as := [className "expr-boundary", key (ea)] ++ select_attrs ++ click_attrs, inner ← view (e,new_address) m, pure [h "span" as inner] | ca (sf.compose x y) := pure (++) <*> view ca x <*> view ca y | ca (sf.of_string s) := pure [h "span" [ on_mouse_enter (Ξ» _, action.on_mouse_enter ca), on_click (Ξ» _, action.on_click ca), key s ] [html.of_string s]] /-- Make an interactive expression. -/ meta def mk {Ξ³} (tooltip : tc subexpr Ξ³) : tc expr Ξ³ := let tooltip_comp := component.with_props_eq (Ξ» (x y : tactic_state Γ— expr Γ— expr.address), x.2.2 = y.2.2) $ component.map_action (action.on_tooltip_action) tooltip in tc.mk_simple (action Ξ³) (option subexpr Γ— option subexpr) (Ξ» e, pure $ (none, none)) (Ξ» e ⟨ca, sa⟩ act, pure $ match act with | (action.on_mouse_enter ⟨e, ea⟩) := ((ca, some (e, ea)), none) | (action.on_mouse_leave_all) := ((ca, none), none) | (action.on_click ⟨e, ea⟩) := if some (e,ea) = ca then ((none, sa), none) else ((some (e, ea), sa), none) | (action.on_tooltip_action g) := ((none, sa), some g) | (action.on_close_tooltip) := ((none, sa), none) end ) (Ξ» e ⟨ca, sa⟩, do ts ← tactic.read, let m : sf := sf.flatten $ to_simple $ tactic_state.pp_tagged ts e, let m : sf := sf.tag_expr [] e m, -- [hack] in pp.cpp I forgot to add an expr-boundary for the root expression. v ← view tooltip_comp (prod.snd <$> ca) (prod.snd <$> sa) ⟨e, []⟩ m, pure $ [ h "span" [ className "expr", key e.hash, on_mouse_leave (Ξ» _, action.on_mouse_leave_all) ] $ v ] ) /-- Render the implicit arguments for an expression in fancy, little pills. -/ meta def implicit_arg_list (tooltip : tc subexpr empty) (e : expr) : tactic $ html empty := do fn ← (mk tooltip) $ expr.get_app_fn e, args ← list.mmap (mk tooltip) $ expr.get_app_args e, pure $ h "div" [] ( (h "span" [className "bg-blue br3 ma1 ph2 white"] [fn]) :: list.map (Ξ» a, h "span" [className "bg-gray br3 ma1 ph2 white"] [a]) args ) meta def type_tooltip : tc subexpr empty := tc.stateless (Ξ» ⟨e,ea⟩, do y ← tactic.infer_type e, y_comp ← mk type_tooltip y, implicit_args ← implicit_arg_list type_tooltip e, pure [ h "div" [] [ h "div" [] [y_comp], h "hr" [] [], implicit_args ] ] ) end interactive_expression meta def html.of_name {Ξ± : Type} : name β†’ html Ξ± | n := html.of_string $ name.to_string n open tactic meta def show_type_component : tc expr empty := tc.stateless (Ξ» x, do y ← infer_type x, y_comp ← interactive_expression.mk interactive_expression.type_tooltip $ y, pure y_comp ) meta def tactic_view_component {Ξ³} (local_c : tc expr Ξ³) (target_c : tc expr Ξ³) : tc unit Ξ³ := tc.stateless $ Ξ» _, do gs ← get_goals, hs : list (html Ξ³) ← gs.mmap (Ξ» g, do ts ← read, gn ← pp g, set_goals [g], lcs ← local_context, lchs : list (html Ξ³) ← lcs.mmap (Ξ» lc, do pn ← pure $ expr.local_pp_name lc, lh : html Ξ³ ← local_c lc, pure $ h "tr" [key $ to_string $ expr.local_uniq_name lc] [ h "td" [cn "goal-hyp b"] [html.of_name pn], h "td" [] [html.of_string " : "], h "td" [] [lh] ] ), t_comp ← target_c g, (expr.mvar u_n pp_n y) ← pure g, pure $ h "table" [key $ expr.hash g, className "font-code"] [ h "tbody" [] $ lchs ++ [ h "tr" [key u_n, className "bt"] [ h "td" [] [] , h "td" [cn "goal-vdash b"] [html.of_string " ⊒ "], h "td" [] [t_comp] ]] ] ), set_goals gs, goal_message : string ← pure $ if gs.length = 0 then "goals accomplished" else if gs.length = 1 then "1 goal" else (to_string gs.length) ++ " goals", goal_message : html Ξ³ ← pure $ h "strong" [cn "goal-goals"] [goal_message], pure $ [h "ul" [className "list pl0"] $ list.map_with_index (Ξ» i x, let border_cn := if i < hs.length then "ba bl-0 bt-0 br-0 b--dotted b--black-30" else "" in h "li" [className $ "lh-copy mv3 " ++ border_cn] [x]) $ (goal_message :: hs)] meta def tactic_render : tc unit string := component.ignore_action $ tactic_view_component show_type_component show_type_component meta def tactic_state_widget : component tactic_state string := tc.to_component tactic_render end widget
3fe88d7f8b11ea767f6b45cd58560e53247a27f7
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/order/nonneg.lean
7838dd352b18bc6054af21c61c352a7bc2fa151d
[ "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
12,333
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import algebra.order.archimedean import algebra.order.sub import algebra.order.with_zero import order.lattice_intervals import order.conditionally_complete_lattice /-! # The type of nonnegative elements This file defines instances and prove some properties about the nonnegative elements `{x : Ξ± // 0 ≀ x}` of an arbitrary type `Ξ±`. Currently we only state instances and states some `simp`/`norm_cast` lemmas. When `Ξ±` is `ℝ`, this will give us some properties about `ℝβ‰₯0`. ## Main declarations * `{x : Ξ± // 0 ≀ x}` is a `canonically_linear_ordered_add_monoid` if `Ξ±` is a `linear_ordered_ring`. * `{x : Ξ± // 0 ≀ x}` is a `linear_ordered_comm_group_with_zero` if `Ξ±` is a `linear_ordered_field`. ## Implementation Notes Instead of `{x : Ξ± // 0 ≀ x}` we could also use `set.Ici (0 : Ξ±)`, which is definitionally equal. However, using the explicit subtype has a big advantage: when writing and element explicitly with a proof of nonnegativity as `⟨x, hx⟩`, the `hx` is expected to have type `0 ≀ x`. If we would use `Ici 0`, then the type is expected to be `x ∈ Ici 0`. Although these types are definitionally equal, this often confuses the elaborator. Similar problems arise when doing cases on an element. The disadvantage is that we have to duplicate some instances about `set.Ici` to this subtype. -/ open set variables {Ξ± : Type*} namespace nonneg /-- This instance uses data fields from `subtype.partial_order` to help type-class inference. The `set.Ici` data fields are definitionally equal, but that requires unfolding semireducible definitions, so type-class inference won't see this. -/ instance order_bot [preorder Ξ±] {a : Ξ±} : order_bot {x : Ξ± // a ≀ x} := { ..set.Ici.order_bot } lemma bot_eq [preorder Ξ±] {a : Ξ±} : (βŠ₯ : {x : Ξ± // a ≀ x}) = ⟨a, le_rfl⟩ := rfl instance no_top_order [partial_order Ξ±] [no_top_order Ξ±] {a : Ξ±} : no_top_order {x : Ξ± // a ≀ x} := set.Ici.no_top_order /-- This instance uses data fields from `subtype.partial_order` to help type-class inference. The `set.Ici` data fields are definitionally equal, but that requires unfolding semireducible definitions, so type-class inference won't see this. -/ instance semilattice_inf_bot [semilattice_inf Ξ±] {a : Ξ±} : semilattice_inf_bot {x : Ξ± // a ≀ x} := { ..nonneg.order_bot, ..set.Ici.semilattice_inf_bot } instance densely_ordered [preorder Ξ±] [densely_ordered Ξ±] {a : Ξ±} : densely_ordered {x : Ξ± // a ≀ x} := show densely_ordered (Ici a), from set.densely_ordered /-- If `Sup βˆ… ≀ a` then `{x : Ξ± // a ≀ x}` is a `conditionally_complete_linear_order_bot`. This instance uses data fields from `subtype.linear_order` to help type-class inference. The `set.Ici` data fields are definitionally equal, but that requires unfolding semireducible definitions, so type-class inference won't see this. -/ @[reducible] protected noncomputable def conditionally_complete_linear_order_bot [conditionally_complete_linear_order Ξ±] {a : Ξ±} (h : Sup βˆ… ≀ a) : conditionally_complete_linear_order_bot {x : Ξ± // a ≀ x} := { cSup_empty := (function.funext_iff.1 (@subset_Sup_def Ξ± (set.Ici a) _ ⟨⟨a, le_rfl⟩⟩) βˆ…).trans $ subtype.eq $ by { rw bot_eq, cases h.lt_or_eq with h2 h2, { simp [h2.not_le] }, simp [h2] }, ..nonneg.order_bot, ..(by apply_instance : linear_order {x : Ξ± // a ≀ x}), .. @ord_connected_subset_conditionally_complete_linear_order Ξ± (set.Ici a) _ ⟨⟨a, le_rfl⟩⟩ _ } instance inhabited [preorder Ξ±] {a : Ξ±} : inhabited {x : Ξ± // a ≀ x} := ⟨⟨a, le_rfl⟩⟩ instance has_zero [has_zero Ξ±] [preorder Ξ±] : has_zero {x : Ξ± // 0 ≀ x} := ⟨⟨0, le_rfl⟩⟩ @[simp, norm_cast] protected lemma coe_zero [has_zero Ξ±] [preorder Ξ±] : ((0 : {x : Ξ± // 0 ≀ x}) : Ξ±) = 0 := rfl @[simp] lemma mk_eq_zero [has_zero Ξ±] [preorder Ξ±] {x : Ξ±} (hx : 0 ≀ x) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) = 0 ↔ x = 0 := subtype.ext_iff instance has_add [add_zero_class Ξ±] [preorder Ξ±] [covariant_class Ξ± Ξ± (+) (≀)] : has_add {x : Ξ± // 0 ≀ x} := ⟨λ x y, ⟨x + y, add_nonneg x.2 y.2⟩⟩ @[simp] lemma mk_add_mk [add_zero_class Ξ±] [preorder Ξ±] [covariant_class Ξ± Ξ± (+) (≀)] {x y : Ξ±} (hx : 0 ≀ x) (hy : 0 ≀ y) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) + ⟨y, hy⟩ = ⟨x + y, add_nonneg hx hy⟩ := rfl @[simp, norm_cast] protected lemma coe_add [add_zero_class Ξ±] [preorder Ξ±] [covariant_class Ξ± Ξ± (+) (≀)] (a b : {x : Ξ± // 0 ≀ x}) : ((a + b : {x : Ξ± // 0 ≀ x}) : Ξ±) = a + b := rfl instance ordered_add_comm_monoid [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid {x : Ξ± // 0 ≀ x} := subtype.coe_injective.ordered_add_comm_monoid (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl (Ξ» x y, rfl) instance linear_ordered_add_comm_monoid [linear_ordered_add_comm_monoid Ξ±] : linear_ordered_add_comm_monoid {x : Ξ± // 0 ≀ x} := subtype.coe_injective.linear_ordered_add_comm_monoid (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl (Ξ» x y, rfl) instance ordered_cancel_add_comm_monoid [ordered_cancel_add_comm_monoid Ξ±] : ordered_cancel_add_comm_monoid {x : Ξ± // 0 ≀ x} := subtype.coe_injective.ordered_cancel_add_comm_monoid (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl (Ξ» x y, rfl) instance linear_ordered_cancel_add_comm_monoid [linear_ordered_cancel_add_comm_monoid Ξ±] : linear_ordered_cancel_add_comm_monoid {x : Ξ± // 0 ≀ x} := subtype.coe_injective.linear_ordered_cancel_add_comm_monoid (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl (Ξ» x y, rfl) /-- Coercion `{x : Ξ± // 0 ≀ x} β†’ Ξ±` as a `add_monoid_hom`. -/ def coe_add_monoid_hom [ordered_add_comm_monoid Ξ±] : {x : Ξ± // 0 ≀ x} β†’+ Ξ± := ⟨coe, nonneg.coe_zero, nonneg.coe_add⟩ @[norm_cast] lemma nsmul_coe [ordered_add_comm_monoid Ξ±] (n : β„•) (r : {x : Ξ± // 0 ≀ x}) : ↑(n β€’ r) = n β€’ (r : Ξ±) := nonneg.coe_add_monoid_hom.map_nsmul _ _ instance archimedean [ordered_add_comm_monoid Ξ±] [archimedean Ξ±] : archimedean {x : Ξ± // 0 ≀ x} := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x : Ξ±) (pos_y : (0 : Ξ±) < y) in ⟨n, show (x : Ξ±) ≀ (n β€’ y : {x : Ξ± // 0 ≀ x}), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ instance has_one [ordered_semiring Ξ±] : has_one {x : Ξ± // 0 ≀ x} := { one := ⟨1, zero_le_one⟩ } @[simp, norm_cast] protected lemma coe_one [ordered_semiring Ξ±] : ((1 : {x : Ξ± // 0 ≀ x}) : Ξ±) = 1 := rfl @[simp] lemma mk_eq_one [ordered_semiring Ξ±] {x : Ξ±} (hx : 0 ≀ x) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) = 1 ↔ x = 1 := subtype.ext_iff instance has_mul [ordered_semiring Ξ±] : has_mul {x : Ξ± // 0 ≀ x} := { mul := Ξ» x y, ⟨x * y, mul_nonneg x.2 y.2⟩ } @[simp, norm_cast] protected lemma coe_mul [ordered_semiring Ξ±] (a b : {x : Ξ± // 0 ≀ x}) : ((a * b : {x : Ξ± // 0 ≀ x}) : Ξ±) = a * b := rfl @[simp] lemma mk_mul_mk [ordered_semiring Ξ±] {x y : Ξ±} (hx : 0 ≀ x) (hy : 0 ≀ y) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) * ⟨y, hy⟩ = ⟨x * y, mul_nonneg hx hy⟩ := rfl instance ordered_semiring [ordered_semiring Ξ±] : ordered_semiring {x : Ξ± // 0 ≀ x} := subtype.coe_injective.ordered_semiring (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl rfl (Ξ» x y, rfl) (Ξ» x y, rfl) instance ordered_comm_semiring [ordered_comm_semiring Ξ±] : ordered_comm_semiring {x : Ξ± // 0 ≀ x} := subtype.coe_injective.ordered_comm_semiring (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl rfl (Ξ» x y, rfl) (Ξ» x y, rfl) -- These prevent noncomputable instances being found, as it does not require `linear_order` which -- is frequently non-computable. instance monoid_with_zero [ordered_semiring Ξ±] : monoid_with_zero {x : Ξ± // 0 ≀ x} := by apply_instance instance comm_monoid_with_zero [ordered_comm_semiring Ξ±] : comm_monoid_with_zero {x : Ξ± // 0 ≀ x} := by apply_instance instance nontrivial [linear_ordered_semiring Ξ±] : nontrivial {x : Ξ± // 0 ≀ x} := ⟨ ⟨0, 1, Ξ» h, zero_ne_one (congr_arg subtype.val h)⟩ ⟩ instance linear_ordered_semiring [linear_ordered_semiring Ξ±] : linear_ordered_semiring {x : Ξ± // 0 ≀ x} := subtype.coe_injective.linear_ordered_semiring (coe : {x : Ξ± // 0 ≀ x} β†’ Ξ±) rfl rfl (Ξ» x y, rfl) (Ξ» x y, rfl) instance linear_ordered_comm_monoid_with_zero [linear_ordered_comm_ring Ξ±] : linear_ordered_comm_monoid_with_zero {x : Ξ± // 0 ≀ x} := { mul_le_mul_left := Ξ» a b h c, mul_le_mul_of_nonneg_left h c.2, ..nonneg.linear_ordered_semiring, ..nonneg.ordered_comm_semiring } /-- Coercion `{x : Ξ± // 0 ≀ x} β†’ Ξ±` as a `ring_hom`. -/ def coe_ring_hom [ordered_semiring Ξ±] : {x : Ξ± // 0 ≀ x} β†’+* Ξ± := ⟨coe, nonneg.coe_one, nonneg.coe_mul, nonneg.coe_zero, nonneg.coe_add⟩ instance has_inv [linear_ordered_field Ξ±] : has_inv {x : Ξ± // 0 ≀ x} := { inv := Ξ» x, ⟨x⁻¹, inv_nonneg.mpr x.2⟩ } @[simp, norm_cast] protected lemma coe_inv [linear_ordered_field Ξ±] (a : {x : Ξ± // 0 ≀ x}) : ((a⁻¹ : {x : Ξ± // 0 ≀ x}) : Ξ±) = a⁻¹ := rfl @[simp] lemma inv_mk [linear_ordered_field Ξ±] {x : Ξ±} (hx : 0 ≀ x) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x})⁻¹ = ⟨x⁻¹, inv_nonneg.mpr hx⟩ := rfl instance linear_ordered_comm_group_with_zero [linear_ordered_field Ξ±] : linear_ordered_comm_group_with_zero {x : Ξ± // 0 ≀ x} := { inv_zero := by { ext, exact inv_zero }, mul_inv_cancel := by { intros a ha, ext, refine mul_inv_cancel (mt (Ξ» h, _) ha), ext, exact h }, ..nonneg.nontrivial, ..nonneg.has_inv, ..nonneg.linear_ordered_comm_monoid_with_zero } instance has_div [linear_ordered_field Ξ±] : has_div {x : Ξ± // 0 ≀ x} := { div := Ξ» x y, ⟨x / y, div_nonneg x.2 y.2⟩ } @[simp, norm_cast] protected lemma coe_div [linear_ordered_field Ξ±] (a b : {x : Ξ± // 0 ≀ x}) : ((a / b : {x : Ξ± // 0 ≀ x}) : Ξ±) = a / b := rfl @[simp] lemma mk_div_mk [linear_ordered_field Ξ±] {x y : Ξ±} (hx : 0 ≀ x) (hy : 0 ≀ y) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) / ⟨y, hy⟩ = ⟨x / y, div_nonneg hx hy⟩ := rfl instance canonically_ordered_add_monoid [ordered_ring Ξ±] : canonically_ordered_add_monoid {x : Ξ± // 0 ≀ x} := { le_iff_exists_add := Ξ» ⟨a, ha⟩ ⟨b, hb⟩, by simpa only [mk_add_mk, subtype.exists, subtype.mk_eq_mk] using le_iff_exists_nonneg_add a b, ..nonneg.ordered_add_comm_monoid, ..nonneg.order_bot } instance canonically_ordered_comm_semiring [ordered_comm_ring Ξ±] [no_zero_divisors Ξ±] : canonically_ordered_comm_semiring {x : Ξ± // 0 ≀ x} := { eq_zero_or_eq_zero_of_mul_eq_zero := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, simp }, ..nonneg.canonically_ordered_add_monoid, ..nonneg.ordered_comm_semiring } instance canonically_linear_ordered_add_monoid [linear_ordered_ring Ξ±] : canonically_linear_ordered_add_monoid {x : Ξ± // 0 ≀ x} := { ..subtype.linear_order _, ..nonneg.canonically_ordered_add_monoid } section linear_order variables [has_zero Ξ±] [linear_order Ξ±] /-- The function `a ↦ max a 0` of type `Ξ± β†’ {x : Ξ± // 0 ≀ x}`. -/ def to_nonneg (a : Ξ±) : {x : Ξ± // 0 ≀ x} := ⟨max a 0, le_max_right _ _⟩ @[simp] lemma coe_to_nonneg {a : Ξ±} : (to_nonneg a : Ξ±) = max a 0 := rfl @[simp] lemma to_nonneg_of_nonneg {a : Ξ±} (h : 0 ≀ a) : to_nonneg a = ⟨a, h⟩ := by simp [to_nonneg, h] @[simp] lemma to_nonneg_coe {a : {x : Ξ± // 0 ≀ x}} : to_nonneg (a : Ξ±) = a := by { cases a with a ha, exact to_nonneg_of_nonneg ha } @[simp] lemma to_nonneg_le {a : Ξ±} {b : {x : Ξ± // 0 ≀ x}} : to_nonneg a ≀ b ↔ a ≀ b := by { cases b with b hb, simp [to_nonneg, hb] } @[simp] lemma to_nonneg_lt {a : {x : Ξ± // 0 ≀ x}} {b : Ξ±} : a < to_nonneg b ↔ ↑a < b := by { cases a with a ha, simp [to_nonneg, ha.not_lt] } instance has_sub [has_sub Ξ±] : has_sub {x : Ξ± // 0 ≀ x} := ⟨λ x y, to_nonneg (x - y)⟩ @[simp] lemma mk_sub_mk [has_sub Ξ±] {x y : Ξ±} (hx : 0 ≀ x) (hy : 0 ≀ y) : (⟨x, hx⟩ : {x : Ξ± // 0 ≀ x}) - ⟨y, hy⟩ = to_nonneg (x - y) := rfl end linear_order instance has_ordered_sub [linear_ordered_ring Ξ±] : has_ordered_sub {x : Ξ± // 0 ≀ x} := ⟨by { rintro ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, simp only [sub_le_iff_le_add, subtype.mk_le_mk, mk_sub_mk, mk_add_mk, to_nonneg_le, subtype.coe_mk]}⟩ end nonneg
e6fd6e31825adb425b4b9c885aab800ca1b99ddf
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/real/irrational.lean
f97077b2d096317683cff5a18903bd333ede06dc
[ "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,255
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Abhimanyu Pallavi Sudhir, Jean Lo, Calle SΓΆnne, Yury Kudryashov. -/ import data.real.basic import data.rat.sqrt import algebra.gcd_domain import ring_theory.multiplicity /-! # Irrational real numbers In this file we define a predicate `irrational` on `ℝ`, prove that the `n`-th root of an integer number is irrational if it is not integer, and that `sqrt q` is irrational if and only if `rat.sqrt q * rat.sqrt q β‰  q ∧ 0 ≀ q`. We also provide dot-style constructors like `irrational.add_rat`, `irrational.rat_sub` etc. -/ open rat real multiplicity /-- A real number is irrational if it is not equal to any rational number. -/ def irrational (x : ℝ) := x βˆ‰ set.range (coe : β„š β†’ ℝ) /-! ### Irrationality of roots of integer and rational numbers -/ /-- If `x^n`, `n > 0`, is integer and is not the `n`-th power of an integer, then `x` is irrational. -/ theorem irrational_nrt_of_notint_nrt {x : ℝ} (n : β„•) (m : β„€) (hxr : x ^ n = m) (hv : Β¬ βˆƒ y : β„€, x = y) (hnpos : 0 < n) : irrational x := begin rintros ⟨⟨N, D, P, C⟩, rfl⟩, rw [← cast_pow] at hxr, have c1 : ((D : β„€) : ℝ) β‰  0, { rw [int.cast_ne_zero, int.coe_nat_ne_zero], exact ne_of_gt P }, have c2 : ((D : β„€) : ℝ) ^ n β‰  0 := pow_ne_zero _ c1, rw [num_denom', cast_pow, cast_mk, div_pow, div_eq_iff_mul_eq c2, ← int.cast_pow, ← int.cast_pow, ← int.cast_mul, int.cast_inj] at hxr, have hdivn : ↑D ^ n ∣ N ^ n := dvd.intro_left m hxr, rw [← int.dvd_nat_abs, ← int.coe_nat_pow, int.coe_nat_dvd, int.nat_abs_pow, nat.pow_dvd_pow_iff hnpos] at hdivn, have hD : D = 1 := by rw [← nat.gcd_eq_right hdivn, C.gcd_eq_one], subst D, refine hv ⟨N, _⟩, rw [num_denom', int.coe_nat_one, mk_eq_div, int.cast_one, div_one, cast_coe_int] end /-- If `x^n = m` is an integer and `n` does not divide the `multiplicity p m`, then `x` is irrational. -/ theorem irrational_nrt_of_n_not_dvd_multiplicity {x : ℝ} (n : β„•) {m : β„€} (hm : m β‰  0) (p : β„•) [hp : fact p.prime] (hxr : x ^ n = m) (hv : (multiplicity (p : β„€) m).get (finite_int_iff.2 ⟨hp.ne_one, hm⟩) % n β‰  0) : irrational x := begin rcases nat.eq_zero_or_pos n with rfl | hnpos, { rw [eq_comm, pow_zero, ← int.cast_one, int.cast_inj] at hxr, simpa [hxr, multiplicity.one_right (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.not_dvd_one)), nat.zero_mod] using hv }, refine irrational_nrt_of_notint_nrt _ _ hxr _ hnpos, rintro ⟨y, rfl⟩, rw [← int.cast_pow, int.cast_inj] at hxr, subst m, have : y β‰  0, { rintro rfl, rw zero_pow hnpos at hm, exact hm rfl }, erw [multiplicity.pow' (nat.prime_iff_prime_int.1 hp) (finite_int_iff.2 ⟨hp.ne_one, this⟩), nat.mul_mod_right] at hv, exact hv rfl end theorem irrational_sqrt_of_multiplicity_odd (m : β„€) (hm : 0 < m) (p : β„•) [hp : fact p.prime] (Hpv : (multiplicity (p : β„€) m).get (finite_int_iff.2 ⟨hp.ne_one, (ne_of_lt hm).symm⟩) % 2 = 1) : irrational (sqrt m) := @irrational_nrt_of_n_not_dvd_multiplicity _ 2 _ (ne.symm (ne_of_lt hm)) p hp (sqr_sqrt (int.cast_nonneg.2 $ le_of_lt hm)) (by rw Hpv; exact one_ne_zero) theorem nat.prime.irrational_sqrt {p : β„•} (hp : nat.prime p) : irrational (sqrt p) := @irrational_sqrt_of_multiplicity_odd p (int.coe_nat_pos.2 hp.pos) p hp $ by simp [multiplicity_self (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.not_dvd_one) : _)]; refl theorem irrational_sqrt_two : irrational (sqrt 2) := by simpa using nat.prime_two.irrational_sqrt theorem irrational_sqrt_rat_iff (q : β„š) : irrational (sqrt q) ↔ rat.sqrt q * rat.sqrt q β‰  q ∧ 0 ≀ q := if H1 : rat.sqrt q * rat.sqrt q = q then iff_of_false (not_not_intro ⟨rat.sqrt q, by rw [← H1, cast_mul, sqrt_mul_self (cast_nonneg.2 $ rat.sqrt_nonneg q), sqrt_eq, abs_of_nonneg (rat.sqrt_nonneg q)]⟩) (Ξ» h, h.1 H1) else if H2 : 0 ≀ q then iff_of_true (Ξ» ⟨r, hr⟩, H1 $ (exists_mul_self _).1 ⟨r, by rwa [eq_comm, sqrt_eq_iff_mul_self_eq (cast_nonneg.2 H2), ← cast_mul, cast_inj] at hr; rw [← hr]; exact real.sqrt_nonneg _⟩) ⟨H1, H2⟩ else iff_of_false (not_not_intro ⟨0, by rw cast_zero; exact (sqrt_eq_zero_of_nonpos (rat.cast_nonpos.2 $ le_of_not_le H2)).symm⟩) (Ξ» h, H2 h.2) instance (q : β„š) : decidable (irrational (sqrt q)) := decidable_of_iff' _ (irrational_sqrt_rat_iff q) /-! ### Adding/subtracting/multiplying by rational numbers -/ lemma rat.not_irrational (q : β„š) : Β¬irrational q := Ξ» h, h ⟨q, rfl⟩ namespace irrational variables (q : β„š) {x y : ℝ} open_locale classical theorem add_cases : irrational (x + y) β†’ irrational x ∨ irrational y := begin delta irrational, contrapose!, rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩, exact ⟨rx + ry, cast_add rx ry⟩ end theorem of_rat_add (h : irrational (q + x)) : irrational x := h.add_cases.elim (Ξ» h, absurd h q.not_irrational) id theorem rat_add (h : irrational x) : irrational (q + x) := of_rat_add (-q) $ by rwa [cast_neg, neg_add_cancel_left] theorem of_add_rat : irrational (x + q) β†’ irrational x := add_comm ↑q x β–Έ of_rat_add q theorem add_rat (h : irrational x) : irrational (x + q) := add_comm ↑q x β–Έ h.rat_add q theorem of_neg (h : irrational (-x)) : irrational x := Ξ» ⟨q, hx⟩, h ⟨-q, by rw [cast_neg, hx]⟩ protected theorem neg (h : irrational x) : irrational (-x) := of_neg $ by rwa neg_neg theorem sub_rat (h : irrational x) : irrational (x - q) := by simpa only [cast_neg] using h.add_rat (-q) theorem rat_sub (h : irrational x) : irrational (q - x) := h.neg.rat_add q theorem of_sub_rat (h : irrational (x - q)) : irrational x := of_add_rat (-q) $ by simpa only [cast_neg] theorem of_rat_sub (h : irrational (q - x)) : irrational x := (h.of_rat_add _).of_neg theorem mul_cases : irrational (x * y) β†’ irrational x ∨ irrational y := begin delta irrational, contrapose!, rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩, exact ⟨rx * ry, cast_mul rx ry⟩ end theorem of_mul_rat (h : irrational (x * q)) : irrational x := h.mul_cases.elim id (Ξ» h, absurd h q.not_irrational) theorem mul_rat (h : irrational x) {q : β„š} (hq : q β‰  0) : irrational (x * q) := of_mul_rat q⁻¹ $ by rwa [mul_assoc, ← cast_mul, mul_inv_cancel hq, cast_one, mul_one] theorem of_rat_mul : irrational (q * x) β†’ irrational x := mul_comm x q β–Έ of_mul_rat q theorem rat_mul (h : irrational x) {q : β„š} (hq : q β‰  0) : irrational (q * x) := mul_comm x q β–Έ h.mul_rat hq theorem of_mul_self (h : irrational (x * x)) : irrational x := h.mul_cases.elim id id theorem of_inv (h : irrational x⁻¹) : irrational x := Ξ» ⟨q, hq⟩, h $ hq β–Έ ⟨q⁻¹, q.cast_inv⟩ protected theorem inv (h : irrational x) : irrational x⁻¹ := of_inv $ by rwa inv_inv' theorem div_cases (h : irrational (x / y)) : irrational x ∨ irrational y := h.mul_cases.imp id of_inv theorem of_rat_div (h : irrational (q / x)) : irrational x := (h.of_rat_mul q).of_inv theorem of_one_div (h : irrational (1 / x)) : irrational x := of_rat_div 1 $ by rwa [cast_one] theorem of_pow : βˆ€ n : β„•, irrational (x^n) β†’ irrational x | 0 := Ξ» h, (h ⟨1, cast_one⟩).elim | (n+1) := Ξ» h, h.mul_cases.elim id (of_pow n) theorem of_fpow : βˆ€ m : β„€, irrational (x^m) β†’ irrational x | (n:β„•) := of_pow n | -[1+n] := Ξ» h, by { rw fpow_neg_succ_of_nat at h, exact h.of_inv.of_pow _ } end irrational section variables {q : β„š} {x : ℝ} open irrational @[simp] theorem irrational_rat_add_iff : irrational (q + x) ↔ irrational x := ⟨of_rat_add q, rat_add q⟩ @[simp] theorem irrational_add_rat_iff : irrational (x + q) ↔ irrational x := ⟨of_add_rat q, add_rat q⟩ @[simp] theorem irrational_rat_sub_iff : irrational (q - x) ↔ irrational x := ⟨of_rat_sub q, rat_sub q⟩ @[simp] theorem irrational_sub_rat_iff : irrational (x - q) ↔ irrational x := ⟨of_sub_rat q, sub_rat q⟩ @[simp] theorem irrational_neg_iff : irrational (-x) ↔ irrational x := ⟨of_neg, irrational.neg⟩ @[simp] theorem irrational_inv_iff : irrational x⁻¹ ↔ irrational x := ⟨of_inv, irrational.inv⟩ end
db9e8f30f001336fb2b79805544fd390fae260fe
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/groupes/cpge_groupe_3_a.lean
cef54c1c24ab71cfe93f7f56547224753942d3f3
[]
no_license
ahayat16/lean_exos
d4f08c30adb601a06511a71b5ffb4d22d12ef77f
682f2552d5b04a8c8eb9e4ab15f875a91b03845c
refs/heads/main
1,693,101,073,585
1,636,479,336,000
1,636,479,336,000
415,000,441
0
0
null
null
null
null
UTF-8
Lean
false
false
214
lean
import data.complex.basic import algebra.field theorem cpge_group_3_a {G1 : Type*} [group G1] {G2 : Type*} [group G2] (H : (subgroup G1)) (f : (G1 β†’* G2) ) : is_subgroup (f '' H) := sorry
a0e8fe100217862a13e9984f306d6d232289a105
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/limits/opposites.lean
981f62eb307d5cfd7972f1098c81a8920725ac6b
[ "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
4,507
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Floris van Doorn -/ import category_theory.limits.shapes.products import category_theory.discrete_category universes v u noncomputable theory open category_theory open category_theory.functor open opposite namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {J : Type v} [small_category J] variable (F : J β₯€ Cα΅’α΅–) /-- If `F.left_op : Jα΅’α΅– β₯€ C` has a colimit, we can construct a limit for `F : J β₯€ Cα΅’α΅–`. -/ lemma has_limit_of_has_colimit_left_op [has_colimit F.left_op] : has_limit F := has_limit.mk { cone := cone_of_cocone_left_op (colimit.cocone F.left_op), is_limit := { lift := Ξ» s, (colimit.desc F.left_op (cocone_left_op_of_cone s)).op, fac' := Ξ» s j, begin rw [cone_of_cocone_left_op_Ο€_app, colimit.cocone_ΞΉ, ←op_comp, colimit.ΞΉ_desc, cocone_left_op_of_cone_ΞΉ_app, has_hom.hom.op_unop], refl, end, uniq' := Ξ» s m w, begin -- It's a pity we can't do this automatically. -- Usually something like this would work by limit.hom_ext, -- but the opposites get in the way of this firing. have u := (colimit.is_colimit F.left_op).uniq (cocone_left_op_of_cone s) (m.unop), convert congr_arg (Ξ» f : _ ⟢ _, f.op) (u _), clear u, intro j, rw [cocone_left_op_of_cone_ΞΉ_app, colimit.cocone_ΞΉ], convert congr_arg (Ξ» f : _ ⟢ _, f.unop) (w (unop j)), clear w, rw [cone_of_cocone_left_op_Ο€_app, colimit.cocone_ΞΉ, has_hom.hom.unop_op], refl, end } } /-- If `C` has colimits of shape `Jα΅’α΅–`, we can construct limits in `Cα΅’α΅–` of shape `J`. -/ lemma has_limits_of_shape_op_of_has_colimits_of_shape [has_colimits_of_shape Jα΅’α΅– C] : has_limits_of_shape J Cα΅’α΅– := { has_limit := Ξ» F, has_limit_of_has_colimit_left_op F } local attribute [instance] has_limits_of_shape_op_of_has_colimits_of_shape /-- If `C` has colimits, we can construct limits for `Cα΅’α΅–`. -/ lemma has_limits_op_of_has_colimits [has_colimits C] : has_limits Cα΅’α΅– := { has_limits_of_shape := Ξ» J π’₯, by { resetI, apply_instance } } /-- If `F.left_op : Jα΅’α΅– β₯€ C` has a limit, we can construct a colimit for `F : J β₯€ Cα΅’α΅–`. -/ lemma has_colimit_of_has_limit_left_op [has_limit F.left_op] : has_colimit F := has_colimit.mk { cocone := cocone_of_cone_left_op (limit.cone F.left_op), is_colimit := { desc := Ξ» s, (limit.lift F.left_op (cone_left_op_of_cocone s)).op, fac' := Ξ» s j, begin rw [cocone_of_cone_left_op_ΞΉ_app, limit.cone_Ο€, ←op_comp, limit.lift_Ο€, cone_left_op_of_cocone_Ο€_app, has_hom.hom.op_unop], refl, end, uniq' := Ξ» s m w, begin have u := (limit.is_limit F.left_op).uniq (cone_left_op_of_cocone s) (m.unop), convert congr_arg (Ξ» f : _ ⟢ _, f.op) (u _), clear u, intro j, rw [cone_left_op_of_cocone_Ο€_app, limit.cone_Ο€], convert congr_arg (Ξ» f : _ ⟢ _, f.unop) (w (unop j)), clear w, rw [cocone_of_cone_left_op_ΞΉ_app, limit.cone_Ο€, has_hom.hom.unop_op], refl, end } } /-- If `C` has colimits of shape `Jα΅’α΅–`, we can construct limits in `Cα΅’α΅–` of shape `J`. -/ lemma has_colimits_of_shape_op_of_has_limits_of_shape [has_limits_of_shape Jα΅’α΅– C] : has_colimits_of_shape J Cα΅’α΅– := { has_colimit := Ξ» F, has_colimit_of_has_limit_left_op F } local attribute [instance] has_colimits_of_shape_op_of_has_limits_of_shape /-- If `C` has limits, we can construct colimits for `Cα΅’α΅–`. -/ lemma has_colimits_op_of_has_limits [has_limits C] : has_colimits Cα΅’α΅– := { has_colimits_of_shape := Ξ» J π’₯, by { resetI, apply_instance } } variables (X : Type v) /-- If `C` has products indexed by `X`, then `Cα΅’α΅–` has coproducts indexed by `X`. -/ lemma has_coproducts_opposite [has_products_of_shape X C] : has_coproducts_of_shape X Cα΅’α΅– := begin haveI : has_limits_of_shape (discrete X)α΅’α΅– C := has_limits_of_shape_of_equivalence (discrete.opposite X).symm, apply_instance end /-- If `C` has coproducts indexed by `X`, then `Cα΅’α΅–` has products indexed by `X`. -/ lemma has_products_opposite [has_coproducts_of_shape X C] : has_products_of_shape X Cα΅’α΅– := begin haveI : has_colimits_of_shape (discrete X)α΅’α΅– C := has_colimits_of_shape_of_equivalence (discrete.opposite X).symm, apply_instance end end category_theory.limits
929b6faa834192c6fbda585b8585015b30fecaa3
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/limits/limits.lean
f75d4673222f4d13c1164a17911d8987d14effc3
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
43,221
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.limits.cones import category_theory.adjunction.basic open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation -- See the notes at the top of cones.lean, explaining why we can't allow `J : Prop` here. variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [category.{v} C] variables {F : J β₯€ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. -/ @[nolint has_inhabited_instance] structure is_limit (t : cone F) := (lift : Ξ  (s : cone F), s.X ⟢ t.X) (fac' : βˆ€ (s : cone F) (j : J), lift s ≫ t.Ο€.app j = s.Ο€.app j . obviously) (uniq' : βˆ€ (s : cone F) (m : s.X ⟢ t.X) (w : βˆ€ j : J, m ≫ t.Ο€.app j = s.Ο€.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp, reassoc] is_limit.fac restate_axiom is_limit.uniq' namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ /-- The universal morphism from any other cone to a limit cone. -/ def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟢ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟢ t} : f = f' := have βˆ€ {g : s ⟢ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm /-- Alternative constructor for `is_limit`, providing a morphism of cones rather than a morphism between the cone points and separately the factorisation condition. -/ def mk_cone_morphism {t : cone F} (lift : Ξ  (s : cone F), s ⟢ t) (uniq' : βˆ€ (s : cone F) (m : s ⟢ t), m = lift s) : is_limit t := { lift := Ξ» s, (lift s).hom, uniq' := Ξ» s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s β‰… t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } /-- Limits of `F` are unique up to isomorphism. -/ -- We may later want to prove the coherence of these isomorphisms. def cone_point_unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s.X β‰… t.X := (cones.forget F).map_iso (unique_up_to_iso P Q) /-- Transport evidence that a cone is a limit cone across an isomorphism of cones. -/ def of_iso_limit {r t : cone F} (P : is_limit r) (i : r β‰… t) : is_limit t := is_limit.mk_cone_morphism (Ξ» s, P.lift_cone_morphism s ≫ i.hom) (Ξ» s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟢ t.X) : m = h.lift { X := W, Ο€ := { app := Ξ» b, m ≫ t.Ο€.app b } } := h.uniq { X := W, Ο€ := { app := Ξ» b, m ≫ t.Ο€.app b } } m (Ξ» b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟢ t.X} (w : βˆ€ j, f ≫ t.Ο€.app j = f' ≫ t.Ο€.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- The universal property of a limit cone: a map `W ⟢ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟢ t.X) β‰… ((const J).obj W ⟢ F) := { hom := Ξ» f, (t.extend f).Ο€, inv := Ξ» Ο€, h.lift { X := W, Ο€ := Ο€ }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟢ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).Ο€ := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X β‰… F.cones := nat_iso.of_components (Ξ» W, is_limit.hom_iso h (unop W)) (by tidy). /-- Another, more explicit, formulation of the universal property of a limit cone. See also `hom_iso`. -/ def hom_iso' (h : is_limit t) (W : C) : ((W ⟢ t.X) : Type v) β‰… { p : Ξ  j, W ⟢ F.obj j // βˆ€ {j j'} (f : j ⟢ j'), p j ≫ F.map f = p j' } := h.hom_iso W β‰ͺ≫ { hom := Ξ» Ο€, ⟨λ j, Ο€.app j, Ξ» j j' f, by convert ←(Ο€.naturality f).symm; apply id_comp⟩, inv := Ξ» p, { app := Ξ» j, p.1 j, naturality' := Ξ» j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C β†’ D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v} D] (G : C β₯€ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Ξ  (s : cone F), s.X ⟢ t.X) (h : βˆ€ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := Ξ» s j, by apply G.map_injective; rw [G.map_comp, h]; apply ht.fac, uniq' := Ξ» s m w, begin apply G.map_injective, rw h, refine ht.uniq (G.map_cone s) _ (Ξ» j, _), convert ←congr_arg (Ξ» f, G.map f) (w j), apply G.map_comp end } /-- If `F` and `G` are naturally isomorphic, then `F.map_cone c` being a limit implies `G.map_cone c` is also a limit. -/ def map_cone_equiv {D : Type u'} [category.{v} D] {K : J β₯€ C} {F G : C β₯€ D} (h : F β‰… G) {c : cone K} (t : is_limit (F.map_cone c)) : is_limit (G.map_cone c) := { lift := Ξ» s, t.lift ((cones.postcompose (iso_whisker_left K h).inv).obj s) ≫ h.hom.app c.X, fac' := Ξ» s j, begin slice_lhs 2 3 {erw ← h.hom.naturality (c.Ο€.app j)}, slice_lhs 1 2 {erw t.fac ((cones.postcompose (iso_whisker_left K h).inv).obj s) j}, dsimp, slice_lhs 2 3 {rw nat_iso.inv_hom_id_app}, rw category.comp_id, end, uniq' := Ξ» s m J, begin rw ← cancel_mono (h.inv.app c.X), apply t.hom_ext, intro j, dsimp, slice_lhs 2 3 {erw ← h.inv.naturality (c.Ο€.app j)}, slice_lhs 1 2 {erw J j}, conv_rhs {congr, rw [category.assoc, nat_iso.hom_inv_id_app, comp_id]}, apply (t.fac ((cones.postcompose (iso_whisker_left K h).inv).obj s) j).symm end } /-- A cone is a limit cone exactly if there is a unique cone morphism from any other cone. -/ def iso_unique_cone_morphism {t : cone F} : is_limit t β‰… Ξ  s, unique (s ⟢ t) := { hom := Ξ» h s, { default := h.lift_cone_morphism s, uniq := Ξ» _, h.uniq_cone_morphism }, inv := Ξ» h, { lift := Ξ» s, (h s).default.hom, uniq' := Ξ» s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } /-- Given two functors which have equivalent categories of cones, we can transport a limiting cone across the equivalence. -/ def of_cone_equiv {D : Type u'} [category.{v} D] {G : K β₯€ D} (h : cone G β₯€ cone F) [is_right_adjoint h] {c : cone G} (t : is_limit c) : is_limit (h.obj c) := mk_cone_morphism (Ξ» s, (adjunction.of_right_adjoint h).hom_equiv s c (t.lift_cone_morphism _)) (Ξ» s m, (adjunction.eq_hom_equiv_apply _ _ _).2 t.uniq_cone_morphism ) namespace of_nat_iso variables {X : C} (h : yoneda.obj X β‰… F.cones) /-- If `F.cones` is represented by `X`, each morphism `f : Y ⟢ X` gives a cone with cone point `Y`. -/ def cone_of_hom {Y : C} (f : Y ⟢ X) : cone F := { X := Y, Ο€ := h.hom.app (op Y) f } /-- If `F.cones` is represented by `X`, each cone `s` gives a morphism `s.X ⟢ X`. -/ def hom_of_cone (s : cone F) : s.X ⟢ X := h.inv.app (op s.X) s.Ο€ @[simp] lemma cone_of_hom_of_cone (s : cone F) : cone_of_hom h (hom_of_cone h s) = s := begin dsimp [cone_of_hom, hom_of_cone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) (op s_X)) s_Ο€, end @[simp] lemma hom_of_cone_of_hom {Y : C} (f : Y ⟢ X) : hom_of_cone h (cone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) (op Y)) f /-- If `F.cones` is represented by `X`, the cone corresponding to the identity morphism on `X` will be a limit cone. -/ def limit_cone : cone F := cone_of_hom h (πŸ™ X) /-- If `F.cones` is represented by `X`, the cone corresponding to a morphism `f : Y ⟢ X` is the limit cone extended by `f`. -/ lemma cone_of_hom_fac {Y : C} (f : Y ⟢ X) : cone_of_hom h f = (limit_cone h).extend f := begin dsimp [cone_of_hom, limit_cone, cone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f.op) (πŸ™ X), dsimp at t, simp only [comp_id] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cones` is represented by `X`, any cone is the extension of the limit cone by the corresponding morphism. -/ lemma cone_fac (s : cone F) : (limit_cone h).extend (hom_of_cone h s) = s := begin rw ←cone_of_hom_of_cone h s, conv_lhs { simp only [hom_of_cone_of_hom] }, apply (cone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cones` is representable, then the cone corresponding to the identity morphism on the representing object is a limit cone. -/ def of_nat_iso {X : C} (h : yoneda.obj X β‰… F.cones) : is_limit (limit_cone h) := { lift := Ξ» s, hom_of_cone h s, fac' := Ξ» s j, begin have h := cone_fac h s, cases s, injection h with h₁ hβ‚‚, simp only [heq_iff_eq] at hβ‚‚, conv_rhs { rw ← hβ‚‚ }, refl, end, uniq' := Ξ» s m w, begin rw ←hom_of_cone_of_hom h m, congr, rw cone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_limit /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. -/ @[nolint has_inhabited_instance] structure is_colimit (t : cocone F) := (desc : Ξ  (s : cocone F), t.X ⟢ s.X) (fac' : βˆ€ (s : cocone F) (j : J), t.ΞΉ.app j ≫ desc s = s.ΞΉ.app j . obviously) (uniq' : βˆ€ (s : cocone F) (m : t.X ⟢ s.X) (w : βˆ€ j : J, t.ΞΉ.app j ≫ m = s.ΞΉ.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp] is_colimit.fac restate_axiom is_colimit.uniq' namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ /-- The universal morphism from a colimit cocone to any other cone. -/ def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟢ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟢ s} : f = f' := have βˆ€ {g : t ⟢ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm /-- Alternative constructor for `is_colimit`, providing a morphism of cocones rather than a morphism between the cocone points and separately the factorisation condition. -/ def mk_cocone_morphism {t : cocone F} (desc : Ξ  (s : cocone F), t ⟢ s) (uniq' : βˆ€ (s : cocone F) (m : t ⟢ s), m = desc s) : is_colimit t := { desc := Ξ» s, (desc s).hom, uniq' := Ξ» s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s β‰… t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } /-- Colimits of `F` are unique up to isomorphism. -/ -- We may later want to prove the coherence of these isomorphisms. def cocone_point_unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s.X β‰… t.X := (cocones.forget F).map_iso (unique_up_to_iso P Q) /-- Transport evidence that a cocone is a colimit cocone across an isomorphism of cocones. -/ def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r β‰… t) : is_colimit t := is_colimit.mk_cocone_morphism (Ξ» s, i.inv ≫ P.desc_cocone_morphism s) (Ξ» s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟢ W) : m = h.desc { X := W, ΞΉ := { app := Ξ» b, t.ΞΉ.app b ≫ m, naturality' := by intros; erw [←assoc, t.ΞΉ.naturality, comp_id, comp_id] } } := h.uniq { X := W, ΞΉ := { app := Ξ» b, t.ΞΉ.app b ≫ m, naturality' := _ } } m (Ξ» b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟢ W} (w : βˆ€ j, t.ΞΉ.app j ≫ f = t.ΞΉ.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- The universal property of a colimit cocone: a map `X ⟢ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟢ W) β‰… (F ⟢ (const J).obj W) := { hom := Ξ» f, (t.extend f).ΞΉ, inv := Ξ» ΞΉ, h.desc { X := W, ΞΉ := ΞΉ }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟢ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ΞΉ := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) β‰… F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) /-- Another, more explicit, formulation of the universal property of a colimit cocone. See also `hom_iso`. -/ def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟢ W) : Type v) β‰… { p : Ξ  j, F.obj j ⟢ W // βˆ€ {j j' : J} (f : j ⟢ j'), F.map f ≫ p j' = p j } := h.hom_iso W β‰ͺ≫ { hom := Ξ» ΞΉ, ⟨λ j, ΞΉ.app j, Ξ» j j' f, by convert ←(ΞΉ.naturality f); apply comp_id⟩, inv := Ξ» p, { app := Ξ» j, p.1 j, naturality' := Ξ» j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C β†’ D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v} D] (G : C β₯€ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Ξ  (s : cocone F), t.X ⟢ s.X) (h : βˆ€ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := Ξ» s j, by apply G.map_injective; rw [G.map_comp, h]; apply ht.fac, uniq' := Ξ» s m w, begin apply G.map_injective, rw h, refine ht.uniq (G.map_cocone s) _ (Ξ» j, _), convert ←congr_arg (Ξ» f, G.map f) (w j), apply G.map_comp end } /-- A cocone is a colimit cocone exactly if there is a unique cocone morphism from any other cocone. -/ def iso_unique_cocone_morphism {t : cocone F} : is_colimit t β‰… Ξ  s, unique (t ⟢ s) := { hom := Ξ» h s, { default := h.desc_cocone_morphism s, uniq := Ξ» _, h.uniq_cocone_morphism }, inv := Ξ» h, { desc := Ξ» s, (h s).default.hom, uniq' := Ξ» s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } /-- Given two functors which have equivalent categories of cocones, we can transport a limiting cocone across the equivalence. -/ def of_cocone_equiv {D : Type u'} [category.{v} D] {G : K β₯€ D} (h : cocone G β₯€ cocone F) [is_left_adjoint h] {c : cocone G} (t : is_colimit c) : is_colimit (h.obj c) := mk_cocone_morphism (Ξ» s, ((adjunction.of_left_adjoint h).hom_equiv c s).symm (t.desc_cocone_morphism _)) (Ξ» s m, (adjunction.hom_equiv_apply_eq _ _ _).1 t.uniq_cocone_morphism) namespace of_nat_iso variables {X : C} (h : coyoneda.obj (op X) β‰… F.cocones) /-- If `F.cocones` is corepresented by `X`, each morphism `f : X ⟢ Y` gives a cocone with cone point `Y`. -/ def cocone_of_hom {Y : C} (f : X ⟢ Y) : cocone F := { X := Y, ΞΉ := h.hom.app Y f } /-- If `F.cocones` is corepresented by `X`, each cocone `s` gives a morphism `X ⟢ s.X`. -/ def hom_of_cocone (s : cocone F) : X ⟢ s.X := h.inv.app s.X s.ΞΉ @[simp] lemma cocone_of_hom_of_cocone (s : cocone F) : cocone_of_hom h (hom_of_cocone h s) = s := begin dsimp [cocone_of_hom, hom_of_cocone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) s_X) s_ΞΉ, end @[simp] lemma hom_of_cocone_of_hom {Y : C} (f : X ⟢ Y) : hom_of_cocone h (cocone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) Y) f /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to the identity morphism on `X` will be a colimit cocone. -/ def colimit_cocone : cocone F := cocone_of_hom h (πŸ™ X) /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to a morphism `f : Y ⟢ X` is the colimit cocone extended by `f`. -/ lemma cocone_of_hom_fac {Y : C} (f : X ⟢ Y) : cocone_of_hom h f = (colimit_cocone h).extend f := begin dsimp [cocone_of_hom, colimit_cocone, cocone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f) (πŸ™ X), dsimp at t, simp only [id_comp] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cocones` is corepresented by `X`, any cocone is the extension of the colimit cocone by the corresponding morphism. -/ lemma cocone_fac (s : cocone F) : (colimit_cocone h).extend (hom_of_cocone h s) = s := begin rw ←cocone_of_hom_of_cocone h s, conv_lhs { simp only [hom_of_cocone_of_hom] }, apply (cocone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cocones` is corepresentable, then the cocone corresponding to the identity morphism on the representing object is a colimit cocone. -/ def of_nat_iso {X : C} (h : coyoneda.obj (op X) β‰… F.cocones) : is_colimit (colimit_cocone h) := { desc := Ξ» s, hom_of_cocone h s, fac' := Ξ» s j, begin have h := cocone_fac h s, cases s, injection h with h₁ hβ‚‚, simp only [heq_iff_eq] at hβ‚‚, conv_rhs { rw ← hβ‚‚ }, refl, end, uniq' := Ξ» s m w, begin rw ←hom_of_cocone_of_hom h m, congr, rw cocone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_colimit section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J β₯€ C) := (cone : cone F) (is_limit : is_limit cone) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J β₯€ C`. -/ class has_limits_of_shape := (has_limit : Ξ  F : J β₯€ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits := (has_limits_of_shape : Ξ  (J : Type v) [π’₯ : small_category J], has_limits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J β₯€ C) : has_limit F := has_limits_of_shape.has_limit F @[priority 100] -- see Note [lower instance priority] instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := has_limits.has_limits_of_shape J /- Interface to the `has_limit` class. -/ /-- The chosen limit cone of a functor. -/ def limit.cone (F : J β₯€ C) [has_limit F] : cone F := has_limit.cone /-- The chosen limit object of a functor. -/ def limit (F : J β₯€ C) [has_limit F] := (limit.cone F).X /-- The projection from the chosen limit object to a value of the functor. -/ def limit.Ο€ (F : J β₯€ C) [has_limit F] (j : J) : limit F ⟢ F.obj j := (limit.cone F).Ο€.app j @[simp] lemma limit.cone_Ο€ {F : J β₯€ C} [has_limit F] (j : J) : (limit.cone F).Ο€.app j = limit.Ο€ _ j := rfl @[simp] lemma limit.w (F : J β₯€ C) [has_limit F] {j j' : J} (f : j ⟢ j') : limit.Ο€ F j ≫ F.map f = limit.Ο€ F j' := (limit.cone F).w f /-- Evidence that the chosen cone is a limit cone. -/ def limit.is_limit (F : J β₯€ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} /-- The morphism from the cone point of any other cone to the chosen limit object. -/ def limit.lift (F : J β₯€ C) [has_limit F] (c : cone F) : c.X ⟢ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J β₯€ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp, reassoc] lemma limit.lift_Ο€ {F : J β₯€ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.Ο€ F j = c.Ο€.app j := is_limit.fac _ c j /-- The cone morphism from any cone to the chosen limit cone. -/ def limit.cone_morphism {F : J β₯€ C} [has_limit F] (c : cone F) : c ⟢ (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J β₯€ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl lemma limit.cone_morphism_Ο€ {F : J β₯€ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.Ο€ F j = c.Ο€.app j := by simp @[ext] lemma limit.hom_ext {F : J β₯€ C} [has_limit F] {X : C} {f f' : X ⟢ limit F} (w : βˆ€ j, f ≫ limit.Ο€ F j = f' ≫ limit.Ο€ F j) : f = f' := (limit.is_limit F).hom_ext w /-- The isomorphism (in `Type`) between morphisms from a specified object `W` to the limit object, and cones with cone point `W`. -/ def limit.hom_iso (F : J β₯€ C) [has_limit F] (W : C) : (W ⟢ limit F) β‰… (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J β₯€ C) [has_limit F] {W : C} (f : W ⟢ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).Ο€ := (limit.is_limit F).hom_iso_hom f /-- The isomorphism (in `Type`) between morphisms from a specified object `W` to the limit object, and an explicit componentwise description of cones with cone point `W`. -/ def limit.hom_iso' (F : J β₯€ C) [has_limit F] (W : C) : ((W ⟢ limit F) : Type v) β‰… { p : Ξ  j, W ⟢ F.obj j // βˆ€ {j j' : J} (f : j ⟢ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J β₯€ C} [has_limit F] (c : cone F) {X : C} (f : X ⟢ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously /-- If we've chosen a limit for a functor `F`, we can transport that choice across a natural isomorphism. -/ def has_limit_of_iso {F G : J β₯€ C} [has_limit F] (Ξ± : F β‰… G) : has_limit G := { cone := (cones.postcompose Ξ±.hom).obj (limit.cone F), is_limit := { lift := Ξ» s, limit.lift F ((cones.postcompose Ξ±.inv).obj s), fac' := Ξ» s j, begin rw [cones.postcompose_obj_Ο€, nat_trans.comp_app, limit.cone_Ο€, ←category.assoc, limit.lift_Ο€], simp end, uniq' := Ξ» s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_Ο€, cones.postcompose_obj_Ο€, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } /-- If a functor `G` has the same collection of cones as a functor `F` which has a limit, then `G` also has a limit. -/ -- See the construction of limits from products and equalizers -- for an example usage. def has_limit.of_cones_iso {J K : Type v} [small_category J] [small_category K] (F : J β₯€ C) (G : K β₯€ C) (h : F.cones β‰… G.cones) [has_limit F] : has_limit G := ⟨_, is_limit.of_nat_iso ((is_limit.nat_iso (limit.is_limit F)) β‰ͺ≫ h)⟩ section pre variables (F) [has_limit F] (E : K β₯€ J) [has_limit (E β‹™ F)] /-- The canonical morphism from the chosen limit of `F` to the chosen limit of `E β‹™ F`. -/ def limit.pre : limit F ⟢ limit (E β‹™ F) := limit.lift (E β‹™ F) { X := limit F, Ο€ := { app := Ξ» k, limit.Ο€ F (E.obj k) } } @[simp] lemma limit.pre_Ο€ (k : K) : limit.pre F E ≫ limit.Ο€ (E β‹™ F) k = limit.Ο€ F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E β‹™ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L β₯€ K) [has_limit (D β‹™ E β‹™ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E β‹™ F) D = limit.pre F (D β‹™ E) := by ext j; erw [assoc, limit.pre_Ο€, limit.pre_Ο€, limit.pre_Ο€]; refl end pre section post variables {D : Type u'} [category.{v} D] variables (F) [has_limit F] (G : C β₯€ D) [has_limit (F β‹™ G)] /-- The canonical morphism from `G` applied to the chosen limit of `F` to the chosen limit of `F β‹™ G`. -/ def limit.post : G.obj (limit F) ⟢ limit (F β‹™ G) := limit.lift (F β‹™ G) { X := G.obj (limit F), Ο€ := { app := Ξ» j, G.map (limit.Ο€ F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_Ο€ (j : J) : limit.post F G ≫ limit.Ο€ (F β‹™ G) j = G.map (limit.Ο€ F j) := by erw is_limit.fac @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F β‹™ G) (G.map_cone c) := by ext; rw [assoc, limit.post_Ο€, ←G.map_comp, limit.lift_Ο€, limit.lift_Ο€]; refl @[simp] lemma limit.post_post {E : Type u''} [category.{v} E] (H : D β₯€ E) [has_limit ((F β‹™ G) β‹™ H)] : /- H G (limit F) ⟢ H (limit (F β‹™ G)) ⟢ limit ((F β‹™ G) β‹™ H) equals -/ /- H G (limit F) ⟢ limit (F β‹™ (G β‹™ H)) -/ H.map (limit.post F G) ≫ limit.post (F β‹™ G) H = limit.post F (G β‹™ H) := by ext; erw [assoc, limit.post_Ο€, ←H.map_comp, limit.post_Ο€, limit.post_Ο€]; refl end post lemma limit.pre_post {D : Type u'} [category.{v} D] (E : K β₯€ J) (F : J β₯€ C) (G : C β₯€ D) [has_limit F] [has_limit (E β‹™ F)] [has_limit (F β‹™ G)] [has_limit ((E β‹™ F) β‹™ G)] : /- G (limit F) ⟢ G (limit (E β‹™ F)) ⟢ limit ((E β‹™ F) β‹™ G) vs -/ /- G (limit F) ⟢ limit F β‹™ G ⟢ limit (E β‹™ (F β‹™ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E β‹™ F) G = limit.post F G ≫ limit.pre (F β‹™ G) E := by ext; erw [assoc, limit.post_Ο€, ←G.map_comp, limit.pre_Ο€, assoc, limit.pre_Ο€, limit.post_Ο€]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K β‰Œ J) [has_limit F] : has_limit (e.functor β‹™ F) := { cone := cone.whisker e.functor (limit.cone F), is_limit := let e' := cones.postcompose (e.inv_fun_id_assoc F).hom in { lift := Ξ» s, limit.lift F (e'.obj (cone.whisker e.inverse s)), fac' := Ξ» s j, begin dsimp, rw [limit.lift_Ο€], dsimp [e'], erw [inv_fun_id_assoc_hom_app, counit_functor, ←s.Ο€.naturality, id_comp] end, uniq' := Ξ» s m w, begin apply limit.hom_ext, intro j, erw [limit.lift_Ο€, ←limit.w F (e.counit_iso.hom.app j)], slice_lhs 1 2 { erw [w (e.inverse.obj j)] }, simp end } } local attribute [elab_simple] inv_fun_id_assoc -- not entirely sure why this is needed /-- If a `E β‹™ F` has a chosen limit, and `E` is an equivalence, we can construct a chosen limit of `F`. -/ def has_limit_of_equivalence_comp (e : K β‰Œ J) [has_limit (e.functor β‹™ F)] : has_limit F := begin haveI : has_limit (e.inverse β‹™ e.functor β‹™ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J β₯€ C) β₯€ C := { obj := Ξ» F, limit F, map := Ξ» F G Ξ±, limit.lift G { X := limit F, Ο€ := { app := Ξ» j, limit.Ο€ F j ≫ Ξ±.app j, naturality' := Ξ» j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := Ξ» F G H Ξ± Ξ², by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J β₯€ C} (Ξ± : F ⟢ G) @[simp, reassoc] lemma limit.map_Ο€ (j : J) : lim.map Ξ± ≫ limit.Ο€ G j = limit.Ο€ F j ≫ Ξ±.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map Ξ± = limit.lift G ((cones.postcompose Ξ±).obj c) := by ext; rw [assoc, limit.map_Ο€, ←assoc, limit.lift_Ο€, limit.lift_Ο€]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K β₯€ J) : lim.map Ξ± ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E Ξ±) := by ext; rw [assoc, limit.pre_Ο€, limit.map_Ο€, assoc, limit.map_Ο€, ←assoc, limit.pre_Ο€]; refl lemma limit.map_pre' [has_limits_of_shape.{v} K C] (F : J β₯€ C) {E₁ Eβ‚‚ : K β₯€ J} (Ξ± : E₁ ⟢ Eβ‚‚) : limit.pre F Eβ‚‚ = limit.pre F E₁ ≫ lim.map (whisker_right Ξ± F) := by ext1; simp [← category.assoc] lemma limit.id_pre (F : J β₯€ C) : limit.pre F (𝟭 _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v} D] [has_limits_of_shape J D] (H : C β₯€ D) : /- H (limit F) ⟢ H (limit G) ⟢ limit (G β‹™ H) vs H (limit F) ⟢ limit (F β‹™ H) ⟢ limit (G β‹™ H) -/ H.map (lim.map Ξ±) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right Ξ± H) := begin ext, rw [assoc, limit.post_Ο€, ←H.map_comp, limit.map_Ο€, H.map_comp], rw [assoc, limit.map_Ο€, ←assoc, limit.post_Ο€], refl end /-- The isomorphism between morphisms from `W` to the cone point of the limit cone for `F` and cones over `F` with cone point `W` is natural in `F`. -/ def lim_yoneda : lim β‹™ yoneda β‰… category_theory.cones J C := nat_iso.of_components (Ξ» F, nat_iso.of_components (Ξ» W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor /-- We can transport chosen limits of shape `J` along an equivalence `J β‰Œ J'`. -/ def has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J β‰Œ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J β₯€ C) := (cocone : cocone F) (is_colimit : is_colimit cocone) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J β₯€ C`. -/ class has_colimits_of_shape := (has_colimit : Ξ  F : J β₯€ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits := (has_colimits_of_shape : Ξ  (J : Type v) [π’₯ : small_category J], has_colimits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J β₯€ C) : has_colimit F := has_colimits_of_shape.has_colimit F @[priority 100] -- see Note [lower instance priority] instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape J /- Interface to the `has_colimit` class. -/ /-- The chosen colimit cocone of a functor. -/ def colimit.cocone (F : J β₯€ C) [has_colimit F] : cocone F := has_colimit.cocone /-- The chosen colimit object of a functor. -/ def colimit (F : J β₯€ C) [has_colimit F] := (colimit.cocone F).X /-- The coprojection from a value of the functor to the chosen colimit object. -/ def colimit.ΞΉ (F : J β₯€ C) [has_colimit F] (j : J) : F.obj j ⟢ colimit F := (colimit.cocone F).ΞΉ.app j @[simp] lemma colimit.cocone_ΞΉ {F : J β₯€ C} [has_colimit F] (j : J) : (colimit.cocone F).ΞΉ.app j = colimit.ΞΉ _ j := rfl @[simp] lemma colimit.w (F : J β₯€ C) [has_colimit F] {j j' : J} (f : j ⟢ j') : F.map f ≫ colimit.ΞΉ F j' = colimit.ΞΉ F j := (colimit.cocone F).w f /-- Evidence that the chosen cocone is a colimit cocone. -/ def colimit.is_colimit (F : J β₯€ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} /-- The morphism from the chosen colimit object to the cone point of any other cocone. -/ def colimit.desc (F : J β₯€ C) [has_colimit F] (c : cocone F) : colimit F ⟢ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J β₯€ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl /-- We have lots of lemmas describing how to simplify `colimit.ΞΉ F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ΞΉ`. We thus use `reassoc` to define additional `@[simp]` lemmas, with an arbitrary extra morphism. (see `tactic/reassoc_axiom.lean`) -/ @[simp, reassoc] lemma colimit.ΞΉ_desc {F : J β₯€ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ΞΉ F j ≫ colimit.desc F c = c.ΞΉ.app j := is_colimit.fac _ c j /-- The cocone morphism from the chosen colimit cocone to any cocone. -/ def colimit.cocone_morphism {F : J β₯€ C} [has_colimit F] (c : cocone F) : (colimit.cocone F) ⟢ c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J β₯€ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl lemma colimit.ΞΉ_cocone_morphism {F : J β₯€ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ΞΉ F j ≫ (colimit.cocone_morphism c).hom = c.ΞΉ.app j := by simp @[ext] lemma colimit.hom_ext {F : J β₯€ C} [has_colimit F] {X : C} {f f' : colimit F ⟢ X} (w : βˆ€ j, colimit.ΞΉ F j ≫ f = colimit.ΞΉ F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w /-- The isomorphism (in `Type`) between morphisms from the colimit object to a specified object `W`, and cocones with cone point `W`. -/ def colimit.hom_iso (F : J β₯€ C) [has_colimit F] (W : C) : (colimit F ⟢ W) β‰… (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J β₯€ C) [has_colimit F] {W : C} (f : colimit F ⟢ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ΞΉ ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f /-- The isomorphism (in `Type`) between morphisms from the colimit object to a specified object `W`, and an explicit componentwise description of cocones with cone point `W`. -/ def colimit.hom_iso' (F : J β₯€ C) [has_colimit F] (W : C) : ((colimit F ⟢ W) : Type v) β‰… { p : Ξ  j, F.obj j ⟢ W // βˆ€ {j j'} (f : j ⟢ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J β₯€ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟢ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end /-- If we've chosen a colimit for a functor `F`, we can transport that choice across a natural isomorphism. -/ -- This has the isomorphism pointing in the opposite direction than in `has_limit_of_iso`. -- This is intentional; it seems to help with elaboration. def has_colimit_of_iso {F G : J β₯€ C} [has_colimit F] (Ξ± : G β‰… F) : has_colimit G := { cocone := (cocones.precompose Ξ±.hom).obj (colimit.cocone F), is_colimit := { desc := Ξ» s, colimit.desc F ((cocones.precompose Ξ±.inv).obj s), fac' := Ξ» s j, begin rw [cocones.precompose_obj_ΞΉ, nat_trans.comp_app, colimit.cocone_ΞΉ], rw [category.assoc, colimit.ΞΉ_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := Ξ» s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ΞΉ_desc, cocones.precompose_obj_ΞΉ, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } /-- If a functor `G` has the same collection of cocones as a functor `F` which has a colimit, then `G` also has a colimit. -/ def has_colimit.of_cocones_iso {J K : Type v} [small_category J] [small_category K] (F : J β₯€ C) (G : K β₯€ C) (h : F.cocones β‰… G.cocones) [has_colimit F] : has_colimit G := ⟨_, is_colimit.of_nat_iso ((is_colimit.nat_iso (colimit.is_colimit F)) β‰ͺ≫ h)⟩ section pre variables (F) [has_colimit F] (E : K β₯€ J) [has_colimit (E β‹™ F)] /-- The canonical morphism from the chosen colimit of `E β‹™ F` to the chosen colimit of `F`. -/ def colimit.pre : colimit (E β‹™ F) ⟢ colimit F := colimit.desc (E β‹™ F) { X := colimit F, ΞΉ := { app := Ξ» k, colimit.ΞΉ F (E.obj k) } } @[simp, reassoc] lemma colimit.ΞΉ_pre (k : K) : colimit.ΞΉ (E β‹™ F) k ≫ colimit.pre F E = colimit.ΞΉ F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E β‹™ F) (c.whisker E) := by ext; rw [←assoc, colimit.ΞΉ_pre]; simp variables {L : Type v} [small_category L] variables (D : L β₯€ K) [has_colimit (D β‹™ E β‹™ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E β‹™ F) D ≫ colimit.pre F E = colimit.pre F (D β‹™ E) := begin ext j, rw [←assoc, colimit.ΞΉ_pre, colimit.ΞΉ_pre], letI : has_colimit ((D β‹™ E) β‹™ F) := show has_colimit (D β‹™ E β‹™ F), by apply_instance, exact (colimit.ΞΉ_pre F (D β‹™ E) j).symm end end pre section post variables {D : Type u'} [category.{v} D] variables (F) [has_colimit F] (G : C β₯€ D) [has_colimit (F β‹™ G)] /-- The canonical morphism from `G` applied to the chosen colimit of `F β‹™ G` to `G` applied to the chosen colimit of `F`. -/ def colimit.post : colimit (F β‹™ G) ⟢ G.obj (colimit F) := colimit.desc (F β‹™ G) { X := G.obj (colimit F), ΞΉ := { app := Ξ» j, G.map (colimit.ΞΉ F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp, reassoc] lemma colimit.ΞΉ_post (j : J) : colimit.ΞΉ (F β‹™ G) j ≫ colimit.post F G = G.map (colimit.ΞΉ F j) := by erw is_colimit.fac @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F β‹™ G) (G.map_cocone c) := by ext; rw [←assoc, colimit.ΞΉ_post, ←G.map_comp, colimit.ΞΉ_desc, colimit.ΞΉ_desc]; refl @[simp] lemma colimit.post_post {E : Type u''} [category.{v} E] (H : D β₯€ E) [has_colimit ((F β‹™ G) β‹™ H)] : /- H G (colimit F) ⟢ H (colimit (F β‹™ G)) ⟢ colimit ((F β‹™ G) β‹™ H) equals -/ /- H G (colimit F) ⟢ colimit (F β‹™ (G β‹™ H)) -/ colimit.post (F β‹™ G) H ≫ H.map (colimit.post F G) = colimit.post F (G β‹™ H) := begin ext, rw [←assoc, colimit.ΞΉ_post, ←H.map_comp, colimit.ΞΉ_post], exact (colimit.ΞΉ_post F (G β‹™ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v} D] (E : K β₯€ J) (F : J β₯€ C) (G : C β₯€ D) [has_colimit F] [has_colimit (E β‹™ F)] [has_colimit (F β‹™ G)] [has_colimit ((E β‹™ F) β‹™ G)] : /- G (colimit F) ⟢ G (colimit (E β‹™ F)) ⟢ colimit ((E β‹™ F) β‹™ G) vs -/ /- G (colimit F) ⟢ colimit F β‹™ G ⟢ colimit (E β‹™ (F β‹™ G)) or -/ colimit.post (E β‹™ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F β‹™ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ΞΉ_post, ←G.map_comp, colimit.ΞΉ_pre, ←assoc], letI : has_colimit (E β‹™ F β‹™ G) := show has_colimit ((E β‹™ F) β‹™ G), by apply_instance, erw [colimit.ΞΉ_pre (F β‹™ G) E j, colimit.ΞΉ_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K β‰Œ J) [has_colimit F] : has_colimit (e.functor β‹™ F) := { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := let e' := cocones.precompose (e.inv_fun_id_assoc F).inv in { desc := Ξ» s, colimit.desc F (e'.obj (cocone.whisker e.inverse s)), fac' := Ξ» s j, begin dsimp, rw [colimit.ΞΉ_desc], dsimp [e'], erw [inv_fun_id_assoc_inv_app, ←functor_unit, s.ΞΉ.naturality, comp_id], refl end, uniq' := Ξ» s m w, begin apply colimit.hom_ext, intro j, erw [colimit.ΞΉ_desc], have := w (e.inverse.obj j), simp at this, erw [←colimit.w F (e.counit_iso.hom.app j)] at this, erw [assoc, ←iso.eq_inv_comp (F.map_iso $ e.counit_iso.app j)] at this, erw [this], simp end } } /-- If a `E β‹™ F` has a chosen colimit, and `E` is an equivalence, we can construct a chosen colimit of `F`. -/ def has_colimit_of_equivalence_comp (e : K β‰Œ J) [has_colimit (e.functor β‹™ F)] : has_colimit F := begin haveI : has_colimit (e.inverse β‹™ e.functor β‹™ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J β₯€ C) β₯€ C := { obj := Ξ» F, colimit F, map := Ξ» F G Ξ±, colimit.desc F { X := colimit G, ΞΉ := { app := Ξ» j, Ξ±.app j ≫ colimit.ΞΉ G j, naturality' := Ξ» j j' f, by erw [comp_id, ←assoc, Ξ±.naturality, assoc, colimit.w] } }, map_comp' := Ξ» F G H Ξ± Ξ², by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J β₯€ C} (Ξ± : F ⟢ G) @[simp, reassoc] lemma colimit.ΞΉ_map (j : J) : colimit.ΞΉ F j ≫ colim.map Ξ± = Ξ±.app j ≫ colimit.ΞΉ G j := by apply is_colimit.fac @[simp] lemma colimit.map_desc (c : cocone G) : colim.map Ξ± ≫ colimit.desc G c = colimit.desc F ((cocones.precompose Ξ±).obj c) := by ext; rw [←assoc, colimit.ΞΉ_map, assoc, colimit.ΞΉ_desc, colimit.ΞΉ_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K β₯€ J) : colimit.pre F E ≫ colim.map Ξ± = colim.map (whisker_left E Ξ±) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ΞΉ_pre, colimit.ΞΉ_map, ←assoc, colimit.ΞΉ_map, assoc, colimit.ΞΉ_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape.{v} K C] (F : J β₯€ C) {E₁ Eβ‚‚ : K β₯€ J} (Ξ± : E₁ ⟢ Eβ‚‚) : colimit.pre F E₁ = colim.map (whisker_right Ξ± F) ≫ colimit.pre F Eβ‚‚ := by ext1; simp [← category.assoc] lemma colimit.pre_id (F : J β₯€ C) : colimit.pre F (𝟭 _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v} D] [has_colimits_of_shape J D] (H : C β₯€ D) : /- H (colimit F) ⟢ H (colimit G) ⟢ colimit (G β‹™ H) vs H (colimit F) ⟢ colimit (F β‹™ H) ⟢ colimit (G β‹™ H) -/ colimit.post F H ≫ H.map (colim.map Ξ±) = colim.map (whisker_right Ξ± H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ΞΉ_post, ←H.map_comp, colimit.ΞΉ_map, H.map_comp], rw [←assoc, colimit.ΞΉ_map, assoc, colimit.ΞΉ_post], refl end /-- The isomorphism between morphisms from the cone point of the chosen colimit cocone for `F` to `W` and cocones over `F` with cone point `W` is natural in `F`. -/ def colim_coyoneda : colim.op β‹™ coyoneda β‰… category_theory.cocones J C := nat_iso.of_components (Ξ» F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor /-- We can transport chosen colimits of shape `J` along an equivalence `J β‰Œ J'`. -/ def has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J β‰Œ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits
522e5559039a71269677fee2dda89df4322a8bbd
556aeb81a103e9e0ac4e1fe0ce1bc6e6161c3c5e
/src/starkware/cairo/lean/semantics/soundness/hoare.lean
86e8502f7f9851b32720ad018e0454c0521ef5e2
[ "Apache-2.0" ]
permissive
starkware-libs/formal-proofs
d6b731604461bf99e6ba820e68acca62a21709e8
f5fa4ba6a471357fd171175183203d0b437f6527
refs/heads/master
1,691,085,444,753
1,690,507,386,000
1,690,507,386,000
410,476,629
32
9
Apache-2.0
1,690,506,773,000
1,632,639,790,000
Lean
UTF-8
Lean
false
false
31,969
lean
/- Definitions and tactics for reasoning about the one-step effects of each assembly instruction. -/ import starkware.cairo.lean.semantics.util import starkware.cairo.lean.semantics.soundness.assembly import starkware.cairo.lean.semantics.air_encoding.instruction section main @[class] def char_ge_2_63 (F : Type*) [field F] : Prop := ring_char F β‰₯ 2^63 variables {F : Type*} [field F] /- Reasoning about contents of memory. -/ def mem_at (mem : F β†’ F) : list F β†’ F β†’ Prop | [] _ := true | (v :: l) pc := mem pc = v ∧ mem_at l (pc + 1) /- The halting instruction is a relative jump to the same spot. In general, we don't prove that a program will halt, but, rather, that *if* it halts it has the desired behavior. This is o.k. because the STARK can certify that the computation has, in fact, halted. To take a step forward, however, we need to verify that the current instruction is not a halting instruction. The following lemmas help with do that efficiently. -/ def jmp_rel_instr : instr := 'jmp_rel[ 'op1[imm] ] section instr variables (op0 : op0_spec) (op1 : op1_spec) (res : res_spec) (dst : dst_spec) (ap_update : bool) lemma assert_eq_ne_jmp_rel : (assert_eq_instr op0 res dst ap_update).to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h, have := congr_arg instruction.opcode_assert_eq h, contradiction end lemma call_ne_jmp_rel (call_abs : bool) : (call_instr call_abs res).to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h, have := congr_arg instruction.opcode_call h, contradiction end lemma ret_ne_jmp_rel : ret_instr.to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h, have := congr_arg instruction.opcode_ret h, contradiction end lemma advance_ap_ne_jmp_rel : (advance_ap_instr op0 res).to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h, have := congr_arg instruction.ap_add h, contradiction end lemma jnz_ne_jmp_rel : (jnz_instr op0 op1 dst ap_update).to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h, have := congr_arg instruction.pc_jnz h, contradiction end /-- Can be used unless the jump has an immediate argument, in which case instead we have to check that the immediate argument is not 0. -/ lemma jump_ne_jmp_rel (jump_abs : bool) (h : res.to_op1.op1_imm = ff) : (jump_instr jump_abs op0 res ap_update).to_instruction β‰  jmp_rel_instr.to_instruction := begin intro h', have := congr_arg instruction.op1_imm h', dsimp [jump_instr] at this, rw h at this, contradiction end end instr /- The definition of a halting execution trace and supporting lemmas. -/ section halting variable [decidable_eq F] variable mem : F β†’ F variables s t : register_state F def is_halting_state := mem s.pc = jmp_rel_instr.to_nat ∧ mem (s.pc + 1) = 0 lemma not_is_halting_state_iff {mem : F β†’ F} {s : register_state F} : Β¬ is_halting_state mem s ↔ mem s.pc β‰  jmp_rel_instr.to_nat ∨ mem (s.pc + 1) β‰  0 := decidable.not_and_distrib def is_halting_trace {n : β„•} (exec : fin (n + 1) β†’ register_state F) : Prop := (βˆ€ i : fin n, next_state mem (exec i.cast_succ) (exec i.succ)) ∧ is_halting_state mem (exec (fin.last n)) theorem eq_succ_of_not_halting_state_0 {mem : F β†’ F} {n : β„•} {exec : fin (n + 1) β†’ register_state F} (h : is_halting_trace mem exec) (h' : Β¬ is_halting_state mem (exec 0)) : βˆƒ m : β„•, n = m.succ := begin apply nat.exists_eq_succ_of_ne_zero, rintro rfl, exact h' h.2 end theorem is_halting_trace_step {mem : F β†’ F} {n : β„•} {exec : fin (n.succ + 1) β†’ register_state F} (h : is_halting_trace mem exec) (h' : Β¬ is_halting_state mem (exec 0)) : is_halting_trace mem (Ξ» i, exec i.succ) := begin split, { intro i, convert h.1 i.succ, rw [fin.cast_succ_fin_succ] }, exact h.2 end theorem is_halting_trace_remainder {m n : β„•} {exec : fin (m + n + 1) β†’ register_state F} (h : is_halting_trace mem exec) : is_halting_trace mem (Ξ» j : fin (n + 1), exec (fin.cast_offset' m j)) := begin rcases h with ⟨h1, h2, h3⟩, rw is_halting_trace, refine ⟨_, h2, h3⟩, intro i, convert h1 (fin.cast_offset m i), rw [fin.cast_offset', fin.succ_cast_offset] end end halting /- The `ensures` predicate, and a bounded version. -/ section ensures variable [decidable_eq F] /-- Given `mem` and start state `s`, any halting trace `exec` with `exec 0 = s` eventually reaches a state `t` in `i` steps such that for some `k ≀ i`, `P k (exec i)`. We use the bound on `k` to ensure a final claim about the range of values that have been range checked. -/ def ensures (mem : F β†’ F) (s : register_state F) (P : β„• β†’ register_state F β†’ Prop) : Prop := βˆ€ n : β„•, βˆ€ exec : fin (n+1) β†’ register_state F, is_halting_trace mem exec β†’ exec 0 = s β†’ βˆƒ i : fin (n + 1), βˆƒ k ≀ i, P k (exec i) /-- for use with code blocks that end with a return -/ @[reducible] def ensures_ret (mem : F β†’ F) (s : register_state F) (P : β„• β†’ register_state F β†’ Prop) : Prop := ensures mem s (Ξ» k t, t.pc = mem (s.fp - 1) ∧ t.fp = mem (s.fp - 2) ∧ P k t) def ensuresb (bound : β„•) (mem : F β†’ F) (s : register_state F) (P : β„• β†’ register_state F β†’ Prop) : Prop := βˆ€ n : β„•, n < bound β†’ βˆ€ exec : fin (n+1) β†’ register_state F, is_halting_trace mem exec β†’ exec 0 = s β†’ βˆƒ i : fin (n + 1), βˆƒ k ≀ i, P k (exec i) theorem ensures_of_ensuresb {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} (h : βˆ€ bound, ensuresb bound mem s P) : ensures mem s P := Ξ» n, h (n + 1) _ (nat.lt_succ_self n) @[reducible] def ensuresb_ret (bound : β„•) (mem : F β†’ F) (s : register_state F) (P : β„• β†’ register_state F β†’ Prop) : Prop := ensuresb bound mem s (Ξ» k t, t.pc = mem (s.fp - 1) ∧ t.fp = mem (s.fp - 2) ∧ P k t) theorem ensures_ret_of_ensuresb_ret {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} (h : βˆ€ bound, ensuresb_ret bound mem s P) : ensures_ret mem s P := Ξ» n, h (n + 1) _ (nat.lt_succ_self n) /-- Takes a step and augments `k` by 1. -/ theorem ensuresb_step {bound : β„•} {mem : F β†’ F} {s t : register_state F} {P : β„• β†’ register_state F β†’ Prop} {Q : Prop} (h0 : Β¬ is_halting_state mem s) (h1 : βˆ€ t', next_state mem s t' β†’ t' = t ∧ Q) (h : Q β†’ ensuresb bound mem t (Ξ» k t', P (k + 1) t')) : ensuresb (bound + 1) mem s P := begin rintros nβ‚€ nβ‚€le exec h2 rfl, rcases eq_succ_of_not_halting_state_0 h2 h0 with ⟨n, rfl⟩, rcases h (h1 _ (h2.1 0)).2 n (nat.le_of_succ_le_succ nβ‚€le) (Ξ» i, exec i.succ) (is_halting_trace_step h2 h0) (h1 _ (h2.1 0)).1 with ⟨i, k, hik, h4⟩, use [i.succ, k.succ], split, { exact fin.succ_le_succ_iff.mpr hik }, convert h4, simp end /- used to add a + 1 if necessary -/ theorem ensuresb_succ {bound : β„•} {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} (h : ensuresb (bound + 1) mem s P) : ensuresb bound mem s P := Ξ» n nlt, h _ (nlt.trans $nat.lt_succ_self _) theorem ensuresb_test {bound : β„•} {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} (h : ensuresb (bound + 1) mem s P) : ensuresb (bound + 1) mem s P := h theorem ensuresb_trans {bound : β„•} {mem : F β†’ F} {s : register_state F} {P Q : β„• β†’ register_state F β†’ Prop} (h0 : ensures mem s P) (h1 : βˆ€ k t, P k t β†’ ensuresb bound mem t (Ξ» k', Q (k + k'))) : ensuresb bound mem s Q := begin rintros n nlt exec h2 h3, rcases h0 _ _ h2 h3 with ⟨i, k, hik1, hik2⟩, rcases fin.exists_eq_add i with ⟨m, j, hm, rfl⟩, have : exec (fin.cast_offset' m 0) = exec i, { congr, ext, exact hm }, rcases h1 _ _ hik2 _ (lt_of_le_of_lt (nat.le_add_left _ _) nlt) _ (is_halting_trace_remainder mem h2) this with ⟨i', k', hi'k', h4⟩, dsimp at h4, have : ↑k + j + 1 ≀ m + j + 1, { apply nat.succ_le_succ, apply nat.add_le_add_right, nth_rewrite_rhs 0 hm, exact hik1 }, use [fin.cast_offset' m i', fin.cast_le this (fin.cast_offset' k k')], split, { rw fin.le_iff_coe_le_coe, simp [fin.cast_offset', fin.cast_offset], nth_rewrite_rhs 0 hm, rw fin.le_iff_coe_le_coe at hik1 hi'k', apply add_le_add hik1 hi'k' }, exact h4 end theorem ensuresb_rec_trans {bound : β„•} {mem : F β†’ F} {s : register_state F} {P Q : β„• β†’ register_state F β†’ Prop} (h0 : ensuresb bound mem s P) (h1 : βˆ€ k t, P k t β†’ ensuresb bound mem t (Ξ» k', Q (k + k'))) : ensuresb bound mem s Q := begin rintros n nlt exec h2 h3, rcases h0 _ nlt _ h2 h3 with ⟨i, k, hik1, hik2⟩, rcases fin.exists_eq_add i with ⟨m, j, hm, rfl⟩, have : exec (fin.cast_offset' m 0) = exec i, { congr, ext, exact hm }, rcases h1 _ _ hik2 _ (lt_of_le_of_lt (nat.le_add_left _ _) nlt) _ (is_halting_trace_remainder mem h2) this with ⟨i', k', hi'k', h4⟩, dsimp at h4, have : ↑k + j + 1 ≀ m + j + 1, { apply nat.succ_le_succ, apply nat.add_le_add_right, nth_rewrite_rhs 0 hm, exact hik1 }, use [fin.cast_offset' m i', fin.cast_le this (fin.cast_offset' k k')], split, { rw fin.le_iff_coe_le_coe, simp [fin.cast_offset', fin.cast_offset], nth_rewrite_rhs 0 hm, rw fin.le_iff_coe_le_coe at hik1 hi'k', apply add_le_add hik1 hi'k' }, exact h4 end theorem ensuresb_id {bound : β„•} {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} (h : P 0 s) : ensuresb (bound + 1) mem s P := begin intros n nlt exec hexec hexec0, use [0, 0, le_refl _], rwa hexec0 end theorem ensuresb_rec {mem : F β†’ F} (P : register_state F β†’ F β†’ β„• β†’ register_state F β†’ Prop) (Haux : F β†’ register_state F β†’ Prop) (s : register_state F) (arg : F) (hHaux : Haux arg s) (hind : βˆ€ bound, (βˆ€ (arg : F) (s: register_state F), Haux arg s β†’ ensuresb bound mem s (P s arg)) β†’ (βˆ€ {arg : F}, βˆ€ {s: register_state F}, Haux arg s β†’ ensuresb (bound + 1) mem s (P s arg))) : ensures mem s (P s arg) := begin have : βˆ€ bound, (βˆ€ {arg : F}, βˆ€ {s: register_state F}, Haux arg s β†’ ensuresb bound mem s (P s arg)), { intro bound, induction bound with bound ih, { intros _ _ _ n nlt, exact absurd nlt (nat.not_lt_zero _)}, apply hind, exact @ih }, apply ensures_of_ensuresb (Ξ» bound, this bound hHaux) end -- for handling blocks theorem ensuresb_ret_trans {bound : β„•} {mem : F β†’ F} {s : register_state F} {P Q : β„• β†’ register_state F β†’ Prop} (h0 : ensuresb_ret bound mem s P) (h1 : βˆ€ k t, P k t β†’ Q k t) : ensuresb_ret bound mem s Q := begin rintros n nlt exec h2 h3, rcases h0 _ nlt _ h2 h3 with ⟨i, k, hik1, hik2, hik3, hik4⟩, use [i, k, hik1, hik2, hik3, h1 _ _ hik4] end end ensures /- Characterize next state transitions. -/ section next_state variable [decidable_eq F] lemma instruction.next_state_iff_of_eq [char_ge : char_ge_2_63 F] {i : instruction} {mem : F β†’ F} {s t : register_state F} (h : mem s.pc = i.to_nat) : next_state mem s t ↔ i.next_state mem s t := begin rw [next_state], split, { rintros ⟨i', i'eq, i'next_state⟩, have : i = i' := inst_unique char_ge _ _ (h.symm.trans i'eq), rwa this }, intro h', use [i, h, h'] end variable {mem : F β†’ F} variable {s : register_state F} variable {P : β„• β†’ register_state F β†’ Prop} theorem assert_eq_ensuresb' [char_ge : char_ge_2_63 F] {bound : β„•} {op0 : op0_spec} {res : res_spec} {dst : dst_spec} {ap_update : bool} (h : mem s.pc = (assert_eq_instr op0 res dst ap_update).to_nat) (h' : compute_dst mem s dst = compute_res mem s op0 res β†’ ensuresb bound mem ⟨bump_pc s res.to_op1.op1_imm, bump_ap s ap_update, s.fp⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact assert_eq_ne_jmp_rel _ _ _ _ (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨bump_pc s res.to_op1.op1_imm, bump_ap s ap_update, s.fp⟩ ∧ compute_dst mem s dst = compute_res mem s op0 res, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_assert_eq] at ht, simp [ht], ext; simp [ht] }, apply ensuresb_step h1 h2 h' _ nlt end theorem jump_ensuresb [char_ge : char_ge_2_63 F] {bound} {op0 : op0_spec} {res : res_spec} {ap_update : bool} {jump_abs : bool} (h : mem s.pc = (jump_instr jump_abs op0 res ap_update).to_nat) (h_aux : res.to_op1.op1_imm = ff) (h' : ensuresb bound mem ⟨jump_pc s jump_abs (compute_res mem s op0 res), bump_ap s ap_update, s.fp⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact jump_ne_jmp_rel _ _ _ _ h_aux (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨jump_pc s jump_abs (compute_res mem s op0 res), bump_ap s ap_update, s.fp⟩ ∧ true, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_jump] at ht, split, ext; simp [ht], trivial }, apply ensuresb_step h1 h2 (Ξ» _, h') _ nlt end def checked_int_nz (x : β„€) (h0 : abs x β‰  0) (h1 : abs x < 2^63) := x lemma checked_int_nz_eq (x : β„€) (h0 : abs x β‰  0) (h1 : abs x < 2^63) : checked_int_nz x h0 h1 = x := rfl meta def abs_lt_tac : tactic unit := `[ { rw abs_of_nonneg, norm_num, norm_num } <|> { rw abs_of_nonpos, norm_num, norm_num } ] notation `'[#nz` x `]` := checked_int_nz x (by norm_num) (by abs_lt_tac) theorem jump_ensuresb' [char_ge : char_ge_2_63 F] {bound : β„•} {op0 : op0_spec} {res : res_spec} {ap_update : bool} {jump_abs : bool} (h : mem s.pc = (jump_instr jump_abs op0 res ap_update).to_nat) {x : β„€} {h0 : abs x β‰  0} {h1 : abs x < 2^63} (h_aux : mem (s.pc + 1) = checked_int_nz x h0 h1) (h' : ensuresb bound mem ⟨jump_pc s jump_abs (compute_res mem s op0 res), bump_ap s ap_update, s.fp⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw [not_is_halting_state_iff, h_aux], right, apply int.cast_ne_zero_of_abs_lt_char h0, apply lt_of_lt_of_le h1, change ((2^ 63 : β„•) : β„€) ≀ _, rw int.coe_nat_le_coe_nat_iff, apply char_ge }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨jump_pc s jump_abs (compute_res mem s op0 res), bump_ap s ap_update, s.fp⟩ ∧ true, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_jump] at ht, split, ext; simp [ht], trivial }, apply ensuresb_step h1 h2 (Ξ» _, h') _ nlt end theorem jnz_ensuresb [char_ge : char_ge_2_63 F] {bound : β„•} {op0 : op0_spec} {op1 : op1_spec} {dst : dst_spec} {ap_update : bool} (h : mem s.pc = (jnz_instr op0 op1 dst ap_update).to_nat) (hβ‚€ : compute_dst mem s dst = 0 β†’ ensuresb bound mem ⟨bump_pc s op1.op1_imm, bump_ap s ap_update, s.fp⟩ (Ξ» k t, P (k + 1) t)) (h₁ : compute_dst mem s dst β‰  0 β†’ ensuresb bound mem ⟨s.pc + compute_op1 mem s op0 op1, bump_ap s ap_update, s.fp⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact jnz_ne_jmp_rel _ _ _ _ (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨ite (compute_dst mem s dst = 0) (bump_pc s op1.op1_imm) (s.pc + compute_op1 mem s op0 op1), bump_ap s ap_update, s.fp⟩ ∧ true, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_jnz] at ht, simp [ht], ext; simp [ht] }, apply ensuresb_step h1 h2 _ _ nlt, by_cases h3 : compute_dst mem s dst = 0, { intro _, rw if_pos h3, exact hβ‚€ h3 }, intro _, rw if_neg h3, exact h₁ h3 end theorem call_ensuresb [char_ge : char_ge_2_63 F] {bound : β„•} {res : res_spec} {call_abs : bool} (h : mem s.pc = (call_instr call_abs res).to_nat) (h' : mem s.ap = s.fp β†’ mem (s.ap + 1) = bump_pc s res.to_op1.op1_imm β†’ ensuresb bound mem ⟨jump_pc s call_abs (compute_res mem s (op0_spec.ap_plus 1) res), s.ap + 2, s.ap + 2⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact call_ne_jmp_rel _ _ (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨jump_pc s call_abs (compute_res mem s (op0_spec.ap_plus 1) res), s.ap + 2, s.ap + 2⟩ ∧ mem (s.ap + 1) = bump_pc s res.to_op1.op1_imm ∧ mem s.ap = s.fp, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_call] at ht, simp [ht], ext; simp [ht] }, apply ensuresb_step h1 h2 _ _ nlt, intro h3, apply h' h3.2 h3.1 end theorem ret_ensuresb [char_ge : char_ge_2_63 F] {bound : β„•} (h : mem s.pc = ret_instr.to_nat) (h' : ensuresb bound mem ⟨mem (s.fp + -1), s.ap, mem (s.fp - 2)⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact ret_ne_jmp_rel (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨mem (s.fp + -1), s.ap, mem (s.fp - 2)⟩ ∧ true, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_ret] at ht, simp [ht], ext; simp [ht] }, apply ensuresb_step h1 h2 (Ξ» _, h') _ nlt end theorem call_ensuresb_trans [char_ge : char_ge_2_63 F] {bound : β„•} {P Q : β„• β†’ register_state F β†’ Prop} {res : res_spec} {call_abs : bool} {x : F} (hβ‚€ : mem s.pc = x) (h₁ : x = (call_instr call_abs res).to_nat) (hβ‚‚ : ensures_ret mem ⟨jump_pc s call_abs (compute_res mem s (op0_spec.ap_plus 1) res), s.ap + 2, s.ap + 2⟩ P) (h₃ : βˆ€ k ap, P k ⟨bump_pc s res.to_op1.op1_imm, ap, s.fp⟩ β†’ ensuresb bound mem ⟨bump_pc s res.to_op1.op1_imm, ap, s.fp⟩ (Ξ» k', Q (k + k' + 1))) : ensuresb (bound + 1) mem s Q := begin apply call_ensuresb (hβ‚€.trans h₁), intros h1 h2, apply ensuresb_trans hβ‚‚, dsimp, rintros k ⟨tpc, tap, tfp⟩, dsimp, rw [add_sub_assoc, add_sub_assoc, sub_self, add_zero], norm_num1, rw [h1, h2], rintros ⟨rfl, rfl, hP⟩, apply h₃ _ _ hP end theorem call_rec_ensuresb_trans [char_ge : char_ge_2_63 F] {bound : β„•} {P Q : β„• β†’ register_state F β†’ Prop} {res : res_spec} {call_abs : bool} {x : F} (hβ‚€ : mem s.pc = x) (h₁ : x = (call_instr call_abs res).to_nat) (hβ‚‚ : ensuresb_ret bound mem ⟨jump_pc s call_abs (compute_res mem s (op0_spec.ap_plus 1) res), s.ap + 2, s.ap + 2⟩ P) (h₃ : βˆ€ k ap, P k ⟨bump_pc s res.to_op1.op1_imm, ap, s.fp⟩ β†’ ensuresb bound mem ⟨bump_pc s res.to_op1.op1_imm, ap, s.fp⟩ (Ξ» k', Q (k + k' + 1))) : ensuresb (bound + 1) mem s Q := begin apply call_ensuresb (hβ‚€.trans h₁), intros h1 h2, apply ensuresb_rec_trans hβ‚‚, dsimp, rintros k ⟨tpc, tap, tfp⟩, dsimp, rw [add_sub_assoc, add_sub_assoc, sub_self, add_zero], norm_num1, rw [h1, h2], rintros ⟨rfl, rfl, hP⟩, apply h₃ _ _ hP end theorem advance_ap_ensuresb [char_ge : char_ge_2_63 F] {bound : β„•} {op0 : op0_spec} {res : res_spec} (h : mem s.pc = (advance_ap_instr op0 res).to_nat) (h' : ensuresb bound mem ⟨bump_pc s res.to_op1.op1_imm, s.ap + compute_res mem s op0 res, s.fp⟩ (Ξ» k t, P (k + 1) t)) : ensuresb (bound + 1) mem s P := begin intros n nlt exec, have h1 : Β¬ is_halting_state mem s, { rw not_is_halting_state_iff, left, rw h, intro h0, exact advance_ap_ne_jmp_rel _ _ (inst_unique char_ge _ _ h0) }, have h2 : βˆ€ t, next_state mem s t β†’ t = ⟨bump_pc s res.to_op1.op1_imm, s.ap + compute_res mem s op0 res, s.fp⟩ ∧ true, { intros t ht, rw [instruction.next_state_iff_of_eq h, ←instr.next_state, next_state_advance_ap] at ht, simp [ht], ext; simp [ht] }, apply ensuresb_step h1 h2 (Ξ» _, h') _ nlt end end next_state /- Theorems to handle the range-checked hypotheses. -/ def is_range_checked (rc_bound : β„•) (a : F) : Prop := βˆƒ n : β„•, n < rc_bound ∧ a = ↑n def range_checked (mem : F β†’ F) (rc_start : F) (k rc_bound : β„•) : Prop := βˆ€ i < k, is_range_checked rc_bound (mem (rc_start + i)) theorem range_checked_mono {mem : F β†’ F} {rc_start : F} {k rc_bound : β„•} (k' : β„•) (k'le : k' ≀ k) (h : range_checked mem rc_start k rc_bound) : range_checked mem rc_start k' rc_bound := Ξ» i iltk', h i (lt_of_lt_of_le iltk' k'le) theorem range_checked_add_left {mem : F β†’ F} {rc_start : F} {k' k rc_bound : β„•} (h : range_checked mem rc_start (k' + k) rc_bound) : range_checked mem rc_start k rc_bound := range_checked_mono _ (nat.le_add_left _ _) h theorem range_checked_add_right {mem : F β†’ F} {rc_start : F} {k' k rc_bound : β„•} (h : range_checked mem rc_start (k + k') rc_bound) : range_checked mem rc_start k rc_bound := range_checked_mono _ (nat.le_add_right _ _) h theorem range_checked_offset {mem : F β†’ F} {rc_start : F} {k' k rc_bound : β„•} (h : range_checked mem rc_start (k + k') rc_bound) : range_checked mem (rc_start + k') k rc_bound := begin intros i iltk, rcases h _ (add_lt_add_right iltk k') with ⟨i', hi'⟩, use i', rwa [add_assoc, ←nat.cast_add, add_comm k'] end theorem range_checked_offset' {mem : F β†’ F} {rc_start : F} {k' k rc_bound : β„•} (h : range_checked mem rc_start (k + k') rc_bound) : range_checked mem (rc_start + k) k' rc_bound := by { rw add_comm at h, exact range_checked_offset h } def rc_ensures (mem : F β†’ F) (rc_bound k : β„•) (a0 a1 : F) (P : Prop) := a1 = a0 + k ∧ (range_checked mem a0 k rc_bound β†’ P) theorem rc_ensures_comp {mem : F β†’ F} {rc_bound k0 k1 : β„•} {a0 a1 a2 : F} {P Q : Prop} (h0 : rc_ensures mem rc_bound k0 a0 a1 P) (h1 : rc_ensures mem rc_bound k1 a1 a2 Q) : rc_ensures mem rc_bound (k0 + k1) a0 a2 (P ∧ Q) := begin rcases h0 with ⟨hk0, rc0⟩, rcases h1 with ⟨hk1, rc1⟩, split, { rw [hk1, hk0, add_assoc, nat.cast_add] }, intro h, split, { exact rc0 (range_checked_add_right h) }, apply rc1, rw hk0, exact range_checked_offset' h end theorem rc_ensures_trans {mem : F β†’ F} {rc_bound k : β„•} {a0 a1 : F} {P Q : Prop} (h1 : rc_ensures mem rc_bound k a0 a1 P) (h2 : P β†’ Q) : rc_ensures mem rc_bound k a0 a1 Q := begin rcases h1 with ⟨hk, h⟩, exact ⟨hk, Ξ» h', h2 (h h')⟩ end end main /- Tactics. -/ lemma add_neg_eq_sub {G : Type*} [add_group G] (a b : G) : a + -b = a - b := by rw sub_eq_add_neg lemma sub_add_eq_add_neg_add {G : Type*} [add_group G] (a b c : G) : a - b + c = a + (-b + c) := by rw [sub_eq_add_neg, add_assoc] namespace tactic setup_tactic_parser variables (F : Type*) [field F] def loc_target : loc := loc.ns [none] meta def mk_simp_arg_list : list pexpr β†’ list simp_arg_type | [] := [] | (p :: ps) := simp_arg_type.expr p :: mk_simp_arg_list ps namespace interactive meta def arith_simps (loc : parse location) : tactic unit := let cfg : simp_config_ext := {}, simp_args := mk_simp_arg_list [ ``(int.cast_zero), ``(int.cast_one), ``(int.cast_bit0), ``(int.cast_bit1), ``(int.cast_neg), ``(nat.cast_zero), ``(nat.cast_one), ``(nat.cast_bit0), ``(nat.cast_bit1), ``(add_assoc), ``(add_sub_assoc), ``(add_zero), ``(add_right_neg), ``(add_left_neg), ``(add_neg_eq_sub), ``(sub_add_eq_add_neg_add), ``(mul_assoc)] in do try $ simp_core cfg.to_simp_config cfg.discharger tt simp_args [] loc >> skip, try $ (tactic.norm_num1 norm_num.derive.step loc >> (try $ simp_core cfg.to_simp_config cfg.discharger tt simp_args [] loc >> skip)) meta def ensures_simps (loc : parse location) : tactic unit := let cfg : simp_config_ext := {}, simp_args := mk_simp_arg_list [``(res_spec.to_op1), ``(compute_res), ``(compute_op1), ``(compute_op0), ``(op1_spec.op1_imm), ``(compute_dst), ``(bump_pc), ``(bump_ap), ``(jump_pc), ``(clip_checked), ``(checked_int_nz_eq), ``(int.cast_zero), ``(int.cast_one), ``(int.cast_bit0), ``(int.cast_bit1), ``(int.cast_neg), ``(nat.cast_zero), ``(nat.cast_one), ``(nat.cast_bit0), ``(nat.cast_bit1), ``(add_assoc), ``(add_sub_assoc), ``(add_zero), ``(add_right_neg), ``(add_left_neg), ``(add_neg_eq_sub), ``(sub_add_eq_add_neg_add), ``(mul_assoc)] in do try $ simp_core cfg.to_simp_config cfg.discharger tt simp_args [] loc >> skip, try (tactic.norm_num1 norm_num.derive.step loc >> (try $ simp_core cfg.to_simp_config cfg.discharger tt simp_args [] loc >> skip)) meta def unpack_memory (code : parse ident) (h : parse $ tk "at" *> ident) (pat : parse $ tk "with" *>rcases_patt_parse) : tactic unit := let cfg : simp_config_ext := {}, simp_args := mk_simp_arg_list [``(mem_at), ``(add_assoc), ``(and_true)] in do unfold [code] (loc.ns [h]), try $ simp_core cfg.to_simp_config cfg.discharger tt simp_args [] (loc.ns [h]) >> skip, try $ tactic.norm_num1 norm_num.derive.step (loc.ns [h]), eh ← resolve_name h, rcases (rcases_args.rcases none eh pat) meta def ensuresb_make_succ : tactic unit := (applyc ``ensuresb_test) <|> (applyc ``ensuresb_succ) meta def step_assert_eq (h : parse ident) (h' : parse ident?) (hw : parse $ tk "with" *> ident) := do ensuresb_make_succ, applyc ``assert_eq_ensuresb', eh ← get_local h, tactic.apply eh, tactic.clear eh, ensures_simps loc_target, match h' with | (some n) := do eh ← get_local n, try $ tactic.rewrite_target eh >> tactic.clear eh --, -- ensures_simps loc_target | none := skip end, intro hw meta def step_jump (h : parse ident) := do ensuresb_make_succ, applyc ``jump_ensuresb, eh ← get_local h, tactic.apply eh, tactic.clear eh, /- first subgoal: not an immediate argument-/ tactic.reflexivity, /- main goal -/ ensures_simps loc_target meta def step_jump_imm (h : parse ident) (h' : parse ident) := do ensuresb_make_succ, applyc ``jump_ensuresb', eh ← get_local h, tactic.apply eh, arith_simps loc_target, eh' ← get_local h', tactic.apply eh', /- main goal -/ ensures_simps loc_target, eh' ← get_local h', try $ tactic.rewrite_target eh', tactic.clear eh, tactic.clear eh', ensures_simps loc_target meta def step_jnz (h : parse ident) (h' : parse ident?) (hw : parse $ tk "with" *> ident) (hw' : parse ident):= do ensuresb_make_succ, applyc ``jnz_ensuresb, eh ← get_local h, tactic.apply eh, tactic.clear eh, swap, tactic.clear eh, focus $ do { ensures_simps loc_target }, match h' with | (some n) := do eh ← get_local n, try $ tactic.rewrite_target eh >> tactic.clear eh, ensures_simps loc_target | none := skip end, ehw' ← tactic.intro hw', try $ tactic.rewrite_target eh, swap, focus $ do { ensures_simps loc_target }, intro hw, match h' with | (some n) := do eh ← get_local n, try $ tactic.rewrite_target eh >> tactic.clear eh, ensures_simps loc_target | none := skip end meta def step_call (h : parse ident) := do ensuresb_make_succ, applyc ``call_ensuresb, eh ← get_local h, tactic.apply eh, tactic.clear eh, ensures_simps loc_target meta def step_ret (h : parse ident) := do ensuresb_make_succ, applyc ``ret_ensuresb, eh ← get_local h, tactic.apply eh, tactic.clear eh, ensures_simps loc_target meta def step_advance_ap (h : parse ident) (h' : parse ident?) := do ensuresb_make_succ, applyc ``advance_ap_ensuresb, eh ← get_local h, tactic.apply eh, tactic.clear eh, ensures_simps loc_target, match h' with | (some n) := do eh ← get_local n, try $ tactic.rewrite_target eh >> tactic.clear eh, ensures_simps loc_target | none := skip end meta def step_sub (h1 : parse ident) (h2 : parse parser.pexpr) : tactic unit := focus1 $ do eh ← get_local h1, ensuresb_make_succ, apply ``(call_ensuresb_trans _ %%eh %%h2 _), refl, all_goals' (tactic.clear eh >> ensures_simps loc_target) meta def step_rec_sub (h1 : parse ident) (h2 : parse parser.pexpr) : tactic unit := focus1 $ do eh ← get_local h1, ensuresb_make_succ, apply ``(call_rec_ensuresb_trans _ %%eh %%h2 _), refl, all_goals' (tactic.clear eh >> ensures_simps loc_target) meta def step_done : tactic unit := `[ ensuresb_make_succ, apply ensuresb_id ] /-- A weaker version of the interactive `use` tactic, which doesn't call `triv` -/ meta def use_only (l : parse pexpr_list_or_texpr) : tactic unit := focus1 $ tactic.use l /-- norm_num1 plus an extra simp rule -/ meta def norm_num2 : tactic unit := `[ norm_num1, try { simp only [add_neg_eq_sub] } ] section open interactive interactive.types expr private meta def mkdef_arg_p_aux : pexpr β†’ parser (name Γ— pexpr) | (app (app (macro _ [const `eq _ ]) (local_const x _ _ _)) h) := pure (x, h) | _ := fail "parse error" private meta def mkdef_arg_p : parser (name Γ— pexpr) := with_desc "expr = id" $ parser.pexpr 0 >>= mkdef_arg_p_aux /-- `mkdef h : x = t` introduces a new variable `x` into the context with assumption `h : x = t`. It is essentially a simplified version of `generalize'` with the equation syntax reversed. -/ meta def mkdef (h : parse ident) (_ : parse $ tk ":") (p : parse mkdef_arg_p) : tactic unit := propagate_tags $ do let (x, p) := p, e ← i_to_expr p, tgt ← target, tgt' ← to_expr ``(Ξ  x, x = %%e β†’ %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h end end interactive end tactic /- Some useful things for the autogenerated proofs. -/ section theorem assert_eq_reduction {F : Type} {a b c d : F} (h : c = d) (h' : a = c ∧ d = b) : a = b := (h'.1.trans h).trans h'.2 variables {F : Type*} [field F] lemma cond_aux1 {a b t : F} (h1 : t = a - b) (h2 : t = 0) : a = b := by { rw h2 at h1, exact eq_of_sub_eq_zero h1.symm } lemma cond_aux2 {a b t : F} (h1 : t = a - b) (h2 : t β‰  0) : a β‰  b := by { intro h3, apply h2, rwa [h3, sub_self] at h1 } lemma eq_sub_of_eq_add {a b c : F} (h: b = a + c) : a = b - c := eq_sub_of_add_eq h.symm -- a trick for inferring the value of the ap at proof time lemma of_register_state [decidable_eq F] {bound : β„•} {mem : F β†’ F} {s : register_state F} {P : β„• β†’ register_state F β†’ Prop} : (βˆ€ x, x = s β†’ ensuresb bound mem s P) β†’ ensuresb bound mem s P := Ξ» h, h s rfl /-- division with a default value -/ def ddiv [decidable_eq F] (a b c : F) : F := if b β‰  0 then a / b else c theorem ddiv_eq [decidable_eq F] {a b c : F} (h : b β‰  0) : ddiv a b c = a / b := if_pos h theorem eq_ddiv_of_eq_mul [decidable_eq F] {a b c : F} (h : a = c * b) : c = ddiv a b c := begin by_cases h' : b β‰  0, { rw [ddiv, if_pos h', eq_div_iff h', h] }, rw [ddiv, if_neg h'] end theorem exists_eq_ddiv_of_eq_mul [decidable_eq F] {a b c : F} (h : a = c * b) : βˆƒ d, c = ddiv a b d := ⟨c, eq_ddiv_of_eq_mul h⟩ -- for handling division by a constant lemma div_eq_mul_inv' {F : Type*} [field F] {x y z : F} (h : y * z = 1) : x / y = x * z := begin have : y β‰  0, { intro yzero, simp [yzero] at h, contradiction }, symmetry, apply eq_div_of_mul_eq this, rw [mul_assoc, mul_comm z, h, mul_one], end end
c33016fa0a7490908de69522dbdf7570f712e255
4727251e0cd73359b15b664c3170e5d754078599
/src/data/matrix/pequiv.lean
9e602fb011eac198d15809c486d27b1afd1ca952
[ "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
5,729
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.matrix.basic import data.pequiv /-! # partial equivalences for matrices Using partial equivalences to represent matrices. This file introduces the function `pequiv.to_matrix`, which returns a matrix containing ones and zeros. For any partial equivalence `f`, `f.to_matrix i j = 1 ↔ f i = some j`. The following important properties of this function are proved `to_matrix_trans : (f.trans g).to_matrix = f.to_matrix ⬝ g.to_matrix` `to_matrix_symm : f.symm.to_matrix = f.to_matrixα΅€` `to_matrix_refl : (pequiv.refl n).to_matrix = 1` `to_matrix_bot : βŠ₯.to_matrix = 0` This theory gives the matrix representation of projection linear maps, and their right inverses. For example, the matrix `(single (0 : fin 1) (i : fin n)).to_matrix` corresponds to the ith projection map from R^n to R. Any injective function `fin m β†’ fin n` gives rise to a `pequiv`, whose matrix is the projection map from R^m β†’ R^n represented by the same function. The transpose of this matrix is the right inverse of this map, sending anything not in the image to zero. ## notations This file uses the notation ` ⬝ ` for `matrix.mul` and `α΅€` for `matrix.transpose`. -/ namespace pequiv open matrix universes u v variables {k l m n : Type*} variables {Ξ± : Type v} open_locale matrix /-- `to_matrix` returns a matrix containing ones and zeros. `f.to_matrix i j` is `1` if `f i = some j` and `0` otherwise -/ def to_matrix [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] (f : m ≃. n) : matrix m n Ξ± | i j := if j ∈ f i then 1 else 0 lemma mul_matrix_apply [fintype m] [decidable_eq m] [semiring Ξ±] (f : l ≃. m) (M : matrix m n Ξ±) (i j) : (f.to_matrix ⬝ M) i j = option.cases_on (f i) 0 (Ξ» fi, M fi j) := begin dsimp [to_matrix, matrix.mul_apply], cases h : f i with fi, { simp [h] }, { rw finset.sum_eq_single fi; simp [h, eq_comm] {contextual := tt} } end lemma to_matrix_symm [decidable_eq m] [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] (f : m ≃. n) : (f.symm.to_matrix : matrix n m Ξ±) = f.to_matrixα΅€ := by ext; simp only [transpose, mem_iff_mem f, to_matrix]; congr @[simp] lemma to_matrix_refl [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] : ((pequiv.refl n).to_matrix : matrix n n Ξ±) = 1 := by ext; simp [to_matrix, one_apply]; congr lemma matrix_mul_apply [fintype m] [semiring Ξ±] [decidable_eq n] (M : matrix l m Ξ±) (f : m ≃. n) (i j) : (M ⬝ f.to_matrix) i j = option.cases_on (f.symm j) 0 (Ξ» fj, M i fj) := begin dsimp [to_matrix, matrix.mul_apply], cases h : f.symm j with fj, { simp [h, ← f.eq_some_iff] }, { rw finset.sum_eq_single fj, { simp [h, ← f.eq_some_iff], }, { intros b H n, simp [h, ← f.eq_some_iff, n.symm], }, { simp, } } end lemma to_pequiv_mul_matrix [fintype m] [decidable_eq m] [semiring Ξ±] (f : m ≃ m) (M : matrix m n Ξ±) : (f.to_pequiv.to_matrix ⬝ M) = Ξ» i, M (f i) := by { ext i j, rw [mul_matrix_apply, equiv.to_pequiv_apply] } lemma to_matrix_trans [fintype m] [decidable_eq m] [decidable_eq n] [semiring Ξ±] (f : l ≃. m) (g : m ≃. n) : ((f.trans g).to_matrix : matrix l n Ξ±) = f.to_matrix ⬝ g.to_matrix := begin ext i j, rw [mul_matrix_apply], dsimp [to_matrix, pequiv.trans], cases f i; simp end @[simp] lemma to_matrix_bot [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] : ((βŠ₯ : pequiv m n).to_matrix : matrix m n Ξ±) = 0 := rfl lemma to_matrix_injective [decidable_eq n] [monoid_with_zero Ξ±] [nontrivial Ξ±] : function.injective (@to_matrix m n Ξ± _ _ _) := begin classical, assume f g, refine not_imp_not.1 _, simp only [matrix.ext_iff.symm, to_matrix, pequiv.ext_iff, not_forall, exists_imp_distrib], assume i hi, use i, cases hf : f i with fi, { cases hg : g i with gi, { cc }, { use gi, simp, } }, { use fi, simp [hf.symm, ne.symm hi] } end lemma to_matrix_swap [decidable_eq n] [ring Ξ±] (i j : n) : (equiv.swap i j).to_pequiv.to_matrix = (1 : matrix n n Ξ±) - (single i i).to_matrix - (single j j).to_matrix + (single i j).to_matrix + (single j i).to_matrix := begin ext, dsimp [to_matrix, single, equiv.swap_apply_def, equiv.to_pequiv, one_apply], split_ifs; simp * at * end @[simp] lemma single_mul_single [fintype n] [decidable_eq k] [decidable_eq m] [decidable_eq n] [semiring Ξ±] (a : m) (b : n) (c : k) : ((single a b).to_matrix : matrix _ _ Ξ±) ⬝ (single b c).to_matrix = (single a c).to_matrix := by rw [← to_matrix_trans, single_trans_single] lemma single_mul_single_of_ne [fintype n] [decidable_eq n] [decidable_eq k] [decidable_eq m] [semiring Ξ±] {b₁ bβ‚‚ : n} (hb : b₁ β‰  bβ‚‚) (a : m) (c : k) : ((single a b₁).to_matrix : matrix _ _ Ξ±) ⬝ (single bβ‚‚ c).to_matrix = 0 := by rw [← to_matrix_trans, single_trans_single_of_ne hb, to_matrix_bot] /-- Restatement of `single_mul_single`, which will simplify expressions in `simp` normal form, when associativity may otherwise need to be carefully applied. -/ @[simp] lemma single_mul_single_right [fintype n] [fintype k] [decidable_eq n] [decidable_eq k] [decidable_eq m] [semiring Ξ±] (a : m) (b : n) (c : k) (M : matrix k l Ξ±) : (single a b).to_matrix ⬝ ((single b c).to_matrix ⬝ M) = (single a c).to_matrix ⬝ M := by rw [← matrix.mul_assoc, single_mul_single] /-- We can also define permutation matrices by permuting the rows of the identity matrix. -/ lemma equiv_to_pequiv_to_matrix [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] (Οƒ : equiv n n) (i j : n) : Οƒ.to_pequiv.to_matrix i j = (1 : matrix n n Ξ±) (Οƒ i) j := if_congr option.some_inj rfl rfl end pequiv
98fd7934f044c0c5a6467fdcc924dd8b63985de2
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/tests/lean/run/struct2.lean
8d4b1b0817cb442a7758e7140d16e4e1bd869b0a
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
232
lean
universe u structure A (Ξ± : Type u) := (f : (Ξ² : Type u) β†’ Ξ± β†’ Ξ² β†’ Ξ±) set_option pp.all true structure B (Ξ± : Type u) extends A Ξ± := (x : Nat) (f := fun Ξ² a b => a) #check B.f._default #check { x := 10 : B Nat}
116c0cffe3bdd114264d8e8b67e8dda315ac4958
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Core.lean
3ca4ab339317fabdb7adfc2bd08e18af54fa5f64
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
64,156
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura notation, basic datatypes and type classes -/ prelude notation `Prop` := Sort 0 notation f ` $ `:1 a:0 := f a /- Logical operations and relations -/ reserve prefix `Β¬`:40 reserve infixr ` ∧ `:35 reserve infixr ` /\ `:35 reserve infixr ` \/ `:30 reserve infixr ` ∨ `:30 reserve infix ` <-> `:20 reserve infix ` ↔ `:20 reserve infix ` = `:50 reserve infix ` == `:50 reserve infix ` != `:50 reserve infix ` ~= `:50 reserve infix ` β‰… `:50 reserve infix ` β‰  `:50 reserve infix ` β‰ˆ `:50 reserve infixr ` β–Έ `:75 /- types and Type constructors -/ reserve infixr ` Γ— `:35 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve infixl ` %β‚™ `:70 reserve prefix `-`:100 reserve infixr ` ^ `:80 reserve infixr ` ∘ `:90 reserve infix ` <= `:50 reserve infix ` ≀ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` β‰₯ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve prefix `!`:40 reserve infixl ` && `:35 reserve infixl ` || `:30 /- other symbols -/ reserve infixl ` ++ `:65 reserve infixr ` :: `:67 /- Control -/ reserve infixr ` <|> `:2 reserve infixr ` >>= `:55 reserve infixr ` >=> `:55 reserve infixl ` <*> `:60 reserve infixl ` <* ` :60 reserve infixr ` *> ` :60 reserve infixr ` >> ` :60 reserve infixr ` <$> `:100 reserve infixr ` <$ ` :100 reserve infixr ` $> ` :100 reserve infixl ` <&> `:100 universes u v w /-- Auxiliary unsafe constant used by the Compiler when erasing proofs from code. -/ unsafe axiom lcProof {Ξ± : Prop} : Ξ± /-- Auxiliary unsafe constant used by the Compiler to mark unreachable code. -/ unsafe axiom lcUnreachable {Ξ± : Sort u} : Ξ± @[inline] def id {Ξ± : Sort u} (a : Ξ±) : Ξ± := a def inline {Ξ± : Sort u} (a : Ξ±) : Ξ± := a @[inline] def flip {Ξ± : Sort u} {Ξ² : Sort v} {Ο† : Sort w} (f : Ξ± β†’ Ξ² β†’ Ο†) : Ξ² β†’ Ξ± β†’ Ο† := fun b a => f a b /- The kernel definitional equality test (t =?= s) has special support for idDelta applications. It implements the following rules 1) (idDelta t) =?= t 2) t =?= (idDelta t) 3) (idDelta t) =?= s IF (unfoldOf t) =?= s 4) t =?= idDelta s IF t =?= (unfoldOf s) This is mechanism for controlling the delta reduction (aka unfolding) used in the kernel. We use idDelta applications to address performance problems when Type checking theorems generated by the equation Compiler. -/ @[inline] def idDelta {Ξ± : Sort u} (a : Ξ±) : Ξ± := a /-- Gadget for optional parameter support. -/ @[reducible] def optParam (Ξ± : Sort u) (default : Ξ±) : Sort u := Ξ± /-- Gadget for marking output parameters in type classes. -/ @[reducible] def outParam (Ξ± : Sort u) : Sort u := Ξ± /-- Auxiliary Declaration used to implement the notation (a : Ξ±) -/ @[reducible] def typedExpr (Ξ± : Sort u) (a : Ξ±) : Ξ± := a /- `idRhs` is an auxiliary Declaration used in the equation Compiler to address performance issues when proving equational theorems. The equation Compiler uses it as a marker. -/ @[macroInline, reducible] def idRhs (Ξ± : Sort u) (a : Ξ±) : Ξ± := a inductive PUnit : Sort u | unit : PUnit /-- An abbreviation for `PUnit.{0}`, its most common instantiation. This Type should be preferred over `PUnit` where possible to avoid unnecessary universe parameters. -/ abbrev Unit : Type := PUnit @[matchPattern] abbrev Unit.unit : Unit := PUnit.unit /- Remark: thunks have an efficient implementation in the runtime. -/ structure Thunk (Ξ± : Type u) : Type u := (fn : Unit β†’ Ξ±) attribute [extern "lean_mk_thunk"] Thunk.mk @[noinline, extern "lean_thunk_pure"] protected def Thunk.pure {Ξ± : Type u} (a : Ξ±) : Thunk Ξ± := ⟨fun _ => a⟩ @[noinline, extern "lean_thunk_get_own"] protected def Thunk.get {Ξ± : Type u} (x : @& Thunk Ξ±) : Ξ± := x.fn () @[noinline, extern "lean_thunk_map"] protected def Thunk.map {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (x : Thunk Ξ±) : Thunk Ξ² := ⟨fun _ => f x.get⟩ @[noinline, extern "lean_thunk_bind"] protected def Thunk.bind {Ξ± : Type u} {Ξ² : Type v} (x : Thunk Ξ±) (f : Ξ± β†’ Thunk Ξ²) : Thunk Ξ² := ⟨fun _ => (f x.get).get⟩ /- Remark: tasks have an efficient implementation in the runtime. -/ structure Task (Ξ± : Type u) : Type u := (fn : Unit β†’ Ξ±) attribute [extern "lean_mk_task"] Task.mk @[noinline, extern "lean_task_pure"] protected def Task.pure {Ξ± : Type u} (a : Ξ±) : Task Ξ± := ⟨fun _ => a⟩ @[noinline, extern "lean_task_get"] protected def Task.get {Ξ± : Type u} (x : @& Task Ξ±) : Ξ± := x.fn () @[noinline, extern "lean_task_map"] protected def Task.map {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (x : Task Ξ±) : Task Ξ² := ⟨fun _ => f x.get⟩ @[noinline, extern "lean_task_bind"] protected def Task.bind {Ξ± : Type u} {Ξ² : Type v} (x : Task Ξ±) (f : Ξ± β†’ Task Ξ²) : Task Ξ² := ⟨fun _ => (f x.get).get⟩ inductive True : Prop | intro : True inductive False : Prop inductive Empty : Type def Not (a : Prop) : Prop := a β†’ False prefix `Β¬` := Not inductive Eq {Ξ± : Sort u} (a : Ξ±) : Ξ± β†’ Prop | refl {} : Eq a @[elabAsEliminator, inline, reducible] def Eq.ndrec.{u1, u2} {Ξ± : Sort u2} {a : Ξ±} {C : Ξ± β†’ Sort u1} (m : C a) {b : Ξ±} (h : Eq a b) : C b := @Eq.rec Ξ± a (fun Ξ± _ => C Ξ±) m b h @[elabAsEliminator, inline, reducible] def Eq.ndrecOn.{u1, u2} {Ξ± : Sort u2} {a : Ξ±} {C : Ξ± β†’ Sort u1} {b : Ξ±} (h : Eq a b) (m : C a) : C b := @Eq.rec Ξ± a (fun Ξ± _ => C Ξ±) m b h /- Initialize the Quotient Module, which effectively adds the following definitions: constant Quot {Ξ± : Sort u} (r : Ξ± β†’ Ξ± β†’ Prop) : Sort u constant Quot.mk {Ξ± : Sort u} (r : Ξ± β†’ Ξ± β†’ Prop) (a : Ξ±) : Quot r constant Quot.lift {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Sort v} (f : Ξ± β†’ Ξ²) : (βˆ€ a b : Ξ±, r a b β†’ Eq (f a) (f b)) β†’ Quot r β†’ Ξ² constant Quot.ind {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Prop} : (βˆ€ a : Ξ±, Ξ² (Quot.mk r a)) β†’ βˆ€ q : Quot r, Ξ² q -/ init_quot inductive HEq {Ξ± : Sort u} (a : Ξ±) : βˆ€ {Ξ² : Sort u}, Ξ² β†’ Prop | refl {} : HEq a structure Prod (Ξ± : Type u) (Ξ² : Type v) := (fst : Ξ±) (snd : Ξ²) attribute [unbox] Prod /-- Similar to `Prod`, but Ξ± and Ξ² can be propositions. We use this Type internally to automatically generate the brecOn recursor. -/ structure PProd (Ξ± : Sort u) (Ξ² : Sort v) := (fst : Ξ±) (snd : Ξ²) structure And (a b : Prop) : Prop := intro :: (left : a) (right : b) structure Iff (a b : Prop) : Prop := intro :: (mp : a β†’ b) (mpr : b β†’ a) /- Eq basic support -/ infix `=` := Eq @[matchPattern] def rfl {Ξ± : Sort u} {a : Ξ±} : a = a := Eq.refl a @[elabAsEliminator] theorem Eq.subst {Ξ± : Sort u} {P : Ξ± β†’ Prop} {a b : Ξ±} (h₁ : a = b) (hβ‚‚ : P a) : P b := Eq.ndrec hβ‚‚ h₁ infixr `β–Έ` := Eq.subst theorem Eq.trans {Ξ± : Sort u} {a b c : Ξ±} (h₁ : a = b) (hβ‚‚ : b = c) : a = c := hβ‚‚ β–Έ h₁ theorem Eq.symm {Ξ± : Sort u} {a b : Ξ±} (h : a = b) : b = a := h β–Έ rfl infix `~=` := HEq infix `β‰…` := HEq @[matchPattern] def HEq.rfl {Ξ± : Sort u} {a : Ξ±} : a β‰… a := HEq.refl a theorem eqOfHEq {Ξ± : Sort u} {a a' : Ξ±} (h : a β‰… a') : a = a' := have βˆ€ (Ξ±' : Sort u) (a' : Ξ±') (h₁ : @HEq Ξ± a Ξ±' a') (hβ‚‚ : Ξ± = Ξ±'), (Eq.recOn hβ‚‚ a : Ξ±') = a' := fun (Ξ±' : Sort u) (a' : Ξ±') (h₁ : @HEq Ξ± a Ξ±' a') => HEq.recOn h₁ (fun (hβ‚‚ : Ξ± = Ξ±) => rfl); show (Eq.ndrecOn (Eq.refl Ξ±) a : Ξ±) = a' from this Ξ± a' h (Eq.refl Ξ±) inductive Sum (Ξ± : Type u) (Ξ² : Type v) | inl (val : Ξ±) : Sum | inr (val : Ξ²) : Sum inductive PSum (Ξ± : Sort u) (Ξ² : Sort v) | inl (val : Ξ±) : PSum | inr (val : Ξ²) : PSum inductive Or (a b : Prop) : Prop | inl (h : a) : Or | inr (h : b) : Or def Or.introLeft {a : Prop} (b : Prop) (ha : a) : Or a b := Or.inl ha def Or.introRight (a : Prop) {b : Prop} (hb : b) : Or a b := Or.inr hb structure Sigma {Ξ± : Type u} (Ξ² : Ξ± β†’ Type v) := mk :: (fst : Ξ±) (snd : Ξ² fst) attribute [unbox] Sigma structure PSigma {Ξ± : Sort u} (Ξ² : Ξ± β†’ Sort v) := mk :: (fst : Ξ±) (snd : Ξ² fst) inductive Bool : Type | false : Bool | true : Bool /- Remark: Subtype must take a Sort instead of Type because of the axiom strongIndefiniteDescription. -/ structure Subtype {Ξ± : Sort u} (p : Ξ± β†’ Prop) := (val : Ξ±) (property : p val) inductive Exists {Ξ± : Sort u} (p : Ξ± β†’ Prop) : Prop | intro (w : Ξ±) (h : p w) : Exists class inductive Decidable (p : Prop) | isFalse (h : Β¬p) : Decidable | isTrue (h : p) : Decidable abbrev DecidablePred {Ξ± : Sort u} (r : Ξ± β†’ Prop) := βˆ€ (a : Ξ±), Decidable (r a) abbrev DecidableRel {Ξ± : Sort u} (r : Ξ± β†’ Ξ± β†’ Prop) := βˆ€ (a b : Ξ±), Decidable (r a b) abbrev DecidableEq (Ξ± : Sort u) := βˆ€ (a b : Ξ±), Decidable (a = b) def decEq {Ξ± : Sort u} [s : DecidableEq Ξ±] (a b : Ξ±) : Decidable (a = b) := s a b inductive Option (Ξ± : Type u) | none : Option | some (val : Ξ±) : Option attribute [unbox] Option export Option (none some) export Bool (false true) inductive List (T : Type u) | nil : List | cons (hd : T) (tl : List) : List infixr `::` := List.cons inductive Nat | zero : Nat | succ (n : Nat) : Nat /- Auxiliary axiom used to implement `sorry`. TODO: add this theorem on-demand. That is, we should only add it if after the first error. -/ axiom sorryAx (Ξ± : Sort u) (synthetic := true) : Ξ± /- Declare builtin and reserved notation -/ class HasZero (Ξ± : Type u) := mk {} :: (zero : Ξ±) class HasOne (Ξ± : Type u) := mk {} :: (one : Ξ±) class HasAdd (Ξ± : Type u) := (add : Ξ± β†’ Ξ± β†’ Ξ±) class HasMul (Ξ± : Type u) := (mul : Ξ± β†’ Ξ± β†’ Ξ±) class HasNeg (Ξ± : Type u) := (neg : Ξ± β†’ Ξ±) class HasSub (Ξ± : Type u) := (sub : Ξ± β†’ Ξ± β†’ Ξ±) class HasDiv (Ξ± : Type u) := (div : Ξ± β†’ Ξ± β†’ Ξ±) class HasMod (Ξ± : Type u) := (mod : Ξ± β†’ Ξ± β†’ Ξ±) class HasModN (Ξ± : Type u) := (modn : Ξ± β†’ Nat β†’ Ξ±) class HasLessEq (Ξ± : Type u) := (LessEq : Ξ± β†’ Ξ± β†’ Prop) class HasLess (Ξ± : Type u) := (Less : Ξ± β†’ Ξ± β†’ Prop) class HasBeq (Ξ± : Type u) := (beq : Ξ± β†’ Ξ± β†’ Bool) class HasAppend (Ξ± : Type u) := (append : Ξ± β†’ Ξ± β†’ Ξ±) class HasOrelse (Ξ± : Type u) := (orelse : Ξ± β†’ Ξ± β†’ Ξ±) class HasAndthen (Ξ± : Type u) := (andthen : Ξ± β†’ Ξ± β†’ Ξ±) class HasEquiv (Ξ± : Sort u) := (Equiv : Ξ± β†’ Ξ± β†’ Prop) class HasEmptyc (Ξ± : Type u) := (emptyc : Ξ±) class HasPow (Ξ± : Type u) (Ξ² : Type v) := (pow : Ξ± β†’ Ξ² β†’ Ξ±) infix `+` := HasAdd.add infix `*` := HasMul.mul infix `-` := HasSub.sub infix `/` := HasDiv.div infix `%` := HasMod.mod infix `%β‚™` := HasModN.modn prefix `-` := HasNeg.neg infix `<=` := HasLessEq.LessEq infix `≀` := HasLessEq.LessEq infix `<` := HasLess.Less infix `==` := HasBeq.beq infix `++` := HasAppend.append notation `βˆ…` := HasEmptyc.emptyc infix `β‰ˆ` := HasEquiv.Equiv infixr `^` := HasPow.pow infixr `/\` := And infixr `∧` := And infixr `\/` := Or infixr `∨` := Or infix `<->` := Iff infix `↔` := Iff -- notation `exists` binders `, ` r:(scoped P, Exists P) := r -- notation `βˆƒ` binders `, ` r:(scoped P, Exists P) := r infixr `<|>` := HasOrelse.orelse infixr `>>` := HasAndthen.andthen @[reducible] def GreaterEq {Ξ± : Type u} [HasLessEq Ξ±] (a b : Ξ±) : Prop := HasLessEq.LessEq b a @[reducible] def Greater {Ξ± : Type u} [HasLess Ξ±] (a b : Ξ±) : Prop := HasLess.Less b a infix `>=` := GreaterEq infix `β‰₯` := GreaterEq infix `>` := Greater @[inline] def bit0 {Ξ± : Type u} [s : HasAdd Ξ±] (a : Ξ±) : Ξ± := a + a @[inline] def bit1 {Ξ± : Type u} [s₁ : HasOne Ξ±] [sβ‚‚ : HasAdd Ξ±] (a : Ξ±) : Ξ± := (bit0 a) + 1 attribute [matchPattern] HasZero.zero HasOne.one bit0 bit1 HasAdd.add HasNeg.neg /- Nat basic instances -/ @[extern "lean_nat_add"] protected def Nat.add : (@& Nat) β†’ (@& Nat) β†’ Nat | a, Nat.zero => a | a, Nat.succ b => Nat.succ (Nat.add a b) /- We mark the following definitions as pattern to make sure they can be used in recursive equations, and reduced by the equation Compiler. -/ attribute [matchPattern] Nat.add Nat.add._main instance : HasZero Nat := ⟨Nat.zero⟩ instance : HasOne Nat := ⟨Nat.succ (Nat.zero)⟩ instance : HasAdd Nat := ⟨Nat.add⟩ /- Auxiliary constant used by equation compiler. -/ constant hugeFuel : Nat := 10000 def std.priority.default : Nat := 1000 def std.priority.max : Nat := 0xFFFFFFFF protected def Nat.prio := std.priority.default + 100 /- Global declarations of right binding strength If a Module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ def std.prec.max : Nat := 1024 -- the strength of application, identifiers, (, [, etc. def std.prec.arrow : Nat := 25 /- The next def is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ def std.prec.maxPlus : Nat := std.prec.max + 10 infixr `Γ—` := Prod -- notation for n-ary tuples /- Some type that is not a scalar value in our runtime. -/ structure NonScalar := (val : Nat) /- Some type that is not a scalar value in our runtime and is universe polymorphic. -/ inductive PNonScalar : Type u | mk (v : Nat) : PNonScalar /- For numeric literals notation -/ class HasOfNat (Ξ± : Type u) := (ofNat : Nat β†’ Ξ±) export HasOfNat (ofNat) instance : HasOfNat Nat := ⟨id⟩ /- sizeof -/ class HasSizeof (Ξ± : Sort u) := (sizeof : Ξ± β†’ Nat) export HasSizeof (sizeof) /- Declare sizeof instances and theorems for types declared before HasSizeof. From now on, the inductive Compiler will automatically generate sizeof instances and theorems. -/ /- Every Type `Ξ±` has a default HasSizeof instance that just returns 0 for every element of `Ξ±` -/ protected def default.sizeof (Ξ± : Sort u) : Ξ± β†’ Nat | a => 0 instance defaultHasSizeof (Ξ± : Sort u) : HasSizeof Ξ± := ⟨default.sizeof α⟩ protected def Nat.sizeof : Nat β†’ Nat | n => n instance : HasSizeof Nat := ⟨Nat.sizeof⟩ protected def Prod.sizeof {Ξ± : Type u} {Ξ² : Type v} [HasSizeof Ξ±] [HasSizeof Ξ²] : (Prod Ξ± Ξ²) β†’ Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (Ξ± : Type u) (Ξ² : Type v) [HasSizeof Ξ±] [HasSizeof Ξ²] : HasSizeof (Prod Ξ± Ξ²) := ⟨Prod.sizeof⟩ protected def Sum.sizeof {Ξ± : Type u} {Ξ² : Type v} [HasSizeof Ξ±] [HasSizeof Ξ²] : (Sum Ξ± Ξ²) β†’ Nat | Sum.inl a => 1 + sizeof a | Sum.inr b => 1 + sizeof b instance (Ξ± : Type u) (Ξ² : Type v) [HasSizeof Ξ±] [HasSizeof Ξ²] : HasSizeof (Sum Ξ± Ξ²) := ⟨Sum.sizeof⟩ protected def PSum.sizeof {Ξ± : Type u} {Ξ² : Type v} [HasSizeof Ξ±] [HasSizeof Ξ²] : (PSum Ξ± Ξ²) β†’ Nat | PSum.inl a => 1 + sizeof a | PSum.inr b => 1 + sizeof b instance (Ξ± : Type u) (Ξ² : Type v) [HasSizeof Ξ±] [HasSizeof Ξ²] : HasSizeof (PSum Ξ± Ξ²) := ⟨PSum.sizeof⟩ protected def Sigma.sizeof {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} [HasSizeof Ξ±] [βˆ€ a, HasSizeof (Ξ² a)] : Sigma Ξ² β†’ Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (Ξ± : Type u) (Ξ² : Ξ± β†’ Type v) [HasSizeof Ξ±] [βˆ€ a, HasSizeof (Ξ² a)] : HasSizeof (Sigma Ξ²) := ⟨Sigma.sizeof⟩ protected def PSigma.sizeof {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} [HasSizeof Ξ±] [βˆ€ a, HasSizeof (Ξ² a)] : PSigma Ξ² β†’ Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (Ξ± : Type u) (Ξ² : Ξ± β†’ Type v) [HasSizeof Ξ±] [βˆ€ a, HasSizeof (Ξ² a)] : HasSizeof (PSigma Ξ²) := ⟨PSigma.sizeof⟩ protected def PUnit.sizeof : PUnit β†’ Nat | u => 1 instance : HasSizeof PUnit := ⟨PUnit.sizeof⟩ protected def Bool.sizeof : Bool β†’ Nat | b => 1 instance : HasSizeof Bool := ⟨Bool.sizeof⟩ protected def Option.sizeof {Ξ± : Type u} [HasSizeof Ξ±] : Option Ξ± β†’ Nat | none => 1 | some a => 1 + sizeof a instance (Ξ± : Type u) [HasSizeof Ξ±] : HasSizeof (Option Ξ±) := ⟨Option.sizeof⟩ protected def List.sizeof {Ξ± : Type u} [HasSizeof Ξ±] : List Ξ± β†’ Nat | List.nil => 1 | List.cons a l => 1 + sizeof a + List.sizeof l instance (Ξ± : Type u) [HasSizeof Ξ±] : HasSizeof (List Ξ±) := ⟨List.sizeof⟩ protected def Subtype.sizeof {Ξ± : Type u} [HasSizeof Ξ±] {p : Ξ± β†’ Prop} : Subtype p β†’ Nat | ⟨a, _⟩ => sizeof a instance {Ξ± : Type u} [HasSizeof Ξ±] (p : Ξ± β†’ Prop) : HasSizeof (Subtype p) := ⟨Subtype.sizeof⟩ theorem natAddZero (n : Nat) : n + 0 = n := rfl theorem optParamEq (Ξ± : Sort u) (default : Ξ±) : optParam Ξ± default = Ξ± := rfl /-- Like `by applyInstance`, but not dependent on the tactic framework. -/ @[reducible] def inferInstance {Ξ± : Type u} [i : Ξ±] : Ξ± := i @[reducible, elabSimple] def inferInstanceAs (Ξ± : Type u) [i : Ξ±] : Ξ± := i /- Boolean operators -/ @[macroInline] def cond {a : Type u} : Bool β†’ a β†’ a β†’ a | true, x, y => x | false, x, y => y @[inline] def condEq {Ξ² : Sort u} (b : Bool) (h₁ : b = true β†’ Ξ²) (hβ‚‚ : b = false β†’ Ξ²) : Ξ² := @Bool.casesOn (Ξ» x => b = x β†’ Ξ²) b hβ‚‚ h₁ rfl @[macroInline] def or : Bool β†’ Bool β†’ Bool | true, _ => true | false, b => b @[macroInline] def and : Bool β†’ Bool β†’ Bool | false, _ => false | true, b => b @[macroInline] def not : Bool β†’ Bool | true => false | false => true @[macroInline] def xor : Bool β†’ Bool β†’ Bool | true, b => not b | false, b => b prefix `!` := not infix `||` := or infix `&&` := and @[extern c inline "#1 || #2"] def strictOr (b₁ bβ‚‚ : Bool) := b₁ || bβ‚‚ @[extern c inline "#1 && #2"] def strictAnd (b₁ bβ‚‚ : Bool) := b₁ && bβ‚‚ @[inline] def bne {Ξ± : Type u} [HasBeq Ξ±] (a b : Ξ±) : Bool := !(a == b) infix `!=` := bne /- Logical connectives an equality -/ def implies (a b : Prop) := a β†’ b theorem implies.trans {p q r : Prop} (h₁ : implies p q) (hβ‚‚ : implies q r) : implies p r := fun hp => hβ‚‚ (h₁ hp) def trivial : True := ⟨⟩ @[macroInline] def False.elim {C : Sort u} (h : False) : C := False.rec (fun _ => C) h @[macroInline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (hβ‚‚ : Β¬a) : b := False.elim (hβ‚‚ h₁) theorem mt {a b : Prop} (h₁ : a β†’ b) (hβ‚‚ : Β¬b) : Β¬a := fun ha => hβ‚‚ (h₁ ha) theorem notFalse : Β¬False := id -- proof irrelevance is built in theorem proofIrrel {a : Prop} (h₁ hβ‚‚ : a) : h₁ = hβ‚‚ := rfl theorem id.def {Ξ± : Sort u} (a : Ξ±) : id a = a := rfl @[macroInline] def Eq.mp {Ξ± Ξ² : Sort u} (h₁ : Ξ± = Ξ²) (hβ‚‚ : Ξ±) : Ξ² := Eq.recOn h₁ hβ‚‚ @[macroInline] def Eq.mpr {Ξ± Ξ² : Sort u} : (Ξ± = Ξ²) β†’ Ξ² β†’ Ξ± := fun h₁ hβ‚‚ => Eq.recOn (Eq.symm h₁) hβ‚‚ @[elabAsEliminator] theorem Eq.substr {Ξ± : Sort u} {p : Ξ± β†’ Prop} {a b : Ξ±} (h₁ : b = a) (hβ‚‚ : p a) : p b := Eq.subst (Eq.symm h₁) hβ‚‚ theorem congr {Ξ± : Sort u} {Ξ² : Sort v} {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {a₁ aβ‚‚ : Ξ±} (h₁ : f₁ = fβ‚‚) (hβ‚‚ : a₁ = aβ‚‚) : f₁ a₁ = fβ‚‚ aβ‚‚ := Eq.subst h₁ (Eq.subst hβ‚‚ rfl) theorem congrFun {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} {f g : βˆ€ x, Ξ² x} (h : f = g) (a : Ξ±) : f a = g a := Eq.subst h (Eq.refl (f a)) theorem congrArg {Ξ± : Sort u} {Ξ² : Sort v} {a₁ aβ‚‚ : Ξ±} (f : Ξ± β†’ Ξ²) (h : a₁ = aβ‚‚) : f a₁ = f aβ‚‚ := congr rfl h theorem transRelLeft {Ξ± : Sort u} {a b c : Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) (h₁ : r a b) (hβ‚‚ : b = c) : r a c := hβ‚‚ β–Έ h₁ theorem transRelRight {Ξ± : Sort u} {a b c : Ξ±} (r : Ξ± β†’ Ξ± β†’ Prop) (h₁ : a = b) (hβ‚‚ : r b c) : r a c := h₁.symm β–Έ hβ‚‚ theorem ofEqTrue {p : Prop} (h : p = True) : p := h.symm β–Έ trivial theorem notOfEqFalse {p : Prop} (h : p = False) : Β¬p := fun hp => h β–Έ hp @[macroInline] def cast {Ξ± Ξ² : Sort u} (h : Ξ± = Ξ²) (a : Ξ±) : Ξ² := Eq.rec a h theorem castProofIrrel {Ξ± Ξ² : Sort u} (h₁ hβ‚‚ : Ξ± = Ξ²) (a : Ξ±) : cast h₁ a = cast hβ‚‚ a := rfl theorem castEq {Ξ± : Sort u} (h : Ξ± = Ξ±) (a : Ξ±) : cast h a = a := rfl @[reducible] def Ne {Ξ± : Sort u} (a b : Ξ±) := Β¬(a = b) infix `β‰ ` := Ne theorem Ne.def {Ξ± : Sort u} (a b : Ξ±) : a β‰  b = Β¬ (a = b) := rfl section Ne variable {Ξ± : Sort u} variables {a b : Ξ±} {p : Prop} theorem Ne.intro (h : a = b β†’ False) : a β‰  b := h theorem Ne.elim (h : a β‰  b) : a = b β†’ False := h theorem Ne.irrefl (h : a β‰  a) : False := h rfl theorem Ne.symm (h : a β‰  b) : b β‰  a := fun h₁ => h (h₁.symm) theorem falseOfNe : a β‰  a β†’ False := Ne.irrefl theorem neFalseOfSelf : p β†’ p β‰  False := fun (hp : p) (h : p = False) => h β–Έ hp theorem neTrueOfNot : Β¬p β†’ p β‰  True := fun (hnp : Β¬p) (h : p = True) => (h β–Έ hnp) trivial theorem trueNeFalse : Β¬True = False := neFalseOfSelf trivial end Ne theorem eqFalseOfNeTrue : βˆ€ {b : Bool}, b β‰  true β†’ b = false | true, h => False.elim (h rfl) | false, h => rfl theorem eqTrueOfNeFalse : βˆ€ {b : Bool}, b β‰  false β†’ b = true | true, h => rfl | false, h => False.elim (h rfl) theorem neFalseOfEqTrue : βˆ€ {b : Bool}, b = true β†’ b β‰  false | true, _ => fun h => Bool.noConfusion h | false, h => Bool.noConfusion h theorem neTrueOfEqFalse : βˆ€ {b : Bool}, b = false β†’ b β‰  true | true, h => Bool.noConfusion h | false, _ => fun h => Bool.noConfusion h section variables {Ξ± Ξ² Ο† : Sort u} {a a' : Ξ±} {b b' : Ξ²} {c : Ο†} @[elabAsEliminator] theorem HEq.ndrec.{u1, u2} {Ξ± : Sort u2} {a : Ξ±} {C : βˆ€ {Ξ² : Sort u2}, Ξ² β†’ Sort u1} (m : C a) {Ξ² : Sort u2} {b : Ξ²} (h : a β‰… b) : C b := @HEq.rec Ξ± a (fun Ξ² b _ => C b) m Ξ² b h @[elabAsEliminator] theorem HEq.ndrecOn.{u1, u2} {Ξ± : Sort u2} {a : Ξ±} {C : βˆ€ {Ξ² : Sort u2}, Ξ² β†’ Sort u1} {Ξ² : Sort u2} {b : Ξ²} (h : a β‰… b) (m : C a) : C b := @HEq.rec Ξ± a (fun Ξ² b _ => C b) m Ξ² b h theorem HEq.elim {Ξ± : Sort u} {a : Ξ±} {p : Ξ± β†’ Sort v} {b : Ξ±} (h₁ : a β‰… b) (hβ‚‚ : p a) : p b := Eq.recOn (eqOfHEq h₁) hβ‚‚ theorem HEq.subst {p : βˆ€ (T : Sort u), T β†’ Prop} (h₁ : a β‰… b) (hβ‚‚ : p Ξ± a) : p Ξ² b := HEq.ndrecOn h₁ hβ‚‚ theorem HEq.symm (h : a β‰… b) : b β‰… a := HEq.ndrecOn h (HEq.refl a) theorem heqOfEq (h : a = a') : a β‰… a' := Eq.subst h (HEq.refl a) theorem HEq.trans (h₁ : a β‰… b) (hβ‚‚ : b β‰… c) : a β‰… c := HEq.subst hβ‚‚ h₁ theorem heqOfHEqOfEq (h₁ : a β‰… b) (hβ‚‚ : b = b') : a β‰… b' := HEq.trans h₁ (heqOfEq hβ‚‚) theorem heqOfEqOfHEq (h₁ : a = a') (hβ‚‚ : a' β‰… b) : a β‰… b := HEq.trans (heqOfEq h₁) hβ‚‚ def typeEqOfHEq (h : a β‰… b) : Ξ± = Ξ² := HEq.ndrecOn h (Eq.refl Ξ±) end theorem eqRecHEq {Ξ± : Sort u} {Ο† : Ξ± β†’ Sort v} : βˆ€ {a a' : Ξ±} (h : a = a') (p : Ο† a), (Eq.recOn h p : Ο† a') β‰… p | a, _, rfl, p => HEq.refl p theorem ofHEqTrue {a : Prop} (h : a β‰… True) : a := ofEqTrue (eqOfHEq h) theorem castHEq : βˆ€ {Ξ± Ξ² : Sort u} (h : Ξ± = Ξ²) (a : Ξ±), cast h a β‰… a | Ξ±, _, rfl, a => HEq.refl a variables {a b c d : Prop} theorem And.elim (h₁ : a ∧ b) (hβ‚‚ : a β†’ b β†’ c) : c := And.rec hβ‚‚ h₁ theorem And.swap : a ∧ b β†’ b ∧ a := fun ⟨ha, hb⟩ => ⟨hb, ha⟩ def And.symm := @And.swap theorem Or.elim (h₁ : a ∨ b) (hβ‚‚ : a β†’ c) (h₃ : b β†’ c) : c := Or.rec hβ‚‚ h₃ h₁ theorem Or.swap (h : a ∨ b) : b ∨ a := Or.elim h Or.inr Or.inl def Or.symm := @Or.swap /- xor -/ def Xor (a b : Prop) : Prop := (a ∧ Β¬ b) ∨ (b ∧ Β¬ a) @[recursor 5] theorem Iff.elim (h₁ : (a β†’ b) β†’ (b β†’ a) β†’ c) (hβ‚‚ : a ↔ b) : c := Iff.rec h₁ hβ‚‚ theorem Iff.left : (a ↔ b) β†’ a β†’ b := Iff.mp theorem Iff.right : (a ↔ b) β†’ b β†’ a := Iff.mpr theorem iffIffImpliesAndImplies (a b : Prop) : (a ↔ b) ↔ (a β†’ b) ∧ (b β†’ a) := Iff.intro (fun h => And.intro h.mp h.mpr) (fun h => Iff.intro h.left h.right) theorem Iff.refl (a : Prop) : a ↔ a := Iff.intro (fun h => h) (fun h => h) theorem Iff.rfl {a : Prop} : a ↔ a := Iff.refl a theorem Iff.trans (h₁ : a ↔ b) (hβ‚‚ : b ↔ c) : a ↔ c := Iff.intro (fun ha => Iff.mp hβ‚‚ (Iff.mp h₁ ha)) (fun hc => Iff.mpr h₁ (Iff.mpr hβ‚‚ hc)) theorem Iff.symm (h : a ↔ b) : b ↔ a := Iff.intro (Iff.right h) (Iff.left h) theorem Iff.comm : (a ↔ b) ↔ (b ↔ a) := Iff.intro Iff.symm Iff.symm theorem Eq.toIff {a b : Prop} (h : a = b) : a ↔ b := Eq.recOn h Iff.rfl theorem neqOfNotIff {a b : Prop} : Β¬(a ↔ b) β†’ a β‰  b := fun h₁ hβ‚‚ => have a ↔ b from Eq.subst hβ‚‚ (Iff.refl a); absurd this h₁ theorem notIffNotOfIff (h₁ : a ↔ b) : Β¬a ↔ Β¬b := Iff.intro (fun (hna : Β¬ a) (hb : b) => hna (Iff.right h₁ hb)) (fun (hnb : Β¬ b) (ha : a) => hnb (Iff.left h₁ ha)) theorem ofIffTrue (h : a ↔ True) : a := Iff.mp (Iff.symm h) trivial theorem notOfIffFalse : (a ↔ False) β†’ Β¬a := Iff.mp theorem iffTrueIntro (h : a) : a ↔ True := Iff.intro (fun hl => trivial) (fun hr => h) theorem iffFalseIntro (h : Β¬a) : a ↔ False := Iff.intro h (False.rec (fun _ => a)) theorem notNotIntro (ha : a) : ¬¬a := fun hna => hna ha theorem notTrue : (Β¬ True) ↔ False := iffFalseIntro (notNotIntro trivial) /- or resolution rulses -/ theorem resolveLeft {a b : Prop} (h : a ∨ b) (na : Β¬ a) : b := Or.elim h (fun ha => absurd ha na) id theorem negResolveLeft {a b : Prop} (h : Β¬ a ∨ b) (ha : a) : b := Or.elim h (fun na => absurd ha na) id theorem resolveRight {a b : Prop} (h : a ∨ b) (nb : Β¬ b) : a := Or.elim h id (fun hb => absurd hb nb) theorem negResolveRight {a b : Prop} (h : a ∨ Β¬ b) (hb : b) : a := Or.elim h id (fun nb => absurd hb nb) /- Exists -/ theorem Exists.elim {Ξ± : Sort u} {p : Ξ± β†’ Prop} {b : Prop} (h₁ : Exists (fun x => p x)) (hβ‚‚ : βˆ€ (a : Ξ±), p a β†’ b) : b := Exists.rec hβ‚‚ h₁ /- Decidable -/ @[inlineIfReduce, nospecialize] def Decidable.decide (p : Prop) [h : Decidable p] : Bool := Decidable.casesOn h (fun h₁ => false) (fun hβ‚‚ => true) export Decidable (isTrue isFalse decide) instance beqOfEq {Ξ± : Type u} [DecidableEq Ξ±] : HasBeq Ξ± := ⟨fun a b => decide (a = b)⟩ theorem decideTrueEqTrue (h : Decidable True) : @decide True h = true := match h with | isTrue h => rfl | isFalse h => False.elim (Iff.mp notTrue h) theorem decideFalseEqFalse (h : Decidable False) : @decide False h = false := match h with | isFalse h => rfl | isTrue h => False.elim h theorem decideEqTrue : βˆ€ {p : Prop} [s : Decidable p], p β†’ decide p = true | _, isTrue _, _ => rfl | _, isFalse h₁, hβ‚‚ => absurd hβ‚‚ h₁ theorem decideEqFalse : βˆ€ {p : Prop} [s : Decidable p], Β¬p β†’ decide p = false | _, isTrue h₁, hβ‚‚ => absurd h₁ hβ‚‚ | _, isFalse h, _ => rfl theorem ofDecideEqTrue {p : Prop} [s : Decidable p] : decide p = true β†’ p := fun h => match s with | isTrue h₁ => h₁ | isFalse h₁ => absurd h (neTrueOfEqFalse (decideEqFalse h₁)) theorem ofDecideEqFalse {p : Prop} [s : Decidable p] : decide p = false β†’ Β¬p := fun h => match s with | isTrue h₁ => absurd h (neFalseOfEqTrue (decideEqTrue h₁)) | isFalse h₁ => h₁ /-- Similar to `decide`, but uses an explicit instance -/ @[inline] def toBoolUsing {p : Prop} (d : Decidable p) : Bool := @decide p d theorem toBoolUsingEqTrue {p : Prop} (d : Decidable p) (h : p) : toBoolUsing d = true := @decideEqTrue _ d h theorem ofBoolUsingEqTrue {p : Prop} {d : Decidable p} (h : toBoolUsing d = true) : p := @ofDecideEqTrue _ d h theorem ofBoolUsingEqFalse {p : Prop} {d : Decidable p} (h : toBoolUsing d = false) : Β¬ p := @ofDecideEqFalse _ d h instance : Decidable True := isTrue trivial instance : Decidable False := isFalse notFalse -- We use "dependent" if-then-else to be able to communicate the if-then-else condition -- to the branches @[macroInline] def dite {Ξ± : Sort u} (c : Prop) [h : Decidable c] : (c β†’ Ξ±) β†’ (Β¬ c β†’ Ξ±) β†’ Ξ± := fun t e => Decidable.casesOn h e t /- if-then-else -/ @[macroInline] def ite {Ξ± : Sort u} (c : Prop) [h : Decidable c] (t e : Ξ±) : Ξ± := Decidable.casesOn h (fun hnc => e) (fun hc => t) namespace Decidable variables {p q : Prop} def recOnTrue [h : Decidable p] {h₁ : p β†’ Sort u} {hβ‚‚ : Β¬p β†’ Sort u} (h₃ : p) (hβ‚„ : h₁ h₃) : (Decidable.recOn h hβ‚‚ h₁ : Sort u) := Decidable.casesOn h (fun h => False.rec _ (h h₃)) (fun h => hβ‚„) def recOnFalse [h : Decidable p] {h₁ : p β†’ Sort u} {hβ‚‚ : Β¬p β†’ Sort u} (h₃ : Β¬p) (hβ‚„ : hβ‚‚ h₃) : (Decidable.recOn h hβ‚‚ h₁ : Sort u) := Decidable.casesOn h (fun h => hβ‚„) (fun h => False.rec _ (h₃ h)) @[macroInline] def byCases {q : Sort u} [s : Decidable p] (h1 : p β†’ q) (h2 : Β¬p β†’ q) : q := match s with | isTrue h => h1 h | isFalse h => h2 h theorem em (p : Prop) [Decidable p] : p ∨ Β¬p := byCases Or.inl Or.inr theorem byContradiction [Decidable p] (h : Β¬p β†’ False) : p := byCases id (fun np => False.elim (h np)) theorem ofNotNot [Decidable p] : Β¬ Β¬ p β†’ p := fun hnn => byContradiction (fun hn => absurd hn hnn) theorem notNotIff (p) [Decidable p] : (Β¬ Β¬ p) ↔ p := Iff.intro ofNotNot notNotIntro theorem notAndIffOrNot (p q : Prop) [d₁ : Decidable p] [dβ‚‚ : Decidable q] : Β¬ (p ∧ q) ↔ Β¬ p ∨ Β¬ q := Iff.intro (fun h => match d₁, dβ‚‚ with | isTrue h₁, isTrue hβ‚‚ => absurd (And.intro h₁ hβ‚‚) h | _, isFalse hβ‚‚ => Or.inr hβ‚‚ | isFalse h₁, _ => Or.inl h₁) (fun (h) ⟨hp, hq⟩ => Or.elim h (fun h => h hp) (fun h => h hq)) end Decidable section variables {p q : Prop} @[inline] def decidableOfDecidableOfIff (hp : Decidable p) (h : p ↔ q) : Decidable q := if hp : p then isTrue (Iff.mp h hp) else isFalse (Iff.mp (notIffNotOfIff h) hp) @[inline] def decidableOfDecidableOfEq (hp : Decidable p) (h : p = q) : Decidable q := decidableOfDecidableOfIff hp h.toIff end section variables {p q : Prop} @[macroInline] instance [Decidable p] [Decidable q] : Decidable (p ∧ q) := if hp : p then if hq : q then isTrue ⟨hp, hq⟩ else isFalse (fun h => hq (And.right h)) else isFalse (fun h => hp (And.left h)) @[macroInline] instance [Decidable p] [Decidable q] : Decidable (p ∨ q) := if hp : p then isTrue (Or.inl hp) else if hq : q then isTrue (Or.inr hq) else isFalse (fun h => Or.elim h hp hq) instance [Decidable p] : Decidable (Β¬p) := if hp : p then isFalse (absurd hp) else isTrue hp @[macroInline] instance implies.Decidable [Decidable p] [Decidable q] : Decidable (p β†’ q) := if hp : p then if hq : q then isTrue (fun h => hq) else isFalse (fun h => absurd (h hp) hq) else isTrue (fun h => absurd h hp) instance [Decidable p] [Decidable q] : Decidable (p ↔ q) := if hp : p then if hq : q then isTrue ⟨fun _ => hq, fun _ => hp⟩ else isFalse $ fun h => hq (h.1 hp) else if hq : q then isFalse $ fun h => hp (h.2 hq) else isTrue $ ⟨fun h => absurd h hp, fun h => absurd h hq⟩ instance [Decidable p] [Decidable q] : Decidable (Xor p q) := if hp : p then if hq : q then isFalse (fun h => Or.elim h (fun ⟨_, h⟩ => h hq : Β¬(p ∧ Β¬ q)) (fun ⟨_, h⟩ => h hp : Β¬(q ∧ Β¬ p))) else isTrue $ Or.inl ⟨hp, hq⟩ else if hq : q then isTrue $ Or.inr ⟨hq, hp⟩ else isFalse (fun h => Or.elim h (fun ⟨h, _⟩ => hp h : Β¬(p ∧ Β¬ q)) (fun ⟨h, _⟩ => hq h : Β¬(q ∧ Β¬ p))) end @[inline] instance {Ξ± : Sort u} [DecidableEq Ξ±] (a b : Ξ±) : Decidable (a β‰  b) := match decEq a b with | isTrue h => isFalse $ fun h' => absurd h h' | isFalse h => isTrue h theorem Bool.falseNeTrue (h : false = true) : False := Bool.noConfusion h @[inline] instance : DecidableEq Bool := fun a b => match a, b with | false, false => isTrue rfl | false, true => isFalse Bool.falseNeTrue | true, false => isFalse (Ne.symm Bool.falseNeTrue) | true, true => isTrue rfl /- if-then-else expression theorems -/ theorem ifPos {c : Prop} [h : Decidable c] (hc : c) {Ξ± : Sort u} {t e : Ξ±} : (ite c t e) = t := match h with | (isTrue hc) => rfl | (isFalse hnc) => absurd hc hnc theorem ifNeg {c : Prop} [h : Decidable c] (hnc : Β¬c) {Ξ± : Sort u} {t e : Ξ±} : (ite c t e) = e := match h with | (isTrue hc) => absurd hc hnc | (isFalse hnc) => rfl theorem difPos {c : Prop} [h : Decidable c] (hc : c) {Ξ± : Sort u} {t : c β†’ Ξ±} {e : Β¬ c β†’ Ξ±} : (dite c t e) = t hc := match h with | (isTrue hc) => rfl | (isFalse hnc) => absurd hc hnc theorem difNeg {c : Prop} [h : Decidable c] (hnc : Β¬c) {Ξ± : Sort u} {t : c β†’ Ξ±} {e : Β¬ c β†’ Ξ±} : (dite c t e) = e hnc := match h with | (isTrue hc) => absurd hc hnc | (isFalse hnc) => rfl -- Remark: dite and ite are "defally equal" when we ignore the proofs. theorem difEqIf (c : Prop) [h : Decidable c] {Ξ± : Sort u} (t : Ξ±) (e : Ξ±) : dite c (fun h => t) (fun h => e) = ite c t e := match h with | (isTrue hc) => rfl | (isFalse hnc) => rfl instance {c t e : Prop} [dC : Decidable c] [dT : Decidable t] [dE : Decidable e] : Decidable (if c then t else e) := match dC with | (isTrue hc) => dT | (isFalse hc) => dE instance {c : Prop} {t : c β†’ Prop} {e : Β¬c β†’ Prop} [dC : Decidable c] [dT : βˆ€ h, Decidable (t h)] [dE : βˆ€ h, Decidable (e h)] : Decidable (if h : c then t h else e h) := match dC with | (isTrue hc) => dT hc | (isFalse hc) => dE hc /-- Universe lifting operation -/ structure ULift.{r, s} (Ξ± : Type s) : Type (max s r) := up :: (down : Ξ±) namespace ULift /- Bijection between Ξ± and ULift.{v} Ξ± -/ theorem upDown {Ξ± : Type u} : βˆ€ (b : ULift.{v} Ξ±), up (down b) = b | up a => rfl theorem downUp {Ξ± : Type u} (a : Ξ±) : down (up.{v} a) = a := rfl end ULift /-- Universe lifting operation from Sort to Type -/ structure PLift (Ξ± : Sort u) : Type u := up :: (down : Ξ±) namespace PLift /- Bijection between Ξ± and PLift Ξ± -/ theorem upDown {Ξ± : Sort u} : βˆ€ (b : PLift Ξ±), up (down b) = b | up a => rfl theorem downUp {Ξ± : Sort u} (a : Ξ±) : down (up a) = a := rfl end PLift /- pointed types -/ structure PointedType := (type : Type u) (val : type) /- Inhabited -/ class Inhabited (Ξ± : Sort u) := mk {} :: (default : Ξ±) constant arbitrary (Ξ± : Sort u) [Inhabited Ξ±] : Ξ± := Inhabited.default Ξ± instance Prop.Inhabited : Inhabited Prop := ⟨True⟩ instance Fun.Inhabited (Ξ± : Sort u) {Ξ² : Sort v} [h : Inhabited Ξ²] : Inhabited (Ξ± β†’ Ξ²) := Inhabited.casesOn h (fun b => ⟨fun a => b⟩) instance Forall.Inhabited (Ξ± : Sort u) {Ξ² : Ξ± β†’ Sort v} [βˆ€ x, Inhabited (Ξ² x)] : Inhabited (βˆ€ x, Ξ² x) := ⟨fun a => arbitrary (Ξ² a)⟩ instance : Inhabited Bool := ⟨false⟩ instance : Inhabited True := ⟨trivial⟩ instance : Inhabited Nat := ⟨0⟩ instance : Inhabited NonScalar := ⟨⟨arbitrary _⟩⟩ instance : Inhabited PNonScalar.{u} := ⟨⟨arbitrary _⟩⟩ instance : Inhabited PointedType := ⟨{type := PUnit, val := ⟨⟩}⟩ class inductive Nonempty (Ξ± : Sort u) : Prop | intro (val : Ξ±) : Nonempty protected def Nonempty.elim {Ξ± : Sort u} {p : Prop} (h₁ : Nonempty Ξ±) (hβ‚‚ : Ξ± β†’ p) : p := Nonempty.rec hβ‚‚ h₁ instance nonemptyOfInhabited {Ξ± : Sort u} [Inhabited Ξ±] : Nonempty Ξ± := ⟨arbitrary α⟩ theorem nonemptyOfExists {Ξ± : Sort u} {p : Ξ± β†’ Prop} : Exists (fun x => p x) β†’ Nonempty Ξ± | ⟨w, h⟩ => ⟨w⟩ /- Subsingleton -/ class inductive Subsingleton (Ξ± : Sort u) : Prop | intro (h : βˆ€ (a b : Ξ±), a = b) : Subsingleton protected def Subsingleton.elim {Ξ± : Sort u} [h : Subsingleton Ξ±] : βˆ€ (a b : Ξ±), a = b := Subsingleton.casesOn h (fun p => p) protected def Subsingleton.helim {Ξ± Ξ² : Sort u} [h : Subsingleton Ξ±] (h : Ξ± = Ξ²) : βˆ€ (a : Ξ±) (b : Ξ²), a β‰… b := Eq.recOn h (fun a b => heqOfEq (Subsingleton.elim a b)) instance subsingletonProp (p : Prop) : Subsingleton p := ⟨fun a b => proofIrrel a b⟩ instance (p : Prop) : Subsingleton (Decidable p) := Subsingleton.intro $ fun d₁ => match d₁ with | (isTrue t₁) => fun dβ‚‚ => match dβ‚‚ with | (isTrue tβ‚‚) => Eq.recOn (proofIrrel t₁ tβ‚‚) rfl | (isFalse fβ‚‚) => absurd t₁ fβ‚‚ | (isFalse f₁) => fun dβ‚‚ => match dβ‚‚ with | (isTrue tβ‚‚) => absurd tβ‚‚ f₁ | (isFalse fβ‚‚) => Eq.recOn (proofIrrel f₁ fβ‚‚) rfl protected theorem recSubsingleton {p : Prop} [h : Decidable p] {h₁ : p β†’ Sort u} {hβ‚‚ : Β¬p β†’ Sort u} [h₃ : βˆ€ (h : p), Subsingleton (h₁ h)] [hβ‚„ : βˆ€ (h : Β¬p), Subsingleton (hβ‚‚ h)] : Subsingleton (Decidable.casesOn h hβ‚‚ h₁) := match h with | (isTrue h) => h₃ h | (isFalse h) => hβ‚„ h section relation variables {Ξ± : Sort u} {Ξ² : Sort v} (r : Ξ² β†’ Ξ² β†’ Prop) def Reflexive := βˆ€ x, r x x def Symmetric := βˆ€ {x y}, r x y β†’ r y x def Transitive := βˆ€ {x y z}, r x y β†’ r y z β†’ r x z def Equivalence := Reflexive r ∧ Symmetric r ∧ Transitive r def Total := βˆ€ x y, r x y ∨ r y x def mkEquivalence (rfl : Reflexive r) (symm : Symmetric r) (trans : Transitive r) : Equivalence r := ⟨rfl, @symm, @trans⟩ def Irreflexive := βˆ€ x, Β¬ r x x def AntiSymmetric := βˆ€ {x y}, r x y β†’ r y x β†’ x = y def emptyRelation (a₁ aβ‚‚ : Ξ±) : Prop := False def Subrelation (q r : Ξ² β†’ Ξ² β†’ Prop) := βˆ€ {x y}, q x y β†’ r x y def InvImage (f : Ξ± β†’ Ξ²) : Ξ± β†’ Ξ± β†’ Prop := fun a₁ aβ‚‚ => r (f a₁) (f aβ‚‚) theorem InvImage.Transitive (f : Ξ± β†’ Ξ²) (h : Transitive r) : Transitive (InvImage r f) := fun (a₁ aβ‚‚ a₃ : Ξ±) (h₁ : InvImage r f a₁ aβ‚‚) (hβ‚‚ : InvImage r f aβ‚‚ a₃) => h h₁ hβ‚‚ theorem InvImage.Irreflexive (f : Ξ± β†’ Ξ²) (h : Irreflexive r) : Irreflexive (InvImage r f) := fun (a : Ξ±) (h₁ : InvImage r f a a) => h (f a) h₁ inductive TC {Ξ± : Sort u} (r : Ξ± β†’ Ξ± β†’ Prop) : Ξ± β†’ Ξ± β†’ Prop | base : βˆ€ a b, r a b β†’ TC a b | trans : βˆ€ a b c, TC a b β†’ TC b c β†’ TC a c @[elabAsEliminator] theorem TC.ndrec.{u1, u2} {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {C : Ξ± β†’ Ξ± β†’ Prop} (m₁ : βˆ€ (a b : Ξ±), r a b β†’ C a b) (mβ‚‚ : βˆ€ (a b c : Ξ±), TC r a b β†’ TC r b c β†’ C a b β†’ C b c β†’ C a c) {a b : Ξ±} (h : TC r a b) : C a b := @TC.rec Ξ± r (fun a b _ => C a b) m₁ mβ‚‚ a b h @[elabAsEliminator] theorem TC.ndrecOn.{u1, u2} {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {C : Ξ± β†’ Ξ± β†’ Prop} {a b : Ξ±} (h : TC r a b) (m₁ : βˆ€ (a b : Ξ±), r a b β†’ C a b) (mβ‚‚ : βˆ€ (a b c : Ξ±), TC r a b β†’ TC r b c β†’ C a b β†’ C b c β†’ C a c) : C a b := @TC.rec Ξ± r (fun a b _ => C a b) m₁ mβ‚‚ a b h end relation section Binary variables {Ξ± : Type u} {Ξ² : Type v} variable (f : Ξ± β†’ Ξ± β†’ Ξ±) def Commutative := βˆ€ a b, f a b = f b a def Associative := βˆ€ a b c, f (f a b) c = f a (f b c) def RightCommutative (h : Ξ² β†’ Ξ± β†’ Ξ²) := βˆ€ b a₁ aβ‚‚, h (h b a₁) aβ‚‚ = h (h b aβ‚‚) a₁ def LeftCommutative (h : Ξ± β†’ Ξ² β†’ Ξ²) := βˆ€ a₁ aβ‚‚ b, h a₁ (h aβ‚‚ b) = h aβ‚‚ (h a₁ b) theorem leftComm : Commutative f β†’ Associative f β†’ LeftCommutative f := fun hcomm hassoc a b c => ((Eq.symm (hassoc a b c)).trans (hcomm a b β–Έ rfl : f (f a b) c = f (f b a) c)).trans (hassoc b a c) theorem rightComm : Commutative f β†’ Associative f β†’ RightCommutative f := fun hcomm hassoc a b c => ((hassoc a b c).trans (hcomm b c β–Έ rfl : f a (f b c) = f a (f c b))).trans (Eq.symm (hassoc a c b)) end Binary /- Subtype -/ namespace Subtype def existsOfSubtype {Ξ± : Type u} {p : Ξ± β†’ Prop} : { x // p x } β†’ Exists (fun x => p x) | ⟨a, h⟩ => ⟨a, h⟩ variables {Ξ± : Type u} {p : Ξ± β†’ Prop} theorem tagIrrelevant {a : Ξ±} (h1 h2 : p a) : mk a h1 = mk a h2 := rfl protected theorem eq : βˆ€ {a1 a2 : {x // p x}}, val a1 = val a2 β†’ a1 = a2 | ⟨x, h1⟩, ⟨.(x), h2⟩, rfl => rfl theorem eta (a : {x // p x}) (h : p (val a)) : mk (val a) h = a := Subtype.eq rfl instance {Ξ± : Type u} {p : Ξ± β†’ Prop} {a : Ξ±} (h : p a) : Inhabited {x // p x} := ⟨⟨a, h⟩⟩ instance {Ξ± : Type u} {p : Ξ± β†’ Prop} [DecidableEq Ξ±] : DecidableEq {x : Ξ± // p x} := fun ⟨a, hβ‚βŸ© ⟨b, hβ‚‚βŸ© => if h : a = b then isTrue (Subtype.eq h) else isFalse (fun h' => Subtype.noConfusion h' (fun h' => absurd h' h)) end Subtype /- Sum -/ section variables {Ξ± : Type u} {Ξ² : Type v} instance Sum.inhabitedLeft [h : Inhabited Ξ±] : Inhabited (Sum Ξ± Ξ²) := ⟨Sum.inl (arbitrary Ξ±)⟩ instance Sum.inhabitedRight [h : Inhabited Ξ²] : Inhabited (Sum Ξ± Ξ²) := ⟨Sum.inr (arbitrary Ξ²)⟩ instance {Ξ± : Type u} {Ξ² : Type v} [DecidableEq Ξ±] [DecidableEq Ξ²] : DecidableEq (Sum Ξ± Ξ²) := fun a b => match a, b with | (Sum.inl a), (Sum.inl b) => if h : a = b then isTrue (h β–Έ rfl) else isFalse (fun h' => Sum.noConfusion h' (fun h' => absurd h' h)) | (Sum.inr a), (Sum.inr b) => if h : a = b then isTrue (h β–Έ rfl) else isFalse (fun h' => Sum.noConfusion h' (fun h' => absurd h' h)) | (Sum.inr a), (Sum.inl b) => isFalse (fun h => Sum.noConfusion h) | (Sum.inl a), (Sum.inr b) => isFalse (fun h => Sum.noConfusion h) end /- Product -/ section variables {Ξ± : Type u} {Ξ² : Type v} instance [Inhabited Ξ±] [Inhabited Ξ²] : Inhabited (Prod Ξ± Ξ²) := ⟨(arbitrary Ξ±, arbitrary Ξ²)⟩ instance [DecidableEq Ξ±] [DecidableEq Ξ²] : DecidableEq (Ξ± Γ— Ξ²) := fun ⟨a, b⟩ ⟨a', b'⟩ => match (decEq a a') with | (isTrue e₁) => match (decEq b b') with | (isTrue eβ‚‚) => isTrue (Eq.recOn e₁ (Eq.recOn eβ‚‚ rfl)) | (isFalse nβ‚‚) => isFalse (fun h => Prod.noConfusion h (fun e₁' eβ‚‚' => absurd eβ‚‚' nβ‚‚)) | (isFalse n₁) => isFalse (fun h => Prod.noConfusion h (fun e₁' eβ‚‚' => absurd e₁' n₁)) instance [HasBeq Ξ±] [HasBeq Ξ²] : HasBeq (Ξ± Γ— Ξ²) := ⟨fun ⟨a₁, bβ‚βŸ© ⟨aβ‚‚, bβ‚‚βŸ© => a₁ == aβ‚‚ && b₁ == bβ‚‚βŸ© instance [HasLess Ξ±] [HasLess Ξ²] : HasLess (Ξ± Γ— Ξ²) := ⟨fun s t => s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)⟩ instance prodHasDecidableLt [HasLess Ξ±] [HasLess Ξ²] [DecidableEq Ξ±] [DecidableEq Ξ²] [βˆ€ (a b : Ξ±), Decidable (a < b)] [βˆ€ (a b : Ξ²), Decidable (a < b)] : βˆ€ (s t : Ξ± Γ— Ξ²), Decidable (s < t) := fun t s => Or.Decidable theorem Prod.ltDef [HasLess Ξ±] [HasLess Ξ²] (s t : Ξ± Γ— Ξ²) : (s < t) = (s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)) := rfl end def Prod.map.{u₁, uβ‚‚, v₁, vβ‚‚} {α₁ : Type u₁} {Ξ±β‚‚ : Type uβ‚‚} {β₁ : Type v₁} {Ξ²β‚‚ : Type vβ‚‚} (f : α₁ β†’ Ξ±β‚‚) (g : β₁ β†’ Ξ²β‚‚) : α₁ Γ— β₁ β†’ Ξ±β‚‚ Γ— Ξ²β‚‚ | (a, b) => (f a, g b) /- Dependent products -/ -- notation `Ξ£` binders `, ` r:(scoped p, Sigma p) := r -- notation `Ξ£'` binders `, ` r:(scoped p, PSigma p) := r theorem exOfPsig {Ξ± : Type u} {p : Ξ± β†’ Prop} : (PSigma (fun x => p x)) β†’ Exists (fun x => p x) | ⟨x, hx⟩ => ⟨x, hx⟩ section variables {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} protected theorem Sigma.eq : βˆ€ {p₁ pβ‚‚ : Sigma (fun a => Ξ² a)} (h₁ : p₁.1 = pβ‚‚.1), (Eq.recOn h₁ p₁.2 : Ξ² pβ‚‚.1) = pβ‚‚.2 β†’ p₁ = pβ‚‚ | ⟨a, b⟩, ⟨.(a), .(b)⟩, rfl, rfl => rfl end section variables {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} protected theorem PSigma.eq : βˆ€ {p₁ pβ‚‚ : PSigma Ξ²} (h₁ : p₁.1 = pβ‚‚.1), (Eq.recOn h₁ p₁.2 : Ξ² pβ‚‚.1) = pβ‚‚.2 β†’ p₁ = pβ‚‚ | ⟨a, b⟩, ⟨.(a), .(b)⟩, rfl, rfl => rfl end /- Universe polymorphic unit -/ theorem punitEq (a b : PUnit) : a = b := PUnit.recOn a (PUnit.recOn b rfl) theorem punitEqPUnit (a : PUnit) : a = () := punitEq a () instance : Subsingleton PUnit := Subsingleton.intro punitEq instance : Inhabited PUnit := ⟨⟨⟩⟩ instance : DecidableEq PUnit := fun a b => isTrue (punitEq a b) /- Setoid -/ class Setoid (Ξ± : Sort u) := (r : Ξ± β†’ Ξ± β†’ Prop) (iseqv {} : Equivalence r) instance setoidHasEquiv {Ξ± : Sort u} [Setoid Ξ±] : HasEquiv Ξ± := ⟨Setoid.r⟩ namespace Setoid variables {Ξ± : Sort u} [Setoid Ξ±] theorem refl (a : Ξ±) : a β‰ˆ a := match Setoid.iseqv Ξ± with | ⟨hRefl, hSymm, hTrans⟩ => hRefl a theorem symm {a b : Ξ±} (hab : a β‰ˆ b) : b β‰ˆ a := match Setoid.iseqv Ξ± with | ⟨hRefl, hSymm, hTrans⟩ => hSymm hab theorem trans {a b c : Ξ±} (hab : a β‰ˆ b) (hbc : b β‰ˆ c) : a β‰ˆ c := match Setoid.iseqv Ξ± with | ⟨hRefl, hSymm, hTrans⟩ => hTrans hab hbc end Setoid /- Propositional extensionality -/ axiom propext {a b : Prop} : (a ↔ b) β†’ a = b theorem eqTrueIntro {a : Prop} (h : a) : a = True := propext (iffTrueIntro h) theorem eqFalseIntro {a : Prop} (h : Β¬a) : a = False := propext (iffFalseIntro h) /- Quotients -/ -- Iff can now be used to do substitutions in a calculation theorem iffSubst {a b : Prop} {p : Prop β†’ Prop} (h₁ : a ↔ b) (hβ‚‚ : p a) : p b := Eq.subst (propext h₁) hβ‚‚ namespace Quot axiom sound : βˆ€ {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {a b : Ξ±}, r a b β†’ Quot.mk r a = Quot.mk r b attribute [elabAsEliminator] lift ind protected theorem liftBeta {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Sort v} (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, r a b β†’ f a = f b) (a : Ξ±) : lift f c (Quot.mk r a) = f a := rfl protected theorem indBeta {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Prop} (p : βˆ€ a, Ξ² (Quot.mk r a)) (a : Ξ±) : (ind p (Quot.mk r a) : Ξ² (Quot.mk r a)) = p a := rfl @[reducible, elabAsEliminator, inline] protected def liftOn {Ξ± : Sort u} {Ξ² : Sort v} {r : Ξ± β†’ Ξ± β†’ Prop} (q : Quot r) (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, r a b β†’ f a = f b) : Ξ² := lift f c q @[elabAsEliminator] protected theorem inductionOn {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Prop} (q : Quot r) (h : βˆ€ a, Ξ² (Quot.mk r a)) : Ξ² q := ind h q theorem existsRep {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} (q : Quot r) : Exists (fun a => (Quot.mk r a) = q) := Quot.inductionOn q (fun a => ⟨a, rfl⟩) section variable {Ξ± : Sort u} variable {r : Ξ± β†’ Ξ± β†’ Prop} variable {Ξ² : Quot r β†’ Sort v} @[reducible, macroInline] protected def indep (f : βˆ€ a, Ξ² (Quot.mk r a)) (a : Ξ±) : PSigma Ξ² := ⟨Quot.mk r a, f a⟩ protected theorem indepCoherent (f : βˆ€ a, Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), (Eq.rec (f a) (sound p) : Ξ² (Quot.mk r b)) = f b) : βˆ€ a b, r a b β†’ Quot.indep f a = Quot.indep f b := fun a b e => PSigma.eq (sound e) (h a b e) protected theorem liftIndepPr1 (f : βˆ€ a, Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), (Eq.rec (f a) (sound p) : Ξ² (Quot.mk r b)) = f b) (q : Quot r) : (lift (Quot.indep f) (Quot.indepCoherent f h) q).1 = q := Quot.ind (fun (a : Ξ±) => Eq.refl (Quot.indep f a).1) q @[reducible, elabAsEliminator, inline] protected def rec (f : βˆ€ a, Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), (Eq.rec (f a) (sound p) : Ξ² (Quot.mk r b)) = f b) (q : Quot r) : Ξ² q := Eq.ndrecOn (Quot.liftIndepPr1 f h q) ((lift (Quot.indep f) (Quot.indepCoherent f h) q).2) @[reducible, elabAsEliminator, inline] protected def recOn (q : Quot r) (f : βˆ€ a, Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), (Eq.rec (f a) (sound p) : Ξ² (Quot.mk r b)) = f b) : Ξ² q := Quot.rec f h q @[reducible, elabAsEliminator, inline] protected def recOnSubsingleton [h : βˆ€ a, Subsingleton (Ξ² (Quot.mk r a))] (q : Quot r) (f : βˆ€ a, Ξ² (Quot.mk r a)) : Ξ² q := Quot.rec f (fun a b h => Subsingleton.elim _ (f b)) q @[reducible, elabAsEliminator, inline] protected def hrecOn (q : Quot r) (f : βˆ€ a, Ξ² (Quot.mk r a)) (c : βˆ€ (a b : Ξ±) (p : r a b), f a β‰… f b) : Ξ² q := Quot.recOn q f $ fun a b p => eqOfHEq $ have p₁ : (Eq.rec (f a) (sound p) : Ξ² (Quot.mk r b)) β‰… f a := eqRecHEq (sound p) (f a); HEq.trans p₁ (c a b p) end end Quot def Quotient {Ξ± : Sort u} (s : Setoid Ξ±) := @Quot Ξ± Setoid.r namespace Quotient @[inline] protected def mk {Ξ± : Sort u} [s : Setoid Ξ±] (a : Ξ±) : Quotient s := Quot.mk Setoid.r a def sound {Ξ± : Sort u} [s : Setoid Ξ±] {a b : Ξ±} : a β‰ˆ b β†’ Quotient.mk a = Quotient.mk b := Quot.sound @[reducible, elabAsEliminator] protected def lift {Ξ± : Sort u} {Ξ² : Sort v} [s : Setoid Ξ±] (f : Ξ± β†’ Ξ²) : (βˆ€ a b, a β‰ˆ b β†’ f a = f b) β†’ Quotient s β†’ Ξ² := Quot.lift f @[elabAsEliminator] protected theorem ind {Ξ± : Sort u} [s : Setoid Ξ±] {Ξ² : Quotient s β†’ Prop} : (βˆ€ a, Ξ² (Quotient.mk a)) β†’ βˆ€ q, Ξ² q := Quot.ind @[reducible, elabAsEliminator, inline] protected def liftOn {Ξ± : Sort u} {Ξ² : Sort v} [s : Setoid Ξ±] (q : Quotient s) (f : Ξ± β†’ Ξ²) (c : βˆ€ a b, a β‰ˆ b β†’ f a = f b) : Ξ² := Quot.liftOn q f c @[elabAsEliminator] protected theorem inductionOn {Ξ± : Sort u} [s : Setoid Ξ±] {Ξ² : Quotient s β†’ Prop} (q : Quotient s) (h : βˆ€ a, Ξ² (Quotient.mk a)) : Ξ² q := Quot.inductionOn q h theorem existsRep {Ξ± : Sort u} [s : Setoid Ξ±] (q : Quotient s) : Exists (fun (a : Ξ±) => Quotient.mk a = q) := Quot.existsRep q section variable {Ξ± : Sort u} variable [s : Setoid Ξ±] variable {Ξ² : Quotient s β†’ Sort v} @[inline] protected def rec (f : βˆ€ a, Ξ² (Quotient.mk a)) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), (Eq.rec (f a) (Quotient.sound p) : Ξ² (Quotient.mk b)) = f b) (q : Quotient s) : Ξ² q := Quot.rec f h q @[reducible, elabAsEliminator, inline] protected def recOn (q : Quotient s) (f : βˆ€ a, Ξ² (Quotient.mk a)) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), (Eq.rec (f a) (Quotient.sound p) : Ξ² (Quotient.mk b)) = f b) : Ξ² q := Quot.recOn q f h @[reducible, elabAsEliminator, inline] protected def recOnSubsingleton [h : βˆ€ a, Subsingleton (Ξ² (Quotient.mk a))] (q : Quotient s) (f : βˆ€ a, Ξ² (Quotient.mk a)) : Ξ² q := @Quot.recOnSubsingleton _ _ _ h q f @[reducible, elabAsEliminator, inline] protected def hrecOn (q : Quotient s) (f : βˆ€ a, Ξ² (Quotient.mk a)) (c : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), f a β‰… f b) : Ξ² q := Quot.hrecOn q f c end section universes uA uB uC variables {Ξ± : Sort uA} {Ξ² : Sort uB} {Ο† : Sort uC} variables [s₁ : Setoid Ξ±] [sβ‚‚ : Setoid Ξ²] @[reducible, elabAsEliminator, inline] protected def liftβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ο†)(c : βˆ€ a₁ aβ‚‚ b₁ bβ‚‚, a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) : Ο† := Quotient.lift (fun (a₁ : Ξ±) => Quotient.lift (f a₁) (fun (a b : Ξ²) => c a₁ a a₁ b (Setoid.refl a₁)) qβ‚‚) (fun (a b : Ξ±) (h : a β‰ˆ b) => @Quotient.ind Ξ² sβ‚‚ (fun (a1 : Quotient sβ‚‚) => (Quotient.lift (f a) (fun (a1 b : Ξ²) => c a a1 a b (Setoid.refl a)) a1) = (Quotient.lift (f b) (fun (a b1 : Ξ²) => c b a b b1 (Setoid.refl b)) a1)) (fun (a' : Ξ²) => c a a' b a' h (Setoid.refl a')) qβ‚‚) q₁ @[reducible, elabAsEliminator, inline] protected def liftOnβ‚‚ (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) (f : Ξ± β†’ Ξ² β†’ Ο†) (c : βˆ€ a₁ aβ‚‚ b₁ bβ‚‚, a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) : Ο† := Quotient.liftβ‚‚ f c q₁ qβ‚‚ @[elabAsEliminator] protected theorem indβ‚‚ {Ο† : Quotient s₁ β†’ Quotient sβ‚‚ β†’ Prop} (h : βˆ€ a b, Ο† (Quotient.mk a) (Quotient.mk b)) (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) : Ο† q₁ qβ‚‚ := Quotient.ind (fun a₁ => Quotient.ind (fun aβ‚‚ => h a₁ aβ‚‚) qβ‚‚) q₁ @[elabAsEliminator] protected theorem inductionOnβ‚‚ {Ο† : Quotient s₁ β†’ Quotient sβ‚‚ β†’ Prop} (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) (h : βˆ€ a b, Ο† (Quotient.mk a) (Quotient.mk b)) : Ο† q₁ qβ‚‚ := Quotient.ind (fun a₁ => Quotient.ind (fun aβ‚‚ => h a₁ aβ‚‚) qβ‚‚) q₁ @[elabAsEliminator] protected theorem inductionOn₃ [s₃ : Setoid Ο†] {Ξ΄ : Quotient s₁ β†’ Quotient sβ‚‚ β†’ Quotient s₃ β†’ Prop} (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) (q₃ : Quotient s₃) (h : βˆ€ a b c, Ξ΄ (Quotient.mk a) (Quotient.mk b) (Quotient.mk c)) : Ξ΄ q₁ qβ‚‚ q₃ := Quotient.ind (fun a₁ => Quotient.ind (fun aβ‚‚ => Quotient.ind (fun a₃ => h a₁ aβ‚‚ a₃) q₃) qβ‚‚) q₁ end section Exact variable {Ξ± : Sort u} private def rel [s : Setoid Ξ±] (q₁ qβ‚‚ : Quotient s) : Prop := Quotient.liftOnβ‚‚ q₁ qβ‚‚ (fun a₁ aβ‚‚ => a₁ β‰ˆ aβ‚‚) (fun a₁ aβ‚‚ b₁ bβ‚‚ a₁b₁ aβ‚‚bβ‚‚ => propext (Iff.intro (fun a₁aβ‚‚ => Setoid.trans (Setoid.symm a₁b₁) (Setoid.trans a₁aβ‚‚ aβ‚‚bβ‚‚)) (fun b₁bβ‚‚ => Setoid.trans a₁b₁ (Setoid.trans b₁bβ‚‚ (Setoid.symm aβ‚‚bβ‚‚))))) private theorem rel.refl [s : Setoid Ξ±] : βˆ€ (q : Quotient s), rel q q := fun q => Quot.inductionOn q (fun a => Setoid.refl a) private theorem eqImpRel [s : Setoid Ξ±] {q₁ qβ‚‚ : Quotient s} : q₁ = qβ‚‚ β†’ rel q₁ qβ‚‚ := fun h => Eq.ndrecOn h (rel.refl q₁) theorem exact [s : Setoid Ξ±] {a b : Ξ±} : Quotient.mk a = Quotient.mk b β†’ a β‰ˆ b := fun h => eqImpRel h end Exact section universes uA uB uC variables {Ξ± : Sort uA} {Ξ² : Sort uB} variables [s₁ : Setoid Ξ±] [sβ‚‚ : Setoid Ξ²] @[reducible, elabAsEliminator] protected def recOnSubsingletonβ‚‚ {Ο† : Quotient s₁ β†’ Quotient sβ‚‚ β†’ Sort uC} [h : βˆ€ a b, Subsingleton (Ο† (Quotient.mk a) (Quotient.mk b))] (q₁ : Quotient s₁) (qβ‚‚ : Quotient sβ‚‚) (f : βˆ€ a b, Ο† (Quotient.mk a) (Quotient.mk b)) : Ο† q₁ qβ‚‚:= @Quotient.recOnSubsingleton _ s₁ (fun q => Ο† q qβ‚‚) (fun a => Quotient.ind (fun b => h a b) qβ‚‚) q₁ (fun a => Quotient.recOnSubsingleton qβ‚‚ (fun b => f a b)) end end Quotient section variable {Ξ± : Type u} variable (r : Ξ± β†’ Ξ± β†’ Prop) inductive EqvGen : Ξ± β†’ Ξ± β†’ Prop | rel : βˆ€ x y, r x y β†’ EqvGen x y | refl : βˆ€ x, EqvGen x x | symm : βˆ€ x y, EqvGen x y β†’ EqvGen y x | trans : βˆ€ x y z, EqvGen x y β†’ EqvGen y z β†’ EqvGen x z theorem EqvGen.isEquivalence : Equivalence (@EqvGen Ξ± r) := mkEquivalence _ EqvGen.refl EqvGen.symm EqvGen.trans def EqvGen.Setoid : Setoid Ξ± := Setoid.mk _ (EqvGen.isEquivalence r) theorem Quot.exact {a b : Ξ±} (H : Quot.mk r a = Quot.mk r b) : EqvGen r a b := @Quotient.exact _ (EqvGen.Setoid r) a b (@congrArg _ _ _ _ (Quot.lift (@Quotient.mk _ (EqvGen.Setoid r)) (fun x y h => Quot.sound (EqvGen.rel x y h))) H) theorem Quot.eqvGenSound {r : Ξ± β†’ Ξ± β†’ Prop} {a b : Ξ±} (H : EqvGen r a b) : Quot.mk r a = Quot.mk r b := EqvGen.recOn H (fun x y h => Quot.sound h) (fun x => rfl) (fun x y _ IH => Eq.symm IH) (fun x y z _ _ IH₁ IHβ‚‚ => Eq.trans IH₁ IHβ‚‚) end instance {Ξ± : Sort u} {s : Setoid Ξ±} [d : βˆ€ (a b : Ξ±), Decidable (a β‰ˆ b)] : DecidableEq (Quotient s) := fun (q₁ qβ‚‚ : Quotient s) => Quotient.recOnSubsingletonβ‚‚ q₁ qβ‚‚ (fun a₁ aβ‚‚ => match (d a₁ aβ‚‚) with | (isTrue h₁) => isTrue (Quotient.sound h₁) | (isFalse hβ‚‚) => isFalse (fun h => absurd (Quotient.exact h) hβ‚‚)) /- Function extensionality -/ namespace Function variables {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} def Equiv (f₁ fβ‚‚ : βˆ€ (x : Ξ±), Ξ² x) : Prop := βˆ€ x, f₁ x = fβ‚‚ x protected theorem Equiv.refl (f : βˆ€ (x : Ξ±), Ξ² x) : Equiv f f := fun x => rfl protected theorem Equiv.symm {f₁ fβ‚‚ : βˆ€ (x : Ξ±), Ξ² x} : Equiv f₁ fβ‚‚ β†’ Equiv fβ‚‚ f₁ := fun h x => Eq.symm (h x) protected theorem Equiv.trans {f₁ fβ‚‚ f₃ : βˆ€ (x : Ξ±), Ξ² x} : Equiv f₁ fβ‚‚ β†’ Equiv fβ‚‚ f₃ β†’ Equiv f₁ f₃ := fun h₁ hβ‚‚ x => Eq.trans (h₁ x) (hβ‚‚ x) protected theorem Equiv.isEquivalence (Ξ± : Sort u) (Ξ² : Ξ± β†’ Sort v) : Equivalence (@Function.Equiv Ξ± Ξ²) := mkEquivalence (@Function.Equiv Ξ± Ξ²) (@Equiv.refl Ξ± Ξ²) (@Equiv.symm Ξ± Ξ²) (@Equiv.trans Ξ± Ξ²) end Function section open Quotient variables {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} @[instance] private def funSetoid (Ξ± : Sort u) (Ξ² : Ξ± β†’ Sort v) : Setoid (βˆ€ (x : Ξ±), Ξ² x) := Setoid.mk (@Function.Equiv Ξ± Ξ²) (Function.Equiv.isEquivalence Ξ± Ξ²) private def extfunApp (f : Quotient $ funSetoid Ξ± Ξ²) : βˆ€ (x : Ξ±), Ξ² x := fun x => Quot.liftOn f (fun (f : βˆ€ (x : Ξ±), Ξ² x) => f x) (fun f₁ fβ‚‚ h => h x) theorem funext {f₁ fβ‚‚ : βˆ€ (x : Ξ±), Ξ² x} (h : βˆ€ x, f₁ x = fβ‚‚ x) : f₁ = fβ‚‚ := show extfunApp (Quotient.mk f₁) = extfunApp (Quotient.mk fβ‚‚) from congrArg extfunApp (sound h) end instance Forall.Subsingleton {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} [βˆ€ a, Subsingleton (Ξ² a)] : Subsingleton (βˆ€ a, Ξ² a) := ⟨fun f₁ fβ‚‚ => funext (fun a => Subsingleton.elim (f₁ a) (fβ‚‚ a))⟩ /- General operations on functions -/ namespace Function universes u₁ uβ‚‚ u₃ uβ‚„ variables {Ξ± : Sort u₁} {Ξ² : Sort uβ‚‚} {Ο† : Sort u₃} {Ξ΄ : Sort uβ‚„} {ΞΆ : Sort u₁} @[inline, reducible] def comp (f : Ξ² β†’ Ο†) (g : Ξ± β†’ Ξ²) : Ξ± β†’ Ο† := fun x => f (g x) infixr ` ∘ ` := Function.comp @[inline, reducible] def onFun (f : Ξ² β†’ Ξ² β†’ Ο†) (g : Ξ± β†’ Ξ²) : Ξ± β†’ Ξ± β†’ Ο† := fun x y => f (g x) (g y) @[inline, reducible] def combine (f : Ξ± β†’ Ξ² β†’ Ο†) (op : Ο† β†’ Ξ΄ β†’ ΞΆ) (g : Ξ± β†’ Ξ² β†’ Ξ΄) : Ξ± β†’ Ξ² β†’ ΞΆ := fun x y => op (f x y) (g x y) @[inline, reducible] def const (Ξ² : Sort uβ‚‚) (a : Ξ±) : Ξ² β†’ Ξ± := fun x => a @[inline, reducible] def swap {Ο† : Ξ± β†’ Ξ² β†’ Sort u₃} (f : βˆ€ x y, Ο† x y) : βˆ€ y x, Ο† x y := fun y x => f x y end Function /- Squash -/ def Squash (Ξ± : Type u) := Quot (fun (a b : Ξ±) => True) def Squash.mk {Ξ± : Type u} (x : Ξ±) : Squash Ξ± := Quot.mk _ x theorem Squash.ind {Ξ± : Type u} {Ξ² : Squash Ξ± β†’ Prop} (h : βˆ€ (a : Ξ±), Ξ² (Squash.mk a)) : βˆ€ (q : Squash Ξ±), Ξ² q := Quot.ind h @[inline] def Squash.lift {Ξ± Ξ²} [Subsingleton Ξ²] (s : Squash Ξ±) (f : Ξ± β†’ Ξ²) : Ξ² := Quot.lift f (fun a b _ => Subsingleton.elim _ _) s instance Squash.Subsingleton {Ξ±} : Subsingleton (Squash Ξ±) := ⟨Squash.ind (fun (a : Ξ±) => Squash.ind (fun (b : Ξ±) => Quot.sound trivial))⟩ namespace Lean /- Kernel reduction hints -/ /-- When the kernel tries to reduce a term `Lean.reduceBool c`, it will invoke the Lean interpreter to evaluate `c`. The kernel will not use the interpreter if `c` is not a constant. This feature is useful for performing proofs by reflection. Remark: the Lean frontend allows terms of the from `Lean.reduceBool t` where `t` is a term not containing free variables. The frontend automatically declares a fresh auxiliary constant `c` and replaces the term with `Lean.reduceBool c`. The main motivation is that the code for `t` will be pre-compiled. Warning: by using this feature, the Lean compiler and interpreter become part of your trusted code base. This is extra 30k lines of code. More importantly, you will probably not be able to check your developement using external type checkers (e.g., Trepplein) that do not implement this feature. Keep in mind that if you are using Lean as programming language, you are already trusting the Lean compiler and interpreter. So, you are mainly losing the capability of type checking your developement using external checkers. -/ def reduceBool (b : Bool) : Bool := b /-- Similar to `Lean.reduceBool` for closed `Nat` terms. Remark: we do not have plans for supporting a generic `reduceValue {Ξ±} (a : Ξ±) : Ξ± := a`. The main issue is that it is non-trivial to convert an arbitrary runtime object back into a Lean expression. We believe `Lean.reduceBool` enables most interesting applications (e.g., proof by reflection). -/ def reduceNat (n : Nat) : Nat := n def ofReduceBool (a b : Bool) (h : reduceBool a = b) : a = b := h def ofReduceNat (a b : Nat) (h : reduceNat a = b) : a = b := h end Lean /- Classical reasoning support -/ namespace Classical axiom choice {Ξ± : Sort u} : Nonempty Ξ± β†’ Ξ± noncomputable def indefiniteDescription {Ξ± : Sort u} (p : Ξ± β†’ Prop) (h : Exists (fun x => p x)) : {x // p x} := choice $ let ⟨x, px⟩ := h; ⟨⟨x, px⟩⟩ noncomputable def choose {Ξ± : Sort u} {p : Ξ± β†’ Prop} (h : Exists (fun x => p x)) : Ξ± := (indefiniteDescription p h).val theorem chooseSpec {Ξ± : Sort u} {p : Ξ± β†’ Prop} (h : Exists (fun x => p x)) : p (choose h) := (indefiniteDescription p h).property /- Diaconescu's theorem: excluded middle from choice, Function extensionality and propositional extensionality. -/ theorem em (p : Prop) : p ∨ Β¬p := let U (x : Prop) : Prop := x = True ∨ p; let V (x : Prop) : Prop := x = False ∨ p; have exU : Exists (fun x => U x) from ⟨True, Or.inl rfl⟩; have exV : Exists (fun x => V x) from ⟨False, Or.inl rfl⟩; let u : Prop := choose exU; let v : Prop := choose exV; have uDef : U u from chooseSpec exU; have vDef : V v from chooseSpec exV; have notUvOrP : u β‰  v ∨ p from Or.elim uDef (fun hut => Or.elim vDef (fun hvf => have hne : u β‰  v from hvf.symm β–Έ hut.symm β–Έ trueNeFalse; Or.inl hne) Or.inr) Or.inr; have pImpliesUv : p β†’ u = v from fun hp => have hpred : U = V from funext $ fun x => have hl : (x = True ∨ p) β†’ (x = False ∨ p) from fun a => Or.inr hp; have hr : (x = False ∨ p) β†’ (x = True ∨ p) from fun a => Or.inr hp; show (x = True ∨ p) = (x = False ∨ p) from propext (Iff.intro hl hr); have hβ‚€ : βˆ€ exU exV, @choose _ U exU = @choose _ V exV from hpred β–Έ fun exU exV => rfl; show u = v from hβ‚€ _ _; Or.elim notUvOrP (fun (hne : u β‰  v) => Or.inr (mt pImpliesUv hne)) Or.inl theorem existsTrueOfNonempty {Ξ± : Sort u} : Nonempty Ξ± β†’ Exists (fun (x : Ξ±) => True) | ⟨x⟩ => ⟨x, trivial⟩ noncomputable def inhabitedOfNonempty {Ξ± : Sort u} (h : Nonempty Ξ±) : Inhabited Ξ± := ⟨choice h⟩ noncomputable def inhabitedOfExists {Ξ± : Sort u} {p : Ξ± β†’ Prop} (h : Exists (fun x => p x)) : Inhabited Ξ± := inhabitedOfNonempty (Exists.elim h (fun w hw => ⟨w⟩)) /- all propositions are Decidable -/ noncomputable def propDecidable (a : Prop) : Decidable a := choice $ Or.elim (em a) (fun ha => ⟨isTrue ha⟩) (fun hna => ⟨isFalse hna⟩) noncomputable def decidableInhabited (a : Prop) : Inhabited (Decidable a) := ⟨propDecidable a⟩ noncomputable def typeDecidableEq (Ξ± : Sort u) : DecidableEq Ξ± := fun x y => propDecidable (x = y) noncomputable def typeDecidable (Ξ± : Sort u) : PSum Ξ± (Ξ± β†’ False) := match (propDecidable (Nonempty Ξ±)) with | (isTrue hp) => PSum.inl (@arbitrary _ (inhabitedOfNonempty hp)) | (isFalse hn) => PSum.inr (fun a => absurd (Nonempty.intro a) hn) noncomputable def strongIndefiniteDescription {Ξ± : Sort u} (p : Ξ± β†’ Prop) (h : Nonempty Ξ±) : {x : Ξ± // Exists (fun (y : Ξ±) => p y) β†’ p x} := @dite _ (Exists (fun (x : Ξ±) => p x)) (propDecidable _) (fun (hp : Exists (fun (x : Ξ±) => p x)) => show {x : Ξ± // Exists (fun (y : Ξ±) => p y) β†’ p x} from let xp := indefiniteDescription _ hp; ⟨xp.val, fun h' => xp.property⟩) (fun hp => ⟨choice h, fun h => absurd h hp⟩) /- the Hilbert epsilon Function -/ noncomputable def epsilon {Ξ± : Sort u} [h : Nonempty Ξ±] (p : Ξ± β†’ Prop) : Ξ± := (strongIndefiniteDescription p h).val theorem epsilonSpecAux {Ξ± : Sort u} (h : Nonempty Ξ±) (p : Ξ± β†’ Prop) : Exists (fun y => p y) β†’ p (@epsilon Ξ± h p) := (strongIndefiniteDescription p h).property theorem epsilonSpec {Ξ± : Sort u} {p : Ξ± β†’ Prop} (hex : Exists (fun y => p y)) : p (@epsilon Ξ± (nonemptyOfExists hex) p) := epsilonSpecAux (nonemptyOfExists hex) p hex theorem epsilonSingleton {Ξ± : Sort u} (x : Ξ±) : @epsilon Ξ± ⟨x⟩ (fun y => y = x) = x := @epsilonSpec Ξ± (fun y => y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiomOfChoice {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} {r : βˆ€ x, Ξ² x β†’ Prop} (h : βˆ€ x, Exists (fun y => r x y)) : Exists (fun (f : βˆ€ x, Ξ² x) => βˆ€ x, r x (f x)) := ⟨_, fun x => chooseSpec (h x)⟩ theorem skolem {Ξ± : Sort u} {b : Ξ± β†’ Sort v} {p : βˆ€ x, b x β†’ Prop} : (βˆ€ x, Exists (fun y => p x y)) ↔ Exists (fun (f : βˆ€ x, b x) => βˆ€ x, p x (f x)) := ⟨axiomOfChoice, fun ⟨f, hw⟩ (x) => ⟨f x, hw x⟩⟩ theorem propComplete (a : Prop) : a = True ∨ a = False := Or.elim (em a) (fun t => Or.inl (eqTrueIntro t)) (fun f => Or.inr (eqFalseIntro f)) -- this supercedes byCases in Decidable theorem byCases {p q : Prop} (hpq : p β†’ q) (hnpq : Β¬p β†’ q) : q := @Decidable.byCases _ _ (propDecidable _) hpq hnpq -- this supercedes byContradiction in Decidable theorem byContradiction {p : Prop} (h : Β¬p β†’ False) : p := @Decidable.byContradiction _ (propDecidable _) h end Classical
c4e10938a527eff02b307e3ae3fb14cc875f6168
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/analysis/normed_space/banach.lean
96bcb19c96800be20572517ed34a13f79ba13a8e
[ "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
12,080
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.metric_space.baire import analysis.normed_space.operator_norm /-! # Banach open mapping theorem This file contains the Banach open mapping theorem, i.e., the fact that a bijective bounded linear map between Banach spaces has a bounded inverse. -/ open function metric set filter finset open_locale classical topological_space big_operators variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {F : Type*} [normed_group F] [normed_space π•œ F] (f : E β†’L[π•œ] F) include π•œ variable [complete_space F] /-- First step of the proof of the Banach open mapping theorem (using completeness of `F`): by Baire's theorem, there exists a ball in `E` whose image closure has nonempty interior. Rescaling everything, it follows that any `y ∈ F` is arbitrarily well approached by images of elements of norm at most `C * βˆ₯yβˆ₯`. For further use, we will only need such an element whose image is within distance `βˆ₯yβˆ₯/2` of `y`, to apply an iterative process. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_approx_preimage_norm_le (surj : surjective f) : βˆƒC β‰₯ 0, βˆ€y, βˆƒx, dist (f x) y ≀ 1/2 * βˆ₯yβˆ₯ ∧ βˆ₯xβˆ₯ ≀ C * βˆ₯yβˆ₯ := begin have A : (⋃n:β„•, closure (f '' (ball 0 n))) = univ, { refine subset.antisymm (subset_univ _) (Ξ»y hy, _), rcases surj y with ⟨x, hx⟩, rcases exists_nat_gt (βˆ₯xβˆ₯) with ⟨n, hn⟩, refine mem_Union.2 ⟨n, subset_closure _⟩, refine (mem_image _ _ _).2 ⟨x, ⟨_, hx⟩⟩, rwa [mem_ball, dist_eq_norm, sub_zero] }, have : βˆƒ (n : β„•) x, x ∈ interior (closure (f '' (ball 0 n))) := nonempty_interior_of_Union_of_closed (Ξ»n, is_closed_closure) A, simp only [mem_interior_iff_mem_nhds, mem_nhds_iff] at this, rcases this with ⟨n, a, Ξ΅, ⟨Ρpos, H⟩⟩, rcases normed_field.exists_one_lt_norm π•œ with ⟨c, hc⟩, refine ⟨(Ξ΅/2)⁻¹ * βˆ₯cβˆ₯ * 2 * n, _, Ξ»y, _⟩, { refine mul_nonneg (mul_nonneg (mul_nonneg _ (norm_nonneg _)) (by norm_num)) _, refine inv_nonneg.2 (div_nonneg' (le_of_lt Ξ΅pos) (by norm_num)), exact nat.cast_nonneg n }, { by_cases hy : y = 0, { use 0, simp [hy] }, { rcases rescale_to_shell hc (half_pos Ξ΅pos) hy with ⟨d, hd, ydle, leyd, dinv⟩, let Ξ΄ := βˆ₯dβˆ₯ * βˆ₯yβˆ₯/4, have Ξ΄pos : 0 < Ξ΄ := div_pos (mul_pos (norm_pos_iff.2 hd) (norm_pos_iff.2 hy)) (by norm_num), have : a + d β€’ y ∈ ball a Ξ΅, by simp [dist_eq_norm, lt_of_le_of_lt ydle (half_lt_self Ξ΅pos)], rcases metric.mem_closure_iff.1 (H this) _ Ξ΄pos with ⟨z₁, z₁im, hβ‚βŸ©, rcases (mem_image _ _ _).1 z₁im with ⟨x₁, hx₁, xzβ‚βŸ©, rw ← xz₁ at h₁, rw [mem_ball, dist_eq_norm, sub_zero] at hx₁, have : a ∈ ball a Ξ΅, by { simp, exact Ξ΅pos }, rcases metric.mem_closure_iff.1 (H this) _ Ξ΄pos with ⟨zβ‚‚, zβ‚‚im, hβ‚‚βŸ©, rcases (mem_image _ _ _).1 zβ‚‚im with ⟨xβ‚‚, hxβ‚‚, xzβ‚‚βŸ©, rw ← xzβ‚‚ at hβ‚‚, rw [mem_ball, dist_eq_norm, sub_zero] at hxβ‚‚, let x := x₁ - xβ‚‚, have I : βˆ₯f x - d β€’ yβˆ₯ ≀ 2 * Ξ΄ := calc βˆ₯f x - d β€’ yβˆ₯ = βˆ₯f x₁ - (a + d β€’ y) - (f xβ‚‚ - a)βˆ₯ : by { congr' 1, simp only [x, f.map_sub], abel } ... ≀ βˆ₯f x₁ - (a + d β€’ y)βˆ₯ + βˆ₯f xβ‚‚ - aβˆ₯ : norm_sub_le _ _ ... ≀ Ξ΄ + Ξ΄ : begin apply add_le_add, { rw [← dist_eq_norm, dist_comm], exact le_of_lt h₁ }, { rw [← dist_eq_norm, dist_comm], exact le_of_lt hβ‚‚ } end ... = 2 * Ξ΄ : (two_mul _).symm, have J : βˆ₯f (d⁻¹ β€’ x) - yβˆ₯ ≀ 1/2 * βˆ₯yβˆ₯ := calc βˆ₯f (d⁻¹ β€’ x) - yβˆ₯ = βˆ₯d⁻¹ β€’ f x - (d⁻¹ * d) β€’ yβˆ₯ : by rwa [f.map_smul _, inv_mul_cancel, one_smul] ... = βˆ₯d⁻¹ β€’ (f x - d β€’ y)βˆ₯ : by rw [mul_smul, smul_sub] ... = βˆ₯dβˆ₯⁻¹ * βˆ₯f x - d β€’ yβˆ₯ : by rw [norm_smul, normed_field.norm_inv] ... ≀ βˆ₯dβˆ₯⁻¹ * (2 * Ξ΄) : begin apply mul_le_mul_of_nonneg_left I, rw inv_nonneg, exact norm_nonneg _ end ... = (βˆ₯dβˆ₯⁻¹ * βˆ₯dβˆ₯) * βˆ₯yβˆ₯ /2 : by { simp only [Ξ΄], ring } ... = βˆ₯yβˆ₯/2 : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hd] } ... = (1/2) * βˆ₯yβˆ₯ : by ring, rw ← dist_eq_norm at J, have π•œ : βˆ₯d⁻¹ β€’ xβˆ₯ ≀ (Ξ΅ / 2)⁻¹ * βˆ₯cβˆ₯ * 2 * ↑n * βˆ₯yβˆ₯ := calc βˆ₯d⁻¹ β€’ xβˆ₯ = βˆ₯dβˆ₯⁻¹ * βˆ₯x₁ - xβ‚‚βˆ₯ : by rw [norm_smul, normed_field.norm_inv] ... ≀ ((Ξ΅ / 2)⁻¹ * βˆ₯cβˆ₯ * βˆ₯yβˆ₯) * (n + n) : begin refine mul_le_mul dinv _ (norm_nonneg _) _, { exact le_trans (norm_sub_le _ _) (add_le_add (le_of_lt hx₁) (le_of_lt hxβ‚‚)) }, { apply mul_nonneg (mul_nonneg _ (norm_nonneg _)) (norm_nonneg _), exact inv_nonneg.2 (le_of_lt (half_pos Ξ΅pos)) } end ... = (Ξ΅ / 2)⁻¹ * βˆ₯cβˆ₯ * 2 * ↑n * βˆ₯yβˆ₯ : by ring, exact ⟨d⁻¹ β€’ x, J, π•œβŸ© } }, end variable [complete_space E] /-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then any point has a preimage with controlled norm. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem exists_preimage_norm_le (surj : surjective f) : βˆƒC > 0, βˆ€y, βˆƒx, f x = y ∧ βˆ₯xβˆ₯ ≀ C * βˆ₯yβˆ₯ := begin obtain ⟨C, C0, hC⟩ := exists_approx_preimage_norm_le f surj, /- Second step of the proof: starting from `y`, we want an exact preimage of `y`. Let `g y` be the approximate preimage of `y` given by the first step, and `h y = y - f(g y)` the part that has no preimage yet. We will iterate this process, taking the approximate preimage of `h y`, leaving only `h^2 y` without preimage yet, and so on. Let `u n` be the approximate preimage of `h^n y`. Then `u` is a converging series, and by design the sum of the series is a preimage of `y`. This uses completeness of `E`. -/ choose g hg using hC, let h := Ξ»y, y - f (g y), have hle : βˆ€y, βˆ₯h yβˆ₯ ≀ (1/2) * βˆ₯yβˆ₯, { assume y, rw [← dist_eq_norm, dist_comm], exact (hg y).1 }, refine ⟨2 * C + 1, by linarith, Ξ»y, _⟩, have hnle : βˆ€n:β„•, βˆ₯(h^[n]) yβˆ₯ ≀ (1/2)^n * βˆ₯yβˆ₯, { assume n, induction n with n IH, { simp only [one_div_eq_inv, nat.nat_zero_eq_zero, one_mul, iterate_zero_apply, pow_zero] }, { rw [iterate_succ'], apply le_trans (hle _) _, rw [pow_succ, mul_assoc], apply mul_le_mul_of_nonneg_left IH, norm_num } }, let u := Ξ»n, g((h^[n]) y), have ule : βˆ€n, βˆ₯u nβˆ₯ ≀ (1/2)^n * (C * βˆ₯yβˆ₯), { assume n, apply le_trans (hg _).2 _, calc C * βˆ₯(h^[n]) yβˆ₯ ≀ C * ((1/2)^n * βˆ₯yβˆ₯) : mul_le_mul_of_nonneg_left (hnle n) C0 ... = (1 / 2) ^ n * (C * βˆ₯yβˆ₯) : by ring }, have sNu : summable (Ξ»n, βˆ₯u nβˆ₯), { refine summable_of_nonneg_of_le (Ξ»n, norm_nonneg _) ule _, exact summable.mul_right _ (summable_geometric_of_lt_1 (by norm_num) (by norm_num)) }, have su : summable u := summable_of_summable_norm sNu, let x := tsum u, have x_ineq : βˆ₯xβˆ₯ ≀ (2 * C + 1) * βˆ₯yβˆ₯ := calc βˆ₯xβˆ₯ ≀ (βˆ‘'n, βˆ₯u nβˆ₯) : norm_tsum_le_tsum_norm sNu ... ≀ (βˆ‘'n, (1/2)^n * (C * βˆ₯yβˆ₯)) : tsum_le_tsum ule sNu (summable.mul_right _ summable_geometric_two) ... = (βˆ‘'n, (1/2)^n) * (C * βˆ₯yβˆ₯) : by { rw tsum_mul_right, exact summable_geometric_two } ... = 2 * (C * βˆ₯yβˆ₯) : by rw tsum_geometric_two ... = 2 * C * βˆ₯yβˆ₯ + 0 : by rw [add_zero, mul_assoc] ... ≀ 2 * C * βˆ₯yβˆ₯ + βˆ₯yβˆ₯ : add_le_add (le_refl _) (norm_nonneg _) ... = (2 * C + 1) * βˆ₯yβˆ₯ : by ring, have fsumeq : βˆ€n:β„•, f(βˆ‘ i in finset.range n, u i) = y - (h^[n]) y, { assume n, induction n with n IH, { simp [f.map_zero] }, { rw [sum_range_succ, f.map_add, IH, iterate_succ'], simp [u, h, sub_eq_add_neg, add_comm, add_left_comm] } }, have : tendsto (Ξ»n, βˆ‘ i in range n, u i) at_top (𝓝 x) := su.has_sum.tendsto_sum_nat, have L₁ : tendsto (Ξ»n, f(βˆ‘ i in range n, u i)) at_top (𝓝 (f x)) := (f.continuous.tendsto _).comp this, simp only [fsumeq] at L₁, have Lβ‚‚ : tendsto (Ξ»n, y - (h^[n]) y) at_top (𝓝 (y - 0)), { refine tendsto_const_nhds.sub _, rw tendsto_iff_norm_tendsto_zero, simp only [sub_zero], refine squeeze_zero (Ξ»_, norm_nonneg _) hnle _, have : 0 = 0 * βˆ₯yβˆ₯, by rw zero_mul, rw this, refine tendsto.mul _ tendsto_const_nhds, exact tendsto_pow_at_top_nhds_0_of_lt_1 (by norm_num) (by norm_num) }, have feq : f x = y - 0, { apply tendsto_nhds_unique _ L₁ Lβ‚‚, simp }, rw sub_zero at feq, exact ⟨x, feq, x_ineq⟩ end /-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is open. -/ theorem open_mapping (surj : surjective f) : is_open_map f := begin assume s hs, rcases exists_preimage_norm_le f surj with ⟨C, Cpos, hC⟩, refine is_open_iff.2 (Ξ»y yfs, _), rcases mem_image_iff_bex.1 yfs with ⟨x, xs, fxy⟩, rcases is_open_iff.1 hs x xs with ⟨Ρ, Ξ΅pos, hΡ⟩, refine ⟨Ρ/C, div_pos Ξ΅pos Cpos, Ξ»z hz, _⟩, rcases hC (z-y) with ⟨w, wim, wnorm⟩, have : f (x + w) = z, by { rw [f.map_add, wim, fxy, add_sub_cancel'_right] }, rw ← this, have : x + w ∈ ball x Ξ΅ := calc dist (x+w) x = βˆ₯wβˆ₯ : by { rw dist_eq_norm, simp } ... ≀ C * βˆ₯z - yβˆ₯ : wnorm ... < C * (Ξ΅/C) : begin apply mul_lt_mul_of_pos_left _ Cpos, rwa [mem_ball, dist_eq_norm] at hz, end ... = Ξ΅ : mul_div_cancel' _ (ne_of_gt Cpos), exact set.mem_image_of_mem _ (hΞ΅ this) end namespace linear_equiv /-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/ theorem continuous_symm (e : E ≃ₗ[π•œ] F) (h : continuous e) : continuous e.symm := begin intros s hs, rw [← e.image_eq_preimage], rw [← e.coe_coe] at h ⊒, exact open_mapping βŸ¨β†‘e, h⟩ e.surjective s hs end /-- Associating to a linear equivalence between Banach spaces a continuous linear equivalence when the direct map is continuous, thanks to the Banach open mapping theorem that ensures that the inverse map is also continuous. -/ def to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[π•œ] F) (h : continuous e) : E ≃L[π•œ] F := { continuous_to_fun := h, continuous_inv_fun := e.continuous_symm h, ..e } @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[π•œ] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h) = e := rfl @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous_symm (e : E ≃ₗ[π•œ] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h).symm = e.symm := rfl end linear_equiv namespace continuous_linear_equiv /-- Convert a bijective continuous linear map `f : E β†’L[π•œ] F` between two Banach spaces to a continuous linear equivalence. -/ noncomputable def of_bijective (f : E β†’L[π•œ] F) (hinj : f.ker = βŠ₯) (hsurj : f.range = ⊀) : E ≃L[π•œ] F := (linear_equiv.of_bijective ↑f hinj hsurj).to_continuous_linear_equiv_of_continuous f.continuous @[simp] lemma coe_fn_of_bijective (f : E β†’L[π•œ] F) (hinj : f.ker = βŠ₯) (hsurj : f.range = ⊀) : ⇑(of_bijective f hinj hsurj) = f := rfl @[simp] lemma of_bijective_symm_apply_apply (f : E β†’L[π•œ] F) (hinj : f.ker = βŠ₯) (hsurj : f.range = ⊀) (x : E) : (of_bijective f hinj hsurj).symm (f x) = x := (of_bijective f hinj hsurj).symm_apply_apply x @[simp] lemma of_bijective_apply_symm_apply (f : E β†’L[π•œ] F) (hinj : f.ker = βŠ₯) (hsurj : f.range = ⊀) (y : F) : f ((of_bijective f hinj hsurj).symm y) = y := (of_bijective f hinj hsurj).apply_symm_apply y end continuous_linear_equiv
48f7f7c06ee22a64771cfba13839716a570b59da
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/measure_theory/pi_system.lean
312e0e6f11155bc2769af525a5d31420db9b23ec
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,773
lean
/- Copyright (c) 2021 Martin Zinkevich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Martin Zinkevich -/ import measure_theory.measurable_space_def import data.equiv.encodable.lattice /-! # Induction principles for measurable sets, related to Ο€-systems and Ξ»-systems. ## Main statements * The main theorem of this file is Dynkin's Ο€-Ξ» theorem, which appears here as an induction principle `induction_on_inter`. Suppose `s` is a collection of subsets of `Ξ±` such that the intersection of two members of `s` belongs to `s` whenever it is nonempty. Let `m` be the Οƒ-algebra generated by `s`. In order to check that a predicate `C` holds on every member of `m`, it suffices to check that `C` holds on the members of `s` and that `C` is preserved by complementation and *disjoint* countable unions. * The proof of this theorem relies on the notion of `is_pi_system`, i.e., a collection of sets which is closed under binary non-empty intersections. Note that this is a small variation around the usual notion in the literature, which often requires that a Ο€-system is non-empty, and closed also under disjoint intersections. This variation turns out to be convenient for the formalization. * The proof of Dynkin's Ο€-Ξ» theorem also requires the notion of `dynkin_system`, i.e., a collection of sets which contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. * `generate_pi_system g` gives the minimal Ο€-system containing `g`. This can be considered a Galois insertion into both measurable spaces and sets. * `generate_from_generate_pi_system_eq` proves that if you start from a collection of sets `g`, take the generated Ο€-system, and then the generated Οƒ-algebra, you get the same result as the Οƒ-algebra generated from `g`. This is useful because there are connections between independent sets that are Ο€-systems and the generated independent spaces. * `mem_generate_pi_system_Union_elim` and `mem_generate_pi_system_Union_elim'` show that any element of the Ο€-system generated from the union of a set of Ο€-systems can be represented as the intersection of a finite number of elements from these sets. ## Implementation details * `is_pi_system` is a predicate, not a type. Thus, we don't explicitly define the galois insertion, nor do we define a complete lattice. In theory, we could define a complete lattice and galois insertion on the subtype corresponding to `is_pi_system`. -/ open measurable_space set open_locale classical /-- A Ο€-system is a collection of subsets of `Ξ±` that is closed under binary intersection of non-disjoint sets. Usually it is also required that the collection is nonempty, but we don't do that here. -/ def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop := βˆ€ s t ∈ C, (s ∩ t : set Ξ±).nonempty β†’ s ∩ t ∈ C namespace measurable_space lemma is_pi_system_measurable_set {Ξ±:Type*} [measurable_space Ξ±] : is_pi_system {s : set Ξ± | measurable_set s} := Ξ» s t hs ht _, hs.inter ht end measurable_space lemma is_pi_system.singleton {Ξ±} (S : set Ξ±) : is_pi_system ({S} : set (set Ξ±)) := begin intros s t h_s h_t h_ne, rw [set.mem_singleton_iff.1 h_s, set.mem_singleton_iff.1 h_t, set.inter_self, set.mem_singleton_iff], end /-- Given a collection `S` of subsets of `Ξ±`, then `generate_pi_system S` is the smallest Ο€-system containing `S`. -/ inductive generate_pi_system {Ξ±} (S : set (set Ξ±)) : set (set Ξ±) | base {s : set Ξ±} (h_s : s ∈ S) : generate_pi_system s | inter {s t : set Ξ±} (h_s : generate_pi_system s) (h_t : generate_pi_system t) (h_nonempty : (s ∩ t).nonempty) : generate_pi_system (s ∩ t) lemma is_pi_system_generate_pi_system {Ξ±} (S : set (set Ξ±)) : is_pi_system (generate_pi_system S) := Ξ» s t h_s h_t h_nonempty, generate_pi_system.inter h_s h_t h_nonempty lemma subset_generate_pi_system_self {Ξ±} (S : set (set Ξ±)) : S βŠ† generate_pi_system S := Ξ» s, generate_pi_system.base lemma generate_pi_system_subset_self {Ξ±} {S : set (set Ξ±)} (h_S : is_pi_system S) : generate_pi_system S βŠ† S := begin intros x h, induction h with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { exact h_s, }, { exact h_S _ _ h_s h_u h_nonempty, }, end lemma generate_pi_system_eq {Ξ±} {S : set (set Ξ±)} (h_pi : is_pi_system S) : generate_pi_system S = S := set.subset.antisymm (generate_pi_system_subset_self h_pi) (subset_generate_pi_system_self S) lemma generate_pi_system_mono {Ξ±} {S T : set (set Ξ±)} (hST : S βŠ† T) : generate_pi_system S βŠ† generate_pi_system T := begin intros t ht, induction ht with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { exact generate_pi_system.base (set.mem_of_subset_of_mem hST h_s),}, { exact is_pi_system_generate_pi_system T _ _ h_s h_u h_nonempty, }, end lemma generate_pi_system_measurable_set {Ξ±} [M : measurable_space Ξ±] {S : set (set Ξ±)} (h_meas_S : βˆ€ s ∈ S, measurable_set s) (t : set Ξ±) (h_in_pi : t ∈ generate_pi_system S) : measurable_set t := begin induction h_in_pi with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { apply h_meas_S _ h_s, }, { apply measurable_set.inter h_s h_u, }, end lemma generate_from_measurable_set_of_generate_pi_system {Ξ±} {g : set (set Ξ±)} (t : set Ξ±) (ht : t ∈ generate_pi_system g) : (generate_from g).measurable_set' t := @generate_pi_system_measurable_set Ξ± (generate_from g) g (Ξ» s h_s_in_g, measurable_set_generate_from h_s_in_g) t ht lemma generate_from_generate_pi_system_eq {Ξ±} {g : set (set Ξ±)} : generate_from (generate_pi_system g) = generate_from g := begin apply le_antisymm; apply generate_from_le, { exact Ξ» t h_t, generate_from_measurable_set_of_generate_pi_system t h_t, }, { exact Ξ» t h_t, measurable_set_generate_from (generate_pi_system.base h_t), }, end /- Every element of the Ο€-system generated by the union of a family of Ο€-systems is a finite intersection of elements from the Ο€-systems. For an indexed union version, see `mem_generate_pi_system_Union_elim'`. -/ lemma mem_generate_pi_system_Union_elim {Ξ± Ξ²} {g : Ξ² β†’ set (set Ξ±)} (h_pi : βˆ€ b, is_pi_system (g b)) (t : set Ξ±) (h_t : t ∈ generate_pi_system (⋃ b, g b)) : βˆƒ (T : finset Ξ²) (f : Ξ² β†’ set Ξ±), (t = β‹‚ b ∈ T, f b) ∧ (βˆ€ b ∈ T, f b ∈ g b) := begin induction h_t with s h_s s t' h_gen_s h_gen_t' h_nonempty h_s h_t', { rcases h_s with ⟨t', ⟨⟨b, rfl⟩, h_s_in_t'⟩⟩, refine ⟨{b}, (Ξ» _, s), _⟩, simpa using h_s_in_t', }, { rcases h_t' with ⟨T_t', ⟨f_t', ⟨rfl, h_t'⟩⟩⟩, rcases h_s with ⟨T_s, ⟨f_s, ⟨rfl, h_s⟩ ⟩ ⟩, use [(T_s βˆͺ T_t'), (Ξ» (b:Ξ²), if (b ∈ T_s) then (if (b ∈ T_t') then (f_s b ∩ (f_t' b)) else (f_s b)) else (if (b ∈ T_t') then (f_t' b) else (βˆ… : set Ξ±)))], split, { ext a, simp_rw [set.mem_inter_iff, set.mem_Inter, finset.mem_union, or_imp_distrib], rw ← forall_and_distrib, split; intros h1 b; by_cases hbs : b ∈ T_s; by_cases hbt : b ∈ T_t'; specialize h1 b; simp only [hbs, hbt, if_true, if_false, true_implies_iff, and_self, false_implies_iff, and_true, true_and] at h1 ⊒, all_goals { exact h1, }, }, intros b h_b, split_ifs with hbs hbt hbt, { refine h_pi b (f_s b) (f_t' b) (h_s b hbs) (h_t' b hbt) (set.nonempty.mono _ h_nonempty), exact set.inter_subset_inter (set.bInter_subset_of_mem hbs) (set.bInter_subset_of_mem hbt), }, { exact h_s b hbs, }, { exact h_t' b hbt, }, { rw finset.mem_union at h_b, apply false.elim (h_b.elim hbs hbt), }, }, end /- Every element of the Ο€-system generated by an indexed union of a family of Ο€-systems is a finite intersection of elements from the Ο€-systems. For a total union version, see `mem_generate_pi_system_Union_elim`. -/ lemma mem_generate_pi_system_Union_elim' {Ξ± Ξ²} {g : Ξ² β†’ set (set Ξ±)} {s: set Ξ²} (h_pi : βˆ€ b ∈ s, is_pi_system (g b)) (t : set Ξ±) (h_t : t ∈ generate_pi_system (⋃ b ∈ s, g b)) : βˆƒ (T : finset Ξ²) (f : Ξ² β†’ set Ξ±), (↑T βŠ† s) ∧ (t = β‹‚ b ∈ T, f b) ∧ (βˆ€ b ∈ T, f b ∈ g b) := begin have : t ∈ generate_pi_system (⋃ (b : subtype s), (g ∘ subtype.val) b), { suffices h1 : (⋃ (b : subtype s), (g ∘ subtype.val) b) = (⋃ b (H : b ∈ s), g b), by rwa h1, ext x, simp only [exists_prop, set.mem_Union, function.comp_app, subtype.exists, subtype.coe_mk], refl }, rcases @mem_generate_pi_system_Union_elim Ξ± (subtype s) (g ∘ subtype.val) (Ξ» b, h_pi b.val b.property) t this with ⟨T, ⟨f,⟨ rfl, h_t'⟩⟩⟩, refine ⟨T.image subtype.val, function.extend subtype.val f (Ξ» (b:Ξ²), (βˆ… : set Ξ±)), by simp, _, _⟩, { ext a, split; { simp only [set.mem_Inter, subtype.forall, finset.set_bInter_finset_image], intros h1 b h_b h_b_in_T, have h2 := h1 b h_b h_b_in_T, revert h2, rw function.extend_apply subtype.val_injective, apply id } }, { intros b h_b, simp_rw [finset.mem_image, exists_prop, subtype.exists, exists_and_distrib_right, exists_eq_right] at h_b, cases h_b, have h_b_alt : b = (subtype.mk b h_b_w).val := rfl, rw [h_b_alt, function.extend_apply subtype.val_injective], apply h_t', apply h_b_h }, end namespace measurable_space variable {Ξ± : Type*} /-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. The main purpose of Dynkin systems is to provide a powerful induction rule for Οƒ-algebras generated by a collection of sets which is stable under intersection. A Dynkin system is also known as a "Ξ»-system" or a "d-system". -/ structure dynkin_system (Ξ± : Type*) := (has : set Ξ± β†’ Prop) (has_empty : has βˆ…) (has_compl : βˆ€ {a}, has a β†’ has aᢜ) (has_Union_nat : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, has (f i)) β†’ has (⋃ i, f i)) namespace dynkin_system @[ext] lemma ext : βˆ€ {d₁ dβ‚‚ : dynkin_system Ξ±}, (βˆ€ s : set Ξ±, d₁.has s ↔ dβ‚‚.has s) β†’ d₁ = dβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system Ξ±) lemma has_compl_iff {a} : d.has aᢜ ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, Ξ» h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) (h : βˆ€ i, d.has (f i)) : d.has (⋃ i, f i) := by { rw ← encodable.Union_decodeβ‚‚, exact d.has_Union_nat (encodable.Union_decodeβ‚‚_disjoint_on hd) (Ξ» n, encodable.Union_decodeβ‚‚_cases d.has_empty h) } theorem has_union {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : s₁ ∩ sβ‚‚ βŠ† βˆ…) : d.has (s₁ βˆͺ sβ‚‚) := by { rw union_eq_Union, exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) } lemma has_diff {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : sβ‚‚ βŠ† s₁) : d.has (s₁ \ sβ‚‚) := begin apply d.has_compl_iff.1, simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) hβ‚‚ (Ξ» x ⟨h₁, hβ‚‚βŸ©, h₁ (h hβ‚‚)), end instance : partial_order (dynkin_system Ξ±) := { le := Ξ» m₁ mβ‚‚, m₁.has ≀ mβ‚‚.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- Every measurable space (Οƒ-algebra) forms a Dynkin system -/ def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± := { has := m.measurable_set', has_empty := m.measurable_set_empty, has_compl := m.measurable_set_compl, has_Union_nat := assume f _ hf, m.measurable_set_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ mβ‚‚ : measurable_space Ξ±} : of_measurable_space m₁ ≀ of_measurable_space mβ‚‚ ↔ m₁ ≀ mβ‚‚ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. This inductive type gives the underlying collection of sets. -/ inductive generate_has (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€ t ∈ s, generate_has t | empty : generate_has βˆ… | compl : βˆ€ {a}, generate_has a β†’ generate_has aᢜ | Union : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, generate_has (f i)) β†’ generate_has (⋃ i, f i) lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sᢜ ↔ generate_has C s := by { refine ⟨_, generate_has.compl⟩, intro h, convert generate_has.compl h, simp } /-- The least Dynkin system containing a collection of basic sets. -/ def generate (s : set (set Ξ±)) : dynkin_system Ξ± := { has := generate_has s, has_empty := generate_has.empty, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl instance : inhabited (dynkin_system Ξ±) := ⟨generate univ⟩ /-- If a Dynkin system is closed under binary intersection, then it forms a `Οƒ`-algebra. -/ def to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) := { measurable_space . measurable_set' := d.has, measurable_set_empty := d.has_empty, measurable_set_compl := assume s h, d.has_compl h, measurable_set_Union := Ξ» f hf, begin rw ←Union_disjointed, exact d.has_Union (disjoint_disjointed _) (Ξ» n, disjointed_rec (Ξ» t i h, h_inter _ _ h $ d.has_compl $ hf i) (hf n)), end } lemma of_measurable_space_to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl /-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/ def restrict_on {s : set Ξ±} (h : d.has s) : dynkin_system Ξ± := { has := Ξ» t, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have tᢜ ∩ s = ((t ∩ s)ᢜ) \ sᢜ, from set.ext $ assume x, by { by_cases x ∈ s; simp [h] }, by { rw [this], exact d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _) }, has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact Ξ» i j h x ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, hd i j h ⟨h₁, hβ‚‚βŸ© }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set Ξ±)} (h : βˆ€ t ∈ s, d.has t) : generate s ≀ d := Ξ» t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±} (hs : (generate C).has s) : (generate_from C).measurable_set' s := generate_le (of_measurable_space (generate_from C)) (Ξ» t, measurable_set_generate_from) s hs lemma generate_inter {s : set (set Ξ±)} (hs : is_pi_system s) {t₁ tβ‚‚ : set Ξ±} (ht₁ : (generate s).has t₁) (htβ‚‚ : (generate s).has tβ‚‚) : (generate s).has (t₁ ∩ tβ‚‚) := have generate s ≀ (generate s).restrict_on htβ‚‚, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≀ (generate s).restrict_on this, from generate_le _ $ assume sβ‚‚ hsβ‚‚, show (generate s).has (sβ‚‚ ∩ s₁), from (sβ‚‚ ∩ s₁).eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ generate_has.empty) (Ξ» h, generate_has.basic _ (hs _ _ hsβ‚‚ hs₁ h)), have (generate s).has (tβ‚‚ ∩ s₁), from this _ htβ‚‚, show (generate s).has (s₁ ∩ tβ‚‚), by rwa [inter_comm], this _ ht₁ /-- **Dynkin's Ο€-Ξ» theorem**: Given a collection of sets closed under binary intersections, then the Dynkin system it generates is equal to the Οƒ-algebra it generates. This result is known as the Ο€-Ξ» theorem. A collection of sets closed under binary intersection is called a Ο€-system (often requiring additionnally that is is non-empty, but we drop this condition in the formalization). -/ lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) : generate_from s = (generate s).to_measurable_space (Ξ» t₁ tβ‚‚, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by { rw [of_measurable_space_to_measurable_space], exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) }) end dynkin_system theorem induction_on_inter {C : set Ξ± β†’ Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±] (h_eq : m = generate_from s) (h_inter : is_pi_system s) (h_empty : C βˆ…) (h_basic : βˆ€ t ∈ s, C t) (h_compl : βˆ€ t, measurable_set t β†’ C t β†’ C tᢜ) (h_union : βˆ€ f : β„• β†’ set Ξ±, pairwise (disjoint on f) β†’ (βˆ€ i, measurable_set (f i)) β†’ (βˆ€ i, C (f i)) β†’ C (⋃ i, f i)) : βˆ€ ⦃t⦄, measurable_set t β†’ C t := have eq : measurable_set = dynkin_system.generate_has s, by { rw [h_eq, dynkin_system.generate_from_eq h_inter], refl }, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by { rw [eq], exact ht }) (assume f hf ht, h_union f hf $ assume i, by { rw [eq], exact ht _ }) end measurable_space
784e7d4341a9e58330e1714cd2c162c5ff8ad61c
735bb6d9c54e20a6bdc031c27bff1717e68886b9
/tools/auto/experiments/set_basic.lean
d6521a984b56ae86cae3a2675cab110fa5401077
[]
no_license
digama0/library_dev
3ea441564c4d7eca54a562b701febaa4db6a1061
56520d5d1dda46d87c98bf3acdf850672fdab00f
refs/heads/master
1,611,047,574,219
1,500,469,648,000
1,500,469,648,000
87,738,883
0
0
null
1,491,771,880,000
1,491,771,879,000
null
UTF-8
Lean
false
false
28,086
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Test examples for "finish". -/ import data.set import ....logic.basic import ....algebra.lattice import ..finish open function lattice auto universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} namespace set @[simp] lemma mem_set_of {a : Ξ±} {p : Ξ± β†’ Prop} : a ∈ {a | p a} = p a := rfl -- 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 theorem union_def {s₁ sβ‚‚ : set Ξ±} : s₁ βˆͺ sβ‚‚ = {a | a ∈ s₁ ∨ a ∈ sβ‚‚} := rfl theorem inter_def {s₁ sβ‚‚ : set Ξ±} : s₁ ∩ sβ‚‚ = {a | a ∈ s₁ ∧ a ∈ sβ‚‚} := rfl theorem union_subset {s t r : set Ξ±} (sr : s βŠ† r) (tr : t βŠ† r) : s βˆͺ t βŠ† r := by finish [subset_def, union_def] /- old proof theorem union_subset {s t r : set Ξ±} (sr : s βŠ† r) (tr : t βŠ† r) : s βˆͺ t βŠ† r := Ξ» x xst, or.elim xst (Ξ» xs, sr xs) (Ξ» xt, tr xt) -/ theorem inter_subset_left (s t : set Ξ±) : s ∩ t βŠ† s := Ξ» x H, and.left H 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] /- old proof theorem subset_inter {s t r : set Ξ±} (rs : r βŠ† s) (rt : r βŠ† t) : r βŠ† s ∩ t := Ξ» x xr, and.intro (rs xr) (rt xr) -/ instance lattice_set : complete_lattice (set Ξ±) := { lattice.complete_lattice . le := has_subset.subset, le_refl := subset.refl, le_trans := assume a b c, subset.trans, le_antisymm := assume a b, subset.antisymm, sup := has_union.union, le_sup_left := subset_union_left, le_sup_right := subset_union_right, sup_le := assume a b c, union_subset, inf := set.inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, le_inf := assume a b c, subset_inter, top := {a | true }, le_top := assume s a h, trivial, bot := βˆ…, bot_le := assume s a, false.elim, Sup := Ξ»s, {a | βˆƒ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, Inf := Ξ»s, {a | βˆ€ t ∈ s, a ∈ t }, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in } /- strict subset -/ def strict_subset (a b : set Ξ±) := a βŠ† b ∧ a β‰  b instance : has_ssubset (set Ξ±) := ⟨strict_subset⟩ /- empty set -/ attribute [simp] mem_empty_eq empty_subset theorem set_eq_def (s t : set Ξ±) : s = t ↔ βˆ€ x, x ∈ s ↔ x ∈ t := ⟨begin intros h x, rw h end, set.ext⟩ theorem empty_def : (βˆ… : set Ξ±) = { x | false } := rfl theorem exists_mem_of_ne_empty {s : set Ξ±} (h : s β‰  βˆ…) : βˆƒ x, x ∈ s := by finish [set_eq_def] /- old proof theorem exists_mem_of_ne_empty {s : set Ξ±} (h : s β‰  βˆ…) : βˆƒ x, x ∈ s := classical.by_contradiction (assume : Β¬ βˆƒ x, x ∈ s, have βˆ€ x, x βˆ‰ s, from forall_not_of_not_exists this, show false, from h (eq_empty_of_forall_not_mem this)) -/ theorem subset_empty_iff (s : set Ξ±) : s βŠ† βˆ… ↔ s = βˆ… := by finish [set_eq_def] /- old proof theorem subset_empty_iff (s : set Ξ±) : s βŠ† βˆ… ↔ s = βˆ… := iff.intro eq_empty_of_subset_empty (assume xeq, begin rw xeq, apply subset.refl end) -/ lemma bounded_forall_empty_iff {p : Ξ± β†’ Prop} : (βˆ€ x ∈ (βˆ… : set Ξ±), p x) ↔ true := by finish [iff_def] /- old proof lemma bounded_forall_empty_iff {p : Ξ± β†’ Prop} : (βˆ€ x ∈ (βˆ… : set Ξ±), p x) ↔ true := iff.intro (assume H, true.intro) (assume H x H1, absurd H1 (not_mem_empty _)) -/ /- universal set -/ theorem mem_univ (x : Ξ±) : x ∈ @univ Ξ± := by trivial theorem mem_univ_iff (x : Ξ±) : x ∈ @univ Ξ± ↔ true := iff.rfl @[simp] theorem mem_univ_eq (x : Ξ±) : x ∈ @univ Ξ± = true := rfl theorem empty_ne_univ [h : inhabited Ξ±] : (βˆ… : set Ξ±) β‰  univ := by clarify [set_eq_def] /- old proof theorem empty_ne_univ [h : inhabited Ξ±] : (βˆ… : set Ξ±) β‰  univ := assume H : βˆ… = univ, absurd (mem_univ (inhabited.default Ξ±)) (eq.rec_on H (not_mem_empty _)) -/ theorem univ_def : @univ Ξ± = { x | true } := rfl @[simp] theorem subset_univ (s : set Ξ±) : s βŠ† univ := Ξ» x H, trivial theorem eq_univ_of_univ_subset {s : set Ξ±} (h : univ βŠ† s) : s = univ := by finish [subset_def, set_eq_def] /- old proof theorem eq_univ_of_univ_subset {s : set Ξ±} (h : univ βŠ† s) : s = univ := eq_of_subset_of_subset (subset_univ s) h -/ theorem eq_univ_of_forall {s : set Ξ±} (H : βˆ€ x, x ∈ s) : s = univ := by finish [set_eq_def] /- theorem eq_univ_of_forall {s : set Ξ±} (H : βˆ€ x, x ∈ s) : s = univ := ext (assume x, iff.intro (assume H', trivial) (assume H', H x)) -/ /- union -/ theorem mem_union_left {x : Ξ±} {a : set Ξ±} (b : set Ξ±) : x ∈ a β†’ x ∈ a βˆͺ b := assume h, or.inl h theorem mem_union_right {x : Ξ±} {b : set Ξ±} (a : set Ξ±) : x ∈ b β†’ x ∈ a βˆͺ b := assume h, or.inr h theorem mem_unionl {x : Ξ±} {a b : set Ξ±} : x ∈ a β†’ x ∈ a βˆͺ b := assume h, or.inl h theorem mem_unionr {x : Ξ±} {a b : set Ξ±} : x ∈ b β†’ x ∈ a βˆͺ b := assume h, or.inr h 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_iff (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 attribute [simp] union_self union_empty empty_union -- union_comm union_assoc theorem union_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ βˆͺ (sβ‚‚ βˆͺ s₃) = sβ‚‚ βˆͺ (s₁ βˆͺ s₃) := by finish [set_eq_def] /- old proof theorem union_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ βˆͺ (sβ‚‚ βˆͺ s₃) = sβ‚‚ βˆͺ (s₁ βˆͺ s₃) := by rw [←union_assoc, union_comm s₁, union_assoc] -/ theorem union_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := by finish [set_eq_def] /- old proof theorem union_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := by rw [union_assoc, union_comm sβ‚‚, union_assoc] -/ theorem union_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ t = t := by finish [subset_def, set_eq_def, iff_def] /- old proof theorem union_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ t = t := eq_of_subset_of_subset (union_subset h (subset.refl _)) (subset_union_right _ _) -/ theorem union_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s βˆͺ t = s := by finish [subset_def, set_eq_def, iff_def] /- old proof theorem union_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s βˆͺ t = s := by rw [union_comm, union_eq_self_of_subset_left h] -/ attribute [simp] union_comm union_assoc union_left_comm /- intersection -/ 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 attribute [simp] inter_self inter_empty empty_inter -- inter_comm inter_assoc theorem nonempty_of_inter_nonempty_right {T : Type} {s t : set T} (h : s ∩ t β‰  βˆ…) : t β‰  βˆ… := by finish [set_eq_def, iff_def] /- old proof theorem nonempty_of_inter_nonempty_right {T : Type} {s t : set T} (h : s ∩ t β‰  βˆ…) : t β‰  βˆ… := assume : t = βˆ…, have s ∩ t = βˆ…, from eq.subst (eq.symm this) (inter_empty s), h this -/ theorem nonempty_of_inter_nonempty_left {T : Type} {s t : set T} (h : s ∩ t β‰  βˆ…) : s β‰  βˆ… := by finish [set_eq_def, iff_def] /- old proof theorem nonempty_of_inter_nonempty_left {T : Type} {s t : set T} (h : s ∩ t β‰  βˆ…) : s β‰  βˆ… := assume : s = βˆ…, have s ∩ t = βˆ…, begin rw this, apply empty_inter end, h this -/ theorem inter_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := by finish [set_eq_def, iff_def] /- currently this does not work theorem inter_left_comm' (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := begin simp [inter_assoc, inter_comm] end -/ /- old proof theorem inter_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := by rw [←inter_assoc, inter_comm s₁, inter_assoc] -/ theorem inter_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := by finish [set_eq_def, iff_def] /- old proof theorem inter_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := by rw [inter_assoc, inter_comm sβ‚‚, inter_assoc] -/ theorem inter_univ (a : set Ξ±) : a ∩ univ = a := ext (assume x, and_true _) theorem univ_inter (a : set Ξ±) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_right {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : s ∩ u βŠ† t ∩ u := by finish [subset_def] /- old proof theorem inter_subset_inter_right {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : s ∩ u βŠ† t ∩ u := assume x, assume xsu, and.intro (H (and.left xsu)) (and.right xsu) -/ theorem inter_subset_inter_left {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : u ∩ s βŠ† u ∩ t := assume x, assume xus, and.intro (and.left xus) (H (and.right xus)) /- old proof theorem inter_subset_inter_left {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : u ∩ s βŠ† u ∩ t := assume x, assume xus, and.intro (and.left xus) (H (and.right xus)) -/ theorem inter_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s ∩ t = s := by finish [subset_def, set_eq_def, iff_def] theorem inter_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s ∩ t = t := by finish [subset_def, set_eq_def, iff_def] /- old proofs theorem inter_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s ∩ t = s := eq_of_subset_of_subset (inter_subset_left _ _) (subset_inter (subset.refl _) h) theorem inter_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s ∩ t = t := by rw [inter_comm, inter_eq_self_of_subset_left h] -/ attribute [simp] inter_comm inter_assoc inter_left_comm /- distributivity laws -/ theorem inter_distrib_left (s t u : set Ξ±) : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := ext (assume x, and_distrib _ _ _) theorem inter_distrib_right (s t u : set Ξ±) : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := ext (assume x, and_distrib_right _ _ _) theorem union_distrib_left (s t u : set Ξ±) : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := ext (assume x, or_distrib _ _ _) theorem union_distrib_right (s t u : set Ξ±) : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := ext (assume x, or_distrib_right _ _ _) /- insert -/ theorem insert_def (x : Ξ±) (a : set Ξ±) : insert x a = { y | y = x ∨ y ∈ a } := rfl theorem subset_insert (x : Ξ±) (a : set Ξ±) : a βŠ† insert x a := assume y, assume 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 := assume h, or.inr h theorem eq_or_mem_of_mem_insert {x a : Ξ±} {s : set Ξ±} : x ∈ insert a s β†’ x = a ∨ x ∈ s := assume h, h theorem mem_of_mem_insert_of_ne {x a : Ξ±} {s : set Ξ±} (xin : x ∈ insert a s) : x β‰  a β†’ x ∈ s := by finish [insert_def] /- old proof theorem mem_of_mem_insert_of_ne {x a : Ξ±} {s : set Ξ±} (xin : x ∈ insert a s) : x β‰  a β†’ x ∈ s := or_resolve_right (eq_or_mem_of_mem_insert xin) -/ @[simp] theorem mem_insert_iff (x a : Ξ±) (s : set Ξ±) : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.refl _ @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : set Ξ±} (h : a ∈ s) : insert a s = s := by finish [set_eq_def, iff_def] /- old proof @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : set Ξ±} (h : a ∈ s) : insert a s = s := ext (assume x, iff.intro (begin intro h, cases h with h' h', rw h', exact h, exact h' end) (mem_insert_of_mem _)) -/ theorem insert_comm (a b : Ξ±) (s : set Ξ±) : insert a (insert b s) = insert b (insert a s) := ext (assume c, by simp) -- TODO(Jeremy): make this automatic theorem insert_ne_empty (a : Ξ±) (s : set Ξ±) : insert a s β‰  βˆ… := begin safe [set_eq_def, iff_def], have h' := h a, clear h, finish end --begin safe [set_eq_def, iff_def]; have h' := h a; finish end /- old proof theorem insert_ne_empty (a : Ξ±) (s : set Ξ±) : insert a s β‰  βˆ… := Ξ» h, absurd (mem_insert a s) begin rw h, apply not_mem_empty end -/ -- 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 /- old proof theorem forall_of_forall_insert {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} (h : βˆ€ x, x ∈ insert a s β†’ P x) : βˆ€ x, x ∈ s β†’ P x := Ξ» x xs, h x (mem_insert_of_mem _ xs) -/ 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 /- old proof 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 | ._ (or.inl rfl) := ha | x (or.inr p) := h x p -/ lemma bounded_forall_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆ€ x ∈ insert a s, P x) ↔ P a ∧ (βˆ€x ∈ s, P x) := by finish [iff_def] /- old proof lemma bounded_forall_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆ€ x ∈ insert a s, P x) ↔ P a ∧ (βˆ€x ∈ s, P x) := ⟨assume h, ⟨h a $ mem_insert a s, forall_of_forall_insert h⟩, assume ⟨P_a, h⟩, forall_insert_of_forall h P_a⟩ -/ /- properties of 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] /- old proof @[simp] theorem mem_singleton_iff (a b : Ξ±) : a ∈ ({b} : set Ξ±) ↔ a = b := iff.intro (assume ainb, or.elim (ainb : a = b ∨ false) (Ξ» aeqb, aeqb) (Ξ» f, false.elim f)) (assume aeqb, or.inl aeqb) -/ -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : Ξ±) : a ∈ ({a} : set Ξ±) := mem_insert a _ theorem eq_of_mem_singleton {x y : Ξ±} (h : x ∈ ({y} : set Ξ±)) : x = y := by finish /- old proof theorem eq_of_mem_singleton {x y : Ξ±} (h : x ∈ ({y} : set Ξ±)) : x = y := or.elim (eq_or_mem_of_mem_insert h) (assume : x = y, this) (assume : x ∈ (βˆ… : set Ξ±), absurd this (not_mem_empty _)) -/ theorem mem_singleton_of_eq {x y : Ξ±} (H : x = y) : x ∈ ({y} : set Ξ±) := by finish /- old proof theorem mem_singleton_of_eq {x y : Ξ±} (H : x = y) : x ∈ ({y} : set Ξ±) := eq.subst (eq.symm H) (mem_singleton y) -/ theorem insert_eq (x : Ξ±) (s : set Ξ±) : insert x s = ({x} : set Ξ±) βˆͺ s := by finish [set_eq_def] /- old proof theorem insert_eq (x : Ξ±) (s : set Ξ±) : insert x s = ({x} : set Ξ±) βˆͺ s := ext (assume y, iff.intro (assume : y ∈ insert x s, or.elim this (assume : y = x, or.inl (or.inl this)) (assume : y ∈ s, or.inr this)) (assume : y ∈ ({x} : set Ξ±) βˆͺ s, or.elim this (assume : y ∈ ({x} : set Ξ±), or.inl (eq_of_mem_singleton this)) (assume : y ∈ s, or.inr this))) -/ @[simp] theorem insert_of_has_insert (x : Ξ±) (a : set Ξ±) : has_insert.insert x a = insert x a := rfl @[simp] theorem pair_eq_singleton (a : Ξ±) : ({a, a} : set Ξ±) = {a} := by finish /- old proof theorem pair_eq_singleton (a : Ξ±) : ({a, a} : set Ξ±) = {a} := begin rw insert_eq_of_mem, apply mem_singleton end -/ theorem singleton_ne_empty (a : Ξ±) : ({a} : set Ξ±) β‰  βˆ… := insert_ne_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 [set_eq_def, iff_def, subset_def] /- old proof theorem eq_sep_of_subset {s t : set Ξ±} (ssubt : s βŠ† t) : s = {x ∈ t | x ∈ s} := ext (assume x, iff.intro (assume : x ∈ s, ⟨ssubt this, this⟩) (assume : x ∈ {x ∈ t | x ∈ s}, this^.right)) -/ theorem sep_subset (s : set Ξ±) (p : Ξ± β†’ Prop) : {x ∈ s | p x} βŠ† s := assume x, assume H, and.left H theorem forall_not_of_sep_empty {s : set Ξ±} {p : Ξ± β†’ Prop} (h : {x ∈ s | p x} = βˆ…) : βˆ€ x ∈ s, Β¬ p x := by finish [set_eq_def] /- old proof theorem forall_not_of_sep_empty {s : set Ξ±} {p : Ξ± β†’ Prop} (h : {x ∈ s | p x} = βˆ…) : βˆ€ x ∈ s, Β¬ p x := assume x, assume : x ∈ s, assume : p x, have x ∈ {x ∈ s | p x}, from ⟨by assumption, this⟩, show false, from ne_empty_of_mem this h -/ /- complement -/ theorem mem_compl {s : set Ξ±} {x : Ξ±} (h : x βˆ‰ s) : x ∈ -s := h 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 [set_eq_def] @[simp] theorem compl_inter_self (s : set Ξ±) : -s ∩ s = βˆ… := by finish [set_eq_def] @[simp] theorem compl_empty : -(βˆ… : set Ξ±) = univ := by finish [set_eq_def] @[simp] theorem compl_union (s t : set Ξ±) : -(s βˆͺ t) = -s ∩ -t := by finish [set_eq_def] @[simp] theorem compl_compl (s : set Ξ±) : -(-s) = s := by finish [set_eq_def] -- ditto theorem compl_inter (s t : set Ξ±) : -(s ∩ t) = -s βˆͺ -t := by finish [set_eq_def] @[simp] theorem compl_univ : -(univ : set Ξ±) = βˆ… := by finish [set_eq_def] 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] theorem union_compl_self (s : set Ξ±) : s βˆͺ -s = univ := by finish [set_eq_def] theorem compl_union_self (s : set Ξ±) : -s βˆͺ s = univ := by finish [set_eq_def] theorem compl_comp_compl : compl ∘ compl = @id (set Ξ±) := funext (Ξ» s, compl_compl s) /- old proofs @[simp] theorem inter_compl_self (s : set Ξ±) : s ∩ -s = βˆ… := ext (assume x, and_not_self_iff _) @[simp] theorem compl_inter_self (s : set Ξ±) : -s ∩ s = βˆ… := ext (assume x, not_and_self_iff _) @[simp] theorem compl_empty : -(βˆ… : set Ξ±) = univ := ext (assume x, not_false_iff) @[simp] theorem compl_union (s t : set Ξ±) : -(s βˆͺ t) = -s ∩ -t := ext (assume x, not_or_iff _ _) theorem compl_compl (s : set Ξ±) : -(-s) = s := ext (assume x, classical.not_not_iff _) -- ditto theorem compl_inter (s t : set Ξ±) : -(s ∩ t) = -s βˆͺ -t := ext (assume x, classical.not_and_iff _ _) @[simp] theorem compl_univ : -(univ : set Ξ±) = βˆ… := ext (assume x, not_true_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] theorem union_compl_self (s : set Ξ±) : s βˆͺ -s = univ := ext (assume x, classical.or_not_self_iff _) theorem compl_union_self (s : set Ξ±) : -s βˆͺ s = univ := ext (assume x, classical.not_or_self_iff _) theorem compl_comp_compl : compl ∘ compl = @id (set Ξ±) := funext (Ξ» s, compl_compl s) -/ /- set difference -/ theorem diff_eq (s t : set Ξ±) : s \ t = s ∩ -t := rfl theorem mem_diff {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 mem_diff_iff (s t : set Ξ±) (x : Ξ±) : x ∈ s \ t ↔ x ∈ s ∧ x βˆ‰ t := iff.rfl @[simp] theorem mem_diff_eq (s t : set Ξ±) (x : Ξ±) : x ∈ s \ t = (x ∈ s ∧ x βˆ‰ t) := rfl theorem union_diff_cancel {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ (t \ s) = t := by finish [set_eq_def, iff_def, subset_def] theorem diff_subset (s t : set Ξ±) : s \ t βŠ† s := by finish [subset_def] theorem compl_eq_univ_diff (s : set Ξ±) : -s = univ \ s := by finish [set_eq_def] /- old proofs theorem union_diff_cancel {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ (t \ s) = t := begin rw [diff_eq, union_distrib_left, union_compl_self, inter_univ, union_eq_self_of_subset_left h] end theorem diff_subset (s t : set Ξ±) : s \ t βŠ† s := @inter_subset_left _ s _ theorem compl_eq_univ_diff (s : set Ξ±) : -s = univ \ s := ext (assume x, iff.intro (assume H, and.intro trivial H) (assume H, and.right H)) -/ /- 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 /- function image -/ section image @[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_eq (f : Ξ± β†’ Ξ²) (s : set Ξ±) (y: Ξ²) : y ∈ image f s = βˆƒ x, x ∈ s ∧ f x = y := rfl -- the introduction rule theorem mem_image {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} {y : Ξ²} (h₁ : x ∈ s) (hβ‚‚ : f x = y) : y ∈ image f s := ⟨x, h₁, hβ‚‚βŸ© theorem mem_image_of_mem (f : Ξ± β†’ Ξ²) {x : Ξ±} {a : set Ξ±} (h : x ∈ a) : f x ∈ image f a := mem_image h rfl def mem_image_elim {f : Ξ± β†’ Ξ²} {s : set Ξ±} {C : Ξ² β†’ Prop} (h : βˆ€ (x : Ξ±), x ∈ s β†’ C (f x)) : βˆ€{y : Ξ²}, y ∈ image f s β†’ C y | ._ ⟨a, a_in, rfl⟩ := h a a_in def mem_image_elim_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} {C : Ξ² β†’ Prop} {y : Ξ²} (h_y : y ∈ image f s) (h : βˆ€ (x : Ξ±), x ∈ s β†’ C (f x)) : C y := mem_image_elim h h_y theorem image_eq_image_of_eq_on {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {s : set Ξ±} (heq : eq_on f₁ fβ‚‚ s) : image f₁ s = image fβ‚‚ s := by finish [set_eq_def, iff_def, mem_image_eq, eq_on] /- old proof theorem image_eq_image_of_eq_on {f₁ fβ‚‚ : Ξ± β†’ Ξ²} {s : set Ξ±} (heq : eq_on f₁ fβ‚‚ s) : f₁ ' s = fβ‚‚ ' s := ext (assume y, iff.intro (assume ⟨x, xs, f₁xeq⟩, mem_image xs ((heq x xs)^.symm^.trans f₁xeq)) (assume ⟨x, xs, fβ‚‚xeq⟩, mem_image xs ((heq x xs)^.trans fβ‚‚xeq))) -/ -- TODO(Jeremy): make automatic lemma image_comp (f : Ξ² β†’ Ξ³) (g : Ξ± β†’ Ξ²) (a : set Ξ±) : image (f ∘ g) a = image f (image g a) := begin safe [set_eq_def, iff_def, mem_image_eq, comp], have h' := h_1 (g a_1), finish end /- old proof lemma image_comp (f : Ξ² β†’ Ξ³) (g : Ξ± β†’ Ξ²) (a : set Ξ±) : (f ∘ g) ' a = f ' (g ' a) := ext (assume z, iff.intro (assume ⟨x, (hx₁ : x ∈ a), (hxβ‚‚ : f (g x) = z)⟩, have g x ∈ g ' a, from mem_image hx₁ rfl, show z ∈ f ' (g ' a), from mem_image this hxβ‚‚) (assume ⟨y, ⟨x, (hz₁ : x ∈ a), (hzβ‚‚ : g x = y)⟩, (hyβ‚‚ : f y = z)⟩, have f (g x) = z, from eq.subst (eq.symm hzβ‚‚) hyβ‚‚, show z ∈ (f ∘ g) ' a, from mem_image hz₁ this)) -/ lemma image_subset {a b : set Ξ±} (f : Ξ± β†’ Ξ²) (h : a βŠ† b) : image f a βŠ† image f b := by finish [subset_def, mem_image_eq] /- old_proof lemma image_subset {a b : set Ξ±} (f : Ξ± β†’ Ξ²) (h : a βŠ† b) : f ' a βŠ† f ' b := assume y, assume ⟨x, hx₁, hxβ‚‚βŸ©, mem_image (h hx₁) hxβ‚‚ -/ theorem image_union (f : Ξ± β†’ Ξ²) (s t : set Ξ±) : image f (s βˆͺ t) = image f s βˆͺ image f t := by finish [set_eq_def, iff_def, mem_image_eq] /- old proof theorem image_union (f : Ξ± β†’ Ξ²) (s t : set Ξ±) : image f (s βˆͺ t) = image f s βˆͺ image f t := ext (assume y, iff.intro (assume ⟨x, (xst : x ∈ s βˆͺ t), (fxy : f x = y)⟩, or.elim xst (assume xs, or.inl (mem_image xs fxy)) (assume xt, or.inr (mem_image xt fxy))) (assume H : y ∈ image f s βˆͺ image f t, or.elim H (assume ⟨x, (xs : x ∈ s), (fxy : f x = y)⟩, mem_image (or.inl xs) fxy) (assume ⟨x, (xt : x ∈ t), (fxy : f x = y)⟩, mem_image (or.inr xt) fxy))) -/ theorem image_empty (f : Ξ± β†’ Ξ²) : image f βˆ… = βˆ… := by finish [set_eq_def, mem_image_eq] /- old proof theorem image_empty (f : Ξ± β†’ Ξ²) : image f βˆ… = βˆ… := eq_empty_of_forall_not_mem (assume y, assume ⟨x, (h : x ∈ βˆ…), h'⟩, h) -/ theorem fix_set_compl (t : set Ξ±) : compl t = - t := rfl -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_image_compl (t : set Ξ±) (S : set (set Ξ±)) : t ∈ image compl S ↔ -t ∈ S := begin safe [mem_image_eq, iff_def, fix_set_compl], have h' := h_1 (- t), safe [compl_compl], rw compl_compl at h, contradiction -- TODO(Jeremy): figure out why safe [compl_compl] doesn't solve it. end /- old proof theorem mem_image_compl (t : set Ξ±) (S : set (set Ξ±)) : t ∈ compl ' S ↔ -t ∈ S := iff.intro (assume ⟨t', (Ht' : t' ∈ S), (Ht : -t' = t)⟩, show -t ∈ S, begin rw [←Ht, compl_compl], exact Ht' end) (assume : -t ∈ S, have -(-t) ∈ compl ' S, from mem_image_of_mem compl this, show t ∈ compl ' S, from compl_compl t β–Έ this) -/ theorem image_id (s : set Ξ±) : image id s = s := by finish [set_eq_def, iff_def, mem_image_eq] /- old proof theorem image_id (s : set Ξ±) : id ' s = s := ext (assume x, iff.intro (assume ⟨x', (hx' : x' ∈ s), (x'eq : x' = x)⟩, show x ∈ s, begin rw [←x'eq], apply hx' end) (assume : x ∈ s, mem_image_of_mem id this)) -/ theorem compl_compl_image (S : set (set Ξ±)) : image compl (image compl S) = S := by rw [←image_comp, compl_comp_compl, image_id] lemma bounded_forall_image_of_bounded_forall {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} (h : βˆ€ x ∈ s, p (f x)) : βˆ€ y ∈ image f s, p y := by finish [mem_image_eq] /- old proof lemma bounded_forall_image_of_bounded_forall {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} (h : βˆ€ x ∈ s, p (f x)) : βˆ€ y ∈ f ' s, p y | ._ ⟨x, s_in, rfl⟩ := h x s_in -/ lemma bounded_forall_image_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} : (βˆ€ y ∈ image f s, p y) ↔ (βˆ€ x ∈ s, p (f x)) := begin safe [mem_image_eq, iff_def], have h' := h_1 (f a), finish end /- old proof lemma bounded_forall_image_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} : (βˆ€ y ∈ f ' s, p y) ↔ (βˆ€ x ∈ s, p (f x)) := iff.intro (assume h x xs, h _ (mem_image_of_mem _ xs)) bounded_forall_image_of_bounded_forall -/ lemma image_insert_eq {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} : image f (insert a s) = insert (f a) (image f s) := begin safe [set_eq_def, iff_def, mem_image_eq], have h' := h_1 a, finish end /- old proof lemma image_insert_eq {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} : f ' insert a s = insert (f a) (f ' s) := set.ext $ assume x, ⟨ assume h, match x, h with | ._, ⟨._, ⟨or.inl rfl, rfl⟩⟩ := mem_insert _ _ | ._, ⟨b, ⟨or.inr h, rfl⟩⟩ := mem_insert_of_mem _ $ mem_image h rfl end, assume h, match x, h with | ._, or.inl rfl := mem_image (mem_insert _ _) rfl | ._, or.inr ⟨x, ⟨_, rfl⟩⟩ := mem_image (mem_insert_of_mem _ β€Ήx ∈ sβ€Ί) rfl end⟩ -/ end image -- TODO(Jeremy): stopped here. end set
e2979705767fba644448394ac586fc98cd8533a2
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/cubical/cubeover.hlean
87727cfed1ff49474a868fad6579f77476979f36
[ "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
4,216
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Cubeovers -/ import .squareover .cube open equiv is_equiv namespace eq -- we need to specify B explicitly, also in pathovers inductive cubeover {A : Type} (B : A β†’ Type) {aβ‚€β‚€β‚€ : A} {bβ‚€β‚€β‚€ : B aβ‚€β‚€β‚€} : Ξ {aβ‚‚β‚€β‚€ aβ‚€β‚‚β‚€ aβ‚‚β‚‚β‚€ aβ‚€β‚€β‚‚ aβ‚‚β‚€β‚‚ aβ‚€β‚‚β‚‚ aβ‚‚β‚‚β‚‚ : A} {p₁₀₀ : aβ‚€β‚€β‚€ = aβ‚‚β‚€β‚€} {p₀₁₀ : aβ‚€β‚€β‚€ = aβ‚€β‚‚β‚€} {p₀₀₁ : aβ‚€β‚€β‚€ = aβ‚€β‚€β‚‚} {p₁₂₀ : aβ‚€β‚‚β‚€ = aβ‚‚β‚‚β‚€} {p₂₁₀ : aβ‚‚β‚€β‚€ = aβ‚‚β‚‚β‚€} {p₂₀₁ : aβ‚‚β‚€β‚€ = aβ‚‚β‚€β‚‚} {p₁₀₂ : aβ‚€β‚€β‚‚ = aβ‚‚β‚€β‚‚} {p₀₁₂ : aβ‚€β‚€β‚‚ = aβ‚€β‚‚β‚‚} {p₀₂₁ : aβ‚€β‚‚β‚€ = aβ‚€β‚‚β‚‚} {p₁₂₂ : aβ‚€β‚‚β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₁₂ : aβ‚‚β‚€β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₂₁ : aβ‚‚β‚‚β‚€ = aβ‚‚β‚‚β‚‚} {s₁₁₀ : square p₀₁₀ p₂₁₀ p₁₀₀ p₁₂₀} {s₁₁₂ : square p₀₁₂ p₂₁₂ p₁₀₂ p₁₂₂} {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} {s₂₁₁ : square p₂₁₀ p₂₁₂ p₂₀₁ p₂₂₁} {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ p₂₀₁} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ p₂₂₁} (c : cube s₀₁₁ s₂₁₁ s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) {bβ‚€β‚‚β‚€ : B aβ‚€β‚‚β‚€} {bβ‚‚β‚€β‚€ : B aβ‚‚β‚€β‚€} {bβ‚‚β‚‚β‚€ : B aβ‚‚β‚‚β‚€} {bβ‚€β‚€β‚‚ : B aβ‚€β‚€β‚‚} {bβ‚€β‚‚β‚‚ : B aβ‚€β‚‚β‚‚} {bβ‚‚β‚€β‚‚ : B aβ‚‚β‚€β‚‚} {bβ‚‚β‚‚β‚‚ : B aβ‚‚β‚‚β‚‚} {q₁₀₀ : pathover B bβ‚€β‚€β‚€ p₁₀₀ bβ‚‚β‚€β‚€} {q₀₁₀ : pathover B bβ‚€β‚€β‚€ p₀₁₀ bβ‚€β‚‚β‚€} {q₀₀₁ : pathover B bβ‚€β‚€β‚€ p₀₀₁ bβ‚€β‚€β‚‚} {q₁₂₀ : pathover B bβ‚€β‚‚β‚€ p₁₂₀ bβ‚‚β‚‚β‚€} {q₂₁₀ : pathover B bβ‚‚β‚€β‚€ p₂₁₀ bβ‚‚β‚‚β‚€} {q₂₀₁ : pathover B bβ‚‚β‚€β‚€ p₂₀₁ bβ‚‚β‚€β‚‚} {q₁₀₂ : pathover B bβ‚€β‚€β‚‚ p₁₀₂ bβ‚‚β‚€β‚‚} {q₀₁₂ : pathover B bβ‚€β‚€β‚‚ p₀₁₂ bβ‚€β‚‚β‚‚} {q₀₂₁ : pathover B bβ‚€β‚‚β‚€ p₀₂₁ bβ‚€β‚‚β‚‚} {q₁₂₂ : pathover B bβ‚€β‚‚β‚‚ p₁₂₂ bβ‚‚β‚‚β‚‚} {q₂₁₂ : pathover B bβ‚‚β‚€β‚‚ p₂₁₂ bβ‚‚β‚‚β‚‚} {q₂₂₁ : pathover B bβ‚‚β‚‚β‚€ p₂₂₁ bβ‚‚β‚‚β‚‚} (t₀₁₁ : squareover B s₀₁₁ q₀₁₀ q₀₁₂ q₀₀₁ q₀₂₁) (t₂₁₁ : squareover B s₂₁₁ q₂₁₀ q₂₁₂ q₂₀₁ q₂₂₁) (t₁₀₁ : squareover B s₁₀₁ q₁₀₀ q₁₀₂ q₀₀₁ q₂₀₁) (t₁₂₁ : squareover B s₁₂₁ q₁₂₀ q₁₂₂ q₀₂₁ q₂₂₁) (t₁₁₀ : squareover B s₁₁₀ q₀₁₀ q₂₁₀ q₁₀₀ q₁₂₀) (t₁₁₂ : squareover B s₁₁₂ q₀₁₂ q₂₁₂ q₁₀₂ q₁₂₂), Type := idcubeo : cubeover B idc idso idso idso idso idso idso -- variables {A : Type} {aβ‚€β‚€β‚€ aβ‚‚β‚€β‚€ aβ‚€β‚‚β‚€ aβ‚‚β‚‚β‚€ aβ‚€β‚€β‚‚ aβ‚‚β‚€β‚‚ aβ‚€β‚‚β‚‚ aβ‚‚β‚‚β‚‚ : A} -- {p₁₀₀ : aβ‚€β‚€β‚€ = aβ‚‚β‚€β‚€} {p₀₁₀ : aβ‚€β‚€β‚€ = aβ‚€β‚‚β‚€} {p₀₀₁ : aβ‚€β‚€β‚€ = aβ‚€β‚€β‚‚} -- {p₁₂₀ : aβ‚€β‚‚β‚€ = aβ‚‚β‚‚β‚€} {p₂₁₀ : aβ‚‚β‚€β‚€ = aβ‚‚β‚‚β‚€} {p₂₀₁ : aβ‚‚β‚€β‚€ = aβ‚‚β‚€β‚‚} -- {p₁₀₂ : aβ‚€β‚€β‚‚ = aβ‚‚β‚€β‚‚} {p₀₁₂ : aβ‚€β‚€β‚‚ = aβ‚€β‚‚β‚‚} {p₀₂₁ : aβ‚€β‚‚β‚€ = aβ‚€β‚‚β‚‚} -- {p₁₂₂ : aβ‚€β‚‚β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₁₂ : aβ‚‚β‚€β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₂₁ : aβ‚‚β‚‚β‚€ = aβ‚‚β‚‚β‚‚} -- {s₁₁₀ : square p₀₁₀ p₂₁₀ p₁₀₀ p₁₂₀} -- {s₁₁₂ : square p₀₁₂ p₂₁₂ p₁₀₂ p₁₂₂} -- {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ p₂₀₁} -- {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ p₂₂₁} -- {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} -- {s₂₁₁ : square p₂₁₀ p₂₁₂ p₂₀₁ p₂₂₁} end eq
272a09c5e462e0a2ebfbdd5099b6aa6a13455342
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/rel_tac1.lean
781ba3d405ee56a7b71ef1f22011360e1018b5b1
[ "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
408
lean
open tactic example (a b : nat) : a = a := by reflexivity example (a : nat) (b : bool) : a == b β†’ b == a := by do intros, symmetry, trace_state, assumption print "-----------" example (a : nat) (b : bool) (c : string) : a == b β†’ b == c β†’ a == c := by do intro_lst [`H1, `H2], transitivity, trace_state, get_local `H1 >>= exact, assumption example (a b : bool) : a == a := by reflexivity
40143fa0948070ed2d04f67da0a0cbab57f6cc39
5b273b8c05e2f73fb74340ce100ce261900a98cd
/limits.lean
8ceca0466e2655243c61e9c15ce1abe3b94be860
[]
no_license
ChrisHughes24/leanstuff1
2eba44bc48da6e544e07495b41e1703f81dc1c24
cbcd788b8b1d07b20b2fff4482c870077a13d1c0
refs/heads/master
1,631,670,333,297
1,527,093,981,000
1,527,093,981,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,874
lean
import data.complex.basic tactic.ring open is_absolute_value lemma finset.exists_max {Ξ± : Type*} [decidable_linear_order Ξ±] [inhabited Ξ±] (s : finset Ξ±) : βˆƒ a : Ξ±, βˆ€ b : Ξ±, b ∈ s β†’ b ≀ a := begin apply finset.induction_on s, existsi default Ξ±,simp, assume a s as hi, cases hi with m hm, existsi max a m, assume b,simp,assume h, cases h with h h, rw h, exact le_max_left _ _, exact le_trans (hm b h) (le_max_right _ _), end lemma seq_bounded_above_of_cau {Ξ± : Type*} {Ξ² : Type*} [discrete_linear_ordered_field Ξ±] [ring Ξ²] {f : β„• β†’ Ξ²} {abv : Ξ² β†’ Ξ±} [is_absolute_value abv] : is_cau_seq abv f β†’ βˆƒ a : Ξ±, βˆ€ i : β„•, abv (f i) < a := begin assume h,unfold is_cau_seq at h, have iΞ± : inhabited Ξ± := ⟨0⟩, cases h 1 (by norm_num) with i hi, have : βˆ€ j : β„•, j β‰₯ i β†’ abv (f j) - abv (f i) < 1,assume j ji, have := hi j ji, exact lt_of_le_of_lt (sub_abv_le_abv_sub _ _ _) this, generalize hs : finset.image (Ξ» k, abv (f k)) (finset.range i) = s, cases finset.exists_max s with ms hms, existsi max (1 + abv (f i)) (ms + 1),assume i₁, cases lt_or_ge i₁ i, have := finset.mem_image_of_mem (Ξ» (k : β„•), abv (f k)) (finset.mem_range.mpr h_1),rw hs at this, have := hms _ this,simp at this, exact lt_of_le_of_lt this (lt_of_lt_of_le (lt_add_one _) (le_max_right _ _)), have := this i₁ h_1, rw sub_lt_iff_lt_add at this, exact lt_of_lt_of_le this (le_max_left _ _), end section real open real lemma real.lim_const {f : β„• β†’ ℝ} (hf : is_cau_seq abs f) (x : ℝ) : x = real.lim f ↔ cau_seq.const abs x β‰ˆ ⟨f, hf⟩ := begin have := real.equiv_lim ⟨f, hf⟩,split,assume h,rw h,simp at this,exact setoid.symm this, assume h,have := setoid.trans h this,rw cau_seq.const_equiv at this,simp at this, exact this, end lemma real.lim_eq_lim_iff_equiv {f g : β„• β†’ ℝ} (hf : is_cau_seq abs f) (hg : is_cau_seq abs g) : real.lim f = real.lim g ↔ @has_equiv.equiv (cau_seq ℝ abs) _ ⟨f, hf⟩ ⟨g, hg⟩ := begin have h₁:= real.lim_const hg (real.lim f), have hβ‚‚:= real.lim_const hf (real.lim g), split,assume h, simp * at *, exact setoid.trans (setoid.symm hβ‚‚) h₁, assume h,rw h₁,have := real.equiv_lim ⟨f, hf⟩,simp at this, exact setoid.trans (setoid.symm this) h, end -- would be useful to have is_cau_seq abs f β†’ is_cau_seq abs g β†’ is_cau_seq abs (Ξ» i, f i + g i) -- in the library, similarly for mul lemma real.lim_add {f g : β„• β†’ ℝ} (hf : is_cau_seq abs f) (hg : is_cau_seq abs g) : real.lim f + real.lim g = real.lim (Ξ» x, f x + g x) := begin have : βˆ€ (Ξ΅ : ℝ), Ξ΅ > 0 β†’ (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ abs (f j + (g j + (-real.lim f + -real.lim g))) < Ξ΅), {assume Ξ΅ Ξ΅0, cases real.equiv_lim ⟨f, hf⟩ (Ξ΅ / 2) (div_pos Ξ΅0 (by norm_num)) with fi hfi,simp at hfi, cases real.equiv_lim ⟨g, hg⟩ (Ξ΅ / 2) (div_pos Ξ΅0 (by norm_num)) with gi hgi,simp at hgi, existsi max fi gi, assume j hj,rw [add_left_comm (g j),←add_assoc (f j)], refine lt_of_le_of_lt (abs_add _ _) _,rw ←add_halves Ξ΅, exact add_lt_add (hfi j (le_trans (le_max_left _ _) hj)) (hgi j (le_trans (le_max_right _ _) hj))}, have cau : is_cau_seq abs (Ξ» (x : β„•), f x + g x), refine cau_seq.of_near _ (cau_seq.const abs (real.lim f + real.lim g)) _,simpa, rw real.lim_const,refine setoid.symm _,exact cau,assume Ξ΅ Ξ΅0,simp,exact this Ξ΅ Ξ΅0, end lemma real.lim_mul {f g : β„• β†’ ℝ} (hf : is_cau_seq abs f) (hg : is_cau_seq abs g) : real.lim f * real.lim g = real.lim (Ξ» x, f x * g x) := begin have : βˆ€ (Ξ΅ : ℝ), Ξ΅ > 0 β†’ (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ abs (f j * g j - (real.lim f * real.lim g)) < Ξ΅), { assume Ξ΅ Ξ΅0, cases seq_bounded_above_of_cau hg with G hG, have G0 : 0 < G := lt_of_le_of_lt (abs_nonneg _) (hG 0), have Gf : 0 < G + abs (lim f) := add_pos_of_pos_of_nonneg G0 (abs_nonneg _), have GΞ΅ : 0 < Ξ΅ / (G + abs (lim f)) := div_pos Ξ΅0 Gf, cases real.equiv_lim ⟨f, hf⟩ _ GΞ΅ with fi hfi, simp at hfi, cases real.equiv_lim ⟨g, hg⟩ _ GΞ΅ with gi hgi, simp at hgi, existsi max fi gi,assume j ji, rw (by simp[mul_add,add_mul];ring : f j * g j -lim f * lim g = (f j - lim f) * g j + lim f * (g j - lim g)), refine lt_of_le_of_lt (abs_add _ _) _, rw [abs_mul], suffices : abs (f j - lim f) * G + abs (lim f * (g j - lim g)) < Ξ΅, exact lt_of_le_of_lt (add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (hG j)) (abs_nonneg _)) (le_refl _)) this, rw abs_mul, have : Ξ΅ = Ξ΅ * G / (G + abs (lim f)) + Ξ΅ * abs (lim f) / (G + abs (lim f)), {rw [←add_div,←mul_add,mul_div_cancel _ (ne_of_lt Gf).symm]}, rw this, refine add_lt_add_of_lt_of_le _ _, {rw [mul_comm,mul_comm Ξ΅,mul_div_assoc], exact mul_lt_mul_of_pos_left (hfi j (le_trans (le_max_left _ _) ji)) G0}, rw [mul_comm Ξ΅,mul_div_assoc], refine mul_le_mul_of_nonneg_left (le_of_lt (hgi j (le_trans (le_max_right _ _) ji))) (abs_nonneg _)}, have cau : is_cau_seq abs (Ξ» (x : β„•), f x * g x), refine cau_seq.of_near _ (cau_seq.const abs (real.lim f * real.lim g)) _,simpa, rw real.lim_const, refine setoid.symm _,exact cau,assume Ξ΅ Ξ΅0,simp, exact this Ξ΅ Ξ΅0, end end real lemma complex.re_const_equiv_of_const_equiv {f : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (z : β„‚) : cau_seq.const complex.abs z β‰ˆ ⟨f, hf⟩ β†’ cau_seq.const abs z.re β‰ˆ ⟨(Ξ» (n : β„•), (f n).re), complex.is_cau_seq_re ⟨f,hf⟩⟩ := begin assume h,assume Ξ΅ Ξ΅0,cases h Ξ΅ Ξ΅0 with i hi,existsi i,assume j ji, replace hi := hi j ji, simp at *, rw [←complex.neg_re,←complex.add_re], exact lt_of_le_of_lt (complex.abs_re_le_abs _) hi, end lemma complex.im_const_equiv_of_const_equiv {f : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (z : β„‚) : cau_seq.const complex.abs z β‰ˆ ⟨f, hf⟩ β†’ cau_seq.const abs z.im β‰ˆ ⟨(Ξ» (n : β„•), (f n).im), complex.is_cau_seq_im ⟨f,hf⟩⟩ := begin assume h,assume Ξ΅ Ξ΅0,cases h Ξ΅ Ξ΅0 with i hi,existsi i,assume j ji, replace hi := hi j ji, simp at *, rw [←complex.neg_im,←complex.add_im], exact lt_of_le_of_lt (complex.abs_im_le_abs _) hi, end lemma complex.lim_const {f : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (z : β„‚) : z = complex.lim f ↔ cau_seq.const complex.abs z β‰ˆ ⟨f, hf⟩ := begin split,have := complex.equiv_lim ⟨f, hf⟩,simp at this, assume h,rw h, exact setoid.symm this,assume h, unfold complex.lim,cases z with zre zim,simp, split, have := real.equiv_lim ⟨(Ξ» (n : β„•), (f n).re), complex.is_cau_seq_re ⟨f,hf⟩⟩, rw ←cau_seq.const_equiv,simp at this, have hf := complex.re_const_equiv_of_const_equiv hf {re := zre, im := zim} h,simp at hf, exact setoid.trans hf this, have := real.equiv_lim ⟨(Ξ» (n : β„•), (f n).im), complex.is_cau_seq_im ⟨f,hf⟩⟩, rw ←cau_seq.const_equiv,simp at this, have hf := complex.im_const_equiv_of_const_equiv hf {re := zre, im := zim} h,simp at hf, exact setoid.trans hf this, end lemma complex.lim_eq_lim_iff_equiv {f g : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (hg : is_cau_seq complex.abs g) : complex.lim f = complex.lim g ↔ @has_equiv.equiv (cau_seq β„‚ complex.abs) _ ⟨f, hf⟩ ⟨g, hg⟩ := begin have h₁:= complex.lim_const hg (complex.lim f), have hβ‚‚:= complex.lim_const hf (complex.lim g), split,assume h, simp * at *, exact setoid.trans (setoid.symm hβ‚‚) h₁, assume h,rw h₁,have := complex.equiv_lim ⟨f, hf⟩,simp at this, exact setoid.trans (setoid.symm this) h, end open complex lemma complex.lim_add {f g : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (hg : is_cau_seq complex.abs g) : complex.lim f + complex.lim g = complex.lim (Ξ» x, f x + g x) := begin have : βˆ€ (Ξ΅ : ℝ), Ξ΅ > 0 β†’ (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ complex.abs (f j + (g j + (-complex.lim f + -complex.lim g))) < Ξ΅), {assume Ξ΅ Ξ΅0, cases complex.equiv_lim ⟨f, hf⟩ (Ξ΅ / 2) (div_pos Ξ΅0 (by norm_num)) with fi hfi,simp at hfi, cases complex.equiv_lim ⟨g, hg⟩ (Ξ΅ / 2) (div_pos Ξ΅0 (by norm_num)) with gi hgi,simp at hgi, existsi max fi gi, assume j hj,rw [add_left_comm (g j),←add_assoc (f j)], refine lt_of_le_of_lt (complex.abs_add _ _) _,rw ←add_halves Ξ΅, exact add_lt_add (hfi j (le_trans (le_max_left _ _) hj)) (hgi j (le_trans (le_max_right _ _) hj))}, have cau : is_cau_seq complex.abs (Ξ» (x : β„•), f x + g x), refine cau_seq.of_near _ (cau_seq.const complex.abs (lim f + lim g)) _,simpa, rw lim_const,refine setoid.symm _,exact cau,assume Ξ΅ Ξ΅0,simp,exact this Ξ΅ Ξ΅0, end lemma complex.lim_mul {f g : β„• β†’ β„‚} (hf : is_cau_seq complex.abs f) (hg : is_cau_seq complex.abs g) : complex.lim f * complex.lim g = complex.lim (Ξ» x, f x * g x) := begin have : βˆ€ (Ξ΅ : ℝ), Ξ΅ > 0 β†’ (βˆƒ (i : β„•), βˆ€ (j : β„•), j β‰₯ i β†’ complex.abs (f j * g j - (complex.lim f * complex.lim g)) < Ξ΅), { assume Ξ΅ Ξ΅0, cases seq_bounded_above_of_cau hg with G hG, have G0 : 0 < G := lt_of_le_of_lt (complex.abs_nonneg _) (hG 0), have Gf : 0 < G + complex.abs (complex.lim f) := add_pos_of_pos_of_nonneg G0 (complex.abs_nonneg _), have GΞ΅ : 0 < Ξ΅ / (G + complex.abs (complex.lim f)) := div_pos Ξ΅0 Gf, cases complex.equiv_lim ⟨f, hf⟩ _ GΞ΅ with fi hfi, simp at hfi, cases complex.equiv_lim ⟨g, hg⟩ _ GΞ΅ with gi hgi, simp at hgi, existsi max fi gi,assume j ji, rw (by simp[mul_add,add_mul];ring : f j * g j -complex.lim f * complex.lim g = (f j - complex.lim f) * g j + complex.lim f * (g j - complex.lim g)), refine lt_of_le_of_lt (complex.abs_add _ _) _, rw [complex.abs_mul], suffices : complex.abs (f j - lim f) * G + complex.abs (lim f * (g j - lim g)) < Ξ΅, exact lt_of_le_of_lt (add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (hG j)) (abs_nonneg _)) (le_refl _)) this, rw complex.abs_mul, have : Ξ΅ = Ξ΅ * G / (G + abs (lim f)) + Ξ΅ * abs (lim f) / (G + abs (lim f)), {rw [←add_div,←mul_add,mul_div_cancel _ (ne_of_lt Gf).symm]}, rw this, refine add_lt_add_of_lt_of_le _ _, {rw [mul_comm,mul_comm Ξ΅,mul_div_assoc], exact mul_lt_mul_of_pos_left (hfi j (le_trans (le_max_left _ _) ji)) G0}, rw [mul_comm Ξ΅,mul_div_assoc], refine mul_le_mul_of_nonneg_left (le_of_lt (hgi j (le_trans (le_max_right _ _) ji))) (abs_nonneg _)}, have cau : is_cau_seq complex.abs (Ξ» (x : β„•), f x * g x), refine cau_seq.of_near _ (cau_seq.const abs (lim f * lim g)) _,simpa, rw lim_const, refine setoid.symm _,exact cau,assume Ξ΅ Ξ΅0,simp, exact this Ξ΅ Ξ΅0, end
ed011c832f292d6488db7e4a1f9078cfe53fc93b
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Util/ReplaceLevel.lean
8767e7321103ac76802423e031f18068ab5e908e
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,463
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Expr namespace Lean namespace Level partial def replace (f? : Level β†’ Option Level) : Level β†’ Level | u => match f? u with | some v => v | none => match u with | max v₁ vβ‚‚ _ => mkLevelMax (replace v₁) (replace vβ‚‚) | imax v₁ vβ‚‚ _ => mkLevelIMax (replace v₁) (replace vβ‚‚) | succ v _ => mkLevelSucc (replace v) | _ => u end Level namespace Expr namespace ReplaceLevelImpl abbrev cacheSize : USize := 8192 structure State := (keys : Array Expr) -- Remark: our "unsafe" implementation relies on the fact that `()` is not a valid Expr (results : Array Expr) abbrev ReplaceM := StateM State @[inline] unsafe def cache (i : USize) (key : Expr) (result : Expr) : ReplaceM Expr := do modify $ fun s => { keys := s.keys.uset i key lcProof, results := s.results.uset i result lcProof }; pure result @[specialize] unsafe def replaceUnsafeM (f? : Level β†’ Option Level) (size : USize) : Expr β†’ ReplaceM Expr | e => do c ← get; let h := ptrAddrUnsafe e; let i := h % size; if ptrAddrUnsafe (c.keys.uget i lcProof) == h then pure $ c.results.uget i lcProof else match e with | Expr.forallE _ d b _ => do d ← replaceUnsafeM d; b ← replaceUnsafeM b; cache i e $ e.updateForallE! d b | Expr.lam _ d b _ => do d ← replaceUnsafeM d; b ← replaceUnsafeM b; cache i e $ e.updateLambdaE! d b | Expr.mdata _ b _ => do b ← replaceUnsafeM b; cache i e $ e.updateMData! b | Expr.letE _ t v b _ => do t ← replaceUnsafeM t; v ← replaceUnsafeM v; b ← replaceUnsafeM b; cache i e $ e.updateLet! t v b | Expr.app f a _ => do f ← replaceUnsafeM f; a ← replaceUnsafeM a; cache i e $ e.updateApp! f a | Expr.proj _ _ b _ => do b ← replaceUnsafeM b; cache i e $ e.updateProj! b | Expr.sort u _ => cache i e $ e.updateSort! (u.replace f?) | Expr.const n us _ => cache i e $ e.updateConst! (us.map (Level.replace f?)) | Expr.localE _ _ _ _ => unreachable! | e => pure e unsafe def initCache : State := { keys := mkArray cacheSize.toNat (cast lcProof ()), -- `()` is not a valid `Expr` results := mkArray cacheSize.toNat (arbitrary _) } @[inline] unsafe def replaceUnsafe (f? : Level β†’ Option Level) (e : Expr) : Expr := (replaceUnsafeM f? cacheSize e).run' initCache end ReplaceLevelImpl @[implementedBy ReplaceLevelImpl.replaceUnsafe] partial def replaceLevel (f? : Level β†’ Option Level) : Expr β†’ Expr | e@(Expr.forallE _ d b _) => let d := replaceLevel d; let b := replaceLevel b; e.updateForallE! d b | e@(Expr.lam _ d b _) => let d := replaceLevel d; let b := replaceLevel b; e.updateLambdaE! d b | e@(Expr.mdata _ b _) => let b := replaceLevel b; e.updateMData! b | e@(Expr.letE _ t v b _) => let t := replaceLevel t; let v := replaceLevel v; let b := replaceLevel b; e.updateLet! t v b | e@(Expr.app f a _) => let f := replaceLevel f; let a := replaceLevel a; e.updateApp! f a | e@(Expr.proj _ _ b _) => let b := replaceLevel b; e.updateProj! b | e@(Expr.sort u _) => e.updateSort! (u.replace f?) | e@(Expr.const n us _) => e.updateConst! (us.map (Level.replace f?)) | e => e end Expr end Lean
53a25e1ce59e42ab376504f5369e40409a8bb6aa
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/cubical/squareover.hlean
41b51adc13d85c19584068a37cec5c7ab8cd10d9
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,881
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Squareovers -/ import .square open eq equiv is_equiv equiv.ops namespace eq -- we give the argument B explicitly, because Lean would find (Ξ»a, B a) by itself, which -- makes the type uglier (of course the two terms are definitionally equal) inductive squareover {A : Type} (B : A β†’ Type) {aβ‚€β‚€ : A} {bβ‚€β‚€ : B aβ‚€β‚€} : Ξ {aβ‚‚β‚€ aβ‚€β‚‚ aβ‚‚β‚‚ : A} {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} {p₁₂ : aβ‚€β‚‚ = aβ‚‚β‚‚} {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} {p₂₁ : aβ‚‚β‚€ = aβ‚‚β‚‚} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) {bβ‚‚β‚€ : B aβ‚‚β‚€} {bβ‚€β‚‚ : B aβ‚€β‚‚} {bβ‚‚β‚‚ : B aβ‚‚β‚‚} (q₁₀ : pathover B bβ‚€β‚€ p₁₀ bβ‚‚β‚€) (q₁₂ : pathover B bβ‚€β‚‚ p₁₂ bβ‚‚β‚‚) (q₀₁ : pathover B bβ‚€β‚€ p₀₁ bβ‚€β‚‚) (q₂₁ : pathover B bβ‚‚β‚€ p₂₁ bβ‚‚β‚‚), Type := idsquareo : squareover B ids idpo idpo idpo idpo variables {A A' : Type} {B : A β†’ Type} {a a' a'' aβ‚€β‚€ aβ‚‚β‚€ aβ‚„β‚€ aβ‚€β‚‚ aβ‚‚β‚‚ aβ‚‚β‚„ aβ‚€β‚„ aβ‚„β‚‚ aβ‚„β‚„ : A} /-aβ‚€β‚€-/ {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} /-aβ‚‚β‚€-/ {p₃₀ : aβ‚‚β‚€ = aβ‚„β‚€} /-aβ‚„β‚€-/ {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} /-s₁₁-/ {p₂₁ : aβ‚‚β‚€ = aβ‚‚β‚‚} /-s₃₁-/ {p₄₁ : aβ‚„β‚€ = aβ‚„β‚‚} /-aβ‚€β‚‚-/ {p₁₂ : aβ‚€β‚‚ = aβ‚‚β‚‚} /-aβ‚‚β‚‚-/ {p₃₂ : aβ‚‚β‚‚ = aβ‚„β‚‚} /-aβ‚„β‚‚-/ {p₀₃ : aβ‚€β‚‚ = aβ‚€β‚„} /-s₁₃-/ {p₂₃ : aβ‚‚β‚‚ = aβ‚‚β‚„} /-s₃₃-/ {p₄₃ : aβ‚„β‚‚ = aβ‚„β‚„} /-aβ‚€β‚„-/ {p₁₄ : aβ‚€β‚„ = aβ‚‚β‚„} /-aβ‚‚β‚„-/ {p₃₄ : aβ‚‚β‚„ = aβ‚„β‚„} /-aβ‚„β‚„-/ {s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁} {s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁} {s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃} {s₃₃ : square p₃₂ p₃₄ p₂₃ p₄₃} {bβ‚€β‚€ : B aβ‚€β‚€} {bβ‚‚β‚€ : B aβ‚‚β‚€} {bβ‚„β‚€ : B aβ‚„β‚€} {bβ‚€β‚‚ : B aβ‚€β‚‚} {bβ‚‚β‚‚ : B aβ‚‚β‚‚} {bβ‚„β‚‚ : B aβ‚„β‚‚} {bβ‚€β‚„ : B aβ‚€β‚„} {bβ‚‚β‚„ : B aβ‚‚β‚„} {bβ‚„β‚„ : B aβ‚„β‚„} /-bβ‚€β‚€-/ {q₁₀ : bβ‚€β‚€ =[p₁₀] bβ‚‚β‚€} /-bβ‚‚β‚€-/ {q₃₀ : bβ‚‚β‚€ =[p₃₀] bβ‚„β‚€} /-bβ‚„β‚€-/ {q₀₁ : bβ‚€β‚€ =[p₀₁] bβ‚€β‚‚} /-t₁₁-/ {q₂₁ : bβ‚‚β‚€ =[p₂₁] bβ‚‚β‚‚} /-t₃₁-/ {q₄₁ : bβ‚„β‚€ =[p₄₁] bβ‚„β‚‚} /-bβ‚€β‚‚-/ {q₁₂ : bβ‚€β‚‚ =[p₁₂] bβ‚‚β‚‚} /-bβ‚‚β‚‚-/ {q₃₂ : bβ‚‚β‚‚ =[p₃₂] bβ‚„β‚‚} /-bβ‚„β‚‚-/ {q₀₃ : bβ‚€β‚‚ =[p₀₃] bβ‚€β‚„} /-t₁₃-/ {q₂₃ : bβ‚‚β‚‚ =[p₂₃] bβ‚‚β‚„} /-t₃₃-/ {q₄₃ : bβ‚„β‚‚ =[p₄₃] bβ‚„β‚„} /-bβ‚€β‚„-/ {q₁₄ : bβ‚€β‚„ =[p₁₄] bβ‚‚β‚„} /-bβ‚‚β‚„-/ {q₃₄ : bβ‚‚β‚„ =[p₃₄] bβ‚„β‚„} /-bβ‚„β‚„-/ definition squareo := @squareover A B aβ‚€β‚€ definition idsquareo [reducible] [constructor] (bβ‚€β‚€ : B aβ‚€β‚€) := @squareover.idsquareo A B aβ‚€β‚€ bβ‚€β‚€ definition idso [reducible] [constructor] := @squareover.idsquareo A B aβ‚€β‚€ bβ‚€β‚€ definition apds (f : Ξ a, B a) (s : square p₁₀ p₁₂ p₀₁ p₂₁) : squareover B s (apdo f p₁₀) (apdo f p₁₂) (apdo f p₀₁) (apdo f p₂₁) := square.rec_on s idso definition vrflo : squareover B vrfl q₁₀ q₁₀ idpo idpo := by induction q₁₀; exact idso definition hrflo : squareover B hrfl idpo idpo q₁₀ q₁₀ := by induction q₁₀; exact idso definition vdeg_squareover {q₁₀' : bβ‚€β‚€ =[p₁₀] bβ‚‚β‚€} (r : q₁₀ = q₁₀') : squareover B vrfl q₁₀ q₁₀' idpo idpo := by induction r; exact vrflo definition hdeg_squareover {q₀₁' : bβ‚€β‚€ =[p₀₁] bβ‚€β‚‚} (r : q₀₁ = q₀₁') : squareover B hrfl idpo idpo q₀₁ q₀₁' := by induction r; exact hrflo definition hconcato (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (t₃₁ : squareover B s₃₁ q₃₀ q₃₂ q₂₁ q₄₁) : squareover B (hconcat s₁₁ s₃₁) (q₁₀ ⬝o q₃₀) (q₁₂ ⬝o q₃₂) q₀₁ q₄₁ := by induction t₃₁; exact t₁₁ definition vconcato (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (t₁₃ : squareover B s₁₃ q₁₂ q₁₄ q₀₃ q₂₃) : squareover B (vconcat s₁₁ s₁₃) q₁₀ q₁₄ (q₀₁ ⬝o q₀₃) (q₂₁ ⬝o q₂₃) := by induction t₁₃; exact t₁₁ definition hinverseo (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B (hinverse s₁₁) q₁₀⁻¹ᡒ q₁₂⁻¹ᡒ q₂₁ q₀₁ := by induction t₁₁; constructor definition vinverseo (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B (vinverse s₁₁) q₁₂ q₁₀ q₀₁⁻¹ᡒ q₂₁⁻¹ᡒ := by induction t₁₁; constructor definition eq_vconcato {q : bβ‚€β‚€ =[p₁₀] bβ‚‚β‚€} (r : q = q₁₀) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B s₁₁ q q₁₂ q₀₁ q₂₁ := by induction r; exact t₁₁ definition vconcato_eq {q : bβ‚€β‚‚ =[p₁₂] bβ‚‚β‚‚} (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : q₁₂ = q) : squareover B s₁₁ q₁₀ q q₀₁ q₂₁ := by induction r; exact t₁₁ definition eq_hconcato {q : bβ‚€β‚€ =[p₀₁] bβ‚€β‚‚} (r : q = q₀₁) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B s₁₁ q₁₀ q₁₂ q q₂₁ := by induction r; exact t₁₁ definition hconcato_eq {q : bβ‚‚β‚€ =[p₂₁] bβ‚‚β‚‚} (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : q₂₁ = q) : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q := by induction r; exact t₁₁ definition pathover_vconcato {p : aβ‚€β‚€ = aβ‚‚β‚€} {sp : p = p₁₀} {q : bβ‚€β‚€ =[p] bβ‚‚β‚€} (r : change_path sp q = q₁₀) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B (sp ⬝pv s₁₁) q q₁₂ q₀₁ q₂₁ := by induction sp; induction r; exact t₁₁ definition vconcato_pathover {p : aβ‚€β‚‚ = aβ‚‚β‚‚} {sp : p₁₂ = p} {q : bβ‚€β‚‚ =[p] bβ‚‚β‚‚} (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : change_path sp q₁₂ = q) : squareover B (s₁₁ ⬝vp sp) q₁₀ q q₀₁ q₂₁ := by induction sp; induction r; exact t₁₁ definition pathover_hconcato {p : aβ‚€β‚€ = aβ‚€β‚‚} {sp : p = p₀₁} {q : bβ‚€β‚€ =[p] bβ‚€β‚‚} (r : change_path sp q = q₀₁) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B (sp ⬝ph s₁₁) q₁₀ q₁₂ q q₂₁ := by induction sp; induction r; exact t₁₁ definition hconcato_pathover {p : aβ‚‚β‚€ = aβ‚‚β‚‚} {sp : p₂₁ = p} {q : bβ‚‚β‚€ =[p] bβ‚‚β‚‚} (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : change_path sp q₂₁ = q) : squareover B (s₁₁ ⬝hp sp) q₁₀ q₁₂ q₀₁ q := by induction sp; induction r; exact t₁₁ -- relating squareovers to squares definition square_of_squareover (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : square (!con_tr ⬝ ap (Ξ»a, p₂₁ β–Έ a) (tr_eq_of_pathover q₁₀)) (tr_eq_of_pathover q₁₂) (ap (Ξ»q, q β–Έ bβ‚€β‚€) (eq_of_square s₁₁) ⬝ !con_tr ⬝ ap (Ξ»a, p₁₂ β–Έ a) (tr_eq_of_pathover q₀₁)) (tr_eq_of_pathover q₂₁) := by induction t₁₁; esimp; constructor /- definition squareover_of_square (q : square (!con_tr ⬝ ap (Ξ»a, p₂₁ β–Έ a) (tr_eq_of_pathover q₁₀)) (tr_eq_of_pathover q₁₂) (ap (Ξ»q, q β–Έ bβ‚€β‚€) (eq_of_square s₁₁) ⬝ !con_tr ⬝ ap (Ξ»a, p₁₂ β–Έ a) (tr_eq_of_pathover q₀₁)) (tr_eq_of_pathover q₂₁)) : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ := sorry -/ definition square_of_squareover_ids {bβ‚€β‚€ bβ‚€β‚‚ bβ‚‚β‚€ bβ‚‚β‚‚ : B a} (t : bβ‚€β‚€ = bβ‚‚β‚€) (b : bβ‚€β‚‚ = bβ‚‚β‚‚) (l : bβ‚€β‚€ = bβ‚€β‚‚) (r : bβ‚‚β‚€ = bβ‚‚β‚‚) (so : squareover B ids (pathover_idp_of_eq t) (pathover_idp_of_eq b) (pathover_idp_of_eq l) (pathover_idp_of_eq r)) : square t b l r := begin let H := square_of_squareover so, -- use apply ... in rewrite [β–Έ* at H,+idp_con at H,+ap_id at H,↑pathover_idp_of_eq at H], rewrite [+to_right_inv !(pathover_equiv_tr_eq (refl a)) at H], exact H end definition squareover_ids_of_square {bβ‚€β‚€ bβ‚€β‚‚ bβ‚‚β‚€ bβ‚‚β‚‚ : B a} (t : bβ‚€β‚€ = bβ‚‚β‚€) (b : bβ‚€β‚‚ = bβ‚‚β‚‚) (l : bβ‚€β‚€ = bβ‚€β‚‚) (r : bβ‚‚β‚€ = bβ‚‚β‚‚) (q : square t b l r) : squareover B ids (pathover_idp_of_eq t) (pathover_idp_of_eq b) (pathover_idp_of_eq l) (pathover_idp_of_eq r) := square.rec_on q idso -- relating pathovers to squareovers definition pathover_of_squareover' (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : q₁₀ ⬝o q₂₁ =[eq_of_square s₁₁] q₀₁ ⬝o q₁₂ := by induction t₁₁; constructor definition pathover_of_squareover {s : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂} (t₁₁ : squareover B (square_of_eq s) q₁₀ q₁₂ q₀₁ q₂₁) : q₁₀ ⬝o q₂₁ =[s] q₀₁ ⬝o q₁₂ := begin revert s t₁₁, refine equiv_rect' !square_equiv_eq⁻¹ᡉ (Ξ»a b, squareover B b _ _ _ _ β†’ _) _, intro s, exact pathover_of_squareover' end definition squareover_of_pathover {s : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂} (r : q₁₀ ⬝o q₂₁ =[s] q₀₁ ⬝o q₁₂) : squareover B (square_of_eq s) q₁₀ q₁₂ q₀₁ q₂₁ := by induction q₁₂; esimp [concato] at r;induction r;induction q₂₁;induction q₁₀;constructor definition pathover_top_of_squareover (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : q₁₀ =[eq_top_of_square s₁₁] q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᡒ := by induction t₁₁; constructor definition squareover_of_pathover_top {s : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹} (r : q₁₀ =[s] q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᡒ) : squareover B (square_of_eq_top s) q₁₀ q₁₂ q₀₁ q₂₁ := by induction q₂₁; induction q₁₂; esimp at r;induction r;induction q₁₀;constructor definition pathover_of_hdeg_squareover {p₀₁' : aβ‚€β‚€ = aβ‚€β‚‚} {r : p₀₁ = p₀₁'} {q₀₁' : bβ‚€β‚€ =[p₀₁'] bβ‚€β‚‚} (t : squareover B (hdeg_square r) idpo idpo q₀₁ q₀₁') : q₀₁ =[r] q₀₁' := by induction r; induction q₀₁'; exact (pathover_of_squareover' t)⁻¹ᡒ definition pathover_of_vdeg_squareover {p₁₀' : aβ‚€β‚€ = aβ‚‚β‚€} {r : p₁₀ = p₁₀'} {q₁₀' : bβ‚€β‚€ =[p₁₀'] bβ‚‚β‚€} (t : squareover B (vdeg_square r) q₁₀ q₁₀' idpo idpo) : q₁₀ =[r] q₁₀' := by induction r; induction q₁₀'; exact pathover_of_squareover' t definition squareover_of_eq_top (r : change_path (eq_top_of_square s₁₁) q₁₀ = q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᡒ) : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ := begin induction s₁₁, revert q₁₂ q₁₀ r, eapply idp_rec_on q₂₁, clear q₂₁, intro q₁₂, eapply idp_rec_on q₁₂, clear q₁₂, esimp, intros, induction r, eapply idp_rec_on q₁₀, constructor end definition eq_top_of_squareover (r : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : change_path (eq_top_of_square s₁₁) q₁₀ = q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᡒ := by induction r; reflexivity /- definition squareover_equiv_pathover (q₁₀ : bβ‚€β‚€ =[p₁₀] bβ‚‚β‚€) (q₁₂ : bβ‚€β‚‚ =[p₁₂] bβ‚‚β‚‚) (q₀₁ : bβ‚€β‚€ =[p₀₁] bβ‚€β‚‚) (q₂₁ : bβ‚‚β‚€ =[p₂₁] bβ‚‚β‚‚) : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ ≃ q₁₀ ⬝o q₂₁ =[eq_of_square s₁₁] q₀₁ ⬝o q₁₂ := begin fapply equiv.MK, { exact pathover_of_squareover}, { intro r, rewrite [-to_left_inv !square_equiv_eq s₁₁], apply squareover_of_pathover, exact r}, { intro r, }, --need characterization of squareover lying over ids. { intro s, induction s, apply idp}, end -/ definition eq_of_vdeg_squareover {q₁₀' : bβ‚€β‚€ =[p₁₀] bβ‚‚β‚€} (p : squareover B vrfl q₁₀ q₁₀' idpo idpo) : q₁₀ = q₁₀' := begin let H := square_of_squareover p, -- use apply ... in induction p₁₀, -- if needed we can remove this induction and use con_tr_idp in types/eq2 rewrite [β–Έ* at H,idp_con at H,+ap_id at H], let H' := eq_of_vdeg_square H, exact eq_of_fn_eq_fn !pathover_equiv_tr_eq H' end -- definition vdeg_tr_squareover {q₁₂ : p₀₁ β–Έ bβ‚€β‚€ =[p₁₂] p₂₁ β–Έ bβ‚‚β‚€} (r : q₁₀ =[_] q₁₂) -- : squareover B s₁₁ q₁₀ q₁₂ !pathover_tr !pathover_tr := -- by induction p;exact vrflo /- charcaterization of pathovers in pathovers -/ -- in this version the fibration (B) of the pathover does not depend on the variable a definition pathover_pathover {a' aβ‚‚' : A'} {p : a' = aβ‚‚'} {f g : A' β†’ A} {b : Ξ a, B (f a)} {bβ‚‚ : Ξ a, B (g a)} {q : Ξ (a' : A'), f a' = g a'} (r : pathover B (b a') (q a') (bβ‚‚ a')) (rβ‚‚ : pathover B (b aβ‚‚') (q aβ‚‚') (bβ‚‚ aβ‚‚')) (s : squareover B (natural_square_tr q p) r rβ‚‚ (pathover_ap B f (apdo b p)) (pathover_ap B g (apdo bβ‚‚ p))) : pathover (Ξ»a, pathover B (b a) (q a) (bβ‚‚ a)) r p rβ‚‚ := begin induction p, esimp at s, apply pathover_idp_of_eq, apply eq_of_vdeg_squareover, exact s end end eq