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
6ff3634f68b39ae54b0ebf4754d91b2beea5f8bd
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Compiler/IR/FreeVars.lean
8a1aae2c6438a47ea32429c819f6b0680d7d413c
[ "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
9,609
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.Compiler.IR.Basic namespace Lean.IR namespace MaxIndex /-! Compute the maximum index `M` used in a declaration. We `M` to initialize the fresh index generator used to create fresh variable and join point names. Recall that we variable and join points share the same namespace in our implementation. -/ abbrev Collector := Index → Index @[inline] private def skip : Collector := id @[inline] private def collect (x : Index) : Collector := fun y => if x > y then x else y @[inline] private def collectVar (x : VarId) : Collector := collect x.idx @[inline] private def collectJP (j : JoinPointId) : Collector := collect j.idx @[inline] private def seq (k₁ k₂ : Collector) : Collector := k₂ ∘ k₁ instance : AndThen Collector where andThen a b := seq a (b ()) private def collectArg : Arg → Collector | Arg.var x => collectVar x | _ => skip private def collectArray {α : Type} (as : Array α) (f : α → Collector) : Collector := fun m => as.foldl (fun m a => f a m) m private def collectArgs (as : Array Arg) : Collector := collectArray as collectArg private def collectParam (p : Param) : Collector := collectVar p.x private def collectParams (ps : Array Param) : Collector := collectArray ps collectParam private def collectExpr : Expr → Collector | Expr.ctor _ ys => collectArgs ys | Expr.reset _ x => collectVar x | Expr.reuse x _ _ ys => collectVar x >> collectArgs ys | Expr.proj _ x => collectVar x | Expr.uproj _ x => collectVar x | Expr.sproj _ _ x => collectVar x | Expr.fap _ ys => collectArgs ys | Expr.pap _ ys => collectArgs ys | Expr.ap x ys => collectVar x >> collectArgs ys | Expr.box _ x => collectVar x | Expr.unbox x => collectVar x | Expr.lit _ => skip | Expr.isShared x => collectVar x | Expr.isTaggedPtr x => collectVar x private def collectAlts (f : FnBody → Collector) (alts : Array Alt) : Collector := collectArray alts fun alt => f alt.body partial def collectFnBody : FnBody → Collector | FnBody.vdecl x _ v b => collectVar x >> collectExpr v >> collectFnBody b | FnBody.jdecl j ys v b => collectJP j >> collectFnBody v >> collectParams ys >> collectFnBody b | FnBody.set x _ y b => collectVar x >> collectArg y >> collectFnBody b | FnBody.uset x _ y b => collectVar x >> collectVar y >> collectFnBody b | FnBody.sset x _ _ y _ b => collectVar x >> collectVar y >> collectFnBody b | FnBody.setTag x _ b => collectVar x >> collectFnBody b | FnBody.inc x _ _ _ b => collectVar x >> collectFnBody b | FnBody.dec x _ _ _ b => collectVar x >> collectFnBody b | FnBody.del x b => collectVar x >> collectFnBody b | FnBody.mdata _ b => collectFnBody b | FnBody.case _ x _ alts => collectVar x >> collectAlts collectFnBody alts | FnBody.jmp j ys => collectJP j >> collectArgs ys | FnBody.ret x => collectArg x | FnBody.unreachable => skip partial def collectDecl : Decl → Collector | .fdecl (xs := xs) (body := b) .. => collectParams xs >> collectFnBody b | .extern (xs := xs) .. => collectParams xs end MaxIndex def FnBody.maxIndex (b : FnBody) : Index := MaxIndex.collectFnBody b 0 def Decl.maxIndex (d : Decl) : Index := MaxIndex.collectDecl d 0 namespace FreeIndices /-! We say a variable (join point) index (aka name) is free in a function body if there isn't a `FnBody.vdecl` (`Fnbody.jdecl`) binding it. -/ abbrev Collector := IndexSet → IndexSet → IndexSet @[inline] private def skip : Collector := fun _ fv => fv @[inline] private def collectIndex (x : Index) : Collector := fun bv fv => if bv.contains x then fv else fv.insert x @[inline] private def collectVar (x : VarId) : Collector := collectIndex x.idx @[inline] private def collectJP (x : JoinPointId) : Collector := collectIndex x.idx @[inline] private def withIndex (x : Index) : Collector → Collector := fun k bv fv => k (bv.insert x) fv @[inline] private def withVar (x : VarId) : Collector → Collector := withIndex x.idx @[inline] private def withJP (x : JoinPointId) : Collector → Collector := withIndex x.idx def insertParams (s : IndexSet) (ys : Array Param) : IndexSet := ys.foldl (init := s) fun s p => s.insert p.x.idx @[inline] private def withParams (ys : Array Param) : Collector → Collector := fun k bv fv => k (insertParams bv ys) fv @[inline] private def seq : Collector → Collector → Collector := fun k₁ k₂ bv fv => k₂ bv (k₁ bv fv) instance : AndThen Collector where andThen a b := seq a (b ()) private def collectArg : Arg → Collector | Arg.var x => collectVar x | _ => skip private def collectArray {α : Type} (as : Array α) (f : α → Collector) : Collector := fun bv fv => as.foldl (fun fv a => f a bv fv) fv private def collectArgs (as : Array Arg) : Collector := collectArray as collectArg private def collectExpr : Expr → Collector | Expr.ctor _ ys => collectArgs ys | Expr.reset _ x => collectVar x | Expr.reuse x _ _ ys => collectVar x >> collectArgs ys | Expr.proj _ x => collectVar x | Expr.uproj _ x => collectVar x | Expr.sproj _ _ x => collectVar x | Expr.fap _ ys => collectArgs ys | Expr.pap _ ys => collectArgs ys | Expr.ap x ys => collectVar x >> collectArgs ys | Expr.box _ x => collectVar x | Expr.unbox x => collectVar x | Expr.lit _ => skip | Expr.isShared x => collectVar x | Expr.isTaggedPtr x => collectVar x private def collectAlts (f : FnBody → Collector) (alts : Array Alt) : Collector := collectArray alts fun alt => f alt.body partial def collectFnBody : FnBody → Collector | FnBody.vdecl x _ v b => collectExpr v >> withVar x (collectFnBody b) | FnBody.jdecl j ys v b => withParams ys (collectFnBody v) >> withJP j (collectFnBody b) | FnBody.set x _ y b => collectVar x >> collectArg y >> collectFnBody b | FnBody.uset x _ y b => collectVar x >> collectVar y >> collectFnBody b | FnBody.sset x _ _ y _ b => collectVar x >> collectVar y >> collectFnBody b | FnBody.setTag x _ b => collectVar x >> collectFnBody b | FnBody.inc x _ _ _ b => collectVar x >> collectFnBody b | FnBody.dec x _ _ _ b => collectVar x >> collectFnBody b | FnBody.del x b => collectVar x >> collectFnBody b | FnBody.mdata _ b => collectFnBody b | FnBody.case _ x _ alts => collectVar x >> collectAlts collectFnBody alts | FnBody.jmp j ys => collectJP j >> collectArgs ys | FnBody.ret x => collectArg x | FnBody.unreachable => skip end FreeIndices def FnBody.collectFreeIndices (b : FnBody) (vs : IndexSet) : IndexSet := FreeIndices.collectFnBody b {} vs def FnBody.freeIndices (b : FnBody) : IndexSet := b.collectFreeIndices {} namespace HasIndex /-! In principle, we can check whether a function body `b` contains an index `i` using `b.freeIndices.contains i`, but it is more efficient to avoid the construction of the set of freeIndices and just search whether `i` occurs in `b` or not. -/ def visitVar (w : Index) (x : VarId) : Bool := w == x.idx def visitJP (w : Index) (x : JoinPointId) : Bool := w == x.idx def visitArg (w : Index) : Arg → Bool | Arg.var x => visitVar w x | _ => false def visitArgs (w : Index) (xs : Array Arg) : Bool := xs.any (visitArg w) def visitParams (w : Index) (ps : Array Param) : Bool := ps.any (fun p => w == p.x.idx) def visitExpr (w : Index) : Expr → Bool | Expr.ctor _ ys => visitArgs w ys | Expr.reset _ x => visitVar w x | Expr.reuse x _ _ ys => visitVar w x || visitArgs w ys | Expr.proj _ x => visitVar w x | Expr.uproj _ x => visitVar w x | Expr.sproj _ _ x => visitVar w x | Expr.fap _ ys => visitArgs w ys | Expr.pap _ ys => visitArgs w ys | Expr.ap x ys => visitVar w x || visitArgs w ys | Expr.box _ x => visitVar w x | Expr.unbox x => visitVar w x | Expr.lit _ => false | Expr.isShared x => visitVar w x | Expr.isTaggedPtr x => visitVar w x partial def visitFnBody (w : Index) : FnBody → Bool | FnBody.vdecl _ _ v b => visitExpr w v || visitFnBody w b | FnBody.jdecl _ _ v b => visitFnBody w v || visitFnBody w b | FnBody.set x _ y b => visitVar w x || visitArg w y || visitFnBody w b | FnBody.uset x _ y b => visitVar w x || visitVar w y || visitFnBody w b | FnBody.sset x _ _ y _ b => visitVar w x || visitVar w y || visitFnBody w b | FnBody.setTag x _ b => visitVar w x || visitFnBody w b | FnBody.inc x _ _ _ b => visitVar w x || visitFnBody w b | FnBody.dec x _ _ _ b => visitVar w x || visitFnBody w b | FnBody.del x b => visitVar w x || visitFnBody w b | FnBody.mdata _ b => visitFnBody w b | FnBody.jmp j ys => visitJP w j || visitArgs w ys | FnBody.ret x => visitArg w x | FnBody.case _ x _ alts => visitVar w x || alts.any (fun alt => visitFnBody w alt.body) | FnBody.unreachable => false end HasIndex def Arg.hasFreeVar (arg : Arg) (x : VarId) : Bool := HasIndex.visitArg x.idx arg def Expr.hasFreeVar (e : Expr) (x : VarId) : Bool := HasIndex.visitExpr x.idx e def FnBody.hasFreeVar (b : FnBody) (x : VarId) : Bool := HasIndex.visitFnBody x.idx b end Lean.IR
1739340304054ff97143cf99b0f3df52649fb614
ac2987d8c7832fb4a87edb6bee26141facbb6fa0
/Mathlib/Util/Time.lean
f3713107d8ed76e7f842c73ca77209e82fa47474
[ "Apache-2.0" ]
permissive
AurelienSaue/mathlib4
52204b9bd9d207c922fe0cf3397166728bb6c2e2
84271fe0875bafdaa88ac41f1b5a7c18151bd0d5
refs/heads/master
1,689,156,096,545
1,629,378,840,000
1,629,378,840,000
389,648,603
0
0
Apache-2.0
1,627,307,284,000
1,627,307,284,000
null
UTF-8
Lean
false
false
680
lean
/- Copyright (c) 2021 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Lean section open Lean Elab Command syntax (name := timeCmd) "#time " command : command /-- Time the elaboration of a command, and print the result (in milliseconds). Example usage: ``` set_option maxRecDepth 100000 in #time example : (List.range 500).length = 500 := rfl ``` -/ @[commandElab timeCmd] def timeCmdElab : CommandElab | `(#time%$tk $stx:command) => do let start ← IO.monoMsNow elabCommand stx logInfoAt tk m!"time: {(← IO.monoMsNow) - start}ms" | _ => throwUnsupportedSyntax end
19b4d768ab53bc9ef3ef79d65a95c0f7c34358f7
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/inequality/12.lean
51ee6a2cb4ff6bc3d109a000c8383eb9e923629a
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
172
lean
theorem le_of_succ_le_succ (a b : mynat) : succ a ≤ succ b → a ≤ b := begin intro h, cases h with n hn, rw succ_add at hn, use n, apply succ_inj, cc, end
d805267161ac29f5a231f3944df67e554788c10a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/subobject/types.lean
f53ee33ba12742776fc5c3e6a91181a8bcb35446
[ "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
2,361
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.subobject.well_powered import category_theory.types /-! # `Type u` is well-powered > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. By building a categorical equivalence `mono_over α ≌ set α` for any `α : Type u`, we deduce that `subobject α ≃o set α` and that `Type u` is well-powered. One would hope that for a particular concrete category `C` (`AddCommGroup`, etc) it's viable to prove `[well_powered C]` without explicitly aligning `subobject X` with the "hand-rolled" definition of subobjects. This may be possible using Lawvere theories, but it remains to be seen whether this just pushes lumps around in the carpet. -/ universes u open category_theory open category_theory.subobject open_locale category_theory.Type lemma subtype_val_mono {α : Type u} (s : set α) : mono ↾(subtype.val : s → α) := (mono_iff_injective _).mpr subtype.val_injective local attribute [instance] subtype_val_mono /-- The category of `mono_over α`, for `α : Type u`, is equivalent to the partial order `set α`. -/ @[simps] noncomputable def types.mono_over_equivalence_set (α : Type u) : mono_over α ≌ set α := { functor := { obj := λ f, set.range f.1.hom, map := λ f g t, hom_of_le begin rintro a ⟨x, rfl⟩, exact ⟨t.1 x, congr_fun t.w x⟩, end, }, inverse := { obj := λ s, mono_over.mk' (subtype.val : s → α), map := λ s t b, mono_over.hom_mk (λ w, ⟨w.1, set.mem_of_mem_of_subset w.2 b.le⟩) (by { ext, simp, }), }, unit_iso := nat_iso.of_components (λ f, mono_over.iso_mk (equiv.of_injective f.1.hom ((mono_iff_injective _).mp f.2)).to_iso (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, eq_to_iso subtype.range_val) (by tidy), } instance : well_powered (Type u) := well_powered_of_essentially_small_mono_over (λ α, essentially_small.mk' (types.mono_over_equivalence_set α)) /-- For `α : Type u`, `subobject α` is order isomorphic to `set α`. -/ noncomputable def types.subobject_equiv_set (α : Type u) : subobject α ≃o set α := (types.mono_over_equivalence_set α).thin_skeleton_order_iso
27ab099dc4382e73e985c00ed7c7db24ee4bdf5b
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/topology/uniform_space/cauchy.lean
87b61de2d0ed4b9deb1f22de69f691b4cc9229e5
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
22,635
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 Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ import topology.uniform_space.basic topology.bases data.set.intervals universes u v open filter topological_space lattice set classical open_locale classical variables {α : Type u} {β : Type v} [uniform_space α] open_locale uniformity topological_space /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := f ≠ ⊥ ∧ filter.prod f f ≤ (𝓤 α) /-- A set `s` is called *complete*, if any Cauchy filter `f` such that `s ∈ f` has a limit in `s` (formally, it satisfies `f ≤ 𝓝 x` for some `x ∈ s`). -/ def is_complete (s : set α) := ∀f, cauchy f → f ≤ principal s → ∃x∈s, f ≤ 𝓝 x lemma cauchy_iff {f : filter α} : cauchy f ↔ (f ≠ ⊥ ∧ (∀ s ∈ 𝓤 α, ∃t∈f.sets, set.prod t t ⊆ s)) := and_congr iff.rfl $ forall_congr $ assume s, forall_congr $ assume hs, mem_prod_same_iff lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (l ≠ ⊥ ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l.prod l) (𝓤 α)) := by rw [cauchy, (≠), map_eq_bot_iff, prod_map_map_eq]; refl lemma cauchy_downwards {f g : filter α} (h_c : cauchy f) (hg : g ≠ ⊥) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy_nhds {a : α} : cauchy (𝓝 a) := ⟨nhds_ne_bot, calc filter.prod (𝓝 a) (𝓝 a) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel s s) : le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ 𝓤 α : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_downwards cauchy_nhds pure_ne_bot (pure_le_nhds a) /-- The common part of the proofs of `le_nhds_of_cauchy_adhp` and `sequentially_complete.le_nhds_of_seq_tendsto_nhds`: if for any entourage `s` one can choose a set `t ∈ f` of diameter `s` such that it contains a point `y` with `(x, y) ∈ s`, then `f` converges to `x`. -/ lemma le_nhds_of_cauchy_adhp_aux {f : filter α} {x : α} (adhs : ∀ s ∈ 𝓤 α, ∃ t ∈ f, (set.prod t t ⊆ s) ∧ ∃ y, (y ∈ t) ∧ (x, y) ∈ s) : f ≤ 𝓝 x := begin -- Consider a neighborhood `s` of `x` assume s hs, -- Take an entourage twice smaller than `s` rcases comp_mem_uniformity_sets (mem_nhds_uniformity_iff.1 hs) with ⟨U, U_mem, hU⟩, -- Take a set `t ∈ f`, `t × t ⊆ U`, and a point `y ∈ t` such that `(x, y) ∈ U` rcases adhs U U_mem with ⟨t, t_mem, ht, y, hy, hxy⟩, apply mem_sets_of_superset t_mem, -- Given a point `z ∈ t`, we have `(x, y) ∈ U` and `(y, z) ∈ t × t ⊆ U`, hence `z ∈ s` exact (λ z hz, hU (prod_mk_mem_comp_rel hxy (ht $ mk_mem_prod hy hz)) rfl) end /-- If `x` is an adherent (cluster) point for a Cauchy filter `f`, then it is a limit point for `f`. -/ lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : f ⊓ 𝓝 x ≠ ⊥) : f ≤ 𝓝 x := le_nhds_of_cauchy_adhp_aux begin assume s hs, -- Take `t ∈ f` such that `t × t ⊆ s`. rcases (cauchy_iff.1 hf).2 s hs with ⟨t, t_mem, ht⟩, use [t, t_mem, ht], exact exists_mem_of_ne_empty (forall_sets_ne_empty_iff_ne_bot.2 adhs _ (inter_mem_inf_sets t_mem (mem_nhds_left x hs))) end lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ 𝓝 x ↔ f ⊓ 𝓝 x ≠ ⊥ := ⟨assume h, (inf_of_le_left h).symm ▸ hf.left, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy_map [uniform_space β] {f : filter α} {m : α → β} (hm : uniform_continuous m) (hf : cauchy f) : cauchy (map m f) := ⟨have f ≠ ⊥, from hf.left, by simp; assumption, calc filter.prod (map m f) (map m f) = map (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy_comap [uniform_space β] {f : filter β} {m : α → β} (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hf : cauchy f) (hb : comap m f ≠ ⊥) : cauchy (comap m f) := ⟨hb, calc filter.prod (comap m f) (comap m f) = comap (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq_of_tendsto_nhds [semilattice_sup β] [nonempty β] (f : β → α) {x} (hx : tendsto f at_top (𝓝 x)) : cauchy_seq f := cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hx lemma cauchy_seq_iff_prod_map [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ map (prod.map u u) at_top ≤ 𝓤 α := iff.trans (and_iff_right (map_ne_bot at_top_ne_bot)) (prod_map_at_top_eq u u ▸ iff.rfl) lemma cauchy_seq_of_controlled [semilattice_sup β] [inhabited β] (U : β → set (α × α)) (hU : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) {f : β → α} (hf : ∀ {N m n : β}, N ≤ m → N ≤ n → (f m, f n) ∈ U N) : cauchy_seq f := cauchy_seq_iff_prod_map.2 begin assume s hs, rw [mem_map, mem_at_top_sets], cases hU s hs with N hN, refine ⟨(N, N), λ mn hmn, _⟩, cases mn with m n, exact hN (hf hmn.1 hmn.2) end /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ 𝓝 x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, by simp, hx⟩ end lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} : cauchy f → cauchy g → cauchy (filter.prod f g) | ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_ne_bot.2 ⟨f_proper, g_proper⟩, let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β, by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap_comp, (∘), lattice.inf_assoc, lattice.inf_comm, lattice.inf_left_comm], lattice.inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩ instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ cauchy_map uniform_continuous_fst hf in let ⟨x2, hx2⟩ := complete_space.complete $ cauchy_map uniform_continuous_snd hf in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem_sets H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} (hl : l ≠ ⊥) : cauchy l ↔ (∃x, l ≤ 𝓝 x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_downwards cauchy_nhds hl hx⟩ lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} (hl : l ≠ ⊥) : cauchy (l.map f) ↔ (∃x, tendsto f l (𝓝 x)) := cauchy_iff_exists_le_nhds (map_ne_bot hl) /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (𝓝 x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (𝓝 v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_sets_iff.2 ⟨univ, univ_mem_sets, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem le_nhds_lim_of_cauchy {α} [uniform_space α] [complete_space α] [inhabited α] {f : filter α} (hf : cauchy f) : f ≤ 𝓝 (lim f) := lim_spec (complete_space.complete hf) lemma is_complete_of_is_closed [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_nhds.mp h x (ne_bot_of_le_ne_bot cf.left (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := finite_subset fk (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ lemma totally_bounded_closure {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : closure_eq_of_is_closed $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ lemma totally_bounded_image [uniform_space β] {f : α → β} {s : set α} (hf : uniform_continuous f) (hs : totally_bounded s) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, finite_image f hfc, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp [-mem_image], exact let ⟨i, hi, ht⟩ := hct x hx in ⟨f i, mem_image_of_mem f hi, ht⟩ end⟩ lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α} (hs : totally_bounded s) (hf : is_ultrafilter f) (h : f ≤ principal s) : cauchy f := ⟨hf.left, assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets, from mem_sets_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets, from mem_of_finite_Union_ultrafilter hf hi this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, (filter.prod f f).sets_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c) := ⟨assume : totally_bounded s, assume f hf hs, ⟨ultrafilter_of f, ultrafilter_of_le, cauchy_of_totally_bounded_of_ultrafilter this (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hs)⟩, assume h : ∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c, assume d hd, classical.by_contradiction $ assume hs, have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}), by simpa using hs, let f := ⨅t:{t : set α // finite t}, principal (s \ (⋃y∈t.val, {x | (x,y) ∈ d})), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h, hd_cover finite_empty $ h.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_ne_bot_of_directed ⟨a⟩ (assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, finite_union ht₁ ht₂⟩, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inl, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inr⟩) (assume ⟨t, ht⟩, by simp [diff_eq_empty]; exact hd_cover ht), have f ≤ principal s, from infi_le_of_le ⟨∅, finite_empty⟩ $ by simp; exact subset.refl s, let ⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this, ⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α c d).mp $ hc₂.right hd in have c ≤ principal s, from le_trans ‹c ≤ f› this, have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this, let ⟨y, hym, hys⟩ := inhabited_of_mem_sets hc₂.left this in let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in have m ⊆ ys, from assume y' hy', show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}), by simp; exact @hmd (y', y) ⟨hy', hym⟩, have c ≤ principal (s - ys), from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_singleton _⟩ $ le_refl _, have (s - ys) ∩ (m ∩ s) ∈ c.sets, from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›, have ∅ ∈ c.sets, from c.sets_of_superset this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm, hc₂.left $ empty_in_sets_eq_bot.mp this⟩ lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f, is_ultrafilter f → f ≤ principal s → cauchy f) := ⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs, assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs, have cauchy (ultrafilter_of f), from h (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs), ⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩ lemma compact_iff_totally_bounded_complete {s : set α} : compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf1 hf2, let ⟨x, xs, fx⟩ := compact_iff_ultrafilter_le_nhds.1 hs f hf1 hf2 in cauchy_downwards (cauchy_nhds) (hf1.1) fx), λ f fc fs, let ⟨a, as, fa⟩ := hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, compact_iff_ultrafilter_le_nhds.2 (λf hf hfs, hc _ (totally_bounded_iff_ultrafilter.1 ht _ hf hfs) hfs)⟩ @[priority 100] -- see Note [lower instance priority] instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa [principal_univ] using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, is_complete_of_is_closed hc⟩ /-! ### Sequentially complete space In this section we prove that a uniform space is complete provided that it is sequentially complete (i.e., any Cauchy sequence converges) and its uniformity filter admits a countable generating set. In particular, this applies to (e)metric spaces, see the files `topology/metric_space/emetric_space` and `topology/metric_space/basic`. More precisely, we assume that there is a sequence of entourages `U_n` such that any other entourage includes one of `U_n`. Then any Cauchy filter `f` generates a decreasing sequence of sets `s_n ∈ f` such that `s_n × s_n ⊆ U_n`. Choose a sequence `x_n∈s_n`. It is easy to show that this is a Cauchy sequence. If this sequence converges to some `a`, then `f ≤ 𝓝 a`. -/ namespace sequentially_complete variables {f : filter α} (hf : cauchy f) {U : ℕ → set (α × α)} (U_mem : ∀ n, U n ∈ 𝓤 α) (U_le : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) open set finset noncomputable theory /-- An auxiliary sequence of sets approximating a Cauchy filter. -/ def set_seq_aux (n : ℕ) : {s : set α // ∃ (_ : s ∈ f), s.prod s ⊆ U n } := indefinite_description _ $ (cauchy_iff.1 hf).2 (U n) (U_mem n) /-- Given a Cauchy filter `f` and a sequence `U` of entourages, `set_seq` provides a sequence of monotonically decreasing sets `s n ∈ f` such that `(s n).prod (s n) ⊆ U`. -/ def set_seq (n : ℕ) : set α := ⋂ m ∈ Iic n, (set_seq_aux hf U_mem m).val lemma set_seq_mem (n : ℕ) : set_seq hf U_mem n ∈ f := Inter_mem_sets (finite_le_nat n) (λ m _, (set_seq_aux hf U_mem m).2.fst) lemma set_seq_mono ⦃m n : ℕ⦄ (h : m ≤ n) : set_seq hf U_mem n ⊆ set_seq hf U_mem m := bInter_subset_bInter_left (λ k hk, le_trans hk h) lemma set_seq_sub_aux (n : ℕ) : set_seq hf U_mem n ⊆ set_seq_aux hf U_mem n := bInter_subset_of_mem right_mem_Iic lemma set_seq_prod_subset {N m n} (hm : N ≤ m) (hn : N ≤ n) : (set_seq hf U_mem m).prod (set_seq hf U_mem n) ⊆ U N := begin assume p hp, refine (set_seq_aux hf U_mem N).2.snd ⟨_, _⟩; apply set_seq_sub_aux, exact set_seq_mono hf U_mem hm hp.1, exact set_seq_mono hf U_mem hn hp.2 end /-- A sequence of points such that `seq n ∈ set_seq n`. Here `set_seq` is a monotonically decreasing sequence of sets `set_seq n ∈ f` with diameters controlled by a given sequence of entourages. -/ def seq (n : ℕ) : α := some $ inhabited_of_mem_sets hf.1 (set_seq_mem hf U_mem n) lemma seq_mem (n : ℕ) : seq hf U_mem n ∈ set_seq hf U_mem n := some_spec $ inhabited_of_mem_sets hf.1 (set_seq_mem hf U_mem n) lemma seq_pair_mem ⦃N m n : ℕ⦄ (hm : N ≤ m) (hn : N ≤ n) : (seq hf U_mem m, seq hf U_mem n) ∈ U N := set_seq_prod_subset hf U_mem hm hn ⟨seq_mem hf U_mem m, seq_mem hf U_mem n⟩ include U_le theorem seq_is_cauchy_seq : cauchy_seq $ seq hf U_mem := cauchy_seq_of_controlled U U_le $ seq_pair_mem hf U_mem /-- If the sequence `sequentially_complete.seq` converges to `a`, then `f ≤ 𝓝 a`. -/ theorem le_nhds_of_seq_tendsto_nhds ⦃a : α⦄ (ha : tendsto (seq hf U_mem) at_top (𝓝 a)) : f ≤ 𝓝 a := le_nhds_of_cauchy_adhp_aux begin assume s hs, rcases U_le s hs with ⟨m, hm⟩, rcases (tendsto_at_top' _ _).1 ha _ (mem_nhds_left a (U_mem m)) with ⟨n, hn⟩, refine ⟨set_seq hf U_mem (max m n), set_seq_mem hf U_mem _, _, seq hf U_mem (max m n), seq_mem hf U_mem _, _⟩, { have := le_max_left m n, exact set.subset.trans (set_seq_prod_subset hf U_mem this this) hm }, { exact hm (hn _ $ le_max_right m n) } end end sequentially_complete namespace uniform_space open sequentially_complete variables (H : has_countable_basis (𝓤 α)) include H /-- A uniform space is complete provided that (a) its uniformity filter has a countable basis; (b) any sequence satisfying a "controlled" version of the Cauchy condition converges. -/ theorem complete_of_convergent_controlled_sequences (U : ℕ → set (α × α)) (U_mem : ∀ n, U n ∈ 𝓤 α) (HU : ∀ u : ℕ → α, (∀ N m n, N ≤ m → N ≤ n → (u m, u n) ∈ U N) → ∃ a, tendsto u at_top (𝓝 a)) : complete_space α := begin rcases (𝓤 α).has_countable_basis_iff_mono_seq'.1 H with ⟨U', U'_mono, hU'⟩, have Hmem : ∀ n, U n ∩ U' n ∈ 𝓤 α, from λ n, inter_mem_sets (U_mem n) (hU'.2 ⟨n, subset.refl _⟩), refine ⟨λ f hf, (HU (seq hf Hmem) (λ N m n hm hn, _)).imp $ le_nhds_of_seq_tendsto_nhds _ _ (λ s hs, _)⟩, { rcases (hU'.1 hs) with ⟨N, hN⟩, exact ⟨N, subset.trans (inter_subset_right _ _) hN⟩ }, { exact inter_subset_left _ _ (seq_pair_mem hf Hmem hm hn) } end /-- A sequentially complete uniform space with a countable basis of the uniformity filter is complete. -/ theorem complete_of_cauchy_seq_tendsto (H' : ∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) : complete_space α := let ⟨U', U'_mono, hU'⟩ := (𝓤 α).has_countable_basis_iff_mono_seq'.1 H in complete_of_convergent_controlled_sequences H U' (λ n, hU'.2 ⟨n, subset.refl _⟩) (λ u hu, H' u $ cauchy_seq_of_controlled U' (λ s hs, hU'.1 hs) hu) protected lemma first_countable_topology : first_countable_topology α := ⟨λ a, by { rw nhds_eq_comap_uniformity, exact H.comap (prod.mk a) }⟩ end uniform_space
c7a8348dd73b4348ccfb2fddd0b036e398c98b71
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/int/modeq.lean
595e529b83fb5a6bafbc1f26b01ff4904578803a
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
6,017
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.int.basic data.nat.modeq namespace int def modeq (n a b : ℤ) := a % n = b % n notation a ` ≡ `:50 b ` [ZMOD `:50 n `]`:0 := modeq n a b namespace modeq variables {n m a b c d : ℤ} @[refl] protected theorem refl (a : ℤ) : a ≡ a [ZMOD n] := @rfl _ _ @[symm] protected theorem symm : a ≡ b [ZMOD n] → b ≡ a [ZMOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [ZMOD n] → b ≡ c [ZMOD n] → a ≡ c [ZMOD n] := eq.trans lemma coe_nat_modeq_iff {a b n : ℕ} : a ≡ b [ZMOD n] ↔ a ≡ b [MOD n] := by unfold modeq nat.modeq; rw ← int.coe_nat_eq_coe_nat_iff; simp [int.coe_nat_mod] instance : decidable (a ≡ b [ZMOD n]) := by unfold modeq; apply_instance theorem modeq_zero_iff : a ≡ 0 [ZMOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] theorem modeq_iff_dvd : a ≡ b [ZMOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm]; simp [int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero] theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [ZMOD n]) : a ≡ b [ZMOD m] := modeq_iff_dvd.2 $ dvd_trans d (modeq_iff_dvd.1 h) theorem modeq_mul_left' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD (c * n)] := or.cases_on (lt_or_eq_of_le hc) (λ hc, by unfold modeq; simp [mul_mod_mul_of_pos _ _ hc, (show _ = _, from h)] ) (λ hc, by simp [hc.symm]) theorem modeq_mul_right' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' hc h theorem modeq_add (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a + c ≡ b + d [ZMOD n] := modeq_iff_dvd.2 $ by simpa using dvd_add (modeq_iff_dvd.1 h₁) (modeq_iff_dvd.1 h₂) theorem modeq_add_cancel_left (h₁ : a ≡ b [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : c ≡ d [ZMOD n] := have (n:ℤ) ∣ a + (-a + (d + -c)), by simpa using dvd_sub (modeq_iff_dvd.1 h₂) (modeq_iff_dvd.1 h₁), modeq_iff_dvd.2 $ by rwa add_neg_cancel_left at this theorem modeq_add_cancel_right (h₁ : c ≡ d [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : a ≡ b [ZMOD n] := by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂ theorem mod_modeq (a n) : a % n ≡ a [ZMOD n] := int.mod_mod _ _ theorem modeq_neg (h : a ≡ b [ZMOD n]) : -a ≡ -b [ZMOD n] := modeq_add_cancel_left h (by simp) theorem modeq_sub (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a - c ≡ b - d [ZMOD n] := by rw [sub_eq_add_neg, sub_eq_add_neg]; exact modeq_add h₁ (modeq_neg h₂) theorem modeq_mul_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD n] := or.cases_on (le_total 0 c) (λ hc, modeq_of_dvd_of_modeq (dvd_mul_left _ _) (modeq_mul_left' hc h)) (λ hc, by rw [← neg_neg c, ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul _ b]; exact modeq_neg (modeq_of_dvd_of_modeq (dvd_mul_left _ _) (modeq_mul_left' (neg_nonneg.2 hc) h))) theorem modeq_mul_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD n] := by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h theorem modeq_mul (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a * c ≡ b * d [ZMOD n] := (modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁) theorem modeq_of_modeq_mul_left (m : ℤ) (h : a ≡ b [ZMOD m * n]) : a ≡ b [ZMOD n] := by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left n m) h theorem modeq_of_modeq_mul_right (m : ℤ) : a ≡ b [ZMOD n * m] → a ≡ b [ZMOD n] := mul_comm m n ▸ modeq_of_modeq_mul_left _ lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℤ} (hmn : nat.coprime m.nat_abs n.nat_abs) : a ≡ b [ZMOD m] ∧ a ≡ b [ZMOD n] ↔ (a ≡ b [ZMOD m * n]) := ⟨λ h, begin rw [int.modeq.modeq_iff_dvd, int.modeq.modeq_iff_dvd] at h, rw [int.modeq.modeq_iff_dvd, ← int.nat_abs_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul], refine hmn.mul_dvd_of_dvd_of_dvd _ _; rw [← int.coe_nat_dvd, int.nat_abs_dvd, int.dvd_nat_abs]; tauto end, λ h, ⟨int.modeq.modeq_of_modeq_mul_right _ h, int.modeq.modeq_of_modeq_mul_left _ h⟩⟩ lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * nat.gcd_a a b ≡ nat.gcd a b [ZMOD b] := by rw [← add_zero ((a : ℤ) * _), nat.gcd_eq_gcd_ab]; exact int.modeq.modeq_add rfl (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)).symm theorem modeq_add_fac {a b n : ℤ} (c : ℤ) (ha : a ≡ b [ZMOD n]) : a + n*c ≡ b [ZMOD n] := calc a + n*c ≡ b + n*c [ZMOD n] : int.modeq.modeq_add ha (int.modeq.refl _) ... ≡ b + 0 [ZMOD n] : int.modeq.modeq_add (int.modeq.refl _) (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)) ... ≡ b [ZMOD n] : by simp open nat lemma mod_coprime {a b : ℕ} (hab : coprime a b) : ∃ y : ℤ, a * y ≡ 1 [ZMOD b] := ⟨ gcd_a a b, have hgcd : nat.gcd a b = 1, from coprime.gcd_eq_one hab, calc ↑a * gcd_a a b ≡ ↑a*gcd_a a b + ↑b*gcd_b a b [ZMOD ↑b] : int.modeq.symm $ modeq_add_fac _ $ int.modeq.refl _ ... ≡ 1 [ZMOD ↑b] : by rw [←gcd_eq_gcd_ab, hgcd]; reflexivity ⟩ lemma exists_unique_equiv (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℤ, 0 ≤ z ∧ z < b ∧ z ≡ a [ZMOD b] := ⟨ a % b, int.mod_nonneg _ (ne_of_gt hb), have a % b < abs b, from int.mod_lt _ (ne_of_gt hb), by rwa abs_of_pos hb at this, by simp [int.modeq] ⟩ lemma exists_unique_equiv_nat (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℕ, ↑z < b ∧ ↑z ≡ a [ZMOD b] := let ⟨z, hz1, hz2, hz3⟩ := exists_unique_equiv a hb in ⟨z.nat_abs, by split; rw [←int.of_nat_eq_coe, int.of_nat_nat_abs_eq_of_nonneg hz1]; assumption⟩ end modeq @[simp] lemma mod_mul_right_mod (a b c : ℤ) : a % (b * c) % b = a % b := int.modeq.modeq_of_modeq_mul_right _ (int.modeq.mod_modeq _ _) @[simp] lemma mod_mul_left_mod (a b c : ℤ) : a % (b * c) % c = a % c := int.modeq.modeq_of_modeq_mul_left _ (int.modeq.mod_modeq _ _) end int
ff34685a66f81e358423b77e4178471a915b022e
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/nat/log.lean
57c08fad1e758b79d8d50f89aff01dd498e20154
[ "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
10,205
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Yaël Dillies -/ import data.nat.pow /-! # Natural number logarithms This file defines two `ℕ`-valued analogs of the logarithm of `n` with base `b`: * `log b n`: Lower logarithm, or floor **log**. Greatest `k` such that `b^k ≤ n`. * `clog b n`: Upper logarithm, or **c**eil **log**. Least `k` such that `n ≤ b^k`. These are interesting because, for `1 < b`, `nat.log b` and `nat.clog b` are respectively right and left adjoints of `nat.pow b`. See `pow_le_iff_le_log` and `le_pow_iff_clog_le`. -/ namespace nat /-! ### Floor logarithm -/ /-- `log b n`, is the logarithm of natural number `n` in base `b`. It returns the largest `k : ℕ` such that `b^k ≤ n`, so if `b^k = n`, it returns exactly `k`. -/ @[pp_nodot] def log (b : ℕ) : ℕ → ℕ | n := if h : b ≤ n ∧ 1 < b then have n / b < n, from div_lt_self ((zero_lt_one.trans h.2).trans_le h.1) h.2, log (n / b) + 1 else 0 lemma log_eq_zero {b n : ℕ} (hnb : n < b ∨ b ≤ 1) : log b n = 0 := begin rw [or_iff_not_and_not, not_lt, not_le] at hnb, rw [log, ←ite_not, if_pos hnb], end lemma log_of_one_lt_of_le {b n : ℕ} (h : 1 < b) (hn : b ≤ n) : log b n = log b (n / b) + 1 := begin rw log, exact if_pos ⟨hn, h⟩, end lemma log_of_lt {b n : ℕ} (hnb : n < b) : log b n = 0 := by rw [log, if_neg (λ h : b ≤ n ∧ 1 < b, h.1.not_lt hnb)] lemma log_of_left_le_one {b n : ℕ} (hb : b ≤ 1) : log b n = 0 := by rw [log, if_neg (λ h : b ≤ n ∧ 1 < b, h.2.not_le hb)] lemma log_eq_zero_iff {b n : ℕ} : log b n = 0 ↔ n < b ∨ b ≤ 1 := begin split, { intro h_log, by_contra h, push_neg at h, have := log_of_one_lt_of_le h.2 h.1, rw h_log at this, exact succ_ne_zero _ this.symm, }, { exact log_eq_zero, }, end lemma log_eq_one_iff {b n : ℕ} : log b n = 1 ↔ n < b * b ∧ 1 < b ∧ b ≤ n := -- This is best possible: if b = 2, n = 5, then 1 < b and b ≤ n but n > b * b. begin split, { intro h_log, have bound : 1 < b ∧ b ≤ n, { contrapose h_log, rw [not_and_distrib, not_lt, not_le, or_comm, ←log_eq_zero_iff] at h_log, rw h_log, exact nat.zero_ne_one, }, cases bound with one_lt_b b_le_n, refine ⟨_, one_lt_b, b_le_n⟩, rw [log_of_one_lt_of_le one_lt_b b_le_n, succ_inj', log_eq_zero_iff, nat.div_lt_iff_lt_mul _ _ (lt_trans zero_lt_one one_lt_b)] at h_log, exact h_log.resolve_right (λ b_small, lt_irrefl _ (lt_of_lt_of_le one_lt_b b_small)), }, { rintros ⟨h, one_lt_b, b_le_n⟩, rw [log_of_one_lt_of_le one_lt_b b_le_n, succ_inj', log_eq_zero_iff, nat.div_lt_iff_lt_mul _ _ (lt_trans zero_lt_one one_lt_b)], exact or.inl h, }, end @[simp] lemma log_zero_left (n : ℕ) : log 0 n = 0 := log_of_left_le_one zero_le_one @[simp] lemma log_zero_right (b : ℕ) : log b 0 = 0 := by { rw log, cases b; refl } @[simp] lemma log_one_left (n : ℕ) : log 1 n = 0 := log_of_left_le_one le_rfl @[simp] lemma log_one_right (b : ℕ) : log b 1 = 0 := if h : b ≤ 1 then log_of_left_le_one h else log_of_lt (not_le.mp h) /-- `pow b` and `log b` (almost) form a Galois connection. -/ lemma pow_le_iff_le_log {b : ℕ} (hb : 1 < b) {x y : ℕ} (hy : 0 < y) : b^x ≤ y ↔ x ≤ log b y := begin induction y using nat.strong_induction_on with y ih generalizing x, cases x, { exact iff_of_true hy (zero_le _) }, rw log, split_ifs, { have b_pos : 0 < b := zero_le_one.trans_lt hb, rw [succ_eq_add_one, add_le_add_iff_right, ←ih (y / b) (div_lt_self hy hb) (nat.div_pos h.1 b_pos), le_div_iff_mul_le _ _ b_pos, pow_succ'] }, { refine iff_of_false (λ hby, h ⟨le_trans _ hby, hb⟩) (not_succ_le_zero _), convert pow_mono hb.le (zero_lt_succ x), exact (pow_one b).symm } end lemma log_pow {b : ℕ} (hb : 1 < b) (x : ℕ) : log b (b ^ x) = x := eq_of_forall_le_iff $ λ z, by { rw ←pow_le_iff_le_log hb (pow_pos (zero_lt_one.trans hb) _), exact (pow_right_strict_mono hb).le_iff_le } lemma log_pos {b n : ℕ} (hb : 1 < b) (hn : b ≤ n) : 0 < log b n := by { rwa [←succ_le_iff, ←pow_le_iff_le_log hb (hb.le.trans hn), pow_one] } lemma lt_pow_succ_log_self {b : ℕ} (hb : 1 < b) {x : ℕ} (hx : 0 < x) : x < b ^ (log b x).succ := begin rw [←not_le, pow_le_iff_le_log hb hx, not_le], exact lt_succ_self _, end lemma pow_log_le_self {b : ℕ} (hb : 1 < b) {x : ℕ} (hx : 0 < x) : b ^ log b x ≤ x := (pow_le_iff_le_log hb hx).2 le_rfl lemma log_le_log_of_le {b n m : ℕ} (h : n ≤ m) : log b n ≤ log b m := begin cases le_or_lt b 1 with hb hb, { rw log_of_left_le_one hb, exact zero_le _ }, { cases nat.eq_zero_or_pos n with hn hn, { rw [hn, log_zero_right], exact zero_le _ }, { rw ←pow_le_iff_le_log hb (hn.trans_le h), exact (pow_log_le_self hb hn).trans h } } end lemma log_le_log_of_left_ge {b c n : ℕ} (hc : 1 < c) (hb : c ≤ b) : log b n ≤ log c n := begin cases n, { simp }, rw ← pow_le_iff_le_log hc (zero_lt_succ n), exact calc c ^ log b n.succ ≤ b ^ log b n.succ : pow_le_pow_of_le_left (le_of_lt $ zero_lt_one.trans hc) hb _ ... ≤ n.succ : pow_log_le_self (lt_of_lt_of_le hc hb) (zero_lt_succ n) end lemma log_monotone {b : ℕ} : monotone (λ n : ℕ, log b n) := λ x y, log_le_log_of_le lemma log_antitone_left {n : ℕ} : antitone_on (λ b, log b n) (set.Ioi 1) := λ _ hc _ _ hb, log_le_log_of_left_ge (set.mem_Iio.1 hc) hb private lemma add_pred_div_lt {b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : (n + b - 1)/b < n := begin rw [div_lt_iff_lt_mul _ _ (zero_lt_one.trans hb), ←succ_le_iff, ←pred_eq_sub_one, succ_pred_eq_of_pos (add_pos (zero_lt_one.trans hn) (zero_lt_one.trans hb))], exact add_le_mul hn hb, end /-! ### Ceil logarithm -/ /-- `clog b n`, is the upper logarithm of natural number `n` in base `b`. It returns the smallest `k : ℕ` such that `n ≤ b^k`, so if `b^k = n`, it returns exactly `k`. -/ @[pp_nodot] def clog (b : ℕ) : ℕ → ℕ | n := if h : 1 < b ∧ 1 < n then have (n + b - 1)/b < n := add_pred_div_lt h.1 h.2, clog ((n + b - 1)/b) + 1 else 0 lemma clog_of_left_le_one {b : ℕ} (hb : b ≤ 1) (n : ℕ) : clog b n = 0 := by rw [clog, if_neg (λ h : 1 < b ∧ 1 < n, h.1.not_le hb)] lemma clog_of_right_le_one {n : ℕ} (hn : n ≤ 1) (b : ℕ) : clog b n = 0 := by rw [clog, if_neg (λ h : 1 < b ∧ 1 < n, h.2.not_le hn)] @[simp] lemma clog_zero_left (n : ℕ) : clog 0 n = 0 := clog_of_left_le_one zero_le_one _ @[simp] lemma clog_zero_right (b : ℕ) : clog b 0 = 0 := clog_of_right_le_one zero_le_one _ @[simp] lemma clog_one_left (n : ℕ) : clog 1 n = 0 := clog_of_left_le_one le_rfl _ @[simp] lemma clog_one_right (b : ℕ) : clog b 1 = 0 := clog_of_right_le_one le_rfl _ lemma clog_of_two_le {b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : clog b n = clog b ((n + b - 1)/b) + 1 := by rw [clog, if_pos (⟨hb, hn⟩ : 1 < b ∧ 1 < n)] lemma clog_pos {b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : 0 < clog b n := by { rw clog_of_two_le hb hn, exact zero_lt_succ _ } lemma clog_eq_one {b n : ℕ} (hn : 2 ≤ n) (h : n ≤ b) : clog b n = 1 := begin rw [clog_of_two_le (hn.trans h) hn, clog_of_right_le_one], have n_pos : 0 < n := zero_lt_two.trans_le hn, rw [←lt_succ_iff, nat.div_lt_iff_lt_mul _ _ (n_pos.trans_le h), ←succ_le_iff, ←pred_eq_sub_one, succ_pred_eq_of_pos (add_pos n_pos (n_pos.trans_le h)), succ_mul, one_mul], exact add_le_add_right h _, end /--`clog b` and `pow b` form a Galois connection. -/ lemma le_pow_iff_clog_le {b : ℕ} (hb : 1 < b) {x y : ℕ} : x ≤ b^y ↔ clog b x ≤ y := begin induction x using nat.strong_induction_on with x ih generalizing y, cases y, { rw [pow_zero], refine ⟨λ h, (clog_of_right_le_one h b).le, _⟩, simp_rw ←not_lt, contrapose!, exact clog_pos hb }, have b_pos : 0 < b := zero_lt_two.trans_le hb, rw clog, split_ifs, { rw [succ_eq_add_one, add_le_add_iff_right, ←ih ((x + b - 1)/b) (add_pred_div_lt hb h.2), nat.div_le_iff_le_mul_add_pred b_pos, ← pow_succ, add_tsub_assoc_of_le (nat.succ_le_of_lt b_pos), add_le_add_iff_right] }, { exact iff_of_true ((not_lt.1 (not_and.1 h hb)).trans $ succ_le_of_lt $ pow_pos b_pos _) (zero_le _) } end lemma clog_pow (b x : ℕ) (hb : 1 < b) : clog b (b ^ x) = x := eq_of_forall_ge_iff $ λ z, by { rw ←le_pow_iff_clog_le hb, exact (pow_right_strict_mono hb).le_iff_le } lemma pow_pred_clog_lt_self {b : ℕ} (hb : 1 < b) {x : ℕ} (hx : 1 < x) : b ^ (clog b x).pred < x := begin rw [←not_le, le_pow_iff_clog_le hb, not_le], exact pred_lt (clog_pos hb hx).ne', end lemma le_pow_clog {b : ℕ} (hb : 1 < b) (x : ℕ) : x ≤ b ^ clog b x := (le_pow_iff_clog_le hb).2 le_rfl lemma clog_le_clog_of_le (b : ℕ) {n m : ℕ} (h : n ≤ m) : clog b n ≤ clog b m := begin cases le_or_lt b 1 with hb hb, { rw clog_of_left_le_one hb, exact zero_le _ }, { obtain rfl | hn := n.eq_zero_or_pos, { rw [clog_zero_right], exact zero_le _ }, { rw ←le_pow_iff_clog_le hb, exact h.trans (le_pow_clog hb _) } } end lemma clog_le_clog_of_left_ge {b c n : ℕ} (hc : 1 < c) (hb : c ≤ b) : clog b n ≤ clog c n := begin cases n, { simp }, rw ← le_pow_iff_clog_le (lt_of_lt_of_le hc hb), exact calc n.succ ≤ c ^ clog c n.succ : le_pow_clog hc _ ... ≤ b ^ clog c n.succ : pow_le_pow_of_le_left (le_of_lt $ zero_lt_one.trans hc) hb _ end lemma clog_monotone (b : ℕ) : monotone (clog b) := λ x y, clog_le_clog_of_le _ lemma clog_antitone_left {n : ℕ} : antitone_on (λ b : ℕ, clog b n) (set.Ioi 1) := λ _ hc _ _ hb, clog_le_clog_of_left_ge (set.mem_Iio.1 hc) hb lemma log_le_clog (b n : ℕ) : log b n ≤ clog b n := begin obtain hb | hb := le_or_lt b 1, { rw log_of_left_le_one hb, exact zero_le _}, cases n, { rw log_zero_right, exact zero_le _}, exact (pow_right_strict_mono hb).le_iff_le.1 ((pow_log_le_self hb $ succ_pos _).trans $ le_pow_clog hb _), end end nat
dee3b04acf7e327db11b462c8668a0b4e3850571
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/lie/weights.lean
e75bad2d9d9386d753b930ba7fdbcfb6ef733802
[ "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
23,978
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.nilpotent import algebra.lie.tensor_product import algebra.lie.character import algebra.lie.engel import algebra.lie.cartan_subalgebra import linear_algebra.eigenspace import ring_theory.tensor_product /-! # Weights and roots of Lie modules and Lie algebras Just as a key tool when studying the behaviour of a linear operator is to decompose the space on which it acts into a sum of (generalised) eigenspaces, a key tool when studying a representation `M` of Lie algebra `L` is to decompose `M` into a sum of simultaneous eigenspaces of `x` as `x` ranges over `L`. These simultaneous generalised eigenspaces are known as the weight spaces of `M`. When `L` is nilpotent, it follows from the binomial theorem that weight spaces are Lie submodules. Even when `L` is not nilpotent, it may be useful to study its representations by restricting them to a nilpotent subalgebra (e.g., a Cartan subalgebra). In the particular case when we view `L` as a module over itself via the adjoint action, the weight spaces of `L` restricted to a nilpotent subalgebra are known as root spaces. Basic definitions and properties of the above ideas are provided in this file. ## Main definitions * `lie_module.weight_space` * `lie_module.is_weight` * `lie_algebra.root_space` * `lie_algebra.is_root` * `lie_algebra.root_space_weight_space_product` * `lie_algebra.root_space_product` * `lie_algebra.zero_root_subalgebra_eq_iff_is_cartan` ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 7--9*](bourbaki1975b) ## Tags lie character, eigenvalue, eigenspace, weight, weight vector, root, root vector -/ universes u v w w₁ w₂ w₃ variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] variables (H : lie_subalgebra R L) [lie_algebra.is_nilpotent R H] variables (M : Type w) [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] namespace lie_module open lie_algebra open tensor_product open tensor_product.lie_module open_locale big_operators open_locale tensor_product /-- Given a Lie module `M` over a Lie algebra `L`, the pre-weight space of `M` with respect to a map `χ : L → R` is the simultaneous generalized eigenspace of the action of all `x : L` on `M`, with eigenvalues `χ x`. See also `lie_module.weight_space`. -/ def pre_weight_space (χ : L → R) : submodule R M := ⨅ (x : L), (to_endomorphism R L M x).maximal_generalized_eigenspace (χ x) lemma mem_pre_weight_space (χ : L → R) (m : M) : m ∈ pre_weight_space M χ ↔ ∀ x, ∃ (k : ℕ), ((to_endomorphism R L M x - (χ x) • 1)^k) m = 0 := by simp [pre_weight_space, -linear_map.pow_apply] variables (R) lemma exists_pre_weight_space_zero_le_ker_of_is_noetherian [is_noetherian R M] (x : L) : ∃ (k : ℕ), pre_weight_space M (0 : L → R) ≤ ((to_endomorphism R L M x)^k).ker := begin use (to_endomorphism R L M x).maximal_generalized_eigenspace_index 0, simp only [← module.End.generalized_eigenspace_zero, pre_weight_space, pi.zero_apply, infi_le, ← (to_endomorphism R L M x).maximal_generalized_eigenspace_eq], end variables {R} (L) /-- See also `bourbaki1975b` Chapter VII §1.1, Proposition 2 (ii). -/ protected lemma weight_vector_multiplication (M₁ : Type w₁) (M₂ : Type w₂) (M₃ : Type w₃) [add_comm_group M₁] [module R M₁] [lie_ring_module L M₁] [lie_module R L M₁] [add_comm_group M₂] [module R M₂] [lie_ring_module L M₂] [lie_module R L M₂] [add_comm_group M₃] [module R M₃] [lie_ring_module L M₃] [lie_module R L M₃] (g : M₁ ⊗[R] M₂ →ₗ⁅R,L⁆ M₃) (χ₁ χ₂ : L → R) : ((g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp (map_incl (pre_weight_space M₁ χ₁) (pre_weight_space M₂ χ₂))).range ≤ pre_weight_space M₃ (χ₁ + χ₂) := begin /- Unpack the statement of the goal. -/ intros m₃, simp only [lie_module_hom.coe_to_linear_map, pi.add_apply, function.comp_app, mem_pre_weight_space, linear_map.coe_comp, tensor_product.map_incl, exists_imp_distrib, linear_map.mem_range], rintros t rfl x, /- Set up some notation. -/ let F : module.End R M₃ := (to_endomorphism R L M₃ x) - (χ₁ x + χ₂ x) • 1, change ∃ k, (F^k) (g _) = 0, /- The goal is linear in `t` so use induction to reduce to the case that `t` is a pure tensor. -/ apply t.induction_on, { use 0, simp only [linear_map.map_zero, lie_module_hom.map_zero], }, swap, { rintros t₁ t₂ ⟨k₁, hk₁⟩ ⟨k₂, hk₂⟩, use max k₁ k₂, simp only [lie_module_hom.map_add, linear_map.map_add, linear_map.pow_map_zero_of_le (le_max_left k₁ k₂) hk₁, linear_map.pow_map_zero_of_le (le_max_right k₁ k₂) hk₂, add_zero], }, /- Now the main argument: pure tensors. -/ rintros ⟨m₁, hm₁⟩ ⟨m₂, hm₂⟩, change ∃ k, (F^k) ((g : M₁ ⊗[R] M₂ →ₗ[R] M₃) (m₁ ⊗ₜ m₂)) = 0, /- Eliminate `g` from the picture. -/ let f₁ : module.End R (M₁ ⊗[R] M₂) := (to_endomorphism R L M₁ x - (χ₁ x) • 1).rtensor M₂, let f₂ : module.End R (M₁ ⊗[R] M₂) := (to_endomorphism R L M₂ x - (χ₂ x) • 1).ltensor M₁, have h_comm_square : F ∘ₗ ↑g = (g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp (f₁ + f₂), { ext m₁ m₂, simp only [← g.map_lie x (m₁ ⊗ₜ m₂), add_smul, sub_tmul, tmul_sub, smul_tmul, lie_tmul_right, tmul_smul, to_endomorphism_apply_apply, lie_module_hom.map_smul, linear_map.one_apply, lie_module_hom.coe_to_linear_map, linear_map.smul_apply, function.comp_app, linear_map.coe_comp, linear_map.rtensor_tmul, lie_module_hom.map_add, linear_map.add_apply, lie_module_hom.map_sub, linear_map.sub_apply, linear_map.ltensor_tmul, algebra_tensor_module.curry_apply, curry_apply, linear_map.to_fun_eq_coe, linear_map.coe_restrict_scalars_eq_coe], abel, }, rsuffices ⟨k, hk⟩ : ∃ k, ((f₁ + f₂)^k) (m₁ ⊗ₜ m₂) = 0, { use k, rw [← linear_map.comp_apply, linear_map.commute_pow_left_of_commute h_comm_square, linear_map.comp_apply, hk, linear_map.map_zero], }, /- Unpack the information we have about `m₁`, `m₂`. -/ simp only [mem_pre_weight_space] at hm₁ hm₂, obtain ⟨k₁, hk₁⟩ := hm₁ x, obtain ⟨k₂, hk₂⟩ := hm₂ x, have hf₁ : (f₁^k₁) (m₁ ⊗ₜ m₂) = 0, { simp only [hk₁, zero_tmul, linear_map.rtensor_tmul, linear_map.rtensor_pow], }, have hf₂ : (f₂^k₂) (m₁ ⊗ₜ m₂) = 0, { simp only [hk₂, tmul_zero, linear_map.ltensor_tmul, linear_map.ltensor_pow], }, /- It's now just an application of the binomial theorem. -/ use k₁ + k₂ - 1, have hf_comm : commute f₁ f₂, { ext m₁ m₂, simp only [linear_map.mul_apply, linear_map.rtensor_tmul, linear_map.ltensor_tmul, algebra_tensor_module.curry_apply, linear_map.to_fun_eq_coe, linear_map.ltensor_tmul, curry_apply, linear_map.coe_restrict_scalars_eq_coe], }, rw hf_comm.add_pow', simp only [tensor_product.map_incl, submodule.subtype_apply, finset.sum_apply, submodule.coe_mk, linear_map.coe_fn_sum, tensor_product.map_tmul, linear_map.smul_apply], /- The required sum is zero because each individual term is zero. -/ apply finset.sum_eq_zero, rintros ⟨i, j⟩ hij, /- Eliminate the binomial coefficients from the picture. -/ suffices : (f₁^i * f₂^j) (m₁ ⊗ₜ m₂) = 0, { rw this, apply smul_zero, }, /- Finish off with appropriate case analysis. -/ cases nat.le_or_le_of_add_eq_add_pred (finset.nat.mem_antidiagonal.mp hij) with hi hj, { rw [(hf_comm.pow_pow i j).eq, linear_map.mul_apply, linear_map.pow_map_zero_of_le hi hf₁, linear_map.map_zero], }, { rw [linear_map.mul_apply, linear_map.pow_map_zero_of_le hj hf₂, linear_map.map_zero], }, end variables {L M} lemma lie_mem_pre_weight_space_of_mem_pre_weight_space {χ₁ χ₂ : L → R} {x : L} {m : M} (hx : x ∈ pre_weight_space L χ₁) (hm : m ∈ pre_weight_space M χ₂) : ⁅x, m⁆ ∈ pre_weight_space M (χ₁ + χ₂) := begin apply lie_module.weight_vector_multiplication L L M M (to_module_hom R L M) χ₁ χ₂, simp only [lie_module_hom.coe_to_linear_map, function.comp_app, linear_map.coe_comp, tensor_product.map_incl, linear_map.mem_range], use [⟨x, hx⟩ ⊗ₜ ⟨m, hm⟩], simp only [submodule.subtype_apply, to_module_hom_apply, tensor_product.map_tmul], refl, end variables (M) /-- If a Lie algebra is nilpotent, then pre-weight spaces are Lie submodules. -/ def weight_space [lie_algebra.is_nilpotent R L] (χ : L → R) : lie_submodule R L M := { lie_mem := λ x m hm, begin rw ← zero_add χ, refine lie_mem_pre_weight_space_of_mem_pre_weight_space _ hm, suffices : pre_weight_space L (0 : L → R) = ⊤, { simp only [this, submodule.mem_top], }, exact lie_algebra.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L, end, .. pre_weight_space M χ } lemma mem_weight_space [lie_algebra.is_nilpotent R L] (χ : L → R) (m : M) : m ∈ weight_space M χ ↔ m ∈ pre_weight_space M χ := iff.rfl /-- See also the more useful form `lie_module.zero_weight_space_eq_top_of_nilpotent`. -/ @[simp] lemma zero_weight_space_eq_top_of_nilpotent' [lie_algebra.is_nilpotent R L] [is_nilpotent R L M] : weight_space M (0 : L → R) = ⊤ := begin rw [← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule], exact infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L M, end lemma coe_weight_space_of_top [lie_algebra.is_nilpotent R L] (χ : L → R) : (weight_space M (χ ∘ (⊤ : lie_subalgebra R L).incl) : submodule R M) = weight_space M χ := begin ext m, simp only [weight_space, lie_submodule.coe_to_submodule_mk, lie_subalgebra.coe_bracket_of_module, function.comp_app, mem_pre_weight_space], split; intros h x, { obtain ⟨k, hk⟩ := h ⟨x, set.mem_univ x⟩, use k, exact hk, }, { obtain ⟨k, hk⟩ := h x, use k, exact hk, }, end @[simp] lemma zero_weight_space_eq_top_of_nilpotent [lie_algebra.is_nilpotent R L] [is_nilpotent R L M] : weight_space M (0 : (⊤ : lie_subalgebra R L) → R) = ⊤ := begin /- We use `coe_weight_space_of_top` as a trick to circumvent the fact that we don't (yet) know `is_nilpotent R (⊤ : lie_subalgebra R L) M` is equivalent to `is_nilpotent R L M`. -/ have h₀ : (0 : L → R) ∘ (⊤ : lie_subalgebra R L).incl = 0, { ext, refl, }, rw [← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule, ← h₀, coe_weight_space_of_top, ← infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L M], refl, end /-- Given a Lie module `M` of a Lie algebra `L`, a weight of `M` with respect to a nilpotent subalgebra `H ⊆ L` is a Lie character whose corresponding weight space is non-empty. -/ def is_weight (χ : lie_character R H) : Prop := weight_space M χ ≠ ⊥ /-- For a non-trivial nilpotent Lie module over a nilpotent Lie algebra, the zero character is a weight with respect to the `⊤` Lie subalgebra. -/ lemma is_weight_zero_of_nilpotent [nontrivial M] [lie_algebra.is_nilpotent R L] [is_nilpotent R L M] : is_weight (⊤ : lie_subalgebra R L) M 0 := by { rw [is_weight, lie_hom.coe_zero, zero_weight_space_eq_top_of_nilpotent], exact top_ne_bot, } /-- A (nilpotent) Lie algebra acts nilpotently on the zero weight space of a Noetherian Lie module. -/ lemma is_nilpotent_to_endomorphism_weight_space_zero [lie_algebra.is_nilpotent R L] [is_noetherian R M] (x : L) : _root_.is_nilpotent $ to_endomorphism R L (weight_space M (0 : L → R)) x := begin obtain ⟨k, hk⟩ := exists_pre_weight_space_zero_le_ker_of_is_noetherian R M x, use k, ext ⟨m, hm⟩, rw [linear_map.zero_apply, lie_submodule.coe_zero, submodule.coe_eq_zero, ← lie_submodule.to_endomorphism_restrict_eq_to_endomorphism, linear_map.pow_restrict, ← set_like.coe_eq_coe, linear_map.restrict_apply, submodule.coe_mk, submodule.coe_zero], exact hk hm, end /-- By Engel's theorem, when the Lie algebra is Noetherian, the zero weight space of a Noetherian Lie module is nilpotent. -/ instance [lie_algebra.is_nilpotent R L] [is_noetherian R L] [is_noetherian R M] : is_nilpotent R L (weight_space M (0 : L → R)) := is_nilpotent_iff_forall.mpr $ is_nilpotent_to_endomorphism_weight_space_zero M end lie_module namespace lie_algebra open_locale tensor_product open tensor_product.lie_module open lie_module /-- Given a nilpotent Lie subalgebra `H ⊆ L`, the root space of a map `χ : H → R` is the weight space of `L` regarded as a module of `H` via the adjoint action. -/ abbreviation root_space (χ : H → R) : lie_submodule R H L := weight_space L χ @[simp] lemma zero_root_space_eq_top_of_nilpotent [h : is_nilpotent R L] : root_space (⊤ : lie_subalgebra R L) 0 = ⊤ := zero_weight_space_eq_top_of_nilpotent L /-- A root of a Lie algebra `L` with respect to a nilpotent subalgebra `H ⊆ L` is a weight of `L`, regarded as a module of `H` via the adjoint action. -/ abbreviation is_root := is_weight H L @[simp] lemma root_space_comap_eq_weight_space (χ : H → R) : (root_space H χ).comap H.incl' = weight_space H χ := begin ext x, let f : H → module.End R L := λ y, to_endomorphism R H L y - (χ y) • 1, let g : H → module.End R H := λ y, to_endomorphism R H H y - (χ y) • 1, suffices : (∀ (y : H), ∃ (k : ℕ), ((f y)^k).comp (H.incl : H →ₗ[R] L) x = 0) ↔ ∀ (y : H), ∃ (k : ℕ), (H.incl : H →ₗ[R] L).comp ((g y)^k) x = 0, { simp only [lie_hom.coe_to_linear_map, lie_subalgebra.coe_incl, function.comp_app, linear_map.coe_comp, submodule.coe_eq_zero] at this, simp only [mem_weight_space, mem_pre_weight_space, lie_subalgebra.coe_incl', lie_submodule.mem_comap, this], }, have hfg : ∀ (y : H), (f y).comp (H.incl : H →ₗ[R] L) = (H.incl : H →ₗ[R] L).comp (g y), { rintros ⟨y, hy⟩, ext ⟨z, hz⟩, simp only [submodule.coe_sub, to_endomorphism_apply_apply, lie_hom.coe_to_linear_map, linear_map.one_apply, lie_subalgebra.coe_incl, lie_subalgebra.coe_bracket_of_module, lie_subalgebra.coe_bracket, linear_map.smul_apply, function.comp_app, submodule.coe_smul_of_tower, linear_map.coe_comp, linear_map.sub_apply], }, simp_rw [linear_map.commute_pow_left_of_commute (hfg _)], end variables {H M} lemma lie_mem_weight_space_of_mem_weight_space {χ₁ χ₂ : H → R} {x : L} {m : M} (hx : x ∈ root_space H χ₁) (hm : m ∈ weight_space M χ₂) : ⁅x, m⁆ ∈ weight_space M (χ₁ + χ₂) := begin apply lie_module.weight_vector_multiplication H L M M ((to_module_hom R L M).restrict_lie H) χ₁ χ₂, simp only [lie_module_hom.coe_to_linear_map, function.comp_app, linear_map.coe_comp, tensor_product.map_incl, linear_map.mem_range], use [⟨x, hx⟩ ⊗ₜ ⟨m, hm⟩], simp only [submodule.subtype_apply, to_module_hom_apply, submodule.coe_mk, lie_module_hom.coe_restrict_lie, tensor_product.map_tmul], end variables (R L H M) /-- Auxiliary definition for `root_space_weight_space_product`, which is close to the deterministic timeout limit. -/ def root_space_weight_space_product_aux {χ₁ χ₂ χ₃ : H → R} (hχ : χ₁ + χ₂ = χ₃) : (root_space H χ₁) →ₗ[R] (weight_space M χ₂) →ₗ[R] (weight_space M χ₃) := { to_fun := λ x, { to_fun := λ m, ⟨⁅(x : L), (m : M)⁆, hχ ▸ (lie_mem_weight_space_of_mem_weight_space x.property m.property) ⟩, map_add' := λ m n, by { simp only [lie_submodule.coe_add, lie_add], refl, }, map_smul' := λ t m, by { conv_lhs { congr, rw [lie_submodule.coe_smul, lie_smul], }, refl, }, }, map_add' := λ x y, by ext m; rw [linear_map.add_apply, linear_map.coe_mk, linear_map.coe_mk, linear_map.coe_mk, subtype.coe_mk, lie_submodule.coe_add, lie_submodule.coe_add, add_lie, subtype.coe_mk, subtype.coe_mk], map_smul' := λ t x, begin simp only [ring_hom.id_apply], ext m, rw [linear_map.smul_apply, linear_map.coe_mk, linear_map.coe_mk, subtype.coe_mk, lie_submodule.coe_smul, smul_lie, lie_submodule.coe_smul, subtype.coe_mk], end, } /-- Given a nilpotent Lie subalgebra `H ⊆ L` together with `χ₁ χ₂ : H → R`, there is a natural `R`-bilinear product of root vectors and weight vectors, compatible with the actions of `H`. -/ def root_space_weight_space_product (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) : (root_space H χ₁) ⊗[R] (weight_space M χ₂) →ₗ⁅R,H⁆ weight_space M χ₃ := lift_lie R H (root_space H χ₁) (weight_space M χ₂) (weight_space M χ₃) { to_linear_map := root_space_weight_space_product_aux R L H M hχ, map_lie' := λ x y, by ext m; rw [root_space_weight_space_product_aux, lie_hom.lie_apply, lie_submodule.coe_sub, linear_map.coe_mk, linear_map.coe_mk, subtype.coe_mk, subtype.coe_mk, lie_submodule.coe_bracket, lie_submodule.coe_bracket, subtype.coe_mk, lie_subalgebra.coe_bracket_of_module, lie_subalgebra.coe_bracket_of_module, lie_submodule.coe_bracket, lie_subalgebra.coe_bracket_of_module, lie_lie], } @[simp] lemma coe_root_space_weight_space_product_tmul (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) (x : root_space H χ₁) (m : weight_space M χ₂) : (root_space_weight_space_product R L H M χ₁ χ₂ χ₃ hχ (x ⊗ₜ m) : M) = ⁅(x : L), (m : M)⁆ := by simp only [root_space_weight_space_product, root_space_weight_space_product_aux, lift_apply, lie_module_hom.coe_to_linear_map, coe_lift_lie_eq_lift_coe, submodule.coe_mk, linear_map.coe_mk, lie_module_hom.coe_mk] /-- Given a nilpotent Lie subalgebra `H ⊆ L` together with `χ₁ χ₂ : H → R`, there is a natural `R`-bilinear product of root vectors, compatible with the actions of `H`. -/ def root_space_product (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) : (root_space H χ₁) ⊗[R] (root_space H χ₂) →ₗ⁅R,H⁆ root_space H χ₃ := root_space_weight_space_product R L H L χ₁ χ₂ χ₃ hχ @[simp] lemma root_space_product_def : root_space_product R L H = root_space_weight_space_product R L H L := rfl lemma root_space_product_tmul (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) (x : root_space H χ₁) (y : root_space H χ₂) : (root_space_product R L H χ₁ χ₂ χ₃ hχ (x ⊗ₜ y) : L) = ⁅(x : L), (y : L)⁆ := by simp only [root_space_product_def, coe_root_space_weight_space_product_tmul] /-- Given a nilpotent Lie subalgebra `H ⊆ L`, the root space of the zero map `0 : H → R` is a Lie subalgebra of `L`. -/ def zero_root_subalgebra : lie_subalgebra R L := { lie_mem' := λ x y hx hy, by { let xy : (root_space H 0) ⊗[R] (root_space H 0) := ⟨x, hx⟩ ⊗ₜ ⟨y, hy⟩, suffices : (root_space_product R L H 0 0 0 (add_zero 0) xy : L) ∈ root_space H 0, { rwa [root_space_product_tmul, subtype.coe_mk, subtype.coe_mk] at this, }, exact (root_space_product R L H 0 0 0 (add_zero 0) xy).property, }, .. (root_space H 0 : submodule R L) } @[simp] lemma coe_zero_root_subalgebra : (zero_root_subalgebra R L H : submodule R L) = root_space H 0 := rfl lemma mem_zero_root_subalgebra (x : L) : x ∈ zero_root_subalgebra R L H ↔ ∀ (y : H), ∃ (k : ℕ), ((to_endomorphism R H L y)^k) x = 0 := by simp only [zero_root_subalgebra, mem_weight_space, mem_pre_weight_space, pi.zero_apply, sub_zero, set_like.mem_coe, zero_smul, lie_submodule.mem_coe_submodule, submodule.mem_carrier, lie_subalgebra.mem_mk_iff] lemma to_lie_submodule_le_root_space_zero : H.to_lie_submodule ≤ root_space H 0 := begin intros x hx, simp only [lie_subalgebra.mem_to_lie_submodule] at hx, simp only [mem_weight_space, mem_pre_weight_space, pi.zero_apply, sub_zero, zero_smul], intros y, unfreezingI { obtain ⟨k, hk⟩ := (infer_instance : is_nilpotent R H) }, use k, let f : module.End R H := to_endomorphism R H H y, let g : module.End R L := to_endomorphism R H L y, have hfg : g.comp (H : submodule R L).subtype = (H : submodule R L).subtype.comp f, { ext z, simp only [to_endomorphism_apply_apply, submodule.subtype_apply, lie_subalgebra.coe_bracket_of_module, lie_subalgebra.coe_bracket, function.comp_app, linear_map.coe_comp], }, change (g^k).comp (H : submodule R L).subtype ⟨x, hx⟩ = 0, rw linear_map.commute_pow_left_of_commute hfg k, have h := iterate_to_endomorphism_mem_lower_central_series R H H y ⟨x, hx⟩ k, rw [hk, lie_submodule.mem_bot] at h, simp only [submodule.subtype_apply, function.comp_app, linear_map.pow_apply, linear_map.coe_comp, submodule.coe_eq_zero], exact h, end lemma le_zero_root_subalgebra : H ≤ zero_root_subalgebra R L H := begin rw [← lie_subalgebra.coe_submodule_le_coe_submodule, ← H.coe_to_lie_submodule, coe_zero_root_subalgebra, lie_submodule.coe_submodule_le_coe_submodule], exact to_lie_submodule_le_root_space_zero R L H, end @[simp] lemma zero_root_subalgebra_normalizer_eq_self : (zero_root_subalgebra R L H).normalizer = zero_root_subalgebra R L H := begin refine le_antisymm _ (lie_subalgebra.le_normalizer _), intros x hx, rw lie_subalgebra.mem_normalizer_iff at hx, rw mem_zero_root_subalgebra, rintros ⟨y, hy⟩, specialize hx y (le_zero_root_subalgebra R L H hy), rw mem_zero_root_subalgebra at hx, obtain ⟨k, hk⟩ := hx ⟨y, hy⟩, rw [← lie_skew, linear_map.map_neg, neg_eq_zero] at hk, use k + 1, rw [linear_map.iterate_succ, linear_map.coe_comp, function.comp_app, to_endomorphism_apply_apply, lie_subalgebra.coe_bracket_of_module, submodule.coe_mk, hk], end /-- If the zero root subalgebra of a nilpotent Lie subalgebra `H` is just `H` then `H` is a Cartan subalgebra. When `L` is Noetherian, it follows from Engel's theorem that the converse holds. See `lie_algebra.zero_root_subalgebra_eq_iff_is_cartan` -/ lemma is_cartan_of_zero_root_subalgebra_eq (h : zero_root_subalgebra R L H = H) : H.is_cartan_subalgebra := { nilpotent := infer_instance, self_normalizing := by { rw ← h, exact zero_root_subalgebra_normalizer_eq_self R L H, } } @[simp] lemma zero_root_subalgebra_eq_of_is_cartan (H : lie_subalgebra R L) [H.is_cartan_subalgebra] [is_noetherian R L] : zero_root_subalgebra R L H = H := begin refine le_antisymm _ (le_zero_root_subalgebra R L H), suffices : root_space H 0 ≤ H.to_lie_submodule, { exact λ x hx, this hx, }, obtain ⟨k, hk⟩ := (root_space H 0).is_nilpotent_iff_exists_self_le_ucs.mp (by apply_instance), exact hk.trans (lie_submodule.ucs_le_of_centralizer_eq_self (by simp) k), end lemma zero_root_subalgebra_eq_iff_is_cartan [is_noetherian R L] : zero_root_subalgebra R L H = H ↔ H.is_cartan_subalgebra := ⟨is_cartan_of_zero_root_subalgebra_eq R L H, by { introsI, simp, }⟩ end lie_algebra namespace lie_module open lie_algebra variables {R L H} /-- A priori, weight spaces are Lie submodules over the Lie subalgebra `H` used to define them. However they are naturally Lie submodules over the (in general larger) Lie subalgebra `zero_root_subalgebra R L H`. Even though it is often the case that `zero_root_subalgebra R L H = H`, it is likely to be useful to have the flexibility not to have to invoke this equality (as well as to work more generally). -/ def weight_space' (χ : H → R) : lie_submodule R (zero_root_subalgebra R L H) M := { lie_mem := λ x m hm, by { have hx : (x : L) ∈ root_space H 0, { rw [← lie_submodule.mem_coe_submodule, ← coe_zero_root_subalgebra], exact x.property, }, rw ← zero_add χ, exact lie_mem_weight_space_of_mem_weight_space hx hm, }, .. (weight_space M χ : submodule R M) } @[simp] lemma coe_weight_space' (χ : H → R) : (weight_space' M χ : submodule R M) = weight_space M χ := rfl end lie_module
06fbba5849167f0eb86e432b87d80d35505441be
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/topology/basic.lean
7c76cc35975620da99fb2629ec39f24618fbde35
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
48,364
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, Jeremy Avigad -/ import order.filter.ultrafilter import order.filter.partial noncomputable theory /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. A filter `F` on `α` has `x` as a cluster point if `is_cluster_pt x F : 𝓝 x ⊓ F ≠ ⊥`. A map `f : ι → α` clusters at `x` along `F : filter ι` if `map_cluster_pt x F f : cluster_pt x (map f F)`. In particular the notion of cluster point of a sequence `u` is `map_cluster_pt x at_top u`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Notation * `𝓝 x`: the filter of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ open set filter classical open_locale classical filter universes u v w /-! ### Topological spaces -/ /-- A topology on `α`. -/ @[protect_proj] structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def topological_space.of_closed {α : Type u} (T : set (set α)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) : topological_space α := { is_open := λ X, Xᶜ ∈ T, is_open_univ := by simp [empty_mem], is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem sᶜ tᶜ hs ht, is_open_sUnion := λ s hs, by rw set.compl_sUnion; exact sInter_mem (set.compl '' s) (λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) } section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open sᶜ @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open sᶜ ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed sᶜ ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = {x | p x}ᶜ ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-! ### Interior of a set -/ /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma is_open.interior_eq {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, is_open.interior_eq⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := is_open_empty.interior_eq @[simp] lemma interior_univ : interior (univ : set α) = univ := is_open_univ.interior_eq @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := is_open_interior.interior_eq @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-! ### Closure of a set -/ /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma is_closed.closure_eq {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma is_closed.closure_subset {s : set α} (hs : is_closed s) : closure s ⊆ s := closure_minimal (subset.refl _) hs lemma is_closed.closure_subset_iff {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, is_closed.closure_eq⟩ lemma closure_subset_iff_is_closed {s : set α} : closure s ⊆ s ↔ is_closed s := ⟨is_closed_of_closure_subset, is_closed.closure_subset⟩ @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := is_closed_empty.closure_eq @[simp] lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ lemma set.nonempty.closure {s : set α} (h : s.nonempty) : set.nonempty (closure s) := let ⟨x, hx⟩ := h in ⟨x, subset_closure hx⟩ @[simp] lemma closure_univ : closure (univ : set α) = univ := is_closed_univ.closure_eq @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := is_closed_closure.closure_eq @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = (interior sᶜ)ᶜ := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior sᶜ = (closure s)ᶜ := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure sᶜ = (interior s)ᶜ := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ oᶜ, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁ nc) in hc (h₂ hs)⟩ /-- A set is dense in a topological space if every point belongs to its closure. -/ def dense (s : set α) : Prop := ∀ x, x ∈ closure s lemma dense_iff_closure_eq {s : set α} : dense s ↔ closure s = univ := by rw [dense, eq_univ_iff_forall] lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ := dense_iff_closure_eq.mp h lemma dense_iff_inter_open {s : set α} : dense s ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h.closure_eq]) U U_op x_in }, { intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end @[mono] lemma dense_of_subset_dense {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : dense s₁) : dense s₂ := λ x, closure_mono h (hd x) /-! ### Frontier of a set -/ /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure sᶜ := by rw [closure_compl, frontier, diff_eq] /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier sᶜ = frontier s := by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure tᶜ) ∪ (closure sᶜ ∩ frontier t) := by simpa only [frontier_compl, ← compl_union] using frontier_inter_subset sᶜ tᶜ lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, hs.closure_eq] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, hs.interior_eq] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure /-- The frontier of a closed set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end lemma closure_eq_interior_union_frontier (s : set α) : closure s = interior s ∪ frontier s := (union_diff_cancel interior_subset_closure).symm lemma closure_eq_self_union_frontier (s : set α) : closure s = s ∪ frontier s := (union_diff_cancel' interior_subset subset_closure).symm /-! ### Neighborhoods -/ /-- A set is called a neighborhood of `a` if it contains an open set around `a`. The set of all neighborhoods of `a` forms a filter, the neighborhood filter at `a`, is here defined as the infimum over the principal filters of all open sets containing `a`. -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) localized "notation `𝓝` := nhds" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := rfl /-- The open sets containing `a` are a basis for the neighborhood filter. See `nhds_basis_opens'` for a variant using open neighborhoods instead. -/ lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) := has_basis_binfi_principal (λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open_inter hs ht⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩) ⟨univ, ⟨mem_univ a, is_open_univ⟩⟩ /-- A filter lies below the neighborhood filter at `a` iff it contains every open set around `a`. -/ lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] /-- To show a filter is above the neighborhood filter at `a`, it suffices to show that it is above the principal filter of some open set `s` containing `a`. -/ lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : 𝓟 s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t := (nhds_basis_opens a).mem_iff.trans ⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩ /-- A predicate is true in a neighborhood of `a` iff it is true for all the points in an open set containing `a`. -/ lemma eventually_nhds_iff {a : α} {p : α → Prop} : (∀ᶠ x in 𝓝 a, p x) ↔ ∃ (t : set α), (∀ x ∈ t, p x) ∧ is_open t ∧ a ∈ t := mem_nhds_sets_iff.trans $ by simp only [subset_def, exists_prop, mem_set_of_eq] lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 (image f s)) := ((nhds_basis_opens a).map f).eq_binfi attribute [irreducible] nhds lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs /-- If a predicate is true in a neighborhood of `a`, then it is true for `a`. -/ lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : p a := mem_of_nhds h lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ lemma is_open.eventually_mem {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : ∀ᶠ x in 𝓝 a, x ∈ s := mem_nhds_sets hs ha /-- The open neighborhoods of `a` are a basis for the neighborhood filter. See `nhds_basis_opens` for a variant using open sets around `a` instead. -/ lemma nhds_basis_opens' (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_open s) (λ x, x) := begin convert nhds_basis_opens a, ext s, split, { rintros ⟨s_in, s_op⟩, exact ⟨mem_of_nhds s_in, s_op⟩ }, { rintros ⟨a_in, s_op⟩, exact ⟨mem_nhds_sets s_op a_in, s_op⟩ }, end /-- If a predicate is true in a neighbourhood of `a`, then for `y` sufficiently close to `a` this predicate is true in a neighbourhood of `y`. -/ lemma filter.eventually.eventually_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : ∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x := let ⟨t, htp, hto, ha⟩ := eventually_nhds_iff.1 h in eventually_nhds_iff.2 ⟨t, λ x hx, eventually_nhds_iff.2 ⟨t, htp, hto, hx⟩, hto, ha⟩ @[simp] lemma eventually_eventually_nhds {p : α → Prop} {a : α} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x) ↔ ∀ᶠ x in 𝓝 a, p x := ⟨λ h, h.self_of_nhds, λ h, h.eventually_nhds⟩ @[simp] lemma nhds_bind_nhds : (𝓝 a).bind 𝓝 = 𝓝 a := filter.ext $ λ s, eventually_eventually_nhds @[simp] lemma eventually_eventually_eq_nhds {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g) ↔ f =ᶠ[𝓝 a] g := eventually_eventually_nhds lemma filter.eventually_eq.eq_of_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : f a = g a := h.self_of_nhds @[simp] lemma eventually_eventually_le_nhds [has_le β] {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g) ↔ f ≤ᶠ[𝓝 a] g := eventually_eventually_nhds /-- If two functions are equal in a neighbourhood of `a`, then for `y` sufficiently close to `a` these functions are equal in a neighbourhood of `y`. -/ lemma filter.eventually_eq.eventually_eq_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g := h.eventually_nhds /-- If `f x ≤ g x` in a neighbourhood of `a`, then for `y` sufficiently close to `a` we have `f x ≤ g x` in a neighbourhood of `y`. -/ lemma filter.eventually_le.eventually_le_nhds [has_le β] {f g : α → β} {a : α} (h : f ≤ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g := h.eventually_nhds theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := ((nhds_basis_opens x).forall_iff hP).trans $ by simp only [and_comm (x ∈ _), and_imp] theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure_sets.2 $ mem_of_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := (tendsto_pure_pure f a).mono_right (pure_le_nhds _) lemma order_top.tendsto_at_top_nhds {α : Type*} [order_top α] [topological_space β] (f : α → β) : tendsto f at_top (𝓝 $ f ⊤) := (tendsto_at_top_pure f).mono_right (pure_le_nhds _) @[simp] instance nhds_ne_bot {a : α} : ne_bot (𝓝 a) := ne_bot_of_le (pure_le_nhds a) /-! ### Cluster points In this section we define [cluster points](https://en.wikipedia.org/wiki/Limit_point) (also known as limit points and accumulation points) of a filter and of a sequence. -/ /-- A point `x` is a cluster point of a filter `F` if 𝓝 x ⊓ F ≠ ⊥. Also known as an accumulation point or a limit point. -/ def cluster_pt (x : α) (F : filter α) : Prop := ne_bot (𝓝 x ⊓ F) lemma cluster_pt.ne_bot {x : α} {F : filter α} (h : cluster_pt x F) : ne_bot (𝓝 x ⊓ F) := h lemma cluster_pt_iff {x : α} {F : filter α} : cluster_pt x F ↔ ∀ {U V : set α}, U ∈ 𝓝 x → V ∈ F → (U ∩ V).nonempty := inf_ne_bot_iff /-- `x` is a cluster point of a set `s` if every neighbourhood of `x` meets `s` on a nonempty set. -/ lemma cluster_pt_principal_iff {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∀ U ∈ 𝓝 x, (U ∩ s).nonempty := inf_principal_ne_bot_iff lemma cluster_pt_principal_iff_frequently {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∃ᶠ y in 𝓝 x, y ∈ s := by simp only [cluster_pt_principal_iff, frequently_iff, set.nonempty, exists_prop, mem_inter_iff] lemma cluster_pt.of_le_nhds {x : α} {f : filter α} (H : f ≤ 𝓝 x) [ne_bot f] : cluster_pt x f := by rwa [cluster_pt, inf_eq_right.mpr H] lemma cluster_pt.of_le_nhds' {x : α} {f : filter α} (H : f ≤ 𝓝 x) (hf : ne_bot f) : cluster_pt x f := cluster_pt.of_le_nhds H lemma cluster_pt.of_nhds_le {x : α} {f : filter α} (H : 𝓝 x ≤ f) : cluster_pt x f := by simp only [cluster_pt, inf_eq_left.mpr H, nhds_ne_bot] lemma cluster_pt.mono {x : α} {f g : filter α} (H : cluster_pt x f) (h : f ≤ g) : cluster_pt x g := ne_bot_of_le_ne_bot H $ inf_le_inf_left _ h lemma cluster_pt.of_inf_left {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x f := H.mono inf_le_left lemma cluster_pt.of_inf_right {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x g := H.mono inf_le_right /-- A point `x` is a cluster point of a sequence `u` along a filter `F` if it is a cluster point of `map u F`. -/ def map_cluster_pt {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : Prop := cluster_pt x (map u F) lemma map_cluster_pt_iff {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : map_cluster_pt x F u ↔ ∀ s ∈ 𝓝 x, ∃ᶠ a in F, u a ∈ s := by { simp_rw [map_cluster_pt, cluster_pt, inf_ne_bot_iff_frequently_left, frequently_map], refl } lemma map_cluster_pt_of_comp {ι δ :Type*} {F : filter ι} {φ : δ → ι} {p : filter δ} {x : α} {u : ι → α} [ne_bot p] (h : tendsto φ p F) (H : tendsto (u ∘ φ) p (𝓝 x)) : map_cluster_pt x F u := begin have := calc map (u ∘ φ) p = map u (map φ p) : map_map ... ≤ map u F : map_mono h, have : map (u ∘ φ) p ≤ 𝓝 x ⊓ map u F, from le_inf H this, exact ne_bot_of_le this end /-! ### Interior, closure and frontier in terms of neighborhoods -/ lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ 𝓟 s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma subset_interior_iff_nhds {s V : set α} : s ⊆ interior V ↔ ∀ x ∈ s, V ∈ 𝓝 x := show (∀ x, x ∈ s → x ∈ _) ↔ _, by simp_rw mem_interior_iff_mem_nhds lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ 𝓟 s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ 𝓟 s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma mem_closure_iff_frequently {s : set α} {a : α} : a ∈ closure s ↔ ∃ᶠ x in 𝓝 a, x ∈ s := by rw [filter.frequently, filter.eventually, ← mem_interior_iff_mem_nhds, closure_eq_compl_interior_compl]; refl alias mem_closure_iff_frequently ↔ _ filter.frequently.mem_closure theorem mem_closure_iff_cluster_pt {s : set α} {a : α} : a ∈ closure s ↔ cluster_pt a (𝓟 s) := mem_closure_iff_frequently.trans cluster_pt_principal_iff_frequently.symm lemma closure_eq_cluster_pts {s : set α} : closure s = {a | cluster_pt a (𝓟 s)} := set.ext $ λ x, mem_closure_iff_cluster_pt theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff_cluster_pt.trans cluster_pt_principal_iff theorem mem_closure_iff_nhds' {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, ∃ y : s, ↑y ∈ t := by simp only [mem_closure_iff_nhds, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_comap_ne_bot {A : set α} {x : α} : x ∈ closure A ↔ ne_bot (comap (coe : A → α) (𝓝 x)) := by simp_rw [mem_closure_iff_nhds, comap_ne_bot_iff, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_nhds_basis {a : α} {p : β → Prop} {s : β → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i := mem_closure_iff_nhds.trans ⟨λ H i hi, let ⟨x, hx⟩ := (H _ $ h.mem_of_mem hi) in ⟨x, hx.2, hx.1⟩, λ H t' ht', let ⟨i, hi, hit⟩ := h.mem_iff.1 ht', ⟨x, xt, hx⟩ := H i hi in ⟨x, hit hx, xt⟩⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ 𝓝 x := begin rw closure_eq_cluster_pts, change cluster_pt x (𝓟 s) ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_cluster_pt {s : set α} : is_closed s ↔ ∀a, cluster_pt a (𝓟 s) → a ∈ s := calc is_closed s ↔ closure s ⊆ s : closure_subset_iff_is_closed.symm ... ↔ (∀a, cluster_pt a (𝓟 s) → a ∈ s) : by simp only [subset_def, mem_closure_iff_cluster_pt] lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀ x, (∀ U ∈ 𝓝 x, (U ∩ s).nonempty) → x ∈ s := by simp_rw [is_closed_iff_cluster_pt, cluster_pt, inf_principal_ne_bot_iff] lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ 𝓝 a, from mem_nhds_sets h hs, have 𝓝 a ⊓ 𝓟 s = 𝓝 a, by rwa [inf_eq_left, le_principal_iff], have cluster_pt a (𝓟 (s ∩ t)), from calc 𝓝 a ⊓ 𝓟 (s ∩ t) = 𝓝 a ⊓ (𝓟 s ⊓ 𝓟 t) : by rw inf_principal ... = 𝓝 a ⊓ 𝓟 t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_cluster_pts] at ht; assumption, by rwa [closure_eq_cluster_pts] lemma dense_inter_of_open_left {s t : set α} (hs : closure s = univ) (ht : closure t = univ) (hso : is_open s) : closure (s ∩ t) = univ := eq_univ_of_subset (closure_minimal (closure_inter_open hso) is_closed_closure) $ by simp only [*, inter_univ] lemma dense_inter_of_open_right {s t : set α} (hs : closure s = univ) (ht : closure t = univ) (hto : is_open t) : closure (s ∩ t) = univ := inter_comm t s ▸ dense_inter_of_open_left ht hs hto lemma closure_diff {s t : set α} : closure s \ closure t ⊆ closure (s \ t) := calc closure s \ closure t = (closure t)ᶜ ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure ((closure t)ᶜ ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma filter.frequently.mem_of_closed {a : α} {s : set α} (h : ∃ᶠ x in 𝓝 a, x ∈ s) (hs : is_closed s) : a ∈ s := hs.closure_subset h.mem_closure lemma is_closed.mem_of_frequently_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hs : is_closed s) (h : ∃ᶠ x in b, f x ∈ s) (hf : tendsto f b (𝓝 a)) : a ∈ s := (hf.frequently $ show ∃ᶠ x in b, (λ y, y ∈ s) (f x), from h).mem_of_closed hs lemma is_closed.mem_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hs : is_closed s) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ s := hs.mem_of_frequently_of_tendsto h.frequently hf lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s := is_closed_closure.mem_of_tendsto hf $ h.mono (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only if it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ 𝓟 s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : 𝓟 sᶜ ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end /-! ### Limits of filters in topological spaces -/ section lim /-- If `f` is a filter, then `Lim f` is a limit of the filter, if it exists. -/ noncomputable def Lim [nonempty α] (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a /-- If `f` is a filter in `β` and `g : β → α` is a function, then `lim f` is a limit of `g` at `f`, if it exists. -/ noncomputable def lim [nonempty α] (f : filter β) (g : β → α) : α := Lim (f.map g) /-- If a filter `f` is majorated by some `𝓝 a`, then it is majorated by `𝓝 (Lim f)`. We formulate this lemma with a `[nonempty α]` argument of `Lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma Lim_spec {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (@Lim _ _ (nonempty_of_exists h) f) := epsilon_spec h /-- If `g` tends to some `𝓝 a` along `f`, then it tends to `𝓝 (lim f g)`. We formulate this lemma with a `[nonempty α]` argument of `lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma lim_spec {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) : tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) := Lim_spec h end lim /-! ### Locally finite families -/ /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, h.subset $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ (f i)ᶜ, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, (f i)ᶜ ∈ (𝓝 a), by simp only [mem_nhds_sets_iff]; exact assume i, ⟨(f i)ᶜ, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | (f i ∩ t).nonempty })⟩ := h₁ a in calc 𝓝 a ≤ 𝓟 (t ∩ (⋂ i∈{i | (f i ∩ t).nonempty }, (f i)ᶜ)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (𝓝 a) _ _ h_sets, apply @filter.Inter_mem_sets _ (𝓝 a) _ _ _ h_fin, exact assume i h, this i end ... ≤ 𝓟 (⋃i, f i)ᶜ : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, exists_imp_distrib, ne_empty_iff_nonempty, set.nonempty], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space /-! ### Continuity -/ section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) : is_open (f ⁻¹' s) := hf s h /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.tendsto {f : α → β} {x : α} (h : continuous_at f x) : tendsto f (𝓝 x) (𝓝 (f x)) := h lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β} (hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) := interior_maximal (preimage_mono interior_subset) (hf _ is_open_interior) lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) := nat.rec_on n continuous_id (λ n ihn, ihn.comp h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) := ((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $ λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, hf _ ht⟩, subset.refl _⟩ lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), from λ a ha, mem_nhds_sets hs ha, show is_open (f ⁻¹' s), from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := continuous_const.continuous_at lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) : continuous_at (f^[n]) x := nat.rec_on n continuous_at_id $ λ n ihn, show continuous_at (f^[n] ∘ f) x, from continuous_at.comp (hx.symm ▸ ihn) hf lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf sᶜ hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma is_closed.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_closed s) : is_closed (f ⁻¹' s) := continuous_iff_is_closed.mp hf s h lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔ ∀ g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] /-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g` are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ (interior {a | p a})ᶜ, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ (frontier {a | p a})ᶜ, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure {a | p a}ᶜ; rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure {a | p a}ᶜ, from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /- Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', simp only [ptendsto'_def, mem_nhds_sets_iff], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), cluster_pt a (𝓟 s) → cluster_pt (f a) (𝓟 (f '' s)), from assume a ha, have h₁ : ¬ map f (𝓝 a ⊓ 𝓟 s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (𝓝 a ⊓ 𝓟 s) ≤ 𝓝 (f a) ⊓ 𝓟 (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp [subset_preimage_image] ), ne_bot_of_le_ne_bot h₁ h₂, by simp [image_subset_iff, closure_eq_cluster_pts]; assumption lemma mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) /-! ### Function with dense range -/ section dense_range variables {κ ι : Type*} (f : κ → β) (g : β → γ) /-- `f : ι → β` has dense range if its range (image) is a dense subset of β. -/ def dense_range := dense (range f) variables {f} lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ := eq_univ_iff_forall.symm lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ := eq_univ_iff_forall.mpr h lemma dense_range.comp (hg : dense_range g) (hf : dense_range f) (cg : continuous g) : dense_range (g ∘ f) := begin have : g '' (closure $ range f) ⊆ closure (g '' range f), from image_closure_subset_closure_image cg, have : closure (g '' closure (range f)) ⊆ closure (g '' range f), by simpa [closure_closure] using (closure_mono this), intro c, rw range_comp, apply this, rw [hf.closure_range, image_univ], exact hg c end /-- If `f : ι → β` has dense range and `β` contains some element, then `ι` must too. -/ def dense_range.inhabited (df : dense_range f) (b : β) : inhabited κ := ⟨classical.choice $ by simpa only [univ_inter, range_nonempty_iff_nonempty] using mem_closure_iff.1 (df b) _ is_open_univ trivial⟩ lemma dense_range.nonempty (hf : dense_range f) : nonempty κ ↔ nonempty β := ⟨nonempty.map f, λ ⟨b⟩, @nonempty_of_inhabited _ (hf.inhabited b)⟩ lemma continuous.dense_image_of_dense_range {f : α → β} (hf : continuous f) (hf' : dense_range f) {s : set α} (hs : dense s) : closure (f '' s) = univ := begin have : f '' (closure s) ⊆ closure (f '' s) := image_closure_subset_closure_image hf, have := closure_mono this, rw [hs.closure_eq, image_univ, hf'.closure_eq, closure_closure] at this, exact eq_univ_of_univ_subset this end end dense_range end continuous
faa5b44a6537304e025334f84301975949a02ccc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/lazylist.lean
10b4dcdc9dd49b36fa524924b15314ef2e5f0113
[ "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
5,622
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universes u v w inductive LazyList (α : Type u) | nil {} : LazyList | cons (hd : α) (tl : LazyList) : LazyList | delayed (t : Thunk LazyList) : LazyList @[extern cpp inline "#2"] def List.toLazy {α : Type u} : List α → LazyList α | [] := LazyList.nil | (h::t) := LazyList.cons h (List.toLazy t) namespace LazyList variable {α : Type u} {β : Type v} {δ : Type w} instance : Inhabited (LazyList α) := ⟨nil⟩ @[inline] protected def pure : α → LazyList α | a := cons a nil partial def get : LazyList α → LazyList α | (delayed as) := get as.get | other := other partial def isEmpty : LazyList α → Bool | nil := true | (cons _ _) := false | (delayed as) := isEmpty as.get partial def toList : LazyList α → List α | nil := [] | (cons a as) := a :: toList as | (delayed as) := toList as.get partial def head [Inhabited α] : LazyList α → α | nil := default α | (cons a as) := a | (delayed as) := head as.get partial def tail : LazyList α → LazyList α | nil := nil | (cons a as) := as | (delayed as) := tail as.get partial def append : LazyList α → LazyList α → LazyList α | nil bs := bs | (cons a as) bs := cons a (delayed (append as bs)) | (delayed as) bs := append as.get bs instance : Append (LazyList α) := ⟨LazyList.append⟩ partial def interleave : LazyList α → LazyList α → LazyList α | nil bs := bs | (cons a as) bs := cons a (delayed (interleave bs as)) | (delayed as) bs := interleave as.get bs @[specialize] partial def map (f : α → β) : LazyList α → LazyList β | nil := nil | (cons a as) := cons (f a) (delayed (map as)) | (delayed as) := map as.get @[specialize] partial def map₂ (f : α → β → δ) : LazyList α → LazyList β → LazyList δ | nil _ := nil | _ nil := nil | (cons a as) (cons b bs) := cons (f a b) (delayed (map₂ as bs)) | (delayed as) bs := map₂ as.get bs | as (delayed bs) := map₂ as bs.get @[inline] def zip : LazyList α → LazyList β → LazyList (α × β) := map₂ Prod.mk partial def join : LazyList (LazyList α) → LazyList α | nil := nil | (cons a as) := append a (delayed (join as)) | (delayed as) := join as.get @[inline] protected partial def bind (x : LazyList α) (f : α → LazyList β) : LazyList β := join (x.map f) instance isMonad : Monad LazyList := { pure := @LazyList.pure, bind := @LazyList.bind, map := @LazyList.map } instance : Alternative LazyList := { failure := λ _, nil, orelse := @LazyList.append, .. LazyList.isMonad } partial def approx : Nat → LazyList α → List α | 0 as := [] | _ nil := [] | (i+1) (cons a as) := a :: approx i as | (i+1) (delayed as) := approx (i+1) as.get @[specialize] partial def iterate (f : α → α) : α → LazyList α | x := cons x (delayed (iterate (f x))) @[specialize] partial def iterate₂ (f : α → α → α) : α → α → LazyList α | x y := cons x (delayed (iterate₂ y (f x y))) @[specialize] partial def filter (p : α → Bool) : LazyList α → LazyList α | nil := nil | (cons a as) := if p a then cons a (delayed (filter as)) else filter as | (delayed as) := filter as.get partial def cycle : LazyList α → LazyList α | xs := xs ++ delayed (cycle xs) partial def repeat : α → LazyList α | a := cons a (delayed (repeat a)) partial def inits : LazyList α → LazyList (LazyList α) | nil := cons nil nil | (cons a as) := cons nil (delayed (map (λ as, cons a as) (inits as))) | (delayed as) := inits as.get private def addOpenBracket (s : String) : String := if s.isEmpty then "[" else s partial def approxToStringAux [ToString α] : Nat → LazyList α → String → String | _ nil r := (if r.isEmpty then "[" else r) ++ "]" | 0 _ r := (if r.isEmpty then "[" else r) ++ ", ..]" | (n+1) (cons a as) r := approxToStringAux n as ((if r.isEmpty then "[" else r ++ ", ") ++ toString a) | n (delayed as) r := approxToStringAux n as.get r def approxToString [ToString α] (as : LazyList α) (n : Nat := 10) : String := as.approxToStringAux n "" instance [ToString α] : ToString (LazyList α) := ⟨approxToString⟩ end LazyList def fib : LazyList Nat := LazyList.iterate₂ (+) 0 1 def tst : LazyList String := do x ← [1, 2, 3].toLazy, y ← [2, 3, 4].toLazy, -- dbgTrace (toString x ++ " " ++ toString y) $ λ _, guard (x + y > 5), pure (toString x ++ " + " ++ toString y ++ " = " ++ toString (x+y)) open LazyList def iota (i : UInt32 := 0) : LazyList UInt32 := iterate (+1) i set_option pp.implicit true set_option trace.compiler.ir.result true partial def sieve : LazyList UInt32 → LazyList UInt32 | nil := nil | (cons a as) := cons a (delayed (sieve (filter (λ b, b % a != 0) as))) | (delayed as) := sieve as.get partial def primes : LazyList UInt32 := sieve (iota 2) def main : IO Unit := do let n := 10, -- IO.println $ tst.isEmpty, -- IO.println $ [1, 2, 3].toLazy.cycle, -- IO.println $ [1, 2, 3].toLazy.cycle.inits, -- IO.println $ ((iota.filter (λ v, v % 5 == 0)).approx 50000).foldl (+) 0, IO.println $ (primes.approx 2000).foldl (+) 0, -- IO.println $ tst.head, -- IO.println $ fib.interleave (iota.map (+100)), -- IO.println $ ((iota.map (+10)).filter (λ v, v % 2 == 0)), pure ()
f8052f34a46ed9b6ccf51c6c5902d8c49331032e
bdb33f8b7ea65f7705fc342a178508e2722eb851
/ring_theory/ideals.lean
2d7b691d70cafaa62a8347a042c551a602ddcecf
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
2,963
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.module universe u class is_ideal {α : Type u} [comm_ring α] (S : set α) extends is_submodule S : Prop class is_proper_ideal {α : Type u} [comm_ring α] (S : set α) extends is_ideal S : Prop := (ne_univ : S ≠ set.univ) class is_prime_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := (mem_or_mem_of_mul_mem : ∀ {x y : α}, x * y ∈ S → x ∈ S ∨ y ∈ S) theorem mem_or_mem_of_mul_eq_zero {α : Type u} [comm_ring α] (S : set α) [is_prime_ideal S] : ∀ {x y : α}, x * y = 0 → x ∈ S ∨ y ∈ S := λ x y hxy, have x * y ∈ S, by rw hxy; from (@is_submodule.zero α α _ _ S _ : (0:α) ∈ S), is_prime_ideal.mem_or_mem_of_mul_mem this class is_maximal_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := mk' :: (eq_or_univ_of_subset : ∀ (T : set α) [is_submodule T], S ⊆ T → T = S ∨ T = set.univ) theorem is_maximal_ideal.mk {α : Type u} [comm_ring α] (S : set α) [is_submodule S] (h₁ : (1:α) ∉ S) (h₂ : ∀ x (T : set α) [is_submodule T], S ⊆ T → x ∉ S → x ∈ T → (1:α) ∈ T) : is_maximal_ideal S := { ne_univ := assume hu, have (1:α) ∈ S, by rw hu; trivial, h₁ this, eq_or_univ_of_subset := assume T ht hst, classical.or_iff_not_imp_left.2 $ assume (hnst : T ≠ S), let ⟨x, hxt, hxns⟩ := set.exists_of_ssubset ⟨hst, hnst.symm⟩ in @@is_submodule.univ_of_one_mem _ T ht $ @@h₂ x T ht hst hxns hxt} def nonunits (α : Type u) [monoid α] : set α := { x | ¬∃ y, y * x = 1 } theorem not_unit_of_mem_maximal_ideal {α : Type u} [comm_ring α] (S : set α) [is_maximal_ideal S] : S ⊆ nonunits α := λ x hx ⟨y, hxy⟩, is_proper_ideal.ne_univ S $ is_submodule.eq_univ_of_contains_unit S x y hx hxy class local_ring (α : Type u) [comm_ring α] := (S : set α) (max : is_maximal_ideal S) (unique : ∀ T [is_maximal_ideal T], S = T) def local_of_nonunits_ideal {α : Type u} [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := have hi : is_submodule (nonunits α), from { zero_ := λ ⟨y, hy⟩, hnze $ by simpa using hy, add_ := h, smul := λ x y hy ⟨z, hz⟩, hy ⟨x * z, by rw [← hz]; simp [mul_left_comm, mul_assoc]⟩ }, { S := nonunits α, max := @@is_maximal_ideal.mk _ (nonunits α) hi (λ ho, ho ⟨1, mul_one 1⟩) $ λ x T ht hst hxns hxt, let ⟨y, hxy⟩ := classical.by_contradiction hxns in by rw [← hxy]; exact @@is_submodule.smul _ _ ht y hxt, unique := λ T hmt, or.cases_on (@@is_maximal_ideal.eq_or_univ_of_subset _ hmt (nonunits α) hi $ λ z hz, @@not_unit_of_mem_maximal_ideal _ T hmt hz) id (λ htu, false.elim $ ((set.set_eq_def _ _).1 htu 1).2 trivial ⟨1, mul_one 1⟩) }
fcd29b8b2051506be18107f0d21948fbaf4ff336
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/filter/filter_product.lean
584dc1f0c727e1f888adb7562a6cff74f2136e28
[ "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
5,067
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir, Yury Kudryashov -/ import order.filter.ultrafilter import order.filter.germ /-! # Ultraproducts If `φ` is an ultrafilter, then the space of germs of functions `f : α → β` at `φ` is called the *ultraproduct*. In this file we prove properties of ultraproducts that rely on `φ` being an ultrafilter. Definitions and properties that work for any filter should go to `order.filter.germ`. ## Tags ultrafilter, ultraproduct -/ universes u v variables {α : Type u} {β : Type v} {φ : ultrafilter α} open_locale classical namespace filter local notation `∀*` binders `, ` r:(scoped p, filter.eventually p φ) := r namespace germ open ultrafilter local notation `β*` := germ (φ : filter α) β /-- If `φ` is an ultrafilter then the ultraproduct is a division ring. -/ instance [division_ring β] : division_ring β* := { mul_inv_cancel := λ f, induction_on f $ λ f hf, coe_eq.2 $ (φ.em (λ y, f y = 0)).elim (λ H, (hf $ coe_eq.2 H).elim) (λ H, H.mono $ λ x, mul_inv_cancel), inv_zero := coe_eq.2 $ by simp only [(∘), inv_zero], .. germ.ring, .. germ.div_inv_monoid, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a field. -/ instance [field β] : field β* := { .. germ.comm_ring, .. germ.division_ring } /-- If `φ` is an ultrafilter then the ultraproduct is a linear order. -/ noncomputable instance [linear_order β] : linear_order β* := { le_total := λ f g, induction_on₂ f g $ λ f g, eventually_or.1 $ eventually_of_forall $ λ x, le_total _ _, decidable_le := by apply_instance, .. germ.partial_order } @[simp, norm_cast] lemma const_div [division_ring β] (x y : β) : (↑(x / y) : β*) = ↑x / ↑y := rfl lemma coe_lt [preorder β] {f g : α → β} : (f : β*) < g ↔ ∀* x, f x < g x := by simp only [lt_iff_le_not_le, eventually_and, coe_le, eventually_not, eventually_le] lemma coe_pos [preorder β] [has_zero β] {f : α → β} : 0 < (f : β*) ↔ ∀* x, 0 < f x := coe_lt lemma const_lt [preorder β] {x y : β} : (↑x : β*) < ↑y ↔ x < y := coe_lt.trans lift_rel_const_iff lemma lt_def [preorder β] : ((<) : β* → β* → Prop) = lift_rel (<) := by { ext ⟨f⟩ ⟨g⟩, exact coe_lt } /-- If `φ` is an ultrafilter then the ultraproduct is an ordered ring. -/ instance [ordered_ring β] : ordered_ring β* := { zero_le_one := const_le zero_le_one, mul_pos := λ x y, induction_on₂ x y $ λ f g hf hg, coe_pos.2 $ (coe_pos.1 hg).mp $ (coe_pos.1 hf).mono $ λ x, mul_pos, .. germ.ring, .. germ.ordered_add_comm_group, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered ring. -/ noncomputable instance [linear_ordered_ring β] : linear_ordered_ring β* := { .. germ.ordered_ring, .. germ.linear_order, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered field. -/ noncomputable instance [linear_ordered_field β] : linear_ordered_field β* := { .. germ.linear_ordered_ring, .. germ.field } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered commutative ring. -/ noncomputable instance [linear_ordered_comm_ring β] : linear_ordered_comm_ring β* := { .. germ.linear_ordered_ring, .. germ.comm_monoid } /-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear ordered commutative group. -/ noncomputable instance [linear_ordered_add_comm_group β] : linear_ordered_add_comm_group β* := { .. germ.ordered_add_comm_group, .. germ.linear_order } lemma max_def [linear_order β] (x y : β*) : max x y = map₂ max x y := induction_on₂ x y $ λ a b, begin cases le_total (a : β*) b, { rw [max_eq_right h, map₂_coe, coe_eq], exact h.mono (λ i hi, (max_eq_right hi).symm) }, { rw [max_eq_left h, map₂_coe, coe_eq], exact h.mono (λ i hi, (max_eq_left hi).symm) } end lemma min_def [K : linear_order β] (x y : β*) : min x y = map₂ min x y := induction_on₂ x y $ λ a b, begin cases le_total (a : β*) b, { rw [min_eq_left h, map₂_coe, coe_eq], exact h.mono (λ i hi, (min_eq_left hi).symm) }, { rw [min_eq_right h, map₂_coe, coe_eq], exact h.mono (λ i hi, (min_eq_right hi).symm) } end lemma abs_def [linear_ordered_add_comm_group β] (x : β*) : |x| = map abs x := induction_on x $ λ a, by exact rfl @[simp] lemma const_max [linear_order β] (x y : β) : (↑(max x y : β) : β*) = max ↑x ↑y := by rw [max_def, map₂_const] @[simp] lemma const_min [linear_order β] (x y : β) : (↑(min x y : β) : β*) = min ↑x ↑y := by rw [min_def, map₂_const] @[simp] lemma const_abs [linear_ordered_add_comm_group β] (x : β) : (↑(|x|) : β*) = |↑x| := by rw [abs_def, map_const] lemma lattice_of_linear_order_eq_filter_germ_lattice [linear_order β] : (@lattice_of_linear_order (filter.germ ↑φ β) filter.germ.linear_order) = filter.germ.lattice := lattice.ext (λ x y, iff.rfl) end germ end filter
ac30d071486d1fac68fa97279de3b4a2e28b4dd2
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/src/uexp/rules/cse344.lean
1daf1d667968a3fbfb9ea01a5a45786911293588
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
1,470
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..meta.cosette_tactics open Expr open Proj open Pred open SQL section parameter uid : datatype parameter uname : datatype parameter size : datatype parameter city : datatype parameter pid : datatype parameter Usr : Schema parameter usr : relation Usr parameter usrUid : Column uid Usr parameter usrUname : Column uname Usr parameter usrCity : Column city Usr parameter Pic : Schema parameter pic : relation Pic parameter picUid : Column uid Pic parameter picSize : Column size Pic parameter picPid : Column pid Pic parameter denver : const city parameter denverNonNull : null ≠ denver parameter gt1000000 : Pred (tree.leaf size) parameter gt3000000 : Pred (tree.leaf size) parameter Γ : Schema definition x'' : Proj (Γ ++ Usr) Usr := right noncomputable definition x''' : Proj ((Γ ++ Usr) ++ Pic) Usr := left ⋅ right noncomputable definition y'' : Proj ((Γ ++ Usr) ++ Pic) Pic := right noncomputable definition x'''' : Proj (Γ ++ (Usr ++ Pic)) Usr := right ⋅ left noncomputable definition y'''' : Proj (Γ ++ (Usr ++ Pic)) Pic := right ⋅ right noncomputable definition leftOuterJoin {s0 s1 : Schema} (q0 : SQL Γ s0) (q1 : SQL Γ s1) (b : Pred (Γ ++ (s0 ++ s1))) : SQL Γ (s0 ++ s1) := SELECT * (FROM2 q0, q1) WHERE b -- Precedence? local notation q0 `LEFT` `OUTER` `JOIN` q1 `ON` b := leftOuterJoin q0 q1 b theorem problem3 : sorry := sorry end
b5413f70460239321f351362120a3579838e2f05
9a0b1b3a653ea926b03d1495fef64da1d14b3174
/tidy/rewrite_search/tracer/unit.lean
c8cd8b8168083370a7464b07ccc4eb39567101b9
[ "Apache-2.0" ]
permissive
khoek/mathlib-tidy
8623b27b4e04e7d598164e7eaf248610d58f768b
866afa6ab597c47f1b72e8fe2b82b97fff5b980f
refs/heads/master
1,585,598,975,772
1,538,659,544,000
1,538,659,544,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,138
lean
import tidy.rewrite_search.core open tidy.rewrite_search namespace tidy.rewrite_search.tracer.unit open tactic meta def unit_tracer_init : tactic (init_result unit) := init_result.pure () meta def unit_tracer_publish_vertex (_ : unit) (_ : vertex) : tactic unit := skip meta def unit_tracer_publish_edge (_ : unit) (_ : edge) : tactic unit := skip meta def unit_tracer_publish_visited (_ : unit) (_ : vertex) : tactic unit := skip meta def unit_tracer_publish_finished (_ : unit) (_ : list edge) : tactic unit := skip meta def unit_tracer_dump (_ : unit) (_ : string) : tactic unit := skip meta def unit_tracer_pause (_ : unit) : tactic unit := skip end tidy.rewrite_search.tracer.unit namespace tidy.rewrite_search.tracer open tidy.rewrite_search.tracer.unit meta def unit_tracer : tracer_constructor unit := λ α β γ, tracer.mk α β γ unit_tracer_init unit_tracer_publish_vertex unit_tracer_publish_edge unit_tracer_publish_visited unit_tracer_publish_finished unit_tracer_dump unit_tracer_pause meta def no {δ : Type} (_ : tracer_constructor δ) : tracer_constructor unit := unit_tracer end tidy.rewrite_search.tracer
30a7b96801d4adc15d48f1e17f87f6d3c0b4c72b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/compiler/lazylist.lean
2b094538606d5794e34220261a278f3533eeb8b2
[ "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
3,907
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universe u v w inductive LazyList (α : Type u) | nil : LazyList α | cons (hd : α) (tl : LazyList α) : LazyList α | delayed (t : Thunk $ LazyList α) : LazyList α @[extern c inline "#2"] def List.toLazy {α : Type u} : List α → LazyList α | [] => LazyList.nil | h::t => LazyList.cons h (toLazy t) namespace LazyList variable {α : Type u} {β : Type v} {δ : Type w} instance : Inhabited (LazyList α) := ⟨nil⟩ @[inline] def pure : α → LazyList α | a => cons a nil partial def isEmpty : LazyList α → Bool | nil => true | cons _ _ => false | delayed as => isEmpty as.get partial def toList : LazyList α → List α | nil => [] | cons a as => a :: toList as | delayed as => toList as.get partial def head [Inhabited α] : LazyList α → α | nil => arbitrary | cons a as => a | delayed as => head as.get partial def tail : LazyList α → LazyList α | nil => nil | cons a as => as | delayed as => tail as.get partial def append : LazyList α → LazyList α → LazyList α | nil, bs => bs | cons a as, bs => delayed (cons a (append as bs)) | delayed as, bs => delayed (append as.get bs) instance : Append (LazyList α) := ⟨LazyList.append⟩ partial def interleave : LazyList α → LazyList α → LazyList α | nil, bs => bs | cons a as, bs => delayed (cons a (interleave bs as)) | delayed as, bs => delayed (interleave as.get bs) partial def map (f : α → β) : LazyList α → LazyList β | nil => nil | cons a as => delayed (cons (f a) (map f as)) | delayed as => delayed (map f as.get) partial def map₂ (f : α → β → δ) : LazyList α → LazyList β → LazyList δ | nil, _ => nil | _, nil => nil | cons a as, cons b bs => delayed (cons (f a b) (map₂ f as bs)) | delayed as, bs => delayed (map₂ f as.get bs) | as, delayed bs => delayed (map₂ f as bs.get) @[inline] def zip : LazyList α → LazyList β → LazyList (α × β) := map₂ Prod.mk partial def join : LazyList (LazyList α) → LazyList α | nil => nil | cons a as => delayed (append a (join as)) | delayed as => delayed (join as.get) @[inline] partial def bind (x : LazyList α) (f : α → LazyList β) : LazyList β := join (x.map f) instance isMonad : Monad LazyList := { pure := @LazyList.pure, bind := @LazyList.bind, map := @LazyList.map } instance : Alternative LazyList := { LazyList.isMonad with failure := nil, orElse := LazyList.append } partial def approx : Nat → LazyList α → List α | 0, as => [] | _, nil => [] | i+1, cons a as => a :: approx i as | i+1, delayed as => approx (i+1) as.get partial def iterate (f : α → α) : α → LazyList α | x => cons x (delayed (iterate f (f x))) partial def iterate₂ (f : α → α → α) : α → α → LazyList α | x, y => cons x (delayed (iterate₂ f y (f x y))) partial def filter (p : α → Bool) : LazyList α → LazyList α | nil => nil | cons a as => delayed (if p a then cons a (filter p as) else filter p as) | delayed as => delayed (filter p as.get) end LazyList def fib : LazyList Nat := LazyList.iterate₂ (· + ·) 0 1 def iota (i : Nat := 0) : LazyList Nat := LazyList.iterate Nat.succ i def tst : LazyList String := do let x ← [1, 2, 3].toLazy; let y ← [2, 3, 4].toLazy; guard (x + y > 5); pure s!"{x} + {y} = {x+y}" def main : IO Unit := do let n := 40; IO.println tst.isEmpty; IO.println tst.head; IO.println <| fib.interleave (iota.map (· + 100)) |>.approx n; IO.println <| iota.map (· + 10) |>.filter (· % 2 == 0) |>.approx n
99333088f741f37fb3517bed9e293799ef9de3b8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/uniform_space/equiv.lean
7103f02e8ca903b46ba697bf89111c6d82d0cf7d
[ "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
12,278
lean
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Sébastien Gouëzel, Zhouhang Zhou, Reid Barton, Anatole Dedecker -/ import topology.homeomorph import topology.uniform_space.uniform_embedding import topology.uniform_space.pi /-! # Uniform isomorphisms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines uniform isomorphisms between two uniform spaces. They are bijections with both directions uniformly continuous. We denote uniform isomorphisms with the notation `≃ᵤ`. # Main definitions * `uniform_equiv α β`: The type of uniform isomorphisms from `α` to `β`. This type can be denoted using the following notation: `α ≃ᵤ β`. -/ open set filter open_locale universes u v variables {α : Type u} {β : Type*} {γ : Type*} {δ : Type*} /-- Uniform isomorphism between `α` and `β` -/ @[nolint has_nonempty_instance] -- not all spaces are homeomorphic to each other structure uniform_equiv (α : Type*) (β : Type*) [uniform_space α] [uniform_space β] extends α ≃ β := (uniform_continuous_to_fun : uniform_continuous to_fun) (uniform_continuous_inv_fun : uniform_continuous inv_fun) infix ` ≃ᵤ `:25 := uniform_equiv namespace uniform_equiv variables [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] instance : has_coe_to_fun (α ≃ᵤ β) (λ _, α → β) := ⟨λe, e.to_equiv⟩ @[simp] lemma uniform_equiv_mk_coe (a : equiv α β) (b c) : ((uniform_equiv.mk a b c) : α → β) = a := rfl /-- Inverse of a uniform isomorphism. -/ protected def symm (h : α ≃ᵤ β) : β ≃ᵤ α := { uniform_continuous_to_fun := h.uniform_continuous_inv_fun, uniform_continuous_inv_fun := h.uniform_continuous_to_fun, to_equiv := h.to_equiv.symm } /-- 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 uniform_equiv (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) @[simp] lemma coe_to_equiv (h : α ≃ᵤ β) : ⇑h.to_equiv = h := rfl @[simp] lemma coe_symm_to_equiv (h : α ≃ᵤ β) : ⇑h.to_equiv.symm = h.symm := rfl lemma to_equiv_injective : function.injective (to_equiv : α ≃ᵤ β → α ≃ β) | ⟨e, h₁, h₂⟩ ⟨e', h₁', h₂'⟩ rfl := rfl @[ext] lemma ext {h h' : α ≃ᵤ β} (H : ∀ x, h x = h' x) : h = h' := to_equiv_injective $ equiv.ext H /-- Identity map as a uniform isomorphism. -/ @[simps apply {fully_applied := ff}] protected def refl (α : Type*) [uniform_space α] : α ≃ᵤ α := { uniform_continuous_to_fun := uniform_continuous_id, uniform_continuous_inv_fun := uniform_continuous_id, to_equiv := equiv.refl α } /-- Composition of two uniform isomorphisms. -/ protected def trans (h₁ : α ≃ᵤ β) (h₂ : β ≃ᵤ γ) : α ≃ᵤ γ := { uniform_continuous_to_fun := h₂.uniform_continuous_to_fun.comp h₁.uniform_continuous_to_fun, uniform_continuous_inv_fun := h₁.uniform_continuous_inv_fun.comp h₂.uniform_continuous_inv_fun, to_equiv := equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ᵤ β) (h₂ : β ≃ᵤ γ) (a : α) : h₁.trans h₂ a = h₂ (h₁ a) := rfl @[simp] lemma uniform_equiv_mk_coe_symm (a : equiv α β) (b c) : ((uniform_equiv.mk a b c).symm : β → α) = a.symm := rfl @[simp] lemma refl_symm : (uniform_equiv.refl α).symm = uniform_equiv.refl α := rfl protected lemma uniform_continuous (h : α ≃ᵤ β) : uniform_continuous h := h.uniform_continuous_to_fun @[continuity] protected lemma continuous (h : α ≃ᵤ β) : continuous h := h.uniform_continuous.continuous protected lemma uniform_continuous_symm (h : α ≃ᵤ β) : uniform_continuous (h.symm) := h.uniform_continuous_inv_fun @[continuity] -- otherwise `by continuity` can't prove continuity of `h.to_equiv.symm` protected lemma continuous_symm (h : α ≃ᵤ β) : continuous (h.symm) := h.uniform_continuous_symm.continuous /-- A uniform isomorphism as a homeomorphism. -/ @[simps] protected def to_homeomorph (e : α ≃ᵤ β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.continuous_symm, .. e.to_equiv } @[simp] lemma apply_symm_apply (h : α ≃ᵤ β) (x : β) : h (h.symm x) = x := h.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (h : α ≃ᵤ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x protected lemma bijective (h : α ≃ᵤ β) : function.bijective h := h.to_equiv.bijective protected lemma injective (h : α ≃ᵤ β) : function.injective h := h.to_equiv.injective protected lemma surjective (h : α ≃ᵤ β) : function.surjective h := h.to_equiv.surjective /-- Change the uniform equiv `f` to make the inverse function definitionally equal to `g`. -/ def change_inv (f : α ≃ᵤ β) (g : β → α) (hg : function.right_inverse g f) : α ≃ᵤ β := have g = f.symm, from funext (λ x, calc g x = f.symm (f (g x)) : (f.left_inv (g x)).symm ... = f.symm x : by rw hg x), { to_fun := f, inv_fun := g, left_inv := by convert f.left_inv, right_inv := by convert f.right_inv, uniform_continuous_to_fun := f.uniform_continuous, uniform_continuous_inv_fun := by convert f.symm.uniform_continuous } @[simp] lemma symm_comp_self (h : α ≃ᵤ β) : ⇑h.symm ∘ ⇑h = id := funext h.symm_apply_apply @[simp] lemma self_comp_symm (h : α ≃ᵤ β) : ⇑h ∘ ⇑h.symm = id := funext h.apply_symm_apply @[simp] lemma range_coe (h : α ≃ᵤ β) : range h = univ := h.surjective.range_eq lemma image_symm (h : α ≃ᵤ β) : image h.symm = preimage h := funext h.symm.to_equiv.image_eq_preimage lemma preimage_symm (h : α ≃ᵤ β) : preimage h.symm = image h := (funext h.to_equiv.image_eq_preimage).symm @[simp] lemma image_preimage (h : α ≃ᵤ β) (s : set β) : h '' (h ⁻¹' s) = s := h.to_equiv.image_preimage s @[simp] lemma preimage_image (h : α ≃ᵤ β) (s : set α) : h ⁻¹' (h '' s) = s := h.to_equiv.preimage_image s protected lemma uniform_inducing (h : α ≃ᵤ β) : uniform_inducing h := uniform_inducing_of_compose h.uniform_continuous h.symm.uniform_continuous $ by simp only [symm_comp_self, uniform_inducing_id] lemma comap_eq (h : α ≃ᵤ β) : uniform_space.comap h ‹_› = ‹_› := by ext : 1; exact h.uniform_inducing.comap_uniformity protected lemma uniform_embedding (h : α ≃ᵤ β) : uniform_embedding h := ⟨h.uniform_inducing, h.injective⟩ /-- Uniform equiv given a uniform embedding. -/ noncomputable def of_uniform_embedding (f : α → β) (hf : uniform_embedding f) : α ≃ᵤ (set.range f) := { uniform_continuous_to_fun := hf.to_uniform_inducing.uniform_continuous.subtype_mk _, uniform_continuous_inv_fun := by simp [hf.to_uniform_inducing.uniform_continuous_iff, uniform_continuous_subtype_coe], to_equiv := equiv.of_injective f hf.inj } /-- If two sets are equal, then they are uniformly equivalent. -/ def set_congr {s t : set α} (h : s = t) : s ≃ᵤ t := { uniform_continuous_to_fun := uniform_continuous_subtype_val.subtype_mk _, uniform_continuous_inv_fun := uniform_continuous_subtype_val.subtype_mk _, to_equiv := equiv.set_congr h } /-- Product of two uniform isomorphisms. -/ def prod_congr (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : α × γ ≃ᵤ β × δ := { uniform_continuous_to_fun := (h₁.uniform_continuous.comp uniform_continuous_fst).prod_mk (h₂.uniform_continuous.comp uniform_continuous_snd), uniform_continuous_inv_fun := (h₁.symm.uniform_continuous.comp uniform_continuous_fst).prod_mk (h₂.symm.uniform_continuous.comp uniform_continuous_snd), to_equiv := h₁.to_equiv.prod_congr h₂.to_equiv } @[simp] lemma prod_congr_symm (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : (h₁.prod_congr h₂).symm = h₁.symm.prod_congr h₂.symm := rfl @[simp] lemma coe_prod_congr (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : ⇑(h₁.prod_congr h₂) = prod.map h₁ h₂ := rfl section variables (α β γ) /-- `α × β` is uniformly isomorphic to `β × α`. -/ def prod_comm : α × β ≃ᵤ β × α := { uniform_continuous_to_fun := uniform_continuous_snd.prod_mk uniform_continuous_fst, uniform_continuous_inv_fun := uniform_continuous_snd.prod_mk uniform_continuous_fst, to_equiv := equiv.prod_comm α β } @[simp] lemma prod_comm_symm : (prod_comm α β).symm = prod_comm β α := rfl @[simp] lemma coe_prod_comm : ⇑(prod_comm α β) = prod.swap := rfl /-- `(α × β) × γ` is uniformly isomorphic to `α × (β × γ)`. -/ def prod_assoc : (α × β) × γ ≃ᵤ α × (β × γ) := { uniform_continuous_to_fun := (uniform_continuous_fst.comp uniform_continuous_fst).prod_mk ((uniform_continuous_snd.comp uniform_continuous_fst).prod_mk uniform_continuous_snd), uniform_continuous_inv_fun := (uniform_continuous_fst.prod_mk (uniform_continuous_fst.comp uniform_continuous_snd)).prod_mk (uniform_continuous_snd.comp uniform_continuous_snd), to_equiv := equiv.prod_assoc α β γ } /-- `α × {*}` is uniformly isomorphic to `α`. -/ @[simps apply {fully_applied := ff}] def prod_punit : α × punit ≃ᵤ α := { to_equiv := equiv.prod_punit α, uniform_continuous_to_fun := uniform_continuous_fst, uniform_continuous_inv_fun := uniform_continuous_id.prod_mk uniform_continuous_const } /-- `{*} × α` is uniformly isomorphic to `α`. -/ def punit_prod : punit × α ≃ᵤ α := (prod_comm _ _).trans (prod_punit _) @[simp] lemma coe_punit_prod : ⇑(punit_prod α) = prod.snd := rfl /-- Uniform equivalence between `ulift α` and `α`. -/ def ulift : ulift.{v u} α ≃ᵤ α := { uniform_continuous_to_fun := uniform_continuous_comap, uniform_continuous_inv_fun := begin have hf : uniform_inducing (@equiv.ulift.{v u} α).to_fun, from ⟨rfl⟩, simp_rw [hf.uniform_continuous_iff], exact uniform_continuous_id, end, .. equiv.ulift } end /-- If `ι` has a unique element, then `ι → α` is homeomorphic to `α`. -/ @[simps { fully_applied := ff }] def fun_unique (ι α : Type*) [unique ι] [uniform_space α] : (ι → α) ≃ᵤ α := { to_equiv := equiv.fun_unique ι α, uniform_continuous_to_fun := Pi.uniform_continuous_proj _ _, uniform_continuous_inv_fun := uniform_continuous_pi.mpr (λ _, uniform_continuous_id) } /-- Uniform isomorphism between dependent functions `Π i : fin 2, α i` and `α 0 × α 1`. -/ @[simps { fully_applied := ff }] def pi_fin_two (α : fin 2 → Type u) [Π i, uniform_space (α i)] : (Π i, α i) ≃ᵤ α 0 × α 1 := { to_equiv := pi_fin_two_equiv α, uniform_continuous_to_fun := (Pi.uniform_continuous_proj _ 0).prod_mk (Pi.uniform_continuous_proj _ 1), uniform_continuous_inv_fun := uniform_continuous_pi.mpr $ fin.forall_fin_two.2 ⟨uniform_continuous_fst, uniform_continuous_snd⟩ } /-- Uniform isomorphism between `α² = fin 2 → α` and `α × α`. -/ @[simps { fully_applied := ff }] def fin_two_arrow : (fin 2 → α) ≃ᵤ α × α := { to_equiv := fin_two_arrow_equiv α, .. pi_fin_two (λ _, α) } /-- A subset of a uniform space is uniformly isomorphic to its image under a uniform isomorphism. -/ def image (e : α ≃ᵤ β) (s : set α) : s ≃ᵤ e '' s := { uniform_continuous_to_fun := (e.uniform_continuous.comp uniform_continuous_subtype_val).subtype_mk _, uniform_continuous_inv_fun := (e.symm.uniform_continuous.comp uniform_continuous_subtype_val).subtype_mk _, to_equiv := e.to_equiv.image s } end uniform_equiv /-- A uniform inducing equiv between uniform spaces is a uniform isomorphism. -/ @[simps] def equiv.to_uniform_equiv_of_uniform_inducing [uniform_space α] [uniform_space β] (f : α ≃ β) (hf : uniform_inducing f) : α ≃ᵤ β := { uniform_continuous_to_fun := hf.uniform_continuous, uniform_continuous_inv_fun := hf.uniform_continuous_iff.2 $ by simpa using uniform_continuous_id, .. f }
765e288a191869ca32047f5cb63c519dd408b0fb
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/algebra/category/natural_transformation.lean
f10aedf51205439176ccca591012cf00c177b7fd
[ "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
2,397
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn -/ import .functor open category eq eq.ops functor inductive natural_transformation {C D : Category} (F G : C ⇒ D) : Type := mk : Π (η : Π(a : C), hom (F a) (G a)), (Π{a b : C} (f : hom a b), G f ∘ η a = η b ∘ F f) → natural_transformation F G infixl `⟹`:25 := natural_transformation -- \==> namespace natural_transformation variables {C D : Category} {F G H I : functor C D} -- definition natural_map [coercion] (η : F ⟹ G) : Π(a : C), F a ⟶ G a := -- natural_transformation.rec (λ x y, x) η theorem naturality (η : F ⟹ G) : Π⦃a b : C⦄ (f : a ⟶ b), G f ∘ η a = η b ∘ F f := natural_transformation.rec (λ x y, y) η protected definition compose (η : G ⟹ H) (θ : F ⟹ G) : F ⟹ H := natural_transformation.mk (λ a, η a ∘ θ a) (λ a b f, calc H f ∘ (η a ∘ θ a) = (H f ∘ η a) ∘ θ a : assoc ... = (η b ∘ G f) ∘ θ a : naturality η f ... = η b ∘ (G f ∘ θ a) : assoc ... = η b ∘ (θ b ∘ F f) : naturality θ f ... = (η b ∘ θ b) ∘ F f : assoc) --congr_arg (λx, η b ∘ x) (naturality θ f) -- this needed to be explicit for some reason (on Oct 24) infixr `∘n`:60 := natural_transformation.compose protected theorem assoc (η₃ : H ⟹ I) (η₂ : G ⟹ H) (η₁ : F ⟹ G) : η₃ ∘n (η₂ ∘n η₁) = (η₃ ∘n η₂) ∘n η₁ := dcongr_arg2 mk (funext (take x, !assoc)) !proof_irrel protected definition id {C D : Category} {F : functor C D} : natural_transformation F F := mk (λa, id) (λa b f, !id_right ⬝ symm !id_left) protected definition ID {C D : Category} (F : functor C D) : natural_transformation F F := natural_transformation.id protected theorem id_left (η : F ⟹ G) : natural_transformation.compose natural_transformation.id η = η := natural_transformation.rec (λf H, dcongr_arg2 mk (funext (take x, !id_left)) !proof_irrel) η protected theorem id_right (η : F ⟹ G) : natural_transformation.compose η natural_transformation.id = η := natural_transformation.rec (λf H, dcongr_arg2 mk (funext (take x, !id_right)) !proof_irrel) η end natural_transformation
2cc246a7db05fcd381893274bb0b666e3871bf95
6fbf10071e62af7238f2de8f9aa83d55d8763907
/examples/exists_properties.lean
6d524f5fa49ed72e720d76b18a3354c8167e842f
[]
no_license
HasanMukati/uva-cs-dm-s19
ee5aad4568a3ca330c2738ed579c30e1308b03b0
3e7177682acdb56a2d16914e0344c10335583dcf
refs/heads/master
1,596,946,213,130
1,568,221,949,000
1,568,221,949,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,913
lean
/- Introduction rule requires a witness where a predicate can be proven to be true for that witness -/ def existsIntro (T: Type) (pred: T → Prop) (witness : T) (proof : pred witness) : ∃(w: T), pred w := exists.intro witness proof example{T: Type}{witness: T} {predicate: T → Prop} {proof: predicate witness}: ∃ m, predicate m := ⟨ witness, proof ⟩ def isEven(n : nat) : Prop := ∃(m : nat), m + m = n lemma pf8is4twice: 4 + 4 = 8 := rfl theorem even8: isEven 8 := exists.intro 4 pf8is4twice theorem even8': ∃(m : nat), m + m = 8 := exists.intro 4 pf8is4twice theorem even8'': isEven 8 := ⟨ 4, pf8is4twice ⟩ theorem even8''' : isEven 8 := begin unfold isEven, -- not necessary exact ⟨ 4, pf8is4twice ⟩ end theorem isNonZ : exists n : nat, 0 ≠ n := exists.intro 1 (λ pf : (0 = 1), nat.no_confusion pf) theorem isNonZ' : exists n : nat, 0 ≠ n := begin have pf0isnt1: (0 ≠ 1), apply nat.no_confusion, exact ⟨ 1, pf0isnt1 ⟩, end theorem isNonZ'' : exists n : nat, 0 ≠ n := begin have pf0isnt1: (0 ≠ 1) := begin apply nat.no_confusion, end, exact ⟨ 1, pf0isnt1 ⟩, end def existsElim { Q : Prop } { T : Type } { P : T → Prop } ( ex : exists x, P x) ( pw2q : ∀ a : T, P a → Q) : Q := exists.elim ex pw2q theorem forgetAProperty'{P S: ℕ → Prop}: (∃ n, P n ∧ S n) → (∃ n, P n) := -- here Q, the conclusion, is -- (exists n, P n) begin assume ex, apply exists.elim ex, assume w Pw, exact ⟨w, Pw.left⟩, end theorem forgetAProperty{P S: ℕ → Prop}: (∃ n, P n ∧ S n) → (∃ n, P n) := -- here Q, the conclusion, is (exists n, P n) begin assume ex, show ∃ (n : ℕ), P n, from begin apply exists.elim ex, -- give one arg, build other assume w Pw, -- assume w and proof of P w show ∃ (n : ℕ), P n, from exists.intro w Pw.left, end, end example: ∀(P Q: Prop), P → Q → P ∧ Q := begin assume P Q, assume pfP pfQ, exact ⟨pfP, pfQ⟩, end theorem reverseProperty{P S: ℕ → Prop}: (∃ n, P n ∧ S n) → (∃ n, S n ∧ P n) := begin assume ex, apply exists.elim ex, assume w Pw, have pfWReverse: S w ∧ P w := and.intro Pw.right Pw.left, exact exists.intro w pfWReverse, end theorem reverseProperty'{P S: ℕ → Prop}: (∃ n, P n ∧ S n) → (∃ n, S n ∧ P n) := begin assume ex, apply exists.elim ex, assume w Pw, apply exists.intro w, exact and.intro Pw.right Pw.left, end theorem reverseProperty''{P S: ℕ → Prop}: (∃ n, P n ∧ S n) → (∃ n, S n ∧ P n) := begin assume ex, have pfw2q: ∀(w : ℕ), P w ∧ S w → (∃ (n : ℕ), S n ∧ P n) := begin assume w Pw, have pfWReverse: S w ∧ P w := and.intro Pw.right Pw.left, exact ⟨w, pfWReverse⟩, end, exact exists.elim ex pfw2q, end def isASquare: ℕ → Prop := λ n, exists m, n = m ^ 2 def isASquare'(n: ℕ) := ∃ m, n = m ^ 2 theorem isPS9 : isASquare 9 := begin unfold isASquare, exact exists.intro 3 (eq.refl 9), end axiom mypred: ℕ → Prop #reduce (λ n, mypred n) 3 theorem not_exists_t_iff_always_not_t {T: Type}{pred: (T → Prop)}: (¬(∃ t: T, pred(t))) ↔ ∀ t: T, ¬pred(t) := begin apply iff.intro, -- ¬(∃ t: T, pred(t)) → ∀ t: T, ¬pred(t) assume pf_not_exists_t, assume t, assume Q, have pf_exists_t := exists.intro t Q, contradiction, -- ∀ t: T, ¬pred(t) → ¬(∃ t: T, pred(t)) assume pf_forall_t_not, assume pf_exists_t, apply exists.elim pf_exists_t, assume w, assume pf_w, have pf_not_w := pf_forall_t_not w, contradiction, end example: ∃(P Q: Prop), (P ∨ Q) ∧ (¬P ∨ ¬Q) := begin apply exists.intro true, apply exists.intro false, have pf_not_f := λ(f: false), f, have pf_not_t_or_not_f := or.intro_right (¬true) pf_not_f, have pf_t_or_f := or.intro_left false true.intro, have pf_t_or_f' : true ∨ false := or.inl true.intro, exact and.intro pf_t_or_f pf_not_t_or_not_f, end example: ∃(P Q: Prop), (P ∨ Q) ∧ (¬P ∨ ¬Q) ∧ (¬P ∨ Q) := begin apply exists.intro false, apply exists.intro true, split, -- P ∨ Q exact or.inr true.intro, split, -- ¬P ∨ ¬Q apply or.inl, assume f, assumption, -- ¬P ∨ Q apply or.inr, exact true.intro, end example: ¬(∃(P Q: Prop), (P ∨ Q) ∧ (¬P ∨ ¬Q) ∧ (¬P ∨ Q) ∧ (¬Q)) := begin assume pf_exists, apply exists.elim pf_exists, assume P pw', apply exists.elim pw', assume Q pw, have pf_not_q := pw.right.right.right, have pf_not_p_or_q := pw.right.right.left, cases pf_not_p_or_q with pf_not_p pf_q, -- ¬P have pf_p_or_q := pw.left, cases pf_p_or_q with pf_p pf_q, -- P contradiction, -- Q contradiction, -- Q contradiction, end example: ∃(P Q R: Prop), (P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R) ∧ (¬P ∨ Q ∨ R) ∧ (¬Q ∨ ¬R) := begin apply exists.intro false, apply exists.intro true, apply exists.intro false, split, -- P ∨ Q ∨ R exact or.inr (or.inl true.intro), have f := λ(f: false), f, split, -- ¬P ∨ ¬Q ∨ ¬R exact or.inl f, split, -- ¬P ∨ Q ∨ R exact or.inl f, -- ¬Q ∨ ¬R exact or.inr f, end example: ∀(T: Type)(pred: T → Prop), ¬(∃(t: T), pred t) ↔ ∀(t: T), ¬(pred t) := begin intros, split, -- forward assume pf_notexists, assume t, assume pf_predt, have pf_exists := exists.intro t pf_predt, contradiction, -- backward assume pf_forall, assume pf_exists, apply exists.elim pf_exists, assume w pw, have pf_npredw := pf_forall w, contradiction, end axiom em: ∀(P: Prop), P ∨ ¬P example: ∀(T: Type)(pred: T → Prop), ¬(∀(t: T), pred t) ↔ ∃(t: T), ¬(pred t) := begin intros, split, -- forward assume pf_notforall, cases (em (∃ (t : T), ¬pred t)) with pf_exists pf_notexists, -- exists assumption, -- doesn't exist have pf_forall_doubleneg: ∀(t: T), ¬¬(pred t) := begin assume t, assume pf_neg, have pf_exists := exists.intro t pf_neg, contradiction, end, have pf_forall: ∀(t: T), pred t := begin assume t, have pf_doubleneg := pf_forall_doubleneg t, cases (em (pred t)) with pf_pred pf_npred, -- pred t assumption, -- ¬pred t contradiction, end, contradiction, -- backward assume pf_existsnot, assume pf_forall, apply exists.elim pf_existsnot, assume w pw, have pf_predw := pf_forall w, contradiction, end
6f3701c9f5c67b53e6b02be7c6d2965f933c22d2
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/analysis/filter.lean
ec7201b2df38af081ce5cf3e06eeb3b1b68fe694
[ "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
11,815
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Computational realization of filters (experimental). -/ import order.filter.cofinite open set filter /-- A `cfilter α σ` is a realization of a filter (base) on `α`, represented by a type `σ` together with operations for the top element and the binary inf operation. -/ structure cfilter (α σ : Type*) [partial_order α] := (f : σ → α) (pt : σ) (inf : σ → σ → σ) (inf_le_left : ∀ a b : σ, f (inf a b) ≤ f a) (inf_le_right : ∀ a b : σ, f (inf a b) ≤ f b) variables {α : Type*} {β : Type*} {σ : Type*} {τ : Type*} namespace cfilter section variables [partial_order α] (F : cfilter α σ) instance : has_coe_to_fun (cfilter α σ) := ⟨_, cfilter.f⟩ @[simp] theorem coe_mk (f pt inf h₁ h₂ a) : (@cfilter.mk α σ _ f pt inf h₁ h₂) a = f a := rfl /-- Map a cfilter to an equivalent representation type. -/ def of_equiv (E : σ ≃ τ) : cfilter α σ → cfilter α τ | ⟨f, p, g, h₁, h₂⟩ := { f := λ a, f (E.symm a), pt := E p, inf := λ a b, E (g (E.symm a) (E.symm b)), inf_le_left := λ a b, by simpa using h₁ (E.symm a) (E.symm b), inf_le_right := λ a b, by simpa using h₂ (E.symm a) (E.symm b) } @[simp] theorem of_equiv_val (E : σ ≃ τ) (F : cfilter α σ) (a : τ) : F.of_equiv E a = F (E.symm a) := by cases F; refl end /-- The filter represented by a `cfilter` is the collection of supersets of elements of the filter base. -/ def to_filter (F : cfilter (set α) σ) : filter α := { sets := {a | ∃ b, F b ⊆ a}, univ_sets := ⟨F.pt, subset_univ _⟩, sets_of_superset := λ x y ⟨b, h⟩ s, ⟨b, subset.trans h s⟩, inter_sets := λ x y ⟨a, h₁⟩ ⟨b, h₂⟩, ⟨F.inf a b, subset_inter (subset.trans (F.inf_le_left _ _) h₁) (subset.trans (F.inf_le_right _ _) h₂)⟩ } @[simp] theorem mem_to_filter_sets (F : cfilter (set α) σ) {a : set α} : a ∈ F.to_filter ↔ ∃ b, F b ⊆ a := iff.rfl end cfilter /-- A realizer for filter `f` is a cfilter which generates `f`. -/ structure filter.realizer (f : filter α) := (σ : Type*) (F : cfilter (set α) σ) (eq : F.to_filter = f) protected def cfilter.to_realizer (F : cfilter (set α) σ) : F.to_filter.realizer := ⟨σ, F, rfl⟩ namespace filter.realizer theorem mem_sets {f : filter α} (F : f.realizer) {a : set α} : a ∈ f ↔ ∃ b, F.F b ⊆ a := by cases F; subst f; simp -- Used because it has better definitional equalities than the eq.rec proof def of_eq {f g : filter α} (e : f = g) (F : f.realizer) : g.realizer := ⟨F.σ, F.F, F.eq.trans e⟩ /-- A filter realizes itself. -/ def of_filter (f : filter α) : f.realizer := ⟨f.sets, { f := subtype.val, pt := ⟨univ, univ_mem_sets⟩, inf := λ ⟨x, h₁⟩ ⟨y, h₂⟩, ⟨_, inter_mem_sets h₁ h₂⟩, inf_le_left := λ ⟨x, h₁⟩ ⟨y, h₂⟩, inter_subset_left x y, inf_le_right := λ ⟨x, h₁⟩ ⟨y, h₂⟩, inter_subset_right x y }, filter_eq $ set.ext $ λ x, set_coe.exists.trans exists_sets_subset_iff⟩ /-- Transfer a filter realizer to another realizer on a different base type. -/ def of_equiv {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) : f.realizer := ⟨τ, F.F.of_equiv E, by refine eq.trans _ F.eq; exact filter_eq (set.ext $ λ x, ⟨λ ⟨s, h⟩, ⟨E.symm s, by simpa using h⟩, λ ⟨t, h⟩, ⟨E t, by simp [h]⟩⟩)⟩ @[simp] theorem of_equiv_σ {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) : (F.of_equiv E).σ = τ := rfl @[simp] theorem of_equiv_F {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) (s : τ) : (F.of_equiv E).F s = F.F (E.symm s) := by delta of_equiv; simp /-- `unit` is a realizer for the principal filter -/ protected def principal (s : set α) : (principal s).realizer := ⟨unit, { f := λ _, s, pt := (), inf := λ _ _, (), inf_le_left := λ _ _, le_refl _, inf_le_right := λ _ _, le_refl _ }, filter_eq $ set.ext $ λ x, ⟨λ ⟨_, s⟩, s, λ h, ⟨(), h⟩⟩⟩ @[simp] theorem principal_σ (s : set α) : (realizer.principal s).σ = unit := rfl @[simp] theorem principal_F (s : set α) (u : unit) : (realizer.principal s).F u = s := rfl /-- `unit` is a realizer for the top filter -/ protected def top : (⊤ : filter α).realizer := (realizer.principal _).of_eq principal_univ @[simp] theorem top_σ : (@realizer.top α).σ = unit := rfl @[simp] theorem top_F (u : unit) : (@realizer.top α).F u = univ := rfl /-- `unit` is a realizer for the bottom filter -/ protected def bot : (⊥ : filter α).realizer := (realizer.principal _).of_eq principal_empty @[simp] theorem bot_σ : (@realizer.bot α).σ = unit := rfl @[simp] theorem bot_F (u : unit) : (@realizer.bot α).F u = ∅ := rfl /-- Construct a realizer for `map m f` given a realizer for `f` -/ protected def map (m : α → β) {f : filter α} (F : f.realizer) : (map m f).realizer := ⟨F.σ, { f := λ s, image m (F.F s), pt := F.F.pt, inf := F.F.inf, inf_le_left := λ a b, image_subset _ (F.F.inf_le_left _ _), inf_le_right := λ a b, image_subset _ (F.F.inf_le_right _ _) }, filter_eq $ set.ext $ λ x, by simp [cfilter.to_filter]; rw F.mem_sets; refl ⟩ @[simp] theorem map_σ (m : α → β) {f : filter α} (F : f.realizer) : (F.map m).σ = F.σ := rfl @[simp] theorem map_F (m : α → β) {f : filter α} (F : f.realizer) (s) : (F.map m).F s = image m (F.F s) := rfl /-- Construct a realizer for `comap m f` given a realizer for `f` -/ protected def comap (m : α → β) {f : filter β} (F : f.realizer) : (comap m f).realizer := ⟨F.σ, { f := λ s, preimage m (F.F s), pt := F.F.pt, inf := F.F.inf, inf_le_left := λ a b, preimage_mono (F.F.inf_le_left _ _), inf_le_right := λ a b, preimage_mono (F.F.inf_le_right _ _) }, filter_eq $ set.ext $ λ x, by cases F; subst f; simp [cfilter.to_filter, mem_comap_sets]; exact ⟨λ ⟨s, h⟩, ⟨_, ⟨s, subset.refl _⟩, h⟩, λ ⟨y, ⟨s, h⟩, h₂⟩, ⟨s, subset.trans (preimage_mono h) h₂⟩⟩⟩ /-- Construct a realizer for the sup of two filters -/ protected def sup {f g : filter α} (F : f.realizer) (G : g.realizer) : (f ⊔ g).realizer := ⟨F.σ × G.σ, { f := λ ⟨s, t⟩, F.F s ∪ G.F t, pt := (F.F.pt, G.F.pt), inf := λ ⟨a, a'⟩ ⟨b, b'⟩, (F.F.inf a b, G.F.inf a' b'), inf_le_left := λ ⟨a, a'⟩ ⟨b, b'⟩, union_subset_union (F.F.inf_le_left _ _) (G.F.inf_le_left _ _), inf_le_right := λ ⟨a, a'⟩ ⟨b, b'⟩, union_subset_union (F.F.inf_le_right _ _) (G.F.inf_le_right _ _) }, filter_eq $ set.ext $ λ x, by cases F; cases G; substs f g; simp [cfilter.to_filter]; exact ⟨λ ⟨s, t, h⟩, ⟨⟨s, subset.trans (subset_union_left _ _) h⟩, ⟨t, subset.trans (subset_union_right _ _) h⟩⟩, λ ⟨⟨s, h₁⟩, ⟨t, h₂⟩⟩, ⟨s, t, union_subset h₁ h₂⟩⟩⟩ /-- Construct a realizer for the inf of two filters -/ protected def inf {f g : filter α} (F : f.realizer) (G : g.realizer) : (f ⊓ g).realizer := ⟨F.σ × G.σ, { f := λ ⟨s, t⟩, F.F s ∩ G.F t, pt := (F.F.pt, G.F.pt), inf := λ ⟨a, a'⟩ ⟨b, b'⟩, (F.F.inf a b, G.F.inf a' b'), inf_le_left := λ ⟨a, a'⟩ ⟨b, b'⟩, inter_subset_inter (F.F.inf_le_left _ _) (G.F.inf_le_left _ _), inf_le_right := λ ⟨a, a'⟩ ⟨b, b'⟩, inter_subset_inter (F.F.inf_le_right _ _) (G.F.inf_le_right _ _) }, filter_eq $ set.ext $ λ x, by cases F; cases G; substs f g; simp [cfilter.to_filter]; exact ⟨λ ⟨s, t, h⟩, ⟨_, ⟨s, subset.refl _⟩, _, ⟨t, subset.refl _⟩, h⟩, λ ⟨y, ⟨s, h₁⟩, z, ⟨t, h₂⟩, h⟩, ⟨s, t, subset.trans (inter_subset_inter h₁ h₂) h⟩⟩⟩ /-- Construct a realizer for the cofinite filter -/ protected def cofinite [decidable_eq α] : (@cofinite α).realizer := ⟨finset α, { f := λ s, {a | a ∉ s}, pt := ∅, inf := (∪), inf_le_left := λ s t a, mt (finset.mem_union_left _), inf_le_right := λ s t a, mt (finset.mem_union_right _) }, filter_eq $ set.ext $ λ x, ⟨λ ⟨s, h⟩, s.finite_to_set.subset (compl_subset_comm.1 h), λ ⟨fs⟩, by exactI ⟨xᶜ.to_finset, λ a (h : a ∉ xᶜ.to_finset), classical.by_contradiction $ λ h', h (mem_to_finset.2 h')⟩⟩⟩ /-- Construct a realizer for filter bind -/ protected def bind {f : filter α} {m : α → filter β} (F : f.realizer) (G : ∀ i, (m i).realizer) : (f.bind m).realizer := ⟨Σ s : F.σ, Π i ∈ F.F s, (G i).σ, { f := λ ⟨s, f⟩, ⋃ i ∈ F.F s, (G i).F (f i H), pt := ⟨F.F.pt, λ i H, (G i).F.pt⟩, inf := λ ⟨a, f⟩ ⟨b, f'⟩, ⟨F.F.inf a b, λ i h, (G i).F.inf (f i (F.F.inf_le_left _ _ h)) (f' i (F.F.inf_le_right _ _ h))⟩, inf_le_left := λ ⟨a, f⟩ ⟨b, f'⟩ x, show (x ∈ ⋃ (i : α) (H : i ∈ F.F (F.F.inf a b)), _) → x ∈ ⋃ i (H : i ∈ F.F a), ((G i).F) (f i H), by simp; exact λ i h₁ h₂, ⟨i, F.F.inf_le_left _ _ h₁, (G i).F.inf_le_left _ _ h₂⟩, inf_le_right := λ ⟨a, f⟩ ⟨b, f'⟩ x, show (x ∈ ⋃ (i : α) (H : i ∈ F.F (F.F.inf a b)), _) → x ∈ ⋃ i (H : i ∈ F.F b), ((G i).F) (f' i H), by simp; exact λ i h₁ h₂, ⟨i, F.F.inf_le_right _ _ h₁, (G i).F.inf_le_right _ _ h₂⟩ }, filter_eq $ set.ext $ λ x, by cases F with _ F _; subst f; simp [cfilter.to_filter, mem_bind_sets]; exact ⟨λ ⟨s, f, h⟩, ⟨F s, ⟨s, subset.refl _⟩, λ i H, (G i).mem_sets.2 ⟨f i H, λ a h', h ⟨_, ⟨i, rfl⟩, _, ⟨H, rfl⟩, h'⟩⟩⟩, λ ⟨y, ⟨s, h⟩, f⟩, let ⟨f', h'⟩ := classical.axiom_of_choice (λ i:F s, (G i).mem_sets.1 (f i (h i.2))) in ⟨s, λ i h, f' ⟨i, h⟩, λ a ⟨_, ⟨i, rfl⟩, _, ⟨H, rfl⟩, m⟩, h' ⟨_, H⟩ m⟩⟩⟩ /-- Construct a realizer for indexed supremum -/ protected def Sup {f : α → filter β} (F : ∀ i, (f i).realizer) : (⨆ i, f i).realizer := let F' : (⨆ i, f i).realizer := ((realizer.bind realizer.top F).of_eq $ filter_eq $ set.ext $ by simp [filter.bind, eq_univ_iff_forall, supr_sets_eq]) in F'.of_equiv $ show (Σ u:unit, Π (i : α), true → (F i).σ) ≃ Π i, (F i).σ, from ⟨λ⟨_,f⟩ i, f i ⟨⟩, λ f, ⟨(), λ i _, f i⟩, λ ⟨⟨⟩, f⟩, by dsimp; congr; simp, λ f, rfl⟩ /-- Construct a realizer for the product of filters -/ protected def prod {f g : filter α} (F : f.realizer) (G : g.realizer) : (f.prod g).realizer := (F.comap _).inf (G.comap _) theorem le_iff {f g : filter α} (F : f.realizer) (G : g.realizer) : f ≤ g ↔ ∀ b : G.σ, ∃ a : F.σ, F.F a ≤ G.F b := ⟨λ H t, F.mem_sets.1 (H (G.mem_sets.2 ⟨t, subset.refl _⟩)), λ H x h, F.mem_sets.2 $ let ⟨s, h₁⟩ := G.mem_sets.1 h, ⟨t, h₂⟩ := H s in ⟨t, subset.trans h₂ h₁⟩⟩ theorem tendsto_iff (f : α → β) {l₁ : filter α} {l₂ : filter β} (L₁ : l₁.realizer) (L₂ : l₂.realizer) : tendsto f l₁ l₂ ↔ ∀ b, ∃ a, ∀ x ∈ L₁.F a, f x ∈ L₂.F b := (le_iff (L₁.map f) L₂).trans $ forall_congr $ λ b, exists_congr $ λ a, image_subset_iff theorem ne_bot_iff {f : filter α} (F : f.realizer) : f ≠ ⊥ ↔ ∀ a : F.σ, (F.F a).nonempty := begin classical, rw [not_iff_comm, ← le_bot_iff, F.le_iff realizer.bot, not_forall], simp only [set.not_nonempty_iff_eq_empty], exact ⟨λ ⟨x, e⟩ _, ⟨x, le_of_eq e⟩, λ h, let ⟨x, h⟩ := h () in ⟨x, le_bot_iff.1 h⟩⟩ end end filter.realizer
244210ffb0a86c1b8ef905cd37d44fbf1a5378a9
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/standard/logic/connectives/prop.lean
68bb68734e0b1a6ed7b2d2e318a8215e0a6b0cdf
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
422
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, Jeremy Avigad ---------------------------------------------------------------------------------------------------- definition Prop [inline] := Type.{0}
438c728d24fea945e670038d8f31949576ccd689
fe84e287c662151bb313504482b218a503b972f3
/src/data/SL2N_prat.lean
ed1333a3319e0139c227105bcbbe17c3640b343e
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
4,824
lean
import data.SL2N data.prat def smul_num (m : SL2N) (q r : ℚ+) : ℚ+ := ⟨m.a * (q : ℚ) + m.b * (r : ℚ), add_pos_of_pos_of_nonneg (mul_pos (nat.cast_pos.mpr m.a_pos) q.property) (mul_nonneg (nat.cast_nonneg m.b) (le_of_lt r.property))⟩ def smul_den (m : SL2N) (q r : ℚ+) : ℚ+ := ⟨m.c * (q : ℚ) + m.d * (r : ℚ), add_pos_of_nonneg_of_pos (mul_nonneg (nat.cast_nonneg m.c) (le_of_lt q.property)) (mul_pos (nat.cast_pos.mpr m.d_pos) r.property)⟩ instance : has_scalar SL2N ℚ+ := ⟨λ m q, (smul_num m q 1) * (smul_den m q 1)⁻¹ ⟩ theorem smul_num_coe (m : SL2N) (q r : ℚ+) : ((smul_num m q r) : ℚ) = m.a * q + m.b * r := rfl theorem smul_den_coe (m : SL2N) (q r : ℚ+) : ((smul_den m q r) : ℚ) = m.c * q + m.d * r := rfl /- theorem smul_coe (m : SL2N) (q : ℚ+) : ((m • q : ℚ+) : ℚ) = (m.a * (q : ℚ) + m.b) / (m.c * (q : ℚ) + m.d) := begin change ((m.a : ℚ) * (q : ℚ) + m.b * ((1 : ℚ+) : ℚ) * (m.c * (q : ℚ) + m.d * ((1 : ℚ+) : ℚ))⁻¹ = (m.a * (q : ℚ) + m.b) / (m.c * (q : ℚ) + m.d), rw[pnat.one_coe,mul_one,mul_one,div_eq_mul_inv], end theorem smul_to_prat (m : SL2N) (u : P2) : m • u.to_prat = (m • u).to_prat := by { have hn : (smul_num m (u.x / u.y) 1) * u.y = smul_num m u.x u.y := by {apply subtype.eq, rw[mul_val,smul_num_val,smul_num_val,one_val,mul_one, add_mul,mul_val,inv_val,mul_assoc,mul_assoc], rw[inv_mul_cancel (ne_of_gt r.property),mul_one],}, } theorem smul_div (m : SL2N) (q r : ℚ+) : m • (q * r⁻¹) = (smul_num m q r) * (smul_den m q r)⁻¹ := by { have hn : (smul_num m (q * r⁻¹) 1) * r = smul_num m q r := by {apply subtype.eq, rw[mul_val,smul_num_val,smul_num_val,one_val,mul_one, add_mul,mul_val,inv_val,mul_assoc,mul_assoc], rw[inv_mul_cancel (ne_of_gt r.property),mul_one],}, have hd : (smul_den m (q * r⁻¹) 1) * r = smul_den m q r := by {apply subtype.eq, rw[mul_val,smul_den_val,smul_den_val,one_val,mul_one, add_mul,mul_val,inv_val,mul_assoc,mul_assoc], rw[inv_mul_cancel (ne_of_gt r.property),mul_one],}, rw[← hn,← hd,mul_inv,mul_assoc,mul_comm r,mul_assoc], rw[inv_mul_self r,mul_one],refl, } instance : mul_action SL2N ℚ+ := { smul := has_scalar.smul, one_smul := λ q, by {apply subtype.eq, rw[smul_val,one_a,one_b,one_c,one_d, nat.cast_zero,nat.cast_one,zero_mul,one_mul, add_zero,zero_add,div_one],}, mul_smul := λ m n q, by { let u := (smul_num m (smul_num n q 1) (smul_den n q 1)), let v := (smul_den m (smul_num n q 1) (smul_den n q 1)), let x := smul_num (m * n) q 1, let y := smul_den (m * n) q 1, suffices huyvx : u * y = v * x, {exact calc (m * n) • q = x * y⁻¹ : rfl ... = (v⁻¹ * (v * x)) * y⁻¹ : by {rw[← mul_assoc v⁻¹,inv_mul_self v,one_mul]} ... = (v⁻¹ * (u * y)) * y⁻¹ : by {rw[← huyvx]} ... = u * v⁻¹ : by {rw[mul_assoc,mul_assoc,mul_inv_self y,mul_one,mul_comm]} ... = m • (n • q) : (smul_div m _ _).symm }, apply subtype.eq,rw[mul_val,mul_val], dsimp[u,v,x,y], simp only [smul_num_val,smul_den_val,one_val,mul_a,mul_b,mul_c,mul_d], simp only [mul_one,one_mul,mul_add,add_mul,nat.cast_add,nat.cast_mul], repeat {rw[mul_assoc]}, ring, } } theorem S_smul_val (q : ℚ+) : (S • q).val = q.val + 1 := by { change (1 * q.val + 1 * (1 : ℚ+).val) * (0 * q.val + 1 * (1 : ℚ+).val)⁻¹ = q.val + 1, rw[one_val,zero_mul,zero_add,one_mul,one_mul], have : (1 : ℚ)⁻¹ = 1 := rfl, rw[this,mul_one], } theorem T_smul_val (q : ℚ+) : (T • q).val = q.val / (q.val + 1) := by { change (1 * q.val + 0 * (1 : ℚ+).val) * (1 * q.val + 1 * (1 : ℚ+).val)⁻¹ = q.val / (q.val + 1), rw[div_eq_mul_inv], rw[one_val,zero_mul,add_zero,one_mul,one_mul] } end prat namespace P2 def to_prat (u : P2) : ℚ+ := ⟨u.x / u.y,by { have hx : (u.x : ℚ) > 0 := nat.cast_pos.mpr u.x_pos, have hy : (u.y : ℚ) > 0 := nat.cast_pos.mpr u.y_pos, exact div_pos hx hy,}⟩ theorem to_prat_val (u : P2) : u.to_prat.val = u.x / u.y := rfl def of_prat (q : ℚ+) : P2 := { x := q.val.num.nat_abs, y := q.val.denom, x_pos := int.nat_abs_pos_of_ne_zero (ne_of_gt (rat.num_pos_iff_pos.mpr q.property)), y_pos := q.val.pos } theorem to_of_prat (q : ℚ+) : to_prat (of_prat q) = q := begin symmetry,apply subtype.eq, change q.val = (q.val.num.nat_abs) / (q.val.denom), have n_pos : q.val.num > 0 := rat.num_pos_iff_pos.mpr q.property, have hn := int.eq_nat_abs_of_zero_le (le_of_lt n_pos), have : (q.val.num : ℚ) = q.val.num.nat_abs := by {rw[← int.cast_coe_nat,← hn],}, rw[← this,← rat.num_denom'' q.val], end end P2 -/
778a111e8b908cca47a621bd6e1793fb93f3cb03
737dc4b96c97368cb66b925eeea3ab633ec3d702
/tests/lean/run/simpStar.lean
aa030341809b79206a1932908d7bf68daac4c170
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
493
lean
constant f (x y : Nat) : Nat constant g (x : Nat) : Nat theorem ex1 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by simp simp [*] theorem ex2 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by simp [*] axiom g_ax (x : Nat) : g x = 0 theorem ex3 (x y : Nat) (h₁ : f x x = g x) (h₂ : f x x < 5) : f x x + f x x = 0 := by simp [*] at * traceState have aux₁ : f x x = g x := h₁ have aux₂ : g x < 5 := h₂ simp [g_ax]
4c9ae32594b87b682123b68f21611758e52d2416
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/data/equiv/local_equiv.lean
d9e05714e04ae01cd826038baedf590e77083b7d
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
22,369
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 data.equiv.basic /-! # Local equivalences This files defines equivalences between subsets of given types. An element `e` of `local_equiv α β` is made of two maps `e.to_fun` and `e.inv_fun` respectively from α to β and from β to α (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively α and β. They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two local equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.to_fun x` and `e.inv_fun y`. ## Main definitions `equiv.to_local_equiv`: associating a local equiv to an equiv, with source = target = univ `local_equiv.symm` : the inverse of a local equiv `local_equiv.trans` : the composition of two local equivs `local_equiv.refl` : the identity local equiv `local_equiv.of_set` : the identity on a set `s` `eq_on_source` : equivalence relation describing the "right" notion of equality for local equivs (see below in implementation notes) ## Implementation notes There are at least three possible implementations of local equivalences: * equivs on subtypes * pairs of functions taking values in `option α` and `option β`, equal to none where the local equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `pequiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The local_equiv version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `to_fun` and `inv_fun` outside of `source` and `target`). In particular, the equality notion between local equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no local equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal local equivs, this is not an issue in practice. Still, we introduce an equivalence relation `eq_on_source` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. -/ open function set variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Local equivalence between subsets `source` and `target` of α and β respectively. The (global) maps `to_fun : α → β` and `inv_fun : β → α` map `source` to `target` and conversely, and are inverse to each other there. The values of `to_fun` outside of `source` and of `inv_fun` outside of `target` are irrelevant. -/ @[nolint has_inhabited_instance] structure local_equiv (α : Type*) (β : Type*) := (to_fun : α → β) (inv_fun : β → α) (source : set α) (target : set β) (map_source' : ∀{x}, x ∈ source → to_fun x ∈ target) (map_target' : ∀{x}, x ∈ target → inv_fun x ∈ source) (left_inv' : ∀{x}, x ∈ source → inv_fun (to_fun x) = x) (right_inv' : ∀{x}, x ∈ target → to_fun (inv_fun x) = x) -- attribute [simp] local_equiv.left_inv local_equiv.right_inv local_equiv.map_source local_equiv.map_target /-- Associating a local_equiv to an equiv-/ def equiv.to_local_equiv (e : equiv α β) : local_equiv α β := { to_fun := e.to_fun, inv_fun := e.inv_fun, source := univ, target := univ, map_source' := λx hx, mem_univ _, map_target' := λy hy, mem_univ _, left_inv' := λx hx, e.left_inv x, right_inv' := λx hx, e.right_inv x } namespace local_equiv variables (e : local_equiv α β) (e' : local_equiv β γ) /-- The inverse of a local equiv -/ protected def symm : local_equiv β α := { to_fun := e.inv_fun, inv_fun := e.to_fun, source := e.target, target := e.source, map_source' := e.map_target', map_target' := e.map_source', left_inv' := e.right_inv', right_inv' := e.left_inv' } instance : has_coe_to_fun (local_equiv α β) := ⟨_, local_equiv.to_fun⟩ @[simp] theorem coe_mk (f : α → β) (g s t ml mr il ir) : (local_equiv.mk f g s t ml mr il ir : α → β) = f := rfl @[simp] theorem coe_symm_mk (f : α → β) (g s t ml mr il ir) : ((local_equiv.mk f g s t ml mr il ir).symm : β → α) = g := rfl @[simp] lemma to_fun_as_coe : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe : e.inv_fun = e.symm := rfl @[simp] lemma map_source {x : α} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h @[simp] lemma map_target {x : β} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h @[simp] lemma left_inv {x : α} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h @[simp] lemma right_inv {x : β} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h /-- Associating to a local_equiv an equiv between the source and the target -/ protected def to_equiv : equiv (e.source) (e.target) := { to_fun := λ x, ⟨e x, e.map_source x.mem⟩, inv_fun := λ y, ⟨e.symm y, e.map_target y.mem⟩, left_inv := λ⟨x, hx⟩, subtype.eq $ e.left_inv hx, right_inv := λ⟨y, hy⟩, subtype.eq $ e.right_inv hy } @[simp] lemma symm_source : e.symm.source = e.target := rfl @[simp] lemma symm_target : e.symm.target = e.source := rfl @[simp] lemma symm_symm : e.symm.symm = e := by { cases e, refl } /-- A local equiv induces a bijection between its source and target -/ lemma bij_on_source : bij_on e e.source e.target := inv_on.bij_on ⟨λ x, e.left_inv, λ x, e.right_inv⟩ (λ x, e.map_source) (λ x, e.map_target) lemma image_eq_target_inter_inv_preimage {s : set α} (h : s ⊆ e.source) : e '' s = e.target ∩ e.symm ⁻¹' s := begin refine subset.antisymm (λx hx, _) (λx hx, _), { rcases (mem_image _ _ _).1 hx with ⟨y, ys, hy⟩, rw ← hy, split, { apply e.map_source, exact h ys }, { rwa [mem_preimage, e.left_inv (h ys)] } }, { rw ← e.right_inv hx.1, exact mem_image_of_mem _ hx.2 } end lemma inv_image_eq_source_inter_preimage {s : set β} (h : s ⊆ e.target) : e.symm '' s = e.source ∩ e ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h lemma source_inter_preimage_inv_preimage (s : set α) : e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s := begin ext, split, { rintros ⟨hx, xs⟩, simp only [mem_preimage, hx, e.left_inv, mem_preimage] at xs, exact ⟨hx, xs⟩ }, { rintros ⟨hx, xs⟩, simp [hx, xs] } end lemma target_inter_inv_preimage_preimage (s : set β) : e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ lemma image_source_eq_target : e '' e.source = e.target := e.bij_on_source.image_eq lemma source_subset_preimage_target : e.source ⊆ e ⁻¹' e.target := λx hx, e.map_source hx lemma inv_image_target_eq_source : e.symm '' e.target = e.source := e.symm.bij_on_source.image_eq lemma target_subset_preimage_source : e.target ⊆ e.symm ⁻¹' e.source := λx hx, e.map_target hx /-- Two local equivs that have the same `source`, same `to_fun` and same `inv_fun`, coincide. -/ @[ext] protected lemma ext (e' : local_equiv α β) (h : ∀x, e x = e' x) (hsymm : ∀x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' := begin have A : (e : α → β) = e', by { ext x, exact h x }, have B : (e.symm : β → α) = e'.symm, by { ext x, exact hsymm x }, have I : e '' e.source = e.target := e.image_source_eq_target, have I' : e' '' e'.source = e'.target := e'.image_source_eq_target, rw [A, hs, I'] at I, cases e; cases e', simp * at * end /-- Restricting a local equivalence to e.source ∩ s -/ protected def restr (s : set α) : local_equiv α β := { to_fun := e, inv_fun := e.symm, source := e.source ∩ s, target := e.target ∩ e.symm⁻¹' s, map_source' := λx hx, begin apply mem_inter, { apply e.map_source, exact hx.1 }, { rw [mem_preimage, e.left_inv], exact hx.2, exact hx.1 }, end, map_target' := λy hy, begin apply mem_inter, { apply e.map_target, exact hy.1 }, { exact hy.2 }, end, left_inv' := λx hx, e.left_inv hx.1, right_inv' := λy hy, e.right_inv hy.1 } @[simp] lemma restr_coe (s : set α) : (e.restr s : α → β) = e := rfl @[simp] lemma restr_coe_symm (s : set α) : ((e.restr s).symm : β → α) = e.symm := rfl @[simp] lemma restr_source (s : set α) : (e.restr s).source = e.source ∩ s := rfl @[simp] lemma restr_target (s : set α) : (e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl lemma restr_eq_of_source_subset {e : local_equiv α β} {s : set α} (h : e.source ⊆ s) : e.restr s = e := local_equiv.ext _ _ (λ_, rfl) (λ_, rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp] lemma restr_univ {e : local_equiv α β} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity local equiv -/ protected def refl (α : Type*) : local_equiv α α := (equiv.refl α).to_local_equiv @[simp] lemma refl_source : (local_equiv.refl α).source = univ := rfl @[simp] lemma refl_target : (local_equiv.refl α).target = univ := rfl @[simp] lemma refl_coe : (local_equiv.refl α : α → α) = id := rfl @[simp] lemma refl_symm : (local_equiv.refl α).symm = local_equiv.refl α := rfl @[simp] lemma refl_restr_source (s : set α) : ((local_equiv.refl α).restr s).source = s := by simp @[simp] lemma refl_restr_target (s : set α) : ((local_equiv.refl α).restr s).target = s := by { change univ ∩ id⁻¹' s = s, simp } /-- The identity local equiv on a set `s` -/ def of_set (s : set α) : local_equiv α α := { to_fun := id, inv_fun := id, source := s, target := s, map_source' := λx hx, hx, map_target' := λx hx, hx, left_inv' := λx hx, rfl, right_inv' := λx hx, rfl } @[simp] lemma of_set_source (s : set α) : (local_equiv.of_set s).source = s := rfl @[simp] lemma of_set_target (s : set α) : (local_equiv.of_set s).target = s := rfl @[simp] lemma of_set_coe (s : set α) : (local_equiv.of_set s : α → α) = id := rfl @[simp] lemma of_set_symm (s : set α) : (local_equiv.of_set s).symm = local_equiv.of_set s := rfl /-- Composing two local equivs if the target of the first coincides with the source of the second. -/ protected def trans' (e' : local_equiv β γ) (h : e.target = e'.source) : local_equiv α γ := { to_fun := e' ∘ e, inv_fun := e.symm ∘ e'.symm, source := e.source, target := e'.target, map_source' := λx hx, by simp [h.symm, hx], map_target' := λy hy, by simp [h, hy], left_inv' := λx hx, by simp [hx, h.symm], right_inv' := λy hy, by simp [hy, h] } /-- Composing two local equivs, by restricting to the maximal domain where their composition is well defined. -/ protected def trans : local_equiv α γ := local_equiv.trans' (e.symm.restr (e'.source)).symm (e'.restr (e.target)) (inter_comm _ _) @[simp] lemma coe_trans : (e.trans e' : α → γ) = e' ∘ e := rfl @[simp] lemma coe_trans_symm : ((e.trans e').symm : γ → α) = e.symm ∘ e'.symm := rfl lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; refl /- This could be considered as a simp lemma, but there are many situations where it makes something simple into something more complicated. -/ lemma trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl lemma trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) := begin symmetry, calc e.source ∩ e ⁻¹' (e.target ∩ e'.source) = (e.source ∩ e ⁻¹' (e.target)) ∩ e ⁻¹' (e'.source) : by rw [preimage_inter, inter_assoc] ... = e.source ∩ e ⁻¹' (e'.source) : by { congr' 1, apply inter_eq_self_of_subset_left e.source_subset_preimage_target } ... = (e.trans e').source : rfl end lemma trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) := begin rw [e.trans_source', e.inv_image_eq_source_inter_preimage, inter_comm], exact inter_subset_left _ _, end lemma image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source := image_source_eq_target (local_equiv.symm (local_equiv.restr (local_equiv.symm e) (e'.source))) lemma trans_target : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl lemma trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm lemma trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm lemma inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm lemma trans_assoc (e'' : local_equiv γ δ) : (e.trans e').trans e'' = e.trans (e'.trans e'') := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source, @preimage_comp α β γ, inter_assoc]) @[simp] lemma trans_refl : e.trans (local_equiv.refl β) = e := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source]) @[simp] lemma refl_trans : (local_equiv.refl α).trans e = e := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source, preimage_id]) lemma trans_refl_restr (s : set β) : e.trans ((local_equiv.refl β).restr s) = e.restr (e ⁻¹' s) := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source]) lemma trans_refl_restr' (s : set β) : e.trans ((local_equiv.refl β).restr s) = e.restr (e.source ∩ e ⁻¹' s) := local_equiv.ext _ _ (λx, rfl) (λx, rfl) $ by { simp [trans_source], rw [← inter_assoc, inter_self] } lemma restr_trans (s : set α) : (e.restr s).trans e' = (e.trans e').restr s := local_equiv.ext _ _ (λx, rfl) (λx, rfl) $ by { simp [trans_source, inter_comm], rwa inter_assoc } /-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. Then `e` and `e'` should really be considered the same local equiv. -/ def eq_on_source (e e' : local_equiv α β) : Prop := e.source = e'.source ∧ (∀x ∈ e.source, e x = e' x) /-- `eq_on_source` is an equivalence relation -/ instance eq_on_source_setoid : setoid (local_equiv α β) := { r := eq_on_source, iseqv := ⟨ λe, by simp [eq_on_source], λe e' h, by { simp [eq_on_source, h.1.symm], exact λx hx, (h.2 x hx).symm }, λe e' e'' h h', ⟨by rwa [← h'.1, ← h.1], λx hx, by { rw [← h'.2 x, h.2 x hx], rwa ← h.1 }⟩⟩ } lemma eq_on_source_refl : e ≈ e := setoid.refl _ /-- If two local equivs are equivalent, so are their inverses -/ lemma eq_on_source_symm {e e' : local_equiv α β} (h : e ≈ e') : e.symm ≈ e'.symm := begin have T : e.target = e'.target, { have : set.bij_on e' e.source e.target := e.bij_on_source.congr h.2, have A : e' '' e.source = e.target := this.image_eq, rw [h.1, e'.bij_on_source.image_eq] at A, exact A.symm }, refine ⟨T, λx hx, _⟩, have xt : x ∈ e.target := hx, rw T at xt, have e's : e'.symm x ∈ e.source, by { rw h.1, apply e'.map_target xt }, have A : e (e.symm x) = x := e.right_inv hx, have B : e (e'.symm x) = x, by { rw h.2, exact e'.right_inv xt, exact e's }, apply e.bij_on_source.inj_on (e.map_target hx) e's, rw [A, B] end /-- Two equivalent local equivs have the same source -/ lemma source_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') : e.source = e'.source := h.1 /-- Two equivalent local equivs have the same target -/ lemma target_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') : e.target = e'.target := (eq_on_source_symm h).1 /-- Two equivalent local equivs coincide on the source -/ lemma apply_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') {x : α} (hx : x ∈ e.source) : e x = e' x := h.2 x hx /-- Two equivalent local equivs have coinciding inverses on the target -/ lemma inv_apply_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') {x : β} (hx : x ∈ e.target) : e.symm x = e'.symm x := (eq_on_source_symm h).2 x hx /-- Composition of local equivs respects equivalence -/ lemma eq_on_source_trans {e e' : local_equiv α β} {f f' : local_equiv β γ} (he : e ≈ e') (hf : f ≈ f') : e.trans f ≈ e'.trans f' := begin split, { have : e.target = e'.target := (eq_on_source_symm he).1, rw [trans_source'', trans_source'', ← this, ← hf.1], exact eq_on.image_eq (λx hx, (eq_on_source_symm he).2 x hx.1) }, { assume x hx, rw trans_source at hx, simp [(he.2 x hx.1).symm, hf.2 _ hx.2] } end /-- Restriction of local equivs respects equivalence -/ lemma eq_on_source_restr {e e' : local_equiv α β} (he : e ≈ e') (s : set α) : e.restr s ≈ e'.restr s := begin split, { simp [he.1] }, { assume x hx, simp only [mem_inter_eq, restr_source] at hx, exact he.2 x hx.1 } end /-- Preimages are respected by equivalence -/ lemma eq_on_source_preimage {e e' : local_equiv α β} (he : e ≈ e') (s : set β) : e.source ∩ e ⁻¹' s = e'.source ∩ e' ⁻¹' s := begin ext x, simp only [mem_inter_eq, mem_preimage], split, { assume hx, rwa [apply_eq_of_eq_on_source (setoid.symm he), source_eq_of_eq_on_source (setoid.symm he)], rw source_eq_of_eq_on_source he at hx, exact hx.1 }, { assume hx, rwa [apply_eq_of_eq_on_source he, source_eq_of_eq_on_source he], rw source_eq_of_eq_on_source (setoid.symm he) at hx, exact hx.1 }, end /-- Composition of a local equiv and its inverse is equivalent to the restriction of the identity to the source -/ lemma trans_self_symm : e.trans e.symm ≈ local_equiv.of_set e.source := begin have A : (e.trans e.symm).source = e.source, by simp [trans_source, inter_eq_self_of_subset_left (source_subset_preimage_target _)], refine ⟨by simp [A], λx hx, _⟩, rw A at hx, simp [hx] end /-- Composition of the inverse of a local equiv and this local equiv is equivalent to the restriction of the identity to the target -/ lemma trans_symm_self : e.symm.trans e ≈ local_equiv.of_set e.target := trans_self_symm (e.symm) /-- Two equivalent local equivs are equal when the source and target are univ -/ lemma eq_of_eq_on_source_univ (e e' : local_equiv α β) (h : e ≈ e') (s : e.source = univ) (t : e.target = univ) : e = e' := begin apply local_equiv.ext _ _ (λx, _) (λx, _) h.1, { apply h.2 x, rw s, exact mem_univ _ }, { apply (eq_on_source_symm h).2 x, rw [symm_source, t], exact mem_univ _ } end section prod /-- The product of two local equivs, as a local equiv on the product. -/ def prod (e : local_equiv α β) (e' : local_equiv γ δ) : local_equiv (α × γ) (β × δ) := { source := set.prod e.source e'.source, target := set.prod e.target e'.target, to_fun := λp, (e p.1, e' p.2), inv_fun := λp, (e.symm p.1, e'.symm p.2), map_source' := λp hp, by { simp at hp, simp [hp] }, map_target' := λp hp, by { simp at hp, simp [map_target, hp] }, left_inv' := λp hp, by { simp at hp, simp [hp] }, right_inv' := λp hp, by { simp at hp, simp [hp] } } @[simp] lemma prod_source (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').source = set.prod e.source e'.source := rfl @[simp] lemma prod_target (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').target = set.prod e.target e'.target := rfl @[simp] lemma prod_coe (e : local_equiv α β) (e' : local_equiv γ δ) : ((e.prod e') : α × γ → β × δ) = (λp, (e p.1, e' p.2)) := rfl @[simp] lemma prod_coe_symm (e : local_equiv α β) (e' : local_equiv γ δ) : ((e.prod e').symm : β × δ → α × γ) = (λp, (e.symm p.1, e'.symm p.2)) := rfl end prod end local_equiv namespace set -- All arguments are explicit to avoid missing information in the pretty printer output /-- A bijection between two sets `s : set α` and `t : set β` provides a local equivalence between `α` and `β`. -/ @[simps] noncomputable def bij_on.to_local_equiv [nonempty α] (f : α → β) (s : set α) (t : set β) (hf : bij_on f s t) : local_equiv α β := { to_fun := f, inv_fun := inv_fun_on f s, source := s, target := t, map_source' := hf.maps_to, map_target' := hf.surj_on.maps_to_inv_fun_on, left_inv' := hf.inv_on_inv_fun_on.1, right_inv' := hf.inv_on_inv_fun_on.2 } /-- A map injective on a subset of its domain provides a local equivalence. -/ @[simp] noncomputable def inj_on.to_local_equiv [nonempty α] (f : α → β) (s : set α) (hf : inj_on f s) : local_equiv α β := hf.bij_on_image.to_local_equiv f s (f '' s) end set namespace equiv /- equivs give rise to local_equiv. We set up simp lemmas to reduce most properties of the local equiv to that of the equiv. -/ variables (e : equiv α β) (e' : equiv β γ) @[simp] lemma to_local_equiv_coe : (e.to_local_equiv : α → β) = e := rfl @[simp] lemma to_local_equiv_symm_coe : (e.to_local_equiv.symm : β → α) = e.symm := rfl @[simp] lemma to_local_equiv_source : e.to_local_equiv.source = univ := rfl @[simp] lemma to_local_equiv_target : e.to_local_equiv.target = univ := rfl @[simp] lemma refl_to_local_equiv : (equiv.refl α).to_local_equiv = local_equiv.refl α := rfl @[simp] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl @[simp] lemma trans_to_local_equiv : (e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [local_equiv.trans_source, equiv.to_local_equiv]) end equiv
2cadeead0bc426f465440c400e25514203e84c6d
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/integral/lebesgue.lean
8f13b3c7896541e19e9d825c23610db6b2ba6fa3
[ "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
128,085
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import measure_theory.measure.mutually_singular import measure_theory.constructions.borel_space import algebra.indicator_function import algebra.support import dynamics.ergodic.measure_preserving /-! # Lebesgue integral for `ℝ≥0∞`-valued functions We define simple functions and show that each Borel measurable function on `ℝ≥0∞` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ℝ≥0∞`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : α → ℝ≥0∞`. * `∫⁻ x, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` with respect to a measure `μ`; * `∫⁻ x, f x`: integral of a function `f : α → ℝ≥0∞` with respect to the canonical measure `volume` on `α`; * `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`. -/ noncomputable theory open set (hiding restrict restrict_apply) filter ennreal function (support) open_locale classical topological_space big_operators nnreal ennreal measure_theory namespace measure_theory variables {α β γ δ : Type*} /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) := (to_fun : α → β) (measurable_set_fiber' : ∀ x, measurable_set (to_fun ⁻¹' {x})) (finite_range' : (set.range to_fun).finite) local infixr ` →ₛ `:25 := simple_func namespace simple_func section measurable variables [measurable_space α] instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) (λ _, α → β) := ⟨to_fun⟩ lemma coe_injective ⦃f g : α →ₛ β⦄ (H : (f : α → β) = g) : f = g := by cases f; cases g; congr; exact H @[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := coe_injective $ funext H lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range' lemma measurable_set_fiber (f : α →ₛ β) (x : β) : measurable_set (f ⁻¹' {x}) := f.measurable_set_fiber' x @[simp] lemma apply_mk (f : α → β) (h h') (x : α) : simple_func.mk f h h' x = f x := rfl /-- Simple function defined on the empty type. -/ def of_is_empty [is_empty α] : α →ₛ β := { to_fun := is_empty_elim, measurable_set_fiber' := λ x, subsingleton.measurable_set, finite_range' := by simp [range_eq_empty] } /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f := finite.mem_to_finset _ theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f := f.finite_range.coe_to_finset theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) : x ∈ f.range := let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in mem_range.2 ⟨a, ha⟩ lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} : (∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) := by simp only [mem_range, set.forall_range_iff] lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} : (∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) := by simpa only [mem_range, exists_prop] using set.exists_range_iff lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range := preimage_singleton_eq_empty.trans $ not_congr mem_range.symm lemma exists_forall_le [nonempty β] [preorder β] [is_directed β (≤)] (f : α →ₛ β) : ∃ C, ∀ x, f x ≤ C := f.range.exists_le.imp $ λ C, forall_range_iff.1 /-- Constant function as a `simple_func`. -/ def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, measurable_set.const _, finite_range_const⟩ instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ default⟩ theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl @[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) : (const α b).range = {b} := finset.coe_injective $ by simp lemma range_const_subset (α) [measurable_space α] (b : β) : (const α b).range ⊆ {b} := finset.coe_subset.1 $ by simp lemma measurable_set_cut (r : α → β → Prop) (f : α →ₛ β) (h : ∀b, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} := begin have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b}, { ext a, suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa, exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ }, rw this, exact measurable_set.bUnion f.finite_range.countable (λ b _, measurable_set.inter (h b) (f.measurable_set_fiber _)) end @[measurability] theorem measurable_set_preimage (f : α →ₛ β) (s) : measurable_set (f ⁻¹' s) := measurable_set_cut (λ _ b, b ∈ s) f (λ b, measurable_set.const (b ∈ s)) /-- A simple function is measurable -/ @[measurability] protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, measurable_set_preimage f s @[measurability] protected theorem ae_measurable [measurable_space β] {μ : measure α} (f : α →ₛ β) : ae_measurable f μ := f.measurable.ae_measurable protected lemma sum_measure_preimage_singleton (f : α →ₛ β) {μ : measure α} (s : finset β) : ∑ y in s, μ (f ⁻¹' {y}) = μ (f ⁻¹' ↑s) := sum_measure_preimage_singleton _ (λ _ _, f.measurable_set_fiber _) lemma sum_range_measure_preimage_singleton (f : α →ₛ β) (μ : measure α) : ∑ y in f.range, μ (f ⁻¹' {y}) = μ univ := by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range] /-- If-then-else as a `simple_func`. -/ def piecewise (s : set α) (hs : measurable_set s) (f g : α →ₛ β) : α →ₛ β := ⟨s.piecewise f g, λ x, by letI : measurable_space β := ⊤; exact f.measurable.piecewise hs g.measurable trivial, (f.finite_range.union g.finite_range).subset range_ite_subset⟩ @[simp] theorem coe_piecewise {s : set α} (hs : measurable_set s) (f g : α →ₛ β) : ⇑(piecewise s hs f g) = s.piecewise f g := rfl theorem piecewise_apply {s : set α} (hs : measurable_set s) (f g : α →ₛ β) (a) : piecewise s hs f g a = if a ∈ s then f a else g a := rfl @[simp] lemma piecewise_compl {s : set α} (hs : measurable_set sᶜ) (f g : α →ₛ β) : piecewise sᶜ hs f g = piecewise s hs.of_compl g f := coe_injective $ by simp [hs] @[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ measurable_set.univ f g = f := coe_injective $ by simp @[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ measurable_set.empty f g = g := coe_injective $ by simp lemma support_indicator [has_zero β] {s : set α} (hs : measurable_set s) (f : α →ₛ β) : function.support (f.piecewise s hs (simple_func.const α 0)) = s ∩ function.support f := set.support_indicator lemma range_indicator {s : set α} (hs : measurable_set s) (hs_nonempty : s.nonempty) (hs_ne_univ : s ≠ univ) (x y : β) : (piecewise s hs (const α x) (const α y)).range = {x, y} := begin ext1 z, rw [mem_range, set.mem_range, finset.mem_insert, finset.mem_singleton], simp_rw piecewise_apply, split; intro h, { obtain ⟨a, haz⟩ := h, by_cases has : a ∈ s, { left, simp only [has, function.const_apply, if_true, coe_const] at haz, exact haz.symm, }, { right, simp only [has, function.const_apply, if_false, coe_const] at haz, exact haz.symm, }, }, { cases h, { obtain ⟨a, has⟩ : ∃ a, a ∈ s, from hs_nonempty, exact ⟨a, by simpa [has] using h.symm⟩, }, { obtain ⟨a, has⟩ : ∃ a, a ∉ s, { by_contra' h, refine hs_ne_univ _, ext1 a, simp [h a], }, exact ⟨a, by simpa [has] using h.symm⟩, }, }, end lemma measurable_bind [measurable_space γ] (f : α →ₛ β) (g : β → α → γ) (hg : ∀ b, measurable (g b)) : measurable (λ a, g (f a) a) := λ s hs, f.measurable_set_cut (λ a b, g b a ∈ s) $ λ b, hg b hs /-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ := ⟨λa, g (f a) a, λ c, f.measurable_set_cut (λ a b, g b a = c) $ λ b, (g b).measurable_set_preimage {c}, (f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl /-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple function `g ∘ f : α →ₛ γ` -/ def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g) theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl @[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl @[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) : (f.map g).range = f.range.image g := finset.coe_injective $ by simp only [coe_range, coe_map, finset.coe_image, range_comp] @[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) : (f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) := by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter, ← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp } lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) : (f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) := map_preimage _ _ _ /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp [measurable_space β] (f : β →ₛ γ) (g : α → β) (hgm : measurable g) : α →ₛ γ := { to_fun := f ∘ g, finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _, measurable_set_fiber' := λ z, hgm (f.measurable_set_fiber z) } @[simp] lemma coe_comp [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : ⇑(f.comp g hgm) = f ∘ g := rfl lemma range_comp_subset_range [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : (f.comp g hgm).range ⊆ f.range := finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range] /-- Extend a `simple_func` along a measurable embedding: `f₁.extend g hg f₂` is the function `F : β →ₛ γ` such that `F ∘ g = f₁` and `F y = f₂ y` whenever `y ∉ range g`. -/ def extend [measurable_space β] (f₁ : α →ₛ γ) (g : α → β) (hg : measurable_embedding g) (f₂ : β →ₛ γ) : β →ₛ γ := { to_fun := function.extend g f₁ f₂, finite_range' := (f₁.finite_range.union $ f₂.finite_range.subset (image_subset_range _ _)).subset (range_extend_subset _ _ _), measurable_set_fiber' := begin letI : measurable_space γ := ⊤, haveI : measurable_singleton_class γ := ⟨λ _, trivial⟩, exact λ x, hg.measurable_extend f₁.measurable f₂.measurable (measurable_set_singleton _) end } @[simp] lemma extend_apply [measurable_space β] (f₁ : α →ₛ γ) {g : α → β} (hg : measurable_embedding g) (f₂ : β →ₛ γ) (x : α) : (f₁.extend g hg f₂) (g x) = f₁ x := function.extend_apply hg.injective _ _ _ @[simp] lemma extend_apply' [measurable_space β] (f₁ : α →ₛ γ) {g : α → β} (hg : measurable_embedding g) (f₂ : β →ₛ γ) {y : β} (h : ¬∃ x, g x = y) : (f₁.extend g hg f₂) y = f₂ y := function.extend_apply' _ _ _ h @[simp] lemma extend_comp_eq' [measurable_space β] (f₁ : α →ₛ γ) {g : α → β} (hg : measurable_embedding g) (f₂ : β →ₛ γ) : (f₁.extend g hg f₂) ∘ g = f₁ := funext $ λ x, extend_apply _ _ _ _ @[simp] lemma extend_comp_eq [measurable_space β] (f₁ : α →ₛ γ) {g : α → β} (hg : measurable_embedding g) (f₂ : β →ₛ γ) : (f₁.extend g hg f₂).comp g hg.measurable = f₁ := coe_injective $ extend_comp_eq' _ _ _ /-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/ def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f) @[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl /-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β` into `λ a, (f a, g a)`. -/ def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) : pair f g ⁻¹' s ×ˢ t = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl /- A special form of `pair_preimage` -/ lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) : (pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) := by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ } theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp @[to_additive] instance [has_one β] : has_one (α →ₛ β) := ⟨const α 1⟩ @[to_additive] instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩ @[to_additive] instance [has_div β] : has_div (α →ₛ β) := ⟨λf g, (f.map (/)).seq g⟩ @[to_additive] instance [has_inv β] : has_inv (α →ₛ β) := ⟨λf, f.map (has_inv.inv)⟩ instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩ instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩ instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩ @[simp, to_additive] lemma const_one [has_one β] : const α (1 : β) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_one [has_one β] : ⇑(1 : α →ₛ β) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl @[simp, norm_cast, to_additive] lemma coe_inv [has_inv β] (f : α →ₛ β) : ⇑(f⁻¹) = f⁻¹ := rfl @[simp, norm_cast, to_additive] lemma coe_div [has_div β] (f g : α →ₛ β) : ⇑(f / g) = f / g := rfl @[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp, norm_cast] lemma coe_sup [has_sup β] (f g : α →ₛ β) : ⇑(f ⊔ g) = f ⊔ g := rfl @[simp, norm_cast] lemma coe_inf [has_inf β] (f g : α →ₛ β) : ⇑(f ⊓ g) = f ⊓ g := rfl @[to_additive] lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl @[to_additive] lemma div_apply [has_div β] (f g : α →ₛ β) (x : α) : (f / g) x = f x / g x := rfl @[to_additive] lemma inv_apply [has_inv β] (f : α →ₛ β) (x : α) : f⁻¹ x = (f x)⁻¹ := rfl lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl lemma inf_apply [has_inf β] (f g : α →ₛ β) (a : α) : (f ⊓ g) a = f a ⊓ g a := rfl @[simp, to_additive] lemma range_one [nonempty α] [has_one β] : (1 : α →ₛ β).range = {1} := finset.ext $ λ x, by simp [eq_comm] @[simp] lemma range_eq_empty_of_is_empty {β} [hα : is_empty α] (f : α →ₛ β) : f.range = ∅ := begin rw ← finset.not_nonempty_iff_eq_empty, by_contra, obtain ⟨y, hy_mem⟩ := h, rw [simple_func.mem_range, set.mem_range] at hy_mem, obtain ⟨x, hxy⟩ := hy_mem, rw is_empty_iff at hα, exact hα x, end lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 := forall_range_iff.2 $ λ x, rfl @[to_additive] lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) := rfl lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl @[to_additive] lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl @[to_additive] theorem map_mul [has_mul β] [has_mul γ] {g : β → γ} (hg : ∀ x y, g (x * y) = g x * g y) (f₁ f₂ : α →ₛ β) : (f₁ * f₂).map g = f₁.map g * f₂.map g := ext $ λ x, hg _ _ variables {K : Type*} instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λ k f, f.map ((•) k)⟩ @[simp] lemma coe_smul [has_scalar K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl instance has_nat_pow [monoid β] : has_pow (α →ₛ β) ℕ := ⟨λ f n, f.map (^ n)⟩ @[simp] lemma coe_pow [monoid β] (f : α →ₛ β) (n : ℕ) : ⇑(f ^ n) = f ^ n := rfl lemma pow_apply [monoid β] (n : ℕ) (f : α →ₛ β) (a : α) : (f ^ n) a = f a ^ n := rfl instance has_int_pow [div_inv_monoid β] : has_pow (α →ₛ β) ℤ := ⟨λ f n, f.map (^ n)⟩ @[simp] lemma coe_zpow [div_inv_monoid β] (f : α →ₛ β) (z : ℤ) : ⇑(f ^ z) = f ^ z := rfl lemma zpow_apply [div_inv_monoid β] (z : ℤ) (f : α →ₛ β) (a : α) : (f ^ z) a = f a ^ z := rfl -- TODO: work out how to generate these instances with `to_additive`, which gets confused by the -- argument order swap between `coe_smul` and `coe_pow`. section additive instance [add_monoid β] : add_monoid (α →ₛ β) := function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add (λ _ _, coe_smul _ _) instance [add_comm_monoid β] : add_comm_monoid (α →ₛ β) := function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add (λ _ _, coe_smul _ _) instance [add_group β] : add_group (α →ₛ β) := function.injective.add_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _) instance [add_comm_group β] : add_comm_group (α →ₛ β) := function.injective.add_comm_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _) end additive @[to_additive] instance [monoid β] : monoid (α →ₛ β) := function.injective.monoid (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_pow @[to_additive] instance [comm_monoid β] : comm_monoid (α →ₛ β) := function.injective.comm_monoid (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_pow @[to_additive] instance [group β] : group (α →ₛ β) := function.injective.group (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_inv coe_div coe_pow coe_zpow @[to_additive] instance [comm_group β] : comm_group (α →ₛ β) := function.injective.comm_group (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_inv coe_div coe_pow coe_zpow instance [semiring K] [add_comm_monoid β] [module K β] : module K (α →ₛ β) := function.injective.module K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩ coe_injective coe_smul lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl instance [preorder β] : preorder (α →ₛ β) := { le_refl := λf a, le_rfl, le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order β] : partial_order (α →ₛ β) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [has_le β] [order_bot β] : order_bot (α →ₛ β) := { bot := const α ⊥, bot_le := λf a, bot_le } instance [has_le β] [order_top β] : order_top (α →ₛ β) := { top := const α ⊤, le_top := λf a, le_top } instance [semilattice_inf β] : semilattice_inf (α →ₛ β) := { inf := (⊓), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup β] : semilattice_sup (α →ₛ β) := { sup := (⊔), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.semilattice_inf } instance [has_le β] [bounded_order β] : bounded_order (α →ₛ β) := { .. simple_func.order_bot, .. simple_func.order_top } lemma finset_sup_apply [semilattice_sup β] [order_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) : s.sup f a = s.sup (λc, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section restrict variables [has_zero β] /-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/ def restrict (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : measurable_set s then piecewise s hs f 0 else 0 theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α} (hs : ¬measurable_set s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : measurable_set s) : ⇑(restrict f s) = indicator s f := by { rw [restrict, dif_pos hs], refl } @[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f := by simp [restrict] @[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 := by simp [restrict] theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) : (f.restrict s).map g = (f.map g).restrict s := ext $ λ x, if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg] else by simp [restrict_of_not_measurable hs, hg] theorem map_coe_ennreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ≥0∞) = (f.map coe).restrict s := map_restrict_of_zero ennreal.coe_zero _ _ theorem map_coe_nnreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ) = (f.map coe).restrict s := map_restrict_of_zero nnreal.coe_zero _ _ theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : measurable_set s) (a) : restrict f s a = indicator s f a := by simp only [f.coe_restrict hs] theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : measurable_set s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm] theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : measurable_set s) {r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} := f.restrict_preimage hs hr.symm lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : measurable_set s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator] lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β} (hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) : r ∈ f '' s := if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr else by { rw [restrict_of_not_measurable hs] at hr, exact (h0 $ eq_zero_of_mem_range_zero hr).elim } @[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) : f.restrict s ≤ g.restrict s := if hs : measurable_set s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)] else by simp only [restrict_of_not_measurable hs, le_refl] end restrict section approx section variables [semilattice_sup β] [order_bot β] [has_zero β] /-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation by simple functions is defined so that in case `β = ℝ≥0∞` it sends each `a` to the supremum of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β := (finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a}) lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) : (approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf measurable_set_Ici) end lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : measurable f) (hg : measurable g) : (approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β] [has_zero β] [measurable_space β] [opens_measurable_space β] (i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) : (⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox /-- A sequence of `ℝ≥0∞`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ℝ≥0∞ := ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ)) lemma ennreal_rat_embed_encode (q : ℚ) : ennreal_rat_embed (encodable.encode q) = real.to_nnreal q := by rw [ennreal_rat_embed, encodable.encodek]; refl /-- Approximate a function `α → ℝ≥0∞` by a sequence of simple functions. -/ def eapprox : (α → ℝ≥0∞) → ℕ → α →ₛ ℝ≥0∞ := approx ennreal_rat_embed lemma eapprox_lt_top (f : α → ℝ≥0∞) (n : ℕ) (a : α) : eapprox f n a < ∞ := begin simp only [eapprox, approx, finset_sup_apply, finset.sup_lt_iff, with_top.zero_lt_top, finset.mem_range, ennreal.bot_eq_zero, restrict], assume b hb, split_ifs, { simp only [coe_zero, coe_piecewise, piecewise_eq_indicator, coe_const], calc {a : α | ennreal_rat_embed b ≤ f a}.indicator (λ x, ennreal_rat_embed b) a ≤ ennreal_rat_embed b : indicator_le_self _ _ a ... < ⊤ : ennreal.coe_lt_top }, { exact with_top.zero_lt_top }, end @[mono] lemma monotone_eapprox (f : α → ℝ≥0∞) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ℝ≥0∞) (hf : measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ℝ≥0∞) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (real.to_nnreal q : ℝ≥0∞) ≤ (⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q], refine le_supr_of_le (le_of_lt q_lt) _, exact le_rfl }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space γ] {f : γ → ℝ≥0∞} {g : α → γ} {n : ℕ} (hf : measurable f) (hg : measurable g) : (eapprox (f ∘ g) n : α → ℝ≥0∞) = (eapprox f n : γ →ₛ ℝ≥0∞) ∘ g := funext $ assume a, approx_comp a hf hg /-- Approximate a function `α → ℝ≥0∞` by a series of simple functions taking their values in `ℝ≥0`. -/ def eapprox_diff (f : α → ℝ≥0∞) : ∀ (n : ℕ), α →ₛ ℝ≥0 | 0 := (eapprox f 0).map ennreal.to_nnreal | (n+1) := (eapprox f (n+1) - eapprox f n).map ennreal.to_nnreal lemma sum_eapprox_diff (f : α → ℝ≥0∞) (n : ℕ) (a : α) : (∑ k in finset.range (n+1), (eapprox_diff f k a : ℝ≥0∞)) = eapprox f n a := begin induction n with n IH, { simp only [nat.nat_zero_eq_zero, finset.sum_singleton, finset.range_one], refl }, { rw [finset.sum_range_succ, nat.succ_eq_add_one, IH, eapprox_diff, coe_map, function.comp_app, coe_sub, pi.sub_apply, ennreal.coe_to_nnreal, add_tsub_cancel_of_le (monotone_eapprox f (nat.le_succ _) _)], apply (lt_of_le_of_lt _ (eapprox_lt_top f (n+1) a)).ne, rw tsub_le_iff_right, exact le_self_add }, end lemma tsum_eapprox_diff (f : α → ℝ≥0∞) (hf : measurable f) (a : α) : (∑' n, (eapprox_diff f n a : ℝ≥0∞)) = f a := by simp_rw [ennreal.tsum_eq_supr_nat' (tendsto_add_at_top_nat 1), sum_eapprox_diff, supr_eapprox_apply f hf a] end eapprox end measurable section measure variables {m : measurable_space α} {μ ν : measure α} /-- Integral of a simple function whose codomain is `ℝ≥0∞`. -/ def lintegral {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (μ : measure α) : ℝ≥0∞ := ∑ x in f.range, x * μ (f ⁻¹' {x}) lemma lintegral_eq_of_subset (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞} (hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] }, { intros, assumption }, { intros b _ hb, refine ⟨b, _, hb, rfl⟩, rw [mem_range, ← preimage_singleton_nonempty], exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 }, { intros, refl } end lemma lintegral_eq_of_subset' (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞} (hs : f.range \ {0} ⊆ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := f.lintegral_eq_of_subset $ λ x hfx _, hs $ finset.mem_sdiff.2 ⟨f.mem_range_self x, mt finset.mem_singleton.1 hfx⟩ /-- Calculate the integral of `(g ∘ f)`, where `g : β → ℝ≥0∞` and `f : α →ₛ β`. -/ lemma map_lintegral (g : β → ℝ≥0∞) (f : α →ₛ β) : (f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) := begin simp only [lintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h }, end lemma add_lintegral (f g : α →ₛ ℝ≥0∞) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ := calc (f + g).lintegral μ = ∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) + ∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ : by rw [map_lintegral, map_lintegral] ... = lintegral f μ + lintegral g μ : rfl lemma const_mul_lintegral (f : α →ₛ ℝ≥0∞) (x : ℝ≥0∞) : (const α x * f).lintegral μ = x * f.lintegral μ := calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) : map_lintegral _ _ ... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.lintegral μ : finset.mul_sum.symm /-- Integral of a simple function `α →ₛ ℝ≥0∞` as a bilinear map. -/ def lintegralₗ {m : measurable_space α} : (α →ₛ ℝ≥0∞) →ₗ[ℝ≥0∞] measure α →ₗ[ℝ≥0∞] ℝ≥0∞ := { to_fun := λ f, { to_fun := lintegral f, map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib], map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] }, map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g), map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) } @[simp] lemma zero_lintegral : (0 : α →ₛ ℝ≥0∞).lintegral μ = 0 := linear_map.ext_iff.1 lintegralₗ.map_zero μ lemma lintegral_add {ν} (f : α →ₛ ℝ≥0∞) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν := (lintegralₗ f).map_add μ ν lemma lintegral_smul (f : α →ₛ ℝ≥0∞) (c : ℝ≥0∞) : f.lintegral (c • μ) = c • f.lintegral μ := (lintegralₗ f).map_smul c μ @[simp] lemma lintegral_zero [measurable_space α] (f : α →ₛ ℝ≥0∞) : f.lintegral 0 = 0 := (lintegralₗ f).map_zero lemma lintegral_sum {m : measurable_space α} {ι} (f : α →ₛ ℝ≥0∞) (μ : ι → measure α) : f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) := begin simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, ← finset.tsum_subtype, ← ennreal.tsum_mul_left], apply ennreal.tsum_comm end lemma restrict_lintegral (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) := calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) : lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s then λ _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self] else false.elim $ hx $ by simp [*] ... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) : finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul] else by rw [restrict_preimage_singleton _ hs hb, inter_comm] lemma lintegral_restrict {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (s : set α) (μ : measure α) : f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) := by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage] lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = f.lintegral (μ.restrict s) := by rw [f.restrict_lintegral hs, lintegral_restrict] lemma const_lintegral (c : ℝ≥0∞) : (const α c).lintegral μ = c * μ univ := begin rw [lintegral], casesI is_empty_or_nonempty α, { simp [μ.eq_zero_of_is_empty] }, { simp [preimage_const_of_mem] }, end lemma const_lintegral_restrict (c : ℝ≥0∞) (s : set α) : (const α c).lintegral (μ.restrict s) = c * μ s := by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter] lemma restrict_const_lintegral (c : ℝ≥0∞) {s : set α} (hs : measurable_set s) : ((const α c).restrict s).lintegral μ = c * μ s := by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict] lemma le_sup_lintegral (f g : α →ₛ ℝ≥0∞) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ := calc f.lintegral μ ⊔ g.lintegral μ = ((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl ... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) : begin rw [map_lintegral, map_lintegral], refine sup_le _ _; refine finset.sum_le_sum (λ a _, mul_le_mul_right' _ _), exact le_sup_left, exact le_sup_right end ... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral] /-- `simple_func.lintegral` is monotone both in function and in measure. -/ @[mono] lemma lintegral_mono {f g : α →ₛ ℝ≥0∞} (hfg : f ≤ g) (hμν : μ ≤ ν) : f.lintegral μ ≤ g.lintegral ν := calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left ... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _ ... = g.lintegral μ : by rw [sup_of_le_right hfg] ... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $ hμν _ (g.measurable_set_preimage _) /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ℝ≥0∞} {g : β →ₛ ℝ≥0∞} {ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) : f.lintegral μ = g.lintegral ν := begin simp only [lintegral, ← H], apply lintegral_eq_of_subset, simp only [H], intros, exact mem_range_of_measure_ne_zero ‹_› end /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ lemma lintegral_congr {f g : α →ₛ ℝ≥0∞} (h : f =ᵐ[μ] g) : f.lintegral μ = g.lintegral μ := lintegral_eq_of_measure_preimage $ λ y, measure_congr $ eventually.set_eq $ h.mono $ λ x hx, by simp [hx] lemma lintegral_map' {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ℝ≥0∞) (g : β →ₛ ℝ≥0∞) (m' : α → β) (eq : ∀ a, f a = g (m' a)) (h : ∀s, measurable_set s → μ' s = μ (m' ⁻¹' s)) : f.lintegral μ = g.lintegral μ' := lintegral_eq_of_measure_preimage $ λ y, by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.measurable_set_preimage _)).symm } lemma lintegral_map {β} [measurable_space β] (g : β →ₛ ℝ≥0∞) {f : α → β} (hf : measurable f) : g.lintegral (measure.map f μ) = (g.comp f hf).lintegral μ := eq.symm $ lintegral_map' _ _ f (λ a, rfl) (λ s hs, measure.map_apply hf hs) end measure section fin_meas_supp open finset function lemma support_eq [measurable_space α] [has_zero β] (f : α →ₛ β) : support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} := set.ext $ λ x, by simp only [mem_support, set.mem_preimage, mem_filter, mem_range_self, true_and, exists_prop, mem_Union, set.mem_range, mem_singleton_iff, exists_eq_right'] variables {m : measurable_space α} [has_zero β] [has_zero γ] {μ : measure α} {f : α →ₛ β} lemma measurable_set_support [measurable_space α] (f : α →ₛ β) : measurable_set (support f) := by { rw f.support_eq, exact finset.measurable_set_bUnion _ (λ y hy, measurable_set_fiber _ _), } /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp {m : measurable_space α} (f : α →ₛ β) (μ : measure α) : Prop := f =ᶠ[μ.cofinite] 0 lemma fin_meas_supp_iff_support : f.fin_meas_supp μ ↔ μ (support f) < ∞ := iff.rfl lemma fin_meas_supp_iff : f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ := begin split, { refine λ h y hy, lt_of_le_of_lt (measure_mono _) h, exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx }, { intro H, rw [fin_meas_supp_iff_support, support_eq], refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _), exact λ y hy, (H y (finset.mem_filter.1 hy).2).ne } end namespace fin_meas_supp lemma meas_preimage_singleton_ne_zero (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) : μ (f ⁻¹' {y}) < ∞ := fin_meas_supp_iff.1 h y hy protected lemma map {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) : (f.map g).fin_meas_supp μ := flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f) lemma of_map {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) : f.fin_meas_supp μ := flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _ lemma map_iff {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) : (f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ := ⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩ protected lemma pair {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (pair f g).fin_meas_supp μ := calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g ... ≤ μ (support f) + μ (support g) : measure_union_le _ _ ... < _ : add_lt_top.2 ⟨hf, hg⟩ protected lemma map₂ [has_zero δ] (hf : f.fin_meas_supp μ) {g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) : ((pair f g).map (function.uncurry op)).fin_meas_supp μ := (hf.pair hg).map H protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f + g).fin_meas_supp μ := by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) } protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f * g).fin_meas_supp μ := by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) } lemma lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a ≠ ∞) : f.lintegral μ < ∞ := begin refine sum_lt_top (λ a ha, _), rcases eq_or_ne a ∞ with rfl|ha, { simp only [ae_iff, ne.def, not_not] at hf, simp [set.preimage, hf] }, { by_cases ha0 : a = 0, { subst a, rwa [zero_mul] }, { exact mul_ne_top ha (fin_meas_supp_iff.1 hm _ ha0).ne } } end lemma of_lintegral_ne_top {f : α →ₛ ℝ≥0∞} (h : f.lintegral μ ≠ ∞) : f.fin_meas_supp μ := begin refine fin_meas_supp_iff.2 (λ b hb, _), rw [f.lintegral_eq_of_subset' (finset.subset_insert b _)] at h, refine ennreal.lt_top_of_mul_ne_top_right _ hb, exact (lt_top_of_sum_ne_top h (finset.mem_insert_self _ _)).ne end lemma iff_lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hf : ∀ᵐ a ∂μ, f a ≠ ∞) : f.fin_meas_supp μ ↔ f.lintegral μ < ∞ := ⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_ne_top h.ne⟩ end fin_meas_supp end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ @[elab_as_eliminator] protected lemma induction {α γ} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop} (h_ind : ∀ c {s} (hs : measurable_set s), P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0))) (h_add : ∀ ⦃f g : simple_func α γ⦄, disjoint (support f) (support g) → P f → P g → P (f + g)) (f : simple_func α γ) : P f := begin generalize' h : f.range \ {0} = s, rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h, revert s f h, refine finset.induction _ _, { intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf, convert h_ind 0 measurable_set.univ, ext x, simp [hf] }, { intros x s hxs ih f hf, have mx := f.measurable_set_preimage {x}, let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f, have Pg : P g, { apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise], rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert, insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union], { rw [set.image_subset_iff], convert set.subset_univ _, exact preimage_const_of_mem (mem_singleton _) }, { rwa [finset.mem_coe] }}, convert h_add _ Pg (h_ind x mx), { ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] }, rintro y, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] } end end simple_func section lintegral open simple_func variables {m : measurable_space α} {μ ν : measure α} /-- The **lower Lebesgue integral** of a function `f` with respect to a measure `μ`. -/ @[irreducible] def lintegral {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : ℝ≥0∞ := ⨆ (g : α →ₛ ℝ≥0∞) (hf : ⇑g ≤ f), g.lintegral μ /-! In the notation for integrals, an expression like `∫⁻ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫⁻ x, f x = 0` will be parsed incorrectly. -/ notation `∫⁻` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral μ r notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral (measure.restrict μ s) r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r theorem simple_func.lintegral_eq_lintegral {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (μ : measure α) : ∫⁻ a, f a ∂ μ = f.lintegral μ := begin rw lintegral, exact le_antisymm (supr₂_le $ λ g hg, lintegral_mono hg $ le_rfl) (le_supr₂_of_le f le_rfl le_rfl) end @[mono] lemma lintegral_mono' {m : measurable_space α} ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂ν := begin rw [lintegral, lintegral], exact supr_mono (λ φ, supr_mono' $ λ hφ, ⟨le_trans hφ hfg, lintegral_mono (le_refl φ) hμν⟩) end lemma lintegral_mono ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := lintegral_mono' (le_refl μ) hfg lemma lintegral_mono_nnreal {f g : α → ℝ≥0} (h : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := begin refine lintegral_mono _, intro a, rw ennreal.coe_le_coe, exact h a, end lemma supr_lintegral_measurable_le_eq_lintegral (f : α → ℝ≥0∞) : (⨆ (g : α → ℝ≥0∞) (g_meas : measurable g) (hg : g ≤ f), ∫⁻ a, g a ∂μ) = ∫⁻ a, f a ∂μ := begin apply le_antisymm, { exact supr_le (λ i, supr_le (λ hi, supr_le (λ h'i, lintegral_mono h'i))) }, { rw lintegral, refine supr₂_le (λ i hi, le_supr₂_of_le i i.measurable $ le_supr_of_le hi _), exact le_of_eq (i.lintegral_eq_lintegral _).symm }, end lemma lintegral_mono_set {m : measurable_space α} ⦃μ : measure α⦄ {s t : set α} {f : α → ℝ≥0∞} (hst : s ⊆ t) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in t, f x ∂μ := lintegral_mono' (measure.restrict_mono hst (le_refl μ)) (le_refl f) lemma lintegral_mono_set' {m : measurable_space α} ⦃μ : measure α⦄ {s t : set α} {f : α → ℝ≥0∞} (hst : s ≤ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in t, f x ∂μ := lintegral_mono' (measure.restrict_mono' hst (le_refl μ)) (le_refl f) lemma monotone_lintegral {m : measurable_space α} (μ : measure α) : monotone (lintegral μ) := lintegral_mono @[simp] lemma lintegral_const (c : ℝ≥0∞) : ∫⁻ a, c ∂μ = c * μ univ := by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const] @[simp] lemma lintegral_one : ∫⁻ a, (1 : ℝ≥0∞) ∂μ = μ univ := by rw [lintegral_const, one_mul] lemma set_lintegral_const (s : set α) (c : ℝ≥0∞) : ∫⁻ a in s, c ∂μ = c * μ s := by rw [lintegral_const, measure.restrict_apply_univ] lemma set_lintegral_one (s) : ∫⁻ a in s, 1 ∂μ = μ s := by rw [set_lintegral_const, one_mul] /-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions `φ : α →ₛ ℝ≥0∞` such that `φ ≤ f`. This lemma says that it suffices to take functions `φ : α →ₛ ℝ≥0`. -/ lemma lintegral_eq_nnreal {m : measurable_space α} (f : α → ℝ≥0∞) (μ : measure α) : (∫⁻ a, f a ∂μ) = (⨆ (φ : α →ₛ ℝ≥0) (hf : ∀ x, ↑(φ x) ≤ f x), (φ.map (coe : ℝ≥0 → ℝ≥0∞)).lintegral μ) := begin rw lintegral, refine le_antisymm (supr₂_le $ assume φ hφ, _) (supr_mono' $ λ φ, ⟨φ.map (coe : ℝ≥0 → ℝ≥0∞), le_rfl⟩), by_cases h : ∀ᵐ a ∂μ, φ a ≠ ∞, { let ψ := φ.map ennreal.to_nnreal, replace h : ψ.map (coe : ℝ≥0 → ℝ≥0∞) =ᵐ[μ] φ := h.mono (λ a, ennreal.coe_to_nnreal), have : ∀ x, ↑(ψ x) ≤ f x := λ x, le_trans ennreal.coe_to_nnreal_le_self (hφ x), exact le_supr_of_le (φ.map ennreal.to_nnreal) (le_supr_of_le this (ge_of_eq $ lintegral_congr h)) }, { have h_meas : μ (φ ⁻¹' {∞}) ≠ 0, from mt measure_zero_iff_ae_nmem.1 h, refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ λ b hb, _), obtain ⟨n, hn⟩ : ∃ n : ℕ, b < n * μ (φ ⁻¹' {∞}), from exists_nat_mul_gt h_meas (ne_of_lt hb), use (const α (n : ℝ≥0)).restrict (φ ⁻¹' {∞}), simp only [lt_supr_iff, exists_prop, coe_restrict, φ.measurable_set_preimage, coe_const, ennreal.coe_indicator, map_coe_ennreal_restrict, simple_func.map_const, ennreal.coe_nat, restrict_const_lintegral], refine ⟨indicator_le (λ x hx, le_trans _ (hφ _)), hn⟩, simp only [mem_preimage, mem_singleton_iff] at hx, simp only [hx, le_top] } end lemma exists_simple_func_forall_lintegral_sub_lt_of_pos {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ φ : α →ₛ ℝ≥0, (∀ x, ↑(φ x) ≤ f x) ∧ ∀ ψ : α →ₛ ℝ≥0, (∀ x, ↑(ψ x) ≤ f x) → (map coe (ψ - φ)).lintegral μ < ε := begin rw lintegral_eq_nnreal at h, have := ennreal.lt_add_right h hε, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp_rw [lt_supr_iff, supr_lt_iff, supr_le_iff] at this, rcases this with ⟨φ, hle : ∀ x, ↑(φ x) ≤ f x, b, hbφ, hb⟩, refine ⟨φ, hle, λ ψ hψ, _⟩, have : (map coe φ).lintegral μ ≠ ∞, from ne_top_of_le_ne_top h (le_supr₂ φ hle), rw [← ennreal.add_lt_add_iff_left this, ← add_lintegral, ← map_add @ennreal.coe_add], refine (hb _ (λ x, le_trans _ (max_le (hle x) (hψ x)))).trans_lt hbφ, norm_cast, simp only [add_apply, sub_apply, add_tsub_eq_max] end theorem supr_lintegral_le {ι : Sort*} (f : ι → α → ℝ≥0∞) : (⨆i, ∫⁻ a, f i a ∂μ) ≤ (∫⁻ a, ⨆i, f i a ∂μ) := begin simp only [← supr_apply], exact (monotone_lintegral μ).le_map_supr end lemma supr₂_lintegral_le {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (⨆ i j, ∫⁻ a, f i j a ∂μ) ≤ (∫⁻ a, ⨆ i j, f i j a ∂μ) := by { convert (monotone_lintegral μ).le_map_supr₂ f, ext1 a, simp only [supr_apply] } theorem le_infi_lintegral {ι : Sort*} (f : ι → α → ℝ≥0∞) : (∫⁻ a, ⨅i, f i a ∂μ) ≤ (⨅i, ∫⁻ a, f i a ∂μ) := by { simp only [← infi_apply], exact (monotone_lintegral μ).map_infi_le } lemma le_infi₂_lintegral {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (∫⁻ a, ⨅ i (h : ι' i), f i h a ∂μ) ≤ (⨅ i (h : ι' i), ∫⁻ a, f i h a ∂μ) := by { convert (monotone_lintegral μ).map_infi₂_le f, ext1 a, simp only [infi_apply] } lemma lintegral_mono_ae {f g : α → ℝ≥0∞} (h : ∀ᵐ a ∂μ, f a ≤ g a) : (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, g a ∂μ) := begin rcases exists_measurable_superset_of_null h with ⟨t, hts, ht, ht0⟩, have : ∀ᵐ x ∂μ, x ∉ t := measure_zero_iff_ae_nmem.1 ht0, rw [lintegral, lintegral], refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict tᶜ) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (simple_func.lintegral_congr $ this.mono $ λ a hnt, _), by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma set_lintegral_mono_ae {s : set α} {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfg : ∀ᵐ x ∂μ, x ∈ s → f x ≤ g x) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in s, g x ∂μ := lintegral_mono_ae $ (ae_restrict_iff $ measurable_set_le hf hg).2 hfg lemma set_lintegral_mono {s : set α} {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfg : ∀ x ∈ s, f x ≤ g x) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in s, g x ∂μ := set_lintegral_mono_ae hf hg (ae_of_all _ hfg) lemma lintegral_congr_ae {f g : α → ℝ≥0∞} (h : f =ᵐ[μ] g) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le) lemma lintegral_congr {f g : α → ℝ≥0∞} (h : ∀ a, f a = g a) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := by simp only [h] lemma set_lintegral_congr {f : α → ℝ≥0∞} {s t : set α} (h : s =ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := by rw [measure.restrict_congr_set h] lemma set_lintegral_congr_fun {f g : α → ℝ≥0∞} {s : set α} (hs : measurable_set s) (hfg : ∀ᵐ x ∂μ, x ∈ s → f x = g x) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in s, g x ∂μ := by { rw lintegral_congr_ae, rw eventually_eq, rwa ae_restrict_iff' hs, } /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin set c : ℝ≥0 → ℝ≥0∞ := coe, set F := λ a:α, ⨆n, f n a, have hF : measurable F := measurable_supr hf, refine le_antisymm _ (supr_lintegral_le _), rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_le (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (λa, r * a), have eq_rs : (const α r : α →ₛ ℝ≥0∞) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}), { assume p, rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] }, have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : ℕ), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (pos_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : ∀r:ℝ≥0∞, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : ∀n, measurable_set {a : α | ⇑(map c rs) a ≤ f n a} := assume n, measurable_set_le (simple_func.measurable _) (hf n), calc (r:ℝ≥0∞) * (s.map c).lintegral μ = ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r}) : by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral] ... = ∑ r in (rs.map c).range, r * μ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : by simp only [(eq _).symm] ... = ∑ r in (rs.map c).range, (⨆n, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : finset.sum_congr rfl $ assume x hx, begin rw [measure_Union_eq_supr (directed_of_sup $ mono x), ennreal.mul_supr], end ... = ⨆n, ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : begin rw [ennreal.finset_sum_supr_nat], assume p i j h, exact mul_le_mul_left' (measure_mono $ mono p h) _ end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).lintegral μ) : begin refine supr_mono (λ n, _), rw [restrict_lintegral _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2 with a, refine and_congr_right _, simp {contextual := tt} } end ... ≤ (⨆n, ∫⁻ a, f n a ∂μ) : begin refine supr_mono (λ n, _), rw [← simple_func.lintegral_eq_lintegral], refine lintegral_mono (assume a, _), simp only [map_apply] at h_meas, simp only [coe_map, restrict_apply _ (h_meas _), (∘)], exact indicator_apply_le id, end end /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with ae_measurable functions. -/ theorem lintegral_supr' {f : ℕ → α → ℝ≥0∞} (hf : ∀n, ae_measurable (f n) μ) (h_mono : ∀ᵐ x ∂μ, monotone (λ n, f n x)) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin simp_rw ←supr_apply, let p : α → (ℕ → ℝ≥0∞) → Prop := λ x f', monotone f', have hp : ∀ᵐ x ∂μ, p x (λ i, f i x), from h_mono, have h_ae_seq_mono : monotone (ae_seq hf p), { intros n m hnm x, by_cases hx : x ∈ ae_seq_set hf p, { exact ae_seq.prop_of_mem_ae_seq_set hf hx hnm, }, { simp only [ae_seq, hx, if_false], exact le_rfl, }, }, rw lintegral_congr_ae (ae_seq.supr hf hp).symm, simp_rw supr_apply, rw @lintegral_supr _ _ μ _ (ae_seq.measurable hf p) h_ae_seq_mono, congr, exact funext (λ n, lintegral_congr_ae (ae_seq.ae_seq_n_eq_fun_n_ae hf hp n)), end /-- Monotone convergence theorem expressed with limits -/ theorem lintegral_tendsto_of_tendsto_of_monotone {f : ℕ → α → ℝ≥0∞} {F : α → ℝ≥0∞} (hf : ∀n, ae_measurable (f n) μ) (h_mono : ∀ᵐ x ∂μ, monotone (λ n, f n x)) (h_tendsto : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 $ F x)) : tendsto (λ n, ∫⁻ x, f n x ∂μ) at_top (𝓝 $ ∫⁻ x, F x ∂μ) := begin have : monotone (λ n, ∫⁻ x, f n x ∂μ) := λ i j hij, lintegral_mono_ae (h_mono.mono $ λ x hx, hx hij), suffices key : ∫⁻ x, F x ∂μ = ⨆n, ∫⁻ x, f n x ∂μ, { rw key, exact tendsto_at_top_supr this }, rw ← lintegral_supr' hf h_mono, refine lintegral_congr_ae _, filter_upwards [h_mono, h_tendsto] with _ hx_mono hx_tendsto using tendsto_nhds_unique hx_tendsto (tendsto_at_top_supr hx_mono), end lemma lintegral_eq_supr_eapprox_lintegral {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, f a ∂μ) = (⨆n, (eapprox f n).lintegral μ) := calc (∫⁻ a, f a ∂μ) = (∫⁻ a, ⨆n, (eapprox f n : α → ℝ≥0∞) a ∂μ) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ℝ≥0∞) a ∂μ) : begin rw [lintegral_supr], { measurability, }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).lintegral μ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral] /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. This lemma states states this fact in terms of `ε` and `δ`. -/ lemma exists_pos_set_lintegral_lt_of_measure_lt {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ δ > 0, ∀ s, μ s < δ → ∫⁻ x in s, f x ∂μ < ε := begin rcases exists_between hε.bot_lt with ⟨ε₂, hε₂0 : 0 < ε₂, hε₂ε⟩, rcases exists_between hε₂0 with ⟨ε₁, hε₁0, hε₁₂⟩, rcases exists_simple_func_forall_lintegral_sub_lt_of_pos h hε₁0.ne' with ⟨φ, hle, hφ⟩, rcases φ.exists_forall_le with ⟨C, hC⟩, use [(ε₂ - ε₁) / C, ennreal.div_pos_iff.2 ⟨(tsub_pos_iff_lt.2 hε₁₂).ne', ennreal.coe_ne_top⟩], refine λ s hs, lt_of_le_of_lt _ hε₂ε, simp only [lintegral_eq_nnreal, supr_le_iff], intros ψ hψ, calc (map coe ψ).lintegral (μ.restrict s) ≤ (map coe φ).lintegral (μ.restrict s) + (map coe (ψ - φ)).lintegral (μ.restrict s) : begin rw [← simple_func.add_lintegral, ← simple_func.map_add @ennreal.coe_add], refine simple_func.lintegral_mono (λ x, _) le_rfl, simp only [add_tsub_eq_max, le_max_right, coe_map, function.comp_app, simple_func.coe_add, simple_func.coe_sub, pi.add_apply, pi.sub_apply, with_top.coe_max] end ... ≤ (map coe φ).lintegral (μ.restrict s) + ε₁ : begin refine add_le_add le_rfl (le_trans _ (hφ _ hψ).le), exact simple_func.lintegral_mono le_rfl measure.restrict_le_self end ... ≤ (simple_func.const α (C : ℝ≥0∞)).lintegral (μ.restrict s) + ε₁ : add_le_add (simple_func.lintegral_mono (λ x, coe_le_coe.2 (hC x)) le_rfl) le_rfl ... = C * μ s + ε₁ : by simp only [←simple_func.lintegral_eq_lintegral, coe_const, lintegral_const, measure.restrict_apply, measurable_set.univ, univ_inter] ... ≤ C * ((ε₂ - ε₁) / C) + ε₁ : add_le_add_right (ennreal.mul_le_mul le_rfl hs.le) _ ... ≤ (ε₂ - ε₁) + ε₁ : add_le_add mul_div_le le_rfl ... = ε₂ : tsub_add_cancel_of_le hε₁₂.le, end /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma tendsto_set_lintegral_zero {ι} {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {l : filter ι} {s : ι → set α} (hl : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫⁻ x in s i, f x ∂μ) l (𝓝 0) := begin simp only [ennreal.nhds_zero, tendsto_infi, tendsto_principal, mem_Iio, ← pos_iff_ne_zero] at hl ⊢, intros ε ε0, rcases exists_pos_set_lintegral_lt_of_measure_lt h ε0.ne' with ⟨δ, δ0, hδ⟩, exact (hl δ δ0).mono (λ i, hδ _) end @[simp] lemma lintegral_add {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, (⨆n, (eapprox f n : α → ℝ≥0∞) a) + (⨆n, (eapprox g n : α → ℝ≥0∞) a) ∂μ) : by simp only [supr_eapprox_apply, hf, hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ℝ≥0∞) a) ∂μ) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).lintegral μ + (eapprox g n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral], refl }, { measurability, }, { assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).lintegral μ) + (⨆n, (eapprox g n).lintegral μ) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl μ) } ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg] lemma lintegral_add' {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, hf.mk f a + hg.mk g a ∂μ) : lintegral_congr_ae (eventually_eq.add hf.ae_eq_mk hg.ae_eq_mk) ... = (∫⁻ a, hf.mk f a ∂μ) + (∫⁻ a, hg.mk g a ∂μ) : lintegral_add hf.measurable_mk hg.measurable_mk ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : begin congr' 1, { exact lintegral_congr_ae hf.ae_eq_mk.symm }, { exact lintegral_congr_ae hg.ae_eq_mk.symm }, end lemma lintegral_zero : (∫⁻ a:α, 0 ∂μ) = 0 := by simp lemma lintegral_zero_fun : (∫⁻ a:α, (0 : α → ℝ≥0∞) a ∂μ) = 0 := by simp @[simp] lemma lintegral_smul_measure (c : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂ (c • μ) = c * ∫⁻ a, f a ∂μ := by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul] @[simp] lemma lintegral_sum_measure {m : measurable_space α} {ι} (f : α → ℝ≥0∞) (μ : ι → measure α) : ∫⁻ a, f a ∂(measure.sum μ) = ∑' i, ∫⁻ a, f a ∂(μ i) := begin simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum], rw [supr_comm], congr, funext s, induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp }, simp only [finset.sum_insert hi, ← hs], refine (ennreal.supr_add_supr _).symm, intros φ ψ, exact ⟨⟨φ ⊔ ψ, λ x, sup_le (φ.2 x) (ψ.2 x)⟩, add_le_add (simple_func.lintegral_mono le_sup_left le_rfl) (finset.sum_le_sum $ λ j hj, simple_func.lintegral_mono le_sup_right le_rfl)⟩ end theorem has_sum_lintegral_measure {ι} {m : measurable_space α} (f : α → ℝ≥0∞) (μ : ι → measure α) : has_sum (λ i, ∫⁻ a, f a ∂(μ i)) (∫⁻ a, f a ∂(measure.sum μ)) := (lintegral_sum_measure f μ).symm ▸ ennreal.summable.has_sum @[simp] lemma lintegral_add_measure {m : measurable_space α} (f : α → ℝ≥0∞) (μ ν : measure α) : ∫⁻ a, f a ∂ (μ + ν) = ∫⁻ a, f a ∂μ + ∫⁻ a, f a ∂ν := by simpa [tsum_fintype] using lintegral_sum_measure f (λ b, cond b μ ν) @[simp] lemma lintegral_finset_sum_measure {ι} {m : measurable_space α} (s : finset ι) (f : α → ℝ≥0∞) (μ : ι → measure α) : ∫⁻ a, f a ∂(∑ i in s, μ i) = ∑ i in s, ∫⁻ a, f a ∂μ i := by { rw [← measure.sum_coe_finset, lintegral_sum_measure, ← finset.tsum_subtype'], refl } @[simp] lemma lintegral_zero_measure {m : measurable_space α} (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂(0 : measure α) = 0 := bot_unique $ by simp [lintegral] lemma set_lintegral_empty (f : α → ℝ≥0∞) : ∫⁻ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, lintegral_zero_measure] lemma set_lintegral_univ (f : α → ℝ≥0∞) : ∫⁻ x in univ, f x ∂μ = ∫⁻ x, f x ∂μ := by rw measure.restrict_univ lemma set_lintegral_measure_zero (s : set α) (f : α → ℝ≥0∞) (hs' : μ s = 0) : ∫⁻ x in s, f x ∂μ = 0 := begin convert lintegral_zero_measure _, exact measure.restrict_eq_zero.2 hs', end lemma lintegral_finset_sum (s : finset β) {f : β → α → ℝ≥0∞} (hf : ∀ b ∈ s, measurable (f b)) : (∫⁻ a, ∑ b in s, f b a ∂μ) = ∑ b in s, ∫⁻ a, f b a ∂μ := begin induction s using finset.induction_on with a s has ih, { simp }, { simp only [finset.sum_insert has], rw [finset.forall_mem_insert] at hf, rw [lintegral_add hf.1 (s.measurable_sum hf.2), ih hf.2] } end @[simp] lemma lintegral_const_mul (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := calc (∫⁻ a, r * f a ∂μ) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a) ∂μ) : by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl } ... = (⨆n, r * (eapprox f n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact mul_le_mul_left' (monotone_eapprox _ h _) _ } end ... = r * (∫⁻ a, f a ∂μ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf] lemma lintegral_const_mul'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin have A : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ a, r * f a ∂μ = ∫⁻ a, r * hf.mk f a ∂μ := lintegral_congr_ae (eventually_eq.fun_comp hf.ae_eq_mk _), rw [A, B, lintegral_const_mul _ hf.measurable_mk], end lemma lintegral_const_mul_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : r * (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, r * f a ∂μ) := begin rw [lintegral, ennreal.mul_supr], refine supr_le (λs, _), rw [ennreal.mul_supr], simp only [supr_le_iff, ge_iff_le], assume hs, rw [← simple_func.const_mul_lintegral, lintegral], refine le_supr_of_le (const α r * s) (le_supr_of_le (λx, _) le_rfl), exact mul_le_mul_left' (hs x) _ end lemma lintegral_const_mul' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin by_cases h : r = 0, { simp [h] }, apply le_antisymm _ (lintegral_const_mul_le r f), have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr, have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv }, have := lintegral_const_mul_le (r⁻¹) (λx, r * f x), simp [(mul_assoc _ _ _).symm, rinv'] at this, simpa [(mul_assoc _ _ _).symm, rinv] using mul_le_mul_left' this r end lemma lintegral_mul_const (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul r hf] lemma lintegral_mul_const'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul'' r hf] lemma lintegral_mul_const_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂μ * r ≤ ∫⁻ a, f a * r ∂μ := by simp_rw [mul_comm, lintegral_const_mul_le r f] lemma lintegral_mul_const' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞): ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul' r f hr] /- A double integral of a product where each factor contains only one variable is a product of integrals -/ lemma lintegral_lintegral_mul {β} [measurable_space β] {ν : measure β} {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g ν) : ∫⁻ x, ∫⁻ y, f x * g y ∂ν ∂μ = ∫⁻ x, f x ∂μ * ∫⁻ y, g y ∂ν := by simp [lintegral_const_mul'' _ hg, lintegral_mul_const'' _ hf] -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : α → β} (h : f =ᵐ[μ] f') (g : β → ℝ≥0∞) : (∫⁻ a, g (f a) ∂μ) = (∫⁻ a, g (f' a) ∂μ) := lintegral_congr_ae $ h.mono $ λ a h, by rw h -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : f₁ =ᵐ[μ] f₁') (h₂ : f₂ =ᵐ[μ] f₂') (g : β → γ → ℝ≥0∞) : (∫⁻ a, g (f₁ a) (f₂ a) ∂μ) = (∫⁻ a, g (f₁' a) (f₂' a) ∂μ) := lintegral_congr_ae $ h₁.mp $ h₂.mono $ λ _ h₂ h₁, by rw [h₁, h₂] @[simp] lemma lintegral_indicator (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : ∫⁻ a, s.indicator f a ∂μ = ∫⁻ a in s, f a ∂μ := begin simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'], apply le_antisymm; refine supr_mono' (subtype.forall.2 $ λ φ hφ, _), { refine ⟨⟨φ, le_trans hφ (indicator_le_self _ _)⟩, _⟩, refine simple_func.lintegral_mono (λ x, _) le_rfl, by_cases hx : x ∈ s, { simp [hx, hs, le_refl] }, { apply le_trans (hφ x), simp [hx, hs, le_refl] } }, { refine ⟨⟨φ.restrict s, λ x, _⟩, le_rfl⟩, simp [hφ x, hs, indicator_le_indicator] } end lemma set_lintegral_eq_const {f : α → ℝ≥0∞} (hf : measurable f) (r : ℝ≥0∞) : ∫⁻ x in {x | f x = r}, f x ∂μ = r * μ {x | f x = r} := begin have : ∀ᵐ x ∂μ, x ∈ {x | f x = r} → f x = r := ae_of_all μ (λ _ hx, hx), rw [set_lintegral_congr_fun _ this], dsimp, rw [lintegral_const, measure.restrict_apply measurable_set.univ, set.univ_inter], exact hf (measurable_set_singleton r) end /-- **Markov's inequality** also known as **Chebyshev's first inequality**. For a version assuming `ae_measurable`, see `mul_meas_ge_le_lintegral₀`. -/ lemma mul_meas_ge_le_lintegral {f : α → ℝ≥0∞} (hf : measurable f) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ := begin have : measurable_set {a : α | ε ≤ f a }, from hf measurable_set_Ici, rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral], refine lintegral_mono (λ a, _), simp only [restrict_apply _ this], exact indicator_apply_le id end /-- **Markov's inequality** also known as **Chebyshev's first inequality**. -/ lemma mul_meas_ge_le_lintegral₀ {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ := begin have A : μ {x | ε ≤ f x} = μ {x | ε ≤ hf.mk f x}, { apply eventually_eq.measure_eq, filter_upwards [hf.ae_eq_mk] with x hx, change (ε ≤ f x) = (ε ≤ hf.mk f x), simp [hx] }, have B : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, rw [A, B], exact mul_meas_ge_le_lintegral hf.measurable_mk ε, end lemma lintegral_eq_top_of_measure_eq_top_pos {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (hμf : 0 < μ {x | f x = ∞}) : ∫⁻ x, f x ∂μ = ∞ := eq_top_iff.mpr $ calc ∞ = ∞ * μ {x | ∞ ≤ f x} : by simp [mul_eq_top, hμf.ne.symm] ... ≤ ∫⁻ x, f x ∂μ : mul_meas_ge_le_lintegral₀ hf ∞ /-- **Markov's inequality** also known as **Chebyshev's first inequality**. -/ lemma meas_ge_le_lintegral_div {f : α → ℝ≥0∞} (hf : ae_measurable f μ) {ε : ℝ≥0∞} (hε : ε ≠ 0) (hε' : ε ≠ ∞) : μ {x | ε ≤ f x} ≤ (∫⁻ a, f a ∂μ) / ε := (ennreal.le_div_iff_mul_le (or.inl hε) (or.inl hε')).2 $ by { rw [mul_comm], exact mul_meas_ge_le_lintegral₀ hf ε } @[simp] lemma lintegral_eq_zero_iff {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ᵐ a ∂μ, f a < n⁻¹, { assume n, rw [ae_iff, ← nonpos_iff_eq_zero, ← @ennreal.zero_div n⁻¹, ennreal.le_div_iff_mul_le, mul_comm], simp only [not_lt], -- TODO: why `rw ← h` fails with "not an equality or an iff"? exacts [h ▸ mul_meas_ge_le_lintegral hf n⁻¹, or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top), or.inr ennreal.zero_ne_top] }, refine (ae_all_iff.2 this).mono (λ a ha, _), by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc ∫⁻ a, f a ∂μ = ∫⁻ a, 0 ∂μ : lintegral_congr_ae h ... = 0 : lintegral_zero } end @[simp] lemma lintegral_eq_zero_iff' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin have : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, rw [this, lintegral_eq_zero_iff hf.measurable_mk], exact ⟨λ H, hf.ae_eq_mk.trans H, λ H, hf.ae_eq_mk.symm.trans H⟩ end lemma lintegral_pos_iff_support {f : α → ℝ≥0∞} (hf : measurable f) : 0 < ∫⁻ a, f a ∂μ ↔ 0 < μ (function.support f) := by simp [pos_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support] /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f n.succ a) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := let ⟨s, hs⟩ := exists_measurable_superset_of_null (ae_iff.1 (ae_all_iff.2 h_mono)) in let g := λ n a, if a ∈ s then 0 else f n a in have g_eq_f : ∀ᵐ a ∂μ, ∀n, g n a = f n a, from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha), calc ∫⁻ a, ⨆n, f n a ∂μ = ∫⁻ a, ⨆n, g n a ∂μ : lintegral_congr_ae $ g_eq_f.mono $ λ a ha, by simp only [ha] ... = ⨆n, (∫⁻ a, g n a ∂μ) : lintegral_supr (assume n, measurable_const.piecewise hs.2.1 (hf n)) (monotone_nat_of_le_succ $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a ∉ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a ∂μ) : by simp only [lintegral_congr_ae (g_eq_f.mono $ λ a ha, ha _)] lemma lintegral_sub {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hg_fin : ∫⁻ a, g a ∂μ ≠ ∞) (h_le : g ≤ᵐ[μ] f) : ∫⁻ a, f a - g a ∂μ = ∫⁻ a, f a ∂μ - ∫⁻ a, g a ∂μ := begin rw [← ennreal.add_left_inj hg_fin, tsub_add_cancel_of_le (lintegral_mono_ae h_le), ← lintegral_add (hf.sub hg) hg], refine lintegral_congr_ae (h_le.mono $ λ x hx, _), exact tsub_add_cancel_of_le hx end lemma lintegral_sub_le (f g : α → ℝ≥0∞) (hf : measurable f) (hg : measurable g) (h : f ≤ᵐ[μ] g) : ∫⁻ x, g x ∂μ - ∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x - f x ∂μ := begin by_cases hfi : ∫⁻ x, f x ∂μ = ∞, { rw [hfi, ennreal.sub_top], exact bot_le }, { rw lintegral_sub hg hf hfi h, refl' } end lemma lintegral_strict_mono_of_ae_le_of_ae_lt_on {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (h_le : f ≤ᵐ[μ] g) {s : set α} (hμs : μ s ≠ 0) (h : ∀ᵐ x ∂μ, x ∈ s → f x < g x) : ∫⁻ x, f x ∂μ < ∫⁻ x, g x ∂μ := begin rw [← tsub_pos_iff_lt, ← lintegral_sub hg hf hfi h_le], by_contra hnlt, rw [not_lt, nonpos_iff_eq_zero, lintegral_eq_zero_iff (hg.sub hf), filter.eventually_eq] at hnlt, simp only [ae_iff, tsub_eq_zero_iff_le, pi.zero_apply, not_lt, not_le] at hnlt h, refine hμs _, push_neg at h, have hs_eq : s = {a : α | a ∈ s ∧ g a ≤ f a} ∪ {a : α | a ∈ s ∧ f a < g a}, { ext1 x, simp_rw [set.mem_union, set.mem_set_of_eq, ← not_le], tauto, }, rw hs_eq, refine measure_union_null h (measure_mono_null _ hnlt), simp, end lemma lintegral_strict_mono {f g : α → ℝ≥0∞} (hμ : μ ≠ 0) (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (h : ∀ᵐ x ∂μ, f x < g x) : ∫⁻ x, f x ∂μ < ∫⁻ x, g x ∂μ := begin rw [ne.def, ← measure.measure_univ_eq_zero] at hμ, refine lintegral_strict_mono_of_ae_le_of_ae_lt_on hf hg hfi (ae_le_of_ae_lt h) hμ _, simpa using h, end lemma set_lintegral_strict_mono {f g : α → ℝ≥0∞} {s : set α} (hsm : measurable_set s) (hs : μ s ≠ 0) (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x in s, f x ∂μ ≠ ∞) (h : ∀ᵐ x ∂μ, x ∈ s → f x < g x) : ∫⁻ x in s, f x ∂μ < ∫⁻ x in s, g x ∂μ := lintegral_strict_mono (by simp [hs]) hf hg hfi ((ae_restrict_iff' hsm).mpr h) /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, f n.succ ≤ᵐ[μ] f n) (h_fin : ∫⁻ a, f 0 a ∂μ ≠ ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := have fn_le_f0 : ∫⁻ a, ⨅n, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ, from lintegral_mono (assume a, infi_le_of_le 0 le_rfl), have fn_le_f0' : (⨅n, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ, from infi_le_of_le 0 le_rfl, (ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $ show ∫⁻ a, f 0 a ∂μ - ∫⁻ a, ⨅n, f n a ∂μ = ∫⁻ a, f 0 a ∂μ - (⨅n, ∫⁻ a, f n a ∂μ), from calc ∫⁻ a, f 0 a ∂μ - (∫⁻ a, ⨅n, f n a ∂μ) = ∫⁻ a, f 0 a - ⨅n, f n a ∂μ: (lintegral_sub (h_meas 0) (measurable_infi h_meas) (ne_top_of_le_ne_top h_fin $ lintegral_mono (assume a, infi_le _ _)) (ae_of_all _ $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a ∂μ : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a ∂μ : lintegral_supr_ae (assume n, (h_meas 0).sub (h_meas n)) (assume n, (h_mono n).mono $ assume a ha, tsub_le_tsub le_rfl ha) ... = ⨆n, ∫⁻ a, f 0 a ∂μ - ∫⁻ a, f n a ∂μ : have h_mono : ∀ᵐ a ∂μ, ∀n:ℕ, f n.succ a ≤ f n a := ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f 0 a := assume n, h_mono.mono $ assume a h, begin induction n with n ih, {exact le_rfl}, {exact le_trans (h n) ih} end, congr_arg supr $ funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (ne_top_of_le_ne_top h_fin $ lintegral_mono_ae $ h_mono n) (h_mono n) ... = ∫⁻ a, f 0 a ∂μ - ⨅n, ∫⁻ a, f n a ∂μ : ennreal.sub_infi.symm /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_anti : antitone f) (h_fin : ∫⁻ a, f 0 a ∂μ ≠ ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := lintegral_infi_ae h_meas (λ n, ae_of_all _ $ h_anti n.le_succ) h_fin /-- Known as Fatou's lemma, version with `ae_measurable` functions -/ lemma lintegral_liminf_le' {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, ae_measurable (f n) μ) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := calc ∫⁻ a, liminf at_top (λ n, f n a) ∂μ = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a ∂μ : by simp only [liminf_eq_supr_infi_of_nat] ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a ∂μ : lintegral_supr' (assume n, ae_measurable_binfi _ (countable_encodable _) h_meas) (ae_of_all μ (assume a n m hnm, infi_le_infi_of_subset $ λ i hi, le_trans hnm hi)) ... ≤ ⨆n:ℕ, ⨅i≥n, ∫⁻ a, f i a ∂μ : supr_mono $ λ n, le_infi₂_lintegral _ ... = at_top.liminf (λ n, ∫⁻ a, f n a ∂μ) : filter.liminf_eq_supr_infi_of_nat.symm /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := lintegral_liminf_le' (λ n, (h_meas n).ae_measurable) lemma limsup_lintegral_le {f : ℕ → α → ℝ≥0∞} {g : α → ℝ≥0∞} (hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, f n ≤ᵐ[μ] g) (h_fin : ∫⁻ a, g a ∂μ ≠ ∞) : limsup at_top (λn, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, f n a) ∂μ := calc limsup at_top (λn, ∫⁻ a, f n a ∂μ) = ⨅n:ℕ, ⨆i≥n, ∫⁻ a, f i a ∂μ : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a ∂μ : infi_mono $ assume n, supr₂_lintegral_le _ ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a ∂μ : begin refine (lintegral_infi _ _ _).symm, { assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas }, { assume n m hnm a, exact (supr_le_supr_of_subset $ λ i hi, le_trans hnm hi) }, { refine ne_top_of_le_ne_top h_fin (lintegral_mono_ae _), refine (ae_all_iff.2 h_bound).mono (λ n hn, _), exact supr_le (λ i, supr_le $ λ hi, hn i) } end ... = ∫⁻ a, limsup at_top (λn, f n a) ∂μ : by simp only [limsup_eq_infi_supr_of_nat] /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ ≠ ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := tendsto_of_le_liminf_of_limsup_le (calc ∫⁻ a, f a ∂μ = ∫⁻ a, liminf at_top (λ (n : ℕ), F n a) ∂μ : lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm ... ≤ liminf at_top (λ n, ∫⁻ a, F n a ∂μ) : lintegral_liminf_le hF_meas) (calc limsup at_top (λ (n : ℕ), ∫⁻ a, F n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, F n a) ∂μ : limsup_lintegral_le hF_meas h_bound h_fin ... = ∫⁻ a, f a ∂μ : lintegral_congr_ae $ h_lim.mono $ λ a h, h.limsup_eq) /-- Dominated convergence theorem for nonnegative functions which are just almost everywhere measurable. -/ lemma tendsto_lintegral_of_dominated_convergence' {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, ae_measurable (F n) μ) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ ≠ ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := begin have : ∀ n, ∫⁻ a, F n a ∂μ = ∫⁻ a, (hF_meas n).mk (F n) a ∂μ := λ n, lintegral_congr_ae (hF_meas n).ae_eq_mk, simp_rw this, apply tendsto_lintegral_of_dominated_convergence bound (λ n, (hF_meas n).measurable_mk) _ h_fin, { have : ∀ n, ∀ᵐ a ∂μ, (hF_meas n).mk (F n) a = F n a := λ n, (hF_meas n).ae_eq_mk.symm, have : ∀ᵐ a ∂μ, ∀ n, (hF_meas n).mk (F n) a = F n a := ae_all_iff.mpr this, filter_upwards [this, h_lim] with a H H', simp_rw H, exact H', }, { assume n, filter_upwards [h_bound n, (hF_meas n).ae_eq_mk] with a H H', rwa H' at H, }, end /-- Dominated convergence theorem for filters with a countable basis -/ lemma tendsto_lintegral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀ᶠ n in l, measurable (F n)) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, F n a ≤ bound a) (h_fin : ∫⁻ a, bound a ∂μ ≠ ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) l (𝓝 $ ∫⁻ a, f a ∂μ) := begin rw tendsto_iff_seq_tendsto, intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { assumption }, { refine h_lim.mono (λ a h_lim, _), apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : ∫⁻ a, ⨆b, f b a ∂μ = ⨆b, ∫⁻ a, f b a ∂μ := begin casesI is_empty_or_nonempty β, { simp [supr_of_empty] }, inhabit β, have : ∀a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _), exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) }, calc ∫⁻ a, ⨆ b, f b a ∂μ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a ∂μ : by simp only [this] ... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a ∂μ : lintegral_supr (assume n, hf _) h_directed.sequence_mono ... = ⨆ b, ∫⁻ a, f b a ∂μ : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (λb, ∫⁻ a, f b a ∂μ) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_mono $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀i, measurable (f i)) : ∫⁻ a, ∑' i, f i a ∂μ = ∑' i, ∫⁻ a, f i a ∂μ := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ (λ i _, hf i)] }, { assume b, exact finset.measurable_sum _ (λ i _, hf i) }, { assume s t, use [s ∪ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end open measure lemma lintegral_Union [encodable β] {s : β → set α} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ = ∑' i, ∫⁻ a in s i, f a ∂μ := by simp only [measure.restrict_Union hd hm, lintegral_sum_measure] lemma lintegral_Union_le [encodable β] (s : β → set α) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ ≤ ∑' i, ∫⁻ a in s i, f a ∂μ := begin rw [← lintegral_sum_measure], exact lintegral_mono' restrict_Union_le le_rfl end lemma lintegral_union {f : α → ℝ≥0∞} {A B : set α} (hA : measurable_set A) (hB : measurable_set B) (hAB : disjoint A B) : ∫⁻ a in A ∪ B, f a ∂μ = ∫⁻ a in A, f a ∂μ + ∫⁻ a in B, f a ∂μ := begin rw [set.union_eq_Union, lintegral_Union, tsum_bool, add_comm], { simp only [to_bool_false_eq_ff, to_bool_true_eq_tt, cond] }, { intros i, exact measurable_set.cond hA hB }, { rwa pairwise_disjoint_on_bool } end lemma lintegral_add_compl (f : α → ℝ≥0∞) {A : set α} (hA : measurable_set A) : ∫⁻ x in A, f x ∂μ + ∫⁻ x in Aᶜ, f x ∂μ = ∫⁻ x, f x ∂μ := by rw [← lintegral_add_measure, measure.restrict_add_restrict_compl hA] lemma lintegral_map {mβ : measurable_space β} {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg], congr' with n : 1, convert simple_func.lintegral_map _ hg, ext1 x, simp only [eapprox_comp hf hg, coe_comp] end lemma lintegral_map' {mβ : measurable_space β} {f : β → ℝ≥0∞} {g : α → β} (hf : ae_measurable f (measure.map g μ)) (hg : ae_measurable g μ) : ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, f (g a) ∂μ := calc ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, hf.mk f a ∂(measure.map g μ) : lintegral_congr_ae hf.ae_eq_mk ... = ∫⁻ a, hf.mk f a ∂(measure.map (hg.mk g) μ) : by { congr' 1, exact measure.map_congr hg.ae_eq_mk } ... = ∫⁻ a, hf.mk f (hg.mk g a) ∂μ : lintegral_map hf.measurable_mk hg.measurable_mk ... = ∫⁻ a, hf.mk f (g a) ∂μ : lintegral_congr_ae $ hg.ae_eq_mk.symm.fun_comp _ ... = ∫⁻ a, f (g a) ∂μ : lintegral_congr_ae (ae_eq_comp hg hf.ae_eq_mk.symm) lemma lintegral_map_le {mβ : measurable_space β} (f : β → ℝ≥0∞) {g : α → β} (hg : measurable g) : ∫⁻ a, f a ∂(measure.map g μ) ≤ ∫⁻ a, f (g a) ∂μ := begin rw [← supr_lintegral_measurable_le_eq_lintegral, ← supr_lintegral_measurable_le_eq_lintegral], refine supr₂_le (λ i hi, supr_le $ λ h'i, _), refine le_supr₂_of_le (i ∘ g) (hi.comp hg) _, exact le_supr_of_le (λ x, h'i (g x)) (le_of_eq (lintegral_map hi hg)) end lemma lintegral_comp [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : lintegral μ (f ∘ g) = ∫⁻ a, f a ∂(map g μ) := (lintegral_map hf hg).symm lemma set_lintegral_map [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} {s : set β} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : ∫⁻ y in s, f y ∂(map g μ) = ∫⁻ x in g ⁻¹' s, f (g x) ∂μ := by rw [restrict_map hg hs, lintegral_map hf hg] /-- If `g : α → β` is a measurable embedding and `f : β → ℝ≥0∞` is any function (not necessarily measurable), then `∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ`. Compare with `lintegral_map` wich applies to any measurable `g : α → β` but requires that `f` is measurable as well. -/ lemma _root_.measurable_embedding.lintegral_map [measurable_space β] {g : α → β} (hg : measurable_embedding g) (f : β → ℝ≥0∞) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin rw [lintegral, lintegral], refine le_antisymm (supr₂_le $ λ f₀ hf₀, _) (supr₂_le $ λ f₀ hf₀, _), { rw [simple_func.lintegral_map _ hg.measurable], have : (f₀.comp g hg.measurable : α → ℝ≥0∞) ≤ f ∘ g, from λ x, hf₀ (g x), exact le_supr_of_le (comp f₀ g hg.measurable) (le_supr _ this) }, { rw [← f₀.extend_comp_eq hg (const _ 0), ← simple_func.lintegral_map, ← simple_func.lintegral_eq_lintegral, ← lintegral], refine lintegral_mono_ae (hg.ae_map_iff.2 $ eventually_of_forall $ λ x, _), exact (extend_apply _ _ _ _).trans_le (hf₀ _) } end /-- The `lintegral` transforms appropriately under a measurable equivalence `g : α ≃ᵐ β`. (Compare `lintegral_map`, which applies to a wider class of functions `g : α → β`, but requires measurability of the function being integrated.) -/ lemma lintegral_map_equiv [measurable_space β] (f : β → ℝ≥0∞) (g : α ≃ᵐ β) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := g.measurable_embedding.lintegral_map f lemma measure_preserving.lintegral_comp {mb : measurable_space β} {ν : measure β} {g : α → β} (hg : measure_preserving g μ ν) {f : β → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f (g a) ∂μ = ∫⁻ b, f b ∂ν := by rw [← hg.map_eq, lintegral_map hf hg.measurable] lemma measure_preserving.lintegral_comp_emb {mb : measurable_space β} {ν : measure β} {g : α → β} (hg : measure_preserving g μ ν) (hge : measurable_embedding g) (f : β → ℝ≥0∞) : ∫⁻ a, f (g a) ∂μ = ∫⁻ b, f b ∂ν := by rw [← hg.map_eq, hge.lintegral_map] lemma measure_preserving.set_lintegral_comp_preimage {mb : measurable_space β} {ν : measure β} {g : α → β} (hg : measure_preserving g μ ν) {s : set β} (hs : measurable_set s) {f : β → ℝ≥0∞} (hf : measurable f) : ∫⁻ a in g ⁻¹' s, f (g a) ∂μ = ∫⁻ b in s, f b ∂ν := by rw [← hg.map_eq, set_lintegral_map hs hf hg.measurable] lemma measure_preserving.set_lintegral_comp_preimage_emb {mb : measurable_space β} {ν : measure β} {g : α → β} (hg : measure_preserving g μ ν) (hge : measurable_embedding g) (f : β → ℝ≥0∞) (s : set β) : ∫⁻ a in g ⁻¹' s, f (g a) ∂μ = ∫⁻ b in s, f b ∂ν := by rw [← hg.map_eq, hge.restrict_map, hge.lintegral_map] lemma measure_preserving.set_lintegral_comp_emb {mb : measurable_space β} {ν : measure β} {g : α → β} (hg : measure_preserving g μ ν) (hge : measurable_embedding g) (f : β → ℝ≥0∞) (s : set α) : ∫⁻ a in s, f (g a) ∂μ = ∫⁻ b in g '' s, f b ∂ν := by rw [← hg.set_lintegral_comp_preimage_emb hge, preimage_image_eq _ hge.injective] section dirac_and_count variable [measurable_space α] lemma lintegral_dirac' (a : α) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac' hf)] lemma lintegral_dirac [measurable_singleton_class α] (a : α) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac f)] lemma lintegral_encodable {α : Type*} {m : measurable_space α} [encodable α] [measurable_singleton_class α] (f : α → ℝ≥0∞) (μ : measure α) : ∫⁻ a, f a ∂μ = ∑' a, f a * μ {a} := begin conv_lhs { rw [← sum_smul_dirac μ, lintegral_sum_measure] }, congr' 1 with a : 1, rw [lintegral_smul_measure, lintegral_dirac, mul_comm], end lemma lintegral_count' {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac' a hf), end lemma lintegral_count [measurable_singleton_class α] (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac a f), end end dirac_and_count lemma ae_lt_top {f : α → ℝ≥0∞} (hf : measurable f) (h2f : ∫⁻ x, f x ∂μ ≠ ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, apply h2f.lt_top.not_le, have : (f ⁻¹' {∞}).indicator ⊤ ≤ f, { intro x, by_cases hx : x ∈ f ⁻¹' {∞}; [simpa [hx], simp [hx]] }, convert lintegral_mono this, rw [lintegral_indicator _ (hf (measurable_set_singleton ∞))], simp [ennreal.top_mul, preimage, h] end lemma ae_lt_top' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h2f : ∫⁻ x, f x ∂μ ≠ ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin have h2f_meas : ∫⁻ x, hf.mk f x ∂μ ≠ ∞, by rwa ← lintegral_congr_ae hf.ae_eq_mk, exact (ae_lt_top hf.measurable_mk h2f_meas).mp (hf.ae_eq_mk.mono (λ x hx h, by rwa hx)), end lemma set_lintegral_lt_top_of_bdd_above {s : set α} (hs : μ s ≠ ∞) {f : α → ℝ≥0} (hf : measurable f) (hbdd : bdd_above (f '' s)) : ∫⁻ x in s, f x ∂μ < ∞ := begin obtain ⟨M, hM⟩ := hbdd, rw mem_upper_bounds at hM, refine lt_of_le_of_lt (set_lintegral_mono hf.coe_nnreal_ennreal (@measurable_const _ _ _ _ ↑M) _) _, { simpa using hM }, { rw lintegral_const, refine ennreal.mul_lt_top ennreal.coe_lt_top.ne _, simp [hs] } end lemma set_lintegral_lt_top_of_is_compact [topological_space α] [opens_measurable_space α] {s : set α} (hs : μ s ≠ ∞) (hsc : is_compact s) {f : α → ℝ≥0} (hf : continuous f) : ∫⁻ x in s, f x ∂μ < ∞ := set_lintegral_lt_top_of_bdd_above hs hf.measurable (hsc.image hf).bdd_above lemma _root_.is_finite_measure.lintegral_lt_top_of_bounded_to_ennreal {α : Type*} [measurable_space α] (μ : measure α) [μ_fin : is_finite_measure μ] {f : α → ℝ≥0∞} (f_bdd : ∃ c : ℝ≥0, ∀ x, f x ≤ c) : ∫⁻ x, f x ∂μ < ∞ := begin cases f_bdd with c hc, apply lt_of_le_of_lt (@lintegral_mono _ _ μ _ _ hc), rw lintegral_const, exact ennreal.mul_lt_top ennreal.coe_lt_top.ne μ_fin.measure_univ_lt_top.ne, end /-- Given a measure `μ : measure α` and a function `f : α → ℝ≥0∞`, `μ.with_density f` is the measure such that for a measurable set `s` we have `μ.with_density f s = ∫⁻ a in s, f a ∂μ`. -/ def measure.with_density {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : measure α := measure.of_measurable (λs hs, ∫⁻ a in s, f a ∂μ) (by simp) (λ s hs hd, lintegral_Union hs hd _) @[simp] lemma with_density_apply (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : μ.with_density f s = ∫⁻ a in s, f a ∂μ := measure.of_measurable_apply s hs lemma with_density_congr_ae {f g : α → ℝ≥0∞} (h : f =ᵐ[μ] g) : μ.with_density f = μ.with_density g := begin apply measure.ext (λ s hs, _), rw [with_density_apply _ hs, with_density_apply _ hs], exact lintegral_congr_ae (ae_restrict_of_ae h) end lemma with_density_add {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : μ.with_density (f + g) = μ.with_density f + μ.with_density g := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.add_apply, with_density_apply _ hs, with_density_apply _ hs, ← lintegral_add hf hg], refl, end lemma with_density_smul (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : μ.with_density (r • f) = r • μ.with_density f := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.coe_smul, pi.smul_apply, with_density_apply _ hs, smul_eq_mul, ← lintegral_const_mul r hf], refl, end lemma with_density_smul' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞) : μ.with_density (r • f) = r • μ.with_density f := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.coe_smul, pi.smul_apply, with_density_apply _ hs, smul_eq_mul, ← lintegral_const_mul' r f hr], refl, end lemma is_finite_measure_with_density {f : α → ℝ≥0∞} (hf : ∫⁻ a, f a ∂μ ≠ ∞) : is_finite_measure (μ.with_density f) := { measure_univ_lt_top := by rwa [with_density_apply _ measurable_set.univ, measure.restrict_univ, lt_top_iff_ne_top] } lemma with_density_absolutely_continuous {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : μ.with_density f ≪ μ := begin refine absolutely_continuous.mk (λ s hs₁ hs₂, _), rw with_density_apply _ hs₁, exact set_lintegral_measure_zero _ _ hs₂ end @[simp] lemma with_density_zero : μ.with_density 0 = 0 := begin ext1 s hs, simp [with_density_apply _ hs], end @[simp] lemma with_density_one : μ.with_density 1 = μ := begin ext1 s hs, simp [with_density_apply _ hs], end lemma with_density_tsum {f : ℕ → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) : μ.with_density (∑' n, f n) = sum (λ n, μ.with_density (f n)) := begin ext1 s hs, simp_rw [sum_apply _ hs, with_density_apply _ hs], change ∫⁻ x in s, (∑' n, f n) x ∂μ = ∑' (i : ℕ), ∫⁻ x, f i x ∂(μ.restrict s), rw ← lintegral_tsum h, refine lintegral_congr (λ x, tsum_apply (pi.summable.2 (λ _, ennreal.summable))), end lemma with_density_indicator {s : set α} (hs : measurable_set s) (f : α → ℝ≥0∞) : μ.with_density (s.indicator f) = (μ.restrict s).with_density f := begin ext1 t ht, rw [with_density_apply _ ht, lintegral_indicator _ hs, restrict_comm hs, ← with_density_apply _ ht] end lemma with_density_indicator_one {s : set α} (hs : measurable_set s) : μ.with_density (s.indicator 1) = μ.restrict s := by rw [with_density_indicator hs, with_density_one] lemma with_density_of_real_mutually_singular {f : α → ℝ} (hf : measurable f) : μ.with_density (λ x, ennreal.of_real $ f x) ⊥ₘ μ.with_density (λ x, ennreal.of_real $ -f x) := begin set S : set α := { x | f x < 0 } with hSdef, have hS : measurable_set S := measurable_set_lt hf measurable_const, refine ⟨S, hS, _, _⟩, { rw [with_density_apply _ hS, lintegral_eq_zero_iff hf.ennreal_of_real, eventually_eq], exact (ae_restrict_mem hS).mono (λ x hx, ennreal.of_real_eq_zero.2 (le_of_lt hx)) }, { rw [with_density_apply _ hS.compl, lintegral_eq_zero_iff hf.neg.ennreal_of_real, eventually_eq], exact (ae_restrict_mem hS.compl).mono (λ x hx, ennreal.of_real_eq_zero.2 (not_lt.1 $ mt neg_pos.1 hx)) }, end lemma restrict_with_density {s : set α} (hs : measurable_set s) (f : α → ℝ≥0∞) : (μ.with_density f).restrict s = (μ.restrict s).with_density f := begin ext1 t ht, rw [restrict_apply ht, with_density_apply _ ht, with_density_apply _ (ht.inter hs), restrict_restrict ht], end lemma with_density_eq_zero {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h : μ.with_density f = 0) : f =ᵐ[μ] 0 := by rw [← lintegral_eq_zero_iff' hf, ← set_lintegral_univ, ← with_density_apply _ measurable_set.univ, h, measure.coe_zero, pi.zero_apply] lemma with_density_apply_eq_zero {f : α → ℝ≥0∞} {s : set α} (hf : measurable f) : μ.with_density f s = 0 ↔ μ ({x | f x ≠ 0} ∩ s) = 0 := begin split, { assume hs, let t := to_measurable (μ.with_density f) s, apply measure_mono_null (inter_subset_inter_right _ (subset_to_measurable (μ.with_density f) s)), have A : μ.with_density f t = 0, by rw [measure_to_measurable, hs], rw [with_density_apply f (measurable_set_to_measurable _ s), lintegral_eq_zero_iff hf, eventually_eq, ae_restrict_iff, ae_iff] at A, swap, { exact hf (measurable_set_singleton 0) }, simp only [pi.zero_apply, mem_set_of_eq, filter.mem_mk] at A, convert A, ext x, simp only [and_comm, exists_prop, mem_inter_eq, iff_self, mem_set_of_eq, mem_compl_eq, not_forall] }, { assume hs, let t := to_measurable μ ({x | f x ≠ 0} ∩ s), have A : s ⊆ t ∪ {x | f x = 0}, { assume x hx, rcases eq_or_ne (f x) 0 with fx|fx, { simp only [fx, mem_union_eq, mem_set_of_eq, eq_self_iff_true, or_true] }, { left, apply subset_to_measurable _ _, exact ⟨fx, hx⟩ } }, apply measure_mono_null A (measure_union_null _ _), { apply with_density_absolutely_continuous, rwa measure_to_measurable }, { have M : measurable_set {x : α | f x = 0} := hf (measurable_set_singleton _), rw [with_density_apply _ M, (lintegral_eq_zero_iff hf)], filter_upwards [ae_restrict_mem M], simp only [imp_self, pi.zero_apply, implies_true_iff] } } end lemma ae_with_density_iff {p : α → Prop} {f : α → ℝ≥0∞} (hf : measurable f) : (∀ᵐ x ∂(μ.with_density f), p x) ↔ ∀ᵐ x ∂μ, f x ≠ 0 → p x := begin rw [ae_iff, ae_iff, with_density_apply_eq_zero hf], congr', ext x, simp only [exists_prop, mem_inter_eq, iff_self, mem_set_of_eq, not_forall], end lemma ae_with_density_iff_ae_restrict {p : α → Prop} {f : α → ℝ≥0∞} (hf : measurable f) : (∀ᵐ x ∂(μ.with_density f), p x) ↔ (∀ᵐ x ∂(μ.restrict {x | f x ≠ 0}), p x) := begin rw [ae_with_density_iff hf, ae_restrict_iff'], { refl }, { exact hf (measurable_set_singleton 0).compl }, end lemma ae_measurable_with_density_iff {E : Type*} [normed_group E] [normed_space ℝ E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : ae_measurable g (μ.with_density (λ x, (f x : ℝ≥0∞))) ↔ ae_measurable (λ x, (f x : ℝ) • g x) μ := begin split, { rintros ⟨g', g'meas, hg'⟩, have A : measurable_set {x : α | f x ≠ 0} := (hf (measurable_set_singleton 0)).compl, refine ⟨λ x, (f x : ℝ) • g' x, hf.coe_nnreal_real.smul g'meas, _⟩, apply @ae_of_ae_restrict_of_ae_restrict_compl _ _ _ {x | f x ≠ 0}, { rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal] at hg', rw ae_restrict_iff' A, filter_upwards [hg'], assume a ha h'a, have : (f a : ℝ≥0∞) ≠ 0, by simpa only [ne.def, coe_eq_zero] using h'a, rw ha this }, { filter_upwards [ae_restrict_mem A.compl], assume x hx, simp only [not_not, mem_set_of_eq, mem_compl_eq] at hx, simp [hx] } }, { rintros ⟨g', g'meas, hg'⟩, refine ⟨λ x, (f x : ℝ)⁻¹ • g' x, hf.coe_nnreal_real.inv.smul g'meas, _⟩, rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal], filter_upwards [hg'], assume x hx h'x, rw [← hx, smul_smul, _root_.inv_mul_cancel, one_smul], simp only [ne.def, coe_eq_zero] at h'x, simpa only [nnreal.coe_eq_zero, ne.def] using h'x } end lemma ae_measurable_with_density_ennreal_iff {f : α → ℝ≥0} (hf : measurable f) {g : α → ℝ≥0∞} : ae_measurable g (μ.with_density (λ x, (f x : ℝ≥0∞))) ↔ ae_measurable (λ x, (f x : ℝ≥0∞) * g x) μ := begin split, { rintros ⟨g', g'meas, hg'⟩, have A : measurable_set {x : α | f x ≠ 0} := (hf (measurable_set_singleton 0)).compl, refine ⟨λ x, f x * g' x, hf.coe_nnreal_ennreal.smul g'meas, _⟩, apply ae_of_ae_restrict_of_ae_restrict_compl {x | f x ≠ 0}, { rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal] at hg', rw ae_restrict_iff' A, filter_upwards [hg'], assume a ha h'a, have : (f a : ℝ≥0∞) ≠ 0, by simpa only [ne.def, coe_eq_zero] using h'a, rw ha this }, { filter_upwards [ae_restrict_mem A.compl], assume x hx, simp only [not_not, mem_set_of_eq, mem_compl_eq] at hx, simp [hx] } }, { rintros ⟨g', g'meas, hg'⟩, refine ⟨λ x, (f x)⁻¹ * g' x, hf.coe_nnreal_ennreal.inv.smul g'meas, _⟩, rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal], filter_upwards [hg'], assume x hx h'x, rw [← hx, ← mul_assoc, ennreal.inv_mul_cancel h'x ennreal.coe_ne_top, one_mul] } end end lintegral end measure_theory open measure_theory measure_theory.simple_func /-- To prove something for an arbitrary measurable function into `ℝ≥0∞`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. 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] theorem measurable.ennreal_induction {α} [measurable_space α] {P : (α → ℝ≥0∞) → Prop} (h_ind : ∀ (c : ℝ≥0∞) ⦃s⦄, measurable_set s → P (indicator s (λ _, c))) (h_add : ∀ ⦃f g : α → ℝ≥0∞⦄, disjoint (support f) (support g) → measurable f → measurable g → P f → P g → P (f + g)) (h_supr : ∀ ⦃f : ℕ → α → ℝ≥0∞⦄ (hf : ∀n, measurable (f n)) (h_mono : monotone f) (hP : ∀ n, P (f n)), P (λ x, ⨆ n, f n x)) ⦃f : α → ℝ≥0∞⦄ (hf : measurable f) : P f := begin convert h_supr (λ n, (eapprox f n).measurable) (monotone_eapprox f) _, { ext1 x, rw [supr_eapprox_apply f hf] }, { exact λ n, simple_func.induction (λ c s hs, h_ind c hs) (λ f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) } end namespace measure_theory variables {α : Type*} {m m0 : measurable_space α} include m /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(μ.with_density f)` as an integral with respect to `μ`, called the base measure. `μ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(μ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ lemma lintegral_with_density_eq_lintegral_mul (μ : measure α) {f : α → ℝ≥0∞} (h_mf : measurable f) : ∀ {g : α → ℝ≥0∞}, measurable g → ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin apply measurable.ennreal_induction, { intros c s h_ms, simp [*, mul_comm _ c, ← indicator_mul_right], }, { intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h, simp [mul_add, *, measurable.mul] }, { intros g h_mea_g h_mono_g h_ind, have : monotone (λ n a, f a * g n a) := λ m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x), simp [lintegral_supr, ennreal.mul_supr, h_mf.mul (h_mea_g _), *] } end lemma set_lintegral_with_density_eq_set_lintegral_mul (μ : measure α) {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) {s : set α} (hs : measurable_set s) : ∫⁻ x in s, g x ∂μ.with_density f = ∫⁻ x in s, (f * g) x ∂μ := by rw [restrict_with_density hs, lintegral_with_density_eq_lintegral_mul _ hf hg] /-- The Lebesgue integral of `g` with respect to the measure `μ.with_density f` coincides with the integral of `f * g`. This version assumes that `g` is almost everywhere measurable. For a version without conditions on `g` but requiring that `f` is almost everywhere finite, see `lintegral_with_density_eq_lintegral_mul_non_measurable` -/ lemma lintegral_with_density_eq_lintegral_mul₀' {μ : measure α} {f : α → ℝ≥0∞} (hf : ae_measurable f μ) {g : α → ℝ≥0∞} (hg : ae_measurable g (μ.with_density f)) : ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin let f' := hf.mk f, have : μ.with_density f = μ.with_density f' := with_density_congr_ae hf.ae_eq_mk, rw this at ⊢ hg, let g' := hg.mk g, calc ∫⁻ a, g a ∂(μ.with_density f') = ∫⁻ a, g' a ∂(μ.with_density f') : lintegral_congr_ae hg.ae_eq_mk ... = ∫⁻ a, (f' * g') a ∂μ : lintegral_with_density_eq_lintegral_mul _ hf.measurable_mk hg.measurable_mk ... = ∫⁻ a, (f' * g) a ∂μ : begin apply lintegral_congr_ae, apply ae_of_ae_restrict_of_ae_restrict_compl {x | f' x ≠ 0}, { have Z := hg.ae_eq_mk, rw [eventually_eq, ae_with_density_iff_ae_restrict hf.measurable_mk] at Z, filter_upwards [Z], assume x hx, simp only [hx, pi.mul_apply] }, { have M : measurable_set {x : α | f' x ≠ 0}ᶜ := (hf.measurable_mk (measurable_set_singleton 0).compl).compl, filter_upwards [ae_restrict_mem M], assume x hx, simp only [not_not, mem_set_of_eq, mem_compl_eq] at hx, simp only [hx, zero_mul, pi.mul_apply] } end ... = ∫⁻ (a : α), (f * g) a ∂μ : begin apply lintegral_congr_ae, filter_upwards [hf.ae_eq_mk], assume x hx, simp only [hx, pi.mul_apply], end end lemma lintegral_with_density_eq_lintegral_mul₀ {μ : measure α} {f : α → ℝ≥0∞} (hf : ae_measurable f μ) {g : α → ℝ≥0∞} (hg : ae_measurable g μ) : ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := lintegral_with_density_eq_lintegral_mul₀' hf (hg.mono' (with_density_absolutely_continuous μ f)) lemma lintegral_with_density_le_lintegral_mul (μ : measure α) {f : α → ℝ≥0∞} (f_meas : measurable f) (g : α → ℝ≥0∞) : ∫⁻ a, g a ∂(μ.with_density f) ≤ ∫⁻ a, (f * g) a ∂μ := begin rw [← supr_lintegral_measurable_le_eq_lintegral, ← supr_lintegral_measurable_le_eq_lintegral], refine supr₂_le (λ i i_meas, supr_le (λ hi, _)), have A : f * i ≤ f * g := λ x, ennreal.mul_le_mul le_rfl (hi x), refine le_supr₂_of_le (f * i) (f_meas.mul i_meas) _, exact le_supr_of_le A (le_of_eq (lintegral_with_density_eq_lintegral_mul _ f_meas i_meas)) end lemma lintegral_with_density_eq_lintegral_mul_non_measurable (μ : measure α) {f : α → ℝ≥0∞} (f_meas : measurable f) (hf : ∀ᵐ x ∂μ, f x < ∞) (g : α → ℝ≥0∞) : ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin refine le_antisymm (lintegral_with_density_le_lintegral_mul μ f_meas g) _, rw [← supr_lintegral_measurable_le_eq_lintegral, ← supr_lintegral_measurable_le_eq_lintegral], refine supr₂_le (λ i i_meas, supr_le $ λ hi, _), have A : (λ x, (f x)⁻¹ * i x) ≤ g, { assume x, dsimp, rw [mul_comm, ← div_eq_mul_inv], exact div_le_of_le_mul' (hi x), }, refine le_supr_of_le (λ x, (f x)⁻¹ * i x) (le_supr_of_le (f_meas.inv.mul i_meas) _), refine le_supr_of_le A _, rw lintegral_with_density_eq_lintegral_mul _ f_meas (f_meas.inv.mul i_meas), apply lintegral_mono_ae, filter_upwards [hf], assume x h'x, rcases eq_or_ne (f x) 0 with hx|hx, { have := hi x, simp only [hx, zero_mul, pi.mul_apply, nonpos_iff_eq_zero] at this, simp [this] }, { apply le_of_eq _, dsimp, rw [← mul_assoc, ennreal.mul_inv_cancel hx h'x.ne, one_mul] } end lemma set_lintegral_with_density_eq_set_lintegral_mul_non_measurable (μ : measure α) {f : α → ℝ≥0∞} (f_meas : measurable f) (g : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) (hf : ∀ᵐ x ∂(μ.restrict s), f x < ∞) : ∫⁻ a in s, g a ∂(μ.with_density f) = ∫⁻ a in s, (f * g) a ∂μ := by rw [restrict_with_density hs, lintegral_with_density_eq_lintegral_mul_non_measurable _ f_meas hf] lemma lintegral_with_density_eq_lintegral_mul_non_measurable₀ (μ : measure α) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h'f : ∀ᵐ x ∂μ, f x < ∞) (g : α → ℝ≥0∞) : ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin let f' := hf.mk f, calc ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, g a ∂(μ.with_density f') : by rw with_density_congr_ae hf.ae_eq_mk ... = ∫⁻ a, (f' * g) a ∂μ : begin apply lintegral_with_density_eq_lintegral_mul_non_measurable _ hf.measurable_mk, filter_upwards [h'f, hf.ae_eq_mk], assume x hx h'x, rwa ← h'x, end ... = ∫⁻ a, (f * g) a ∂μ : begin apply lintegral_congr_ae, filter_upwards [hf.ae_eq_mk], assume x hx, simp only [hx, pi.mul_apply], end end lemma set_lintegral_with_density_eq_set_lintegral_mul_non_measurable₀ (μ : measure α) {f : α → ℝ≥0∞} {s : set α} (hf : ae_measurable f (μ.restrict s)) (g : α → ℝ≥0∞) (hs : measurable_set s) (h'f : ∀ᵐ x ∂(μ.restrict s), f x < ∞) : ∫⁻ a in s, g a ∂(μ.with_density f) = ∫⁻ a in s, (f * g) a ∂μ := by rw [restrict_with_density hs, lintegral_with_density_eq_lintegral_mul_non_measurable₀ _ hf h'f] lemma with_density_mul (μ : measure α) {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : μ.with_density (f * g) = (μ.with_density f).with_density g := begin ext1 s hs, simp [with_density_apply _ hs, restrict_with_density hs, lintegral_with_density_eq_lintegral_mul _ hf hg], end /-- In a sigma-finite measure space, there exists an integrable function which is positive everywhere (and with an arbitrarily small integral). -/ lemma exists_pos_lintegral_lt_of_sigma_finite (μ : measure α) [sigma_finite μ] {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, 0 < g x) ∧ measurable g ∧ (∫⁻ x, g x ∂μ < ε) := begin /- Let `s` be a covering of `α` by pairwise disjoint measurable sets of finite measure. Let `δ : ℕ → ℝ≥0` be a positive function such that `∑' i, μ (s i) * δ i < ε`. Then the function that is equal to `δ n` on `s n` is a positive function with integral less than `ε`. -/ set s : ℕ → set α := disjointed (spanning_sets μ), have : ∀ n, μ (s n) < ∞, from λ n, (measure_mono $ disjointed_subset _ _).trans_lt (measure_spanning_sets_lt_top μ n), obtain ⟨δ, δpos, δsum⟩ : ∃ δ : ℕ → ℝ≥0, (∀ i, 0 < δ i) ∧ ∑' i, μ (s i) * δ i < ε, from ennreal.exists_pos_tsum_mul_lt_of_encodable ε0 _ (λ n, (this n).ne), set N : α → ℕ := spanning_sets_index μ, have hN_meas : measurable N := measurable_spanning_sets_index μ, have hNs : ∀ n, N ⁻¹' {n} = s n := preimage_spanning_sets_index_singleton μ, refine ⟨δ ∘ N, λ x, δpos _, measurable_from_nat.comp hN_meas, _⟩, simpa [lintegral_comp measurable_from_nat.coe_nnreal_ennreal hN_meas, hNs, lintegral_encodable, measurable_spanning_sets_index, mul_comm] using δsum, end lemma lintegral_trim {μ : measure α} (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : measurable[m] f) : ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ := begin refine @measurable.ennreal_induction α m (λ f, ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ) _ _ _ f hf, { intros c s hs, rw [lintegral_indicator _ hs, lintegral_indicator _ (hm s hs), set_lintegral_const, set_lintegral_const], suffices h_trim_s : μ.trim hm s = μ s, by rw h_trim_s, exact trim_measurable_set_eq hm hs, }, { intros f g hfg hf hg hf_prop hg_prop, have h_m := lintegral_add hf hg, have h_m0 := lintegral_add (measurable.mono hf hm le_rfl) (measurable.mono hg hm le_rfl), rwa [hf_prop, hg_prop, ← h_m0] at h_m, }, { intros f hf hf_mono hf_prop, rw lintegral_supr hf hf_mono, rw lintegral_supr (λ n, measurable.mono (hf n) hm le_rfl) hf_mono, congr, exact funext (λ n, hf_prop n), }, end lemma lintegral_trim_ae {μ : measure α} (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : ae_measurable f (μ.trim hm)) : ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ := by rw [lintegral_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk), lintegral_congr_ae hf.ae_eq_mk, lintegral_trim hm hf.measurable_mk] section sigma_finite variables {E : Type*} [normed_group E] [measurable_space E] [opens_measurable_space E] lemma univ_le_of_forall_fin_meas_le {μ : measure α} (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (C : ℝ≥0∞) {f : set α → ℝ≥0∞} (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → f s ≤ C) (h_F_lim : ∀ S : ℕ → set α, (∀ n, measurable_set[m] (S n)) → monotone S → f (⋃ n, S n) ≤ ⨆ n, f (S n)) : f univ ≤ C := begin let S := @spanning_sets _ m (μ.trim hm) _, have hS_mono : monotone S, from @monotone_spanning_sets _ m (μ.trim hm) _, have hS_meas : ∀ n, measurable_set[m] (S n), from @measurable_spanning_sets _ m (μ.trim hm) _, rw ← @Union_spanning_sets _ m (μ.trim hm), refine (h_F_lim S hS_meas hS_mono).trans _, refine supr_le (λ n, hf (S n) (hS_meas n) _), exact ((le_trim hm).trans_lt (@measure_spanning_sets_lt_top _ m (μ.trim hm) _ n)).ne, end /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure in a sub-σ-algebra and the measure is σ-finite on that sub-σ-algebra, then the integral over the whole space is bounded by that same constant. Version for a measurable function. See `lintegral_le_of_forall_fin_meas_le'` for the more general `ae_measurable` version. -/ lemma lintegral_le_of_forall_fin_meas_le_of_measurable {μ : measure α} (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (C : ℝ≥0∞) {f : α → ℝ≥0∞} (hf_meas : measurable f) (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := begin have : ∫⁻ x in univ, f x ∂μ = ∫⁻ x, f x ∂μ, by simp only [measure.restrict_univ], rw ← this, refine univ_le_of_forall_fin_meas_le hm C hf (λ S hS_meas hS_mono, _), rw ← lintegral_indicator, swap, { exact hm (⋃ n, S n) (@measurable_set.Union _ _ m _ _ hS_meas), }, have h_integral_indicator : (⨆ n, ∫⁻ x in S n, f x ∂μ) = ⨆ n, ∫⁻ x, (S n).indicator f x ∂μ, { congr, ext1 n, rw lintegral_indicator _ (hm _ (hS_meas n)), }, rw [h_integral_indicator, ← lintegral_supr], { refine le_of_eq (lintegral_congr (λ x, _)), simp_rw indicator_apply, by_cases hx_mem : x ∈ Union S, { simp only [hx_mem, if_true], obtain ⟨n, hxn⟩ := mem_Union.mp hx_mem, refine le_antisymm (trans _ (le_supr _ n)) (supr_le (λ i, _)), { simp only [hxn, le_refl, if_true], }, { by_cases hxi : x ∈ S i; simp [hxi], }, }, { simp only [hx_mem, if_false], rw mem_Union at hx_mem, push_neg at hx_mem, refine le_antisymm (zero_le _) (supr_le (λ n, _)), simp only [hx_mem n, if_false, nonpos_iff_eq_zero], }, }, { exact λ n, hf_meas.indicator (hm _ (hS_meas n)), }, { intros n₁ n₂ hn₁₂ a, simp_rw indicator_apply, split_ifs, { exact le_rfl, }, { exact absurd (mem_of_mem_of_subset h (hS_mono hn₁₂)) h_1, }, { exact zero_le _, }, { exact le_rfl, }, }, end /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure in a sub-σ-algebra and the measure is σ-finite on that sub-σ-algebra, then the integral over the whole space is bounded by that same constant. -/ lemma lintegral_le_of_forall_fin_meas_le' {μ : measure α} (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (C : ℝ≥0∞) {f : _ → ℝ≥0∞} (hf_meas : ae_measurable f μ) (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := begin let f' := hf_meas.mk f, have hf' : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f' x ∂μ ≤ C, { refine λ s hs hμs, (le_of_eq _).trans (hf s hs hμs), refine lintegral_congr_ae (ae_restrict_of_ae (hf_meas.ae_eq_mk.mono (λ x hx, _))), rw hx, }, rw lintegral_congr_ae hf_meas.ae_eq_mk, exact lintegral_le_of_forall_fin_meas_le_of_measurable hm C hf_meas.measurable_mk hf', end omit m /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure and the measure is σ-finite, then the integral over the whole space is bounded by that same constant. -/ lemma lintegral_le_of_forall_fin_meas_le [measurable_space α] {μ : measure α} [sigma_finite μ] (C : ℝ≥0∞) {f : α → ℝ≥0∞} (hf_meas : ae_measurable f μ) (hf : ∀ s, measurable_set s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := @lintegral_le_of_forall_fin_meas_le' _ _ _ _ _ (by rwa trim_eq_self) C _ hf_meas hf /-- A sigma-finite measure is absolutely continuous with respect to some finite measure. -/ lemma exists_absolutely_continuous_is_finite_measure {m : measurable_space α} (μ : measure α) [sigma_finite μ] : ∃ (ν : measure α), is_finite_measure ν ∧ μ ≪ ν := begin obtain ⟨g, gpos, gmeas, hg⟩ : ∃ (g : α → ℝ≥0), (∀ (x : α), 0 < g x) ∧ measurable g ∧ ∫⁻ (x : α), ↑(g x) ∂μ < 1 := exists_pos_lintegral_lt_of_sigma_finite μ (ennreal.zero_lt_one).ne', refine ⟨μ.with_density (λ x, g x), is_finite_measure_with_density hg.ne_top, _⟩, have : μ = (μ.with_density (λ x, g x)).with_density (λ x, (g x)⁻¹), { have A : (λ (x : α), (g x : ℝ≥0∞)) * (λ (x : α), (↑(g x))⁻¹) = 1, { ext1 x, exact ennreal.mul_inv_cancel (ennreal.coe_ne_zero.2 ((gpos x).ne')) ennreal.coe_ne_top }, rw [← with_density_mul _ gmeas.coe_nnreal_ennreal gmeas.coe_nnreal_ennreal.inv, A, with_density_one] }, conv_lhs { rw this }, exact with_density_absolutely_continuous _ _, end end sigma_finite end measure_theory
dd1acebd6f6430524cb7dbca7f1927ebcd121f86
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/images.lean
dedd6ed32d2d5eb8869ec194bb5d30bfff73c8ae
[]
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
31,977
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.equalizers import Mathlib.category_theory.limits.shapes.pullbacks import Mathlib.category_theory.limits.shapes.strong_epi import Mathlib.PostPort universes v u l namespace Mathlib /-! # Categorical images We define the categorical image of `f` as a factorisation `f = e ≫ m` through a monomorphism `m`, so that `m` factors through the `m'` in any other such factorisation. ## Main definitions * A `mono_factorisation` is a factorisation `f = e ≫ m`, where `m` is a monomorphism * `is_image F` means that a given mono factorisation `F` has the universal property of the image. * `has_image f` means that we have chosen an image for the morphism `f : X ⟶ Y`. * In this case, `image f` is the image object, `image.ι f : image f ⟶ Y` is the monomorphism `m` of the factorisation and `factor_thru_image f : X ⟶ image f` is the morphism `e`. * `has_images C` means that every morphism in `C` has an image. * Let `f : X ⟶ Y` and `g : P ⟶ Q` be morphisms in `C`, which we will represent as objects of the arrow category `arrow C`. Then `sq : f ⟶ g` is a commutative square in `C`. If `f` and `g` have images, then `has_image_map sq` represents the fact that there is a morphism `i : image f ⟶ image g` making the diagram X ----→ image f ----→ Y | | | | | | ↓ ↓ ↓ P ----→ image g ----→ Q commute, where the top row is the image factorisation of `f`, the bottom row is the image factorisation of `g`, and the outer rectangle is the commutative square `sq`. * If a category `has_images`, then `has_image_maps` means that every commutative square admits an image map. * If a category `has_images`, then `has_strong_epi_images` means that the morphism to the image is always a strong epimorphism. ## Main statements * When `C` has equalizers, the morphism `e` appearing in an image factorisation is an epimorphism. * When `C` has strong epi images, then these images admit image maps. ## Future work * TODO: coimages, and abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. -/ namespace category_theory.limits /-- A factorisation of a morphism `f = e ≫ m`, with `m` monic. -/ structure mono_factorisation {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) where I : C m : I ⟶ Y m_mono : mono m e : X ⟶ I fac' : autoParam (e ≫ m = f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) @[simp] theorem mono_factorisation.fac {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (c : mono_factorisation f) : mono_factorisation.e c ≫ mono_factorisation.m c = f := sorry @[simp] theorem mono_factorisation.fac_assoc {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (c : mono_factorisation f) {X' : C} (f' : Y ⟶ X') : mono_factorisation.e c ≫ mono_factorisation.m c ≫ f' = f ≫ f' := sorry namespace mono_factorisation /-- The obvious factorisation of a monomorphism through itself. -/ def self {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : mono_factorisation f := mk X f 𝟙 -- I'm not sure we really need this, but the linter says that an inhabited instance ought to exist... protected instance inhabited {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : Inhabited (mono_factorisation f) := { default := self f } /-- The morphism `m` in a factorisation `f = e ≫ m` through a monomorphism is uniquely determined. -/ theorem ext {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {F : mono_factorisation f} {F' : mono_factorisation f} (hI : I F = I F') (hm : m F = eq_to_hom hI ≫ m F') : F = F' := sorry end mono_factorisation /-- Data exhibiting that a given factorisation through a mono is initial. -/ structure is_image {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (F : mono_factorisation f) where lift : (F' : mono_factorisation f) → mono_factorisation.I F ⟶ mono_factorisation.I F' lift_fac' : autoParam (∀ (F' : mono_factorisation f), lift F' ≫ mono_factorisation.m F' = mono_factorisation.m F) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) @[simp] theorem is_image.lift_fac {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} (c : is_image F) (F' : mono_factorisation f) : is_image.lift c F' ≫ mono_factorisation.m F' = mono_factorisation.m F := sorry @[simp] theorem is_image.lift_fac_assoc {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} (c : is_image F) (F' : mono_factorisation f) {X' : C} (f' : Y ⟶ X') : is_image.lift c F' ≫ mono_factorisation.m F' ≫ f' = mono_factorisation.m F ≫ f' := sorry @[simp] theorem is_image.fac_lift_assoc {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} (hF : is_image F) (F' : mono_factorisation f) {X' : C} (f' : mono_factorisation.I F' ⟶ X') : mono_factorisation.e F ≫ is_image.lift hF F' ≫ f' = mono_factorisation.e F' ≫ f' := sorry namespace is_image /-- The trivial factorisation of a monomorphism satisfies the universal property. -/ def self {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : is_image (mono_factorisation.self f) := mk fun (F' : mono_factorisation f) => mono_factorisation.e F' protected instance inhabited {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : Inhabited (is_image (mono_factorisation.self f)) := { default := self f } /-- Two factorisations through monomorphisms satisfying the universal property must factor through isomorphic objects. -/ -- TODO this is another good candidate for a future `unique_up_to_canonical_iso`. @[simp] theorem iso_ext_hom {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} {F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : iso.hom (iso_ext hF hF') = lift hF F' := Eq.refl (iso.hom (iso_ext hF hF')) theorem iso_ext_hom_m {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} {F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : iso.hom (iso_ext hF hF') ≫ mono_factorisation.m F' = mono_factorisation.m F := sorry theorem iso_ext_inv_m {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} {F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : iso.inv (iso_ext hF hF') ≫ mono_factorisation.m F = mono_factorisation.m F' := sorry theorem e_iso_ext_hom {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} {F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : mono_factorisation.e F ≫ iso.hom (iso_ext hF hF') = mono_factorisation.e F' := sorry theorem e_iso_ext_inv {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {F : mono_factorisation f} {F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : mono_factorisation.e F' ≫ iso.inv (iso_ext hF hF') = mono_factorisation.e F := sorry end is_image /-- Data exhibiting that a morphism `f` has an image. -/ structure image_factorisation {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) where F : mono_factorisation f is_image : is_image F protected instance inhabited_image_factorisation {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : Inhabited (image_factorisation f) := { default := image_factorisation.mk (mono_factorisation.self f) (is_image.self f) } /-- `has_image f` means that there exists an image factorisation of `f`. -/ class has_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) mk' :: where (exists_image : Nonempty (image_factorisation f)) theorem has_image.mk {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (F : image_factorisation f) : has_image f := has_image.mk' (Nonempty.intro F) /-- The chosen factorisation of `f` through a monomorphism. -/ def image.mono_factorisation {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : mono_factorisation f := image_factorisation.F (Classical.choice has_image.exists_image) /-- The witness of the universal property for the chosen factorisation of `f` through a monomorphism. -/ def image.is_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : is_image (image.mono_factorisation f) := image_factorisation.is_image (Classical.choice has_image.exists_image) /-- The categorical image of a morphism. -/ /-- The inclusion of the image of a morphism into the target. -/ def image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : C := mono_factorisation.I sorry def image.ι {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : image f ⟶ Y := mono_factorisation.m (image.mono_factorisation f) @[simp] theorem image.as_ι {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : mono_factorisation.m (image.mono_factorisation f) = image.ι f := rfl protected instance image.ι.category_theory.mono {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : mono (image.ι f) := mono_factorisation.m_mono (image.mono_factorisation f) /-- The map from the source to the image of a morphism. -/ /-- Rewrite in terms of the `factor_thru_image` interface. -/ def factor_thru_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : X ⟶ image f := mono_factorisation.e (image.mono_factorisation f) @[simp] theorem as_factor_thru_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : mono_factorisation.e (image.mono_factorisation f) = factor_thru_image f := rfl @[simp] theorem image.fac_assoc {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] {X' : C} (f' : Y ⟶ X') : factor_thru_image f ≫ image.ι f ≫ f' = f ≫ f' := sorry /-- Any other factorisation of the morphism `f` through a monomorphism receives a map from the image. -/ def image.lift {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F' : mono_factorisation f) : image f ⟶ mono_factorisation.I F' := is_image.lift (image.is_image f) F' @[simp] theorem image.lift_fac {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F' : mono_factorisation f) : image.lift F' ≫ mono_factorisation.m F' = image.ι f := is_image.lift_fac' (image.is_image f) F' @[simp] theorem image.fac_lift {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F' : mono_factorisation f) : factor_thru_image f ≫ image.lift F' = mono_factorisation.e F' := is_image.fac_lift (image.is_image f) F' @[simp] theorem is_image.lift_ι_assoc {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] {F : mono_factorisation f} (hF : is_image F) {X' : C} (f' : Y ⟶ X') : is_image.lift hF (image.mono_factorisation f) ≫ image.ι f ≫ f' = mono_factorisation.m F ≫ f' := sorry -- TODO we could put a category structure on `mono_factorisation f`, -- with the morphisms being `g : I ⟶ I'` commuting with the `m`s -- (they then automatically commute with the `e`s) -- and show that an `image_of f` gives an initial object there -- (uniqueness of the lift comes for free). protected instance lift_mono {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F' : mono_factorisation f) : mono (image.lift F') := mono_of_mono (image.lift F') (mono_factorisation.m F') theorem has_image.uniq {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F' : mono_factorisation f) (l : image f ⟶ mono_factorisation.I F') (w : l ≫ mono_factorisation.m F' = image.ι f) : l = image.lift F' := sorry /-- `has_images` represents a choice of image for every morphism -/ class has_images (C : Type u) [category C] where has_image : ∀ {X Y : C} (f : X ⟶ Y), has_image f /-- The image of a monomorphism is isomorphic to the source. -/ def image_mono_iso_source {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [mono f] : image f ≅ X := is_image.iso_ext (image.is_image f) (is_image.self f) @[simp] theorem image_mono_iso_source_inv_ι {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [mono f] : iso.inv (image_mono_iso_source f) ≫ image.ι f = f := sorry @[simp] theorem image_mono_iso_source_hom_self_assoc {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [mono f] {X' : C} (f' : Y ⟶ X') : iso.hom (image_mono_iso_source f) ≫ f ≫ f' = image.ι f ≫ f' := sorry -- This is the proof that `factor_thru_image f` is an epimorphism -- from https://en.wikipedia.org/wiki/Image_(category_theory), which is in turn taken from: -- Mitchell, Barry (1965), Theory of categories, MR 0202787, p.12, Proposition 10.1 theorem image.ext {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] {W : C} {g : image f ⟶ W} {h : image f ⟶ W} [has_limit (parallel_pair g h)] (w : factor_thru_image f ≫ g = factor_thru_image f ≫ h) : g = h := sorry protected instance factor_thru_image.category_theory.epi {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [∀ {Z : C} (g h : image f ⟶ Z), has_limit (parallel_pair g h)] : epi (factor_thru_image f) := epi.mk fun (Z : C) (g h : image f ⟶ Z) (w : factor_thru_image f ≫ g = factor_thru_image f ≫ h) => image.ext f w theorem epi_image_of_epi {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [E : epi f] : epi (image.ι f) := epi_of_epi (factor_thru_image f) (image.ι f) theorem epi_of_epi_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] [epi (image.ι f)] [epi (factor_thru_image f)] : epi f := eq.mpr (id (Eq._oldrec (Eq.refl (epi f)) (Eq.symm (image.fac f)))) (epi_comp (factor_thru_image f) (image.ι f)) /-- An equation between morphisms gives a comparison map between the images (which momentarily we prove is an iso). -/ def image.eq_to_hom {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {f' : X ⟶ Y} [has_image f] [has_image f'] (h : f = f') : image f ⟶ image f' := image.lift (mono_factorisation.mk (image f') (image.ι f') (factor_thru_image f')) protected instance image.eq_to_hom.category_theory.is_iso {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {f' : X ⟶ Y} [has_image f] [has_image f'] (h : f = f') : is_iso (image.eq_to_hom h) := is_iso.mk (image.eq_to_hom sorry) /-- An equation between morphisms gives an isomorphism between the images. -/ def image.eq_to_iso {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {f' : X ⟶ Y} [has_image f] [has_image f'] (h : f = f') : image f ≅ image f' := as_iso (image.eq_to_hom h) /-- As long as the category has equalizers, the image inclusion maps commute with `image.eq_to_iso`. -/ theorem image.eq_fac {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {f' : X ⟶ Y} [has_image f] [has_image f'] [has_equalizers C] (h : f = f') : image.ι f = iso.hom (image.eq_to_iso h) ≫ image.ι f' := sorry /-- The comparison map `image (f ≫ g) ⟶ image g`. -/ def image.pre_comp {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) [has_image g] [has_image (f ≫ g)] : image (f ≫ g) ⟶ image g := image.lift (mono_factorisation.mk (image g) (image.ι g) (f ≫ factor_thru_image g)) @[simp] theorem image.factor_thru_image_pre_comp {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) [has_image g] [has_image (f ≫ g)] : factor_thru_image (f ≫ g) ≫ image.pre_comp f g = f ≫ factor_thru_image g := sorry /-- The two step comparison map `image (f ≫ (g ≫ h)) ⟶ image (g ≫ h) ⟶ image h` agrees with the one step comparison map `image (f ≫ (g ≫ h)) ≅ image ((f ≫ g) ≫ h) ⟶ image h`. -/ theorem image.pre_comp_comp {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) {W : C} (h : Z ⟶ W) [has_image (g ≫ h)] [has_image (f ≫ g ≫ h)] [has_image h] [has_image ((f ≫ g) ≫ h)] : image.pre_comp f (g ≫ h) ≫ image.pre_comp g h = image.eq_to_hom (Eq.symm (category.assoc f g h)) ≫ image.pre_comp (f ≫ g) h := sorry /-- `image.pre_comp f g` is an isomorphism when `f` is an isomorphism (we need `C` to have equalizers to prove this). -/ protected instance image.is_iso_precomp_iso {C : Type u} [category C] {X : C} {Y : C} {Z : C} (g : Y ⟶ Z) [has_equalizers C] (f : X ≅ Y) [has_image g] [has_image (iso.hom f ≫ g)] : is_iso (image.pre_comp (iso.hom f) g) := is_iso.mk (image.lift (mono_factorisation.mk (image (iso.hom f ≫ g)) (image.ι (iso.hom f ≫ g)) (iso.inv f ≫ factor_thru_image (iso.hom f ≫ g)))) -- Note that in general we don't have the other comparison map you might expect -- `image f ⟶ image (f ≫ g)`. /-- Postcomposing by an isomorphism induces an isomorphism on the image. -/ def image.post_comp_is_iso {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) [has_equalizers C] [is_iso g] [has_image f] [has_image (f ≫ g)] : image f ≅ image (f ≫ g) := iso.mk (image.lift (mono_factorisation.mk (image (f ≫ g)) (image.ι (f ≫ g) ≫ inv g) (factor_thru_image (f ≫ g)))) (image.lift (mono_factorisation.mk (image f) (image.ι f ≫ g) (factor_thru_image f))) @[simp] theorem image.post_comp_is_iso_hom_comp_image_ι_assoc {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) [has_equalizers C] [is_iso g] [has_image f] [has_image (f ≫ g)] {X' : C} (f' : Z ⟶ X') : iso.hom (image.post_comp_is_iso f g) ≫ image.ι (f ≫ g) ≫ f' = image.ι f ≫ g ≫ f' := sorry @[simp] theorem image.post_comp_is_iso_inv_comp_image_ι_assoc {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) {Z : C} (g : Y ⟶ Z) [has_equalizers C] [is_iso g] [has_image f] [has_image (f ≫ g)] {X' : C} (f' : Y ⟶ X') : iso.inv (image.post_comp_is_iso f g) ≫ image.ι f ≫ f' = image.ι (f ≫ g) ≫ inv g ≫ f' := sorry end category_theory.limits namespace category_theory.limits protected instance hom.has_image {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [has_image f] : has_image (comma.hom (arrow.mk f)) := (fun (this : has_image f) => this) _inst_2 /-- An image map is a morphism `image f → image g` fitting into a commutative square and satisfying the obvious commutativity conditions. -/ structure image_map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) where map : image (comma.hom f) ⟶ image (comma.hom g) map_ι' : autoParam (map ≫ image.ι (comma.hom g) = image.ι (comma.hom f) ≫ comma_morphism.right sq) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) protected instance inhabited_image_map {C : Type u} [category C] {f : arrow C} [has_image (comma.hom f)] : Inhabited (image_map 𝟙) := { default := image_map.mk 𝟙 } @[simp] theorem image_map.map_ι {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] {sq : f ⟶ g} (c : image_map sq) : image_map.map c ≫ image.ι (comma.hom g) = image.ι (comma.hom f) ≫ comma_morphism.right sq := sorry @[simp] theorem image_map.map_ι_assoc {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] {sq : f ⟶ g} (c : image_map sq) {X' : C} (f' : comma.right g ⟶ X') : image_map.map c ≫ image.ι (comma.hom g) ≫ f' = image.ι (comma.hom f) ≫ comma_morphism.right sq ≫ f' := sorry @[simp] theorem image_map.factor_map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) (m : image_map sq) : factor_thru_image (comma.hom f) ≫ image_map.map m = comma_morphism.left sq ≫ factor_thru_image (comma.hom g) := sorry /-- To give an image map for a commutative square with `f` at the top and `g` at the bottom, it suffices to give a map between any mono factorisation of `f` and any image factorisation of `g`. -/ def image_map.transport {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) (F : mono_factorisation (comma.hom f)) {F' : mono_factorisation (comma.hom g)} (hF' : is_image F') {map : mono_factorisation.I F ⟶ mono_factorisation.I F'} (map_ι : map ≫ mono_factorisation.m F' = mono_factorisation.m F ≫ comma_morphism.right sq) : image_map sq := image_map.mk (image.lift F ≫ map ≫ is_image.lift hF' (image.mono_factorisation (comma.hom g))) /-- `has_image_map sq` means that there is an `image_map` for the square `sq`. -/ class has_image_map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) mk' :: where (has_image_map : Nonempty (image_map sq)) theorem has_image_map.mk {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] {sq : f ⟶ g} (m : image_map sq) : has_image_map sq := has_image_map.mk' (Nonempty.intro m) theorem has_image_map.transport {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) (F : mono_factorisation (comma.hom f)) {F' : mono_factorisation (comma.hom g)} (hF' : is_image F') (map : mono_factorisation.I F ⟶ mono_factorisation.I F') (map_ι : map ≫ mono_factorisation.m F' = mono_factorisation.m F ≫ comma_morphism.right sq) : has_image_map sq := has_image_map.mk (image_map.transport sq F hF' map_ι) /-- Obtain an `image_map` from a `has_image_map` instance. -/ def has_image_map.image_map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] : image_map sq := Classical.choice has_image_map.has_image_map theorem image_map.ext_iff {C : Type u} {_inst_1 : category C} {f : arrow C} {g : arrow C} {_inst_2 : has_image (comma.hom f)} {_inst_3 : has_image (comma.hom g)} {sq : f ⟶ g} (x : image_map sq) (y : image_map sq) : x = y ↔ image_map.map x = image_map.map y := sorry protected instance image_map.subsingleton {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) : subsingleton (image_map sq) := subsingleton.intro fun (a b : image_map sq) => image_map.ext a b (iff.mp (cancel_mono (image.ι (comma.hom g))) (eq.mpr (id ((fun (a a_1 : image (comma.hom f) ⟶ functor.obj 𝟭 (comma.right g)) (e_1 : a = a_1) (ᾰ ᾰ_1 : image (comma.hom f) ⟶ functor.obj 𝟭 (comma.right g)) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (image_map.map a ≫ image.ι (comma.hom g)) (image.ι (comma.hom f) ≫ comma_morphism.right sq) (image_map.map_ι a) (image_map.map b ≫ image.ι (comma.hom g)) (image.ι (comma.hom f) ≫ comma_morphism.right sq) (image_map.map_ι b))) (Eq.refl (image.ι (comma.hom f) ≫ comma_morphism.right sq)))) /-- The map on images induced by a commutative square. -/ def image.map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] : image (comma.hom f) ⟶ image (comma.hom g) := image_map.map (has_image_map.image_map sq) theorem image.factor_map {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] : factor_thru_image (comma.hom f) ≫ image.map sq = comma_morphism.left sq ≫ factor_thru_image (comma.hom g) := sorry theorem image.map_ι {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] : image.map sq ≫ image.ι (comma.hom g) = image.ι (comma.hom f) ≫ comma_morphism.right sq := sorry theorem image.map_hom_mk'_ι {C : Type u} [category C] {X : C} {Y : C} {P : C} {Q : C} {k : X ⟶ Y} [has_image k] {l : P ⟶ Q} [has_image l] {m : X ⟶ P} {n : Y ⟶ Q} (w : m ≫ l = k ≫ n) [has_image_map (arrow.hom_mk' w)] : image.map (arrow.hom_mk' w) ≫ image.ι l = image.ι k ≫ n := image.map_ι (arrow.hom_mk' w) /-- Image maps for composable commutative squares induce an image map in the composite square. -/ def image_map_comp {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] {h : arrow C} [has_image (comma.hom h)] (sq' : g ⟶ h) [has_image_map sq'] : image_map (sq ≫ sq') := image_map.mk (image.map sq ≫ image.map sq') @[simp] theorem image.map_comp {C : Type u} [category C] {f : arrow C} {g : arrow C} [has_image (comma.hom f)] [has_image (comma.hom g)] (sq : f ⟶ g) [has_image_map sq] {h : arrow C} [has_image (comma.hom h)] (sq' : g ⟶ h) [has_image_map sq'] [has_image_map (sq ≫ sq')] : image.map (sq ≫ sq') = image.map sq ≫ image.map sq' := sorry /-- The identity `image f ⟶ image f` fits into the commutative square represented by the identity morphism `𝟙 f` in the arrow category. -/ def image_map_id {C : Type u} [category C] (f : arrow C) [has_image (comma.hom f)] : image_map 𝟙 := image_map.mk 𝟙 @[simp] theorem image.map_id {C : Type u} [category C] (f : arrow C) [has_image (comma.hom f)] [has_image_map 𝟙] : image.map 𝟙 = 𝟙 := sorry /-- If a category `has_image_maps`, then all commutative squares induce morphisms on images. -/ class has_image_maps (C : Type u) [category C] [has_images C] where has_image_map : ∀ {f g : arrow C} (st : f ⟶ g), has_image_map st /-- The functor from the arrow category of `C` to `C` itself that maps a morphism to its image and a commutative square to the induced morphism on images. -/ @[simp] theorem im_map {C : Type u} [category C] [has_images C] [has_image_maps C] (_x : arrow C) : ∀ (_x_1 : arrow C) (st : _x ⟶ _x_1), functor.map im st = image.map st := fun (_x_1 : arrow C) (st : _x ⟶ _x_1) => Eq.refl (functor.map im st) /-- A strong epi-mono factorisation is a decomposition `f = e ≫ m` with `e` a strong epimorphism and `m` a monomorphism. -/ structure strong_epi_mono_factorisation {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) extends mono_factorisation f where e_strong_epi : strong_epi (mono_factorisation.e _to_mono_factorisation) /-- Satisfying the inhabited linter -/ protected instance strong_epi_mono_factorisation_inhabited {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [strong_epi f] : Inhabited (strong_epi_mono_factorisation f) := { default := strong_epi_mono_factorisation.mk (mono_factorisation.mk Y 𝟙 f) } /-- A mono factorisation coming from a strong epi-mono factorisation always has the universal property of the image. -/ def strong_epi_mono_factorisation.to_mono_is_image {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) : is_image (strong_epi_mono_factorisation.to_mono_factorisation F) := is_image.mk fun (G : mono_factorisation f) => arrow.lift (arrow.hom_mk' sorry) /-- A category has strong epi-mono factorisations if every morphism admits a strong epi-mono factorisation. -/ class has_strong_epi_mono_factorisations (C : Type u) [category C] mk' :: where (has_fac : ∀ {X Y : C} (f : X ⟶ Y), Nonempty (strong_epi_mono_factorisation f)) theorem has_strong_epi_mono_factorisations.mk {C : Type u} [category C] (d : {X Y : C} → (f : X ⟶ Y) → strong_epi_mono_factorisation f) : has_strong_epi_mono_factorisations C := has_strong_epi_mono_factorisations.mk' fun (X Y : C) (f : X ⟶ Y) => Nonempty.intro (d f) protected instance has_images_of_has_strong_epi_mono_factorisations {C : Type u} [category C] [has_strong_epi_mono_factorisations C] : has_images C := has_images.mk sorry /-- A category has strong epi images if it has all images and `factor_thru_image f` is a strong epimorphism for all `f`. -/ class has_strong_epi_images (C : Type u) [category C] [has_images C] where strong_factor_thru_image : ∀ {X Y : C} (f : X ⟶ Y), strong_epi (factor_thru_image f) /-- If there is a single strong epi-mono factorisation of `f`, then every image factorisation is a strong epi-mono factorisation. -/ theorem strong_epi_of_strong_epi_mono_factorisation {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) {F' : mono_factorisation f} (hF' : is_image F') : strong_epi (mono_factorisation.e F') := sorry theorem strong_epi_factor_thru_image_of_strong_epi_mono_factorisation {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (F : strong_epi_mono_factorisation f) : strong_epi (factor_thru_image f) := strong_epi_of_strong_epi_mono_factorisation F (image.is_image f) /-- If we constructed our images from strong epi-mono factorisations, then these images are strong epi images. -/ protected instance has_strong_epi_images_of_has_strong_epi_mono_factorisations {C : Type u} [category C] [has_strong_epi_mono_factorisations C] : has_strong_epi_images C := has_strong_epi_images.mk fun (X Y : C) (f : X ⟶ Y) => strong_epi_factor_thru_image_of_strong_epi_mono_factorisation (Classical.choice (has_strong_epi_mono_factorisations.has_fac f)) /-- A category with strong epi images has image maps. -/ protected instance has_image_maps_of_has_strong_epi_images {C : Type u} [category C] [has_images C] [has_strong_epi_images C] : has_image_maps C := has_image_maps.mk sorry /-- If a category has images, equalizers and pullbacks, then images are automatically strong epi images. -/ protected instance has_strong_epi_images_of_has_pullbacks_of_has_equalizers {C : Type u} [category C] [has_images C] [has_pullbacks C] [has_equalizers C] : has_strong_epi_images C := sorry /-- If `C` has strong epi mono factorisations, then the image is unique up to isomorphism, in that if `f` factors as a strong epi followed by a mono, this factorisation is essentially the image factorisation. -/ def image.iso_strong_epi_mono {C : Type u} [category C] [has_strong_epi_mono_factorisations C] {X : C} {Y : C} {f : X ⟶ Y} {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : I' ≅ image f := is_image.iso_ext (strong_epi_mono_factorisation.to_mono_is_image (strong_epi_mono_factorisation.mk (mono_factorisation.mk I' m e))) (image.is_image f) @[simp] theorem image.iso_strong_epi_mono_hom_comp_ι {C : Type u} [category C] [has_strong_epi_mono_factorisations C] {X : C} {Y : C} {f : X ⟶ Y} {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : iso.hom (image.iso_strong_epi_mono e m comm) ≫ image.ι f = m := is_image.lift_fac (strong_epi_mono_factorisation.to_mono_is_image (strong_epi_mono_factorisation.mk (mono_factorisation.mk I' m e))) (image.mono_factorisation f) @[simp] theorem image.iso_strong_epi_mono_inv_comp_mono {C : Type u} [category C] [has_strong_epi_mono_factorisations C] {X : C} {Y : C} {f : X ⟶ Y} {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : iso.inv (image.iso_strong_epi_mono e m comm) ≫ m = image.ι f := image.lift_fac (strong_epi_mono_factorisation.to_mono_factorisation (strong_epi_mono_factorisation.mk (mono_factorisation.mk I' m e)))
7ae7f15aca21f0bb95543bb547f52e804b5a1ea7
ca1ad81c8733787aba30f7a8d63f418508e12812
/clfrags/src/hilbert/wr/proofs/pt.lean
675fa16ef1b8135cbb73e4236759026dc256b32e
[]
no_license
greati/hilbert-classical-fragments
5cdbe07851e979c8a03c621a5efd4d24bbfa333a
18a21ac6b2e890060eb4ae65752fc0245394d226
refs/heads/master
1,591,973,117,184
1,573,822,710,000
1,573,822,710,000
194,334,439
2
0
null
null
null
null
UTF-8
Lean
false
false
11,692
lean
import hilbert.wr.pt namespace clfrags namespace hilbert namespace wr namespace pt theorem pt₇ {a b c d e : Prop} (h₁ : pt (pt a b c) d e) : pt a b (pt c d e) := have h₂ : pt d (pt a b c) e, from pt₂ h₁, have h₃ : pt d e (pt a b c), from pt₃ h₂, have h₄ : pt (pt d e a) b c, from pt₆ h₃, have h₅ : pt b (pt d e a) c, from pt₂ h₄, have h₆ : pt b c (pt d e a), from pt₃ h₅, have h₇ : pt (pt b c d) e a, from pt₆ h₆, have h₈ : pt e (pt b c d) a, from pt₂ h₇, have h₉ : pt e a (pt b c d), from pt₃ h₈, have h₁₀ : pt (pt e a b) c d, from pt₆ h₉, have h₁₁ : pt c (pt e a b) d, from pt₂ h₁₀, have h₁₂ : pt c d (pt e a b), from pt₃ h₁₁, have h₁₃ : pt (pt c d e) a b, from pt₆ h₁₂, have h₁₄ : pt a (pt c d e) b, from pt₂ h₁₃, show pt a b (pt c d e), from pt₃ h₁₄ theorem pt₂_pt {a b c d e : Prop} (h₁ : pt d e (pt a b c)) : pt d e (pt b a c) := have h₂ : pt d (pt a b c) e, from pt₃ h₁, have h₃ : pt (pt a b c) d e, from pt₂ h₂, have h₄ : pt a b (pt c d e), from pt₇ h₃, have h₅ : pt b a (pt c d e), from pt₂ h₄, have h₆ : pt (pt b a c) d e, from pt₆ h₅, have h₇ : pt d (pt b a c) e, from pt₂ h₆, show pt d e (pt b a c), from pt₃ h₇ theorem pt₃_pt {a b c d e : Prop} (h₁ : pt d e (pt a b c)) : pt d e (pt a c b) := have h₂ : pt (pt d e a) b c, from pt₆ h₁, have h₃ : pt (pt d e a) c b, from pt₃ h₂, show pt d e (pt a c b), from pt₇ h₃ theorem pt₄_pt {a b c d : Prop} (h₁ : pt c d a) : pt c d (pt a b b) := have h₂ : pt (pt c d a) b b, from pt₄ h₁, show pt c d (pt a b b), from pt₇ h₂ theorem pt₅_pt {a b c d : Prop} (h₁ : pt c d (pt a b b)) : pt c d a := have h₂ : pt (pt c d a) b b, from pt₆ h₁, show pt c d a, from pt₅ h₂ theorem pt₆_pt {a b c d e f g : Prop} (h₁ : pt f g (pt a b (pt c d e))) : pt f g (pt (pt a b c) d e) := have h₂ : pt (pt a b (pt c d e)) f g, from pt₂ (pt₃ h₁), have h₃ : pt a b (pt (pt c d e) f g), from pt₇ h₂, have h₄ : pt (pt (pt c d e) f g) a b, from pt₂ (pt₃ h₃), have h₅ : pt (pt c d e) f (pt g a b), from pt₇ h₄, have h₆ : pt c d (pt e f (pt g a b)), from pt₇ h₅, have h₇ : pt (pt e f (pt g a b)) c d, from pt₂ (pt₃ h₆), have h₈ : pt e f (pt (pt g a b) c d), from pt₇ h₇, have h₉ : pt (pt (pt g a b) c d) e f, from pt₂ (pt₃ h₈), have h₁₀ : pt (pt g a b) c (pt d e f), from pt₇ h₉, have h₁₁ : pt g a (pt b c (pt d e f)), from pt₇ h₁₀, have h₁₂ : pt (pt b c (pt d e f)) g a, from pt₂ (pt₃ h₁₁), have h₁₃ : pt b c (pt (pt d e f) g a), from pt₇ h₁₂, have h₁₄ : pt (pt (pt d e f) g a) b c, from pt₂ (pt₃ h₁₃), have h₁₅ : pt (pt d e f) g (pt a b c), from pt₇ h₁₄, have h₁₆ : pt d e (pt f g (pt a b c)), from pt₇ h₁₅, have h₁₇ : pt (pt f g (pt a b c)) d e, from pt₂ (pt₃ h₁₆), show pt f g (pt (pt a b c) d e), from pt₇ h₁₇ theorem pt₈ {a b c : Prop} (h₁ : pt (pt a b c) a b) : c := have h₂ : pt a (pt a b c) b, from pt₂ h₁, have h₃ : pt a b (pt a b c), from pt₃ h₂, have h₄ : pt a b (pt a c b), from pt₃_pt h₃, have h₅ : pt a b (pt c a b), from pt₂_pt h₄, have h₆ : pt a (pt c a b) b, from pt₃ h₅, have h₇ : pt (pt c a b) a b, from pt₂ h₆, have h₈ : pt c a (pt b a b), from pt₇ h₇, have h₉ : pt c a (pt a b b), from pt₂_pt h₈, have h₁₀ : pt c a a, from pt₅_pt h₉, show c, from pt₅ h₁₀ theorem pt₉ {a b c : Prop} (h₁ : pt (pt a b c) a c) : b := have h₂ : pt a b (pt c a c), from pt₇ h₁, have h₃ : pt a b (pt a c c), from pt₂_pt h₂, have h₄ : pt a b a, from pt₅_pt h₃, have h₅ : pt b a a, from pt₂ h₄, show b, from pt₅ h₅ theorem pt₁₀ {a b c : Prop} (h₁ : pt (pt a b c) b c) : a := have h₂ : pt a b (pt c b c), from pt₇ h₁, have h₃ : pt a b (pt b c c), from pt₂_pt h₂, have h₄ : pt a b b, from pt₅_pt h₃, show a, from pt₅ h₄ lemma pt₁₁' {a b c d e f : Prop} (h₁ : pt e f (pt (pt a b c) c d)) : pt e f (pt a b d) := have h₂ : pt e f (pt c d (pt a b c)), from pt₃_pt (pt₂_pt h₁), have h₃ : pt e f (pt (pt c d a) b c), from pt₆_pt h₂, have h₄ : pt e f (pt b c (pt c d a)), from pt₃_pt (pt₂_pt h₃), have h₅ : pt e f (pt (pt b c c) d a), from pt₆_pt h₄, have h₆ : pt e f (pt d a (pt b c c)), from pt₃_pt (pt₂_pt h₅), have h₇ : pt e f (pt (pt d a b) c c), from pt₆_pt h₆, have h₈ : pt e f (pt d a b), from pt₅_pt h₇, show pt e f (pt a b d), from pt₃_pt (pt₂_pt h₈) lemma pt₁₁'' {a b c d e f : Prop} (h₁ : pt e f (pt (pt a b c) b d)) : pt e f (pt a c d) := have h₂ : pt e f (pt b d (pt a b c)), from pt₃_pt (pt₂_pt h₁), have h₃ : pt e f (pt (pt b d a) b c), from pt₆_pt h₂, have h₄ : pt e f (pt c b (pt b d a)), from pt₂_pt (pt₃_pt (pt₂_pt h₃)), have h₅ : pt e f (pt (pt c b b) d a), from pt₆_pt h₄, have h₆ : pt e f (pt d a (pt c b b)), from pt₃_pt (pt₂_pt h₅), have h₇ : pt e f (pt (pt d a c) b b), from pt₆_pt h₆, have h₈ : pt e f (pt d a c), from pt₅_pt h₇, show pt e f (pt a c d), from pt₃_pt (pt₂_pt h₈) lemma pt₁₁''' {a b c d e f : Prop} (h₁ : pt e f (pt (pt a b c) a d)) : pt e f (pt b c d) := have h₂ : pt e f (pt d a (pt a b c)), from pt₂_pt (pt₃_pt (pt₂_pt h₁)), have h₃ : pt e f (pt (pt d a a) b c), from pt₆_pt h₂, have h₄ : pt e f (pt b c (pt d a a)), from pt₃_pt (pt₂_pt h₃), have h₅ : pt e f (pt (pt b c d) a a), from pt₆_pt h₄, show pt e f (pt b c d), from pt₅_pt h₅ theorem pt₁₁ {a b c d : Prop} (h₁ : pt (pt (pt a b c) a d) (pt (pt a b c) b d) (pt (pt a b c) c d)) : d := have h₂ : pt (pt (pt a b c) a d) (pt (pt a b c) b d) (pt a b d), from pt₁₁' h₁, have h₃ : pt (pt (pt a b c) a d) (pt a b d) (pt (pt a b c) b d), from pt₃ h₂, have h₄ : pt (pt (pt a b c) a d) (pt a b d) (pt a c d), from pt₁₁'' h₃, have h₅ : pt (pt a b d) (pt (pt a b c) a d) (pt a c d), from pt₂ h₄, have h₆ : pt (pt a b d) (pt a c d) (pt (pt a b c) a d) , from pt₃ h₅, have h₇ : pt (pt a b d) (pt a c d) (pt b c d) , from pt₁₁''' h₆, have h₈ : pt a b (pt d (pt a c d) (pt b c d)) , from pt₇ h₇, have h₉ : pt a b (pt (pt a c d) d (pt b c d)) , from pt₂_pt h₈, have h₁₀ : pt (pt a b (pt a c d)) d (pt b c d) , from pt₆ h₉, have h₁₁ : pt (pt a b (pt a c d)) d (pt b d c) , from pt₃_pt h₁₀, have h₁₂ : pt (pt a b (pt a c d)) d (pt d b c) , from pt₂_pt h₁₁, have h₁₃ : pt (pt (pt a b (pt a c d)) d d) b c, from pt₆ h₁₂, have h₁₄ : pt b c (pt (pt a b (pt a c d)) d d), from pt₃ (pt₂ h₁₃), have h₁₅ : pt b c (pt a b (pt a c d)), from pt₅_pt h₁₄, have h₁₆ : pt b c (pt b a (pt a c d)), from pt₂_pt h₁₅, have h₁₇ : pt (pt b c b) a (pt a c d), from pt₆ h₁₆, have h₁₈ : pt a (pt a c d) (pt b c b), from pt₃ (pt₂ h₁₇), have h₁₉ : pt a (pt a c d) (pt c b b), from pt₂_pt h₁₈, have h₂₀ : pt a (pt a c d) c, from pt₅_pt h₁₉, have h₂₁ : pt (pt a c d) a c, from pt₂ h₂₀, show d, from pt₈ h₂₁ theorem pt₁₂ {a b c d e : Prop} (h₁ : pt a b c) (h₂ : d) (h₃ : e) : pt a b (pt c d e) := have h₄ : pt (pt a b c) d e, from pt₁ h₁ h₂ h₃, show pt a b (pt c d e), from pt₇ h₄ theorem pt₁₃ {a b c d e : Prop} (h₁ : pt a b c) (h₂ : pt a b d) (h₃ : e) : (pt c d e) := have h₄ : pt a b (pt c (pt a b d) e), from pt₁₂ h₁ h₂ h₃, have h₅ : pt a b (pt (pt a b d) c e), from pt₂_pt h₄, have h₆ : pt (pt a b (pt a b d)) c e, from pt₆ h₅, have h₇ : pt c e (pt a b (pt a b d)), from pt₃ (pt₂ h₆), have h₈ : pt c e (pt (pt a b d) a b), from pt₂_pt (pt₃_pt h₇), have h₉ : pt c e (pt b d b), from pt₁₁''' h₈, have h₁₀ : pt c e (pt d b b), from pt₂_pt h₉, have h₁₁ : pt c e d, from pt₅_pt h₁₀, show pt c d e, from pt₃ h₁₁ theorem pt₁₄ {a b c d e : Prop} (h₁ : pt a b c) (h₂ : pt a b d) (h₃ : pt a b e) : pt a b (pt c d e) := have h₄ : pt c d (pt a b e), from pt₁₃ h₁ h₂ h₃, have h₅ : pt c d (pt e a b), from pt₂_pt (pt₃_pt h₄), have h₅ : pt (pt c d e) a b, from pt₆ h₅, show pt a b (pt c d e), from pt₃ (pt₂ h₅) theorem pt₄' {a b c : Prop} (h₁ : a) : pt (pt a b c) b c := have h₂ : pt a b b, from pt₄ h₁, have h₃ : pt (pt a b b) c c, from pt₄ h₂, have h₄ : pt a b (pt b c c), from pt₇ h₃, have h₅ : pt a b (pt c b c), from pt₂_pt h₄, show pt (pt a b c) b c, from pt₆ h₅ end pt end wr end hilbert end clfrags
7b364350f795f8e6a11f583cfe19cb4f79d7d86e
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/analysis/normed_space/hahn_banach.lean
99f989a688b6a0d75d31dcfc36e7539b12121e2f
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
6,989
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import analysis.normed_space.operator_norm import analysis.normed_space.extend import analysis.convex.cone import data.complex.is_R_or_C /-! # Hahn-Banach theorem In this file we prove a version of Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ` and `ℂ`. In order to state and prove its corollaries uniformly, we prove the statements for a field `𝕜` satisfying `is_R_or_C 𝕜`. In this setting, `exists_dual_vector` states that, for any nonzero `x`, there exists a continuous linear form `g` of norm `1` with `g x = ∥x∥` (where the norm has to be interpreted as an element of `𝕜`). -/ universes u v /-- The norm of `x` as an element of `𝕜` (a normed algebra over `ℝ`). This is needed in particular to state equalities of the form `g x = norm' 𝕜 x` when `g` is a linear function. For the concrete cases of `ℝ` and `ℂ`, this is just `∥x∥` and `↑∥x∥`, respectively. -/ noncomputable def norm' (𝕜 : Type*) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] {E : Type*} [normed_group E] (x : E) : 𝕜 := algebra_map ℝ 𝕜 ∥x∥ lemma norm'_def (𝕜 : Type*) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] {E : Type*} [normed_group E] (x : E) : norm' 𝕜 x = (algebra_map ℝ 𝕜 ∥x∥) := rfl lemma norm_norm' (𝕜 : Type*) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] (A : Type*) [normed_group A] (x : A) : ∥norm' 𝕜 x∥ = ∥x∥ := by rw [norm'_def, norm_algebra_map_eq, norm_norm] namespace real variables {E : Type*} [normed_group E] [normed_space ℝ E] /-- Hahn-Banach theorem for continuous linear functions over `ℝ`. -/ theorem exists_extension_norm_eq (p : subspace ℝ E) (f : p →L[ℝ] ℝ) : ∃ g : E →L[ℝ] ℝ, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ := begin rcases exists_extension_of_le_sublinear ⟨p, f⟩ (λ x, ∥f∥ * ∥x∥) (λ c hc x, by simp only [norm_smul c x, real.norm_eq_abs, abs_of_pos hc, mul_left_comm]) (λ x y, _) (λ x, le_trans (le_abs_self _) (f.le_op_norm _)) with ⟨g, g_eq, g_le⟩, set g' := g.mk_continuous (∥f∥) (λ x, abs_le.2 ⟨neg_le.1 $ g.map_neg x ▸ norm_neg x ▸ g_le (-x), g_le x⟩), { refine ⟨g', g_eq, _⟩, { apply le_antisymm (g.mk_continuous_norm_le (norm_nonneg f) _), refine f.op_norm_le_bound (norm_nonneg _) (λ x, _), dsimp at g_eq, rw ← g_eq, apply g'.le_op_norm } }, { simp only [← mul_add], exact mul_le_mul_of_nonneg_left (norm_add_le x y) (norm_nonneg f) } end end real section is_R_or_C open is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [normed_group F] [normed_space 𝕜 F] /-- Hahn-Banach theorem for continuous linear functions over `𝕜` satisyfing `is_R_or_C 𝕜`. -/ theorem exists_extension_norm_eq (p : subspace 𝕜 F) (f : p →L[𝕜] 𝕜) : ∃ g : F →L[𝕜] 𝕜, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ := begin letI : module ℝ F := restrict_scalars.semimodule ℝ 𝕜 F, letI : is_scalar_tower ℝ 𝕜 F := restrict_scalars.is_scalar_tower _ _ _, letI : normed_space ℝ F := normed_space.restrict_scalars _ 𝕜 _, letI : normed_space ℝ p := (by apply_instance : normed_space ℝ (submodule.restrict_scalars ℝ p)), -- Let `fr: p →L[ℝ] ℝ` be the real part of `f`. let fr := re_clm.comp (f.restrict_scalars ℝ), have fr_apply : ∀ x, fr x = re (f x) := λ x, rfl, -- Use the real version to get a norm-preserving extension of `fr`, which -- we'll call `g : F →L[ℝ] ℝ`. rcases real.exists_extension_norm_eq (p.restrict_scalars ℝ) fr with ⟨g, ⟨hextends, hnormeq⟩⟩, -- Now `g` can be extended to the `F →L[𝕜] 𝕜` we need. use g.extend_to_𝕜, -- It is an extension of `f`. have h : ∀ x : p, g.extend_to_𝕜 x = f x, { assume x, change (g (x : F) : 𝕜) - (I : 𝕜) * g ((((I : 𝕜) • x) : p) : F) = f x, rw [hextends, hextends], change (re (f x) : 𝕜) - (I : 𝕜) * (re (f ((I : 𝕜) • x))) = f x, apply ext, { simp only [add_zero, algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add, zero_sub, I_im', zero_mul, of_real_re, eq_self_iff_true, sub_zero, mul_neg_eq_neg_mul_symm, of_real_neg, mul_re, mul_zero, sub_neg_eq_add, continuous_linear_map.map_smul] }, { simp only [algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add, zero_sub, I_im', zero_mul, of_real_re, mul_neg_eq_neg_mul_symm, mul_im, zero_add, of_real_neg, mul_re, sub_neg_eq_add, continuous_linear_map.map_smul] } }, refine ⟨h, _⟩, -- And we derive the equality of the norms by bounding on both sides. refine le_antisymm _ _, { calc ∥g.extend_to_𝕜∥ ≤ ∥g∥ : g.extend_to_𝕜.op_norm_le_bound g.op_norm_nonneg (norm_bound _) ... = ∥fr∥ : hnormeq ... ≤ ∥re_clm∥ * ∥f∥ : continuous_linear_map.op_norm_comp_le _ _ ... = ∥f∥ : by rw [norm_re_clm, one_mul] }, { exact f.op_norm_le_bound g.extend_to_𝕜.op_norm_nonneg (λ x, h x ▸ g.extend_to_𝕜.le_op_norm x) }, end end is_R_or_C section dual_vector variables {𝕜 : Type v} [is_R_or_C 𝕜] variables {E : Type u} [normed_group E] [normed_space 𝕜 E] open continuous_linear_equiv submodule open_locale classical lemma coord_norm' (x : E) (h : x ≠ 0) : ∥norm' 𝕜 x • coord 𝕜 x h∥ = 1 := by rw [norm_smul, norm_norm', coord_norm, mul_inv_cancel (mt norm_eq_zero.mp h)] /-- Corollary of Hahn-Banach. Given a nonzero element `x` of a normed space, there exists an element of the dual space, of norm `1`, whose value on `x` is `∥x∥`. -/ theorem exists_dual_vector (x : E) (h : x ≠ 0) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = norm' 𝕜 x := begin let p : submodule 𝕜 E := 𝕜 ∙ x, let f := norm' 𝕜 x • coord 𝕜 x h, obtain ⟨g, hg⟩ := exists_extension_norm_eq p f, use g, split, { rw [hg.2, coord_norm'] }, { calc g x = g (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw coe_mk ... = (norm' 𝕜 x • coord 𝕜 x h) (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw ← hg.1 ... = norm' 𝕜 x : by simp } end /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, and choosing the dual element arbitrarily when `x = 0`. -/ theorem exists_dual_vector' [nontrivial E] (x : E) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = norm' 𝕜 x := begin by_cases hx : x = 0, { obtain ⟨y, hy⟩ := exists_ne (0 : E), obtain ⟨g, hg⟩ : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g y = norm' 𝕜 y := exists_dual_vector y hy, refine ⟨g, hg.left, _⟩, rw [norm'_def, hx, norm_zero, ring_hom.map_zero, continuous_linear_map.map_zero] }, { exact exists_dual_vector x hx } end end dual_vector
2e3c5979983f9ca5f5127896c1c7da362533ea4b
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/tests/lean/run/blast9.lean
652239b0cb283781fe6370bae258fe9472970bf6
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
212
lean
import data.list open list example (p : Prop) (a b c : nat) : [a, b, c] = [] → p := by blast lemma l1 (a b c d e f : nat) : [a, b, c] = [d, e, f] → a = d ∧ b = e ∧ c = f := by blast reveal l1 print l1
ffbe137d13f45f1847fc314a8bb4494565fa49ce
f3be49eddff7edf577d3d3666e314d995f7a6357
/TBA/Exercises/Exercise3.lean
d7c1f0b8be180a4924df1692e70db692210b6174
[]
no_license
IPDSnelting/tba-2021
8b930bcd2f4aae44a2ddc86e72b77f84e6d46e82
b6390e55b768423d3266969e81d19290129c5914
refs/heads/master
1,686,754,693,583
1,625,135,602,000
1,625,136,365,000
355,124,341
50
7
null
1,625,133,762,000
1,617,699,824,000
Lean
UTF-8
Lean
false
false
5,071
lean
/- NATURAL NUMBERS We saw the definition of the natural numbers `Nat` and the addition on them on the slides. Since we want to define it ourselves and not use Lean's built-in version, we will call ours `Nat'`. -/ inductive Nat' : Type | zero : Nat' | succ : Nat' → Nat' open Nat' def add (m n : Nat') : Nat' := match n with | zero => m | succ n => succ (add m n) -- Let us now try to recursively define multiplication on the `Nat'`. def mul (m n : Nat') : Nat' := _ -- Let's define `≤` as an inductive predicate on `Nat'`s! inductive LE' : Nat' → Nat' → Prop where | refl (n : Nat') : LE' n n -- TODO: We're off to a good start since `≤` is certainly reflexive, but something is still -- missing (or else we'd just redefine `Eq` for `Nat'`s). Can you think of one more constructor -- that makes it so `LE' n m` holds whenever we would intuitively expect `n ≤ m` to hold? -- hint: it remains to be shown that `LE' n m` should hold when `m` is *greater* than `n`, so -- the new constructor should probably involve `Nat'`'s `succ` constructor to reach those greater numbers. -- hint: it should be an *inductive* case, meaning using another `LE'` application as an assumption -- note: `LE'` could also be defined in terms of `add`, but that makes working with it awkward, -- so let's not do that. | _ -- Now let's prove some things about `LE'`. But first we'll give it the standard notation. infix:50 " ≤ " => LE' example (n : Nat') : n ≤ succ (succ n) := _ -- This one is a bit harder: we will need induction! -- As described on the slides, induction is just a special case of recursion theorem le_add (m n : Nat') : m ≤ add m n := match n with -- This is the base case | zero => _ -- This is the inductive case. You probably want to use `le_add m n` (the inductive hypothesis) somewhere inside of it! | succ n => -- Lean automatically converts `add m (succ n)` to `succ (add m n)` for us when necessary, but it can help -- to make the conversion explicit. `show` simply lets us restate the goal, using any definitionally equivalent type. show m ≤ succ (add m n) from _ -- Now try proving this theorem on `add` using the same induction scheme theorem zero_add (n : Nat') : add zero n = add n zero := _ /- LISTS The type List α of lists on a Type α is defined to be the type on the constructors nil : List α and cons : (x : α) → (xs : List α) → List α. We can use [] as a notation for nil and x :: xs as a notation for cons x xs. -/ open List -- Let's define something we can't in most other functional languages: -- a function returning the first element of a list given a proof that is is non-empty! (xs ≠ [] is shorthand for ¬ (xs = [])) -- hint: match on `xs` *before* using `fun` so that the `xs` in the assumption is replaced by the match! def hd (xs : List α) : xs ≠ [] → α := _ /- TREES Define a type Tree α of binary trees with labels of type α. Each tree is either a labelled leaf or a labelled node with two trees attached to it. -/ inductive Tree (α : Type) : Type where open Tree -- Now, let us define the depth and the size of a tree. You can use the function Nat.max to get the maximum of two natural numbers. The depth of a leaf is 1. def depth (t : Tree α) : Nat := _ def size (t : Tree α) : Nat := _ -- We can turn a tree into a list by traversing it in various ways, depending on whether we add the root -- before its subtrees (preOrder), between its subtrees (inOrder) or after its subtrees (postOrder). -- Define preOrder, inOrder, and postOrder as functions Tree α → List α. def preOrder (t : Tree α) : List α := _ def inOrder (t : Tree α) : List α := _ def postOrder (t : Tree α) : List α := _ -- Define a function which returns the mirror image of a given tree. def mirror (t : Tree α) : Tree α := _ -- Now we prove to facts about this function: -- First, we prove that it is involutive (mirroring a tree twice returns the original tree). -- Then, we prove that the mirror image of two trees is equal, if and only if the trees themselves are. -- Useful lemmas, if you don't want to use ▸ as much (but it is also just as doable with some `have`s and ▸): #check @Eq.trans #check @Eq.symm #check @congrArg theorem mirror_involutive (t : Tree α) : mirror (mirror t) = t := _ theorem mirror_eq (s t : Tree α) : mirror s = mirror t ↔ s = t := _ /- STRUCTURES -/ -- Define the structure `Semigroup α` for a semigroup on a type `α`. -- Reminder: A semigroup is an algebraic structure with an associative binary operation `mul`. structure Semigroup (α : Type) where -- Now extend the structure to one for a monoid on α. -- Reminder : A monoid is a semigroup with an element which acts as the left and right identity on `mul`. structure Monoid (α : Type) extends Semigroup α where -- Now try to instantiate the type `Nat'` as a monoid. -- Leave out the three proofs (associativiy, left and right inverse), we'll learn better ways to write such proofs next week. def Nat'Monoid : Monoid Nat' := _
b5715699453509d7c78f17ae84e2a7e1464b8542
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world4/level8.lean
dd13efad34178dd0416ab90ff3e37e61034fa8c6
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
1,841
lean
import game.world4.level7 -- hide -- incantation for importing ring into framework -- hide import tactic.ring -- hide meta def less_leaky.interactive.ring := tactic.interactive.ring -- hide namespace mynat -- hide instance : comm_semiring mynat := by structure_helper -- you just levelled up /- # World 4 : Power World ## Level 8 : `add_squared` [final boss music] You see something written on the stone dungeon wall: ``` begin rw one_eq_succ_zero, repeat {rw pow_succ}, ... ``` and you can't make out the rest because there's a kind of thing in the way that will magically disappear but only when you've beaten the boss. [editor's note: Actual Lean natural numbers do have `2`, but I figured now was no time to introduce it; the first thing you'd do with it would be change it to `succ(1)` anyway] -/ /- Theorem For all naturals $a$ and $b$, we have $$(a+b)^2=a^2+b^2+2ab.$$ -/ lemma add_squared (a b : mynat) : (a + b) ^ (succ(1)) = a ^ (succ(1)) + b^(succ(1)) + (succ(1))*a*b := begin [less_leaky] rw one_eq_succ_zero, repeat {rw pow_succ}, repeat {rw pow_zero}, ring, end /- As the boss lies smouldering, you notice on the dungeon wall that <a href="https://twitter.com/XenaProject/status/1190453646904958976?s=20/" target="blank"> two more lines of code are now visible under the first two...</a> (Twitter.com) I just beat this level with 27 rewrites followed by a `refl`. Can you do any better? If you beat it then well done. Do you fancy doing $(a+b)^3$ now? You might want to read <a href="https://xenaproject.wordpress.com/2018/06/13/ab3/" target="blank"> this Xena Project blog post</a> before you start though. -/ /- The next world is not yet written -- it will be an abstract function world where several more tactics will be introduced. Coming soon. -/ end mynat -- hide
879163e84137ee9fa06daeade1f258284566c3aa
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/ring_theory/ideals.lean
1e163a71dc6656d6a20c3fac421a6788b7e29466
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
11,230
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes -/ import algebra.module tactic.ring linear_algebra.quotient_module universes u v variables {α : Type u} {β : Type v} [comm_ring α] {a b : α} open set function local attribute [instance] classical.prop_decidable class is_ideal {α : Type u} [comm_ring α] (S : set α) extends is_submodule S : Prop namespace is_ideal protected lemma zero (S : set α) [is_ideal S] : (0 : α) ∈ S := is_submodule.zero_ α S protected lemma add {S : set α} [is_ideal S] : a ∈ S → b ∈ S → a + b ∈ S := is_submodule.add_ α lemma neg_iff {S : set α} [is_ideal S] : a ∈ S ↔ -a ∈ S := ⟨is_submodule.neg, λ h, neg_neg a ▸ is_submodule.neg h⟩ protected lemma sub {S : set α} [is_ideal S] : a ∈ S → b ∈ S → a - b ∈ S := is_submodule.sub lemma mul_left {S : set α} [is_ideal S] : b ∈ S → a * b ∈ S := @is_submodule.smul α α _ _ _ _ a _ lemma mul_right {S : set α} [is_ideal S] : a ∈ S → a * b ∈ S := mul_comm b a ▸ mul_left def trivial (α : Type*) [comm_ring α] : set α := {0} instance : is_ideal (trivial α) := by refine {..}; simp [trivial] {contextual := tt} instance univ : is_ideal (@univ α) := {} instance span (S : set α) : is_ideal (span S) := {} end is_ideal class is_proper_ideal {α : Type u} [comm_ring α] (S : set α) extends is_ideal S : Prop := (ne_univ : S ≠ set.univ) lemma is_proper_ideal_iff_one_not_mem {S : set α} [hS : is_ideal S] : is_proper_ideal S ↔ (1 : α) ∉ S := ⟨λ h h1, by exactI is_proper_ideal.ne_univ S (eq_univ_iff_forall.2 (λ a, mul_one a ▸ is_ideal.mul_left h1)), λ h, {ne_univ := mt eq_univ_iff_forall.1 (λ ha, h (ha _)), ..hS}⟩ class is_prime_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := (mem_or_mem_of_mul_mem : ∀ {x y : α}, x * y ∈ S → x ∈ S ∨ y ∈ S) theorem mem_or_mem_of_mul_eq_zero {α : Type u} [comm_ring α] (S : set α) [is_prime_ideal S] : ∀ {x y : α}, x * y = 0 → x ∈ S ∨ y ∈ S := λ x y hxy, have x * y ∈ S, by rw hxy; from (@is_submodule.zero α α _ _ S _ : (0:α) ∈ S), is_prime_ideal.mem_or_mem_of_mul_mem this class is_maximal_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := mk' :: (eq_or_univ_of_subset : ∀ (T : set α) [is_submodule T], S ⊆ T → T = S ∨ T = set.univ) theorem is_maximal_ideal.mk {α : Type u} [comm_ring α] (S : set α) [is_submodule S] (h₁ : (1:α) ∉ S) (h₂ : ∀ x (T : set α) [is_submodule T], S ⊆ T → x ∉ S → x ∈ T → (1:α) ∈ T) : is_maximal_ideal S := { ne_univ := assume hu, have (1:α) ∈ S, by rw hu; trivial, h₁ this, eq_or_univ_of_subset := assume T ht hst, classical.or_iff_not_imp_left.2 $ assume (hnst : T ≠ S), let ⟨x, hxt, hxns⟩ := set.exists_of_ssubset ⟨hst, hnst.symm⟩ in @@is_submodule.univ_of_one_mem _ T ht $ @@h₂ x T ht hst hxns hxt} instance is_maximal_ideal.is_prime_ideal (S : set α) [hS : is_maximal_ideal S] : is_prime_ideal S := { mem_or_mem_of_mul_mem := λ x y hxy, or_iff_not_imp_left.2 (λ h, have (span (insert x S)) = univ := or.resolve_left (is_maximal_ideal.eq_or_univ_of_subset _ (subset.trans (subset_insert _ _) subset_span)) (λ hS, h $ hS ▸ subset_span (mem_insert _ _)), have (1 : α) ∈ span (insert x S) := this.symm ▸ mem_univ _, let ⟨a, ha⟩ := mem_span_insert.1 this in have hy : y * (1 + a • x) - a * (x * y) = y := by rw smul_eq_mul; ring, hy ▸ is_ideal.sub (is_ideal.mul_left (span_eq_of_is_submodule (show is_submodule S, by apply_instance) ▸ ha)) (is_ideal.mul_left hxy)), ..hS } def nonunits (α : Type u) [monoid α] : set α := { x | ¬∃ y, y * x = 1 } theorem not_unit_of_mem_proper_ideal {α : Type u} [comm_ring α] (S : set α) [is_proper_ideal S] : S ⊆ nonunits α := λ x hx ⟨y, hxy⟩, is_proper_ideal.ne_univ S $ is_submodule.eq_univ_of_contains_unit S x y hx hxy class local_ring (α : Type u) [comm_ring α] := (S : set α) (max : is_maximal_ideal S) (unique : ∀ T [is_maximal_ideal T], S = T) def local_of_nonunits_ideal {α : Type u} [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := have hi : is_submodule (nonunits α), from { zero_ := λ ⟨y, hy⟩, hnze $ by simpa using hy, add_ := h, smul := λ x y hy ⟨z, hz⟩, hy ⟨x * z, by rw [← hz]; simp [mul_left_comm, mul_assoc]⟩ }, { S := nonunits α, max := @@is_maximal_ideal.mk _ (nonunits α) hi (λ ho, ho ⟨1, mul_one 1⟩) $ λ x T ht hst hxns hxt, let ⟨y, hxy⟩ := classical.by_contradiction hxns in by rw [← hxy]; exact @@is_submodule.smul _ _ ht y hxt, unique := λ T hmt, or.cases_on (@@is_maximal_ideal.eq_or_univ_of_subset _ hmt (nonunits α) hi $ λ z hz, @@not_unit_of_mem_proper_ideal _ T (by resetI; apply_instance) hz) id (λ htu, false.elim $ ((set.ext_iff _ _).1 htu 1).2 trivial ⟨1, mul_one 1⟩) } instance is_ideal.preimage [comm_ring β] (S : set β) (f : α → β) [is_ring_hom f] [is_ideal S] : is_ideal (f ⁻¹' S) := { to_is_submodule := { zero_ := show f 0 ∈ S, by rw is_ring_hom.map_zero f; exact is_ideal.zero _, add_ := λ x y hx hy, show f (x + y) ∈ S, by rw is_ring_hom.map_add f; exact is_ideal.add hx hy, smul := λ c x hx, show f (c * x) ∈ S, by rw is_ring_hom.map_mul f; exact is_ideal.mul_left hx } } instance is_proper_ideal.preimage [comm_ring β] (S : set β) (f : α → β) [is_ring_hom f] [hT : is_proper_ideal S] : is_proper_ideal (f ⁻¹' S) := { ne_univ := mt eq_univ_iff_forall.1 (λ h, is_proper_ideal_iff_one_not_mem.1 hT (is_ring_hom.map_one f ▸ h _)), ..is_ideal.preimage S f } instance is_prime_ideal.preimage [comm_ring β] (S : set β) (f : α → β) [is_ring_hom f] [is_prime_ideal S] : is_prime_ideal (f ⁻¹' S) := { mem_or_mem_of_mul_mem := λ x y (hxy : f (x * y) ∈ S), show f x ∈ S ∨ f y ∈ S, from is_prime_ideal.mem_or_mem_of_mul_mem (by rwa ← is_ring_hom.map_mul f), ..is_proper_ideal.preimage S f } namespace quotient_ring open is_ideal def quotient_rel (S : set α) [is_ideal S] := is_submodule.quotient_rel S def quotient (S : set α) [is_ideal S] := quotient (quotient_rel S) def mk {S : set α} [is_ideal S] (a : α) : quotient S := quotient.mk' a instance {S : set α} [is_ideal S] : has_coe α (quotient S) := ⟨mk⟩ protected lemma eq {S : set α} [is_ideal S] {a b : α} : (a : quotient S) = b ↔ a - b ∈ S := quotient.eq' instance (S : set α) [is_ideal S] : comm_ring (quotient S) := { mul := λ a b, quotient.lift_on₂' a b (λ a b, ((a * b : α) : quotient S)) (λ a₁ a₂ b₁ b₂ (h₁ : a₁ - b₁ ∈ S) (h₂ : a₂ - b₂ ∈ S), quotient.sound' (show a₁ * a₂ - b₁ * b₂ ∈ S, from have h : a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁ = a₁ * a₂ - b₁ * b₂, by ring, h ▸ is_ideal.add (mul_left h₁) (mul_right h₂))), mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg mk (mul_comm a b), one := (1 : α), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg mk (right_distrib a b c), ..is_submodule.quotient.add_comm_group S } instance is_ring_hom_mk (S : set α) [is_ideal S] : @is_ring_hom _ (quotient S) _ _ mk := ⟨λ _ _, rfl, λ _ _, rfl, rfl⟩ instance (S T : set α) [is_ideal S] [is_ideal T] : is_ideal (mk '' S : set (quotient T)) := { to_is_submodule := { zero_ := ⟨0, is_ideal.zero _, rfl⟩, add_ := λ x y ⟨a, ha⟩ ⟨b, hb⟩, ⟨a + b, is_ideal.add ha.1 hb.1, ha.2 ▸ hb.2 ▸ rfl⟩, smul := λ c x ⟨a, ha⟩, quotient.induction_on' c (λ c, ⟨c * a, mul_left ha.1, ha.2 ▸ rfl⟩) } } @[simp] lemma coe_zero (S : set α) [is_ideal S] : ((0 : α) : quotient S) = 0 := rfl @[simp] lemma coe_one (S : set α) [is_ideal S] : ((1 : α) : quotient S) = 1 := rfl @[simp] lemma coe_add (S : set α) [is_ideal S] (a b : α) : ((a + b : α) : quotient S) = a + b := rfl @[simp] lemma coe_mul (S : set α) [is_ideal S] (a b : α) : ((a * b : α) : quotient S) = a * b := rfl @[simp] lemma coe_neg (S : set α) [is_ideal S] (a : α) : ((-a : α) : quotient S) = -a := rfl @[simp] lemma coe_sub (S : set α) [is_ideal S] (a b : α) : ((a - b : α) : quotient S) = a - b := rfl @[simp] lemma coe_pow (S : set α) [is_ideal S] (a : α) (n : ℕ) : ((a ^ n : α) : quotient S) = a ^ n := by induction n; simp [*, pow_succ] lemma eq_zero_iff_mem {S : set α} [is_ideal S] : (a : quotient S) = 0 ↔ a ∈ S := by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq' instance (S : set α) [is_proper_ideal S] : nonzero_comm_ring (quotient S) := { zero_ne_one := ne.symm $ mt eq_zero_iff_mem.1 (is_proper_ideal_iff_one_not_mem.1 (by apply_instance)), ..quotient_ring.comm_ring S } instance (S : set α) [is_prime_ideal S] : integral_domain (quotient S) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (is_prime_ideal.mem_or_mem_of_mul_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), ..quotient_ring.nonzero_comm_ring S } lemma exists_inv {S : set α} [is_maximal_ideal S] {a : quotient S} : a ≠ 0 → ∃ b : quotient S, a * b = 1 := quotient.induction_on' a $ λ a ha, classical.by_contradiction $ λ h, have haS : a ∉ S := mt eq_zero_iff_mem.2 ha, by haveI hS : is_proper_ideal (span (set.insert a S)) := is_proper_ideal_iff_one_not_mem.2 (mt mem_span_insert.1 $ λ ⟨b, hb⟩, h ⟨-b, quotient_ring.eq.2 (neg_iff.2 (begin rw [neg_sub, mul_neg_eq_neg_mul_symm, sub_eq_add_neg, neg_neg, mul_comm], rw span_eq_of_is_submodule (show is_submodule S, by apply_instance) at hb, exact hb end))⟩); exact have span (set.insert a S) = S := or.resolve_right (is_maximal_ideal.eq_or_univ_of_subset (span (set.insert a S)) (subset.trans (subset_insert _ _) subset_span)) (is_proper_ideal.ne_univ _), haS (this ▸ subset_span (mem_insert _ _)) /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications -/ protected noncomputable def field (S : set α) [is_maximal_ideal S] : field (quotient S) := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_mul_cancel := λ a (ha : a ≠ 0), show dite _ _ _ * a = _, by rw [mul_comm, dif_neg ha]; exact classical.some_spec (exists_inv ha), ..quotient_ring.integral_domain S } end quotient_ring
0477c294f7d1d2e7c96a5e1f109aaee0c7586212
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/homeomorph.lean
b57762b6e6c4e2fcf5b15ea451e51fd9b98fd615
[ "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
23,901
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Sébastien Gouëzel, Zhouhang Zhou, Reid Barton -/ import logic.equiv.fin import topology.dense_embedding import topology.support /-! # Homeomorphisms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines homeomorphisms between two topological spaces. They are bijections with both directions continuous. We denote homeomorphisms with the notation `≃ₜ`. # Main definitions * `homeomorph α β`: The type of homeomorphisms from `α` to `β`. This type can be denoted using the following notation: `α ≃ₜ β`. # Main results * Pretty much every topological property is preserved under homeomorphisms. * `homeomorph.homeomorph_of_continuous_open`: A continuous bijection that is an open map is a homeomorphism. -/ open set filter open_locale topology variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Homeomorphism between `α` and `β`, also called topological isomorphism -/ @[nolint has_nonempty_instance] -- not all spaces are homeomorphic to each other structure homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β] extends α ≃ β := (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') infix ` ≃ₜ `:25 := homeomorph namespace homeomorph variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] instance : has_coe_to_fun (α ≃ₜ β) (λ _, α → β) := ⟨λe, e.to_equiv⟩ @[simp] lemma homeomorph_mk_coe (a : equiv α β) (b c) : ((homeomorph.mk a b c) : α → β) = a := rfl /-- Inverse of a homeomorphism. -/ protected def symm (h : α ≃ₜ β) : β ≃ₜ α := { continuous_to_fun := h.continuous_inv_fun, continuous_inv_fun := h.continuous_to_fun, to_equiv := h.to_equiv.symm } /-- 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 homeomorph (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) @[simp] lemma coe_to_equiv (h : α ≃ₜ β) : ⇑h.to_equiv = h := rfl @[simp] lemma coe_symm_to_equiv (h : α ≃ₜ β) : ⇑h.to_equiv.symm = h.symm := rfl lemma to_equiv_injective : function.injective (to_equiv : α ≃ₜ β → α ≃ β) | ⟨e, h₁, h₂⟩ ⟨e', h₁', h₂'⟩ rfl := rfl @[ext] lemma ext {h h' : α ≃ₜ β} (H : ∀ x, h x = h' x) : h = h' := to_equiv_injective $ equiv.ext H @[simp] lemma symm_symm (h : α ≃ₜ β) : h.symm.symm = h := ext $ λ _, rfl /-- Identity map as a homeomorphism. -/ @[simps apply {fully_applied := ff}] protected def refl (α : Type*) [topological_space α] : α ≃ₜ α := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, to_equiv := equiv.refl α } /-- Composition of two homeomorphisms. -/ protected def trans (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) : α ≃ₜ γ := { continuous_to_fun := h₂.continuous_to_fun.comp h₁.continuous_to_fun, continuous_inv_fun := h₁.continuous_inv_fun.comp h₂.continuous_inv_fun, to_equiv := equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) (a : α) : h₁.trans h₂ a = h₂ (h₁ a) := rfl @[simp] lemma homeomorph_mk_coe_symm (a : equiv α β) (b c) : ((homeomorph.mk a b c).symm : β → α) = a.symm := rfl @[simp] lemma refl_symm : (homeomorph.refl α).symm = homeomorph.refl α := rfl @[continuity] protected lemma continuous (h : α ≃ₜ β) : continuous h := h.continuous_to_fun @[continuity] -- otherwise `by continuity` can't prove continuity of `h.to_equiv.symm` protected lemma continuous_symm (h : α ≃ₜ β) : continuous (h.symm) := h.continuous_inv_fun @[simp] lemma apply_symm_apply (h : α ≃ₜ β) (x : β) : h (h.symm x) = x := h.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (h : α ≃ₜ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x @[simp] lemma self_trans_symm (h : α ≃ₜ β) : h.trans h.symm = homeomorph.refl α := by { ext, apply symm_apply_apply } @[simp] lemma symm_trans_self (h : α ≃ₜ β) : h.symm.trans h = homeomorph.refl β := by { ext, apply apply_symm_apply } protected lemma bijective (h : α ≃ₜ β) : function.bijective h := h.to_equiv.bijective protected lemma injective (h : α ≃ₜ β) : function.injective h := h.to_equiv.injective protected lemma surjective (h : α ≃ₜ β) : function.surjective h := h.to_equiv.surjective /-- Change the homeomorphism `f` to make the inverse function definitionally equal to `g`. -/ def change_inv (f : α ≃ₜ β) (g : β → α) (hg : function.right_inverse g f) : α ≃ₜ β := have g = f.symm, from funext (λ x, calc g x = f.symm (f (g x)) : (f.left_inv (g x)).symm ... = f.symm x : by rw hg x), { to_fun := f, inv_fun := g, left_inv := by convert f.left_inv, right_inv := by convert f.right_inv, continuous_to_fun := f.continuous, continuous_inv_fun := by convert f.symm.continuous } @[simp] lemma symm_comp_self (h : α ≃ₜ β) : ⇑h.symm ∘ ⇑h = id := funext h.symm_apply_apply @[simp] lemma self_comp_symm (h : α ≃ₜ β) : ⇑h ∘ ⇑h.symm = id := funext h.apply_symm_apply @[simp] lemma range_coe (h : α ≃ₜ β) : range h = univ := h.surjective.range_eq lemma image_symm (h : α ≃ₜ β) : image h.symm = preimage h := funext h.symm.to_equiv.image_eq_preimage lemma preimage_symm (h : α ≃ₜ β) : preimage h.symm = image h := (funext h.to_equiv.image_eq_preimage).symm @[simp] lemma image_preimage (h : α ≃ₜ β) (s : set β) : h '' (h ⁻¹' s) = s := h.to_equiv.image_preimage s @[simp] lemma preimage_image (h : α ≃ₜ β) (s : set α) : h ⁻¹' (h '' s) = s := h.to_equiv.preimage_image s protected lemma inducing (h : α ≃ₜ β) : inducing h := inducing_of_inducing_compose h.continuous h.symm.continuous $ by simp only [symm_comp_self, inducing_id] lemma induced_eq (h : α ≃ₜ β) : topological_space.induced h ‹_› = ‹_› := h.inducing.1.symm protected lemma quotient_map (h : α ≃ₜ β) : quotient_map h := quotient_map.of_quotient_map_compose h.symm.continuous h.continuous $ by simp only [self_comp_symm, quotient_map.id] lemma coinduced_eq (h : α ≃ₜ β) : topological_space.coinduced h ‹_› = ‹_› := h.quotient_map.2.symm protected lemma embedding (h : α ≃ₜ β) : embedding h := ⟨h.inducing, h.injective⟩ /-- Homeomorphism given an embedding. -/ noncomputable def of_embedding (f : α → β) (hf : embedding f) : α ≃ₜ (set.range f) := { continuous_to_fun := hf.continuous.subtype_mk _, continuous_inv_fun := by simp [hf.continuous_iff, continuous_subtype_coe], to_equiv := equiv.of_injective f hf.inj } protected lemma second_countable_topology [topological_space.second_countable_topology β] (h : α ≃ₜ β) : topological_space.second_countable_topology α := h.inducing.second_countable_topology lemma is_compact_image {s : set α} (h : α ≃ₜ β) : is_compact (h '' s) ↔ is_compact s := h.embedding.is_compact_iff_is_compact_image.symm lemma is_compact_preimage {s : set β} (h : α ≃ₜ β) : is_compact (h ⁻¹' s) ↔ is_compact s := by rw ← image_symm; exact h.symm.is_compact_image @[simp] lemma comap_cocompact (h : α ≃ₜ β) : comap h (cocompact β) = cocompact α := (comap_cocompact_le h.continuous).antisymm $ (has_basis_cocompact.le_basis_iff (has_basis_cocompact.comap h)).2 $ λ K hK, ⟨h ⁻¹' K, h.is_compact_preimage.2 hK, subset.rfl⟩ @[simp] lemma map_cocompact (h : α ≃ₜ β) : map h (cocompact α) = cocompact β := by rw [← h.comap_cocompact, map_comap_of_surjective h.surjective] protected lemma compact_space [compact_space α] (h : α ≃ₜ β) : compact_space β := { is_compact_univ := by { rw [← image_univ_of_surjective h.surjective, h.is_compact_image], apply compact_space.is_compact_univ } } protected lemma t0_space [t0_space α] (h : α ≃ₜ β) : t0_space β := h.symm.embedding.t0_space protected lemma t1_space [t1_space α] (h : α ≃ₜ β) : t1_space β := h.symm.embedding.t1_space protected lemma t2_space [t2_space α] (h : α ≃ₜ β) : t2_space β := h.symm.embedding.t2_space protected lemma t3_space [t3_space α] (h : α ≃ₜ β) : t3_space β := h.symm.embedding.t3_space protected lemma dense_embedding (h : α ≃ₜ β) : dense_embedding h := { dense := h.surjective.dense_range, .. h.embedding } @[simp] lemma is_open_preimage (h : α ≃ₜ β) {s : set β} : is_open (h ⁻¹' s) ↔ is_open s := h.quotient_map.is_open_preimage @[simp] lemma is_open_image (h : α ≃ₜ β) {s : set α} : is_open (h '' s) ↔ is_open s := by rw [← preimage_symm, is_open_preimage] protected lemma is_open_map (h : α ≃ₜ β) : is_open_map h := λ s, h.is_open_image.2 @[simp] lemma is_closed_preimage (h : α ≃ₜ β) {s : set β} : is_closed (h ⁻¹' s) ↔ is_closed s := by simp only [← is_open_compl_iff, ← preimage_compl, is_open_preimage] @[simp] lemma is_closed_image (h : α ≃ₜ β) {s : set α} : is_closed (h '' s) ↔ is_closed s := by rw [← preimage_symm, is_closed_preimage] protected lemma is_closed_map (h : α ≃ₜ β) : is_closed_map h := λ s, h.is_closed_image.2 protected lemma open_embedding (h : α ≃ₜ β) : open_embedding h := open_embedding_of_embedding_open h.embedding h.is_open_map protected lemma closed_embedding (h : α ≃ₜ β) : closed_embedding h := closed_embedding_of_embedding_closed h.embedding h.is_closed_map protected lemma normal_space [normal_space α] (h : α ≃ₜ β) : normal_space β := h.symm.closed_embedding.normal_space lemma preimage_closure (h : α ≃ₜ β) (s : set β) : h ⁻¹' (closure s) = closure (h ⁻¹' s) := h.is_open_map.preimage_closure_eq_closure_preimage h.continuous _ lemma image_closure (h : α ≃ₜ β) (s : set α) : h '' (closure s) = closure (h '' s) := by rw [← preimage_symm, preimage_closure] lemma preimage_interior (h : α ≃ₜ β) (s : set β) : h⁻¹' (interior s) = interior (h ⁻¹' s) := h.is_open_map.preimage_interior_eq_interior_preimage h.continuous _ lemma image_interior (h : α ≃ₜ β) (s : set α) : h '' (interior s) = interior (h '' s) := by rw [← preimage_symm, preimage_interior] lemma preimage_frontier (h : α ≃ₜ β) (s : set β) : h ⁻¹' (frontier s) = frontier (h ⁻¹' s) := h.is_open_map.preimage_frontier_eq_frontier_preimage h.continuous _ lemma image_frontier (h : α ≃ₜ β) (s : set α) : h '' frontier s = frontier (h '' s) := by rw [←preimage_symm, preimage_frontier] @[to_additive] lemma _root_.has_compact_mul_support.comp_homeomorph {M} [has_one M] {f : β → M} (hf : has_compact_mul_support f) (φ : α ≃ₜ β) : has_compact_mul_support (f ∘ φ) := hf.comp_closed_embedding φ.closed_embedding @[simp] lemma map_nhds_eq (h : α ≃ₜ β) (x : α) : map h (𝓝 x) = 𝓝 (h x) := h.embedding.map_nhds_of_mem _ (by simp) lemma symm_map_nhds_eq (h : α ≃ₜ β) (x : α) : map h.symm (𝓝 (h x)) = 𝓝 x := by rw [h.symm.map_nhds_eq, h.symm_apply_apply] lemma nhds_eq_comap (h : α ≃ₜ β) (x : α) : 𝓝 x = comap h (𝓝 (h x)) := h.embedding.to_inducing.nhds_eq_comap x @[simp] lemma comap_nhds_eq (h : α ≃ₜ β) (y : β) : comap h (𝓝 y) = 𝓝 (h.symm y) := by rw [h.nhds_eq_comap, h.apply_symm_apply] /-- If an bijective map `e : α ≃ β` is continuous and open, then it is a homeomorphism. -/ def homeomorph_of_continuous_open (e : α ≃ β) (h₁ : continuous e) (h₂ : is_open_map e) : α ≃ₜ β := { continuous_to_fun := h₁, continuous_inv_fun := begin rw continuous_def, intros s hs, convert ← h₂ s hs using 1, apply e.image_eq_preimage end, to_equiv := e } @[simp] lemma comp_continuous_on_iff (h : α ≃ₜ β) (f : γ → α) (s : set γ) : continuous_on (h ∘ f) s ↔ continuous_on f s := h.inducing.continuous_on_iff.symm @[simp] lemma comp_continuous_iff (h : α ≃ₜ β) {f : γ → α} : continuous (h ∘ f) ↔ continuous f := h.inducing.continuous_iff.symm @[simp] lemma comp_continuous_iff' (h : α ≃ₜ β) {f : β → γ} : continuous (f ∘ h) ↔ continuous f := h.quotient_map.continuous_iff.symm lemma comp_continuous_at_iff (h : α ≃ₜ β) (f : γ → α) (x : γ) : continuous_at (h ∘ f) x ↔ continuous_at f x := h.inducing.continuous_at_iff.symm lemma comp_continuous_at_iff' (h : α ≃ₜ β) (f : β → γ) (x : α) : continuous_at (f ∘ h) x ↔ continuous_at f (h x) := h.inducing.continuous_at_iff' (by simp) lemma comp_continuous_within_at_iff (h : α ≃ₜ β) (f : γ → α) (s : set γ) (x : γ) : continuous_within_at f s x ↔ continuous_within_at (h ∘ f) s x := h.inducing.continuous_within_at_iff @[simp] lemma comp_is_open_map_iff (h : α ≃ₜ β) {f : γ → α} : is_open_map (h ∘ f) ↔ is_open_map f := begin refine ⟨_, λ hf, h.is_open_map.comp hf⟩, intros hf, rw [← function.comp.left_id f, ← h.symm_comp_self, function.comp.assoc], exact h.symm.is_open_map.comp hf, end @[simp] lemma comp_is_open_map_iff' (h : α ≃ₜ β) {f : β → γ} : is_open_map (f ∘ h) ↔ is_open_map f := begin refine ⟨_, λ hf, hf.comp h.is_open_map⟩, intros hf, rw [← function.comp.right_id f, ← h.self_comp_symm, ← function.comp.assoc], exact hf.comp h.symm.is_open_map, end /-- If two sets are equal, then they are homeomorphic. -/ def set_congr {s t : set α} (h : s = t) : s ≃ₜ t := { continuous_to_fun := continuous_inclusion h.subset, continuous_inv_fun := continuous_inclusion h.symm.subset, to_equiv := equiv.set_congr h } /-- Sum of two homeomorphisms. -/ def sum_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : α ⊕ γ ≃ₜ β ⊕ δ := { continuous_to_fun := h₁.continuous.sum_map h₂.continuous, continuous_inv_fun := h₁.symm.continuous.sum_map h₂.symm.continuous, to_equiv := h₁.to_equiv.sum_congr h₂.to_equiv } /-- Product of two homeomorphisms. -/ def prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : α × γ ≃ₜ β × δ := { continuous_to_fun := (h₁.continuous.comp continuous_fst).prod_mk (h₂.continuous.comp continuous_snd), continuous_inv_fun := (h₁.symm.continuous.comp continuous_fst).prod_mk (h₂.symm.continuous.comp continuous_snd), to_equiv := h₁.to_equiv.prod_congr h₂.to_equiv } @[simp] lemma prod_congr_symm (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : (h₁.prod_congr h₂).symm = h₁.symm.prod_congr h₂.symm := rfl @[simp] lemma coe_prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : ⇑(h₁.prod_congr h₂) = prod.map h₁ h₂ := rfl section variables (α β γ) /-- `α × β` is homeomorphic to `β × α`. -/ def prod_comm : α × β ≃ₜ β × α := { continuous_to_fun := continuous_snd.prod_mk continuous_fst, continuous_inv_fun := continuous_snd.prod_mk continuous_fst, to_equiv := equiv.prod_comm α β } @[simp] lemma prod_comm_symm : (prod_comm α β).symm = prod_comm β α := rfl @[simp] lemma coe_prod_comm : ⇑(prod_comm α β) = prod.swap := rfl /-- `(α × β) × γ` is homeomorphic to `α × (β × γ)`. -/ def prod_assoc : (α × β) × γ ≃ₜ α × (β × γ) := { continuous_to_fun := (continuous_fst.comp continuous_fst).prod_mk ((continuous_snd.comp continuous_fst).prod_mk continuous_snd), continuous_inv_fun := (continuous_fst.prod_mk (continuous_fst.comp continuous_snd)).prod_mk (continuous_snd.comp continuous_snd), to_equiv := equiv.prod_assoc α β γ } /-- `α × {*}` is homeomorphic to `α`. -/ @[simps apply {fully_applied := ff}] def prod_punit : α × punit ≃ₜ α := { to_equiv := equiv.prod_punit α, continuous_to_fun := continuous_fst, continuous_inv_fun := continuous_id.prod_mk continuous_const } /-- `{*} × α` is homeomorphic to `α`. -/ def punit_prod : punit × α ≃ₜ α := (prod_comm _ _).trans (prod_punit _) @[simp] lemma coe_punit_prod : ⇑(punit_prod α) = prod.snd := rfl /-- If both `α` and `β` have a unique element, then `α ≃ₜ β`. -/ @[simps] def _root_.homeomorph.homeomorph_of_unique [unique α] [unique β] : α ≃ₜ β := { continuous_to_fun := @continuous_const α β _ _ default, continuous_inv_fun := @continuous_const β α _ _ default, .. equiv.equiv_of_unique α β } end /-- If each `β₁ i` is homeomorphic to `β₂ i`, then `Π i, β₁ i` is homeomorphic to `Π i, β₂ i`. -/ @[simps apply to_equiv] def Pi_congr_right {ι : Type*} {β₁ β₂ : ι → Type*} [Π i, topological_space (β₁ i)] [Π i, topological_space (β₂ i)] (F : Π i, β₁ i ≃ₜ β₂ i) : (Π i, β₁ i) ≃ₜ (Π i, β₂ i) := { continuous_to_fun := continuous_pi (λ i, (F i).continuous.comp $ continuous_apply i), continuous_inv_fun := continuous_pi (λ i, (F i).symm.continuous.comp $ continuous_apply i), to_equiv := equiv.Pi_congr_right (λ i, (F i).to_equiv) } @[simp] lemma Pi_congr_right_symm {ι : Type*} {β₁ β₂ : ι → Type*} [Π i, topological_space (β₁ i)] [Π i, topological_space (β₂ i)] (F : Π i, β₁ i ≃ₜ β₂ i) : (Pi_congr_right F).symm = Pi_congr_right (λ i, (F i).symm) := rfl /-- `ulift α` is homeomorphic to `α`. -/ def {u v} ulift {α : Type u} [topological_space α] : ulift.{v u} α ≃ₜ α := { continuous_to_fun := continuous_ulift_down, continuous_inv_fun := continuous_ulift_up, to_equiv := equiv.ulift } section distrib /-- `(α ⊕ β) × γ` is homeomorphic to `α × γ ⊕ β × γ`. -/ def sum_prod_distrib : (α ⊕ β) × γ ≃ₜ α × γ ⊕ β × γ := homeomorph.symm $ homeomorph_of_continuous_open (equiv.sum_prod_distrib α β γ).symm ((continuous_inl.prod_map continuous_id).sum_elim (continuous_inr.prod_map continuous_id)) $ (is_open_map_inl.prod is_open_map.id).sum_elim (is_open_map_inr.prod is_open_map.id) /-- `α × (β ⊕ γ)` is homeomorphic to `α × β ⊕ α × γ`. -/ def prod_sum_distrib : α × (β ⊕ γ) ≃ₜ α × β ⊕ α × γ := (prod_comm _ _).trans $ sum_prod_distrib.trans $ sum_congr (prod_comm _ _) (prod_comm _ _) variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)] /-- `(Σ i, σ i) × β` is homeomorphic to `Σ i, (σ i × β)`. -/ def sigma_prod_distrib : ((Σ i, σ i) × β) ≃ₜ (Σ i, (σ i × β)) := homeomorph.symm $ homeomorph_of_continuous_open (equiv.sigma_prod_distrib σ β).symm (continuous_sigma $ λ i, continuous_sigma_mk.fst'.prod_mk continuous_snd) (is_open_map_sigma.2 $ λ i, is_open_map_sigma_mk.prod is_open_map.id) end distrib /-- If `ι` has a unique element, then `ι → α` is homeomorphic to `α`. -/ @[simps { fully_applied := ff }] def fun_unique (ι α : Type*) [unique ι] [topological_space α] : (ι → α) ≃ₜ α := { to_equiv := equiv.fun_unique ι α, continuous_to_fun := continuous_apply _, continuous_inv_fun := continuous_pi (λ _, continuous_id) } /-- Homeomorphism between dependent functions `Π i : fin 2, α i` and `α 0 × α 1`. -/ @[simps { fully_applied := ff }] def {u} pi_fin_two (α : fin 2 → Type u) [Π i, topological_space (α i)] : (Π i, α i) ≃ₜ α 0 × α 1 := { to_equiv := pi_fin_two_equiv α, continuous_to_fun := (continuous_apply 0).prod_mk (continuous_apply 1), continuous_inv_fun := continuous_pi $ fin.forall_fin_two.2 ⟨continuous_fst, continuous_snd⟩ } /-- Homeomorphism between `α² = fin 2 → α` and `α × α`. -/ @[simps { fully_applied := ff }] def fin_two_arrow : (fin 2 → α) ≃ₜ α × α := { to_equiv := fin_two_arrow_equiv α, .. pi_fin_two (λ _, α) } /-- A subset of a topological space is homeomorphic to its image under a homeomorphism. -/ @[simps] def image (e : α ≃ₜ β) (s : set α) : s ≃ₜ e '' s := { continuous_to_fun := by continuity!, continuous_inv_fun := by continuity!, to_equiv := e.to_equiv.image s, } /-- `set.univ α` is homeomorphic to `α`. -/ @[simps { fully_applied := ff }] def set.univ (α : Type*) [topological_space α] : (univ : set α) ≃ₜ α := { to_equiv := equiv.set.univ α, continuous_to_fun := continuous_subtype_coe, continuous_inv_fun := continuous_id.subtype_mk _ } /-- `s ×ˢ t` is homeomorphic to `s × t`. -/ @[simps] def set.prod (s : set α) (t : set β) : ↥(s ×ˢ t) ≃ₜ s × t := { to_equiv := equiv.set.prod s t, continuous_to_fun := (continuous_subtype_coe.fst.subtype_mk _).prod_mk (continuous_subtype_coe.snd.subtype_mk _), continuous_inv_fun := (continuous_subtype_coe.fst'.prod_mk continuous_subtype_coe.snd').subtype_mk _ } section variable {ι : Type*} /-- The topological space `Π i, β i` can be split as a product by separating the indices in ι depending on whether they satisfy a predicate p or not.-/ @[simps] def pi_equiv_pi_subtype_prod (p : ι → Prop) (β : ι → Type*) [Π i, topological_space (β i)] [decidable_pred p] : (Π i, β i) ≃ₜ (Π i : {x // p x}, β i) × Π i : {x // ¬p x}, β i := { to_equiv := equiv.pi_equiv_pi_subtype_prod p β, continuous_to_fun := by apply continuous.prod_mk; exact continuous_pi (λ j, continuous_apply j), continuous_inv_fun := continuous_pi $ λ j, begin dsimp only [equiv.pi_equiv_pi_subtype_prod], split_ifs, exacts [(continuous_apply _).comp continuous_fst, (continuous_apply _).comp continuous_snd], end } variables [decidable_eq ι] (i : ι) /-- A product of topological spaces can be split as the binary product of one of the spaces and the product of all the remaining spaces. -/ @[simps] def pi_split_at (β : ι → Type*) [Π j, topological_space (β j)] : (Π j, β j) ≃ₜ β i × Π j : {j // j ≠ i}, β j := { to_equiv := equiv.pi_split_at i β, continuous_to_fun := (continuous_apply i).prod_mk (continuous_pi $ λ j, continuous_apply j), continuous_inv_fun := continuous_pi $ λ j, by { dsimp only [equiv.pi_split_at], split_ifs, subst h, exacts [continuous_fst, (continuous_apply _).comp continuous_snd] } } variable (β) /-- A product of copies of a topological space can be split as the binary product of one copy and the product of all the remaining copies. -/ @[simps] def fun_split_at : (ι → β) ≃ₜ β × ({j // j ≠ i} → β) := pi_split_at i _ end end homeomorph /-- An inducing equiv between topological spaces is a homeomorphism. -/ @[simps] def equiv.to_homeomorph_of_inducing [topological_space α] [topological_space β] (f : α ≃ β) (hf : inducing f) : α ≃ₜ β := { continuous_to_fun := hf.continuous, continuous_inv_fun := hf.continuous_iff.2 $ by simpa using continuous_id, .. f } namespace continuous variables [topological_space α] [topological_space β] lemma continuous_symm_of_equiv_compact_to_t2 [compact_space α] [t2_space β] {f : α ≃ β} (hf : continuous f) : continuous f.symm := begin rw continuous_iff_is_closed, intros C hC, have hC' : is_closed (f '' C) := (hC.is_compact.image hf).is_closed, rwa equiv.image_eq_preimage at hC', end /-- Continuous equivalences from a compact space to a T2 space are homeomorphisms. This is not true when T2 is weakened to T1 (see `continuous.homeo_of_equiv_compact_to_t2.t1_counterexample`). -/ @[simps] def homeo_of_equiv_compact_to_t2 [compact_space α] [t2_space β] {f : α ≃ β} (hf : continuous f) : α ≃ₜ β := { continuous_to_fun := hf, continuous_inv_fun := hf.continuous_symm_of_equiv_compact_to_t2, ..f } end continuous
42d033739f4f75c0285ea34870b267d1e282ddc9
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/algebra/basic.lean
1035879d532500d2d48e0a794ba20c15b1ffcdf8
[ "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
57,609
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 tactic.nth_rewrite import data.matrix.basic import data.equiv.ring_aut import linear_algebra.tensor_product import ring_theory.subring import deprecated.subring import algebra.opposites /-! # Algebra over Commutative Semiring In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`, algebra equivalences `alg_equiv`. We also define usual operations on `alg_hom`s (`id`, `comp`). `subalgebra`s are defined in `algebra.algebra.subalgebra`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. For the category of `R`-algebras, denoted `Algebra R`, see the file `algebra/category/Algebra/basic.lean`. ## Notations * `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`. * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ universes u v w u₁ v₁ open_locale tensor_product big_operators section prio -- We set this priority to 0 later in this file set_option extends_priority 200 /- control priority of `instance [algebra R A] : has_scalar R A` -/ /-- Given a commutative (semi)ring `R`, an `R`-algebra is a (possibly noncommutative) (semi)ring `A` endowed with a morphism of rings `R →+* A` which lands in the center of `A`. For convenience, this typeclass extends `has_scalar R A` where the scalar action must agree with left multiplication by the image of the structure morphism. Given an `algebra R A` instance, the structure morphism `R →+* A` is denoted `algebra_map R A`. -/ @[nolint has_inhabited_instance] class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] extends has_scalar R A, R →+* A := (commutes' : ∀ r x, to_fun r * x = x * to_fun r) (smul_def' : ∀ r x, r • x = to_fun r * x) end prio /-- Embedding `R →+* A` given by `algebra` structure. -/ def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A := algebra.to_ring_hom /-- Creating an algebra from a morphism to the center of a semiring. -/ def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S) (h : ∀ c x, i c * x = x * i c) : algebra R S := { smul := λ c x, i c * x, commutes' := h, smul_def' := λ c x, rfl, to_ring_hom := i} /-- Creating an algebra from a morphism to a commutative semiring. -/ def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : algebra R S := i.to_algebra' $ λ _, mul_comm _ lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : @algebra_map R S _ _ i.to_algebra = i := rfl namespace algebra variables {R : Type u} {S : Type v} {A : Type w} {B : Type*} /-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure. If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra` over `R`. -/ def of_module' [comm_semiring R] [semiring A] [module R A] (h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x) (h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A := { to_fun := λ r, r • 1, map_one' := one_smul _ _, map_mul' := λ r₁ r₂, by rw [h₁, mul_smul], map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul r₁ r₂ 1, commutes' := λ r x, by simp only [h₁, h₂], smul_def' := λ r x, by simp only [h₁] } /-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure. If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A` is an `algebra` over `R`. -/ def of_module [comm_semiring R] [semiring A] [module R A] (h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y)) (h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A := of_module' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one]) section semiring variables [comm_semiring R] [comm_semiring S] variables [semiring A] [algebra R A] [semiring B] [algebra R B] lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x /-- To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree, it suffices to check the `algebra_map`s agree. -/ -- We'll later use this to show `algebra ℤ M` is a subsingleton. @[ext] lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A) (w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } = by { haveI := Q, exact algebra_map R A r }) : P = Q := begin unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ }, congr, { funext r a, replace w := congr_arg (λ s, s * a) (w r), simp only [←algebra.smul_def''] at w, apply w, }, { ext r, exact w r, }, { apply proof_irrel_heq, }, { apply proof_irrel_heq, }, end @[priority 200] -- see Note [lower instance priority] instance to_module : module R A := { one_smul := by simp [smul_def''], mul_smul := by simp [smul_def'', mul_assoc], smul_add := by simp [smul_def'', mul_add], smul_zero := by simp [smul_def''], add_smul := by simp [smul_def'', add_mul], zero_smul := by simp [smul_def''] } -- from now on, we don't want to use the following instance anymore attribute [instance, priority 0] algebra.to_has_scalar lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 := calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm ... = r • 1 : (algebra.smul_def r 1).symm lemma algebra_map_eq_smul_one' : ⇑(algebra_map R A) = λ r, r • (1 : A) := funext algebra_map_eq_smul_one theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) := by rw [← mul_assoc, ← commutes, mul_assoc] instance _root_.is_scalar_tower.right : is_scalar_tower R A A := ⟨λ x y z, by rw [smul_eq_mul, smul_eq_mul, smul_def, smul_def, mul_assoc]⟩ /-- This is just a special case of the global `mul_smul_comm` lemma that requires less typeclass search (and was here first). -/ @[simp] protected lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := -- TODO: set up `is_scalar_tower.smul_comm_class` earlier so that we can actually prove this using -- `mul_smul_comm s x y`. by rw [smul_def, smul_def, left_comm] /-- This is just a special case of the global `smul_mul_assoc` lemma that requires less typeclass search (and was here first). -/ @[simp] protected lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := smul_mul_assoc r x y section variables {r : R} {a : A} @[simp] lemma bit0_smul_one : bit0 r • (1 : A) = r • 2 := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit1_smul_one : bit1 r • (1 : A) = r • 2 + 1 := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a := by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel } end variables (R A) /-- The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`, packaged as an `R`-linear map. -/ protected def linear_map : R →ₗ[R] A := { map_smul' := λ x y, by simp [algebra.smul_def], ..algebra_map R A } @[simp] lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl instance id : algebra R R := (ring_hom.id R).to_algebra variables {R A} namespace id @[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl @[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl end id section prod variables (R A B) instance : algebra R (A × B) := { commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] }, smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] }, .. prod.module, .. ring_hom.prod (algebra_map R A) (algebra_map R B) } variables {R A B} @[simp] lemma algebra_map_prod_apply (r : R) : algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl end prod /-- Algebra over a subsemiring. This builds upon `subsemiring.module`. -/ instance of_subsemiring (S : subsemiring R) : algebra S A := { smul := (•), commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp S.subtype } /-- Algebra over a subring. This builds upon `subring.module`. -/ instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : subring R) : algebra S A := { smul := (•), .. algebra.of_subsemiring S.to_subsemiring, .. (algebra_map R A).comp S.subtype } lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S →+* R) = subring.subtype S := rfl lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S → R) = subtype.val := rfl lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) : algebra_map S R x = x := rfl section local attribute [instance] subset.comm_ring /-- Algebra over a set that is closed under the ring operations. -/ local attribute [instance] def of_is_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : set R) [is_subring S] : algebra S A := algebra.of_subring S.to_subring lemma is_subring_coe_algebra_map_hom {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S →+* R) = is_subring.subtype S := rfl lemma is_subring_coe_algebra_map {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S → R) = subtype.val := rfl lemma is_subring_algebra_map_apply {R : Type*} [comm_ring R] (S : set R) [is_subring S] (x : S) : algebra_map S R x = x := rfl lemma set_range_subset {R : Type*} [comm_ring R] {T₁ T₂ : set R} [is_subring T₁] (hyp : T₁ ⊆ T₂) : set.range (algebra_map T₁ R) ⊆ T₂ := begin rintros x ⟨⟨t, ht⟩, rfl⟩, exact hyp ht, end end /-- Explicit characterization of the submonoid map in the case of an algebra. `S` is made explicit to help with type inference -/ def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S] (M : submonoid R) : (submonoid S) := submonoid.map (algebra_map R S : R →* S) M lemma mem_algebra_map_submonoid_of_mem [algebra R S] {M : submonoid R} (x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M := set.mem_image_of_mem (algebra_map R S) x.2 end semiring section ring variables [comm_ring R] variables (R) /-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure. -/ def semiring_to_ring [semiring A] [algebra R A] : ring A := { ..module.add_comm_monoid_to_add_comm_group R, ..(infer_instance : semiring A) } variables {R} lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) : x * (x - algebra_map R A r) = (x - algebra_map R A r) * x := by rw [mul_sub, ←commutes, sub_mul] lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) : x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x := begin induction n with n ih, { simp }, { rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc], } end /-- If `algebra_map R A` is injective and `A` has no zero divisors, `R`-multiples in `A` are zero only if one of the factors is zero. Cannot be an instance because there is no `injective (algebra_map R A)` typeclass. -/ lemma no_zero_smul_divisors.of_algebra_map_injective [semiring A] [algebra R A] [no_zero_divisors A] (h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A := ⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left ((algebra_map R A).injective_iff.mp h _)⟩ end ring section field variables [field R] [semiring A] [algebra R A] @[priority 100] -- see note [lower instance priority] instance [nontrivial A] [no_zero_divisors A] : no_zero_smul_divisors R A := no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective end field end algebra namespace opposite variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] instance : algebra R Aᵒᵖ := { to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _, smul_def' := λ c x, unop_injective $ by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] }, commutes' := λ r, op_induction $ λ x, by dsimp; simp only [← op_mul, algebra.commutes], ..opposite.has_scalar A R } @[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵒᵖ c = op (algebra_map R A c) := rfl end opposite namespace module variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [module R M] instance endomorphism_algebra : algebra R (M →ₗ[R] M) := { to_fun := λ r, r • linear_map.id, map_one' := one_smul _ _, map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul _ _ _, map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] }, commutes' := by { intros, ext, simp }, smul_def' := by { intros, ext, simp } } lemma algebra_map_End_eq_smul_id (a : R) : (algebra_map R (End R M)) a = a • linear_map.id := rfl @[simp] lemma algebra_map_End_apply (a : R) (m : M) : (algebra_map R (End R M)) a m = a • m := rfl @[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v) [field K] [add_comm_group V] [module K V] (a : K) (ha : a ≠ 0) : ((algebra_map K (End K V)) a).ker = ⊥ := linear_map.ker_smul _ _ ha end module instance matrix_algebra (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_semiring R] : algebra R (matrix n n R) := { commutes' := by { intros, simp [matrix.scalar], }, smul_def' := by { intros, simp [matrix.scalar], }, ..(matrix.scalar n) } @[simp] lemma matrix.algebra_map_eq_smul (n : Type u) {R : Type v} [decidable_eq n] [fintype n] [comm_semiring R] (r : R) : (algebra_map R (matrix n n R)) r = r • 1 := rfl set_option old_structure_cmd true /-- Defining the homomorphism in the category R-Alg. -/ @[nolint has_inhabited_instance] structure alg_hom (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`" infixr ` →ₐ `:25 := alg_hom _ notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} section semiring variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩ initialize_simps_projections alg_hom (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : A →ₐ[R] B) : f.to_fun = f := rfl instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩ instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩ instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩ @[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) : ⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl @[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl variables (φ : A →ₐ[R] B) theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ := by { cases φ₁, cases φ₂, congr, exact H } theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) := λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B), from congr_arg _ H theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) := ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) := ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := H ▸ rfl protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := h ▸ rfl @[ext] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := coe_fn_inj $ funext H theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := ⟨alg_hom.congr_fun, ext⟩ @[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) : (⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl @[simp] theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext $ φ.commutes @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := φ.to_ring_hom.map_add r s @[simp] lemma map_zero : φ 0 = 0 := φ.to_ring_hom.map_zero @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := φ.to_ring_hom.map_mul x y @[simp] lemma map_one : φ 1 = 1 := φ.to_ring_hom.map_one @[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x := by simp only [algebra.smul_def, map_mul, commutes] @[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n := φ.to_ring_hom.map_pow x n lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) : φ (∑ x in s, f x) = ∑ x in s, φ (f x) := φ.to_ring_hom.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.sum g) = f.sum (λ i a, φ (g i a)) := φ.map_sum _ _ @[simp] lemma map_nat_cast (n : ℕ) : φ n = n := φ.to_ring_hom.map_nat_cast n @[simp] lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := φ.to_ring_hom.map_bit0 x @[simp] lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := φ.to_ring_hom.map_bit1 x /-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/ def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B := { to_fun := f, commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one], .. f } @[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl section variables (R A) /-- Identity map as an `alg_hom`. -/ protected def id : A →ₐ[R] A := { commutes' := λ _, rfl, ..ring_hom.id A } @[simp] lemma coe_id : ⇑(alg_hom.id R A) = id := rfl @[simp] lemma id_to_ring_hom : (alg_hom.id R A : A →+* A) = ring_hom.id _ := rfl end lemma id_apply (p : A) : alg_hom.id R A p = p := rfl /-- Composition of algebra homeomorphisms. -/ def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C := { commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl, .. φ₁.to_ring_hom.comp ↑φ₂ } @[simp] lemma coe_comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂) = φ₁ ∘ φ₂ := rfl lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl lemma comp_to_ring_hom (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂ : A →+* C) = (φ₁ : B →+* C).comp ↑φ₂ := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, map_add' := φ.map_add, map_smul' := φ.map_smul } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H @[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl lemma map_list_prod (s : list A) : φ s.prod = (s.map φ).prod := φ.to_ring_hom.map_list_prod s end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) lemma map_multiset_prod (s : multiset A) : φ s.prod = (s.map φ).prod := φ.to_ring_hom.map_multiset_prod s lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) : φ (∏ x in s, f x) = ∏ x in s, φ (f x) := φ.to_ring_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.prod g) = f.prod (λ i a, φ (g i a)) := φ.map_prod _ _ end comm_semiring section ring variables [comm_semiring R] [ring A] [ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_neg (x) : φ (-x) = -φ x := φ.to_ring_hom.map_neg x @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := φ.to_ring_hom.map_sub x y @[simp] lemma map_int_cast (n : ℤ) : φ n = n := φ.to_ring_hom.map_int_cast n end ring section division_ring variables [comm_ring R] [division_ring A] [division_ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ := φ.to_ring_hom.map_inv x @[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y := φ.to_ring_hom.map_div x y end division_ring theorem injective_iff {R A B : Type*} [comm_semiring R] [ring A] [semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : function.injective f ↔ (∀ x, f x = 0 → x = 0) := ring_hom.injective_iff (f : A →+* B) end alg_hom @[simp] lemma rat.smul_one_eq_coe {A : Type*} [division_ring A] [algebra ℚ A] (m : ℚ) : m • (1 : A) = ↑m := by rw [algebra.smul_def, mul_one, ring_hom.eq_rat_cast] set_option old_structure_cmd true /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure alg_equiv (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) attribute [nolint doc_blame] alg_equiv.to_ring_equiv attribute [nolint doc_blame] alg_equiv.to_equiv attribute [nolint doc_blame] alg_equiv.to_add_equiv attribute [nolint doc_blame] alg_equiv.to_mul_equiv notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A' namespace alg_equiv variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁} section semiring variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] variables (e : A₁ ≃ₐ[R] A₂) instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩ @[ext] lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} : Π {x x' : A₁}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := h ▸ rfl lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) := begin intros f g w, ext, exact congr_fun w a, end instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩ @[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} : ⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun := rfl @[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) : (⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl @[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl -- TODO: decide on a simp-normal form so that only one of these two lemmas is needed @[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl @[simp] lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) := begin intros f g w, ext, replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) := congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w, exact congr_fun w a, end @[simp] lemma map_add : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add @[simp] lemma map_zero : e 0 = 0 := e.to_add_equiv.map_zero @[simp] lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul @[simp] lemma map_one : e 1 = 1 := e.to_mul_equiv.map_one @[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r := e.commutes' lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∑ x in s, f x) = ∑ x in s, e (f x) := e.to_add_equiv.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum (λ i b, e (g i b)) := e.map_sum _ _ /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to_*_hom` projections. The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/ def to_alg_hom : A₁ →ₐ[R] A₂ := { map_one' := e.map_one, map_zero' := e.map_zero, ..e } instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) := ⟨to_alg_hom⟩ @[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl @[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e := rfl /-- The two paths coercion can take to a `ring_hom` are equivalent -/ lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) := rfl @[simp] lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow lemma injective : function.injective e := e.to_equiv.injective lemma surjective : function.surjective e := e.to_equiv.surjective lemma bijective : function.bijective e := e.to_equiv.bijective instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩ instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩ /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := 1 @[simp] lemma refl_to_alg_hom : ↑(refl : A₁ ≃ₐ[R] A₁) = alg_hom.id R A₁ := rfl @[simp] lemma coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr, change _ = e _, rw e.commutes, }, ..e.to_ring_equiv.symm, } /-- See Note [custom simps projection] -/ def simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply) @[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl @[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e := by { ext, refl, } lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) : (⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) : (⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm = { to_fun := f', inv_fun := f, ..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'], ..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), } @[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp] lemma coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ := by { ext, simp } @[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ := by { ext, simp } theorem left_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.left_inverse e.symm e := e.left_inv theorem right_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.right_inverse e.symm e := e.right_inv /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') := { to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom, inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom, left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp], simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] }, right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm], simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } } lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply], congr, exact (e₂.symm_apply_apply _).symm } @[simp] lemma arrow_congr_refl : arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) := by { ext, refl } @[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := by { ext, refl } @[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := by { ext, refl } /-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/ def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ := { inv_fun := g, left_inv := alg_hom.ext_iff.1 h₂, right_inv := alg_hom.ext_iff.1 h₁, ..f } /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ := { .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f } /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { to_fun := e.to_fun, map_add' := λ x y, by simp, map_smul' := λ r x, by simp [algebra.smul_def''], inv_fun := e.symm.to_fun, left_inv := e.left_inv, right_inv := e.right_inv, } @[simp] lemma to_linear_equiv_apply (e : A₁ ≃ₐ[R] A₂) (x : A₁) : e.to_linear_equiv x = e x := rfl theorem to_linear_equiv_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_equiv = e₂.to_linear_equiv) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_equiv x = e₂.to_linear_equiv x, by rw H /-- Interpret an algebra equivalence as a linear map. -/ def to_linear_map : A₁ →ₗ[R] A₂ := e.to_alg_hom.to_linear_map @[simp] lemma to_alg_hom_to_linear_map : (e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_equiv_to_linear_map : e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl theorem to_linear_map_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_map = e₂.to_linear_map) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_map x = e₂.to_linear_map x, by rw H @[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl section of_linear_equiv variables (l : A₁ ≃ₗ[R] A₂) (map_mul : ∀ x y : A₁, l (x * y) = l x * l y) (commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r) /-- Upgrade a linear equivalence to an algebra equivalence, given that it distributes over multiplication and action of scalars. -/ def of_linear_equiv : A₁ ≃ₐ[R] A₂ := { to_fun := l, inv_fun := l.symm, map_mul' := map_mul, commutes' := commutes, ..l } @[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) : of_linear_equiv e.to_linear_equiv map_mul commutes = e := by { ext, refl } @[simp] lemma to_linear_equiv_of_linear_equiv : to_linear_equiv (of_linear_equiv l map_mul commutes) = l := by { ext, refl } @[simp] lemma of_linear_equiv_apply (x : A₁) : of_linear_equiv l map_mul commutes x = l x := rfl end of_linear_equiv instance aut : group (A₁ ≃ₐ[R] A₁) := { mul := λ ϕ ψ, ψ.trans ϕ, mul_assoc := λ ϕ ψ χ, rfl, one := 1, one_mul := λ ϕ, by { ext, refl }, mul_one := λ ϕ, by { ext, refl }, inv := symm, mul_left_inv := λ ϕ, by { ext, exact symm_apply_apply ϕ a } } @[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl /-- An algebra isomorphism induces a group isomorphism between automorphism groups -/ @[simps apply] def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) := { to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ), inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm), left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] }, right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] }, map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } } @[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) := by { ext, refl } @[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl @[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) : (aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∏ x in s, f x) = ∏ x in s, e (f x) := e.to_alg_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod (λ i a, e (g i a)) := e.to_alg_hom.map_finsupp_prod f g end comm_semiring section ring variables [comm_ring R] [ring A₁] [ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_neg (x) : e (-x) = -e x := e.to_alg_hom.map_neg x @[simp] lemma map_sub (x y) : e (x - y) = e x - e y := e.to_alg_hom.map_sub x y end ring section division_ring variables [comm_ring R] [division_ring A₁] [division_ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ := e.to_alg_hom.map_inv x @[simp] lemma map_div (x y) : e (x / y) = e x / e y := e.to_alg_hom.map_div x y end division_ring end alg_equiv namespace matrix /-! ### `matrix` section Specialize `matrix.one_map` and `matrix.zero_map` to `alg_hom` and `alg_equiv`. TODO: there should be a way to avoid restating these for each `foo_hom`. -/ variables {R A₁ A₂ n : Type*} [fintype n] section semiring variables [comm_semiring R] [semiring A₁] [algebra R A₁] [semiring A₂] [algebra R A₂] /-- A version of `matrix.one_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_one [decidable_eq n] (f : A₁ →ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.one_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_one [decidable_eq n] (f : A₁ ≃ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.zero_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_zero (f : A₁ →ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero /-- A version of `matrix.zero_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_zero (f : A₁ ≃ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero end semiring end matrix namespace algebra variables (R : Type u) (S : Type v) (A : Type w) include R S A /-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it when `algebra R S` and `algebra S A`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. -/ /- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and `algebra ?m_1 A -/ /- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the appropriate type classes -/ @[nolint unused_arguments] def comap : Type w := A instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h instance comap.semiring [h : semiring A] : semiring (comap R S A) := h instance comap.ring [h : ring A] : ring (comap R S A) := h instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] : algebra S (comap R S A) := h /-- Identity homomorphism `A →ₐ[S] comap R S A`. -/ def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] : A →ₐ[S] comap R S A := alg_hom.id S A /-- Identity homomorphism `comap R S A →ₐ[S] A`. -/ def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] : comap R S A →ₐ[S] A := alg_hom.id S A variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] /-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map R S r • x : A), commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _, .. (algebra_map S A).comp (algebra_map R S) } /-- Embedding of `S` into `comap R S A`. -/ def to_comap : S →ₐ[R] comap R S A := { commutes' := λ r, rfl, .. algebra_map S A } theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl end algebra section variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] include R /-- R ⟶ S induces S-Alg ⥤ R-Alg. See `alg_hom.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/ def alg_hom.comap (φ : A →ₐ[S] B) : algebra.comap R S A →ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } /-- `alg_hom.comap` for `alg_equiv`. See `alg_equiv.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/ def alg_equiv.comap (φ : A ≃ₐ[S] B) : algebra.comap R S A ≃ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } end section nat variables {R : Type*} [semiring R] -- Lower the priority so that `algebra.id` is picked most of the time when working with -- `ℕ`-algebras. This is only an issue since `algebra.id` and `algebra_nat` are not yet defeq. -- TODO: fix this by adding an `of_nat` field to semirings. /-- Semiring ⥤ ℕ-Alg -/ @[priority 99] instance algebra_nat : algebra ℕ R := { commutes' := nat.cast_commute, smul_def' := λ _ _, nsmul_eq_mul _ _, to_ring_hom := nat.cast_ring_hom R } instance nat_algebra_subsingleton : subsingleton (algebra ℕ R) := ⟨λ P Q, by { ext, simp, }⟩ end nat namespace ring_hom variables {R S : Type*} /-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/ def to_nat_alg_hom [semiring R] [semiring S] (f : R →+* S) : R →ₐ[ℕ] S := { to_fun := f, commutes' := λ n, by simp, .. f } /-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/ def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) : R →ₐ[ℤ] S := { commutes' := λ n, by simp, .. f } @[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r := ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r /-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/ def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) : R →ₐ[ℚ] S := { commutes' := f.map_rat_algebra_map, .. f } end ring_hom namespace rat instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α := (rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x @[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ := subsingleton.elim _ _ -- TODO[gh-6025]: make this an instance once safe to do so lemma algebra_rat_subsingleton {α} [semiring α] : subsingleton (algebra ℚ α) := ⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩ end rat namespace algebra open module variables (R : Type u) (A : Type v) variables [comm_semiring R] [semiring A] [algebra R A] /-- `algebra_map` as an `alg_hom`. -/ def of_id : R →ₐ[R] A := { commutes' := λ _, rfl, .. algebra_map R A } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₐ[R] (End R A) := { map_one' := by { ext a, exact one_mul a }, map_mul' := by { intros a b, ext c, exact mul_assoc a b c }, map_zero' := by { ext a, exact zero_mul a }, commutes' := by { intro r, ext a, dsimp, rw [smul_def] }, .. (show A →ₗ[R] A →ₗ[R] A, from linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm])) } variables {A} /-- The multiplication on the left in an algebra is a linear map. -/ def lmul_left (r : A) : A →ₗ A := lmul R A r /-- The multiplication on the right in an algebra is a linear map. -/ def lmul_right (r : A) : A →ₗ A := (lmul R A).to_linear_map.flip r /-- Simultaneous multiplication on the left and right is a linear map. -/ def lmul_left_right (vw: A × A) : A →ₗ[R] A := (lmul_right R vw.2).comp (lmul_left R vw.1) /-- The multiplication map on an algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/ def lmul' : A ⊗[R] A →ₗ[R] A := tensor_product.lift (lmul R A).to_linear_map variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R p q = q * p := rfl @[simp] lemma lmul_left_right_apply (vw : A × A) (p : A) : lmul_left_right R vw p = vw.1 * p * vw.2 := rfl @[simp] lemma lmul_left_one : lmul_left R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, one_mul, id.def, lmul_left_apply] } @[simp] lemma lmul_left_mul (a b : A) : lmul_left R (a * b) = (lmul_left R a).comp (lmul_left R b) := by { ext, simp only [lmul_left_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul_right_one : lmul_right R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, mul_one, id.def, lmul_right_apply] } @[simp] lemma lmul_right_mul (a b : A) : lmul_right R (a * b) = (lmul_right R b).comp (lmul_right R a) := by { ext, simp only [lmul_right_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul'_apply {x y : A} : lmul' R (x ⊗ₜ y) = x * y := by simp only [algebra.lmul', tensor_product.lift.tmul, alg_hom.to_linear_map_apply, lmul_apply] instance linear_map.module' (R : Type u) [comm_semiring R] (M : Type v) [add_comm_monoid M] [module R M] (S : Type w) [comm_semiring S] [algebra R S] : module S (M →ₗ[R] S) := { smul := λ s f, linear_map.llcomp _ _ _ _ (algebra.lmul R S s) f, one_smul := λ f, linear_map.ext $ λ x, one_mul _, mul_smul := λ s₁ s₂ f, linear_map.ext $ λ x, mul_assoc _ _ _, smul_add := λ s f g, linear_map.map_add _ _ _, smul_zero := λ s, linear_map.map_zero _, add_smul := λ s₁ s₂ f, linear_map.ext $ λ x, add_mul _ _ _, zero_smul := λ f, linear_map.ext $ λ x, zero_mul _ } end algebra section ring namespace algebra variables {R A : Type*} [comm_semiring R] [ring A] [algebra R A] lemma lmul_left_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul_left R x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_right_injective' hx } lemma lmul_right_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul_right R x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_left_injective' hx } lemma lmul_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul R A x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_right_injective' hx } end algebra end ring section int variables (R : Type*) [ring R] -- Lower the priority so that `algebra.id` is picked most of the time when working with -- `ℤ`-algebras. This is only an issue since `algebra.id ℤ` and `algebra_int ℤ` are not yet defeq. -- TODO: fix this by adding an `of_int` field to rings. /-- Ring ⥤ ℤ-Alg -/ @[priority 99] instance algebra_int : algebra ℤ R := { commutes' := int.cast_commute, smul_def' := λ _ _, gsmul_eq_mul _ _, to_ring_hom := int.cast_ring_hom R } variables {R} instance int_algebra_subsingleton : subsingleton (algebra ℤ R) := ⟨λ P Q, by { ext, simp, }⟩ end int /-! The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra. We couldn't set this up back in `algebra.pi_instances` because this file imports it. -/ namespace pi variable {I : Type u} -- The indexing type variable {R : Type*} -- The scalar type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) variables (I f) instance algebra {r : comm_semiring R} [s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] : algebra R (Π i : I, f i) := { commutes' := λ a f, begin ext, simp [algebra.commutes], end, smul_def' := λ a f, begin ext, simp [algebra.smul_def''], end, ..pi.ring_hom (λ i, algebra_map R (f i)) } @[simp] lemma algebra_map_apply {r : comm_semiring R} [s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] (a : R) (i : I) : algebra_map R (Π i, f i) a i = algebra_map R (f i) a := rfl -- One could also build a `Π i, R i`-algebra structure on `Π i, A i`, -- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful. variables {I} (R) (f) /-- `function.eval` as an `alg_hom`. The name matches `pi.eval_ring_hom`, `pi.eval_monoid_hom`, etc. -/ @[simps] def eval_alg_hom {r : comm_semiring R} [Π i, semiring (f i)] [Π i, algebra R (f i)] (i : I) : (Π i, f i) →ₐ[R] f i := { to_fun := λ f, f i, commutes' := λ r, rfl, .. pi.eval_ring_hom f i} end pi section is_scalar_tower variables {R : Type*} [comm_semiring R] variables (A : Type*) [semiring A] [algebra R A] variables {M : Type*} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M] variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A N] lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m := by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul] @[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m := (algebra_compatible_smul A r m).symm variable {A} @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M := ⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul, ←algebra_compatible_smul]⟩ @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M := smul_comm_class.symm _ _ _ lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m := smul_comm _ _ _ namespace linear_map instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) := ⟨restrict_scalars R⟩ variables (R) {A M N} @[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) : (f.restrict_scalars R : M → N) = f := rfl @[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) : ((f : M →ₗ[R] N) : M → N) = f := rfl /-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over a commutative semiring `R` and `M` a module over `R`. -/ def lto_fun (R : Type u) (M : Type v) (A : Type w) [comm_semiring R] [add_comm_monoid M] [module R M] [comm_ring A] [algebra R A] : (M →ₗ[R] A) →ₗ[A] (M → A) := { to_fun := linear_map.to_fun, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } end linear_map end is_scalar_tower section restrict_scalars /- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then `S`-modules are also `R`-modules. -/ section type_synonym variables (R A M : Type*) /-- Warning: use this type synonym judiciously! The preferred way of working with an `A`-module `M` as `R`-module (where `A` is an `R`-algebra), is by `[module R M] [module A M] [is_scalar_tower R A M]`. When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`, provided as a type synonym `module.restrict_scalars R A M := M`. -/ @[nolint unused_arguments] def restrict_scalars (R A M : Type*) : Type* := M instance [I : inhabited M] : inhabited (restrict_scalars R A M) := I instance [I : add_comm_monoid M] : add_comm_monoid (restrict_scalars R A M) := I instance [I : add_comm_group M] : add_comm_group (restrict_scalars R A M) := I instance restrict_scalars.module_orig [semiring A] [add_comm_monoid M] [I : module A M] : module A (restrict_scalars R A M) := I variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module A M] /-- When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`. The preferred way of setting this up is `[module R M] [module A M] [is_scalar_tower R A M]`. -/ instance : module R (restrict_scalars R A M) := module.comp_hom M (algebra_map R A) lemma restrict_scalars_smul_def (c : R) (x : restrict_scalars R A M) : c • x = ((algebra_map R A c) • x : M) := rfl instance : is_scalar_tower R A (restrict_scalars R A M) := ⟨λ r A M, by { rw [algebra.smul_def, mul_smul], refl }⟩ instance submodule.restricted_module (V : submodule A M) : module R V := restrict_scalars.module R A V instance submodule.restricted_module_is_scalar_tower (V : submodule A M) : is_scalar_tower R A V := restrict_scalars.is_scalar_tower R A V end type_synonym /-! TODO: The following lemmas no longer involve `algebra` at all, and could be moved closer to `algebra/module/submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥` are all defined in `linear_algebra/basic.lean`. -/ section module open module variables (R S M N : Type*) [semiring R] [semiring S] [has_scalar R S] variables [add_comm_monoid M] [module R M] [module S M] [is_scalar_tower R S M] variables [add_comm_monoid N] [module R N] [module S N] [is_scalar_tower R S N] variables {S M N} namespace submodule /-- `V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars, corresponding to `V`, an `S`-submodule of the original `S`-module. -/ @[simps] def restrict_scalars (V : submodule S M) : submodule R M := { carrier := V.carrier, zero_mem' := V.zero_mem, smul_mem' := λ c m h, V.smul_of_tower_mem c h, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma restrict_scalars_mem (V : submodule S M) (m : M) : m ∈ V.restrict_scalars R ↔ m ∈ V := iff.refl _ variables (R S M) lemma restrict_scalars_injective : function.injective (restrict_scalars R : submodule S M → submodule R M) := λ V₁ V₂ h, ext $ by convert set.ext_iff.1 (set_like.ext'_iff.1 h); refl @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule S M} : restrict_scalars R V₁ = restrict_scalars R V₂ ↔ V₁ = V₂ := (restrict_scalars_injective R _ _).eq_iff @[simp] lemma restrict_scalars_bot : restrict_scalars R (⊥ : submodule S M) = ⊥ := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊤ : submodule S M) = ⊤ := rfl /-- If `S` is an `R`-algebra, then the `R`-module generated by a set `X` is included in the `S`-module generated by `X`. -/ lemma span_le_restrict_scalars (X : set M) : span R (X : set M) ≤ restrict_scalars R (span S X) := submodule.span_le.mpr submodule.subset_span end submodule @[simp] lemma linear_map.ker_restrict_scalars (f : M →ₗ[S] N) : (f.restrict_scalars R).ker = f.ker.restrict_scalars R := rfl end module end restrict_scalars namespace submodule variables (R A M : Type*) variables [comm_semiring R] [semiring A] [algebra R A] [add_comm_monoid M] variables [module R M] [module A M] [is_scalar_tower R A M] /-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the `R`-module generated by a set `X` equals the `A`-module generated by `X`. -/ lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) : span R X = restrict_scalars R (span A X) := begin apply (span_le_restrict_scalars R A M X).antisymm (λ m hm, _), refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem _) (λ a m hm, _), obtain ⟨r, rfl⟩ := hsur a, simpa [algebra_map_smul] using smul_mem _ r hm end end submodule
f42016dae7388c36be5cdf58a44013fd7a8825b3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/converter/binders.lean
7b8061f7609b7911c6b377ff3c3cd37d63ada6ca
[]
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
1,797
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 Binder elimination -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.default import Mathlib.PostPort universes u v namespace Mathlib namespace old_conv /- congr should forward data! -/ end old_conv /- Binder elimination: We assume a binder `B : p → Π (α : Sort u), (α → t) → t`, where `t` is a type depending on `p`. Examples: ∃: there is no `p` and `t` is `Prop`. ⨅, ⨆: here p is `β` and `[complete_lattice β]`, `p` is `β` Problem: ∀x, _ should be a binder, but is not a constant! Provide a mechanism to rewrite: B (x : α) ..x.. (h : x = t), p x = B ..x/t.., p t Here ..x.. are binders, maybe also some constants which provide commutativity rules with `B`. -/ theorem exists_elim_eq_left {α : Sort u} (a : α) (p : (a' : α) → a' = a → Prop) : (∃ (a' : α), ∃ (h : a' = a), p a' h) ↔ p a rfl := sorry theorem exists_elim_eq_right {α : Sort u} (a : α) (p : (a' : α) → a = a' → Prop) : (∃ (a' : α), ∃ (h : a = a'), p a' h) ↔ p a rfl := sorry theorem forall_comm {α : Sort u} {β : Sort v} (p : α → β → Prop) : (∀ (a : α) (b : β), p a b) ↔ ∀ (b : β) (a : α), p a b := { mp := fun (h : ∀ (a : α) (b : β), p a b) (b : β) (a : α) => h a b, mpr := fun (h : ∀ (b : β) (a : α), p a b) (b : α) (a : β) => h a b } theorem forall_elim_eq_left {α : Sort u} (a : α) (p : (a' : α) → a' = a → Prop) : (∀ (a' : α) (h : a' = a), p a' h) ↔ p a rfl := sorry theorem forall_elim_eq_right {α : Sort u} (a : α) (p : (a' : α) → a = a' → Prop) : (∀ (a' : α) (h : a = a'), p a' h) ↔ p a rfl := sorry
50a820d2c70025f6f4bf8908f7a18000c4fc8143
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/affine_space/midpoint.lean
6754a232b6f861e4841a2aac8c11f460600a704d
[ "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
9,469
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.char_p.invertible import linear_algebra.affine_space.affine_equiv /-! # Midpoint of a segment ## Main definitions * `midpoint R x y`: midpoint of the segment `[x, y]`. We define it for `x` and `y` in a module over a ring `R` with invertible `2`. * `add_monoid_hom.of_map_midpoint`: construct an `add_monoid_hom` given a map `f` such that `f` sends zero to zero and midpoints to midpoints. ## Main theorems * `midpoint_eq_iff`: `z` is the midpoint of `[x, y]` if and only if `x + y = z + z`, * `midpoint_unique`: `midpoint R x y` does not depend on `R`; * `midpoint x y` is linear both in `x` and `y`; * `point_reflection_midpoint_left`, `point_reflection_midpoint_right`: `equiv.point_reflection (midpoint R x y)` swaps `x` and `y`. We do not mark most lemmas as `@[simp]` because it is hard to tell which side is simpler. ## Tags midpoint, add_monoid_hom -/ open affine_map affine_equiv section variables (R : Type*) {V V' P P' : Type*} [ring R] [invertible (2:R)] [add_comm_group V] [module R V] [add_torsor V P] [add_comm_group V'] [module R V'] [add_torsor V' P'] include V /-- `midpoint x y` is the midpoint of the segment `[x, y]`. -/ def midpoint (x y : P) : P := line_map x y (⅟2:R) variables {R} {x y z : P} include V' @[simp] lemma affine_map.map_midpoint (f : P →ᵃ[R] P') (a b : P) : f (midpoint R a b) = midpoint R (f a) (f b) := f.apply_line_map a b _ @[simp] lemma affine_equiv.map_midpoint (f : P ≃ᵃ[R] P') (a b : P) : f (midpoint R a b) = midpoint R (f a) (f b) := f.apply_line_map a b _ omit V' @[simp] lemma affine_equiv.point_reflection_midpoint_left (x y : P) : point_reflection R (midpoint R x y) x = y := by rw [midpoint, point_reflection_apply, line_map_apply, vadd_vsub, vadd_vadd, ← add_smul, ← two_mul, mul_inv_of_self, one_smul, vsub_vadd] lemma midpoint_comm (x y : P) : midpoint R x y = midpoint R y x := by rw [midpoint, ← line_map_apply_one_sub, one_sub_inv_of_two, midpoint] @[simp] lemma affine_equiv.point_reflection_midpoint_right (x y : P) : point_reflection R (midpoint R x y) y = x := by rw [midpoint_comm, affine_equiv.point_reflection_midpoint_left] lemma midpoint_vsub_midpoint (p₁ p₂ p₃ p₄ : P) : midpoint R p₁ p₂ -ᵥ midpoint R p₃ p₄ = midpoint R (p₁ -ᵥ p₃) (p₂ -ᵥ p₄) := line_map_vsub_line_map _ _ _ _ _ lemma midpoint_vadd_midpoint (v v' : V) (p p' : P) : midpoint R v v' +ᵥ midpoint R p p' = midpoint R (v +ᵥ p) (v' +ᵥ p') := line_map_vadd_line_map _ _ _ _ _ lemma midpoint_eq_iff {x y z : P} : midpoint R x y = z ↔ point_reflection R z x = y := eq_comm.trans ((injective_point_reflection_left_of_module R x).eq_iff' (affine_equiv.point_reflection_midpoint_left x y)).symm @[simp] lemma midpoint_vsub_left (p₁ p₂ : P) : midpoint R p₁ p₂ -ᵥ p₁ = (⅟2:R) • (p₂ -ᵥ p₁) := line_map_vsub_left _ _ _ @[simp] lemma midpoint_vsub_right (p₁ p₂ : P) : midpoint R p₁ p₂ -ᵥ p₂ = (⅟2:R) • (p₁ -ᵥ p₂) := by rw [midpoint_comm, midpoint_vsub_left] @[simp] lemma left_vsub_midpoint (p₁ p₂ : P) : p₁ -ᵥ midpoint R p₁ p₂ = (⅟2:R) • (p₁ -ᵥ p₂) := left_vsub_line_map _ _ _ @[simp] lemma right_vsub_midpoint (p₁ p₂ : P) : p₂ -ᵥ midpoint R p₁ p₂ = (⅟2:R) • (p₂ -ᵥ p₁) := by rw [midpoint_comm, left_vsub_midpoint] lemma midpoint_vsub (p₁ p₂ p : P) : midpoint R p₁ p₂ -ᵥ p = (⅟2:R) • (p₁ -ᵥ p) + (⅟2:R) • (p₂ -ᵥ p) := by rw [←vsub_sub_vsub_cancel_right p₁ p p₂, smul_sub, sub_eq_add_neg, ←smul_neg, neg_vsub_eq_vsub_rev, add_assoc, inv_of_two_smul_add_inv_of_two_smul, ←vadd_vsub_assoc, midpoint_comm, midpoint, line_map_apply] lemma vsub_midpoint (p₁ p₂ p : P) : p -ᵥ midpoint R p₁ p₂ = (⅟2:R) • (p -ᵥ p₁) + (⅟2:R) • (p -ᵥ p₂) := by rw [←neg_vsub_eq_vsub_rev, midpoint_vsub, neg_add, ←smul_neg, ←smul_neg, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] @[simp] lemma midpoint_sub_left (v₁ v₂ : V) : midpoint R v₁ v₂ - v₁ = (⅟2:R) • (v₂ - v₁) := midpoint_vsub_left v₁ v₂ @[simp] lemma midpoint_sub_right (v₁ v₂ : V) : midpoint R v₁ v₂ - v₂ = (⅟2:R) • (v₁ - v₂) := midpoint_vsub_right v₁ v₂ @[simp] lemma left_sub_midpoint (v₁ v₂ : V) : v₁ - midpoint R v₁ v₂ = (⅟2:R) • (v₁ - v₂) := left_vsub_midpoint v₁ v₂ @[simp] lemma right_sub_midpoint (v₁ v₂ : V) : v₂ - midpoint R v₁ v₂ = (⅟2:R) • (v₂ - v₁) := right_vsub_midpoint v₁ v₂ variable (R) @[simp] lemma midpoint_eq_left_iff {x y : P} : midpoint R x y = x ↔ x = y := by rw [midpoint_eq_iff, point_reflection_self] @[simp] lemma left_eq_midpoint_iff {x y : P} : x = midpoint R x y ↔ x = y := by rw [eq_comm, midpoint_eq_left_iff] @[simp] lemma midpoint_eq_right_iff {x y : P} : midpoint R x y = y ↔ x = y := by rw [midpoint_comm, midpoint_eq_left_iff, eq_comm] @[simp] lemma right_eq_midpoint_iff {x y : P} : y = midpoint R x y ↔ x = y := by rw [eq_comm, midpoint_eq_right_iff] lemma midpoint_eq_midpoint_iff_vsub_eq_vsub {x x' y y' : P} : midpoint R x y = midpoint R x' y' ↔ x -ᵥ x' = y' -ᵥ y := by rw [← @vsub_eq_zero_iff_eq V, midpoint_vsub_midpoint, midpoint_eq_iff, point_reflection_apply, vsub_eq_sub, zero_sub, vadd_eq_add, add_zero, neg_eq_iff_neg_eq, neg_vsub_eq_vsub_rev, eq_comm] lemma midpoint_eq_iff' {x y z : P} : midpoint R x y = z ↔ equiv.point_reflection z x = y := midpoint_eq_iff /-- `midpoint` does not depend on the ring `R`. -/ lemma midpoint_unique (R' : Type*) [ring R'] [invertible (2:R')] [module R' V] (x y : P) : midpoint R x y = midpoint R' x y := (midpoint_eq_iff' R).2 $ (midpoint_eq_iff' R').1 rfl @[simp] lemma midpoint_self (x : P) : midpoint R x x = x := line_map_same_apply _ _ @[simp] lemma midpoint_add_self (x y : V) : midpoint R x y + midpoint R x y = x + y := calc midpoint R x y +ᵥ midpoint R x y = midpoint R x y +ᵥ midpoint R y x : by rw midpoint_comm ... = x + y : by rw [midpoint_vadd_midpoint, vadd_eq_add, vadd_eq_add, add_comm, midpoint_self] lemma midpoint_zero_add (x y : V) : midpoint R 0 (x + y) = midpoint R x y := (midpoint_eq_midpoint_iff_vsub_eq_vsub R).2 $ by simp [sub_add_eq_sub_sub_swap] lemma midpoint_eq_smul_add (x y : V) : midpoint R x y = (⅟2 : R) • (x + y) := by rw [midpoint_eq_iff, point_reflection_apply, vsub_eq_sub, vadd_eq_add, sub_add_eq_add_sub, ← two_smul R, smul_smul, mul_inv_of_self, one_smul, add_sub_cancel'] @[simp] lemma midpoint_self_neg (x : V) : midpoint R x (-x) = 0 := by rw [midpoint_eq_smul_add, add_neg_self, smul_zero] @[simp] lemma midpoint_neg_self (x : V) : midpoint R (-x) x = 0 := by simpa using midpoint_self_neg R (-x) @[simp] lemma midpoint_sub_add (x y : V) : midpoint R (x - y) (x + y) = x := by rw [sub_eq_add_neg, ← vadd_eq_add, ← vadd_eq_add, ← midpoint_vadd_midpoint]; simp @[simp] lemma midpoint_add_sub (x y : V) : midpoint R (x + y) (x - y) = x := by rw midpoint_comm; simp end lemma line_map_inv_two {R : Type*} {V P : Type*} [division_ring R] [char_zero R] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : line_map a b (2⁻¹:R) = midpoint R a b := rfl lemma line_map_one_half {R : Type*} {V P : Type*} [division_ring R] [char_zero R] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : line_map a b (1/2:R) = midpoint R a b := by rw [one_div, line_map_inv_two] lemma homothety_inv_of_two {R : Type*} {V P : Type*} [comm_ring R] [invertible (2:R)] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : homothety a (⅟2:R) b = midpoint R a b := rfl lemma homothety_inv_two {k : Type*} {V P : Type*} [field k] [char_zero k] [add_comm_group V] [module k V] [add_torsor V P] (a b : P) : homothety a (2⁻¹:k) b = midpoint k a b := rfl lemma homothety_one_half {k : Type*} {V P : Type*} [field k] [char_zero k] [add_comm_group V] [module k V] [add_torsor V P] (a b : P) : homothety a (1/2:k) b = midpoint k a b := by rw [one_div, homothety_inv_two] @[simp] lemma pi_midpoint_apply {k ι : Type*} {V : Π i : ι, Type*} {P : Π i : ι, Type*} [field k] [invertible (2:k)] [Π i, add_comm_group (V i)] [Π i, module k (V i)] [Π i, add_torsor (V i) (P i)] (f g : Π i, P i) (i : ι) : midpoint k f g i = midpoint k (f i) (g i) := rfl namespace add_monoid_hom variables (R R' : Type*) {E F : Type*} [ring R] [invertible (2:R)] [add_comm_group E] [module R E] [ring R'] [invertible (2:R')] [add_comm_group F] [module R' F] /-- A map `f : E → F` sending zero to zero and midpoints to midpoints is an `add_monoid_hom`. -/ def of_map_midpoint (f : E → F) (h0 : f 0 = 0) (hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) : E →+ F := { to_fun := f, map_zero' := h0, map_add' := λ x y, calc f (x + y) = f 0 + f (x + y) : by rw [h0, zero_add] ... = midpoint R' (f 0) (f (x + y)) + midpoint R' (f 0) (f (x + y)) : (midpoint_add_self _ _ _).symm ... = f (midpoint R x y) + f (midpoint R x y) : by rw [← hm, midpoint_zero_add] ... = f x + f y : by rw [hm, midpoint_add_self] } @[simp] lemma coe_of_map_midpoint (f : E → F) (h0 : f 0 = 0) (hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) : ⇑(of_map_midpoint R R' f h0 hm) = f := rfl end add_monoid_hom
01e1b35b619b74fd4010410b1e68265998e43617
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/order.lean
0e004906849123204b983770e3357c277356002f
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
2,831
lean
import algebra.order_functions universe variables u section parameters {α : Type u} parameters [partial_order α] parameters x y : α lemma indirect_eq_left_iff : x = y ↔ (∀ z, z ≤ x ↔ z ≤ y) := begin split ; intro h, { subst y, intro, refl }, apply @le_antisymm α, { rw ← h }, { rw h }, end lemma indirect_eq_right_iff : x = y ↔ (∀ z, x ≤ z ↔ y ≤ z) := begin split ; intro h, { subst y, intro, refl }, apply @le_antisymm α, { rw h }, { rw ← h }, end lemma indirect_le_left_iff : x ≤ y ↔ (∀ z, z ≤ x → z ≤ y) := begin split ; intro h, { intro z, apply flip le_trans h, }, { apply h, refl }, end lemma indirect_le_right_iff : x ≤ y ↔ (∀ z, y ≤ z → x ≤ z) := begin split ; intro h, { intro z, apply le_trans h, }, { apply h, refl }, end parameters {x y} lemma eq_of_indirect_left_iff (h : ∀ z, z ≤ x ↔ z ≤ y) : x = y := indirect_eq_left_iff.mpr h lemma eq_of_indirect_eq_right (h : ∀ z, x ≤ z ↔ y ≤ z) : x = y := indirect_eq_right_iff.mpr h lemma le_of_indirect_le_left (h : ∀ z, z ≤ x → z ≤ y) : x ≤ y := indirect_le_left_iff.mpr h lemma le_of_indirect_le_right (h : ∀ z, y ≤ z → x ≤ z) : x ≤ y := indirect_le_right_iff.mpr h parameter z : α lemma indirect_left_left_of_eq (h : x = y) : z ≤ x ↔ z ≤ y := indirect_eq_left_iff.mp h z lemma indirect_eq_right_of_eq (h : x = y) : x ≤ z ↔ y ≤ z := indirect_eq_right_iff.mp h z lemma indirect_le_left_of_le (h : x ≤ y) : z ≤ x → z ≤ y := indirect_le_left_iff.mp h z lemma indirect_le_right_of_le (h : x ≤ y) : y ≤ z → x ≤ z := indirect_le_right_iff.mp h z end section parameters {α : Type u} parameters x y z : α variable [decidable_linear_order α] lemma le_max_iff_le_or_le : x ≤ max y z ↔ x ≤ y ∨ x ≤ z := begin split ; intros H ; cases decidable.em (y ≤ z) with H' H' ; try { unfold max at H } ; try { unfold max }, { rw if_pos H' at H, right, apply H }, { rw if_neg H' at H, left, apply H }, { rw if_pos H', cases H with H H, transitivity y ; assumption, assumption }, { rw if_neg H', have H₂ := le_of_not_ge H', cases H with H H, assumption, transitivity z ; assumption } end parameters {x y} lemma le_max_of_le_left (h : x ≤ y) : x ≤ max y z := by simp [le_max_iff_le_or_le,h] lemma lt_max_of_lt_left (h : x < y) : x < max y z := begin simp only [lt_iff_not_ge,(≥),max_le_iff,decidable.not_and_iff_or_not] at ⊢ h, left, assumption, end lemma le_max_of_le_right (h : x ≤ z) : x ≤ max y z := by simp [le_max_iff_le_or_le,h] lemma lt_max_of_lt_right (h : x < z) : x < max y z := begin simp only [lt_iff_not_ge,(≥),max_le_iff,decidable.not_and_iff_or_not] at ⊢ h, right, assumption, end end
89ea652d362afb75726d96d72b6c429f33a4159b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/field_simp.lean
cbe7c11dc5a4e816166038d768b2e911ce8a0373
[ "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
1,141
lean
import algebra.ring.basic import tactic.field_simp import tactic.ring /-! ## `field_simp` tests. Check that `field_simp` works for units of a ring. -/ variables {R : Type*} [comm_ring R] (a b c d e f g : R) (u₁ u₂ : Rˣ) /-- Check that `divp_add_divp_same` takes priority over `divp_add_divp`. -/ example : a /ₚ u₁ + b /ₚ u₁ = (a + b) /ₚ u₁ := by field_simp /-- Check that `divp_sub_divp_same` takes priority over `divp_sub_divp`. -/ example : a /ₚ u₁ - b /ₚ u₁ = (a - b) /ₚ u₁ := by field_simp /-- Combining `eq_divp_iff_mul_eq` and `divp_eq_iff_mul_eq`. -/ example : a /ₚ u₁ = b /ₚ u₂ ↔ a * u₂ = b * u₁ := by field_simp /-- Making sure inverses of units are rewritten properly. -/ example : ↑u₁⁻¹ = 1 /ₚ u₁ := by field_simp /-- Checking arithmetic expressions. -/ example : (f - (e + c * -(a /ₚ u₁) * b + d) - g) = (f * u₁ - (e * u₁ + c * (-a) * b + d * u₁) - g * u₁) /ₚ u₁ := by field_simp /-- Division of units. -/ example : a /ₚ (u₁ / u₂) = a * u₂ /ₚ u₁ := by field_simp example : a /ₚ u₁ /ₚ u₂ = a /ₚ (u₂ * u₁) := by field_simp
a31f7a88eb7ead96f56003a2452bd47545d08dd0
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/monad.lean
1fbf1be534d37fab20cb067c0dd0b5e5c1263383
[ "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
1,058
lean
/- Copyright (c) Luke Nelson and Jared Roesch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson and Jared Roesch -/ prelude import init.applicative init.string init.trace universe variables u v structure [class] monad (M : Type u → Type v) extends functor M : Type (max u+1 v) := (ret : Π {A : Type u}, A → M A) (bind : Π {A B : Type u}, M A → (A → M B) → M B) attribute [inline] definition return {M : Type u → Type v} [monad M] {A : Type u} : A → M A := monad.ret M definition fapp {M : Type u → Type v} [monad M] {A B : Type u} (f : M (A → B)) (a : M A) : M B := do g ← f, b ← a, return (g b) attribute [inline, instance] definition monad_is_applicative (M : Type u → Type v) [monad M] : applicative M := applicative.mk (@fmap _ _) (@return _ _) (@fapp _ _) attribute [inline] definition monad.and_then {A B : Type u} {M : Type u → Type v} [monad M] (a : M A) (b : M B) : M B := do a, b infixl ` >>= `:2 := monad.bind infixl ` >> `:2 := monad.and_then
a39b81a1a6c45d340262343603836f2f4a5e4800
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/real/irrational.lean
6c67f6fbc36efb45a48203bcb745939e78e3304e
[ "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
17,728
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.sqrt import tactic.interval_cases import ring_theory.algebraic import data.rat.sqrt import ring_theory.int.basic /-! # 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 : ℚ → ℝ) lemma irrational_iff_ne_rational (x : ℝ) : irrational x ↔ ∀ a b : ℤ, x ≠ a / b := by simp only [irrational, rat.forall, cast_mk, not_exists, set.mem_range, cast_coe_int, cast_div, eq_comm] /-- A transcendental real number is irrational. -/ lemma transcendental.irrational {r : ℝ} (tr : transcendental ℚ r) : irrational r := by { rintro ⟨a, rfl⟩, exact tr (is_algebraic_algebra_map a) } /-! ### 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, obtain rfl : D = 1 := by rw [← nat.gcd_eq_right hdivn, C.gcd_eq_one], 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.1.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.1.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.1) (finite_int_iff.2 ⟨hp.1.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.1.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 (sq_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 /-- **Irrationality of the Square Root of 2** -/ 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, rat.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) /-! ### Dot-style operations on `irrational` #### Coercion of a rational/integer/natural number is not irrational -/ namespace irrational variable {x : ℝ} /-! #### Irrational number is not equal to a rational/integer/natural number -/ theorem ne_rat (h : irrational x) (q : ℚ) : x ≠ q := λ hq, h ⟨q, hq.symm⟩ theorem ne_int (h : irrational x) (m : ℤ) : x ≠ m := by { rw ← rat.cast_coe_int, exact h.ne_rat _ } theorem ne_nat (h : irrational x) (m : ℕ) : x ≠ m := h.ne_int m theorem ne_zero (h : irrational x) : x ≠ 0 := by exact_mod_cast h.ne_nat 0 theorem ne_one (h : irrational x) : x ≠ 1 := by simpa only [nat.cast_one] using h.ne_nat 1 end irrational @[simp] lemma rat.not_irrational (q : ℚ) : ¬irrational q := λ h, h ⟨q, rfl⟩ @[simp] lemma int.not_irrational (m : ℤ) : ¬irrational m := λ h, h.ne_int m rfl @[simp] lemma nat.not_irrational (m : ℕ) : ¬irrational m := λ h, h.ne_nat m rfl namespace irrational variables (q : ℚ) {x y : ℝ} /-! #### Addition of rational/integer/natural numbers -/ /-- If `x + y` is irrational, then at least one of `x` and `y` is irrational. -/ 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.resolve_left q.not_irrational 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_int_add (m : ℤ) (h : irrational (m + x)) : irrational x := by { rw ← cast_coe_int at h, exact h.of_rat_add m } theorem of_add_int (m : ℤ) (h : irrational (x + m)) : irrational x := of_int_add m $ add_comm x m ▸ h theorem int_add (h : irrational x) (m : ℤ) : irrational (m + x) := by { rw ← cast_coe_int, exact h.rat_add m } theorem add_int (h : irrational x) (m : ℤ) : irrational (x + m) := add_comm ↑m x ▸ h.int_add m theorem of_nat_add (m : ℕ) (h : irrational (m + x)) : irrational x := h.of_int_add m theorem of_add_nat (m : ℕ) (h : irrational (x + m)) : irrational x := h.of_add_int m theorem nat_add (h : irrational x) (m : ℕ) : irrational (m + x) := h.int_add m theorem add_nat (h : irrational x) (m : ℕ) : irrational (x + m) := h.add_int m /-! #### Negation -/ 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 /-! #### Subtraction of rational/integer/natural numbers -/ theorem sub_rat (h : irrational x) : irrational (x - q) := by simpa only [sub_eq_add_neg, cast_neg] using h.add_rat (-q) theorem rat_sub (h : irrational x) : irrational (q - x) := by simpa only [sub_eq_add_neg] using h.neg.rat_add q theorem of_sub_rat (h : irrational (x - q)) : irrational x := (of_add_rat (-q) $ by simpa only [cast_neg, sub_eq_add_neg] using h) theorem of_rat_sub (h : irrational (q - x)) : irrational x := of_neg (of_rat_add q (by simpa only [sub_eq_add_neg] using h)) theorem sub_int (h : irrational x) (m : ℤ) : irrational (x - m) := by simpa only [rat.cast_coe_int] using h.sub_rat m theorem int_sub (h : irrational x) (m : ℤ) : irrational (m - x) := by simpa only [rat.cast_coe_int] using h.rat_sub m theorem of_sub_int (m : ℤ) (h : irrational (x - m)) : irrational x := of_sub_rat m $ by rwa rat.cast_coe_int theorem of_int_sub (m : ℤ) (h : irrational (m - x)) : irrational x := of_rat_sub m $ by rwa rat.cast_coe_int theorem sub_nat (h : irrational x) (m : ℕ) : irrational (x - m) := h.sub_int m theorem nat_sub (h : irrational x) (m : ℕ) : irrational (m - x) := h.int_sub m theorem of_sub_nat (m : ℕ) (h : irrational (x - m)) : irrational x := h.of_sub_int m theorem of_nat_sub (m : ℕ) (h : irrational (m - x)) : irrational x := h.of_int_sub m /-! #### Multiplication by rational numbers -/ 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.resolve_right 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_int (m : ℤ) (h : irrational (x * m)) : irrational x := of_mul_rat m $ by rwa cast_coe_int theorem of_int_mul (m : ℤ) (h : irrational (m * x)) : irrational x := of_rat_mul m $ by rwa cast_coe_int theorem mul_int (h : irrational x) {m : ℤ} (hm : m ≠ 0) : irrational (x * m) := by { rw ← cast_coe_int, refine h.mul_rat _, rwa int.cast_ne_zero } theorem int_mul (h : irrational x) {m : ℤ} (hm : m ≠ 0) : irrational (m * x) := mul_comm x m ▸ h.mul_int hm theorem of_mul_nat (m : ℕ) (h : irrational (x * m)) : irrational x := h.of_mul_int m theorem of_nat_mul (m : ℕ) (h : irrational (m * x)) : irrational x := h.of_int_mul m theorem mul_nat (h : irrational x) {m : ℕ} (hm : m ≠ 0) : irrational (x * m) := h.mul_int $ int.coe_nat_ne_zero.2 hm theorem nat_mul (h : irrational x) {m : ℕ} (hm : m ≠ 0) : irrational (m * x) := h.int_mul $ int.coe_nat_ne_zero.2 hm /-! #### Inverse -/ 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 /-! #### Division -/ 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_div_rat (h : irrational (x / q)) : irrational x := h.div_cases.resolve_right q.not_irrational theorem rat_div (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (q / x) := h.inv.rat_mul hq theorem div_rat (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (x / q) := by { rw [div_eq_mul_inv, ← cast_inv], exact h.mul_rat (inv_ne_zero hq) } theorem of_int_div (m : ℤ) (h : irrational (m / x)) : irrational x := h.div_cases.resolve_left m.not_irrational theorem of_div_int (m : ℤ) (h : irrational (x / m)) : irrational x := h.div_cases.resolve_right m.not_irrational theorem int_div (h : irrational x) {m : ℤ} (hm : m ≠ 0) : irrational (m / x) := h.inv.int_mul hm theorem div_int (h : irrational x) {m : ℤ} (hm : m ≠ 0) : irrational (x / m) := by { rw ← cast_coe_int, refine h.div_rat _, rwa int.cast_ne_zero } theorem of_nat_div (m : ℕ) (h : irrational (m / x)) : irrational x := h.of_int_div m theorem of_div_nat (m : ℕ) (h : irrational (x / m)) : irrational x := h.of_div_int m theorem nat_div (h : irrational x) {m : ℕ} (hm : m ≠ 0) : irrational (m / x) := h.inv.nat_mul hm theorem div_nat (h : irrational x) {m : ℕ} (hm : m ≠ 0) : irrational (x / m) := h.div_int $ by rwa int.coe_nat_ne_zero theorem of_one_div (h : irrational (1 / x)) : irrational x := of_rat_div 1 $ by rwa [cast_one] /-! #### Natural and integerl power -/ theorem of_mul_self (h : irrational (x * x)) : irrational x := h.mul_cases.elim id id theorem of_pow : ∀ n : ℕ, irrational (x^n) → irrational x | 0 := λ h, by { rw pow_zero at h, exact (h ⟨1, cast_one⟩).elim } | (n+1) := λ h, by { rw pow_succ at h, exact h.mul_cases.elim id (of_pow n) } theorem of_zpow : ∀ m : ℤ, irrational (x^m) → irrational x | (n:ℕ) := of_pow n | -[1+n] := λ h, by { rw zpow_neg_succ_of_nat at h, exact h.of_inv.of_pow _ } end irrational section polynomial open polynomial open_locale polynomial variables (x : ℝ) (p : ℤ[X]) lemma one_lt_nat_degree_of_irrational_root (hx : irrational x) (p_nonzero : p ≠ 0) (x_is_root : aeval x p = 0) : 1 < p.nat_degree := begin by_contra rid, rcases exists_eq_X_add_C_of_nat_degree_le_one (not_lt.1 rid) with ⟨a, b, rfl⟩, clear rid, have : (a : ℝ) * x = -b, by simpa [eq_neg_iff_add_eq_zero] using x_is_root, rcases em (a = 0) with (rfl|ha), { obtain rfl : b = 0, by simpa, simpa using p_nonzero }, { rw [mul_comm, ← eq_div_iff_mul_eq, eq_comm] at this, refine hx ⟨-b / a, _⟩, assumption_mod_cast, assumption_mod_cast } end end polynomial section variables {q : ℚ} {m : ℤ} {n : ℕ} {x : ℝ} open irrational /-! ### Simplification lemmas about operations -/ @[simp] theorem irrational_rat_add_iff : irrational (q + x) ↔ irrational x := ⟨of_rat_add q, rat_add q⟩ @[simp] theorem irrational_int_add_iff : irrational (m + x) ↔ irrational x := ⟨of_int_add m, λ h, h.int_add m⟩ @[simp] theorem irrational_nat_add_iff : irrational (n + x) ↔ irrational x := ⟨of_nat_add n, λ h, h.nat_add n⟩ @[simp] theorem irrational_add_rat_iff : irrational (x + q) ↔ irrational x := ⟨of_add_rat q, add_rat q⟩ @[simp] theorem irrational_add_int_iff : irrational (x + m) ↔ irrational x := ⟨of_add_int m, λ h, h.add_int m⟩ @[simp] theorem irrational_add_nat_iff : irrational (x + n) ↔ irrational x := ⟨of_add_nat n, λ h, h.add_nat n⟩ @[simp] theorem irrational_rat_sub_iff : irrational (q - x) ↔ irrational x := ⟨of_rat_sub q, rat_sub q⟩ @[simp] theorem irrational_int_sub_iff : irrational (m - x) ↔ irrational x := ⟨of_int_sub m, λ h, h.int_sub m⟩ @[simp] theorem irrational_nat_sub_iff : irrational (n - x) ↔ irrational x := ⟨of_nat_sub n, λ h, h.nat_sub n⟩ @[simp] theorem irrational_sub_rat_iff : irrational (x - q) ↔ irrational x := ⟨of_sub_rat q, sub_rat q⟩ @[simp] theorem irrational_sub_int_iff : irrational (x - m) ↔ irrational x := ⟨of_sub_int m, λ h, h.sub_int m⟩ @[simp] theorem irrational_sub_nat_iff : irrational (x - n) ↔ irrational x := ⟨of_sub_nat n, λ h, h.sub_nat n⟩ @[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⟩ @[simp] theorem irrational_rat_mul_iff : irrational (q * x) ↔ q ≠ 0 ∧ irrational x := ⟨λ h, ⟨rat.cast_ne_zero.1 $ left_ne_zero_of_mul h.ne_zero, h.of_rat_mul q⟩, λ h, h.2.rat_mul h.1⟩ @[simp] theorem irrational_mul_rat_iff : irrational (x * q) ↔ q ≠ 0 ∧ irrational x := by rw [mul_comm, irrational_rat_mul_iff] @[simp] theorem irrational_int_mul_iff : irrational (m * x) ↔ m ≠ 0 ∧ irrational x := by rw [← cast_coe_int, irrational_rat_mul_iff, int.cast_ne_zero] @[simp] theorem irrational_mul_int_iff : irrational (x * m) ↔ m ≠ 0 ∧ irrational x := by rw [← cast_coe_int, irrational_mul_rat_iff, int.cast_ne_zero] @[simp] theorem irrational_nat_mul_iff : irrational (n * x) ↔ n ≠ 0 ∧ irrational x := by rw [← cast_coe_nat, irrational_rat_mul_iff, nat.cast_ne_zero] @[simp] theorem irrational_mul_nat_iff : irrational (x * n) ↔ n ≠ 0 ∧ irrational x := by rw [← cast_coe_nat, irrational_mul_rat_iff, nat.cast_ne_zero] @[simp] theorem irrational_rat_div_iff : irrational (q / x) ↔ q ≠ 0 ∧ irrational x := by simp [div_eq_mul_inv] @[simp] theorem irrational_div_rat_iff : irrational (x / q) ↔ q ≠ 0 ∧ irrational x := by rw [div_eq_mul_inv, ← cast_inv, irrational_mul_rat_iff, ne.def, inv_eq_zero] @[simp] theorem irrational_int_div_iff : irrational (m / x) ↔ m ≠ 0 ∧ irrational x := by simp [div_eq_mul_inv] @[simp] theorem irrational_div_int_iff : irrational (x / m) ↔ m ≠ 0 ∧ irrational x := by rw [← cast_coe_int, irrational_div_rat_iff, int.cast_ne_zero] @[simp] theorem irrational_nat_div_iff : irrational (n / x) ↔ n ≠ 0 ∧ irrational x := by simp [div_eq_mul_inv] @[simp] theorem irrational_div_nat_iff : irrational (x / n) ↔ n ≠ 0 ∧ irrational x := by rw [← cast_coe_nat, irrational_div_rat_iff, nat.cast_ne_zero] /-- There is an irrational number `r` between any two reals `x < r < y`. -/ theorem exists_irrational_btwn {x y : ℝ} (h : x < y) : ∃ r, irrational r ∧ x < r ∧ r < y := let ⟨q, ⟨hq1, hq2⟩⟩ := (exists_rat_btwn ((sub_lt_sub_iff_right (real.sqrt 2)).mpr h)) in ⟨q + real.sqrt 2, irrational_sqrt_two.rat_add _, sub_lt_iff_lt_add.mp hq1, lt_sub_iff_add_lt.mp hq2⟩ end
aee1ef6446d268dd09be114b3a2995ed9df0a11e
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/nilpotent.lean
bea2a8d771b38d789e6176c7fcfb16e072626c78
[ "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
38,035
lean
/- Copyright (c) 2021 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Ines Wright, Joachim Breitner -/ import group_theory.quotient_group import group_theory.solvable import group_theory.p_group import group_theory.sylow import data.nat.factorization.basic import tactic.tfae /-! # Nilpotent groups An API for nilpotent groups, that is, groups for which the upper central series reaches `⊤`. ## Main definitions Recall that if `H K : subgroup G` then `⁅H, K⁆ : subgroup G` is the subgroup of `G` generated by the commutators `hkh⁻¹k⁻¹`. Recall also Lean's conventions that `⊤` denotes the subgroup `G` of `G`, and `⊥` denotes the trivial subgroup `{1}`. * `upper_central_series G : ℕ → subgroup G` : the upper central series of a group `G`. This is an increasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊥` and `H (n + 1) / H n` is the centre of `G / H n`. * `lower_central_series G : ℕ → subgroup G` : the lower central series of a group `G`. This is a decreasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊤` and `H (n + 1) = ⁅H n, G⁆`. * `is_nilpotent` : A group G is nilpotent if its upper central series reaches `⊤`, or equivalently if its lower central series reaches `⊥`. * `nilpotency_class` : the length of the upper central series of a nilpotent group. * `is_ascending_central_series (H : ℕ → subgroup G) : Prop` and * `is_descending_central_series (H : ℕ → subgroup G) : Prop` : Note that in the literature a "central series" for a group is usually defined to be a *finite* sequence of normal subgroups `H 0`, `H 1`, ..., starting at `⊤`, finishing at `⊥`, and with each `H n / H (n + 1)` central in `G / H (n + 1)`. In this formalisation it is convenient to have two weaker predicates on an infinite sequence of subgroups `H n` of `G`: we say a sequence is a *descending central series* if it starts at `G` and `⁅H n, ⊤⁆ ⊆ H (n + 1)` for all `n`. Note that this series may not terminate at `⊥`, and the `H i` need not be normal. Similarly a sequence is an *ascending central series* if `H 0 = ⊥` and `⁅H (n + 1), ⊤⁆ ⊆ H n` for all `n`, again with no requirement that the series reaches `⊤` or that the `H i` are normal. ## Main theorems `G` is *defined* to be nilpotent if the upper central series reaches `⊤`. * `nilpotent_iff_finite_ascending_central_series` : `G` is nilpotent iff some ascending central series reaches `⊤`. * `nilpotent_iff_finite_descending_central_series` : `G` is nilpotent iff some descending central series reaches `⊥`. * `nilpotent_iff_lower` : `G` is nilpotent iff the lower central series reaches `⊥`. * The `nilpotency_class` can likeways be obtained from these equivalent definitions, see `least_ascending_central_series_length_eq_nilpotency_class`, `least_descending_central_series_length_eq_nilpotency_class` and `lower_central_series_length_eq_nilpotency_class`. * If `G` is nilpotent, then so are its subgroups, images, quotients and preimages. Binary and finite products of nilpotent groups are nilpotent. Infinite products are nilpotent if their nilpotent class is bounded. Corresponding lemmas about the `nilpotency_class` are provided. * The `nilpotency_class` of `G ⧸ center G` is given explicitly, and an induction principle is derived from that. * `is_nilpotent.to_is_solvable`: If `G` is nilpotent, it is solvable. ## Warning A "central series" is usually defined to be a finite sequence of normal subgroups going from `⊥` to `⊤` with the property that each subquotient is contained within the centre of the associated quotient of `G`. This means that if `G` is not nilpotent, then none of what we have called `upper_central_series G`, `lower_central_series G` or the sequences satisfying `is_ascending_central_series` or `is_descending_central_series` are actually central series. Note that the fact that the upper and lower central series are not central series if `G` is not nilpotent is a standard abuse of notation. -/ open subgroup section with_group variables {G : Type*} [group G] (H : subgroup G) [normal H] /-- If `H` is a normal subgroup of `G`, then the set `{x : G | ∀ y : G, x*y*x⁻¹*y⁻¹ ∈ H}` is a subgroup of `G` (because it is the preimage in `G` of the centre of the quotient group `G/H`.) -/ def upper_central_series_step : subgroup G := { carrier := {x : G | ∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ H}, one_mem' := λ y, by simp [subgroup.one_mem], mul_mem' := λ a b ha hb y, begin convert subgroup.mul_mem _ (ha (b * y * b⁻¹)) (hb y) using 1, group, end, inv_mem' := λ x hx y, begin specialize hx y⁻¹, rw [mul_assoc, inv_inv] at ⊢ hx, exact subgroup.normal.mem_comm infer_instance hx, end } lemma mem_upper_central_series_step (x : G) : x ∈ upper_central_series_step H ↔ ∀ y, x * y * x⁻¹ * y⁻¹ ∈ H := iff.rfl open quotient_group /-- The proof that `upper_central_series_step H` is the preimage of the centre of `G/H` under the canonical surjection. -/ lemma upper_central_series_step_eq_comap_center : upper_central_series_step H = subgroup.comap (mk' H) (center (G ⧸ H)) := begin ext, rw [mem_comap, mem_center_iff, forall_coe], apply forall_congr, intro y, rw [coe_mk', ←quotient_group.coe_mul, ←quotient_group.coe_mul, eq_comm, eq_iff_div_mem, div_eq_mul_inv, mul_inv_rev, mul_assoc], end instance : normal (upper_central_series_step H) := begin rw upper_central_series_step_eq_comap_center, apply_instance, end variable (G) /-- An auxiliary type-theoretic definition defining both the upper central series of a group, and a proof that it is normal, all in one go. -/ def upper_central_series_aux : ℕ → Σ' (H : subgroup G), normal H | 0 := ⟨⊥, infer_instance⟩ | (n + 1) := let un := upper_central_series_aux n, un_normal := un.2 in by exactI ⟨upper_central_series_step un.1, infer_instance⟩ /-- `upper_central_series G n` is the `n`th term in the upper central series of `G`. -/ def upper_central_series (n : ℕ) : subgroup G := (upper_central_series_aux G n).1 instance (n : ℕ) : normal (upper_central_series G n) := (upper_central_series_aux G n).2 @[simp] lemma upper_central_series_zero : upper_central_series G 0 = ⊥ := rfl @[simp] lemma upper_central_series_one : upper_central_series G 1 = center G := begin ext, simp only [upper_central_series, upper_central_series_aux, upper_central_series_step, center, set.center, mem_mk, mem_bot, set.mem_set_of_eq], exact forall_congr (λ y, by rw [mul_inv_eq_one, mul_inv_eq_iff_eq_mul, eq_comm]), end /-- The `n+1`st term of the upper central series `H i` has underlying set equal to the `x` such that `⁅x,G⁆ ⊆ H n`-/ lemma mem_upper_central_series_succ_iff (n : ℕ) (x : G) : x ∈ upper_central_series G (n + 1) ↔ ∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ upper_central_series G n := iff.rfl -- is_nilpotent is already defined in the root namespace (for elements of rings). /-- A group `G` is nilpotent if its upper central series is eventually `G`. -/ class group.is_nilpotent (G : Type*) [group G] : Prop := (nilpotent [] : ∃ n : ℕ, upper_central_series G n = ⊤) open group variable {G} /-- A sequence of subgroups of `G` is an ascending central series if `H 0` is trivial and `⁅H (n + 1), G⁆ ⊆ H n` for all `n`. Note that we do not require that `H n = G` for some `n`. -/ def is_ascending_central_series (H : ℕ → subgroup G) : Prop := H 0 = ⊥ ∧ ∀ (x : G) (n : ℕ), x ∈ H (n + 1) → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H n /-- A sequence of subgroups of `G` is a descending central series if `H 0` is `G` and `⁅H n, G⁆ ⊆ H (n + 1)` for all `n`. Note that we do not requre that `H n = {1}` for some `n`. -/ def is_descending_central_series (H : ℕ → subgroup G) := H 0 = ⊤ ∧ ∀ (x : G) (n : ℕ), x ∈ H n → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H (n + 1) /-- Any ascending central series for a group is bounded above by the upper central series. -/ lemma ascending_central_series_le_upper (H : ℕ → subgroup G) (hH : is_ascending_central_series H) : ∀ n : ℕ, H n ≤ upper_central_series G n | 0 := hH.1.symm ▸ le_refl ⊥ | (n + 1) := begin intros x hx, rw mem_upper_central_series_succ_iff, exact λ y, ascending_central_series_le_upper n (hH.2 x n hx y), end variable (G) /-- The upper central series of a group is an ascending central series. -/ lemma upper_central_series_is_ascending_central_series : is_ascending_central_series (upper_central_series G) := ⟨rfl, λ x n h, h⟩ lemma upper_central_series_mono : monotone (upper_central_series G) := begin refine monotone_nat_of_le_succ _, intros n x hx y, rw [mul_assoc, mul_assoc, ← mul_assoc y x⁻¹ y⁻¹], exact mul_mem hx (normal.conj_mem (upper_central_series.subgroup.normal G n) x⁻¹ (inv_mem hx) y) end /-- A group `G` is nilpotent iff there exists an ascending central series which reaches `G` in finitely many steps. -/ theorem nilpotent_iff_finite_ascending_central_series : is_nilpotent G ↔ ∃ n : ℕ, ∃ H : ℕ → subgroup G, is_ascending_central_series H ∧ H n = ⊤ := begin split, { rintro ⟨n, nH⟩, refine ⟨_, _, upper_central_series_is_ascending_central_series G, nH⟩ }, { rintro ⟨n, H, hH, hn⟩, use n, rw [eq_top_iff, ←hn], exact ascending_central_series_le_upper H hH n } end lemma is_decending_rev_series_of_is_ascending {H: ℕ → subgroup G} {n : ℕ} (hn : H n = ⊤) (hasc : is_ascending_central_series H) : is_descending_central_series (λ (m : ℕ), H (n - m)) := begin cases hasc with h0 hH, refine ⟨hn, λ x m hx g, _⟩, dsimp at hx, by_cases hm : n ≤ m, { rw [tsub_eq_zero_of_le hm, h0, subgroup.mem_bot] at hx, subst hx, convert subgroup.one_mem _, group }, { push_neg at hm, apply hH, convert hx, rw [tsub_add_eq_add_tsub (nat.succ_le_of_lt hm), nat.succ_sub_succ] }, end lemma is_ascending_rev_series_of_is_descending {H: ℕ → subgroup G} {n : ℕ} (hn : H n = ⊥) (hdesc : is_descending_central_series H) : is_ascending_central_series (λ (m : ℕ), H (n - m)) := begin cases hdesc with h0 hH, refine ⟨hn, λ x m hx g, _⟩, dsimp only at hx ⊢, by_cases hm : n ≤ m, { have hnm : n - m = 0 := tsub_eq_zero_iff_le.mpr hm, rw [hnm, h0], exact mem_top _ }, { push_neg at hm, convert hH x _ hx g, rw [tsub_add_eq_add_tsub (nat.succ_le_of_lt hm), nat.succ_sub_succ] }, end /-- A group `G` is nilpotent iff there exists a descending central series which reaches the trivial group in a finite time. -/ theorem nilpotent_iff_finite_descending_central_series : is_nilpotent G ↔ ∃ n : ℕ, ∃ H : ℕ → subgroup G, is_descending_central_series H ∧ H n = ⊥ := begin rw nilpotent_iff_finite_ascending_central_series, split, { rintro ⟨n, H, hH, hn⟩, refine ⟨n, λ m, H (n - m), is_decending_rev_series_of_is_ascending G hn hH, _⟩, rw tsub_self, exact hH.1 }, { rintro ⟨n, H, hH, hn⟩, refine ⟨n, λ m, H (n - m), is_ascending_rev_series_of_is_descending G hn hH, _⟩, rw tsub_self, exact hH.1 }, end /-- The lower central series of a group `G` is a sequence `H n` of subgroups of `G`, defined by `H 0` is all of `G` and for `n≥1`, `H (n + 1) = ⁅H n, G⁆` -/ def lower_central_series (G : Type*) [group G] : ℕ → subgroup G | 0 := ⊤ | (n+1) := ⁅lower_central_series n, ⊤⁆ variable {G} @[simp] lemma lower_central_series_zero : lower_central_series G 0 = ⊤ := rfl @[simp] lemma lower_central_series_one : lower_central_series G 1 = commutator G := rfl lemma mem_lower_central_series_succ_iff (n : ℕ) (q : G) : q ∈ lower_central_series G (n + 1) ↔ q ∈ closure {x | ∃ (p ∈ lower_central_series G n) (q ∈ (⊤ : subgroup G)), p * q * p⁻¹ * q⁻¹ = x} := iff.rfl lemma lower_central_series_succ (n : ℕ) : lower_central_series G (n + 1) = closure {x | ∃ (p ∈ lower_central_series G n) (q ∈ (⊤ : subgroup G)), p * q * p⁻¹ * q⁻¹ = x} := rfl instance (n : ℕ) : normal (lower_central_series G n) := begin induction n with d hd, { exact (⊤ : subgroup G).normal_of_characteristic }, { exactI subgroup.commutator_normal (lower_central_series G d) ⊤ }, end lemma lower_central_series_antitone : antitone (lower_central_series G) := begin refine antitone_nat_of_succ_le (λ n x hx, _), simp only [mem_lower_central_series_succ_iff, exists_prop, mem_top, exists_true_left, true_and] at hx, refine closure_induction hx _ (subgroup.one_mem _) (@subgroup.mul_mem _ _ _) (@subgroup.inv_mem _ _ _), rintros y ⟨z, hz, a, ha⟩, rw [← ha, mul_assoc, mul_assoc, ← mul_assoc a z⁻¹ a⁻¹], exact mul_mem hz (normal.conj_mem (lower_central_series.subgroup.normal n) z⁻¹ (inv_mem hz) a) end /-- The lower central series of a group is a descending central series. -/ theorem lower_central_series_is_descending_central_series : is_descending_central_series (lower_central_series G) := begin split, refl, intros x n hxn g, exact commutator_mem_commutator hxn (mem_top g), end /-- Any descending central series for a group is bounded below by the lower central series. -/ lemma descending_central_series_ge_lower (H : ℕ → subgroup G) (hH : is_descending_central_series H) : ∀ n : ℕ, lower_central_series G n ≤ H n | 0 := hH.1.symm ▸ le_refl ⊤ | (n + 1) := commutator_le.mpr (λ x hx q _, hH.2 x n (descending_central_series_ge_lower n hx) q) /-- A group is nilpotent if and only if its lower central series eventually reaches the trivial subgroup. -/ theorem nilpotent_iff_lower_central_series : is_nilpotent G ↔ ∃ n, lower_central_series G n = ⊥ := begin rw nilpotent_iff_finite_descending_central_series, split, { rintro ⟨n, H, ⟨h0, hs⟩, hn⟩, use n, rw [eq_bot_iff, ←hn], exact descending_central_series_ge_lower H ⟨h0, hs⟩ n }, { rintro ⟨n, hn⟩, exact ⟨n, lower_central_series G, lower_central_series_is_descending_central_series, hn⟩ }, end section classical open_locale classical variables [hG : is_nilpotent G] include hG variable (G) /-- The nilpotency class of a nilpotent group is the smallest natural `n` such that the `n`'th term of the upper central series is `G`. -/ noncomputable def group.nilpotency_class : ℕ := nat.find (is_nilpotent.nilpotent G) variable {G} @[simp] lemma upper_central_series_nilpotency_class : upper_central_series G (group.nilpotency_class G) = ⊤ := nat.find_spec (is_nilpotent.nilpotent G) lemma upper_central_series_eq_top_iff_nilpotency_class_le {n : ℕ} : (upper_central_series G n = ⊤) ↔ (group.nilpotency_class G ≤ n) := begin split, { intro h, exact (nat.find_le h), }, { intro h, apply eq_top_iff.mpr, rw ← upper_central_series_nilpotency_class, exact (upper_central_series_mono _ h), } end /-- The nilpotency class of a nilpotent `G` is equal to the smallest `n` for which an ascending central series reaches `G` in its `n`'th term. -/ lemma least_ascending_central_series_length_eq_nilpotency_class : nat.find ((nilpotent_iff_finite_ascending_central_series G).mp hG) = group.nilpotency_class G := begin refine le_antisymm (nat.find_mono _) (nat.find_mono _), { intros n hn, exact ⟨upper_central_series G, upper_central_series_is_ascending_central_series G, hn ⟩, }, { rintros n ⟨H, ⟨hH, hn⟩⟩, rw [←top_le_iff, ←hn], exact ascending_central_series_le_upper H hH n, } end /-- The nilpotency class of a nilpotent `G` is equal to the smallest `n` for which the descending central series reaches `⊥` in its `n`'th term. -/ lemma least_descending_central_series_length_eq_nilpotency_class : nat.find ((nilpotent_iff_finite_descending_central_series G).mp hG) = group.nilpotency_class G := begin rw ← least_ascending_central_series_length_eq_nilpotency_class, refine le_antisymm (nat.find_mono _) (nat.find_mono _), { rintros n ⟨H, ⟨hH, hn⟩⟩, refine ⟨(λ m, H (n - m)), is_decending_rev_series_of_is_ascending G hn hH, _⟩, rw tsub_self, exact hH.1 }, { rintros n ⟨H, ⟨hH, hn⟩⟩, refine ⟨(λ m, H (n - m)), is_ascending_rev_series_of_is_descending G hn hH, _⟩, rw tsub_self, exact hH.1 }, end /-- The nilpotency class of a nilpotent `G` is equal to the length of the lower central series. -/ lemma lower_central_series_length_eq_nilpotency_class : nat.find (nilpotent_iff_lower_central_series.mp hG) = @group.nilpotency_class G _ _ := begin rw ← least_descending_central_series_length_eq_nilpotency_class, refine le_antisymm (nat.find_mono _) (nat.find_mono _), { rintros n ⟨H, ⟨hH, hn⟩⟩, rw [←le_bot_iff, ←hn], exact (descending_central_series_ge_lower H hH n), }, { rintros n h, exact ⟨lower_central_series G, ⟨lower_central_series_is_descending_central_series, h⟩⟩ }, end @[simp] lemma lower_central_series_nilpotency_class : lower_central_series G (group.nilpotency_class G) = ⊥ := begin rw ← lower_central_series_length_eq_nilpotency_class, exact (nat.find_spec (nilpotent_iff_lower_central_series.mp _)) end lemma lower_central_series_eq_bot_iff_nilpotency_class_le {n : ℕ} : (lower_central_series G n = ⊥) ↔ (group.nilpotency_class G ≤ n) := begin split, { intro h, rw ← lower_central_series_length_eq_nilpotency_class, exact (nat.find_le h), }, { intro h, apply eq_bot_iff.mpr, rw ← lower_central_series_nilpotency_class, exact (lower_central_series_antitone h), } end end classical lemma lower_central_series_map_subtype_le (H : subgroup G) (n : ℕ) : (lower_central_series H n).map H.subtype ≤ lower_central_series G n := begin induction n with d hd, { simp }, { rw [lower_central_series_succ, lower_central_series_succ, monoid_hom.map_closure], apply subgroup.closure_mono, rintros x1 ⟨x2, ⟨x3, hx3, x4, hx4, rfl⟩, rfl⟩, exact ⟨x3, (hd (mem_map.mpr ⟨x3, hx3, rfl⟩)), x4, by simp⟩ } end /-- A subgroup of a nilpotent group is nilpotent -/ instance subgroup.is_nilpotent (H : subgroup G) [hG : is_nilpotent G] : is_nilpotent H := begin rw nilpotent_iff_lower_central_series at *, rcases hG with ⟨n, hG⟩, use n, have := lower_central_series_map_subtype_le H n, simp only [hG, set_like.le_def, mem_map, forall_apply_eq_imp_iff₂, exists_imp_distrib] at this, exact eq_bot_iff.mpr (λ x hx, subtype.ext (this x hx)), end /-- A the nilpotency class of a subgroup is less or equal the the nilpotency class of the group -/ lemma subgroup.nilpotency_class_le (H : subgroup G) [hG : is_nilpotent G] : group.nilpotency_class H ≤ group.nilpotency_class G := begin repeat { rw ← lower_central_series_length_eq_nilpotency_class }, apply nat.find_mono, intros n hG, have := lower_central_series_map_subtype_le H n, simp only [hG, set_like.le_def, mem_map, forall_apply_eq_imp_iff₂, exists_imp_distrib] at this, exact eq_bot_iff.mpr (λ x hx, subtype.ext (this x hx)), end @[priority 100] instance is_nilpotent_of_subsingleton [subsingleton G] : is_nilpotent G := nilpotent_iff_lower_central_series.2 ⟨0, subsingleton.elim ⊤ ⊥⟩ lemma upper_central_series.map {H : Type*} [group H] {f : G →* H} (h : function.surjective f) (n : ℕ) : subgroup.map f (upper_central_series G n) ≤ upper_central_series H n := begin induction n with d hd, { simp }, { rintros _ ⟨x, hx : x ∈ upper_central_series G d.succ, rfl⟩ y', rcases h y' with ⟨y, rfl⟩, simpa using hd (mem_map_of_mem f (hx y)) } end lemma lower_central_series.map {H : Type*} [group H] (f : G →* H) (n : ℕ) : subgroup.map f (lower_central_series G n) ≤ lower_central_series H n := begin induction n with d hd, { simp [nat.nat_zero_eq_zero] }, { rintros a ⟨x, hx : x ∈ lower_central_series G d.succ, rfl⟩, refine closure_induction hx _ (by simp [f.map_one, subgroup.one_mem _]) (λ y z hy hz, by simp [monoid_hom.map_mul, subgroup.mul_mem _ hy hz]) (λ y hy, by simp [f.map_inv, subgroup.inv_mem _ hy]), rintros a ⟨y, hy, z, ⟨-, rfl⟩⟩, apply mem_closure.mpr, exact λ K hK, hK ⟨f y, hd (mem_map_of_mem f hy), by simp [commutator_element_def]⟩ } end lemma lower_central_series_succ_eq_bot {n : ℕ} (h : lower_central_series G n ≤ center G) : lower_central_series G (n + 1) = ⊥ := begin rw [lower_central_series_succ, closure_eq_bot_iff, set.subset_singleton_iff], rintro x ⟨y, hy1, z, ⟨⟩, rfl⟩, rw [mul_assoc, ←mul_inv_rev, mul_inv_eq_one, eq_comm], exact mem_center_iff.mp (h hy1) z, end /-- The preimage of a nilpotent group is nilpotent if the kernel of the homomorphism is contained in the center -/ lemma is_nilpotent_of_ker_le_center {H : Type*} [group H] (f : G →* H) (hf1 : f.ker ≤ center G) (hH : is_nilpotent H) : is_nilpotent G := begin rw nilpotent_iff_lower_central_series at *, rcases hH with ⟨n, hn⟩, use (n + 1), refine lower_central_series_succ_eq_bot (le_trans ((subgroup.map_eq_bot_iff _).mp _) hf1), exact eq_bot_iff.mpr (hn ▸ (lower_central_series.map f n)), end lemma nilpotency_class_le_of_ker_le_center {H : Type*} [group H] (f : G →* H) (hf1 : f.ker ≤ center G) (hH : is_nilpotent H) : @group.nilpotency_class G _ (is_nilpotent_of_ker_le_center f hf1 hH) ≤ group.nilpotency_class H + 1 := begin rw ← lower_central_series_length_eq_nilpotency_class, apply nat.find_min', refine lower_central_series_succ_eq_bot (le_trans ((subgroup.map_eq_bot_iff _).mp _) hf1), apply eq_bot_iff.mpr, apply (le_trans (lower_central_series.map f _)), simp only [lower_central_series_nilpotency_class, le_bot_iff], end /-- The range of a surjective homomorphism from a nilpotent group is nilpotent -/ lemma nilpotent_of_surjective {G' : Type*} [group G'] [h : is_nilpotent G] (f : G →* G') (hf : function.surjective f) : is_nilpotent G' := begin unfreezingI { rcases h with ⟨n, hn⟩ }, use n, apply eq_top_iff.mpr, calc ⊤ = f.range : symm (f.range_top_of_surjective hf) ... = subgroup.map f ⊤ : monoid_hom.range_eq_map _ ... = subgroup.map f (upper_central_series G n) : by rw hn ... ≤ upper_central_series G' n : upper_central_series.map hf n, end /-- The nilpotency class of the range of a surejctive homomorphism from a nilpotent group is less or equal the nilpotency class of the domain -/ lemma nilpotency_class_le_of_surjective {G' : Type*} [group G'] (f : G →* G') (hf : function.surjective f) [h : is_nilpotent G] : @group.nilpotency_class G' _ (nilpotent_of_surjective _ hf) ≤ group.nilpotency_class G := begin apply nat.find_mono, intros n hn, apply eq_top_iff.mpr, calc ⊤ = f.range : symm (f.range_top_of_surjective hf) ... = subgroup.map f ⊤ : monoid_hom.range_eq_map _ ... = subgroup.map f (upper_central_series G n) : by rw hn ... ≤ upper_central_series G' n : upper_central_series.map hf n, end /-- Nilpotency respects isomorphisms -/ lemma nilpotent_of_mul_equiv {G' : Type*} [group G'] [h : is_nilpotent G] (f : G ≃* G') : is_nilpotent G' := nilpotent_of_surjective f.to_monoid_hom (mul_equiv.surjective f) /-- A quotient of a nilpotent group is nilpotent -/ instance nilpotent_quotient_of_nilpotent (H : subgroup G) [H.normal] [h : is_nilpotent G] : is_nilpotent (G ⧸ H) := nilpotent_of_surjective _ (show function.surjective (quotient_group.mk' H), by tidy) /-- The nilpotency class of a quotient of `G` is less or equal the nilpotency class of `G` -/ lemma nilpotency_class_quotient_le (H : subgroup G) [H.normal] [h : is_nilpotent G] : group.nilpotency_class (G ⧸ H) ≤ group.nilpotency_class G := nilpotency_class_le_of_surjective _ _ -- This technical lemma helps with rewriting the subgroup, which occurs in indices private lemma comap_center_subst {H₁ H₂ : subgroup G} [normal H₁] [normal H₂] (h : H₁ = H₂) : comap (mk' H₁) (center (G ⧸ H₁)) = comap (mk' H₂) (center (G ⧸ H₂)) := by unfreezingI { subst h } lemma comap_upper_central_series_quotient_center (n : ℕ) : comap (mk' (center G)) (upper_central_series (G ⧸ center G) n) = upper_central_series G n.succ := begin induction n with n ih, { simp, }, { let Hn := upper_central_series (G ⧸ center G) n, calc comap (mk' (center G)) (upper_central_series_step Hn) = comap (mk' (center G)) (comap (mk' Hn) (center ((G ⧸ center G) ⧸ Hn))) : by rw upper_central_series_step_eq_comap_center ... = comap (mk' (comap (mk' (center G)) Hn)) (center (G ⧸ (comap (mk' (center G)) Hn))) : quotient_group.comap_comap_center ... = comap (mk' (upper_central_series G n.succ)) (center (G ⧸ upper_central_series G n.succ)) : comap_center_subst ih ... = upper_central_series_step (upper_central_series G n.succ) : symm (upper_central_series_step_eq_comap_center _), } end lemma nilpotency_class_zero_iff_subsingleton [is_nilpotent G] : group.nilpotency_class G = 0 ↔ subsingleton G := by simp [group.nilpotency_class, nat.find_eq_zero, subsingleton_iff_bot_eq_top] /-- Quotienting the `center G` reduces the nilpotency class by 1 -/ lemma nilpotency_class_quotient_center [hH : is_nilpotent G] : group.nilpotency_class (G ⧸ center G) = group.nilpotency_class G - 1 := begin generalize hn : group.nilpotency_class G = n, rcases n with rfl | n, { simp [nilpotency_class_zero_iff_subsingleton] at *, haveI := hn, apply_instance, }, { suffices : group.nilpotency_class (G ⧸ center G) = n, by simpa, apply le_antisymm, { apply upper_central_series_eq_top_iff_nilpotency_class_le.mp, apply (@comap_injective G _ _ _ (mk' (center G)) (surjective_quot_mk _)), rw [ comap_upper_central_series_quotient_center, comap_top, ← hn], exact upper_central_series_nilpotency_class, }, { apply le_of_add_le_add_right, calc n + 1 = n.succ : rfl ... = group.nilpotency_class G : symm hn ... ≤ group.nilpotency_class (G ⧸ center G) + 1 : nilpotency_class_le_of_ker_le_center _ (le_of_eq (ker_mk _)) _, } } end /-- The nilpotency class of a non-trivial group is one more than its quotient by the center -/ lemma nilpotency_class_eq_quotient_center_plus_one [hH : is_nilpotent G] [nontrivial G] : group.nilpotency_class G = group.nilpotency_class (G ⧸ center G) + 1 := begin rw nilpotency_class_quotient_center, rcases h : group.nilpotency_class G, { exfalso, rw nilpotency_class_zero_iff_subsingleton at h, resetI, apply (false_of_nontrivial_of_subsingleton G), }, { simp } end /-- If the quotient by `center G` is nilpotent, then so is G. -/ lemma of_quotient_center_nilpotent (h : is_nilpotent (G ⧸ center G)) : is_nilpotent G := begin obtain ⟨n, hn⟩ := h.nilpotent, use n.succ, simp [← comap_upper_central_series_quotient_center, hn], end /-- A custom induction principle for nilpotent groups. The base case is a trivial group (`subsingleton G`), and in the induction step, one can assume the hypothesis for the group quotiented by its center. -/ @[elab_as_eliminator] lemma nilpotent_center_quotient_ind {P : Π G [group G], by exactI ∀ [is_nilpotent G], Prop} (G : Type*) [group G] [is_nilpotent G] (hbase : ∀ G [group G] [subsingleton G], by exactI P G) (hstep : ∀ G [group G], by exactI ∀ [is_nilpotent G], by exactI ∀ (ih : P (G ⧸ center G)), P G) : P G := begin obtain ⟨n, h⟩ : ∃ n, group.nilpotency_class G = n := ⟨ _, rfl⟩, unfreezingI { induction n with n ih generalizing G }, { haveI := nilpotency_class_zero_iff_subsingleton.mp h, exact hbase _, }, { have hn : group.nilpotency_class (G ⧸ center G) = n := by simp [nilpotency_class_quotient_center, h], exact hstep _ (ih _ hn), }, end lemma derived_le_lower_central (n : ℕ) : derived_series G n ≤ lower_central_series G n := by { induction n with i ih, { simp }, { apply commutator_mono ih, simp } } /-- Abelian groups are nilpotent -/ @[priority 100] instance comm_group.is_nilpotent {G : Type*} [comm_group G] : is_nilpotent G := begin use 1, rw upper_central_series_one, apply comm_group.center_eq_top, end /-- Abelian groups have nilpotency class at most one -/ lemma comm_group.nilpotency_class_le_one {G : Type*} [comm_group G] : group.nilpotency_class G ≤ 1 := begin apply upper_central_series_eq_top_iff_nilpotency_class_le.mp, rw upper_central_series_one, apply comm_group.center_eq_top, end /-- Groups with nilpotency class at most one are abelian -/ def comm_group_of_nilpotency_class [is_nilpotent G] (h : group.nilpotency_class G ≤ 1) : comm_group G := group.comm_group_of_center_eq_top $ begin rw ← upper_central_series_one, exact upper_central_series_eq_top_iff_nilpotency_class_le.mpr h, end section prod variables {G₁ G₂ : Type*} [group G₁] [group G₂] lemma lower_central_series_prod (n : ℕ): lower_central_series (G₁ × G₂) n = (lower_central_series G₁ n).prod (lower_central_series G₂ n) := begin induction n with n ih, { simp, }, { calc lower_central_series (G₁ × G₂) n.succ = ⁅lower_central_series (G₁ × G₂) n, ⊤⁆ : rfl ... = ⁅(lower_central_series G₁ n).prod (lower_central_series G₂ n), ⊤⁆ : by rw ih ... = ⁅(lower_central_series G₁ n).prod (lower_central_series G₂ n), (⊤ : subgroup G₁).prod ⊤⁆ : by simp ... = ⁅lower_central_series G₁ n, (⊤ : subgroup G₁)⁆.prod ⁅lower_central_series G₂ n, ⊤⁆ : commutator_prod_prod _ _ _ _ ... = (lower_central_series G₁ n.succ).prod (lower_central_series G₂ n.succ) : rfl } end /-- Products of nilpotent groups are nilpotent -/ instance is_nilpotent_prod [is_nilpotent G₁] [is_nilpotent G₂] : is_nilpotent (G₁ × G₂) := begin rw nilpotent_iff_lower_central_series, refine ⟨max (group.nilpotency_class G₁) (group.nilpotency_class G₂), _ ⟩, rw [lower_central_series_prod, lower_central_series_eq_bot_iff_nilpotency_class_le.mpr (le_max_left _ _), lower_central_series_eq_bot_iff_nilpotency_class_le.mpr (le_max_right _ _), bot_prod_bot], end /-- The nilpotency class of a product is the max of the nilpotency classes of the factors -/ lemma nilpotency_class_prod [is_nilpotent G₁] [is_nilpotent G₂] : group.nilpotency_class (G₁ × G₂) = max (group.nilpotency_class G₁) (group.nilpotency_class G₂) := begin refine eq_of_forall_ge_iff (λ k, _), simp only [max_le_iff, ← lower_central_series_eq_bot_iff_nilpotency_class_le, lower_central_series_prod, prod_eq_bot_iff ], end end prod section bounded_pi -- First the case of infinite products with bounded nilpotency class variables {η : Type*} {Gs : η → Type*} [∀ i, group (Gs i)] lemma lower_central_series_pi_le (n : ℕ): lower_central_series (Π i, Gs i) n ≤ subgroup.pi set.univ (λ i, lower_central_series (Gs i) n) := begin let pi := λ (f : Π i, subgroup (Gs i)), subgroup.pi set.univ f, induction n with n ih, { simp [pi_top] }, { calc lower_central_series (Π i, Gs i) n.succ = ⁅lower_central_series (Π i, Gs i) n, ⊤⁆ : rfl ... ≤ ⁅pi (λ i, (lower_central_series (Gs i) n)), ⊤⁆ : commutator_mono ih (le_refl _) ... = ⁅pi (λ i, (lower_central_series (Gs i) n)), pi (λ i, ⊤)⁆ : by simp [pi, pi_top] ... ≤ pi (λ i, ⁅(lower_central_series (Gs i) n), ⊤⁆) : commutator_pi_pi_le _ _ ... = pi (λ i, lower_central_series (Gs i) n.succ) : rfl } end /-- products of nilpotent groups are nilpotent if their nipotency class is bounded -/ lemma is_nilpotent_pi_of_bounded_class [∀ i, is_nilpotent (Gs i)] (n : ℕ) (h : ∀ i, group.nilpotency_class (Gs i) ≤ n) : is_nilpotent (Π i, Gs i) := begin rw nilpotent_iff_lower_central_series, refine ⟨n, _⟩, rw eq_bot_iff, apply le_trans (lower_central_series_pi_le _), rw [← eq_bot_iff, pi_eq_bot_iff], intros i, apply lower_central_series_eq_bot_iff_nilpotency_class_le.mpr (h i), end end bounded_pi section finite_pi -- Now for finite products variables {η : Type*} {Gs : η → Type*} [∀ i, group (Gs i)] lemma lower_central_series_pi_of_finite [finite η] (n : ℕ) : lower_central_series (Π i, Gs i) n = subgroup.pi set.univ (λ i, lower_central_series (Gs i) n) := begin let pi := λ (f : Π i, subgroup (Gs i)), subgroup.pi set.univ f, induction n with n ih, { simp [pi_top] }, { calc lower_central_series (Π i, Gs i) n.succ = ⁅lower_central_series (Π i, Gs i) n, ⊤⁆ : rfl ... = ⁅pi (λ i, (lower_central_series (Gs i) n)), ⊤⁆ : by rw ih ... = ⁅pi (λ i, (lower_central_series (Gs i) n)), pi (λ i, ⊤)⁆ : by simp [pi, pi_top] ... = pi (λ i, ⁅(lower_central_series (Gs i) n), ⊤⁆) : commutator_pi_pi_of_finite _ _ ... = pi (λ i, lower_central_series (Gs i) n.succ) : rfl } end /-- n-ary products of nilpotent groups are nilpotent -/ instance is_nilpotent_pi [finite η] [∀ i, is_nilpotent (Gs i)] : is_nilpotent (Π i, Gs i) := begin casesI nonempty_fintype η, rw nilpotent_iff_lower_central_series, refine ⟨finset.univ.sup (λ i, group.nilpotency_class (Gs i)), _⟩, rw [lower_central_series_pi_of_finite, pi_eq_bot_iff], intros i, apply lower_central_series_eq_bot_iff_nilpotency_class_le.mpr, exact @finset.le_sup _ _ _ _ finset.univ (λ i, group.nilpotency_class (Gs i)) _ (finset.mem_univ i), end /-- The nilpotency class of an n-ary product is the sup of the nilpotency classes of the factors -/ lemma nilpotency_class_pi [fintype η] [∀ i, is_nilpotent (Gs i)] : group.nilpotency_class (Π i, Gs i) = finset.univ.sup (λ i, group.nilpotency_class (Gs i)) := begin apply eq_of_forall_ge_iff, intros k, simp only [finset.sup_le_iff, ← lower_central_series_eq_bot_iff_nilpotency_class_le, lower_central_series_pi_of_finite, pi_eq_bot_iff, finset.mem_univ, true_implies_iff ], end end finite_pi /-- A nilpotent subgroup is solvable -/ @[priority 100] instance is_nilpotent.to_is_solvable [h : is_nilpotent G]: is_solvable G := begin obtain ⟨n, hn⟩ := nilpotent_iff_lower_central_series.1 h, use n, rw [eq_bot_iff, ←hn], exact derived_le_lower_central n, end lemma normalizer_condition_of_is_nilpotent [h : is_nilpotent G] : normalizer_condition G := begin -- roughly based on https://groupprops.subwiki.org/wiki/Nilpotent_implies_normalizer_condition rw normalizer_condition_iff_only_full_group_self_normalizing, apply nilpotent_center_quotient_ind G; unfreezingI { clear_dependent G }, { introsI G _ _ H _, apply subsingleton.elim, }, { introsI G _ _ ih H hH, have hch : center G ≤ H := subgroup.center_le_normalizer.trans (le_of_eq hH), have hkh : (mk' (center G)).ker ≤ H, by simpa using hch, have hsur : function.surjective (mk' (center G)), by exact surjective_quot_mk _, let H' := H.map (mk' (center G)), have hH' : H'.normalizer = H', { apply comap_injective hsur, rw [comap_normalizer_eq_of_surjective _ hsur, comap_map_eq_self hkh], exact hH, }, apply map_injective_of_ker_le (mk' (center G)) hkh le_top, exact (ih H' hH').trans (symm (map_top_of_surjective _ hsur)), }, end end with_group section with_finite_group open group fintype variables {G : Type*} [hG : group G] include hG /-- A p-group is nilpotent -/ lemma is_p_group.is_nilpotent [finite G] {p : ℕ} [hp : fact (nat.prime p)] (h : is_p_group p G) : is_nilpotent G := begin casesI nonempty_fintype G, classical, unfreezingI { revert hG, induction val using fintype.induction_subsingleton_or_nontrivial with G hG hS G hG hN ih }, { apply_instance, }, { introI _, intro h, have hcq : fintype.card (G ⧸ center G) < fintype.card G, { rw card_eq_card_quotient_mul_card_subgroup (center G), apply lt_mul_of_one_lt_right, exact (fintype.card_pos_iff.mpr has_one.nonempty), exact ((subgroup.one_lt_card_iff_ne_bot _).mpr (ne_of_gt h.bot_lt_center)), }, have hnq : is_nilpotent (G ⧸ center G) := ih _ hcq (h.to_quotient (center G)), exact (of_quotient_center_nilpotent hnq), } end variables [fintype G] /-- If a finite group is the direct product of its Sylow groups, it is nilpotent -/ theorem is_nilpotent_of_product_of_sylow_group (e : (Π p : (fintype.card G).factorization.support, Π P : sylow p G, (↑P : subgroup G)) ≃* G) : is_nilpotent G := begin classical, let ps := (fintype.card G).factorization.support, haveI : ∀ (p : ps) (P : sylow p G), is_nilpotent (↑P : subgroup G), { intros p P, haveI : fact (nat.prime ↑p) := fact.mk (nat.prime_of_mem_factorization (finset.coe_mem p)), exact P.is_p_group'.is_nilpotent, }, exact nilpotent_of_mul_equiv e, end /-- A finite group is nilpotent iff the normalizer condition holds, and iff all maximal groups are normal and iff all sylow groups are normal and iff the group is the direct product of its sylow groups. -/ theorem is_nilpotent_of_finite_tfae : tfae [ is_nilpotent G, normalizer_condition G, ∀ (H : subgroup G), is_coatom H → H.normal, ∀ (p : ℕ) (hp : fact p.prime) (P : sylow p G), (↑P : subgroup G).normal, nonempty ((Π p : (card G).factorization.support, Π P : sylow p G, (↑P : subgroup G)) ≃* G) ] := begin tfae_have : 1 → 2, { exact @normalizer_condition_of_is_nilpotent _ _ }, tfae_have : 2 → 3, { exact λ h H, normalizer_condition.normal_of_coatom H h }, tfae_have : 3 → 4, { introsI h p _ P, exact sylow.normal_of_all_max_subgroups_normal h _ }, tfae_have : 4 → 5, { exact λ h, nonempty.intro (sylow.direct_product_of_normal h) }, tfae_have : 5 → 1, { rintros ⟨e⟩, exact is_nilpotent_of_product_of_sylow_group e }, tfae_finish, end end with_finite_group
d4ef536949ff5ca79f79273ddbf41b337e140fef
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/combinatorics/hall.lean
d60ed279b9662c9ebc0860b0eba60a8805cad166
[ "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
14,787
lean
/- Copyright (c) 2021 Alena Gusakov, Bhavik Mehta, Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alena Gusakov, Bhavik Mehta, Kyle Miller -/ import data.fintype.basic import data.rel import data.set.finite /-! # Hall's Marriage Theorem Given a list of finite subsets $X_1,X_2,\dots,X_n$ of some given set $S$, Hall in [Hall1935] gave a necessary and sufficient condition for there to be a list of distinct elements $x_1,x_2,\dots,x_n$ with $x_i\in X_i$ for each $i$: it is when for each $k$, the union of every $k$ of these subsets has at least $k$ elements. This file proves this for an indexed family `t : ι → finset α` of finite sets, with `[fintype ι]`, along with some variants of the statement. The list of distinct representatives is given by an injective function `f : ι → α` such that `∀ i, f i ∈ t i`. A description of this formalization is in [Gusakov2021]. ## Main statements * `finset.all_card_le_bUnion_card_iff_exists_injective` is in terms of `t : ι → finset α`. * `fintype.all_card_le_rel_image_card_iff_exists_injective` is in terms of a relation `r : α → β → Prop` such that `rel.image r {a}` is a finite set for all `a : α`. * `fintype.all_card_le_filter_rel_iff_exists_injective` is in terms of a relation `r : α → β → Prop` on finite types, with the Hall condition given in terms of `finset.univ.filter`. ## Todo * The theorem is still true even if `ι` is not a finite type. The infinite case follows from a compactness argument. * The statement of the theorem in terms of bipartite graphs is in preparation. ## Tags Hall's Marriage Theorem, indexed families -/ open finset universes u v namespace hall_marriage_theorem variables {ι : Type u} {α : Type v} [fintype ι] theorem hall_hard_inductive_zero (t : ι → finset α) (hn : fintype.card ι = 0) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin rw fintype.card_eq_zero_iff at hn, exactI ⟨is_empty_elim, is_empty_elim, is_empty_elim⟩, end variables {t : ι → finset α} [decidable_eq α] lemma hall_cond_of_erase {x : ι} (a : α) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) (s' : finset {x' : ι | x' ≠ x}) : s'.card ≤ (s'.bUnion (λ x', (t x').erase a)).card := begin haveI := classical.dec_eq ι, specialize ha (s'.image coe), rw [nonempty.image_iff, finset.card_image_of_injective s' subtype.coe_injective] at ha, by_cases he : s'.nonempty, { have ha' : s'.card < (s'.bUnion (λ x, t x)).card, { specialize ha he (λ h, by { have h' := mem_univ x, rw ←h at h', simpa using h' }), convert ha using 2, ext x, simp only [mem_image, mem_bUnion, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], }, rw ←erase_bUnion, by_cases hb : a ∈ s'.bUnion (λ x, t x), { rw card_erase_of_mem hb, exact nat.le_pred_of_lt ha' }, { rw erase_eq_of_not_mem hb, exact nat.le_of_lt ha' }, }, { rw [nonempty_iff_ne_empty, not_not] at he, subst s', simp }, end /-- First case of the inductive step: assuming that `∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card` and that the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_A {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI : nonempty ι := fintype.card_pos_iff.mp (hn.symm ▸ nat.succ_pos _), haveI := classical.dec_eq ι, /- Choose an arbitrary element `x : ι` and `y : t x`. -/ let x := classical.arbitrary ι, have tx_ne : (t x).nonempty, { rw ←finset.card_pos, apply nat.lt_of_lt_of_le nat.one_pos, convert ht {x}, rw finset.singleton_bUnion, }, rcases classical.indefinite_description _ tx_ne with ⟨y, hy⟩, /- Restrict to everything except `x` and `y`. -/ let ι' := {x' : ι | x' ≠ x}, let t' : ι' → finset α := λ x', (t x').erase y, have card_ι' : fintype.card ι' = n, { convert congr_arg (λ m, m - 1) hn, convert set.card_ne_eq _, }, rcases ih t' card_ι'.le (hall_cond_of_erase y ha) with ⟨f', hfinj, hfr⟩, /- Extend the resulting function. -/ refine ⟨λ z, if h : z = x then y else f' ⟨z, h⟩, _, _⟩, { rintro z₁ z₂, have key : ∀ {x}, y ≠ f' x, { intros x h, specialize hfr x, rw ←h at hfr, simpa using hfr, }, by_cases h₁ : z₁ = x; by_cases h₂ : z₂ = x; simp [h₁, h₂, hfinj.eq_iff, key, key.symm], }, { intro z, split_ifs with hz, { rwa hz }, { specialize hfr ⟨z, hz⟩, rw mem_erase at hfr, exact hfr.2, }, }, end lemma hall_cond_of_restrict {ι : Type u} {t : ι → finset α} {s : finset ι} (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (s : set ι)) : s'.card ≤ (s'.bUnion (λ a', t a')).card := begin haveI := classical.dec_eq ι, convert ht (s'.image coe) using 1, { rw card_image_of_injective _ subtype.coe_injective, }, { apply congr_arg, ext y, simp, }, end lemma hall_cond_of_compl {ι : Type u} {t : ι → finset α} {s : finset ι} (hus : s.card = (s.bUnion t).card) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (sᶜ : set ι)) : s'.card ≤ (s'.bUnion (λ x', t x' \ s.bUnion t)).card := begin haveI := classical.dec_eq ι, have : s'.card = (s ∪ s'.image coe).card - s.card, { rw [card_disjoint_union, nat.add_sub_cancel_left, card_image_of_injective _ subtype.coe_injective], simp only [disjoint_left, not_exists, mem_image, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], intros x hx hc h, exact (hc hx).elim }, rw [this, hus], apply (nat.sub_le_sub_right (ht _) _).trans _, rw ← card_sdiff, { have : (s ∪ s'.image subtype.val).bUnion t \ s.bUnion t ⊆ s'.bUnion (λ x', t x' \ s.bUnion t), { intros t, simp only [mem_bUnion, mem_sdiff, not_exists, mem_image, and_imp, mem_union, exists_and_distrib_right, exists_imp_distrib], rintro x (hx | ⟨x', hx', rfl⟩) rat hs, { exact (hs x hx rat).elim }, { exact ⟨⟨x', hx', rat⟩, hs⟩, } }, exact (card_le_of_subset this).trans le_rfl, }, { apply bUnion_subset_bUnion_of_subset_left, apply subset_union_left } end /-- Second case of the inductive step: assuming that `∃ (s : finset ι), s ≠ univ → s.card = (s.bUnion t).card` and that the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_B {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (s : finset ι) (hs : s.nonempty) (hns : s ≠ univ) (hus : s.card = (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI := classical.dec_eq ι, /- Restrict to `s` -/ let ι' := (s : set ι), let t' : ι' → finset α := λ x', t x', rw nat.add_one at hn, have card_ι'_le : fintype.card ι' ≤ n, { apply nat.le_of_lt_succ, rw ←hn, convert (card_lt_iff_ne_univ _).mpr hns, convert fintype.card_coe _ }, rcases ih t' card_ι'_le (hall_cond_of_restrict ht) with ⟨f', hf', hsf'⟩, /- Restrict to `sᶜ` in the domain and `(s.bUnion t)ᶜ` in the codomain. -/ let ι'' := (s : set ι)ᶜ, let t'' : ι'' → finset α := λ a'', t a'' \ s.bUnion t, have card_ι''_le : fintype.card ι'' ≤ n, { apply nat.le_of_lt_succ, rw ←hn, convert (card_compl_lt_iff_nonempty _).mpr hs, convert fintype.card_coe _, rw coe_compl, }, rcases ih t'' card_ι''_le (hall_cond_of_compl hus ht) with ⟨f'', hf'', hsf''⟩, /- Put them together -/ have f'_mem_bUnion : ∀ {x'} (hx' : x' ∈ s), f' ⟨x', hx'⟩ ∈ s.bUnion t, { intros x' hx', rw mem_bUnion, exact ⟨x', hx', hsf' _⟩, }, have f''_not_mem_bUnion : ∀ {x''} (hx'' : ¬ x'' ∈ s), ¬ f'' ⟨x'', hx''⟩ ∈ s.bUnion t, { intros x'' hx'', have h := hsf'' ⟨x'', hx''⟩, rw mem_sdiff at h, exact h.2, }, have im_disj : ∀ {x' x'' : ι} {hx' : x' ∈ s} {hx'' : ¬x'' ∈ s}, f' ⟨x', hx'⟩ ≠ f'' ⟨x'', hx''⟩, { intros _ _ hx' hx'' h, apply f''_not_mem_bUnion hx'', rw ←h, apply f'_mem_bUnion, }, refine ⟨λ x, if h : x ∈ s then f' ⟨x, h⟩ else f'' ⟨x, h⟩, _, _⟩, { exact hf'.dite _ hf'' @im_disj }, { intro x, split_ifs, { exact hsf' ⟨x, h⟩ }, { exact sdiff_subset _ _ (hsf'' ⟨x, h⟩) } } end /-- If `ι` has cardinality `n + 1` and the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι`. -/ theorem hall_hard_inductive_step {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin by_cases h : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card, { exact hall_hard_inductive_step_A hn ht @ih h, }, { push_neg at h, rcases h with ⟨s, sne, snu, sle⟩, have seq := nat.le_antisymm (ht _) sle, exact hall_hard_inductive_step_B hn ht @ih s sne snu seq, }, end /-- Here we combine the base case and the inductive step into a full strong induction proof, thus completing the proof of the second direction. -/ theorem hall_hard_inductive {n : ℕ} (hn : fintype.card ι = n) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin tactic.unfreeze_local_instances, revert ι, refine nat.strong_induction_on n (λ n' ih, _), intros _ _ t hn ht, rcases n' with (_|_), { exact hall_hard_inductive_zero t hn }, { apply hall_hard_inductive_step hn ht, introsI ι' _ _ hι', exact ih (fintype.card ι') (nat.lt_succ_of_le hι') rfl, }, end end hall_marriage_theorem /-- This the version of Hall's Marriage Theorem in terms of indexed families of finite sets `t : ι → finset α`. It states that there is a set of distinct representatives if and only if every union of `k` of the sets has at least `k` elements. Recall that `s.bUnion t` is the union of all the sets `t i` for `i ∈ s`. -/ theorem finset.all_card_le_bUnion_card_iff_exists_injective {ι α : Type*} [fintype ι] [decidable_eq α] (t : ι → finset α) : (∀ (s : finset ι), s.card ≤ (s.bUnion t).card) ↔ (∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x) := begin split, { exact hall_marriage_theorem.hall_hard_inductive rfl }, { rintro ⟨f, hf₁, hf₂⟩ s, rw ←card_image_of_injective s hf₁, apply card_le_of_subset, intro _, rw [mem_image, mem_bUnion], rintros ⟨x, hx, rfl⟩, exact ⟨x, hx, hf₂ x⟩, }, end /-- Given a relation such that the image of every singleton set is finite, then the image of every finite set is finite. -/ instance {α β : Type*} [decidable_eq β] (r : α → β → Prop) [∀ (a : α), fintype (rel.image r {a})] (A : finset α) : fintype (rel.image r A) := begin have h : rel.image r A = (A.bUnion (λ a, (rel.image r {a}).to_finset) : set β), { ext, simp [rel.image], }, rw [h], apply finset_coe.fintype, end /-- This is a version of Hall's Marriage Theorem in terms of a relation between types `α` and `β` such that `α` is finite and the image of each `x : α` is finite (it suffices for `β` to be finite). There is an injective function `α → β` respecting the relation iff every subset of `k` terms of `α` is related to at least `k` terms of `β`. If `[fintype β]`, then `[∀ (a : α), fintype (rel.image r {a})]` is automatically implied. -/ theorem fintype.all_card_le_rel_image_card_iff_exists_injective {α β : Type*} [fintype α] [decidable_eq β] (r : α → β → Prop) [∀ (a : α), fintype (rel.image r {a})] : (∀ (A : finset α), A.card ≤ fintype.card (rel.image r A)) ↔ (∃ (f : α → β), function.injective f ∧ ∀ x, r x (f x)) := begin let r' := λ a, (rel.image r {a}).to_finset, have h : ∀ (A : finset α), fintype.card (rel.image r A) = (A.bUnion r').card, { intro A, rw ←set.to_finset_card, apply congr_arg, ext b, simp [rel.image], }, have h' : ∀ (f : α → β) x, r x (f x) ↔ f x ∈ r' x, { simp [rel.image], }, simp only [h, h'], apply finset.all_card_le_bUnion_card_iff_exists_injective, end /-- This is a version of Hall's Marriage Theorem in terms of a relation between finite types. There is an injective function `α → β` respecting the relation iff every subset of `k` terms of `α` is related to at least `k` terms of `β`. It is like `fintype.all_card_le_rel_image_card_iff_exists_injective` but uses `finset.filter` rather than `rel.image`. -/ theorem fintype.all_card_le_filter_rel_iff_exists_injective {α β : Type*} [fintype α] [fintype β] (r : α → β → Prop) [∀ a, decidable_pred (r a)] : (∀ (A : finset α), A.card ≤ (univ.filter (λ (b : β), ∃ a ∈ A, r a b)).card) ↔ (∃ (f : α → β), function.injective f ∧ ∀ x, r x (f x)) := begin haveI := classical.dec_eq β, let r' := λ a, univ.filter (λ b, r a b), have h : ∀ (A : finset α), (univ.filter (λ (b : β), ∃ a ∈ A, r a b)) = (A.bUnion r'), { intro A, ext b, simp, }, have h' : ∀ (f : α → β) x, r x (f x) ↔ f x ∈ r' x, { simp, }, simp_rw [h, h'], apply finset.all_card_le_bUnion_card_iff_exists_injective, end
743537844827a5cc4e7355c3d52176b08179d51f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/closed/functor.lean
8d59e3074eaf6e685c0bdfc2060f24de414f12ca
[ "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,599
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 category_theory.closed.cartesian import category_theory.limits.preserves.shapes.binary_products import category_theory.adjunction.fully_faithful /-! # Cartesian closed functors Define the exponential comparison morphisms for a functor which preserves binary products, and use them to define a cartesian closed functor: one which (naturally) preserves exponentials. Define the Frobenius morphism, and show it is an isomorphism iff the exponential comparison is an isomorphism. ## TODO Some of the results here are true more generally for closed objects and for closed monoidal categories, and these could be generalised. ## References https://ncatlab.org/nlab/show/cartesian+closed+functor https://ncatlab.org/nlab/show/Frobenius+reciprocity ## Tags Frobenius reciprocity, cartesian closed functor -/ namespace category_theory open category limits cartesian_closed universes v u u' variables {C : Type u} [category.{v} C] variables {D : Type u'} [category.{v} D] variables [has_finite_products C] [has_finite_products D] variables (F : C ⥤ D) {L : D ⥤ C} noncomputable theory /-- The Frobenius morphism for an adjunction `L ⊣ F` at `A` is given by the morphism L(FA ⨯ B) ⟶ LFA ⨯ LB ⟶ A ⨯ LB natural in `B`, where the first morphism is the product comparison and the latter uses the counit of the adjunction. We will show that if `C` and `D` are cartesian closed, then this morphism is an isomorphism for all `A` iff `F` is a cartesian closed functor, i.e. it preserves exponentials. -/ def frobenius_morphism (h : L ⊣ F) (A : C) : prod.functor.obj (F.obj A) ⋙ L ⟶ L ⋙ prod.functor.obj A := prod_comparison_nat_trans L (F.obj A) ≫ whisker_left _ (prod.functor.map (h.counit.app _)) /-- If `F` is full and faithful and has a left adjoint `L` which preserves binary products, then the Frobenius morphism is an isomorphism. -/ instance frobenius_morphism_iso_of_preserves_binary_products (h : L ⊣ F) (A : C) [preserves_limits_of_shape (discrete walking_pair) L] [full F] [faithful F] : is_iso (frobenius_morphism F h A) := begin apply nat_iso.is_iso_of_is_iso_app _, intro B, dsimp [frobenius_morphism], apply_instance, end variables [cartesian_closed C] [cartesian_closed D] variables [preserves_limits_of_shape (discrete walking_pair) F] /-- The exponential comparison map. `F` is a cartesian closed functor if this is an iso for all `A`. -/ def exp_comparison (A : C) : exp A ⋙ F ⟶ F ⋙ exp (F.obj A) := transfer_nat_trans (exp.adjunction A) (exp.adjunction (F.obj A)) (prod_comparison_nat_iso F A).inv lemma exp_comparison_ev (A B : C) : limits.prod.map (𝟙 (F.obj A)) ((exp_comparison F A).app B) ≫ (ev (F.obj A)).app (F.obj B) = inv (prod_comparison F _ _) ≫ F.map ((ev _).app _) := begin convert transfer_nat_trans_counit _ _ (prod_comparison_nat_iso F A).inv B, ext, simp, end lemma coev_exp_comparison (A B : C) : F.map ((coev A).app B) ≫ (exp_comparison F A).app (A ⨯ B) = (coev _).app (F.obj B) ≫ (exp (F.obj A)).map (inv (prod_comparison F A B)) := begin convert unit_transfer_nat_trans _ _ (prod_comparison_nat_iso F A).inv B, ext, dsimp, simp, end lemma uncurry_exp_comparison (A B : C) : cartesian_closed.uncurry ((exp_comparison F A).app B) = inv (prod_comparison F _ _) ≫ F.map ((ev _).app _) := by rw [uncurry_eq, exp_comparison_ev] /-- The exponential comparison map is natural in `A`. -/ lemma exp_comparison_whisker_left {A A' : C} (f : A' ⟶ A) : exp_comparison F A ≫ whisker_left _ (pre (F.map f)) = whisker_right (pre f) _ ≫ exp_comparison F A' := begin ext B, dsimp, apply uncurry_injective, rw [uncurry_natural_left, uncurry_natural_left, uncurry_exp_comparison, uncurry_pre, prod.map_swap_assoc, ←F.map_id, exp_comparison_ev, ←F.map_id, ←prod_comparison_inv_natural_assoc, ←prod_comparison_inv_natural_assoc, ←F.map_comp, ←F.map_comp, prod_map_pre_app_comp_ev], end /-- The functor `F` is cartesian closed (ie preserves exponentials) if each natural transformation `exp_comparison F A` is an isomorphism -/ class cartesian_closed_functor := (comparison_iso : ∀ A, is_iso (exp_comparison F A)) attribute [instance] cartesian_closed_functor.comparison_iso lemma frobenius_morphism_mate (h : L ⊣ F) (A : C) : transfer_nat_trans_self (h.comp _ _ (exp.adjunction A)) ((exp.adjunction (F.obj A)).comp _ _ h) (frobenius_morphism F h A) = exp_comparison F A := begin rw ←equiv.eq_symm_apply, ext B : 2, dsimp [frobenius_morphism, transfer_nat_trans_self, transfer_nat_trans, adjunction.comp], simp only [id_comp, comp_id], rw [←L.map_comp_assoc, prod.map_id_comp, assoc, exp_comparison_ev, prod.map_id_comp, assoc, ← F.map_id, ← prod_comparison_inv_natural_assoc, ← F.map_comp, ev_coev, F.map_id (A ⨯ L.obj B), comp_id], apply prod.hom_ext, { rw [assoc, assoc, ←h.counit_naturality, ←L.map_comp_assoc, assoc, inv_prod_comparison_map_fst], simp }, { rw [assoc, assoc, ←h.counit_naturality, ←L.map_comp_assoc, assoc, inv_prod_comparison_map_snd], simp }, end /-- If the exponential comparison transformation (at `A`) is an isomorphism, then the Frobenius morphism at `A` is an isomorphism. -/ lemma frobenius_morphism_iso_of_exp_comparison_iso (h : L ⊣ F) (A : C) [i : is_iso (exp_comparison F A)] : is_iso (frobenius_morphism F h A) := begin rw ←frobenius_morphism_mate F h at i, exact @@transfer_nat_trans_self_of_iso _ _ _ _ _ i, end /-- If the Frobenius morphism at `A` is an isomorphism, then the exponential comparison transformation (at `A`) is an isomorphism. -/ lemma exp_comparison_iso_of_frobenius_morphism_iso (h : L ⊣ F) (A : C) [i : is_iso (frobenius_morphism F h A)] : is_iso (exp_comparison F A) := by { rw ← frobenius_morphism_mate F h, apply_instance } /-- If `F` is full and faithful, and has a left adjoint which preserves binary products, then it is cartesian closed. TODO: Show the converse, that if `F` is cartesian closed and its left adjoint preserves binary products, then it is full and faithful. -/ def cartesian_closed_functor_of_left_adjoint_preserves_binary_products (h : L ⊣ F) [full F] [faithful F] [preserves_limits_of_shape (discrete walking_pair) L] : cartesian_closed_functor F := { comparison_iso := λ A, exp_comparison_iso_of_frobenius_morphism_iso F h _ } end category_theory
491d3f4a64b7ae93ea5c35067f6265bfaba3078e
26ac254ecb57ffcb886ff709cf018390161a9225
/src/ring_theory/polynomial/rational_root.lean
5ccd0c8d337cb1c71efbed3976282a5dd0bcadb4
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
9,392
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import ring_theory.polynomial.basic import ring_theory.localization /-! # Rational root theorem and integral root theorem This file contains the rational root theorem and integral root theorem. The rational root theorem for a unique factorization domain `A` with localization `S`, states that the roots of `p : polynomial A` in `A`'s field of fractions are of the form `x / y` with `x y : A`, `x ∣ p.coeff 0` and `y ∣ p.leading_coeff`. The corollary is the integral root theorem `is_integer_of_is_root_of_monic`: if `p` is monic, its roots must be integers. Finally, we use this to show unique factorization domains are integrally closed. ## References * https://en.wikipedia.org/wiki/Rational_root_theorem -/ section scale_roots variables {A K R S : Type*} [integral_domain A] [field K] [comm_ring R] [comm_ring S] variables {M : submonoid A} {f : localization_map M S} {g : fraction_map A K} open finsupp polynomial /-- `scale_roots p s` is a polynomial with root `r * s` for each root `r` of `p`. -/ noncomputable def scale_roots (p : polynomial R) (s : R) : polynomial R := on_finset p.support (λ i, coeff p i * s ^ (p.nat_degree - i)) (λ i h, mem_support_iff.mpr (left_ne_zero_of_mul h)) @[simp] lemma coeff_scale_roots (p : polynomial R) (s : R) (i : ℕ) : (scale_roots p s).coeff i = coeff p i * s ^ (p.nat_degree - i) := rfl lemma coeff_scale_roots_nat_degree (p : polynomial R) (s : R) : (scale_roots p s).coeff p.nat_degree = p.leading_coeff := by rw [leading_coeff, coeff_scale_roots, nat.sub_self, pow_zero, mul_one] @[simp] lemma zero_scale_roots (s : R) : scale_roots 0 s = 0 := by { ext, simp } lemma scale_roots_ne_zero {p : polynomial R} (hp : p ≠ 0) (s : R) : scale_roots p s ≠ 0 := begin intro h, have : p.coeff p.nat_degree ≠ 0 := mt leading_coeff_eq_zero.mp hp, have : (scale_roots p s).coeff p.nat_degree = 0 := congr_fun (congr_arg (coeff : polynomial R → ℕ → R) h) p.nat_degree, rw [coeff_scale_roots_nat_degree] at this, contradiction end lemma support_scale_roots_le (p : polynomial R) (s : R) : (scale_roots p s).support ≤ p.support := begin intros i, simp only [mem_support_iff, scale_roots, on_finset_apply], exact left_ne_zero_of_mul end lemma support_scale_roots_eq (p : polynomial R) {s : R} (hs : s ∈ non_zero_divisors R) : (scale_roots p s).support = p.support := le_antisymm (support_scale_roots_le p s) begin intro i, simp only [mem_support_iff, scale_roots, on_finset_apply], intros p_ne_zero ps_zero, have := ((non_zero_divisors R).pow_mem hs (p.nat_degree - i)) _ ps_zero, contradiction end @[simp] lemma degree_scale_roots (p : polynomial R) {s : R} : degree (scale_roots p s) = degree p := begin haveI := classical.prop_decidable, by_cases hp : p = 0, { rw [hp, zero_scale_roots] }, have := scale_roots_ne_zero hp s, refine le_antisymm (finset.sup_mono (support_scale_roots_le p s)) (degree_le_degree _), rw coeff_scale_roots_nat_degree, intro h, have := leading_coeff_eq_zero.mp h, contradiction, end @[simp] lemma nat_degree_scale_roots (p : polynomial R) (s : R) : nat_degree (scale_roots p s) = nat_degree p := by simp only [nat_degree, degree_scale_roots] lemma monic_scale_roots_iff {p : polynomial R} (s : R) : monic (scale_roots p s) ↔ monic p := by simp [monic, leading_coeff] lemma scale_roots_eval₂_eq_zero {p : polynomial S} (f : S →+* R) {r : R} {s : S} (hr : eval₂ f r p = 0) (hs : s ∈ non_zero_divisors S) : eval₂ f (f s * r) (scale_roots p s) = 0 := calc (scale_roots p s).support.sum (λ i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) = p.support.sum (λ (i : ℕ), f (p.coeff i) * f s ^ (p.nat_degree - i + i) * r ^ i) : finset.sum_congr (support_scale_roots_eq p hs) (λ i hi, by simp_rw [f.map_mul, f.map_pow, pow_add, mul_pow, mul_assoc]) ... = p.support.sum (λ (i : ℕ), f s ^ p.nat_degree * (f (p.coeff i) * r ^ i)) : finset.sum_congr rfl (λ i hi, by { rw [mul_assoc, mul_left_comm, nat.sub_add_cancel], exact le_nat_degree_of_ne_zero (mem_support_iff.mp hi) }) ... = f s ^ p.nat_degree * eval₂ f r p : finset.mul_sum.symm ... = 0 : by rw [hr, _root_.mul_zero] lemma scale_roots_aeval_eq_zero [algebra S R] {p : polynomial S} {r : R} {s : S} (hr : aeval S R r p = 0) (hs : s ∈ non_zero_divisors S) : aeval S R (algebra_map S R s * r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero (algebra_map S R) hr hs lemma scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero {p : polynomial A} {f : A →+* K} (hf : function.injective f) {r s : A} (hr : eval₂ f (f r / f s) p = 0) (hs : s ∈ non_zero_divisors A) : eval₂ f (f r) (scale_roots p s) = 0 := begin convert scale_roots_eval₂_eq_zero f hr hs, rw [←mul_div_assoc, mul_comm, mul_div_cancel], exact @map_ne_zero_of_mem_non_zero_divisors _ _ _ _ _ hf ⟨s, hs⟩ end lemma scale_roots_aeval_eq_zero_of_aeval_div_eq_zero [algebra A K] (inj : function.injective (algebra_map A K)) {p : polynomial A} {r s : A} (hr : aeval A K (algebra_map A K r / algebra_map A K s) p = 0) (hs : s ∈ non_zero_divisors A) : aeval A K (algebra_map A K r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero inj hr hs lemma scale_roots_aeval_eq_zero_of_aeval_mk'_eq_zero {p : polynomial A} {r : A} {s : M} (hr : aeval A f.codomain (f.mk' r s) p = 0) (hM : M ≤ non_zero_divisors A) : aeval A f.codomain (f.to_map r) (scale_roots p s) = 0 := begin convert scale_roots_eval₂_eq_zero f.to_map hr (hM s.2), rw aeval_def, congr, apply (f.mk'_spec' r s).symm end lemma num_is_root_scale_roots_of_aeval_eq_zero [unique_factorization_domain A] (g : fraction_map A K) {p : polynomial A} {x : g.codomain} (hr : aeval A g.codomain x p = 0) : is_root (scale_roots p (g.denom x)) (g.num x) := begin apply is_root_of_eval₂_map_eq_zero g.injective, refine scale_roots_aeval_eq_zero_of_aeval_mk'_eq_zero _ (le_refl (non_zero_divisors A)), rw g.mk'_num_denom, exact hr end end scale_roots section rational_root_theorem variables {A K : Type*} [integral_domain A] [unique_factorization_domain A] [field K] variables {f : fraction_map A K} open polynomial unique_factorization_domain /-- Rational root theorem part 1: if `r : f.codomain` is a root of a polynomial over the ufd `A`, then the numerator of `r` divides the constant coefficient -/ theorem num_dvd_of_is_root {p : polynomial A} {r} (hr : aeval A f.codomain r p = 0) : f.num r ∣ p.coeff 0 := begin suffices : f.num r ∣ (scale_roots p (f.denom r)).coeff 0, { simp only [coeff_scale_roots, nat.sub_zero] at this, haveI := classical.prop_decidable, by_cases hr : f.num r = 0, { obtain ⟨u, hu⟩ := is_unit_pow p.nat_degree (f.is_unit_denom_of_num_eq_zero hr), rw ←hu at this, exact dvd_mul_unit_iff.mp this }, { refine dvd_of_dvd_mul_left_of_no_prime_factors hr _ this, intros q dvd_num dvd_denom_pow hq, apply hq.not_unit, exact f.num_denom_reduced r dvd_num (hq.dvd_of_dvd_pow dvd_denom_pow) } }, convert dvd_term_of_is_root_of_dvd_terms 0 (num_is_root_scale_roots_of_aeval_eq_zero f hr) _, { rw [pow_zero, mul_one] }, intros j hj, apply dvd_mul_of_dvd_right, convert pow_dvd_pow (f.num r) (nat.succ_le_of_lt (bot_lt_iff_ne_bot.mpr hj)), exact (pow_one _).symm end /-- Rational root theorem part 2: if `r : f.codomain` is a root of a polynomial over the ufd `A`, then the denominator of `r` divides the leading coefficient -/ theorem denom_dvd_of_is_root {p : polynomial A} {r} (hr : aeval A f.codomain r p = 0) : (f.denom r : A) ∣ p.leading_coeff := begin suffices : (f.denom r : A) ∣ p.leading_coeff * f.num r ^ p.nat_degree, { refine dvd_of_dvd_mul_left_of_no_prime_factors (mem_non_zero_divisors_iff_ne_zero.mp (f.denom r).2) _ this, intros q dvd_denom dvd_num_pow hq, apply hq.not_unit, exact f.num_denom_reduced r (hq.dvd_of_dvd_pow dvd_num_pow) dvd_denom }, rw ←coeff_scale_roots_nat_degree, apply dvd_term_of_is_root_of_dvd_terms _ (num_is_root_scale_roots_of_aeval_eq_zero f hr), intros j hj, by_cases h : j < p.nat_degree, { refine dvd_mul_of_dvd_left (dvd_mul_of_dvd_right _ _) _, convert pow_dvd_pow _ (nat.succ_le_iff.mpr (nat.lt_sub_left_of_add_lt _)), { exact (pow_one _).symm }, simpa using h }, rw [←nat_degree_scale_roots p (f.denom r)] at *, rw [coeff_eq_zero_of_nat_degree_lt (lt_of_le_of_ne (le_of_not_gt h) hj.symm), zero_mul], exact dvd_zero _ end /-- Integral root theorem: if `r : f.codomain` is a root of a monic polynomial over the ufd `A`, then `r` is an integer -/ theorem is_integer_of_is_root_of_monic {p : polynomial A} (hp : monic p) {r} (hr : aeval A f.codomain r p = 0) : f.is_integer r := f.is_integer_of_is_unit_denom (is_unit_of_dvd_one _ (hp ▸ denom_dvd_of_is_root hr)) namespace unique_factorization_domain lemma integer_of_integral {x : f.codomain} : is_integral A x → f.is_integer x := λ ⟨p, hp, hx⟩, is_integer_of_is_root_of_monic hp hx lemma integrally_closed : integral_closure A f.codomain = ⊥ := eq_bot_iff.mpr (λ x hx, algebra.mem_bot.mpr (integer_of_integral hx)) end unique_factorization_domain end rational_root_theorem
119cdec9ebd5beac615495a806cddaefba2fabee
d0f9af2b0ace5ce352570d61b09019c8ef4a3b96
/exam_2/satisfiability/exam2key.lean
98de602e9172973c167bfdd45a4bb54b7f45a839
[]
no_license
jngo13/Discrete-Mathematics
8671540ef2da7c75915d32332dd20c02f001474e
bf674a866e61f60e6e6d128df85fa73819091787
refs/heads/master
1,675,615,657,924
1,609,142,011,000
1,609,142,011,000
267,190,341
0
0
null
null
null
null
UTF-8
Lean
false
false
12,669
lean
import .satisfiability import .rules_of_reasoning .. /- CS2102 Exam #2: Propositional Logic -/ -- You submitted an exam: [3 points free!] /- [20 points] 2 for header 9 for reaching until list of interpretations (First 2 lines of is_satisfaible) 4.5 for the helper function (fold_or) and 4.5 for calling it from is_satisfiable #1. Define a predicate function, is_satisfiable: pExp → bool that returns true iff the given proposition is satisfiable. Hint: Model your answer on our definition of is_valid. You may need to define one or more helper functions. You should test your solution, but we will only grade your definition. Please write test cases in a separate file. Do not submit that file. -/ --returns true if at least one interpretation results in --the expression being true def foldr_or : list bool → bool | [] := ff | (h::t) := bor h (foldr_or t) -- Answer here def is_satisfiable (e : pExp) : bool := let n := number_of_variables_in_expression e in let is := interpretations_for_n_vars n in let rs := map_pEval_over_interps e is in foldr_or rs /- [8 points] 1 point each #2. Clearly the propositions in rules_of_reasoning.lean that are valid are also satisfiable. But what about the following? Apply your satisfiability predicate function to decide whether or not each of the following formulae is satisfiable or not. Write an "#eval is_satisfiable e" command for each expression. A. The "fallacies" in that file (the ones that aren't valid). B. The proposition, pFalse. C. The following propositions: 1. P ∧ ¬ P 2. P ∨ ¬ P 3. = (¬x1 ∨ x2) ∧ (¬x2 ∨ x3 ∨ ¬x4) ∧ (x1 ∨ x2 ∨ x3 ∨ ¬x4) Note that for 3. you will have to define four new variables. Call them x1, x2, x3, and x4. x1...x4 are needed for last one but is not graded -/ def x1 : pExp := pExp.pVar (var.mk 0) def x2 : pExp := pExp.pVar (var.mk 1) def x3 : pExp := pExp.pVar (var.mk 2) def x4 : pExp := pExp.pVar (var.mk 3) -- Answers here --All satisfiable but not valid #eval is_satisfiable true_imp #eval is_satisfiable affirm_consequence #eval is_satisfiable affirm_disjunct #eval is_satisfiable deny_antecedent --False is unsatisfiable #eval is_satisfiable pExp.pFalse #eval is_satisfiable (P ∧ ¬ P) --unsatisfiable #eval is_satisfiable (P ∨ ¬ P) --satisfiable (valid) #eval is_satisfiable ((¬x1 ∨ x2) ∧ (¬x2 ∨ x3 ∨ ¬x4) ∧ (x1 ∨ x2 ∨ x3 ∨ ¬x4)) --also satisfiable (all true) /- 20 POINTS -- 8 points for generating all interpretations -- 8 points for seeing that you need to find a satisfying one -- 4 points for returning the value correctly as an option value 3. In the previous problems, you defined a satisfiability predicate function that returns true or false depending on whether a given formula is satisfiable or not. Often we will want to know not only whether there exists a solution but an actual example of a solution, if there is one. Define a function called sat_solver : pExp → option (var → bool) that returns a satisfying interpretation (as "some interpretation") if there is one, or "none" otherwise. Hint: Model your solution on our validity checker. First compute the list of interpretations for a given expression, e, then reduce that list to a value of type option (var → bool). Evaluate e under each interpretation in the list until either (A) you find one, call it i, for which e evaluates to true, or until you reach the empty list, empty handed. -/ -- Answer here --helper function def reduce_interpretations : pExp → list (var → bool) → option (var → bool) | e list.nil := none | e (h::t) := match (pEval e h) with | tt := some h | ff := reduce_interpretations e t end def sat_solver (e : pExp) : option (var → bool) := let n := number_of_variables_in_expression e in let is := interpretations_for_n_vars n in reduce_interpretations e is -- Example (OPTIONAL) #reduce sat_solver (P ∧ Q >> (¬ P)) -- for getting the truth value you can also do (OPTIONAL) def option_to_interp'' : option (var → bool) → (var → bool) | none := λ (v : var), ff | (some i) := i #reduce option_to_interp'' (sat_solver (P ∧ Q >> (¬ P))) #eval pEval (P ∧ Q >> (¬ P)) (option_to_interp'' (sat_solver (P ∧ Q >> (¬ P)))) /- [10 points] All or nothing/ Just make sure it works 4. Define a predicate function, is_unsatisfiable : pExp → bool, that takes a propositional logic formula, e, and returns true iff e is unsatisfiable. Hint: Easy. Build on what you have. -/ -- Answer here -- 3 different ways: --1 --------------------------------------------------------------------- def is_unsatisfiable (e : pExp) : bool := ¬ (is_satisfiable e) --------------------------------------------------------------------- --2 --------------------------------------------------------------------- def F_to_T : list bool → list bool | [] := [] | (h::t) := ((¬h) :: F_to_T t) def is_unsatisfiable' (e : pExp) : bool := let n := number_of_variables_in_expression e in let is := interpretations_for_n_vars n in let rs := map_pEval_over_interps e is in let ft := F_to_T rs in foldr_and ft --------------------------------------------------------------------- --3 --------------------------------------------------------------------- def is_unsatisfiable'' : pExp → bool | e := match (sat_solver e) with | none := tt | _ := ff end --------------------------------------------------------------------- -- For proof checking check these propositions #eval is_unsatisfiable ((P ∧ ¬ Q) ∨ ((Q ∧ ¬ P))) --ff #eval is_unsatisfiable ((P ∧ ¬ Q)) --ff #eval is_unsatisfiable (P ∧ ¬ P) --tt #eval is_unsatisfiable pExp.pFalse --tt #eval is_unsatisfiable pExp.pTrue --ff /- [15 points] 4 => for finding list of interps 8 points => Find which interps work as a counter example (match...with or if..else or simply called sat_solver with ¬e) 3 => returning as an option 5. Given a propositional logic expression, e, and an incorrect claim that it's valid, we often want to produce a counter-example to that claim. Such a counter example is an interpretation under which the expression is not true. Equivalently, a counterexample is an interpretation under which the *negation* of the expression *is* true. Define a function, counterexample : pExp → option (var → bool), that takes any expression e and returns either a counterexample (as "some interpretation") or "none" if there isn't one. Hint: given e, try to find a model for the expression, ¬ e. -/ -- Answer here -- 2 different ways --1 --------------------------------------------------------------------- def mk_counter_example : pExp → list (var → bool) → option (var → bool) | e list.nil := none | e (h::t) := match (pEval e h) with | ff := some h | tt := mk_counter_example e t end def counter_example (e : pExp) : option (var → bool) := let n := number_of_variables_in_expression e in let is := interpretations_for_n_vars n in mk_counter_example e is --------------------------------------------------------------------- --2 --------------------------------------------------------------------- def counter_example' (e : pExp) : option (var → bool) := let n := number_of_variables_in_expression e in let is := interpretations_for_n_vars n in sat_solver (¬ e) --------------------------------------------------------------------- -- OPTIONAL for checking def option_to_interp : option (var → bool) → (var → bool) | none := λ (v : var), ff | (some i) := i #reduce pEval deny_antecedent (option_to_interp (counter_example' deny_antecedent)) /- ONE POINT EACH (24 points) 6. Give an English language example for every valid rule of reasoning in rules_of_reasoning.lean, and also give an English language counter-example for each fallacy. For example, for the rule, P >> Q >> P ∧ Q, you could say, "If the ball is red then if (in addition to that) the box is blue, then (in this context) the ball is red AND the box is blue." It will be easiest is you re-use the same words for P, Q, and R, in all of your examples. E.g., P means "the ball is red", Q means "the box is blue", etc. Copy the contents of rules_of_reasoning.lean into this comment block and under each rule give your English language sentence. -/ --english examples etc. /- def true_intro : pExp := pTrue True is true. def false_elim := pFalse >> P False implies anything. If false is true then anything is true. def true_imp := pTrue >> P Fallacy. True implies anything. If true is true then anything is true. If 1=1 then 1=0. def and_intro := P >> Q >> P ∧ Q If it's raining then if the streets are wet then it's raining and the streets are wet. def and_elim_left := P ∧ Q >> P If it's raining and the streets are wet then it's raining. def and_elim_right := P ∧ Q >> Q If it's raining and the streets are wet then the streets are wet. def or_intro_left := P >> P ∨ Q If it's raining then it's raining or the the streets are wet. def or_intro_right := Q >> P ∨ Q If the streets are wet then it's raining or the the streets are wet. ------------------------------------------------------------------------------ (First 8) def or_elim := P ∨ Q >> (P >> R) >> (Q >> R) >> R If it's raining or the sprinkler is running, then if whenever its raining the streets are wet, then if whenever the sprinkler is running the streets are wet, then the streets are wet. def iff_intro := (P >> Q) >> (Q >> P) >> (P ↔ Q) If it's the case that if it's spring the flowers are blooming, and then it's also the case that if the flowers are blooming then it's springtime, then the flowers are blooming if and only if it's spring. def iff_intro' := (P >> Q) ∧ (Q >> P) >> (P ↔ Q) If it's the case that if it's spring the flowers are blooming, and it's also the case that if the flowers are blooming then it's springtime, then the flowers are blooming if and only if it's spring. def iff_elim_left := (P ↔ Q) >> (P >> Q) If the flowers are blooming if and only if it's spring, then if the flowers are blooming then it's spring. def iff_elim_right := (P ↔ Q) >> (Q >> P) If the flowers are blooming if and only if it's spring, then if it's spring then the flowers are blooming. def arrow_elim := (P >> Q) >> (P >> Q) If whenever it's raining the streets are wet, then if it's raining, the streets are wet. def resolution := (P ∨ Q) >> (¬ Q ∨ R) >> (P ∨ R) If the pizza is hot (P) or the soda is cold (Q), then if the soda is warm (¬ Q) or the cheese sticks are tasty (R), then the pizza is hot or the cheese sticks are tasty. def unit_resolution := (P ∨ Q) >> ((¬ Q) >> P) If the pizza is hot or the soda is cold, then if the soda is not cold, the pizza must be hot. -------------------------------------------------------------------------------------- (Middle 8) def syllogism := (P >> Q) >> (Q >> R) >> (P >> R) If being sick makes you sad, and being sad makes you cry, then being sick makes you cry. def modus_tollens := (P >> Q) >> (¬ Q >> ¬ P) If being sick makes you cry, then if you're not crying you must not be sick. def neg_elim := (¬ ¬ P) >> P If a natural number, n, is not not even, then n is even. If you're not not happy, then you're happy. (The connotation of not not happy in English is not really exactly the same as the connotation of happy. So here the English and the logic don't quite align.) def excluded_middle := P ∨ (¬ P) A natural number, n, is even or n is not even. def neg_intro := (P >> pFalse) >> (¬ P) If the assumption that a = b let's you deduce something that's impossible, then a=b can't be true, and must be false. def affirm_consequence := (P >> Q) >> (Q >> P) If it's raining then the streets are wet, then if the streets are wet it must be raining. This is fallacy, as the streets could be wet even if it's not raining (by other causes). def affirm_disjunct := (P ∨ Q) >> (P >> ¬ Q) Professor Foo is great or Prof. Bar is great, so if Prof. Foo is great then Prof. Bar must not be. This is also a fallacy, as both can be great. def deny_antecedent := (P >> Q) >> (¬ P >> ¬ Q) If it's raining means the streets are wet, then if it's not raining the streets must be dry. Also a fallacy, for the same reason as above. The streets could be wet even it's not raining. Maybe someone spilled their drink. -------------------------------------------------------------------------------- (Last 8) -/
925f1d66c04c7681d52e6c9eac67356f24195f0c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/set/intervals/instances.lean
21a3ed791b359dcafd6aac468f09911ef4450fc2
[ "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
10,051
lean
/- Copyright (c) 2022 Stuart Presnell. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stuart Presnell, Eric Wieser, Yaël Dillies, Patrick Massot, Scott Morrison -/ import algebra.group_power.order import algebra.ring.regular /-! # Algebraic instances for unit intervals For suitably structured underlying type `α`, we exhibit the structure of the unit intervals (`set.Icc`, `set.Ioc`, `set.Ioc`, and `set.Ioo`) from `0` to `1`. Note: Instances for the interval `Ici 0` are dealt with in `algebra/order/nonneg.lean`. ## Main definitions The strongest typeclass provided on each interval is: * `set.Icc.cancel_comm_monoid_with_zero` * `set.Ico.comm_semigroup` * `set.Ioc.comm_monoid` * `set.Ioo.comm_semigroup` ## TODO * algebraic instances for intervals -1 to 1 * algebraic instances for `Ici 1` * algebraic instances for `(Ioo (-1) 1)ᶜ` * provide `has_distrib_neg` instances where applicable * prove versions of `mul_le_{left,right}` for other intervals * prove versions of the lemmas in `topology/unit_interval` with `ℝ` generalized to some arbitrary ordered semiring -/ open set variables {α : Type*} section ordered_semiring variables [ordered_semiring α] /-! ### Instances for `↥(set.Icc 0 1)` -/ namespace set.Icc instance has_zero : has_zero (Icc (0:α) 1) := { zero := ⟨0, left_mem_Icc.2 zero_le_one⟩ } instance has_one : has_one (Icc (0:α) 1) := { one := ⟨1, right_mem_Icc.2 zero_le_one⟩ } @[simp, norm_cast] lemma coe_zero : ↑(0 : Icc (0:α) 1) = (0 : α) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : Icc (0:α) 1) = (1 : α) := rfl @[simp] lemma mk_zero (h : (0 : α) ∈ Icc (0 : α) 1) : (⟨0, h⟩ : Icc (0:α) 1) = 0 := rfl @[simp] lemma mk_one (h : (1 : α) ∈ Icc (0 : α) 1) : (⟨1, h⟩ : Icc (0:α) 1) = 1 := rfl @[simp, norm_cast] lemma coe_eq_zero {x : Icc (0:α) 1} : (x : α) = 0 ↔ x = 0 := by { symmetry, exact subtype.ext_iff } lemma coe_ne_zero {x : Icc (0:α) 1} : (x : α) ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr coe_eq_zero @[simp, norm_cast] lemma coe_eq_one {x : Icc (0:α) 1} : (x : α) = 1 ↔ x = 1 := by { symmetry, exact subtype.ext_iff } lemma coe_ne_one {x : Icc (0:α) 1} : (x : α) ≠ 1 ↔ x ≠ 1 := not_iff_not.mpr coe_eq_one lemma coe_nonneg (x : Icc (0:α) 1) : 0 ≤ (x : α) := x.2.1 lemma coe_le_one (x : Icc (0:α) 1) : (x : α) ≤ 1 := x.2.2 /-- like `coe_nonneg`, but with the inequality in `Icc (0:α) 1`. -/ lemma nonneg {t : Icc (0:α) 1} : 0 ≤ t := t.2.1 /-- like `coe_le_one`, but with the inequality in `Icc (0:α) 1`. -/ lemma le_one {t : Icc (0:α) 1} : t ≤ 1 := t.2.2 instance has_mul : has_mul (Icc (0:α) 1) := { mul := λ p q, ⟨p*q, ⟨mul_nonneg p.2.1 q.2.1, mul_le_one p.2.2 q.2.1 q.2.2⟩⟩ } instance has_pow : has_pow (Icc (0:α) 1) ℕ := { pow := λ p n, ⟨p.1 ^ n, ⟨pow_nonneg p.2.1 n, pow_le_one n p.2.1 p.2.2⟩⟩ } @[simp, norm_cast] lemma coe_mul (x y : Icc (0:α) 1) : ↑(x * y) = (x * y : α) := rfl @[simp, norm_cast] lemma coe_pow (x : Icc (0:α) 1) (n : ℕ) : ↑(x ^ n) = (x ^ n : α) := rfl lemma mul_le_left {x y : Icc (0:α) 1} : x * y ≤ x := (mul_le_mul_of_nonneg_left y.2.2 x.2.1).trans_eq (mul_one x) lemma mul_le_right {x y : Icc (0:α) 1} : x * y ≤ y := (mul_le_mul_of_nonneg_right x.2.2 y.2.1).trans_eq (one_mul y) instance monoid_with_zero : monoid_with_zero (Icc (0:α) 1) := subtype.coe_injective.monoid_with_zero _ coe_zero coe_one coe_mul coe_pow instance comm_monoid_with_zero {α : Type*} [ordered_comm_semiring α] : comm_monoid_with_zero (Icc (0:α) 1) := subtype.coe_injective.comm_monoid_with_zero _ coe_zero coe_one coe_mul coe_pow instance cancel_monoid_with_zero {α : Type*} [ordered_ring α] [no_zero_divisors α] : cancel_monoid_with_zero (Icc (0:α) 1) := @function.injective.cancel_monoid_with_zero α _ no_zero_divisors.to_cancel_monoid_with_zero _ _ _ _ coe subtype.coe_injective coe_zero coe_one coe_mul coe_pow instance cancel_comm_monoid_with_zero {α : Type*} [ordered_comm_ring α] [no_zero_divisors α] : cancel_comm_monoid_with_zero (Icc (0:α) 1) := @function.injective.cancel_comm_monoid_with_zero α _ no_zero_divisors.to_cancel_comm_monoid_with_zero _ _ _ _ coe subtype.coe_injective coe_zero coe_one coe_mul coe_pow variables {β : Type*} [ordered_ring β] lemma one_sub_mem {t : β} (ht : t ∈ Icc (0:β) 1) : 1 - t ∈ Icc (0:β) 1 := by { rw mem_Icc at *, exact ⟨sub_nonneg.2 ht.2, (sub_le_self_iff _).2 ht.1⟩ } lemma mem_iff_one_sub_mem {t : β} : t ∈ Icc (0:β) 1 ↔ 1 - t ∈ Icc (0:β) 1 := ⟨one_sub_mem, λ h, (sub_sub_cancel 1 t) ▸ one_sub_mem h⟩ lemma one_sub_nonneg (x : Icc (0:β) 1) : 0 ≤ 1 - (x : β) := by simpa using x.2.2 lemma one_sub_le_one (x : Icc (0:β) 1) : 1 - (x : β) ≤ 1 := by simpa using x.2.1 end set.Icc /-! ### Instances for `↥(set.Ico 0 1)` -/ namespace set.Ico instance has_zero [nontrivial α] : has_zero (Ico (0:α) 1) := { zero := ⟨0, left_mem_Ico.2 zero_lt_one⟩ } @[simp, norm_cast] lemma coe_zero [nontrivial α] : ↑(0 : Ico (0:α) 1) = (0 : α) := rfl @[simp] lemma mk_zero [nontrivial α] (h : (0 : α) ∈ Ico (0 : α) 1) : (⟨0, h⟩ : Ico (0:α) 1) = 0 := rfl @[simp, norm_cast] lemma coe_eq_zero [nontrivial α] {x : Ico (0:α) 1} : (x : α) = 0 ↔ x = 0 := by { symmetry, exact subtype.ext_iff } lemma coe_ne_zero [nontrivial α] {x : Ico (0:α) 1} : (x : α) ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr coe_eq_zero lemma coe_nonneg (x : Ico (0:α) 1) : 0 ≤ (x : α) := x.2.1 lemma coe_lt_one (x : Ico (0:α) 1) : (x : α) < 1 := x.2.2 /-- like `coe_nonneg`, but with the inequality in `Ico (0:α) 1`. -/ lemma nonneg [nontrivial α] {t : Ico (0:α) 1} : 0 ≤ t := t.2.1 instance has_mul : has_mul (Ico (0:α) 1) := { mul := λ p q, ⟨p*q, ⟨mul_nonneg p.2.1 q.2.1, mul_lt_one_of_nonneg_of_lt_one_right p.2.2.le q.2.1 q.2.2⟩⟩ } @[simp, norm_cast] lemma coe_mul (x y : Ico (0:α) 1) : ↑(x * y) = (x * y : α) := rfl instance semigroup : semigroup (Ico (0:α) 1) := subtype.coe_injective.semigroup _ coe_mul instance comm_semigroup {α : Type*} [ordered_comm_semiring α] : comm_semigroup (Ico (0:α) 1) := subtype.coe_injective.comm_semigroup _ coe_mul end set.Ico end ordered_semiring variables [strict_ordered_semiring α] /-! ### Instances for `↥(set.Ioc 0 1)` -/ namespace set.Ioc instance has_one [nontrivial α] : has_one (Ioc (0:α) 1) := { one := ⟨1, ⟨zero_lt_one, le_refl 1⟩⟩ } @[simp, norm_cast] lemma coe_one [nontrivial α] : ↑(1 : Ioc (0:α) 1) = (1 : α) := rfl @[simp] lemma mk_one [nontrivial α] (h : (1 : α) ∈ Ioc (0 : α) 1) : (⟨1, h⟩ : Ioc (0:α) 1) = 1 := rfl @[simp, norm_cast] lemma coe_eq_one [nontrivial α] {x : Ioc (0:α) 1} : (x : α) = 1 ↔ x = 1 := by { symmetry, exact subtype.ext_iff } lemma coe_ne_one [nontrivial α] {x : Ioc (0:α) 1} : (x : α) ≠ 1 ↔ x ≠ 1 := not_iff_not.mpr coe_eq_one lemma coe_pos (x : Ioc (0:α) 1) : 0 < (x : α) := x.2.1 lemma coe_le_one (x : Ioc (0:α) 1) : (x : α) ≤ 1 := x.2.2 /-- like `coe_le_one`, but with the inequality in `Ioc (0:α) 1`. -/ lemma le_one [nontrivial α] {t : Ioc (0:α) 1} : t ≤ 1 := t.2.2 instance has_mul : has_mul (Ioc (0:α) 1) := { mul := λ p q, ⟨p.1 * q.1, ⟨mul_pos p.2.1 q.2.1, mul_le_one p.2.2 (le_of_lt q.2.1) q.2.2⟩⟩ } instance has_pow : has_pow (Ioc (0:α) 1) ℕ := { pow := λ p n, ⟨p.1 ^ n, ⟨pow_pos p.2.1 n, pow_le_one n (le_of_lt p.2.1) p.2.2⟩⟩ } @[simp, norm_cast] lemma coe_mul (x y : Ioc (0:α) 1) : ↑(x * y) = (x * y : α) := rfl @[simp, norm_cast] lemma coe_pow (x : Ioc (0:α) 1) (n : ℕ) : ↑(x ^ n) = (x ^ n : α) := rfl instance semigroup : semigroup (Ioc (0:α) 1) := subtype.coe_injective.semigroup _ coe_mul instance monoid [nontrivial α] : monoid (Ioc (0:α) 1) := subtype.coe_injective.monoid _ coe_one coe_mul coe_pow instance comm_semigroup {α : Type*} [strict_ordered_comm_semiring α] : comm_semigroup (Ioc (0:α) 1) := subtype.coe_injective.comm_semigroup _ coe_mul instance comm_monoid {α : Type*} [strict_ordered_comm_semiring α] [nontrivial α] : comm_monoid (Ioc (0:α) 1) := subtype.coe_injective.comm_monoid _ coe_one coe_mul coe_pow instance cancel_monoid {α : Type*} [strict_ordered_ring α] [is_domain α] : cancel_monoid (Ioc (0:α) 1) := { mul_left_cancel := λ a b c h, subtype.ext $ mul_left_cancel₀ a.prop.1.ne' $ (congr_arg subtype.val h : _), mul_right_cancel := λ a b c h, subtype.ext $ mul_right_cancel₀ b.prop.1.ne' $ (congr_arg subtype.val h : _), ..set.Ioc.monoid} instance cancel_comm_monoid {α : Type*} [strict_ordered_comm_ring α] [is_domain α] : cancel_comm_monoid (Ioc (0:α) 1) := { ..set.Ioc.cancel_monoid, ..set.Ioc.comm_monoid } end set.Ioc /-! ### Instances for `↥(set.Ioo 0 1)` -/ namespace set.Ioo lemma pos (x : Ioo (0:α) 1) : 0 < (x : α) := x.2.1 lemma lt_one (x : Ioo (0:α) 1) : (x : α) < 1 := x.2.2 instance has_mul : has_mul (Ioo (0:α) 1) := { mul := λ p q, ⟨p.1 * q.1, ⟨mul_pos p.2.1 q.2.1, mul_lt_one_of_nonneg_of_lt_one_right p.2.2.le q.2.1.le q.2.2⟩⟩ } @[simp, norm_cast] lemma coe_mul (x y : Ioo (0:α) 1) : ↑(x * y) = (x * y : α) := rfl instance semigroup : semigroup (Ioo (0:α) 1) := subtype.coe_injective.semigroup _ coe_mul instance comm_semigroup {α : Type*} [strict_ordered_comm_semiring α] : comm_semigroup (Ioo (0:α) 1) := subtype.coe_injective.comm_semigroup _ coe_mul variables {β : Type*} [ordered_ring β] lemma one_sub_mem {t : β} (ht : t ∈ Ioo (0:β) 1) : 1 - t ∈ Ioo (0:β) 1 := begin rw mem_Ioo at *, refine ⟨sub_pos.2 ht.2, _⟩, exact lt_of_le_of_ne ((sub_le_self_iff 1).2 ht.1.le) (mt sub_eq_self.mp ht.1.ne'), end lemma mem_iff_one_sub_mem {t : β} : t ∈ Ioo (0:β) 1 ↔ 1 - t ∈ Ioo (0:β) 1 := ⟨one_sub_mem, λ h, (sub_sub_cancel 1 t) ▸ one_sub_mem h⟩ lemma one_minus_pos (x : Ioo (0:β) 1) : 0 < 1 - (x : β) := by simpa using x.2.2 lemma one_minus_lt_one (x : Ioo (0:β) 1) : 1 - (x : β) < 1 := by simpa using x.2.1 end set.Ioo
b97d26349c7bfc5a8cda6252be4ab6df411690af
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/closure_bug7.lean
48bb9627b4b2b5ae4c0a4ecccf0d385ad7a335d8
[ "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
508
lean
def f (x : Nat) : Nat × (Nat → String) := let x1 := x + 1; let x2 := x + 2; let x3 := x + 3; let x4 := x + 4; let x5 := x + 5; let x6 := x + 6; let x7 := x + 7; let x8 := x + 8; let x9 := x + 9; let x10 := x + 10; let x11 := x + 11; let x12 := x + 12; let x13 := x + 13; let x14 := x + 14; let x15 := x + 15; let x16 := x + 16; let x17 := x + 17; (x, fun y => toString [x1, x2, x3, x4, x5, x6, x7]) def main (xs : List String) : IO Unit := IO.println ((f (xs.headD "0").toNat!).2 (xs.headD "0").toNat!)
1ba056c1fd044e56a84239d226e7a40b9f2da7a7
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/model_theory/basic.lean
4b1d0765fc66f3ee21ccddc87269c61c7dc00e30
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,514
lean
/- Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn -/ import data.nat.basic import data.set_like.basic import data.set.lattice import order.closure /-! # Basics on First-Order Structures This file defines first-order languages and structures in the style of the [Flypitch project](https://flypitch.github.io/). ## Main Definitions * A `first_order.language` defines a language as a pair of functions from the natural numbers to `Type l`. One sends `n` to the type of `n`-ary functions, and the other sends `n` to the type of `n`-ary relations. * A `first_order.language.Structure` interprets the symbols of a given `first_order.language` in the context of a given type. * A `first_order.language.hom`, denoted `M →[L] N`, is a map from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations (although only in the forward direction). * A `first_order.language.embedding`, denoted `M ↪[L] N`, is an embedding from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. * A `first_order.language.equiv`, denoted `M ≃[L] N`, is an equivalence from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. ## References For the Flypitch project: - [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*] [flypitch_cpp] - [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of the continuum hypothesis*][flypitch_itp] -/ universe variables u v namespace first_order /-- A first-order language consists of a type of functions of every natural-number arity and a type of relations of every natural-number arity. -/ @[nolint check_univs] -- false positive structure language := (functions : ℕ → Type u) (relations : ℕ → Type v) namespace language /-- The empty language has no symbols. -/ def empty : language := ⟨λ _, pempty, λ _, pempty⟩ instance : inhabited language := ⟨empty⟩ /-- The type of constants in a given language. -/ @[nolint has_inhabited_instance] def const (L : language) := L.functions 0 variable (L : language) /-- A language is relational when it has no function symbols. -/ class is_relational : Prop := (empty_functions : ∀ n, L.functions n → false) /-- A language is algebraic when it has no relation symbols. -/ class is_algebraic : Prop := (empty_relations : ∀ n, L.relations n → false) variable {L} instance is_relational_of_empty_functions {symb : ℕ → Type*} : is_relational ⟨λ _, pempty, symb⟩ := ⟨by { intro n, apply pempty.elim }⟩ instance is_algebraic_of_empty_relations {symb : ℕ → Type*} : is_algebraic ⟨symb, λ _, pempty⟩ := ⟨by { intro n, apply pempty.elim }⟩ instance is_relational_empty : is_relational (empty) := language.is_relational_of_empty_functions instance is_algebraic_empty : is_algebraic (empty) := language.is_algebraic_of_empty_relations variables (L) (M : Type*) /-- A first-order structure on a type `M` consists of interpretations of all the symbols in a given language. Each function of arity `n` is interpreted as a function sending tuples of length `n` (modeled as `(fin n → M)`) to `M`, and a relation of arity `n` is a function from tuples of length `n` to `Prop`. -/ class Structure := (fun_map : ∀{n}, L.functions n → (fin n → M) → M) (rel_map : ∀{n}, L.relations n → (fin n → M) → Prop) variables (N : Type*) [L.Structure M] [L.Structure N] open first_order.language.Structure /-- A homomorphism between first-order structures is a function that commutes with the interpretations of functions and maps tuples in one structure where a given relation is true to tuples in the second structure where that relation is still true. -/ protected structure hom := (to_fun : M → N) (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r x → rel_map r (to_fun ∘ x) . obviously) localized "notation A ` →[`:25 L `] ` B := L.hom A B" in first_order /-- An embedding of first-order structures is an embedding that commutes with the interpretations of functions and relations. -/ protected structure embedding extends M ↪ N := (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r (to_fun ∘ x) ↔ rel_map r x . obviously) localized "notation A ` ↪[`:25 L `] ` B := L.embedding A B" in first_order /-- An equivalence of first-order structures is an equivalence that commutes with the interpretations of functions and relations. -/ protected structure equiv extends M ≃ N := (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r (to_fun ∘ x) ↔ rel_map r x . obviously) localized "notation A ` ≃[`:25 L `] ` B := L.equiv A B" in first_order variables {L M N} {P : Type*} [L.Structure P] {Q : Type*} [L.Structure Q] instance : has_coe_t L.const M := ⟨λ c, fun_map c fin.elim0⟩ lemma fun_map_eq_coe_const {c : L.const} {x : fin 0 → M} : fun_map c x = c := congr rfl (funext fin.elim0) namespace hom @[simps] instance has_coe_to_fun : has_coe_to_fun (M →[L] N) := ⟨(λ _, M → N), first_order.language.hom.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : M →[L] N} : f.to_fun = (f : M → N) := rfl lemma coe_injective : @function.injective (M →[L] N) (M → N) coe_fn | f g h := by {cases f, cases g, cases h, refl} @[ext] lemma ext ⦃f g : M →[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {f g : M →[L] N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ @[simp] lemma map_fun (φ : M →[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := φ.map_fun' f x @[simp] lemma map_const (φ : M →[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M →[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r x → rel_map r (φ ∘ x) := φ.map_rel' r x variables (L) (M) /-- The identity map from a structure to itself -/ @[refl] def id : M →[L] M := { to_fun := id } variables {L} {M} instance : inhabited (M →[L] M) := ⟨id L M⟩ @[simp] lemma id_apply (x : M) : id L M x = x := rfl /-- Composition of first-order homomorphisms -/ @[trans] def comp (hnp : N →[L] P) (hmn : M →[L] N) : M →[L] P := { to_fun := hnp ∘ hmn, map_rel' := λ _ _ _ h, by simp [h] } @[simp] lemma comp_apply (g : N →[L] P) (f : M →[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order homomorphisms is associative. -/ lemma comp_assoc (f : M →[L] N) (g : N →[L] P) (h : P →[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl end hom namespace embedding @[simps] instance has_coe_to_fun : has_coe_to_fun (M ↪[L] N) := ⟨(λ _, M → N), λ f, f.to_fun⟩ @[simp] lemma map_fun (φ : M ↪[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := φ.map_fun' f x @[simp] lemma map_const (φ : M ↪[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M ↪[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := φ.map_rel' r x /-- A first-order embedding is also a first-order homomorphism. -/ def to_hom (f : M ↪[L] N) : M →[L] N := { to_fun := f } @[simp] lemma coe_to_hom {f : M ↪[L] N} : (f.to_hom : M → N) = f := rfl lemma coe_injective : @function.injective (M ↪[L] N) (M → N) coe_fn | f g h := begin cases f, cases g, simp only, ext x, exact function.funext_iff.1 h x, end @[ext] lemma ext ⦃f g : M ↪[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {f g : M ↪[L] N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ lemma injective (f : M ↪[L] N) : function.injective f := f.to_embedding.injective /-- In an algebraic language, any injective homomorphism is an embedding. -/ @[simps] def of_injective [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : M ↪[L] N := { inj' := hf, map_rel' := λ n r, (is_algebraic.empty_relations n r).elim, .. f } @[simp] lemma coe_fn_of_injective [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : (of_injective hf : M → N) = f := rfl @[simp] lemma of_injective_to_hom [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : (of_injective hf).to_hom = f := by { ext, simp } variables (L) (M) /-- The identity embedding from a structure to itself -/ @[refl] def refl : M ↪[L] M := { to_embedding := function.embedding.refl M } variables {L} {M} instance : inhabited (M ↪[L] M) := ⟨refl L M⟩ @[simp] lemma refl_apply (x : M) : refl L M x = x := rfl /-- Composition of first-order embeddings -/ @[trans] def comp (hnp : N ↪[L] P) (hmn : M ↪[L] N) : M ↪[L] P := { to_fun := hnp ∘ hmn, inj' := hnp.injective.comp hmn.injective } @[simp] lemma comp_apply (g : N ↪[L] P) (f : M ↪[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order embeddings is associative. -/ lemma comp_assoc (f : M ↪[L] N) (g : N ↪[L] P) (h : P ↪[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl end embedding namespace equiv /-- The inverse of a first-order equivalence is a first-order equivalence. -/ @[symm] def symm (f : M ≃[L] N) : N ≃[L] M := { map_fun' := λ n f' x, begin simp only [equiv.to_fun_as_coe], rw [equiv.symm_apply_eq], refine eq.trans _ (f.map_fun' f' (f.to_equiv.symm ∘ x)).symm, rw [← function.comp.assoc, equiv.to_fun_as_coe, equiv.self_comp_symm, function.comp.left_id] end, map_rel' := λ n r x, begin simp only [equiv.to_fun_as_coe], refine (f.map_rel' r (f.to_equiv.symm ∘ x)).symm.trans _, rw [← function.comp.assoc, equiv.to_fun_as_coe, equiv.self_comp_symm, function.comp.left_id] end, .. f.to_equiv.symm } @[simps] instance has_coe_to_fun : has_coe_to_fun (M ≃[L] N) := ⟨(λ _, M → N), λ f, f.to_fun⟩ @[simp] lemma map_fun (φ : M ≃[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := φ.map_fun' f x @[simp] lemma map_const (φ : M ≃[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M ≃[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := φ.map_rel' r x /-- A first-order equivalence is also a first-order embedding. -/ def to_embedding (f : M ≃[L] N) : M ↪[L] N := { to_fun := f, inj' := f.to_equiv.injective } /-- A first-order equivalence is also a first-order embedding. -/ def to_hom (f : M ≃[L] N) : M →[L] N := { to_fun := f } @[simp] lemma to_embedding_to_hom (f : M ≃[L] N) : f.to_embedding.to_hom = f.to_hom := rfl @[simp] lemma coe_to_hom {f : M ≃[L] N} : (f.to_hom : M → N) = (f : M → N) := rfl @[simp] lemma coe_to_embedding (f : M ≃[L] N) : (f.to_embedding : M → N) = (f : M → N) := rfl lemma coe_injective : @function.injective (M ≃[L] N) (M → N) coe_fn | f g h := begin cases f, cases g, simp only, ext x, exact function.funext_iff.1 h x, end @[ext] lemma ext ⦃f g : M ≃[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {f g : M ≃[L] N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ lemma injective (f : M ≃[L] N) : function.injective f := f.to_embedding.injective variables (L) (M) /-- The identity equivalence from a structure to itself -/ @[refl] def refl : M ≃[L] M := { to_equiv := equiv.refl M } variables {L} {M} instance : inhabited (M ≃[L] M) := ⟨refl L M⟩ @[simp] lemma refl_apply (x : M) : refl L M x = x := rfl /-- Composition of first-order equivalences -/ @[trans] def comp (hnp : N ≃[L] P) (hmn : M ≃[L] N) : M ≃[L] P := { to_fun := hnp ∘ hmn, .. (hmn.to_equiv.trans hnp.to_equiv) } @[simp] lemma comp_apply (g : N ≃[L] P) (f : M ≃[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order homomorphisms is associative. -/ lemma comp_assoc (f : M ≃[L] N) (g : N ≃[L] P) (h : P ≃[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl end equiv section closed_under open set variables {n : ℕ} (f : L.functions n) (s : set M) /-- Indicates that a set in a given structure is a closed under a function symbol. -/ def closed_under : Prop := ∀ (x : fin n → M), (∀ i : fin n, x i ∈ s) → fun_map f x ∈ s variable (L) @[simp] lemma closed_under_univ : closed_under f (univ : set M) := λ _ _, mem_univ _ variables {L f s} {t : set M} namespace closed_under lemma inter (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ∩ t) := λ x h, mem_inter (hs x (λ i, mem_of_mem_inter_left (h i))) (ht x (λ i, mem_of_mem_inter_right (h i))) lemma inf (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ⊓ t) := hs.inter ht variables {S : set (set M)} lemma Inf (hS : ∀ s, s ∈ S → closed_under f s) : closed_under f (Inf S) := λ x h s hs, hS s hs x (λ i, h i s hs) end closed_under end closed_under variables (L) (M) /-- A substructure of a structure `M` is a set closed under application of function symbols. -/ structure substructure := (carrier : set M) (fun_mem : ∀{n}, ∀ (f : L.functions n), closed_under f carrier) variables {L} {M} namespace substructure instance : set_like (L.substructure M) M := ⟨substructure.carrier, λ p q h, by cases p; cases q; congr'⟩ /-- See Note [custom simps projection] -/ def simps.coe (S : L.substructure M) : set M := S initialize_simps_projections substructure (carrier → coe) @[simp] lemma mem_carrier {s : L.substructure M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two substructures are equal if they have the same elements. -/ @[ext] theorem ext {S T : L.substructure M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy a substructure replacing `carrier` with a set that is equal to it. -/ protected def copy (S : L.substructure M) (s : set M) (hs : s = S) : L.substructure M := { carrier := s, fun_mem := λ n f, hs.symm ▸ (S.fun_mem f) } variable {S : L.substructure M} @[simp] lemma coe_copy {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl lemma copy_eq {s : set M} (hs : s = S) : S.copy s hs = S := set_like.coe_injective hs lemma const_mem {c : L.const} : ↑c ∈ S := mem_carrier.2 (S.fun_mem c _ fin.elim0) /-- The substructure `M` of the structure `M`. -/ instance : has_top (L.substructure M) := ⟨{ carrier := set.univ, fun_mem := λ n f x h, set.mem_univ _ }⟩ instance : inhabited (L.substructure M) := ⟨⊤⟩ @[simp] lemma mem_top (x : M) : x ∈ (⊤ : L.substructure M) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : L.substructure M) : set M) = set.univ := rfl /-- The inf of two substructures is their intersection. -/ instance : has_inf (L.substructure M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, fun_mem := λ n f, (S₁.fun_mem f).inf (S₂.fun_mem f) }⟩ @[simp] lemma coe_inf (p p' : L.substructure M) : ((p ⊓ p' : L.substructure M) : set M) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : L.substructure M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (L.substructure M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, fun_mem := λ n f, closed_under.Inf begin rintro _ ⟨t, rfl⟩, by_cases h : t ∈ s, { simpa [h] using t.fun_mem f }, { simp [h] } end }⟩ @[simp, norm_cast] lemma coe_Inf (S : set (L.substructure M)) : ((Inf S : L.substructure M) : set M) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (L.substructure M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff lemma mem_infi {ι : Sort*} {S : ι → L.substructure M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → L.substructure M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Substructures of a structure form a complete lattice. -/ instance : complete_lattice (L.substructure M) := { le := (≤), lt := (<), top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (L.substructure M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from set_like.coe_subset_coe) is_glb_binfi } variable (L) /-- The `L.substructure` generated by a set. -/ def closure : lower_adjoint (coe : L.substructure M → set M) := ⟨λ s, Inf {S | s ⊆ S}, λ s S, ⟨set.subset.trans (λ x hx, mem_Inf.2 $ λ S hS, hS hx), λ h, Inf_le h⟩⟩ variables {L} {s : set M} lemma mem_closure {x : M} : x ∈ closure L s ↔ ∀ S : L.substructure M, s ⊆ S → x ∈ S := mem_Inf /-- The substructure generated by a set includes the set. -/ @[simp] lemma subset_closure : s ⊆ closure L s := (closure L).le_closure s @[simp] lemma closed (S : L.substructure M) : (closure L).closed (S : set M) := congr rfl ((closure L).eq_of_le set.subset.rfl (λ x xS, mem_closure.2 (λ T hT, hT xS))) open set /-- A substructure `S` includes `closure L s` if and only if it includes `s`. -/ @[simp] lemma closure_le : closure L s ≤ S ↔ s ⊆ S := (closure L).closure_le_closed_iff_le s S.closed /-- Substructure closure of a set is monotone in its argument: if `s ⊆ t`, then `closure L s ≤ closure L t`. -/ lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure L s ≤ closure L t := (closure L).monotone h lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure L s) : closure L s = S := (closure L).eq_of_le h₁ h₂ variable (S) /-- An induction principle for closure membership. If `p` holds for all elements of `s`, and is preserved under function symbols, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure L s) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := (@closure_le L M _ ⟨set_of p, λ n, Hfun⟩ _).2 Hs h /-- If `s` is a dense set in a structure `M`, `substructure.closure L s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, and verify that `p` is preserved under function symbols. -/ @[elab_as_eliminator] lemma dense_induction {p : M → Prop} (x : M) {s : set M} (hs : closure L s = ⊤) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := have ∀ x ∈ closure L s, p x, from λ x hx, closure_induction hx Hs (λ n, Hfun), by simpa [hs] using this x variables (L) (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure L M _) coe := { choice := λ s _, closure L s, gc := (closure L).gc, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variables {L} {M} /-- Closure of a substructure `S` equals `S`. -/ @[simp] lemma closure_eq : closure L (S : set M) = S := (substructure.gi L M).l_u_eq S @[simp] lemma closure_empty : closure L (∅ : set M) = ⊥ := (substructure.gi L M).gc.l_bot @[simp] lemma closure_univ : closure L (univ : set M) = ⊤ := @coe_top L M _ ▸ closure_eq ⊤ lemma closure_union (s t : set M) : closure L (s ∪ t) = closure L s ⊔ closure L t := (substructure.gi L M).gc.l_sup lemma closure_Union {ι} (s : ι → set M) : closure L (⋃ i, s i) = ⨆ i, closure L (s i) := (substructure.gi L M).gc.l_supr /-! ### `comap` and `map` -/ /-- The preimage of a substructure along a homomorphism is a substructure. -/ @[simps] def comap (φ : M →[L] N) (S : L.substructure N) : L.substructure M := { carrier := (φ ⁻¹' S), fun_mem := λ n f x hx, begin rw [mem_preimage, φ.map_fun], exact S.fun_mem f (φ ∘ x) hx, end } @[simp] lemma mem_comap {S : L.substructure N} {f : M →[L] N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl lemma comap_comap (S : L.substructure P) (g : N →[L] P) (f : M →[L] N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp] lemma comap_id (S : L.substructure P) : S.comap (hom.id _ _) = S := ext (by simp) /-- The image of a substructure along a homomorphism is a substructure. -/ @[simps] def map (φ : M →[L] N) (S : L.substructure M) : L.substructure N := { carrier := (φ '' S), fun_mem := λ n f x hx, (mem_image _ _ _).1 ⟨fun_map f (λ i, classical.some (hx i)), S.fun_mem f _ (λ i, (classical.some_spec (hx i)).1), begin simp only [hom.map_fun, set_like.mem_coe], exact congr rfl (funext (λ i, (classical.some_spec (hx i)).2)), end⟩ } @[simp] lemma mem_map {f : M →[L] N} {S : L.substructure M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex lemma mem_map_of_mem (f : M →[L] N) {S : L.substructure M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx lemma apply_coe_mem_map (f : M →[L] N) (S : L.substructure M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop lemma map_map (g : N →[L] P) (f : M →[L] N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ lemma map_le_iff_le_comap {f : M →[L] N} {S : L.substructure M} {T : L.substructure N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff lemma gc_map_comap (f : M →[L] N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap lemma map_le_of_le_comap {T : L.substructure N} {f : M →[L] N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le lemma le_comap_of_map_le {T : L.substructure N} {f : M →[L] N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u lemma le_comap_map {f : M →[L] N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ lemma map_comap_le {S : L.substructure N} {f : M →[L] N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ lemma monotone_map {f : M →[L] N} : monotone (map f) := (gc_map_comap f).monotone_l lemma monotone_comap {f : M →[L] N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp] lemma map_comap_map {f : M →[L] N} : ((S.map f).comap f).map f = S.map f := congr_fun ((gc_map_comap f).l_u_l_eq_l) _ @[simp] lemma comap_map_comap {S : L.substructure N} {f : M →[L] N} : ((S.comap f).map f).comap f = S.comap f := congr_fun ((gc_map_comap f).u_l_u_eq_u) _ lemma map_sup (S T : L.substructure M) (f : M →[L] N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (S T : L.substructure N) (f : M →[L] N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : M →[L] N) : (⊥ : L.substructure M).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : M →[L] N) : (⊤ : L.substructure N).comap f = ⊤ := (gc_map_comap f).u_top @[simp] lemma map_id (S : L.substructure M) : S.map (hom.id L M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →[L] N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : L.substructure M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : L.substructure M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → L.substructure M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : L.substructure M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → L.substructure M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : L.substructure M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →[L] N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : L.substructure N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → L.substructure N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → L.substructure N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : L.substructure N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion instance induced_Structure {S : L.substructure M} : L.Structure S := { fun_map := λ n f x, ⟨fun_map f (λ i, x i), S.fun_mem f (λ i, x i) (λ i, (x i).2)⟩, rel_map := λ n r x, rel_map r (λ i, (x i : M)) } /-- The natural embedding of an `L.substructure` of `M` into `M`. -/ def subtype (S : L.substructure M) : S ↪[L] M := { to_fun := coe, inj' := subtype.coe_injective } @[simp] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- An induction principle on elements of the type `substructure.closure L s`. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. The difference with `substructure.closure_induction` is that this acts on the subtype. -/ @[elab_as_eliminator] lemma closure_induction' (s : set M) {p : closure L s → Prop} (Hs : ∀ x (h : x ∈ s), p ⟨x, subset_closure h⟩) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) (x : closure L s) : p x := subtype.rec_on x $ λ x hx, begin refine exists.elim _ (λ (hx : x ∈ closure L s) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hs x hx⟩) (λ n f x hx, ⟨(closure L s).fun_mem f _ (λ i, classical.some (hx i)), Hfun f (λ i, ⟨x i, classical.some (hx i)⟩) (λ i, classical.some_spec (hx i))⟩), end end substructure namespace hom open substructure /-- The substructure of elements `x : M` such that `f x = g x` -/ def eq_locus (f g : M →[L] N) : substructure L M := { carrier := {x : M | f x = g x}, fun_mem := λ n fn x hx, by { have h : f ∘ x = g ∘ x := by { ext, repeat {rw function.comp_apply}, apply hx, }, simp [h], } } /-- If two `L.hom`s are equal on a set, then they are equal on its substructure closure. -/ lemma eq_on_closure {f g : M →[L] N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure L s) := show closure L s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_top {f g : M →[L] N} (h : set.eq_on f g (⊤ : substructure L M)) : f = g := ext $ λ x, h trivial variable {s : set M} lemma eq_of_eq_on_dense (hs : closure L s = ⊤) {f g : M →[L] N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h end hom end language end first_order
4fc44e1443c3b47caa0c002f85ec26eb556e0326
037dba89703a79cd4a4aec5e959818147f97635d
/src/2022/sets/sheet1.lean
d7363f12eef79f4113ecd21795733da942724859
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
2,504
lean
/- Copyright (c) 2022 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard -/ import tactic -- imports all the Lean tactics /-! # Sets in Lean, example sheet 1 : "forall" (`∀`) A lot of questions about sets can be reduced to questions about logic. We explain how to do this in Lean. All the sets we consider on this and the next few sheets will all be subsets of an underlying set `X`, which is our "universe" where all the maths we do will take place. This underlying set `X` is called a "type" in Lean. It plays the role of a "universe" -- every element we consider will always be an element of `X`, and every subset we consider will be a subset of `X`. Notation: elements of `X` are called *terms* of the type `X` and the notation is `x : X`. All the variables `x`, `y` and `z` which appear in this sheet will always be terms of type `X`, i.e. elements of the set `X` if you want to think set-theoretically. All the sets `A`, `B`, `C` etc we consider will be subsets of `X`. If `x : X` then `x` may or may not be an element of `A`, `B`, `C`, but it will always be an element of `X`. ## Tactics Tactics you will need to know for this sheet: * `intro` (it works for `∀` goals as well as for `P → Q`) * `specialize` ### The `intro` tactic If the goal is `∀ x, P(x)` then `intro a,` lets `a` be an arbitrary element of `X` (or, more precisely, an arbitrary term of type `X`) and changes the goal to `P(a)`. ### The `specialize` tactic If we have a hypothesis `h : ∀ x, P(x)` and we have a term `a : X` then `specialize h a,` changes `h` to `h : P(a)`. -/ -- set up variables variables (X : Type) -- Everything will be a subset of `X` (A B C D E : set X) -- A,B,C,D,E are subsets of `X` (x y z : X) -- x,y,z are elements of `X` or, more precisely, terms of type `X` -- You can start this one with `intro x` example : ∀ x : X, x ∈ A → x ∈ A := begin sorry, end example : ∀ x : X, (x ∈ A ∧ x ∈ B) → x ∈ A := begin sorry end -- for this one if you start with `intros h x` then you might well -- need `specialize h x` later on. This is how to use hypotheses with `∀` in. example : (∀ x, x ∈ A ∧ x ∈ B) → (∀ x, x ∈ A) := begin sorry end example : (∀ x, x ∈ A ∧ x ∈ B) → (∀ y, y ∈ B ∧ y ∈ A) := begin sorry end example : (∀ x, x ∈ A → x ∈ B) → (∀ y, y ∈ B → y ∈ C) → (∀ z, z ∈ A → z ∈ C) := begin sorry end
25fd867cc84952c3a7dc4250a63b009a7e73254c
7cef822f3b952965621309e88eadf618da0c8ae9
/src/algebra/ordered_ring.lean
6e3a36b7e565398fb9f32bc0ff8383fc7276eef4
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
21,768
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.split_ifs order.basic algebra.order algebra.ordered_group algebra.ring data.nat.cast universe u variable {α : Type u} -- `mul_nonneg` and `mul_pos` in core are stated in terms of `≥` and `>`, so we restate them here -- for use in syntactic tactics (e.g. `simp` and `rw`). lemma mul_nonneg' [ordered_semiring α] {a b : α} : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := mul_nonneg lemma mul_pos' [ordered_semiring α] {a b : α} (ha : 0 < a) (hb : 0 < b) : 0 < a * b := mul_pos ha hb section linear_ordered_semiring variable [linear_ordered_semiring α] /-- `0 < 2`: an alternative version of `two_pos` that only assumes `linear_ordered_semiring`. -/ lemma zero_lt_two : (0:α) < 2 := by { rw [← zero_add (0:α), bit0], exact add_lt_add zero_lt_one zero_lt_one } @[simp] lemma mul_le_mul_left {a b c : α} (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_left h' h, λ h', mul_le_mul_of_nonneg_left h' (le_of_lt h)⟩ @[simp] lemma mul_le_mul_right {a b c : α} (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_right h' h, λ h', mul_le_mul_of_nonneg_right h' (le_of_lt h)⟩ @[simp] lemma mul_lt_mul_left {a b c : α} (h : 0 < c) : c * a < c * b ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_left h' (le_of_lt h), λ h', mul_lt_mul_of_pos_left h' h⟩ @[simp] lemma mul_lt_mul_right {a b c : α} (h : 0 < c) : a * c < b * c ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_right h' (le_of_lt h), λ h', mul_lt_mul_of_pos_right h' h⟩ lemma mul_lt_mul'' {a b c d : α} (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d := (lt_or_eq_of_le h4).elim (λ b0, mul_lt_mul h1 (le_of_lt h2) b0 (le_trans h3 (le_of_lt h1))) (λ b0, by rw [← b0, mul_zero]; exact mul_pos (lt_of_le_of_lt h3 h1) (lt_of_le_of_lt h4 h2)) lemma le_mul_iff_one_le_left {a b : α} (hb : 0 < b) : b ≤ a * b ↔ 1 ≤ a := suffices 1 * b ≤ a * b ↔ 1 ≤ a, by rwa one_mul at this, mul_le_mul_right hb lemma lt_mul_iff_one_lt_left {a b : α} (hb : 0 < b) : b < a * b ↔ 1 < a := suffices 1 * b < a * b ↔ 1 < a, by rwa one_mul at this, mul_lt_mul_right hb lemma le_mul_iff_one_le_right {a b : α} (hb : 0 < b) : b ≤ b * a ↔ 1 ≤ a := suffices b * 1 ≤ b * a ↔ 1 ≤ a, by rwa mul_one at this, mul_le_mul_left hb lemma lt_mul_iff_one_lt_right {a b : α} (hb : 0 < b) : b < b * a ↔ 1 < a := suffices b * 1 < b * a ↔ 1 < a, by rwa mul_one at this, mul_lt_mul_left hb lemma lt_mul_of_one_lt_right' {a b : α} (hb : 0 < b) : 1 < a → b < b * a := (lt_mul_iff_one_lt_right hb).2 lemma le_mul_of_one_le_right' {a b : α} (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ b * a := suffices b * 1 ≤ b * a, by rwa mul_one at this, mul_le_mul_of_nonneg_left h hb lemma le_mul_of_one_le_left' {a b : α} (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a * b := suffices 1 * b ≤ a * b, by rwa one_mul at this, mul_le_mul_of_nonneg_right h hb theorem mul_nonneg_iff_right_nonneg_of_pos {a b : α} (h : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b := ⟨assume : 0 ≤ b * a, nonneg_of_mul_nonneg_right this h, assume : 0 ≤ b, mul_nonneg this $ le_of_lt h⟩ lemma bit1_pos {a : α} (h : 0 ≤ a) : 0 < bit1 a := lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one lemma bit1_pos' {a : α} (h : 0 < a) : 0 < bit1 a := bit1_pos (le_of_lt h) lemma lt_add_one (a : α) : a < a + 1 := lt_add_of_le_of_pos (le_refl _) zero_lt_one lemma lt_one_add (a : α) : a < 1 + a := by { rw [add_comm], apply lt_add_one } lemma one_lt_two : 1 < (2 : α) := lt_add_one _ lemma one_lt_mul {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := (one_mul (1 : α)) ▸ mul_lt_mul' ha hb zero_le_one (lt_of_lt_of_le zero_lt_one ha) lemma mul_le_one {a b : α} (ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 := begin rw ← one_mul (1 : α), apply mul_le_mul; {assumption <|> apply zero_le_one} end lemma one_lt_mul_of_le_of_lt {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul' ha hb zero_le_one (lt_of_lt_of_le zero_lt_one ha) lemma one_lt_mul_of_lt_of_le {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul ha hb zero_lt_one (le_trans zero_le_one (le_of_lt ha)) lemma mul_le_of_le_one_right {a b : α} (ha : 0 ≤ a) (hb1 : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_of_nonneg_left hb1 ha ... = a : mul_one a lemma mul_le_of_le_one_left {a b : α} (hb : 0 ≤ b) (ha1 : a ≤ 1) : a * b ≤ b := calc a * b ≤ 1 * b : mul_le_mul ha1 (le_refl b) hb zero_le_one ... = b : one_mul b lemma mul_lt_one_of_nonneg_of_lt_one_left {a b : α} (ha0 : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := calc a * b ≤ a : mul_le_of_le_one_right ha0 hb ... < 1 : ha lemma mul_lt_one_of_nonneg_of_lt_one_right {a b : α} (ha : a ≤ 1) (hb0 : 0 ≤ b) (hb : b < 1) : a * b < 1 := calc a * b ≤ b : mul_le_of_le_one_left hb0 ha ... < 1 : hb lemma mul_le_iff_le_one_left {a b : α} (hb : 0 < b) : a * b ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).2 (not_lt_of_ge h)), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).1 (not_lt_of_ge h)) ⟩ lemma mul_lt_iff_lt_one_left {a b : α} (hb : 0 < b) : a * b < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).2 (not_le_of_gt h)), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).1 (not_le_of_gt h)) ⟩ lemma mul_le_iff_le_one_right {a b : α} (hb : 0 < b) : b * a ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).2 (not_lt_of_ge h)), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).1 (not_lt_of_ge h)) ⟩ lemma mul_lt_iff_lt_one_right {a b : α} (hb : 0 < b) : b * a < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).2 (not_le_of_gt h)), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).1 (not_le_of_gt h)) ⟩ lemma nonpos_of_mul_nonneg_left {a b : α} (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 := le_of_not_gt (λ ha, absurd h (not_le_of_gt (mul_neg_of_pos_of_neg ha hb))) lemma nonpos_of_mul_nonneg_right {a b : α} (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 := le_of_not_gt (λ hb, absurd h (not_le_of_gt (mul_neg_of_neg_of_pos ha hb))) lemma neg_of_mul_pos_left {a b : α} (h : 0 < a * b) (hb : b ≤ 0) : a < 0 := lt_of_not_ge (λ ha, absurd h (not_lt_of_ge (mul_nonpos_of_nonneg_of_nonpos ha hb))) lemma neg_of_mul_pos_right {a b : α} (h : 0 < a * b) (ha : a ≤ 0) : b < 0 := lt_of_not_ge (λ hb, absurd h (not_lt_of_ge (mul_nonpos_of_nonpos_of_nonneg ha hb))) end linear_ordered_semiring section decidable_linear_ordered_semiring variable [decidable_linear_ordered_semiring α] @[simp] lemma decidable.mul_le_mul_left {a b c : α} (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_left h @[simp] lemma decidable.mul_le_mul_right {a b c : α} (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_right h end decidable_linear_ordered_semiring -- The proof doesn't need commutativity but we have no `decidable_linear_ordered_ring` @[simp] lemma abs_two [decidable_linear_ordered_comm_ring α] : abs (2:α) = 2 := abs_of_pos $ by refine zero_lt_two @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_no_top_order {α : Type*} [linear_ordered_semiring α] : no_top_order α := ⟨assume a, ⟨a + 1, lt_add_of_pos_right _ zero_lt_one⟩⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_no_bot_order {α : Type*} [linear_ordered_ring α] : no_bot_order α := ⟨assume a, ⟨a - 1, sub_lt_iff_lt_add.mpr $ lt_add_of_pos_right _ zero_lt_one⟩⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_domain [s : linear_ordered_ring α] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_ring.eq_zero_or_eq_zero_of_mul_eq_zero α s, ..s } section linear_ordered_ring variable [linear_ordered_ring α] @[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_left h' h, λ h', mul_le_mul_of_nonpos_left h' (le_of_lt h)⟩ @[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_right h' h, λ h', mul_le_mul_of_nonpos_right h' (le_of_lt h)⟩ @[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_left_of_neg h) @[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_right_of_neg h) lemma sub_one_lt (a : α) : a - 1 < a := sub_lt_iff_lt_add.2 (lt_add_one a) lemma mul_self_pos {a : α} (ha : a ≠ 0) : 0 < a * a := by rcases lt_trichotomy a 0 with h|h|h; [exact mul_pos_of_neg_of_neg h h, exact (ha h).elim, exact mul_pos h h] lemma mul_self_le_mul_self_of_le_of_neg_le {x y : α} (h₁ : x ≤ y) (h₂ : -x ≤ y) : x * x ≤ y * y := begin cases le_total 0 x, { exact mul_self_le_mul_self h h₁ }, { rw ← neg_mul_neg, exact mul_self_le_mul_self (neg_nonneg_of_nonpos h) h₂ } end lemma nonneg_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a := le_of_not_gt (λ ha, absurd h (not_le_of_gt (mul_pos_of_neg_of_neg ha hb))) lemma nonneg_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b := le_of_not_gt (λ hb, absurd h (not_le_of_gt (mul_pos_of_neg_of_neg ha hb))) lemma pos_of_mul_neg_left {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a := lt_of_not_ge (λ ha, absurd h (not_lt_of_ge (mul_nonneg_of_nonpos_of_nonpos ha hb))) lemma pos_of_mul_neg_right {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b := lt_of_not_ge (λ hb, absurd h (not_lt_of_ge (mul_nonneg_of_nonpos_of_nonpos ha hb))) /- The sum of two squares is zero iff both elements are zero. -/ lemma mul_self_add_mul_self_eq_zero {x y : α} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := begin split; intro h, swap, { rcases h with ⟨rfl, rfl⟩, simp }, have : y * y ≤ 0, { rw [← h], apply le_add_of_nonneg_left (mul_self_nonneg x) }, have : y * y = 0 := le_antisymm this (mul_self_nonneg y), have hx : x = 0, { rwa [this, add_zero, mul_self_eq_zero] at h }, rw mul_self_eq_zero at this, split; assumption end end linear_ordered_ring set_option old_structure_cmd true section prio set_option default_priority 100 -- see Note [default priority] /-- Extend `nonneg_comm_group` to support ordered rings specified by their nonnegative elements -/ class nonneg_ring (α : Type*) extends ring α, zero_ne_one_class α, nonneg_comm_group α := (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (mul_pos : ∀ {a b}, pos a → pos b → pos (a * b)) /-- Extend `nonneg_comm_group` to support linearly ordered rings specified by their nonnegative elements -/ class linear_nonneg_ring (α : Type*) extends domain α, nonneg_comm_group α := (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (nonneg_total : ∀ a, nonneg a ∨ nonneg (-a)) end prio namespace nonneg_ring open nonneg_comm_group variable [s : nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_ordered_ring : ordered_ring α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, add_lt_add_left := @add_lt_add_left _ _, add_le_add_left := @add_le_add_left _ _, mul_nonneg := λ a b, by simp [nonneg_def.symm]; exact mul_nonneg, mul_pos := λ a b, by simp [pos_def.symm]; exact mul_pos, ..s } def to_linear_nonneg_ring (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : linear_nonneg_ring α := { nonneg_total := nonneg_total, eq_zero_or_eq_zero_of_mul_eq_zero := suffices ∀ {a} b : α, nonneg a → a * b = 0 → a = 0 ∨ b = 0, from λ a b, (nonneg_total a).elim (this b) (λ na, by simpa using this b na), suffices ∀ {a b : α}, nonneg a → nonneg b → a * b = 0 → a = 0 ∨ b = 0, from λ a b na, (nonneg_total b).elim (this na) (λ nb, by simpa using this na nb), λ a b na nb z, classical.by_cases (λ nna : nonneg (-a), or.inl (nonneg_antisymm na nna)) (λ pa, classical.by_cases (λ nnb : nonneg (-b), or.inr (nonneg_antisymm nb nnb)) (λ pb, absurd z $ ne_of_gt $ pos_def.1 $ mul_pos ((pos_iff _ _).2 ⟨na, pa⟩) ((pos_iff _ _).2 ⟨nb, pb⟩))), ..s } end nonneg_ring namespace linear_nonneg_ring open nonneg_comm_group variable [s : linear_nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_nonneg_ring : nonneg_ring α := { mul_pos := λ a b pa pb, let ⟨a1, a2⟩ := (pos_iff α a).1 pa, ⟨b1, b2⟩ := (pos_iff α b).1 pb in have ab : nonneg (a * b), from mul_nonneg a1 b1, (pos_iff α _).2 ⟨ab, λ hn, have a * b = 0, from nonneg_antisymm ab hn, (eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).elim (ne_of_gt (pos_def.1 pa)) (ne_of_gt (pos_def.1 pb))⟩, ..s } @[priority 100] -- see Note [lower instance priority] instance to_linear_order : linear_order α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := nonneg_total_iff.1 nonneg_total, ..s } @[priority 100] -- see Note [lower instance priority] instance to_linear_ordered_ring : linear_ordered_ring α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := @le_total _ _, add_lt_add_left := @add_lt_add_left _ _, add_le_add_left := @add_le_add_left _ _, mul_nonneg := by simp [nonneg_def.symm]; exact @mul_nonneg _ _, mul_pos := by simp [pos_def.symm]; exact @nonneg_ring.mul_pos _ _, zero_lt_one := lt_of_not_ge $ λ (h : nonneg (0 - 1)), begin rw [zero_sub] at h, have := mul_nonneg h h, simp at this, exact zero_ne_one _ (nonneg_antisymm this h).symm end, ..s } @[priority 80] -- see Note [lower instance priority] instance to_decidable_linear_ordered_comm_ring [decidable_pred (@nonneg α _)] [comm : @is_commutative α (*)] : decidable_linear_ordered_comm_ring α := { decidable_le := by apply_instance, decidable_eq := by apply_instance, decidable_lt := by apply_instance, mul_comm := is_commutative.comm (*), ..@linear_nonneg_ring.to_linear_ordered_ring _ s } end linear_nonneg_ring section prio set_option default_priority 100 -- see Note [default priority] class canonically_ordered_comm_semiring (α : Type*) extends canonically_ordered_monoid α, comm_semiring α, zero_ne_one_class α := (mul_eq_zero_iff (a b : α) : a * b = 0 ↔ a = 0 ∨ b = 0) end prio namespace canonically_ordered_semiring open canonically_ordered_monoid variable [canonically_ordered_comm_semiring α] lemma mul_le_mul {a b c d : α} (hab : a ≤ b) (hcd : c ≤ d) : a * c ≤ b * d := begin rcases (le_iff_exists_add _ _).1 hab with ⟨b, rfl⟩, rcases (le_iff_exists_add _ _).1 hcd with ⟨d, rfl⟩, suffices : a * c ≤ a * c + (a * d + b * c + b * d), by simpa [mul_add, add_mul], exact (le_iff_exists_add _ _).2 ⟨_, rfl⟩ end /-- A version of `zero_lt_one : 0 < 1` for a `canonically_ordered_comm_semiring`. -/ lemma zero_lt_one : (0:α) < 1 := lt_of_le_of_ne (zero_le 1) zero_ne_one lemma mul_pos {a b : α} : 0 < a * b ↔ (0 < a) ∧ (0 < b) := by simp only [zero_lt_iff_ne_zero, ne.def, canonically_ordered_comm_semiring.mul_eq_zero_iff, not_or_distrib] end canonically_ordered_semiring instance : canonically_ordered_comm_semiring ℕ := { le_iff_exists_add := assume a b, ⟨assume h, let ⟨c, hc⟩ := nat.le.dest h in ⟨c, hc.symm⟩, assume ⟨c, hc⟩, hc.symm ▸ nat.le_add_right _ _⟩, zero_ne_one := ne_of_lt zero_lt_one, mul_eq_zero_iff := assume a b, iff.intro nat.eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}), bot := 0, bot_le := nat.zero_le, .. (infer_instance : ordered_comm_monoid ℕ), .. (infer_instance : linear_ordered_semiring ℕ), .. (infer_instance : comm_semiring ℕ) } namespace with_top variables [canonically_ordered_comm_semiring α] [decidable_eq α] instance : mul_zero_class (with_top α) := { zero := 0, mul := λm n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)), zero_mul := assume a, if_pos $ or.inl rfl, mul_zero := assume a, if_pos $ or.inr rfl } instance : has_one (with_top α) := ⟨↑(1:α)⟩ lemma mul_def {a b : with_top α} : a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl @[simp] theorem top_ne_zero [partial_order α] : ⊤ ≠ (0 : with_top α) . @[simp] theorem zero_ne_top [partial_order α] : (0 : with_top α) ≠ ⊤ . @[simp] theorem coe_eq_zero [partial_order α] {a : α} : (a : with_top α) = 0 ↔ a = 0 := iff.intro (assume h, match a, h with _, rfl := rfl end) (assume h, h.symm ▸ rfl) @[simp] theorem zero_eq_coe [partial_order α] {a : α} : 0 = (a : with_top α) ↔ a = 0 := by rw [eq_comm, coe_eq_zero] @[simp] theorem coe_zero [partial_order α] : ↑(0 : α) = (0 : with_top α) := rfl @[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ := top_mul top_ne_zero lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b := decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha, decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb, by simp [*, mul_def]; refl lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b)) | none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤, by simp [hb] | (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm private lemma comm (a b : with_top α) : a * b = b * a := begin by_cases ha : a = 0, { simp [ha] }, by_cases hb : b = 0, { simp [hb] }, simp [ha, hb, mul_def, option.bind_comm a b, mul_comm] end @[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := begin have H : ∀x:α, (¬x = 0) ↔ (⊤ : with_top α) * ↑x = ⊤ := λx, ⟨λhx, by simp [top_mul, hx], λhx f, by simpa [f] using hx⟩, cases a; cases b; simp [none_eq_top, top_mul, coe_ne_top, some_eq_coe, coe_mul.symm], { rw [H b] }, { rw [H a, comm] } end private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c := begin cases c, { show (a + b) * ⊤ = a * ⊤ + b * ⊤, by_cases ha : a = 0; simp [ha] }, { show (a + b) * c = a * c + b * c, by_cases hc : c = 0, { simp [hc] }, simp [mul_coe hc], cases a; cases b, repeat { refl <|> exact congr_arg some (add_mul _ _ _) } } end private lemma mul_eq_zero (a b : with_top α) : a * b = 0 ↔ a = 0 ∨ b = 0 := by cases a; cases b; dsimp [mul_def]; split_ifs; simp [*, none_eq_top, some_eq_coe, canonically_ordered_comm_semiring.mul_eq_zero_iff] at * private lemma assoc (a b c : with_top α) : (a * b) * c = a * (b * c) := begin cases a, { by_cases hb : b = 0; by_cases hc : c = 0; simp [*, none_eq_top, mul_eq_zero b c] }, cases b, { by_cases ha : a = 0; by_cases hc : c = 0; simp [*, none_eq_top, some_eq_coe, mul_eq_zero ↑a c] }, cases c, { by_cases ha : a = 0; by_cases hb : b = 0; simp [*, none_eq_top, some_eq_coe, mul_eq_zero ↑a ↑b] }, simp [some_eq_coe, coe_mul.symm, mul_assoc] end private lemma one_mul' : ∀a : with_top α, 1 * a = a | none := show ((1:α) : with_top α) * ⊤ = ⊤, by simp [-with_bot.coe_one] | (some a) := show ((1:α) : with_top α) * a = a, by simp [coe_mul.symm, -with_bot.coe_one] instance [canonically_ordered_comm_semiring α] [decidable_eq α] : canonically_ordered_comm_semiring (with_top α) := { one := (1 : α), right_distrib := distrib', left_distrib := assume a b c, by rw [comm, distrib', comm b, comm c]; refl, mul_assoc := assoc, mul_comm := comm, mul_eq_zero_iff := mul_eq_zero, one_mul := one_mul', mul_one := assume a, by rw [comm, one_mul'], zero_ne_one := assume h, @zero_ne_one α _ $ option.some.inj h, .. with_top.add_comm_monoid, .. with_top.mul_zero_class, .. with_top.canonically_ordered_monoid } @[simp] lemma coe_nat : ∀(n : nat), ((n : α) : with_top α) = n | 0 := rfl | (n+1) := have (((1 : nat) : α) : with_top α) = ((1 : nat) : with_top α) := rfl, by rw [nat.cast_add, coe_add, nat.cast_add, coe_nat n, this] @[simp] lemma nat_ne_top (n : nat) : (n : with_top α ) ≠ ⊤ := by rw [←coe_nat n]; apply coe_ne_top @[simp] lemma top_ne_nat (n : nat) : (⊤ : with_top α) ≠ n := by rw [←coe_nat n]; apply top_ne_coe @[elab_as_eliminator] lemma nat_induction {P : with_top ℕ → Prop} (a : with_top ℕ) (h0 : P 0) (hsuc : ∀n:ℕ, P n → P n.succ) (htop : (∀n : ℕ, P n) → P ⊤) : P a := begin have A : ∀n:ℕ, P n, { assume n, induction n with n IH, { exact h0 }, { exact hsuc n IH } }, cases a, { exact htop A }, { exact A a } end end with_top
d9b3dd960dbc31c3b4af33f4db12ab75504ace4d
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/test/sanity_check.lean
764364636c5ad37eb93e0da9a08ea0db13fa1716
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
2,135
lean
import tactic.sanity_check def foo1 (n m : ℕ) : ℕ := n + 1 def foo2 (n m : ℕ) : m = m := by refl lemma foo3 (n m : ℕ) : ℕ := n - m lemma foo.foo (n m : ℕ) : n ≥ n := le_refl n instance bar.bar : has_add ℕ := by apply_instance -- we don't check the name of instances -- section -- local attribute [instance, priority 1001] classical.prop_decidable -- lemma foo4 : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) -- end open tactic run_cmd do let t := name × list ℕ, e ← get_env, l ← e.mfilter (λ d, return $ e.in_current_file' d.to_name && ¬ d.to_name.is_internal && ¬ d.is_auto_generated e), l2 ← fold_over_with_cond l (return ∘ check_unused_arguments), guard $ l2.length = 3, let l2 : list t := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ (⟨`foo1, [2]⟩ : t) ∈ l2, guard $ (⟨`foo2, [1]⟩ : t) ∈ l2, guard $ (⟨`foo.foo, [2]⟩ : t) ∈ l2, l2 ← fold_over_with_cond l incorrect_def_lemma, guard $ l2.length = 2, let l2 : list (name × _) := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo2, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo3, l3 ← fold_over_with_cond l dup_namespace, guard $ l3.length = 1, guard $ ∃(x ∈ l3), (x : declaration × _).1.to_name = `foo.foo, l4 ← fold_over_with_cond l illegal_constants_in_statement, guard $ l4.length = 1, guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo.foo, -- guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo4, s ← sanity_check ff, guard $ "/- (slow tests skipped) -/\n".is_suffix_of s.to_string, s2 ← sanity_check tt, guard $ s.to_string ≠ s2.to_string, skip /- check customizability and sanity_skip -/ @[sanity_skip] def bar.foo : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) meta def dummy_check (d : declaration) : tactic (option string) := return $ if d.to_name.last = "foo" then some "gotcha!" else none run_cmd do s ← sanity_check tt [(dummy_check, "found nothing", "found something")], guard $ "/- found something: -/\n#print foo.foo /- gotcha! -/\n\n".is_suffix_of s.to_string
4e0bcd38e64e87c3d062eb8c4434be965f04b48f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/gcd_monoid/finset.lean
b0857a36fe7edf5ef7ead44d77d1a80d699a6b0f
[ "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
8,623
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.finset.fold import algebra.gcd_monoid.multiset /-! # GCD and LCM operations on finsets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions - `finset.gcd` - the greatest common denominator of a `finset` of elements of a `gcd_monoid` - `finset.lcm` - the least common multiple of a `finset` of elements of a `gcd_monoid` ## Implementation notes Many of the proofs use the lemmas `gcd.def` and `lcm.def`, which relate `finset.gcd` and `finset.lcm` to `multiset.gcd` and `multiset.lcm`. TODO: simplify with a tactic and `data.finset.lattice` ## Tags finset, gcd -/ variables {α β γ : Type*} namespace finset open multiset variables [cancel_comm_monoid_with_zero α] [normalized_gcd_monoid α] /-! ### lcm -/ section lcm /-- Least common multiple of a finite set -/ def lcm (s : finset β) (f : β → α) : α := s.fold gcd_monoid.lcm 1 f variables {s s₁ s₂ : finset β} {f : β → α} lemma lcm_def : s.lcm f = (s.1.map f).lcm := rfl @[simp] lemma lcm_empty : (∅ : finset β).lcm f = 1 := fold_empty @[simp] lemma lcm_dvd_iff {a : α} : s.lcm f ∣ a ↔ (∀b ∈ s, f b ∣ a) := begin apply iff.trans multiset.lcm_dvd, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma lcm_dvd {a : α} : (∀b ∈ s, f b ∣ a) → s.lcm f ∣ a := lcm_dvd_iff.2 lemma dvd_lcm {b : β} (hb : b ∈ s) : f b ∣ s.lcm f := lcm_dvd_iff.1 dvd_rfl _ hb @[simp] lemma lcm_insert [decidable_eq β] {b : β} : (insert b s : finset β).lcm f = gcd_monoid.lcm (f b) (s.lcm f) := begin by_cases h : b ∈ s, { rw [insert_eq_of_mem h, (lcm_eq_right_iff (f b) (s.lcm f) (multiset.normalize_lcm (s.1.map f))).2 (dvd_lcm h)] }, apply fold_insert h, end @[simp] lemma lcm_singleton {b : β} : ({b} : finset β).lcm f = normalize (f b) := multiset.lcm_singleton @[simp] lemma normalize_lcm : normalize (s.lcm f) = s.lcm f := by simp [lcm_def] lemma lcm_union [decidable_eq β] : (s₁ ∪ s₂).lcm f = gcd_monoid.lcm (s₁.lcm f) (s₂.lcm f) := finset.induction_on s₁ (by rw [empty_union, lcm_empty, lcm_one_left, normalize_lcm]) $ λ a s has ih, by rw [insert_union, lcm_insert, lcm_insert, ih, lcm_assoc] theorem lcm_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a ∈ s₂, f a = g a) : s₁.lcm f = s₂.lcm g := by { subst hs, exact finset.fold_congr hfg } lemma lcm_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.lcm f ∣ s.lcm g := lcm_dvd (λ b hb, (h b hb).trans (dvd_lcm hb)) lemma lcm_mono (h : s₁ ⊆ s₂) : s₁.lcm f ∣ s₂.lcm f := lcm_dvd $ assume b hb, dvd_lcm (h hb) lemma lcm_image [decidable_eq β] {g : γ → β} (s : finset γ) : (s.image g).lcm f = s.lcm (f ∘ g) := by { classical, induction s using finset.induction with c s hc ih; simp [*] } lemma lcm_eq_lcm_image [decidable_eq α] : s.lcm f = (s.image f).lcm id := eq.symm $ lcm_image _ theorem lcm_eq_zero_iff [nontrivial α] : s.lcm f = 0 ↔ 0 ∈ f '' s := by simp only [multiset.mem_map, lcm_def, multiset.lcm_eq_zero_iff, set.mem_image, mem_coe, ← finset.mem_def] end lcm /-! ### gcd -/ section gcd /-- Greatest common divisor of a finite set -/ def gcd (s : finset β) (f : β → α) : α := s.fold gcd_monoid.gcd 0 f variables {s s₁ s₂ : finset β} {f : β → α} lemma gcd_def : s.gcd f = (s.1.map f).gcd := rfl @[simp] lemma gcd_empty : (∅ : finset β).gcd f = 0 := fold_empty lemma dvd_gcd_iff {a : α} : a ∣ s.gcd f ↔ ∀b ∈ s, a ∣ f b := begin apply iff.trans multiset.dvd_gcd, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma gcd_dvd {b : β} (hb : b ∈ s) : s.gcd f ∣ f b := dvd_gcd_iff.1 dvd_rfl _ hb lemma dvd_gcd {a : α} : (∀b ∈ s, a ∣ f b) → a ∣ s.gcd f := dvd_gcd_iff.2 @[simp] lemma gcd_insert [decidable_eq β] {b : β} : (insert b s : finset β).gcd f = gcd_monoid.gcd (f b) (s.gcd f) := begin by_cases h : b ∈ s, { rw [insert_eq_of_mem h, (gcd_eq_right_iff (f b) (s.gcd f) (multiset.normalize_gcd (s.1.map f))).2 (gcd_dvd h)] ,}, apply fold_insert h, end @[simp] lemma gcd_singleton {b : β} : ({b} : finset β).gcd f = normalize (f b) := multiset.gcd_singleton @[simp] lemma normalize_gcd : normalize (s.gcd f) = s.gcd f := by simp [gcd_def] lemma gcd_union [decidable_eq β] : (s₁ ∪ s₂).gcd f = gcd_monoid.gcd (s₁.gcd f) (s₂.gcd f) := finset.induction_on s₁ (by rw [empty_union, gcd_empty, gcd_zero_left, normalize_gcd]) $ λ a s has ih, by rw [insert_union, gcd_insert, gcd_insert, ih, gcd_assoc] theorem gcd_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a ∈ s₂, f a = g a) : s₁.gcd f = s₂.gcd g := by { subst hs, exact finset.fold_congr hfg } lemma gcd_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.gcd f ∣ s.gcd g := dvd_gcd (λ b hb, (gcd_dvd hb).trans (h b hb)) lemma gcd_mono (h : s₁ ⊆ s₂) : s₂.gcd f ∣ s₁.gcd f := dvd_gcd $ assume b hb, gcd_dvd (h hb) lemma gcd_image [decidable_eq β] {g : γ → β} (s : finset γ) : (s.image g).gcd f = s.gcd (f ∘ g) := by { classical, induction s using finset.induction with c s hc ih; simp [*] } lemma gcd_eq_gcd_image [decidable_eq α] : s.gcd f = (s.image f).gcd id := eq.symm $ gcd_image _ theorem gcd_eq_zero_iff : s.gcd f = 0 ↔ ∀ (x : β), x ∈ s → f x = 0 := begin rw [gcd_def, multiset.gcd_eq_zero_iff], split; intro h, { intros b bs, apply h (f b), simp only [multiset.mem_map, mem_def.1 bs], use b, simp [mem_def.1 bs] }, { intros a as, rw multiset.mem_map at as, rcases as with ⟨b, ⟨bs, rfl⟩⟩, apply h b (mem_def.1 bs) } end lemma gcd_eq_gcd_filter_ne_zero [decidable_pred (λ (x : β), f x = 0)] : s.gcd f = (s.filter (λ x, f x ≠ 0)).gcd f := begin classical, transitivity ((s.filter (λ x, f x = 0)) ∪ (s.filter (λ x, f x ≠ 0))).gcd f, { rw filter_union_filter_neg_eq }, rw gcd_union, transitivity gcd_monoid.gcd (0 : α) _, { refine congr (congr rfl _) rfl, apply s.induction_on, { simp }, intros a s has h, rw filter_insert, split_ifs with h1; simp [h, h1], }, simp [gcd_zero_left, normalize_gcd], end lemma gcd_mul_left {a : α} : s.gcd (λ x, a * f x) = normalize a * s.gcd f := begin classical, apply s.induction_on, { simp }, intros b t hbt h, rw [gcd_insert, gcd_insert, h, ← gcd_mul_left], apply ((normalize_associated a).mul_right _).gcd_eq_right end lemma gcd_mul_right {a : α} : s.gcd (λ x, f x * a) = s.gcd f * normalize a := begin classical, apply s.induction_on, { simp }, intros b t hbt h, rw [gcd_insert, gcd_insert, h, ← gcd_mul_right], apply ((normalize_associated a).mul_left _).gcd_eq_right end lemma extract_gcd' (f g : β → α) (hs : ∃ x, x ∈ s ∧ f x ≠ 0) (hg : ∀ b ∈ s, f b = s.gcd f * g b) : s.gcd g = 1 := ((@mul_right_eq_self₀ _ _ (s.gcd f) _).1 $ by conv_lhs { rw [← normalize_gcd, ← gcd_mul_left, ← gcd_congr rfl hg] }).resolve_right $ by {contrapose! hs, exact gcd_eq_zero_iff.1 hs} lemma extract_gcd (f : β → α) (hs : s.nonempty) : ∃ g : β → α, (∀ b ∈ s, f b = s.gcd f * g b) ∧ s.gcd g = 1 := begin classical, by_cases h : ∀ x ∈ s, f x = (0 : α), { refine ⟨λ b, 1, λ b hb, by rw [h b hb, gcd_eq_zero_iff.2 h, mul_one], _⟩, rw [gcd_eq_gcd_image, image_const hs, gcd_singleton, id, normalize_one] }, { choose g' hg using @gcd_dvd _ _ _ _ s f, have := λ b hb, _, push_neg at h, refine ⟨λ b, if hb : b ∈ s then g' hb else 0, this, extract_gcd' f _ h this⟩, rw [dif_pos hb, hg hb] }, end end gcd end finset namespace finset section is_domain variables [comm_ring α] [is_domain α] [normalized_gcd_monoid α] lemma gcd_eq_of_dvd_sub {s : finset β} {f g : β → α} {a : α} (h : ∀ x : β, x ∈ s → a ∣ f x - g x) : gcd_monoid.gcd a (s.gcd f) = gcd_monoid.gcd a (s.gcd g) := begin classical, revert h, apply s.induction_on, { simp }, intros b s bs hi h, rw [gcd_insert, gcd_insert, gcd_comm (f b), ← gcd_assoc, hi (λ x hx, h _ (mem_insert_of_mem hx)), gcd_comm a, gcd_assoc, gcd_comm a (gcd_monoid.gcd _ _), gcd_comm (g b), gcd_assoc _ _ a, gcd_comm _ a], exact congr_arg _ (gcd_eq_of_dvd_sub_right (h _ (mem_insert_self _ _))) end end is_domain end finset
00e599db5bcc0a1a9864f36dfd94963b9024a0e4
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/linear_algebra/finite_dimensional.lean
650967a0a656ff58e8531c529b540c2444392580
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
65,464
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 algebra.algebra.subalgebra import field_theory.finiteness import linear_algebra.dimension import ring_theory.principal_ideal_domain /-! # Finite dimensional vector spaces Definition and basic properties of finite dimensional vector spaces, of their dimensions, and of linear maps on such spaces. ## Main definitions Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of finite-dimensionality of `V`: - it admits a finite basis. - it is finitely generated. - it is noetherian, i.e., every subspace is finitely generated. We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of proof, it is defined using the third point of view, i.e., as `is_noetherian`. However, we prove that all these points of view are equivalent, with the following lemmas (in the namespace `finite_dimensional`): - `basis.of_vector_space_index.fintype` states that a finite-dimensional vector space has a finite basis - `finite_dimensional.fintype_basis_index` states that a basis of a finite-dimensional vector space contains finitely many basis vectors - `finite_dimensional.finset_basis` states that a finite-dimensional vector space has a basis indexed by a `finset` - `finite_dimensional.fin_basis` and `finite_dimensional.fin_basis_of_finrank_eq` are bases for finite dimensional vector spaces, where the index type is `fin` - `of_fintype_basis` states that the existence of a basis indexed by a finite type implies finite-dimensionality - `of_finset_basis` states that the existence of a basis indexed by a `finset` implies finite-dimensionality - `of_finite_basis` states that the existence of a basis indexed by a finite set implies finite-dimensionality - `iff_fg` states that the space is finite-dimensional if and only if it is finitely generated Also defined is `finrank`, the dimension of a finite dimensional space, returning a `nat`, as opposed to `module.rank`, which returns a `cardinal`. When the space has infinite dimension, its `finrank` is by convention set to `0`. Preservation of finite-dimensionality and formulas for the dimension are given for - submodules - quotients (for the dimension of a quotient, see `finrank_quotient_add_finrank`) - linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.finrank_eq` - image under a linear map (the rank-nullity formula is in `finrank_range_add_finrank_ker`) Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`, and the equivalence between left-inverse and right-inverse in `mul_eq_one_comm` and `comp_eq_id_comm`. ## Implementation notes Most results are deduced from the corresponding results for the general dimension (as a cardinal), in `dimension.lean`. Not all results have been ported yet. Much of this file could be generalised away from fields or division rings. You should not assume that there has been any effort to state lemmas as generally as possible. One of the characterizations of finite-dimensionality is in terms of finite generation. This property is currently defined only for submodules, so we express it through the fact that the maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is not very convenient to use, although there are some helper functions. However, this becomes very convenient when speaking of submodules which are finite-dimensional, as this notion coincides with the fact that the submodule is finitely generated (as a submodule of the whole space). This equivalence is proved in `submodule.fg_iff_finite_dimensional`. -/ universes u v v' w open_locale classical open cardinal submodule module function /-- `finite_dimensional` vector spaces are defined to be noetherian modules. Use `finite_dimensional.iff_fg` or `finite_dimensional.of_fintype_basis` to prove finite dimension from a conventional definition. -/ @[reducible] def finite_dimensional (K V : Type*) [division_ring K] [add_comm_group V] [module K V] := is_noetherian K V namespace finite_dimensional open is_noetherian section division_ring variables (K : Type u) (V : Type v) [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] variables (K V) -- Without this apparently redundant instance we get typeclass search errors -- in `analysis.normed_space.finite_dimension`. instance finite_dimensional_pi {ι} [fintype ι] : finite_dimensional K (ι → K) := is_noetherian_pi /-- A finite dimensional vector space over a finite field is finite -/ noncomputable def fintype_of_fintype [fintype K] [is_noetherian K V] : fintype V := module.fintype_of_fintype (finset_basis K V) variables {K V} /-- If a vector space has a finite basis, then it is finite-dimensional. -/ lemma of_fintype_basis {ι : Type w} [fintype ι] (h : basis ι K V) : finite_dimensional K V := iff_fg.2 $ ⟨⟨finset.univ.image h, by { convert h.span_eq, simp } ⟩⟩ /-- If a vector space has a basis indexed by elements of a finite set, then it is finite-dimensional. -/ lemma of_finite_basis {ι : Type w} {s : set ι} (h : basis s K V) (hs : set.finite s) : finite_dimensional K V := by haveI := hs.fintype; exact of_fintype_basis h /-- If a vector space has a finite basis, then it is finite-dimensional, finset style. -/ lemma of_finset_basis {ι : Type w} {s : finset ι} (h : basis s K V) : finite_dimensional K V := of_finite_basis h s.finite_to_set /-- A subspace of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) : finite_dimensional K S := is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_omega K V)) /-- A quotient of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) : finite_dimensional K (quotient S) := is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_quotient_le _) (dim_lt_omega K V)) /-- The rank of a module as a natural number. Defined by convention to be `0` if the space has infinite rank. For a vector space `V` over a field `K`, this is the same as the finite dimension of `V` over `K`. -/ noncomputable def finrank (K V : Type*) [division_ring K] [add_comm_group V] [module K V] : ℕ := (module.rank K V).to_nat /-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its `finrank`. -/ lemma finrank_eq_dim (K : Type u) (V : Type v) [division_ring K] [add_comm_group V] [module K V] [finite_dimensional K V] : (finrank K V : cardinal.{v}) = module.rank K V := by rw [finrank, cast_to_nat_of_lt_omega (dim_lt_omega K V)] lemma finrank_eq_of_dim_eq {n : ℕ} (h : module.rank K V = ↑ n) : finrank K V = n := begin apply_fun to_nat at h, rw to_nat_cast at h, exact_mod_cast h, end lemma finrank_of_infinite_dimensional {K V : Type*} [division_ring K] [add_comm_group V] [module K V] (h : ¬finite_dimensional K V) : finrank K V = 0 := dif_neg $ mt is_noetherian.iff_dim_lt_omega.2 h lemma finite_dimensional_of_finrank {K V : Type*} [division_ring K] [add_comm_group V] [module K V] (h : 0 < finrank K V) : finite_dimensional K V := by { contrapose h, simp [finrank_of_infinite_dimensional h] } lemma finite_dimensional_of_finrank_eq_succ {K V : Type*} [field K] [add_comm_group V] [module K V] {n : ℕ} (hn : finrank K V = n.succ) : finite_dimensional K V := finite_dimensional_of_finrank $ by rw hn; exact n.succ_pos /-- We can infer `finite_dimensional K V` in the presence of `[fact (finrank K V = n + 1)]`. Declare this as a local instance where needed. -/ lemma fact_finite_dimensional_of_finrank_eq_succ {K V : Type*} [field K] [add_comm_group V] [module K V] (n : ℕ) [fact (finrank K V = n + 1)] : finite_dimensional K V := finite_dimensional_of_finrank $ by convert nat.succ_pos n; apply fact.out /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. -/ lemma finrank_eq_card_basis {ι : Type w} [fintype ι] (h : basis ι K V) : finrank K V = fintype.card ι := begin haveI : finite_dimensional K V := of_fintype_basis h, have := dim_eq_card_basis h, rw ← finrank_eq_dim at this, exact_mod_cast this end /-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its `finrank`. -/ lemma finrank_eq_card_basis' [finite_dimensional K V] {ι : Type w} (h : basis ι K V) : (finrank K V : cardinal.{w}) = cardinal.mk ι := begin haveI : fintype ι := fintype_basis_index h, rw [cardinal.fintype_card, finrank_eq_card_basis h] end /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. This lemma uses a `finset` instead of indexed types. -/ lemma finrank_eq_card_finset_basis {ι : Type w} {b : finset ι} (h : basis.{w} b K V) : finrank K V = finset.card b := by rw [finrank_eq_card_basis h, fintype.card_coe] variables (K V) /-- A finite dimensional vector space has a basis indexed by `fin (finrank K V)`. -/ noncomputable def fin_basis [finite_dimensional K V] : basis (fin (finrank K V)) K V := have h : fintype.card (finset_basis_index K V) = finrank K V, from (finrank_eq_card_basis (finset_basis K V)).symm, (finset_basis K V).reindex (fintype.equiv_fin_of_card_eq h) /-- An `n`-dimensional vector space has a basis indexed by `fin n`. -/ noncomputable def fin_basis_of_finrank_eq [finite_dimensional K V] {n : ℕ} (hn : finrank K V = n) : basis (fin n) K V := (fin_basis K V).reindex (fin.cast hn).to_equiv variables {K V} /-- A module with dimension 1 has a basis with one element. -/ noncomputable def basis_unique (ι : Type*) [unique ι] (h : finrank K V = 1) : basis ι K V := begin haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le), exact (fin_basis_of_finrank_eq K V h).reindex equiv_of_unique_of_unique end @[simp] lemma basis_unique.repr_eq_zero_iff {ι : Type*} [unique ι] {h : finrank K V = 1} {v : V} {i : ι} : (basis_unique ι h).repr v i = 0 ↔ v = 0 := ⟨λ hv, (basis_unique ι h).repr.map_eq_zero_iff.mp (finsupp.ext $ λ j, subsingleton.elim i j ▸ hv), λ hv, by rw [hv, linear_equiv.map_zero, finsupp.zero_apply]⟩ lemma cardinal_mk_le_finrank_of_linear_independent [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : linear_independent K b) : cardinal.mk ι ≤ finrank K V := begin rw ← lift_le.{_ (max v w)}, simpa [← finrank_eq_dim K V] using cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max v w)} h end lemma fintype_card_le_finrank_of_linear_independent [finite_dimensional K V] {ι : Type*} [fintype ι] {b : ι → V} (h : linear_independent K b) : fintype.card ι ≤ finrank K V := by simpa [fintype_card] using cardinal_mk_le_finrank_of_linear_independent h lemma finset_card_le_finrank_of_linear_independent [finite_dimensional K V] {b : finset V} (h : linear_independent K (λ x, x : b → V)) : b.card ≤ finrank K V := begin rw ←fintype.card_coe, exact fintype_card_le_finrank_of_linear_independent h, end lemma lt_omega_of_linear_independent {ι : Type w} [finite_dimensional K V] {v : ι → V} (h : linear_independent K v) : cardinal.mk ι < cardinal.omega := begin apply cardinal.lift_lt.1, apply lt_of_le_of_lt, apply cardinal_lift_le_dim_of_linear_independent h, rw [←finrank_eq_dim, cardinal.lift_omega, cardinal.lift_nat_cast], apply cardinal.nat_lt_omega, end lemma not_linear_independent_of_infinite {ι : Type w} [inf : infinite ι] [finite_dimensional K V] (v : ι → V) : ¬ linear_independent K v := begin intro h_lin_indep, have : ¬ omega ≤ mk ι := not_le.mpr (lt_omega_of_linear_independent h_lin_indep), have : omega ≤ mk ι := infinite_iff.mp inf, contradiction end /-- A finite dimensional space has positive `finrank` iff it has a nonzero element. -/ lemma finrank_pos_iff_exists_ne_zero [finite_dimensional K V] : 0 < finrank K V ↔ ∃ x : V, x ≠ 0 := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_exists_ne_zero K V _ _ _) /-- A finite dimensional space has positive `finrank` iff it is nontrivial. -/ lemma finrank_pos_iff [finite_dimensional K V] : 0 < finrank K V ↔ nontrivial V := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_nontrivial K V _ _ _) /-- A finite dimensional space is nontrivial if it has positive `finrank`. -/ lemma nontrivial_of_finrank_pos (h : 0 < finrank K V) : nontrivial V := begin haveI : finite_dimensional K V := finite_dimensional_of_finrank h, rwa finrank_pos_iff at h end /-- A finite dimensional space is nontrivial if it has `finrank` equal to the successor of a natural number. -/ lemma nontrivial_of_finrank_eq_succ {n : ℕ} (hn : finrank K V = n.succ) : nontrivial V := nontrivial_of_finrank_pos (by rw hn; exact n.succ_pos) /-- A nontrivial finite dimensional space has positive `finrank`. -/ lemma finrank_pos [finite_dimensional K V] [h : nontrivial V] : 0 < finrank K V := finrank_pos_iff.mpr h /-- A finite dimensional space has zero `finrank` iff it is a subsingleton. This is the `finrank` version of `dim_zero_iff`. -/ lemma finrank_zero_iff [finite_dimensional K V] : finrank K V = 0 ↔ subsingleton V := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_zero_iff K V _ _ _) /-- A finite dimensional space that is a subsingleton has zero `finrank`. -/ lemma finrank_zero_of_subsingleton [h : subsingleton V] : finrank K V = 0 := finrank_zero_iff.2 h lemma basis.subset_extend {s : set V} (hs : linear_independent K (coe : s → V)) : s ⊆ hs.extend (set.subset_univ _) := hs.subset_extend _ /-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the whole space. -/ lemma eq_top_of_finrank_eq [finite_dimensional K V] {S : submodule K V} (h : finrank K S = finrank K V) : S = ⊤ := begin set bS := basis.of_vector_space K S with bS_eq, have : linear_independent K (coe : (coe '' basis.of_vector_space_index K S : set V) → V), from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ (submodule.subtype S) (by simpa using bS.linear_independent) (by simp), set b := basis.extend this with b_eq, letI : fintype (this.extend _) := classical.choice (finite_of_linear_independent (by simpa using b.linear_independent)), letI : fintype (subtype.val '' basis.of_vector_space_index K S) := classical.choice (finite_of_linear_independent this), letI : fintype (basis.of_vector_space_index K S) := classical.choice (finite_of_linear_independent (by simpa using bS.linear_independent)), have : subtype.val '' (basis.of_vector_space_index K S) = this.extend (set.subset_univ _), from set.eq_of_subset_of_card_le (this.subset_extend _) (by rw [set.card_image_of_injective _ subtype.val_injective, ← finrank_eq_card_basis bS, ← finrank_eq_card_basis b, h]; apply_instance), rw [← b.span_eq, b_eq, basis.coe_extend, subtype.range_coe, ← this, ← subtype_eq_val, span_image], have := bS.span_eq, rw [bS_eq, basis.coe_of_vector_space, subtype.range_coe] at this, rw [this, map_top (submodule.subtype S), range_subtype], end variable (K) /-- A division_ring is one-dimensional as a vector space over itself. -/ @[simp] lemma finrank_self : finrank K K = 1 := begin have := dim_self K, rw [←finrank_eq_dim] at this, exact_mod_cast this end instance finite_dimensional_self : finite_dimensional K K := by apply_instance /-- The vector space of functions on a fintype ι has finrank equal to the cardinality of ι. -/ @[simp] lemma finrank_fintype_fun_eq_card {ι : Type v} [fintype ι] : finrank K (ι → K) = fintype.card ι := begin have : module.rank K (ι → K) = fintype.card ι := dim_fun', rwa [← finrank_eq_dim, nat_cast_inj] at this, end /-- The vector space of functions on `fin n` has finrank equal to `n`. -/ @[simp] lemma finrank_fin_fun {n : ℕ} : finrank K (fin n → K) = n := by simp /-- The submodule generated by a finite set is finite-dimensional. -/ theorem span_of_finite {A : set V} (hA : set.finite A) : finite_dimensional K (submodule.span K A) := is_noetherian_span_of_finite K hA /-- The submodule generated by a single element is finite-dimensional. -/ instance (x : V) : finite_dimensional K (K ∙ x) := by {apply span_of_finite, simp} /-- Pushforwards of finite-dimensional submodules are finite-dimensional. -/ instance (f : V →ₗ[K] V₂) (p : submodule K V) [h : finite_dimensional K p] : finite_dimensional K (p.map f) := begin unfreezingI { rw [finite_dimensional, is_noetherian.iff_dim_lt_omega ] at h ⊢ }, rw [← cardinal.lift_lt.{v' v}], rw [← cardinal.lift_lt.{v v'}] at h, rw [cardinal.lift_omega] at h ⊢, exact (lift_dim_map_le f p).trans_lt h end /-- Pushforwards of finite-dimensional submodules have a smaller finrank. -/ lemma finrank_map_le (f : V →ₗ[K] V₂) (p : submodule K V) [finite_dimensional K p] : finrank K (p.map f) ≤ finrank K p := begin rw [← cardinal.nat_cast_le.{max v v'}, ← cardinal.lift_nat_cast.{v' v}, ← cardinal.lift_nat_cast.{v v'}, finrank_eq_dim K p, finrank_eq_dim K (p.map f)], exact lift_dim_map_le f p end variable {K} section open_locale big_operators open finset /-- If a finset has cardinality larger than the dimension of the space, then there is a nontrivial linear relation amongst its elements. -/ lemma exists_nontrivial_relation_of_dim_lt_card [finite_dimensional K V] {t : finset V} (h : finrank K V < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin have := mt finset_card_le_finrank_of_linear_independent (by { simpa using h }), rw linear_dependent_iff at this, obtain ⟨s, g, sum, z, zm, nonzero⟩ := this, -- Now we have to extend `g` to all of `t`, then to all of `V`. let f : V → K := λ x, if h : x ∈ t then if (⟨x, h⟩ : t) ∈ s then g ⟨x, h⟩ else 0 else 0, -- and finally clean up the mess caused by the extension. refine ⟨f, _, _⟩, { dsimp [f], rw ← sum, fapply sum_bij_ne_zero (λ v hvt _, (⟨v, hvt⟩ : {v // v ∈ t})), { intros v hvt H, dsimp, rw [dif_pos hvt] at H, contrapose! H, rw [if_neg H, zero_smul], }, { intros _ _ _ _ _ _, exact subtype.mk.inj, }, { intros b hbs hb, use b, simpa only [hbs, exists_prop, dif_pos, finset.mk_coe, and_true, if_true, finset.coe_mem, eq_self_iff_true, exists_prop_of_true, ne.def] using hb, }, { intros a h₁, dsimp, rw [dif_pos h₁], intro h₂, rw [if_pos], contrapose! h₂, rw [if_neg h₂, zero_smul], }, }, { refine ⟨z, z.2, _⟩, dsimp only [f], erw [dif_pos z.2, if_pos]; rwa [subtype.coe_eta] }, end /-- If a finset has cardinality larger than `finrank + 1`, then there is a nontrivial linear relation amongst its elements, such that the coefficients of the relation sum to zero. -/ lemma exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card [finite_dimensional K V] {t : finset V} (h : finrank K V + 1 < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin -- Pick an element x₀ ∈ t, have card_pos : 0 < t.card := lt_trans (nat.succ_pos _) h, obtain ⟨x₀, m⟩ := (finset.card_pos.1 card_pos).bex, -- and apply the previous lemma to the {xᵢ - x₀} let shift : V ↪ V := ⟨λ x, x - x₀, sub_left_injective⟩, let t' := (t.erase x₀).map shift, have h' : finrank K V < t'.card, { simp only [t', card_map, finset.card_erase_of_mem m], exact nat.lt_pred_iff.mpr h, }, -- to obtain a function `g`. obtain ⟨g, gsum, x₁, x₁_mem, nz⟩ := exists_nontrivial_relation_of_dim_lt_card h', -- Then obtain `f` by translating back by `x₀`, -- and setting the value of `f` at `x₀` to ensure `∑ e in t, f e = 0`. let f : V → K := λ z, if z = x₀ then - ∑ z in (t.erase x₀), g (z - x₀) else g (z - x₀), refine ⟨f, _ ,_ ,_⟩, -- After this, it's a matter of verifiying the properties, -- based on the corresponding properties for `g`. { show ∑ (e : V) in t, f e • e = 0, -- We prove this by splitting off the `x₀` term of the sum, -- which is itself a sum over `t.erase x₀`, -- combining the two sums, and -- observing that after reindexing we have exactly -- ∑ (x : V) in t', g x • x = 0. simp only [f], conv_lhs { apply_congr, skip, rw [ite_smul], }, rw [finset.sum_ite], conv { congr, congr, apply_congr, simp [filter_eq', m], }, conv { congr, congr, skip, apply_congr, simp [filter_ne'], }, rw [sum_singleton, neg_smul, add_comm, ←sub_eq_add_neg, sum_smul, ←sum_sub_distrib], simp only [←smul_sub], -- At the end we have to reindex the sum, so we use `change` to -- express the summand using `shift`. change (∑ (x : V) in t.erase x₀, (λ e, g e • e) (shift x)) = 0, rw ←sum_map _ shift, exact gsum, }, { show ∑ (e : V) in t, f e = 0, -- Again we split off the `x₀` term, -- observing that it exactly cancels the other terms. rw [← insert_erase m, sum_insert (not_mem_erase x₀ t)], dsimp [f], rw [if_pos rfl], conv_lhs { congr, skip, apply_congr, skip, rw if_neg (show x ≠ x₀, from (mem_erase.mp H).1), }, exact neg_add_self _, }, { show ∃ (x : V) (H : x ∈ t), f x ≠ 0, -- We can use x₁ + x₀. refine ⟨x₁ + x₀, _, _⟩, { rw finset.mem_map at x₁_mem, rcases x₁_mem with ⟨x₁, x₁_mem, rfl⟩, rw mem_erase at x₁_mem, simp only [x₁_mem, sub_add_cancel, function.embedding.coe_fn_mk], }, { dsimp only [f], rwa [if_neg, add_sub_cancel], rw [add_left_eq_self], rintro rfl, simpa only [sub_eq_zero, exists_prop, finset.mem_map, embedding.coe_fn_mk, eq_self_iff_true, mem_erase, not_true, exists_eq_right, ne.def, false_and] using x₁_mem, } }, end section variables {L : Type*} [linear_ordered_field L] variables {W : Type v} [add_comm_group W] [module L W] /-- A slight strengthening of `exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card` available when working over an ordered field: we can ensure a positive coefficient, not just a nonzero coefficient. -/ lemma exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card [finite_dimensional L W] {t : finset W} (h : finrank L W + 1 < t.card) : ∃ f : W → L, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, 0 < f x := begin obtain ⟨f, sum, total, nonzero⟩ := exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card h, exact ⟨f, sum, total, exists_pos_of_sum_zero_of_exists_nonzero f total nonzero⟩, end end end end division_ring section field variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- In a vector space with dimension 1, each set {v} is a basis for `v ≠ 0`. -/ noncomputable def basis_singleton (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) : basis ι K V := let b := basis_unique ι h in b.map (linear_equiv.smul_of_unit (units.mk0 (b.repr v (default ι)) (mt basis_unique.repr_eq_zero_iff.mp hv))) @[simp] lemma basis_singleton_apply (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) (i : ι) : basis_singleton ι h v hv i = v := calc basis_singleton ι h v hv i = (((basis_unique ι h).repr) v) (default ι) • (basis_unique ι h) (default ι) : by simp [subsingleton.elim i (default ι), basis_singleton, linear_equiv.smul_of_unit] ... = v : by rw [← finsupp.total_unique K (basis.repr _ v), basis.total_repr] @[simp] lemma range_basis_singleton (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) : set.range (basis_singleton ι h v hv) = {v} := by rw [set.range_unique, basis_singleton_apply] end field end finite_dimensional variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] section zero_dim open finite_dimensional lemma finite_dimensional_of_dim_eq_zero (h : module.rank K V = 0) : finite_dimensional K V := begin dsimp [finite_dimensional], rw [is_noetherian.iff_dim_lt_omega, h], exact cardinal.omega_pos end lemma finite_dimensional_of_dim_eq_one (h : module.rank K V = 1) : finite_dimensional K V := begin dsimp [finite_dimensional], rw [is_noetherian.iff_dim_lt_omega, h], exact one_lt_omega end lemma finrank_eq_zero_of_dim_eq_zero [finite_dimensional K V] (h : module.rank K V = 0) : finrank K V = 0 := begin convert finrank_eq_dim K V, rw h, norm_cast end lemma finrank_eq_zero_of_basis_imp_not_finite (h : ∀ s : set V, basis.{v} (s : set V) K V → ¬ s.finite) : finrank K V = 0 := dif_neg (λ dim_lt, h _ (basis.of_vector_space K V) ((basis.of_vector_space K V).finite_index_of_dim_lt_omega dim_lt)) lemma finrank_eq_zero_of_basis_imp_false (h : ∀ s : finset V, basis.{v} (s : set V) K V → false) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h hs.to_finset (by { convert b, simp })) lemma finrank_eq_zero_of_not_exists_basis (h : ¬ (∃ s : finset V, nonempty (basis (s : set V) K V))) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩) lemma finrank_eq_zero_of_not_exists_basis_finite (h : ¬ ∃ (s : set V) (b : basis.{v} (s : set V) K V), s.finite) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h ⟨s, b, hs⟩) lemma finrank_eq_zero_of_not_exists_basis_finset (h : ¬ ∃ (s : finset V), nonempty (basis s K V)) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩) variables (K V) instance finite_dimensional_bot : finite_dimensional K (⊥ : submodule K V) := finite_dimensional_of_dim_eq_zero $ by simp @[simp] lemma finrank_bot : finrank K (⊥ : submodule K V) = 0 := begin convert finrank_eq_dim K (⊥ : submodule K V), rw dim_bot, norm_cast end variables {K V} lemma bot_eq_top_of_dim_eq_zero (h : module.rank K V = 0) : (⊥ : submodule K V) = ⊤ := begin haveI := finite_dimensional_of_dim_eq_zero h, apply eq_top_of_finrank_eq, rw [finrank_bot, finrank_eq_zero_of_dim_eq_zero h] end @[simp] theorem dim_eq_zero {S : submodule K V} : module.rank K S = 0 ↔ S = ⊥ := ⟨λ h, (submodule.eq_bot_iff _).2 $ λ x hx, congr_arg subtype.val $ ((submodule.eq_bot_iff _).1 $ eq.symm $ bot_eq_top_of_dim_eq_zero h) ⟨x, hx⟩ submodule.mem_top, λ h, by rw [h, dim_bot]⟩ @[simp] theorem finrank_eq_zero {S : submodule K V} [finite_dimensional K S] : finrank K S = 0 ↔ S = ⊥ := by rw [← dim_eq_zero, ← finrank_eq_dim, ← @nat.cast_zero cardinal, cardinal.nat_cast_inj] end zero_dim namespace submodule open is_noetherian finite_dimensional /-- A submodule is finitely generated if and only if it is finite-dimensional -/ theorem fg_iff_finite_dimensional (s : submodule K V) : s.fg ↔ finite_dimensional K s := ⟨λh, is_noetherian_of_fg_of_noetherian s h, λh, by { rw ← map_subtype_top s, exact fg_map (iff_fg.1 h).out }⟩ /-- A submodule contained in a finite-dimensional submodule is finite-dimensional. -/ lemma finite_dimensional_of_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (h : S₁ ≤ S₂) : finite_dimensional K S₁ := is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_le_of_submodule _ _ h) (dim_lt_omega K S₂)) /-- The inf of two submodules, the first finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_left (S₁ S₂ : submodule K V) [finite_dimensional K S₁] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_left /-- The inf of two submodules, the second finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_right (S₁ S₂ : submodule K V) [finite_dimensional K S₂] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_right /-- The sup of two finite-dimensional submodules is finite-dimensional. -/ instance finite_dimensional_sup (S₁ S₂ : submodule K V) [h₁ : finite_dimensional K S₁] [h₂ : finite_dimensional K S₂] : finite_dimensional K (S₁ ⊔ S₂ : submodule K V) := begin rw ←submodule.fg_iff_finite_dimensional at *, exact submodule.fg_sup h₁ h₂ end /-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding quotient add up to the dimension of the space. -/ theorem finrank_quotient_add_finrank [finite_dimensional K V] (s : submodule K V) : finrank K s.quotient + finrank K s = finrank K V := begin have := dim_quotient_add_dim s, rw [← finrank_eq_dim, ← finrank_eq_dim, ← finrank_eq_dim] at this, exact_mod_cast this end /-- The dimension of a submodule is bounded by the dimension of the ambient space. -/ lemma finrank_le [finite_dimensional K V] (s : submodule K V) : finrank K s ≤ finrank K V := by { rw ← s.finrank_quotient_add_finrank, exact nat.le_add_left _ _ } /-- The dimension of a strict submodule is strictly bounded by the dimension of the ambient space. -/ lemma finrank_lt [finite_dimensional K V] {s : submodule K V} (h : s < ⊤) : finrank K s < finrank K V := begin rw [← s.finrank_quotient_add_finrank, add_comm], exact nat.lt_add_of_zero_lt_left _ _ (finrank_pos_iff.mpr (quotient.nontrivial_of_lt_top _ h)) end /-- The dimension of a quotient is bounded by the dimension of the ambient space. -/ lemma finrank_quotient_le [finite_dimensional K V] (s : submodule K V) : finrank K s.quotient ≤ finrank K V := by { rw ← s.finrank_quotient_add_finrank, exact nat.le_add_right _ _ } /-- The sum of the dimensions of s + t and s ∩ t is the sum of the dimensions of s and t -/ theorem dim_sup_add_dim_inf_eq (s t : submodule K V) [finite_dimensional K s] [finite_dimensional K t] : finrank K ↥(s ⊔ t) + finrank K ↥(s ⊓ t) = finrank K ↥s + finrank K ↥t := begin have key : module.rank K ↥(s ⊔ t) + module.rank K ↥(s ⊓ t) = module.rank K s + module.rank K t := dim_sup_add_dim_inf_eq s t, repeat { rw ←finrank_eq_dim at key }, norm_cast at key, exact key end lemma eq_top_of_disjoint [finite_dimensional K V] (s t : submodule K V) (hdim : finrank K s + finrank K t = finrank K V) (hdisjoint : disjoint s t) : s ⊔ t = ⊤ := begin have h_finrank_inf : finrank K ↥(s ⊓ t) = 0, { rw [disjoint, le_bot_iff] at hdisjoint, rw [hdisjoint, finrank_bot] }, apply eq_top_of_finrank_eq, rw ←hdim, convert s.dim_sup_add_dim_inf_eq t, rw h_finrank_inf, refl, end end submodule namespace linear_equiv open finite_dimensional /-- Finite dimensionality is preserved under linear equivalence. -/ protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : finite_dimensional K V₂ := is_noetherian_of_linear_equiv f /-- The dimension of a finite dimensional space is preserved under linear equivalence. -/ theorem finrank_eq (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : finrank K V = finrank K V₂ := begin haveI : finite_dimensional K V₂ := f.finite_dimensional, simpa [← finrank_eq_dim] using f.lift_dim_eq end /-- Pushforwards of finite-dimensional submodules along a `linear_equiv` have the same finrank. -/ lemma finrank_map_eq (f : V ≃ₗ[K] V₂) (p : submodule K V) [finite_dimensional K p] : finrank K (p.map (f : V →ₗ[K] V₂)) = finrank K p := (f.of_submodule p).finrank_eq.symm end linear_equiv instance finite_dimensional_finsupp {ι : Type*} [fintype ι] [finite_dimensional K V] : finite_dimensional K (ι →₀ V) := (finsupp.linear_equiv_fun_on_fintype K V ι).symm.finite_dimensional namespace finite_dimensional /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : finrank K V = finrank K V₂) : nonempty (V ≃ₗ[K] V₂) := nonempty_linear_equiv_of_lift_dim_eq $ by simp only [← finrank_eq_dim, cond, lift_nat_cast] /-- Two finite-dimensional vector spaces are isomorphic if and only if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_iff_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] : nonempty (V ≃ₗ[K] V₂) ↔ finrank K V = finrank K V₂ := ⟨λ ⟨h⟩, h.finrank_eq, λ h, nonempty_linear_equiv_of_finrank_eq h⟩ section variables (V V₂) /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ noncomputable def linear_equiv.of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ := classical.choice $ nonempty_linear_equiv_of_finrank_eq cond end lemma eq_of_le_of_finrank_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : finrank K S₂ ≤ finrank K S₁) : S₁ = S₂ := begin rw ←linear_equiv.finrank_eq (submodule.comap_subtype_equiv_of_le hle) at hd, exact le_antisymm hle (submodule.comap_subtype_eq_top.1 (eq_top_of_finrank_eq (le_antisymm (comap (submodule.subtype S₂) S₁).finrank_le hd))), end /-- If a submodule is less than or equal to a finite-dimensional submodule with the same dimension, they are equal. -/ lemma eq_of_le_of_finrank_eq {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : finrank K S₁ = finrank K S₂) : S₁ = S₂ := eq_of_le_of_finrank_le hle hd.ge variables [finite_dimensional K V] [finite_dimensional K V₂] /-- Given isomorphic subspaces `p q` of vector spaces `V` and `V₁` respectively, `p.quotient` is isomorphic to `q.quotient`. -/ noncomputable def linear_equiv.quot_equiv_of_equiv {p : subspace K V} {q : subspace K V₂} (f₁ : p ≃ₗ[K] q) (f₂ : V ≃ₗ[K] V₂) : p.quotient ≃ₗ[K] q.quotient := linear_equiv.of_finrank_eq _ _ begin rw [← @add_right_cancel_iff _ _ (finrank K p), submodule.finrank_quotient_add_finrank, linear_equiv.finrank_eq f₁, submodule.finrank_quotient_add_finrank, linear_equiv.finrank_eq f₂], end /-- Given the subspaces `p q`, if `p.quotient ≃ₗ[K] q`, then `q.quotient ≃ₗ[K] p` -/ noncomputable def linear_equiv.quot_equiv_of_quot_equiv {p q : subspace K V} (f : p.quotient ≃ₗ[K] q) : q.quotient ≃ₗ[K] p := linear_equiv.of_finrank_eq _ _ begin rw [← @add_right_cancel_iff _ _ (finrank K q), submodule.finrank_quotient_add_finrank, ← linear_equiv.finrank_eq f, add_comm, submodule.finrank_quotient_add_finrank] end @[simp] lemma finrank_map_subtype_eq (p : subspace K V) (q : subspace K p) : finite_dimensional.finrank K (q.map p.subtype) = finite_dimensional.finrank K q := (submodule.equiv_subtype_map p q).symm.finrank_eq end finite_dimensional namespace linear_map open finite_dimensional /-- On a finite-dimensional space, an injective linear map is surjective. -/ lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V} (hinj : injective f) : surjective f := begin have h := dim_eq_of_injective _ hinj, rw [← finrank_eq_dim, ← finrank_eq_dim, nat_cast_inj] at h, exact range_eq_top.1 (eq_top_of_finrank_eq h.symm) end /-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/ lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} : injective f ↔ surjective f := ⟨surjective_of_injective, λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in have function.right_inverse g f, from linear_map.ext_iff.1 hg, (left_inverse_of_surjective_of_right_inverse (surjective_of_injective this.injective) this).injective⟩ lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective] /-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they are also inverse to each other on the other side. -/ lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) : g * f = 1 := have ginj : injective g, from has_left_inverse.injective ⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩, let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective (range_eq_top.2 (injective_iff_surjective.1 ginj)) in have f * (g * i) = f * 1, from congr_arg _ hi, by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 := ⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩ /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id := mul_eq_one_comm /-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/ lemma finite_dimensional_of_surjective [h : finite_dimensional K V] (f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ := is_noetherian_of_surjective V f hf /-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) : finite_dimensional K f.range := f.quot_ker_equiv_range.finite_dimensional /-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to the dimension of the source space. -/ theorem finrank_range_add_finrank_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) : finrank K f.range + finrank K f.ker = finrank K V := by { rw [← f.quot_ker_equiv_range.finrank_eq], exact submodule.finrank_quotient_add_finrank _ } end linear_map namespace linear_equiv open finite_dimensional variables [finite_dimensional K V] /-- The linear equivalence corresponging to an injective endomorphism. -/ noncomputable def of_injective_endo (f : V →ₗ[K] V) (h_inj : injective f) : V ≃ₗ[K] V := linear_equiv.of_bijective f h_inj $ linear_map.injective_iff_surjective.mp h_inj @[simp] lemma coe_of_injective_endo (f : V →ₗ[K] V) (h_inj : injective f) : ⇑(of_injective_endo f h_inj) = f := rfl @[simp] lemma of_injective_endo_right_inv (f : V →ₗ[K] V) (h_inj : injective f) : f * (of_injective_endo f h_inj).symm = 1 := linear_map.ext $ (of_injective_endo f h_inj).apply_symm_apply @[simp] lemma of_injective_endo_left_inv (f : V →ₗ[K] V) (h_inj : injective f) : ((of_injective_endo f h_inj).symm : V →ₗ[K] V) * f = 1 := linear_map.ext $ (of_injective_endo f h_inj).symm_apply_apply end linear_equiv namespace linear_map lemma is_unit_iff [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.ker = ⊥ := begin split, { rintro ⟨u, rfl⟩, exact linear_map.ker_eq_bot_of_inverse u.inv_mul }, { intro h_inj, rw ker_eq_bot at h_inj, exact ⟨⟨f, (linear_equiv.of_injective_endo f h_inj).symm.to_linear_map, linear_equiv.of_injective_endo_right_inv f h_inj, linear_equiv.of_injective_endo_left_inv f h_inj⟩, rfl⟩ } end end linear_map open module finite_dimensional section top @[simp] theorem finrank_top : finrank K (⊤ : submodule K V) = finrank K V := by { unfold finrank, simp [dim_top] } end top lemma finrank_zero_iff_forall_zero [finite_dimensional K V] : finrank K V = 0 ↔ ∀ x : V, x = 0 := finrank_zero_iff.trans (subsingleton_iff_forall_eq 0) /-- If `ι` is an empty type and `V` is zero-dimensional, there is a unique `ι`-indexed basis. -/ noncomputable def basis_of_finrank_zero [finite_dimensional K V] {ι : Type*} [is_empty ι] (hV : finrank K V = 0) : basis ι K V := begin haveI : subsingleton V := finrank_zero_iff.1 hV, exact basis.empty _ end namespace linear_map theorem injective_iff_surjective_of_finrank_eq_finrank [finite_dimensional K V] [finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} : function.injective f ↔ function.surjective f := begin have := finrank_range_add_finrank_ker f, rw [← ker_eq_bot, ← range_eq_top], refine ⟨λ h, _, λ h, _⟩, { rw [h, finrank_bot, add_zero, H] at this, exact eq_top_of_finrank_eq this }, { rw [h, finrank_top, H] at this, exact finrank_eq_zero.1 (add_right_injective _ this) } end lemma ker_eq_bot_iff_range_eq_top_of_finrank_eq_finrank [finite_dimensional K V] [finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective_of_finrank_eq_finrank H] theorem finrank_le_finrank_of_injective [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : function.injective f) : finrank K V ≤ finrank K V₂ := calc finrank K V = finrank K f.range + finrank K f.ker : (finrank_range_add_finrank_ker f).symm ... = finrank K f.range : by rw [ker_eq_bot.2 hf, finrank_bot, add_zero] ... ≤ finrank K V₂ : submodule.finrank_le _ /-- Given a linear map `f` between two vector spaces with the same dimension, if `ker f = ⊥` then `linear_equiv_of_injective` is the induced isomorphism between the two vector spaces. -/ noncomputable def linear_equiv_of_injective [finite_dimensional K V] [finite_dimensional K V₂] (f : V →ₗ[K] V₂) (hf : injective f) (hdim : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ := linear_equiv.of_bijective f hf $ (linear_map.injective_iff_surjective_of_finrank_eq_finrank hdim).mp hf @[simp] lemma linear_equiv_of_injective_apply [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : injective f) (hdim : finrank K V = finrank K V₂) (x : V) : f.linear_equiv_of_injective hf hdim x = f x := rfl end linear_map namespace alg_hom lemma bijective {F : Type*} [field F] {E : Type*} [field E] [algebra F E] [finite_dimensional F E] (ϕ : E →ₐ[F] E) : function.bijective ϕ := have inj : function.injective ϕ.to_linear_map := ϕ.to_ring_hom.injective, ⟨inj, (linear_map.injective_iff_surjective_of_finrank_eq_finrank rfl).mp inj⟩ end alg_hom /-- Bijection between algebra equivalences and algebra homomorphisms -/ noncomputable def alg_equiv_equiv_alg_hom (F : Type u) [field F] (E : Type v) [field E] [algebra F E] [finite_dimensional F E] : (E ≃ₐ[F] E) ≃ (E →ₐ[F] E) := { to_fun := λ ϕ, ϕ.to_alg_hom, inv_fun := λ ϕ, alg_equiv.of_bijective ϕ ϕ.bijective, left_inv := λ _, by {ext, refl}, right_inv := λ _, by {ext, refl} } section /-- An integral domain that is module-finite as an algebra over a field is a field. -/ noncomputable def field_of_finite_dimensional (F K : Type*) [field F] [integral_domain K] [algebra F K] [finite_dimensional F K] : field K := { inv := λ x, if H : x = 0 then 0 else classical.some $ (show function.surjective (algebra.lmul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' H).1) 1, mul_inv_cancel := λ x hx, show x * dite _ _ _ = _, by { rw dif_neg hx, exact classical.some_spec ((show function.surjective (algebra.lmul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' hx).1) 1) }, inv_zero := dif_pos rfl, .. ‹integral_domain K› } end namespace submodule lemma finrank_mono [finite_dimensional K V] : monotone (λ (s : submodule K V), finrank K s) := λ s t hst, calc finrank K s = finrank K (comap t.subtype s) : linear_equiv.finrank_eq (comap_subtype_equiv_of_le hst).symm ... ≤ finrank K t : submodule.finrank_le _ lemma lt_of_le_of_finrank_lt_finrank {s t : submodule K V} (le : s ≤ t) (lt : finrank K s < finrank K t) : s < t := lt_of_le_of_ne le (λ h, ne_of_lt lt (by rw h)) lemma lt_top_of_finrank_lt_finrank {s : submodule K V} (lt : finrank K s < finrank K V) : s < ⊤ := begin rw ← @finrank_top K V at lt, exact lt_of_le_of_finrank_lt_finrank le_top lt end lemma finrank_lt_finrank_of_lt [finite_dimensional K V] {s t : submodule K V} (hst : s < t) : finrank K s < finrank K t := begin rw linear_equiv.finrank_eq (comap_subtype_equiv_of_le (le_of_lt hst)).symm, refine finrank_lt (lt_of_le_of_ne le_top _), intro h_eq_top, rw comap_subtype_eq_top at h_eq_top, apply not_le_of_lt hst h_eq_top, end lemma finrank_add_eq_of_is_compl [finite_dimensional K V] {U W : submodule K V} (h : is_compl U W) : finrank K U + finrank K W = finrank K V := begin rw [← submodule.dim_sup_add_dim_inf_eq, top_le_iff.1 h.2, le_bot_iff.1 h.1, finrank_bot, add_zero], exact finrank_top end end submodule section span open submodule lemma finrank_span_le_card (s : set V) [fin : fintype s] : finrank K (span K s) ≤ s.to_finset.card := begin haveI := span_of_finite K ⟨fin⟩, have : module.rank K (span K s) ≤ (mk s : cardinal) := dim_span_le s, rw [←finrank_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this, exact_mod_cast this end lemma finrank_span_finset_le_card (s : finset V) : finrank K (span K (s : set V)) ≤ s.card := calc finrank K (span K (s : set V)) ≤ (s : set V).to_finset.card : finrank_span_le_card s ... = s.card : by simp lemma finrank_span_eq_card {ι : Type*} [fintype ι] {b : ι → V} (hb : linear_independent K b) : finrank K (span K (set.range b)) = fintype.card ι := begin haveI : finite_dimensional K (span K (set.range b)) := span_of_finite K (set.finite_range b), have : module.rank K (span K (set.range b)) = (mk (set.range b) : cardinal) := dim_span hb, rwa [←finrank_eq_dim, ←lift_inj, mk_range_eq_of_injective hb.injective, cardinal.fintype_card, lift_nat_cast, lift_nat_cast, nat_cast_inj] at this, end lemma finrank_span_set_eq_card (s : set V) [fin : fintype s] (hs : linear_independent K (coe : s → V)) : finrank K (span K s) = s.to_finset.card := begin haveI := span_of_finite K ⟨fin⟩, have : module.rank K (span K s) = (mk s : cardinal) := dim_span_set hs, rw [←finrank_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this, exact_mod_cast this end lemma finrank_span_finset_eq_card (s : finset V) (hs : linear_independent K (coe : s → V)) : finrank K (span K (s : set V)) = s.card := begin convert finrank_span_set_eq_card ↑s hs, ext, simp end lemma span_lt_of_subset_of_card_lt_finrank {s : set V} [fintype s] {t : submodule K V} (subset : s ⊆ t) (card_lt : s.to_finset.card < finrank K t) : span K s < t := lt_of_le_of_finrank_lt_finrank (span_le.mpr subset) (lt_of_le_of_lt (finrank_span_le_card _) card_lt) lemma span_lt_top_of_card_lt_finrank {s : set V} [fintype s] (card_lt : s.to_finset.card < finrank K V) : span K s < ⊤ := lt_top_of_finrank_lt_finrank (lt_of_le_of_lt (finrank_span_le_card _) card_lt) lemma finrank_span_singleton {v : V} (hv : v ≠ 0) : finrank K (K ∙ v) = 1 := begin apply le_antisymm, { exact finrank_span_le_card ({v} : set V) }, { rw [nat.succ_le_iff, finrank_pos_iff], use [⟨v, mem_span_singleton_self v⟩, 0], simp [hv] } end end span section basis lemma linear_independent_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] {b : ι → V} (span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) : linear_independent K b := linear_independent_iff'.mpr $ λ s g dependent i i_mem_s, begin by_contra gx_ne_zero, -- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1` -- spans a vector space of dimension `n`. refine ne_of_lt (span_lt_top_of_card_lt_finrank (show (b '' (set.univ \ {i})).to_finset.card < finrank K V, from _)) _, { calc (b '' (set.univ \ {i})).to_finset.card = ((set.univ \ {i}).to_finset.image b).card : by rw [set.to_finset_card, fintype.card_of_finset] ... ≤ (set.univ \ {i}).to_finset.card : finset.card_image_le ... = (finset.univ.erase i).card : congr_arg finset.card (finset.ext (by simp [and_comm])) ... < finset.univ.card : finset.card_erase_lt_of_mem (finset.mem_univ i) ... = finrank K V : card_eq }, -- We already have that `b '' univ` spans the whole space, -- so we only need to show that the span of `b '' (univ \ {i})` contains each `b j`. refine trans (le_antisymm (span_mono (set.image_subset_range _ _)) (span_le.mpr _)) span_eq, rintros _ ⟨j, rfl, rfl⟩, -- The case that `j ≠ i` is easy because `b j ∈ b '' (univ \ {i})`. by_cases j_eq : j = i, swap, { refine subset_span ⟨j, (set.mem_diff _).mpr ⟨set.mem_univ _, _⟩, rfl⟩, exact mt set.mem_singleton_iff.mp j_eq }, -- To show `b i ∈ span (b '' (univ \ {i}))`, we use that it's a weighted sum -- of the other `b j`s. rw [j_eq, set_like.mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)), from _], { refine submodule.neg_mem _ (smul_mem _ _ (sum_mem _ (λ k hk, _))), obtain ⟨k_ne_i, k_mem⟩ := finset.mem_erase.mp hk, refine smul_mem _ _ (subset_span ⟨k, _, rfl⟩), simpa using k_mem }, -- To show `b i` is a weighted sum of the other `b j`s, we'll rewrite this sum -- to have the form of the assumption `dependent`. apply eq_neg_of_add_eq_zero, calc b i + (g i)⁻¹ • (s.erase i).sum (λ j, g j • b j) = (g i)⁻¹ • (g i • b i + (s.erase i).sum (λ j, g j • b j)) : by rw [smul_add, ←mul_smul, inv_mul_cancel gx_ne_zero, one_smul] ... = (g i)⁻¹ • 0 : congr_arg _ _ ... = 0 : smul_zero _, -- And then it's just a bit of manipulation with finite sums. rwa [← finset.insert_erase i_mem_s, finset.sum_insert (finset.not_mem_erase _ _)] at dependent end /-- A finite family of vectors is linearly independent if and only if its cardinality equals the dimension of its span. -/ lemma linear_independent_iff_card_eq_finrank_span {ι : Type*} [fintype ι] {b : ι → V} : linear_independent K b ↔ fintype.card ι = finrank K (span K (set.range b)) := begin split, { intro h, exact (finrank_span_eq_card h).symm }, { intro hc, let f := (submodule.subtype (span K (set.range b))), let b' : ι → span K (set.range b) := λ i, ⟨b i, mem_span.2 (λ p hp, hp (set.mem_range_self _))⟩, have hs : span K (set.range b') = ⊤, { rw eq_top_iff', intro x, have h : span K (f '' (set.range b')) = map f (span K (set.range b')) := span_image f, have hf : f '' (set.range b') = set.range b, { ext x, simp [set.mem_image, set.mem_range] }, rw hf at h, have hx : (x : V) ∈ span K (set.range b) := x.property, conv at hx { congr, skip, rw h }, simpa [mem_map] using hx }, have hi : f.ker = ⊥ := ker_subtype _, convert (linear_independent_of_span_eq_top_of_card_eq_finrank hs hc).map' _ hi } end /-- A family of `finrank K V` vectors forms a basis if they span the whole space. -/ noncomputable def basis_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V) (span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) : basis ι K V := basis.mk (linear_independent_of_span_eq_top_of_card_eq_finrank span_eq card_eq) span_eq @[simp] lemma coe_basis_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V) (span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) : ⇑(basis_of_span_eq_top_of_card_eq_finrank b span_eq card_eq) = b := basis.coe_mk _ _ /-- A finset of `finrank K V` vectors forms a basis if they span the whole space. -/ @[simps] noncomputable def finset_basis_of_span_eq_top_of_card_eq_finrank {s : finset V} (span_eq : span K (s : set V) = ⊤) (card_eq : s.card = finrank K V) : basis (s : set V) K V := basis_of_span_eq_top_of_card_eq_finrank (coe : (s : set V) → V) ((@subtype.range_coe_subtype _ (λ x, x ∈ s)).symm ▸ span_eq) (trans (fintype.card_coe _) card_eq) /-- A set of `finrank K V` vectors forms a basis if they span the whole space. -/ @[simps] noncomputable def set_basis_of_span_eq_top_of_card_eq_finrank {s : set V} [fintype s] (span_eq : span K s = ⊤) (card_eq : s.to_finset.card = finrank K V) : basis s K V := basis_of_span_eq_top_of_card_eq_finrank (coe : s → V) ((@subtype.range_coe_subtype _ s).symm ▸ span_eq) (trans s.to_finset_card.symm card_eq) lemma span_eq_top_of_linear_independent_of_card_eq_finrank {ι : Type*} [hι : nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : span K (set.range b) = ⊤ := begin by_cases fin : (finite_dimensional K V), { haveI := fin, by_contra ne_top, have lt_top : span K (set.range b) < ⊤ := lt_of_le_of_ne le_top ne_top, exact ne_of_lt (submodule.finrank_lt lt_top) (trans (finrank_span_eq_card lin_ind) card_eq) }, { exfalso, apply ne_of_lt (fintype.card_pos_iff.mpr hι), symmetry, calc fintype.card ι = finrank K V : card_eq ... = 0 : dif_neg (mt is_noetherian.iff_dim_lt_omega.mpr fin) } end /-- A linear independent family of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def basis_of_linear_independent_of_card_eq_finrank {ι : Type*} [nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : basis ι K V := basis.mk lin_ind $ span_eq_top_of_linear_independent_of_card_eq_finrank lin_ind card_eq @[simp] lemma coe_basis_of_linear_independent_of_card_eq_finrank {ι : Type*} [nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : ⇑(basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = b := basis.coe_mk _ _ /-- A linear independent finset of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def finset_basis_of_linear_independent_of_card_eq_finrank {s : finset V} (hs : s.nonempty) (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) : basis s K V := @basis_of_linear_independent_of_card_eq_finrank _ _ _ _ _ _ ⟨(⟨hs.some, hs.some_spec⟩ : s)⟩ _ _ lin_ind (trans (fintype.card_coe _) card_eq) @[simp] lemma coe_finset_basis_of_linear_independent_of_card_eq_finrank {s : finset V} (hs : s.nonempty) (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) : ⇑(finset_basis_of_linear_independent_of_card_eq_finrank hs lin_ind card_eq) = coe := basis.coe_mk _ _ /-- A linear independent set of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def set_basis_of_linear_independent_of_card_eq_finrank {s : set V} [nonempty s] [fintype s] (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) : basis s K V := basis_of_linear_independent_of_card_eq_finrank lin_ind (trans s.to_finset_card.symm card_eq) @[simp] lemma coe_set_basis_of_linear_independent_of_card_eq_finrank {s : set V} [nonempty s] [fintype s] (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) : ⇑(set_basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = coe := basis.coe_mk _ _ end basis /-! We now give characterisations of `finrank K V = 1` and `finrank K V ≤ 1`. -/ section finrank_eq_one /-- If there is a nonzero vector and every other vector is a multiple of it, then the module has dimension one. -/ lemma finrank_eq_one (v : V) (n : v ≠ 0) (h : ∀ w : V, ∃ c : K, c • v = w) : finrank K V = 1 := begin obtain ⟨b⟩ := (basis.basis_singleton_iff punit).mpr ⟨v, n, h⟩, rw [finrank_eq_card_basis b, fintype.card_punit] end /-- If every vector is a multiple of some `v : V`, then `V` has dimension at most one. -/ lemma finrank_le_one (v : V) (h : ∀ w : V, ∃ c : K, c • v = w) : finrank K V ≤ 1 := begin by_cases n : v = 0, { subst n, convert zero_le_one, haveI := subsingleton_of_forall_eq (0 : V) (λ w, by { obtain ⟨c, rfl⟩ := h w, simp, }), exact finrank_zero_of_subsingleton, }, { exact (finrank_eq_one v n h).le, } end /-- A vector space with a nonzero vector `v` has dimension 1 iff `v` spans. -/ lemma finrank_eq_one_iff_of_nonzero (v : V) (nz : v ≠ 0) : finrank K V = 1 ↔ span K ({v} : set V) = ⊤ := ⟨λ h, by simpa using (basis_singleton punit h v nz).span_eq, λ s, finrank_eq_card_basis (basis.mk (linear_independent_singleton nz) (by { convert s, simp }))⟩ /-- A module with a nonzero vector `v` has dimension 1 iff every vector is a multiple of `v`. -/ lemma finrank_eq_one_iff_of_nonzero' (v : V) (nz : v ≠ 0) : finrank K V = 1 ↔ ∀ w : V, ∃ c : K, c • v = w := begin rw finrank_eq_one_iff_of_nonzero v nz, apply span_singleton_eq_top_iff, end /-- A module has dimension 1 iff there is some `v : V` so `{v}` is a basis. -/ lemma finrank_eq_one_iff (ι : Type*) [unique ι] : finrank K V = 1 ↔ nonempty (basis ι K V) := begin fsplit, { intro h, haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le), exact ⟨basis_unique ι h⟩ }, { rintro ⟨b⟩, simpa using finrank_eq_card_basis b } end /-- A module has dimension 1 iff there is some nonzero `v : V` so every vector is a multiple of `v`. -/ lemma finrank_eq_one_iff' : finrank K V = 1 ↔ ∃ (v : V) (n : v ≠ 0), ∀ w : V, ∃ c : K, c • v = w := begin convert finrank_eq_one_iff punit, simp only [exists_prop, eq_iff_iff, ne.def], convert (basis.basis_singleton_iff punit).symm, funext v, simp, apply_instance, apply_instance, -- Not sure why this aren't found automatically. end /-- A finite dimensional module has dimension at most 1 iff there is some `v : V` so every vector is a multiple of `v`. -/ lemma finrank_le_one_iff [finite_dimensional K V] : finrank K V ≤ 1 ↔ ∃ (v : V), ∀ w : V, ∃ c : K, c • v = w := begin fsplit, { intro h, by_cases h' : finrank K V = 0, { use 0, intro w, use 0, haveI := finrank_zero_iff.mp h', apply subsingleton.elim, }, { replace h' := zero_lt_iff.mpr h', have : finrank K V = 1, { linarith }, obtain ⟨v, -, p⟩ := finrank_eq_one_iff'.mp this, use ⟨v, p⟩, }, }, { rintro ⟨v, p⟩, exact finrank_le_one v p, } end end finrank_eq_one section subalgebra_dim open module variables {F E : Type*} [field F] [field E] [algebra F E] lemma subalgebra.dim_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : module.rank F S = 1 := begin rw [← S.to_submodule_equiv.dim_eq, h, (linear_equiv.of_eq (⊥ : subalgebra F E).to_submodule _ algebra.to_submodule_bot).dim_eq, dim_span_set], exacts [mk_singleton _, linear_independent_singleton one_ne_zero] end @[simp] lemma subalgebra.dim_bot : module.rank F (⊥ : subalgebra F E) = 1 := subalgebra.dim_eq_one_of_eq_bot rfl lemma subalgebra_top_dim_eq_submodule_top_dim : module.rank F (⊤ : subalgebra F E) = module.rank F (⊤ : submodule F E) := by { rw ← algebra.top_to_submodule, refl } lemma subalgebra_top_finrank_eq_submodule_top_finrank : finrank F (⊤ : subalgebra F E) = finrank F (⊤ : submodule F E) := by { rw ← algebra.top_to_submodule, refl } lemma subalgebra.dim_top : module.rank F (⊤ : subalgebra F E) = module.rank F E := by { rw subalgebra_top_dim_eq_submodule_top_dim, exact dim_top F E } instance subalgebra.finite_dimensional_bot : finite_dimensional F (⊥ : subalgebra F E) := finite_dimensional_of_dim_eq_one subalgebra.dim_bot @[simp] lemma subalgebra.finrank_bot : finrank F (⊥ : subalgebra F E) = 1 := begin have : module.rank F (⊥ : subalgebra F E) = 1 := subalgebra.dim_bot, rw ← finrank_eq_dim at this, norm_cast at *, simp *, end lemma subalgebra.finrank_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : finrank F S = 1 := by { rw h, exact subalgebra.finrank_bot } lemma subalgebra.eq_bot_of_finrank_one {S : subalgebra F E} (h : finrank F S = 1) : S = ⊥ := begin rw eq_bot_iff, let b : set S := {1}, have : fintype b := unique.fintype, have b_lin_ind : linear_independent F (coe : b → S) := linear_independent_singleton one_ne_zero, have b_card : fintype.card b = 1 := fintype.card_of_subsingleton _, let hb := set_basis_of_linear_independent_of_card_eq_finrank b_lin_ind (by simp only [*, set.to_finset_card]), have b_spans := hb.span_eq, intros x hx, rw [algebra.mem_bot], have x_in_span_b : (⟨x, hx⟩ : S) ∈ submodule.span F b, { rw [coe_set_basis_of_linear_independent_of_card_eq_finrank, subtype.range_coe] at b_spans, rw b_spans, exact submodule.mem_top, }, obtain ⟨a, ha⟩ := submodule.mem_span_singleton.mp x_in_span_b, replace ha : a • 1 = x := by injections with ha, exact ⟨a, by rw [← ha, algebra.smul_def, mul_one]⟩, end lemma subalgebra.eq_bot_of_dim_one {S : subalgebra F E} (h : module.rank F S = 1) : S = ⊥ := begin haveI : finite_dimensional F S := finite_dimensional_of_dim_eq_one h, rw ← finrank_eq_dim at h, norm_cast at h, exact subalgebra.eq_bot_of_finrank_one h, end @[simp] lemma subalgebra.bot_eq_top_of_dim_eq_one (h : module.rank F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← dim_top, ← subalgebra_top_dim_eq_submodule_top_dim] at h, exact eq.symm (subalgebra.eq_bot_of_dim_one h), end @[simp] lemma subalgebra.bot_eq_top_of_finrank_eq_one (h : finrank F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← finrank_top, ← subalgebra_top_finrank_eq_submodule_top_finrank] at h, exact eq.symm (subalgebra.eq_bot_of_finrank_one h), end @[simp] theorem subalgebra.dim_eq_one_iff {S : subalgebra F E} : module.rank F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_dim_one, subalgebra.dim_eq_one_of_eq_bot⟩ @[simp] theorem subalgebra.finrank_eq_one_iff {S : subalgebra F E} : finrank F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_finrank_one, subalgebra.finrank_eq_one_of_eq_bot⟩ end subalgebra_dim namespace module namespace End lemma exists_ker_pow_eq_ker_pow_succ [finite_dimensional K V] (f : End K V) : ∃ (k : ℕ), k ≤ finrank K V ∧ (f ^ k).ker = (f ^ k.succ).ker := begin classical, by_contradiction h_contra, simp_rw [not_exists, not_and] at h_contra, have h_le_ker_pow : ∀ (n : ℕ), n ≤ (finrank K V).succ → n ≤ finrank K (f ^ n).ker, { intros n hn, induction n with n ih, { exact zero_le (finrank _ _) }, { have h_ker_lt_ker : (f ^ n).ker < (f ^ n.succ).ker, { refine lt_of_le_of_ne _ (h_contra n (nat.le_of_succ_le_succ hn)), rw pow_succ, apply linear_map.ker_le_ker_comp }, have h_finrank_lt_finrank : finrank K (f ^ n).ker < finrank K (f ^ n.succ).ker, { apply submodule.finrank_lt_finrank_of_lt h_ker_lt_ker }, calc n.succ ≤ (finrank K ↥(linear_map.ker (f ^ n))).succ : nat.succ_le_succ (ih (nat.le_of_succ_le hn)) ... ≤ finrank K ↥(linear_map.ker (f ^ n.succ)) : nat.succ_le_of_lt h_finrank_lt_finrank } }, have h_le_finrank_V : ∀ n, finrank K (f ^ n).ker ≤ finrank K V := λ n, submodule.finrank_le _, have h_any_n_lt: ∀ n, n ≤ (finrank K V).succ → n ≤ finrank K V := λ n hn, (h_le_ker_pow n hn).trans (h_le_finrank_V n), show false, from nat.not_succ_le_self _ (h_any_n_lt (finrank K V).succ (finrank K V).succ.le_refl), end lemma ker_pow_constant {f : End K V} {k : ℕ} (h : (f ^ k).ker = (f ^ k.succ).ker) : ∀ m, (f ^ k).ker = (f ^ (k + m)).ker | 0 := by simp | (m + 1) := begin apply le_antisymm, { rw [add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_constant m, add_comm m 1, ←add_assoc, pow_add, pow_add f k m], change linear_map.ker ((f ^ (k + 1)).comp (f ^ m)) ≤ linear_map.ker ((f ^ k).comp (f ^ m)), rw [linear_map.ker_comp, linear_map.ker_comp, h, nat.add_one], exact le_refl _, } end lemma ker_pow_eq_ker_pow_finrank_of_le [finite_dimensional K V] {f : End K V} {m : ℕ} (hm : finrank K V ≤ m) : (f ^ m).ker = (f ^ finrank K V).ker := begin obtain ⟨k, h_k_le, hk⟩ : ∃ k, k ≤ finrank K V ∧ linear_map.ker (f ^ k) = linear_map.ker (f ^ k.succ) := exists_ker_pow_eq_ker_pow_succ f, calc (f ^ m).ker = (f ^ (k + (m - k))).ker : by rw nat.add_sub_of_le (h_k_le.trans hm) ... = (f ^ k).ker : by rw ker_pow_constant hk _ ... = (f ^ (k + (finrank K V - k))).ker : ker_pow_constant hk (finrank K V - k) ... = (f ^ finrank K V).ker : by rw nat.add_sub_of_le h_k_le end lemma ker_pow_le_ker_pow_finrank [finite_dimensional K V] (f : End K V) (m : ℕ) : (f ^ m).ker ≤ (f ^ finrank K V).ker := begin by_cases h_cases: m < finrank K V, { rw [←nat.add_sub_of_le (nat.le_of_lt h_cases), add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_eq_ker_pow_finrank_of_le (le_of_not_lt h_cases)], exact le_refl _ } end end End end module
7c6168420997609764eed8135f72b8f152fc4975
63abd62053d479eae5abf4951554e1064a4c45b4
/src/tactic/group.lean
e5f46f0df365d36e9460735bd28a561073e69e44
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
3,675
lean
/- Copyright (c) 2020. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Patrick Massot -/ import tactic.ring import tactic.doc_commands /-! # `group` Normalizes expressions in the language of groups. The basic idea is to use the simplifier to put everything into a product of group powers (`gpow` which takes a group element and an integer), then simplify the exponents using the `ring` tactic. The process needs to be repeated since `ring` can normalize an exponent to zero, leading to a factor that can be removed before collecting exponents again. The simplifier step also uses some extra lemmas to avoid some `ring` invocations. ## Tags group_theory -/ -- The next four lemmas are not general purpose lemmas, they are intended for use only by -- the `group` tactic. lemma tactic.group.gpow_trick {G : Type*} [group G] (a b : G) (n m : ℤ) : a*b^n*b^m = a*b^(n+m) := by rw [mul_assoc, ← gpow_add] lemma tactic.group.gpow_trick_one {G : Type*} [group G] (a b : G) (m : ℤ) : a*b*b^m = a*b^(m+1) := by rw [mul_assoc, mul_self_gpow] lemma tactic.group.gpow_trick_one' {G : Type*} [group G] (a b : G) (n : ℤ) : a*b^n*b = a*b^(n+1) := by rw [mul_assoc, mul_gpow_self] lemma tactic.group.gpow_trick_sub {G : Type*} [group G] (a b : G) (n m : ℤ) : a*b^n*b^(-m) = a*b^(n-m) := by rw [mul_assoc, ← gpow_add] ; refl namespace tactic open tactic.simp_arg_type tactic.interactive interactive tactic.group. /-- Auxilliary tactic for the `group` tactic. Calls the simplifier only. -/ meta def aux_group₁ (locat : loc) : tactic unit := simp_core {} skip tt [ expr ``(mul_one), expr ``(one_mul), expr ``(one_pow), expr ``(one_gpow), expr ``(sub_self), expr ``(add_neg_self), expr ``(neg_add_self), expr ``(neg_neg), expr ``(nat.sub_self), expr ``(int.coe_nat_add), expr ``(int.coe_nat_mul), expr ``(int.coe_nat_zero), expr ``(int.coe_nat_one), expr ``(int.coe_nat_bit0), expr ``(int.coe_nat_bit1), expr ``(int.mul_neg_eq_neg_mul_symm), expr ``(int.neg_mul_eq_neg_mul_symm), symm_expr ``(gpow_coe_nat), symm_expr ``(gpow_neg_one), symm_expr ``(gpow_mul), symm_expr ``(gpow_add_one), symm_expr ``(gpow_one_add), symm_expr ``(gpow_add), expr ``(mul_gpow_neg_one), expr ``(gpow_zero), expr ``(mul_gpow), symm_expr ``(mul_assoc), expr ``(gpow_trick), expr ``(gpow_trick_one), expr ``(gpow_trick_one'), expr ``(gpow_trick_sub), expr ``(tactic.ring.horner)] [] locat /-- Auxilliary tactic for the `group` tactic. Calls `ring` to normalize exponents. -/ meta def aux_group₂ (locat : loc) : tactic unit := ring none tactic.ring.normalize_mode.raw locat end tactic namespace tactic.interactive setup_tactic_parser open tactic /-- Tactic for normalizing expressions in multiplicative groups, without assuming commutativity, using only the group axioms without any information about which group is manipulated. (For additive commutative groups, use the `abel` tactic instead.) Example: ```lean example {G : Type} [group G] (a b c d : G) (h : c = (a*b^2)*((b*b)⁻¹*a⁻¹)*d) : a*c*d⁻¹ = a := begin group at h, -- normalizes `h` which becomes `h : c = d` rw h, -- the goal is now `a*d*d⁻¹ = a` group, -- which then normalized and closed end ``` -/ meta def group (locat : parse location) : tactic unit := do when locat.include_goal `[rw ← mul_inv_eq_one], try (aux_group₁ locat), repeat (aux_group₂ locat ; aux_group₁ locat) end tactic.interactive add_tactic_doc { name := "group", category := doc_category.tactic, decl_names := [`tactic.interactive.group], tags := ["decision procedure", "simplification"] }
373ddfa8daf51015cfe024f7ebc57fbb7c7b73c8
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/core.lean
2f7f4b9d56169b8c0209ffebd4eb82083069eebb
[ "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
2,500
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 category_theory.groupoid import control.equiv_functor import category_theory.types namespace category_theory universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- The core of a category C is the groupoid whose morphisms are all the isomorphisms of C. -/ @[nolint has_inhabited_instance] def core (C : Type u₁) := C variables {C : Type u₁} [category.{v₁} C] instance core_category : groupoid.{v₁} (core C) := { hom := λ X Y : C, X ≅ Y, inv := λ X Y f, iso.symm f, id := λ X, iso.refl X, comp := λ X Y Z f g, iso.trans f g } namespace core @[simp] lemma id_hom (X : core C) : iso.hom (𝟙 X) = 𝟙 X := rfl @[simp] lemma comp_hom {X Y Z : core C} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).hom = f.hom ≫ g.hom := rfl /-- The core of a category is naturally included in the category. -/ def inclusion : core C ⥤ C := { obj := id, map := λ X Y f, f.hom } variables {G : Type u₂} [groupoid.{v₂} G] /-- A functor from a groupoid to a category C factors through the core of C. -/ -- Note that this function is not functorial -- (consider the two functors from [0] to [1], and the natural transformation between them). def functor_to_core (F : G ⥤ C) : G ⥤ core C := { obj := λ X, F.obj X, map := λ X Y f, ⟨F.map f, F.map (inv f)⟩ } /-- We can functorially associate to any functor from a groupoid to the core of a category `C`, a functor from the groupoid to `C`, simply by composing with the embedding `core C ⥤ C`. -/ def forget_functor_to_core : (G ⥤ core C) ⥤ (G ⥤ C) := (whiskering_right _ _ _).obj inclusion end core /-- `of_equiv_functor m` lifts a type-level `equiv_functor` to a categorical functor `core (Type u₁) ⥤ core (Type u₂)`. -/ def of_equiv_functor (m : Type u₁ → Type u₂) [equiv_functor m] : core (Type u₁) ⥤ core (Type u₂) := { obj := m, map := λ α β f, (equiv_functor.map_equiv m f.to_equiv).to_iso, -- These are not very pretty. map_id' := λ α, begin ext, exact (congr_fun (equiv_functor.map_refl _) x), end, map_comp' := λ α β γ f g, begin ext, simp only [equiv_functor.map_equiv_apply, equiv.to_iso_hom, function.comp_app, core.comp_hom, types_comp], erw [iso.to_equiv_comp, equiv_functor.map_trans], end, } end category_theory
5a0591da633939033f10d758d11cca5a34356956
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/instances/irrational.lean
7fcc884c550de14970d9eb27e2773324e277e677
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,328
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 data.real.irrational import topology.metric_space.baire /-! # Topology of irrational numbers In this file we prove the following theorems: * `is_Gδ_irrational`, `dense_irrational`, `eventually_residual_irrational`: irrational numbers form a dense Gδ set; * `irrational.eventually_forall_le_dist_cast_div`, `irrational.eventually_forall_le_dist_cast_div_of_denom_le`; `irrational.eventually_forall_le_dist_cast_rat_of_denom_le`: a sufficiently small neighborhood of an irrational number is disjoint with the set of rational numbers with bounded denominator. We also provide `order_topology`, `no_min_order`, `no_max_order`, and `densely_ordered` instances for `{x // irrational x}`. ## Tags irrational, residual -/ open set filter metric open_locale filter topological_space lemma is_Gδ_irrational : is_Gδ {x | irrational x} := (countable_range _).is_Gδ_compl lemma dense_irrational : dense {x : ℝ | irrational x} := begin refine real.is_topological_basis_Ioo_rat.dense_iff.2 _, simp only [mem_Union, mem_singleton_iff], rintro _ ⟨a, b, hlt, rfl⟩ hne, rw inter_comm, exact exists_irrational_btwn (rat.cast_lt.2 hlt) end lemma eventually_residual_irrational : ∀ᶠ x in residual ℝ, irrational x := eventually_residual.2 ⟨_, is_Gδ_irrational, dense_irrational, λ _, id⟩ namespace irrational variable {x : ℝ} instance : order_topology {x // irrational x} := induced_order_topology _ (λ x y, iff.rfl) $ λ x y hlt, let ⟨a, ha, hxa, hay⟩ := exists_irrational_btwn hlt in ⟨⟨a, ha⟩, hxa, hay⟩ instance : no_max_order {x // irrational x} := ⟨λ ⟨x, hx⟩, ⟨⟨x + (1 : ℕ), hx.add_nat 1⟩, by simp⟩⟩ instance : no_min_order {x // irrational x} := ⟨λ ⟨x, hx⟩, ⟨⟨x - (1 : ℕ), hx.sub_nat 1⟩, by simp⟩⟩ instance : densely_ordered {x // irrational x} := ⟨λ x y hlt, let ⟨z, hz, hxz, hzy⟩ := exists_irrational_btwn hlt in ⟨⟨z, hz⟩, hxz, hzy⟩⟩ lemma eventually_forall_le_dist_cast_div (hx : irrational x) (n : ℕ) : ∀ᶠ ε : ℝ in 𝓝 0, ∀ m : ℤ, ε ≤ dist x (m / n) := begin have A : is_closed (range (λ m, n⁻¹ * m : ℤ → ℝ)), from ((is_closed_map_smul₀ (n⁻¹ : ℝ)).comp int.closed_embedding_coe_real.is_closed_map).closed_range, have B : x ∉ range (λ m, n⁻¹ * m : ℤ → ℝ), { rintro ⟨m, rfl⟩, simpa using hx }, rcases metric.mem_nhds_iff.1 (A.is_open_compl.mem_nhds B) with ⟨ε, ε0, hε⟩, refine (ge_mem_nhds ε0).mono (λ δ hδ m, not_lt.1 $ λ hlt, _), rw dist_comm at hlt, refine hε (ball_subset_ball hδ hlt) ⟨m, _⟩, simp [div_eq_inv_mul] end lemma eventually_forall_le_dist_cast_div_of_denom_le (hx : irrational x) (n : ℕ) : ∀ᶠ ε : ℝ in 𝓝 0, ∀ (k ≤ n) (m : ℤ), ε ≤ dist x (m / k) := (finite_le_nat n).eventually_all.2 $ λ k hk, hx.eventually_forall_le_dist_cast_div k lemma eventually_forall_le_dist_cast_rat_of_denom_le (hx : irrational x) (n : ℕ) : ∀ᶠ ε : ℝ in 𝓝 0, ∀ r : ℚ, r.denom ≤ n → ε ≤ dist x r := (hx.eventually_forall_le_dist_cast_div_of_denom_le n).mono $ λ ε H r hr, H r.denom hr r.num end irrational
4c7e3593d5879786fb591e9e4a7770a93d8a7a13
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch4/ex0411.lean
988d7b209e4636cd07b3289b9516d5d3f0620daf
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
343
lean
open classical variables (α : Type) (p : α → Prop) example (h : ¬ ∀ x, ¬ p x) : ∃ x, p x := by_contradiction (assume h1 : ¬ ∃ x, p x, have h2 : ∀ x, ¬ p x, from assume x, assume h3 : p x, have h4 : ∃ x, p x, from ⟨x, h3⟩, show false, from h1 h4, show false, from h h2)
dc123f32c8a16dedcfa94782998068c0c912b188
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/measure_theory/simple_func_dense.lean
0eb6c0d0e0200ab4d6230d194e8c455e7f4ebca1
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
14,749
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou Show that each Borel measurable function can be approximated, both pointwise and in L¹ norm, by a sequence of simple functions. -/ import measure_theory.l1_space 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 open_locale measure_theory variables [measure_space α] [normed_group β] [second_countable_topology β] local infixr ` →ₛ `:25 := simple_func 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_add]; simp only [measurable.coe_nnnorm hfm, measurable.add] ... < ⊤ : 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_le_lintegral _ _ (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_add (measurable.coe_nnnorm hfm) (measurable.coe_nnnorm 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
3fd1622bf948f6ff58241729f30fd2b95a862e89
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/category/Module/kernels.lean
5a92d60c8bb4f417d540ceb38f424c80af28e1de
[]
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,499
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.category.Module.basic import Mathlib.PostPort universes u v u_1 namespace Mathlib /-! # The concrete (co)kernels in the category of modules are (co)kernels in the categorical sense. -/ namespace Module /-- The kernel cone induced by the concrete kernel. -/ def kernel_cone {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) : category_theory.limits.kernel_fork f := category_theory.limits.kernel_fork.of_ι (as_hom (submodule.subtype (linear_map.ker f))) sorry /-- The kernel of a linear map is a kernel in the categorical sense. -/ def kernel_is_limit {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) : category_theory.limits.is_limit (kernel_cone f) := category_theory.limits.fork.is_limit.mk (kernel_cone f) (fun (s : category_theory.limits.fork f 0) => linear_map.cod_restrict (linear_map.ker f) (category_theory.limits.fork.ι s) sorry) sorry sorry /-- The cokernel cocone induced by the projection onto the quotient. -/ def cokernel_cocone {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) : category_theory.limits.cokernel_cofork f := category_theory.limits.cokernel_cofork.of_π (as_hom (submodule.mkq (linear_map.range f))) sorry /-- The projection onto the quotient is a cokernel in the categorical sense. -/ def cokernel_is_colimit {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) : category_theory.limits.is_colimit (cokernel_cocone f) := category_theory.limits.cofork.is_colimit.mk (cokernel_cocone f) (fun (s : category_theory.limits.cofork f 0) => submodule.liftq (linear_map.range f) (category_theory.limits.cofork.π s) sorry) sorry sorry /-- The category of R-modules has kernels, given by the inclusion of the kernel submodule. -/ theorem has_kernels_Module {R : Type u} [ring R] : category_theory.limits.has_kernels (Module R) := category_theory.limits.has_kernels.mk fun (X Y : Module R) (f : X ⟶ Y) => category_theory.limits.has_limit.mk (category_theory.limits.limit_cone.mk (kernel_cone f) (kernel_is_limit f)) /-- The category or R-modules has cokernels, given by the projection onto the quotient. -/ theorem has_cokernels_Module {R : Type u} [ring R] : category_theory.limits.has_cokernels (Module R) := sorry
290b2602cc94094157c3a72641dc808e8f25454a
e0b0b1648286e442507eb62344760d5cd8d13f2d
/stage0/src/Init/Notation.lean
8ee8da61ac62597ccde809819e3d6a8d6975b83b
[ "Apache-2.0" ]
permissive
MULXCODE/lean4
743ed389e05e26e09c6a11d24607ad5a697db39b
4675817a9e89824eca37192364cd47a4027c6437
refs/heads/master
1,682,231,879,857
1,620,423,501,000
1,620,423,501,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,157
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 Notation for operators defined at Prelude.lean -/ prelude import Init.Prelude -- DSL for specifying parser precedences and priorities namespace Lean.Parser.Syntax syntax:65 (name := addPrec) prec " + " prec:66 : prec syntax:65 (name := subPrec) prec " - " prec:66 : prec syntax:65 (name := addPrio) prio " + " prio:66 : prio syntax:65 (name := subPrio) prio " - " prio:66 : prio end Lean.Parser.Syntax macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...) macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...) macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...) macro "(" p:prec ")" : prec => p macro "min" : prec => `(10) -- minimum precedence used in term parsers macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean` /- `max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`. We use `max_prec` to workaround bootstrapping issues. -/ macro "max_prec" : term => `(1024) macro "default" : prio => `(1000) macro "low" : prio => `(100) macro "mid" : prio => `(1000) macro "high" : prio => `(10000) macro "(" p:prio ")" : prio => p -- Basic notation for defining parsers syntax stx "+" : stx syntax stx "*" : stx syntax stx "?" : stx syntax:2 stx " <|> " stx:1 : stx macro_rules | `(stx| $p +) => `(stx| many1($p)) | `(stx| $p *) => `(stx| many($p)) | `(stx| $p ?) => `(stx| optional($p)) | `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂)) /- Comma-separated sequence. -/ macro:max x:stx ",*" : stx => `(stx| sepBy($x, ",", ", ")) macro:max x:stx ",+" : stx => `(stx| sepBy1($x, ",", ", ")) /- Comma-separated sequence with optional trailing comma. -/ macro:max x:stx ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep)) macro:max x:stx ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep)) macro "!" x:stx : stx => `(stx| notFollowedBy($x)) syntax (name := rawNatLit) "nat_lit " num : term infixr:90 " ∘ " => Function.comp infixr:35 " × " => Prod infixl:55 " ||| " => HOr.hOr infixl:58 " ^^^ " => HXor.hXor infixl:60 " &&& " => HAnd.hAnd infixl:65 " + " => HAdd.hAdd infixl:65 " - " => HSub.hSub infixl:70 " * " => HMul.hMul infixl:70 " / " => HDiv.hDiv infixl:70 " % " => HMod.hMod infixl:75 " <<< " => HShiftLeft.hShiftLeft infixl:75 " >>> " => HShiftRight.hShiftRight infixr:80 " ^ " => HPow.hPow prefix:100 "-" => Neg.neg prefix:100 "~~~" => Complement.complement /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators. It addresses issue #382. -/ macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y) macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y) macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y) macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y) macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y) macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y) macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y) macro_rules | `($x % $y) => `(binop% HMod.hMod $x $y) macro_rules | `($x <<< $y) => `(binop% HShiftLeft.hShiftLeft $x $y) macro_rules | `($x >>> $y) => `(binop% HShiftRight.hShiftRight $x $y) macro_rules | `($x ^ $y) => `(binop% HPow.hPow $x $y) -- declare ASCII alternatives first so that the latter Unicode unexpander wins infix:50 " <= " => LE.le infix:50 " ≤ " => LE.le infix:50 " < " => LT.lt infix:50 " >= " => GE.ge infix:50 " ≥ " => GE.ge infix:50 " > " => GT.gt infix:50 " = " => Eq infix:50 " == " => BEq.beq infix:50 " ~= " => HEq infix:50 " ≅ " => HEq /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations. It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and `i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but `binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/ macro_rules | `($x <= $y) => `(binrel% LE.le $x $y) macro_rules | `($x ≤ $y) => `(binrel% LE.le $x $y) macro_rules | `($x < $y) => `(binrel% LT.lt $x $y) macro_rules | `($x > $y) => `(binrel% GT.gt $x $y) macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y) macro_rules | `($x ≥ $y) => `(binrel% GE.ge $x $y) macro_rules | `($x = $y) => `(binrel% Eq $x $y) macro_rules | `($x == $y) => `(binrel% BEq.beq $x $y) infixr:35 " /\\ " => And infixr:35 " ∧ " => And infixr:30 " \\/ " => Or infixr:30 " ∨ " => Or notation:max "¬" p:40 => Not p infixl:35 " && " => and infixl:30 " || " => or notation:max "!" b:40 => not b infixl:65 " ++ " => HAppend.hAppend infixr:67 " :: " => List.cons infixr:20 " <|> " => HOrElse.hOrElse infixr:60 " >> " => HAndThen.hAndThen infixl:55 " >>= " => Bind.bind infixl:60 " <*> " => Seq.seq infixl:60 " <* " => SeqLeft.seqLeft infixr:60 " *> " => SeqRight.seqRight infixr:100 " <$> " => Functor.map syntax (name := termDepIfThenElse) ppGroup(ppDedent("if " ident " : " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $h:ident : $c then $t:term else $e:term) => ``(dite $c (fun $h:ident => $t) (fun $h:ident => $e)) syntax (name := termIfThenElse) ppGroup(ppDedent("if " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $c then $t:term else $e:term) => ``(ite $c $t $e) macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term => `(match $d:term with | $pat:term => $t | _ => $e) syntax:min term "<|" term:min : term macro_rules | `($f $args* <| $a) => let args := args.push a; `($f $args*) | `($f <| $a) => `($f $a) syntax:min term "|>" term:min1 : term macro_rules | `($a |> $f $args*) => let args := args.push a; `($f $args*) | `($a |> $f) => `($f $a) -- Haskell-like pipe <| -- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations. syntax:min term atomic("$" ws) term:min : term macro_rules | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) syntax "{ " ident (" : " term)? " // " term " }" : term macro_rules | `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p)) /- `without_expected_type t` instructs Lean to elaborate `t` without an expected type. Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until expected type is known. So, `without_expected_type` is not effective in this case. -/ macro "without_expected_type " x:term : term => `(let aux := $x; aux) syntax "[" term,* "]" : term syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals namespace Lean macro_rules | `([ $elems,* ]) => do let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do match i, skip with | 0, _ => pure result | i+1, true => expandListLit i false result | i+1, false => expandListLit i true (← ``(List.cons $(elems.elemsAndSeps[i]) $result)) if elems.elemsAndSeps.size < 64 then expandListLit elems.elemsAndSeps.size false (← ``(List.nil)) else `(%[ $elems,* | List.nil ]) namespace Parser.Tactic syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic syntax (name := rename) "rename " term " => " ident : tactic syntax (name := revert) "revert " (colGt ident)+ : tactic syntax (name := clear) "clear " (colGt ident)+ : tactic syntax (name := subst) "subst " (colGt ident)+ : tactic syntax (name := assumption) "assumption" : tactic syntax (name := contradiction) "contradiction" : tactic syntax (name := apply) "apply " term : tactic syntax (name := exact) "exact " term : tactic syntax (name := refine) "refine " term : tactic syntax (name := refine') "refine' " term : tactic syntax (name := constructor) "constructor" : tactic syntax (name := case) "case " ident (ident <|> "_")* " => " tacticSeq : tactic syntax (name := allGoals) "allGoals " tacticSeq : tactic syntax (name := focus) "focus " tacticSeq : tactic syntax (name := skip) "skip" : tactic syntax (name := done) "done" : tactic syntax (name := traceState) "traceState" : tactic syntax (name := failIfSuccess) "failIfSuccess " tacticSeq : tactic syntax (name := generalize) "generalize " atomic(ident " : ")? term:51 " = " ident : tactic syntax (name := paren) "(" tacticSeq ")" : tactic syntax (name := withReducible) "withReducible " tacticSeq : tactic syntax (name := withReducibleAndInstances) "withReducibleAndInstances " tacticSeq : tactic syntax (name := first) "first " "|"? sepBy1(tacticSeq, "|") : tactic syntax (name := rotateLeft) "rotateLeft" (num)? : tactic syntax (name := rotateRight) "rotateRight" (num)? : tactic macro "try " t:tacticSeq : tactic => `(first $t | skip) macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; allGoals $y:tactic)) macro "rfl" : tactic => `(exact rfl) macro "admit" : tactic => `(exact sorry) macro "inferInstance" : tactic => `(exact inferInstance) syntax locationWildcard := "*" syntax locationHyp := (colGt ident)+ ("⊢" <|> "|-")? -- TODO: delete syntax locationTargets := (colGt ident)+ ("⊢" <|> "|-")? syntax location := withPosition("at " locationWildcard <|> locationHyp) syntax (name := change) "change " term (location)? : tactic syntax (name := changeWith) "change " term " with " term (location)? : tactic syntax rwRule := ("←" <|> "<-")? term syntax rwRuleSeq := "[" rwRule,+,? "]" syntax (name := rewriteSeq) "rewrite " rwRuleSeq (location)? : tactic syntax (name := erewriteSeq) "erewrite " rwRuleSeq (location)? : tactic syntax (name := rwSeq) "rw " rwRuleSeq (location)? : tactic syntax (name := erwSeq) "erw " rwRuleSeq (location)? : tactic def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do -- We show the `rfl` state on `]` let seq := stx[1] let rbrak := seq[2] -- Replace `]` token with one without position information in the expanded tactic let seq := seq.setArg 2 (mkAtom "]") let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 1 seq `(tactic| $tac; try (withReducible rfl%$rbrak)) @[macro rwSeq] def expandRwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite" @[macro erwSeq] def expandERwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.erewriteSeq "erewrite" syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? term syntax simpErase := "-" ident syntax (name := simp) "simp " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? (location)? : tactic syntax (name := simpAll) "simp_all " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic -- Auxiliary macro for lifting have/suffices/let/... -- It makes sure the "continuation" `?_` is the main goal after refining macro "refineLift " e:term : tactic => `(focus (refine noImplicitLambda% $e; rotateRight)) macro "have " d:haveDecl : tactic => `(refineLift have $d:haveDecl; ?_) /- We use a priority > default, to avoid ambiguity with previous `have` notation -/ macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p) macro "suffices " d:sufficesDecl : tactic => `(refineLift suffices $d:sufficesDecl; ?_) macro "let " d:letDecl : tactic => `(refineLift let $d:letDecl; ?_) macro "show " e:term : tactic => `(refineLift show $e:term from ?_) syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic macro_rules | `(tactic| let rec $d:letRecDecls) => `(tactic| refineLift let rec $d:letRecDecls; ?_) -- Similar to `refineLift`, but using `refine'` macro "refineLift' " e:term : tactic => `(focus (refine' noImplicitLambda% $e; rotateRight)) macro "have' " d:haveDecl : tactic => `(refineLift' have $d:haveDecl; ?_) macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p) macro "let' " d:letDecl : tactic => `(refineLift' let $d:letDecl; ?_) syntax inductionAlt := "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq) syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic syntax casesTarget := atomic(ident " : ")? term syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic syntax (name := existsIntro) "exists " term : tactic syntax "repeat " tacticSeq : tactic macro_rules | `(tactic| repeat $seq) => `(tactic| first ($seq); repeat $seq | skip) syntax "trivial" : tactic macro_rules | `(tactic| trivial) => `(tactic| assumption) macro_rules | `(tactic| trivial) => `(tactic| rfl) macro_rules | `(tactic| trivial) => `(tactic| contradiction) macro_rules | `(tactic| trivial) => `(tactic| apply True.intro) macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial) macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq) end Tactic namespace Attr -- simp attribute syntax syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr end Attr end Parser end Lean macro "‹" type:term "›" : term => `((by assumption : $type))
1881ebba5448a9bdbff6755e1592c3b6da87e955
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Compiler/InitAttr.lean
dee6ce9cc4ae7e2f921d3c4e14827b2557108eab
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
2,410
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 -/ prelude import Init.Lean.Environment import Init.Lean.Attributes namespace Lean private def getIOTypeArg : Expr → Option Expr | Expr.app (Expr.const `IO _ _) arg _ => some arg | _ => none private def isUnitType : Expr → Bool | Expr.const `Unit _ _ => true | _ => false private def isIOUnit (type : Expr) : Bool := match getIOTypeArg type with | some type => isUnitType type | _ => false def mkInitAttr : IO (ParametricAttribute Name) := registerParametricAttribute `init "initialization procedure for global references" $ fun env declName stx => match env.find? declName with | none => Except.error "unknown declaration" | some decl => match attrParamSyntaxToIdentifier stx with | some initFnName => match env.find? initFnName with | none => Except.error ("unknown initialization function '" ++ toString initFnName ++ "'") | some initDecl => match getIOTypeArg initDecl.type with | none => Except.error ("initialization function '" ++ toString initFnName ++ "' must have type of the form `IO <type>`") | some initTypeArg => if decl.type == initTypeArg then Except.ok initFnName else Except.error ("initialization function '" ++ toString initFnName ++ "' type mismatch") | _ => match stx with | Syntax.missing => if isIOUnit decl.type then Except.ok Name.anonymous else Except.error "initialization function must have type `IO Unit`" | _ => Except.error "unexpected kind of argument" @[init mkInitAttr] constant initAttr : ParametricAttribute Name := arbitrary _ def isIOUnitInitFn (env : Environment) (fn : Name) : Bool := match initAttr.getParam env fn with | some Name.anonymous => true | _ => false @[export lean_get_init_fn_name_for] def getInitFnNameFor (env : Environment) (fn : Name) : Option Name := match initAttr.getParam env fn with | some Name.anonymous => none | some n => some n | _ => none def hasInitAttr (env : Environment) (fn : Name) : Bool := (getInitFnNameFor env fn).isSome def setInitAttr (env : Environment) (declName : Name) (initFnName : Name := Name.anonymous) : Except String Environment := initAttr.setParam env declName initFnName end Lean
cadb69df0e79d07998b7c42b7c0b59ce4367402f
0a7cf55e50e699ca449b876d32f72a58919da5b9
/src/lady_or_tiger.lean
3b7a5af1b136c523963035e366cb0d54e57ccf17
[]
no_license
stanescuUW/LeanPuzzles
afaa0a10c8d9cb8dc5651e1287e8d901d22b9e7c
5cd8d072ec88deb890b6cacd464bc198b7453ff8
refs/heads/master
1,667,707,546,355
1,593,179,367,000
1,593,179,367,000
273,576,407
3
0
null
null
null
null
UTF-8
Lean
false
false
5,410
lean
/- Copyright (c) 2020 Dan Stanescu. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dan Stanescu and Yuri G. Kudryashov. -/ import tactic /-! # Six logic puzzles. -/ /-- The first six puzzles from: "The Lady or the Tiger? And Other Logic Puzzles" by Raymond Smullyan. First three contributed to the Lean Zulip chat by Yury G. Kudryashov but apparently set up by his seven-years-old son. Slightly modified in appearance (for readability) but not in content by D. Stanescu. A king has prisoners choose between two rooms. The signs on the room doors offer enough information to allow them to make the right choice between rooms with ladies (i.e. presumably happiness) and rooms with tigers (i.e. presumably death). In the first three puzzles, the king explains that it is always the case that one of the door signs is true while the other one is false. -/ inductive door_leads_to | lady | tiger notation `y` := door_leads_to.lady notation `n` := door_leads_to.tiger structure Q := (d₁ d₂ : door_leads_to) /-- First puzzle: Sign on the first door indicates that there's a lady in this room and a tiger in the other room. Sign on second door indicates that in one of the rooms there's a lady and in one room a tiger. -/ def Q1 := Q namespace Q1 variables (q : Q1) -- Sign on first door def D1 := q.1 = y ∧ q.2 = n -- Sign on second door def D2 := (q.1 = y ∨ q.2 = y) ∧ (q.1 = n ∨ q.2 = n) def H := q.D1 ∧ ¬q.D2 ∨ ¬q.D1 ∧ q.D2 -- Terse proof. lemma answer_t : q.H → q.1 = n ∧ q.2 = y := by rcases q with ⟨_|_,_|_⟩; simp [H, D1, D2] -- Verbose proof. State changes can be seen by clicking after each comma. lemma answer_v : q.H → q.1 = n ∧ q.2 = y := begin rcases q with ⟨_|_,_|_⟩, simp [H], simp [D1], simp [D2], simp[H], simp [D1], simp [D2], simp [H], simp [H], simp [D1], simp [D2], done end end Q1 /-- Second puzzle: Sign on the first door indicates that at least one room contains a lady. Sign on second door indicates there's a tiger in the other room. -/ def Q2 := Q namespace Q2 variables (q : Q2) -- Sign on first door def D1 := q.1 = y ∨ q.2 = y -- Sign on second door def D2 := q.1 = n def H := q.D1 ∧ q.D2 ∨ ¬q.D1 ∧ ¬q.D2 lemma answer : q.H → q.1 = n ∧ q.2 = y := by rcases q with ⟨_|_,_|_⟩; simp [H, D1, D2] end Q2 /-- Third puzzle: Sign on the first door indicates that there's either a tiger in this room or a lady in the other. Sign on second door indicates there's a lady in the other room. -/ def Q3 := Q namespace Q3 variables (q : Q3) -- Sign on first door def D1 := q.1 = n ∨ q.2 = y -- Sign on second door def D2 := q.1 = y def H := q.D1 ∧ q.D2 ∨ ¬q.D1 ∧ ¬q.D2 lemma answer : q.H → q.1 = y ∧ q.2 = y := by rcases q with ⟨_|_,_|_⟩; simp [H, D1, D2] end Q3 /-! Puzzles 4-7 from the same book: "The Lady or the Tiger? And Other Logic Puzzles" by Raymond Smullyan. Solutions written by D. Stanescu in the same framework as above. In these puzzles, the king explains that the sign on the first door (D1) is true if a lady is in in that room and is false if a tiger is in that room. The opposite is true for the sign on the second door (D2), which is true if a tiger is hidden behind it but false otherwise. -/ /-- Puzzle number four: First door sign says that both rooms contain ladies. Second door sign is identical. -/ def Q4 := Q namespace Q4 variables (q : Q4) -- Sign on first door def D1 := q.1 = y ∧ q.2 = y -- Sign on second door def D2 := q.1 = y ∧ q.2 = y -- one way to set up this problem def H1 := q.1 = y ∧ q.D1 ∨ q.1 = n ∧ ¬ q.D1 def H2 := q.2 = y ∧ ¬ q.D2 ∨ q.2 = n ∧ q.D2 def H := q.H1 ∧ q.H2 -- Verbose proof lemma answer_v : q.H → q.1 = n ∧ q.2 = y := begin rcases q with ⟨_|_,_|_⟩, simp [H], simp [H1], simp [D1], simp [H2], simp [D2], simp [H], simp [H1], simp [D1], simp [H], simp [H1], simp [H], simp [H1], simp [D1], simp [H2], simp [D2], done end -- Terse proof lemma answer_t : q.H → q.1 = n ∧ q.2 = y := begin rcases q with ⟨_|_,_|_⟩; simp [H, H1, D1, H2, D2], done end end Q4 /-- Puzzle number five: First door sign says that at least one room contains a lady. Second door sign says : "In the other room there is a lady." -/ def Q5 := Q namespace Q5 variables (q : Q5) -- Sign on first door def D1 := q.1 = y ∨ q.2 = y ∨ q.1 = y ∧ q.2 = y -- Sign on second door def D2 := q.1 = y -- same setup as Q4 above def H1 := q.1 = y ∧ q.D1 ∨ q.1 = n ∧ ¬ q.D1 def H2 := q.2 = y ∧ ¬ q.D2 ∨ q.2 = n ∧ q.D2 def H := q.H1 ∧ q.H2 lemma answer : q.H → q.1 = y ∧ q.2 = n := begin rcases q with ⟨_|_,_|_⟩; simp [H, H1, D1, H2, D2], done end end Q5 /-- Puzzle number six: First door sign says that it makes no difference which room the prisoner picks. Second door sign is the same as in the previous puzzle. Apparently the king is particularly fond of this puzzle. -/ def Q6 := Q namespace Q6 variables (q : Q6) -- Sign on first door def D1 := q.1 = q.2 -- Sign on second door def D2 := q.1 = y -- same setup as Q4 above def H1 := q.1 = y ∧ q.D1 ∨ q.1 = n ∧ ¬ q.D1 def H2 := q.2 = y ∧ ¬ q.D2 ∨ q.2 = n ∧ q.D2 def H := q.H1 ∧ q.H2 lemma answer : q.H → q.1 = n ∧ q.2 = y := begin rcases q with ⟨_|_,_|_⟩; simp [H, H1, D1, H2, D2], done end end Q6
5b30de9de421ddf713a217e12987522bf4c25d2d
297c4ceafbbaed2a59b6215504d09e6bf201a2ee
/lemmas.lean
069cee9390db4fbacfa6487ba664bc3fa06fe0d9
[]
no_license
minchaowu/Kruskal.lean3
559c91b82033ce44ea61593adcec9cfff725c88d
a14516f47b21e636e9df914fc6ebe64cbe5cd38d
refs/heads/master
1,611,010,001,429
1,497,935,421,000
1,497,935,421,000
82,000,982
1
0
null
null
null
null
UTF-8
Lean
false
false
3,985
lean
import tools.super .card open classical nat function set tactic section -- some facts about sets in the old library parameter {X : Type} -- theorem ext {a b : set X} (H : ∀x, x ∈ a ↔ x ∈ b) : a = b := funext (λ x, propext (H x)) -- theorem not_mem_empty (x : X) : ¬ (x ∈ (∅ : set X)) := λ H, H -- theorem eq_empty_of_forall_not_mem {s : set X} (H : ∀ x, x ∉ s) : s = ∅ := -- set.ext (λ x, iff.intro (λ xs, absurd xs (H x)) (λ xe, absurd xe (set.not_mem_empty _))) -- theorem exists_mem_of_ne_empty {s : set X} (H : s ≠ ∅) : ∃ x, x ∈ s := -- by_contradiction (λ H', H (eq_empty_of_forall_not_mem (forall_not_of_not_exists H'))) end section -- the least number principle. def complete_induction_on (n : ℕ) {p : ℕ → Prop} (h : ∀ n, (∀ m < n, p m) → p n) : p n := suffices ∀ n, ∀ m < n, p m, from this (succ n) _ (lt_succ_self n), take n, nat.rec_on n (take m, suppose m < 0, absurd this (not_lt_zero m)) (take n, assume ih : ∀ m < n, p m, take m, suppose m < succ n, or.elim (lt_or_eq_of_le (le_of_lt_succ this)) (ih m) (suppose m = n, begin rw this, apply h, exact ih end)) lemma wf_aux {A : set ℕ} (n : ℕ) : n ∈ A → ∃ a, a ∈ A ∧ ∀ b, b ∈ A → a ≤ b := @complete_induction_on n (λ x, x ∈ A → ∃ a, a ∈ A ∧ ∀ b, b ∈ A → a ≤ b) (λ k ih h, by_cases (suppose ∃ m, m ∈ A ∧ m <k, let ⟨m, Hmem, Hlt⟩ := this in ih m Hlt Hmem) (λ Hn, have ∀ m, m ∈ A → ¬ m < k, by super, ⟨k,h,(λ m h, le_of_not_gt $ this m h)⟩)) theorem wf_of_le (S : set ℕ) (H : S ≠ ∅) : ∃ a, a ∈ S ∧ ∀ b, b ∈ S → a ≤ b := let ⟨n,Hn⟩ := exists_mem_of_ne_empty H in wf_aux n Hn noncomputable definition least (S : set ℕ) (H : S ≠ ∅) : ℕ := some (wf_of_le S H) theorem least_is_mem (S : set ℕ) (H : S ≠ ∅) : least S H ∈ S := let ⟨bound, Ha⟩ := some_spec (wf_of_le S H) in bound theorem minimality {S : set ℕ} (neq : S ≠ ∅): ∀ x, x ∈ S → least S neq ≤ x := λ x Hx, let ⟨bound, Ha⟩ := some_spec (wf_of_le S neq) in Ha x Hx end lemma nonzero_card_of_finite {A : Type} {S : set A} (H : card S ≠ 0) : finite S := by_contradiction (suppose ¬ finite S, have card S = 0, from card_of_not_finite this, H this) lemma mem_not_in_diff {A : Type} {S : set A} {a : A} : a ∉ S \ insert a (∅ : set A) := assume h, have a ∉ insert a (∅ : set A), from not_mem_of_mem_diff h, this (mem_singleton a) lemma insert_of_diff_singleton {A : Type} {S : set A} {a : A} (H : a ∈ S) : insert a (S \ insert a (∅ : set A)) = S := begin apply eq_of_subset_of_subset, intros x h, apply or.elim h, intro h1, simp [H, h1], -- simp, intro hr, apply and.left hr, intros x h', cases (decidable.em (x ∈ insert a (∅ : set A))), apply or.inl, apply eq_of_mem_singleton, assumption, apply or.inr, apply and.intro, repeat {assumption} end -- lemma union_of_diff_singleton {A : Type} {S : set A} {a : A} (H : a ∈ S) -- : S \ (insert a (∅ : set A)) ∪ (insert a (∅ : set A)) = S := -- begin -- apply eq_of_subset_of_subset, -- intros x h, apply or.elim h, intro hl, apply and.left hl, -- intro hr, have h : x = a, from (mem_singleton_iff x a)^.mp hr, -- rewrite h, simp, -- intros x h', cases (decidable.em (x ∈ insert a (∅ : set A))), -- apply or.inr, simp, apply or.inl, apply and.intro, repeat {simp} -- end -- lemma finite_singleton {A : Type} {a : A} : finite '{a} := -- have carda : card '{a} = 1, from card_singleton a, -- have (1:ℕ) ≠ 0, from dec_trivial, -- have card '{a} ≠ 0, by+ rewrite -carda at this;exact this, -- nonzero_card_of_finite this -- lemma sub_of_eq {A : Type} {S T: set A} (H : S = T) : S ⊆ T := -- have T ⊆ T, from subset.refl T, -- by+ rewrite -H at this{1};exact this -- theorem ne_empty_of_mem' {X : Type} {s : set X} {x : X} (H : x ∈ s) : s ≠ ∅ := -- begin intro Hs, rewrite Hs at H, apply not_mem_empty _ H end --this is on github
dcc12f43039c94dbf5bfab147d95c14601476c4d
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/algebra/group_with_zero.lean
1ee0e7dd838852512611889a64699c2be83ab022
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
18,654
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 algebra.group.units /-! # G₀roups with an adjoined zero element This file describes structures that are not usually studied on their own right in mathematics, namely a special sort of monoid: apart from a distinguished “zero element” they form a group, or in other words, they are groups with an adjoined zero element. Examples are: * division rings; * the value monoid of a multiplicative valuation; * in particular, the non-negative real numbers. ## Main definitions * `group_with_zero` * `comm_group_with_zero` ## Implementation details As is usual in mathlib, we extend the inverse function to the zero element, and require `0⁻¹ = 0`. -/ set_option old_structure_cmd true section prio set_option default_priority 10 -- see Note [default priority] /-- A type `M` is a “monoid with zero” if it is a monoid with zero element. -/ class monoid_with_zero (G₀ : Type*) extends monoid G₀, mul_zero_class G₀. /-- A type `M` is a commutative “monoid with zero” if it is a commutative monoid with zero element. -/ class comm_monoid_with_zero (G₀ : Type*) extends monoid G₀, mul_zero_class G₀. /-- A type `G₀` is a “group with zero” if it is a monoid with zero element (distinct from `1`) such that every nonzero element is invertible. The type is required to come with an “inverse” function, and the inverse of `0` must be `0`. Examples include division rings and the ordered monoids that are the target of valuations in general valuation theory.-/ class group_with_zero (G₀ : Type*) extends monoid_with_zero G₀, has_inv G₀, zero_ne_one_class G₀ := (inv_zero : (0 : G₀)⁻¹ = 0) (mul_inv_cancel : ∀ a:G₀, a ≠ 0 → a * a⁻¹ = 1) /-- A type `G₀` is a commutative “group with zero” if it is a commutative monoid with zero element (distinct from `1`) such that every nonzero element is invertible. The type is required to come with an “inverse” function, and the inverse of `0` must be `0`. -/ class comm_group_with_zero (G₀ : Type*) extends comm_monoid G₀, group_with_zero G₀. /-- The division operation on a group with zero element. -/ instance group_with_zero.has_div {G₀ : Type*} [group_with_zero G₀] : has_div G₀ := ⟨λ g h, g * h⁻¹⟩ end prio lemma div_eq_mul_inv {G₀ : Type*} [group_with_zero G₀] {a b : G₀} : a / b = a * b⁻¹ := rfl section group_with_zero variables {G₀ : Type*} [group_with_zero G₀] @[simp] lemma inv_zero' : (0 : G₀)⁻¹ = 0 := group_with_zero.inv_zero @[simp] lemma mul_inv_cancel' (a : G₀) (h : a ≠ 0) : a * a⁻¹ = 1 := group_with_zero.mul_inv_cancel a h @[simp] lemma mul_inv_cancel_assoc_left (a b : G₀) (h : b ≠ 0) : (a * b) * b⁻¹ = a := calc (a * b) * b⁻¹ = a * (b * b⁻¹) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma mul_inv_cancel_assoc_right (a b : G₀) (h : a ≠ 0) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = (a * a⁻¹) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] lemma inv_ne_zero' {a : G₀} (h : a ≠ 0) : a⁻¹ ≠ 0 := assume a_eq_0, by simpa [a_eq_0] using mul_inv_cancel' a h @[simp] lemma inv_mul_cancel' (a : G₀) (h : a ≠ 0) : a⁻¹ * a = 1 := calc a⁻¹ * a = (a⁻¹ * a) * a⁻¹ * a⁻¹⁻¹ : by simp [inv_ne_zero' h] ... = a⁻¹ * a⁻¹⁻¹ : by simp [h] ... = 1 : by simp [inv_ne_zero' h] @[simp] lemma inv_mul_cancel_assoc_left (a b : G₀) (h : b ≠ 0) : (a * b⁻¹) * b = a := calc (a * b⁻¹) * b = a * (b⁻¹ * b) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma inv_mul_cancel_assoc_right (a b : G₀) (h : a ≠ 0) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = (a⁻¹ * a) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] @[simp] lemma inv_inv'' (a : G₀) : a⁻¹⁻¹ = a := begin classical, by_cases h : a = 0, { simp [h] }, calc a⁻¹⁻¹ = a * (a⁻¹ * a⁻¹⁻¹) : by simp [h] ... = a : by simp [inv_ne_zero' h] end lemma inv_involutive' : function.involutive (has_inv.inv : G₀ → G₀) := inv_inv'' lemma ne_zero_of_mul_right_eq_one' (a b : G₀) (h : a * b = 1) : a ≠ 0 := assume a_eq_0, zero_ne_one (by simpa [a_eq_0] using h : (0:G₀) = 1) lemma ne_zero_of_mul_left_eq_one' (a b : G₀) (h : a * b = 1) : b ≠ 0 := assume b_eq_0, zero_ne_one (by simpa [b_eq_0] using h : (0:G₀) = 1) lemma eq_inv_of_mul_right_eq_one' (a b : G₀) (h : a * b = 1) : b = a⁻¹ := calc b = a⁻¹ * (a * b) : (inv_mul_cancel_assoc_right _ _ $ ne_zero_of_mul_right_eq_one' a b h).symm ... = a⁻¹ : by simp [h] lemma eq_inv_of_mul_left_eq_one' (a b : G₀) (h : a * b = 1) : a = b⁻¹ := calc a = (a * b) * b⁻¹ : (mul_inv_cancel_assoc_left _ _ (ne_zero_of_mul_left_eq_one' a b h)).symm ... = b⁻¹ : by simp [h] @[simp] lemma inv_one' : (1 : G₀)⁻¹ = 1 := eq.symm $ eq_inv_of_mul_right_eq_one' _ _ (mul_one 1) lemma inv_injective' : function.injective (@has_inv.inv G₀ _) := inv_involutive'.injective @[simp] lemma inv_inj'' (g h : G₀) : g⁻¹ = h⁻¹ ↔ g = h := ⟨assume H, inv_injective' H, congr_arg _⟩ lemma inv_eq_iff {g h : G₀} : g⁻¹ = h ↔ h⁻¹ = g := by rw [← inv_inj'', eq_comm, inv_inv''] @[simp] lemma coe_unit_mul_inv' (a : units G₀) : (a : G₀) * a⁻¹ = 1 := mul_inv_cancel' _ $ ne_zero_of_mul_right_eq_one' _ (a⁻¹ : units G₀) $ by simp @[simp] lemma coe_unit_inv_mul' (a : units G₀) : (a⁻¹ : G₀) * a = 1 := inv_mul_cancel' _ $ ne_zero_of_mul_right_eq_one' _ (a⁻¹ : units G₀) $ by simp @[simp] lemma unit_ne_zero (a : units G₀) : (a : G₀) ≠ 0 := assume a_eq_0, zero_ne_one $ calc 0 = (a : G₀) * a⁻¹ : by simp [a_eq_0] ... = 1 : by simp end group_with_zero namespace units variables {G₀ : Type*} [group_with_zero G₀] variables {a b : G₀} /-- Embed a non-zero element of a `group_with_zero` into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : G₀) (ha : a ≠ 0) : units G₀ := ⟨a, a⁻¹, mul_inv_cancel' _ ha, inv_mul_cancel' _ ha⟩ @[simp] lemma coe_mk0 {a : G₀} (h : a ≠ 0) : (mk0 a h : G₀) = a := rfl @[simp] theorem inv_eq_inv (u : units G₀) : (↑u⁻¹ : G₀) = u⁻¹ := (mul_left_inj u).1 $ by { rw [units.mul_inv, mul_inv_cancel'], apply unit_ne_zero } @[simp] lemma mk0_coe (u : units G₀) (h : (u : G₀) ≠ 0) : mk0 (u : G₀) h = u := units.ext rfl @[simp] lemma mk0_inj {a b : G₀} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ end units section group_with_zero variables {G₀ : Type*} [group_with_zero G₀] lemma mul_eq_zero' (a b : G₀) (h : a * b = 0) : a = 0 ∨ b = 0 := begin classical, contrapose! h, exact unit_ne_zero ((units.mk0 a h.1) * (units.mk0 b h.2)) end section prio set_option default_priority 10 -- see Note [default priority] instance group_with_zero.no_zero_divisors : no_zero_divisors G₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := mul_eq_zero', .. (‹_› : group_with_zero G₀) } end prio @[simp] lemma mul_eq_zero_iff' {a b : G₀} : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨mul_eq_zero' a b, by rintro (H|H); simp [H]⟩ lemma mul_ne_zero'' {a b : G₀} (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := begin assume ab0, rw mul_eq_zero_iff' at ab0, cases ab0; contradiction end lemma mul_ne_zero_iff {a b : G₀} : a * b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := by { classical, rw ← not_iff_not, push_neg, exact mul_eq_zero_iff' } lemma mul_left_cancel' {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : x * y = x * z) : y = z := calc y = x⁻¹ * (x * y) : by rw inv_mul_cancel_assoc_right _ _ hx ... = x⁻¹ * (x * z) : by rw h ... = z : by rw inv_mul_cancel_assoc_right _ _ hx lemma mul_right_cancel' {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : y * x = z * x) : y = z := calc y = (y * x) * x⁻¹ : by rw mul_inv_cancel_assoc_left _ _ hx ... = (z * x) * x⁻¹ : by rw h ... = z : by rw mul_inv_cancel_assoc_left _ _ hx lemma mul_inv_eq_of_eq_mul' {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : y = z * x) : y * x⁻¹ = z := h.symm ▸ (mul_inv_cancel_assoc_left z x hx) lemma eq_mul_inv_of_mul_eq' {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : z * x = y) : z = y * x⁻¹ := eq.symm $ mul_inv_eq_of_eq_mul' hx h.symm lemma mul_inv_rev' (x y : G₀) : (x * y)⁻¹ = y⁻¹ * x⁻¹ := begin classical, by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, symmetry, apply eq_inv_of_mul_left_eq_one', simp [mul_assoc, hx, hy] end theorem inv_comm_of_comm' {a b : G₀} (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:G₀, a⁻¹ * x * a⁻¹) H.symm, classical, by_cases h : a = 0, { rw [h, inv_zero', zero_mul, mul_zero] }, rwa [inv_mul_cancel_assoc_right _ _ h, mul_assoc, mul_inv_cancel_assoc_left _ _ h] at this, end @[simp] lemma div_self' {a : G₀} (h : a ≠ 0) : a / a = 1 := mul_inv_cancel' _ h @[simp] lemma div_one' (a : G₀) : a / 1 = a := show a * 1⁻¹ = a, by rw [inv_one', mul_one] lemma one_div (a : G₀) : 1 / a = a⁻¹ := one_mul _ @[simp] lemma zero_div' (a : G₀) : 0 / a = 0 := zero_mul _ @[simp] lemma div_zero' (a : G₀) : a / 0 = 0 := show a * 0⁻¹ = 0, by rw [inv_zero', mul_zero] @[simp] lemma div_mul_cancel' (a : G₀) {b : G₀} (h : b ≠ 0) : a / b * b = a := inv_mul_cancel_assoc_left a b h @[simp] lemma mul_div_cancel'' (a : G₀) {b : G₀} (h : b ≠ 0) : a * b / b = a := mul_inv_cancel_assoc_left a b h lemma mul_div_assoc'' {a b c : G₀} : a * b / c = a * (b / c) := mul_assoc _ _ _ local attribute [simp] div_eq_mul_inv mul_comm mul_assoc mul_left_comm mul_inv_cancel inv_mul_cancel lemma div_eq_mul_one_div' (a b : G₀) : a / b = a * (1 / b) := by simp lemma mul_one_div_cancel' {a : G₀} (h : a ≠ 0) : a * (1 / a) = 1 := by simp [h] lemma one_div_mul_cancel' {a : G₀} (h : a ≠ 0) : (1 / a) * a = 1 := by simp [h] lemma one_div_one' : 1 / 1 = (1:G₀) := div_self' (ne.symm zero_ne_one) lemma one_div_ne_zero' {a : G₀} (h : a ≠ 0) : 1 / a ≠ 0 := assume : 1 / a = 0, have 0 = (1:G₀), from eq.symm (by rw [← mul_one_div_cancel' h, this, mul_zero]), absurd this zero_ne_one lemma mul_ne_zero_comm'' {a b : G₀} (h : a * b ≠ 0) : b * a ≠ 0 := by { rw mul_ne_zero_iff at h ⊢, rwa and_comm } lemma eq_one_div_of_mul_eq_one' {a b : G₀} (h : a * b = 1) : b = 1 / a := have a ≠ 0, from assume : a = 0, have 0 = (1:G₀), by rwa [this, zero_mul] at h, absurd this zero_ne_one, have b = (1 / a) * a * b, by rw [one_div_mul_cancel' this, one_mul], show b = 1 / a, by rwa [mul_assoc, h, mul_one] at this lemma eq_one_div_of_mul_eq_one_left' {a b : G₀} (h : b * a = 1) : b = 1 / a := have a ≠ 0, from assume : a = 0, have 0 = (1:G₀), by rwa [this, mul_zero] at h, absurd this zero_ne_one, by rw [← h, mul_div_assoc'', div_self' this, mul_one] @[simp] lemma one_div_div' (a b : G₀) : 1 / (a / b) = b / a := by rw [one_div, div_eq_mul_inv, mul_inv_rev', inv_inv'', div_eq_mul_inv] @[simp] lemma one_div_one_div' (a : G₀) : 1 / (1 / a) = a := by simp lemma eq_of_one_div_eq_one_div' {a b : G₀} (h : 1 / a = 1 / b) : a = b := by rw [← one_div_one_div' a, h, one_div_one_div'] end group_with_zero section group_with_zero variables {G₀ : Type*} [group_with_zero G₀] variables {a b c : G₀} @[simp] lemma inv_eq_zero {a : G₀} : a⁻¹ = 0 ↔ a = 0 := by rw [inv_eq_iff, inv_zero', eq_comm] lemma one_div_mul_one_div_rev (a b : G₀) : (1 / a) * (1 / b) = 1 / (b * a) := by simp only [div_eq_mul_inv, one_mul, mul_inv_rev'] theorem divp_eq_div (a : G₀) (u : units G₀) : a /ₚ u = a / u := congr_arg _ $ units.inv_eq_inv _ @[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma inv_div : (a / b)⁻¹ = b / a := (mul_inv_rev' _ _).trans (by rw inv_inv''; refl) lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ := (mul_inv_rev' _ _).symm lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := mul_ne_zero'' ha (inv_ne_zero' hb) lemma div_ne_zero_iff (hb : b ≠ 0) : a / b ≠ 0 ↔ a ≠ 0 := ⟨mt (λ h, by rw [h, zero_div']), λ ha, div_ne_zero ha hb⟩ lemma div_eq_zero_iff (hb : b ≠ 0) : a / b = 0 ↔ a = 0 := by haveI := classical.prop_decidable; exact not_iff_not.1 (div_ne_zero_iff hb) lemma div_right_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_right_inj] lemma mul_right_inj' (hc : c ≠ 0) : a * c = b * c ↔ a = b := by rw [← inv_inv'' c, ← div_eq_mul_inv, ← div_eq_mul_inv, div_right_inj' (inv_ne_zero' hc)] lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := ⟨λ h, by rw [← h, div_mul_cancel' _ hb], λ h, by rw [← h, mul_div_cancel'' _ hb]⟩ end group_with_zero section comm_group_with_zero -- comm variables {G₀ : Type*} [comm_group_with_zero G₀] {a b c : G₀} lemma mul_inv'' : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev', mul_comm] lemma one_div_mul_one_div' (a b : G₀) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [one_div_mul_one_div_rev, mul_comm b] lemma div_mul_right' {a : G₀} (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := eq.symm (calc 1 / b = a * ((1 / a) * (1 / b)) : by rw [← mul_assoc, one_div a, mul_inv_cancel' a ha, one_mul] ... = a * (1 / (b * a)) : by rw one_div_mul_one_div_rev ... = a * (a * b)⁻¹ : by rw [← one_div, mul_comm a b]) lemma div_mul_left' {a b : G₀} (hb : b ≠ 0) : b / (a * b) = 1 / a := by rw [mul_comm a, div_mul_right' _ hb] lemma mul_div_cancel_left' {a : G₀} (b : G₀) (ha : a ≠ 0) : a * b / a = b := by rw [mul_comm a, (mul_div_cancel'' _ ha)] lemma mul_div_cancel''' (a : G₀) {b : G₀} (hb : b ≠ 0) : b * (a / b) = a := by rw [mul_comm, (div_mul_cancel' _ hb)] local attribute [simp] mul_assoc mul_comm mul_left_comm lemma div_mul_div' (a b c d : G₀) : (a / b) * (c / d) = (a * c) / (b * d) := by { simp [div_eq_mul_inv], rw [mul_inv_rev', mul_comm d⁻¹] } lemma mul_div_mul_left' (a b : G₀) {c : G₀} (hc : c ≠ 0) : (c * a) / (c * b) = a / b := by rw [← div_mul_div', div_self' hc, one_mul] lemma mul_div_mul_right' (a b : G₀) {c : G₀} (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rw [mul_comm a, mul_comm b, mul_div_mul_left' _ _ hc] lemma div_mul_eq_mul_div' (a b c : G₀) : (b / c) * a = (b * a) / c := by simp [div_eq_mul_inv] lemma div_mul_eq_mul_div_comm' (a b c : G₀) : (b / c) * a = b * (a / c) := by rw [div_mul_eq_mul_div', ← one_mul c, ← div_mul_div', div_one', one_mul] lemma mul_eq_mul_of_div_eq_div' (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [← mul_one (a*d), mul_assoc, mul_comm d, ← mul_assoc, ← div_self' hb, ← div_mul_eq_mul_div_comm', h, div_mul_eq_mul_div', div_mul_cancel' _ hd] lemma div_div_eq_mul_div' (a b c : G₀) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div', one_div_div', ← mul_div_assoc''] lemma div_div_eq_div_mul' (a b c : G₀) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div', div_mul_div', mul_one] lemma div_div_div_div_eq' (a : G₀) {b c d : G₀} : (a / b) / (c / d) = (a * d) / (b * c) := by rw [div_div_eq_mul_div', div_mul_eq_mul_div', div_div_eq_div_mul'] lemma div_mul_eq_div_mul_one_div' (a b c : G₀) : a / (b * c) = (a / b) * (1 / c) := by rw [← div_div_eq_div_mul', ← div_eq_mul_one_div'] lemma eq_of_mul_eq_mul_of_nonzero_left' {a b c : G₀} (h : a ≠ 0) (h₂ : a * b = a * c) : b = c := by rw [← one_mul b, ← div_self' h, div_mul_eq_mul_div', h₂, mul_div_cancel_left' _ h] lemma eq_of_mul_eq_mul_of_nonzero_right' {a b c : G₀} (h : c ≠ 0) (h2 : a * c = b * c) : a = b := by rw [← mul_one a, ← div_self' h, ← mul_div_assoc'', h2, mul_div_cancel'' _ h] lemma ne_zero_of_one_div_ne_zero' {a : G₀} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, div_zero'] at h, contradiction end lemma eq_zero_of_one_div_eq_zero' {a : G₀} (h : 1 / a = 0) : a = 0 := classical.by_cases (assume ha, ha) (assume ha, false.elim ((one_div_ne_zero' ha) h)) lemma div_helper' {a : G₀} (b : G₀) (h : a ≠ 0) : (1 / (a * b)) * a = 1 / b := by rw [div_mul_eq_mul_div', one_mul, div_mul_right' _ h] end comm_group_with_zero section comm_group_with_zero variables {G₀ : Type*} [comm_group_with_zero G₀] {a b c d : G₀} lemma div_eq_inv_mul' : a / b = b⁻¹ * a := mul_comm _ _ lemma mul_div_right_comm (a b c : G₀) : (a * b) / c = (a / c) * b := by rw [div_eq_mul_inv, mul_assoc, mul_comm b, ← mul_assoc]; refl lemma mul_comm_div' (a b c : G₀) : (a / b) * c = a * (c / b) := by rw [← mul_div_assoc'', mul_div_right_comm] lemma div_mul_comm' (a b c : G₀) : (a / b) * c = (c / b) * a := by rw [div_mul_eq_mul_div', mul_comm, mul_div_right_comm] lemma mul_div_comm (a b c : G₀) : a * (b / c) = b * (a / c) := by rw [← mul_div_assoc'', mul_comm, mul_div_assoc''] lemma div_right_comm' (a : G₀) : (a / b) / c = (a / c) / b := by rw [div_div_eq_div_mul', div_div_eq_div_mul', mul_comm] lemma div_div_div_cancel_right' (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [div_div_eq_mul_div', div_mul_cancel' _ hc] lemma div_mul_div_cancel' (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc'', div_mul_cancel' _ hc] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := calc a / b = c / d ↔ a / b * (b * d) = c / d * (b * d) : by rw [mul_right_inj' (mul_ne_zero'' hb hd)] ... ↔ a * d = c * b : by rw [← mul_assoc, div_mul_cancel' _ hb, ← mul_assoc, mul_right_comm, div_mul_cancel' _ hd] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff _ _ a b c 1 hb one_ne_zero lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff _ _ c 1 a b one_ne_zero hb lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := by rw [div_eq_mul_inv, inv_div, mul_div_cancel''' _ ha] end comm_group_with_zero
3f341b7fa3fb7a01383f1fa096bd6e025059002b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/deprecated/ring.lean
1bf7bdd5eabed7c765c797a44ae7787b6295bd1f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,150
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.deprecated.group import Mathlib.PostPort universes u v l u_1 u_2 namespace Mathlib /-! # Unbundled semiring and ring homomorphisms (deprecated) This file defines typeclasses for unbundled semiring and ring homomorphisms. Though these classes are deprecated, they are still widely used in mathlib, and probably will not go away before Lean 4 because Lean 3 often fails to coerce a bundled homomorphism to a function. ## main definitions is_semiring_hom (deprecated), is_ring_hom (deprecated) ## Tags is_semiring_hom, is_ring_hom -/ /-- Predicate for semiring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ class is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) where map_zero : f 0 = 0 map_one : f 1 = 1 map_add : ∀ {x y : α}, f (x + y) = f x + f y map_mul : ∀ {x y : α}, f (x * y) = f x * f y namespace is_semiring_hom /-- The identity map is a semiring homomorphism. -/ protected instance id {α : Type u} [semiring α] : is_semiring_hom id := mk (Eq.refl (id 0)) (Eq.refl (id 1)) (fun (x y : α) => Eq.refl (id (x + y))) fun (x y : α) => Eq.refl (id (x * y)) /-- The composition of two semiring homomorphisms is a semiring homomorphism. -/ -- see Note [no instance on morphisms] theorem comp {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] {γ : Type u_1} [semiring γ] (g : β → γ) [is_semiring_hom g] : is_semiring_hom (g ∘ f) := sorry /-- A semiring homomorphism is an additive monoid homomorphism. -/ protected instance is_add_monoid_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] : is_add_monoid_hom f := is_add_monoid_hom.mk (map_zero f) /-- A semiring homomorphism is a monoid homomorphism. -/ protected instance is_monoid_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] : is_monoid_hom f := is_monoid_hom.mk (map_one f) end is_semiring_hom /-- Predicate for ring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ class is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) where map_one : f 1 = 1 map_mul : ∀ {x y : α}, f (x * y) = f x * f y map_add : ∀ {x y : α}, f (x + y) = f x + f y namespace is_ring_hom /-- A map of rings that is a semiring homomorphism is also a ring homomorphism. -/ theorem of_semiring {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [H : is_semiring_hom f] : is_ring_hom f := mk (is_semiring_hom.map_one f) (is_semiring_hom.map_mul f) (is_semiring_hom.map_add f) /-- Ring homomorphisms map zero to zero. -/ theorem map_zero {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] : f 0 = 0 := sorry /-- Ring homomorphisms preserve additive inverses. -/ theorem map_neg {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] {x : α} : f (-x) = -f x := sorry /-- Ring homomorphisms preserve subtraction. -/ theorem map_sub {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] {x : α} {y : α} : f (x - y) = f x - f y := sorry /-- The identity map is a ring homomorphism. -/ protected instance id {α : Type u} [ring α] : is_ring_hom id := mk (Eq.refl (id 1)) (fun (x y : α) => Eq.refl (id (x * y))) fun (x y : α) => Eq.refl (id (x + y)) /-- The composition of two ring homomorphisms is a ring homomorphism. -/ -- see Note [no instance on morphisms] theorem comp {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] {γ : Type u_1} [ring γ] (g : β → γ) [is_ring_hom g] : is_ring_hom (g ∘ f) := sorry /-- A ring homomorphism is also a semiring homomorphism. -/ protected instance is_semiring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] : is_semiring_hom f := is_semiring_hom.mk (map_zero f) (map_one f) (map_add f) (map_mul f) protected instance is_add_group_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) [is_ring_hom f] : is_add_group_hom f := is_add_group_hom.mk end is_ring_hom namespace ring_hom /-- Interpret `f : α → β` with `is_semiring_hom f` as a ring homomorphism. -/ def of {α : Type u} {β : Type v} [rα : semiring α] [rβ : semiring β] (f : α → β) [is_semiring_hom f] : α →+* β := mk f sorry sorry sorry sorry @[simp] theorem coe_of {α : Type u} {β : Type v} [rα : semiring α] [rβ : semiring β] (f : α → β) [is_semiring_hom f] : ⇑(of f) = f := rfl protected instance is_semiring_hom {α : Type u} {β : Type v} [rα : semiring α] [rβ : semiring β] (f : α →+* β) : is_semiring_hom ⇑f := is_semiring_hom.mk (map_zero f) (map_one f) (map_add f) (map_mul f) protected instance is_ring_hom {α : Type u_1} {γ : Type u_2} [ring α] [ring γ] (g : α →+* γ) : is_ring_hom ⇑g := is_ring_hom.of_semiring ⇑g
c15e7343423ceb964e33bffe4b1530179e0df9b0
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/lessons/lesson17.lean
65481105311fe0a9b76a95c3450f3a9bbf435b99
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
2,569
lean
#check @reflexive #check @symmetric #check @transitive #check @empty_relation #check @irreflexive #check @anti_symmetric #check @total #check @reflexive namespace n section def ltgt : ℤ → ℤ → Prop := λ x y : ℤ, x < y ∧ y < x local infix `<<` : 50 := ltgt theorem ltgt_empty : ∀ (a b : ℤ), ¬ (ltgt a b) := begin assume a b, assume h, have : a < a := lt.trans h.1 h.2, have : ¬ a < a := lt_irrefl a, contradiction end example : irreflexive ltgt := begin assume x xx, have : x < x := lt.trans xx.1 xx.2, apply lt_irrefl x this, end example : irreflexive ltgt := λ x h, ltgt_empty x x h example : symmetric ltgt := begin assume x y h, cases h, have : x < x := lt.trans h_left h_right, have : ¬ x < x := lt_irrefl x, contradiction end example : symmetric ltgt := λ x y h, false.elim (ltgt_empty x y h) example : transitive ltgt := begin assume x y z xy yz, split, exact lt.trans xy.1 yz.1, exact lt.trans yz.2 xy.2, end example : transitive ltgt := λ x y z xy yz, false.elim (ltgt_empty x y xy) example : anti_symmetric ltgt := begin assume x y xy yx, have xlty : x < y := xy.1, have yltx : y < x := xy.2, have xltx : x < x := lt.trans xlty yltx, have : ¬ x < x := lt_irrefl x, contradiction, end example : anti_symmetric ltgt := λ x y xy yx, false.elim (ltgt_empty x y xy) example : ¬ total ltgt := begin assume h, have : 1 << 2 ∨ 2 << 1 := h 1 2, have t : ¬ (2 : ℤ) < 1 := dec_trivial, cases this, have : (2 : ℤ) < 1 := this.2, contradiction, have : (2 : ℤ) < 1 := this.1, contradiction end def connected {α : Type} : (α → α → Prop) → Prop := λ r, ∀ (x y : α), x ≠ y → r x y ∨ r y x example : ¬ connected ltgt := begin assume h, have ne : (1 : ℤ) ≠ 2 := dec_trivial, have : 1<<2 ∨ 2<<1 := (h 1 2) ne, cases this, apply ltgt_empty 1 2 this, apply ltgt_empty 2 1 this, end end end n
b061a1b90418c9577fdd8081a94f84430d4aa7f8
6b2a480f27775cba4f3ae191b1c1387a29de586e
/group_rep_2/Reynold_operator/reynold_scalar_product.lean
7e72ad7f646fd0297bd1cb7f21267d7dbf43ba40
[]
no_license
Or7ando/group_representation
a681de2e19d1930a1e1be573d6735a2f0b8356cb
9b576984f17764ebf26c8caa2a542d248f1b50d2
refs/heads/master
1,662,413,107,324
1,590,302,389,000
1,590,302,389,000
258,130,829
0
1
null
null
null
null
UTF-8
Lean
false
false
3,787
lean
import Reynold_operator.reynold import Tools.tools import basic_definitions.matrix_representation open_locale big_operators open_locale matrix set_option pp.generalized_field_notation false open Reynold linear_map matrix character universes u v w /-- Data for the file. Here we make the assumtion that `X` and `Y` live in the same universe because of `matrix X Y R` that require this. -/ variables {G : Type u} [group G][fintype G][decidable_eq G] {R : Type v} [comm_ring R] {X : Type w} [fintype X][decidable_eq X] (ρ : group_representation G R (X → R)) {Y : Type w} [fintype Y][decidable_eq Y] (ρ' : group_representation G R (Y → R)) /-! # Interpretation of scalar product in term of Reynold operator. * I recall the theory : * let `ρ : G → GL( X → R)` and `ρ' : G → GL (Y → R)` with the variables above. * let `φ ψ : G → R `. * then `scalar_product G R φ ψ := ∑ t, (φ t) * (ψ t⁻¹)` * We define `χ : group_representation G R (X → R) → G → R := trace to_matrix (ρ g)` * Note for the moment we don't invert `card G` * We define `Reynold operator : (X → R →ₗ[R] Y → R) →ₗ[R] ρ ⟶ ρ'` * The definition is : `Re f := ∑ g : G, ρ' g⁻¹ ∘ f ∘ ρ g` * The next theorem formalize : * `∑ g, (χ ρ g) * (χ ρ' g⁻¹) = ∑ g, ( ∑ x : X, to_matrix ( ρ g) x x ) ( ∑ y : Y, to_matrix (ρ' g⁻¹ ) y y)` * ` = ∑ (x,y) : X × Y, ∑ g, to_matrix ( ρ g) x x ) ( ∑ y : Y, to_matrix (ρ' g⁻¹ ) y y)` * ` = ∑ (x,y) : X × Y, to_matrix (Re ρ ρ' ( to_lin (ℰ y x) )).ℓ y x` * Where `ℰ y x` is the `elementary matrix ! * see the files `tools.matrix` for the calculus. * The fact is : Reynold operator is quasi trivial in two case * 1. If ` not (ρ ≃ᵣ ρ')` (not isomorphic) and ` ρ ρ' : irreducible`. * If this case `Reynold opérator` is `0`. That depend of the theorem `Schur₁` in basic definition.irreducible * And we can calculate scalar product with this theorem and it's zero. * 2. if ` (ρ = ρ')` (equal) and ` ρ : irreducible` and " algebricaly closed base field ". * in this case, `Reynold opérator` send a morphism `f` to an λ • 1 and we can compute. -/ /-! Wihout condition : `to_matrix (Re ρ ρ' ( to_lin (ℰ y0 x0))) = (∑ s, (mat ρ' s⁻¹ ) ⬝ (ℰ y0 x0) ⬝ (mat ρ s ) )` -/ theorem to_matrix_Reynold_E ( x0 : X) (y0 : Y) : linear_map.to_matrix (Re ρ ρ' ( to_lin (ℰ y0 x0))).ℓ = (∑ s, (mat ρ' s⁻¹ ) ⬝ (ℰ y0 x0) ⬝ (mat ρ s ) ) := begin rw reynold_ext', rw to_matrix_sum, congr,funext w,rw mixte_conj_ext, rw to_matrix_mul, rw to_matrix_mul, rw to_lin_to_matrix, exact rfl, end theorem interpretation_produit_scalaire : scalar_product G R (χ ρ ) (χ ρ' ) = ∑ (y : X × Y), to_matrix (Re ρ ρ' ( to_lin (ℰ y.snd y.fst) )).ℓ y.snd y.fst := begin rw scalar_product_ext, conv_lhs{ apply_congr,skip, rw chi_ext, rw chi_ext, erw finset.sum_mul_sum, }, erw finset.sum_comm, congr,funext, conv_lhs{ apply_congr,skip, rw mul_comm, rw ← mul_E_mul', }, rw to_matrix_Reynold_E, rw sum_apply_mat, end theorem trace_Reynold_E ( x : X) : matrix.trace X R R (∑ s, (mat ρ s⁻¹ ) ⬝ (ℰ x x) ⬝ (mat ρ s ) ) = fintype.card G := begin rw sum_trace, conv_lhs{ apply_congr,skip, rw trace_conj, rw trace_E,rw if_pos rfl, }, rw finset.sum_const, rw add_monoid.smul_eq_mul, rw mul_one, exact rfl, end
ae1c0726698b0eca293a7b805a0f3ba30596bdaf
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/full_subcategory.lean
60b93294742e0801b5b00e1f2c593c83cbf16505
[ "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
6,755
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.creates /-! # Limits in full subcategories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We introduce the notion of a property closed under taking limits and show that if `P` is closed under taking limits, then limits in `full_subcategory P` can be constructed from limits in `C`. More precisely, the inclusion creates such limits. -/ noncomputable theory universes w' w v u open category_theory namespace category_theory.limits /-- We say that a property is closed under limits of shape `J` if whenever all objects in a `J`-shaped diagram have the property, any limit of this diagram also has the property. -/ def closed_under_limits_of_shape {C : Type u} [category.{v} C] (J : Type w) [category.{w'} J] (P : C → Prop) : Prop := ∀ ⦃F : J ⥤ C⦄ ⦃c : cone F⦄ (hc : is_limit c), (∀ j, P (F.obj j)) → P c.X /-- We say that a property is closed under colimits of shape `J` if whenever all objects in a `J`-shaped diagram have the property, any colimit of this diagram also has the property. -/ def closed_under_colimits_of_shape {C : Type u} [category.{v} C] (J : Type w) [category.{w'} J] (P : C → Prop) : Prop := ∀ ⦃F : J ⥤ C⦄ ⦃c : cocone F⦄ (hc : is_colimit c), (∀ j, P (F.obj j)) → P c.X section variables {C : Type u} [category.{v} C] {J : Type w} [category.{w'} J] {P : C → Prop} lemma closed_under_limits_of_shape.limit (h : closed_under_limits_of_shape J P) {F : J ⥤ C} [has_limit F] : (∀ j, P (F.obj j)) → P (limit F) := h (limit.is_limit _) lemma closed_under_colimits_of_shape.colimit (h : closed_under_colimits_of_shape J P) {F : J ⥤ C} [has_colimit F] : (∀ j, P (F.obj j)) → P (colimit F) := h (colimit.is_colimit _) end section variables {J : Type w} [category.{w'} J] {C : Type u} [category.{v} C] {P : C → Prop} /-- If a `J`-shaped diagram in `full_subcategory P` has a limit cone in `C` whose cone point lives in the full subcategory, then this defines a limit in the full subcategory. -/ def creates_limit_full_subcategory_inclusion' (F : J ⥤ full_subcategory P) {c : cone (F ⋙ full_subcategory_inclusion P)} (hc : is_limit c) (h : P c.X) : creates_limit F (full_subcategory_inclusion P) := creates_limit_of_fully_faithful_of_iso' hc ⟨_, h⟩ (iso.refl _) /-- If a `J`-shaped diagram in `full_subcategory P` has a limit in `C` whose cone point lives in the full subcategory, then this defines a limit in the full subcategory. -/ def creates_limit_full_subcategory_inclusion (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] (h : P (limit (F ⋙ full_subcategory_inclusion P))) : creates_limit F (full_subcategory_inclusion P) := creates_limit_full_subcategory_inclusion' F (limit.is_limit _) h /-- If a `J`-shaped diagram in `full_subcategory P` has a colimit cocone in `C` whose cocone point lives in the full subcategory, then this defines a colimit in the full subcategory. -/ def creates_colimit_full_subcategory_inclusion' (F : J ⥤ full_subcategory P) {c : cocone (F ⋙ full_subcategory_inclusion P)} (hc : is_colimit c) (h : P c.X) : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_of_fully_faithful_of_iso' hc ⟨_, h⟩ (iso.refl _) /-- If a `J`-shaped diagram in `full_subcategory P` has a colimit in `C` whose cocone point lives in the full subcategory, then this defines a colimit in the full subcategory. -/ def creates_colimit_full_subcategory_inclusion (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] (h : P (colimit (F ⋙ full_subcategory_inclusion P))) : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_full_subcategory_inclusion' F (colimit.is_colimit _) h /-- If `P` is closed under limits of shape `J`, then the inclusion creates such limits. -/ def creates_limit_full_subcategory_inclusion_of_closed (h : closed_under_limits_of_shape J P) (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] : creates_limit F (full_subcategory_inclusion P) := creates_limit_full_subcategory_inclusion F (h.limit (λ j, (F.obj j).property)) /-- If `P` is closed under limits of shape `J`, then the inclusion creates such limits. -/ def creates_limits_of_shape_full_subcategory_inclusion (h : closed_under_limits_of_shape J P) [has_limits_of_shape J C] : creates_limits_of_shape J (full_subcategory_inclusion P) := { creates_limit := λ F, creates_limit_full_subcategory_inclusion_of_closed h F } lemma has_limit_of_closed_under_limits (h : closed_under_limits_of_shape J P) (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] : has_limit F := have creates_limit F (full_subcategory_inclusion P), from creates_limit_full_subcategory_inclusion_of_closed h F, by exactI has_limit_of_created F (full_subcategory_inclusion P) lemma has_limits_of_shape_of_closed_under_limits (h : closed_under_limits_of_shape J P) [has_limits_of_shape J C] : has_limits_of_shape J (full_subcategory P) := { has_limit := λ F, has_limit_of_closed_under_limits h F } /-- If `P` is closed under colimits of shape `J`, then the inclusion creates such colimits. -/ def creates_colimit_full_subcategory_inclusion_of_closed (h : closed_under_colimits_of_shape J P) (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_full_subcategory_inclusion F (h.colimit (λ j, (F.obj j).property)) /-- If `P` is closed under colimits of shape `J`, then the inclusion creates such colimits. -/ def creates_colimits_of_shape_full_subcategory_inclusion (h : closed_under_colimits_of_shape J P) [has_colimits_of_shape J C] : creates_colimits_of_shape J (full_subcategory_inclusion P) := { creates_colimit := λ F, creates_colimit_full_subcategory_inclusion_of_closed h F } lemma has_colimit_of_closed_under_colimits (h : closed_under_colimits_of_shape J P) (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] : has_colimit F := have creates_colimit F (full_subcategory_inclusion P), from creates_colimit_full_subcategory_inclusion_of_closed h F, by exactI has_colimit_of_created F (full_subcategory_inclusion P) lemma has_colimits_of_shape_of_closed_under_colimits (h : closed_under_colimits_of_shape J P) [has_colimits_of_shape J C] : has_colimits_of_shape J (full_subcategory P) := { has_colimit := λ F, has_colimit_of_closed_under_colimits h F } end end category_theory.limits
25266a132c0a81774323aadfa1bca54156e2ae5a
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/docStr.lean
0b516423124babf84209933a7aa7f0cc367234d2
[ "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
2,588
lean
import Lean /-- Foo structure is just a test -/ structure Foo where /-- main name -/ name : String := "hello" /-- documentation for the second field -/ val : Nat := 0 /-- documenting test axiom -/ axiom myAxiom : Nat structure Boo (α : Type) where /-- Boo constructor has a custom name -/ makeBoo :: /-- Boo.x docString -/ x : Nat y : Bool /-- inductive datatype Tree documentation -/ inductive Tree (α : Type) where | /-- Tree.node documentation -/ node : List (Tree α) → Tree α | /-- Tree.leaf stores the values -/ leaf : α → Tree α namespace Bla /-- documenting definition in namespace -/ def test (x : Nat) : Nat := aux x + 1 where /-- We can document 'where' functions too -/ aux x := x + 2 end Bla def f (x : Nat) : IO Nat := do let rec /-- let rec documentation at f -/ foo | 0 => 1 | x+1 => foo x + 2 return foo x def g (x : Nat) : Nat := let rec /-- let rec documentation at g -/ foo | 0 => 1 | x+1 => foo x + 2 foo x open Lean def printDocString (declName : Name) : MetaM Unit := do match (← findDocString? (← getEnv) declName) with | some docStr => IO.println (repr docStr) | none => IO.println s!"doc string for '{declName}' is not available" def printDocStringTest : MetaM Unit := do printDocString `Foo printDocString `Foo.name printDocString `Foo.val printDocString `myAxiom printDocString `Boo printDocString `Boo.makeBoo printDocString `Boo.x printDocString `Boo.y printDocString `Tree printDocString `Tree.node printDocString `Tree.leaf printDocString `Bla.test printDocString `Bla.test.aux printDocString `f printDocString `f.foo printDocString `g printDocString `g.foo printDocString `optParam printDocString `namedPattern printDocString `Lean.Meta.forallTelescopeReducing #eval printDocStringTest def printRanges (declName : Name) : MetaM Unit := do match (← findDeclarationRanges? declName) with | some range => IO.println f!"{declName} :={Std.Format.indentD <| repr range}" | none => IO.println s!"range for '{declName}' is not available" def printRangesTest : MetaM Unit := do printRanges `Foo printRanges `Foo.name printRanges `Foo.val printRanges `myAxiom printRanges `Boo printRanges `Boo.makeBoo printRanges `Boo.x printRanges `Boo.y printRanges `Tree printRanges `Tree.rec printRanges `Tree.casesOn printRanges `Tree.node printRanges `Tree.leaf printRanges `Bla.test printRanges `Bla.test.aux printRanges `f printRanges `f.foo printRanges `g printRanges `g.foo #eval printRangesTest
54124537ec7e9fa34859653b437551363bc203cd
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/slim_check.lean
fbe094f1b7ac97c78ede95ec2b7cc3bb389ec415
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,083
lean
import tactic.slim_check import testing.slim_check.functions import .mk_slim_check_test example : true := begin have : ∀ i j : ℕ, i < j → j < i, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! i := 0 j := 1 guard: 0 < 1 (by construction) issue: 1 < 0 does not hold (0 shrinks) ------------------- ", admit, trivial end example : true := begin have : (∀ x : ℕ, 2 ∣ x → x < 100), success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 104 issue: 104 < 100 does not hold (2 shrinks) ------------------- ", admit, trivial end example (xs : list ℕ) (w : ∃ x ∈ xs, x < 3) : true := begin have : ∀ y ∈ xs, y < 5, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! xs := [5, 5, 0, 1] x := 0 y := 5 issue: 5 < 5 does not hold (5 shrinks) ------------------- ", admit, trivial end example (x : ℕ) (h : 2 ∣ x) : true := begin have : x < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 104 issue: 104 < 100 does not hold (2 shrinks) ------------------- ", admit, trivial end example (α : Type) (xs ys : list α) : true := begin have : xs ++ ys = ys ++ xs, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! α := ℤ xs := [0] ys := [1] issue: [0, 1] = [1, 0] does not hold (4 shrinks) ------------------- ", admit, trivial end example : true := begin have : ∀ x ∈ [1,2,3], x < 4, slim_check { random_seed := some 257, quiet := tt }, -- success trivial, end open function slim_check example (f : ℤ → ℤ) (h : injective f) : true := begin have : monotone (f ∘ small.mk), success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [2 ↦ 3, 3 ↦ 9, 4 ↦ 6, 5 ↦ 4, 6 ↦ 2, 8 ↦ 5, 9 ↦ 8, x ↦ x] x := 3 y := 4 guard: 3 ≤ 4 (by construction) issue: 9 ≤ 6 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : ℤ → ℤ) (h : injective f) (g : ℤ → ℤ) (h : injective g) (i) : true := begin have : f i = g i, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [x ↦ x] g := [1 ↦ 2, 2 ↦ 1, x ↦ x] i := 1 issue: 1 = 2 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : ℤ → ℤ) (h : injective f) : true := begin have : monotone f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [2 ↦ 3, 3 ↦ 9, 4 ↦ 6, 5 ↦ 4, 6 ↦ 2, 8 ↦ 5, 9 ↦ 8, x ↦ x] x := 3 y := 4 guard: 3 ≤ 4 (by construction) issue: 9 ≤ 6 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : ℤ → ℤ) : true := begin have : injective f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [_ ↦ 0] x := 0 y := -1 guard: 0 = 0 issue: 0 = -1 does not hold (0 shrinks) ------------------- ", admit, trivial, end example (f : ℤ → ℤ) : true := begin have : monotone f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [-6 ↦ 97, 0 ↦ 0, _ ↦ 4] x := -6 y := -2 guard: -6 ≤ -2 (by construction) issue: 97 ≤ 4 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (xs ys : list ℤ) (h : xs ~ ys) : true := begin have : list.qsort (λ x y, x ≠ y) xs = list.qsort (λ x y, x ≠ y) ys, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! xs := [0, 1] ys := [1, 0] guard: [0, 1] ~ [1, 0] (by construction) issue: [0, 1] = [1, 0] does not hold (4 shrinks) ------------------- ", admit, trivial end example (x y : ℕ) : true := begin have : y ≤ x → x + y < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 59 y := 41 guard: 41 ≤ 59 (by construction) issue: 100 < 100 does not hold (8 shrinks) ------------------- ", admit, trivial, end example (x : ℤ) : true := begin have : x ≤ 3 → 3 ≤ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 2 guard: 2 ≤ 3 (by construction) issue: 3 ≤ 2 does not hold (1 shrinks) ------------------- ", admit, trivial, end example (x y : ℤ) : true := begin have : y ≤ x → x + y < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 52 y := 52 guard: 52 ≤ 52 (by construction) issue: 104 < 100 does not hold (4 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : x ∨ y → y ∧ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff guard: (true ∨ false) issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : (¬x ↔ y) → y ∧ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff guard: (¬ true ↔ false) issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin -- deterministic have : (x ↔ y) → y ∨ x, success_if_fail_with_msg { slim_check } " =================== Found problems! x := ff y := ff guard: (false ↔ false) issue: false does not hold issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin -- deterministic have : y ∨ x, success_if_fail_with_msg { slim_check } " =================== Found problems! x := ff y := ff issue: false does not hold issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : x ↔ y, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff issue: false does not hold issue: ¬ true does not hold (0 shrinks) ------------------- ", admit, trivial, end example (f : ℕ →₀ ℕ) : true := begin have : f = 0, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [0 ↦ 1, _ ↦ 0] issue: finsupp.single 0 1 = 0 does not hold (2 shrinks) ------------------- ", admit, trivial, end example (f : Π₀ n : ℕ, ℕ) : true := begin have : f.update 0 0 = 0, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [1 ↦ 1, _ ↦ 0] (1 shrinks) ------------------- ", admit, trivial, end
2977a54ba1ec245571c2d62302491d0c25f9b4f0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/nat/basic.lean
21a13844f3805f83eda82f44b692282c77e05224
[ "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
30,225
lean
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import order.basic import algebra.group_with_zero.basic import algebra.ring.defs /-! # Basic operations on the natural numbers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains: - instances on the natural numbers - some basic lemmas about natural numbers - extra recursors: * `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers * `decreasing_induction`: recursion growing downwards * `le_rec_on'`, `decreasing_induction'`: versions with slightly weaker assumptions * `strong_rec'`: recursion based on strong inequalities - decidability instances on predicates about the natural numbers Many theorems that used to live in this file have been moved to `data.nat.order`, so that this file requires fewer imports. For each section here there is a corresponding section in that file with additional results. It may be possible to move some of these results here, by tweaking their proofs. -/ universes u v /-! ### instances -/ instance : nontrivial ℕ := ⟨⟨0, 1, nat.zero_ne_one⟩⟩ instance : comm_semiring ℕ := { add := nat.add, add_assoc := nat.add_assoc, zero := nat.zero, zero_add := nat.zero_add, add_zero := nat.add_zero, add_comm := nat.add_comm, mul := nat.mul, mul_assoc := nat.mul_assoc, one := nat.succ nat.zero, one_mul := nat.one_mul, mul_one := nat.mul_one, left_distrib := nat.left_distrib, right_distrib := nat.right_distrib, zero_mul := nat.zero_mul, mul_zero := nat.mul_zero, mul_comm := nat.mul_comm, nat_cast := λ n, n, nat_cast_zero := rfl, nat_cast_succ := λ n, rfl, nsmul := λ m n, m * n, nsmul_zero' := nat.zero_mul, nsmul_succ' := λ n x, by rw [nat.succ_eq_add_one, nat.add_comm, nat.right_distrib, nat.one_mul] } /-! Extra instances to short-circuit type class resolution and ensure computability -/ instance : add_comm_monoid ℕ := infer_instance instance : add_monoid ℕ := infer_instance instance : monoid ℕ := infer_instance instance : comm_monoid ℕ := infer_instance instance : comm_semigroup ℕ := infer_instance instance : semigroup ℕ := infer_instance instance : add_comm_semigroup ℕ := infer_instance instance : add_semigroup ℕ := infer_instance instance : distrib ℕ := infer_instance instance : semiring ℕ := infer_instance protected lemma nat.nsmul_eq_mul (m n : ℕ) : m • n = m * n := rfl instance nat.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero ℕ := { mul_left_cancel_of_ne_zero := λ _ _ _ h1 h2, nat.eq_of_mul_eq_mul_left (nat.pos_of_ne_zero h1) h2, .. nat.comm_semiring } attribute [simp] nat.not_lt_zero nat.succ_ne_zero nat.succ_ne_self nat.zero_ne_one nat.one_ne_zero nat.zero_ne_bit1 nat.bit1_ne_zero nat.bit0_ne_one nat.one_ne_bit0 nat.bit0_ne_bit1 nat.bit1_ne_bit0 variables {m n k : ℕ} namespace nat /-! ### Recursion and `forall`/`exists` -/ @[simp] lemma and_forall_succ {p : ℕ → Prop} : (p 0 ∧ ∀ n, p (n + 1)) ↔ ∀ n, p n := ⟨λ h n, nat.cases_on n h.1 h.2, λ h, ⟨h _, λ n, h _⟩⟩ @[simp] lemma or_exists_succ {p : ℕ → Prop} : (p 0 ∨ ∃ n, p (n + 1)) ↔ ∃ n, p n := ⟨λ h, h.elim (λ h0, ⟨0, h0⟩) (λ ⟨n, hn⟩, ⟨n + 1, hn⟩), by { rintro ⟨(_|n), hn⟩, exacts [or.inl hn, or.inr ⟨n, hn⟩]}⟩ /-! ### `succ` -/ lemma _root_.has_lt.lt.nat_succ_le {n m : ℕ} (h : n < m) : succ n ≤ m := succ_le_of_lt h lemma succ_eq_one_add (n : ℕ) : n.succ = 1 + n := by rw [nat.succ_eq_add_one, nat.add_comm] theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b := have h3 : a ≤ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3)) lemma eq_of_le_of_lt_succ {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n + 1) : m = n := nat.le_antisymm (le_of_succ_le_succ h₂) h₁ theorem one_add (n : ℕ) : 1 + n = succ n := by simp [add_comm] @[simp] lemma succ_pos' {n : ℕ} : 0 < succ n := succ_pos n theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m := ⟨succ.inj, congr_arg _⟩ theorem succ_injective : function.injective nat.succ := λ x y, succ.inj lemma succ_ne_succ {n m : ℕ} : succ n ≠ succ m ↔ n ≠ m := succ_injective.ne_iff @[simp] lemma succ_succ_ne_one (n : ℕ) : n.succ.succ ≠ 1 := succ_ne_succ.mpr n.succ_ne_zero @[simp] lemma one_lt_succ_succ (n : ℕ) : 1 < n.succ.succ := succ_lt_succ $ succ_pos n theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ theorem max_succ_succ {m n : ℕ} : max (succ m) (succ n) = succ (max m n) := begin by_cases h1 : m ≤ n, rw [max_eq_right h1, max_eq_right (succ_le_succ h1)], { rw not_le at h1, have h2 := le_of_lt h1, rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] } end lemma not_succ_lt_self {n : ℕ} : ¬succ n < n := not_lt_of_ge (nat.le_succ _) theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n := ⟨le_of_lt_succ, lt_succ_of_le⟩ lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ lemma lt_iff_add_one_le {m n : ℕ} : m < n ↔ m + 1 ≤ n := by rw succ_le_iff -- Just a restatement of `nat.lt_succ_iff` using `+1`. lemma lt_add_one_iff {a b : ℕ} : a < b + 1 ↔ a ≤ b := lt_succ_iff -- A flipped version of `lt_add_one_iff`. lemma lt_one_add_iff {a b : ℕ} : a < 1 + b ↔ a ≤ b := by simp only [add_comm, lt_succ_iff] -- This is true reflexively, by the definition of `≤` on ℕ, -- but it's still useful to have, to convince Lean to change the syntactic type. lemma add_one_le_iff {a b : ℕ} : a + 1 ≤ b ↔ a < b := iff.refl _ lemma one_add_le_iff {a b : ℕ} : 1 + a ≤ b ↔ a < b := by simp only [add_comm, add_one_le_iff] theorem of_le_succ {n m : ℕ} (H : n ≤ m.succ) : n ≤ m ∨ n = m.succ := H.lt_or_eq_dec.imp le_of_lt_succ id lemma succ_lt_succ_iff {m n : ℕ} : succ m < succ n ↔ m < n := ⟨lt_of_succ_lt_succ, succ_lt_succ⟩ lemma div_le_iff_le_mul_add_pred {m n k : ℕ} (n0 : 0 < n) : m / n ≤ k ↔ m ≤ n * k + (n - 1) := begin rw [← lt_succ_iff, div_lt_iff_lt_mul n0, succ_mul, mul_comm], cases n, {cases n0}, exact lt_succ_iff, end lemma two_lt_of_ne : ∀ {n}, n ≠ 0 → n ≠ 1 → n ≠ 2 → 2 < n | 0 h _ _ := (h rfl).elim | 1 _ h _ := (h rfl).elim | 2 _ _ h := (h rfl).elim | (n+3) _ _ _ := dec_trivial theorem forall_lt_succ {P : ℕ → Prop} {n : ℕ} : (∀ m < n + 1, P m) ↔ (∀ m < n, P m) ∧ P n := by simp only [lt_succ_iff, decidable.le_iff_eq_or_lt, forall_eq_or_imp, and.comm] theorem exists_lt_succ {P : ℕ → Prop} {n : ℕ} : (∃ m < n + 1, P m) ↔ (∃ m < n, P m) ∨ P n := by { rw ←not_iff_not, push_neg, exact forall_lt_succ } /-! ### `add` -/ -- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding -- during pattern matching. These lemmas package them back up as typeclass -- mediated operations. @[simp] theorem add_def {a b : ℕ} : nat.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℕ} : nat.mul a b = a * b := rfl lemma exists_eq_add_of_le (h : m ≤ n) : ∃ k : ℕ, n = m + k := ⟨n - m, (nat.add_sub_of_le h).symm⟩ lemma exists_eq_add_of_le' (h : m ≤ n) : ∃ k : ℕ, n = k + m := ⟨n - m, (nat.sub_add_cancel h).symm⟩ lemma exists_eq_add_of_lt (h : m < n) : ∃ k : ℕ, n = m + k + 1 := ⟨n - (m + 1), by rw [add_right_comm, nat.add_sub_of_le h]⟩ /-! ### `pred` -/ @[simp] lemma add_succ_sub_one (n m : ℕ) : (n + succ m) - 1 = n + m := by rw [add_succ, succ_sub_one] @[simp] lemma succ_add_sub_one (n m : ℕ) : (succ n + m) - 1 = n + m := by rw [succ_add, succ_sub_one] lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H] @[simp] lemma pred_eq_succ_iff {n m : ℕ} : pred n = succ m ↔ n = m + 2 := by cases n; split; rintro ⟨⟩; refl theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) := by rw [← nat.sub_one, nat.sub_sub, one_add, sub_succ] lemma le_pred_of_lt {n m : ℕ} (h : m < n) : m ≤ n - 1 := nat.sub_le_sub_right h 1 lemma le_of_pred_lt {m n : ℕ} : pred m < n → m ≤ n := match m with | 0 := le_of_lt | m+1 := id end /-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/ @[simp] lemma pred_one_add (n : ℕ) : pred (1 + n) = n := by rw [add_comm, add_one, pred_succ] /-! ### `mul` -/ theorem two_mul_ne_two_mul_add_one {n m} : 2 * n ≠ 2 * m + 1 := mt (congr_arg (%2)) (by { rw [add_comm, add_mul_mod_self_left, mul_mod_right, mod_eq_of_lt]; simp }) lemma mul_ne_mul_left {a b c : ℕ} (ha : 0 < a) : b * a ≠ c * a ↔ b ≠ c := (mul_left_injective₀ ha.ne').ne_iff lemma mul_ne_mul_right {a b c : ℕ} (ha : 0 < a) : a * b ≠ a * c ↔ b ≠ c := (mul_right_injective₀ ha.ne').ne_iff lemma mul_right_eq_self_iff {a b : ℕ} (ha : 0 < a) : a * b = a ↔ b = 1 := suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this, mul_right_inj' ha.ne' lemma mul_left_eq_self_iff {a b : ℕ} (hb : 0 < b) : a * b = b ↔ a = 1 := by rw [mul_comm, nat.mul_right_eq_self_iff hb] lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans decidable.le_iff_lt_or_eq /-! ### Recursion and induction principles This section is here due to dependencies -- the lemmas here require some of the lemmas proved above, and some of the results in later sections depend on the definitions in this section. -/ @[simp] lemma rec_zero {C : ℕ → Sort u} (h0 : C 0) (h : ∀ n, C n → C (n + 1)) : (nat.rec h0 h : Π n, C n) 0 = h0 := rfl @[simp] lemma rec_add_one {C : ℕ → Sort u} (h0 : C 0) (h : ∀ n, C n → C (n + 1)) (n : ℕ) : (nat.rec h0 h : Π n, C n) (n + 1) = h n ((nat.rec h0 h : Π n, C n) n) := rfl /-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k`, there is a map from `C n` to each `C m`, `n ≤ m`. For a version where the assumption is only made when `k ≥ n`, see `le_rec_on'`. -/ @[elab_as_eliminator] def le_rec_on {C : ℕ → Sort u} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π {k}, C k → C (k+1)) → C n → C m | 0 H next x := eq.rec_on (nat.eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next $ le_rec_on h @next x) (λ h : n = m + 1, eq.rec_on h x) theorem le_rec_on_self {C : ℕ → Sort u} {n} {h : n ≤ n} {next} (x : C n) : (le_rec_on h next x : C n) = x := by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self] theorem le_rec_on_succ {C : ℕ → Sort u} {n m} (h1 : n ≤ m) {h2 : n ≤ m+1} {next} (x : C n) : (le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) := by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] } theorem le_rec_on_succ' {C : ℕ → Sort u} {n} {h : n ≤ n+1} {next} (x : C n) : (le_rec_on h next x : C (n+1)) = next x := by rw [le_rec_on_succ (le_refl n), le_rec_on_self] theorem le_rec_on_trans {C : ℕ → Sort u} {n m k} (hnm : n ≤ m) (hmk : m ≤ k) {next} (x : C n) : (le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) := begin induction hmk with k hmk ih, { rw le_rec_on_self }, rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ] end theorem le_rec_on_succ_left {C : ℕ → Sort u} {n m} (h1 : n ≤ m) (h2 : n+1 ≤ m) {next : Π{{k}}, C k → C (k+1)} (x : C n) : (le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) := begin rw [subsingleton.elim h1 (le_trans (le_succ n) h2), le_rec_on_trans (le_succ n) h2, le_rec_on_succ'] end theorem le_rec_on_injective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.injective (next n)) : function.injective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H }, intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H) end theorem le_rec_on_surjective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.surjective (next n)) : function.surjective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self }, intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ end /-- Recursion principle based on `<`. -/ @[elab_as_eliminator] protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n | n := H n (λ m hm, strong_rec' m) /-- Recursion principle based on `<` applied to some natural number. -/ @[elab_as_eliminator] def strong_rec_on' {P : ℕ → Sort*} (n : ℕ) (h : ∀ n, (∀ m, m < n → P m) → P n) : P n := nat.strong_rec' h n theorem strong_rec_on_beta' {P : ℕ → Sort*} {h} {n : ℕ} : (strong_rec_on' n h : P n) = h n (λ m hmn, (strong_rec_on' m h : P m)) := by { simp only [strong_rec_on'], rw nat.strong_rec' } /-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/ @[elab_as_eliminator] lemma le_induction {P : nat → Prop} {m} (h0 : P m) (h1 : ∀ n, m ≤ n → P n → P (n + 1)) : ∀ n, m ≤ n → P n := by apply nat.less_than_or_equal.rec h0; exact h1 /-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≤ n`. Also works for functions to `Sort*`. For a version assuming only the assumption for `k < n`, see `decreasing_induction'`. -/ @[elab_as_eliminator] def decreasing_induction {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (hP : P n) : P m := le_rec_on mn (λ k ih hsk, ih $ h k hsk) (λ h, h) hP @[simp] lemma decreasing_induction_self {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {n : ℕ} (nn : n ≤ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP := by { dunfold decreasing_induction, rw [le_rec_on_self] } lemma decreasing_induction_succ {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (msn : m ≤ n + 1) (hP : P (n+1)) : (decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) := by { dunfold decreasing_induction, rw [le_rec_on_succ] } @[simp] lemma decreasing_induction_succ' {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m : ℕ} (msm : m ≤ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP := by { dunfold decreasing_induction, rw [le_rec_on_succ'] } lemma decreasing_induction_trans {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n k : ℕ} (mn : m ≤ n) (nk : n ≤ k) (hP : P k) : (decreasing_induction h (le_trans mn nk) hP : P m) = decreasing_induction h mn (decreasing_induction h nk hP) := by { induction nk with k nk ih, rw [decreasing_induction_self], rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] } lemma decreasing_induction_succ_left {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (smn : m + 1 ≤ n) (mn : m ≤ n) (hP : P n) : (decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) := by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans, decreasing_induction_succ'] } /-- Given `P : ℕ → ℕ → Sort*`, if for all `a b : ℕ` we can extend `P` from the rectangle strictly below `(a,b)` to `P a b`, then we have `P n m` for all `n m : ℕ`. Note that for non-`Prop` output it is preferable to use the equation compiler directly if possible, since this produces equation lemmas. -/ @[elab_as_eliminator] def strong_sub_recursion {P : ℕ → ℕ → Sort*} (H : ∀ a b, (∀ x y, x < a → y < b → P x y) → P a b) : Π (n m : ℕ), P n m | n m := H n m (λ x y hx hy, strong_sub_recursion x y) /-- Given `P : ℕ → ℕ → Sort*`, if we have `P i 0` and `P 0 i` for all `i : ℕ`, and for any `x y : ℕ` we can extend `P` from `(x,y+1)` and `(x+1,y)` to `(x+1,y+1)` then we have `P n m` for all `n m : ℕ`. Note that for non-`Prop` output it is preferable to use the equation compiler directly if possible, since this produces equation lemmas. -/ @[elab_as_eliminator] def pincer_recursion {P : ℕ → ℕ → Sort*} (Ha0 : ∀ a : ℕ, P a 0) (H0b : ∀ b : ℕ, P 0 b) (H : ∀ x y : ℕ, P x y.succ → P x.succ y → P x.succ y.succ) : ∀ (n m : ℕ), P n m | a 0 := Ha0 a | 0 b := H0b b | (nat.succ a) (nat.succ b) := H _ _ (pincer_recursion _ _) (pincer_recursion _ _) /-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k ≥ n`, there is a map from `C n` to each `C m`, `n ≤ m`. -/ @[elab_as_eliminator] def le_rec_on' {C : ℕ → Sort*} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π ⦃k⦄, n ≤ k → C k → C (k+1)) → C n → C m | 0 H next x := eq.rec_on (nat.eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next h $ le_rec_on' h next x) (λ h : n = m + 1, eq.rec_on h x) /-- Decreasing induction: if `P (k+1)` implies `P k` for all `m ≤ k < n`, then `P n` implies `P m`. Also works for functions to `Sort*`. Weakens the assumptions of `decreasing_induction`. -/ @[elab_as_eliminator] def decreasing_induction' {P : ℕ → Sort*} {m n : ℕ} (h : ∀ k < n, m ≤ k → P (k+1) → P k) (mn : m ≤ n) (hP : P n) : P m := begin -- induction mn using nat.le_rec_on' generalizing h hP -- this doesn't work unfortunately refine le_rec_on' mn _ _ h hP; clear h hP mn n, { intros n mn ih h hP, apply ih, { exact λ k hk, h k hk.step }, { exact h n (lt_succ_self n) mn hP } }, { intros h hP, exact hP } end /-! ### `div` -/ attribute [simp] nat.div_self /-- A version of `nat.div_lt_self` using successors, rather than additional hypotheses. -/ lemma div_lt_self' (n b : ℕ) : (n+1)/(b+2) < n+1 := nat.div_lt_self (nat.succ_pos n) (nat.succ_lt_succ (nat.succ_pos _)) theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := le_div_iff_mul_le k0 theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 lemma one_le_div_iff {a b : ℕ} (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a := by rw [le_div_iff_mul_le hb, one_mul] lemma div_lt_one_iff {a b : ℕ} (hb : 0 < b) : a / b < 1 ↔ a < b := lt_iff_lt_of_le_iff_le $ one_le_div_iff hb protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k := (nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self _ _) h lemma lt_of_div_lt_div {m n k : ℕ} : m / k < n / k → m < n := lt_imp_lt_of_le_imp_le $ λ h, nat.div_le_div_right h protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (λ h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≤ a : hba)) lemma lt_mul_of_div_lt {a b c : ℕ} (h : a / c < b) (w : 0 < c) : a < b * c := lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le w).2 lemma mul_div_le_mul_div_assoc (a b c : ℕ) : a * (b / c) ≤ (a * b) / c := if hc0 : c = 0 then by simp [hc0] else (nat.le_div_iff_mul_le (nat.pos_of_ne_zero hc0)).2 (by rw [mul_assoc]; exact nat.mul_le_mul_left _ (nat.div_mul_le_self _ _)) protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] /-- Alias of `nat.mul_div_mul` -/ --TODO: Update `nat.mul_div_mul` in the core? protected lemma mul_div_mul_left (a b : ℕ) {c : ℕ} (hc : 0 < c) : c * a / (c * b) = a / b := nat.mul_div_mul a b hc protected lemma mul_div_mul_right (a b : ℕ) {c : ℕ} (hc : 0 < c) : a * c / (b * c) = a / b := by rw [mul_comm, mul_comm b, a.mul_div_mul_left b hc] lemma lt_div_mul_add {a b : ℕ} (hb : 0 < b) : a < a/b*b + b := begin rw [←nat.succ_mul, ←nat.div_lt_iff_lt_mul hb], exact nat.lt_succ_self _, end @[simp] protected lemma div_left_inj {a b d : ℕ} (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b := begin refine ⟨λ h, _, congr_arg _⟩, rw [←nat.mul_div_cancel' hda, ←nat.mul_div_cancel' hdb, h], end /-! ### `mod`, `dvd` -/ lemma mod_eq_iff_lt {a b : ℕ} (h : b ≠ 0) : a % b = a ↔ a < b := begin cases b, contradiction, exact ⟨λ h, h.ge.trans_lt (mod_lt _ (succ_pos _)), mod_eq_of_lt⟩, end @[simp] lemma mod_succ_eq_iff_lt {a b : ℕ} : a % b.succ = a ↔ a < b.succ := mod_eq_iff_lt (succ_ne_zero _) lemma div_add_mod (m k : ℕ) : k * (m / k) + m % k = m := (nat.add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : ℕ) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : ℕ) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } /-- See also `nat.div_mod_equiv` for a similar statement as an `equiv`. -/ protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩, λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩ protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, mul_right_inj' ha.ne'] protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, mul_left_inj' hc.ne'] @[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n := (nat.eq_zero_or_pos n).elim (λ n0, by simp [n0]) (λ npos, mod_eq_of_lt (mod_lt _ npos)) @[simp] theorem mod_add_mod (m n k : ℕ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℕ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : ℕ) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : ℕ} (i : ℕ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℕ} (i : ℕ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] lemma mul_mod (a b n : ℕ) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc, add_mul_mod_self_right] } end lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : ∃ d, b / c = a * d, from h, have h2 : ∃ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show ∃ d, b = c * a * d, from ⟨d, by cc⟩ lemma eq_of_dvd_of_div_eq_one {a b : ℕ} (w : a ∣ b) (h : b / a = 1) : a = b := by rw [←nat.div_mul_cancel w, h, one_mul] lemma eq_zero_of_dvd_of_div_eq_zero {a b : ℕ} (w : a ∣ b) (h : b / a = 0) : b = 0 := by rw [←nat.div_mul_cancel w, h, zero_mul] lemma div_le_div_left {a b c : ℕ} (h₁ : c ≤ b) (h₂ : 0 < c) : a / b ≤ a / c := (nat.le_div_iff_mul_le h₂).2 $ le_trans (nat.mul_le_mul_left _ h₁) (div_mul_le_self _ _) lemma lt_iff_le_pred : ∀ {m n : ℕ}, 0 < n → (m < n ↔ m ≤ n - 1) | m (n+1) _ := lt_succ_iff lemma mul_div_le (m n : ℕ) : n * (m / n) ≤ m := begin cases nat.eq_zero_or_pos n with n0 h, { rw [n0, zero_mul], exact m.zero_le }, { rw [mul_comm, ← nat.le_div_iff_mul_le' h] }, end lemma lt_mul_div_succ (m : ℕ) {n : ℕ} (n0 : 0 < n) : m < n * ((m / n) + 1) := begin rw [mul_comm, ← nat.div_lt_iff_lt_mul' n0], exact lt_succ_self _ end lemma mul_add_mod (a b c : ℕ) : (a * b + c) % b = c % b := by simp [nat.add_mod] lemma mul_add_mod_of_lt {a b c : ℕ} (h : c < b) : (a * b + c) % b = c := by rw [nat.mul_add_mod, nat.mod_eq_of_lt h] lemma pred_eq_self_iff {n : ℕ} : n.pred = n ↔ n = 0 := by { cases n; simp [(nat.succ_ne_self _).symm] } /-! ### `find` -/ section find variables {p q : ℕ → Prop} [decidable_pred p] [decidable_pred q] lemma find_eq_iff (h : ∃ n : ℕ, p n) : nat.find h = m ↔ p m ∧ ∀ n < m, ¬ p n := begin split, { rintro rfl, exact ⟨nat.find_spec h, λ _, nat.find_min h⟩ }, { rintro ⟨hm, hlt⟩, exact le_antisymm (nat.find_min' h hm) (not_lt.1 $ imp_not_comm.1 (hlt _) $ nat.find_spec h) } end @[simp] lemma find_lt_iff (h : ∃ n : ℕ, p n) (n : ℕ) : nat.find h < n ↔ ∃ m < n, p m := ⟨λ h2, ⟨nat.find h, h2, nat.find_spec h⟩, λ ⟨m, hmn, hm⟩, (nat.find_min' h hm).trans_lt hmn⟩ @[simp] lemma find_le_iff (h : ∃ n : ℕ, p n) (n : ℕ) : nat.find h ≤ n ↔ ∃ m ≤ n, p m := by simp only [exists_prop, ← lt_succ_iff, find_lt_iff] @[simp] lemma le_find_iff (h : ∃ (n : ℕ), p n) (n : ℕ) : n ≤ nat.find h ↔ ∀ m < n, ¬ p m := by simp_rw [← not_lt, find_lt_iff, not_exists] @[simp] lemma lt_find_iff (h : ∃ n : ℕ, p n) (n : ℕ) : n < nat.find h ↔ ∀ m ≤ n, ¬ p m := by simp only [← succ_le_iff, le_find_iff, succ_le_succ_iff] @[simp] lemma find_eq_zero (h : ∃ n : ℕ, p n) : nat.find h = 0 ↔ p 0 := by simp [find_eq_iff] theorem find_mono (h : ∀ n, q n → p n) {hp : ∃ n, p n} {hq : ∃ n, q n} : nat.find hp ≤ nat.find hq := nat.find_min' _ (h _ (nat.find_spec hq)) lemma find_le {h : ∃ n, p n} (hn : p n) : nat.find h ≤ n := (nat.find_le_iff _ _).2 ⟨n, le_rfl, hn⟩ lemma find_comp_succ (h₁ : ∃ n, p n) (h₂ : ∃ n, p (n + 1)) (h0 : ¬ p 0) : nat.find h₁ = nat.find h₂ + 1 := begin refine (find_eq_iff _).2 ⟨nat.find_spec h₂, λ n hn, _⟩, cases n with n, exacts [h0, @nat.find_min (λ n, p (n + 1)) _ h₂ _ (succ_lt_succ_iff.1 hn)] end end find /-! ### `find_greatest` -/ section find_greatest /-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P Q : ℕ → Prop} [decidable_pred P] {b : ℕ} @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl lemma find_greatest_succ (n : ℕ) : nat.find_greatest P (n + 1) = if P (n + 1) then n + 1 else nat.find_greatest P n := rfl @[simp] lemma find_greatest_eq : ∀ {b}, P b → nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not (h : ¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] end find_greatest /-! ### decidability of predicates -/ instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) : ∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (λ n, dec_trivial) }, cases IH (λ k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (λ k h', (le_of_lt_succ h').lt_or_eq_dec.elim (h _) (λ e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (λ hn, hn _ _) p) } end instance decidable_forall_fin {n : ℕ} (P : fin n → Prop) [H : decidable_pred P] : decidable (∀ i, P i) := decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop) [H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) := decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩ instance decidable_exists_lt {P : ℕ → Prop} [h : decidable_pred P] : decidable_pred (λ n, ∃ (m : ℕ), m < n ∧ P m) | 0 := is_false (by simp) | (n + 1) := decidable_of_decidable_of_iff (@or.decidable _ _ (decidable_exists_lt n) (h n)) (by simp only [lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, exists_eq_left]) instance decidable_exists_le {P : ℕ → Prop} [h : decidable_pred P] : decidable_pred (λ n, ∃ (m : ℕ), m ≤ n ∧ P m) := λ n, decidable_of_iff (∃ m, m < n + 1 ∧ P m) (exists_congr (λ x, and_congr_left' lt_succ_iff)) end nat
e004db79fec13d328db21e64787558361e50dddf
74caf7451c921a8d5ab9c6e2b828c9d0a35aae95
/tests/lean/run/assert_tac3.lean
be8679902e344a5ba2b85ee1e4674154326092cf
[ "Apache-2.0" ]
permissive
sakas--/lean
f37b6fad4fd4206f2891b89f0f8135f57921fc3f
570d9052820be1d6442a5cc58ece37397f8a9e4c
refs/heads/master
1,586,127,145,194
1,480,960,018,000
1,480,960,635,000
40,137,176
0
0
null
1,438,621,351,000
1,438,621,351,000
null
UTF-8
Lean
false
false
813
lean
open tactic definition tst2 (a : nat) : a = a := by do assert `x (expr.const `nat []), rotate 1, trace_state, a ← get_local `a, mk_app `eq.refl [a] >>= exact, a ← get_local `a, exact a, return () print tst2 definition tst3 (a b : nat) : a = a := by do define `x (expr.const `nat []), rotate 1, trace_state, x ← get_local `x, mk_app `eq.refl [x] >>= exact, trace "-- second goal was indirectly solved by the previous tactic", trace_state, return () definition tst4 (a : nat) : a = a := begin assert x : nat, rotate 1, exact eq.refl a, exact a end definition tst5 (a : nat) : a = a := begin definev x : nat := a, trace_state, exact eq.refl x end definition tst6 (a : nat) : a = a := begin note x := a, trace_state, exact eq.refl x end
2fbe478241336e32a305c7ea7cd701a39ebc1716
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/algebra/operations.lean
1a62ffbb7f7d87630d0cad6c286206820556f665
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,051
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.basic /-! # Multiplication and division of submodules of an algebra. An interface for multiplication and division of sub-R-modules of an R-algebra A is developed. ## Main definitions Let `R` be a commutative ring (or semiring) and aet `A` be an `R`-algebra. * `1 : submodule R A` : the R-submodule R of the R-algebra A * `has_mul (submodule R A)` : multiplication of two sub-R-modules M and N of A is defined to be the smallest submodule containing all the products `m * n`. * `has_div (submodule R A)` : `I / J` is defined to be the submodule consisting of all `a : A` such that `a • J ⊆ I` It is proved that `submodule R A` is a semiring, and also an algebra over `set A`. ## Tags multiplication of submodules, division of subodules, submodule semiring -/ universes u v open algebra set namespace submodule variables {R : Type u} [comm_semiring R] section ring variables {A : Type v} [semiring A] [algebra R A] variables (S T : set A) {M N P Q : submodule R A} {m n : A} /-- `1 : submodule R A` is the submodule R of A. -/ instance : has_one (submodule R A) := ⟨submodule.map (of_id R A).to_linear_map (⊤ : submodule R R)⟩ theorem one_eq_map_top : (1 : submodule R A) = submodule.map (of_id R A).to_linear_map (⊤ : submodule R R) := rfl theorem one_eq_span : (1 : submodule R A) = R ∙ 1 := begin apply submodule.ext, intro a, erw [mem_map, mem_span_singleton], apply exists_congr, intro r, simpa [smul_def], end theorem one_le : (1 : submodule R A) ≤ P ↔ (1 : A) ∈ P := by simpa only [one_eq_span, span_le, set.singleton_subset_iff] /-- Multiplication of sub-R-modules of an R-algebra A. The submodule `M * N` is the smallest R-submodule of `A` containing the elements `m * n` for `m ∈ M` and `n ∈ N`. -/ instance : has_mul (submodule R A) := ⟨λ M N, ⨆ s : M, N.map $ algebra.lmul R A s.1⟩ theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := (le_supr _ ⟨m, hm⟩ : _ ≤ M * N) ⟨n, hn, rfl⟩ theorem mul_le : M * N ≤ P ↔ ∀ (m ∈ M) (n ∈ N), m * n ∈ P := ⟨λ H m hm n hn, H $ mul_mem_mul hm hn, λ H, supr_le $ λ ⟨m, hm⟩, map_le_iff_le_comap.2 $ λ n hn, H m hm n hn⟩ @[elab_as_eliminator] protected theorem mul_induction_on {C : A → Prop} {r : A} (hr : r ∈ M * N) (hm : ∀ (m ∈ M) (n ∈ N), C (m * n)) (h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y)) (hs : ∀ (r : R) x, C x → C (r • x)) : C r := (@mul_le _ _ _ _ _ _ _ ⟨C, h0, ha, hs⟩).2 hm hr variables R theorem span_mul_span : span R S * span R T = span R (S * T) := begin apply le_antisymm, { rw mul_le, intros a ha b hb, apply span_induction ha, work_on_goal 0 { intros, apply span_induction hb, work_on_goal 0 { intros, exact subset_span ⟨_, _, ‹_›, ‹_›, rfl⟩ } }, all_goals { intros, simp only [mul_zero, zero_mul, zero_mem, left_distrib, right_distrib, mul_smul_comm, smul_mul_assoc], try {apply add_mem _ _ _}, try {apply smul_mem _ _ _} }, assumption' }, { rw span_le, rintros _ ⟨a, b, ha, hb, rfl⟩, exact mul_mem_mul (subset_span ha) (subset_span hb) } end variables {R} variables (M N P Q) protected theorem mul_assoc : (M * N) * P = M * (N * P) := le_antisymm (mul_le.2 $ λ mn hmn p hp, suffices M * N ≤ (M * (N * P)).comap (algebra.lmul_right R p), from this hmn, mul_le.2 $ λ m hm n hn, show m * n * p ∈ M * (N * P), from (mul_assoc m n p).symm ▸ mul_mem_mul hm (mul_mem_mul hn hp)) (mul_le.2 $ λ m hm np hnp, suffices N * P ≤ (M * N * P).comap (algebra.lmul_left R m), from this hnp, mul_le.2 $ λ n hn p hp, show m * (n * p) ∈ M * N * P, from mul_assoc m n p ▸ mul_mem_mul (mul_mem_mul hm hn) hp) @[simp] theorem mul_bot : M * ⊥ = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hn ⊢; rw [hn, mul_zero] @[simp] theorem bot_mul : ⊥ * M = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hm ⊢; rw [hm, zero_mul] @[simp] protected theorem one_mul : (1 : submodule R A) * M = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, one_mul, span_eq] } @[simp] protected theorem mul_one : M * 1 = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, mul_one, span_eq] } variables {M N P Q} @[mono] theorem mul_le_mul (hmp : M ≤ P) (hnq : N ≤ Q) : M * N ≤ P * Q := mul_le.2 $ λ m hm n hn, mul_mem_mul (hmp hm) (hnq hn) theorem mul_le_mul_left (h : M ≤ N) : M * P ≤ N * P := mul_le_mul h (le_refl P) theorem mul_le_mul_right (h : N ≤ P) : M * N ≤ M * P := mul_le_mul (le_refl M) h variables (M N P) theorem mul_sup : M * (N ⊔ P) = M * N ⊔ M * P := le_antisymm (mul_le.2 $ λ m hm np hnp, let ⟨n, hn, p, hp, hnp⟩ := mem_sup.1 hnp in mem_sup.2 ⟨_, mul_mem_mul hm hn, _, mul_mem_mul hm hp, hnp ▸ (mul_add m n p).symm⟩) (sup_le (mul_le_mul_right le_sup_left) (mul_le_mul_right le_sup_right)) theorem sup_mul : (M ⊔ N) * P = M * P ⊔ N * P := le_antisymm (mul_le.2 $ λ mn hmn p hp, let ⟨m, hm, n, hn, hmn⟩ := mem_sup.1 hmn in mem_sup.2 ⟨_, mul_mem_mul hm hp, _, mul_mem_mul hn hp, hmn ▸ (add_mul m n p).symm⟩) (sup_le (mul_le_mul_left le_sup_left) (mul_le_mul_left le_sup_right)) lemma mul_subset_mul : (↑M : set A) * (↑N : set A) ⊆ (↑(M * N) : set A) := by { rintros _ ⟨i, j, hi, hj, rfl⟩, exact mul_mem_mul hi hj } lemma map_mul {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') : map f.to_linear_map (M * N) = map f.to_linear_map M * map f.to_linear_map N := calc map f.to_linear_map (M * N) = ⨆ (i : M), (N.map (lmul R A i)).map f.to_linear_map : map_supr _ _ ... = map f.to_linear_map M * map f.to_linear_map N : begin apply congr_arg Sup, ext S, split; rintros ⟨y, hy⟩, { use [f y, mem_map.mpr ⟨y.1, y.2, rfl⟩], refine trans _ hy, ext, simp }, { obtain ⟨y', hy', fy_eq⟩ := mem_map.mp y.2, use [y', hy'], refine trans _ hy, rw f.to_linear_map_apply at fy_eq, ext, simp [fy_eq] } end section decidable_eq open_locale classical lemma mem_span_mul_finite_of_mem_span_mul {S : set A} {S' : set A} {x : A} (hx : x ∈ span R (S * S')) : ∃ (T T' : finset A), ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ x ∈ span R (T * T' : set A) := begin obtain ⟨U, h, hU⟩ := mem_span_finite_of_mem_span hx, obtain ⟨T, T', hS, hS', h⟩ := finset.subset_mul h, use [T, T', hS, hS'], have h' : (U : set A) ⊆ T * T', { assumption_mod_cast, }, have h'' := span_mono h' hU, assumption, end end decidable_eq lemma mem_span_mul_finite_of_mem_mul {P Q : submodule R A} {x : A} (hx : x ∈ P * Q) : ∃ (T T' : finset A), (T : set A) ⊆ P ∧ (T' : set A) ⊆ Q ∧ x ∈ span R (T * T' : set A) := submodule.mem_span_mul_finite_of_mem_span_mul (by rwa [← submodule.span_eq P, ← submodule.span_eq Q, submodule.span_mul_span] at hx) variables {M N P} /-- Sub-R-modules of an R-algebra form a semiring. -/ instance : semiring (submodule R A) := { one_mul := submodule.one_mul, mul_one := submodule.mul_one, mul_assoc := submodule.mul_assoc, zero_mul := bot_mul, mul_zero := mul_bot, left_distrib := mul_sup, right_distrib := sup_mul, ..submodule.add_comm_monoid_submodule, ..submodule.has_one, ..submodule.has_mul } variables (M) lemma pow_subset_pow {n : ℕ} : (↑M : set A)^n ⊆ ↑(M^n : submodule R A) := begin induction n with n ih, { erw [pow_zero, pow_zero, set.singleton_subset_iff], rw [set_like.mem_coe, ← one_le], exact le_refl _ }, { rw [pow_succ, pow_succ], refine set.subset.trans (set.mul_subset_mul (subset.refl _) ih) _, apply mul_subset_mul } end /-- `span` is a semiring homomorphism (recall multiplication is pointwise multiplication of subsets on either side). -/ def span.ring_hom : set_semiring A →+* submodule R A := { to_fun := submodule.span R, map_zero' := span_empty, map_one' := le_antisymm (span_le.2 $ singleton_subset_iff.2 ⟨1, ⟨⟩, (algebra_map R A).map_one⟩) (map_le_iff_le_comap.2 $ λ r _, mem_span_singleton.2 ⟨r, (algebra_map_eq_smul_one r).symm⟩), map_add' := span_union, map_mul' := λ s t, by erw [span_mul_span, ← image_mul_prod] } end ring section comm_ring variables {A : Type v} [comm_semiring A] [algebra R A] variables {M N : submodule R A} {m n : A} theorem mul_mem_mul_rev (hm : m ∈ M) (hn : n ∈ N) : n * m ∈ M * N := mul_comm m n ▸ mul_mem_mul hm hn variables (M N) protected theorem mul_comm : M * N = N * M := le_antisymm (mul_le.2 $ λ r hrm s hsn, mul_mem_mul_rev hsn hrm) (mul_le.2 $ λ r hrn s hsm, mul_mem_mul_rev hsm hrn) /-- Sub-R-modules of an R-algebra A form a semiring. -/ instance : comm_semiring (submodule R A) := { mul_comm := submodule.mul_comm, .. submodule.semiring } variables (R A) /-- R-submodules of the R-algebra A are a module over `set A`. -/ instance semimodule_set : semimodule (set_semiring A) (submodule R A) := { smul := λ s P, span R s * P, smul_add := λ _ _ _, mul_add _ _ _, add_smul := λ s t P, show span R (s ⊔ t) * P = _, by { erw [span_union, right_distrib] }, mul_smul := λ s t P, show _ = _ * (_ * _), by { rw [← mul_assoc, span_mul_span, ← image_mul_prod] }, one_smul := λ P, show span R {(1 : A)} * P = _, by { conv_lhs {erw ← span_eq P}, erw [span_mul_span, one_mul, span_eq] }, zero_smul := λ P, show span R ∅ * P = ⊥, by erw [span_empty, bot_mul], smul_zero := λ _, mul_bot _ } variables {R A} lemma smul_def {s : set_semiring A} {P : submodule R A} : s • P = span R s * P := rfl lemma smul_le_smul {s t : set_semiring A} {M N : submodule R A} (h₁ : s.down ≤ t.down) (h₂ : M ≤ N) : s • M ≤ t • N := mul_le_mul (span_mono h₁) h₂ lemma smul_singleton (a : A) (M : submodule R A) : ({a} : set A).up • M = M.map (lmul_left _ a) := begin conv_lhs {rw ← span_eq M}, change span _ _ * span _ _ = _, rw [span_mul_span], apply le_antisymm, { rw span_le, rintros _ ⟨b, m, hb, hm, rfl⟩, rw [set_like.mem_coe, mem_map, set.mem_singleton_iff.mp hb], exact ⟨m, hm, rfl⟩ }, { rintros _ ⟨m, hm, rfl⟩, exact subset_span ⟨a, m, set.mem_singleton a, hm, rfl⟩ } end section quotient /-- The elements of `I / J` are the `x` such that `x • J ⊆ I`. In fact, we define `x ∈ I / J` to be `∀ y ∈ J, x * y ∈ I` (see `mem_div_iff_forall_mul_mem`), which is equivalent to `x • J ⊆ I` (see `mem_div_iff_smul_subset`), but nicer to use in proofs. This is the general form of the ideal quotient, traditionally written $I : J$. -/ instance : has_div (submodule R A) := ⟨ λ I J, { carrier := { x | ∀ y ∈ J, x * y ∈ I }, zero_mem' := λ y hy, by { rw zero_mul, apply submodule.zero_mem }, add_mem' := λ a b ha hb y hy, by { rw add_mul, exact submodule.add_mem _ (ha _ hy) (hb _ hy) }, smul_mem' := λ r x hx y hy, by { rw algebra.smul_mul_assoc, exact submodule.smul_mem _ _ (hx _ hy) } } ⟩ lemma mem_div_iff_forall_mul_mem {x : A} {I J : submodule R A} : x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I := iff.refl _ lemma mem_div_iff_smul_subset {x : A} {I J : submodule R A} : x ∈ I / J ↔ x • (J : set A) ⊆ I := ⟨ λ h y ⟨y', hy', xy'_eq_y⟩, by { rw ← xy'_eq_y, apply h, assumption }, λ h y hy, h (set.smul_mem_smul_set hy) ⟩ lemma le_div_iff {I J K : submodule R A} : I ≤ J / K ↔ ∀ (x ∈ I) (z ∈ K), x * z ∈ J := iff.refl _ lemma le_div_iff_mul_le {I J K : submodule R A} : I ≤ J / K ↔ I * K ≤ J := by rw [le_div_iff, mul_le] @[simp] lemma one_le_one_div {I : submodule R A} : 1 ≤ 1 / I ↔ I ≤ 1 := begin split, all_goals {intro hI}, {rwa [le_div_iff_mul_le, one_mul] at hI}, {rwa [le_div_iff_mul_le, one_mul]}, end lemma le_self_mul_one_div {I : submodule R A} (hI : I ≤ 1) : I ≤ I * (1 / I) := begin rw [← mul_one I] {occs := occurrences.pos [1]}, apply mul_le_mul_right (one_le_one_div.mpr hI), end lemma mul_one_div_le_one {I : submodule R A} : I * (1 / I) ≤ 1 := begin rw submodule.mul_le, intros m hm n hn, rw [submodule.mem_div_iff_forall_mul_mem] at hn, rw mul_comm, exact hn m hm, end @[simp] lemma map_div {B : Type*} [comm_ring B] [algebra R B] (I J : submodule R A) (h : A ≃ₐ[R] B) : (I / J).map h.to_linear_map = I.map h.to_linear_map / J.map h.to_linear_map := begin ext x, simp only [mem_map, mem_div_iff_forall_mul_mem], split, { rintro ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, exact ⟨x * y, hx _ hy, h.map_mul x y⟩ }, { rintro hx, refine ⟨h.symm x, λ z hz, _, h.apply_symm_apply x⟩, obtain ⟨xz, xz_mem, hxz⟩ := hx (h z) ⟨z, hz, rfl⟩, convert xz_mem, apply h.injective, erw [h.map_mul, h.apply_symm_apply, hxz] } end end quotient end comm_ring end submodule
1bba4f2747ca88fcbf420c9fd16434bd3f224e8b
e151e9053bfd6d71740066474fc500a087837323
/src/hott/types/pullback.lean
a10ae6a51b1ca74530b22213b7dde15c37c617db
[ "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
6,297
lean
/- 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 Theorems about pullbacks -/ import hott.cubical.square universes u v w hott_theory namespace hott open hott.eq hott.equiv hott.is_equiv function hott.prod unit is_trunc hott.sigma variables {A₀₀ : Type _} {A₂₀ : Type _} {A₄₀ : Type _} {A₀₂ : Type _} {A₂₂ : Type _} {A₄₂ : Type _} (f₁₀ : A₀₀ → A₂₀) (f₃₀ : A₂₀ → A₄₀) (f₀₁ : A₀₀ → A₀₂) (f₂₁ : A₂₀ → A₂₂) (f₄₁ : A₄₀ → A₄₂) (f₁₂ : A₀₂ → A₂₂) (f₃₂ : A₂₂ → A₄₂) structure pullback (f₂₁ : A₂₀ → A₂₂) (f₁₂ : A₀₂ → A₂₂) := (fst : A₂₀) (snd : A₀₂) (fst_snd : f₂₁ fst = f₁₂ snd) namespace pullback @[hott] protected def sigma_char : pullback f₂₁ f₁₂ ≃ Σ(a₂₀ : A₂₀) (a₀₂ : A₀₂), f₂₁ a₂₀ = f₁₂ a₀₂ := begin fapply equiv.MK, { intro x, induction x with a₂₀ a₀₂ p, exact ⟨a₂₀, a₀₂, p⟩}, { intro x, induction x with a₂₀ y, induction y with a₀₂ p, exact pullback.mk a₂₀ a₀₂ p}, { intro x, induction x with a₂₀ y, induction y with a₀₂ p, reflexivity}, { intro x, induction x with a₂₀ a₀₂ p, reflexivity}, end variables {f₁₀ f₃₀ f₀₁ f₂₁ f₄₁ f₁₂ f₃₂} @[hott] def pullback_corec (p : Πa, f₂₁ (f₁₀ a) = f₁₂ (f₀₁ a)) (a : A₀₀) : pullback f₂₁ f₁₂ := pullback.mk (f₁₀ a) (f₀₁ a) (p a) @[hott] def pullback_eq {x y : pullback f₂₁ f₁₂} (p1 : fst x = fst y) (p2 : snd x = snd y) (r : square (fst_snd x) (fst_snd y) (ap f₂₁ p1) (ap f₁₂ p2)) : x = y := begin induction y, induction x, dsimp at *, induction p1, induction p2, exact ap (pullback.mk _ _) (eq_of_vdeg_square r) end @[hott] def pullback_comm_equiv : pullback f₁₂ f₂₁ ≃ pullback f₂₁ f₁₂ := begin fapply equiv.MK, { intro v, induction v with x y p, exact pullback.mk y x p⁻¹}, { intro v, induction v with x y p, exact pullback.mk y x p⁻¹}, { intro v, induction v, dsimp, exact ap _ (inv_inv _)}, { intro v, induction v, dsimp, exact ap _ (inv_inv _)}, end @[hott] def pullback_unit_equiv : pullback (λ(x : A₀₂), star) (λ(x : A₂₀), star) ≃ A₀₂ × A₂₀ := begin fapply equiv.MK, { intro v, induction v with x y p, exact (x, y)}, { intro v, induction v with x y, exact pullback.mk x y idp}, { intro v, induction v, reflexivity}, { intro v, induction v, dsimp, apply ap _ (is_prop.elim _ _), apply_instance }, end @[hott] def pullback_along {f : A₂₀ → A₂₂} (g : A₀₂ → A₂₂) : pullback f g → A₂₀ := fst postfix `^*`:(max+1) := pullback_along variables (f₁₀ f₃₀ f₀₁ f₂₁ f₄₁ f₁₂ f₃₂) structure pullback_square (f₁₀ : A₀₀ → A₂₀) (f₁₂ : A₀₂ → A₂₂) (f₀₁ : A₀₀ → A₀₂) (f₂₁ : A₂₀ → A₂₂) := (comm : Πa, f₂₁ (f₁₀ a) = f₁₂ (f₀₁ a)) (is_pullback : is_equiv (pullback_corec comm : A₀₀ → pullback f₂₁ f₁₂)) attribute [instance] pullback_square.is_pullback @[hott] def pbs_comm := @pullback_square.comm @[hott] def pullback_square_pullback : pullback_square (fst : pullback f₂₁ f₁₂ → A₂₀) f₁₂ snd f₂₁ := pullback_square.mk fst_snd (adjointify _ (λf, f) (λf, by induction f; reflexivity) (λg, by induction g; reflexivity)) variables {f₁₀ f₃₀ f₀₁ f₂₁ f₄₁ f₁₂ f₃₂} @[hott] def pullback_square_equiv (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) : A₀₀ ≃ pullback f₂₁ f₁₂ := equiv.mk _ (pullback_square.is_pullback s) @[hott] def of_pullback (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) (x : pullback f₂₁ f₁₂) : A₀₀ := inv (pullback_square_equiv s) x @[hott] def right_of_pullback (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) (x : pullback f₂₁ f₁₂) : f₁₀ (of_pullback s x) = fst x := ap fst (to_right_inv (pullback_square_equiv s) x) @[hott] def down_of_pullback (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) (x : pullback f₂₁ f₁₂) : f₀₁ (of_pullback s x) = snd x := ap snd (to_right_inv (pullback_square_equiv s) x) -- @[hott] def pullback_square_compose_inverse (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) -- (t : pullback_square f₃₀ f₃₂ f₂₁ f₄₁) (x : pullback f₄₁ (f₃₂ ∘ f₁₂)) : A₀₀ := -- let a₂₀' : pullback f₄₁ f₃₂ := -- pullback.mk (fst x) (f₁₂ (snd x)) (fst_snd x) in -- let a₂₀ : A₂₀ := -- of_pullback t a₂₀' in -- have a₀₀' : pullback f₂₁ f₁₂, -- from pullback.mk a₂₀ (snd x) !down_of_pullback, -- show A₀₀, -- from of_pullback s a₀₀' -- local attribute pullback_square_compose_inverse [reducible] -- @[hott] def down_psci (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) -- (t : pullback_square f₃₀ f₃₂ f₂₁ f₄₁) (x : pullback f₄₁ (f₃₂ ∘ f₁₂)) : -- f₀₁ (pullback_square_compose_inverse s t x) = snd x := -- by apply down_of_pullback -- @[hott] def pullback_square_compose (s : pullback_square f₁₀ f₁₂ f₀₁ f₂₁) -- (t : pullback_square f₃₀ f₃₂ f₂₁ f₄₁) : pullback_square (f₃₀ ∘ f₁₀) (f₃₂ ∘ f₁₂) f₀₁ f₄₁ := -- pullback_square.mk -- (λa, pbs_comm t (f₁₀ a) ⬝ ap f₃₂ (pbs_comm s a)) -- (adjointify _ -- (pullback_square_compose_inverse s t) -- begin -- intro x, induction x with x y p, esimp, -- fapply pullback_eq: esimp, -- { exact ap f₃₀ !right_of_pullback ⬝ !right_of_pullback}, -- { apply down_of_pullback}, -- { esimp, exact sorry } -- end -- begin -- intro x, esimp, exact sorry -- end) end pullback end hott
302973814e9c44cbd5d4e76facabf3d73a469919
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/multiplication/6.lean
ca70c30641e1adb3081ac328ff95872630916063
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
184
lean
lemma succ_mul (a b : mynat) : succ a * b = a * b + b := induction b with n hn, repeat {rw mul_zero}, simp, repeat {rw mul_succ}, rw hn, repeat {rw add_succ}, simp, end
9c35aa0261de36124b75b40339a0eb88ba171397
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/analytic/linear.lean
aedfcc640553b4f00d34d777a676484b72866db0
[]
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,696
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.analytic.basic import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # 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)`. -/ 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 {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) : formal_multilinear_series 𝕜 E F := sorry @[simp] theorem fpower_series_apply_add_two {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) (n : ℕ) : fpower_series f x (n + bit0 1) = 0 := rfl @[simp] theorem fpower_series_radius {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) : formal_multilinear_series.radius (fpower_series f x) = ⊤ := formal_multilinear_series.radius_eq_top_of_forall_image_add_eq_zero (fpower_series f x) (bit0 1) fun (n : ℕ) => rfl protected theorem has_fpower_series_on_ball {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) : has_fpower_series_on_ball (⇑f) (fpower_series f x) x ⊤ := sorry protected theorem has_fpower_series_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) : has_fpower_series_at (⇑f) (fpower_series f x) x := Exists.intro ⊤ (continuous_linear_map.has_fpower_series_on_ball f x) protected theorem analytic_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) (x : E) : analytic_at 𝕜 (⇑f) x := has_fpower_series_at.analytic_at (continuous_linear_map.has_fpower_series_at f x)
2dc3a2843b7642b3f6e87e5b78e9de450ecf236b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/closure_bug5.lean
1690d3d1bec1a5f51c8a31c0daf5533b9dbead40
[ "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
541
lean
def f (x : Nat) : Nat × (Nat → String) := let x1 := x + 1; let x2 := x + 2; let x3 := x + 3; let x4 := x + 4; let x5 := x + 5; let x6 := x + 6; let x7 := x + 7; let x8 := x + 8; let x9 := x + 9; let x10 := x + 10; let x11 := x + 11; let x12 := x + 12; let x13 := x + 13; let x14 := x + 14; let x15 := x + 15; let x16 := x + 16; let x17 := x + 17; (x, fun y => toString [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14]) def main (xs : List String) : IO Unit := IO.println ((f (xs.headD "0").toNat!).2 (xs.headD "0").toNat!)
0a4141c7dc6b4b935fbedc73f2874f51bdc71e4e
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/algebraic_geometry/prime_spectrum.lean
859b28c709ea5a5f9d46e762a92e3c33734ac9ef
[ "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
20,112
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 topology.opens import ring_theory.ideal.prod import linear_algebra.finsupp import algebra.punit_instances /-! # Prime spectrum of a commutative ring The prime spectrum of a commutative ring is the type of all prime ideals. It is naturally endowed with a topology: the Zariski topology. (It is also naturally endowed with a sheaf of rings, which is constructed in `algebraic_geometry.structure_sheaf`.) ## Main definitions * `prime_spectrum R`: The prime spectrum of a commutative ring `R`, i.e., the set of all prime ideals of `R`. * `zero_locus s`: The zero locus of a subset `s` of `R` is the subset of `prime_spectrum R` consisting of all prime ideals that contain `s`. * `vanishing_ideal t`: The vanishing ideal of a subset `t` of `prime_spectrum R` is the intersection of points in `t` (viewed as prime ideals). ## Conventions We denote subsets of rings with `s`, `s'`, etc... whereas we denote subsets of prime spectra with `t`, `t'`, etc... ## Inspiration/contributors The contents of this file draw inspiration from <https://github.com/ramonfmir/lean-scheme> which has contributions from Ramon Fernandez Mir, Kevin Buzzard, Kenny Lau, and Chris Hughes (on an earlier repository). -/ noncomputable theory open_locale classical universes u v variables (R : Type u) [comm_ring R] /-- The prime spectrum of a commutative ring `R` is the type of all prime ideals of `R`. It is naturally endowed with a topology (the Zariski topology), and a sheaf of commutative rings (see `algebraic_geometry.structure_sheaf`). It is a fundamental building block in algebraic geometry. -/ @[nolint has_inhabited_instance] def prime_spectrum := {I : ideal R // I.is_prime} variable {R} namespace prime_spectrum /-- A method to view a point in the prime spectrum of a commutative ring as an ideal of that ring. -/ abbreviation as_ideal (x : prime_spectrum R) : ideal R := x.val instance is_prime (x : prime_spectrum R) : x.as_ideal.is_prime := x.2 /-- The prime spectrum of the zero ring is empty. -/ lemma punit (x : prime_spectrum punit) : false := x.1.ne_top_iff_one.1 x.2.1 $ subsingleton.elim (0 : punit) 1 ▸ x.1.zero_mem section variables (R) (S : Type v) [comm_ring S] /-- The prime spectrum of `R × S` is in bijection with the disjoint unions of the prime spectrum of `R` and the prime spectrum of `S`. -/ noncomputable def prime_spectrum_prod : prime_spectrum (R × S) ≃ prime_spectrum R ⊕ prime_spectrum S := ideal.prime_ideals_equiv R S variables {R S} @[simp] lemma prime_spectrum_prod_symm_inl_as_ideal (x : prime_spectrum R) : ((prime_spectrum_prod R S).symm (sum.inl x)).as_ideal = ideal.prod x.as_ideal ⊤ := by { cases x, refl } @[simp] lemma prime_spectrum_prod_symm_inr_as_ideal (x : prime_spectrum S) : ((prime_spectrum_prod R S).symm (sum.inr x)).as_ideal = ideal.prod ⊤ x.as_ideal := by { cases x, refl } end @[ext] lemma ext {x y : prime_spectrum R} : x = y ↔ x.as_ideal = y.as_ideal := subtype.ext_iff_val /-- The zero locus of a set `s` of elements of a commutative ring `R` is the set of all prime ideals of the ring that contain the set `s`. An element `f` of `R` can be thought of as a dependent function on the prime spectrum of `R`. At a point `x` (a prime ideal) the function (i.e., element) `f` takes values in the quotient ring `R` modulo the prime ideal `x`. In this manner, `zero_locus s` is exactly the subset of `prime_spectrum R` where all "functions" in `s` vanish simultaneously. -/ def zero_locus (s : set R) : set (prime_spectrum R) := {x | s ⊆ x.as_ideal} @[simp] lemma mem_zero_locus (x : prime_spectrum R) (s : set R) : x ∈ zero_locus s ↔ s ⊆ x.as_ideal := iff.rfl @[simp] lemma zero_locus_span (s : set R) : zero_locus (ideal.span s : set R) = zero_locus s := by { ext x, exact (submodule.gi R R).gc s x.as_ideal } /-- The vanishing ideal of a set `t` of points of the prime spectrum of a commutative ring `R` is the intersection of all the prime ideals in the set `t`. An element `f` of `R` can be thought of as a dependent function on the prime spectrum of `R`. At a point `x` (a prime ideal) the function (i.e., element) `f` takes values in the quotient ring `R` modulo the prime ideal `x`. In this manner, `vanishing_ideal t` is exactly the ideal of `R` consisting of all "functions" that vanish on all of `t`. -/ def vanishing_ideal (t : set (prime_spectrum R)) : ideal R := ⨅ (x : prime_spectrum R) (h : x ∈ t), x.as_ideal lemma coe_vanishing_ideal (t : set (prime_spectrum R)) : (vanishing_ideal t : set R) = {f : R | ∀ x : prime_spectrum R, x ∈ t → f ∈ x.as_ideal} := begin ext f, rw [vanishing_ideal, set_like.mem_coe, submodule.mem_infi], apply forall_congr, intro x, rw [submodule.mem_infi], end lemma mem_vanishing_ideal (t : set (prime_spectrum R)) (f : R) : f ∈ vanishing_ideal t ↔ ∀ x : prime_spectrum R, x ∈ t → f ∈ x.as_ideal := by rw [← set_like.mem_coe, coe_vanishing_ideal, set.mem_set_of_eq] @[simp] lemma vanishing_ideal_singleton (x : prime_spectrum R) : vanishing_ideal ({x} : set (prime_spectrum R)) = x.as_ideal := by simp [vanishing_ideal] lemma subset_zero_locus_iff_le_vanishing_ideal (t : set (prime_spectrum R)) (I : ideal R) : t ⊆ zero_locus I ↔ I ≤ vanishing_ideal t := ⟨λ h f k, (mem_vanishing_ideal _ _).mpr (λ x j, (mem_zero_locus _ _).mpr (h j) k), λ h, λ x j, (mem_zero_locus _ _).mpr (le_trans h (λ f h, ((mem_vanishing_ideal _ _).mp h) x j))⟩ section gc variable (R) /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc : @galois_connection (ideal R) (order_dual (set (prime_spectrum R))) _ _ (λ I, zero_locus I) (λ t, vanishing_ideal t) := λ I t, subset_zero_locus_iff_le_vanishing_ideal t I /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_set : @galois_connection (set R) (order_dual (set (prime_spectrum R))) _ _ (λ s, zero_locus s) (λ t, vanishing_ideal t) := have ideal_gc : galois_connection (ideal.span) coe := (submodule.gi R R).gc, by simpa [zero_locus_span, function.comp] using ideal_gc.compose (gc R) lemma subset_zero_locus_iff_subset_vanishing_ideal (t : set (prime_spectrum R)) (s : set R) : t ⊆ zero_locus s ↔ s ⊆ vanishing_ideal t := (gc_set R) s t end gc lemma subset_vanishing_ideal_zero_locus (s : set R) : s ⊆ vanishing_ideal (zero_locus s) := (gc_set R).le_u_l s lemma le_vanishing_ideal_zero_locus (I : ideal R) : I ≤ vanishing_ideal (zero_locus I) := (gc R).le_u_l I @[simp] lemma vanishing_ideal_zero_locus_eq_radical (I : ideal R) : vanishing_ideal (zero_locus (I : set R)) = I.radical := ideal.ext $ λ f, begin rw [mem_vanishing_ideal, ideal.radical_eq_Inf, submodule.mem_Inf], exact ⟨(λ h x hx, h ⟨x, hx.2⟩ hx.1), (λ h x hx, h x.1 ⟨hx, x.2⟩)⟩ end @[simp] lemma zero_locus_radical (I : ideal R) : zero_locus (I.radical : set R) = zero_locus I := vanishing_ideal_zero_locus_eq_radical I ▸ (gc R).l_u_l_eq_l I lemma subset_zero_locus_vanishing_ideal (t : set (prime_spectrum R)) : t ⊆ zero_locus (vanishing_ideal t) := (gc R).l_u_le t lemma zero_locus_anti_mono {s t : set R} (h : s ⊆ t) : zero_locus t ⊆ zero_locus s := (gc_set R).monotone_l h lemma zero_locus_anti_mono_ideal {s t : ideal R} (h : s ≤ t) : zero_locus (t : set R) ⊆ zero_locus (s : set R) := (gc R).monotone_l h lemma vanishing_ideal_anti_mono {s t : set (prime_spectrum R)} (h : s ⊆ t) : vanishing_ideal t ≤ vanishing_ideal s := (gc R).monotone_u h lemma zero_locus_subset_zero_locus_iff (I J : ideal R) : zero_locus (I : set R) ⊆ zero_locus (J : set R) ↔ J ≤ I.radical := ⟨λ h, ideal.radical_le_radical_iff.mp (vanishing_ideal_zero_locus_eq_radical I ▸ vanishing_ideal_zero_locus_eq_radical J ▸ vanishing_ideal_anti_mono h), λ h, zero_locus_radical I ▸ zero_locus_anti_mono_ideal h⟩ lemma zero_locus_subset_zero_locus_singleton_iff (f g : R) : zero_locus ({f} : set R) ⊆ zero_locus {g} ↔ g ∈ (ideal.span ({f} : set R)).radical := by rw [← zero_locus_span {f}, ← zero_locus_span {g}, zero_locus_subset_zero_locus_iff, ideal.span_le, set.singleton_subset_iff, set_like.mem_coe] lemma zero_locus_bot : zero_locus ((⊥ : ideal R) : set R) = set.univ := (gc R).l_bot @[simp] lemma zero_locus_singleton_zero : zero_locus ({0} : set R) = set.univ := zero_locus_bot @[simp] lemma zero_locus_empty : zero_locus (∅ : set R) = set.univ := (gc_set R).l_bot @[simp] lemma vanishing_ideal_univ : vanishing_ideal (∅ : set (prime_spectrum R)) = ⊤ := by simpa using (gc R).u_top lemma zero_locus_empty_of_one_mem {s : set R} (h : (1:R) ∈ s) : zero_locus s = ∅ := begin rw set.eq_empty_iff_forall_not_mem, intros x hx, rw mem_zero_locus at hx, have x_prime : x.as_ideal.is_prime := by apply_instance, have eq_top : x.as_ideal = ⊤, { rw ideal.eq_top_iff_one, exact hx h }, apply x_prime.ne_top eq_top, end @[simp] lemma zero_locus_singleton_one : zero_locus ({1} : set R) = ∅ := zero_locus_empty_of_one_mem (set.mem_singleton (1 : R)) lemma zero_locus_empty_iff_eq_top {I : ideal R} : zero_locus (I : set R) = ∅ ↔ I = ⊤ := begin split, { contrapose!, intro h, apply set.ne_empty_iff_nonempty.mpr, rcases ideal.exists_le_maximal I h with ⟨M, hM, hIM⟩, exact ⟨⟨M, hM.is_prime⟩, hIM⟩ }, { rintro rfl, apply zero_locus_empty_of_one_mem, trivial } end @[simp] lemma zero_locus_univ : zero_locus (set.univ : set R) = ∅ := zero_locus_empty_of_one_mem (set.mem_univ 1) lemma zero_locus_sup (I J : ideal R) : zero_locus ((I ⊔ J : ideal R) : set R) = zero_locus I ∩ zero_locus J := (gc R).l_sup lemma zero_locus_union (s s' : set R) : zero_locus (s ∪ s') = zero_locus s ∩ zero_locus s' := (gc_set R).l_sup lemma vanishing_ideal_union (t t' : set (prime_spectrum R)) : vanishing_ideal (t ∪ t') = vanishing_ideal t ⊓ vanishing_ideal t' := (gc R).u_inf lemma zero_locus_supr {ι : Sort*} (I : ι → ideal R) : zero_locus ((⨆ i, I i : ideal R) : set R) = (⋂ i, zero_locus (I i)) := (gc R).l_supr lemma zero_locus_Union {ι : Sort*} (s : ι → set R) : zero_locus (⋃ i, s i) = (⋂ i, zero_locus (s i)) := (gc_set R).l_supr lemma zero_locus_bUnion (s : set (set R)) : zero_locus (⋃ s' ∈ s, s' : set R) = ⋂ s' ∈ s, zero_locus s' := by simp only [zero_locus_Union] lemma vanishing_ideal_Union {ι : Sort*} (t : ι → set (prime_spectrum R)) : vanishing_ideal (⋃ i, t i) = (⨅ i, vanishing_ideal (t i)) := (gc R).u_infi lemma zero_locus_inf (I J : ideal R) : zero_locus ((I ⊓ J : ideal R) : set R) = zero_locus I ∪ zero_locus J := set.ext $ λ x, by simpa using x.2.inf_le lemma union_zero_locus (s s' : set R) : zero_locus s ∪ zero_locus s' = zero_locus ((ideal.span s) ⊓ (ideal.span s') : ideal R) := by { rw zero_locus_inf, simp } lemma zero_locus_mul (I J : ideal R) : zero_locus ((I * J : ideal R) : set R) = zero_locus I ∪ zero_locus J := set.ext $ λ x, by simpa using x.2.mul_le lemma zero_locus_singleton_mul (f g : R) : zero_locus ({f * g} : set R) = zero_locus {f} ∪ zero_locus {g} := set.ext $ λ x, by simpa using x.2.mul_mem_iff_mem_or_mem @[simp] lemma zero_locus_pow (I : ideal R) {n : ℕ} (hn : 0 < n) : zero_locus ((I ^ n : ideal R) : set R) = zero_locus I := zero_locus_radical (I ^ n) ▸ (I.radical_pow n hn).symm ▸ zero_locus_radical I @[simp] lemma zero_locus_singleton_pow (f : R) (n : ℕ) (hn : 0 < n) : zero_locus ({f ^ n} : set R) = zero_locus {f} := set.ext $ λ x, by simpa using x.2.pow_mem_iff_mem n hn lemma sup_vanishing_ideal_le (t t' : set (prime_spectrum R)) : vanishing_ideal t ⊔ vanishing_ideal t' ≤ vanishing_ideal (t ∩ t') := begin intros r, rw [submodule.mem_sup, mem_vanishing_ideal], rintro ⟨f, hf, g, hg, rfl⟩ x ⟨hxt, hxt'⟩, rw mem_vanishing_ideal at hf hg, apply submodule.add_mem; solve_by_elim end lemma mem_compl_zero_locus_iff_not_mem {f : R} {I : prime_spectrum R} : I ∈ (zero_locus {f} : set (prime_spectrum R))ᶜ ↔ f ∉ I.as_ideal := by rw [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff]; refl /-- The Zariski topology on the prime spectrum of a commutative ring is defined via the closed sets of the topology: they are exactly those sets that are the zero locus of a subset of the ring. -/ instance zariski_topology : topological_space (prime_spectrum R) := topological_space.of_closed (set.range prime_spectrum.zero_locus) (⟨set.univ, by simp⟩) begin intros Zs h, rw set.sInter_eq_Inter, let f : Zs → set R := λ i, classical.some (h i.2), have hf : ∀ i : Zs, ↑i = zero_locus (f i) := λ i, (classical.some_spec (h i.2)).symm, simp only [hf], exact ⟨_, zero_locus_Union _⟩ end (by { rintro _ _ ⟨s, rfl⟩ ⟨t, rfl⟩, exact ⟨_, (union_zero_locus s t).symm⟩ }) lemma is_open_iff (U : set (prime_spectrum R)) : is_open U ↔ ∃ s, Uᶜ = zero_locus s := by simp only [@eq_comm _ Uᶜ]; refl lemma is_closed_iff_zero_locus (Z : set (prime_spectrum R)) : is_closed Z ↔ ∃ s, Z = zero_locus s := by rw [← is_open_compl_iff, is_open_iff, compl_compl] lemma is_closed_zero_locus (s : set R) : is_closed (zero_locus s) := by { rw [is_closed_iff_zero_locus], exact ⟨s, rfl⟩ } lemma zero_locus_vanishing_ideal_eq_closure (t : set (prime_spectrum R)) : zero_locus (vanishing_ideal t : set R) = closure t := begin apply set.subset.antisymm, { rintro x hx t' ⟨ht', ht⟩, obtain ⟨fs, rfl⟩ : ∃ s, t' = zero_locus s, by rwa [is_closed_iff_zero_locus] at ht', rw [subset_zero_locus_iff_subset_vanishing_ideal] at ht, exact set.subset.trans ht hx }, { rw (is_closed_zero_locus _).closure_subset_iff, exact subset_zero_locus_vanishing_ideal t } end lemma vanishing_ideal_closure (t : set (prime_spectrum R)) : vanishing_ideal (closure t) = vanishing_ideal t := zero_locus_vanishing_ideal_eq_closure t ▸ (gc R).u_l_u_eq_u t section comap variables {S : Type v} [comm_ring S] {S' : Type*} [comm_ring S'] /-- The function between prime spectra of commutative rings induced by a ring homomorphism. This function is continuous. -/ def comap (f : R →+* S) : prime_spectrum S → prime_spectrum R := λ y, ⟨ideal.comap f y.as_ideal, infer_instance⟩ variables (f : R →+* S) @[simp] lemma comap_as_ideal (y : prime_spectrum S) : (comap f y).as_ideal = ideal.comap f y.as_ideal := rfl @[simp] lemma comap_id : comap (ring_hom.id R) = id := funext $ λ _, subtype.ext $ ideal.ext $ λ _, iff.rfl @[simp] lemma comap_comp (f : R →+* S) (g : S →+* S') : comap (g.comp f) = comap f ∘ comap g := funext $ λ _, subtype.ext $ ideal.ext $ λ _, iff.rfl @[simp] lemma preimage_comap_zero_locus (s : set R) : (comap f) ⁻¹' (zero_locus s) = zero_locus (f '' s) := begin ext x, simp only [mem_zero_locus, set.mem_preimage, comap_as_ideal, set.image_subset_iff], refl end lemma comap_continuous (f : R →+* S) : continuous (comap f) := begin rw continuous_iff_is_closed, simp only [is_closed_iff_zero_locus], rintro _ ⟨s, rfl⟩, exact ⟨_, preimage_comap_zero_locus f s⟩ end end comap section basic_open /-- `basic_open r` is the open subset containing all prime ideals not containing `r`. -/ def basic_open (r : R) : topological_space.opens (prime_spectrum R) := { val := { x | r ∉ x.as_ideal }, property := ⟨{r}, set.ext $ λ x, set.singleton_subset_iff.trans $ not_not.symm⟩ } @[simp] lemma mem_basic_open (f : R) (x : prime_spectrum R) : x ∈ basic_open f ↔ f ∉ x.as_ideal := iff.rfl lemma is_open_basic_open {a : R} : is_open ((basic_open a) : set (prime_spectrum R)) := (basic_open a).property @[simp] lemma basic_open_eq_zero_locus_compl (r : R) : (basic_open r : set (prime_spectrum R)) = (zero_locus {r})ᶜ := set.ext $ λ x, by simpa only [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff] @[simp] lemma basic_open_one : basic_open (1 : R) = ⊤ := topological_space.opens.ext $ by {simp, refl} @[simp] lemma basic_open_zero : basic_open (0 : R) = ⊥ := topological_space.opens.ext $ by {simp, refl} lemma basic_open_le_basic_open_iff (f g : R) : basic_open f ≤ basic_open g ↔ f ∈ (ideal.span ({g} : set R)).radical := by rw [topological_space.opens.le_def, basic_open_eq_zero_locus_compl, basic_open_eq_zero_locus_compl, set.le_eq_subset, set.compl_subset_compl, zero_locus_subset_zero_locus_singleton_iff] lemma basic_open_mul (f g : R) : basic_open (f * g) = basic_open f ⊓ basic_open g := topological_space.opens.ext $ by {simp [zero_locus_singleton_mul]} lemma basic_open_mul_le_left (f g : R) : basic_open (f * g) ≤ basic_open f := by { rw basic_open_mul f g, exact inf_le_left } lemma basic_open_mul_le_right (f g : R) : basic_open (f * g) ≤ basic_open g := by { rw basic_open_mul f g, exact inf_le_right } @[simp] lemma basic_open_pow (f : R) (n : ℕ) (hn : 0 < n) : basic_open (f ^ n) = basic_open f := topological_space.opens.ext $ by simpa using zero_locus_singleton_pow f n hn lemma is_topological_basis_basic_opens : topological_space.is_topological_basis (set.range (λ (r : R), (basic_open r : set (prime_spectrum R)))) := begin apply topological_space.is_topological_basis_of_open_of_nhds, { rintros _ ⟨r, rfl⟩, exact is_open_basic_open }, { rintros p U hp ⟨s, hs⟩, rw [← compl_compl U, set.mem_compl_eq, ← hs, mem_zero_locus, set.not_subset] at hp, obtain ⟨f, hfs, hfp⟩ := hp, refine ⟨basic_open f, ⟨f, rfl⟩, hfp, _⟩, rw [← set.compl_subset_compl, ← hs, basic_open_eq_zero_locus_compl, compl_compl], exact zero_locus_anti_mono (set.singleton_subset_iff.mpr hfs) } end lemma is_compact_basic_open (f : R) : is_compact (basic_open f : set (prime_spectrum R)) := is_compact_of_finite_subfamily_closed $ λ ι Z hZc hZ, begin let I : ι → ideal R := λ i, vanishing_ideal (Z i), have hI : ∀ i, Z i = zero_locus (I i) := λ i, by simpa only [zero_locus_vanishing_ideal_eq_closure] using (hZc i).closure_eq.symm, rw [basic_open_eq_zero_locus_compl f, set.inter_comm, ← set.diff_eq, set.diff_eq_empty, funext hI, ← zero_locus_supr] at hZ, obtain ⟨n, hn⟩ : f ∈ (⨆ (i : ι), I i).radical, { rw ← vanishing_ideal_zero_locus_eq_radical, apply vanishing_ideal_anti_mono hZ, exact (subset_vanishing_ideal_zero_locus {f} (set.mem_singleton f)) }, rcases submodule.exists_finset_of_mem_supr I hn with ⟨s, hs⟩, use s, -- Using simp_rw here, because `hI` and `zero_locus_supr` need to be applied underneath binders simp_rw [basic_open_eq_zero_locus_compl f, set.inter_comm, ← set.diff_eq, set.diff_eq_empty, hI, ← zero_locus_supr], rw ← zero_locus_radical, -- this one can't be in `simp_rw` because it would loop apply zero_locus_anti_mono, rw set.singleton_subset_iff, exact ⟨n, hs⟩ end end basic_open /-- The prime spectrum of a commutative ring is a compact topological space. -/ instance : compact_space (prime_spectrum R) := { compact_univ := by { convert is_compact_basic_open (1 : R), rw basic_open_one, refl } } section order /-! ## The specialization order We endow `prime_spectrum R` with a partial order, where `x ≤ y` if and only if `y ∈ closure {x}`. TODO: maybe define sober topological spaces, and generalise this instance to those -/ instance : partial_order (prime_spectrum R) := subtype.partial_order _ @[simp] lemma as_ideal_le_as_ideal (x y : prime_spectrum R) : x.as_ideal ≤ y.as_ideal ↔ x ≤ y := subtype.coe_le_coe @[simp] lemma as_ideal_lt_as_ideal (x y : prime_spectrum R) : x.as_ideal < y.as_ideal ↔ x < y := subtype.coe_lt_coe lemma le_iff_mem_closure (x y : prime_spectrum R) : x ≤ y ↔ y ∈ closure ({x} : set (prime_spectrum R)) := by rw [← as_ideal_le_as_ideal, ← zero_locus_vanishing_ideal_eq_closure, mem_zero_locus, vanishing_ideal_singleton, set_like.coe_subset_coe] end order end prime_spectrum
b4dd150e5079e3669ed930fe8d5f7d65341ce01f
097294e9b80f0d9893ac160b9c7219aa135b51b9
/instructor/propositional_logic/precedence/associativity_and_precedence.lean
b46a3af33bc7b5a49ffe1443cdba77f445244a7a
[]
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
3,440
lean
import .propositional_logic_syntax_and_semantics open pExp -- left associative -- associative #check 2 + 3 + 4 #check (2 + 3) + 4 #check 2 + (3 + 4) #eval 2 + 3 + 4 #eval (2 + 3) + 4 #eval 2 + (3 + 4) -- - is left associative -- - is not associative #check 5 - 3 - 1 #check 5 - (3 - 1) #check (5 - 3) - 1 #eval (5 - 3) - 1 #eval 5 - (3 - 1) -- precedence, bind strenth #check 3 * 4 + 5 #check (3 * 4) + 5 #check 3 * (4 + 5) #eval (3 * 4) + 5 #eval 3 * (4 + 5) /- Motivation: Can't overlad →. First attempt at a solution: overload > to mean pImp, as follows. notation e1 > e2 := pImp e1 e2 Problem: > is a reserved notation in Lean. We can overload it but we cannot change either is precedence or its associativity. The notation is thus non-standard and confusing. -/ def P := pVar (var.mk 0) def Q := pVar (var.mk 1) def R := pVar (var.mk 2) -- associativity is wrong -- left associative X #check P > Q > R #check P > (Q > R) #check (P > Q) > R #check nat → nat → nat #check nat → (nat → nat) #check (nat → nat) → nat -- associativity is wrong #check P ∧ Q > Q > R #check (P ∧ Q) > (Q > R) #check P ∧ (Q > Q) > R -- precedence is wrong, too #check P ∨ Q > Q > R #check (P ∨ Q) > (Q > R) #check P ∨ (Q > Q) > R #check P ∧ Q > Q > R #check (P ∧ Q) > (Q > R) #check P ∧ (Q > Q) > R #check P > Q ↔ Q > P #check (P > Q) ↔ (Q > P) /- Solution: Define our own infix operator with appropriate precedence (also called) binding strength. First, let's see where the other reserved operators that we're using (such as ∧ and ∨) get their binding strengths: from one of Lean's libraries. Here's what appears there (some details omitted). /- /- Logical operations and relations -/ reserve prefix `¬`:40 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 infixl ` ⬝ `:75 reserve infixr ` ▸ `:75 reserve infixr ` ▹ `:75 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve prefix `-`:100 reserve infixr ` ^ `:80 reserve infixr ` ∘ `:90 -- input with \comp reserve infix ` <= `:50 reserve infix ` ≤ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` ≥ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve infixl ` && `:70 reserve infixl ` || `:65 -/ -/ -- Here's our new notation infixr ` >> ` : 25 := pImp -- associativity is correct #check P >> Q >> R #check P >> (Q >> R) #check (P >> Q) >> R -- precedence is (almost) correct #check P ∧ Q >> Q >> R #check (P ∧ Q >> Q) >> R #check (P ∧ Q) >> (Q >> R) #check P ∧ (Q >> Q) >> R #check P ∨ Q >> Q >> R #check P ∨ Q >> (Q >> R) #check P ∨ (Q >> (Q >> R)) --uh oh, another bug! #check (P ∨ Q) >> (Q >> R) /- What is wrong? How to fix it? -/ #check (P ∨ Q) >> (Q >> R) #check (P ∨ Q) >> Q >> R axioms X Y Z : Prop #check X ∨ Y → Y → Z #check X ∨ Y → (Y → Z) #check (P ∨ Q) >> Q >> R #check P ∨ (Q >> Q) >> R #check P ∧ Q >> Q >> R #check (P ∧ Q) >> (Q >> R) #check P ∧ (Q >> Q) >> R #check P >> Q ↔ Q >> P #check (P >> Q) ↔ (Q >> P)
236aa83d8e8df8714ec164f101dd72fc43f5b9b3
c9e78e68dc955b2325401aec3a6d3240cd8b83f4
/src/LTS/default.lean
e95af4e6f45136023c8755162b5407bbe5f02fbf
[]
no_license
loganrjmurphy/lean-strategies
4b8dd54771bb421c929a8bcb93a528ce6c1a70f1
832ea28077701b977b4fc59ed9a8ce6911654e59
refs/heads/master
1,682,732,168,860
1,621,444,295,000
1,621,444,295,000
278,458,841
3
0
null
1,613,755,728,000
1,594,324,763,000
Lean
UTF-8
Lean
false
false
17
lean
import LTS.defs
21203f0aea2014f4e0afc5bd671a4da4363fcc12
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/ring_theory/polynomial.lean
52c3afeed8a2cb05516017e7d15a4db40f23ca34
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
14,091
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Ring-theoretic supplement of data.polynomial. Main result: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. -/ import data.polynomial data.multivariate_polynomial import ring_theory.principal_ideal_domain import ring_theory.subring universes u v w namespace polynomial variables (R : Type u) [comm_ring R] [decidable_eq R] /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : polynomial R} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n): degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, X^n) : finset (polynomial R)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← finsupp.sum_single p, finsupp.sum, submodule.mem_coe], refine submodule.sum_mem _ (λ k hk, _), have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [single_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, apply le_trans (degree_X_pow_le _) (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : polynomial R) : polynomial (ring.closure (↑p.frange : set R)) := ⟨p.support, λ i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem _ else ring.subset_closure $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ @[simp] theorem coeff_restriction {p : polynomial R} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := rfl @[simp] theorem coeff_restriction' {p : polynomial R} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := rfl @[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := rfl @[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := rfl @[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) ↔ monic p := ⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩ @[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := rfl @[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 := ext.2 $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables {S : Type v} [comm_ring S] {f : R → S} {x : S} theorem eval₂_restriction {p : polynomial R} : eval₂ f x p = eval₂ (f ∘ subtype.val) x p.restriction := rfl section to_subring variables (p : polynomial R) (T : set R) [is_subring T] /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T. -/ def to_subring (hp : ↑p.frange ⊆ T) : polynomial T := ⟨p.support, λ i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem _ else hp $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ variables (hp : ↑p.frange ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := rfl @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := rfl @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := rfl @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := rfl @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := ⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩ omit hp @[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (set.empty_subset _) = 0 := rfl @[simp] theorem to_subring_one : to_subring (1 : polynomial R) T (set.subset.trans (finset.coe_subset.2 finsupp.frange_single) (set.singleton_subset_iff.2 (is_submonoid.one_mem _))) = 1 := ext.2 $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl end to_subring variables (T : set R) [is_subring T] /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefificents are in the ambient ring. -/ def of_subring (p : polynomial T) : polynomial R := ⟨p.support, subtype.val ∘ p.to_fun, λ n, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ h, congr_arg subtype.val h, λ h, subtype.eq h⟩)⟩ @[simp] theorem frange_of_subring {p : polynomial T} : ↑(p.of_subring T).frange ⊆ T := λ y H, let ⟨hy, x, hx⟩ := finsupp.mem_frange.1 H in hx ▸ (p.to_fun x).2 end polynomial variables {R : Type u} [comm_ring R] [decidable_eq R] namespace ideal open polynomial /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) := { carrier := I.carrier, zero := I.zero_mem, add := λ _ _, I.add_mem, smul := λ c x H, by rw [← C_mul']; exact submodule.smul_mem _ _ H } variables {I : ideal (polynomial R)} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ leading_coeff p = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, lattice.bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add' (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_refl _ }, { rw [leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [submodule.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add' hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, nat.add_sub_cancel' H], exact le_refl _ end /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, { exact ⟨0⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end theorem is_fg_degree_le [is_noetherian_ring R] (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end ideal /-- Hilbert basis theorem. -/ theorem is_noetherian_ring_polynomial [is_noetherian_ring R] : is_noetherian_ring (polynomial R) := ⟨assume I : ideal (polynomial R), let L := I.leading_coeff in let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) (set.ne_empty_of_mem ⟨0, rfl⟩) in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min well_founded_submodule_gt _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set (polynomial R)), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin change I ≤ ideal.span ↑s, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, letI : nonzero_comm_ring R := { zero_ne_one := this, ..(infer_instance : comm_ring R) }, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul_eq', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, nat.add_sub_cancel', hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ theorem is_noetherian_ring_mv_polynomial_fin {n : ℕ} [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin n) R) := begin induction n with n ih, { exact is_noetherian_ring_of_ring_equiv R ((mv_polynomial.pempty_ring_equiv R).symm.trans $ mv_polynomial.ring_equiv_of_equiv _ ⟨pempty.elim, fin.elim0, λ x, pempty.elim x, λ x, fin.elim0 x⟩) }, exact @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ (mv_polynomial (fin (n+1)) R) _ ((mv_polynomial.option_equiv_left _ _).symm.trans (mv_polynomial.ring_equiv_of_equiv _ ⟨λ x, option.rec_on x 0 fin.succ, λ x, fin.cases none some x, by rintro ⟨none | x⟩; [refl, exact fin.cases_succ _], λ x, fin.cases rfl (λ i, show (option.rec_on (fin.cases none some (fin.succ i) : option (fin n)) 0 fin.succ : fin n.succ) = _, by rw fin.cases_succ) x⟩)) (@@is_noetherian_ring_polynomial _ _ ih) end theorem is_noetherian_ring_mv_polynomial_of_fintype {σ : Type v} [fintype σ] [decidable_eq σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := trunc.induction_on (fintype.equiv_fin σ) $ λ e, @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (mv_polynomial.ring_equiv_of_equiv _ e.symm) is_noetherian_ring_mv_polynomial_fin
1b1858c3c0e6f12f3aaef5c0780e2d595e380635
4727251e0cd73359b15b664c3170e5d754078599
/test/continuity.lean
6801d76bd80ae7abd5a57e4767e7b4bc0581cdad
[ "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
1,861
lean
import topology.tactic import topology.algebra.monoid import topology.instances.real import analysis.special_functions.trigonometric.basic example {X Y : Type*} [topological_space X] [topological_space Y] (f₁ f₂ : X → Y) (hf₁ : continuous f₁) (hf₂ : continuous f₂) (g : Y → ℝ) (hg : continuous g) : continuous (λ x, (max (g (f₁ x)) (g (f₂ x))) + 1) := by guard_proof_term { continuity } ((hg.comp hf₁).max (hg.comp hf₂)).add continuous_const example {κ ι : Type} (K : κ → Type) [∀ k, topological_space (K k)] (I : ι → Type) [∀ i, topological_space (I i)] (e : κ ≃ ι) (F : Π k, homeomorph (K k) (I (e k))) : continuous (λ (f : Π k, K k) (i : ι), F (e.symm i) (f (e.symm i))) := by guard_proof_term { continuity } @continuous_pi _ _ _ _ _ (λ (f : Π k, K k) i, (F (e.symm i)) (f (e.symm i))) (λ (i : ι), ((F (e.symm i)).continuous).comp (continuous_apply (e.symm i))) open real local attribute [continuity] continuous_exp continuous_sin continuous_cos example : continuous (λ x : ℝ, exp ((max x (-x)) + sin x)^2) := by guard_proof_term { continuity } (continuous_exp.comp ((continuous_id.max continuous_id.neg).add continuous_sin)).pow 2 example : continuous (λ x : ℝ, exp ((max x (-x)) + sin (cos x))^2) := by guard_proof_term { continuity } (continuous_exp.comp ((continuous_id'.max continuous_id'.neg).add (continuous_sin.comp continuous_cos))).pow 2 -- Without `md := semireducible` in the call to `apply_rules` in `continuity`, -- this last example would have run off the rails: -- ``` -- example : continuous (λ x : ℝ, exp ((max x (-x)) + sin (cos x))^2) := -- by show_term { continuity! } -- ``` -- produces lots of subgoals, including many copies of -- ⊢ continuous complex.re -- ⊢ continuous complex.exp -- ⊢ continuous coe -- ⊢ continuous (λ (x : ℝ), ↑x)
8bb59b818254d2e131832daf34846d825f7764b7
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Lean/Elab/Log.lean
24c40ef30e319ada47d3bb263f00512ca46772fe
[ "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
2,850
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.Elab.Util import Lean.Elab.Exception namespace Lean namespace Elab class MonadLog (m : Type → Type) := (getRef : m Syntax) (getFileMap : m FileMap) (getFileName : m String) (logMessage : Message → m Unit) instance monadLogTrans (m n) [MonadLog m] [MonadLift m n] : MonadLog n := { getRef := liftM (MonadLog.getRef : m _), getFileMap := liftM (MonadLog.getFileMap : m _), getFileName := liftM (MonadLog.getFileName : m _), logMessage := fun msg => liftM (MonadLog.logMessage msg : m _ ) } export MonadLog (getFileMap getFileName logMessage) open MonadLog (getRef) variables {m : Type → Type} [Monad m] [MonadLog m] [AddMessageDataContext m] def getRefPos : m String.Pos := do ref ← getRef; pure $ ref.getPos.getD 0 def getRefPosition : m Position := do pos ← getRefPos; fileMap ← getFileMap; pure $ fileMap.toPosition pos def logAt (ref : Syntax) (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error): m Unit := do currRef ← getRef; let ref := replaceRef ref currRef; let pos := ref.getPos.getD 0; fileMap ← getFileMap; fileName ← getFileName; msgData ← addMessageDataContext msgData; logMessage { fileName := fileName, pos := fileMap.toPosition pos, data := msgData, severity := severity } def logErrorAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.error def logWarningAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.warning def logInfoAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.information def log (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error): m Unit := do ref ← getRef; logAt ref msgData severity def logError (msgData : MessageData) : m Unit := log msgData MessageSeverity.error def logWarning (msgData : MessageData) : m Unit := log msgData MessageSeverity.warning def logInfo (msgData : MessageData) : m Unit := log msgData MessageSeverity.information def logException [MonadIO m] (ex : Exception) : m Unit := do match ex with | Exception.error ref msg => logErrorAt ref msg | Exception.internal id => unless (id == abortExceptionId) do name ← liftIO $ id.getName; logError ("internal exception: " ++ name) def logTrace (cls : Name) (msgData : MessageData) : m Unit := do logInfo (MessageData.tagged cls msgData) @[inline] def trace [MonadOptions m] (cls : Name) (msg : Unit → MessageData) : m Unit := do opts ← getOptions; when (checkTraceOption opts cls) $ logTrace cls (msg ()) def logDbgTrace [MonadOptions m] (msg : MessageData) : m Unit := do trace `Elab.debug fun _ => msg end Elab end Lean
45353889766dd79882051c28cb520157eca8283d
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Compiler/IR/Borrow.lean
366bbe413b96aca42b6e694a895d04ebe0c6dd5c
[ "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
10,917
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.Compiler.ExportAttr import Lean.Compiler.IR.CompilerM import Lean.Compiler.IR.NormIds namespace Lean namespace IR namespace Borrow namespace OwnedSet abbrev Key := FunId × Index def beq : Key → Key → Bool | (f₁, x₁), (f₂, x₂) => f₁ == f₂ && x₁ == x₂ instance : BEq Key := ⟨beq⟩ def getHash : Key → UInt64 | (f, x) => mixHash (hash f) (hash x) instance : Hashable Key := ⟨getHash⟩ end OwnedSet open OwnedSet (Key) in abbrev OwnedSet := Std.HashMap Key Unit def OwnedSet.insert (s : OwnedSet) (k : OwnedSet.Key) : OwnedSet := Std.HashMap.insert s k () def OwnedSet.contains (s : OwnedSet) (k : OwnedSet.Key) : Bool := Std.HashMap.contains s k /-! We perform borrow inference in a block of mutually recursive functions. Join points are viewed as local functions, and are identified using their local id + the name of the surrounding function. We keep a mapping from function and joint points to parameters (`Array Param`). Recall that `Param` contains the field `borrow`. -/ namespace ParamMap inductive Key where | decl (name : FunId) | jp (name : FunId) (jpid : JoinPointId) deriving BEq def getHash : Key → UInt64 | Key.decl n => hash n | Key.jp n id => mixHash (hash n) (hash id) instance : Hashable Key := ⟨getHash⟩ end ParamMap open ParamMap (Key) abbrev ParamMap := Std.HashMap Key (Array Param) def ParamMap.fmt (map : ParamMap) : Format := let fmts := map.fold (fun fmt k ps => let k := match k with | ParamMap.Key.decl n => format n | ParamMap.Key.jp n id => format n ++ ":" ++ format id fmt ++ Format.line ++ k ++ " -> " ++ formatParams ps) Format.nil "{" ++ (Format.nest 1 fmts) ++ "}" instance : ToFormat ParamMap := ⟨ParamMap.fmt⟩ instance : ToString ParamMap := ⟨fun m => Format.pretty (format m)⟩ namespace InitParamMap /-- Mark parameters that take a reference as borrow -/ def initBorrow (ps : Array Param) : Array Param := ps.map fun p => { p with borrow := p.ty.isObj } /-- We do perform borrow inference for constants marked as `export`. Reason: we current write wrappers in C++ for using exported functions. These wrappers use smart pointers such as `object_ref`. When writing a new wrapper we need to know whether an argument is a borrow inference or not. We can revise this decision when we implement code for generating the wrappers automatically. -/ def initBorrowIfNotExported (exported : Bool) (ps : Array Param) : Array Param := if exported then ps else initBorrow ps partial def visitFnBody (fnid : FunId) : FnBody → StateM ParamMap Unit | FnBody.jdecl j xs v b => do modify fun m => m.insert (ParamMap.Key.jp fnid j) (initBorrow xs) visitFnBody fnid v visitFnBody fnid b | FnBody.case _ _ _ alts => alts.forM fun alt => visitFnBody fnid alt.body | e => do unless e.isTerminal do let (_, b) := e.split visitFnBody fnid b def visitDecls (env : Environment) (decls : Array Decl) : StateM ParamMap Unit := decls.forM fun decl => match decl with | .fdecl (f := f) (xs := xs) (body := b) .. => do let exported := isExport env f modify fun m => m.insert (ParamMap.Key.decl f) (initBorrowIfNotExported exported xs) visitFnBody f b | _ => pure () end InitParamMap def mkInitParamMap (env : Environment) (decls : Array Decl) : ParamMap := (InitParamMap.visitDecls env decls *> get).run' {} /-! Apply the inferred borrow annotations stored at `ParamMap` to a block of mutually recursive functions. -/ namespace ApplyParamMap partial def visitFnBody (fn : FunId) (paramMap : ParamMap) : FnBody → FnBody | FnBody.jdecl j _ v b => let v := visitFnBody fn paramMap v let b := visitFnBody fn paramMap b match paramMap.find? (ParamMap.Key.jp fn j) with | some ys => FnBody.jdecl j ys v b | none => unreachable! | FnBody.case tid x xType alts => FnBody.case tid x xType <| alts.map fun alt => alt.modifyBody (visitFnBody fn paramMap) | e => if e.isTerminal then e else let (instr, b) := e.split let b := visitFnBody fn paramMap b instr.setBody b def visitDecls (decls : Array Decl) (paramMap : ParamMap) : Array Decl := decls.map fun decl => match decl with | Decl.fdecl f _ ty b info => let b := visitFnBody f paramMap b match paramMap.find? (ParamMap.Key.decl f) with | some xs => Decl.fdecl f xs ty b info | none => unreachable! | other => other end ApplyParamMap def applyParamMap (decls : Array Decl) (map : ParamMap) : Array Decl := ApplyParamMap.visitDecls decls map structure BorrowInfCtx where env : Environment decls : Array Decl -- block of mutually recursive functions currFn : FunId := default -- Function being analyzed. paramSet : IndexSet := {} -- Set of all function parameters in scope. This is used to implement the heuristic at `ownArgsUsingParams` structure BorrowInfState where /-- Set of variables that must be `owned`. -/ owned : OwnedSet := {} modified : Bool := false paramMap : ParamMap abbrev M := ReaderT BorrowInfCtx (StateM BorrowInfState) def getCurrFn : M FunId := do let ctx ← read pure ctx.currFn def markModified : M Unit := modify fun s => { s with modified := true } def ownVar (x : VarId) : M Unit := do let currFn ← getCurrFn modify fun s => if s.owned.contains (currFn, x.idx) then s else { s with owned := s.owned.insert (currFn, x.idx), modified := true } def ownArg (x : Arg) : M Unit := match x with | Arg.var x => ownVar x | _ => pure () def ownArgs (xs : Array Arg) : M Unit := xs.forM ownArg def isOwned (x : VarId) : M Bool := do let currFn ← getCurrFn let s ← get return s.owned.contains (currFn, x.idx) /-- Updates `map[k]` using the current set of `owned` variables. -/ def updateParamMap (k : ParamMap.Key) : M Unit := do let s ← get match s.paramMap.find? k with | some ps => do let ps ← ps.mapM fun (p : Param) => do if !p.borrow then pure p else if (← isOwned p.x) then markModified pure { p with borrow := false } else pure p modify fun s => { s with paramMap := s.paramMap.insert k ps } | none => pure () def getParamInfo (k : ParamMap.Key) : M (Array Param) := do let s ← get match s.paramMap.find? k with | some ps => pure ps | none => match k with | ParamMap.Key.decl fn => do let ctx ← read match findEnvDecl ctx.env fn with | some decl => pure decl.params | none => unreachable! | _ => unreachable! /-- For each ps[i], if ps[i] is owned, then mark xs[i] as owned. -/ def ownArgsUsingParams (xs : Array Arg) (ps : Array Param) : M Unit := xs.size.forM fun i => do let x := xs[i]! let p := ps[i]! unless p.borrow do ownArg x /-- For each xs[i], if xs[i] is owned, then mark ps[i] as owned. We use this action to preserve tail calls. That is, if we have a tail call `f xs`, if the i-th parameter is borrowed, but `xs[i]` is owned we would have to insert a `dec xs[i]` after `f xs` and consequently "break" the tail call. -/ def ownParamsUsingArgs (xs : Array Arg) (ps : Array Param) : M Unit := xs.size.forM fun i => do let x := xs[i]! let p := ps[i]! match x with | Arg.var x => if (← isOwned x) then ownVar p.x | _ => pure () /-- Mark `xs[i]` as owned if it is one of the parameters `ps`. We use this action to mark function parameters that are being "packed" inside constructors. This is a heuristic, and is not related with the effectiveness of the reset/reuse optimization. It is useful for code such as ``` def f (x y : obj) := let z := ctor_1 x y; ret z ``` -/ def ownArgsIfParam (xs : Array Arg) : M Unit := do let ctx ← read xs.forM fun x => do match x with | Arg.var x => if ctx.paramSet.contains x.idx then ownVar x | _ => pure () def collectExpr (z : VarId) : Expr → M Unit | Expr.reset _ x => ownVar z *> ownVar x | Expr.reuse x _ _ ys => ownVar z *> ownVar x *> ownArgsIfParam ys | Expr.ctor _ xs => ownVar z *> ownArgsIfParam xs | Expr.proj _ x => do if (← isOwned x) then ownVar z if (← isOwned z) then ownVar x | Expr.fap g xs => do let ps ← getParamInfo (ParamMap.Key.decl g) ownVar z *> ownArgsUsingParams xs ps | Expr.ap x ys => ownVar z *> ownVar x *> ownArgs ys | Expr.pap _ xs => ownVar z *> ownArgs xs | _ => pure () def preserveTailCall (x : VarId) (v : Expr) (b : FnBody) : M Unit := do let ctx ← read match v, b with | (Expr.fap g ys), (FnBody.ret (Arg.var z)) => if ctx.decls.any (·.name == g) && x == z then let ps ← getParamInfo (ParamMap.Key.decl g) ownParamsUsingArgs ys ps | _, _ => pure () def updateParamSet (ctx : BorrowInfCtx) (ps : Array Param) : BorrowInfCtx := { ctx with paramSet := ps.foldl (fun s p => s.insert p.x.idx) ctx.paramSet } partial def collectFnBody : FnBody → M Unit | FnBody.jdecl j ys v b => do withReader (fun ctx => updateParamSet ctx ys) (collectFnBody v) let ctx ← read updateParamMap (ParamMap.Key.jp ctx.currFn j) collectFnBody b | FnBody.vdecl x _ v b => collectFnBody b *> collectExpr x v *> preserveTailCall x v b | FnBody.jmp j ys => do let ctx ← read let ps ← getParamInfo (ParamMap.Key.jp ctx.currFn j) ownArgsUsingParams ys ps -- for making sure the join point can reuse ownParamsUsingArgs ys ps -- for making sure the tail call is preserved | FnBody.case _ _ _ alts => alts.forM fun alt => collectFnBody alt.body | e => do unless e.isTerminal do collectFnBody e.body partial def collectDecl : Decl → M Unit | .fdecl (f := f) (xs := ys) (body := b) .. => withReader (fun ctx => let ctx := updateParamSet ctx ys; { ctx with currFn := f }) do collectFnBody b updateParamMap (ParamMap.Key.decl f) | _ => pure () /-- Keep executing `x` until it reaches a fixpoint -/ @[inline] partial def whileModifing (x : M Unit) : M Unit := do modify fun s => { s with modified := false } x let s ← get if s.modified then whileModifing x else pure () def collectDecls : M ParamMap := do whileModifing ((← read).decls.forM collectDecl) let s ← get pure s.paramMap def infer (env : Environment) (decls : Array Decl) : ParamMap := collectDecls { env, decls } |>.run' { paramMap := mkInitParamMap env decls } end Borrow def inferBorrow (decls : Array Decl) : CompilerM (Array Decl) := do let env ← getEnv let paramMap := Borrow.infer env decls pure (Borrow.applyParamMap decls paramMap) end IR end Lean
9d8bb3df7a325be98f2131ff5766f76e0f230433
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/determinant_auto.lean
d82b5f8a3094fa403b89a27fccf41062830b59b4
[]
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
6,115
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Tim Baanen -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.matrix.pequiv import Mathlib.data.fintype.card import Mathlib.group_theory.perm.sign import Mathlib.algebra.algebra.basic import Mathlib.tactic.ring import Mathlib.linear_algebra.alternating import Mathlib.PostPort universes u v w z u_1 namespace Mathlib namespace matrix /-- The determinant of a matrix given by the Leibniz formula. -/ def det {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) : R := finset.sum finset.univ fun (σ : equiv.perm n) => ↑↑(coe_fn equiv.perm.sign σ) * finset.prod finset.univ fun (i : n) => M (coe_fn σ i) i @[simp] theorem det_diagonal {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {d : n → R} : det (diagonal d) = finset.prod finset.univ fun (i : n) => d i := sorry @[simp] theorem det_zero {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (h : Nonempty n) : det 0 = 0 := sorry @[simp] theorem det_one {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] : det 1 = 1 := sorry theorem det_eq_one_of_card_eq_zero {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {A : matrix n n R} (h : fintype.card n = 0) : det A = 1 := sorry theorem det_mul_aux {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {M : matrix n n R} {N : matrix n n R} {p : n → n} (H : ¬function.bijective p) : (finset.sum finset.univ fun (σ : equiv.perm n) => ↑↑(coe_fn equiv.perm.sign σ) * finset.prod finset.univ fun (x : n) => M (coe_fn σ x) (p x) * N (p x) x) = 0 := sorry @[simp] theorem det_mul {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) (N : matrix n n R) : det (matrix.mul M N) = det M * det N := sorry protected instance det.is_monoid_hom {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] : is_monoid_hom det := is_monoid_hom.mk det_one /-- Transposing a matrix preserves the determinant. -/ @[simp] theorem det_transpose {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) : det (transpose M) = det M := sorry /-- The determinant of a permutation matrix equals its sign. -/ @[simp] theorem det_permutation {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (σ : equiv.perm n) : det (pequiv.to_matrix (equiv.to_pequiv σ)) = ↑(coe_fn equiv.perm.sign σ) := sorry /-- Permuting the columns changes the sign of the determinant. -/ theorem det_permute {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (σ : equiv.perm n) (M : matrix n n R) : (det fun (i : n) => M (coe_fn σ i)) = ↑(coe_fn equiv.perm.sign σ) * det M := sorry @[simp] theorem det_smul {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {A : matrix n n R} {c : R} : det (c • A) = c ^ fintype.card n * det A := sorry theorem ring_hom.map_det {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {S : Type w} [comm_ring S] {M : matrix n n R} {f : R →+* S} : coe_fn f (det M) = det (coe_fn (ring_hom.map_matrix f) M) := sorry theorem alg_hom.map_det {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {S : Type w} [comm_ring S] [algebra R S] {T : Type z} [comm_ring T] [algebra R T] {M : matrix n n S} {f : alg_hom R S T} : coe_fn f (det M) = det (coe_fn (ring_hom.map_matrix ↑f) M) := sorry /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ theorem det_eq_zero_of_row_eq_zero {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {A : matrix n n R} (i : n) (h : ∀ (j : n), A i j = 0) : det A = 0 := sorry theorem det_eq_zero_of_column_eq_zero {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {A : matrix n n R} (j : n) (h : ∀ (i : n), A i j = 0) : det A = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (det A = 0)) (Eq.symm (det_transpose A)))) (det_eq_zero_of_row_eq_zero j h) /-- If a matrix has a repeated row, the determinant will be zero. -/ theorem det_zero_of_row_eq {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {M : matrix n n R} {i : n} {j : n} (i_ne_j : i ≠ j) (hij : M i = M j) : det M = 0 := sorry theorem det_update_column_add {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) (j : n) (u : n → R) (v : n → R) : det (update_column M j (u + v)) = det (update_column M j u) + det (update_column M j v) := sorry theorem det_update_row_add {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) (j : n) (u : n → R) (v : n → R) : det (update_row M j (u + v)) = det (update_row M j u) + det (update_row M j v) := sorry theorem det_update_column_smul {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_column M j (s • u)) = s * det (update_column M j u) := sorry theorem det_update_row_smul {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_row M j (s • u)) = s * det (update_row M j u) := sorry /-- `det` is an alternating multilinear map over the rows of the matrix. See also `is_basis.det`. -/ def det_row_multilinear {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] : alternating_map R (n → R) R n := alternating_map.mk det sorry sorry sorry @[simp] theorem det_block_diagonal {n : Type u} [DecidableEq n] [fintype n] {R : Type v} [comm_ring R] {o : Type u_1} [fintype o] [DecidableEq o] (M : o → matrix n n R) : det (block_diagonal M) = finset.prod finset.univ fun (k : o) => det (M k) := sorry end Mathlib
fe94e4d8705450ce0365784da54ada68789e0853
88892181780ff536a81e794003fe058062f06758
/src/lib/functional.lean
0a30b9dda06888b17a47929b3046ecc355d20c92
[]
no_license
AtnNn/lean-sandbox
fe2c44280444e8bb8146ab8ac391c82b480c0a2e
8c68afbdc09213173aef1be195da7a9a86060a97
refs/heads/master
1,623,004,395,876
1,579,969,507,000
1,579,969,507,000
146,666,368
0
0
null
null
null
null
UTF-8
Lean
false
false
83
lean
universes u v @[reducible] def const {α : Sort u} (x : Sort v) := λ (_ : α), x
9adc4407e9e87992a49a931aff640fffa9cc5308
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/topology/uniform_space/completion.lean
c46e5d2abdbaae0d9f7488f85bba936be128649a
[ "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
23,257
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Hausdorff completions of uniform spaces. The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces into all uniform spaces. Any uniform space `α` gets a completion `completion α` and a morphism (ie. uniformly continuous map) `completion : α → completion α` which solves the universal mapping problem of factorizing morphisms from `α` to any complete Hausdorff uniform space `β`. It means any uniformly continuous `f : α → β` gives rise to a unique morphism `completion.extension f : completion α → β` such that `f = completion.extension f ∘ completion α`. Actually `completion.extension f` is defined for all maps from `α` to `β` but it has the desired properties only if `f` is uniformly continuous. Beware that `completion α` is not injective if `α` is not Hausdorff. But its image is always dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense. For every uniform spaces `α` and `β`, it turns `f : α → β` into a morphism `completion.map f : completion α → completion β` such that `coe ∘ f = (completion.map f) ∘ coe` provided `f` is uniformly continuous. This construction is compatible with composition. In this file we introduce the following concepts: * `Cauchy α` the uniform completion of the uniform space `α` (using Cauchy filters). These are not minimal filters. * `completion α := quotient (separation_setoid (Cauchy α))` the Hausdorff completion. This formalization is mostly based on N. Bourbaki: General Topology I. M. James: Topologies and Uniformities From a slightly different perspective in order to reuse material in topology.uniform_space.basic. -/ import topology.uniform_space.abstract_completion noncomputable theory open filter set universes u v w x open_locale uniformity classical topological_space filter /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] variables {β : Type v} {γ : Type w} variables [uniform_space β] [uniform_space γ] def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ filter.prod (p.1.val) (p.2.val) } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem_sets (α×α) (filter.prod (p.1.val) (p.2.val)) private lemma symm_gen : map prod.swap ((𝓤 α).lift' gen) ≤ (𝓤 α).lift' gen := calc map prod.swap ((𝓤 α).lift' gen) = (𝓤 α).lift' (λs:set (α×α), {p | s ∈ filter.prod (p.2.val) (p.1.val) }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem_sets, function.comp, image_swap_eq_preimage_swap, -subtype.val_eq_coe] end ... ≤ (𝓤 α).lift' gen : uniformity_lift_le_swap (monotone_principal.comp (monotone_set_of $ assume p, @monotone_mem_sets (α×α) ((filter.prod ((p.2).val) ((p.1).val))))) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h, -subtype.val_eq_coe], exact le_refl _ end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val), t₂, (ht₂ : t₂ ∈ h.val), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val), t₄, (ht₄ : t₄ ∈ g.val), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val, from inter_mem_sets ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := nonempty_of_mem_sets (h.property.left) this in (filter.prod f.val g.val).sets_of_superset (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) ≤ (𝓤 α).lift' gen := calc ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) = (𝓤 α).lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ (𝓤 α).lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = ((𝓤 α).lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ (𝓤 α).lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := (𝓤 α).lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, gen t ⊆ s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, ∀ f g : Cauchy α, t ∈ filter.prod f.1 g.1 → (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ λ t h, prod.forall /-- Embedding of `α` into its completion -/ def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : α → Cauchy α) := ⟨have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc comap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓤 α).lift' gen) = (𝓤 α).lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : comap_lift'_eq monotone_gen ... = 𝓤 α : by simp [this]⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := { inj := assume a₁ a₂ h, pure_injective $ subtype.ext_iff_val.1 h, ..uniform_inducing_pure_cauchy } lemma pure_cauchy_dense : ∀x, x ∈ closure (range pure_cauchy) := assume f, have h_ex : ∀ s ∈ 𝓤 (Cauchy α), ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ filter.prod (f.val) (f.val), from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := nonempty_of_mem_sets f.property.left ht in have t'' ∈ filter.prod f.val (pure x), from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, h $ mk_mem_prod hx hx, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp [closure_eq_cluster_pts, cluster_pt, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm], exact (lift'_ne_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ⟨_, this⟩) end lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy := uniform_inducing_pure_cauchy.dense_inducing pure_cauchy_dense lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy := uniform_embedding_pure_cauchy.dense_embedding pure_cauchy_dense lemma nonempty_Cauchy_iff : nonempty (Cauchy α) ↔ nonempty α := begin split ; rintro ⟨c⟩, { have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c, obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial, exact ⟨a⟩ }, { exact ⟨pure_cauchy c⟩ } end section set_option eqn_compiler.zeta true instance : complete_space (Cauchy α) := complete_space_extension uniform_inducing_pure_cauchy pure_cauchy_dense $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ (𝓤 $ Cauchy α).lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (filter.prod f (pure x)).sets_of_superset (prod_mem_prod ht' hx) h, f.sets_of_superset ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end instance [inhabited α] : inhabited (Cauchy α) := ⟨pure_cauchy $ default α⟩ instance [h : nonempty α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a section extend def extend (f : α → β) : (Cauchy α → β) := if uniform_continuous f then dense_inducing_pure_cauchy.extend f else λ x, f (classical.inhabited_of_nonempty $ nonempty_Cauchy_iff.1 ⟨x⟩).default variables [separated_space β] lemma extend_pure_cauchy {f : α → β} (hf : uniform_continuous f) (a : α) : extend f (pure_cauchy a) = f a := begin rw [extend, if_pos hf], exact uniformly_extend_of_ind uniform_inducing_pure_cauchy pure_cauchy_dense hf _ end variables [_root_.complete_space β] lemma uniform_continuous_extend {f : α → β} : uniform_continuous (extend f) := begin by_cases hf : uniform_continuous f, { rw [extend, if_pos hf], exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy pure_cauchy_dense hf }, { rw [extend, if_neg hf], exact uniform_continuous_of_const (assume a b, by congr) } end end extend end theorem Cauchy_eq {α : Type*} [inhabited α] [uniform_space α] [complete_space α] [separated_space α] {f g : Cauchy α} : Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy α) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du), _, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), λ x h, _⟩, cases x with a b, cases h with h₁ h₂, rw ← e at h₂, exact dt ⟨_, h₁, h₂⟩ }, { intros H, refine separated_def.1 (by apply_instance) _ _ (λ t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : ∀ (f : Cauchy α) (x ∈ f.1), Lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_cluster_pts, exact ne_bot_of_le_ne_bot f.2.1 (le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) }, have := dc.closure_subset_iff.2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end section local attribute [instance] uniform_space.separation_setoid lemma separated_pure_cauchy_injective {α : Type*} [uniform_space α] [s : separated_space α] : function.injective (λa:α, ⟦pure_cauchy a⟧) | a b h := separated_def.1 s _ _ $ assume s hs, let ⟨t, ht, hts⟩ := by rw [← (@uniform_embedding_pure_cauchy α _).comap_uniformity, filter.mem_comap_sets] at hs; exact hs in have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht, @hts (a, b) this end end Cauchy local attribute [instance] uniform_space.separation_setoid open Cauchy set namespace uniform_space variables (α : Type*) [uniform_space α] variables {β : Type*} [uniform_space β] variables {γ : Type*} [uniform_space γ] instance complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (f.comap (λx, ⟦x⟧)), from cauchy_comap comap_quotient_le_uniformity hf $ comap_ne_bot_of_surj hf.left $ assume b, quotient.exists_rep _, let ⟨x, (hx : f.comap (λx, ⟦x⟧) ≤ 𝓝 x)⟩ := complete_space.complete this in ⟨⟦x⟧, calc f = map (λx, ⟦x⟧) (f.comap (λx, ⟦x⟧)) : (map_comap $ univ_mem_sets' $ assume b, quotient.exists_rep _).symm ... ≤ map (λx, ⟦x⟧) (𝓝 x) : map_mono hx ... ≤ _ : continuous_iff_continuous_at.mp uniform_continuous_quotient_mk.continuous _⟩⟩ /-- Hausdorff completion of `α` -/ def completion := quotient (separation_setoid $ Cauchy α) namespace completion instance [inhabited α] : inhabited (completion α) := by unfold completion; apply_instance @[priority 50] instance : uniform_space (completion α) := by dunfold completion ; apply_instance instance : complete_space (completion α) := by dunfold completion ; apply_instance instance : separated_space (completion α) := by dunfold completion ; apply_instance instance : regular_space (completion α) := separated_regular /-- Automatic coercion from `α` to its completion. Not always injective. -/ instance : has_coe_t α (completion α) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t] protected lemma coe_eq : (coe : α → completion α) = quotient.mk ∘ pure_cauchy := rfl lemma comap_coe_eq_uniformity : (𝓤 _).comap (λ(p:α×α), ((p.1 : completion α), (p.2 : completion α))) = 𝓤 α := begin have : (λx:α×α, ((x.1 : completion α), (x.2 : completion α))) = (λx:(Cauchy α)×(Cauchy α), (⟦x.1⟧, ⟦x.2⟧)) ∘ (λx:α×α, (pure_cauchy x.1, pure_cauchy x.2)), { ext ⟨a, b⟩; simp; refl }, rw [this, ← filter.comap_comap], change filter.comap _ (filter.comap _ (𝓤 $ quotient $ separation_setoid $ Cauchy α)) = 𝓤 α, rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity] end lemma uniform_inducing_coe : uniform_inducing (coe : α → completion α) := ⟨comap_coe_eq_uniformity α⟩ variables {α} lemma dense : dense_range (coe : α → completion α) := begin rw [dense_range_iff_closure_range, completion.coe_eq, range_comp], exact quotient_dense_of_dense pure_cauchy_dense end variables (α) def cpkg {α : Type*} [uniform_space α] : abstract_completion α := { space := completion α, coe := coe, uniform_struct := by apply_instance, complete := by apply_instance, separation := by apply_instance, uniform_inducing := completion.uniform_inducing_coe α, dense := completion.dense } instance abstract_completion.inhabited : inhabited (abstract_completion α) := ⟨cpkg⟩ local attribute [instance] abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation lemma nonempty_completion_iff : nonempty (completion α) ↔ nonempty α := (dense_range.nonempty (cpkg.dense)).symm lemma uniform_continuous_coe : uniform_continuous (coe : α → completion α) := cpkg.uniform_continuous_coe lemma continuous_coe : continuous (coe : α → completion α) := cpkg.continuous_coe lemma uniform_embedding_coe [separated_space α] : uniform_embedding (coe : α → completion α) := { comap_uniformity := comap_coe_eq_uniformity α, inj := separated_pure_cauchy_injective } variable {α} lemma dense_inducing_coe : dense_inducing (coe : α → completion α) := { dense := dense, ..(uniform_inducing_coe α).inducing } lemma dense_embedding_coe [separated_space α]: dense_embedding (coe : α → completion α) := { inj := separated_pure_cauchy_injective, ..dense_inducing_coe } lemma dense₂ : dense_range (λx:α × β, ((x.1 : completion α), (x.2 : completion β))) := dense.prod dense lemma dense₃ : dense_range (λx:α × (β × γ), ((x.1 : completion α), ((x.2.1 : completion β), (x.2.2 : completion γ)))) := dense.prod dense₂ @[elab_as_eliminator] lemma induction_on {p : completion α → Prop} (a : completion α) (hp : is_closed {a | p a}) (ih : ∀a:α, p a) : p a := is_closed_property dense hp ih a @[elab_as_eliminator] lemma induction_on₂ {p : completion α → completion β → Prop} (a : completion α) (b : completion β) (hp : is_closed {x : completion α × completion β | p x.1 x.2}) (ih : ∀(a:α) (b:β), p a b) : p a b := have ∀x : completion α × completion β, p x.1 x.2, from is_closed_property dense₂ hp $ assume ⟨a, b⟩, ih a b, this (a, b) @[elab_as_eliminator] lemma induction_on₃ {p : completion α → completion β → completion γ → Prop} (a : completion α) (b : completion β) (c : completion γ) (hp : is_closed {x : completion α × completion β × completion γ | p x.1 x.2.1 x.2.2}) (ih : ∀(a:α) (b:β) (c:γ), p a b c) : p a b c := have ∀x : completion α × completion β × completion γ, p x.1 x.2.1 x.2.2, from is_closed_property dense₃ hp $ assume ⟨a, b, c⟩, ih a b c, this (a, b, c) lemma ext [t2_space β] {f g : completion α → β} (hf : continuous f) (hg : continuous g) (h : ∀a:α, f a = g a) : f = g := cpkg.funext hf hg h section extension variables {f : α → β} /-- "Extension" to the completion. It is defined for any map `f` but returns an arbitrary constant value if `f` is not uniformly continuous -/ protected def extension (f : α → β) : completion α → β := cpkg.extend f variables [separated_space β] @[simp] lemma extension_coe (hf : uniform_continuous f) (a : α) : (completion.extension f) a = f a := cpkg.extend_coe hf a variables [complete_space β] lemma uniform_continuous_extension : uniform_continuous (completion.extension f) := cpkg.uniform_continuous_extend lemma continuous_extension : continuous (completion.extension f) := cpkg.continuous_extend lemma extension_unique (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g (a : completion α)) : completion.extension f = g := cpkg.extend_unique hf hg h @[simp] lemma extension_comp_coe {f : completion α → β} (hf : uniform_continuous f) : completion.extension (f ∘ coe) = f := cpkg.extend_comp_coe hf end extension section map variables {f : α → β} /-- Completion functor acting on morphisms -/ protected def map (f : α → β) : completion α → completion β := cpkg.map cpkg f lemma uniform_continuous_map : uniform_continuous (completion.map f) := cpkg.uniform_continuous_map cpkg f lemma continuous_map : continuous (completion.map f) := cpkg.continuous_map cpkg f @[simp] lemma map_coe (hf : uniform_continuous f) (a : α) : (completion.map f) a = f a := cpkg.map_coe cpkg hf a lemma map_unique {f : α → β} {g : completion α → completion β} (hg : uniform_continuous g) (h : ∀a:α, ↑(f a) = g a) : completion.map f = g := cpkg.map_unique cpkg hg h @[simp] lemma map_id : completion.map (@id α) = id := cpkg.map_id lemma extension_map [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β} (hf : uniform_continuous f) (hg : uniform_continuous g) : completion.extension f ∘ completion.map g = completion.extension (f ∘ g) := completion.ext (continuous_extension.comp continuous_map) continuous_extension $ by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe] lemma map_comp {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : completion.map g ∘ completion.map f = completion.map (g ∘ f) := extension_map ((uniform_continuous_coe _).comp hg) hf end map /- In this section we construct isomorphisms between the completion of a uniform space and the completion of its separation quotient -/ section separation_quotient_completion def completion_separation_quotient_equiv (α : Type u) [uniform_space α] : completion (separation_quotient α) ≃ completion α := begin refine ⟨completion.extension (separation_quotient.lift (coe : α → completion α)), completion.map quotient.mk, _, _⟩, { assume a, refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _, rintros ⟨a⟩, show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) ↑⟦a⟧) = ↑⟦a⟧, rw [extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α), completion.map_coe uniform_continuous_quotient_mk] ; apply_instance }, { assume a, refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) _, assume a, rw [map_coe uniform_continuous_quotient_mk, extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α) _] ; apply_instance } end lemma uniform_continuous_completion_separation_quotient_equiv : uniform_continuous ⇑(completion_separation_quotient_equiv α) := uniform_continuous_extension lemma uniform_continuous_completion_separation_quotient_equiv_symm : uniform_continuous ⇑(completion_separation_quotient_equiv α).symm := uniform_continuous_map end separation_quotient_completion section extension₂ variables (f : α → β → γ) open function protected def extension₂ (f : α → β → γ) : completion α → completion β → γ := cpkg.extend₂ cpkg f variables [separated_space γ] {f} @[simp] lemma extension₂_coe_coe (hf : uniform_continuous₂ f) (a : α) (b : β) : completion.extension₂ f a b = f a b := cpkg.extension₂_coe_coe cpkg hf a b variables [complete_space γ] (f) lemma uniform_continuous_extension₂ : uniform_continuous₂ (completion.extension₂ f) := cpkg.uniform_continuous_extension₂ cpkg f end extension₂ section map₂ open function protected def map₂ (f : α → β → γ) : completion α → completion β → completion γ := cpkg.map₂ cpkg cpkg f lemma uniform_continuous_map₂ (f : α → β → γ) : uniform_continuous₂ (completion.map₂ f) := cpkg.uniform_continuous_map₂ cpkg cpkg f lemma continuous_map₂ {δ} [topological_space δ] {f : α → β → γ} {a : δ → completion α} {b : δ → completion β} (ha : continuous a) (hb : continuous b) : continuous (λd:δ, completion.map₂ f (a d) (b d)) := cpkg.continuous_map₂ cpkg cpkg ha hb lemma map₂_coe_coe (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) : completion.map₂ f (a : completion α) (b : completion β) = f a b := cpkg.map₂_coe_coe cpkg cpkg a b f hf end map₂ end completion end uniform_space
c56a1a5708c2562ce26bf8ec074c94cfff08b90c
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/general_linear_group.lean
c06fd53a381f8154e3f2d68a7d69c81f005c4d0c
[ "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
8,497
lean
/- Copyright (c) 2021 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import linear_algebra.matrix.nonsingular_inverse import linear_algebra.special_linear_group /-! # The General Linear group $GL(n, R)$ This file defines the elements of the General Linear group `general_linear_group n R`, consisting of all invertible `n` by `n` `R`-matrices. ## Main definitions * `matrix.general_linear_group` is the type of matrices over R which are units in the matrix ring. * `matrix.GL_pos` gives the subgroup of matrices with positive determinant (over a linear ordered ring). ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map -- disable this instance so we do not accidentally use it in lemmas. local attribute [-instance] special_linear_group.has_coe_to_fun /-- `GL n R` is the group of `n` by `n` `R`-matrices with unit determinant. Defined as a subtype of matrices-/ abbreviation general_linear_group (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_ring R] : Type* := (matrix n n R)ˣ notation `GL` := general_linear_group namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] /-- The determinant of a unit matrix is itself a unit. -/ @[simps] def det : GL n R →* Rˣ := { to_fun := λ A, { val := (↑A : matrix n n R).det, inv := (↑(A⁻¹) : matrix n n R).det, val_inv := by rw [←det_mul, ←mul_eq_mul, A.mul_inv, det_one], inv_val := by rw [←det_mul, ←mul_eq_mul, A.inv_mul, det_one]}, map_one' := units.ext det_one, map_mul' := λ A B, units.ext $ det_mul _ _ } /--The `GL n R` and `general_linear_group R n` groups are multiplicatively equivalent-/ def to_lin : (GL n R) ≃* (linear_map.general_linear_group R (n → R)) := units.map_equiv to_lin_alg_equiv'.to_mul_equiv /--Given a matrix with invertible determinant we get an element of `GL n R`-/ def mk' (A : matrix n n R) (h : invertible (matrix.det A)) : GL n R := unit_of_det_invertible A /--Given a matrix with unit determinant we get an element of `GL n R`-/ noncomputable def mk'' (A : matrix n n R) (h : is_unit (matrix.det A)) : GL n R := nonsing_inv_unit A h /--Given a matrix with non-zero determinant over a field, we get an element of `GL n K`-/ def mk_of_det_ne_zero {K : Type*} [field K] (A : matrix n n K) (h : matrix.det A ≠ 0) : GL n K := mk' A (invertible_of_nonzero h) lemma ext_iff (A B : GL n R) : A = B ↔ (∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) := units.ext_iff.trans matrix.ext_iff.symm /-- Not marked `@[ext]` as the `ext` tactic already solves this. -/ lemma ext ⦃A B : GL n R⦄ (h : ∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) : A = B := units.ext $ matrix.ext h section coe_lemmas variables (A B : GL n R) @[simp] lemma coe_mul : ↑(A * B) = (↑A : matrix n n R) ⬝ (↑B : matrix n n R) := rfl @[simp] lemma coe_one : ↑(1 : GL n R) = (1 : matrix n n R) := rfl lemma coe_inv : ↑(A⁻¹) = (↑A : matrix n n R)⁻¹ := begin letI := A.invertible, exact inv_of_eq_nonsing_inv (↑A : matrix n n R), end /-- An element of the matrix general linear group on `(n) [fintype n]` can be considered as an element of the endomorphism general linear group on `n → R`. -/ def to_linear : general_linear_group n R ≃* linear_map.general_linear_group R (n → R) := units.map_equiv matrix.to_lin_alg_equiv'.to_ring_equiv.to_mul_equiv -- Note that without the `@` and `‹_›`, lean infers `λ a b, _inst_1 a b` instead of `_inst_1` as the -- decidability argument, which prevents `simp` from obtaining the instance by unification. -- These `λ a b, _inst a b` terms also appear in the type of `A`, but simp doesn't get confused by -- them so for now we do not care. @[simp] lemma coe_to_linear : (@to_linear n ‹_› ‹_› _ _ A : (n → R) →ₗ[R] (n → R)) = matrix.mul_vec_lin A := rfl @[simp] lemma to_linear_apply (v : n → R) : (@to_linear n ‹_› ‹_› _ _ A) v = matrix.mul_vec_lin ↑A v := rfl end coe_lemmas end general_linear_group namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_general_linear_group : has_coe (special_linear_group n R) (GL n R) := ⟨λ A, ⟨↑A, ↑(A⁻¹), congr_arg coe (mul_right_inv A), congr_arg coe (mul_left_inv A)⟩⟩ @[simp] lemma coe_to_GL_det (g : special_linear_group n R) : (g : GL n R).det = 1 := units.ext g.prop end special_linear_group section variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] section variables (n R) /-- This is the subgroup of `nxn` matrices with entries over a linear ordered ring and positive determinant. -/ def GL_pos : subgroup (GL n R) := (units.pos_subgroup R).comap general_linear_group.det end @[simp] lemma mem_GL_pos (A : GL n R) : A ∈ GL_pos n R ↔ 0 < (A.det : R) := iff.rfl end section has_neg variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] [fact (even (fintype.card n))] /-- Formal operation of negation on general linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (GL_pos n R) := ⟨λ g, ⟨-g, begin rw [mem_GL_pos, general_linear_group.coe_det_apply, units.coe_neg, det_neg, (fact.out $ even $ fintype.card n).neg_one_pow, one_mul], exact g.prop, end⟩⟩ instance : has_distrib_neg (GL_pos n R) := { neg := has_neg.neg, neg_neg := λ x, subtype.ext $ neg_neg _, neg_mul := λ x y, subtype.ext $ neg_mul _ _, mul_neg := λ x y, subtype.ext $ mul_neg _ _ } @[simp] lemma GL_pos.coe_neg (g : GL_pos n R) : ↑(- g) = - (↑g : matrix n n R) := rfl @[simp] lemma GL_pos.coe_neg_apply (g : GL_pos n R) (i j : n) : (↑(-g) : matrix n n R) i j = -((↑g : matrix n n R) i j) := rfl end has_neg namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [linear_ordered_comm_ring R] /-- `special_linear_group n R` embeds into `GL_pos n R` -/ def to_GL_pos : special_linear_group n R →* GL_pos n R := { to_fun := λ A, ⟨(A : GL n R), show 0 < (↑A : matrix n n R).det, from A.prop.symm ▸ zero_lt_one⟩, map_one' := subtype.ext $ units.ext $ rfl, map_mul' := λ A₁ A₂, subtype.ext $ units.ext $ rfl } instance : has_coe (special_linear_group n R) (GL_pos n R) := ⟨to_GL_pos⟩ lemma coe_eq_to_GL_pos : (coe : special_linear_group n R → GL_pos n R) = to_GL_pos := rfl lemma to_GL_pos_injective : function.injective (to_GL_pos : special_linear_group n R → GL_pos n R) := (show function.injective ((coe : GL_pos n R → matrix n n R) ∘ to_GL_pos), from subtype.coe_injective).of_comp /-- Coercing a `special_linear_group` via `GL_pos` and `GL` is the same as coercing striaght to a matrix. -/ @[simp] lemma coe_GL_pos_coe_GL_coe_matrix (g : special_linear_group n R) : (↑(↑(↑g : GL_pos n R) : GL n R) : matrix n n R) = ↑g := rfl @[simp] lemma coe_to_GL_pos_to_GL_det (g : special_linear_group n R) : ((g : GL_pos n R) : GL n R).det = 1 := units.ext g.prop variable [fact (even (fintype.card n))] @[norm_cast] lemma coe_GL_pos_neg (g : special_linear_group n R) : ↑(-g) = -(↑g : GL_pos n R) := subtype.ext $ units.ext rfl end special_linear_group section examples /-- The matrix [a, -b; b, a] (inspired by multiplication by a complex number); it is an element of $GL_2(R)$ if `a ^ 2 + b ^ 2` is nonzero. -/ @[simps coe {fully_applied := ff}] def plane_conformal_matrix {R} [field R] (a b : R) (hab : a ^ 2 + b ^ 2 ≠ 0) : matrix.general_linear_group (fin 2) R := general_linear_group.mk_of_det_ne_zero ![![a, -b], ![b, a]] (by simpa [det_fin_two, sq] using hab) /- TODO: Add Iwasawa matrices `n_x=![![1,x],![0,1]]`, `a_t=![![exp(t/2),0],![0,exp(-t/2)]]` and `k_θ==![![cos θ, sin θ],![-sin θ, cos θ]]` -/ end examples namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] -- this section should be last to ensure we do not use it in lemmas section coe_fn_instance /-- This instance is here for convenience, but is not the simp-normal form. -/ instance : has_coe_to_fun (GL n R) (λ _, n → n → R) := { coe := λ A, A.val } @[simp] lemma coe_fn_eq_coe (A : GL n R) : ⇑A = (↑A : matrix n n R) := rfl end coe_fn_instance end general_linear_group end matrix
a470bc4734977431e09b5e5038f88d0ea0dc98ee
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1237.lean
e41798c960b398787f04f5c43c190e63d2b5e932
[ "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
633
lean
inductive Tree (α : Type) : Type where | leaf : Tree α | node (t : Tree α) (f : Tree α) : Tree α inductive Subtree : Tree α → Tree α → Prop where | left_head (t f : Tree α) : Subtree t (Tree.node t f) | right_head (t f : Tree α) : Subtree f (Tree.node t f) def dec_subtree (n m : Tree α) : Decidable (Subtree n m) := match n, m with | .leaf, .node t f => let ht := dec_subtree .leaf t let hf := dec_subtree .leaf f match ht, hf with | .isFalse ht, .isFalse hf => .isFalse (fun h => match h with | .left_head _ _ => sorry | _ => sorry ) | _, _ => sorry | _, _ => sorry
c24eba603046216fd7a460410c4bcab197073ba4
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group/hom.lean
83d20128734b53fe5b94d583695571e21fdfb112
[ "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
48,054
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov -/ import algebra.group.commute import algebra.group_with_zero.defs import data.fun_like.basic /-! # Monoid and group homomorphisms This file defines the bundled structures for monoid and group homomorphisms. Namely, we define `monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp., additive) monoids or groups. We also define coercion to a function, and usual operations: composition, identity homomorphism, pointwise multiplication and pointwise inversion. This file also defines the lesser-used (and notation-less) homomorphism types which are used as building blocks for other homomorphisms: * `zero_hom` * `one_hom` * `add_hom` * `mul_hom` * `monoid_with_zero_hom` ## Notations * `→+`: Bundled `add_monoid` homs. Also use for `add_group` homs. * `→*`: Bundled `monoid` homs. Also use for `group` homs. * `→*₀`: Bundled `monoid_with_zero` homs. Also use for `group_with_zero` homs. ## Implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. Historically this file also included definitions of unbundled homomorphism classes; they were deprecated and moved to `deprecated/group`. ## Tags monoid_hom, add_monoid_hom -/ variables {M : Type*} {N : Type*} {P : Type*} -- monoids variables {G : Type*} {H : Type*} -- groups variables {F : Type*} -- homs -- for easy multiple inheritance set_option old_structure_cmd true section zero /-- `zero_hom M N` is the type of functions `M → N` that preserve zero. When possible, instead of parametrizing results over `(f : zero_hom M N)`, you should parametrize over `(F : Type*) [zero_hom_class F M N] (f : F)`. When you extend this structure, make sure to also extend `zero_hom_class`. -/ structure zero_hom (M : Type*) (N : Type*) [has_zero M] [has_zero N] := (to_fun : M → N) (map_zero' : to_fun 0 = 0) /-- `zero_hom_class F M N` states that `F` is a type of zero-preserving homomorphisms. You should extend this typeclass when you extend `zero_hom`. -/ class zero_hom_class (F : Type*) (M N : out_param $ Type*) [has_zero M] [has_zero N] extends fun_like F M (λ _, N) := (map_zero : ∀ (f : F), f 0 = 0) -- Instances and lemmas are defined below through `@[to_additive]`. end zero section add /-- `add_hom M N` is the type of functions `M → N` that preserve addition. When possible, instead of parametrizing results over `(f : add_hom M N)`, you should parametrize over `(F : Type*) [add_hom_class F M N] (f : F)`. When you extend this structure, make sure to extend `add_hom_class`. -/ structure add_hom (M : Type*) (N : Type*) [has_add M] [has_add N] := (to_fun : M → N) (map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y) /-- `add_hom_class F M N` states that `F` is a type of addition-preserving homomorphisms. You should declare an instance of this typeclass when you extend `add_hom`. -/ class add_hom_class (F : Type*) (M N : out_param $ Type*) [has_add M] [has_add N] extends fun_like F M (λ _, N) := (map_add : ∀ (f : F) (x y : M), f (x + y) = f x + f y) -- Instances and lemmas are defined below through `@[to_additive]`. end add section add_zero /-- `M →+ N` is the type of functions `M → N` that preserve the `add_zero_class` structure. `add_monoid_hom` is also used for group homomorphisms. When possible, instead of parametrizing results over `(f : M →+ N)`, you should parametrize over `(F : Type*) [add_monoid_hom_class F M N] (f : F)`. When you extend this structure, make sure to extend `add_monoid_hom_class`. -/ @[ancestor zero_hom add_hom] structure add_monoid_hom (M : Type*) (N : Type*) [add_zero_class M] [add_zero_class N] extends zero_hom M N, add_hom M N attribute [nolint doc_blame] add_monoid_hom.to_add_hom attribute [nolint doc_blame] add_monoid_hom.to_zero_hom infixr ` →+ `:25 := add_monoid_hom /-- `add_monoid_hom_class F M N` states that `F` is a type of `add_zero_class`-preserving homomorphisms. You should also extend this typeclass when you extend `add_monoid_hom`. -/ @[ancestor add_hom_class zero_hom_class] class add_monoid_hom_class (F : Type*) (M N : out_param $ Type*) [add_zero_class M] [add_zero_class N] extends add_hom_class F M N, zero_hom_class F M N -- Instances and lemmas are defined below through `@[to_additive]`. end add_zero section one variables [has_one M] [has_one N] /-- `one_hom M N` is the type of functions `M → N` that preserve one. When possible, instead of parametrizing results over `(f : one_hom M N)`, you should parametrize over `(F : Type*) [one_hom_class F M N] (f : F)`. When you extend this structure, make sure to also extend `one_hom_class`. -/ @[to_additive] structure one_hom (M : Type*) (N : Type*) [has_one M] [has_one N] := (to_fun : M → N) (map_one' : to_fun 1 = 1) /-- `one_hom_class F M N` states that `F` is a type of one-preserving homomorphisms. You should extend this typeclass when you extend `one_hom`. -/ @[to_additive] class one_hom_class (F : Type*) (M N : out_param $ Type*) [has_one M] [has_one N] extends fun_like F M (λ _, N) := (map_one : ∀ (f : F), f 1 = 1) @[to_additive] instance one_hom.one_hom_class : one_hom_class (one_hom M N) M N := { coe := one_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_one := one_hom.map_one' } @[simp, to_additive] lemma map_one [one_hom_class F M N] (f : F) : f 1 = 1 := one_hom_class.map_one f @[to_additive] lemma map_eq_one_iff [one_hom_class F M N] (f : F) (hf : function.injective f) {x : M} : f x = 1 ↔ x = 1 := hf.eq_iff' (map_one f) @[to_additive] instance [one_hom_class F M N] : has_coe_t F (one_hom M N) := ⟨λ f, { to_fun := f, map_one' := map_one f }⟩ end one section mul variables [has_mul M] [has_mul N] /-- `mul_hom M N` is the type of functions `M → N` that preserve multiplication. When possible, instead of parametrizing results over `(f : mul_hom M N)`, you should parametrize over `(F : Type*) [mul_hom_class F M N] (f : F)`. When you extend this structure, make sure to extend `mul_hom_class`. -/ @[to_additive] structure mul_hom (M : Type*) (N : Type*) [has_mul M] [has_mul N] := (to_fun : M → N) (map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y) /-- `mul_hom_class F M N` states that `F` is a type of multiplication-preserving homomorphisms. You should declare an instance of this typeclass when you extend `mul_hom`. -/ @[to_additive] class mul_hom_class (F : Type*) (M N : out_param $ Type*) [has_mul M] [has_mul N] extends fun_like F M (λ _, N) := (map_mul : ∀ (f : F) (x y : M), f (x * y) = f x * f y) @[to_additive] instance mul_hom.mul_hom_class : mul_hom_class (mul_hom M N) M N := { coe := mul_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_mul := mul_hom.map_mul' } @[simp, to_additive] lemma map_mul [mul_hom_class F M N] (f : F) (x y : M) : f (x * y) = f x * f y := mul_hom_class.map_mul f x y @[to_additive] instance [mul_hom_class F M N] : has_coe_t F (mul_hom M N) := ⟨λ f, { to_fun := f, map_mul' := map_mul f }⟩ end mul section mul_one variables [mul_one_class M] [mul_one_class N] /-- `M →* N` is the type of functions `M → N` that preserve the `monoid` structure. `monoid_hom` is also used for group homomorphisms. When possible, instead of parametrizing results over `(f : M →+ N)`, you should parametrize over `(F : Type*) [monoid_hom_class F M N] (f : F)`. When you extend this structure, make sure to extend `monoid_hom_class`. -/ @[ancestor one_hom mul_hom, to_additive] structure monoid_hom (M : Type*) (N : Type*) [mul_one_class M] [mul_one_class N] extends one_hom M N, mul_hom M N attribute [nolint doc_blame] monoid_hom.to_mul_hom attribute [nolint doc_blame] monoid_hom.to_one_hom infixr ` →* `:25 := monoid_hom /-- `monoid_hom_class F M N` states that `F` is a type of `monoid`-preserving homomorphisms. You should also extend this typeclass when you extend `monoid_hom`. -/ @[ancestor mul_hom_class one_hom_class, to_additive] class monoid_hom_class (F : Type*) (M N : out_param $ Type*) [mul_one_class M] [mul_one_class N] extends mul_hom_class F M N, one_hom_class F M N @[to_additive] instance monoid_hom.monoid_hom_class : monoid_hom_class (M →* N) M N := { coe := monoid_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_mul := monoid_hom.map_mul', map_one := monoid_hom.map_one' } @[to_additive] instance [monoid_hom_class F M N] : has_coe_t F (M →* N) := ⟨λ f, { to_fun := f, map_one' := map_one f, map_mul' := map_mul f }⟩ @[to_additive] lemma map_mul_eq_one [monoid_hom_class F M N] (f : F) {a b : M} (h : a * b = 1) : f a * f b = 1 := by rw [← map_mul, h, map_one] /-- Group homomorphisms preserve inverse. -/ @[simp, to_additive] theorem map_inv [group G] [group H] [monoid_hom_class F G H] (f : F) (g : G) : f g⁻¹ = (f g)⁻¹ := eq_inv_of_mul_eq_one $ map_mul_eq_one f $ inv_mul_self g /-- Group homomorphisms preserve division. -/ @[simp, to_additive] theorem map_mul_inv [group G] [group H] [monoid_hom_class F G H] (f : F) (g h : G) : f (g * h⁻¹) = f g * (f h)⁻¹ := by rw [map_mul, map_inv] /-- Group homomorphisms preserve division. -/ @[simp, to_additive] lemma map_div [group G] [group H] [monoid_hom_class F G H] (f : F) (x y : G) : f (x / y) = f x / f y := by rw [div_eq_mul_inv, div_eq_mul_inv, map_mul_inv] @[simp, to_additive map_nsmul] theorem map_pow [monoid G] [monoid H] [monoid_hom_class F G H] (f : F) (a : G) : ∀ (n : ℕ), f (a ^ n) = (f a) ^ n | 0 := by rw [pow_zero, pow_zero, map_one] | (n+1) := by rw [pow_succ, pow_succ, map_mul, map_pow] @[to_additive] theorem map_zpow' [div_inv_monoid G] [div_inv_monoid H] [monoid_hom_class F G H] (f : F) (hf : ∀ (x : G), f (x⁻¹) = (f x)⁻¹) (a : G) : ∀ n : ℤ, f (a ^ n) = (f a) ^ n | (n : ℕ) := by rw [zpow_coe_nat, map_pow, zpow_coe_nat] | -[1+n] := by rw [zpow_neg_succ_of_nat, hf, map_pow, ← zpow_neg_succ_of_nat] /-- Group homomorphisms preserve integer power. -/ @[simp, to_additive /-" Additive group homomorphisms preserve integer scaling. "-/] theorem map_zpow [group G] [group H] [monoid_hom_class F G H] (f : F) (g : G) (n : ℤ) : f (g ^ n) = (f g) ^ n := map_zpow' f (map_inv f) g n end mul_one section mul_zero_one variables [mul_zero_one_class M] [mul_zero_one_class N] /-- `M →*₀ N` is the type of functions `M → N` that preserve the `monoid_with_zero` structure. `monoid_with_zero_hom` is also used for group homomorphisms. When possible, instead of parametrizing results over `(f : M →+ N)`, you should parametrize over `(F : Type*) [monoid_with_zero_hom_class F M N] (f : F)`. When you extend this structure, make sure to extend `monoid_with_zero_hom_class`. -/ @[ancestor zero_hom monoid_hom] structure monoid_with_zero_hom (M : Type*) (N : Type*) [mul_zero_one_class M] [mul_zero_one_class N] extends zero_hom M N, monoid_hom M N attribute [nolint doc_blame] monoid_with_zero_hom.to_monoid_hom attribute [nolint doc_blame] monoid_with_zero_hom.to_zero_hom infixr ` →*₀ `:25 := monoid_with_zero_hom /-- `monoid_with_zero_hom_class F M N` states that `F` is a type of `monoid_with_zero`-preserving homomorphisms. You should also extend this typeclass when you extend `monoid_with_zero_hom`. -/ class monoid_with_zero_hom_class (F : Type*) (M N : out_param $ Type*) [mul_zero_one_class M] [mul_zero_one_class N] extends monoid_hom_class F M N, zero_hom_class F M N instance monoid_with_zero_hom.monoid_with_zero_hom_class : monoid_with_zero_hom_class (M →*₀ N) M N := { coe := monoid_with_zero_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_mul := monoid_with_zero_hom.map_mul', map_one := monoid_with_zero_hom.map_one', map_zero := monoid_with_zero_hom.map_zero' } instance [monoid_with_zero_hom_class F M N] : has_coe_t F (M →*₀ N) := ⟨λ f, { to_fun := f, map_one' := map_one f, map_zero' := map_zero f, map_mul' := map_mul f }⟩ end mul_zero_one -- completely uninteresting lemmas about coercion to function, that all homs need section coes /-! Bundled morphisms can be down-cast to weaker bundlings -/ @[to_additive] instance monoid_hom.has_coe_to_one_hom {mM : mul_one_class M} {mN : mul_one_class N} : has_coe (M →* N) (one_hom M N) := ⟨monoid_hom.to_one_hom⟩ @[to_additive] instance monoid_hom.has_coe_to_mul_hom {mM : mul_one_class M} {mN : mul_one_class N} : has_coe (M →* N) (mul_hom M N) := ⟨monoid_hom.to_mul_hom⟩ instance monoid_with_zero_hom.has_coe_to_monoid_hom {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} : has_coe (M →*₀ N) (M →* N) := ⟨monoid_with_zero_hom.to_monoid_hom⟩ instance monoid_with_zero_hom.has_coe_to_zero_hom {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} : has_coe (M →*₀ N) (zero_hom M N) := ⟨monoid_with_zero_hom.to_zero_hom⟩ /-! The simp-normal form of morphism coercion is `f.to_..._hom`. This choice is primarily because this is the way things were before the above coercions were introduced. Bundled morphisms defined elsewhere in Mathlib may choose `↑f` as their simp-normal form instead. -/ @[simp, to_additive] lemma monoid_hom.coe_eq_to_one_hom {mM : mul_one_class M} {mN : mul_one_class N} (f : M →* N) : (f : one_hom M N) = f.to_one_hom := rfl @[simp, to_additive] lemma monoid_hom.coe_eq_to_mul_hom {mM : mul_one_class M} {mN : mul_one_class N} (f : M →* N) : (f : mul_hom M N) = f.to_mul_hom := rfl @[simp] lemma monoid_with_zero_hom.coe_eq_to_monoid_hom {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} (f : M →*₀ N) : (f : M →* N) = f.to_monoid_hom := rfl @[simp] lemma monoid_with_zero_hom.coe_eq_to_zero_hom {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} (f : M →*₀ N) : (f : zero_hom M N) = f.to_zero_hom := rfl -- Fallback `has_coe_to_fun` instances to help the elaborator @[to_additive] instance {mM : has_one M} {mN : has_one N} : has_coe_to_fun (one_hom M N) (λ _, M → N) := ⟨one_hom.to_fun⟩ @[to_additive] instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (mul_hom M N) (λ _, M → N) := ⟨mul_hom.to_fun⟩ @[to_additive] instance {mM : mul_one_class M} {mN : mul_one_class N} : has_coe_to_fun (M →* N) (λ _, M → N) := ⟨monoid_hom.to_fun⟩ instance {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} : has_coe_to_fun (M →*₀ N) (λ _, M → N) := ⟨monoid_with_zero_hom.to_fun⟩ -- these must come after the coe_to_fun definitions initialize_simps_projections zero_hom (to_fun → apply) initialize_simps_projections add_hom (to_fun → apply) initialize_simps_projections add_monoid_hom (to_fun → apply) initialize_simps_projections one_hom (to_fun → apply) initialize_simps_projections mul_hom (to_fun → apply) initialize_simps_projections monoid_hom (to_fun → apply) initialize_simps_projections monoid_with_zero_hom (to_fun → apply) @[simp, to_additive] lemma one_hom.to_fun_eq_coe [has_one M] [has_one N] (f : one_hom M N) : f.to_fun = f := rfl @[simp, to_additive] lemma mul_hom.to_fun_eq_coe [has_mul M] [has_mul N] (f : mul_hom M N) : f.to_fun = f := rfl @[simp, to_additive] lemma monoid_hom.to_fun_eq_coe [mul_one_class M] [mul_one_class N] (f : M →* N) : f.to_fun = f := rfl @[simp] lemma monoid_with_zero_hom.to_fun_eq_coe [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : f.to_fun = f := rfl @[simp, to_additive] lemma one_hom.coe_mk [has_one M] [has_one N] (f : M → N) (h1) : (one_hom.mk f h1 : M → N) = f := rfl @[simp, to_additive] lemma mul_hom.coe_mk [has_mul M] [has_mul N] (f : M → N) (hmul) : (mul_hom.mk f hmul : M → N) = f := rfl @[simp, to_additive] lemma monoid_hom.coe_mk [mul_one_class M] [mul_one_class N] (f : M → N) (h1 hmul) : (monoid_hom.mk f h1 hmul : M → N) = f := rfl @[simp] lemma monoid_with_zero_hom.coe_mk [mul_zero_one_class M] [mul_zero_one_class N] (f : M → N) (h0 h1 hmul) : (monoid_with_zero_hom.mk f h0 h1 hmul : M → N) = f := rfl @[simp, to_additive] lemma monoid_hom.to_one_hom_coe [mul_one_class M] [mul_one_class N] (f : M →* N) : (f.to_one_hom : M → N) = f := rfl @[simp, to_additive] lemma monoid_hom.to_mul_hom_coe [mul_one_class M] [mul_one_class N] (f : M →* N) : (f.to_mul_hom : M → N) = f := rfl @[simp] lemma monoid_with_zero_hom.to_zero_hom_coe [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : (f.to_zero_hom : M → N) = f := rfl @[simp] lemma monoid_with_zero_hom.to_monoid_hom_coe [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : (f.to_monoid_hom : M → N) = f := rfl @[to_additive] theorem one_hom.congr_fun [has_one M] [has_one N] {f g : one_hom M N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : one_hom M N, h x) h @[to_additive] theorem mul_hom.congr_fun [has_mul M] [has_mul N] {f g : mul_hom M N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : mul_hom M N, h x) h @[to_additive] theorem monoid_hom.congr_fun [mul_one_class M] [mul_one_class N] {f g : M →* N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : M →* N, h x) h theorem monoid_with_zero_hom.congr_fun [mul_zero_one_class M] [mul_zero_one_class N] {f g : M →*₀ N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : M →*₀ N, h x) h @[to_additive] theorem one_hom.congr_arg [has_one M] [has_one N] (f : one_hom M N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] theorem mul_hom.congr_arg [has_mul M] [has_mul N] (f : mul_hom M N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] theorem monoid_hom.congr_arg [mul_one_class M] [mul_one_class N] (f : M →* N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h theorem monoid_with_zero_hom.congr_arg [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] lemma one_hom.coe_inj [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[to_additive] lemma mul_hom.coe_inj [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[to_additive] lemma monoid_hom.coe_inj [mul_one_class M] [mul_one_class N] ⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl lemma monoid_with_zero_hom.coe_inj [mul_zero_one_class M] [mul_zero_one_class N] ⦃f g : M →*₀ N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[ext, to_additive] lemma one_hom.ext [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : ∀ x, f x = g x) : f = g := one_hom.coe_inj (funext h) @[ext, to_additive] lemma mul_hom.ext [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : ∀ x, f x = g x) : f = g := mul_hom.coe_inj (funext h) @[ext, to_additive] lemma monoid_hom.ext [mul_one_class M] [mul_one_class N] ⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g := monoid_hom.coe_inj (funext h) @[ext] lemma monoid_with_zero_hom.ext [mul_zero_one_class M] [mul_zero_one_class N] ⦃f g : M →*₀ N⦄ (h : ∀ x, f x = g x) : f = g := monoid_with_zero_hom.coe_inj (funext h) @[to_additive] lemma one_hom.ext_iff [has_one M] [has_one N] {f g : one_hom M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, one_hom.ext h⟩ @[to_additive] lemma mul_hom.ext_iff [has_mul M] [has_mul N] {f g : mul_hom M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, mul_hom.ext h⟩ @[to_additive] lemma monoid_hom.ext_iff [mul_one_class M] [mul_one_class N] {f g : M →* N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, monoid_hom.ext h⟩ lemma monoid_with_zero_hom.ext_iff [mul_zero_one_class M] [mul_zero_one_class N] {f g : M →*₀ N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, monoid_with_zero_hom.ext h⟩ @[simp, to_additive] lemma one_hom.mk_coe [has_one M] [has_one N] (f : one_hom M N) (h1) : one_hom.mk f h1 = f := one_hom.ext $ λ _, rfl @[simp, to_additive] lemma mul_hom.mk_coe [has_mul M] [has_mul N] (f : mul_hom M N) (hmul) : mul_hom.mk f hmul = f := mul_hom.ext $ λ _, rfl @[simp, to_additive] lemma monoid_hom.mk_coe [mul_one_class M] [mul_one_class N] (f : M →* N) (h1 hmul) : monoid_hom.mk f h1 hmul = f := monoid_hom.ext $ λ _, rfl @[simp] lemma monoid_with_zero_hom.mk_coe [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) (h0 h1 hmul) : monoid_with_zero_hom.mk f h0 h1 hmul = f := monoid_with_zero_hom.ext $ λ _, rfl end coes /-- Copy of a `one_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ @[to_additive "Copy of a `zero_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities."] protected def one_hom.copy {hM : has_one M} {hN : has_one N} (f : one_hom M N) (f' : M → N) (h : f' = f) : one_hom M N := { to_fun := f', map_one' := h.symm ▸ f.map_one' } /-- Copy of a `mul_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ @[to_additive "Copy of an `add_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities."] protected def mul_hom.copy {hM : has_mul M} {hN : has_mul N} (f : mul_hom M N) (f' : M → N) (h : f' = f) : mul_hom M N := { to_fun := f', map_mul' := h.symm ▸ f.map_mul' } /-- Copy of a `monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ @[to_additive "Copy of an `add_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities."] protected def monoid_hom.copy {hM : mul_one_class M} {hN : mul_one_class N} (f : M →* N) (f' : M → N) (h : f' = f) : M →* N := { ..f.to_one_hom.copy f' h, ..f.to_mul_hom.copy f' h } /-- Copy of a `monoid_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def monoid_with_zero_hom.copy {hM : mul_zero_one_class M} {hN : mul_zero_one_class N} (f : M →*₀ N) (f' : M → N) (h : f' = f) : M →* N := { ..f.to_zero_hom.copy f' h, ..f.to_monoid_hom.copy f' h } @[to_additive] protected lemma one_hom.map_one [has_one M] [has_one N] (f : one_hom M N) : f 1 = 1 := f.map_one' /-- If `f` is a monoid homomorphism then `f 1 = 1`. -/ @[to_additive] protected lemma monoid_hom.map_one [mul_one_class M] [mul_one_class N] (f : M →* N) : f 1 = 1 := f.map_one' protected lemma monoid_with_zero_hom.map_one [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : f 1 = 1 := f.map_one' /-- If `f` is an additive monoid homomorphism then `f 0 = 0`. -/ add_decl_doc add_monoid_hom.map_zero protected lemma monoid_with_zero_hom.map_zero [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : f 0 = 0 := f.map_zero' @[to_additive] protected lemma mul_hom.map_mul [has_mul M] [has_mul N] (f : mul_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b /-- If `f` is a monoid homomorphism then `f (a * b) = f a * f b`. -/ @[to_additive] protected lemma monoid_hom.map_mul [mul_one_class M] [mul_one_class N] (f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b protected lemma monoid_with_zero_hom.map_mul [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b /-- If `f` is an additive monoid homomorphism then `f (a + b) = f a + f b`. -/ add_decl_doc add_monoid_hom.map_add namespace monoid_hom variables {mM : mul_one_class M} {mN : mul_one_class N} {mP : mul_one_class P} variables [group G] [comm_group H] include mM mN @[to_additive] lemma map_mul_eq_one (f : M →* N) {a b : M} (h : a * b = 1) : f a * f b = 1 := map_mul_eq_one f h /-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too."] lemma map_exists_right_inv (f : M →* N) {x : M} (hx : ∃ y, x * y = 1) : ∃ y, f x * y = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ /-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_add_unit.map`."] lemma map_exists_left_inv (f : M →* N) {x : M} (hx : ∃ y, y * x = 1) : ∃ y, y * f x = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ end monoid_hom /-- Inversion on a commutative group, considered as a monoid homomorphism. -/ @[to_additive "Inversion on a commutative additive group, considered as an additive monoid homomorphism."] def comm_group.inv_monoid_hom {G : Type*} [comm_group G] : G →* G := { to_fun := has_inv.inv, map_one' := one_inv, map_mul' := mul_inv } /-- The identity map from a type with 1 to itself. -/ @[to_additive, simps] def one_hom.id (M : Type*) [has_one M] : one_hom M M := { to_fun := λ x, x, map_one' := rfl, } /-- The identity map from a type with multiplication to itself. -/ @[to_additive, simps] def mul_hom.id (M : Type*) [has_mul M] : mul_hom M M := { to_fun := λ x, x, map_mul' := λ _ _, rfl, } /-- The identity map from a monoid to itself. -/ @[to_additive, simps] def monoid_hom.id (M : Type*) [mul_one_class M] : M →* M := { to_fun := λ x, x, map_one' := rfl, map_mul' := λ _ _, rfl, } /-- The identity map from a monoid_with_zero to itself. -/ @[simps] def monoid_with_zero_hom.id (M : Type*) [mul_zero_one_class M] : M →*₀ M := { to_fun := λ x, x, map_zero' := rfl, map_one' := rfl, map_mul' := λ _ _, rfl, } /-- The identity map from an type with zero to itself. -/ add_decl_doc zero_hom.id /-- The identity map from an type with addition to itself. -/ add_decl_doc add_hom.id /-- The identity map from an additive monoid to itself. -/ add_decl_doc add_monoid_hom.id /-- Composition of `one_hom`s as a `one_hom`. -/ @[to_additive] def one_hom.comp [has_one M] [has_one N] [has_one P] (hnp : one_hom N P) (hmn : one_hom M N) : one_hom M P := { to_fun := hnp ∘ hmn, map_one' := by simp, } /-- Composition of `mul_hom`s as a `mul_hom`. -/ @[to_additive] def mul_hom.comp [has_mul M] [has_mul N] [has_mul P] (hnp : mul_hom N P) (hmn : mul_hom M N) : mul_hom M P := { to_fun := hnp ∘ hmn, map_mul' := by simp, } /-- Composition of monoid morphisms as a monoid morphism. -/ @[to_additive] def monoid_hom.comp [mul_one_class M] [mul_one_class N] [mul_one_class P] (hnp : N →* P) (hmn : M →* N) : M →* P := { to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp, } /-- Composition of `monoid_with_zero_hom`s as a `monoid_with_zero_hom`. -/ def monoid_with_zero_hom.comp [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] (hnp : N →*₀ P) (hmn : M →*₀ N) : M →*₀ P := { to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_mul' := by simp, } /-- Composition of `zero_hom`s as a `zero_hom`. -/ add_decl_doc zero_hom.comp /-- Composition of `add_hom`s as a `add_hom`. -/ add_decl_doc add_hom.comp /-- Composition of additive monoid morphisms as an additive monoid morphism. -/ add_decl_doc add_monoid_hom.comp @[simp, to_additive] lemma one_hom.coe_comp [has_one M] [has_one N] [has_one P] (g : one_hom N P) (f : one_hom M N) : ⇑(g.comp f) = g ∘ f := rfl @[simp, to_additive] lemma mul_hom.coe_comp [has_mul M] [has_mul N] [has_mul P] (g : mul_hom N P) (f : mul_hom M N) : ⇑(g.comp f) = g ∘ f := rfl @[simp, to_additive] lemma monoid_hom.coe_comp [mul_one_class M] [mul_one_class N] [mul_one_class P] (g : N →* P) (f : M →* N) : ⇑(g.comp f) = g ∘ f := rfl @[simp] lemma monoid_with_zero_hom.coe_comp [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] (g : N →*₀ P) (f : M →*₀ N) : ⇑(g.comp f) = g ∘ f := rfl @[to_additive] lemma one_hom.comp_apply [has_one M] [has_one N] [has_one P] (g : one_hom N P) (f : one_hom M N) (x : M) : g.comp f x = g (f x) := rfl @[to_additive] lemma mul_hom.comp_apply [has_mul M] [has_mul N] [has_mul P] (g : mul_hom N P) (f : mul_hom M N) (x : M) : g.comp f x = g (f x) := rfl @[to_additive] lemma monoid_hom.comp_apply [mul_one_class M] [mul_one_class N] [mul_one_class P] (g : N →* P) (f : M →* N) (x : M) : g.comp f x = g (f x) := rfl lemma monoid_with_zero_hom.comp_apply [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] (g : N →*₀ P) (f : M →*₀ N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of monoid homomorphisms is associative. -/ @[to_additive] lemma one_hom.comp_assoc {Q : Type*} [has_one M] [has_one N] [has_one P] [has_one Q] (f : one_hom M N) (g : one_hom N P) (h : one_hom P Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma mul_hom.comp_assoc {Q : Type*} [has_mul M] [has_mul N] [has_mul P] [has_mul Q] (f : mul_hom M N) (g : mul_hom N P) (h : mul_hom P Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma monoid_hom.comp_assoc {Q : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] [mul_one_class Q] (f : M →* N) (g : N →* P) (h : P →* Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl lemma monoid_with_zero_hom.comp_assoc {Q : Type*} [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] [mul_zero_one_class Q] (f : M →*₀ N) (g : N →*₀ P) (h : P →*₀ Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma one_hom.cancel_right [has_one M] [has_one N] [has_one P] {g₁ g₂ : one_hom N P} {f : one_hom M N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, one_hom.ext $ hf.forall.2 (one_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma mul_hom.cancel_right [has_mul M] [has_mul N] [has_mul P] {g₁ g₂ : mul_hom N P} {f : mul_hom M N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, mul_hom.ext $ hf.forall.2 (mul_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma monoid_hom.cancel_right [mul_one_class M] [mul_one_class N] [mul_one_class P] {g₁ g₂ : N →* P} {f : M →* N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, monoid_hom.ext $ hf.forall.2 (monoid_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ lemma monoid_with_zero_hom.cancel_right [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] {g₁ g₂ : N →*₀ P} {f : M →*₀ N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, monoid_with_zero_hom.ext $ hf.forall.2 (monoid_with_zero_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma one_hom.cancel_left [has_one M] [has_one N] [has_one P] {g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply], λ h, h ▸ rfl⟩ @[to_additive] lemma mul_hom.cancel_left [has_mul M] [has_mul N] [has_mul P] {g : mul_hom N P} {f₁ f₂ : mul_hom M N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, mul_hom.ext $ λ x, hg $ by rw [← mul_hom.comp_apply, h, mul_hom.comp_apply], λ h, h ▸ rfl⟩ @[to_additive] lemma monoid_hom.cancel_left [mul_one_class M] [mul_one_class N] [mul_one_class P] {g : N →* P} {f₁ f₂ : M →* N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, monoid_hom.ext $ λ x, hg $ by rw [← monoid_hom.comp_apply, h, monoid_hom.comp_apply], λ h, h ▸ rfl⟩ lemma monoid_with_zero_hom.cancel_left [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] {g : N →*₀ P} {f₁ f₂ : M →*₀ N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, monoid_with_zero_hom.ext $ λ x, hg $ by rw [ ← monoid_with_zero_hom.comp_apply, h, monoid_with_zero_hom.comp_apply], λ h, h ▸ rfl⟩ @[to_additive] lemma monoid_hom.to_one_hom_injective [mul_one_class M] [mul_one_class N] : function.injective (monoid_hom.to_one_hom : (M →* N) → one_hom M N) := λ f g h, monoid_hom.ext $ one_hom.ext_iff.mp h @[to_additive] lemma monoid_hom.to_mul_hom_injective [mul_one_class M] [mul_one_class N] : function.injective (monoid_hom.to_mul_hom : (M →* N) → mul_hom M N) := λ f g h, monoid_hom.ext $ mul_hom.ext_iff.mp h lemma monoid_with_zero_hom.to_monoid_hom_injective [monoid_with_zero M] [monoid_with_zero N] : function.injective (monoid_with_zero_hom.to_monoid_hom : (M →*₀ N) → M →* N) := λ f g h, monoid_with_zero_hom.ext $ monoid_hom.ext_iff.mp h lemma monoid_with_zero_hom.to_zero_hom_injective [monoid_with_zero M] [monoid_with_zero N] : function.injective (monoid_with_zero_hom.to_zero_hom : (M →*₀ N) → zero_hom M N) := λ f g h, monoid_with_zero_hom.ext $ zero_hom.ext_iff.mp h @[simp, to_additive] lemma one_hom.comp_id [has_one M] [has_one N] (f : one_hom M N) : f.comp (one_hom.id M) = f := one_hom.ext $ λ x, rfl @[simp, to_additive] lemma mul_hom.comp_id [has_mul M] [has_mul N] (f : mul_hom M N) : f.comp (mul_hom.id M) = f := mul_hom.ext $ λ x, rfl @[simp, to_additive] lemma monoid_hom.comp_id [mul_one_class M] [mul_one_class N] (f : M →* N) : f.comp (monoid_hom.id M) = f := monoid_hom.ext $ λ x, rfl @[simp] lemma monoid_with_zero_hom.comp_id [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : f.comp (monoid_with_zero_hom.id M) = f := monoid_with_zero_hom.ext $ λ x, rfl @[simp, to_additive] lemma one_hom.id_comp [has_one M] [has_one N] (f : one_hom M N) : (one_hom.id N).comp f = f := one_hom.ext $ λ x, rfl @[simp, to_additive] lemma mul_hom.id_comp [has_mul M] [has_mul N] (f : mul_hom M N) : (mul_hom.id N).comp f = f := mul_hom.ext $ λ x, rfl @[simp, to_additive] lemma monoid_hom.id_comp [mul_one_class M] [mul_one_class N] (f : M →* N) : (monoid_hom.id N).comp f = f := monoid_hom.ext $ λ x, rfl @[simp] lemma monoid_with_zero_hom.id_comp [mul_zero_one_class M] [mul_zero_one_class N] (f : M →*₀ N) : (monoid_with_zero_hom.id N).comp f = f := monoid_with_zero_hom.ext $ λ x, rfl @[to_additive add_monoid_hom.map_nsmul] protected theorem monoid_hom.map_pow [monoid M] [monoid N] (f : M →* N) (a : M) (n : ℕ) : f (a ^ n) = (f a) ^ n := map_pow f a n @[to_additive] protected theorem monoid_hom.map_zpow' [div_inv_monoid M] [div_inv_monoid N] (f : M →* N) (hf : ∀ x, f (x⁻¹) = (f x)⁻¹) (a : M) (n : ℤ) : f (a ^ n) = (f a) ^ n := map_zpow' f hf a n @[to_additive] theorem monoid_hom.map_div' [div_inv_monoid M] [div_inv_monoid N] (f : M →* N) (hf : ∀ x, f (x⁻¹) = (f x)⁻¹) (a b : M) : f (a / b) = f a / f b := by rw [div_eq_mul_inv, div_eq_mul_inv, f.map_mul, hf] section End namespace monoid variables (M) [mul_one_class M] /-- The monoid of endomorphisms. -/ protected def End := M →* M namespace End instance : monoid (monoid.End M) := { mul := monoid_hom.comp, one := monoid_hom.id M, mul_assoc := λ _ _ _, monoid_hom.comp_assoc _ _ _, mul_one := monoid_hom.comp_id, one_mul := monoid_hom.id_comp } instance : inhabited (monoid.End M) := ⟨1⟩ instance : has_coe_to_fun (monoid.End M) (λ _, M → M) := ⟨monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : monoid.End M) : M → M) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : monoid.End M) : M → M) = f ∘ g := rfl end monoid namespace add_monoid variables (A : Type*) [add_zero_class A] /-- The monoid of endomorphisms. -/ protected def End := A →+ A namespace End instance : monoid (add_monoid.End A) := { mul := add_monoid_hom.comp, one := add_monoid_hom.id A, mul_assoc := λ _ _ _, add_monoid_hom.comp_assoc _ _ _, mul_one := add_monoid_hom.comp_id, one_mul := add_monoid_hom.id_comp } instance : inhabited (add_monoid.End A) := ⟨1⟩ instance : has_coe_to_fun (add_monoid.End A) (λ _, A → A) := ⟨add_monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : add_monoid.End A) : A → A) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : add_monoid.End A) : A → A) = f ∘ g := rfl end add_monoid end End /-- `1` is the homomorphism sending all elements to `1`. -/ @[to_additive] instance [has_one M] [has_one N] : has_one (one_hom M N) := ⟨⟨λ _, 1, rfl⟩⟩ /-- `1` is the multiplicative homomorphism sending all elements to `1`. -/ @[to_additive] instance [has_mul M] [mul_one_class N] : has_one (mul_hom M N) := ⟨⟨λ _, 1, λ _ _, (one_mul 1).symm⟩⟩ /-- `1` is the monoid homomorphism sending all elements to `1`. -/ @[to_additive] instance [mul_one_class M] [mul_one_class N] : has_one (M →* N) := ⟨⟨λ _, 1, rfl, λ _ _, (one_mul 1).symm⟩⟩ /-- `0` is the homomorphism sending all elements to `0`. -/ add_decl_doc zero_hom.has_zero /-- `0` is the additive homomorphism sending all elements to `0`. -/ add_decl_doc add_hom.has_zero /-- `0` is the additive monoid homomorphism sending all elements to `0`. -/ add_decl_doc add_monoid_hom.has_zero @[simp, to_additive] lemma one_hom.one_apply [has_one M] [has_one N] (x : M) : (1 : one_hom M N) x = 1 := rfl @[simp, to_additive] lemma monoid_hom.one_apply [mul_one_class M] [mul_one_class N] (x : M) : (1 : M →* N) x = 1 := rfl @[simp, to_additive] lemma one_hom.one_comp [has_one M] [has_one N] [has_one P] (f : one_hom M N) : (1 : one_hom N P).comp f = 1 := rfl @[simp, to_additive] lemma one_hom.comp_one [has_one M] [has_one N] [has_one P] (f : one_hom N P) : f.comp (1 : one_hom M N) = 1 := by { ext, simp only [one_hom.map_one, one_hom.coe_comp, function.comp_app, one_hom.one_apply] } @[to_additive] instance [has_one M] [has_one N] : inhabited (one_hom M N) := ⟨1⟩ @[to_additive] instance [has_mul M] [mul_one_class N] : inhabited (mul_hom M N) := ⟨1⟩ @[to_additive] instance [mul_one_class M] [mul_one_class N] : inhabited (M →* N) := ⟨1⟩ -- unlike the other homs, `monoid_with_zero_hom` does not have a `1` or `0` instance [mul_zero_one_class M] : inhabited (M →*₀ M) := ⟨monoid_with_zero_hom.id M⟩ namespace monoid_hom variables [mM : mul_one_class M] [mN : mul_one_class N] [mP : mul_one_class P] variables [group G] [comm_group H] /-- Given two monoid morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid morphism sending `x` to `f x * g x`. -/ @[to_additive] instance {M N} {mM : mul_one_class M} [comm_monoid N] : has_mul (M →* N) := ⟨λ f g, { to_fun := λ m, f m * g m, map_one' := show f 1 * g 1 = 1, by simp, map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y), rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end }⟩ /-- Given two additive monoid morphisms `f`, `g` to an additive commutative monoid, `f + g` is the additive monoid morphism sending `x` to `f x + g x`. -/ add_decl_doc add_monoid_hom.has_add @[simp, to_additive] lemma mul_apply {M N} {mM : mul_one_class M} {mN : comm_monoid N} (f g : M →* N) (x : M) : (f * g) x = f x * g x := rfl @[simp, to_additive] lemma one_comp [mul_one_class M] [mul_one_class N] [mul_one_class P] (f : M →* N) : (1 : N →* P).comp f = 1 := rfl @[simp, to_additive] lemma comp_one [mul_one_class M] [mul_one_class N] [mul_one_class P] (f : N →* P) : f.comp (1 : M →* N) = 1 := by { ext, simp only [map_one, coe_comp, function.comp_app, one_apply] } @[to_additive] lemma mul_comp [mul_one_class M] [comm_monoid N] [comm_monoid P] (g₁ g₂ : N →* P) (f : M →* N) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl @[to_additive] lemma comp_mul [mul_one_class M] [comm_monoid N] [comm_monoid P] (g : N →* P) (f₁ f₂ : M →* N) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := by { ext, simp only [mul_apply, function.comp_app, map_mul, coe_comp] } /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {G} [group G] [monoid M] {f g : G →* M} {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (map_mul_eq_one f $ inv_mul_self x) $ h.symm ▸ g.map_mul_eq_one $ mul_inv_self x /-- Group homomorphisms preserve inverse. -/ @[to_additive] protected theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ := map_inv f g /-- Group homomorphisms preserve integer power. -/ @[to_additive /-" Additive group homomorphisms preserve integer scaling. "-/] protected theorem map_zpow {G H} [group G] [group H] (f : G →* H) (g : G) (n : ℤ) : f (g ^ n) = (f g) ^ n := map_zpow f g n /-- Group homomorphisms preserve division. -/ @[to_additive /-" Additive group homomorphisms preserve subtraction. "-/] protected theorem map_div {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g / h) = f g / f h := map_div f g h /-- Group homomorphisms preserve division. -/ @[to_additive] protected theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g * h⁻¹) = (f g) * (f h)⁻¹ := map_mul_inv f g h /-- A homomorphism from a group to a monoid is injective iff its kernel is trivial. For the iff statement on the triviality of the kernel, see `monoid_hom.injective_iff'`. -/ @[to_additive /-" A homomorphism from an additive group to an additive monoid is injective iff its kernel is trivial. For the iff statement on the triviality of the kernel, see `add_monoid_hom.injective_iff'`. "-/] lemma injective_iff {G H} [group G] [mul_one_class H] (f : G →* H) : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h x, (map_eq_one_iff f h).mp, λ h x y hxy, mul_inv_eq_one.1 $ h _ $ by rw [f.map_mul, hxy, ← f.map_mul, mul_inv_self, f.map_one]⟩ /-- A homomorphism from a group to a monoid is injective iff its kernel is trivial, stated as an iff on the triviality of the kernel. For the implication, see `monoid_hom.injective_iff`. -/ @[to_additive /-" A homomorphism from an additive group to an additive monoid is injective iff its kernel is trivial, stated as an iff on the triviality of the kernel. For the implication, see `add_monoid_hom.injective_iff`. "-/] lemma injective_iff' {G H} [group G] [mul_one_class H] (f : G →* H) : function.injective f ↔ (∀ a, f a = 1 ↔ a = 1) := f.injective_iff.trans $ forall_congr $ λ a, ⟨λ h, ⟨h, λ H, H.symm ▸ f.map_one⟩, iff.mp⟩ include mM /-- Makes a group homomorphism from a proof that the map preserves multiplication. -/ @[to_additive "Makes an additive group homomorphism from a proof that the map preserves addition.", simps {fully_applied := ff}] def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G := { to_fun := f, map_mul' := map_mul, map_one' := mul_left_eq_self.1 $ by rw [←map_mul, mul_one] } omit mM /-- Makes a group homomorphism from a proof that the map preserves right division `λ x y, x * y⁻¹`. See also `monoid_hom.of_map_div` for a version using `λ x y, x / y`. -/ @[to_additive "Makes an additive group homomorphism from a proof that the map preserves the operation `λ a b, a + -b`. See also `add_monoid_hom.of_map_sub` for a version using `λ a b, a - b`."] def of_map_mul_inv {H : Type*} [group H] (f : G → H) (map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) : G →* H := mk' f $ λ x y, calc f (x * y) = f x * (f $ 1 * 1⁻¹ * y⁻¹)⁻¹ : by simp only [one_mul, one_inv, ← map_div, inv_inv] ... = f x * f y : by { simp only [map_div], simp only [mul_right_inv, one_mul, inv_inv] } @[simp, to_additive] lemma coe_of_map_mul_inv {H : Type*} [group H] (f : G → H) (map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) : ⇑(of_map_mul_inv f map_div) = f := rfl /-- Define a morphism of additive groups given a map which respects ratios. -/ @[to_additive /-"Define a morphism of additive groups given a map which respects difference."-/] def of_map_div {H : Type*} [group H] (f : G → H) (hf : ∀ x y, f (x / y) = f x / f y) : G →* H := of_map_mul_inv f (by simpa only [div_eq_mul_inv] using hf) @[simp, to_additive] lemma coe_of_map_div {H : Type*} [group H] (f : G → H) (hf : ∀ x y, f (x / y) = f x / f y) : ⇑(of_map_div f hf) = f := rfl /-- If `f` is a monoid homomorphism to a commutative group, then `f⁻¹` is the homomorphism sending `x` to `(f x)⁻¹`. -/ @[to_additive] instance {M G} [mul_one_class M] [comm_group G] : has_inv (M →* G) := ⟨λ f, mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul]⟩ /-- If `f` is an additive monoid homomorphism to an additive commutative group, then `-f` is the homomorphism sending `x` to `-(f x)`. -/ add_decl_doc add_monoid_hom.has_neg @[simp, to_additive] lemma inv_apply {M G} {mM : mul_one_class M} {gG : comm_group G} (f : M →* G) (x : M) : f⁻¹ x = (f x)⁻¹ := rfl @[simp, to_additive] lemma inv_comp {M N A} {mM : mul_one_class M} {gN : mul_one_class N} {gA : comm_group A} (φ : N →* A) (ψ : M →* N) : φ⁻¹.comp ψ = (φ.comp ψ)⁻¹ := by { ext, simp only [function.comp_app, inv_apply, coe_comp] } @[simp, to_additive] lemma comp_inv {M A B} {mM : mul_one_class M} {mA : comm_group A} {mB : comm_group B} (φ : A →* B) (ψ : M →* A) : φ.comp ψ⁻¹ = (φ.comp ψ)⁻¹ := by { ext, simp only [function.comp_app, inv_apply, map_inv, coe_comp] } /-- If `f` and `g` are monoid homomorphisms to a commutative group, then `f / g` is the homomorphism sending `x` to `(f x) / (g x)`. -/ @[to_additive] instance {M G} [mul_one_class M] [comm_group G] : has_div (M →* G) := ⟨λ f g, mk' (λ x, f x / g x) $ λ a b, by simp [div_eq_mul_inv, mul_assoc, mul_left_comm, mul_comm]⟩ /-- If `f` and `g` are monoid homomorphisms to an additive commutative group, then `f - g` is the homomorphism sending `x` to `(f x) - (g x)`. -/ add_decl_doc add_monoid_hom.has_sub @[simp, to_additive] lemma div_apply {M G} {mM : mul_one_class M} {gG : comm_group G} (f g : M →* G) (x : M) : (f / g) x = f x / g x := rfl end monoid_hom /-- Given two monoid with zero morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid with zero morphism sending `x` to `f x * g x`. -/ instance {M N} {hM : mul_zero_one_class M} [comm_monoid_with_zero N] : has_mul (M →*₀ N) := ⟨λ f g, { to_fun := λ a, f a * g a, map_zero' := by rw [map_zero, zero_mul], ..(f * g : M →* N) }⟩ section commute variables [has_mul M] [has_mul N] {a x y : M} @[simp, to_additive] protected lemma semiconj_by.map [mul_hom_class F M N] (h : semiconj_by a x y) (f : F) : semiconj_by (f a) (f x) (f y) := by simpa only [semiconj_by, map_mul] using congr_arg f h @[simp, to_additive] protected lemma commute.map [mul_hom_class F M N] (h : commute x y) (f : F) : commute (f x) (f y) := h.map f end commute
cd7eb2a4decabc551e482821546e6731f18926cf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/nat/gcd/basic.lean
36a748926949e8f626e8bb8c2810993419791073
[ "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
24,197
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 algebra.group_power.basic import algebra.group_with_zero.divisibility import data.nat.order.lemmas /-! # Definitions and properties of `nat.gcd`, `nat.lcm`, and `nat.coprime` Generalizations of these are provided in a later file as `gcd_monoid.gcd` and `gcd_monoid.lcm`. Note that the global `is_coprime` is not a straightforward generalization of `nat.coprime`, see `nat.is_coprime_iff_coprime` for the connection between the two. -/ namespace nat /-! ### `gcd` -/ theorem gcd_dvd (m n : ℕ) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (λn, by rw gcd_zero_left; exact ⟨dvd_zero n, dvd_refl n⟩) (λm n npos, by rw ←gcd_rec; exact λ ⟨IH₁, IH₂⟩, ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (m n : ℕ) : gcd m n ∣ m := (gcd_dvd m n).left theorem gcd_dvd_right (m n : ℕ) : gcd m n ∣ n := (gcd_dvd m n).right theorem gcd_le_left {m} (n) (h : 0 < m) : gcd m n ≤ m := le_of_dvd h $ gcd_dvd_left m n theorem gcd_le_right (m) {n} (h : 0 < n) : gcd m n ≤ n := le_of_dvd h $ gcd_dvd_right m n theorem dvd_gcd {m n k : ℕ} : k ∣ m → k ∣ n → k ∣ gcd m n := gcd.induction m n (λn _ kn, by rw gcd_zero_left; exact kn) (λn m mpos IH H1 H2, by rw gcd_rec; exact IH ((dvd_mod_iff H1).2 H2) H1) theorem dvd_gcd_iff {m n k : ℕ} : k ∣ gcd m n ↔ k ∣ m ∧ k ∣ n := iff.intro (λ h, ⟨h.trans (gcd_dvd m n).left, h.trans (gcd_dvd m n).right⟩) (λ h, dvd_gcd h.left h.right) theorem gcd_comm (m n : ℕ) : gcd m n = gcd n m := dvd_antisymm (dvd_gcd (gcd_dvd_right m n) (gcd_dvd_left m n)) (dvd_gcd (gcd_dvd_right n m) (gcd_dvd_left n m)) theorem gcd_eq_left_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd m n = m := ⟨λ h, by rw [gcd_rec, mod_eq_zero_of_dvd h, gcd_zero_left], λ h, h ▸ gcd_dvd_right m n⟩ theorem gcd_eq_right_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd n m = m := by rw gcd_comm; apply gcd_eq_left_iff_dvd theorem gcd_assoc (m n k : ℕ) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) @[simp] theorem gcd_one_right (n : ℕ) : gcd n 1 = 1 := eq.trans (gcd_comm n 1) $ gcd_one_left n theorem gcd_mul_left (m n k : ℕ) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (λk, by repeat {rw mul_zero <|> rw gcd_zero_left}) (λk n H IH, by rwa [←mul_mod_mul_left, ←gcd_rec, ←gcd_rec] at IH) theorem gcd_mul_right (m n k : ℕ) : gcd (m * n) (k * n) = gcd m k * n := by rw [mul_comm m n, mul_comm k n, mul_comm (gcd m k) n, gcd_mul_left] theorem gcd_pos_of_pos_left {m : ℕ} (n : ℕ) (mpos : 0 < m) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_left m n) mpos theorem gcd_pos_of_pos_right (m : ℕ) {n : ℕ} (npos : 0 < n) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_right m n) npos theorem eq_zero_of_gcd_eq_zero_left {m n : ℕ} (H : gcd m n = 0) : m = 0 := or.elim (nat.eq_zero_or_pos m) id (assume H1 : 0 < m, absurd (eq.symm H) (ne_of_lt (gcd_pos_of_pos_left _ H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : ℕ} (H : gcd m n = 0) : n = 0 := by rw gcd_comm at H; exact eq_zero_of_gcd_eq_zero_left H @[simp] theorem gcd_eq_zero_iff {i j : ℕ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin split, { intro h, exact ⟨eq_zero_of_gcd_eq_zero_left h, eq_zero_of_gcd_eq_zero_right h⟩, }, { rintro ⟨rfl, rfl⟩, exact nat.gcd_zero_right 0 } end theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m / k) (n / k) = gcd m n / k := or.elim (nat.eq_zero_or_pos k) (λk0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right]) (λH3, nat.eq_of_mul_eq_mul_right H3 $ by rw [ nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right, nat.div_mul_cancel H1, nat.div_mul_cancel H2]) theorem gcd_greatest {a b d : ℕ} (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : ℕ, e ∣ a → e ∣ b → e ∣ d) : d = a.gcd b := (dvd_antisymm (hd _ (gcd_dvd_left a b) (gcd_dvd_right a b)) (dvd_gcd hda hdb)).symm theorem gcd_dvd_gcd_of_dvd_left {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd m n ∣ gcd k n := dvd_gcd ((gcd_dvd_left m n).trans H) (gcd_dvd_right m n) theorem gcd_dvd_gcd_of_dvd_right {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd n m ∣ gcd n k := dvd_gcd (gcd_dvd_left n m) ((gcd_dvd_right n m).trans H) theorem gcd_dvd_gcd_mul_left (m n k : ℕ) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (m n k : ℕ) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (m n k : ℕ) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : ℕ) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {m n : ℕ} (H : m ∣ n) : gcd m n = m := dvd_antisymm (gcd_dvd_left _ _) (dvd_gcd dvd_rfl H) theorem gcd_eq_right {m n : ℕ} (H : n ∣ m) : gcd m n = n := by rw [gcd_comm, gcd_eq_left H] -- Lemmas where one argument is a multiple of the other @[simp] lemma gcd_mul_left_left (m n : ℕ) : gcd (m * n) n = n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (dvd_mul_left _ _) dvd_rfl) @[simp] lemma gcd_mul_left_right (m n : ℕ) : gcd n (m * n) = n := by rw [gcd_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_left (m n : ℕ) : gcd (n * m) n = n := by rw [mul_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_right (m n : ℕ) : gcd n (n * m) = n := by rw [gcd_comm, gcd_mul_right_left] -- Lemmas for repeated application of `gcd` @[simp] lemma gcd_gcd_self_right_left (m n : ℕ) : gcd m (gcd m n) = gcd m n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (gcd_dvd_left _ _) dvd_rfl) @[simp] lemma gcd_gcd_self_right_right (m n : ℕ) : gcd m (gcd n m) = gcd n m := by rw [gcd_comm n m, gcd_gcd_self_right_left] @[simp] lemma gcd_gcd_self_left_right (m n : ℕ) : gcd (gcd n m) m = gcd n m := by rw [gcd_comm, gcd_gcd_self_right_right] @[simp] lemma gcd_gcd_self_left_left (m n : ℕ) : gcd (gcd m n) m = gcd m n := by rw [gcd_comm m n, gcd_gcd_self_left_right] -- Lemmas where one argument consists of addition of a multiple of the other @[simp] lemma gcd_add_mul_right_right (m n k : ℕ) : gcd m (n + k * m) = gcd m n := by simp [gcd_rec m (n + k * m), gcd_rec m n] @[simp] lemma gcd_add_mul_left_right (m n k : ℕ) : gcd m (n + m * k) = gcd m n := by simp [gcd_rec m (n + m * k), gcd_rec m n] @[simp] lemma gcd_mul_right_add_right (m n k : ℕ) : gcd m (k * m + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_mul_left_add_right (m n k : ℕ) : gcd m (m * k + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_add_mul_right_left (m n k : ℕ) : gcd (m + k * n) n = gcd m n := by rw [gcd_comm, gcd_add_mul_right_right, gcd_comm] @[simp] lemma gcd_add_mul_left_left (m n k : ℕ) : gcd (m + n * k) n = gcd m n := by rw [gcd_comm, gcd_add_mul_left_right, gcd_comm] @[simp] lemma gcd_mul_right_add_left (m n k : ℕ) : gcd (k * n + m) n = gcd m n := by rw [gcd_comm, gcd_mul_right_add_right, gcd_comm] @[simp] lemma gcd_mul_left_add_left (m n k : ℕ) : gcd (n * k + m) n = gcd m n := by rw [gcd_comm, gcd_mul_left_add_right, gcd_comm] -- Lemmas where one argument consists of an addition of the other @[simp] lemma gcd_add_self_right (m n : ℕ) : gcd m (n + m) = gcd m n := eq.trans (by rw one_mul) (gcd_add_mul_right_right m n 1) @[simp] lemma gcd_add_self_left (m n : ℕ) : gcd (m + n) n = gcd m n := by rw [gcd_comm, gcd_add_self_right, gcd_comm] @[simp] lemma gcd_self_add_left (m n : ℕ) : gcd (m + n) m = gcd n m := by rw [add_comm, gcd_add_self_left] @[simp] lemma gcd_self_add_right (m n : ℕ) : gcd m (m + n) = gcd m n := by rw [add_comm, gcd_add_self_right] /-! ### `lcm` -/ theorem lcm_comm (m n : ℕ) : lcm m n = lcm n m := by delta lcm; rw [mul_comm, gcd_comm] @[simp] theorem lcm_zero_left (m : ℕ) : lcm 0 m = 0 := by delta lcm; rw [zero_mul, nat.zero_div] @[simp] theorem lcm_zero_right (m : ℕ) : lcm m 0 = 0 := lcm_comm 0 m ▸ lcm_zero_left m @[simp] theorem lcm_one_left (m : ℕ) : lcm 1 m = m := by delta lcm; rw [one_mul, gcd_one_left, nat.div_one] @[simp] theorem lcm_one_right (m : ℕ) : lcm m 1 = m := lcm_comm 1 m ▸ lcm_one_left m @[simp] theorem lcm_self (m : ℕ) : lcm m m = m := or.elim (nat.eq_zero_or_pos m) (λh, by rw [h, lcm_zero_left]) (λh, by delta lcm; rw [gcd_self, nat.mul_div_cancel _ h]) theorem dvd_lcm_left (m n : ℕ) : m ∣ lcm m n := dvd.intro (n / gcd m n) (nat.mul_div_assoc _ $ gcd_dvd_right m n).symm theorem dvd_lcm_right (m n : ℕ) : n ∣ lcm m n := lcm_comm n m ▸ dvd_lcm_left n m theorem gcd_mul_lcm (m n : ℕ) : gcd m n * lcm m n = m * n := by delta lcm; rw [nat.mul_div_cancel' ((gcd_dvd_left m n).trans (dvd_mul_right m n))] theorem lcm_dvd {m n k : ℕ} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (nat.eq_zero_or_pos k) (λh, by rw h; exact dvd_zero _) (λkpos, dvd_of_mul_dvd_mul_left (gcd_pos_of_pos_left n (pos_of_dvd_of_pos H1 kpos)) $ by rw [gcd_mul_lcm, ←gcd_mul_right, mul_comm n k]; exact dvd_gcd (mul_dvd_mul_left _ H2) (mul_dvd_mul_right H1 _)) theorem lcm_dvd_mul (m n : ℕ) : lcm m n ∣ m * n := lcm_dvd (dvd_mul_right _ _) (dvd_mul_left _ _) lemma lcm_dvd_iff {m n k : ℕ} : lcm m n ∣ k ↔ m ∣ k ∧ n ∣ k := ⟨λ h, ⟨(dvd_lcm_left _ _).trans h, (dvd_lcm_right _ _).trans h⟩, and_imp.2 lcm_dvd⟩ theorem lcm_assoc (m n k : ℕ) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm (lcm_dvd (lcm_dvd (dvd_lcm_left m (lcm n k)) ((dvd_lcm_left n k).trans (dvd_lcm_right m (lcm n k)))) ((dvd_lcm_right n k).trans (dvd_lcm_right m (lcm n k)))) (lcm_dvd ((dvd_lcm_left m n).trans (dvd_lcm_left (lcm m n) k)) (lcm_dvd ((dvd_lcm_right m n).trans (dvd_lcm_left (lcm m n) k)) (dvd_lcm_right (lcm m n) k))) theorem lcm_ne_zero {m n : ℕ} (hm : m ≠ 0) (hn : n ≠ 0) : lcm m n ≠ 0 := by { intro h, simpa [h, hm, hn] using gcd_mul_lcm m n, } /-! ### `coprime` See also `nat.coprime_of_dvd` and `nat.coprime_of_dvd'` to prove `nat.coprime m n`. -/ instance (m n : ℕ) : decidable (coprime m n) := by unfold coprime; apply_instance theorem coprime_iff_gcd_eq_one {m n : ℕ} : coprime m n ↔ gcd m n = 1 := iff.rfl theorem coprime.gcd_eq_one {m n : ℕ} (h : coprime m n) : gcd m n = 1 := h theorem coprime.lcm_eq_mul {m n : ℕ} (h : coprime m n) : lcm m n = m * n := by rw [←one_mul (lcm m n), ←h.gcd_eq_one, gcd_mul_lcm] theorem coprime.symm {m n : ℕ} : coprime n m → coprime m n := (gcd_comm m n).trans theorem coprime_comm {m n : ℕ} : coprime n m ↔ coprime m n := ⟨coprime.symm, coprime.symm⟩ theorem coprime.symmetric : symmetric coprime := λ m n, coprime.symm theorem coprime.dvd_of_dvd_mul_right {m n k : ℕ} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := let t := dvd_gcd (dvd_mul_left k m) H2 in by rwa [gcd_mul_left, H1.gcd_eq_one, mul_one] at t theorem coprime.dvd_of_dvd_mul_left {m n k : ℕ} (H1 : coprime k m) (H2 : k ∣ m * n) : k ∣ n := by rw mul_comm at H2; exact H1.dvd_of_dvd_mul_right H2 theorem coprime.dvd_mul_right {m n k : ℕ} (H : coprime k n) : k ∣ m * n ↔ k ∣ m := ⟨H.dvd_of_dvd_mul_right, λ h, dvd_mul_of_dvd_left h n⟩ theorem coprime.dvd_mul_left {m n k : ℕ} (H : coprime k m) : k ∣ m * n ↔ k ∣ n := ⟨H.dvd_of_dvd_mul_left, λ h, dvd_mul_of_dvd_right h m⟩ theorem coprime.gcd_mul_left_cancel {k : ℕ} (m : ℕ) {n : ℕ} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, by rw [coprime, gcd_assoc, H.symm.gcd_eq_one, gcd_one_right], dvd_antisymm (dvd_gcd (H1.dvd_of_dvd_mul_left (gcd_dvd_left _ _)) (gcd_dvd_right _ _)) (gcd_dvd_gcd_mul_left _ _ _) theorem coprime.gcd_mul_right_cancel (m : ℕ) {k n : ℕ} (H : coprime k n) : gcd (m * k) n = gcd m n := by rw [mul_comm m k, H.gcd_mul_left_cancel m] theorem coprime.gcd_mul_left_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (k * n) = gcd m n := by rw [gcd_comm m n, gcd_comm m (k * n), H.gcd_mul_left_cancel n] theorem coprime.gcd_mul_right_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (n * k) = gcd m n := by rw [mul_comm n k, H.gcd_mul_left_cancel_right n] theorem coprime_div_gcd_div_gcd {m n : ℕ} (H : 0 < gcd m n) : coprime (m / gcd m n) (n / gcd m n) := by rw [coprime_iff_gcd_eq_one, gcd_div (gcd_dvd_left m n) (gcd_dvd_right m n), nat.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : ℕ} (dgt1 : 1 < d) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := λ co, not_lt_of_ge (le_of_dvd zero_lt_one $ by rw [←co.gcd_eq_one]; exact dvd_gcd Hm Hn) dgt1 theorem exists_coprime {m n : ℕ} (H : 0 < gcd m n) : ∃ m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, coprime_div_gcd_div_gcd H, (nat.div_mul_cancel (gcd_dvd_left m n)).symm, (nat.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_coprime' {m n : ℕ} (H : 0 < gcd m n) : ∃ g m' n', 0 < g ∧ coprime m' n' ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_coprime H in ⟨_, m', n', H, h⟩ @[simp] theorem coprime_add_self_right {m n : ℕ} : coprime m (n + m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_right] @[simp] theorem coprime_self_add_right {m n : ℕ} : coprime m (m + n) ↔ coprime m n := by rw [add_comm, coprime_add_self_right] @[simp] theorem coprime_add_self_left {m n : ℕ} : coprime (m + n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_left] @[simp] theorem coprime_self_add_left {m n : ℕ} : coprime (m + n) m ↔ coprime n m := by rw [coprime, coprime, gcd_self_add_left] @[simp] lemma coprime_add_mul_right_right (m n k : ℕ) : coprime m (n + k * m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_right] @[simp] lemma coprime_add_mul_left_right (m n k : ℕ) : coprime m (n + m * k) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_right] @[simp] lemma coprime_mul_right_add_right (m n k : ℕ) : coprime m (k * m + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_right] @[simp] lemma coprime_mul_left_add_right (m n k : ℕ) : coprime m (m * k + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_right] @[simp] lemma coprime_add_mul_right_left (m n k : ℕ) : coprime (m + k * n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_left] @[simp] lemma coprime_add_mul_left_left (m n k : ℕ) : coprime (m + n * k) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_left] @[simp] lemma coprime_mul_right_add_left (m n k : ℕ) : coprime (k * n + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_left] @[simp] lemma coprime_mul_left_add_left (m n k : ℕ) : coprime (n * k + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_left] theorem coprime.mul {m n k : ℕ} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := (H1.gcd_mul_left_cancel n).trans H2 theorem coprime.mul_right {k m n : ℕ} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := (H1.symm.mul H2.symm).symm theorem coprime.coprime_dvd_left {m k n : ℕ} (H1 : m ∣ k) (H2 : coprime k n) : coprime m n := eq_one_of_dvd_one (by delta coprime at H2; rw ← H2; exact gcd_dvd_gcd_of_dvd_left _ H1) theorem coprime.coprime_dvd_right {m k n : ℕ} (H1 : n ∣ m) (H2 : coprime k m) : coprime k n := (H2.symm.coprime_dvd_left H1).symm theorem coprime.coprime_mul_left {k m n : ℕ} (H : coprime (k * m) n) : coprime m n := H.coprime_dvd_left (dvd_mul_left _ _) theorem coprime.coprime_mul_right {k m n : ℕ} (H : coprime (m * k) n) : coprime m n := H.coprime_dvd_left (dvd_mul_right _ _) theorem coprime.coprime_mul_left_right {k m n : ℕ} (H : coprime m (k * n)) : coprime m n := H.coprime_dvd_right (dvd_mul_left _ _) theorem coprime.coprime_mul_right_right {k m n : ℕ} (H : coprime m (n * k)) : coprime m n := H.coprime_dvd_right (dvd_mul_right _ _) theorem coprime.coprime_div_left {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ m) : coprime (m / a) n := begin by_cases a_split : (a = 0), { subst a_split, rw zero_dvd_iff at dvd, simpa [dvd] using cmn, }, { rcases dvd with ⟨k, rfl⟩, rw nat.mul_div_cancel_left _ (nat.pos_of_ne_zero a_split), exact coprime.coprime_mul_left cmn, }, end theorem coprime.coprime_div_right {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ n) : coprime m (n / a) := (coprime.coprime_div_left cmn.symm dvd).symm lemma coprime_mul_iff_left {k m n : ℕ} : coprime (m * n) k ↔ coprime m k ∧ coprime n k := ⟨λ h, ⟨coprime.coprime_mul_right h, coprime.coprime_mul_left h⟩, λ ⟨h, _⟩, by rwa [coprime_iff_gcd_eq_one, coprime.gcd_mul_left_cancel n h]⟩ lemma coprime_mul_iff_right {k m n : ℕ} : coprime k (m * n) ↔ coprime k m ∧ coprime k n := by simpa only [coprime_comm] using coprime_mul_iff_left lemma coprime.gcd_left (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) n := hmn.coprime_dvd_left $ gcd_dvd_right k m lemma coprime.gcd_right (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime m (gcd k n) := hmn.coprime_dvd_right $ gcd_dvd_right k n lemma coprime.gcd_both (k l : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) (gcd l n) := (hmn.gcd_left k).gcd_right l lemma coprime.mul_dvd_of_dvd_of_dvd {a n m : ℕ} (hmn : coprime m n) (hm : m ∣ a) (hn : n ∣ a) : m * n ∣ a := let ⟨k, hk⟩ := hm in hk.symm ▸ mul_dvd_mul_left _ (hmn.symm.dvd_of_dvd_mul_left (hk ▸ hn)) theorem coprime_one_left : ∀ n, coprime 1 n := gcd_one_left theorem coprime_one_right : ∀ n, coprime n 1 := gcd_one_right theorem coprime.pow_left {m k : ℕ} (n : ℕ) (H1 : coprime m k) : coprime (m ^ n) k := nat.rec_on n (coprime_one_left _) (λn IH, H1.mul IH) theorem coprime.pow_right {m k : ℕ} (n : ℕ) (H1 : coprime k m) : coprime k (m ^ n) := (H1.symm.pow_left n).symm theorem coprime.pow {k l : ℕ} (m n : ℕ) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) := (H1.pow_left _).pow_right _ @[simp] lemma coprime_pow_left_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) : nat.coprime (a ^ n) b ↔ nat.coprime a b := begin obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero hn.ne', rw [pow_succ, nat.coprime_mul_iff_left], exact ⟨and.left, λ hab, ⟨hab, hab.pow_left _⟩⟩ end @[simp] lemma coprime_pow_right_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) : nat.coprime a (b ^ n) ↔ nat.coprime a b := by rw [nat.coprime_comm, coprime_pow_left_iff hn, nat.coprime_comm] theorem coprime.eq_one_of_dvd {k m : ℕ} (H : coprime k m) (d : k ∣ m) : k = 1 := by rw [← H.gcd_eq_one, gcd_eq_left d] @[simp] theorem coprime_zero_left (n : ℕ) : coprime 0 n ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_zero_right (n : ℕ) : coprime n 0 ↔ n = 1 := by simp [coprime] theorem not_coprime_zero_zero : ¬ coprime 0 0 := by simp @[simp] theorem coprime_one_left_iff (n : ℕ) : coprime 1 n ↔ true := by simp [coprime] @[simp] theorem coprime_one_right_iff (n : ℕ) : coprime n 1 ↔ true := by simp [coprime] @[simp] theorem coprime_self (n : ℕ) : coprime n n ↔ n = 1 := by simp [coprime] lemma gcd_mul_of_coprime_of_dvd {a b c : ℕ} (hac : coprime a c) (b_dvd_c : b ∣ c) : gcd (a * b) c = b := begin rcases exists_eq_mul_left_of_dvd b_dvd_c with ⟨d, rfl⟩, rw [gcd_mul_right], convert one_mul b, exact coprime.coprime_mul_right_right hac, end lemma coprime.eq_of_mul_eq_zero {m n : ℕ} (h : m.coprime n) (hmn : m * n = 0) : m = 0 ∧ n = 1 ∨ m = 1 ∧ n = 0 := (nat.eq_zero_of_mul_eq_zero hmn).imp (λ hm, ⟨hm, n.coprime_zero_left.mp $ hm ▸ h⟩) (λ hn, ⟨m.coprime_zero_left.mp $ hn ▸ h.symm, hn⟩) /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. See `exists_dvd_and_dvd_of_dvd_mul` for the more general but less constructive version for other `gcd_monoid`s. -/ def prod_dvd_and_dvd_of_dvd_prod {m n k : ℕ} (H : k ∣ m * n) : { d : {m' // m' ∣ m} × {n' // n' ∣ n} // k = d.1 * d.2 } := begin cases h0 : (gcd k m), case nat.zero { obtain rfl : k = 0 := eq_zero_of_gcd_eq_zero_left h0, obtain rfl : m = 0 := eq_zero_of_gcd_eq_zero_right h0, exact ⟨⟨⟨0, dvd_refl 0⟩, ⟨n, dvd_refl n⟩⟩, (zero_mul n).symm⟩ }, case nat.succ : tmp { have hpos : 0 < gcd k m := h0.symm ▸ nat.zero_lt_succ _; clear h0 tmp, have hd : gcd k m * (k / gcd k m) = k := (nat.mul_div_cancel' (gcd_dvd_left k m)), refine ⟨⟨⟨gcd k m, gcd_dvd_right k m⟩, ⟨k / gcd k m, _⟩⟩, hd.symm⟩, apply dvd_of_mul_dvd_mul_left hpos, rw [hd, ← gcd_mul_right], exact dvd_gcd (dvd_mul_right _ _) H } end lemma dvd_mul {x m n : ℕ} : x ∣ (m * n) ↔ ∃ y z, y ∣ m ∧ z ∣ n ∧ y * z = x := begin split, { intro h, obtain ⟨⟨⟨y, hy⟩, ⟨z, hz⟩⟩, rfl⟩ := prod_dvd_and_dvd_of_dvd_prod h, exact ⟨y, z, hy, hz, rfl⟩, }, { rintro ⟨y, z, hy, hz, rfl⟩, exact mul_dvd_mul hy hz }, end theorem gcd_mul_dvd_mul_gcd (k m n : ℕ) : gcd k (m * n) ∣ gcd k m * gcd k n := begin rcases (prod_dvd_and_dvd_of_dvd_prod $ gcd_dvd_right k (m * n)) with ⟨⟨⟨m', hm'⟩, ⟨n', hn'⟩⟩, h⟩, replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n', exact dvd_gcd hn'k hn' } end theorem coprime.gcd_mul (k : ℕ) {m n : ℕ} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := dvd_antisymm (gcd_mul_dvd_mul_gcd k m n) ((h.gcd_both k k).mul_dvd_of_dvd_of_dvd (gcd_dvd_gcd_mul_right_right _ _ _) (gcd_dvd_gcd_mul_left_right _ _ _)) theorem pow_dvd_pow_iff {a b n : ℕ} (n0 : 0 < n) : a ^ n ∣ b ^ n ↔ a ∣ b := begin refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩, cases nat.eq_zero_or_pos (gcd a b) with g0 g0, { simp [eq_zero_of_gcd_eq_zero_right g0] }, rcases exists_coprime' g0 with ⟨g, a', b', g0', co, rfl, rfl⟩, rw [mul_pow, mul_pow] at h, replace h := dvd_of_mul_dvd_mul_right (pow_pos g0' _) h, have := pow_dvd_pow a' n0, rw [pow_one, (co.pow n n).eq_one_of_dvd h] at this, simp [eq_one_of_dvd_one this] end lemma gcd_mul_gcd_of_coprime_of_mul_eq_mul {a b c d : ℕ} (cop : c.coprime d) (h : a * b = c * d) : a.gcd c * b.gcd c = c := begin apply dvd_antisymm, { apply nat.coprime.dvd_of_dvd_mul_right (nat.coprime.mul (cop.gcd_left _) (cop.gcd_left _)), rw ← h, apply mul_dvd_mul (gcd_dvd _ _).1 (gcd_dvd _ _).1 }, { rw [gcd_comm a _, gcd_comm b _], transitivity c.gcd (a * b), rw [h, gcd_mul_right_right d c], apply gcd_mul_dvd_mul_gcd } end /-- If `k:ℕ` divides coprime `a` and `b` then `k = 1` -/ lemma eq_one_of_dvd_coprimes {a b k : ℕ} (h_ab_coprime : coprime a b) (hka : k ∣ a) (hkb : k ∣ b) : k = 1 := begin rw coprime_iff_gcd_eq_one at h_ab_coprime, have h1 := dvd_gcd hka hkb, rw h_ab_coprime at h1, exact nat.dvd_one.mp h1, end lemma coprime.mul_add_mul_ne_mul {m n a b : ℕ} (cop : coprime m n) (ha : a ≠ 0) (hb : b ≠ 0) : a * m + b * n ≠ m * n := begin intro h, obtain ⟨x, rfl⟩ : n ∣ a := cop.symm.dvd_of_dvd_mul_right ((nat.dvd_add_iff_left (dvd_mul_left n b)).mpr ((congr_arg _ h).mpr (dvd_mul_left n m))), obtain ⟨y, rfl⟩ : m ∣ b := cop.dvd_of_dvd_mul_right ((nat.dvd_add_iff_right (dvd_mul_left m (n*x))).mpr ((congr_arg _ h).mpr (dvd_mul_right m n))), rw [mul_comm, mul_ne_zero_iff, ←one_le_iff_ne_zero] at ha hb, refine mul_ne_zero hb.2 ha.2 (eq_zero_of_mul_eq_self_left (ne_of_gt (add_le_add ha.1 hb.1)) _), rw [← mul_assoc, ← h, add_mul, add_mul, mul_comm _ n, ←mul_assoc, mul_comm y] end end nat
7bf62a078f8506f8938fa072968b1d6e47096c1e
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/nondegenerate.lean
5dfbf769f0461a0ca744723747ff0477428df3ef
[ "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
2,535
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.matrix.basic import linear_algebra.matrix.determinant import linear_algebra.matrix.adjugate /-! # Matrices associated with non-degenerate bilinear forms ## Main definitions * `matrix.nondegenerate A`: the proposition that when interpreted as a bilinear form, the matrix `A` is nondegenerate. -/ namespace matrix variables {m R A : Type*} [fintype m] [comm_ring R] /-- A matrix `M` is nondegenerate if for all `v ≠ 0`, there is a `w ≠ 0` with `w ⬝ M ⬝ v ≠ 0`. -/ def nondegenerate (M : matrix m m R) := ∀ v, (∀ w, matrix.dot_product v (mul_vec M w) = 0) → v = 0 /-- If `M` is nondegenerate and `w ⬝ M ⬝ v = 0` for all `w`, then `v = 0`. -/ lemma nondegenerate.eq_zero_of_ortho {M : matrix m m R} (hM : nondegenerate M) {v : m → R} (hv : ∀ w, matrix.dot_product v (mul_vec M w) = 0) : v = 0 := hM v hv /-- If `M` is nondegenerate and `v ≠ 0`, then there is some `w` such that `w ⬝ M ⬝ v ≠ 0`. -/ lemma nondegenerate.exists_not_ortho_of_ne_zero {M : matrix m m R} (hM : nondegenerate M) {v : m → R} (hv : v ≠ 0) : ∃ w, matrix.dot_product v (mul_vec M w) ≠ 0 := not_forall.mp (mt hM.eq_zero_of_ortho hv) variables [comm_ring A] [is_domain A] /-- If `M` has a nonzero determinant, then `M` as a bilinear form on `n → A` is nondegenerate. See also `bilin_form.nondegenerate_of_det_ne_zero'` and `bilin_form.nondegenerate_of_det_ne_zero`. -/ theorem nondegenerate_of_det_ne_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) : nondegenerate M := begin intros v hv, ext i, specialize hv (M.cramer (pi.single i 1)), refine (mul_eq_zero.mp _).resolve_right hM, convert hv, simp only [mul_vec_cramer M (pi.single i 1), dot_product, pi.smul_apply, smul_eq_mul], rw [finset.sum_eq_single i, pi.single_eq_same, mul_one], { intros j _ hj, simp [hj] }, { intros, have := finset.mem_univ i, contradiction } end theorem eq_zero_of_vec_mul_eq_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) {v : m → A} (hv : M.vec_mul v = 0) : v = 0 := (nondegenerate_of_det_ne_zero hM).eq_zero_of_ortho (λ w, by rw [dot_product_mul_vec, hv, zero_dot_product]) theorem eq_zero_of_mul_vec_eq_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) {v : m → A} (hv : M.mul_vec v = 0) : v = 0 := eq_zero_of_vec_mul_eq_zero (by rwa det_transpose) ((vec_mul_transpose M v).trans hv) end matrix
664efb41a6a83cb6f446141f8e0636bcb06a7864
64874bd1010548c7f5a6e3e8902efa63baaff785
/hott/init/util.hlean
4c36ab16e0f5e32564e7aedacfc0de44758e275e
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
521
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Auxiliary definitions used by automation -/ prelude import init.trunc open truncation definition eq_rec_eq.{l₁ l₂} {A : Type.{l₁}} {B : A → Type.{l₂}} [h : is_hset A] {a : A} (b : B a) (p : a = a) : b = @eq.rec.{l₂ l₁} A a (λ (a' : A) (h : a = a'), B a') b a p := eq.rec_on (is_hset.elim (eq.refl a) p) (eq.refl (eq.rec_on (eq.refl a) b))
a100c090861242e37178c7f52f7e4776e04ef1fd
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/witt_vector/structure_polynomial.lean
44075d444f732ed69ef8915b4fa08b1ad906550f
[ "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
18,516
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import data.fin.vec_notation import field_theory.finite.polynomial import number_theory.basic import ring_theory.witt_vector.witt_polynomial /-! # Witt structure polynomials In this file we prove the main theorem that makes the whole theory of Witt vectors work. Briefly, consider a polynomial `Φ : mv_polynomial idx ℤ` over the integers, with polynomials variables indexed by an arbitrary type `idx`. Then there exists a unique family of polynomials `φ : ℕ → mv_polynomial (idx × ℕ) Φ` such that for all `n : ℕ` we have (`witt_structure_int_exists_unique`) ``` bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℤ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. N.b.: As far as we know, these polynomials do not have a name in the literature, so we have decided to call them the “Witt structure polynomials”. See `witt_structure_int`. ## Special cases With the main result of this file in place, we apply it to certain special polynomials. For example, by taking `Φ = X tt + X ff` resp. `Φ = X tt * X ff` we obtain families of polynomials `witt_add` resp. `witt_mul` (with type `ℕ → mv_polynomial (bool × ℕ) ℤ`) that will be used in later files to define the addition and multiplication on the ring of Witt vectors. ## Outline of the proof The proof of `witt_structure_int_exists_unique` is rather technical, and takes up most of this file. We start by proving the analogous version for polynomials with rational coefficients, instead of integer coefficients. In this case, the solution is rather easy, since the Witt polynomials form a faithful change of coordinates in the polynomial ring `mv_polynomial ℕ ℚ`. We therefore obtain a family of polynomials `witt_structure_rat Φ` for every `Φ : mv_polynomial idx ℚ`. If `Φ` has integer coefficients, then the polynomials `witt_structure_rat Φ n` do so as well. Proving this claim is the essential core of this file, and culminates in `map_witt_structure_int`, which proves that upon mapping the coefficients of `witt_structure_int Φ n` from the integers to the rationals, one obtains `witt_structure_rat Φ n`. Ultimately, the proof of `map_witt_structure_int` relies on ``` dvd_sub_pow_of_dvd_sub {R : Type*} [comm_ring R] {p : ℕ} {a b : R} : (p : R) ∣ a - b → ∀ (k : ℕ), (p : R) ^ (k + 1) ∣ a ^ p ^ k - b ^ p ^ k ``` ## Main results * `witt_structure_rat Φ`: the family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℚ` associated with `Φ : mv_polynomial idx ℚ` and satisfying the property explained above. * `witt_structure_rat_prop`: the proof that `witt_structure_rat` indeed satisfies the property. * `witt_structure_int Φ`: the family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℤ` associated with `Φ : mv_polynomial idx ℤ` and satisfying the property explained above. * `map_witt_structure_int`: the proof that the integral polynomials `with_structure_int Φ` are equal to `witt_structure_rat Φ` when mapped to polynomials with rational coefficients. * `witt_structure_int_prop`: the proof that `witt_structure_int` indeed satisfies the property. * Five families of polynomials that will be used to define the ring structure on the ring of Witt vectors: - `witt_vector.witt_zero` - `witt_vector.witt_one` - `witt_vector.witt_add` - `witt_vector.witt_mul` - `witt_vector.witt_neg` (We also define `witt_vector.witt_sub`, and later we will prove that it describes subtraction, which is defined as `λ a b, a + -b`. See `witt_vector.sub_coeff` for this proof.) ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ open mv_polynomial open set open finset (range) open finsupp (single) -- This lemma reduces a bundled morphism to a "mere" function, -- and consequently the simplifier cannot use a lot of powerful simp-lemmas. -- We disable this locally, and probably it should be disabled globally in mathlib. local attribute [-simp] coe_eval₂_hom variables {p : ℕ} {R : Type*} {idx : Type*} [comm_ring R] open_locale witt open_locale big_operators section p_prime variables (p) [hp : fact p.prime] include hp /-- `witt_structure_rat Φ` is a family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℚ` that are uniquely characterised by the property that ``` bind₁ (witt_structure_rat p Φ) (witt_polynomial p ℚ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℚ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `witt_structure_rat Φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. See `witt_structure_rat_prop` for this property, and `witt_structure_rat_exists_unique` for the fact that `witt_structure_rat` gives the unique family of polynomials with this property. These polynomials turn out to have integral coefficients, but it requires some effort to show this. See `witt_structure_int` for the version with integral coefficients, and `map_witt_structure_int` for the fact that it is equal to `witt_structure_rat` when mapped to polynomials over the rationals. -/ noncomputable def witt_structure_rat (Φ : mv_polynomial idx ℚ) (n : ℕ) : mv_polynomial (idx × ℕ) ℚ := bind₁ (λ k, bind₁ (λ i, rename (prod.mk i) (W_ ℚ k)) Φ) (X_in_terms_of_W p ℚ n) theorem witt_structure_rat_prop (Φ : mv_polynomial idx ℚ) (n : ℕ) : bind₁ (witt_structure_rat p Φ) (W_ ℚ n) = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ := calc bind₁ (witt_structure_rat p Φ) (W_ ℚ n) = bind₁ (λ k, bind₁ (λ i, (rename (prod.mk i)) (W_ ℚ k)) Φ) (bind₁ (X_in_terms_of_W p ℚ) (W_ ℚ n)) : by { rw bind₁_bind₁, exact eval₂_hom_congr (ring_hom.ext_rat _ _) rfl rfl } ... = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ : by rw [bind₁_X_in_terms_of_W_witt_polynomial p _ n, bind₁_X_right] theorem witt_structure_rat_exists_unique (Φ : mv_polynomial idx ℚ) : ∃! (φ : ℕ → mv_polynomial (idx × ℕ) ℚ), ∀ (n : ℕ), bind₁ φ (W_ ℚ n) = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ := begin refine ⟨witt_structure_rat p Φ, _, _⟩, { intro n, apply witt_structure_rat_prop }, { intros φ H, funext n, rw show φ n = bind₁ φ (bind₁ (W_ ℚ) (X_in_terms_of_W p ℚ n)), { rw [bind₁_witt_polynomial_X_in_terms_of_W p, bind₁_X_right] }, rw [bind₁_bind₁], exact eval₂_hom_congr (ring_hom.ext_rat _ _) (funext H) rfl }, end lemma witt_structure_rat_rec_aux (Φ : mv_polynomial idx ℚ) (n : ℕ) : witt_structure_rat p Φ n * C (p ^ n : ℚ) = bind₁ (λ b, rename (λ i, (b, i)) (W_ ℚ n)) Φ - ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p Φ i) ^ p ^ (n - i) := begin have := X_in_terms_of_W_aux p ℚ n, replace := congr_arg (bind₁ (λ k : ℕ, bind₁ (λ i, rename (prod.mk i) (W_ ℚ k)) Φ)) this, rw [alg_hom.map_mul, bind₁_C_right] at this, rw [witt_structure_rat, this], clear this, conv_lhs { simp only [alg_hom.map_sub, bind₁_X_right] }, rw sub_right_inj, simp only [alg_hom.map_sum, alg_hom.map_mul, bind₁_C_right, alg_hom.map_pow], refl end /-- Write `witt_structure_rat p φ n` in terms of `witt_structure_rat p φ i` for `i < n`. -/ lemma witt_structure_rat_rec (Φ : mv_polynomial idx ℚ) (n : ℕ) : (witt_structure_rat p Φ n) = C (1 / p ^ n : ℚ) * (bind₁ (λ b, (rename (λ i, (b, i)) (W_ ℚ n))) Φ - ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p Φ i) ^ p ^ (n - i)) := begin calc witt_structure_rat p Φ n = C (1 / p ^ n : ℚ) * (witt_structure_rat p Φ n * C (p ^ n : ℚ)) : _ ... = _ : by rw witt_structure_rat_rec_aux, rw [mul_left_comm, ← C_mul, div_mul_cancel, C_1, mul_one], exact pow_ne_zero _ (nat.cast_ne_zero.2 hp.1.ne_zero), end /-- `witt_structure_int Φ` is a family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℤ` that are uniquely characterised by the property that ``` bind₁ (witt_structure_int p Φ) (witt_polynomial p ℤ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℤ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `witt_structure_int Φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. See `witt_structure_int_prop` for this property, and `witt_structure_int_exists_unique` for the fact that `witt_structure_int` gives the unique family of polynomials with this property. -/ noncomputable def witt_structure_int (Φ : mv_polynomial idx ℤ) (n : ℕ) : mv_polynomial (idx × ℕ) ℤ := finsupp.map_range rat.num (rat.coe_int_num 0) (witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) n) variable {p} lemma bind₁_rename_expand_witt_polynomial (Φ : mv_polynomial idx ℤ) (n : ℕ) (IH : ∀ m : ℕ, m < (n + 1) → map (int.cast_ring_hom ℚ) (witt_structure_int p Φ m) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) m) : bind₁ (λ b, rename (λ i, (b, i)) (expand p (W_ ℤ n))) Φ = bind₁ (λ i, expand p (witt_structure_int p Φ i)) (W_ ℤ n) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_bind₁, map_rename, map_expand, rename_expand, map_witt_polynomial], have key := (witt_structure_rat_prop p (map (int.cast_ring_hom ℚ) Φ) n).symm, apply_fun expand p at key, simp only [expand_bind₁] at key, rw key, clear key, apply eval₂_hom_congr' rfl _ rfl, rintro i hi -, rw [witt_polynomial_vars, finset.mem_range] at hi, simp only [IH i hi], end lemma C_p_pow_dvd_bind₁_rename_witt_polynomial_sub_sum (Φ : mv_polynomial idx ℤ) (n : ℕ) (IH : ∀ m : ℕ, m < n → map (int.cast_ring_hom ℚ) (witt_structure_int p Φ m) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) m) : C ↑(p ^ n) ∣ (bind₁ (λ (b : idx), rename (λ i, (b, i)) (witt_polynomial p ℤ n)) Φ - ∑ i in range n, C (↑p ^ i) * witt_structure_int p Φ i ^ p ^ (n - i)) := begin cases n, { simp only [is_unit_one, int.coe_nat_zero, int.coe_nat_succ, zero_add, pow_zero, C_1, is_unit.dvd] }, -- prepare a useful equation for rewriting have key := bind₁_rename_expand_witt_polynomial Φ n IH, apply_fun (map (int.cast_ring_hom (zmod (p ^ (n + 1))))) at key, conv_lhs at key { simp only [map_bind₁, map_rename, map_expand, map_witt_polynomial] }, -- clean up and massage rw [nat.succ_eq_add_one, C_dvd_iff_zmod, ring_hom.map_sub, sub_eq_zero, map_bind₁], simp only [map_rename, map_witt_polynomial, witt_polynomial_zmod_self], rw key, clear key IH, rw [bind₁, aeval_witt_polynomial, ring_hom.map_sum, ring_hom.map_sum, finset.sum_congr rfl], intros k hk, rw [finset.mem_range, nat.lt_succ_iff] at hk, simp only [← sub_eq_zero, ← ring_hom.map_sub, ← C_dvd_iff_zmod, C_eq_coe_nat, ← mul_sub, ← nat.cast_pow], rw show p ^ (n + 1) = p ^ k * p ^ (n - k + 1), { rw [← pow_add, ←add_assoc], congr' 2, rw [add_comm, ←tsub_eq_iff_eq_add_of_le hk] }, rw [nat.cast_mul, nat.cast_pow, nat.cast_pow], apply mul_dvd_mul_left, rw show p ^ (n + 1 - k) = p * p ^ (n - k), { rw [← pow_succ, ← tsub_add_eq_add_tsub hk] }, rw [pow_mul], -- the machine! apply dvd_sub_pow_of_dvd_sub, rw [← C_eq_coe_nat, C_dvd_iff_zmod, ring_hom.map_sub, sub_eq_zero, map_expand, ring_hom.map_pow, mv_polynomial.expand_zmod], end variables (p) @[simp] lemma map_witt_structure_int (Φ : mv_polynomial idx ℤ) (n : ℕ) : map (int.cast_ring_hom ℚ) (witt_structure_int p Φ n) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) n := begin apply nat.strong_induction_on n, clear n, intros n IH, rw [witt_structure_int, map_map_range_eq_iff, int.coe_cast_ring_hom], intro c, rw [witt_structure_rat_rec, coeff_C_mul, mul_comm, mul_div_assoc', mul_one], have sum_induction_steps : map (int.cast_ring_hom ℚ) (∑ i in range n, C (p ^ i : ℤ) * (witt_structure_int p Φ i) ^ p ^ (n - i)) = ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) i) ^ p ^ (n - i), { rw [ring_hom.map_sum], apply finset.sum_congr rfl, intros i hi, rw finset.mem_range at hi, simp only [IH i hi, ring_hom.map_mul, ring_hom.map_pow, map_C], refl }, simp only [← sum_induction_steps, ← map_witt_polynomial p (int.cast_ring_hom ℚ), ← map_rename, ← map_bind₁, ← ring_hom.map_sub, coeff_map], rw show (p : ℚ)^n = ((p^n : ℕ) : ℤ), by norm_cast, rw [← rat.denom_eq_one_iff, eq_int_cast, rat.denom_div_cast_eq_one_iff], swap, { exact_mod_cast pow_ne_zero n hp.1.ne_zero }, revert c, rw [← C_dvd_iff_dvd_coeff], exact C_p_pow_dvd_bind₁_rename_witt_polynomial_sub_sum Φ n IH, end variables (p) theorem witt_structure_int_prop (Φ : mv_polynomial idx ℤ) (n) : bind₁ (witt_structure_int p Φ) (witt_polynomial p ℤ n) = bind₁ (λ i, rename (prod.mk i) (W_ ℤ n)) Φ := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, have := witt_structure_rat_prop p (map (int.cast_ring_hom ℚ) Φ) n, simpa only [map_bind₁, ← eval₂_hom_map_hom, eval₂_hom_C_left, map_rename, map_witt_polynomial, alg_hom.coe_to_ring_hom, map_witt_structure_int], end lemma eq_witt_structure_int (Φ : mv_polynomial idx ℤ) (φ : ℕ → mv_polynomial (idx × ℕ) ℤ) (h : ∀ n, bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i, rename (prod.mk i) (W_ ℤ n)) Φ) : φ = witt_structure_int p Φ := begin funext k, apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, rw map_witt_structure_int, refine congr_fun _ k, apply unique_of_exists_unique (witt_structure_rat_exists_unique p (map (int.cast_ring_hom ℚ) Φ)), { intro n, specialize h n, apply_fun map (int.cast_ring_hom ℚ) at h, simpa only [map_bind₁, ← eval₂_hom_map_hom, eval₂_hom_C_left, map_rename, map_witt_polynomial, alg_hom.coe_to_ring_hom] using h, }, { intro n, apply witt_structure_rat_prop } end theorem witt_structure_int_exists_unique (Φ : mv_polynomial idx ℤ) : ∃! (φ : ℕ → mv_polynomial (idx × ℕ) ℤ), ∀ (n : ℕ), bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i : idx, (rename (prod.mk i) (W_ ℤ n))) Φ := ⟨witt_structure_int p Φ, witt_structure_int_prop _ _, eq_witt_structure_int _ _⟩ theorem witt_structure_prop (Φ : mv_polynomial idx ℤ) (n) : aeval (λ i, map (int.cast_ring_hom R) (witt_structure_int p Φ i)) (witt_polynomial p ℤ n) = aeval (λ i, rename (prod.mk i) (W n)) Φ := begin convert congr_arg (map (int.cast_ring_hom R)) (witt_structure_int_prop p Φ n) using 1; rw hom_bind₁; apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, { refl }, { simp only [map_rename, map_witt_polynomial] } end lemma witt_structure_int_rename {σ : Type*} (Φ : mv_polynomial idx ℤ) (f : idx → σ) (n : ℕ) : witt_structure_int p (rename f Φ) n = rename (prod.map f id) (witt_structure_int p Φ n) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_rename, map_witt_structure_int, witt_structure_rat, rename_bind₁, rename_rename, bind₁_rename], refl end @[simp] lemma constant_coeff_witt_structure_rat_zero (Φ : mv_polynomial idx ℚ) : constant_coeff (witt_structure_rat p Φ 0) = constant_coeff Φ := by simp only [witt_structure_rat, bind₁, map_aeval, X_in_terms_of_W_zero, constant_coeff_rename, constant_coeff_witt_polynomial, aeval_X, constant_coeff_comp_algebra_map, eval₂_hom_zero'_apply, ring_hom.id_apply] lemma constant_coeff_witt_structure_rat (Φ : mv_polynomial idx ℚ) (h : constant_coeff Φ = 0) (n : ℕ) : constant_coeff (witt_structure_rat p Φ n) = 0 := by simp only [witt_structure_rat, eval₂_hom_zero'_apply, h, bind₁, map_aeval, constant_coeff_rename, constant_coeff_witt_polynomial, constant_coeff_comp_algebra_map, ring_hom.id_apply, constant_coeff_X_in_terms_of_W] @[simp] lemma constant_coeff_witt_structure_int_zero (Φ : mv_polynomial idx ℤ) : constant_coeff (witt_structure_int p Φ 0) = constant_coeff Φ := begin have inj : function.injective (int.cast_ring_hom ℚ), { intros m n, exact int.cast_inj.mp, }, apply inj, rw [← constant_coeff_map, map_witt_structure_int, constant_coeff_witt_structure_rat_zero, constant_coeff_map], end lemma constant_coeff_witt_structure_int (Φ : mv_polynomial idx ℤ) (h : constant_coeff Φ = 0) (n : ℕ) : constant_coeff (witt_structure_int p Φ n) = 0 := begin have inj : function.injective (int.cast_ring_hom ℚ), { intros m n, exact int.cast_inj.mp, }, apply inj, rw [← constant_coeff_map, map_witt_structure_int, constant_coeff_witt_structure_rat, ring_hom.map_zero], rw [constant_coeff_map, h, ring_hom.map_zero], end variable (R) -- we could relax the fintype on `idx`, but then we need to cast from finset to set. -- for our applications `idx` is always finite. lemma witt_structure_rat_vars [fintype idx] (Φ : mv_polynomial idx ℚ) (n : ℕ) : (witt_structure_rat p Φ n).vars ⊆ finset.univ ×ˢ finset.range (n + 1) := begin rw witt_structure_rat, intros x hx, simp only [finset.mem_product, true_and, finset.mem_univ, finset.mem_range], obtain ⟨k, hk, hx'⟩ := mem_vars_bind₁ _ _ hx, obtain ⟨i, -, hx''⟩ := mem_vars_bind₁ _ _ hx', obtain ⟨j, hj, rfl⟩ := mem_vars_rename _ _ hx'', rw [witt_polynomial_vars, finset.mem_range] at hj, replace hk := X_in_terms_of_W_vars_subset p _ hk, rw finset.mem_range at hk, exact lt_of_lt_of_le hj hk, end -- we could relax the fintype on `idx`, but then we need to cast from finset to set. -- for our applications `idx` is always finite. lemma witt_structure_int_vars [fintype idx] (Φ : mv_polynomial idx ℤ) (n : ℕ) : (witt_structure_int p Φ n).vars ⊆ finset.univ ×ˢ finset.range (n + 1) := begin have : function.injective (int.cast_ring_hom ℚ) := int.cast_injective, rw [← vars_map_of_injective _ this, map_witt_structure_int], apply witt_structure_rat_vars, end end p_prime
b93e3b7a6b6a57df3e460ffcec7689ca85b7ef9d
9028d228ac200bbefe3a711342514dd4e4458bff
/src/algebra/monoid_algebra.lean
3df528d69b642d20b9449fa3ad8d0eeeb95e9bc3
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,670
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, Yury G. Kudryashov, Scott Morrison -/ import algebra.algebra.basic /-! # Monoid algebras When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynomial σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale classical big_operators open finset finsupp universes u₁ u₂ u₃ variables (k : Type u₁) (G : Type u₂) /-! ### Multiplicative monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k end namespace monoid_algebra variables {k G} /-! #### Semiring structure -/ section semiring variables [semiring k] [monoid G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], .. finsupp.add_comm_monoid } end semiring instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.semiring } /-! #### Derived instances -/ section derived_instances instance [ring k] : add_group (monoid_algebra k G) := finsupp.add_group instance [ring k] [monoid G] : ring (monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. monoid_algebra.semiring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { mul_comm := mul_comm, .. monoid_algebra.ring} instance {R : Type*} [semiring R] [semiring k] [semimodule R k] : has_scalar R (monoid_algebra k G) := finsupp.has_scalar instance {R : Type*} [semiring R] [semiring k] [semimodule R k] : semimodule R (monoid_algebra k G) := finsupp.semimodule G k instance [group G] [semiring k] : distrib_mul_action G (monoid_algebra k G) := finsupp.comap_distrib_mul_action_self end derived_instances section misc_theorems variables [semiring k] [monoid G] local attribute [reducible] monoid_algebra lemma mul_apply (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma mul_apply_antidiagonal (f g : monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) : mul_apply f g x ... = ∑ p in f.support.product g.support, F p : finset.sum_product.symm ... = ∑ p in (f.support.product g.support).filter (λ p : G × G, p.1 * p.2 = x), f p.1 * g p.2 : (finset.sum_filter _ _).symm ... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 : sum_congr (by { ext, simp only [mem_filter, mem_product, hs, and_comm] }) (λ _ _, rfl) ... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _) $ λ p hps hp, begin simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢, by_cases h1 : f p.1 = 0, { rw [h1, zero_mul] }, { rw [hp hps h1, mul_zero] } end lemma support_mul (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset @[simp] lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [mul_zero, single_zero])) @[simp] lemma single_pow {a : G} {b : k} : ∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n) | 0 := rfl | (n+1) := by simp only [pow_succ, single_pow n, single_mul_single] section variables (k G) /-- Embedding of a monoid into its monoid algebra. -/ def of : G →* monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by rw [single_mul_single, one_mul] } end @[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl lemma mul_single_apply_aux (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, a * x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) = ite (a₁ * x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_one_apply (f : monoid_algebra k G) (r : k) (x : G) : (f * single 1 r) x = f x * r := f.mul_single_apply_aux $ λ a, by rw [mul_one] lemma single_mul_apply_aux (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, x * a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr' with g s, split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_one_mul_apply (f : monoid_algebra k G) (r : k) (x : G) : (single 1 r * f) x = r * f x := f.single_mul_apply_aux $ λ a, by rw [one_mul] end misc_theorems /-! #### Algebra structure -/ section algebra local attribute [reducible] monoid_algebra lemma single_one_comm [comm_semiring k] [monoid G] (r : k) (f : monoid_algebra k G) : single 1 r * f = f * single 1 r := by { ext, rw [single_one_mul_apply, mul_single_one_apply, mul_comm] } /-- `finsupp.single 1` as a `ring_hom` -/ @[simps] def single_one_ring_hom [semiring k] [monoid G] : k →+* monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, one_mul], ..finsupp.single_add_hom 1} /-- The instance `algebra k (monoid_algebra A G)` whenever we have `algebra k A`. In particular this provides the instance `algebra k (monoid_algebra k G)`. -/ instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : algebra k (monoid_algebra A G) := { smul_def' := λ r a, by { ext, simp [single_one_mul_apply, algebra.smul_def''], }, commutes' := λ r f, by { ext, simp [single_one_mul_apply, mul_single_one_apply, algebra.commutes], }, ..single_one_ring_hom.comp (algebra_map k A) } /-- `finsupp.single 1` as a `alg_hom` -/ @[simps] def single_one_alg_hom {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : A →ₐ[k] monoid_algebra A G := { commutes' := λ r, by { ext, simp, refl, }, ..single_one_ring_hom} @[simp] lemma coe_algebra_map {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : (algebra_map k (monoid_algebra A G) : k → monoid_algebra A G) = single 1 ∘ (algebra_map k A) := rfl lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) : single a b = (algebra_map k (monoid_algebra k G) : k → monoid_algebra k G) b * of k G a := by simp lemma single_algebra_map_eq_algebra_map_mul_of {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] (a : G) (b : k) : single a (algebra_map k A b) = (algebra_map k (monoid_algebra A G) : k → monoid_algebra A G) b * of A G a := by simp end algebra section lift variables (k G) [comm_semiring k] [monoid G] (A : Type u₃) [semiring A] [algebra k A] local attribute [reducible] monoid_algebra /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (G →* A) ≃ (monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : monoid_algebra k G →* A).comp (of k G), to_fun := λ F, { to_fun := λ f, f.sum (λ a b, b • F a), map_one' := by { rw [one_def, sum_single_index, one_smul, F.map_one], apply zero_smul }, map_mul' := λ f g, begin rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index], work_on_goal 1 { intros, rw zero_smul, }, work_on_goal 1 { intros, rw add_smul, }, refine finset.sum_congr rfl (λ a ha, _), simp only, rw [finsupp.mul_sum, finsupp.sum_sum_index], work_on_goal 1 { intros, rw zero_smul, }, work_on_goal 1 { intros, rw add_smul, }, refine finset.sum_congr rfl (λ a' ha', _), simp only, rw [sum_single_index, F.map_mul, algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_smul, mul_comm], apply zero_smul, end, map_zero' := sum_zero_index, map_add' := λ f g, begin rw [sum_add_index], { intros, rw zero_smul, }, { intros, rw add_smul, }, end, commutes' := λ r, begin rw [coe_algebra_map, sum_single_index, F.map_one, algebra.smul_def, mul_one, algebra.id.map_eq_self], apply zero_smul end, }, left_inv := λ f, begin ext x, simp [sum_single_index] end, right_inv := λ F, begin ext f, conv_rhs { rw ← f.sum_single }, simp [← F.map_smul, finsupp.sum, ← F.map_sum] end } variables {k G A} lemma lift_apply (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F a) := rfl @[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] A) (x : G) : (lift k G A).symm F x = F (single x 1) := rfl lemma lift_of (F : G →* A) (x) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : G →* A) (a b) : lift k G A F (single a b) = b • F a := by rw [single_eq_algebra_map_mul_of, ← algebra.smul_def, alg_hom.map_smul, lift_of] lemma lift_unique' (F : monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ -- @[ext] -- FIXME I would really like to make this an `ext` lemma, but it seems to cause `ext` to loop. lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := (lift k G A).symm.injective $ monoid_hom.ext h end lift section local attribute [reducible] monoid_algebra variables (k) /-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/ def group_smul.linear_map [group G] [comm_ring k] (V : Type u₃) [add_comm_group V] [module (monoid_algebra k G) V] (g : G) : (semimodule.restrict_scalars k (monoid_algebra k G) V) →ₗ[k] (semimodule.restrict_scalars k (monoid_algebra k G) V) := { to_fun := λ v, (single g (1 : k) • v : V), map_add' := λ x y, smul_add (single g (1 : k)) x y, map_smul' := λ c x, by simp only [semimodule.restrict_scalars_smul_def, coe_algebra_map, ←mul_smul, single_one_comm], }. @[simp] lemma group_smul.linear_map_apply [group G] [comm_ring k] (V : Type u₃) [add_comm_group V] [module (monoid_algebra k G) V] (g : G) (v : V) : (group_smul.linear_map k V g) v = (single g (1 : k) • v : V) := rfl section variables {k} variables [group G] [comm_ring k] {V : Type u₃} {gV : add_comm_group V} {mV : module (monoid_algebra k G) V} {W : Type u₃} {gW : add_comm_group W} {mW : module (monoid_algebra k G) W} (f : (semimodule.restrict_scalars k (monoid_algebra k G) V) →ₗ[k] (semimodule.restrict_scalars k (monoid_algebra k G) W)) (h : ∀ (g : G) (v : V), f (single g (1 : k) • v : V) = (single g (1 : k) • (f v) : W)) include h /-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/ def equivariant_of_linear_of_comm : V →ₗ[monoid_algebra k G] W := { to_fun := f, map_add' := λ v v', by simp, map_smul' := λ c v, begin apply finsupp.induction c, { simp, }, { intros g r c' nm nz w, rw [add_smul, linear_map.map_add, w, add_smul, add_left_inj, single_eq_algebra_map_mul_of, ←smul_smul, ←smul_smul], erw [f.map_smul, h g v], refl, } end, } @[simp] lemma equivariant_of_linear_of_comm_apply (v : V) : (equivariant_of_linear_of_comm f h) v = f v := rfl end end section universe ui variable {ι : Type ui} local attribute [reducible] monoid_algebra lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∏ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, prod_insert has, prod_insert has] end section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] local attribute [reducible] monoid_algebra @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) := calc (f * g) x = sum f (λ a b, (single a b * g) x) : by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single] ... = _ : by simp only [single_mul_apply, finsupp.sum] -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) := calc (f * g) x = sum g (λ a b, (f * single a b) x) : by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single] ... = _ : by simp only [mul_single_apply, finsupp.sum] end end monoid_algebra /-! ### Additive monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def add_monoid_algebra := G →₀ k end namespace add_monoid_algebra variables {k G} /-! #### Semiring structure -/ section semiring variables [semiring k] [add_monoid G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], .. finsupp.add_comm_monoid } end semiring instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [add_comm] end, .. add_monoid_algebra.semiring } /-! #### Derived instances -/ section derived_instances instance [ring k] : add_group (add_monoid_algebra k G) := finsupp.add_group instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { mul_comm := mul_comm, .. add_monoid_algebra.ring} variables {R : Type*} instance [semiring R] [semiring k] [semimodule R k] : has_scalar R (add_monoid_algebra k G) := finsupp.has_scalar instance [semiring R] [semiring k] [semimodule R k] : semimodule R (add_monoid_algebra k G) := finsupp.semimodule G k /-! It is hard to state the equivalent of `distrib_mul_action G (add_monoid_algebra k G)` because we've never discussed actions of additive groups. -/ end derived_instances section misc_theorems variables [semiring k] [add_monoid G] local attribute [reducible] add_monoid_algebra lemma mul_apply (f g : add_monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma support_mul (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : add_monoid_algebra k G) * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := (sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [mul_zero, single_zero])) section variables (k G) /-- Embedding of a monoid into its monoid algebra. -/ def of : multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by { rw [single_mul_single, one_mul], refl } } end @[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl lemma mul_single_apply_aux (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, a + x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ + a₂ = z) (b₁ * b₂) 0) = ite (a₁ + x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_zero_apply (f : add_monoid_algebra k G) (r : k) (x : G) : (f * single 0 r) x = f x * r := f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero] lemma single_mul_apply_aux (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, x + a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x + a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x + a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr' with g s, split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_zero_mul_apply (f : add_monoid_algebra k G) (r : k) (x : G) : (single 0 r * f) x = r * f x := f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add] end misc_theorems /-! #### Algebra structure -/ section algebra variables {R : Type*} local attribute [reducible] add_monoid_algebra /-- `finsupp.single 0` as a `ring_hom` -/ @[simps] def single_zero_ring_hom [semiring k] [add_monoid G] : k →+* add_monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, zero_add], ..finsupp.single_add_hom 0} /-- The instance `algebra R (add_monoid_algebra k G)` whenever we have `algebra R k`. In particular this provides the instance `algebra k (add_monoid_algebra k G)`. -/ instance [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : algebra R (add_monoid_algebra k G) := { smul_def' := λ r a, by { ext, simp [single_zero_mul_apply, algebra.smul_def''], }, commutes' := λ r f, by { ext, simp [single_zero_mul_apply, mul_single_zero_apply, algebra.commutes], }, ..single_zero_ring_hom.comp (algebra_map R k) } /-- `finsupp.single 0` as a `alg_hom` -/ @[simps] def single_zero_alg_hom [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : k →ₐ[R] add_monoid_algebra k G := { commutes' := λ r, by { ext, simp, refl, }, ..single_zero_ring_hom} @[simp] lemma coe_algebra_map [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : (algebra_map R (add_monoid_algebra k G) : R → add_monoid_algebra k G) = single 0 ∘ (algebra_map R k) := rfl end algebra section lift /-- Any monoid homomorphism `multiplicative G →* A` can be lifted to an algebra homomorphism `add_monoid_algebra k G →ₐ[k] A`. -/ def lift [comm_semiring k] [add_monoid G] {A : Type u₃} [semiring A] [algebra k A] : (multiplicative G →* A) ≃ (add_monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, ((f : add_monoid_algebra k G →+* A) : add_monoid_algebra k G →* A).comp (of k G), to_fun := λ F, { -- The proofs here are almost identical to `monoid_algebra.lift`, but use `erw` instead of `rw` -- to unfold `multiplicative` to_fun := λ f, f.sum (λ a b, b • F a), map_one' := by { rw [one_def, sum_single_index, one_smul], erw [F.map_one], apply zero_smul }, map_mul' := λ f g, begin rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index], work_on_goal 1 { intros, rw zero_smul, }, work_on_goal 1 { intros, rw add_smul, }, refine finset.sum_congr rfl (λ a ha, _), simp only, rw [finsupp.mul_sum, finsupp.sum_sum_index], work_on_goal 1 { intros, rw zero_smul, }, work_on_goal 1 { intros, rw add_smul, }, refine finset.sum_congr rfl (λ a' ha', _), simp only, rw [sum_single_index], erw [F.map_mul], rw [algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_smul, mul_comm], apply zero_smul, end, map_zero' := sum_zero_index, map_add' := λ f g, begin rw [sum_add_index], { intros, rw zero_smul, }, { intros, rw add_smul, }, end, commutes' := λ r, begin rw [coe_algebra_map, sum_single_index], erw [F.map_one], rw [algebra.smul_def, mul_one, algebra.id.map_eq_self], apply zero_smul end, }, left_inv := λ f, begin ext x, simp [sum_single_index] end, right_inv := λ F, begin ext f, conv_rhs { rw ← f.sum_single }, simp [← F.map_smul, finsupp.sum, ← F.map_sum] end } lemma alg_hom_ext {A : Type u₃} [comm_semiring k] [add_monoid G] [semiring A] [algebra k A] ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (finsupp.single x 1) = φ₂ (finsupp.single x 1)) : φ₁ = φ₂ := lift.symm.injective $ by {ext, apply h} lemma alg_hom_ext_iff {A : Type u₃} [comm_semiring k] [add_monoid G] [semiring A] [algebra k A] ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ : (∀ x, φ₁ (finsupp.single x 1) = φ₂ (finsupp.single x 1)) ↔ φ₁ = φ₂ := ⟨λ h, alg_hom_ext h, by rintro rfl _; refl⟩ end lift section local attribute [reducible] add_monoid_algebra universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∑ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] end end add_monoid_algebra
098e53aa8fd27e0b07fbfc3ff239ff47f16508f2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/tactic/simps.lean
fc4a67b1a725afcba76de0f0285a1ede023a6248
[ "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
46,681
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import tactic.protected import tactic.to_additive /-! # simps attribute This file defines the `@[simps]` attribute, to automatically generate `simp` lemmas reducing a definition when projections are applied to it. ## Implementation Notes There are three attributes being defined here * `@[simps]` is the attribute for objects of a structure or instances of a class. It will automatically generate simplification lemmas for each projection of the object/instance that contains data. See the doc strings for `simps_attr` and `simps_cfg` for more details and configuration options. * `@[_simps_str]` is automatically added to structures that have been used in `@[simps]` at least once. This attribute contains the data of the projections used for this structure by all following invocations of `@[simps]`. * `@[notation_class]` should be added to all classes that define notation, like `has_mul` and `has_zero`. This specifies that the projections that `@[simps]` used are the projections from these notation classes instead of the projections of the superclasses. Example: if `has_mul` is tagged with `@[notation_class]` then the projection used for `semigroup` will be `λ α hα, @has_mul.mul α (@semigroup.to_has_mul α hα)` instead of `@semigroup.mul`. ## Tags structures, projections, simp, simplifier, generates declarations -/ open tactic expr option sum setup_tactic_parser declare_trace simps.verbose declare_trace simps.debug /-- Projection data for a single projection of a structure, consisting of the following fields: - the name used in the generated `simp` lemmas - an expression used by simps for the projection. It must be definitionally equal to an original projection (or a composition of multiple projections). These expressions can contain the universe parameters specified in the first argument of `simps_str_attr`. - a list of natural numbers, which is the projection number(s) that have to be applied to the expression. For example the list `[0, 1]` corresponds to applying the first projection of the structure, and then the second projection of the resulting structure (this assumes that the target of the first projection is a structure with at least two projections). The composition of these projections is required to be definitionally equal to the provided expression. - A boolean specifying whether `simp` lemmas are generated for this projection by default. - A boolean specifying whether this projection is written as prefix. -/ @[protect_proj, derive [has_reflect, inhabited]] meta structure projection_data := (name : name) (expr : expr) (proj_nrs : list ℕ) (is_default : bool) (is_prefix : bool) /-- Temporary projection data parsed from `initialize_simps_projections` before the expression matching this projection has been found. Only used internally in `simps_get_raw_projections`. -/ meta structure parsed_projection_data := (orig_name : name) -- name for this projection used in the structure definition (new_name : name) -- name for this projection used in the generated `simp` lemmas (is_default : bool) (is_prefix : bool) section open format meta instance : has_to_tactic_format projection_data := ⟨λ ⟨a, b, c, d, e⟩, (λ x, group $ nest 1 $ to_fmt "⟨" ++ to_fmt a ++ to_fmt "," ++ line ++ x ++ to_fmt "," ++ line ++ to_fmt c ++ to_fmt "," ++ line ++ to_fmt d ++ to_fmt "," ++ line ++ to_fmt e ++ to_fmt "⟩") <$> pp b⟩ meta instance : has_to_format parsed_projection_data := ⟨λ ⟨a, b, c, d⟩, group $ nest 1 $ to_fmt "⟨" ++ to_fmt a ++ to_fmt "," ++ line ++ to_fmt b ++ to_fmt "," ++ line ++ to_fmt c ++ to_fmt "," ++ line ++ to_fmt d ++ to_fmt "⟩"⟩ end /-- The type of rules that specify how metadata for projections in changes. See `initialize_simps_projection`. -/ abbreviation projection_rule := (name × name ⊕ name) × bool /-- The `@[_simps_str]` attribute specifies the preferred projections of the given structure, used by the `@[simps]` attribute. - This will usually be tagged by the `@[simps]` tactic. - You can also generate this with the command `initialize_simps_projections`. - To change the default value, see Note [custom simps projection]. - You are strongly discouraged to add this attribute manually. - The first argument is the list of names of the universe variables used in the structure - The second argument is a list that consists of the projection data for each projection. -/ @[user_attribute] meta def simps_str_attr : user_attribute unit (list name × list projection_data) := { name := `_simps_str, descr := "An attribute specifying the projection of the given structure.", parser := failed } /-- The `@[notation_class]` attribute specifies that this is a notation class, and this notation should be used instead of projections by @[simps]. * The first argument `tt` for notation classes and `ff` for classes applied to the structure, like `has_coe_to_sort` and `has_coe_to_fun` * The second argument is the name of the projection (by default it is the first projection of the structure) -/ @[user_attribute] meta def notation_class_attr : user_attribute unit (bool × option name) := { name := `notation_class, descr := "An attribute specifying that this is a notation class. Used by @[simps].", parser := prod.mk <$> (option.is_none <$> (tk "*")?) <*> ident? } attribute [notation_class] has_zero has_one has_add has_mul has_inv has_neg has_sub has_div has_dvd has_mod has_le has_lt has_append has_andthen has_union has_inter has_sdiff has_equiv has_subset has_ssubset has_emptyc has_insert has_singleton has_sep has_mem has_pow attribute [notation_class* coe_sort] has_coe_to_sort attribute [notation_class* coe_fn] has_coe_to_fun /-- Returns the projection information of a structure. -/ meta def projections_info (l : list projection_data) (pref : string) (str : name) : tactic format := do ⟨defaults, nondefaults⟩ ← return $ l.partition_map $ λ s, if s.is_default then inl s else inr s, to_print ← defaults.mmap $ λ s, to_string <$> let prefix_str := if s.is_prefix then "(prefix) " else "" in pformat!"Projection {prefix_str}{s.name}: {s.expr}", let print2 := string.join $ (nondefaults.map (λ nm : projection_data, to_string nm.1)).intersperse ", ", let to_print := to_print ++ if nondefaults.length = 0 then [] else ["No lemmas are generated for the projections: " ++ print2 ++ "."], let to_print := string.join $ to_print.intersperse "\n > ", return format!"[simps] > {pref} {str}:\n > {to_print}" /-- Auxiliary function of `get_composite_of_projections`. -/ meta def get_composite_of_projections_aux : Π (str : name) (proj : string) (x : expr) (pos : list ℕ) (args : list expr), tactic (expr × list ℕ) | str proj x pos args := do e ← get_env, projs ← e.structure_fields str, let proj_info := projs.map_with_index $ λ n p, (λ x, (x, n, p)) <$> proj.get_rest ("_" ++ p.last), when (proj_info.filter_map id = []) $ fail!"Failed to find constructor {proj.popn 1} in structure {str}.", (proj_rest, index, proj_nm) ← return (proj_info.filter_map id).ilast, str_d ← e.get str, let proj_e : expr := const (str ++ proj_nm) str_d.univ_levels, proj_d ← e.get (str ++ proj_nm), type ← infer_type x, let params := get_app_args type, let univs := proj_d.univ_params.zip type.get_app_fn.univ_levels, let new_x := (proj_e.instantiate_univ_params univs).mk_app $ params ++ [x], let new_pos := pos ++ [index], if proj_rest.is_empty then return (new_x.lambdas args, new_pos) else do type ← infer_type new_x, (type_args, tgt) ← open_pis_whnf type, let new_str := tgt.get_app_fn.const_name, get_composite_of_projections_aux new_str proj_rest (new_x.mk_app type_args) new_pos (args ++ type_args) /-- Given a structure `str` and a projection `proj`, that could be multiple nested projections (separated by `_`), returns an expression that is the composition of these projections and a list of natural numbers, that are the projection numbers of the applied projections. -/ meta def get_composite_of_projections (str : name) (proj : string) : tactic (expr × list ℕ) := do e ← get_env, str_d ← e.get str, let str_e : expr := const str str_d.univ_levels, type ← infer_type str_e, (type_args, tgt) ← open_pis_whnf type, let str_ap := str_e.mk_app type_args, x ← mk_local' `x binder_info.default str_ap, get_composite_of_projections_aux str ("_" ++ proj) x [] $ type_args ++ [x] /-- Get the projections used by `simps` associated to a given structure `str`. The returned information is also stored in a parameter of the attribute `@[_simps_str]`, which is given to `str`. If `str` already has this attribute, the information is read from this attribute instead. See the documentation for this attribute for the data this tactic returns. The returned universe levels are the universe levels of the structure. For the projections there are three cases * If the declaration `{structure_name}.simps.{projection_name}` has been declared, then the value of this declaration is used (after checking that it is definitionally equal to the actual projection. If you rename the projection name, the declaration should have the *new* projection name. * You can also declare a custom projection that is a composite of multiple projections. * Otherwise, for every class with the `notation_class` attribute, and the structure has an instance of that notation class, then the projection of that notation class is used for the projection that is definitionally equal to it (if there is such a projection). This means in practice that coercions to function types and sorts will be used instead of a projection, if this coercion is definitionally equal to a projection. Furthermore, for notation classes like `has_mul` and `has_zero` those projections are used instead of the corresponding projection. Projections for coercions and notation classes are not automatically generated if they are composites of multiple projections (for example when you use `extend` without the `old_structure_cmd`). * Otherwise, the projection of the structure is chosen. For example: ``simps_get_raw_projections env `prod`` gives the default projections ``` ([u, v], [prod.fst.{u v}, prod.snd.{u v}]) ``` while ``simps_get_raw_projections env `equiv`` gives ``` ([u_1, u_2], [λ α β, coe_fn, λ {α β} (e : α ≃ β), ⇑(e.symm), left_inv, right_inv]) ``` after declaring the coercion from `equiv` to function and adding the declaration ``` def equiv.simps.inv_fun {α β} (e : α ≃ β) : β → α := e.symm ``` Optionally, this command accepts three optional arguments: * If `trace_if_exists` the command will always generate a trace message when the structure already has the attribute `@[_simps_str]`. * The `rules` argument accepts a list of pairs `sum.inl (old_name, new_name)`. This is used to change the projection name `old_name` to the custom projection name `new_name`. Example: for the structure `equiv` the projection `to_fun` could be renamed `apply`. This name will be used for parsing and generating projection names. This argument is ignored if the structure already has an existing attribute. If an element of `rules` is of the form `sum.inr name`, this means that the projection `name` will not be applied by default. * if `trc` is true, this tactic will trace information. -/ -- if performance becomes a problem, possible heuristic: use the names of the projections to -- skip all classes that don't have the corresponding field. meta def simps_get_raw_projections (e : environment) (str : name) (trace_if_exists : bool := ff) (rules : list projection_rule := []) (trc := ff) : tactic (list name × list projection_data) := do let trc := trc || is_trace_enabled_for `simps.verbose, has_attr ← has_attribute' `_simps_str str, if has_attr then do data ← simps_str_attr.get_param str, -- We always print the projections when they already exists and are called by -- `initialize_simps_projections`. when (trace_if_exists || is_trace_enabled_for `simps.verbose) $ projections_info data.2 "Already found projection information for structure" str >>= trace, return data else do when trc trace!"[simps] > generating projection information for structure {str}.", when_tracing `simps.debug trace!"[simps] > Applying the rules {rules}.", d_str ← e.get str, let raw_univs := d_str.univ_params, let raw_levels := level.param <$> raw_univs, /- Figure out projections, including renamings. The information for a projection is (before we figure out the `expr` of the projection: `(original name, given name, is default, is prefix)`. The first projections are always the actual projections of the structure, but `rules` could specify custom projections that are compositions of multiple projections. -/ projs ← e.structure_fields str, let projs : list parsed_projection_data := projs.map $ λ nm, ⟨nm, nm, tt, ff⟩, let projs : list parsed_projection_data := rules.foldl (λ projs rule, match rule with | (inl (old_nm, new_nm), is_prefix) := if old_nm ∈ projs.map (λ x, x.new_name) then projs.map $ λ proj, if proj.new_name = old_nm then { new_name := new_nm, is_prefix := is_prefix, ..proj } else proj else projs ++ [⟨old_nm, new_nm, tt, is_prefix⟩] | (inr nm, is_prefix) := if nm ∈ projs.map (λ x, x.new_name) then projs.map $ λ proj, if proj.new_name = nm then { is_default := ff, is_prefix := is_prefix, ..proj } else proj else projs ++ [⟨nm, nm, ff, is_prefix⟩] end) projs, when_tracing `simps.debug trace!"[simps] > Projection info after applying the rules: {projs}.", when ¬ (projs.map $ λ x, x.new_name : list name).nodup $ fail $ "Invalid projection names. Two projections have the same name. This is likely because a custom composition of projections was given the same name as an " ++ "existing projection. Solution: rename the existing projection (before renaming the custom " ++ "projection).", /- Define the raw expressions for the projections, by default as the projections (as an expression), but this can be overriden by the user. -/ raw_exprs_and_nrs ← projs.mmap $ λ ⟨orig_nm, new_nm, _, _⟩, do { (raw_expr, nrs) ← get_composite_of_projections str orig_nm.last, custom_proj ← do { decl ← e.get (str ++ `simps ++ new_nm.last), let custom_proj := decl.value.instantiate_univ_params $ decl.univ_params.zip raw_levels, when trc trace! "[simps] > found custom projection for {new_nm}:\n > {custom_proj}", return custom_proj } <|> return raw_expr, is_def_eq custom_proj raw_expr <|> -- if the type of the expression is different, we show a different error message, because -- that is more likely going to be helpful. do { custom_proj_type ← infer_type custom_proj, raw_expr_type ← infer_type raw_expr, b ← succeeds (is_def_eq custom_proj_type raw_expr_type), if b then fail!"Invalid custom projection:\n {custom_proj} Expression is not definitionally equal to\n {raw_expr}" else fail!"Invalid custom projection:\n {custom_proj} Expression has different type than {str ++ orig_nm}. Given type:\n {custom_proj_type} Expected type:\n {raw_expr_type}" }, return (custom_proj, nrs) }, let raw_exprs := raw_exprs_and_nrs.map prod.fst, /- Check for other coercions and type-class arguments to use as projections instead. -/ (args, _) ← open_pis d_str.type, let e_str := (expr.const str raw_levels).mk_app args, automatic_projs ← attribute.get_instances `notation_class, raw_exprs ← automatic_projs.mfoldl (λ (raw_exprs : list expr) class_nm, do { (is_class, proj_nm) ← notation_class_attr.get_param class_nm, proj_nm ← proj_nm <|> (e.structure_fields_full class_nm).map list.head, /- For this class, find the projection. `raw_expr` is the projection found applied to `args`, and `lambda_raw_expr` has the arguments `args` abstracted. -/ (raw_expr, lambda_raw_expr) ← if is_class then (do guard $ args.length = 1, let e_inst_type := (const class_nm raw_levels).mk_app args, (hyp, e_inst) ← try_for 1000 (mk_conditional_instance e_str e_inst_type), raw_expr ← mk_mapp proj_nm [args.head, e_inst], clear hyp, -- Note: `expr.bind_lambda` doesn't give the correct type raw_expr_lambda ← lambdas [hyp] raw_expr, return (raw_expr, raw_expr_lambda.lambdas args)) else (do e_inst_type ← to_expr (((const class_nm []).app (pexpr.of_expr e_str)).app ``(_)), e_inst ← try_for 1000 (mk_instance e_inst_type), raw_expr ← mk_mapp proj_nm [e_str, none, e_inst], return (raw_expr, raw_expr.lambdas args)), raw_expr_whnf ← whnf raw_expr, let relevant_proj := raw_expr_whnf.binding_body.get_app_fn.const_name, /- Use this as projection, if the function reduces to a projection, and this projection has not been overrriden by the user. -/ guard $ projs.any $ λ x, x.1 = relevant_proj.last ∧ ¬ e.contains (str ++ `simps ++ x.new_name.last), let pos := projs.find_index (λ x, x.1 = relevant_proj.last), when trc trace! " > using {proj_nm} instead of the default projection {relevant_proj.last}.", when_tracing `simps.debug trace!"[simps] > The raw projection is:\n {lambda_raw_expr}", return $ raw_exprs.update_nth pos lambda_raw_expr } <|> return raw_exprs) raw_exprs, let positions := raw_exprs_and_nrs.map prod.snd, let proj_names := projs.map (λ x, x.new_name), let defaults := projs.map (λ x, x.is_default), let prefixes := projs.map (λ x, x.is_prefix), let projs := proj_names.zip_with5 projection_data.mk raw_exprs positions defaults prefixes, /- make all proof non-default. -/ projs ← projs.mmap $ λ proj, is_proof proj.expr >>= λ b, return $ if b then { is_default := ff, .. proj } else proj, when trc $ projections_info projs "generated projections for" str >>= trace, simps_str_attr.set str (raw_univs, projs) tt, when_tracing `simps.debug trace! "[simps] > Generated raw projection data: \n{(raw_univs, projs)}", return (raw_univs, projs) /-- Parse a rule for `initialize_simps_projections`. It is either `<name>→<name>` or `-<name>`, possibly following by `as_prefix`.-/ meta def simps_parse_rule : parser projection_rule := prod.mk <$> ((λ x y, inl (x, y)) <$> ident <*> (tk "->" >> ident) <|> inr <$> (tk "-" >> ident)) <*> is_some <$> (tk "as_prefix")? /-- You can specify custom projections for the `@[simps]` attribute. To do this for the projection `my_structure.original_projection` by adding a declaration `my_structure.simps.my_projection` that is definitionally equal to `my_structure.original_projection` but has the projection in the desired (simp-normal) form. Then you can call ``` initialize_simps_projections (original_projection → my_projection, ...) ``` to register this projection. See `initialize_simps_projections_cmd` for more information. You can also specify custom projections that are definitionally equal to a composite of multiple projections. This is often desirable when extending structures (without `old_structure_cmd`). `has_coe_to_fun` and notation class (like `has_mul`) instances will be automatically used, if they are definitionally equal to a projection of the structure (but not when they are equal to the composite of multiple projections). -/ library_note "custom simps projection" /-- This command specifies custom names and custom projections for the simp attribute `simps_attr`. * You can specify custom names by writing e.g. `initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply)`. * See Note [custom simps projection] and the examples below for information how to declare custom projections. * If no custom projection is specified, the projection will be `coe_fn`/`⇑` if a `has_coe_to_fun` instance has been declared, or the notation of a notation class (like `has_mul`) if such an instance is available. If none of these cases apply, the projection itself will be used. * You can disable a projection by default by running `initialize_simps_projections equiv (-inv_fun)` This will ensure that no simp lemmas are generated for this projection, unless this projection is explicitly specified by the user. * If you want the projection name added as a prefix in the generated lemma name, you can add the `as_prefix` modifier: `initialize_simps_projections equiv (to_fun → coe as_prefix)` Note that this does not influence the parsing of projection names: if you have a declaration `foo` and you want to apply the projections `snd`, `coe` (which is a prefix) and `fst`, in that order you can run `@[simps snd_coe_fst] def foo ...` and this will generate a lemma with the name `coe_foo_snd_fst`. * Run `initialize_simps_projections?` (or `set_option trace.simps.verbose true`) to see the generated projections. * You can declare a new name for a projection that is the composite of multiple projections, e.g. ``` structure A := (proj : ℕ) structure B extends A initialize_simps_projections? B (to_A_proj → proj, -to_A) ``` You can also make your custom projection that is definitionally equal to a composite of projections. In this case, coercions and notation classes are not automatically recognized, and should be manually given by giving a custom projection. This is especially useful when extending a structure (without `old_structure_cmd`). In the above example, it is desirable to add `-to_A`, so that `@[simps]` doesn't automatically apply the `B.to_A` projection and then recursively the `A.proj` projection in the lemmas it generates. If you want to get both the `foo_proj` and `foo_to_A` simp lemmas, you can use `@[simps, simps to_A]`. * Running `initialize_simps_projections my_struc` without arguments is not necessary, it has the same effect if you just add `@[simps]` to a declaration. * If you do anything to change the default projections, make sure to call either `@[simps]` or `initialize_simps_projections` in the same file as the structure declaration. Otherwise, you might have a file that imports the structure, but not your custom projections. Some common uses: * If you define a new homomorphism-like structure (like `mul_hom`) you can just run `initialize_simps_projections` after defining the `has_coe_to_fun` instance ``` instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (M →ₙ* N) := ... initialize_simps_projections mul_hom (to_fun → apply) ``` This will generate `foo_apply` lemmas for each declaration `foo`. * If you prefer `coe_foo` lemmas that state equalities between functions, use `initialize_simps_projections mul_hom (to_fun → coe as_prefix)` In this case you have to use `@[simps {fully_applied := ff}]` or equivalently `@[simps as_fn]` whenever you call `@[simps]`. * You can also initialize to use both, in which case you have to choose which one to use by default, by using either of the following ``` initialize_simps_projections mul_hom (to_fun → apply, to_fun → coe, -coe as_prefix) initialize_simps_projections mul_hom (to_fun → apply, to_fun → coe as_prefix, -apply) ``` In the first case, you can get both lemmas using `@[simps, simps coe as_fn]` and in the second case you can get both lemmas using `@[simps as_fn, simps apply]`. * If your new homomorphism-like structure extends another structure (without `old_structure_cmd`) (like `rel_embedding`), then you have to specify explicitly that you want to use a coercion as a custom projection. For example ``` def rel_embedding.simps.apply (h : r ↪r s) : α → β := h initialize_simps_projections rel_embedding (to_embedding_to_fun → apply, -to_embedding) ``` * If you have an isomorphism-like structure (like `equiv`) you often want to define a custom projection for the inverse: ``` def equiv.simps.symm_apply (e : α ≃ β) : β → α := e.symm initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply) ``` -/ @[user_command] meta def initialize_simps_projections_cmd (_ : parse $ tk "initialize_simps_projections") : parser unit := do env ← get_env, trc ← is_some <$> (tk "?")?, ns ← (prod.mk <$> ident <*> (tk "(" >> sep_by (tk ",") simps_parse_rule <* tk ")")?)*, ns.mmap' $ λ data, do nm ← resolve_constant data.1, simps_get_raw_projections env nm tt (data.2.get_or_else []) trc add_tactic_doc { name := "initialize_simps_projections", category := doc_category.cmd, decl_names := [`initialize_simps_projections_cmd], tags := ["simplification"] } /-- Configuration options for the `@[simps]` attribute. * `attrs` specifies the list of attributes given to the generated lemmas. Default: ``[`simp]``. The attributes can be either basic attributes, or user attributes without parameters. There are two attributes which `simps` might add itself: * If ``[`simp]`` is in the list, then ``[`_refl_lemma]`` is added automatically if appropriate. * If the definition is marked with `@[to_additive ...]` then all generated lemmas are marked with `@[to_additive]`. This is governed by the `add_additive` configuration option. * if `simp_rhs` is `tt` then the right-hand-side of the generated lemmas will be put in simp-normal form. More precisely: `dsimp, simp` will be called on all these expressions. See note [dsimp, simp]. * `type_md` specifies how aggressively definitions are unfolded in the type of expressions for the purposes of finding out whether the type is a function type. Default: `instances`. This will unfold coercion instances (so that a coercion to a function type is recognized as a function type), but not declarations like `set`. * `rhs_md` specifies how aggressively definition in the declaration are unfolded for the purposes of finding out whether it is a constructor. Default: `none` Exception: `@[simps]` will automatically add the options `{rhs_md := semireducible, simp_rhs := tt}` if the given definition is not a constructor with the given reducibility setting for `rhs_md`. * If `fully_applied` is `ff` then the generated `simp` lemmas will be between non-fully applied terms, i.e. equalities between functions. This does not restrict the recursive behavior of `@[simps]`, so only the "final" projection will be non-fully applied. However, it can be used in combination with explicit field names, to get a partially applied intermediate projection. * The option `not_recursive` contains the list of names of types for which `@[simps]` doesn't recursively apply projections. For example, given an equivalence `α × β ≃ β × α` one usually wants to only apply the projections for `equiv`, and not also those for `×`. This option is only relevant if no explicit projection names are given as argument to `@[simps]`. * The option `trace` is set to `tt` when you write `@[simps?]`. In this case, the attribute will print all generated lemmas. It is almost the same as setting the option `trace.simps.verbose`, except that it doesn't print information about the found projections. * if `add_additive` is `some nm` then `@[to_additive]` is added to the generated lemma. This option is automatically set to `tt` when the original declaration was tagged with `@[to_additive, simps]` (in that order), where `nm` is the additive name of the original declaration. -/ @[derive [has_reflect, inhabited]] structure simps_cfg := (attrs := [`simp]) (simp_rhs := ff) (type_md := transparency.instances) (rhs_md := transparency.none) (fully_applied := tt) (not_recursive := [`prod, `pprod]) (trace := ff) (add_additive := @none name) /-- A common configuration for `@[simps]`: generate equalities between functions instead equalities between fully applied expressions. -/ def as_fn : simps_cfg := {fully_applied := ff} /-- A common configuration for `@[simps]`: don't tag the generated lemmas with `@[simp]`. -/ def lemmas_only : simps_cfg := {attrs := []} /-- Get the projections of a structure used by `@[simps]` applied to the appropriate arguments. Returns a list of tuples ``` (corresponding right-hand-side, given projection name, projection expression, projection numbers, used by default, is prefix) ``` (where all fields except the first are packed in a `projection_data` structure) one for each projection. The given projection name is the name for the projection used by the user used to generate (and parse) projection names. For example, in the structure Example 1: ``simps_get_projection_exprs env `(α × β) `(⟨x, y⟩)`` will give the output ``` [(`(x), `fst, `(@prod.fst.{u v} α β), [0], tt, ff), (`(y), `snd, `(@prod.snd.{u v} α β), [1], tt, ff)] ``` Example 2: ``simps_get_projection_exprs env `(α ≃ α) `(⟨id, id, λ _, rfl, λ _, rfl⟩)`` will give the output ``` [(`(id), `apply, `(coe), [0], tt, ff), (`(id), `symm_apply, `(λ f, ⇑f.symm), [1], tt, ff), ..., ...] ``` -/ meta def simps_get_projection_exprs (e : environment) (tgt : expr) (rhs : expr) (cfg : simps_cfg) : tactic $ list $ expr × projection_data := do let params := get_app_args tgt, -- the parameters of the structure (params.zip $ (get_app_args rhs).take params.length).mmap' (λ ⟨a, b⟩, is_def_eq a b) <|> fail "unreachable code (1)", let str := tgt.get_app_fn.const_name, let rhs_args := (get_app_args rhs).drop params.length, -- the fields of the object (raw_univs, proj_data) ← simps_get_raw_projections e str ff [] cfg.trace, let univs := raw_univs.zip tgt.get_app_fn.univ_levels, let new_proj_data : list $ expr × projection_data := proj_data.map $ λ proj, (rhs_args.inth proj.proj_nrs.head, { expr := (proj.expr.instantiate_univ_params univs).instantiate_lambdas_or_apps params, proj_nrs := proj.proj_nrs.tail, .. proj }), return new_proj_data /-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`, `args` is the list of local constants occurring, and `univs` is the list of universe variables. -/ meta def simps_add_projection (nm : name) (type lhs rhs : expr) (args : list expr) (univs : list name) (cfg : simps_cfg) : tactic unit := do when_tracing `simps.debug trace! "[simps] > Planning to add the equality\n > {lhs} = ({rhs} : {type})", lvl ← get_univ_level type, -- simplify `rhs` if `cfg.simp_rhs` is true (rhs, prf) ← do { guard cfg.simp_rhs, rhs' ← rhs.dsimp {fail_if_unchanged := ff}, when_tracing `simps.debug $ when (rhs ≠ rhs') trace! "[simps] > `dsimp` simplified rhs to\n > {rhs'}", (rhsprf1, rhsprf2, ns) ← rhs'.simp {fail_if_unchanged := ff}, when_tracing `simps.debug $ when (rhs' ≠ rhsprf1) trace! "[simps] > `simp` simplified rhs to\n > {rhsprf1}", return (prod.mk rhsprf1 rhsprf2) } <|> return (rhs, const `eq.refl [lvl] type lhs), let eq_ap := const `eq [lvl] type lhs rhs, decl_name ← get_unused_decl_name nm, let decl_type := eq_ap.pis args, let decl_value := prf.lambdas args, let decl := declaration.thm decl_name univs decl_type (pure decl_value), when cfg.trace trace! "[simps] > adding projection {decl_name}:\n > {decl_type}", decorate_error ("Failed to add projection lemma " ++ decl_name.to_string ++ ". Nested error:") $ add_decl decl, b ← succeeds $ is_def_eq lhs rhs, when (b ∧ `simp ∈ cfg.attrs) (set_basic_attribute `_refl_lemma decl_name tt), cfg.attrs.mmap' $ λ nm, set_attribute nm decl_name tt, when cfg.add_additive.is_some $ to_additive.attr.set decl_name ⟨ff, cfg.trace, cfg.add_additive.iget, none, tt⟩ tt /-- Derive lemmas specifying the projections of the declaration. If `todo` is non-empty, it will generate exactly the names in `todo`. `to_apply` is non-empty after a custom projection that is a composition of multiple projections was just used. In that case we need to apply these projections before we continue changing lhs. -/ meta def simps_add_projections : Π (e : environment) (nm : name) (type lhs rhs : expr) (args : list expr) (univs : list name) (must_be_str : bool) (cfg : simps_cfg) (todo : list string) (to_apply : list ℕ), tactic unit | e nm type lhs rhs args univs must_be_str cfg todo to_apply := do -- we don't want to unfold non-reducible definitions (like `set`) to apply more arguments when_tracing `simps.debug trace! "[simps] > Type of the expression before normalizing: {type}", (type_args, tgt) ← open_pis_whnf type cfg.type_md, when_tracing `simps.debug trace!"[simps] > Type after removing pi's: {tgt}", tgt ← whnf tgt, when_tracing `simps.debug trace!"[simps] > Type after reduction: {tgt}", let new_args := args ++ type_args, let lhs_ap := lhs.instantiate_lambdas_or_apps type_args, let rhs_ap := rhs.instantiate_lambdas_or_apps type_args, let str := tgt.get_app_fn.const_name, /- We want to generate the current projection if it is in `todo` -/ let todo_next := todo.filter (≠ ""), /- Don't recursively continue if `str` is not a structure or if the structure is in `not_recursive`. -/ if e.is_structure str ∧ ¬(todo = [] ∧ str ∈ cfg.not_recursive ∧ ¬must_be_str) then do [intro] ← return $ e.constructors_of str | fail "unreachable code (3)", rhs_whnf ← whnf rhs_ap cfg.rhs_md, (rhs_ap, todo_now) ← -- `todo_now` means that we still have to generate the current simp lemma if ¬ is_constant_of rhs_ap.get_app_fn intro ∧ is_constant_of rhs_whnf.get_app_fn intro then /- If this was a desired projection, we want to apply it before taking the whnf. However, if the current field is an eta-expansion (see below), we first want to eta-reduce it and only then construct the projection. This makes the flow of this function messy. -/ when ("" ∈ todo ∧ to_apply = []) (if cfg.fully_applied then simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else simps_add_projection nm type lhs rhs args univs cfg) >> return (rhs_whnf, ff) else return (rhs_ap, "" ∈ todo ∧ to_apply = []), if is_constant_of (get_app_fn rhs_ap) intro then do -- if the value is a constructor application proj_info ← simps_get_projection_exprs e tgt rhs_ap cfg, when_tracing `simps.debug trace!"[simps] > Raw projection information:\n {proj_info}", eta ← rhs_ap.is_eta_expansion, -- check whether `rhs_ap` is an eta-expansion let rhs_ap := eta.lhoare rhs_ap, -- eta-reduce `rhs_ap` /- As a special case, we want to automatically generate the current projection if `rhs_ap` was an eta-expansion. Also, when this was a desired projection, we need to generate the current projection if we haven't done it above. -/ when (todo_now ∨ (todo = [] ∧ eta.is_some ∧ to_apply = [])) $ if cfg.fully_applied then simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else simps_add_projection nm type lhs rhs args univs cfg, /- If we are in the middle of a composite projection. -/ when (to_apply ≠ []) $ do { ⟨new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix⟩ ← return $ proj_info.inth to_apply.head, new_type ← infer_type new_rhs, when_tracing `simps.debug trace!"[simps] > Applying a custom composite projection. Current lhs: > {lhs_ap}", simps_add_projections e nm new_type lhs_ap new_rhs new_args univs ff cfg todo to_apply.tail }, /- We stop if no further projection is specified or if we just reduced an eta-expansion and we automatically choose projections -/ when ¬(to_apply ≠ [] ∨ todo = [""] ∨ (eta.is_some ∧ todo = [])) $ do let projs : list name := proj_info.map $ λ x, x.snd.name, let todo := if to_apply = [] then todo_next else todo, -- check whether all elements in `todo` have a projection as prefix guard (todo.all $ λ x, projs.any $ λ proj, ("_" ++ proj.last).is_prefix_of x) <|> let x := (todo.find $ λ x, projs.all $ λ proj, ¬ ("_" ++ proj.last).is_prefix_of x).iget, simp_lemma := nm.append_suffix x, needed_proj := (x.split_on '_').tail.head in fail! "Invalid simp lemma {simp_lemma}. Structure {str} does not have projection {needed_proj}. The known projections are: {projs} You can also see this information by running `initialize_simps_projections? {str}`. Note: these projection names might not correspond to the projection names of the structure.", proj_info.mmap_with_index' $ λ proj_nr ⟨new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix⟩, do new_type ← infer_type new_rhs, let new_todo := todo.filter_map $ λ x, x.get_rest ("_" ++ proj.last), -- we only continue with this field if it is non-propositional or mentioned in todo when ((is_default ∧ todo = []) ∨ new_todo ≠ []) $ do let new_lhs := proj_expr.instantiate_lambdas_or_apps [lhs_ap], let new_nm := nm.append_to_last proj.last is_prefix, let new_cfg := { add_additive := cfg.add_additive.map $ λ nm, nm.append_to_last (to_additive.guess_name proj.last) is_prefix, ..cfg }, when_tracing `simps.debug trace!"[simps] > Recursively add projections for: > {new_lhs}", simps_add_projections e new_nm new_type new_lhs new_rhs new_args univs ff new_cfg new_todo proj_nrs -- if I'm about to run into an error, try to set the transparency for `rhs_md` higher. else if cfg.rhs_md = transparency.none ∧ (must_be_str ∨ todo_next ≠ [] ∨ to_apply ≠ []) then do when cfg.trace trace! "[simps] > The given definition is not a constructor application: > {rhs_ap} > Retrying with the options {{ rhs_md := semireducible, simp_rhs := tt}.", simps_add_projections e nm type lhs rhs args univs must_be_str { rhs_md := semireducible, simp_rhs := tt, ..cfg} todo to_apply else do when (to_apply ≠ []) $ fail!"Invalid simp lemma {nm}. The given definition is not a constructor application:\n {rhs_ap}", when must_be_str $ fail!"Invalid `simps` attribute. The body is not a constructor application:\n {rhs_ap}", when (todo_next ≠ []) $ fail!"Invalid simp lemma {nm.append_suffix todo_next.head}. The given definition is not a constructor application:\n {rhs_ap}", if cfg.fully_applied then simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else simps_add_projection nm type lhs rhs args univs cfg else do when must_be_str $ fail!"Invalid `simps` attribute. Target {str} is not a structure", when (todo_next ≠ [] ∧ str ∉ cfg.not_recursive) $ let first_todo := todo_next.head in fail!"Invalid simp lemma {nm.append_suffix first_todo}. Projection {(first_todo.split_on '_').tail.head} doesn't exist, because target is not a structure.", if cfg.fully_applied then simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else simps_add_projection nm type lhs rhs args univs cfg /-- `simps_tac` derives `simp` lemmas for all (nested) non-Prop projections of the declaration. If `todo` is non-empty, it will generate exactly the names in `todo`. If `short_nm` is true, the generated names will only use the last projection name. If `trc` is true, trace as if `trace.simps.verbose` is true. -/ meta def simps_tac (nm : name) (cfg : simps_cfg := {}) (todo : list string := []) (trc := ff) : tactic unit := do e ← get_env, d ← e.get nm, let lhs : expr := const d.to_name d.univ_levels, let todo := todo.dedup.map $ λ proj, "_" ++ proj, let cfg := { trace := cfg.trace || is_trace_enabled_for `simps.verbose || trc, ..cfg }, b ← has_attribute' `to_additive nm, cfg ← if b then do { dict ← to_additive.aux_attr.get_cache, when cfg.trace trace!"[simps] > @[to_additive] will be added to all generated lemmas.", return { add_additive := dict.find nm, ..cfg } } else return cfg, simps_add_projections e nm d.type lhs d.value [] d.univ_params tt cfg todo [] /-- The parser for the `@[simps]` attribute. -/ meta def simps_parser : parser (bool × list string × simps_cfg) := do /- note: we don't check whether the user has written a nonsense namespace in an argument. -/ prod.mk <$> is_some <$> (tk "?")? <*> (prod.mk <$> many (name.last <$> ident) <*> (do some e ← parser.pexpr? | return {}, eval_pexpr simps_cfg e)) /-- The `@[simps]` attribute automatically derives lemmas specifying the projections of this declaration. Example: ```lean @[simps] def foo : ℕ × ℤ := (1, 2) ``` derives two `simp` lemmas: ```lean @[simp] lemma foo_fst : foo.fst = 1 @[simp] lemma foo_snd : foo.snd = 2 ``` * It does not derive `simp` lemmas for the prop-valued projections. * It will automatically reduce newly created beta-redexes, but will not unfold any definitions. * If the structure has a coercion to either sorts or functions, and this is defined to be one of the projections, then this coercion will be used instead of the projection. * If the structure is a class that has an instance to a notation class, like `has_mul`, then this notation is used instead of the corresponding projection. * You can specify custom projections, by giving a declaration with name `{structure_name}.simps.{projection_name}`. See Note [custom simps projection]. Example: ```lean def equiv.simps.inv_fun (e : α ≃ β) : β → α := e.symm @[simps] def equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm⟩ ``` generates ``` @[simp] lemma equiv.trans_to_fun : ∀ {α β γ} (e₁ e₂) (a : α), ⇑(e₁.trans e₂) a = (⇑e₂ ∘ ⇑e₁) a @[simp] lemma equiv.trans_inv_fun : ∀ {α β γ} (e₁ e₂) (a : γ), ⇑((e₁.trans e₂).symm) a = (⇑(e₁.symm) ∘ ⇑(e₂.symm)) a ``` * You can specify custom projection names, by specifying the new projection names using `initialize_simps_projections`. Example: `initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply)`. See `initialize_simps_projections_cmd` for more information. * If one of the fields itself is a structure, this command will recursively create `simp` lemmas for all fields in that structure. * Exception: by default it will not recursively create `simp` lemmas for fields in the structures `prod` and `pprod`. You can give explicit projection names or change the value of `simps_cfg.not_recursive` to override this behavior. Example: ```lean structure my_prod (α β : Type*) := (fst : α) (snd : β) @[simps] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩ ``` generates ```lean @[simp] lemma foo_fst : foo.fst = (1, 2) @[simp] lemma foo_snd_fst : foo.snd.fst = 3 @[simp] lemma foo_snd_snd : foo.snd.snd = 4 ``` * You can use `@[simps proj1 proj2 ...]` to only generate the projection lemmas for the specified projections. * Recursive projection names can be specified using `proj1_proj2_proj3`. This will create a lemma of the form `foo.proj1.proj2.proj3 = ...`. Example: ```lean structure my_prod (α β : Type*) := (fst : α) (snd : β) @[simps fst fst_fst snd] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩ ``` generates ```lean @[simp] lemma foo_fst : foo.fst = (1, 2) @[simp] lemma foo_fst_fst : foo.fst.fst = 1 @[simp] lemma foo_snd : foo.snd = {fst := 3, snd := 4} ``` * If one of the values is an eta-expanded structure, we will eta-reduce this structure. Example: ```lean structure equiv_plus_data (α β) extends α ≃ β := (data : bool) @[simps] def bar {α} : equiv_plus_data α α := { data := tt, ..equiv.refl α } ``` generates the following: ```lean @[simp] lemma bar_to_equiv : ∀ {α : Sort*}, bar.to_equiv = equiv.refl α @[simp] lemma bar_data : ∀ {α : Sort*}, bar.data = tt ``` This is true, even though Lean inserts an eta-expanded version of `equiv.refl α` in the definition of `bar`. * For configuration options, see the doc string of `simps_cfg`. * The precise syntax is `('simps' ident* e)`, where `e` is an expression of type `simps_cfg`. * `@[simps]` reduces let-expressions where necessary. * When option `trace.simps.verbose` is true, `simps` will print the projections it finds and the lemmas it generates. The same can be achieved by using `@[simps?]`, except that in this case it will not print projection information. * Use `@[to_additive, simps]` to apply both `to_additive` and `simps` to a definition, making sure that `simps` comes after `to_additive`. This will also generate the additive versions of all `simp` lemmas. -/ /- If one of the fields is a partially applied constructor, we will eta-expand it (this likely never happens, so is not included in the official doc). -/ @[user_attribute] meta def simps_attr : user_attribute unit (bool × list string × simps_cfg) := { name := `simps, descr := "Automatically derive lemmas specifying the projections of this declaration.", parser := simps_parser, after_set := some $ λ n _ persistent, do guard persistent <|> fail "`simps` currently cannot be used as a local attribute", (trc, todo, cfg) ← simps_attr.get_param n, simps_tac n cfg todo trc } add_tactic_doc { name := "simps", category := doc_category.attr, decl_names := [`simps_attr], tags := ["simplification"] }
ced8e13d7b657670f368ce1ee1d3253317139c6a
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/ring_theory/int/basic.lean
fd6cc19f00e4a3513c40cb3f12f97611ac7e50e1
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,010
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, Jens Wagemaker, Aaron Anderson -/ import data.int.basic import data.int.gcd import ring_theory.multiplicity import ring_theory.principal_ideal_domain /-! # Divisibility over ℕ and ℤ This file collects results for the integers and natural numbers that use abstract algebra in their proofs or cases of ℕ and ℤ being examples of structures in abstract algebra. ## Main statements * `nat.prime_iff`: `nat.prime` coincides with the general definition of `prime` * `nat.irreducible_iff_prime`: a non-unit natural number is only divisible by `1` iff it is prime * `nat.factors_eq`: the multiset of elements of `nat.factors` is equal to the factors given by the `unique_factorization_monoid` instance * ℤ is a `normalization_monoid` * ℤ is a `gcd_monoid` ## Tags prime, irreducible, natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor, prime factorization, prime factors, unique factorization, unique factors -/ theorem nat.prime_iff {p : ℕ} : p.prime ↔ prime p := begin split; intro h, { refine ⟨h.ne_zero, ⟨_, λ a b, _⟩⟩, { rw nat.is_unit_iff, apply h.ne_one }, { apply h.dvd_mul.1 } }, { refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.ne_one rfl }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.2.2 m n (hn ▸ dvd_refl _) with hpm hpn, { right, apply nat.dvd_antisymm (dvd.intro _ hn.symm) hpm }, { left, cases n, { exfalso, rw [hn, mul_zero] at h, apply h.ne_zero rfl }, apply nat.eq_of_mul_eq_mul_right (nat.succ_pos _), rw [← hn, one_mul], apply nat.dvd_antisymm hpn (dvd.intro m _), rw [mul_comm, hn], }, } } end theorem nat.irreducible_iff_prime {p : ℕ} : irreducible p ↔ prime p := begin refine ⟨λ h, _, irreducible_of_prime⟩, rw ← nat.prime_iff, refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.not_unit is_unit_one, }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.is_unit_or_is_unit hn with um un, { left, rw nat.is_unit_iff.1 um, }, { right, rw [hn, nat.is_unit_iff.1 un, mul_one], } } end namespace nat instance : wf_dvd_monoid ℕ := ⟨begin apply rel_hom.well_founded _ (with_top.well_founded_lt nat.lt_wf), refine ⟨λ x, if x = 0 then ⊤ else x, _⟩, intros a b h, cases a, { exfalso, revert h, simp [dvd_not_unit] }, cases b, {simp [succ_ne_zero, with_top.coe_lt_top]}, cases dvd_and_not_dvd_iff.2 h with h1 h2, simp only [succ_ne_zero, with_top.coe_lt_coe, if_false], apply lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h1) (λ con, h2 _), rw con, end⟩ instance : unique_factorization_monoid ℕ := ⟨λ _, nat.irreducible_iff_prime⟩ end nat namespace int section normalization_monoid instance : normalization_monoid ℤ := { norm_unit := λa:ℤ, if 0 ≤ a then 1 else -1, norm_unit_zero := if_pos (le_refl _), norm_unit_mul := assume a b hna hnb, begin cases hna.lt_or_lt with ha ha; cases hnb.lt_or_lt with hb hb; simp [mul_nonneg_iff, ha.le, ha.not_le, hb.le, hb.not_le] end, norm_unit_coe_units := assume u, (units_eq_one_or u).elim (assume eq, eq.symm ▸ if_pos zero_le_one) (assume eq, eq.symm ▸ if_neg (not_le_of_gt $ show (-1:ℤ) < 0, by dec_trivial)), } lemma normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z := show z * ↑(ite _ _ _) = z, by rw [if_pos h, units.coe_one, mul_one] lemma normalize_of_neg {z : ℤ} (h : z < 0) : normalize z = -z := show z * ↑(ite _ _ _) = -z, by rw [if_neg (not_le_of_gt h), units.coe_neg, units.coe_one, mul_neg_one] lemma normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n := normalize_of_nonneg (coe_nat_le_coe_nat_of_le $ nat.zero_le n) theorem coe_nat_abs_eq_normalize (z : ℤ) : (z.nat_abs : ℤ) = normalize z := begin by_cases 0 ≤ z, { simp [nat_abs_of_nonneg h, normalize_of_nonneg h] }, { simp [of_nat_nat_abs_of_nonpos (le_of_not_ge h), normalize_of_neg (lt_of_not_ge h)] } end end normalization_monoid section gcd_monoid instance : gcd_monoid ℤ := { gcd := λa b, int.gcd a b, lcm := λa b, int.lcm a b, gcd_dvd_left := assume a b, int.gcd_dvd_left _ _, gcd_dvd_right := assume a b, int.gcd_dvd_right _ _, dvd_gcd := assume a b c, dvd_gcd, normalize_gcd := assume a b, normalize_coe_nat _, gcd_mul_lcm := by intros; rw [← int.coe_nat_mul, gcd_mul_lcm, coe_nat_abs_eq_normalize], lcm_zero_left := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_left _, lcm_zero_right := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_right _, .. int.normalization_monoid } lemma coe_gcd (i j : ℤ) : ↑(int.gcd i j) = gcd_monoid.gcd i j := rfl lemma coe_lcm (i j : ℤ) : ↑(int.lcm i j) = gcd_monoid.lcm i j := rfl lemma nat_abs_gcd (i j : ℤ) : nat_abs (gcd_monoid.gcd i j) = int.gcd i j := rfl lemma nat_abs_lcm (i j : ℤ) : nat_abs (gcd_monoid.lcm i j) = int.lcm i j := rfl end gcd_monoid lemma exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ) (h : is_unit u), (int.nat_abs a : ℤ) = u * a := begin cases (nat_abs_eq a) with h, { use [1, is_unit_one], rw [← h, one_mul], }, { use [-1, is_unit_int.mpr rfl], rw [ ← neg_eq_iff_neg_eq.mp (eq.symm h)], simp only [neg_mul_eq_neg_mul_symm, one_mul] } end lemma gcd_eq_one_iff_coprime {a b : ℤ} : int.gcd a b = 1 ↔ is_coprime a b := begin split, { intro hg, obtain ⟨ua, hua, ha⟩ := exists_unit_of_abs a, obtain ⟨ub, hub, hb⟩ := exists_unit_of_abs b, use [(nat.gcd_a (int.nat_abs a) (int.nat_abs b)) * ua, (nat.gcd_b (int.nat_abs a) (int.nat_abs b)) * ub], rw [mul_assoc, ← ha, mul_assoc, ← hb, mul_comm, mul_comm _ (int.nat_abs b : ℤ), ← nat.gcd_eq_gcd_ab], norm_cast, exact hg }, { rintro ⟨r, s, h⟩, by_contradiction hg, obtain ⟨p, ⟨hp, ha, hb⟩⟩ := nat.prime.not_coprime_iff_dvd.mp hg, apply nat.prime.not_dvd_one hp, apply coe_nat_dvd.mp, change (p : ℤ) ∣ 1, rw [← h], exact dvd_add (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr ha) _) (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr hb) _), } end lemma sqr_of_gcd_eq_one {a b c : ℤ} (h : int.gcd a b = 1) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := begin have h' : gcd_monoid.gcd a b = 1, { rw [← coe_gcd, h], dec_trivial }, obtain ⟨d, ⟨u, hu⟩⟩ := exists_associated_pow_of_mul_eq_pow h' heq, use d, rw ← hu, cases int.units_eq_one_or u with hu' hu'; { rw hu', simp } end lemma sqr_of_coprime {a b c : ℤ} (h : is_coprime a b) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := sqr_of_gcd_eq_one (gcd_eq_one_iff_coprime.mpr h) heq lemma nat_abs_euclidean_domain_gcd (a b : ℤ) : int.nat_abs (euclidean_domain.gcd a b) = int.gcd a b := begin apply nat.dvd_antisymm; rw ← int.coe_nat_dvd, { rw int.nat_abs_dvd, exact int.dvd_gcd (euclidean_domain.gcd_dvd_left _ _) (euclidean_domain.gcd_dvd_right _ _) }, { rw int.dvd_nat_abs, exact euclidean_domain.dvd_gcd (int.gcd_dvd_left _ _) (int.gcd_dvd_right _ _) } end end int theorem irreducible_iff_nat_prime : ∀(a : ℕ), irreducible a ↔ nat.prime a | 0 := by simp [nat.not_prime_zero] | 1 := by simp [nat.prime, one_lt_two] | (n + 2) := have h₁ : ¬n + 2 = 1, from dec_trivial, begin simp [h₁, nat.prime, irreducible_iff, (≥), nat.le_add_left 2 n, (∣)], refine forall_congr (assume a, forall_congr $ assume b, forall_congr $ assume hab, _), by_cases a = 1; simp [h], split, { assume hb, simpa [hb] using hab.symm }, { assume ha, subst ha, have : n + 2 > 0, from dec_trivial, refine nat.eq_of_mul_eq_mul_left this _, rw [← hab, mul_one] } end lemma nat.prime_iff_prime {p : ℕ} : p.prime ↔ _root_.prime (p : ℕ) := ⟨λ hp, ⟨pos_iff_ne_zero.1 hp.pos, mt is_unit_iff_dvd_one.1 hp.not_dvd_one, λ a b, hp.dvd_mul.1⟩, λ hp, ⟨nat.one_lt_iff_ne_zero_and_ne_one.2 ⟨hp.1, λ h1, hp.2.1 $ h1.symm ▸ is_unit_one⟩, λ a h, let ⟨b, hab⟩ := h in (hp.2.2 a b (hab ▸ dvd_refl _)).elim (λ ha, or.inr (nat.dvd_antisymm h ha)) (λ hb, or.inl (have hpb : p = b, from nat.dvd_antisymm hb (hab.symm ▸ dvd_mul_left _ _), (nat.mul_right_inj (show 0 < p, from nat.pos_of_ne_zero hp.1)).1 $ by rw [hpb, mul_comm, ← hab, hpb, mul_one]))⟩⟩ lemma nat.prime_iff_prime_int {p : ℕ} : p.prime ↔ _root_.prime (p : ℤ) := ⟨λ hp, ⟨int.coe_nat_ne_zero_iff_pos.2 hp.pos, mt is_unit_int.1 hp.ne_one, λ a b h, by rw [← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul, hp.dvd_mul] at h; rwa [← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd]⟩, λ hp, nat.prime_iff_prime.2 ⟨int.coe_nat_ne_zero.1 hp.1, mt nat.is_unit_iff.1 $ λ h, by simpa [h, not_prime_one] using hp, λ a b, by simpa only [int.coe_nat_dvd, (int.coe_nat_mul _ _).symm] using hp.2.2 a b⟩⟩ /-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/ def associates_int_equiv_nat : associates ℤ ≃ ℕ := begin refine ⟨λz, z.out.nat_abs, λn, associates.mk n, _, _⟩, { refine (assume a, quotient.induction_on' a $ assume a, associates.mk_eq_mk_iff_associated.2 $ associated.symm $ ⟨norm_unit a, _⟩), show normalize a = int.nat_abs (normalize a), rw [int.coe_nat_abs_eq_normalize, normalize_idem] }, { intro n, dsimp, rw [associates.out_mk ↑n, ← int.coe_nat_abs_eq_normalize, int.nat_abs_of_nat, int.nat_abs_of_nat] } end lemma int.prime.dvd_mul {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : p ∣ m.nat_abs ∨ p ∣ n.nat_abs := begin apply (nat.prime.dvd_mul hp).mp, rw ← int.nat_abs_mul, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_mul' {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : (p : ℤ) ∣ m ∨ (p : ℤ) ∣ n := begin rw [int.coe_nat_dvd_left, int.coe_nat_dvd_left], exact int.prime.dvd_mul hp h end lemma int.prime.dvd_pow {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : p ∣ n.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ k hp, rw ← int.nat_abs_pow, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_pow' {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : (p : ℤ) ∣ n := begin rw int.coe_nat_dvd_left, exact int.prime.dvd_pow hp h end lemma prime_two_or_dvd_of_dvd_two_mul_pow_self_two {m : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ 2 * m ^ 2) : p = 2 ∨ p ∣ int.nat_abs m := begin cases int.prime.dvd_mul hp h with hp2 hpp, { apply or.intro_left, exact le_antisymm (nat.le_of_dvd zero_lt_two hp2) (nat.prime.two_le hp) }, { apply or.intro_right, rw [pow_two, int.nat_abs_mul] at hpp, exact (or_self _).mp ((nat.prime.dvd_mul hp).mp hpp)} end instance nat.unique_units : unique (units ℕ) := { default := 1, uniq := nat.units_eq_one } open unique_factorization_monoid theorem nat.factors_eq {n : ℕ} : factors n = n.factors := begin cases n, {refl}, rw [← multiset.rel_eq, ← associated_eq_eq], apply factors_unique (irreducible_of_factor) _, { rw [multiset.coe_prod, nat.prod_factors (nat.succ_pos _)], apply factors_prod (nat.succ_ne_zero _) }, { apply_instance }, { intros x hx, rw [nat.irreducible_iff_prime, ← nat.prime_iff], apply nat.mem_factors hx, } end lemma nat.factors_multiset_prod_of_irreducible {s : multiset ℕ} (h : ∀ (x : ℕ), x ∈ s → irreducible x) : unique_factorization_monoid.factors (s.prod) = s := begin rw [← multiset.rel_eq, ← associated_eq_eq], apply (unique_factorization_monoid.factors_unique irreducible_of_factor h (factors_prod _)), rw [ne.def, multiset.prod_eq_zero_iff], intro con, exact not_irreducible_zero (h 0 con), end namespace multiplicity lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs := by simp only [finite_def, ← int.nat_abs_dvd_abs_iff, int.nat_abs_pow] lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) := begin have := int.nat_abs_eq a, have := @int.nat_abs_ne_zero_of_ne_zero b, rw [finite_int_iff_nat_abs_finite, finite_nat_iff, pos_iff_ne_zero], split; finish end instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_nat_iff.symm instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_int_iff.symm end multiplicity lemma induction_on_primes {P : ℕ → Prop} (h₀ : P 0) (h₁ : P 1) (h : ∀ p a : ℕ, p.prime → P a → P (p * a)) (n : ℕ) : P n := begin apply unique_factorization_monoid.induction_on_prime, exact h₀, { intros n h, rw nat.is_unit_iff.1 h, exact h₁, }, { intros a p _ hp ha, exact h p a (nat.prime_iff_prime.2 hp) ha, }, end lemma int.associated_nat_abs (k : ℤ) : associated k k.nat_abs := associated_of_dvd_dvd (int.coe_nat_dvd_right.mpr (dvd_refl _)) (int.nat_abs_dvd.mpr (dvd_refl _)) lemma int.prime_iff_nat_abs_prime {k : ℤ} : prime k ↔ nat.prime k.nat_abs := begin rw nat.prime_iff_prime_int, rw prime_iff_of_associated (int.associated_nat_abs k), end theorem int.associated_iff_nat_abs {a b : ℤ} : associated a b ↔ a.nat_abs = b.nat_abs := begin rw [←dvd_dvd_iff_associated, ←int.nat_abs_dvd_abs_iff, ←int.nat_abs_dvd_abs_iff, dvd_dvd_iff_associated], exact associated_iff_eq, end lemma int.associated_iff {a b : ℤ} : associated a b ↔ (a = b ∨ a = -b) := begin rw int.associated_iff_nat_abs, exact int.nat_abs_eq_nat_abs_iff, end
67f5401c6dbecf4e3f51308da96ebccd80828c0c
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/matrix/basic.lean
15e69ec7cbb8fb1a54dc98b8cb977a52bd7e136e
[ "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
58,984
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang -/ import algebra.big_operators.pi import algebra.module.pi import algebra.module.linear_map import algebra.big_operators.ring import algebra.star.pi import algebra.algebra.basic import data.equiv.ring import data.fintype.card import data.matrix.dmatrix /-! # Matrices This file defines basic properties of matrices. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ universes u u' v w open_locale big_operators open dmatrix /-- `matrix m n` is the type of matrices whose rows are indexed by `m` and whose columns are indexed by `n`. -/ def matrix (m : Type u) (n : Type u') (α : Type v) : Type (max u u' v) := m → n → α variables {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variables {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace matrix section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`. This is available in bundled forms as: * `add_monoid_hom.map_matrix` * `linear_map.map_matrix` * `ring_hom.map_matrix` * `alg_hom.map_matrix` * `equiv.map_matrix` * `add_equiv.map_matrix` * `linear_equiv.map_matrix` * `ring_equiv.map_matrix` * `alg_equiv.map_matrix` -/ def map (M : matrix m n α) (f : α → β) : matrix m n β := λ i j, f (M i j) @[simp] lemma map_apply {M : matrix m n α} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) := rfl @[simp] lemma map_id (M : matrix m n α) : M.map id = M := by { ext, refl, } @[simp] lemma map_map {M : matrix m n α} {β γ : Type*} {f : α → β} {g : β → γ} : (M.map f).map g = M.map (g ∘ f) := by { ext, refl, } /-- The transpose of a matrix. -/ def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix /-- The conjugate transpose of a matrix defined in term of `star`. -/ def conj_transpose [has_star α] (M : matrix m n α) : matrix n m α := M.transpose.map star localized "postfix `ᴴ`:1500 := matrix.conj_transpose" in matrix /-- `matrix.col u` is the column matrix whose entries are given by `u`. -/ def col (w : m → α) : matrix m unit α | x y := w x /-- `matrix.row u` is the row matrix whose entries are given by `u`. -/ def row (v : n → α) : matrix unit n α | x y := v y instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_zero_class α] : add_zero_class (matrix m n α) := pi.add_zero_class instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [has_sub α] : has_sub (matrix m n α) := pi.has_sub instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group instance [unique α] : unique (matrix m n α) := pi.unique instance [subsingleton α] : subsingleton (matrix m n α) := pi.subsingleton instance [nonempty m] [nonempty n] [nontrivial α] : nontrivial (matrix m n α) := function.nontrivial instance [has_scalar R α] : has_scalar R (matrix m n α) := pi.has_scalar instance [has_scalar R α] [has_scalar S α] [smul_comm_class R S α] : smul_comm_class R S (matrix m n α) := pi.smul_comm_class instance [has_scalar R S] [has_scalar R α] [has_scalar S α] [is_scalar_tower R S α] : is_scalar_tower R S (matrix m n α) := pi.is_scalar_tower instance [monoid R] [mul_action R α] : mul_action R (matrix m n α) := pi.mul_action _ instance [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R (matrix m n α) := pi.distrib_mul_action _ instance [semiring R] [add_comm_monoid α] [module R α] : module R (matrix m n α) := pi.module _ _ _ @[simp] lemma map_zero [has_zero α] [has_zero β] (f : α → β) (h : f 0 = 0) : (0 : matrix m n α).map f = 0 := by { ext, simp [h], } lemma map_add [has_add α] [has_add β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ + a₂) = f a₁ + f a₂) (M N : matrix m n α) : (M + N).map f = M.map f + N.map f := ext $ λ _ _, hf _ _ lemma map_sub [has_sub α] [has_sub β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ - a₂) = f a₁ - f a₂) (M N : matrix m n α) : (M - N).map f = M.map f - N.map f := ext $ λ _ _, hf _ _ lemma map_smul [has_scalar R α] [has_scalar R β] (f : α → β) (r : R) (hf : ∀ a, f (r • a) = r • f a) (M : matrix m n α) : (r • M).map f = r • (M.map f) := ext $ λ _ _, hf _ lemma _root_.is_smul_regular.matrix [has_scalar R S] {k : R} (hk : is_smul_regular S k) : is_smul_regular (matrix m n S) k := is_smul_regular.pi $ λ _, is_smul_regular.pi $ λ _, hk lemma _root_.is_left_regular.matrix [has_mul α] {k : α} (hk : is_left_regular k) : is_smul_regular (matrix m n α) k := hk.is_smul_regular.matrix -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_empty_left [is_empty m] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim i }⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_empty_right [is_empty n] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim j }⟩ end matrix open_locale matrix namespace matrix section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. Note that bundled versions exist as: * `matrix.diagonal_add_monoid_hom` * `matrix.diagonal_linear_map` * `matrix.diagonal_ring_hom` * `matrix.diagonal_alg_hom` -/ def diagonal [has_zero α] (d : n → α) : matrix n n α | i j := if i = j then d i else 0 @[simp] theorem diagonal_apply_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_apply_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_apply_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_apply_ne h.symm lemma diagonal_injective [has_zero α] : function.injective (diagonal : (n → α) → matrix n n α) := λ d₁ d₂ h, funext $ λ i, by simpa using matrix.ext_iff.mpr h i i @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by { ext, simp [diagonal] } @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_apply_ne' h] } end @[simp] theorem diagonal_add [add_zero_class α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] @[simp] theorem diagonal_smul [monoid R] [add_monoid α] [distrib_mul_action R α] (r : R) (d : n → α) : diagonal (r • d) = r • diagonal d := by ext i j; by_cases h : i = j; simp [h] variables (n α) /-- `matrix.diagonal` as an `add_monoid_hom`. -/ @[simps] def diagonal_add_monoid_hom [add_zero_class α] : (n → α) →+ matrix n n α := { to_fun := diagonal, map_zero' := diagonal_zero, map_add' := λ x y, (diagonal_add x y).symm,} variables (R) /-- `matrix.diagonal` as a `linear_map`. -/ @[simps] def diagonal_linear_map [semiring R] [add_comm_monoid α] [module R α] : (n → α) →ₗ[R] matrix n n α := { map_smul' := diagonal_smul, .. diagonal_add_monoid_hom n α,} variables {n α R} @[simp] lemma diagonal_map [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} : (diagonal d).map f = diagonal (λ m, f (d m)) := by { ext, simp only [diagonal, map_apply], split_ifs; simp [h], } @[simp] lemma diagonal_conj_transpose [semiring α] [star_ring α] (v : n → α) : (diagonal v)ᴴ = diagonal (star v) := begin rw [conj_transpose, diagonal_transpose, diagonal_map (star_zero _)], refl, end section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq i @[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_apply_ne theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_apply_ne' @[simp] lemma map_one [has_zero β] [has_one β] (f : α → β) (h₀ : f 0 = 0) (h₁ : f 1 = 1) : (1 : matrix n n α).map f = (1 : matrix n n β) := by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], } lemma one_eq_pi_single {i j} : (1 : matrix n n α) i j = pi.single i 1 j := by simp only [one_apply, pi.single_apply, eq_comm]; congr -- deal with decidable_eq end one section numeral @[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_monoid α] [has_one α] lemma bit1_apply (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_apply_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_apply] @[simp] lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_apply, h] end numeral end diagonal section dot_product variable [fintype m] /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i lemma dot_product_assoc [fintype n] [non_unital_semiring α] (u : m → α) (w : n → α) (v : matrix m n α) : dot_product (λ j, dot_product u (λ i, v i j)) w = dot_product u (λ i, dot_product (v i) w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [comm_semiring α] (v w : m → α) : dot_product v w = dot_product w v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : dot_product v w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] (u v w : m → α) @[simp] lemma dot_product_zero : dot_product v 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' : dot_product v (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product : dot_product 0 v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' : dot_product (λ _, (0 : α)) v = 0 := zero_dot_product v @[simp] lemma add_dot_product : dot_product (u + v) w = dot_product u w + dot_product v w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add : dot_product u (v + w) = dot_product u v + dot_product u w := by simp [dot_product, mul_add, finset.sum_add_distrib] end non_unital_non_assoc_semiring section non_unital_non_assoc_semiring_decidable variables [decidable_eq m] [non_unital_non_assoc_semiring α] (u v w : m → α) @[simp] lemma diagonal_dot_product (i : m) : dot_product (diagonal v i) w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal (i : m) : dot_product v (diagonal w i) = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal' (i : m) : dot_product v (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma single_dot_product (x : α) (i : m) : dot_product (pi.single i x) v = x * v i := have ∀ j ≠ i, pi.single i x j * v j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_single (x : α) (i : m) : dot_product v (pi.single i x) = v i * x := have ∀ j ≠ i, v j * pi.single i x j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp end non_unital_non_assoc_semiring_decidable section ring variables [ring α] (u v w : m → α) @[simp] lemma neg_dot_product : dot_product (-v) w = - dot_product v w := by simp [dot_product] @[simp] lemma dot_product_neg : dot_product v (-w) = - dot_product v w := by simp [dot_product] @[simp] lemma sub_dot_product : dot_product (u - v) w = dot_product u w - dot_product v w := by simp [sub_eq_add_neg] @[simp] lemma dot_product_sub : dot_product u (v - w) = dot_product u v - dot_product u w := by simp [sub_eq_add_neg] end ring section distrib_mul_action variables [monoid R] [has_mul α] [add_comm_monoid α] [distrib_mul_action R α] @[simp] lemma smul_dot_product [is_scalar_tower R α α] (x : R) (v w : m → α) : dot_product (x • v) w = x • dot_product v w := by simp [dot_product, finset.smul_sum, smul_mul_assoc] @[simp] lemma dot_product_smul [smul_comm_class R α α] (x : R) (v w : m → α) : dot_product v (x • w) = x • dot_product v w := by simp [dot_product, finset.smul_sum, mul_smul_comm] end distrib_mul_action section star_ring variables [semiring α] [star_ring α] (v w : m → α) lemma star_dot_product_star : dot_product (star v) (star w) = star (dot_product w v) := by simp [dot_product] lemma star_dot_product : dot_product (star v) w = star (dot_product (star w) v) := by simp [dot_product] lemma dot_product_star : dot_product v (star w) = star (dot_product w (star v)) := by simp [dot_product] end star_ring end dot_product /-- `M ⬝ N` is the usual product of matrices `M` and `N`, i.e. we have that `(M ⬝ N) i k` is the dot product of the `i`-th row of `M` by the `k`-th column of `N`. This is currently only defined when `m` is finite. -/ protected def mul [fintype m] [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, dot_product (λ j, M i j) (λ j, N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_apply [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [fintype n] [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [fintype n] [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_apply' [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = dot_product (λ j, M i j) (λ j, N j k) := rfl @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := ((diagonal_add_monoid_hom n α).map_neg d).symm lemma sum_apply [add_comm_monoid α] (i : m) (j : n) (s : finset β) (g : β → matrix m n α) : (∑ c in s, g c) i j = ∑ c in s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] @[simp] protected theorem mul_zero [fintype n] (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul [fintype m] (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add [fintype n] (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul [fintype m] (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } instance [fintype n] : non_unital_non_assoc_semiring (matrix n n α) := { mul := (*), add := (+), zero := 0, mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, .. matrix.add_comm_monoid} @[simp] theorem diagonal_mul [fintype m] [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [fintype n] [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] theorem diagonal_mul_diagonal [fintype n] [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [fintype n] [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ /-- Left multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_left [fintype m] (M : matrix l m α) : matrix m n α →+ matrix l n α := { to_fun := λ x, M ⬝ x, map_zero' := matrix.mul_zero _, map_add' := matrix.mul_add _ } /-- Right multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_right [fintype m] (M : matrix m n α) : matrix l m α →+ matrix l n α := { to_fun := λ x, x ⬝ M, map_zero' := matrix.zero_mul _, map_add' := λ _ _, matrix.add_mul _ _ _ } protected lemma sum_mul [fintype m] (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (add_monoid_hom_mul_right M : matrix l m α →+ _).map_sum f s protected lemma mul_sum [fintype m] (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (add_monoid_hom_mul_left M : matrix m n α →+ _).map_sum f s end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring α] @[simp] protected theorem one_mul [fintype m] [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [fintype n] [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [fintype n] [decidable_eq n] : non_assoc_semiring (matrix n n α) := { one := 1, one_mul := matrix.one_mul, mul_one := matrix.mul_one, .. matrix.non_unital_non_assoc_semiring } @[simp] lemma map_mul [fintype n] {L : matrix m n α} {M : matrix n o α} [non_assoc_semiring β] {f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f := by { ext, simp [mul_apply, ring_hom.map_sum], } variables (α n) /-- `matrix.diagonal` as a `ring_hom`. -/ @[simps] def diagonal_ring_hom [fintype n] [decidable_eq n] : (n → α) →+* matrix n n α := { to_fun := diagonal, map_one' := diagonal_one, map_mul' := λ _ _, (diagonal_mul_diagonal' _ _).symm, .. diagonal_add_monoid_hom n α } end non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] [fintype m] [fintype n] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : non_unital_semiring (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.non_unital_non_assoc_semiring } end non_unital_semiring section semiring variables [semiring α] instance [fintype n] [decidable_eq n] : semiring (matrix n n α) := { ..matrix.non_unital_semiring, ..matrix.non_assoc_semiring } end semiring section ring variables [ring α] [fintype n] @[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } protected theorem sub_mul (M M' : matrix m n α) (N : matrix n o α) : (M - M') ⬝ N = M ⬝ N - M' ⬝ N := by rw [sub_eq_add_neg, matrix.add_mul, neg_mul, sub_eq_add_neg] protected theorem mul_sub (M : matrix m n α) (N N' : matrix n o α) : M ⬝ (N - N') = M ⬝ N - M ⬝ N' := by rw [sub_eq_add_neg, matrix.mul_add, mul_neg, sub_eq_add_neg] end ring instance [fintype n] [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } section semiring variables [semiring α] lemma smul_eq_diagonal_mul [fintype m] [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } @[simp] lemma smul_mul [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (M : matrix m n α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } /-- This instance enables use with `smul_mul_assoc`. -/ instance semiring.is_scalar_tower [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] : is_scalar_tower R (matrix n n α) (matrix n n α) := ⟨λ r m n, matrix.smul_mul r m n⟩ @[simp] lemma mul_smul [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] (M : matrix m n α) (a : R) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } /-- This instance enables use with `mul_smul_comm`. -/ instance semiring.smul_comm_class [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] : smul_comm_class R (matrix n n α) (matrix n n α) := ⟨λ r m n, (matrix.mul_smul m r n).symm⟩ @[simp] lemma mul_mul_left [fintype n] (M : matrix m n α) (N : matrix n o α) (a : α) : (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) := smul_mul a M N /-- The ring homomorphism `α →+* matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α := { to_fun := λ a, a • 1, map_one' := by simp, map_mul' := by { intros, ext, simp [mul_assoc], }, .. (smul_add_hom α _).flip (1 : matrix n n α) } section scalar variables [decidable_eq n] [fintype n] @[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl lemma scalar_apply_eq (a : α) (i : n) : scalar n a i i = a := by simp only [coe_scalar, smul_eq_mul, mul_one, one_apply_eq, pi.smul_apply] lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) : scalar n a i j = 0 := by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, pi.smul_apply, smul_zero] lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := begin split, { intro h, inhabit n, rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] }, { rintro rfl, refl } end end scalar end semiring section comm_semiring variables [comm_semiring α] [fintype n] lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) : M ⬝ (λ i j, a * N i j) = a • (M ⬝ N) := mul_smul M a N lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M := by simp [commute, semiconj_by] end comm_semiring section algebra variables [fintype n] [decidable_eq n] variables [comm_semiring R] [semiring α] [semiring β] [algebra R α] [algebra R β] instance : algebra R (matrix n n α) := { commutes' := λ r x, begin ext, simp [matrix.scalar, matrix.mul_apply, matrix.one_apply, algebra.commutes, smul_ite], end, smul_def' := λ r x, begin ext, simp [matrix.scalar, algebra.smul_def'' r], end, ..((matrix.scalar n).comp (algebra_map R α)) } lemma algebra_map_matrix_apply {r : R} {i j : n} : algebra_map R (matrix n n α) r i j = if i = j then algebra_map R α r else 0 := begin dsimp [algebra_map, algebra.to_ring_hom, matrix.scalar], split_ifs with h; simp [h, matrix.one_apply_ne], end lemma algebra_map_eq_diagonal (r : R) : algebra_map R (matrix n n α) r = diagonal (algebra_map R (n → α) r) := matrix.ext $ λ i j, algebra_map_matrix_apply @[simp] lemma algebra_map_eq_smul (r : R) : algebra_map R (matrix n n R) r = r • (1 : matrix n n R) := rfl lemma algebra_map_eq_diagonal_ring_hom : algebra_map R (matrix n n α) = (diagonal_ring_hom n α).comp (algebra_map R _) := ring_hom.ext algebra_map_eq_diagonal @[simp] lemma map_algebra_map (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebra_map R α r) = algebra_map R β r) : (algebra_map R (matrix n n α) r).map f = algebra_map R (matrix n n β) r := begin rw [algebra_map_eq_diagonal, algebra_map_eq_diagonal, diagonal_map hf], congr' 1 with x, simp only [hf₂, pi.algebra_map_apply] end variables (R) /-- `matrix.diagonal` as an `alg_hom`. -/ @[simps] def diagonal_alg_hom : (n → α) →ₐ[R] matrix n n α := { to_fun := diagonal, commutes' := λ r, (algebra_map_eq_diagonal r).symm, .. diagonal_ring_hom n α } end algebra end matrix /-! ### Bundled versions of `matrix.map` -/ namespace equiv /-- The `equiv` between spaces of matrices induced by an `equiv` between their coefficients. This is `matrix.map` as an `equiv`. -/ @[simps apply] def map_matrix (f : α ≃ β) : matrix m n α ≃ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, left_inv := λ M, matrix.ext $ λ _ _, f.symm_apply_apply _, right_inv := λ M, matrix.ext $ λ _ _, f.apply_symm_apply _, } @[simp] lemma map_matrix_refl : (equiv.refl α).map_matrix = equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ β) (g : β ≃ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ _) := rfl end equiv namespace add_monoid_hom variables [add_zero_class α] [add_zero_class β] [add_zero_class γ] /-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their coefficients. This is `matrix.map` as an `add_monoid_hom`. -/ @[simps] def map_matrix (f : α →+ β) : matrix m n α →+ matrix m n β := { to_fun := λ M, M.map f, map_zero' := matrix.map_zero f f.map_zero, map_add' := matrix.map_add f f.map_add } @[simp] lemma map_matrix_id : (add_monoid_hom.id α).map_matrix = add_monoid_hom.id (matrix m n α) := rfl @[simp] lemma map_matrix_comp (f : β →+ γ) (g : α →+ β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →+ _) := rfl end add_monoid_hom namespace add_equiv variables [has_add α] [has_add β] [has_add γ] /-- The `add_equiv` between spaces of matrices induced by an `add_equiv` between their coefficients. This is `matrix.map` as an `add_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+ β) : matrix m n α ≃+ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, map_add' := matrix.map_add f f.map_add, .. f.to_equiv.map_matrix } @[simp] lemma map_matrix_refl : (add_equiv.refl α).map_matrix = add_equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃+ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃+ _) := rfl end add_equiv namespace linear_map variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_map` between spaces of matrices induced by a `linear_map` between their coefficients. This is `matrix.map` as a `linear_map`. -/ @[simps] def map_matrix (f : α →ₗ[R] β) : matrix m n α →ₗ[R] matrix m n β := { to_fun := λ M, M.map f, map_add' := matrix.map_add f f.map_add, map_smul' := λ r, matrix.map_smul f r (f.map_smul r), } @[simp] lemma map_matrix_id : linear_map.id.map_matrix = (linear_map.id : matrix m n α →ₗ[R] _) := rfl @[simp] lemma map_matrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →ₗ[R] _) := rfl end linear_map namespace linear_equiv variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_equiv` between spaces of matrices induced by an `linear_equiv` between their coefficients. This is `matrix.map` as an `linear_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₗ[R] β) : matrix m n α ≃ₗ[R] matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_equiv.map_matrix, .. f.to_linear_map.map_matrix } @[simp] lemma map_matrix_refl : (linear_equiv.refl R α).map_matrix = linear_equiv.refl R (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₗ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ₗ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ₗ[R] _) := rfl end linear_equiv namespace ring_hom variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_hom` between spaces of square matrices induced by a `ring_hom` between their coefficients. This is `matrix.map` as a `ring_hom`. -/ @[simps] def map_matrix (f : α →+* β) : matrix m m α →+* matrix m m β := { to_fun := λ M, M.map f, map_one' := by simp, map_mul' := λ L M, matrix.map_mul, .. f.to_add_monoid_hom.map_matrix } @[simp] lemma map_matrix_id : (ring_hom.id α).map_matrix = ring_hom.id (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →+* γ) (g : α →+* β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →+* _) := rfl end ring_hom namespace ring_equiv variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_equiv` between spaces of square matrices induced by a `ring_equiv` between their coefficients. This is `matrix.map` as a `ring_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+* β) : matrix m m α ≃+* matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_ring_hom.map_matrix, .. f.to_add_equiv.map_matrix } @[simp] lemma map_matrix_refl : (ring_equiv.refl α).map_matrix = ring_equiv.refl (matrix m m α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+* β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃+* _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃+* _) := rfl end ring_equiv namespace alg_hom variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_hom` between spaces of square matrices induced by a `alg_hom` between their coefficients. This is `matrix.map` as a `alg_hom`. -/ @[simps] def map_matrix (f : α →ₐ[R] β) : matrix m m α →ₐ[R] matrix m m β := { to_fun := λ M, M.map f, commutes' := λ r, matrix.map_algebra_map r f f.map_zero (f.commutes r), .. f.to_ring_hom.map_matrix } @[simp] lemma map_matrix_id : (alg_hom.id R α).map_matrix = alg_hom.id R (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →ₐ[R] _) := rfl end alg_hom namespace alg_equiv variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_equiv` between spaces of square matrices induced by a `alg_equiv` between their coefficients. This is `matrix.map` as a `alg_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₐ[R] β) : matrix m m α ≃ₐ[R] matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_alg_hom.map_matrix, .. f.to_ring_equiv.map_matrix } @[simp] lemma map_matrix_refl : alg_equiv.refl.map_matrix = (alg_equiv.refl : matrix m m α ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₐ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃ₐ[R] _) := rfl end alg_equiv open_locale matrix namespace matrix /-- For two vectors `w` and `v`, `vec_mul_vec w v i j` is defined to be `w i * v j`. Put another way, `vec_mul_vec w v` is exactly `col w ⬝ row v`. -/ def vec_mul_vec [has_mul α] (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] /-- `mul_vec M v` is the matrix-vector product of `M` and `v`, where `v` is seen as a column matrix. Put another way, `mul_vec M v` is the vector whose entries are those of `M ⬝ col v` (see `col_mul_vec`). -/ def mul_vec [fintype n] (M : matrix m n α) (v : n → α) : m → α | i := dot_product (λ j, M i j) v /-- `vec_mul v M` is the vector-matrix product of `v` and `M`, where `v` is seen as a row matrix. Put another way, `vec_mul v M` is the vector whose entries are those of `row v ⬝ M` (see `row_vec_mul`). -/ def vec_mul [fintype m] (v : m → α) (M : matrix m n α) : n → α | j := dot_product v (λ i, M i j) /-- Left multiplication by a matrix, as an `add_monoid_hom` from vectors to vectors. -/ @[simps] def mul_vec.add_monoid_hom_left [fintype n] (v : n → α) : matrix m n α →+ m → α := { to_fun := λ M, mul_vec M v, map_zero' := by ext; simp [mul_vec]; refl, map_add' := λ x y, by { ext m, apply add_dot_product } } lemma mul_vec_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x /-- Associate the dot product of `mul_vec` to the left. -/ lemma dot_product_mul_vec [fintype n] [fintype m] [non_unital_semiring R] (v : m → R) (A : matrix m n R) (w : n → R) : dot_product v (mul_vec A w) = dot_product (vec_mul v A) w := by simp only [dot_product, vec_mul, mul_vec, finset.mul_sum, finset.sum_mul, mul_assoc]; exact finset.sum_comm @[simp] lemma mul_vec_zero [fintype n] (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma zero_vec_mul [fintype m] (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma zero_mul_vec [fintype n] (v : n → α) : mul_vec (0 : matrix m n α) v = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero [fintype m] (v : m → α) : vec_mul v (0 : matrix m n α) = 0 := by { ext, simp [vec_mul] } lemma vec_mul_vec_eq (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp [vec_mul_vec, mul_apply], refl } lemma smul_mul_vec_assoc [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (A : matrix m n α) (b : n → α) : (a • A).mul_vec b = a • (A.mul_vec b) := by { ext, apply smul_dot_product, } lemma mul_vec_add [fintype n] (A : matrix m n α) (x y : n → α) : A.mul_vec (x + y) = A.mul_vec x + A.mul_vec y := by { ext, apply dot_product_add } lemma add_mul_vec [fintype n] (A B : matrix m n α) (x : n → α) : (A + B).mul_vec x = A.mul_vec x + B.mul_vec x := by { ext, apply add_dot_product } lemma vec_mul_add [fintype m] (A B : matrix m n α) (x : m → α) : vec_mul x (A + B) = vec_mul x A + vec_mul x B := by { ext, apply dot_product_add } lemma add_vec_mul [fintype m] (A : matrix m n α) (x y : m → α) : vec_mul (x + y) A = vec_mul x A + vec_mul y A := by { ext, apply add_dot_product } lemma vec_mul_smul [fintype n] [comm_semiring R] [semiring S] [algebra R S] (M : matrix n m S) (b : R) (v : n → S) : M.vec_mul (b • v) = b • M.vec_mul v := by { ext i, simp only [vec_mul, dot_product, finset.smul_sum, pi.smul_apply, smul_mul_assoc] } lemma mul_vec_smul [fintype n] [comm_semiring R] [semiring S] [algebra R S] (M : matrix m n S) (b : R) (v : n → S) : M.mul_vec (b • v) = b • M.mul_vec v := by { ext i, simp only [mul_vec, dot_product, finset.smul_sum, pi.smul_apply, mul_smul_comm] } end non_unital_non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] [fintype n] @[simp] lemma vec_mul_vec_mul [fintype m] (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec [fintype o] (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } end non_unital_semiring section non_assoc_semiring variables [fintype m] [decidable_eq m] [non_assoc_semiring α] @[simp] lemma one_mul_vec (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } end non_assoc_semiring section ring variables [ring α] lemma neg_vec_mul [fintype m] (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg [fintype m] (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec [fintype n] (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg [fintype n] (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } end ring section comm_semiring variables [comm_semiring α] lemma mul_vec_smul_assoc [fintype n] (A : matrix m n α) (b : n → α) (a : α) : A.mul_vec (a • b) = a • (A.mul_vec b) := by { ext, apply dot_product_smul } lemma mul_vec_transpose [fintype m] (A : matrix m n α) (x : m → α) : mul_vec Aᵀ x = vec_mul x A := by { ext, apply dot_product_comm } lemma vec_mul_transpose [fintype n] (A : matrix m n α) (x : n → α) : vec_mul x Aᵀ = mul_vec A x := by { ext, apply dot_product_comm } end comm_semiring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma transpose_apply (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, unfold has_one.one transpose, by_cases i = j, { simp only [h, diagonal_apply_eq] }, { simp only [diagonal_apply_ne h, diagonal_apply_ne (λ p, h (symm p))] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_sub [has_sub α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᵀ = Mᵀ - Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [comm_semiring α] [fintype n] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul {R : Type*} [has_scalar R α] (c : R) (M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [has_neg α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl lemma transpose_map {f : α → β} {M : matrix m n α} : Mᵀ.map f = (M.map f)ᵀ := by { ext, refl } end transpose section conj_transpose open_locale matrix /-- Tell `simp` what the entries are in a conjugate transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma conj_transpose_apply [has_star α] (M : matrix m n α) (i j) : M.conj_transpose j i = star (M i j) := rfl @[simp] lemma conj_transpose_conj_transpose [has_involutive_star α] (M : matrix m n α) : Mᴴᴴ = M := by ext; simp @[simp] lemma conj_transpose_zero [semiring α] [star_ring α] : (0 : matrix m n α)ᴴ = 0 := by ext i j; simp @[simp] lemma conj_transpose_one [decidable_eq n] [semiring α] [star_ring α]: (1 : matrix n n α)ᴴ = 1 := by simp [conj_transpose] @[simp] lemma conj_transpose_add [semiring α] [star_ring α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᴴ = Mᴴ + Nᴴ := by ext i j; simp @[simp] lemma conj_transpose_sub [ring α] [star_ring α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᴴ = Mᴴ - Nᴴ := by ext i j; simp @[simp] lemma conj_transpose_smul [comm_monoid α] [star_monoid α] (c : α) (M : matrix m n α) : (c • M)ᴴ = (star c) • Mᴴ := by ext i j; simp [mul_comm] @[simp] lemma conj_transpose_mul [fintype n] [semiring α] [star_ring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᴴ = Nᴴ ⬝ Mᴴ := by ext i j; simp [mul_apply] @[simp] lemma conj_transpose_neg [ring α] [star_ring α] (M : matrix m n α) : (- M)ᴴ = - Mᴴ := by ext i j; simp end conj_transpose section star /-- When `α` has a star operation, square matrices `matrix n n α` have a star operation equal to `matrix.conj_transpose`. -/ instance [has_star α] : has_star (matrix n n α) := {star := conj_transpose} lemma star_eq_conj_transpose [has_star α] (M : matrix m m α) : star M = Mᴴ := rfl @[simp] lemma star_apply [has_star α] (M : matrix n n α) (i j) : (star M) i j = star (M j i) := rfl instance [has_involutive_star α] : has_involutive_star (matrix n n α) := { star_involutive := conj_transpose_conj_transpose } /-- When `α` is a `*`-(semi)ring, `matrix.has_star` is also a `*`-(semi)ring. -/ instance [fintype n] [decidable_eq n] [semiring α] [star_ring α] : star_ring (matrix n n α) := { star_add := conj_transpose_add, star_mul := conj_transpose_mul, } /-- A version of `star_mul` for `⬝` instead of `*`. -/ lemma star_mul [fintype n] [semiring α] [star_ring α] (M N : matrix n n α) : star (M ⬝ N) = star N ⬝ star M := conj_transpose_mul _ _ end star /-- Given maps `(r_reindex : l → m)` and `(c_reindex : o → n)` reindexing the rows and columns of a matrix `M : matrix m n α`, the matrix `M.minor r_reindex c_reindex : matrix l o α` is defined by `(M.minor r_reindex c_reindex) i j = M (r_reindex i) (c_reindex j)` for `(i,j) : l × o`. Note that the total number of row and columns does not have to be preserved. -/ def minor (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : matrix l o α := λ i j, A (r_reindex i) (c_reindex j) @[simp] lemma minor_apply (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) (i j) : A.minor r_reindex c_reindex i j = A (r_reindex i) (c_reindex j) := rfl @[simp] lemma minor_id_id (A : matrix m n α) : A.minor id id = A := ext $ λ _ _, rfl @[simp] lemma minor_minor {l₂ o₂ : Type*} (A : matrix m n α) (r₁ : l → m) (c₁ : o → n) (r₂ : l₂ → l) (c₂ : o₂ → o) : (A.minor r₁ c₁).minor r₂ c₂ = A.minor (r₁ ∘ r₂) (c₁ ∘ c₂) := ext $ λ _ _, rfl @[simp] lemma transpose_minor (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.minor r_reindex c_reindex)ᵀ = Aᵀ.minor c_reindex r_reindex := ext $ λ _ _, rfl @[simp] lemma conj_transpose_minor [has_star α] (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.minor r_reindex c_reindex)ᴴ = Aᴴ.minor c_reindex r_reindex := ext $ λ _ _, rfl lemma minor_add [has_add α] (A B : matrix m n α) : ((A + B).minor : (l → m) → (o → n) → matrix l o α) = A.minor + B.minor := rfl lemma minor_neg [has_neg α] (A : matrix m n α) : ((-A).minor : (l → m) → (o → n) → matrix l o α) = -A.minor := rfl lemma minor_sub [has_sub α] (A B : matrix m n α) : ((A - B).minor : (l → m) → (o → n) → matrix l o α) = A.minor - B.minor := rfl @[simp] lemma minor_zero [has_zero α] : ((0 : matrix m n α).minor : (l → m) → (o → n) → matrix l o α) = 0 := rfl lemma minor_smul {R : Type*} [semiring R] [add_comm_monoid α] [module R α] (r : R) (A : matrix m n α) : ((r • A : matrix m n α).minor : (l → m) → (o → n) → matrix l o α) = r • A.minor := rfl lemma minor_map (f : α → β) (e₁ : l → m) (e₂ : o → n) (A : matrix m n α) : (A.map f).minor e₁ e₂ = (A.minor e₁ e₂).map f := rfl /-- Given a `(m × m)` diagonal matrix defined by a map `d : m → α`, if the reindexing map `e` is injective, then the resulting matrix is again diagonal. -/ lemma minor_diagonal [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l → m) (he : function.injective e) : (diagonal d).minor e e = diagonal (d ∘ e) := ext $ λ i j, begin rw minor_apply, by_cases h : i = j, { rw [h, diagonal_apply_eq, diagonal_apply_eq], }, { rw [diagonal_apply_ne h, diagonal_apply_ne (he.ne h)], }, end lemma minor_one [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l → m) (he : function.injective e) : (1 : matrix m m α).minor e e = 1 := minor_diagonal _ e he lemma minor_mul [fintype n] [fintype o] [semiring α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o → n) (e₃ : q → p) (he₂ : function.bijective e₂) : (M ⬝ N).minor e₁ e₃ = (M.minor e₁ e₂) ⬝ (N.minor e₂ e₃) := ext $ λ _ _, (he₂.sum_comp _).symm /-! `simp` lemmas for `matrix.minor`s interaction with `matrix.diagonal`, `1`, and `matrix.mul` for when the mappings are bundled. -/ @[simp] lemma minor_diagonal_embedding [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ↪ m) : (diagonal d).minor e e = diagonal (d ∘ e) := minor_diagonal d e e.injective @[simp] lemma minor_diagonal_equiv [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ≃ m) : (diagonal d).minor e e = diagonal (d ∘ e) := minor_diagonal d e e.injective @[simp] lemma minor_one_embedding [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ↪ m) : (1 : matrix m m α).minor e e = 1 := minor_one e e.injective @[simp] lemma minor_one_equiv [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ≃ m) : (1 : matrix m m α).minor e e = 1 := minor_one e e.injective lemma minor_mul_equiv [fintype n] [fintype o] [semiring α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o ≃ n) (e₃ : q → p) : (M ⬝ N).minor e₁ e₃ = (M.minor e₁ e₂) ⬝ (N.minor e₂ e₃) := minor_mul M N e₁ e₂ e₃ e₂.bijective lemma mul_minor_one [fintype n] [fintype o] [semiring α] [decidable_eq o] (e₁ : n ≃ o) (e₂ : l → o) (M : matrix m n α) : M ⬝ (1 : matrix o o α).minor e₁ e₂ = minor M id (e₁.symm ∘ e₂) := begin let A := M.minor id e₁.symm, have : M = A.minor id e₁, { simp only [minor_minor, function.comp.right_id, minor_id_id, equiv.symm_comp_self], }, rw [this, ←minor_mul_equiv], simp only [matrix.mul_one, minor_minor, function.comp.right_id, minor_id_id, equiv.symm_comp_self], end lemma one_minor_mul [fintype m] [fintype o] [semiring α] [decidable_eq o] (e₁ : l → o) (e₂ : m ≃ o) (M : matrix m n α) : ((1 : matrix o o α).minor e₁ e₂).mul M = minor M (e₂.symm ∘ e₁) id := begin let A := M.minor e₂.symm id, have : M = A.minor e₂ id, { simp only [minor_minor, function.comp.right_id, minor_id_id, equiv.symm_comp_self], }, rw [this, ←minor_mul_equiv], simp only [matrix.one_mul, minor_minor, function.comp.right_id, minor_id_id, equiv.symm_comp_self], end /-- The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence. -/ def reindex (eₘ : m ≃ l) (eₙ : n ≃ o) : matrix m n α ≃ matrix l o α := { to_fun := λ M, M.minor eₘ.symm eₙ.symm, inv_fun := λ M, M.minor eₘ eₙ, left_inv := λ M, by simp, right_inv := λ M, by simp, } @[simp] lemma reindex_apply (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : reindex eₘ eₙ M = M.minor eₘ.symm eₙ.symm := rfl @[simp] lemma reindex_refl_refl (A : matrix m n α) : reindex (equiv.refl _) (equiv.refl _) A = A := A.minor_id_id @[simp] lemma reindex_symm (eₘ : m ≃ l) (eₙ : n ≃ o) : (reindex eₘ eₙ).symm = (reindex eₘ.symm eₙ.symm : matrix l o α ≃ _) := rfl @[simp] lemma reindex_trans {l₂ o₂ : Type*} (eₘ : m ≃ l) (eₙ : n ≃ o) (eₘ₂ : l ≃ l₂) (eₙ₂ : o ≃ o₂) : (reindex eₘ eₙ).trans (reindex eₘ₂ eₙ₂) = (reindex (eₘ.trans eₘ₂) (eₙ.trans eₙ₂) : matrix m n α ≃ _) := equiv.ext $ λ A, (A.minor_minor eₘ.symm eₙ.symm eₘ₂.symm eₙ₂.symm : _) lemma transpose_reindex (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᵀ = (reindex eₙ eₘ Mᵀ) := rfl lemma conj_transpose_reindex [has_star α] (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᴴ = (reindex eₙ eₘ Mᴴ) := rfl /-- The left `n × l` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := minor A id (fin.cast_add r) /-- The right `n × r` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := minor A id (fin.nat_add l) /-- The top `u × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := minor A (fin.cast_add d) id /-- The bottom `d × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := minor A (fin.nat_add u) id /-- The top-right `u × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) /-- The bottom-right `d × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) /-- The top-left `u × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) /-- The bottom-left `d × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [has_add α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [has_scalar R α] (x : R) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [has_add α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [has_scalar R α] (x : R) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma col_apply (v : m → α) (i j) : matrix.col v i j = v i := rfl @[simp] lemma row_apply (v : m → α) (i j) : matrix.row v i j = v j := rfl @[simp] lemma transpose_col (v : m → α) : (matrix.col v)ᵀ = matrix.row v := by { ext, refl } @[simp] lemma transpose_row (v : m → α) : (matrix.row v)ᵀ = matrix.col v := by { ext, refl } @[simp] lemma conj_transpose_col [has_star α] (v : m → α) : (col v)ᴴ = row (star v) := by { ext, refl } @[simp] lemma conj_transpose_row [has_star α] (v : m → α) : (row v)ᴴ = col (star v) := by { ext, refl } lemma row_vec_mul [fintype m] [semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [fintype m] [semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [fintype n] [semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [fintype n] [semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} @[simp] lemma row_mul_col_apply [fintype m] [has_mul α] [add_comm_monoid α] (v w : m → α) (i j) : (row v ⬝ col w) i j = dot_product v w := rfl end row_col section update /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row [decidable_eq n] (M : matrix n m α) (i : n) (b : m → α) : matrix n m α := function.update M i b /-- Update, i.e. replace the `j`th column of matrix `A` with the values in `b`. -/ def update_column [decidable_eq m] (M : matrix n m α) (j : m) (b : n → α) : matrix n m α := λ i, function.update (M i) j (b i) variables {M : matrix n m α} {i : n} {j : m} {b : m → α} {c : n → α} @[simp] lemma update_row_self [decidable_eq n] : update_row M i b i = b := function.update_same i b M @[simp] lemma update_column_self [decidable_eq m] : update_column M j c i j = c i := function.update_same j (c i) (M i) @[simp] lemma update_row_ne [decidable_eq n] {i' : n} (i_ne : i' ≠ i) : update_row M i b i' = M i' := function.update_noteq i_ne b M @[simp] lemma update_column_ne [decidable_eq m] {j' : m} (j_ne : j' ≠ j) : update_column M j c i j' = M i j' := function.update_noteq j_ne (c i) (M i) lemma update_row_apply [decidable_eq n] {i' : n} : update_row M i b i' j = if i' = i then b j else M i' j := begin by_cases i' = i, { rw [h, update_row_self, if_pos rfl] }, { rwa [update_row_ne h, if_neg h] } end lemma update_column_apply [decidable_eq m] {j' : m} : update_column M j c i j' = if j' = j then c i else M i j' := begin by_cases j' = j, { rw [h, update_column_self, if_pos rfl] }, { rwa [update_column_ne h, if_neg h] } end @[simp] lemma update_column_subsingleton [subsingleton m] (A : matrix n m R) (i : m) (b : n → R) : A.update_column i b = (col b).minor id (function.const m ()) := begin ext x y, simp [update_column_apply, subsingleton.elim i y] end @[simp] lemma update_row_subsingleton [subsingleton n] (A : matrix n m R) (i : n) (b : m → R) : A.update_row i b = (row b).minor (function.const n ()) id := begin ext x y, simp [update_column_apply, subsingleton.elim i x] end lemma map_update_row [decidable_eq n] (f : α → β) : map (update_row M i b) f = update_row (M.map f) i (f ∘ b) := begin ext i' j', rw [update_row_apply, map_apply, map_apply, update_row_apply], exact apply_ite f _ _ _, end lemma map_update_column [decidable_eq m] (f : α → β) : map (update_column M j c) f = update_column (M.map f) j (f ∘ c) := begin ext i' j', rw [update_column_apply, map_apply, map_apply, update_column_apply], exact apply_ite f _ _ _, end lemma update_row_transpose [decidable_eq m] : update_row Mᵀ j c = (update_column M j c)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_column_transpose [decidable_eq n] : update_column Mᵀ i b = (update_row M i b)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_row_conj_transpose [decidable_eq m] [has_star α] : update_row Mᴴ j (star c) = (update_column M j c)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_row_transpose, map_update_column], refl, end lemma update_column_conj_transpose [decidable_eq n] [has_star α] : update_column Mᴴ i (star b) = (update_row M i b)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_column_transpose, map_update_row], refl, end @[simp] lemma update_row_eq_self [decidable_eq m] (A : matrix m n α) {i : m} : A.update_row i (A i) = A := function.update_eq_self i A @[simp] lemma update_column_eq_self [decidable_eq n] (A : matrix m n α) {i : n} : A.update_column i (λ j, A j i) = A := funext $ λ j, function.update_eq_self i (A j) end update end matrix namespace ring_hom variables [fintype n] [semiring α] [semiring β] lemma map_matrix_mul (M : matrix m n α) (N : matrix n o α) (i : m) (j : o) (f : α →+* β) : f (matrix.mul M N i j) = matrix.mul (λ i j, f (M i j)) (λ i j, f (N i j)) i j := by simp [matrix.mul_apply, ring_hom.map_sum] lemma map_dot_product [semiring R] [semiring S] (f : R →+* S) (v w : n → R) : f (matrix.dot_product v w) = matrix.dot_product (f ∘ v) (f ∘ w) := by simp only [matrix.dot_product, f.map_sum, f.map_mul] lemma map_vec_mul [semiring R] [semiring S] (f : R →+* S) (M : matrix n m R) (v : n → R) (i : m) : f (M.vec_mul v i) = ((M.map f).vec_mul (f ∘ v) i) := by simp only [matrix.vec_mul, matrix.map_apply, ring_hom.map_dot_product] lemma map_mul_vec [semiring R] [semiring S] (f : R →+* S) (M : matrix m n R) (v : n → R) (i : m) : f (M.mul_vec v i) = ((M.map f).mul_vec (f ∘ v) i) := by simp only [matrix.mul_vec, matrix.map_apply, ring_hom.map_dot_product] end ring_hom
e48c2e752ea7430eb5d059933409a28d7e8e56ff
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/group_action/prod.lean
c7289b6e30868b69c5fd2b42abdc41d4df528747
[ "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
4,400
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Eric Wieser -/ import algebra.group.prod import group_theory.group_action.defs /-! # Prod instances for additive and multiplicative actions This file defines instances for binary product of additive and multiplicative actions and provides scalar multiplication as a homomorphism from `α × β` to `β`. ## Main declarations * `smul_mul_hom`/`smul_monoid_hom`: Scalar multiplication bundled as a multiplicative/monoid homomorphism. -/ variables {M N P α β : Type*} namespace prod section variables [has_scalar M α] [has_scalar M β] [has_scalar N α] [has_scalar N β] (a : M) (x : α × β) @[to_additive prod.has_vadd] instance : has_scalar M (α × β) := ⟨λa p, (a • p.1, a • p.2)⟩ @[simp, to_additive] theorem smul_fst : (a • x).1 = a • x.1 := rfl @[simp, to_additive] theorem smul_snd : (a • x).2 = a • x.2 := rfl @[simp, to_additive] theorem smul_mk (a : M) (b : α) (c : β) : a • (b, c) = (a • b, a • c) := rfl @[to_additive] theorem smul_def (a : M) (x : α × β) : a • x = (a • x.1, a • x.2) := rfl instance [has_scalar M N] [is_scalar_tower M N α] [is_scalar_tower M N β] : is_scalar_tower M N (α × β) := ⟨λ x y z, mk.inj_iff.mpr ⟨smul_assoc _ _ _, smul_assoc _ _ _⟩⟩ @[to_additive] instance [smul_comm_class M N α] [smul_comm_class M N β] : smul_comm_class M N (α × β) := { smul_comm := λ r s x, mk.inj_iff.mpr ⟨smul_comm _ _ _, smul_comm _ _ _⟩ } instance [has_scalar Mᵐᵒᵖ α] [has_scalar Mᵐᵒᵖ β] [is_central_scalar M α] [is_central_scalar M β] : is_central_scalar M (α × β) := ⟨λ r m, prod.ext (op_smul_eq_smul _ _) (op_smul_eq_smul _ _)⟩ @[to_additive has_faithful_vadd_left] instance has_faithful_scalar_left [has_faithful_scalar M α] [nonempty β] : has_faithful_scalar M (α × β) := ⟨λ x y h, let ⟨b⟩ := ‹nonempty β› in eq_of_smul_eq_smul $ λ a : α, by injection h (a, b)⟩ @[to_additive has_faithful_vadd_right] instance has_faithful_scalar_right [nonempty α] [has_faithful_scalar M β] : has_faithful_scalar M (α × β) := ⟨λ x y h, let ⟨a⟩ := ‹nonempty α› in eq_of_smul_eq_smul $ λ b : β, by injection h (a, b)⟩ end @[to_additive] instance smul_comm_class_both [monoid N] [monoid P] [has_scalar M N] [has_scalar M P] [smul_comm_class M N N] [smul_comm_class M P P] : smul_comm_class M (N × P) (N × P) := ⟨λ c x y, by simp [smul_def, mul_def, mul_smul_comm]⟩ instance is_scalar_tower_both [monoid N] [monoid P] [has_scalar M N] [has_scalar M P] [is_scalar_tower M N N] [is_scalar_tower M P P] : is_scalar_tower M (N × P) (N × P) := ⟨λ c x y, by simp [smul_def, mul_def, smul_mul_assoc]⟩ @[to_additive] instance {m : monoid M} [mul_action M α] [mul_action M β] : mul_action M (α × β) := { mul_smul := λ a₁ a₂ p, mk.inj_iff.mpr ⟨mul_smul _ _ _, mul_smul _ _ _⟩, one_smul := λ ⟨b, c⟩, mk.inj_iff.mpr ⟨one_smul _ _, one_smul _ _⟩ } instance {R M N : Type*} {r : monoid R} [add_monoid M] [add_monoid N] [distrib_mul_action R M] [distrib_mul_action R N] : distrib_mul_action R (M × N) := { smul_add := λ a p₁ p₂, mk.inj_iff.mpr ⟨smul_add _ _ _, smul_add _ _ _⟩, smul_zero := λ a, mk.inj_iff.mpr ⟨smul_zero _, smul_zero _⟩ } instance {R M N : Type*} {r : monoid R} [monoid M] [monoid N] [mul_distrib_mul_action R M] [mul_distrib_mul_action R N] : mul_distrib_mul_action R (M × N) := { smul_mul := λ a p₁ p₂, mk.inj_iff.mpr ⟨smul_mul' _ _ _, smul_mul' _ _ _⟩, smul_one := λ a, mk.inj_iff.mpr ⟨smul_one _, smul_one _⟩ } end prod /-! ### Scalar multiplication as a homomorphism -/ section bundled_smul /-- Scalar multiplication as a multiplicative homomorphism. -/ @[simps] def smul_mul_hom [monoid α] [has_mul β] [mul_action α β] [is_scalar_tower α β β] [smul_comm_class α β β] : mul_hom (α × β) β := { to_fun := λ a, a.1 • a.2, map_mul' := λ a b, (smul_mul_smul _ _ _ _).symm } /-- Scalar multiplication as a monoid homomorphism. -/ @[simps] def smul_monoid_hom [monoid α] [mul_one_class β] [mul_action α β] [is_scalar_tower α β β] [smul_comm_class α β β] : α × β →* β := { map_one' := one_smul _ _, .. smul_mul_hom } end bundled_smul
f1e5cc0893d73599fcefb1a8403f3b19f54119a4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/convex/star.lean
6544563a61111f4d918acca2e21778c62ebdf381
[ "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
15,997
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 analysis.convex.segment /-! # Star-convex sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This files defines star-convex sets (aka star domains, star-shaped set, radially convex set). A set is star-convex at `x` if every segment from `x` to a point in the set is contained in the set. This is the prototypical example of a contractible set in homotopy theory (by scaling every point towards `x`), but has wider uses. Note that this has nothing to do with star rings, `has_star` and co. ## Main declarations * `star_convex 𝕜 x s`: `s` is star-convex at `x` with scalars `𝕜`. ## Implementation notes Instead of saying that a set is star-convex, we say a set is star-convex *at a point*. This has the advantage of allowing us to talk about convexity as being "everywhere star-convexity" and of making the union of star-convex sets be star-convex. Incidentally, this choice means we don't need to assume a set is nonempty for it to be star-convex. Concretely, the empty set is star-convex at every point. ## TODO Balanced sets are star-convex. The closure of a star-convex set is star-convex. Star-convex sets are contractible. A nonempty open star-convex set in `ℝ^n` is diffeomorphic to the entire space. -/ open set open_locale convex pointwise variables {𝕜 E F : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 F] (x : E) (s : set E) /-- Star-convexity of sets. `s` is star-convex at `x` if every segment from `x` to a point in `s` is contained in `s`. -/ def star_convex : Prop := ∀ ⦃y : E⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s variables {𝕜 x s} {t : set E} lemma star_convex_iff_segment_subset : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → [x -[𝕜] y] ⊆ s := begin split, { rintro h y hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hy ha hb hab }, { rintro h y hy a b ha hb hab, exact h hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma star_convex.segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : [x -[𝕜] y] ⊆ s := star_convex_iff_segment_subset.1 h hy lemma star_convex.open_segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hy) /-- Alternative definition of star-convexity, in terms of pointwise set operations. -/ lemma star_convex_iff_pointwise_add_subset : star_convex 𝕜 x s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • {x} + b • s ⊆ s := begin refine ⟨_, λ h y hy a b ha hb hab, h ha hb hab (add_mem_add (smul_mem_smul_set $ mem_singleton _) ⟨_, hy, rfl⟩)⟩, rintro hA a b ha hb hab w ⟨au, bv, ⟨u, (rfl : u = x), rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hv ha hb hab, end lemma star_convex_empty (x : E) : star_convex 𝕜 x ∅ := λ y hy, hy.elim lemma star_convex_univ (x : E) : star_convex 𝕜 x univ := λ _ _ _ _ _ _ _, trivial lemma star_convex.inter (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∩ t) := λ y hy a b ha hb hab, ⟨hs hy.left ha hb hab, ht hy.right ha hb hab⟩ lemma star_convex_sInter {S : set (set E)} (h : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋂₀ S) := λ y hy a b ha hb hab s hs, h s hs (hy s hs) ha hb hab lemma star_convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋂ i, s i) := (sInter_range s) ▸ star_convex_sInter $ forall_range_iff.2 h lemma star_convex.union (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∪ t) := begin rintro y (hy | hy) a b ha hb hab, { exact or.inl (hs hy ha hb hab) }, { exact or.inr (ht hy ha hb hab) } end lemma star_convex_Union {ι : Sort*} {s : ι → set E} (hs : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋃ i, s i) := begin rintro y hy a b ha hb hab, rw mem_Union at ⊢ hy, obtain ⟨i, hy⟩ := hy, exact ⟨i, hs i hy ha hb hab⟩, end lemma star_convex_sUnion {S : set (set E)} (hS : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋃₀ S) := begin rw sUnion_eq_Union, exact star_convex_Union (λ s, hS _ s.2), end lemma star_convex.prod {y : F} {s : set E} {t : set F} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x, y) (s ×ˢ t) := λ y hy a b ha hb hab, ⟨hs hy.1 ha hb hab, ht hy.2 ha hb hab⟩ lemma star_convex_pi {ι : Type*} {E : ι → Type*} [Π i, add_comm_monoid (E i)] [Π i, has_smul 𝕜 (E i)] {x : Π i, E i} {s : set ι} {t : Π i, set (E i)} (ht : ∀ ⦃i⦄, i ∈ s → star_convex 𝕜 (x i) (t i)) : star_convex 𝕜 x (s.pi t) := λ y hy a b ha hb hab i hi, ht hi (hy i hi) ha hb hab end has_smul section module variables [module 𝕜 E] [module 𝕜 F] {x y z : E} {s : set E} lemma star_convex.mem (hs : star_convex 𝕜 x s) (h : s.nonempty) : x ∈ s := begin obtain ⟨y, hy⟩ := h, convert hs hy zero_le_one le_rfl (add_zero 1), rw [one_smul, zero_smul, add_zero], end lemma star_convex_iff_forall_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y hy a b ha hb hab, obtain rfl | ha := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_smul, zero_smul, zero_add] }, obtain rfl | hb := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_smul, zero_smul, add_zero] }, exact h hy ha hb hab, end lemma star_convex_iff_forall_ne_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy _ a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] }, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, exact h hy hxy ha' hb' hab, end lemma star_convex_iff_open_segment_subset (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → open_segment 𝕜 x y ⊆ s := star_convex_iff_segment_subset.trans $ forall₂_congr $ λ y hy, (open_segment_subset_iff_segment_subset hx hy).symm lemma star_convex_singleton (x : E) : star_convex 𝕜 x {x} := begin rintro y (rfl : y = x) a b ha hb hab, exact convex.combo_self hab _, end lemma star_convex.linear_image (hs : star_convex 𝕜 x s) (f : E →ₗ[𝕜] F) : star_convex 𝕜 (f x) (s.image f) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, exact ⟨a • x + b • y', hs hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma star_convex.is_linear_image (hs : star_convex 𝕜 x s) {f : E → F} (hf : is_linear_map 𝕜 f) : star_convex 𝕜 (f x) (f '' s) := hs.linear_image $ hf.mk' f lemma star_convex.linear_preimage {s : set F} (f : E →ₗ[𝕜] F) (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (s.preimage f) := begin intros y hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hy ha hb hab, end lemma star_convex.is_linear_preimage {s : set F} {f : E → F} (hs : star_convex 𝕜 (f x) s) (hf : is_linear_map 𝕜 f) : star_convex 𝕜 x (preimage f s) := hs.linear_preimage $ hf.mk' f lemma star_convex.add {t : set E} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x + y) (s + t) := by { rw ←add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma star_convex.add_left (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (z + x) ((λ x, z + x) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end lemma star_convex.add_right (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (x + z) ((λ x, x + z) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a star-convex set is also star-convex. -/ lemma star_convex.preimage_add_right (hs : star_convex 𝕜 (z + x) s) : star_convex 𝕜 x ((λ x, z + x) ⁻¹' s) := begin intros y hy a b ha hb hab, have h := hs hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a star-convex set is also star-convex. -/ lemma star_convex.preimage_add_left (hs : star_convex 𝕜 (x + z) s) : star_convex 𝕜 x ((λ x, x + z) ⁻¹' s) := begin rw add_comm at hs, simpa only [add_comm] using hs.preimage_add_right, end end module end add_comm_monoid section add_comm_group variables [add_comm_group E] [module 𝕜 E] {x y : E} lemma star_convex.sub' {s : set (E × E)} (hs : star_convex 𝕜 (x, y) s) : star_convex 𝕜 (x - y) ((λ x : E × E, x.1 - x.2) '' s) := hs.is_linear_image is_linear_map.is_linear_map_sub end add_comm_group end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {x : E} {s : set E} lemma star_convex.smul (hs : star_convex 𝕜 x s) (c : 𝕜) : star_convex 𝕜 (c • x) (c • s) := hs.linear_image $ linear_map.lsmul _ _ c lemma star_convex.preimage_smul {c : 𝕜} (hs : star_convex 𝕜 (c • x) s) : star_convex 𝕜 x ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma star_convex.affinity (hs : star_convex 𝕜 x s) (z : E) (c : 𝕜) : star_convex 𝕜 (z + c • x) ((λ x, z + c • x) '' s) := begin have h := (hs.smul c).add_left z, rwa [←image_smul, image_image] at h, end end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [smul_with_zero 𝕜 E]{s : set E} lemma star_convex_zero_iff : star_convex 𝕜 0 s ↔ ∀ ⦃x : E⦄, x ∈ s → ∀ ⦃a : 𝕜⦄, 0 ≤ a → a ≤ 1 → a • x ∈ s := begin refine forall_congr (λ x, forall_congr $ λ hx, ⟨λ h a ha₀ ha₁, _, λ h a b ha hb hab, _⟩), { simpa only [sub_add_cancel, eq_self_iff_true, forall_true_left, zero_add, smul_zero] using h (sub_nonneg_of_le ha₁) ha₀ }, { rw [smul_zero, zero_add], exact h hb (by { rw ←hab, exact le_add_of_nonneg_left ha }) } end end add_comm_monoid section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {x y : E} {s t : set E} lemma star_convex.add_smul_mem (hs : star_convex 𝕜 x s) (hy : x + y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : x + t • y ∈ s := begin have h : x + t • y = (1 - t) • x + t • (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hy (sub_nonneg_of_le ht₁) ht₀ (sub_add_cancel _ _), end lemma star_convex.smul_mem (hs : star_convex 𝕜 0 s) (hx : x ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : t • x ∈ s := by simpa using hs.add_smul_mem (by simpa using hx) ht₀ ht₁ lemma star_convex.add_smul_sub_mem (hs : star_convex 𝕜 x s) (hy : y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : x + t • (y - x) ∈ s := begin apply hs.segment_subset hy, rw segment_eq_image', exact mem_image_of_mem _ ⟨ht₀, ht₁⟩, end /-- The preimage of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (f ⁻¹' s) := begin intros y hy a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs hy ha hb hab, end /-- The image of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_image (f : E →ᵃ[𝕜] F) {s : set E} (hs : star_convex 𝕜 x s) : star_convex 𝕜 (f x) (f '' s) := begin rintro y ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x + b • y', ⟨hs hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hy'f], end lemma star_convex.neg (hs : star_convex 𝕜 x s) : star_convex 𝕜 (-x) (-s) := by { rw ←image_neg, exact hs.is_linear_image is_linear_map.is_linear_map_neg } lemma star_convex.sub (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x - y) (s - t) := by { simp_rw sub_eq_add_neg, exact hs.add ht.neg } end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [module 𝕜 E] {x : E} {s : set E} /-- Alternative definition of star-convexity, using division. -/ lemma star_convex_iff_div : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s := ⟨λ h y hy a b ha hb hab, begin apply h hy, { have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at ha' }, { have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at hb' }, { rw ←add_div, exact div_self hab.ne' } end, λ h y hy a b ha hb hab, begin have h', from h hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma star_convex.mem_smul (hs : star_convex 𝕜 0 s) (hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) : x ∈ t • s := begin rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne', exact hs.smul_mem hx (inv_nonneg.2 $ zero_le_one.trans ht) (inv_le_one ht), end end add_comm_group end linear_ordered_field /-! #### Star-convex sets in an ordered space Relates `star_convex` and `set.ord_connected`. -/ section ord_connected lemma set.ord_connected.star_convex [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {x : E} {s : set E} (hs : s.ord_connected) (hx : x ∈ s) (h : ∀ y ∈ s, x ≤ y ∨ y ≤ x) : star_convex 𝕜 x s := begin intros y hy a b ha hb hab, obtain hxy | hyx := h _ hy, { refine hs.out hx hy (mem_Icc.2 ⟨_, _⟩), calc x = a • x + b • x : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _ ... = y : convex.combo_self hab _ }, { refine hs.out hy hx (mem_Icc.2 ⟨_, _⟩), calc y = a • y + b • y : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _, calc a • x + b • y ≤ a • x + b • x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _ ... = x : convex.combo_self hab _ } end lemma star_convex_iff_ord_connected [linear_ordered_field 𝕜] {x : 𝕜} {s : set 𝕜} (hx : x ∈ s) : star_convex 𝕜 x s ↔ s.ord_connected := by simp_rw [ord_connected_iff_uIcc_subset_left hx, star_convex_iff_segment_subset, segment_eq_uIcc] alias star_convex_iff_ord_connected ↔ star_convex.ord_connected _ end ord_connected
b63b8821a3e9e39d47956c5367ccf68cd8b92207
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/group_with_zero/power.lean
04129dafa0dc61226d2489fef6ae9d1ffcddb870
[ "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
6,476
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 algebra.group_power.lemmas import data.int.bitwise /-! # Powers of elements of groups with an adjoined zero element > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define integer power functions for groups with an adjoined zero element. This generalises the integer power function on a division ring. -/ section group_with_zero variables {G₀ : Type*} [group_with_zero G₀] {a : G₀} {m n : ℕ} section nat_pow theorem pow_sub₀ (a : G₀) {m n : ℕ} (ha : a ≠ 0) (h : n ≤ m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := have h1 : m - n + n = m, from tsub_add_cancel_of_le h, have h2 : a ^ (m - n) * a ^ n = a ^ m, by rw [←pow_add, h1], by simpa only [div_eq_mul_inv] using eq_div_of_mul_eq (pow_ne_zero _ ha) h2 lemma pow_sub_of_lt (a : G₀) {m n : ℕ} (h : n < m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := begin obtain rfl | ha := eq_or_ne a 0, { rw [zero_pow (tsub_pos_of_lt h), zero_pow (n.zero_le.trans_lt h), zero_mul] }, { exact pow_sub₀ _ ha h.le } end theorem pow_inv_comm₀ (a : G₀) (m n : ℕ) : (a⁻¹) ^ m * a ^ n = a ^ n * (a⁻¹) ^ m := (commute.refl a).inv_left₀.pow_pow m n lemma inv_pow_sub₀ (ha : a ≠ 0) (h : n ≤ m) : a⁻¹ ^ (m - n) = (a ^ m)⁻¹ * a ^ n := by rw [pow_sub₀ _ (inv_ne_zero ha) h, inv_pow, inv_pow, inv_inv] lemma inv_pow_sub_of_lt (a : G₀) (h : n < m) : a⁻¹ ^ (m - n) = (a ^ m)⁻¹ * a ^ n := by rw [pow_sub_of_lt a⁻¹ h, inv_pow, inv_pow, inv_inv] end nat_pow end group_with_zero section zpow open int variables {G₀ : Type*} [group_with_zero G₀] local attribute [ematch] le_of_lt lemma zero_zpow : ∀ z : ℤ, z ≠ 0 → (0 : G₀) ^ z = 0 | (n : ℕ) h := by { rw [zpow_coe_nat, zero_pow'], simpa using h } | -[1+n] h := by simp lemma zero_zpow_eq (n : ℤ) : (0 : G₀) ^ n = if n = 0 then 1 else 0 := begin split_ifs with h, { rw [h, zpow_zero] }, { rw [zero_zpow _ h] } end lemma zpow_add_one₀ {a : G₀} (ha : a ≠ 0) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a | (n : ℕ) := by simp only [← int.coe_nat_succ, zpow_coe_nat, pow_succ'] | -[1+0] := by erw [zpow_zero, zpow_neg_succ_of_nat, pow_one, inv_mul_cancel ha] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, zpow_neg, neg_add, neg_add_cancel_right, zpow_neg, ← int.coe_nat_succ, zpow_coe_nat, zpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev, mul_assoc, inv_mul_cancel ha, mul_one] lemma zpow_sub_one₀ {a : G₀} (ha : a ≠ 0) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : by rw [mul_assoc, mul_inv_cancel ha, mul_one] ... = a^n * a⁻¹ : by rw [← zpow_add_one₀ ha, sub_add_cancel] lemma zpow_add₀ {a : G₀} (ha : a ≠ 0) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, zpow_add_one₀ ha, ihn, mul_assoc] }, { rw [zpow_sub_one₀ ha, ← mul_assoc, ← ihn, ← zpow_sub_one₀ ha, add_sub_assoc] } end lemma zpow_add' {a : G₀} {m n : ℤ} (h : a ≠ 0 ∨ m + n ≠ 0 ∨ m = 0 ∧ n = 0) : a ^ (m + n) = a ^ m * a ^ n := begin by_cases hm : m = 0, { simp [hm] }, by_cases hn : n = 0, { simp [hn] }, by_cases ha : a = 0, { subst a, simp only [false_or, eq_self_iff_true, not_true, ne.def, hm, hn, false_and, or_false] at h, rw [zero_zpow _ h, zero_zpow _ hm, zero_mul] }, { exact zpow_add₀ ha m n } end theorem zpow_one_add₀ {a : G₀} (h : a ≠ 0) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [zpow_add₀ h, zpow_one] theorem semiconj_by.zpow_right₀ {a x y : G₀} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := by simp [h.pow_right n] | -[1+n] := by simp [(h.pow_right (n + 1)).inv_right₀] theorem commute.zpow_right₀ {a b : G₀} (h : commute a b) : ∀ m : ℤ, commute a (b^m) := h.zpow_right₀ theorem commute.zpow_left₀ {a b : G₀} (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.zpow_right₀ m).symm theorem commute.zpow_zpow₀ {a b : G₀} (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.zpow_left₀ m).zpow_right₀ n theorem commute.zpow_self₀ (a : G₀) (n : ℤ) : commute (a^n) a := (commute.refl a).zpow_left₀ n theorem commute.self_zpow₀ (a : G₀) (n : ℤ) : commute a (a^n) := (commute.refl a).zpow_right₀ n theorem commute.zpow_zpow_self₀ (a : G₀) (m n : ℤ) : commute (a^m) (a^n) := (commute.refl a).zpow_zpow₀ m n theorem zpow_bit1₀ (a : G₀) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := begin rw [← zpow_bit0, bit1, zpow_add', zpow_one], right, left, apply bit1_ne_zero end lemma zpow_ne_zero_of_ne_zero {a : G₀} (ha : a ≠ 0) : ∀ (z : ℤ), a ^ z ≠ 0 | (n : ℕ) := by { rw zpow_coe_nat, exact pow_ne_zero _ ha } | -[1+n] := by { rw zpow_neg_succ_of_nat, exact inv_ne_zero (pow_ne_zero _ ha) } lemma zpow_sub₀ {a : G₀} (ha : a ≠ 0) (z1 z2 : ℤ) : a ^ (z1 - z2) = a ^ z1 / a ^ z2 := by rw [sub_eq_add_neg, zpow_add₀ ha, zpow_neg, div_eq_mul_inv] theorem zpow_bit1' (a : G₀) (n : ℤ) : a ^ bit1 n = (a * a) ^ n * a := by rw [zpow_bit1₀, (commute.refl a).mul_zpow] lemma zpow_eq_zero {x : G₀} {n : ℤ} (h : x ^ n = 0) : x = 0 := classical.by_contradiction $ λ hx, zpow_ne_zero_of_ne_zero hx n h lemma zpow_eq_zero_iff {a : G₀} {n : ℤ} (hn : n ≠ 0) : a ^ n = 0 ↔ a = 0 := ⟨zpow_eq_zero, λ ha, ha.symm ▸ zero_zpow _ hn⟩ lemma zpow_ne_zero {x : G₀} (n : ℤ) : x ≠ 0 → x ^ n ≠ 0 := mt zpow_eq_zero theorem zpow_neg_mul_zpow_self (n : ℤ) {x : G₀} (h : x ≠ 0) : x ^ (-n) * x ^ n = 1 := begin rw [zpow_neg], exact inv_mul_cancel (zpow_ne_zero n h) end end zpow section variables {G₀ : Type*} [comm_group_with_zero G₀] lemma div_sq_cancel (a b : G₀) : a ^ 2 * b / a = a * b := begin by_cases ha : a = 0, { simp [ha] }, rw [sq, mul_assoc, mul_div_cancel_left _ ha] end end /-- If a monoid homomorphism `f` between two `group_with_zero`s maps `0` to `0`, then it maps `x^n`, `n : ℤ`, to `(f x)^n`. -/ @[simp] lemma map_zpow₀ {F G₀ G₀' : Type*} [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero_hom_class F G₀ G₀'] (f : F) (x : G₀) (n : ℤ) : f (x ^ n) = f x ^ n := map_zpow' f (map_inv₀ f) x n