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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
6933840dce4938086c73335eca4519ad4aaab4b9 | 761fea1362b10b4c588c2dfc0ae90c70b119e35d | /src/premise_selection.lean | 0d9227c89bee91813b0a6d152c92249acf3ccc4f | [] | no_license | holtzermann17/mm-lean | 382a29fca5245f97cf488c525ed0c9594917f73b | a9130d71ed448f62df28d4128043b707bad85ccd | refs/heads/master | 1,588,477,413,982 | 1,553,885,046,000 | 1,553,885,046,000 | 178,404,617 | 0 | 0 | null | 1,553,863,829,000 | 1,553,863,828,000 | null | UTF-8 | Lean | false | false | 415 | lean | import imports k_nn main
open tactic
meta def find_relevant_facts (mm_fml : string) (b := ff) : tactic string :=
do e ← preprocess mm_fml,
(contents_map, features_map, names) ← get_all_decls,
let relevant_facts := find_k_most_relevant_facts_to_expr e contents_map features_map names.snd 10,
relevant_facts.mmap (λ f, do tp ← mk_const f.1 >>= infer_type, return (f.1, tp)) >>= print_name_type_list
|
365baedc7c468585f7b93105630b9fca9e84c0c2 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/category_theory/lifting_properties/basic.lean | 9171c760b84d69954ead34e573886c244f8c017d | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 3,446 | lean | /-
Copyright (c) 2021 Jakob Scholbach. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob Scholbach, Joël Riou
-/
import category_theory.comm_sq
/-!
# Lifting properties
This file defines the lifting property of two morphisms in a category and
shows basic properties of this notion.
## Main results
- `has_lifting_property`: the definition of the lifting property
## Tags
lifting property
@TODO :
1) define llp/rlp with respect to a `morphism_property`
2) retracts, direct/inverse images, (co)products, adjunctions
-/
universe v
namespace category_theory
open category
variables {C : Type*} [category C] {A B B' X Y Y' : C}
(i : A ⟶ B) (i' : B ⟶ B') (p : X ⟶ Y) (p' : Y ⟶ Y')
/-- `has_lifting_property i p` means that `i` has the left lifting
property with respect to `p`, or equivalently that `p` has
the right lifting property with respect to `i`. -/
class has_lifting_property : Prop :=
(sq_has_lift : ∀ {f : A ⟶ X} {g : B ⟶ Y} (sq : comm_sq f i p g), sq.has_lift)
@[priority 100]
instance sq_has_lift_of_has_lifting_property {f : A ⟶ X} {g : B ⟶ Y} (sq : comm_sq f i p g)
[hip : has_lifting_property i p] : sq.has_lift := by apply hip.sq_has_lift
namespace has_lifting_property
variables {i p}
lemma op (h : has_lifting_property i p) : has_lifting_property p.op i.op :=
⟨λ f g sq, begin
simp only [comm_sq.has_lift.iff_unop, quiver.hom.unop_op],
apply_instance,
end⟩
lemma unop {A B X Y : Cᵒᵖ} {i : A ⟶ B} {p : X ⟶ Y}
(h : has_lifting_property i p) : has_lifting_property p.unop i.unop :=
⟨λ f g sq, begin
rw comm_sq.has_lift.iff_op,
simp only [quiver.hom.op_unop],
apply_instance,
end⟩
lemma iff_op : has_lifting_property i p ↔ has_lifting_property p.op i.op := ⟨op, unop⟩
lemma iff_unop {A B X Y : Cᵒᵖ} (i : A ⟶ B) (p : X ⟶ Y) :
has_lifting_property i p ↔ has_lifting_property p.unop i.unop := ⟨unop, op⟩
variables (i p)
@[priority 100]
instance of_left_iso [is_iso i] : has_lifting_property i p :=
⟨λ f g sq, comm_sq.has_lift.mk'
{ l := inv i ≫ f,
fac_left' := by simp only [is_iso.hom_inv_id_assoc],
fac_right' := by simp only [sq.w, assoc, is_iso.inv_hom_id_assoc], }⟩
@[priority 100]
instance of_right_iso [is_iso p] : has_lifting_property i p :=
⟨λ f g sq, comm_sq.has_lift.mk'
{ l := g ≫ inv p,
fac_left' := by simp only [← sq.w_assoc, is_iso.hom_inv_id, comp_id],
fac_right' := by simp only [assoc, is_iso.inv_hom_id, comp_id], }⟩
instance of_comp_left [has_lifting_property i p] [has_lifting_property i' p] :
has_lifting_property (i ≫ i') p :=
⟨λ f g sq, begin
have fac := sq.w,
rw assoc at fac,
exact comm_sq.has_lift.mk'
{ l := (comm_sq.mk (comm_sq.mk fac).fac_right).lift,
fac_left' := by simp only [assoc, comm_sq.fac_left],
fac_right' := by simp only [comm_sq.fac_right], },
end⟩
instance of_comp_right [has_lifting_property i p] [has_lifting_property i p'] :
has_lifting_property i (p ≫ p') :=
⟨λ f g sq, begin
have fac := sq.w,
rw ← assoc at fac,
let sq₂ := (comm_sq.mk ((comm_sq.mk fac).fac_left.symm)).lift,
exact comm_sq.has_lift.mk'
{ l := (comm_sq.mk ((comm_sq.mk fac).fac_left.symm)).lift,
fac_left' := by simp only [comm_sq.fac_left],
fac_right' := by simp only [comm_sq.fac_right_assoc, comm_sq.fac_right], },
end⟩
end has_lifting_property
end category_theory
|
5ae1aac44f5e83943f547d46f6aa6511fbbbad9f | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Lean/Data/Lsp/Diagnostics.lean | 21ab1ae1e9d5333ab96fec81242f3f20fe396ddb | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,757 | lean | /-
Copyright (c) 2020 Marc Huisinga. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Marc Huisinga, Wojciech Nawrocki
-/
import Lean.Data.Json
import Lean.Data.Lsp.Basic
import Lean.Data.Lsp.Utf16
import Lean.Message
/-! Definitions and functionality for emitting diagnostic information
such as errors, warnings and #command outputs from the LSP server. -/
namespace Lean
namespace Lsp
open Json
inductive DiagnosticSeverity
| error | warning | information | hint
instance : FromJson DiagnosticSeverity := ⟨fun j =>
match j.getNat? with
| some 1 => DiagnosticSeverity.error
| some 2 => DiagnosticSeverity.warning
| some 3 => DiagnosticSeverity.information
| some 4 => DiagnosticSeverity.hint
| _ => none⟩
instance : ToJson DiagnosticSeverity := ⟨fun o =>
match o with
| DiagnosticSeverity.error => 1
| DiagnosticSeverity.warning => 2
| DiagnosticSeverity.information => 3
| DiagnosticSeverity.hint => 4⟩
inductive DiagnosticCode
| int (i : Int)
| string (s : String)
instance : FromJson DiagnosticCode := ⟨fun j =>
match j with
| num (i : Int) => DiagnosticCode.int i
| str s => DiagnosticCode.string s
| _ => none⟩
instance : ToJson DiagnosticCode := ⟨fun o =>
match o with
| DiagnosticCode.int i => i
| DiagnosticCode.string s => s⟩
inductive DiagnosticTag
| unnecessary
| deprecated
instance : FromJson DiagnosticTag := ⟨fun j =>
match j.getNat? with
| some 1 => DiagnosticTag.unnecessary
| some 2 => DiagnosticTag.deprecated
| _ => none⟩
instance : ToJson DiagnosticTag := ⟨fun o =>
match o with
| DiagnosticTag.unnecessary => (1 : Nat)
| DiagnosticTag.deprecated => (2 : Nat)⟩
structure DiagnosticRelatedInformation :=
(location : Location)
(message : String)
instance : FromJson DiagnosticRelatedInformation := ⟨fun j => do
let location ← j.getObjValAs? Location "location"
let message ← j.getObjValAs? String "message"
pure ⟨location, message⟩⟩
instance : ToJson DiagnosticRelatedInformation := ⟨fun o =>
mkObj [
⟨"location", toJson o.location⟩,
⟨"message", o.message⟩]⟩
structure Diagnostic :=
(range : Range)
(severity? : Option DiagnosticSeverity := none)
(code? : Option DiagnosticCode := none)
(source? : Option String := none)
(message : String)
(tags? : Option (Array DiagnosticTag) := none)
(relatedInformation? : Option (Array DiagnosticRelatedInformation) := none)
instance : FromJson Diagnostic := ⟨fun j => do
let range ← j.getObjValAs? Range "range"
let severity? := j.getObjValAs? DiagnosticSeverity "severity"
let code? := j.getObjValAs? DiagnosticCode "code"
let source? := j.getObjValAs? String "source"
let message ← j.getObjValAs? String "message"
let tags? := j.getObjValAs? (Array DiagnosticTag) "tags"
let relatedInformation? := j.getObjValAs? (Array DiagnosticRelatedInformation) "relatedInformation"
pure ⟨range, severity?, code?, source?, message, tags?, relatedInformation?⟩⟩
instance : ToJson Diagnostic := ⟨fun o => mkObj $
opt "severity" o.severity? ++
opt "code" o.code? ++
opt "source" o.source? ++
opt "tags" o.tags? ++
opt "relatedInformation" o.relatedInformation? ++ [
⟨"range", toJson o.range⟩,
⟨"message", o.message⟩]⟩
structure PublishDiagnosticsParams :=
(uri : DocumentUri)
(version? : Option Int := none)
(diagnostics: Array Diagnostic)
instance : FromJson PublishDiagnosticsParams := ⟨fun j => do
let uri ← j.getObjValAs? DocumentUri "uri"
let version? := j.getObjValAs? Int "version"
let diagnostics ← j.getObjValAs? (Array Diagnostic) "diagnostics"
pure ⟨uri, version?, diagnostics⟩⟩
instance : ToJson PublishDiagnosticsParams := ⟨fun o => mkObj $
opt "version" o.version? ++ [
⟨"uri", toJson o.uri⟩,
⟨"diagnostics", toJson o.diagnostics⟩]⟩
/-- Transform a Lean Message concerning the given text into an LSP Diagnostic. -/
def msgToDiagnostic (text : FileMap) (m : Message) : IO Diagnostic := do
let low : Lsp.Position := text.leanPosToLspPos m.pos
let high : Lsp.Position := match m.endPos with
| some endPos => text.leanPosToLspPos endPos
| none => low
let range : Range := ⟨low, high⟩
let severity := match m.severity with
| MessageSeverity.information => DiagnosticSeverity.information
| MessageSeverity.warning => DiagnosticSeverity.warning
| MessageSeverity.error => DiagnosticSeverity.error
let source := "Lean 4 server"
let message ← m.data.toString
pure { range := range,
severity? := severity,
source? := source,
message := message
}
end Lsp
end Lean
|
373bbc6eb2f607e8425487c9bc11978a2af9ce4b | b7b549d2cf38ac9d4e49372b7ad4d37f70449409 | /src/LeanLLVM/LLVMCodes.lean | cd1feb5bd8569d250206c08db84282b1659abebe | [
"Apache-2.0"
] | permissive | GaloisInc/lean-llvm | 7cc196172fe02ff3554edba6cc82f333c30fdc2b | 36e2ec604ae22d8ec1b1b66eca0f8887880db6c6 | refs/heads/master | 1,637,359,020,356 | 1,629,332,114,000 | 1,629,402,464,000 | 146,700,234 | 29 | 1 | Apache-2.0 | 1,631,225,695,000 | 1,535,607,191,000 | Lean | UTF-8 | Lean | false | false | 4,991 | lean | namespace LLVM
namespace Code
-- C.F. <llvm/IR/Type.h>, Type::TypeID
inductive TypeID : Type
| void
| half
| float
| double
| x86FP80
| fp128
| ppcFP128
| label
| metadata
| x86mmx
| token
| integer
| function
| struct
| array
| pointer
| vector
namespace TypeID
def asString : TypeID -> String
| void => "void"
| half => "half"
| float => "float"
| double => "double"
| x86FP80 => "x86_fp80"
| fp128 => "fp128"
| ppcFP128 => "ppc_fp128"
| label => "label"
| metadata => "metadata"
| x86mmx => "x86_mmx"
| token => "token"
| integer => "integer"
| function => "function"
| struct => "struct"
| array => "array"
| pointer => "pointer"
| vector => "vector"
end TypeID
-- | Instruction type tag
--
-- Note. Constructor names are generated from LLVM files and capitalized
-- according to names in files.
inductive Instr : Type
| Unused -- Burn constructor number 0, which is not used for any instruction value
| Ret
| Br
| Switch
| IndirectBr
| Invoke
| Resume
| Unreachable
| CleanupRet
| CatchRet
| CatchSwitch
| FNeg
| Add
| FAdd
| Sub
| FSub
| Mul
| FMul
| UDiv
| SDiv
| FDiv
| URem
| SRem
| FRem
| Shl
| LShr
| AShr
| And
| Or
| Xor
| Alloca
| Load
| Store
| GetElementPtr
| Fence
| AtomicCmpXchg
| AtomicRMW
| Trunc
| ZExt
| SExt
| FPToUI
| FPToSI
| UIToFP
| SIToFP
| FPTrunc
| FPExt
| PtrToInt
| IntToPtr
| BitCast
| AddrSpaceCast
| CleanupPad
| CatchPad
| ICmp
| FCmp
| PHI
| Call
| Select
| UserOp1
| UserOp2
| VAArg
| ExtractElement
| InsertElement
| ShuffleVector
| ExtractValue
| InsertValue
| LandingPad
namespace Instr
def asString : Instr -> String
| Unused => "Unused"
| Ret => "Ret"
| Br => "Br"
| Switch => "Switch"
| IndirectBr => "IndirectBr"
| Invoke => "Invoke"
| Resume => "Resume"
| Unreachable => "Unreachable"
| CleanupRet => "CleanupRet"
| CatchRet => "CatchRet"
| CatchSwitch => "CatchSwitch"
| FNeg => "FNeg"
| Add => "Add"
| FAdd => "FAdd"
| Sub => "Sub"
| FSub => "FSub"
| Mul => "Mul"
| FMul => "FMul"
| UDiv => "UDiv"
| SDiv => "SDiv"
| FDiv => "FDiv"
| URem => "URem"
| SRem => "SRem"
| FRem => "FRem"
| Shl => "Shl"
| LShr => "LShr"
| AShr => "AShr"
| And => "And"
| Or => "Or"
| Xor => "Xor"
| Alloca => "Alloca"
| Load => "Load"
| Store => "Store"
| GetElementPtr => "GetElementPtr"
| Fence => "Fence"
| AtomicCmpXchg => "AtomicCmpXchg"
| AtomicRMW => "AtomicRMW"
| Trunc => "Trunc"
| ZExt => "ZExt"
| SExt => "SExt"
| FPToUI => "FPToUI"
| FPToSI => "FPToSI"
| UIToFP => "UIToFP"
| SIToFP => "SIToFP"
| FPTrunc => "FPTrunc"
| FPExt => "FPExt"
| PtrToInt => "PtrToInt"
| IntToPtr => "IntToPtr"
| BitCast => "BitCast"
| AddrSpaceCast => "AddrSpaceCast"
| CleanupPad => "CleanupPad"
| CatchPad => "CatchPad"
| ICmp => "ICmp"
| FCmp => "FCmp"
| PHI => "PHI"
| Call => "Call"
| Select => "Select"
| UserOp1 => "UserOp1"
| UserOp2 => "UserOp2"
| VAArg => "VAArg"
| ExtractElement => "ExtractElement"
| InsertElement => "InsertElement"
| ShuffleVector => "ShuffleVector"
| ExtractValue => "ExtractValue"
| InsertValue => "InsertValue"
| LandingPad => "LandingPad"
end Instr
-- | Const type tag
--
-- Note. Constructor names are generated from LLVM files and capitalized
-- according to names in files.
inductive Const : Type
| Function
| GlobalAlias
| GlobalIFunc
| GlobalVariable
| BlockAddress
| ConstantExpr
| ConstantArray
| ConstantStruct
| ConstantVector
| UndefValue
| ConstantAggregateZero
| ConstantDataArray
| ConstantDataVector
| ConstantInt
| ConstantFP
| ConstantPointerNull
| ConstantTokenNone
namespace Const
def asString : Const -> String
| Function => "Function"
| GlobalAlias => "GlobalAlias"
| GlobalIFunc => "GlobalIFunc"
| GlobalVariable => "GlobalVariable"
| BlockAddress => "BlockAddress"
| ConstantExpr => "ConstantExpr"
| ConstantArray => "ConstantArray"
| ConstantStruct => "ConstantStruct"
| ConstantVector => "ConstantVector"
| UndefValue => "UndefValue"
| ConstantAggregateZero => "ConstantAggregateZero"
| ConstantDataArray => "ConstantDataArray"
| ConstantDataVector => "ConstantDataVector"
| ConstantInt => "ConstantInt"
| ConstantFP => "ConstantFP"
| ConstantPointerNull => "ConstantPointerNull"
| ConstantTokenNone => "ConstantTokenNone"
end Const
-- C.F. llvm/IR/InstrTypes.h, enum Predicate
inductive FCmp
| false
| oeq
| ogt
| oge
| olt
| ole
| one
| ord
| uno
| ueq
| ugt
| uge
| ult
| ule
| une
| true
namespace FCmp
def asString : FCmp -> String
| false => "false"
| oeq => "oeq"
| ogt => "ogt"
| oge => "oge"
| olt => "olt"
| ole => "ole"
| one => "one"
| ord => "ord"
| uno => "uno"
| ueq => "ueq"
| ugt => "ugt"
| uge => "uge"
| ult => "ult"
| ule => "ule"
| une => "une"
| true => "true"
end FCmp
-- C.F. llvm/IR/InstrTypes.h, enum Predicate
inductive ICmp
| eq
| ne
| ugt
| uge
| ult
| ule
| sgt
| sge
| slt
| sle
namespace ICmp
def asString : ICmp -> String
| eq => "eq"
| ne => "ne"
| ugt => "ugt"
| uge => "uge"
| ult => "ult"
| ule => "ule"
| sgt => "sgt"
| sge => "sge"
| slt => "slt"
| sle => "sle"
end ICmp
end Code
end LLVM
|
39e474230d34db046585b74f7792eecd61320e69 | 0707842a58b971bc2c537fdcab2f5cef1c12d77a | /lean4/01_hello_world.lean | 03075b33e5e72b32669cdcd9684d1d449339bc76 | [] | no_license | adolfont/LearningProgramming | 7a41e5dfde8df72fc0b4a23592999ecdb22a26e0 | 866e6654d347287bd0c63aa31d18705174f00324 | refs/heads/master | 1,652,902,832,503 | 1,651,315,660,000 | 1,651,315,660,000 | 1,328,601 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 99 | lean | def main : IO Unit :=
IO.println "Hello, world!"
#eval main
#eval 2+2
#eval println! "ADOLFO" |
954be86debf10a2e4f742342958ff93358979ccb | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/metric_space/gromov_hausdorff_auto.lean | 540bc83c545ed76a8aab9a717ee24ab42f7abd0f | [] | 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 | 8,216 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sébastien Gouëzel
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.metric_space.closeds
import Mathlib.set_theory.cardinal
import Mathlib.topology.metric_space.gromov_hausdorff_realized
import Mathlib.topology.metric_space.completion
import Mathlib.PostPort
universes u v w l
namespace Mathlib
/-!
# Gromov-Hausdorff distance
This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces
up to isometry.
We introduce the space of all nonempty compact metric spaces, up to isometry,
called `GH_space`, and endow it with a metric space structure. The distance,
known as the Gromov-Hausdorff distance, is defined as follows: given two
nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance
between all possible isometric embeddings of `X` and `Y` in all metric spaces.
To define properly the Gromov-Hausdorff space, we consider the non-empty
compact subsets of `ℓ^∞(ℝ)` up to isometry, which is a well-defined type,
and define the distance as the infimum of the Hausdorff distance over all
embeddings in `ℓ^∞(ℝ)`. We prove that this coincides with the previous description,
as all separable metric spaces embed isometrically into `ℓ^∞(ℝ)`, through an
embedding called the Kuratowski embedding.
To prove that we have a distance, we should show that if spaces can be coupled
to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff
distance is realized, i.e., there is a coupling for which the Hausdorff distance
is exactly the Gromov-Hausdorff distance. This follows from a compactness
argument, essentially following from Arzela-Ascoli.
## Main results
We prove the most important properties of the Gromov-Hausdorff space: it is a polish space,
i.e., it is complete and second countable. We also prove the Gromov compactness criterion.
-/
namespace Gromov_Hausdorff
/- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient
of nonempty compact subsets of `ℓ^∞(ℝ)` by identifying isometric sets.
Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty
compact type to `GH_space`. -/
/-- Equivalence relation identifying two nonempty compact sets which are isometric -/
/-- This is indeed an equivalence relation -/
/-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/
protected instance isometry_rel.setoid : setoid (topological_space.nonempty_compacts ℓ_infty_ℝ) :=
setoid.mk isometry_rel is_equivalence_isometry_rel
/-- The Gromov-Hausdorff space -/
def GH_space := quotient isometry_rel.setoid
/-- Map any nonempty compact type to `GH_space` -/
def to_GH_space (α : Type u) [metric_space α] [compact_space α] [Nonempty α] : GH_space :=
quotient.mk (nonempty_compacts.Kuratowski_embedding α)
protected instance GH_space.inhabited : Inhabited GH_space :=
{ default := Quot.mk setoid.r { val := singleton 0, property := sorry } }
/-- A metric space representative of any abstract point in `GH_space` -/
def GH_space.rep (p : GH_space) := ↥(subtype.val (quot.out p))
theorem eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [Nonempty α]
{p : topological_space.nonempty_compacts ℓ_infty_ℝ} :
quotient.mk p = to_GH_space α ↔
∃ (Ψ : α → ℓ_infty_ℝ), isometry Ψ ∧ set.range Ψ = subtype.val p :=
sorry
theorem eq_to_GH_space {p : topological_space.nonempty_compacts ℓ_infty_ℝ} :
quotient.mk p = to_GH_space ↥(subtype.val p) :=
iff.mpr eq_to_GH_space_iff
(Exists.intro (fun (x : ↥(subtype.val p)) => ↑x)
{ left := isometry_subtype_coe, right := subtype.range_coe })
protected instance rep_GH_space_metric_space {p : GH_space} : metric_space (GH_space.rep p) :=
subtype.metric_space
protected instance rep_GH_space_compact_space {p : GH_space} : compact_space (GH_space.rep p) :=
topological_space.nonempty_compacts.to_compact_space
protected instance rep_GH_space_nonempty {p : GH_space} : Nonempty (GH_space.rep p) :=
topological_space.nonempty_compacts.to_nonempty
theorem GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (GH_space.rep p) = p :=
id
(eq.mpr
(id
(Eq._oldrec (Eq.refl (to_GH_space ↥(subtype.val (quot.out p)) = p))
(Eq.symm eq_to_GH_space)))
(quot.out_eq p))
/-- Two nonempty compact spaces have the same image in `GH_space` if and only if they are
isometric. -/
theorem to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α]
[Nonempty α] {β : Type u} [metric_space β] [compact_space β] [Nonempty β] :
to_GH_space α = to_GH_space β ↔ Nonempty (α ≃ᵢ β) :=
sorry
/-- Distance on `GH_space`: the distance between two nonempty compact spaces is the infimum
Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition,
we only consider embeddings in `ℓ^∞(ℝ)`, but we will prove below that it works for all spaces. -/
protected instance GH_space.has_dist : has_dist GH_space :=
has_dist.mk
fun (x y : GH_space) =>
Inf
((fun
(p :
topological_space.nonempty_compacts ℓ_infty_ℝ ×
topological_space.nonempty_compacts ℓ_infty_ℝ) =>
metric.Hausdorff_dist (subtype.val (prod.fst p)) (subtype.val (prod.snd p))) ''
set.prod
(set_of fun (a : topological_space.nonempty_compacts ℓ_infty_ℝ) => quotient.mk a = x)
(set_of fun (b : topological_space.nonempty_compacts ℓ_infty_ℝ) => quotient.mk b = y))
/-- The Gromov-Hausdorff distance between two nonempty compact metric spaces, equal by definition to
the distance of the equivalence classes of these spaces in the Gromov-Hausdorff space. -/
def GH_dist (α : Type u) (β : Type v) [metric_space α] [Nonempty α] [compact_space α]
[metric_space β] [Nonempty β] [compact_space β] : ℝ :=
dist (to_GH_space α) (to_GH_space β)
theorem dist_GH_dist (p : GH_space) (q : GH_space) :
dist p q = GH_dist (GH_space.rep p) (GH_space.rep q) :=
sorry
/-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance
of isometric copies of the spaces, in any metric space. -/
theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [Nonempty α]
{β : Type v} [metric_space β] [compact_space β] [Nonempty β] {γ : Type w} [metric_space γ]
{Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) :
GH_dist α β ≤ metric.Hausdorff_dist (set.range Φ) (set.range Ψ) :=
sorry
/-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance,
essentially by design. -/
theorem Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [Nonempty α]
{β : Type v} [metric_space β] [compact_space β] [Nonempty β] :
metric.Hausdorff_dist (set.range (optimal_GH_injl α β)) (set.range (optimal_GH_injr α β)) =
GH_dist α β :=
sorry
/-- The Gromov-Hausdorff distance can also be realized by a coupling in `ℓ^∞(ℝ)`, by embedding
the optimal coupling through its Kuratowski embedding. -/
theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [Nonempty α]
(β : Type v) [metric_space β] [compact_space β] [Nonempty β] :
∃ (Φ : α → ℓ_infty_ℝ),
∃ (Ψ : β → ℓ_infty_ℝ),
isometry Φ ∧
isometry Ψ ∧ GH_dist α β = metric.Hausdorff_dist (set.range Φ) (set.range Ψ) :=
sorry
-- without the next two lines, `{ exact hΦ.is_closed }` in the next
-- proof is very slow, as the `t2_space` instance is very hard to find
/-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/
protected instance GH_space_metric_space : metric_space GH_space := sorry
end Gromov_Hausdorff
/-- In particular, nonempty compacts of a metric space map to `GH_space`. We register this
in the |
a4e7d8d058c13b614e78b0e2bddaedac61b8d814 | ebf7140a9ea507409ff4c994124fa36e79b4ae35 | /src/exercises_sources/thursday/category_theory/exercise6.lean | 0d93a0819e9e242b73e1d915b916f67bd92646bf | [] | no_license | fundou/lftcm2020 | 3e88d58a92755ea5dd49f19c36239c35286ecf5e | 99d11bf3bcd71ffeaef0250caa08ecc46e69b55b | refs/heads/master | 1,685,610,799,304 | 1,624,070,416,000 | 1,624,070,416,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,338 | lean | import category_theory.limits.shapes.pullbacks
/-!
Thanks to Markus Himmel for suggesting this question.
-/
open category_theory
open category_theory.limits
/-!
Let C be a category, X and Y be objects and f : X ⟶ Y be a morphism. Show that f is an epimorphism
if and only if the diagram
X --f--→ Y
| |
f 𝟙
| |
↓ ↓
Y --𝟙--→ Y
is a pushout.
-/
variables {C : Type*} [category C]
def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] :
is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) :=
-- Hint: you can start a proof with `fapply pushout_cocone.is_colimit.mk`
-- to save a little bit of work over just building a `is_colimit` structure directly.
sorry
theorem epi_of_pushout {X Y : C} (f : X ⟶ Y)
(is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f :=
-- Hint: You can use `pushout_cocone.mk` to conveniently construct a cocone over a cospan.
-- Hint: use `is_colim.desc` to construct the map from a colimit cocone to any other cocone.
-- Hint: use `is_colim.fac` to show that this map gives a factorisation of the cocone maps through the colimit cocone.
-- Hint: if `simp` won't correctly simplify `𝟙 X ≫ f`, try `dsimp, simp`.
sorry
/-!
There are some further hints in
`hints/category_theory/exercise6/`
-/
|
6c6f0b58696a1c5144be11922672fed29c2bc9b6 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/topology/algebra/polynomial.lean | b502d00ae0bb3b755e69be5dca3c93d5cd6685f5 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 2,117 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Analytic facts about polynomials.
-/
import topology.algebra.ring
import data.polynomial.div
import data.real.cau_seq
open polynomial is_absolute_value
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma polynomial.tendsto_infinity {α β : Type*} [comm_ring α] [discrete_linear_ordered_field β]
(abv : α → β) [is_absolute_value abv] {p : polynomial α} (h : 0 < degree p) :
∀ x : β, ∃ r > 0, ∀ z : α, r < abv z → x < abv (p.eval z) :=
degree_pos_induction_on p h
(λ a ha x, ⟨max (x / abv a) 1, (lt_max_iff.2 (or.inr zero_lt_one)), λ z hz,
by simp [max_lt_iff, div_lt_iff' ((abv_pos abv).2 ha), abv_mul abv] at *; tauto⟩)
(λ p hp ih x, let ⟨r, hr0, hr⟩ := ih x in
⟨max r 1, lt_max_iff.2 (or.inr zero_lt_one), λ z hz, by rw [eval_mul, eval_X, abv_mul abv];
calc x < abv (p.eval z) : hr _ (max_lt_iff.1 hz).1
... ≤ abv (eval z p) * abv z : le_mul_of_ge_one_right
(abv_nonneg _ _) (max_le_iff.1 (le_of_lt hz)).2⟩)
(λ p a hp ih x, let ⟨r, hr0, hr⟩ := ih (x + abv a) in
⟨r, hr0, λ z hz, by rw [eval_add, eval_C, ← sub_neg_eq_add];
exact lt_of_lt_of_le (lt_sub_iff_add_lt.2
(by rw abv_neg abv; exact (hr z hz)))
(le_trans (le_abs_self _) (abs_abv_sub_le_abv_sub _ _ _))⟩)
@[continuity]
lemma polynomial.continuous_eval {α} [comm_semiring α] [topological_space α]
[topological_semiring α] (p : polynomial α) : continuous (λ x, p.eval x) :=
begin
apply p.induction,
{ convert continuous_const,
ext, show polynomial.eval x 0 = 0, from rfl },
{ intros a b f haf hb hcts,
simp only [polynomial.eval_add],
refine continuous.add _ hcts,
have : ∀ x, finsupp.sum (finsupp.single a b) (λ (e : ℕ) (a : α), a * x ^ e) = b * x ^a,
from λ x, finsupp.sum_single_index (by simp),
convert continuous.mul _ _,
{ ext, apply this },
{ apply_instance },
{ apply continuous_const },
{ apply continuous_pow }}
end
|
e8bbbe1e5fa588da2a8f57118a57e42c111f8595 | 26bff4ed296b8373c92b6b025f5d60cdf02104b9 | /hott/algebra/precategory/yoneda.hlean | 32de74b009eeb8a3a4d17321d512dbea21a63b9d | [
"Apache-2.0"
] | permissive | guiquanz/lean | b8a878ea24f237b84b0e6f6be2f300e8bf028229 | 242f8ba0486860e53e257c443e965a82ee342db3 | refs/heads/master | 1,526,680,092,098 | 1,427,492,833,000 | 1,427,493,281,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,097 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.precategory.yoneda
Authors: Floris van Doorn
-/
--note: modify definition in category.set
import algebra.category.constructions .iso
open category eq category.ops functor prod.ops is_trunc iso
set_option pp.beta true
namespace yoneda
set_option class.conservative false
definition representable_functor_assoc [C : Precategory] {a1 a2 a3 a4 a5 a6 : C}
(f1 : hom a5 a6) (f2 : hom a4 a5) (f3 : hom a3 a4) (f4 : hom a2 a3) (f5 : hom a1 a2)
: (f1 ∘ f2) ∘ f3 ∘ (f4 ∘ f5) = f1 ∘ (f2 ∘ f3 ∘ f4) ∘ f5 :=
calc
_ = f1 ∘ f2 ∘ f3 ∘ f4 ∘ f5 : by rewrite -assoc
... = f1 ∘ (f2 ∘ f3) ∘ f4 ∘ f5 : by rewrite -assoc
... = f1 ∘ ((f2 ∘ f3) ∘ f4) ∘ f5 : by rewrite -(assoc (f2 ∘ f3) _ _)
... = _ : by rewrite (assoc f2 f3 f4)
definition hom_functor (C : Precategory) : Cᵒᵖ ×c C ⇒ set :=
functor.mk (λ(x : Cᵒᵖ ×c C), homset x.1 x.2)
(λ(x y : Cᵒᵖ ×c C) (f : _) (h : homset x.1 x.2), f.2 ∘⁅ C ⁆ (h ∘⁅ C ⁆ f.1))
begin
intro x, apply eq_of_homotopy, intro h, exact (!id_left ⬝ !id_right)
end
begin
intros (x, y, z, g, f), apply eq_of_homotopy, intro h,
exact (representable_functor_assoc g.2 f.2 h f.1 g.1),
end
end yoneda
open is_equiv equiv
namespace functor
open prod nat_trans
variables {C D E : Precategory} (F : C ×c D ⇒ E) (G : C ⇒ E ^c D)
definition functor_curry_ob [reducible] (c : C) : E ^c D :=
functor.mk (λd, F (c,d))
(λd d' g, F (id, g))
(λd, !respect_id)
(λd₁ d₂ d₃ g' g, calc
F (id, g' ∘ g) = F (id ∘ id, g' ∘ g) : by rewrite id_comp
... = F ((id,g') ∘ (id, g)) : by esimp
... = F (id,g') ∘ F (id, g) : by rewrite respect_comp)
local abbreviation Fob := @functor_curry_ob
definition functor_curry_hom ⦃c c' : C⦄ (f : c ⟶ c') : Fob F c ⟹ Fob F c' :=
nat_trans.mk (λd, F (f, id))
(λd d' g, calc
F (id, g) ∘ F (f, id) = F (id ∘ f, g ∘ id) : respect_comp F
... = F (f, g ∘ id) : by rewrite id_left
... = F (f, g) : by rewrite id_right
... = F (f ∘ id, g) : by rewrite id_right
... = F (f ∘ id, id ∘ g) : by rewrite id_left
... = F (f, id) ∘ F (id, g) : (respect_comp F (f, id) (id, g))⁻¹ᵖ)
local abbreviation Fhom := @functor_curry_hom
theorem functor_curry_hom_def ⦃c c' : C⦄ (f : c ⟶ c') (d : D) :
(Fhom F f) d = to_fun_hom F (f, id) := idp
theorem functor_curry_id (c : C) : Fhom F (ID c) = nat_trans.id :=
nat_trans_eq_mk (λd, respect_id F _)
theorem functor_curry_comp ⦃c c' c'' : C⦄ (f' : c' ⟶ c'') (f : c ⟶ c')
: Fhom F (f' ∘ f) = Fhom F f' ∘n Fhom F f :=
nat_trans_eq_mk (λd, calc
natural_map (Fhom F (f' ∘ f)) d = F (f' ∘ f, id) : functor_curry_hom_def
... = F (f' ∘ f, id ∘ id) : by rewrite id_comp
... = F ((f',id) ∘ (f, id)) : by esimp
... = F (f',id) ∘ F (f, id) : respect_comp F
... = natural_map ((Fhom F f') ∘ (Fhom F f)) d : by esimp)
definition functor_curry [reducible] : C ⇒ E ^c D :=
functor.mk (functor_curry_ob F)
(functor_curry_hom F)
(functor_curry_id F)
(functor_curry_comp F)
definition functor_uncurry_ob [reducible] (p : C ×c D) : E :=
to_fun_ob (G p.1) p.2
local abbreviation Gob := @functor_uncurry_ob
definition functor_uncurry_hom ⦃p p' : C ×c D⦄ (f : hom p p') : Gob G p ⟶ Gob G p' :=
to_fun_hom (to_fun_ob G p'.1) f.2 ∘ natural_map (to_fun_hom G f.1) p.2
local abbreviation Ghom := @functor_uncurry_hom
theorem functor_uncurry_id (p : C ×c D) : Ghom G (ID p) = id :=
calc
Ghom G (ID p) = to_fun_hom (to_fun_ob G p.1) id ∘ natural_map (to_fun_hom G id) p.2 : by esimp
... = id ∘ natural_map (to_fun_hom G id) p.2 : by rewrite respect_id
... = id ∘ natural_map nat_trans.id p.2 : by rewrite respect_id
... = id : id_comp
theorem functor_uncurry_comp ⦃p p' p'' : C ×c D⦄ (f' : p' ⟶ p'') (f : p ⟶ p')
: Ghom G (f' ∘ f) = Ghom G f' ∘ Ghom G f :=
calc
Ghom G (f' ∘ f)
= to_fun_hom (to_fun_ob G p''.1) (f'.2 ∘ f.2) ∘ natural_map (to_fun_hom G (f'.1 ∘ f.1)) p.2 : by esimp
... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2)
∘ natural_map (to_fun_hom G (f'.1 ∘ f.1)) p.2 : by rewrite respect_comp
... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2)
∘ natural_map (to_fun_hom G f'.1 ∘ to_fun_hom G f.1) p.2 : by rewrite respect_comp
... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2)
∘ (natural_map (to_fun_hom G f'.1) p.2 ∘ natural_map (to_fun_hom G f.1) p.2) : by esimp
... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2)
∘ (natural_map (to_fun_hom G f'.1) p.2 ∘ natural_map (to_fun_hom G f.1) p.2) : by esimp
... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ natural_map (to_fun_hom G f'.1) p'.2)
∘ (to_fun_hom (to_fun_ob G p'.1) f.2 ∘ natural_map (to_fun_hom G f.1) p.2) :
square_prepostcompose (!naturality⁻¹ᵖ) _ _
... = Ghom G f' ∘ Ghom G f : by esimp
definition functor_uncurry [reducible] : C ×c D ⇒ E :=
functor.mk (functor_uncurry_ob G)
(functor_uncurry_hom G)
(functor_uncurry_id G)
(functor_uncurry_comp G)
theorem functor_uncurry_functor_curry : functor_uncurry (functor_curry F) = F :=
functor_eq (λp, ap (to_fun_ob F) !prod.eta)
begin
intros (cd, cd', fg),
cases cd with (c,d), cases cd' with (c',d'), cases fg with (f,g),
apply concat, apply id_leftright,
show (functor_uncurry (functor_curry F)) (f, g) = F (f,g),
from calc
(functor_uncurry (functor_curry F)) (f, g) = to_fun_hom F (id, g) ∘ to_fun_hom F (f, id) : by esimp
... = F (id ∘ f, g ∘ id) : respect_comp F (id,g) (f,id)
... = F (f, g ∘ id) : by rewrite id_left
... = F (f,g) : by rewrite id_right,
end
definition functor_curry_functor_uncurry_ob (c : C)
: functor_curry (functor_uncurry G) c = G c :=
begin
fapply functor_eq,
{intro d, apply idp},
{intros (d, d', g),
apply concat, apply id_leftright,
show to_fun_hom (functor_curry (functor_uncurry G) c) g = to_fun_hom (G c) g,
from calc
to_fun_hom (functor_curry (functor_uncurry G) c) g
= to_fun_hom (G c) g ∘ natural_map (to_fun_hom G (ID c)) d : by esimp
... = to_fun_hom (G c) g ∘ natural_map (ID (G c)) d
: by rewrite respect_id
... = to_fun_hom (G c) g : id_right}
end
theorem functor_curry_functor_uncurry : functor_curry (functor_uncurry G) = G :=
begin
fapply functor_eq, exact (functor_curry_functor_uncurry_ob G),
intros (c, c', f),
fapply nat_trans_eq_mk,
intro d,
apply concat,
{apply (ap (λx, x ∘ _)),
apply concat, apply natural_map_hom_of_eq, apply (ap hom_of_eq), apply ap010_functor_eq},
apply concat,
{apply (ap (λx, _ ∘ x)), apply (ap (λx, _ ∘ x)),
apply concat, apply natural_map_inv_of_eq,
apply (ap (λx, hom_of_eq x⁻¹)), apply ap010_functor_eq},
apply concat, apply id_leftright,
apply concat, apply (ap (λx, x ∘ _)), apply respect_id,
apply id_left
end
definition prod_functor_equiv_functor_functor (C D E : Precategory)
: (C ×c D ⇒ E) ≃ (C ⇒ E ^c D) :=
equiv.MK functor_curry
functor_uncurry
functor_curry_functor_uncurry
functor_uncurry_functor_curry
definition functor_prod_flip (C D : Precategory) : C ×c D ⇒ D ×c C :=
functor.mk (λp, (p.2, p.1))
(λp p' h, (h.2, h.1))
(λp, idp)
(λp p' p'' h' h, idp)
definition functor_prod_flip_functor_prod_flip (C D : Precategory)
: functor_prod_flip D C ∘f (functor_prod_flip C D) = functor.id :=
begin
fapply functor_eq, {intro p, apply prod.eta},
intros (p, p', h), cases p with (c, d), cases p' with (c', d'),
apply id_leftright,
end
end functor
open functor
namespace yoneda
--should this be defined as "yoneda_embedding Cᵒᵖ"?
definition contravariant_yoneda_embedding (C : Precategory) : Cᵒᵖ ⇒ set ^c C :=
functor_curry !hom_functor
definition yoneda_embedding (C : Precategory) : C ⇒ set ^c Cᵒᵖ :=
functor_curry (!hom_functor ∘f !functor_prod_flip)
end yoneda
|
6107aa5de9fbf0e23867bc75f9d6902ccab2180c | caa1512363b76923d0e9cdb716122a5c26c3c6bc | /src/colvec.lean | 3cc1711dab1ad8a53d92e1834a908c54c2f04e46 | [] | no_license | apurvanakade/cvx | deb20e425ce478159a98e1ffc0d37f9c88a89280 | b47784831339df5a3e45f5cddd84edc545f95808 | refs/heads/master | 1,687,403,288,536 | 1,555,930,740,000 | 1,555,930,740,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,835 | lean |
import tactic.interactive
import .mat .one_by_one_matrix
import tactic.transfer
import logic.relator
import .inner_product_space
-- TODO: move
namespace fin
open finset
lemma sum_image_succ_univ {α : Type} [comm_ring α] {n : nat} (f : fin (n + 1) → α) :
sum (image fin.succ univ) (λ x, f x) = sum univ (λ i, f (fin.succ i)) :=
begin
rw finset.sum_image,
intros _ _ _ _ h,
apply (eq_iff_veq _ _).2,
apply (eq_iff_veq _ _).1 (succ.inj h),
end
lemma image_succ_univ (n : nat) :
insert (⟨0, nat.zero_lt_succ n⟩ : fin (n + 1)) (image fin.succ (univ : finset (fin n)))
= (univ : finset (fin (n + 1))) :=
begin
apply subset.antisymm,
{ intros k hk,
rw mem_insert at hk,
apply or.elim hk (λ _, mem_univ) (λ _, mem_univ),
},
{ intros k _,
by_cases h_cases: k = ⟨0, nat.zero_lt_succ n⟩,
{ rw h_cases,
apply mem_insert_self _ },
{ apply mem_insert_of_mem,
rw mem_image,
use fin.pred k h_cases,
use mem_univ _,
apply succ_pred } }
end
lemma not_mem_image_succ_univ (n : nat) :
¬ (⟨0, nat.zero_lt_succ n⟩ : fin (n + 1)) ∈ image fin.succ (univ : finset (fin n)) :=
begin
intros h,
rw mem_image at h,
apply exists.elim h,
intros k hk,
apply exists.elim hk,
intros _ hk',
rw eq_iff_veq at hk',
simp at hk',
exact nat.succ_ne_zero _ hk'
end
lemma sum_succ {α : Type} [comm_ring α] {n : nat} (f : fin (n + 1) → α) :
finset.sum finset.univ (λ x : fin (n + 1), f x) = f ⟨0, nat.zero_lt_succ n⟩ + finset.sum finset.univ (λ x : fin n, f x.succ) :=
begin
rw [← sum_image_succ_univ],
rw [← image_succ_univ n],
rw sum_insert,
apply not_mem_image_succ_univ n
end
end fin
section transport
open tactic
@[user_attribute]
meta def to_rowvec_attr : user_attribute (name_map name) name :=
{ name := `to_rowvec,
descr := "Transport column vector to row vector",
cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do
val ← to_rowvec_attr.get_param n,
pure $ dict.insert n val) mk_name_map, []⟩,
parser := lean.parser.ident,
after_set := some $ λ src _ _, do
env ← get_env,
dict ← to_rowvec_attr.get_cache,
tgt ← to_rowvec_attr.get_param src,
(get_decl tgt >> skip) <|>
transport_with_dict dict src tgt }
end transport
def colvec (n : Type) [fintype n] (α : Type) : Type := matrix n (fin 1) α
def rowvec (n : Type) [fintype n] (α : Type) : Type := matrix (fin 1) n α
attribute [to_rowvec rowvec] colvec
section instances
variables {α : Type} {n : Type} [fintype n]
instance colvec.has_scalar [has_mul α] : has_scalar α (colvec n α) := matrix.has_scalar
instance rowvec.has_scalar [has_mul α] : has_scalar α (rowvec n α) := matrix.has_scalar
attribute [to_rowvec rowvec.has_scalar] colvec.has_scalar
instance colvec.has_add [has_add α] : has_add (colvec n α) := matrix.has_add
instance rowvec.has_add [has_add α] : has_add (rowvec n α) := matrix.has_add
attribute [to_rowvec rowvec.has_add] colvec.has_add
instance colvec.has_zero [has_zero α] : has_zero (colvec n α) := matrix.has_zero
instance rowvec.has_zero [has_zero α] : has_zero (rowvec n α) := matrix.has_zero
attribute [to_rowvec rowvec.has_zero] colvec.has_zero
instance colvec.add_comm_group [add_comm_group α] : add_comm_group (colvec n α) := matrix.add_comm_group
instance rowvec.add_comm_group [add_comm_group α] : add_comm_group (rowvec n α) := matrix.add_comm_group
attribute [to_rowvec rowvec.add_comm_group] colvec.add_comm_group
instance colvec.module [ring α] : module α (colvec n α) := matrix.module
instance rowvec.module [ring α] : module α (rowvec n α) := matrix.module
attribute [to_rowvec rowvec.add_comm_group] colvec.add_comm_group
def colvec.nth (x : colvec n α) (i : n) : α := x i 0
def rowvec.nth (x : rowvec n α) (i : n) : α := x 0 i
attribute [to_rowvec rowvec.nth] colvec.nth
def colvec.mk (f : n → α) : colvec n α := λ i j, f i
def rowvec.mk (f : n → α) : rowvec n α := λ i j, f j
attribute [to_rowvec rowvec.mk] colvec.mk
@[simp, to_rowvec rowvec.nth_mk]
lemma colvec.nth_mk (f : n → α) : colvec.nth (colvec.mk f) = f :=
by refl
@[simp, to_rowvec rowvec.nth_mk]
lemma colvec.nth_zero [has_zero α] (i : n): (0 : colvec n α).nth i = 0 :=
by refl
lemma colvec.eq_of_nth_eq (x : colvec n α) (y : colvec n α) (h : ∀i, x.nth i = y.nth i) : x = y :=
begin
ext i j,
rw fin.eq_zero_fin1 j,
apply h,
end
lemma rowvec.eq_of_nth_eq (x : rowvec n α) (y : rowvec n α) (h : ∀i, x.nth i = y.nth i) : x = y :=
begin
ext i j,
rw fin.eq_zero_fin1 i,
apply h,
end
attribute [to_rowvec rowvec.eq_of_nth_eq] colvec.eq_of_nth_eq
end instances
section inner
variables {α : Type} [has_mul α] [add_comm_monoid α] {n : Type} [fintype n]
def colvec.inner (v w : colvec n α) : α := (matrix.mul vᵀ w) 0 0
def rowvec.inner (v w : rowvec n α) : α := (matrix.mul v wᵀ) 0 0
end inner
section transfer
variables {α : Type} [has_mul α] [add_comm_monoid α] {n : Type} [fintype n]
open transfer
/- Set up relators for transfer tactic to transfer from colvec to rowvec -/
lemma colvec.rel_eq_inner {α : Type} [comm_ring α]:
((λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ (λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ @eq α) rowvec.inner colvec.inner :=
begin
intros x xt hx y yt hy,
unfold colvec.inner rowvec.inner,
rw [hy,←hx,matrix.transpose_transpose]
end
lemma colvec.rel_transpose_add {α : Type} [comm_ring α]:
((λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ (λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ (λ (x : rowvec n α) (y : colvec n α), xᵀ = y)) (+) (+) :=
begin
intros x xt hx y yt hy,
rw [←hx,←hy,matrix.transpose_add]
end
lemma relator.rel_eq_add {α : Type} [comm_ring α]:
((@eq α) ⇒ (@eq α) ⇒ (@eq α)) (+) (+) :=
begin
intros x x' hx y y' hy,
rw [hx,hy]
end
lemma relator.rel_le {α : Type} [has_le α] :
(@eq α ⇒ @eq α ⇒ (↔)) (≤) (≤) :=
λ _ _ h1 _ _ h2, by rw [h1, h2]
lemma relator.rel_pos {α : Type} [has_le α] [has_zero α] :
(@eq α ⇒ (↔)) (has_le.le 0) (has_le.le 0) :=
λ _ _ h, by rw [h]
lemma relator.rel_eq_const {α : Type} {a : rowvec n α} :
((@eq (rowvec n α))) (a) (a) := rfl
lemma relator.rel_eq_zero_transpose {α : Type} [has_zero α] :
((λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ (↔)) (eq 0) (eq 0) :=
begin
intros x y h,
convert matrix.eq_iff_transpose_eq _ _,
apply h.symm
end
lemma colvec.rel_smul_transpose {α : Type} [has_mul α] (a : α) :
((λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ (λ (x : rowvec n α) (y : colvec n α), xᵀ = y)) (has_scalar.smul a) (has_scalar.smul a) :=
λ _ _ h, by rw [matrix.transpose_smul,h]
lemma relator.rel_mul_const {α : Type} [has_mul α] (a : α) :
((=) ⇒ (=)) (has_mul.mul a) (has_mul.mul a) :=
λ _ _ h, by rw [h]
lemma relator.rel_eq_zero {α : Type} [has_zero α] :
((@eq α) ⇒ (↔)) (eq 0) (eq 0) :=
λ _ _ h, by rw [h]
-- TODO: move
lemma relator.rel_imp' : ((↔) ⇒ (↔) ⇒ (↔)) (→) (→) :=
λ _ _ h1 _ _ h2, by rw [h1, h2]
-- TODO: move
lemma relator.rel_eq_same {α : Type} : (@eq α ⇒ @eq α ⇒ (↔)) (=) (=) :=
begin
apply relator.rel_eq,
split,
exact λ a b c h1 h2, eq.trans h1 h2.symm,
exact λ a b c h1 h2, eq.trans h1.symm h2
end
instance colvec.bi_total_transpose : relator.bi_total (λ (x : rowvec n α) (y : colvec n α), xᵀ = y) :=
⟨λ x, ⟨xᵀ, eq.refl _⟩, λ x, ⟨xᵀ, matrix.transpose_transpose _⟩⟩
lemma colvec.rel_forall_transpose : (((λ (x : rowvec n α) (y : colvec n α), xᵀ = y) ⇒ iff) ⇒ iff) (λp, ∀i, p i) (λq, ∀i, q i) :=
by apply relator.rel_forall_of_total
meta def colvec.transfer : tactic unit :=
transfer [
`relator.rel_eq_zero_transpose,
`relator.rel_eq_zero,
`relator.rel_eq_same,
`relator.rel_eq_add,
`relator.rel_pos,
`relator.rel_imp',
`colvec.rel_forall_transpose,
`colvec.rel_eq_inner,
`colvec.rel_transpose_add,
`relator.rel_eq_const,
`colvec.rel_smul_transpose,
`relator.rel_mul_const
]
end transfer
section inner
variables {α : Type} [comm_ring α] {n : Type} [fintype n] (a : α) (x y z : colvec n α)
lemma colvec.inner_comm : x.inner y = y.inner x :=
begin
dsimp [colvec.inner, colvec, mat],
intros,
rw [←matrix.transpose_transpose ((matrix.transpose y).mul x), matrix.transpose_mul, matrix.transpose_transpose y],
refl
end
lemma rowvec.inner_comm : ∀ (x y : rowvec n α), x.inner y = y.inner x :=
begin colvec.transfer, exact colvec.inner_comm end
lemma colvec.inner_add_left : (x + y).inner z = x.inner z + y.inner z :=
by simp [colvec.inner, colvec, mat, matrix.transpose_add, matrix.add_mul']
lemma rowvec.inner_add_left : ∀ (x y z : rowvec n α), (x + y).inner z = x.inner z + y.inner z :=
begin colvec.transfer, exact colvec.inner_add_left end
lemma colvec.inner_smul_left : (a • x).inner y = a * x.inner y :=
by simp [colvec.inner, colvec, mat, matrix.smul_mul, matrix.transpose_smul]; refl
lemma rowvec.inner_smul_left (a : α) : ∀ (x y : rowvec n α), (a • x).inner y = a * x.inner y :=
begin colvec.transfer, exact colvec.inner_smul_left a end
end inner
section inner
variables {α : Type} [linear_ordered_comm_ring α] {m : Type} [fintype m] {n : Type} [fintype n] [decidable_eq n] (a : α) (x y z : colvec n α)
lemma colvec.inner_self_nonneg : 0 ≤ x.inner x :=
begin
dsimp [colvec.inner, colvec, mat, matrix.mul],
apply finset.zero_le_sum,
intros i _,
apply mul_self_nonneg
end
lemma rowvec.inner_self_nonneg : ∀ (x : rowvec n α), 0 ≤ x.inner x :=
begin colvec.transfer, exact colvec.inner_self_nonneg end
lemma colvec.eq_zero_of_inner_self_eq_zero : 0 = x.inner x → 0 = x :=
begin
dsimp [colvec.inner, colvec, mat, matrix.mul],
intros h,
apply colvec.eq_of_nth_eq,
intro i,
apply (eq_zero_of_mul_self_eq_zero ((finset.sum_eq_zero_iff_of_nonneg _).1 h.symm i (finset.mem_univ _))).symm,
intros i hi,
apply mul_self_nonneg
end
lemma rowvec.eq_zero_of_inner_self_eq_zero : ∀ (x : rowvec n α), 0 = x.inner x → 0 = x :=
begin colvec.transfer, exact colvec.eq_zero_of_inner_self_eq_zero end
local attribute [instance] classical.prop_decidable
noncomputable instance colvec.real_inner_product_space : real_inner_product_space (colvec n ℝ) :=
{
inner := colvec.inner,
inner_add_left := colvec.inner_add_left,
inner_smul_left := colvec.inner_smul_left,
inner_comm := colvec.inner_comm,
inner_self_nonneg := colvec.inner_self_nonneg,
eq_zero_of_inner_self_eq_zero := λ x h, (colvec.eq_zero_of_inner_self_eq_zero x h.symm).symm
}
noncomputable instance rowvec.real_inner_product_space : real_inner_product_space (rowvec n ℝ) :=
{
inner := rowvec.inner,
inner_add_left := rowvec.inner_add_left,
inner_smul_left := rowvec.inner_smul_left,
inner_comm := rowvec.inner_comm,
inner_self_nonneg := rowvec.inner_self_nonneg,
eq_zero_of_inner_self_eq_zero := λ x h, (rowvec.eq_zero_of_inner_self_eq_zero x h.symm).symm
}
lemma colvec.inner_eq_sum_nth :
x.inner y = finset.univ.sum (λi, x.nth i * y.nth i) :=
by unfold colvec.inner matrix.mul colvec.nth; refl
lemma rowvec.inner_eq_sum_nth (x : rowvec n α) (y : rowvec n α) :
x.inner y = finset.univ.sum (λi, x.nth i * y.nth i) :=
by unfold rowvec.inner matrix.mul colvec.nth; refl
attribute [to_rowvec rowvec.inner_eq_sum_nth] colvec.inner_eq_sum_nth
--TODO @[to_rowvec rowvec.inner_mul]
lemma colvec.inner_mul (A : matrix m n ℝ) (x : colvec n ℝ) (y : colvec m ℝ) :
⟪ x, Aᵀ.mul y ⟫ = ⟪ y, A.mul x ⟫ :=
begin
unfold has_inner.inner colvec.inner,
rw [←one_by_one_matrix.transpose ℝ (matrix.mul xᵀ (matrix.mul Aᵀ y)),
matrix.transpose_mul, matrix.transpose_mul, ←matrix.mul_assoc,
matrix.transpose_transpose, matrix.transpose_transpose]
end
end inner
section fin_index
variables {α : Type} {n : nat} (a : α) (x y z : colvec (fin n) α)
@[to_rowvec rowvec.head]
def colvec.head (x : colvec (fin (n+1)) α) : α := x.nth ⟨0, nat.zero_lt_succ n⟩
@[to_rowvec rowvec.tail]
def colvec.tail (x : colvec (fin (n+1)) α) : colvec (fin n) α :=
colvec.mk (λ i, x.nth ⟨i.1.succ, nat.succ_lt_succ i.2⟩)
@[to_rowvec rowvec.cons]
def colvec.cons (c : α) (x : colvec (fin n) α) : colvec (fin (n+1)) α :=
colvec.mk (λ i, dite (i.val = 0) (λ_, c) (λhi, x.nth ⟨i.val.pred,nat.pred_lt_pred hi i.2⟩))
@[simp, to_rowvec rowvec.head_smul]
lemma colvec.head_smul [has_mul α] (c : α) (x : colvec (fin (n+1)) α) :
(c • x).head = c * x.head :=
by refl
@[simp, to_rowvec rowvec.tail_smul]
lemma colvec.tail_smul [has_mul α] (c : α) (x : colvec (fin (n+1)) α) :
(c • x).tail = c • x.tail :=
by refl
@[simp, to_rowvec rowvec.head_cons]
lemma colvec.head_cons (c : α) (x : colvec (fin n) α) : (colvec.cons c x).head = c :=
by refl
-- TODO: get rid of this?
attribute [to_rowvec rowvec.head.equations._eqn_1] colvec.head.equations._eqn_1
attribute [to_rowvec rowvec.tail.equations._eqn_1] colvec.tail.equations._eqn_1
attribute [to_rowvec rowvec.cons.equations._eqn_1] colvec.cons.equations._eqn_1
@[simp, to_rowvec rowvec.tail_cons]
lemma colvec.tail_cons (x : colvec (fin n) α) (c : α) : (colvec.cons c x).tail = x :=
by apply colvec.eq_of_nth_eq; simp [colvec.tail,colvec.cons,nat.pred_succ,dif_eq_if, nat.succ_ne_zero]
@[simp, to_rowvec rowvec.cons_head_tail]
lemma colvec.cons_head_tail (x : colvec (fin (n+1)) α) : colvec.cons x.head x.tail = x :=
begin
apply colvec.eq_of_nth_eq,
intro i,
unfold colvec.cons colvec.head colvec.tail,
by_cases h_cases : i.val = 0,
{ have hi : i = ⟨0, nat.zero_lt_succ n⟩,
from (fin.eq_iff_veq _ _).2 h_cases,
simp [hi] },
{ simp [h_cases, nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero h_cases)] },
end
--TODO: @[to_rowvec rowvec.last]
lemma colvec.mul_head_add_mul_tail {α : Type} [comm_ring α] (x y : colvec (fin (n+1)) ℝ) :
x.head * y.head + ⟪ x.tail, y.tail ⟫ = ⟪ x, y ⟫ :=
begin
unfold has_inner.inner,
simp only [colvec.inner_eq_sum_nth],
unfold colvec.head colvec.tail,
rw fin.sum_succ (λ j : fin (n+1), x.nth j * y.nth j),
congr,
simp,
funext,
congr,
apply (fin.eq_iff_veq _ _).2,
simp,
apply (fin.eq_iff_veq _ _).2,
simp,
end
@[to_rowvec rowvec.last]
def colvec.last (x : colvec (fin (n+1)) α) : α := x.nth ⟨n, nat.lt_succ_self n⟩
@[to_rowvec rowvec.butlast]
def colvec.butlast (x : colvec (fin (n+1)) α) : colvec (fin n) α :=
colvec.mk (λ i, x.nth ⟨i.1, nat.le_succ_of_le i.2⟩)
@[to_rowvec rowvec.snoc]
def colvec.snoc (x : colvec (fin n) α) (c : α) : colvec (fin (n+1)) α :=
colvec.mk (λ i, dite (i.val < n) (λhi, x.nth ⟨i.val,hi⟩) (λ_, c))
--TODO: Lemmas about last, butlast, snoc
end fin_index
|
f724880682e272176e50da3eff52f2cfe9efdbaa | bdb33f8b7ea65f7705fc342a178508e2722eb851 | /data/set/countable.lean | b5994de8087efcd8ed52280ced92dbf23b0f3406 | [
"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 | 6,439 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
Countable sets.
-/
import data.encodable data.set.finite logic.function
noncomputable theory
open function set encodable
open classical (hiding some)
local attribute [instance] prop_decidable
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
namespace set
/-- Countable sets
A set is countable if there exists a injective functions from the set into the natural numbers.
This is choosen instead of surjective functions, as this would require that α is non empty.
-/
def countable (s : set α) : Prop := ∃f:α → ℕ, ∀x ∈ s, ∀y ∈ s, f x = f y → x = y
lemma countable_iff_exists_surjective [ne : inhabited α] {s : set α} :
countable s ↔ (∃f:ℕ → α, s ⊆ range f) :=
iff.intro
(assume ⟨f, hf⟩, ⟨inv_fun_on f s, assume a ha, ⟨f a, inv_fun_on_eq' hf ha⟩⟩)
(assume ⟨f, hf⟩, ⟨inv_fun f, assume x hx y hy h,
calc x = f (inv_fun f x) : (inv_fun_eq $ hf hx).symm
... = f (inv_fun f y) : by rw [h]
... = y : inv_fun_eq $ hf hy⟩)
lemma countable.to_encodable {s : set α} (h : countable s) : encodable s :=
let f := classical.some h in
have hf : ∀x∈s, ∀y∈s, f x = f y → x = y, from classical.some_spec h,
let f' : {a // a ∈ s} → ℕ := f ∘ subtype.val in
encodable_of_inj f' $ assume ⟨a, ha⟩ ⟨b, hb⟩ (h : f a = f b), subtype.eq $ hf a ha b hb h
lemma countable_encodable' (s : set α) [encodable s] : countable s :=
⟨λx, if h : x ∈ s then @encode s _ ⟨x, h⟩ else 0, assume x hx y hy h,
have @encode s _ ⟨x, hx⟩ = @encode s _ ⟨y, hy⟩,
by simp [hx, hy] at h; assumption,
have decode s (@encode s _ ⟨x, hx⟩) = decode s (@encode s _ ⟨y, hy⟩),
from congr_arg _ this,
by simpa [encodek]⟩
lemma countable_encodable [e : encodable α] {s : set α} : countable s :=
⟨encode, assume x _ y _ eq,
have decode α (encode x) = decode α (encode y), from congr_arg _ eq,
by simpa [encodek]⟩
@[simp] lemma countable_empty : countable (∅ : set α) :=
⟨λ_, 0, by simp⟩
@[simp] lemma countable_singleton {a : α} : countable ({a} : set α) :=
⟨λ_, 0, by simp⟩
lemma countable_subset {s₁ s₂ : set α} (h : s₁ ⊆ s₂) : countable s₂ → countable s₁
| ⟨f, hf⟩ := ⟨f, assume x hx y hy eq, hf x (h hx) y (h hy) eq⟩
lemma countable_image {s : set α} {f : α → β} (hs : countable s) : countable (f '' s) :=
let f' : s → f '' s := λ⟨a, ha⟩, ⟨f a, mem_image_of_mem f ha⟩ in
have hf' : surjective f', from assume ⟨b, a, ha, hab⟩, ⟨⟨a, ha⟩, subtype.eq hab⟩,
@countable_encodable' _ _ $ @encodable_of_inj _ _ hs.to_encodable (surj_inv hf') (injective_surj_inv hf')
lemma countable_sUnion {s : set (set α)} (hs : countable s) (h : ∀a∈s, countable a) :
countable (⋃₀ s) :=
by_cases
(assume : nonempty α, let ⟨a⟩ := this, inh : inhabited α := ⟨a⟩ in
let ⟨fs, hfs⟩ := countable_iff_exists_surjective.mp hs in
have ∀t, ∃ft:ℕ → α, t ∈ s → t ⊆ range ft,
from assume t,
by_cases
(assume : t ∈ s,
let ⟨ft, hft⟩ := (@countable_iff_exists_surjective α inh _).mp $ h t this in
⟨ft, assume _, hft⟩)
(assume : t ∉ s, ⟨λ_, a, assume h, (this h).elim⟩),
have ∃ft:(∀t:set α, ℕ → α), ∀t∈s, t ⊆ range (ft t),
by simp [classical.skolem] at this; assumption,
let ⟨ft, hft⟩ := this in
(@countable_iff_exists_surjective α inh _).mpr
⟨(λp:ℕ×ℕ, ft (fs p.1) p.2) ∘ nat.unpair,
by simp [subset_def];
from assume a t ht ha,
let ⟨i, hi⟩ := hfs ht, ⟨j, hj⟩ := hft t ht ha in
⟨nat.mkpair i j, by simp [function.comp, nat.unpair_mkpair, hi, hj]⟩⟩)
(assume : ¬ nonempty α, ⟨λ_, 0, assume a, (this ⟨a⟩).elim⟩)
lemma countable_bUnion {s : set α} {t : α → set β} (hs : countable s) (ht : ∀a∈s, countable (t a)) :
countable (⋃a∈s, t a) :=
have ⋃₀ (t '' s) = (⋃a∈s, t a), from lattice.Sup_image,
by rw [←this];
from (countable_sUnion (countable_image hs) $ assume a ⟨s', hs', eq⟩, eq ▸ ht s' hs')
lemma countable_Union {t : α → set β} [encodable α] (ht : ∀a, countable (t a)) :
countable (⋃a, t a) :=
suffices countable (⋃a∈(univ : set α), t a), by simpa,
countable_bUnion countable_encodable (assume a _, ht a)
lemma countable_Union_Prop {p : Prop} {t : p → set β} (ht : ∀h:p, countable (t h)) :
countable (⋃h:p, t h) :=
by by_cases p; simp [h, ht]
lemma countable_union {s₁ s₂ : set α} (h₁ : countable s₁) (h₂ : countable s₂) : countable (s₁ ∪ s₂) :=
have s₁ ∪ s₂ = (⨆b ∈ ({tt, ff} : set bool), bool.cases_on b s₁ s₂),
by simp [lattice.supr_or, lattice.supr_sup_eq]; refl,
by rw [this]; from countable_bUnion countable_encodable (assume b,
match b with
| tt := by simp [h₂]
| ff := by simp [h₁]
end)
lemma countable_insert {s : set α} {a : α} (h : countable s) : countable (insert a s) :=
by rw [set.insert_eq]; from countable_union countable_singleton h
lemma countable_finite {s : set α} : finite s → countable s
| ⟨h⟩ := by resetI; haveI := (trunc_encodable_of_fintype s).out; apply countable_encodable'
lemma countable_set_of_finite_subset {s : set α} (h : countable s) :
countable {t | finite t ∧ t ⊆ s } :=
have {t | finite t ∧ t ⊆ s } ⊆
(λt : finset {a:α // a ∈ s}, {a:α | ∃h:a∈s, subtype.mk a h ∈ t}) '' univ,
from assume t ht,
begin
cases ht with ht₁ ht₂, revert ht₂,
refine finite.induction_on ht₁ _ (λ a t ha ht ih, _); intro ht₂,
{ exact ⟨∅, mem_univ _, by simp⟩ },
{ exact
have has : a ∈ s, from ht₂ $ mem_insert _ _,
have t ⊆ s, from assume x hx, ht₂ $ mem_insert_of_mem _ hx,
let ⟨t', ht', eq⟩ := ih this in
⟨insert ⟨a, has⟩ t', mem_univ _,
set.ext $ assume x,
begin
simp [eq.symm, iff_def, or_imp_distrib, has] {contextual:=tt},
constructor,
exact assume hxs, or.imp_right (assume hxt', ⟨hxs, hxt'⟩),
exact assume hxs hxt', ⟨hxs, or.inr hxt'⟩,
end⟩ }
end,
by haveI enc := h.to_encodable; exact
countable_subset this (countable_image countable_encodable)
end set
|
d09a3944a7cb7950bae6af9daa5425b823f42a0d | 4727251e0cd73359b15b664c3170e5d754078599 | /src/linear_algebra/matrix/transvection.lean | 0f5f266db9a49ad2e0c1fcafe229c2845a87b950 | [
"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 | 32,951 | lean | /-
Copyright (c) 2021 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.matrix.basis
import data.matrix.dmatrix
import linear_algebra.matrix.determinant
import linear_algebra.matrix.trace
import linear_algebra.matrix.reindex
import tactic.field_simp
/-!
# Transvections
Transvections are matrices of the form `1 + std_basis_matrix i j c`, where `std_basis_matrix i j c`
is the basic matrix with a `c` at position `(i, j)`. Multiplying by such a transvection on the left
(resp. on the right) amounts to adding `c` times the `j`-th row to to the `i`-th row
(resp `c` times the `i`-th column to the `j`-th column). Therefore, they are useful to present
algorithms operating on rows and columns.
Transvections are a special case of *elementary matrices* (according to most references, these also
contain the matrices exchanging rows, and the matrices multiplying a row by a constant).
We show that, over a field, any matrix can be written as `L ⬝ D ⬝ L'`, where `L` and `L'` are
products of transvections and `D` is diagonal. In other words, one can reduce a matrix to diagonal
form by operations on its rows and columns, a variant of Gauss' pivot algorithm.
## Main definitions and results
* `transvection i j c` is the matrix equal to `1 + std_basis_matrix i j c`.
* `transvection_struct n R` is a structure containing the data of `i, j, c` and a proof that
`i ≠ j`. These are often easier to manipulate than straight matrices, especially in inductive
arguments.
* `exists_list_transvec_mul_diagonal_mul_list_transvec` states that any matrix `M` over a field can
be written in the form `t_1 ⬝ ... ⬝ t_k ⬝ D ⬝ t'_1 ⬝ ... ⬝ t'_l`, where `D` is diagonal and
the `t_i`, `t'_j` are transvections.
* `diagonal_transvection_induction` shows that a property which is true for diagonal matrices and
transvections, and invariant under product, is true for all matrices.
* `diagonal_transvection_induction_of_det_ne_zero` is the same statement over invertible matrices.
## Implementation details
The proof of the reduction results is done inductively on the size of the matrices, reducing an
`(r + 1) × (r + 1)` matrix to a matrix whose last row and column are zeroes, except possibly for
the last diagonal entry. This step is done as follows.
If all the coefficients on the last row and column are zero, there is nothing to do. Otherwise,
one can put a nonzero coefficient in the last diagonal entry by a row or column operation, and then
subtract this last diagonal entry from the other entries in the last row and column to make them
vanish.
This step is done in the type `fin r ⊕ unit`, where `fin r` is useful to choose arbitrarily some
order in which we cancel the coefficients, and the sum structure is useful to use the formalism of
block matrices.
To proceed with the induction, we reindex our matrices to reduce to the above situation.
-/
universes u₁ u₂
namespace matrix
open_locale matrix
variables (n p : Type*) (R : Type u₂) {𝕜 : Type*} [field 𝕜]
variables [decidable_eq n] [decidable_eq p]
variables [comm_ring R]
section transvection
variables {R n} (i j : n)
/-- The transvection matrix `transvection i j c` is equal to the identity plus `c` at position
`(i, j)`. Multiplying by it on the left (as in `transvection i j c ⬝ M`) corresponds to adding
`c` times the `j`-th line of `M` to its `i`-th line. Multiplying by it on the right corresponds
to adding `c` times the `i`-th column to the `j`-th column. -/
def transvection (c : R) : matrix n n R := 1 + matrix.std_basis_matrix i j c
@[simp] lemma transvection_zero : transvection i j (0 : R) = 1 :=
by simp [transvection]
section
variable [fintype n]
/-- A transvection matrix is obtained from the identity by adding `c` times the `j`-th row to
the `i`-th row. -/
lemma update_row_eq_transvection (c : R) :
update_row (1 : matrix n n R) i (((1 : matrix n n R)) i + c • (1 : matrix n n R) j) =
transvection i j c :=
begin
ext a b,
by_cases ha : i = a, by_cases hb : j = b,
{ simp only [update_row, transvection, ha, hb, function.update_same, std_basis_matrix.apply_same,
pi.add_apply, one_apply_eq, pi.smul_apply, mul_one, algebra.id.smul_eq_mul], },
{ simp only [update_row, transvection, ha, hb, std_basis_matrix.apply_of_ne, function.update_same,
pi.add_apply, ne.def, not_false_iff, pi.smul_apply, and_false, one_apply_ne,
algebra.id.smul_eq_mul, mul_zero] },
{ simp only [update_row, transvection, ha, ne.symm ha, std_basis_matrix.apply_of_ne, add_zero,
algebra.id.smul_eq_mul, function.update_noteq, ne.def, not_false_iff, dmatrix.add_apply,
pi.smul_apply, mul_zero, false_and] },
end
lemma transvection_mul_transvection_same (h : i ≠ j) (c d : R) :
transvection i j c ⬝ transvection i j d = transvection i j (c + d) :=
by simp [transvection, matrix.add_mul, matrix.mul_add, h, h.symm, add_smul, add_assoc,
std_basis_matrix_add]
@[simp] lemma transvection_mul_apply_same (b : n) (c : R) (M : matrix n n R) :
(transvection i j c ⬝ M) i b = M i b + c * M j b :=
by simp [transvection, matrix.add_mul]
@[simp] lemma mul_transvection_apply_same (a : n) (c : R) (M : matrix n n R) :
(M ⬝ transvection i j c) a j = M a j + c * M a i :=
by simp [transvection, matrix.mul_add, mul_comm]
@[simp] lemma transvection_mul_apply_of_ne (a b : n) (ha : a ≠ i) (c : R) (M : matrix n n R) :
(transvection i j c ⬝ M) a b = M a b :=
by simp [transvection, matrix.add_mul, ha]
@[simp] lemma mul_transvection_apply_of_ne (a b : n) (hb : b ≠ j) (c : R) (M : matrix n n R) :
(M ⬝ transvection i j c) a b = M a b :=
by simp [transvection, matrix.mul_add, hb]
@[simp] lemma det_transvection_of_ne (h : i ≠ j) (c : R) : det (transvection i j c) = 1 :=
by rw [← update_row_eq_transvection i j, det_update_row_add_smul_self _ h, det_one]
end
variables (R n)
/-- A structure containing all the information from which one can build a nontrivial transvection.
This structure is easier to manipulate than transvections as one has a direct access to all the
relevant fields. -/
@[nolint has_inhabited_instance]
structure transvection_struct :=
(i j : n)
(hij : i ≠ j)
(c : R)
instance [nontrivial n] : nonempty (transvection_struct n R) :=
by { choose x y hxy using exists_pair_ne n, exact ⟨⟨x, y, hxy, 0⟩⟩ }
namespace transvection_struct
variables {R n}
/-- Associating to a `transvection_struct` the corresponding transvection matrix. -/
def to_matrix (t : transvection_struct n R) : matrix n n R :=
transvection t.i t.j t.c
@[simp] lemma to_matrix_mk (i j : n) (hij : i ≠ j) (c : R) :
transvection_struct.to_matrix ⟨i, j, hij, c⟩ = transvection i j c := rfl
@[simp] protected lemma det [fintype n] (t : transvection_struct n R) : det t.to_matrix = 1 :=
det_transvection_of_ne _ _ t.hij _
@[simp] lemma det_to_matrix_prod [fintype n] (L : list (transvection_struct n 𝕜)) :
det ((L.map to_matrix).prod) = 1 :=
begin
induction L with t L IH,
{ simp },
{ simp [IH], }
end
/-- The inverse of a `transvection_struct`, designed so that `t.inv.to_matrix` is the inverse of
`t.to_matrix`. -/
@[simps] protected def inv (t : transvection_struct n R) : transvection_struct n R :=
{ i := t.i,
j := t.j,
hij := t.hij,
c := - t.c }
section
variable [fintype n]
lemma inv_mul (t : transvection_struct n R) :
t.inv.to_matrix ⬝ t.to_matrix = 1 :=
by { rcases t, simp [to_matrix, transvection_mul_transvection_same, t_hij] }
lemma mul_inv (t : transvection_struct n R) :
t.to_matrix ⬝ t.inv.to_matrix = 1 :=
by { rcases t, simp [to_matrix, transvection_mul_transvection_same, t_hij] }
lemma reverse_inv_prod_mul_prod (L : list (transvection_struct n R)) :
(L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝ (L.map to_matrix).prod = 1 :=
begin
induction L with t L IH,
{ simp },
{ suffices : (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝
(t.inv.to_matrix ⬝ t.to_matrix) ⬝ (L.map to_matrix).prod = 1, by simpa [matrix.mul_assoc],
simpa [inv_mul] using IH, }
end
lemma prod_mul_reverse_inv_prod (L : list (transvection_struct n R)) :
(L.map to_matrix).prod ⬝ (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod = 1 :=
begin
induction L with t L IH,
{ simp },
{ suffices : t.to_matrix ⬝ ((L.map to_matrix).prod ⬝
(L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod) ⬝ t.inv.to_matrix = 1,
by simpa [matrix.mul_assoc],
simp_rw [IH, matrix.mul_one, t.mul_inv], }
end
end
variables (p)
open sum
/-- Given a `transvection_struct` on `n`, define the corresponding `transvection_struct` on `n ⊕ p`
using the identity on `p`. -/
def sum_inl (t : transvection_struct n R) : transvection_struct (n ⊕ p) R :=
{ i := inl t.i,
j := inl t.j,
hij := by simp [t.hij],
c := t.c }
lemma to_matrix_sum_inl (t : transvection_struct n R) :
(t.sum_inl p).to_matrix = from_blocks t.to_matrix 0 0 1 :=
begin
cases t,
ext a b,
cases a; cases b,
{ by_cases h : a = b;
simp [transvection_struct.sum_inl, transvection, h, std_basis_matrix], },
{ simp [transvection_struct.sum_inl, transvection] },
{ simp [transvection_struct.sum_inl, transvection] },
{ by_cases h : a = b;
simp [transvection_struct.sum_inl, transvection, h] },
end
@[simp] lemma sum_inl_to_matrix_prod_mul [fintype n] [fintype p]
(M : matrix n n R) (L : list (transvection_struct n R)) (N : matrix p p R) :
(L.map (to_matrix ∘ sum_inl p)).prod ⬝ from_blocks M 0 0 N
= from_blocks ((L.map to_matrix).prod ⬝ M) 0 0 N :=
begin
induction L with t L IH,
{ simp },
{ simp [matrix.mul_assoc, IH, to_matrix_sum_inl, from_blocks_multiply], },
end
@[simp] lemma mul_sum_inl_to_matrix_prod [fintype n] [fintype p]
(M : matrix n n R) (L : list (transvection_struct n R)) (N : matrix p p R) :
(from_blocks M 0 0 N) ⬝ (L.map (to_matrix ∘ sum_inl p)).prod
= from_blocks (M ⬝ (L.map to_matrix).prod) 0 0 N :=
begin
induction L with t L IH generalizing M N,
{ simp },
{ simp [IH, to_matrix_sum_inl, from_blocks_multiply], },
end
variable {p}
/-- Given a `transvection_struct` on `n` and an equivalence between `n` and `p`, define the
corresponding `transvection_struct` on `p`. -/
def reindex_equiv (e : n ≃ p) (t : transvection_struct n R) : transvection_struct p R :=
{ i := e t.i,
j := e t.j,
hij := by simp [t.hij],
c := t.c }
variables [fintype n] [fintype p]
lemma to_matrix_reindex_equiv (e : n ≃ p) (t : transvection_struct n R) :
(t.reindex_equiv e).to_matrix = reindex_alg_equiv R e t.to_matrix :=
begin
cases t,
ext a b,
simp only [reindex_equiv, transvection, mul_boole, algebra.id.smul_eq_mul, to_matrix_mk,
minor_apply, reindex_apply, dmatrix.add_apply, pi.smul_apply, reindex_alg_equiv_apply],
by_cases ha : e t_i = a; by_cases hb : e t_j = b; by_cases hab : a = b;
simp [ha, hb, hab, ← e.apply_eq_iff_eq_symm_apply, std_basis_matrix]
end
lemma to_matrix_reindex_equiv_prod (e : n ≃ p) (L : list (transvection_struct n R)) :
(L.map (to_matrix ∘ (reindex_equiv e))).prod = reindex_alg_equiv R e (L.map to_matrix).prod :=
begin
induction L with t L IH,
{ simp },
{ simp only [to_matrix_reindex_equiv, IH, function.comp_app, list.prod_cons, mul_eq_mul,
reindex_alg_equiv_apply, list.map],
exact (reindex_alg_equiv_mul _ _ _ _).symm }
end
end transvection_struct
end transvection
/-!
# Reducing matrices by left and right multiplication by transvections
In this section, we show that any matrix can be reduced to diagonal form by left and right
multiplication by transvections (or, equivalently, by elementary operations on lines and columns).
The main step is to kill the last row and column of a matrix in `fin r ⊕ unit` with nonzero last
coefficient, by subtracting this coefficient from the other ones. The list of these operations is
recorded in `list_transvec_col M` and `list_transvec_row M`. We have to analyze inductively how
these operations affect the coefficients in the last row and the last column to conclude that they
have the desired effect.
Once this is done, one concludes the reduction by induction on the size
of the matrices, through a suitable reindexing to identify any fintype with `fin r ⊕ unit`.
-/
namespace pivot
variables {R} {r : ℕ} (M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜)
open sum unit fin transvection_struct
/-- A list of transvections such that multiplying on the left with these transvections will replace
the last column with zeroes. -/
def list_transvec_col : list (matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) :=
list.of_fn $ λ i : fin r, transvection (inl i) (inr star) $
-M (inl i) (inr star) / M (inr star) (inr star)
/-- A list of transvections such that multiplying on the right with these transvections will replace
the last row with zeroes. -/
def list_transvec_row : list (matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) :=
list.of_fn $ λ i : fin r, transvection (inr star) (inl i) $
-M (inr star) (inl i) / M (inr star) (inr star)
/-- Multiplying by some of the matrices in `list_transvec_col M` does not change the last row. -/
lemma list_transvec_col_mul_last_row_drop (i : fin r ⊕ unit) {k : ℕ} (hk : k ≤ r) :
(((list_transvec_col M).drop k).prod ⬝ M) (inr star) i = M (inr star) i :=
begin
apply nat.decreasing_induction' _ hk,
{ simp only [list_transvec_col, list.length_of_fn, matrix.one_mul, list.drop_eq_nil_of_le,
list.prod_nil], },
{ assume n hn hk IH,
have hn' : n < (list_transvec_col M).length, by simpa [list_transvec_col] using hn,
rw ← list.cons_nth_le_drop_succ hn',
simpa [list_transvec_col, matrix.mul_assoc] }
end
/-- Multiplying by all the matrices in `list_transvec_col M` does not change the last row. -/
lemma list_transvec_col_mul_last_row (i : fin r ⊕ unit) :
((list_transvec_col M).prod ⬝ M) (inr star) i = M (inr star) i :=
by simpa using list_transvec_col_mul_last_row_drop M i (zero_le _)
/-- Multiplying by all the matrices in `list_transvec_col M` kills all the coefficients in the
last column but the last one. -/
lemma list_transvec_col_mul_last_col (hM : M (inr star) (inr star) ≠ 0) (i : fin r) :
((list_transvec_col M).prod ⬝ M) (inl i) (inr star) = 0 :=
begin
suffices H : ∀ (k : ℕ), k ≤ r → (((list_transvec_col M).drop k).prod ⬝ M) (inl i) (inr star) =
if k ≤ i then 0 else M (inl i) (inr star),
by simpa only [if_true, list.drop.equations._eqn_1] using H 0 (zero_le _),
assume k hk,
apply nat.decreasing_induction' _ hk,
{ simp only [list_transvec_col, list.length_of_fn, matrix.one_mul, list.drop_eq_nil_of_le,
list.prod_nil],
rw if_neg,
simpa only [not_le] using i.2 },
{ assume n hn hk IH,
have hn' : n < (list_transvec_col M).length, by simpa [list_transvec_col] using hn,
let n' : fin r := ⟨n, hn⟩,
rw ← list.cons_nth_le_drop_succ hn',
have A : (list_transvec_col M).nth_le n hn' = transvection (inl n') (inr star)
(-M (inl n') (inr star) / M (inr star) (inr star)), by simp [list_transvec_col],
simp only [matrix.mul_assoc, A, matrix.mul_eq_mul, list.prod_cons],
by_cases h : n' = i,
{ have hni : n = i,
{ cases i, simp only [subtype.mk_eq_mk] at h, simp [h] },
rw [h, transvection_mul_apply_same, IH, list_transvec_col_mul_last_row_drop _ _ hn, ← hni],
field_simp [hM] },
{ have hni : n ≠ i,
{ rintros rfl, cases i, simpa using h },
simp only [transvection_mul_apply_of_ne, ne.def, not_false_iff, ne.symm h],
rw IH,
rcases le_or_lt (n+1) i with hi|hi,
{ simp only [hi, n.le_succ.trans hi, if_true] },
{ rw [if_neg, if_neg],
{ simpa only [hni.symm, not_le, or_false] using nat.lt_succ_iff_lt_or_eq.1 hi },
{ simpa only [not_le] using hi } } } }
end
/-- Multiplying by some of the matrices in `list_transvec_row M` does not change the last column. -/
lemma mul_list_transvec_row_last_col_take (i : fin r ⊕ unit) {k : ℕ} (hk : k ≤ r) :
(M ⬝ ((list_transvec_row M).take k).prod) i (inr star) = M i (inr star) :=
begin
induction k with k IH,
{ simp only [matrix.mul_one, list.take_zero, list.prod_nil], },
{ have hkr : k < r := hk,
let k' : fin r := ⟨k, hkr⟩,
have : (list_transvec_row M).nth k = ↑(transvection (inr unit.star) (inl k')
(-M (inr unit.star) (inl k') / M (inr unit.star) (inr unit.star))),
{ simp only [list_transvec_row, list.of_fn_nth_val, hkr, dif_pos, list.nth_of_fn], refl },
simp only [list.take_succ, ← matrix.mul_assoc, this, list.prod_append, matrix.mul_one,
matrix.mul_eq_mul, list.prod_cons, list.prod_nil, option.to_list_some],
rw [mul_transvection_apply_of_ne, IH hkr.le],
simp only [ne.def, not_false_iff], }
end
/-- Multiplying by all the matrices in `list_transvec_row M` does not change the last column. -/
lemma mul_list_transvec_row_last_col (i : fin r ⊕ unit) :
(M ⬝ (list_transvec_row M).prod) i (inr star) = M i (inr star) :=
begin
have A : (list_transvec_row M).length = r, by simp [list_transvec_row],
rw [← list.take_length (list_transvec_row M), A],
simpa using mul_list_transvec_row_last_col_take M i le_rfl,
end
/-- Multiplying by all the matrices in `list_transvec_row M` kills all the coefficients in the
last row but the last one. -/
lemma mul_list_transvec_row_last_row (hM : M (inr star) (inr star) ≠ 0) (i : fin r) :
(M ⬝ (list_transvec_row M).prod) (inr star) (inl i) = 0 :=
begin
suffices H : ∀ (k : ℕ), k ≤ r → (M ⬝ ((list_transvec_row M).take k).prod) (inr star) (inl i) =
if k ≤ i then M (inr star) (inl i) else 0,
{ have A : (list_transvec_row M).length = r, by simp [list_transvec_row],
rw [← list.take_length (list_transvec_row M), A],
have : ¬ (r ≤ i), by simpa using i.2,
simpa only [this, ite_eq_right_iff] using H r le_rfl },
assume k hk,
induction k with n IH,
{ simp only [if_true, matrix.mul_one, list.take_zero, zero_le', list.prod_nil] },
{ have hnr : n < r := hk,
let n' : fin r := ⟨n, hnr⟩,
have A : (list_transvec_row M).nth n = ↑(transvection (inr unit.star) (inl n')
(-M (inr unit.star) (inl n') / M (inr unit.star) (inr unit.star))),
{ simp only [list_transvec_row, list.of_fn_nth_val, hnr, dif_pos, list.nth_of_fn], refl },
simp only [list.take_succ, A, ← matrix.mul_assoc, list.prod_append, matrix.mul_one,
matrix.mul_eq_mul, list.prod_cons, list.prod_nil, option.to_list_some],
by_cases h : n' = i,
{ have hni : n = i,
{ cases i, simp only [subtype.mk_eq_mk] at h, simp only [h, coe_mk] },
have : ¬ (n.succ ≤ i), by simp only [← hni, n.lt_succ_self, not_le],
simp only [h, mul_transvection_apply_same, list.take, if_false,
mul_list_transvec_row_last_col_take _ _ hnr.le, hni.le, this, if_true, IH hnr.le],
field_simp [hM] },
{ have hni : n ≠ i,
{ rintros rfl, cases i, simpa using h },
simp only [IH hnr.le, ne.def, mul_transvection_apply_of_ne, not_false_iff, ne.symm h],
rcases le_or_lt (n+1) i with hi|hi,
{ simp [hi, n.le_succ.trans hi, if_true], },
{ rw [if_neg, if_neg],
{ simpa only [not_le] using hi },
{ simpa only [hni.symm, not_le, or_false] using nat.lt_succ_iff_lt_or_eq.1 hi } } } }
end
/-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` kills
all the coefficients in the last row but the last one. -/
lemma list_transvec_col_mul_mul_list_transvec_row_last_col
(hM : M (inr star) (inr star) ≠ 0) (i : fin r) :
((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) (inr star) (inl i) = 0 :=
begin
have : list_transvec_row M = list_transvec_row ((list_transvec_col M).prod ⬝ M),
by simp [list_transvec_row, list_transvec_col_mul_last_row],
rw this,
apply mul_list_transvec_row_last_row,
simpa [list_transvec_col_mul_last_row] using hM
end
/-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` kills
all the coefficients in the last column but the last one. -/
lemma list_transvec_col_mul_mul_list_transvec_row_last_row
(hM : M (inr star) (inr star) ≠ 0) (i : fin r) :
((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) (inl i) (inr star) = 0 :=
begin
have : list_transvec_col M = list_transvec_col (M ⬝ (list_transvec_row M).prod),
by simp [list_transvec_col, mul_list_transvec_row_last_col],
rw [this, matrix.mul_assoc],
apply list_transvec_col_mul_last_col,
simpa [mul_list_transvec_row_last_col] using hM
end
/-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` turns
the matrix in block-diagonal form. -/
lemma is_two_block_diagonal_list_transvec_col_mul_mul_list_transvec_row
(hM : M (inr star) (inr star) ≠ 0) :
is_two_block_diagonal ((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) :=
begin
split,
{ ext i j,
have : j = star, by simp only [eq_iff_true_of_subsingleton],
simp [to_blocks₁₂, this, list_transvec_col_mul_mul_list_transvec_row_last_row M hM] },
{ ext i j,
have : i = star, by simp only [eq_iff_true_of_subsingleton],
simp [to_blocks₂₁, this, list_transvec_col_mul_mul_list_transvec_row_last_col M hM] },
end
/-- There exist two lists of `transvection_struct` such that multiplying by them on the left and
on the right makes a matrix block-diagonal, when the last coefficient is nonzero. -/
lemma exists_is_two_block_diagonal_of_ne_zero (hM : M (inr star) (inr star) ≠ 0) :
∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)),
is_two_block_diagonal ((L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod) :=
begin
let L : list (transvection_struct (fin r ⊕ unit) 𝕜) :=
list.of_fn (λ i : fin r, ⟨inl i, inr star, by simp,
-M (inl i) (inr star) / M (inr star) (inr star)⟩),
let L' : list (transvection_struct (fin r ⊕ unit) 𝕜) :=
list.of_fn (λ i : fin r, ⟨inr star, inl i, by simp,
-M (inr star) (inl i) / M (inr star) (inr star)⟩),
refine ⟨L, L', _⟩,
have A : L.map to_matrix = list_transvec_col M, by simp [L, list_transvec_col, (∘)],
have B : L'.map to_matrix = list_transvec_row M, by simp [L, list_transvec_row, (∘)],
rw [A, B],
exact is_two_block_diagonal_list_transvec_col_mul_mul_list_transvec_row M hM
end
/-- There exist two lists of `transvection_struct` such that multiplying by them on the left and
on the right makes a matrix block-diagonal. -/
lemma exists_is_two_block_diagonal_list_transvec_mul_mul_list_transvec
(M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) :
∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)),
is_two_block_diagonal ((L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod) :=
begin
by_cases H : is_two_block_diagonal M, { refine ⟨list.nil, list.nil, by simpa using H⟩ },
-- we have already proved this when the last coefficient is nonzero
by_cases hM : M (inr star) (inr star) ≠ 0,
{ exact exists_is_two_block_diagonal_of_ne_zero M hM },
-- when the last coefficient is zero but there is a nonzero coefficient on the last row or the
-- last column, we will first put this nonzero coefficient in last position, and then argue as
-- above.
push_neg at hM,
simp [not_and_distrib, is_two_block_diagonal, to_blocks₁₂, to_blocks₂₁] at H,
have : ∃ (i : fin r), M (inl i) (inr star) ≠ 0 ∨ M (inr star) (inl i) ≠ 0,
{ cases H,
{ contrapose! H,
ext i j,
convert (H i).1,
simp only [eq_iff_true_of_subsingleton] },
{ contrapose! H,
ext i j,
convert (H j).2,
simp only [eq_iff_true_of_subsingleton] } },
rcases this with ⟨i, h|h⟩,
{ let M' := transvection (inr unit.star) (inl i) 1 ⬝ M,
have hM' : M' (inr star) (inr star) ≠ 0, by simpa [M', hM],
rcases exists_is_two_block_diagonal_of_ne_zero M' hM' with ⟨L, L', hLL'⟩,
rw matrix.mul_assoc at hLL',
refine ⟨L ++ [⟨inr star, inl i, by simp, 1⟩], L', _⟩,
simp only [list.map_append, list.prod_append, matrix.mul_one, to_matrix_mk, list.prod_cons,
list.prod_nil, mul_eq_mul, list.map, matrix.mul_assoc (L.map to_matrix).prod],
exact hLL' },
{ let M' := M ⬝ transvection (inl i) (inr star) 1,
have hM' : M' (inr star) (inr star) ≠ 0, by simpa [M', hM],
rcases exists_is_two_block_diagonal_of_ne_zero M' hM' with ⟨L, L', hLL'⟩,
refine ⟨L, ⟨inl i, inr star, by simp, 1⟩ :: L', _⟩,
simp only [←matrix.mul_assoc, to_matrix_mk, list.prod_cons, mul_eq_mul, list.map],
rw [matrix.mul_assoc (L.map to_matrix).prod],
exact hLL' }
end
/-- Inductive step for the reduction: if one knows that any size `r` matrix can be reduced to
diagonal form by elementary operations, then one deduces it for matrices over `fin r ⊕ unit`. -/
lemma exists_list_transvec_mul_mul_list_transvec_eq_diagonal_induction
(IH : ∀ (M : matrix (fin r) (fin r) 𝕜),
∃ (L₀ L₀' : list (transvection_struct (fin r) 𝕜)) (D₀ : (fin r) → 𝕜),
(L₀.map to_matrix).prod ⬝ M ⬝ (L₀'.map to_matrix).prod = diagonal D₀)
(M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) :
∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)) (D : fin r ⊕ unit → 𝕜),
(L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D :=
begin
rcases exists_is_two_block_diagonal_list_transvec_mul_mul_list_transvec M with ⟨L₁, L₁', hM⟩,
let M' := (L₁.map to_matrix).prod ⬝ M ⬝ (L₁'.map to_matrix).prod,
let M'' := to_blocks₁₁ M',
rcases IH M'' with ⟨L₀, L₀', D₀, h₀⟩,
set c := M' (inr star) (inr star) with hc,
refine ⟨L₀.map (sum_inl unit) ++ L₁, L₁' ++ L₀'.map (sum_inl unit),
sum.elim D₀ (λ _, M' (inr star) (inr star)), _⟩,
suffices :
(L₀.map (to_matrix ∘ sum_inl unit)).prod ⬝ M' ⬝ (L₀'.map (to_matrix ∘ sum_inl unit)).prod =
diagonal (sum.elim D₀ (λ _, c)), by simpa [M', matrix.mul_assoc, c],
have : M' = from_blocks M'' 0 0 (diagonal (λ _, c)),
{ rw ← from_blocks_to_blocks M',
congr,
{ exact hM.1 },
{ exact hM.2 },
{ ext i j, rw [hc, to_blocks₂₂], congr } },
rw this,
simp [h₀],
end
variables {n p} [fintype n] [fintype p]
/-- Reduction to diagonal form by elementary operations is invariant under reindexing. -/
lemma reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal (M : matrix p p 𝕜) (e : p ≃ n)
(H : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜),
(L.map to_matrix).prod ⬝ (matrix.reindex_alg_equiv 𝕜 e M) ⬝ (L'.map to_matrix).prod
= diagonal D) :
∃ (L L' : list (transvection_struct p 𝕜)) (D : p → 𝕜),
(L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D :=
begin
rcases H with ⟨L₀, L₀', D₀, h₀⟩,
refine ⟨L₀.map (reindex_equiv e.symm), L₀'.map (reindex_equiv e.symm), D₀ ∘ e, _⟩,
have : M = reindex_alg_equiv 𝕜 e.symm (reindex_alg_equiv 𝕜 e M),
by simp only [equiv.symm_symm, minor_minor, reindex_apply, minor_id_id, equiv.symm_comp_self,
reindex_alg_equiv_apply],
rw this,
simp only [to_matrix_reindex_equiv_prod, list.map_map, reindex_alg_equiv_apply],
simp only [← reindex_alg_equiv_apply, ← reindex_alg_equiv_mul, h₀],
simp only [equiv.symm_symm, reindex_apply, minor_diagonal_equiv, reindex_alg_equiv_apply],
end
/-- Any matrix can be reduced to diagonal form by elementary operations. Formulated here on `Type 0`
because we will make an induction using `fin r`.
See `exists_list_transvec_mul_mul_list_transvec_eq_diagonal` for the general version (which follows
from this one and reindexing). -/
lemma exists_list_transvec_mul_mul_list_transvec_eq_diagonal_aux
(n : Type) [fintype n] [decidable_eq n]
(M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜),
(L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D :=
begin
unfreezingI { induction hn : fintype.card n with r IH generalizing n M },
{ refine ⟨list.nil, list.nil, λ _, 1, _⟩,
ext i j,
rw fintype.card_eq_zero_iff at hn,
exact hn.elim' i },
{ have e : n ≃ fin r ⊕ unit,
{ refine fintype.equiv_of_card_eq _,
rw hn,
convert (@fintype.card_sum (fin r) unit _ _).symm,
simp },
apply reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal M e,
apply exists_list_transvec_mul_mul_list_transvec_eq_diagonal_induction
(λ N, IH (fin r) N (by simp)) }
end
/-- Any matrix can be reduced to diagonal form by elementary operations. -/
theorem exists_list_transvec_mul_mul_list_transvec_eq_diagonal
(M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜),
(L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D :=
begin
have e : n ≃ fin (fintype.card n) := fintype.equiv_of_card_eq (by simp),
apply reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal M e,
apply exists_list_transvec_mul_mul_list_transvec_eq_diagonal_aux
end
/-- Any matrix can be written as the product of transvections, a diagonal matrix, and
transvections.-/
theorem exists_list_transvec_mul_diagonal_mul_list_transvec
(M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜),
M = (L.map to_matrix).prod ⬝ diagonal D ⬝ (L'.map to_matrix).prod :=
begin
rcases exists_list_transvec_mul_mul_list_transvec_eq_diagonal M with ⟨L, L', D, h⟩,
refine ⟨L.reverse.map transvection_struct.inv, L'.reverse.map transvection_struct.inv, D, _⟩,
suffices : M =
((L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝ (L.map to_matrix).prod) ⬝ M ⬝
((L'.map to_matrix).prod ⬝ (L'.reverse.map (to_matrix ∘ transvection_struct.inv)).prod),
by simpa [← h, matrix.mul_assoc],
rw [reverse_inv_prod_mul_prod, prod_mul_reverse_inv_prod, matrix.one_mul, matrix.mul_one],
end
end pivot
open pivot transvection_struct
variables {n} [fintype n]
/-- Induction principle for matrices based on transvections: if a property is true for all diagonal
matrices, all transvections, and is stable under product, then it is true for all matrices. This is
the useful way to say that matrices are generated by diagonal matrices and transvections.
We state a slightly more general version: to prove a property for a matrix `M`, it suffices to
assume that the diagonal matrices we consider have the same determinant as `M`. This is useful to
obtain similar principles for `SLₙ` or `GLₙ`. -/
lemma diagonal_transvection_induction (P : matrix n n 𝕜 → Prop) (M : matrix n n 𝕜)
(hdiag : ∀ D : n → 𝕜, det (diagonal D) = det M → P (diagonal D))
(htransvec : ∀ (t : transvection_struct n 𝕜), P t.to_matrix)
(hmul : ∀ A B, P A → P B → P (A ⬝ B)) :
P M :=
begin
rcases exists_list_transvec_mul_diagonal_mul_list_transvec M with ⟨L, L', D, h⟩,
have PD : P (diagonal D) := hdiag D (by simp [h]),
suffices H : ∀ (L₁ L₂ : list (transvection_struct n 𝕜)) (E : matrix n n 𝕜),
P E → P ((L₁.map to_matrix).prod ⬝ E ⬝ (L₂.map to_matrix).prod),
by { rw h, apply H L L', exact PD },
assume L₁ L₂ E PE,
induction L₁ with t L₁ IH,
{ simp only [matrix.one_mul, list.prod_nil, list.map],
induction L₂ with t L₂ IH generalizing E,
{ simpa },
{ simp only [←matrix.mul_assoc, list.prod_cons, mul_eq_mul, list.map],
apply IH,
exact hmul _ _ PE (htransvec _) } },
{ simp only [matrix.mul_assoc, list.prod_cons, mul_eq_mul, list.map] at ⊢ IH,
exact hmul _ _ (htransvec _) IH }
end
/-- Induction principle for invertible matrices based on transvections: if a property is true for
all invertible diagonal matrices, all transvections, and is stable under product of invertible
matrices, then it is true for all invertible matrices. This is the useful way to say that
invertible matrices are generated by invertible diagonal matrices and transvections. -/
lemma diagonal_transvection_induction_of_det_ne_zero (P : matrix n n 𝕜 → Prop)
(M : matrix n n 𝕜) (hMdet : det M ≠ 0)
(hdiag : ∀ D : n → 𝕜, det (diagonal D) ≠ 0 → P (diagonal D))
(htransvec : ∀ (t : transvection_struct n 𝕜), P t.to_matrix)
(hmul : ∀ A B, det A ≠ 0 → det B ≠ 0 → P A → P B → P (A ⬝ B)) :
P M :=
begin
let Q : matrix n n 𝕜 → Prop := λ N, det N ≠ 0 ∧ P N,
have : Q M,
{ apply diagonal_transvection_induction Q M,
{ assume D hD,
have detD : det (diagonal D) ≠ 0, by { rw hD, exact hMdet },
exact ⟨detD, hdiag _ detD⟩ },
{ assume t,
exact ⟨by simp, htransvec t⟩ },
{ assume A B QA QB,
exact ⟨by simp [QA.1, QB.1], hmul A B QA.1 QB.1 QA.2 QB.2⟩ } },
exact this.2
end
end matrix
|
2412aef2fbe97368db4f2b84c00e160eea0c4f4f | d0c6b2ba2af981e9ab0a98f6e169262caad4b9b9 | /stage0/src/Init/NotationExtra.lean | 41a0be4517ef54cb88e81793a237a3263541f91c | [
"Apache-2.0"
] | permissive | fizruk/lean4 | 953b7dcd76e78c17a0743a2c1a918394ab64bbc0 | 545ed50f83c570f772ade4edbe7d38a078cbd761 | refs/heads/master | 1,677,655,987,815 | 1,612,393,885,000 | 1,612,393,885,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,788 | 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
Extra notation that depends on Init/Meta
-/
prelude
import Init.Meta
import Init.Data.Array.Subarray
namespace Lean
-- Auxiliary parsers and functions for declaring notation with binders
syntax binderIdent := ident <|> "_"
syntax unbracktedExplicitBinders := binderIdent+ (" : " term)?
syntax bracketedExplicitBinders := "(" binderIdent+ " : " term ")"
syntax explicitBinders := bracketedExplicitBinders+ <|> unbracktedExplicitBinders
def expandExplicitBindersAux (combinator : Syntax) (idents : Array Syntax) (type? : Option Syntax) (body : Syntax) : MacroM Syntax :=
let rec loop (i : Nat) (acc : Syntax) := do
match i with
| 0 => pure acc
| i+1 =>
let ident := idents[i][0]
let acc ← match ident.isIdent, type? with
| true, none => `($combinator fun $ident => $acc)
| true, some type => `($combinator fun $ident:ident : $type => $acc)
| false, none => `($combinator fun _ => $acc)
| false, some type => `($combinator fun _ : $type => $acc)
loop i acc
loop idents.size body
def expandBrackedBindersAux (combinator : Syntax) (binders : Array Syntax) (body : Syntax) : MacroM Syntax :=
let rec loop (i : Nat) (acc : Syntax) := do
match i with
| 0 => pure acc
| i+1 =>
let idents := binders[i][1].getArgs
let type := binders[i][3]
loop i (← expandExplicitBindersAux combinator idents (some type) acc)
loop binders.size body
def expandExplicitBinders (combinatorDeclName : Name) (explicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do
let combinator := mkIdentFrom (← getRef) combinatorDeclName
let explicitBinders := explicitBinders[0]
if explicitBinders.getKind == `Lean.unbracktedExplicitBinders then
let idents := explicitBinders[0].getArgs
let type? := if explicitBinders[1].isNone then none else some explicitBinders[1][1]
expandExplicitBindersAux combinator idents type? body
else if explicitBinders.getArgs.all (·.getKind == `Lean.bracketedExplicitBinders) then
expandBrackedBindersAux combinator explicitBinders.getArgs body
else
Macro.throwError "unexpected explicit binder"
def expandBrackedBinders (combinatorDeclName : Name) (bracketedExplicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do
let combinator := mkIdentFrom (← getRef) combinatorDeclName
expandBrackedBindersAux combinator #[bracketedExplicitBinders] body
syntax unifConstraint := term (" =?= " <|> " ≟ ") term
syntax unifConstraintElem := colGe unifConstraint ", "?
syntax attrKind "unif_hint " (ident)? bracketedBinder* " where " withPosition(unifConstraintElem*) ("|-" <|> "⊢ ") unifConstraint : command
private def mkHintBody (cs : Array Syntax) (p : Syntax) : MacroM Syntax := do
let mut body ← `($(p[0]) = $(p[2]))
for c in cs.reverse do
body ← `($(c[0][0]) = $(c[0][2]) → $body)
return body
macro_rules
| `($kind:attrKind unif_hint $bs:explicitBinder* where $cs* |- $p) => do
let body ← mkHintBody cs p
`(@[$kind:attrKind unificationHint] def hint $bs:explicitBinder* : Sort _ := $body)
| `($kind:attrKind unif_hint $n:ident $bs* where $cs* |- $p) => do
let body ← mkHintBody cs p
`(@[$kind:attrKind unificationHint] def $n:ident $bs:explicitBinder* : Sort _ := $body)
end Lean
open Lean
macro "∃" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b
macro "exists" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b
macro "Σ" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Sigma xs b
macro "Σ'" xs:explicitBinders ", " b:term : term => expandExplicitBinders `PSigma xs b
macro:35 xs:bracketedExplicitBinders "×" b:term:35 : term => expandBrackedBinders `Sigma xs b
macro:35 xs:bracketedExplicitBinders "×'" b:term:35 : term => expandBrackedBinders `PSigma xs b
syntax "funext " (colGt term:max)+ : tactic
macro_rules
| `(tactic|funext $xs*) =>
if xs.size == 1 then
`(tactic| apply funext; intro $(xs[0]):term)
else
`(tactic| apply funext; intro $(xs[0]):term; funext $(xs[1:])*)
macro_rules
| `(%[ $[$x],* | $k ]) =>
if x.size < 8 then
x.foldrM (init := k) fun x k =>
`(List.cons $x $k)
else
let m := x.size / 2
let y := x[m:]
let z := x[:m]
`(let y := %[ $[$y],* | $k ]
%[ $[$z],* | y ])
/-
Expands
```
class abbrev C <params> := D_1, ..., D_n
```
into
```
class C <params> extends D_1, ..., D_n
instance <params> [D_1] ... [D_n] : C <params> :=
C.mk _ ... _ inferInstance ... inferInstance
```
-/
syntax "class " "abbrev " ident bracketedBinder* ":=" withPosition(group(colGe term ","?)*) : command
macro_rules
| `(class abbrev $name:ident $params* := $[ $parents:term $[,]? ]*) => do
let mut auxBinders := #[]
let mut typeArgs := #[]
let mut ctorArgs := #[]
let hole ← `(_)
for param in params do
match param with
| `(bracketedBinder| ( $ids:ident* $[: $type:term]? ) ) =>
auxBinders := auxBinders.push (← `(bracketedBinder| { $ids:ident* }) )
typeArgs := typeArgs ++ ids
ctorArgs := ctorArgs ++ ids.map fun _ => hole
| `(bracketedBinder| [ $id:ident : $type:term ] ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs.push hole
ctorArgs := ctorArgs.push hole
| `(bracketedBinder| [ $type:term ] ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs.push hole
ctorArgs := ctorArgs.push hole
| `(bracketedBinder| { $ids:ident* $[: $type ]? } ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs ++ ids.map fun _ => hole
ctorArgs := typeArgs ++ ids.map fun _ => hole
| _ => Lean.Macro.throwErrorAt param "unexpected binder at `class abbrev` macro"
let inferInst ← `(inferInstance)
for parent in parents do
auxBinders := auxBinders.push (← `(bracketedBinder| [ $parent:term ]))
ctorArgs := ctorArgs.push inferInst
let view := Lean.extractMacroScopes name.getId
let ctor := mkIdentFrom name { view with name := view.name ++ `mk }.review
`(class $name:ident $params* extends $[$parents:term],*
instance $auxBinders:explicitBinder* : @ $name:ident $typeArgs* :=
@ $ctor:ident $ctorArgs*)
/-
Similar to `first`, but succeeds only if one the given tactics solves the curretn goal.
-/
syntax "solve " "|"? sepBy1(tacticSeq, "|") : tactic
macro_rules
| `(tactic| solve $[|]? $ts:tacticSeq|*) => `(tactic| focus first $[($ts); done]|*)
|
9798e5206d17f7f33d0c92a0c6fed47ed24da47c | 78630e908e9624a892e24ebdd21260720d29cf55 | /src/logic_first_order/fol_14.lean | ca3595a9f121699dc924b374858a619f321c9a77 | [
"CC0-1.0"
] | permissive | tomasz-lisowski/lean-logic-examples | 84e612466776be0a16c23a0439ff8ef6114ddbe1 | 2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d | refs/heads/master | 1,683,334,199,431 | 1,621,938,305,000 | 1,621,938,305,000 | 365,041,573 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 187 | lean | namespace fol_14
variable A : Type
variable P : A → Prop
variables c d : A
theorem fol_14 (h : ∀ x, P c → P x) : P c → P d :=
assume h1: P c,
show P d, from (h d) h1
end fol_14 |
788f14222840b8c51206ae2a0981fcd3e842e78e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/order/hom/monoid.lean | 10859efb337d821fe15286a168b7ea9fe8c59ca4 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 21,539 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import data.pi.algebra
import algebra.hom.group
import algebra.order.group.instances
import algebra.order.monoid.with_zero.defs
import order.hom.basic
/-!
# Ordered monoid and group homomorphisms
This file defines morphisms between (additive) ordered monoids.
## Types of morphisms
* `order_add_monoid_hom`: Ordered additive monoid homomorphisms.
* `order_monoid_hom`: Ordered monoid homomorphisms.
* `order_monoid_with_zero_hom`: Ordered monoid with zero homomorphisms.
## Typeclasses
* `order_add_monoid_hom_class`
* `order_monoid_hom_class`
* `order_monoid_with_zero_hom_class`
## Notation
* `→+o`: Bundled ordered additive monoid homs. Also use for additive groups homs.
* `→*o`: Bundled ordered monoid homs. Also use for groups homs.
* `→*₀o`: Bundled ordered monoid with zero homs. Also use for groups 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 `order_group_hom` -- the idea is that `order_monoid_hom` is used.
The constructor for `order_monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate
constructor `order_monoid_hom.mk'` will construct ordered group homs (i.e. ordered monoid homs
between ordered 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 `order_monoid_hom`. When
they can be inferred from the type it is faster to use this method than to use type class inference.
## Tags
ordered monoid, ordered group, monoid with zero
-/
open function
variables {F α β γ δ : Type*}
section add_monoid
/-- `α →+o β` is the type of monotone functions `α → β` that preserve the `ordered_add_comm_monoid`
structure.
`order_add_monoid_hom` is also used for ordered group homomorphisms.
When possible, instead of parametrizing results over `(f : α →+o β)`,
you should parametrize over `(F : Type*) [order_add_monoid_hom_class F α β] (f : F)`.
When you extend this structure, make sure to extend `order_add_monoid_hom_class`. -/
structure order_add_monoid_hom (α β : Type*) [preorder α] [preorder β] [add_zero_class α]
[add_zero_class β] extends α →+ β :=
(monotone' : monotone to_fun)
infixr ` →+o `:25 := order_add_monoid_hom
section
set_option old_structure_cmd true
/-- `order_add_monoid_hom_class F α β` states that `F` is a type of ordered monoid homomorphisms.
You should also extend this typeclass when you extend `order_add_monoid_hom`. -/
@[ancestor add_monoid_hom_class]
class order_add_monoid_hom_class (F : Type*) (α β : out_param $ Type*) [preorder α] [preorder β]
[add_zero_class α] [add_zero_class β]
extends add_monoid_hom_class F α β :=
(monotone (f : F) : monotone f)
end
-- Instances and lemmas are defined below through `@[to_additive]`.
end add_monoid
section monoid
variables [preorder α] [preorder β] [mul_one_class α] [mul_one_class β]
/-- `α →*o β` is the type of functions `α → β` that preserve the `ordered_comm_monoid` structure.
`order_monoid_hom` is also used for ordered group homomorphisms.
When possible, instead of parametrizing results over `(f : α →*o β)`,
you should parametrize over `(F : Type*) [order_monoid_hom_class F α β] (f : F)`.
When you extend this structure, make sure to extend `order_monoid_hom_class`. -/
@[to_additive]
structure order_monoid_hom (α β : Type*) [preorder α] [preorder β] [mul_one_class α]
[mul_one_class β]
extends α →* β :=
(monotone' : monotone to_fun)
infixr ` →*o `:25 := order_monoid_hom
section
set_option old_structure_cmd true
/-- `order_monoid_hom_class F α β` states that `F` is a type of ordered monoid homomorphisms.
You should also extend this typeclass when you extend `order_monoid_hom`. -/
@[ancestor monoid_hom_class, to_additive]
class order_monoid_hom_class (F : Type*) (α β : out_param $ Type*)
[preorder α] [preorder β] [mul_one_class α] [mul_one_class β]
extends monoid_hom_class F α β :=
(monotone (f : F) : monotone f)
end
@[priority 100, to_additive] -- See note [lower instance priority]
instance order_monoid_hom_class.to_order_hom_class [order_monoid_hom_class F α β] :
order_hom_class F α β :=
{ map_rel := order_monoid_hom_class.monotone,
.. ‹order_monoid_hom_class F α β› }
@[to_additive]
instance [order_monoid_hom_class F α β] : has_coe_t F (α →*o β) :=
⟨λ f, { to_fun := f, map_one' := map_one f, map_mul' := map_mul f,
monotone' := order_monoid_hom_class.monotone _ }⟩
end monoid
section monoid_with_zero
variables [preorder α] [preorder β] [mul_zero_one_class α] [mul_zero_one_class β]
/-- `order_monoid_with_zero_hom α β` is the type of functions `α → β` that preserve
the `monoid_with_zero` structure.
`order_monoid_with_zero_hom` is also used for group homomorphisms.
When possible, instead of parametrizing results over `(f : α →+ β)`,
you should parametrize over `(F : Type*) [order_monoid_with_zero_hom_class F α β] (f : F)`.
When you extend this structure, make sure to extend `order_monoid_with_zero_hom_class`. -/
structure order_monoid_with_zero_hom (α β : Type*) [preorder α] [preorder β]
[mul_zero_one_class α] [mul_zero_one_class β]
extends α →*₀ β :=
(monotone' : monotone to_fun)
infixr ` →*₀o `:25 := order_monoid_with_zero_hom
section
set_option old_structure_cmd true
/-- `order_monoid_with_zero_hom_class F α β` states that `F` is a type of
ordered monoid with zero homomorphisms.
You should also extend this typeclass when you extend `order_monoid_with_zero_hom`. -/
class order_monoid_with_zero_hom_class (F : Type*) (α β : out_param $ Type*)
[preorder α] [preorder β] [mul_zero_one_class α] [mul_zero_one_class β]
extends monoid_with_zero_hom_class F α β :=
(monotone (f : F) : monotone f)
end
@[priority 100] -- See note [lower instance priority]
instance order_monoid_with_zero_hom_class.to_order_monoid_hom_class
[order_monoid_with_zero_hom_class F α β] : order_monoid_hom_class F α β :=
{ .. ‹order_monoid_with_zero_hom_class F α β› }
instance [order_monoid_with_zero_hom_class F α β] : has_coe_t F (α →*₀o β) :=
⟨λ f, { to_fun := f, map_one' := map_one f, map_zero' := map_zero f, map_mul' := map_mul f,
monotone' := order_monoid_with_zero_hom_class.monotone _ }⟩
end monoid_with_zero
section ordered_add_comm_monoid
variables [ordered_add_comm_monoid α] [ordered_add_comm_monoid β] [order_add_monoid_hom_class F α β]
(f : F) {a : α}
include β
lemma map_nonneg (ha : 0 ≤ a) : 0 ≤ f a := by { rw ←map_zero f, exact order_hom_class.mono _ ha }
lemma map_nonpos (ha : a ≤ 0) : f a ≤ 0 := by { rw ←map_zero f, exact order_hom_class.mono _ ha }
end ordered_add_comm_monoid
section ordered_add_comm_group
variables [ordered_add_comm_group α]
[ordered_add_comm_monoid β] [add_monoid_hom_class F α β] (f : F)
lemma monotone_iff_map_nonneg : monotone (f : α → β) ↔ ∀ a, 0 ≤ a → 0 ≤ f a :=
⟨λ h a, by { rw ←map_zero f, apply h }, λ h a b hl,
by { rw [←sub_add_cancel b a, map_add f], exact le_add_of_nonneg_left (h _ $ sub_nonneg.2 hl) }⟩
lemma antitone_iff_map_nonpos : antitone (f : α → β) ↔ ∀ a, 0 ≤ a → f a ≤ 0 :=
monotone_to_dual_comp_iff.symm.trans $ monotone_iff_map_nonneg _
lemma monotone_iff_map_nonpos : monotone (f : α → β) ↔ ∀ a ≤ 0, f a ≤ 0 :=
antitone_comp_of_dual_iff.symm.trans $ antitone_iff_map_nonpos _
lemma antitone_iff_map_nonneg : antitone (f : α → β) ↔ ∀ a ≤ 0, 0 ≤ f a :=
monotone_comp_of_dual_iff.symm.trans $ monotone_iff_map_nonneg _
variable [covariant_class β β (+) (<)]
lemma strict_mono_iff_map_pos : strict_mono (f : α → β) ↔ ∀ a, 0 < a → 0 < f a :=
⟨λ h a, by { rw ←map_zero f, apply h }, λ h a b hl,
by { rw [←sub_add_cancel b a, map_add f], exact lt_add_of_pos_left _ (h _ $ sub_pos.2 hl) }⟩
lemma strict_anti_iff_map_neg : strict_anti (f : α → β) ↔ ∀ a, 0 < a → f a < 0 :=
strict_mono_to_dual_comp_iff.symm.trans $ strict_mono_iff_map_pos _
lemma strict_mono_iff_map_neg : strict_mono (f : α → β) ↔ ∀ a < 0, f a < 0 :=
strict_anti_comp_of_dual_iff.symm.trans $ strict_anti_iff_map_neg _
lemma strict_anti_iff_map_pos : strict_anti (f : α → β) ↔ ∀ a < 0, 0 < f a :=
strict_mono_comp_of_dual_iff.symm.trans $ strict_mono_iff_map_pos _
end ordered_add_comm_group
namespace order_monoid_hom
section preorder
variables [preorder α] [preorder β] [preorder γ] [preorder δ] [mul_one_class α]
[mul_one_class β] [mul_one_class γ] [mul_one_class δ] {f g : α →*o β}
@[to_additive]
instance : order_monoid_hom_class (α →*o β) α β :=
{ coe := λ f, f.to_fun,
coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' },
map_mul := λ f, f.map_mul',
map_one := λ f, f.map_one',
monotone := λ f, f.monotone' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
@[to_additive "Helper instance for when there's too many metavariables to apply
`fun_like.has_coe_to_fun` directly."]
instance : has_coe_to_fun (α →*o β) (λ _, α → β) := fun_like.has_coe_to_fun
-- Other lemmas should be accessed through the `fun_like` API
@[ext, to_additive] lemma ext (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h
@[to_additive] lemma to_fun_eq_coe (f : α →*o β) : f.to_fun = (f : α → β) := rfl
@[simp, to_additive] lemma coe_mk (f : α →* β) (h) : (order_monoid_hom.mk f h : α → β) = f := rfl
@[simp, to_additive] lemma mk_coe (f : α →*o β) (h) : order_monoid_hom.mk (f : α →* β) h = f :=
by { ext, refl }
/-- Reinterpret an ordered monoid homomorphism as an order homomorphism. -/
@[to_additive "Reinterpret an ordered additive monoid homomorphism as an order homomorphism."]
def to_order_hom (f : α →*o β) : α →o β := { ..f }
@[simp, to_additive] lemma coe_monoid_hom (f : α →*o β) : ((f : α →* β) : α → β) = f := rfl
@[simp, to_additive] lemma coe_order_hom (f : α →*o β) : ((f : α →o β) : α → β) = f := rfl
@[to_additive] lemma to_monoid_hom_injective : injective (to_monoid_hom : _ → α →* β) :=
λ f g h, ext $ by convert fun_like.ext_iff.1 h
@[to_additive] lemma to_order_hom_injective : injective (to_order_hom : _ → α →o β) :=
λ f g h, ext $ by convert fun_like.ext_iff.1 h
/-- Copy of an `order_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix
definitional equalities. -/
@[to_additive "Copy of an `order_monoid_hom` with a new `to_fun` equal to the old one. Useful to fix
definitional equalities."]
protected def copy (f : α →*o β) (f' : α → β) (h : f' = f) : α →*o β :=
{ to_fun := f',
monotone' := h.symm.subst f.monotone',
..f.to_monoid_hom.copy f' h }
@[simp, to_additive] lemma coe_copy (f : α →*o β) (f' : α → β) (h : f' = f) :
⇑(f.copy f' h) = f' :=
rfl
@[to_additive] lemma copy_eq (f : α →*o β) (f' : α → β) (h : f' = f) :
f.copy f' h = f :=
fun_like.ext' h
variables (α)
/-- The identity map as an ordered monoid homomorphism. -/
@[to_additive "The identity map as an ordered additive monoid homomorphism."]
protected def id : α →*o α := { ..monoid_hom.id α, ..order_hom.id }
@[simp, to_additive] lemma coe_id : ⇑(order_monoid_hom.id α) = id := rfl
@[to_additive] instance : inhabited (α →*o α) := ⟨order_monoid_hom.id α⟩
variables {α}
/-- Composition of `order_monoid_hom`s as an `order_monoid_hom`. -/
@[to_additive "Composition of `order_add_monoid_hom`s as an `order_add_monoid_hom`"]
def comp (f : β →*o γ) (g : α →*o β) : α →*o γ :=
{ ..f.to_monoid_hom.comp (g : α →* β), ..f.to_order_hom.comp (g : α →o β) }
@[simp, to_additive] lemma coe_comp (f : β →*o γ) (g : α →*o β) : (f.comp g : α → γ) = f ∘ g := rfl
@[simp, to_additive] lemma comp_apply (f : β →*o γ) (g : α →*o β) (a : α) :
(f.comp g) a = f (g a) := rfl
@[simp, to_additive] lemma coe_comp_monoid_hom (f : β →*o γ) (g : α →*o β) :
(f.comp g : α →* γ) = (f : β →* γ).comp g := rfl
@[simp, to_additive] lemma coe_comp_order_hom (f : β →*o γ) (g : α →*o β) :
(f.comp g : α →o γ) = (f : β →o γ).comp g := rfl
@[simp, to_additive] lemma comp_assoc (f : γ →*o δ) (g : β →*o γ) (h : α →*o β) :
(f.comp g).comp h = f.comp (g.comp h) := rfl
@[simp, to_additive] lemma comp_id (f : α →*o β) : f.comp (order_monoid_hom.id α) = f :=
ext $ λ a, rfl
@[simp, to_additive] lemma id_comp (f : α →*o β) : (order_monoid_hom.id β).comp f = f :=
ext $ λ a, rfl
@[to_additive]
lemma cancel_right {g₁ g₂ : β →*o γ} {f : α →*o β} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩
@[to_additive]
lemma cancel_left {g : β →*o γ} {f₁ f₂ : α →*o β} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩
/-- `1` is the homomorphism sending all elements to `1`. -/
@[to_additive "`1` is the homomorphism sending all elements to `1`."]
instance : has_one (α →*o β) := ⟨{ monotone' := monotone_const, ..(1 : α →* β) }⟩
@[simp, to_additive] lemma coe_one : ⇑(1 : α →*o β) = 1 := rfl
@[simp, to_additive] lemma one_apply (a : α) : (1 : α →*o β) a = 1 := rfl
@[simp, to_additive] lemma one_comp (f : α →*o β) : (1 : β →*o γ).comp f = 1 := rfl
@[simp, to_additive] lemma comp_one (f : β →*o γ) : f.comp (1 : α →*o β) = 1 :=
by { ext, exact map_one f }
end preorder
section mul
variables [ordered_comm_monoid α] [ordered_comm_monoid β] [ordered_comm_monoid γ]
/-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism
sending `a` to `f a * g a`. -/
@[to_additive "For two ordered additive monoid morphisms `f` and `g`, their product is the ordered
additive monoid morphism sending `a` to `f a + g a`."]
instance : has_mul (α →*o β) :=
⟨λ f g, { monotone' := f.monotone'.mul' g.monotone', ..(f * g : α →* β) }⟩
@[simp, to_additive] lemma coe_mul (f g : α →*o β) : ⇑(f * g) = f * g := rfl
@[simp, to_additive] lemma mul_apply (f g : α →*o β) (a : α) : (f * g) a = f a * g a := rfl
@[to_additive] lemma mul_comp (g₁ g₂ : β →*o γ) (f : α →*o β) :
(g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl
@[to_additive] lemma comp_mul (g : β →*o γ) (f₁ f₂ : α →*o β) :
g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := by { ext, exact map_mul g _ _ }
end mul
section ordered_comm_monoid
variables {hα : ordered_comm_monoid α} {hβ : ordered_comm_monoid β}
include hα hβ
@[simp, to_additive] lemma to_monoid_hom_eq_coe (f : α →*o β) : f.to_monoid_hom = f :=
by { ext, refl }
@[simp, to_additive] lemma to_order_hom_eq_coe (f : α →*o β) : f.to_order_hom = f := rfl
end ordered_comm_monoid
section ordered_comm_group
variables {hα : ordered_comm_group α} {hβ : ordered_comm_group β}
include hα hβ
/-- Makes an ordered group homomorphism from a proof that the map preserves multiplication. -/
@[to_additive "Makes an ordered additive group homomorphism from a proof that the map preserves
addition.",
simps {fully_applied := ff}]
def mk' (f : α → β) (hf : monotone f) (map_mul : ∀ a b : α, f (a * b) = f a * f b) : α →*o β :=
{ monotone' := hf,
..monoid_hom.mk' f map_mul }
end ordered_comm_group
end order_monoid_hom
namespace order_monoid_with_zero_hom
section preorder
variables [preorder α] [preorder β] [preorder γ] [preorder δ] [mul_zero_one_class α]
[mul_zero_one_class β] [mul_zero_one_class γ] [mul_zero_one_class δ] {f g : α →*₀o β}
instance : order_monoid_with_zero_hom_class (α →*₀o β) α β :=
{ coe := λ f, f.to_fun,
coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' },
map_mul := λ f, f.map_mul',
map_one := λ f, f.map_one',
map_zero := λ f, f.map_zero',
monotone := λ f, f.monotone' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (α →*₀o β) (λ _, α → β) := fun_like.has_coe_to_fun
-- Other lemmas should be accessed through the `fun_like` API
@[ext] lemma ext (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h
lemma to_fun_eq_coe (f : α →*₀o β) : f.to_fun = (f : α → β) := rfl
@[simp] lemma coe_mk (f : α →*₀ β) (h) : (order_monoid_with_zero_hom.mk f h : α → β) = f := rfl
@[simp] lemma mk_coe (f : α →*₀o β) (h) : order_monoid_with_zero_hom.mk (f : α →*₀ β) h = f :=
by { ext, refl }
/-- Reinterpret an ordered monoid with zero homomorphism as an order monoid homomorphism. -/
def to_order_monoid_hom (f : α →*₀o β) : α →*o β := { ..f }
@[simp] lemma coe_monoid_with_zero_hom (f : α →*₀o β) : ⇑(f : α →*₀ β) = f := rfl
@[simp] lemma coe_order_monoid_hom (f : α →*₀o β) : ⇑(f : α →*o β) = f := rfl
lemma to_order_monoid_hom_injective : injective (to_order_monoid_hom : _ → α →*o β) :=
λ f g h, ext $ by convert fun_like.ext_iff.1 h
lemma to_monoid_with_zero_hom_injective : injective (to_monoid_with_zero_hom : _ → α →*₀ β) :=
λ f g h, ext $ by convert fun_like.ext_iff.1 h
/-- Copy of an `order_monoid_with_zero_hom` with a new `to_fun` equal to the old one. Useful to fix
definitional equalities. -/
protected def copy (f : α →*₀o β) (f' : α → β) (h : f' = f) : α →*o β :=
{ to_fun := f',
.. f.to_order_monoid_hom.copy f' h, .. f.to_monoid_with_zero_hom.copy f' h }
@[simp] lemma coe_copy (f : α →*₀o β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl
lemma copy_eq (f : α →*₀o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h
variables (α)
/-- The identity map as an ordered monoid with zero homomorphism. -/
protected def id : α →*₀o α := { ..monoid_with_zero_hom.id α, ..order_hom.id }
@[simp] lemma coe_id : ⇑(order_monoid_with_zero_hom.id α) = id := rfl
instance : inhabited (α →*₀o α) := ⟨order_monoid_with_zero_hom.id α⟩
variables {α}
/-- Composition of `order_monoid_with_zero_hom`s as an `order_monoid_with_zero_hom`. -/
def comp (f : β →*₀o γ) (g : α →*₀o β) : α →*₀o γ :=
{ ..f.to_monoid_with_zero_hom.comp (g : α →*₀ β), ..f.to_order_monoid_hom.comp (g : α →*o β) }
@[simp] lemma coe_comp (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α → γ) = f ∘ g := rfl
@[simp] lemma comp_apply (f : β →*₀o γ) (g : α →*₀o β) (a : α) : (f.comp g) a = f (g a) := rfl
@[simp] lemma coe_comp_monoid_with_zero_hom (f : β →*₀o γ) (g : α →*₀o β) :
(f.comp g : α →*₀ γ) = (f : β →*₀ γ).comp g := rfl
@[simp] lemma coe_comp_order_monoid_hom (f : β →*₀o γ) (g : α →*₀o β) :
(f.comp g : α →*o γ) = (f : β →*o γ).comp g := rfl
@[simp] lemma comp_assoc (f : γ →*₀o δ) (g : β →*₀o γ) (h : α →*₀o β) :
(f.comp g).comp h = f.comp (g.comp h) := rfl
@[simp] lemma comp_id (f : α →*₀o β) : f.comp (order_monoid_with_zero_hom.id α) = f :=
ext $ λ a, rfl
@[simp] lemma id_comp (f : α →*₀o β) : (order_monoid_with_zero_hom.id β).comp f = f :=
ext $ λ a, rfl
lemma cancel_right {g₁ g₂ : β →*₀o γ} {f : α →*₀o β} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩
lemma cancel_left {g : β →*₀o γ} {f₁ f₂ : α →*₀o β} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩
end preorder
section mul
variables [linear_ordered_comm_monoid_with_zero α] [linear_ordered_comm_monoid_with_zero β]
[linear_ordered_comm_monoid_with_zero γ]
/-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism
sending `a` to `f a * g a`. -/
instance : has_mul (α →*₀o β) :=
⟨λ f g, { monotone' := f.monotone'.mul' g.monotone', ..(f * g : α →*₀ β) }⟩
@[simp] lemma coe_mul (f g : α →*₀o β) : ⇑(f * g) = f * g := rfl
@[simp] lemma mul_apply (f g : α →*₀o β) (a : α) : (f * g) a = f a * g a := rfl
lemma mul_comp (g₁ g₂ : β →*₀o γ) (f : α →*₀o β) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl
lemma comp_mul (g : β →*₀o γ) (f₁ f₂ : α →*₀o β) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ :=
ext $ λ _, map_mul g _ _
end mul
section linear_ordered_comm_monoid_with_zero
variables {hα : preorder α} {hα' : mul_zero_one_class α} {hβ : preorder β}
{hβ' : mul_zero_one_class β}
include hα hα' hβ hβ'
@[simp] lemma to_monoid_with_zero_hom_eq_coe (f : α →*₀o β) : f.to_monoid_with_zero_hom = f :=
by { ext, refl }
@[simp] lemma to_order_monoid_hom_eq_coe (f : α →*₀o β) : f.to_order_monoid_hom = f := rfl
end linear_ordered_comm_monoid_with_zero
end order_monoid_with_zero_hom
|
cc8d7559b75542c3ff28b676550ea648f9179580 | ea5678cc400c34ff95b661fa26d15024e27ea8cd | /MWE.lean | 65b4dea295b0eaf15a38fe2d170d7e8a9d5494f5 | [] | no_license | ChrisHughes24/leanstuff | dca0b5349c3ed893e8792ffbd98cbcadaff20411 | 9efa85f72efaccd1d540385952a6acc18fce8687 | refs/heads/master | 1,654,883,241,759 | 1,652,873,885,000 | 1,652,873,885,000 | 134,599,537 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,561 | lean | import data.complex.basic
open cau_seq
namespace real
lemma eq_lim_of_const_equiv {f : cau_seq ℝ abs} {x : ℝ} (h : cau_seq.const abs x ≈ f) : x = lim f :=
const_equiv.mp (setoid.trans h (equiv_lim f))
lemma lim_eq_of_equiv_const {f : cau_seq ℝ abs} {x : ℝ} (h : f ≈ cau_seq.const abs x) : lim f = x :=
(eq_lim_of_const_equiv (setoid.symm h)).symm
lemma lim_eq_lim_of_equiv {f g : cau_seq ℝ abs} (h : f ≈ g) : lim f = lim g :=
lim_eq_of_equiv_const (setoid.trans h (equiv_lim g))
@[simp] lemma lim_const (x : ℝ) : lim (const abs x) = x :=
lim_eq_of_equiv_const (setoid.refl _)
lemma lim_add (f g : cau_seq ℝ abs) : lim f + lim g = lim ⇑(f + g) :=
eq_lim_of_const_equiv (show lim_zero (const abs (lim ⇑f + lim ⇑g) - (f + g)),
from by rw [const_add, add_sub_comm];
exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)))
lemma lim_mul_lim (f g : cau_seq ℝ abs) : lim f * lim g = lim ⇑(f * g) :=
eq_lim_of_const_equiv (show lim_zero (const abs (lim ⇑f * lim ⇑g) - f * g),
from have h : const abs (lim ⇑f * lim ⇑g) - f * g = g * (const abs (lim f) - f)
+ const abs (lim f) * (const abs (lim g) - g) :=
by simp [mul_sub, mul_comm, const_mul, mul_add],
by rw h; exact add_lim_zero (mul_lim_zero _ (setoid.symm (equiv_lim f)))
(mul_lim_zero _ (setoid.symm (equiv_lim g))))
lemma lim_mul (f : cau_seq ℝ abs) (x : ℝ) : lim f * x = lim ⇑(f * const abs x) :=
by rw [← lim_mul_lim, lim_const]
lemma lim_neg (f : cau_seq ℝ abs) : lim ⇑(-f) = -lim f :=
lim_eq_of_equiv_const (show lim_zero (-f - const abs (-lim ⇑f)),
from by rw [const_neg, sub_neg_eq_add, add_comm];
exact setoid.symm (equiv_lim f))
lemma lim_eq_zero_iff (f : cau_seq ℝ abs) : lim f = 0 ↔ lim_zero f :=
⟨assume h,
by have hf := equiv_lim f;
rw h at hf;
exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl),
assume h,
have h₁ : f = (f - const abs 0) := ext (λ n, by simp [sub_apply, const_apply]),
by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩
end real
namespace complex
local notation `abs'` := _root_.abs
open cau_seq
lemma is_cau_seq_coe (f : cau_seq ℝ abs') : is_cau_seq abs (λ n, f n) :=
show ∀ ε > 0, ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abs (↑(f j) - ↑(f i)) < ε,
from by simp only [(of_real_sub _ _).symm, abs_of_real]; exact f.2
lemma is_cau_seq_of_im_re {f : ℕ → ℂ} (hre : is_cau_seq abs' (λ n, (f n).re)) (him : is_cau_seq abs' (λ n, (f n).im)) :
is_cau_seq abs f :=
have h : f = (λ n, (f n).re + (f n).im * I) := funext (λ n, (re_add_im (f n)).symm),
let g : cau_seq ℂ abs := ⟨_, is_cau_seq_coe ⟨_, hre⟩⟩ + ⟨_, is_cau_seq_coe ⟨_, him⟩⟩ * cau_seq.const abs I in
by rw h; exact g.2
namespace cau_seq
noncomputable def conj (f : cau_seq ℂ abs) : cau_seq ℂ abs := ⟨λ n, complex.conj (f n), is_cau_seq_of_im_re (is_cau_seq_re f) (is_cau_seq_im (-f))⟩
end cau_seq
open cau_seq
theorem const_equiv1 {α β : Type*} [discrete_linear_ordered_field α] [ring β] {abv : β → α} [is_absolute_value abv] {x y : β} : const abv x ≈ const abv y ↔ x = y :=
show lim_zero _ ↔ _, by rw [← const_sub, const_lim_zero, sub_eq_zero]
@[simp] lemma conj_apply (f : cau_seq ℂ abs) (n : ℕ) : (cau_seq.conj f) n = conj (f n) := rfl
lemma eq_lim_of_const_equiv {f : cau_seq ℂ abs} {x : ℂ} (h : cau_seq.const abs x ≈ f) : x = lim f :=
const_equiv1.mp (setoid.trans h (equiv_lim f))
lemma lim_eq_of_equiv_const {f : cau_seq ℂ abs} {x : ℂ} (h : f ≈ cau_seq.const abs x) : lim f = x :=
(eq_lim_of_const_equiv (setoid.symm h)).symm
lemma lim_eq_lim_of_equiv {f g : cau_seq ℂ abs} (h : f ≈ g) : lim f = lim g :=
lim_eq_of_equiv_const (setoid.trans h (equiv_lim g))
@[simp] lemma lim_const (x : ℂ) : lim (const abs x) = x :=
lim_eq_of_equiv_const (setoid.refl _)
lemma lim_add (f g : cau_seq ℂ abs) : lim f + lim g = lim ⇑(f + g) :=
eq_lim_of_const_equiv (show lim_zero (const abs (lim ⇑f + lim ⇑g) - (f + g)),
from by rw [const_add, add_sub_comm];
exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)))
lemma lim_mul_lim (f g : cau_seq ℂ abs) : lim f * lim g = lim ⇑(f * g) :=
eq_lim_of_const_equiv (show lim_zero (const abs (lim ⇑f * lim ⇑g) - f * g),
from have h : const abs (lim ⇑f * lim ⇑g) - f * g = g * (const abs (lim f) - f)
+ const abs (lim f) * (const abs (lim g) - g) :=
by simp [mul_sub, mul_comm, const_mul, mul_add],
by rw h; exact add_lim_zero (mul_lim_zero _ (setoid.symm (equiv_lim f)))
(mul_lim_zero _ (setoid.symm (equiv_lim g))))
lemma lim_mul (f : cau_seq ℂ abs) (x : ℝ) : lim f * x = lim ⇑(f * const abs x) :=
by rw [← lim_mul_lim, lim_const]
lemma lim_neg (f : cau_seq ℂ abs) : lim ⇑(-f) = -lim f :=
lim_eq_of_equiv_const (show lim_zero (-f - const abs (-lim ⇑f)),
from by rw [const_neg, sub_neg_eq_add, add_comm];
exact setoid.symm (equiv_lim f))
lemma lim_conj (f : cau_seq ℂ abs) : lim (cau_seq.conj f) = conj (lim f) :=
begin
unfold lim,
apply ext,
{ simp },
{ simp,
have := real.lim_neg ⟨_, is_cau_seq_im f⟩,
exact this }
end
-- f : cau_seq ℝ abs, -f = (λ n, -f n)
lemma lim_eq_zero_iff (f : cau_seq ℂ abs) : lim f = 0 ↔ lim_zero f :=
⟨assume h,
by have hf := equiv_lim f;
rw h at hf;
exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl),
assume h,
have h₁ : f = (f - const abs 0) := cau_seq.ext (λ n, by simp [sub_apply, const_apply]),
by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩
end complex |
5dfde2c634c3f5f7efcb852d5ff10e6bfa5ac5ca | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/490.lean | f09bf7c634b71b0ee23eb929be622caf3e99eea6 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 118 | lean | structure {u} foo : Type (u+2) :=
(elim : Type u → Type u)
set_option pp.universes true
#check foo.elim
#check foo
|
17a1140d1cb4c572a89539224ef266b93bc6a625 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/bench/dag_hassorry_issue.lean | 2c32c0fc23573b7db1ce8032991fb66e017bb600 | [
"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 | 1,745 | lean | import Lean
open Lean
def mkEqX (bidx : Nat) : Expr :=
mkApp3 (mkConst ``Eq [levelOne]) (mkConst ``Nat []) (.bvar bidx) (.bvar bidx)
def mkReflX (bidx : Nat) : Expr :=
mkApp2 (mkConst ``Eq.refl [levelOne]) (mkConst ``Nat []) (.bvar bidx)
def mkAnds (n : Nat) : Expr :=
match n with
| 0 => mkConst ``True []
| n+1 => mkAnd (mkEqX n) (mkAnds n)
def mkAndIntro (p q h₁ h₂ : Expr) : Expr :=
mkApp4 (mkConst ``And.intro []) p q h₁ h₂
def mkRefls (n : Nat) (ands : List Expr) : Expr :=
match n, ands with
| n+1, and::ands => mkAndIntro (mkEqX n) and (mkReflX n) (mkRefls n ands)
| _, _ => mkConst ``True.intro []
def mkLambdas (n : Nat) (p : Expr) : Expr :=
match n with
| 0 => p
| n+1 => .lam `x (mkConst ``Nat []) (mkLambdas n p) .default
def mkForalls (n : Nat) (p : Expr) : Expr :=
match n with
| 0 => p
| n+1 => .forallE `x (mkConst ``Nat []) (mkForalls n p) .default
partial def toAndList (ands : Expr) (acc : List Expr) : List Expr :=
if ands.isAppOf ``And then
toAndList (ands.getArg! 1) (ands.getArg! 1 :: acc)
else
(ands :: acc).reverse
def test (n : Nat) : CoreM Unit := do
let and := mkAnds n
let type := mkForalls n and
let andList := toAndList and []
let value := mkLambdas n (mkRefls n andList)
addDecl <| .thmDecl {
name := (`test_abst).appendIndexAfter n
levelParams := []
type, value
}
return ()
def main (args : List String) : IO Unit := do
let [size] := args | throw (IO.userError s!"unexpected number of arguments, numeral expected")
initSearchPath (← findSysroot)
let env ← importModules [{ module := `Init.Prelude }] {} 0
discard <| test size.toNat! |>.toIO { fileName := "<test>", fileMap := default } { env }
IO.println "ok"
|
a385c068b215bb9fea549fa939583683606fb679 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/big_operators/pi.lean | fa32e9ff5a7c137b6dd08607f7d41db8fa84f526 | [
"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 | 3,902 | 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
-/
import algebra.ring.pi
import algebra.big_operators.basic
import data.fintype.basic
import algebra.group.prod
/-!
# Big operators for Pi Types
This file contains theorems relevant to big operators in binary and arbitrary product
of monoids and groups
-/
open_locale big_operators
namespace pi
@[to_additive]
lemma list_prod_apply {α : Type*} {β : α → Type*} [Πa, monoid (β a)] (a : α) (l : list (Πa, β a)) :
l.prod a = (l.map (λf:Πa, β a, f a)).prod :=
(eval_monoid_hom β a).map_list_prod _
@[to_additive]
lemma multiset_prod_apply {α : Type*} {β : α → Type*} [∀a, comm_monoid (β a)] (a : α)
(s : multiset (Πa, β a)) : s.prod a = (s.map (λf:Πa, β a, f a)).prod :=
(eval_monoid_hom β a).map_multiset_prod _
end pi
@[simp, to_additive]
lemma finset.prod_apply {α : Type*} {β : α → Type*} {γ} [∀a, comm_monoid (β a)] (a : α)
(s : finset γ) (g : γ → Πa, β a) : (∏ c in s, g c) a = ∏ c in s, g c a :=
(pi.eval_monoid_hom β a).map_prod _ _
/-- An 'unapplied' analogue of `finset.prod_apply`. -/
@[to_additive "An 'unapplied' analogue of `finset.sum_apply`."]
lemma finset.prod_fn {α : Type*} {β : α → Type*} {γ} [∀a, comm_monoid (β a)]
(s : finset γ) (g : γ → Πa, β a) : (∏ c in s, g c) = (λ a, ∏ c in s, g c a) :=
funext (λ a, finset.prod_apply _ _ _)
@[simp, to_additive]
lemma fintype.prod_apply {α : Type*} {β : α → Type*} {γ : Type*} [fintype γ]
[∀a, comm_monoid (β a)] (a : α) (g : γ → Πa, β a) : (∏ c, g c) a = ∏ c, g c a :=
finset.prod_apply a finset.univ g
@[to_additive prod_mk_sum]
lemma prod_mk_prod {α β γ : Type*} [comm_monoid α] [comm_monoid β] (s : finset γ)
(f : γ → α) (g : γ → β) : (∏ x in s, f x, ∏ x in s, g x) = ∏ x in s, (f x, g x) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s rfl (by simp [prod.ext_iff] {contextual := tt})
section single
variables {I : Type*} [decidable_eq I] {Z : I → Type*}
variables [Π i, add_comm_monoid (Z i)]
-- As we only defined `single` into `add_monoid`, we only prove the `finset.sum` version here.
lemma finset.univ_sum_single [fintype I] (f : Π i, Z i) :
∑ i, pi.single i (f i) = f :=
by { ext a, simp }
lemma add_monoid_hom.functions_ext [fintype I] (G : Type*)
[add_comm_monoid G] (g h : (Π i, Z i) →+ G)
(w : ∀ (i : I) (x : Z i), g (pi.single i x) = h (pi.single i x)) : g = h :=
begin
ext k,
rw [← finset.univ_sum_single k, g.map_sum, h.map_sum],
simp only [w]
end
/-- This is used as the ext lemma instead of `add_monoid_hom.functions_ext` for reasons explained in
note [partially-applied ext lemmas]. -/
@[ext]
lemma add_monoid_hom.functions_ext' [fintype I] (M : Type*) [add_comm_monoid M]
(g h : (Π i, Z i) →+ M)
(H : ∀ i, g.comp (add_monoid_hom.single Z i) = h.comp (add_monoid_hom.single Z i)) :
g = h :=
have _ := λ i, add_monoid_hom.congr_fun (H i), -- elab without an expected type
g.functions_ext M h this
end single
section ring_hom
open pi
variables {I : Type*} [decidable_eq I] {f : I → Type*}
variables [Π i, semiring (f i)]
@[ext] lemma ring_hom.functions_ext [fintype I] (G : Type*) [semiring G] (g h : (Π i, f i) →+* G)
(w : ∀ (i : I) (x : f i), g (single i x) = h (single i x)) : g = h :=
ring_hom.coe_add_monoid_hom_injective $
add_monoid_hom.functions_ext G (g : (Π i, f i) →+ G) h w
end ring_hom
namespace prod
variables {α β γ : Type*} [comm_monoid α] [comm_monoid β] {s : finset γ} {f : γ → α × β}
@[to_additive]
lemma fst_prod : (∏ c in s, f c).1 = ∏ c in s, (f c).1 :=
(monoid_hom.fst α β).map_prod f s
@[to_additive]
lemma snd_prod : (∏ c in s, f c).2 = ∏ c in s, (f c).2 :=
(monoid_hom.snd α β).map_prod f s
end prod
|
54efd643c8c6a2beb29cf8d32372553a68788625 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/measure_theory/measure/vector_measure.lean | 2bb06f31d6fcb9765afe98e2aa779cec16118dc7 | [
"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 | 50,230 | lean | /-
Copyright (c) 2021 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import measure_theory.measure.measure_space
import analysis.complex.basic
/-!
# Vector valued measures
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines vector valued measures, which are σ-additive functions from a set to a add monoid
`M` such that it maps the empty set and non-measurable sets to zero. In the case
that `M = ℝ`, we called the vector measure a signed measure and write `signed_measure α`.
Similarly, when `M = ℂ`, we call the measure a complex measure and write `complex_measure α`.
## Main definitions
* `measure_theory.vector_measure` is a vector valued, σ-additive function that maps the empty
and non-measurable set to zero.
* `measure_theory.vector_measure.map` is the pushforward of a vector measure along a function.
* `measure_theory.vector_measure.restrict` is the restriction of a vector measure on some set.
## Notation
* `v ≤[i] w` means that the vector measure `v` restricted on the set `i` is less than or equal
to the vector measure `w` restricted on `i`, i.e. `v.restrict i ≤ w.restrict i`.
## Implementation notes
We require all non-measurable sets to be mapped to zero in order for the extensionality lemma
to only compare the underlying functions for measurable sets.
We use `has_sum` instead of `tsum` in the definition of vector measures in comparison to `measure`
since this provides summablity.
## Tags
vector measure, signed measure, complex measure
-/
noncomputable theory
open_locale classical big_operators nnreal ennreal measure_theory
namespace measure_theory
variables {α β : Type*} {m : measurable_space α}
/-- A vector measure on a measurable space `α` is a σ-additive `M`-valued function (for some `M`
an add monoid) such that the empty set and non-measurable sets are mapped to zero. -/
structure vector_measure (α : Type*) [measurable_space α]
(M : Type*) [add_comm_monoid M] [topological_space M] :=
(measure_of' : set α → M)
(empty' : measure_of' ∅ = 0)
(not_measurable' ⦃i : set α⦄ : ¬ measurable_set i → measure_of' i = 0)
(m_Union' ⦃f : ℕ → set α⦄ :
(∀ i, measurable_set (f i)) → pairwise (disjoint on f) →
has_sum (λ i, measure_of' (f i)) (measure_of' (⋃ i, f i)))
/-- A `signed_measure` is a `ℝ`-vector measure. -/
abbreviation signed_measure (α : Type*) [measurable_space α] := vector_measure α ℝ
/-- A `complex_measure` is a `ℂ`-vector_measure. -/
abbreviation complex_measure (α : Type*) [measurable_space α] := vector_measure α ℂ
open set measure_theory
namespace vector_measure
section
variables {M : Type*} [add_comm_monoid M] [topological_space M]
include m
instance : has_coe_to_fun (vector_measure α M) (λ _, set α → M) :=
⟨vector_measure.measure_of'⟩
initialize_simps_projections vector_measure (measure_of' → apply)
@[simp]
lemma measure_of_eq_coe (v : vector_measure α M) : v.measure_of' = v := rfl
@[simp]
lemma empty (v : vector_measure α M) : v ∅ = 0 := v.empty'
lemma not_measurable (v : vector_measure α M)
{i : set α} (hi : ¬ measurable_set i) : v i = 0 := v.not_measurable' hi
lemma m_Union (v : vector_measure α M) {f : ℕ → set α}
(hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) :
has_sum (λ i, v (f i)) (v (⋃ i, f i)) :=
v.m_Union' hf₁ hf₂
lemma of_disjoint_Union_nat [t2_space M] (v : vector_measure α M) {f : ℕ → set α}
(hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) :
v (⋃ i, f i) = ∑' i, v (f i) :=
(v.m_Union hf₁ hf₂).tsum_eq.symm
lemma coe_injective : @function.injective (vector_measure α M) (set α → M) coe_fn :=
λ v w h, by { cases v, cases w, congr' }
lemma ext_iff' (v w : vector_measure α M) :
v = w ↔ ∀ i : set α, v i = w i :=
by rw [← coe_injective.eq_iff, function.funext_iff]
lemma ext_iff (v w : vector_measure α M) :
v = w ↔ ∀ i : set α, measurable_set i → v i = w i :=
begin
split,
{ rintro rfl _ _, refl },
{ rw ext_iff',
intros h i,
by_cases hi : measurable_set i,
{ exact h i hi },
{ simp_rw [not_measurable _ hi] } }
end
@[ext] lemma ext {s t : vector_measure α M}
(h : ∀ i : set α, measurable_set i → s i = t i) : s = t :=
(ext_iff s t).2 h
variables [t2_space M] {v : vector_measure α M} {f : ℕ → set α}
lemma has_sum_of_disjoint_Union [countable β] {f : β → set α}
(hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) :
has_sum (λ i, v (f i)) (v (⋃ i, f i)) :=
begin
casesI nonempty_encodable β,
set g := λ i : ℕ, ⋃ (b : β) (H : b ∈ encodable.decode₂ β i), f b with hg,
have hg₁ : ∀ i, measurable_set (g i),
{ exact λ _, measurable_set.Union (λ b, measurable_set.Union $ λ _, hf₁ b) },
have hg₂ : pairwise (disjoint on g),
{ exact encodable.Union_decode₂_disjoint_on hf₂ },
have := v.of_disjoint_Union_nat hg₁ hg₂,
rw [hg, encodable.Union_decode₂] at this,
have hg₃ : (λ (i : β), v (f i)) = (λ i, v (g (encodable.encode i))),
{ ext, rw hg, simp only,
congr, ext y, simp only [exists_prop, mem_Union, option.mem_def],
split,
{ intro hy,
refine ⟨x, (encodable.decode₂_is_partial_inv _ _).2 rfl, hy⟩ },
{ rintro ⟨b, hb₁, hb₂⟩,
rw (encodable.decode₂_is_partial_inv _ _) at hb₁,
rwa ← encodable.encode_injective hb₁ } },
rw [summable.has_sum_iff, this, ← tsum_Union_decode₂],
{ exact v.empty },
{ rw hg₃, change summable ((λ i, v (g i)) ∘ encodable.encode),
rw function.injective.summable_iff encodable.encode_injective,
{ exact (v.m_Union hg₁ hg₂).summable },
{ intros x hx,
convert v.empty,
simp only [Union_eq_empty, option.mem_def, not_exists, mem_range] at ⊢ hx,
intros i hi,
exact false.elim ((hx i) ((encodable.decode₂_is_partial_inv _ _).1 hi)) } }
end
lemma of_disjoint_Union [countable β] {f : β → set α}
(hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) :
v (⋃ i, f i) = ∑' i, v (f i) :=
(has_sum_of_disjoint_Union hf₁ hf₂).tsum_eq.symm
lemma of_union {A B : set α}
(h : disjoint A B) (hA : measurable_set A) (hB : measurable_set B) :
v (A ∪ B) = v A + v B :=
begin
rw [union_eq_Union, of_disjoint_Union, tsum_fintype, fintype.sum_bool, cond, cond],
exacts [λ b, bool.cases_on b hB hA, pairwise_disjoint_on_bool.2 h]
end
lemma of_add_of_diff {A B : set α} (hA : measurable_set A) (hB : measurable_set B)
(h : A ⊆ B) : v A + v (B \ A) = v B :=
begin
rw [← of_union disjoint_sdiff_right hA (hB.diff hA), union_diff_cancel h],
apply_instance,
end
lemma of_diff {M : Type*} [add_comm_group M]
[topological_space M] [t2_space M] {v : vector_measure α M}
{A B : set α} (hA : measurable_set A) (hB : measurable_set B)
(h : A ⊆ B) : v (B \ A) = v B - (v A) :=
begin
rw [← of_add_of_diff hA hB h, add_sub_cancel'],
apply_instance,
end
lemma of_diff_of_diff_eq_zero {A B : set α}
(hA : measurable_set A) (hB : measurable_set B) (h' : v (B \ A) = 0) :
v (A \ B) + v B = v A :=
begin
symmetry,
calc v A = v (A \ B ∪ A ∩ B) : by simp only [set.diff_union_inter]
... = v (A \ B) + v (A ∩ B) :
by { rw of_union,
{ rw disjoint.comm,
exact set.disjoint_of_subset_left (A.inter_subset_right B) disjoint_sdiff_self_right },
{ exact hA.diff hB },
{ exact hA.inter hB } }
... = v (A \ B) + v (A ∩ B ∪ B \ A) :
by { rw [of_union, h', add_zero],
{ exact set.disjoint_of_subset_left (A.inter_subset_left B) disjoint_sdiff_self_right },
{ exact hA.inter hB },
{ exact hB.diff hA } }
... = v (A \ B) + v B :
by { rw [set.union_comm, set.inter_comm, set.diff_union_inter] }
end
lemma of_Union_nonneg {M : Type*} [topological_space M]
[ordered_add_comm_monoid M] [order_closed_topology M]
{v : vector_measure α M} (hf₁ : ∀ i, measurable_set (f i))
(hf₂ : pairwise (disjoint on f)) (hf₃ : ∀ i, 0 ≤ v (f i)) :
0 ≤ v (⋃ i, f i) :=
(v.of_disjoint_Union_nat hf₁ hf₂).symm ▸ tsum_nonneg hf₃
lemma of_Union_nonpos {M : Type*} [topological_space M]
[ordered_add_comm_monoid M] [order_closed_topology M]
{v : vector_measure α M} (hf₁ : ∀ i, measurable_set (f i))
(hf₂ : pairwise (disjoint on f)) (hf₃ : ∀ i, v (f i) ≤ 0) :
v (⋃ i, f i) ≤ 0 :=
(v.of_disjoint_Union_nat hf₁ hf₂).symm ▸ tsum_nonpos hf₃
lemma of_nonneg_disjoint_union_eq_zero {s : signed_measure α} {A B : set α}
(h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B)
(hA₂ : 0 ≤ s A) (hB₂ : 0 ≤ s B)
(hAB : s (A ∪ B) = 0) : s A = 0 :=
begin
rw of_union h hA₁ hB₁ at hAB,
linarith,
apply_instance,
end
lemma of_nonpos_disjoint_union_eq_zero {s : signed_measure α} {A B : set α}
(h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B)
(hA₂ : s A ≤ 0) (hB₂ : s B ≤ 0)
(hAB : s (A ∪ B) = 0) : s A = 0 :=
begin
rw of_union h hA₁ hB₁ at hAB,
linarith,
apply_instance,
end
end
section has_smul
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables {R : Type*} [semiring R] [distrib_mul_action R M] [has_continuous_const_smul R M]
include m
/-- Given a real number `r` and a signed measure `s`, `smul r s` is the signed
measure corresponding to the function `r • s`. -/
def smul (r : R) (v : vector_measure α M) : vector_measure α M :=
{ measure_of' := r • v,
empty' := by rw [pi.smul_apply, empty, smul_zero],
not_measurable' := λ _ hi, by rw [pi.smul_apply, v.not_measurable hi, smul_zero],
m_Union' := λ _ hf₁ hf₂, has_sum.const_smul _ (v.m_Union hf₁ hf₂) }
instance : has_smul R (vector_measure α M) := ⟨smul⟩
@[simp] lemma coe_smul (r : R) (v : vector_measure α M) : ⇑(r • v) = r • v := rfl
lemma smul_apply (r : R) (v : vector_measure α M) (i : set α) :
(r • v) i = r • v i := rfl
end has_smul
section add_comm_monoid
variables {M : Type*} [add_comm_monoid M] [topological_space M]
include m
instance : has_zero (vector_measure α M) :=
⟨⟨0, rfl, λ _ _, rfl, λ _ _ _, has_sum_zero⟩⟩
instance : inhabited (vector_measure α M) := ⟨0⟩
@[simp] lemma coe_zero : ⇑(0 : vector_measure α M) = 0 := rfl
lemma zero_apply (i : set α) : (0 : vector_measure α M) i = 0 := rfl
variables [has_continuous_add M]
/-- The sum of two vector measure is a vector measure. -/
def add (v w : vector_measure α M) : vector_measure α M :=
{ measure_of' := v + w,
empty' := by simp,
not_measurable' := λ _ hi,
by simp [v.not_measurable hi, w.not_measurable hi],
m_Union' := λ f hf₁ hf₂,
has_sum.add (v.m_Union hf₁ hf₂) (w.m_Union hf₁ hf₂) }
instance : has_add (vector_measure α M) := ⟨add⟩
@[simp] lemma coe_add (v w : vector_measure α M) : ⇑(v + w) = v + w := rfl
lemma add_apply (v w : vector_measure α M) (i : set α) : (v + w) i = v i + w i := rfl
instance : add_comm_monoid (vector_measure α M) :=
function.injective.add_comm_monoid _ coe_injective coe_zero coe_add (λ _ _, coe_smul _ _)
/-- `coe_fn` is an `add_monoid_hom`. -/
@[simps]
def coe_fn_add_monoid_hom : vector_measure α M →+ (set α → M) :=
{ to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add }
end add_comm_monoid
section add_comm_group
variables {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M]
include m
/-- The negative of a vector measure is a vector measure. -/
def neg (v : vector_measure α M) : vector_measure α M :=
{ measure_of' := -v,
empty' := by simp,
not_measurable' := λ _ hi, by simp [v.not_measurable hi],
m_Union' := λ f hf₁ hf₂, has_sum.neg $ v.m_Union hf₁ hf₂ }
instance : has_neg (vector_measure α M) := ⟨neg⟩
@[simp] lemma coe_neg (v : vector_measure α M) : ⇑(-v) = - v := rfl
lemma neg_apply (v : vector_measure α M) (i : set α) :(-v) i = - v i := rfl
/-- The difference of two vector measure is a vector measure. -/
def sub (v w : vector_measure α M) : vector_measure α M :=
{ measure_of' := v - w,
empty' := by simp,
not_measurable' := λ _ hi,
by simp [v.not_measurable hi, w.not_measurable hi],
m_Union' := λ f hf₁ hf₂,
has_sum.sub (v.m_Union hf₁ hf₂)
(w.m_Union hf₁ hf₂) }
instance : has_sub (vector_measure α M) := ⟨sub⟩
@[simp] lemma coe_sub (v w : vector_measure α M) : ⇑(v - w) = v - w := rfl
lemma sub_apply (v w : vector_measure α M) (i : set α) : (v - w) i = v i - w i := rfl
instance : add_comm_group (vector_measure α M) :=
function.injective.add_comm_group _ coe_injective coe_zero coe_add coe_neg coe_sub
(λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _)
end add_comm_group
section distrib_mul_action
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables {R : Type*} [semiring R] [distrib_mul_action R M] [has_continuous_const_smul R M]
include m
instance [has_continuous_add M] : distrib_mul_action R (vector_measure α M) :=
function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul
end distrib_mul_action
section module
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables {R : Type*} [semiring R] [module R M] [has_continuous_const_smul R M]
include m
instance [has_continuous_add M] : module R (vector_measure α M) :=
function.injective.module R coe_fn_add_monoid_hom coe_injective coe_smul
end module
end vector_measure
namespace measure
include m
/-- A finite measure coerced into a real function is a signed measure. -/
@[simps]
def to_signed_measure (μ : measure α) [hμ : is_finite_measure μ] : signed_measure α :=
{ measure_of' := λ i : set α, if measurable_set i then (μ.measure_of i).to_real else 0,
empty' := by simp [μ.empty],
not_measurable' := λ _ hi, if_neg hi,
m_Union' :=
begin
intros _ hf₁ hf₂,
rw [μ.m_Union hf₁ hf₂, ennreal.tsum_to_real_eq, if_pos (measurable_set.Union hf₁),
summable.has_sum_iff],
{ congr, ext n, rw if_pos (hf₁ n) },
{ refine @summable_of_nonneg_of_le _ (ennreal.to_real ∘ μ ∘ f) _ _ _ _,
{ intro, split_ifs,
exacts [ennreal.to_real_nonneg, le_rfl] },
{ intro, split_ifs,
exacts [le_rfl, ennreal.to_real_nonneg] },
exact summable_measure_to_real hf₁ hf₂ },
{ intros a ha,
apply ne_of_lt hμ.measure_univ_lt_top,
rw [eq_top_iff, ← ha, outer_measure.measure_of_eq_coe, coe_to_outer_measure],
exact measure_mono (set.subset_univ _) }
end }
lemma to_signed_measure_apply_measurable {μ : measure α} [is_finite_measure μ]
{i : set α} (hi : measurable_set i) :
μ.to_signed_measure i = (μ i).to_real :=
if_pos hi
-- Without this lemma, `singular_part_neg` in `measure_theory.decomposition.lebesgue` is
-- extremely slow
lemma to_signed_measure_congr {μ ν : measure α} [is_finite_measure μ] [is_finite_measure ν]
(h : μ = ν) : μ.to_signed_measure = ν.to_signed_measure :=
by { congr, exact h }
lemma to_signed_measure_eq_to_signed_measure_iff
{μ ν : measure α} [is_finite_measure μ] [is_finite_measure ν] :
μ.to_signed_measure = ν.to_signed_measure ↔ μ = ν :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ ext1 i hi,
have : μ.to_signed_measure i = ν.to_signed_measure i,
{ rw h },
rwa [to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi,
ennreal.to_real_eq_to_real] at this;
{ exact measure_ne_top _ _ } },
{ congr, assumption }
end
@[simp] lemma to_signed_measure_zero :
(0 : measure α).to_signed_measure = 0 :=
by { ext i hi, simp }
@[simp] lemma to_signed_measure_add (μ ν : measure α) [is_finite_measure μ] [is_finite_measure ν] :
(μ + ν).to_signed_measure = μ.to_signed_measure + ν.to_signed_measure :=
begin
ext i hi,
rw [to_signed_measure_apply_measurable hi, add_apply,
ennreal.to_real_add (ne_of_lt (measure_lt_top _ _ )) (ne_of_lt (measure_lt_top _ _)),
vector_measure.add_apply, to_signed_measure_apply_measurable hi,
to_signed_measure_apply_measurable hi],
all_goals { apply_instance }
end
@[simp] lemma to_signed_measure_smul (μ : measure α) [is_finite_measure μ] (r : ℝ≥0) :
(r • μ).to_signed_measure = r • μ.to_signed_measure :=
begin
ext i hi,
rw [to_signed_measure_apply_measurable hi, vector_measure.smul_apply,
to_signed_measure_apply_measurable hi, coe_smul, pi.smul_apply,
ennreal.to_real_smul],
end
/-- A measure is a vector measure over `ℝ≥0∞`. -/
@[simps]
def to_ennreal_vector_measure (μ : measure α) : vector_measure α ℝ≥0∞ :=
{ measure_of' := λ i : set α, if measurable_set i then μ i else 0,
empty' := by simp [μ.empty],
not_measurable' := λ _ hi, if_neg hi,
m_Union' := λ _ hf₁ hf₂,
begin
rw summable.has_sum_iff ennreal.summable,
{ rw [if_pos (measurable_set.Union hf₁), measure_theory.measure_Union hf₂ hf₁],
exact tsum_congr (λ n, if_pos (hf₁ n)) },
end }
lemma to_ennreal_vector_measure_apply_measurable
{μ : measure α} {i : set α} (hi : measurable_set i) :
μ.to_ennreal_vector_measure i = μ i :=
if_pos hi
@[simp] lemma to_ennreal_vector_measure_zero :
(0 : measure α).to_ennreal_vector_measure = 0 :=
by { ext i hi, simp }
@[simp] lemma to_ennreal_vector_measure_add (μ ν : measure α) :
(μ + ν).to_ennreal_vector_measure = μ.to_ennreal_vector_measure + ν.to_ennreal_vector_measure :=
begin
refine measure_theory.vector_measure.ext (λ i hi, _),
rw [to_ennreal_vector_measure_apply_measurable hi, add_apply, vector_measure.add_apply,
to_ennreal_vector_measure_apply_measurable hi, to_ennreal_vector_measure_apply_measurable hi]
end
lemma to_signed_measure_sub_apply {μ ν : measure α} [is_finite_measure μ] [is_finite_measure ν]
{i : set α} (hi : measurable_set i) :
(μ.to_signed_measure - ν.to_signed_measure) i = (μ i).to_real - (ν i).to_real :=
begin
rw [vector_measure.sub_apply, to_signed_measure_apply_measurable hi,
measure.to_signed_measure_apply_measurable hi, sub_eq_add_neg]
end
end measure
namespace vector_measure
open measure
section
/-- A vector measure over `ℝ≥0∞` is a measure. -/
def ennreal_to_measure {m : measurable_space α} (v : vector_measure α ℝ≥0∞) : measure α :=
of_measurable (λ s _, v s) v.empty (λ f hf₁ hf₂, v.of_disjoint_Union_nat hf₁ hf₂)
lemma ennreal_to_measure_apply {m : measurable_space α} {v : vector_measure α ℝ≥0∞}
{s : set α} (hs : measurable_set s) : ennreal_to_measure v s = v s :=
by rw [ennreal_to_measure, of_measurable_apply _ hs]
/-- The equiv between `vector_measure α ℝ≥0∞` and `measure α` formed by
`measure_theory.vector_measure.ennreal_to_measure` and
`measure_theory.measure.to_ennreal_vector_measure`. -/
@[simps] def equiv_measure [measurable_space α] : vector_measure α ℝ≥0∞ ≃ measure α :=
{ to_fun := ennreal_to_measure,
inv_fun := to_ennreal_vector_measure,
left_inv := λ _, ext (λ s hs,
by rw [to_ennreal_vector_measure_apply_measurable hs, ennreal_to_measure_apply hs]),
right_inv := λ _, measure.ext (λ s hs,
by rw [ennreal_to_measure_apply hs, to_ennreal_vector_measure_apply_measurable hs]) }
end
section
variables [measurable_space α] [measurable_space β]
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables (v : vector_measure α M)
/-- The pushforward of a vector measure along a function. -/
def map (v : vector_measure α M) (f : α → β) :
vector_measure β M :=
if hf : measurable f then
{ measure_of' := λ s, if measurable_set s then v (f ⁻¹' s) else 0,
empty' := by simp,
not_measurable' := λ i hi, if_neg hi,
m_Union' :=
begin
intros g hg₁ hg₂,
convert v.m_Union (λ i, hf (hg₁ i)) (λ i j hij, (hg₂ hij).preimage _),
{ ext i, rw if_pos (hg₁ i) },
{ rw [preimage_Union, if_pos (measurable_set.Union hg₁)] },
end } else 0
lemma map_not_measurable {f : α → β} (hf : ¬ measurable f) : v.map f = 0 :=
dif_neg hf
lemma map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) :
v.map f s = v (f ⁻¹' s) :=
by { rw [map, dif_pos hf], exact if_pos hs }
@[simp] lemma map_id : v.map id = v :=
ext (λ i hi, by rw [map_apply v measurable_id hi, preimage_id])
@[simp] lemma map_zero (f : α → β) : (0 : vector_measure α M).map f = 0 :=
begin
by_cases hf : measurable f,
{ ext i hi,
rw [map_apply _ hf hi, zero_apply, zero_apply] },
{ exact dif_neg hf }
end
section
variables {N : Type*} [add_comm_monoid N] [topological_space N]
/-- Given a vector measure `v` on `M` and a continuous add_monoid_hom `f : M → N`, `f ∘ v` is a
vector measure on `N`. -/
def map_range (v : vector_measure α M) (f : M →+ N) (hf : continuous f) : vector_measure α N :=
{ measure_of' := λ s, f (v s),
empty' := by rw [empty, add_monoid_hom.map_zero],
not_measurable' := λ i hi, by rw [not_measurable v hi, add_monoid_hom.map_zero],
m_Union' := λ g hg₁ hg₂, has_sum.map (v.m_Union hg₁ hg₂) f hf }
@[simp] lemma map_range_apply {f : M →+ N} (hf : continuous f) {s : set α} :
v.map_range f hf s = f (v s) :=
rfl
@[simp] lemma map_range_id :
v.map_range (add_monoid_hom.id M) continuous_id = v :=
by { ext, refl }
@[simp] lemma map_range_zero {f : M →+ N} (hf : continuous f) :
map_range (0 : vector_measure α M) f hf = 0 :=
by { ext, simp }
section has_continuous_add
variables [has_continuous_add M] [has_continuous_add N]
@[simp] lemma map_range_add {v w : vector_measure α M} {f : M →+ N} (hf : continuous f) :
(v + w).map_range f hf = v.map_range f hf + w.map_range f hf :=
by { ext, simp }
/-- Given a continuous add_monoid_hom `f : M → N`, `map_range_hom` is the add_monoid_hom mapping the
vector measure `v` on `M` to the vector measure `f ∘ v` on `N`. -/
def map_range_hom (f : M →+ N) (hf : continuous f) : vector_measure α M →+ vector_measure α N :=
{ to_fun := λ v, v.map_range f hf,
map_zero' := map_range_zero hf,
map_add' := λ _ _, map_range_add hf }
end has_continuous_add
section module
variables {R : Type*} [semiring R] [module R M] [module R N]
variables [has_continuous_add M] [has_continuous_add N]
[has_continuous_const_smul R M] [has_continuous_const_smul R N]
/-- Given a continuous linear map `f : M → N`, `map_rangeₗ` is the linear map mapping the
vector measure `v` on `M` to the vector measure `f ∘ v` on `N`. -/
def map_rangeₗ (f : M →ₗ[R] N) (hf : continuous f) : vector_measure α M →ₗ[R] vector_measure α N :=
{ to_fun := λ v, v.map_range f.to_add_monoid_hom hf,
map_add' := λ _ _, map_range_add hf,
map_smul' := by { intros, ext, simp } }
end module
end
/-- The restriction of a vector measure on some set. -/
def restrict (v : vector_measure α M) (i : set α) :
vector_measure α M :=
if hi : measurable_set i then
{ measure_of' := λ s, if measurable_set s then v (s ∩ i) else 0,
empty' := by simp,
not_measurable' := λ i hi, if_neg hi,
m_Union' :=
begin
intros f hf₁ hf₂,
convert v.m_Union (λ n, (hf₁ n).inter hi)
(hf₂.mono $ λ i j, disjoint.mono inf_le_left inf_le_left),
{ ext n, rw if_pos (hf₁ n) },
{ rw [Union_inter, if_pos (measurable_set.Union hf₁)] }
end } else 0
lemma restrict_not_measurable {i : set α} (hi : ¬ measurable_set i) :
v.restrict i = 0 :=
dif_neg hi
lemma restrict_apply {i : set α} (hi : measurable_set i)
{j : set α} (hj : measurable_set j) : v.restrict i j = v (j ∩ i) :=
by { rw [restrict, dif_pos hi], exact if_pos hj }
lemma restrict_eq_self {i : set α} (hi : measurable_set i)
{j : set α} (hj : measurable_set j) (hij : j ⊆ i) : v.restrict i j = v j :=
by rw [restrict_apply v hi hj, inter_eq_left_iff_subset.2 hij]
@[simp] lemma restrict_empty : v.restrict ∅ = 0 :=
ext (λ i hi, by rw [restrict_apply v measurable_set.empty hi, inter_empty, v.empty, zero_apply])
@[simp] lemma restrict_univ : v.restrict univ = v :=
ext (λ i hi, by rw [restrict_apply v measurable_set.univ hi, inter_univ])
@[simp] lemma restrict_zero {i : set α} :
(0 : vector_measure α M).restrict i = 0 :=
begin
by_cases hi : measurable_set i,
{ ext j hj, rw [restrict_apply 0 hi hj], refl },
{ exact dif_neg hi }
end
section has_continuous_add
variables [has_continuous_add M]
lemma map_add (v w : vector_measure α M) (f : α → β) :
(v + w).map f = v.map f + w.map f :=
begin
by_cases hf : measurable f,
{ ext i hi,
simp [map_apply _ hf hi] },
{ simp [map, dif_neg hf] }
end
/-- `vector_measure.map` as an additive monoid homomorphism. -/
@[simps] def map_gm (f : α → β) : vector_measure α M →+ vector_measure β M :=
{ to_fun := λ v, v.map f,
map_zero' := map_zero f,
map_add' := λ _ _, map_add _ _ f }
lemma restrict_add (v w : vector_measure α M) (i : set α) :
(v + w).restrict i = v.restrict i + w.restrict i :=
begin
by_cases hi : measurable_set i,
{ ext j hj,
simp [restrict_apply _ hi hj] },
{ simp [restrict_not_measurable _ hi] }
end
/--`vector_measure.restrict` as an additive monoid homomorphism. -/
@[simps] def restrict_gm (i : set α) : vector_measure α M →+ vector_measure α M :=
{ to_fun := λ v, v.restrict i,
map_zero' := restrict_zero,
map_add' := λ _ _, restrict_add _ _ i }
end has_continuous_add
end
section
variables [measurable_space β]
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables {R : Type*} [semiring R] [distrib_mul_action R M] [has_continuous_const_smul R M]
include m
@[simp] lemma map_smul {v : vector_measure α M} {f : α → β} (c : R) :
(c • v).map f = c • v.map f :=
begin
by_cases hf : measurable f,
{ ext i hi,
simp [map_apply _ hf hi] },
{ simp only [map, dif_neg hf],
-- `smul_zero` does not work since we do not require `has_continuous_add`
ext i hi, simp }
end
@[simp] lemma restrict_smul {v :vector_measure α M} {i : set α} (c : R) :
(c • v).restrict i = c • v.restrict i :=
begin
by_cases hi : measurable_set i,
{ ext j hj,
simp [restrict_apply _ hi hj] },
{ simp only [restrict_not_measurable _ hi],
-- `smul_zero` does not work since we do not require `has_continuous_add`
ext j hj, simp }
end
end
section
variables [measurable_space β]
variables {M : Type*} [add_comm_monoid M] [topological_space M]
variables {R : Type*} [semiring R] [module R M]
[has_continuous_const_smul R M] [has_continuous_add M]
include m
/-- `vector_measure.map` as a linear map. -/
@[simps] def mapₗ (f : α → β) : vector_measure α M →ₗ[R] vector_measure β M :=
{ to_fun := λ v, v.map f,
map_add' := λ _ _, map_add _ _ f,
map_smul' := λ _ _, map_smul _ }
/-- `vector_measure.restrict` as an additive monoid homomorphism. -/
@[simps] def restrictₗ (i : set α) : vector_measure α M →ₗ[R] vector_measure α M :=
{ to_fun := λ v, v.restrict i,
map_add' := λ _ _, restrict_add _ _ i,
map_smul' := λ _ _, restrict_smul _ }
end
section
variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M]
include m
/-- Vector measures over a partially ordered monoid is partially ordered.
This definition is consistent with `measure.partial_order`. -/
instance : partial_order (vector_measure α M) :=
{ le := λ v w, ∀ i, measurable_set i → v i ≤ w i,
le_refl := λ v i hi, le_rfl,
le_trans := λ u v w h₁ h₂ i hi, le_trans (h₁ i hi) (h₂ i hi),
le_antisymm := λ v w h₁ h₂, ext (λ i hi, le_antisymm (h₁ i hi) (h₂ i hi)) }
variables {u v w : vector_measure α M}
lemma le_iff : v ≤ w ↔ ∀ i, measurable_set i → v i ≤ w i :=
iff.rfl
lemma le_iff' : v ≤ w ↔ ∀ i, v i ≤ w i :=
begin
refine ⟨λ h i, _, λ h i hi, h i⟩,
by_cases hi : measurable_set i,
{ exact h i hi },
{ rw [v.not_measurable hi, w.not_measurable hi] }
end
end
localized "notation (name := vector_measure.restrict) v ` ≤[`:50 i:50 `] `:0 w:50 :=
measure_theory.vector_measure.restrict v i ≤ measure_theory.vector_measure.restrict w i"
in measure_theory
section
variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M]
variables (v w : vector_measure α M)
lemma restrict_le_restrict_iff {i : set α} (hi : measurable_set i) :
v ≤[i] w ↔ ∀ ⦃j⦄, measurable_set j → j ⊆ i → v j ≤ w j :=
⟨λ h j hj₁ hj₂, (restrict_eq_self v hi hj₁ hj₂) ▸ (restrict_eq_self w hi hj₁ hj₂) ▸ h j hj₁,
λ h, le_iff.1 (λ j hj, (restrict_apply v hi hj).symm ▸ (restrict_apply w hi hj).symm ▸
h (hj.inter hi) (set.inter_subset_right j i))⟩
lemma subset_le_of_restrict_le_restrict {i : set α}
(hi : measurable_set i) (hi₂ : v ≤[i] w) {j : set α} (hj : j ⊆ i) :
v j ≤ w j :=
begin
by_cases hj₁ : measurable_set j,
{ exact (restrict_le_restrict_iff _ _ hi).1 hi₂ hj₁ hj },
{ rw [v.not_measurable hj₁, w.not_measurable hj₁] },
end
lemma restrict_le_restrict_of_subset_le {i : set α}
(h : ∀ ⦃j⦄, measurable_set j → j ⊆ i → v j ≤ w j) : v ≤[i] w :=
begin
by_cases hi : measurable_set i,
{ exact (restrict_le_restrict_iff _ _ hi).2 h },
{ rw [restrict_not_measurable v hi, restrict_not_measurable w hi],
exact le_rfl },
end
lemma restrict_le_restrict_subset {i j : set α}
(hi₁ : measurable_set i) (hi₂ : v ≤[i] w) (hij : j ⊆ i) : v ≤[j] w :=
restrict_le_restrict_of_subset_le v w (λ k hk₁ hk₂,
subset_le_of_restrict_le_restrict v w hi₁ hi₂ (set.subset.trans hk₂ hij))
lemma le_restrict_empty : v ≤[∅] w :=
begin
intros j hj,
rw [restrict_empty, restrict_empty]
end
lemma le_restrict_univ_iff_le : v ≤[univ] w ↔ v ≤ w :=
begin
split,
{ intros h s hs,
have := h s hs,
rwa [restrict_apply _ measurable_set.univ hs, inter_univ,
restrict_apply _ measurable_set.univ hs, inter_univ] at this },
{ intros h s hs,
rw [restrict_apply _ measurable_set.univ hs, inter_univ,
restrict_apply _ measurable_set.univ hs, inter_univ],
exact h s hs }
end
end
section
variables {M : Type*} [topological_space M] [ordered_add_comm_group M] [topological_add_group M]
variables (v w : vector_measure α M)
lemma neg_le_neg {i : set α} (hi : measurable_set i) (h : v ≤[i] w) : -w ≤[i] -v :=
begin
intros j hj₁,
rw [restrict_apply _ hi hj₁, restrict_apply _ hi hj₁, neg_apply, neg_apply],
refine neg_le_neg _,
rw [← restrict_apply _ hi hj₁, ← restrict_apply _ hi hj₁],
exact h j hj₁,
end
@[simp]
lemma neg_le_neg_iff {i : set α} (hi : measurable_set i) : -w ≤[i] -v ↔ v ≤[i] w :=
⟨λ h, neg_neg v ▸ neg_neg w ▸ neg_le_neg _ _ hi h, λ h, neg_le_neg _ _ hi h⟩
end
section
variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M]
variables (v w : vector_measure α M) {i j : set α}
lemma restrict_le_restrict_Union {f : ℕ → set α}
(hf₁ : ∀ n, measurable_set (f n)) (hf₂ : ∀ n, v ≤[f n] w) :
v ≤[⋃ n, f n] w :=
begin
refine restrict_le_restrict_of_subset_le v w (λ a ha₁ ha₂, _),
have ha₃ : (⋃ n, a ∩ disjointed f n) = a,
{ rwa [← inter_Union, Union_disjointed, inter_eq_left_iff_subset] },
have ha₄ : pairwise (disjoint on (λ n, a ∩ disjointed f n)),
{ exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) },
rw [← ha₃, v.of_disjoint_Union_nat _ ha₄, w.of_disjoint_Union_nat _ ha₄],
refine tsum_le_tsum (λ n, (restrict_le_restrict_iff v w (hf₁ n)).1 (hf₂ n) _ _) _ _,
{ exact (ha₁.inter (measurable_set.disjointed hf₁ n)) },
{ exact set.subset.trans (set.inter_subset_right _ _) (disjointed_subset _ _) },
{ refine (v.m_Union (λ n, _) _).summable,
{ exact ha₁.inter (measurable_set.disjointed hf₁ n) },
{ exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) } },
{ refine (w.m_Union (λ n, _) _).summable,
{ exact ha₁.inter (measurable_set.disjointed hf₁ n) },
{ exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) } },
{ intro n, exact (ha₁.inter (measurable_set.disjointed hf₁ n)) },
{ exact λ n, ha₁.inter (measurable_set.disjointed hf₁ n) }
end
lemma restrict_le_restrict_countable_Union [countable β] {f : β → set α}
(hf₁ : ∀ b, measurable_set (f b)) (hf₂ : ∀ b, v ≤[f b] w) :
v ≤[⋃ b, f b] w :=
begin
casesI nonempty_encodable β,
rw ← encodable.Union_decode₂,
refine restrict_le_restrict_Union v w _ _,
{ intro n, measurability },
{ intro n,
cases encodable.decode₂ β n with b,
{ simp },
{ simp [hf₂ b] } }
end
lemma restrict_le_restrict_union
(hi₁ : measurable_set i) (hi₂ : v ≤[i] w)
(hj₁ : measurable_set j) (hj₂ : v ≤[j] w) :
v ≤[i ∪ j] w :=
begin
rw union_eq_Union,
refine restrict_le_restrict_countable_Union v w _ _,
{ measurability },
{ rintro (_ | _); simpa }
end
end
section
variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M]
variables (v w : vector_measure α M) {i j : set α}
lemma nonneg_of_zero_le_restrict (hi₂ : 0 ≤[i] v) :
0 ≤ v i :=
begin
by_cases hi₁ : measurable_set i,
{ exact (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hi₁ set.subset.rfl },
{ rw v.not_measurable hi₁ },
end
lemma nonpos_of_restrict_le_zero (hi₂ : v ≤[i] 0) :
v i ≤ 0 :=
begin
by_cases hi₁ : measurable_set i,
{ exact (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hi₁ set.subset.rfl },
{ rw v.not_measurable hi₁ }
end
lemma zero_le_restrict_not_measurable (hi : ¬ measurable_set i) :
0 ≤[i] v :=
begin
rw [restrict_zero, restrict_not_measurable _ hi],
exact le_rfl,
end
lemma restrict_le_zero_of_not_measurable (hi : ¬ measurable_set i) :
v ≤[i] 0 :=
begin
rw [restrict_zero, restrict_not_measurable _ hi],
exact le_rfl,
end
lemma measurable_of_not_zero_le_restrict (hi : ¬ 0 ≤[i] v) : measurable_set i :=
not.imp_symm (zero_le_restrict_not_measurable _) hi
lemma measurable_of_not_restrict_le_zero (hi : ¬ v ≤[i] 0) : measurable_set i :=
not.imp_symm (restrict_le_zero_of_not_measurable _) hi
lemma zero_le_restrict_subset (hi₁ : measurable_set i) (hij : j ⊆ i) (hi₂ : 0 ≤[i] v):
0 ≤[j] v :=
restrict_le_restrict_of_subset_le _ _
(λ k hk₁ hk₂, (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hk₁ (set.subset.trans hk₂ hij))
lemma restrict_le_zero_subset (hi₁ : measurable_set i) (hij : j ⊆ i) (hi₂ : v ≤[i] 0):
v ≤[j] 0 :=
restrict_le_restrict_of_subset_le _ _
(λ k hk₁ hk₂, (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hk₁ (set.subset.trans hk₂ hij))
end
section
variables {M : Type*} [topological_space M] [linear_ordered_add_comm_monoid M]
variables (v w : vector_measure α M) {i j : set α}
include m
lemma exists_pos_measure_of_not_restrict_le_zero (hi : ¬ v ≤[i] 0) :
∃ j : set α, measurable_set j ∧ j ⊆ i ∧ 0 < v j :=
begin
have hi₁ : measurable_set i := measurable_of_not_restrict_le_zero _ hi,
rw [restrict_le_restrict_iff _ _ hi₁] at hi,
push_neg at hi,
obtain ⟨j, hj₁, hj₂, hj⟩ := hi,
exact ⟨j, hj₁, hj₂, hj⟩,
end
end
section
variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M]
[covariant_class M M (+) (≤)] [has_continuous_add M]
include m
instance covariant_add_le :
covariant_class (vector_measure α M) (vector_measure α M) (+) (≤) :=
⟨λ u v w h i hi, add_le_add_left (h i hi) _⟩
end
section
variables {L M N : Type*}
variables [add_comm_monoid L] [topological_space L] [add_comm_monoid M] [topological_space M]
[add_comm_monoid N] [topological_space N]
include m
/-- A vector measure `v` is absolutely continuous with respect to a measure `μ` if for all sets
`s`, `μ s = 0`, we have `v s = 0`. -/
def absolutely_continuous (v : vector_measure α M) (w : vector_measure α N) :=
∀ ⦃s : set α⦄, w s = 0 → v s = 0
localized "infix (name := vector_measure.absolutely_continuous)
` ≪ᵥ `:50 := measure_theory.vector_measure.absolutely_continuous"
in measure_theory
open_locale measure_theory
namespace absolutely_continuous
variables {v : vector_measure α M} {w : vector_measure α N}
lemma mk (h : ∀ ⦃s : set α⦄, measurable_set s → w s = 0 → v s = 0) : v ≪ᵥ w :=
begin
intros s hs,
by_cases hmeas : measurable_set s,
{ exact h hmeas hs },
{ exact not_measurable v hmeas }
end
lemma eq {w : vector_measure α M} (h : v = w) : v ≪ᵥ w :=
λ s hs, h.symm ▸ hs
@[refl] lemma refl (v : vector_measure α M) : v ≪ᵥ v :=
eq rfl
@[trans] lemma trans {u : vector_measure α L} {v : vector_measure α M} {w : vector_measure α N}
(huv : u ≪ᵥ v) (hvw : v ≪ᵥ w) : u ≪ᵥ w :=
λ _ hs, huv $ hvw hs
lemma zero (v : vector_measure α N) : (0 : vector_measure α M) ≪ᵥ v :=
λ s _, vector_measure.zero_apply s
lemma neg_left {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M]
{v : vector_measure α M} {w : vector_measure α N} (h : v ≪ᵥ w) : -v ≪ᵥ w :=
λ s hs, by { rw [neg_apply, h hs, neg_zero] }
lemma neg_right {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N]
{v : vector_measure α M} {w : vector_measure α N} (h : v ≪ᵥ w) : v ≪ᵥ -w :=
begin
intros s hs,
rw [neg_apply, neg_eq_zero] at hs,
exact h hs
end
lemma add [has_continuous_add M] {v₁ v₂ : vector_measure α M} {w : vector_measure α N}
(hv₁ : v₁ ≪ᵥ w) (hv₂ : v₂ ≪ᵥ w) : v₁ + v₂ ≪ᵥ w :=
λ s hs, by { rw [add_apply, hv₁ hs, hv₂ hs, zero_add] }
lemma sub {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M]
{v₁ v₂ : vector_measure α M} {w : vector_measure α N} (hv₁ : v₁ ≪ᵥ w) (hv₂ : v₂ ≪ᵥ w) :
v₁ - v₂ ≪ᵥ w :=
λ s hs, by { rw [sub_apply, hv₁ hs, hv₂ hs, zero_sub, neg_zero] }
lemma smul {R : Type*} [semiring R] [distrib_mul_action R M] [has_continuous_const_smul R M]
{r : R} {v : vector_measure α M} {w : vector_measure α N} (h : v ≪ᵥ w) :
r • v ≪ᵥ w :=
λ s hs, by { rw [smul_apply, h hs, smul_zero] }
lemma map [measure_space β] (h : v ≪ᵥ w) (f : α → β) :
v.map f ≪ᵥ w.map f :=
begin
by_cases hf : measurable f,
{ refine mk (λ s hs hws, _),
rw map_apply _ hf hs at hws ⊢,
exact h hws },
{ intros s hs,
rw [map_not_measurable v hf, zero_apply] }
end
lemma ennreal_to_measure {μ : vector_measure α ℝ≥0∞} :
(∀ ⦃s : set α⦄, μ.ennreal_to_measure s = 0 → v s = 0) ↔ v ≪ᵥ μ :=
begin
split; intro h,
{ refine mk (λ s hmeas hs, h _),
rw [← hs, ennreal_to_measure_apply hmeas] },
{ intros s hs,
by_cases hmeas : measurable_set s,
{ rw ennreal_to_measure_apply hmeas at hs,
exact h hs },
{ exact not_measurable v hmeas } },
end
end absolutely_continuous
/-- Two vector measures `v` and `w` are said to be mutually singular if there exists a measurable
set `s`, such that for all `t ⊆ s`, `v t = 0` and for all `t ⊆ sᶜ`, `w t = 0`.
We note that we do not require the measurability of `t` in the definition since this makes it easier
to use. This is equivalent to the definition which requires measurability. To prove
`mutually_singular` with the measurability condition, use
`measure_theory.vector_measure.mutually_singular.mk`. -/
def mutually_singular (v : vector_measure α M) (w : vector_measure α N) : Prop :=
∃ (s : set α), measurable_set s ∧ (∀ t ⊆ s, v t = 0) ∧ (∀ t ⊆ sᶜ, w t = 0)
localized "infix (name := vector_measure.mutually_singular)
` ⟂ᵥ `:60 := measure_theory.vector_measure.mutually_singular" in measure_theory
namespace mutually_singular
variables {v v₁ v₂ : vector_measure α M} {w w₁ w₂ : vector_measure α N}
lemma mk (s : set α) (hs : measurable_set s)
(h₁ : ∀ t ⊆ s, measurable_set t → v t = 0)
(h₂ : ∀ t ⊆ sᶜ, measurable_set t → w t = 0) : v ⟂ᵥ w :=
begin
refine ⟨s, hs, λ t hst, _, λ t hst, _⟩;
by_cases ht : measurable_set t,
{ exact h₁ t hst ht },
{ exact not_measurable v ht },
{ exact h₂ t hst ht },
{ exact not_measurable w ht }
end
lemma symm (h : v ⟂ᵥ w) : w ⟂ᵥ v :=
let ⟨s, hmeas, hs₁, hs₂⟩ := h in
⟨sᶜ, hmeas.compl, hs₂, λ t ht, hs₁ _ (compl_compl s ▸ ht : t ⊆ s)⟩
lemma zero_right : v ⟂ᵥ (0 : vector_measure α N) :=
⟨∅, measurable_set.empty, λ t ht, (subset_empty_iff.1 ht).symm ▸ v.empty, λ _ _, zero_apply _⟩
lemma zero_left : (0 : vector_measure α M) ⟂ᵥ w :=
zero_right.symm
lemma add_left [t2_space N] [has_continuous_add M] (h₁ : v₁ ⟂ᵥ w) (h₂ : v₂ ⟂ᵥ w) : v₁ + v₂ ⟂ᵥ w :=
begin
obtain ⟨u, hmu, hu₁, hu₂⟩ := h₁,
obtain ⟨v, hmv, hv₁, hv₂⟩ := h₂,
refine mk (u ∩ v) (hmu.inter hmv) (λ t ht hmt, _) (λ t ht hmt, _),
{ rw [add_apply, hu₁ _ (subset_inter_iff.1 ht).1, hv₁ _ (subset_inter_iff.1 ht).2, zero_add] },
{ rw compl_inter at ht,
rw [(_ : t = (uᶜ ∩ t) ∪ (vᶜ \ uᶜ ∩ t)),
of_union _ (hmu.compl.inter hmt) ((hmv.compl.diff hmu.compl).inter hmt),
hu₂, hv₂, add_zero],
{ exact subset.trans (inter_subset_left _ _) (diff_subset _ _) },
{ exact inter_subset_left _ _ },
{ apply_instance },
{ exact disjoint_sdiff_self_right.mono (inter_subset_left _ _) (inter_subset_left _ _) },
{ apply subset.antisymm;
intros x hx,
{ by_cases hxu' : x ∈ uᶜ,
{ exact or.inl ⟨hxu', hx⟩ },
rcases ht hx with (hxu | hxv),
exacts [false.elim (hxu' hxu), or.inr ⟨⟨hxv, hxu'⟩, hx⟩] },
{ rcases hx; exact hx.2 } } },
end
lemma add_right [t2_space M] [has_continuous_add N] (h₁ : v ⟂ᵥ w₁) (h₂ : v ⟂ᵥ w₂) : v ⟂ᵥ w₁ + w₂ :=
(add_left h₁.symm h₂.symm).symm
lemma smul_right {R : Type*} [semiring R] [distrib_mul_action R N] [has_continuous_const_smul R N]
(r : R) (h : v ⟂ᵥ w) : v ⟂ᵥ r • w :=
let ⟨s, hmeas, hs₁, hs₂⟩ := h in
⟨s, hmeas, hs₁, λ t ht, by simp only [coe_smul, pi.smul_apply, hs₂ t ht, smul_zero]⟩
lemma smul_left {R : Type*} [semiring R] [distrib_mul_action R M] [has_continuous_const_smul R M]
(r : R) (h : v ⟂ᵥ w) : r • v ⟂ᵥ w :=
(smul_right r h.symm).symm
lemma neg_left {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M]
{v : vector_measure α M} {w : vector_measure α N} (h : v ⟂ᵥ w) : -v ⟂ᵥ w :=
begin
obtain ⟨u, hmu, hu₁, hu₂⟩ := h,
refine ⟨u, hmu, λ s hs, _, hu₂⟩,
rw [neg_apply v s, neg_eq_zero],
exact hu₁ s hs
end
lemma neg_right {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N]
{v : vector_measure α M} {w : vector_measure α N} (h : v ⟂ᵥ w) : v ⟂ᵥ -w :=
h.symm.neg_left.symm
@[simp]
lemma neg_left_iff {M : Type*} [add_comm_group M] [topological_space M] [topological_add_group M]
{v : vector_measure α M} {w : vector_measure α N} :
-v ⟂ᵥ w ↔ v ⟂ᵥ w :=
⟨λ h, neg_neg v ▸ h.neg_left, neg_left⟩
@[simp]
lemma neg_right_iff {N : Type*} [add_comm_group N] [topological_space N] [topological_add_group N]
{v : vector_measure α M} {w : vector_measure α N} :
v ⟂ᵥ -w ↔ v ⟂ᵥ w :=
⟨λ h, neg_neg w ▸ h.neg_right, neg_right⟩
end mutually_singular
section trim
omit m
/-- Restriction of a vector measure onto a sub-σ-algebra. -/
@[simps] def trim {m n : measurable_space α} (v : vector_measure α M) (hle : m ≤ n) :
@vector_measure α m M _ _ :=
{ measure_of' := λ i, if measurable_set[m] i then v i else 0,
empty' := by rw [if_pos measurable_set.empty, v.empty],
not_measurable' := λ i hi, by rw if_neg hi,
m_Union' := λ f hf₁ hf₂,
begin
have hf₁' : ∀ k, measurable_set[n] (f k) := λ k, hle _ (hf₁ k),
convert v.m_Union hf₁' hf₂,
{ ext n, rw if_pos (hf₁ n) },
{ rw if_pos (@measurable_set.Union _ _ m _ _ hf₁) }
end }
variables {n : measurable_space α} {v : vector_measure α M}
lemma trim_eq_self : v.trim le_rfl = v :=
begin
ext1 i hi,
exact if_pos hi,
end
@[simp] lemma zero_trim (hle : m ≤ n) :
(0 : vector_measure α M).trim hle = 0 :=
begin
ext1 i hi,
exact if_pos hi,
end
lemma trim_measurable_set_eq (hle : m ≤ n) {i : set α} (hi : measurable_set[m] i) :
v.trim hle i = v i :=
if_pos hi
lemma restrict_trim (hle : m ≤ n) {i : set α} (hi : measurable_set[m] i) :
@vector_measure.restrict α m M _ _ (v.trim hle) i = (v.restrict i).trim hle :=
begin
ext j hj,
rw [restrict_apply, trim_measurable_set_eq hle hj, restrict_apply, trim_measurable_set_eq],
all_goals { measurability }
end
end trim
end
end vector_measure
namespace signed_measure
open vector_measure
open_locale measure_theory
include m
/-- The underlying function for `signed_measure.to_measure_of_zero_le`. -/
def to_measure_of_zero_le' (s : signed_measure α) (i : set α) (hi : 0 ≤[i] s)
(j : set α) (hj : measurable_set j) : ℝ≥0∞ :=
@coe ℝ≥0 ℝ≥0∞ _ ⟨s.restrict i j, le_trans (by simp) (hi j hj)⟩
/-- Given a signed measure `s` and a positive measurable set `i`, `to_measure_of_zero_le`
provides the measure, mapping measurable sets `j` to `s (i ∩ j)`. -/
def to_measure_of_zero_le (s : signed_measure α) (i : set α)
(hi₁ : measurable_set i) (hi₂ : 0 ≤[i] s) : measure α :=
measure.of_measurable (s.to_measure_of_zero_le' i hi₂)
(by { simp_rw [to_measure_of_zero_le', s.restrict_apply hi₁ measurable_set.empty,
set.empty_inter i, s.empty], refl })
begin
intros f hf₁ hf₂,
have h₁ : ∀ n, measurable_set (i ∩ f n) := λ n, hi₁.inter (hf₁ n),
have h₂ : pairwise (disjoint on λ (n : ℕ), i ∩ f n),
{ intros n m hnm,
exact (((hf₂ hnm).inf_left' i).inf_right' i) },
simp only [to_measure_of_zero_le', s.restrict_apply hi₁ (measurable_set.Union hf₁),
set.inter_comm, set.inter_Union, s.of_disjoint_Union_nat h₁ h₂,
ennreal.some_eq_coe, id.def],
have h : ∀ n, 0 ≤ s (i ∩ f n) :=
λ n, s.nonneg_of_zero_le_restrict
(s.zero_le_restrict_subset hi₁ (inter_subset_left _ _) hi₂),
rw [nnreal.coe_tsum_of_nonneg h, ennreal.coe_tsum],
{ refine tsum_congr (λ n, _),
simp_rw [s.restrict_apply hi₁ (hf₁ n), set.inter_comm] },
{ exact (nnreal.summable_coe_of_nonneg h).2 (s.m_Union h₁ h₂).summable }
end
variables (s : signed_measure α) {i j : set α}
lemma to_measure_of_zero_le_apply (hi : 0 ≤[i] s)
(hi₁ : measurable_set i) (hj₁ : measurable_set j) :
s.to_measure_of_zero_le i hi₁ hi j =
@coe ℝ≥0 ℝ≥0∞ _ ⟨s (i ∩ j), nonneg_of_zero_le_restrict s
(zero_le_restrict_subset s hi₁ (set.inter_subset_left _ _) hi)⟩ :=
by simp_rw [to_measure_of_zero_le, measure.of_measurable_apply _ hj₁, to_measure_of_zero_le',
s.restrict_apply hi₁ hj₁, set.inter_comm]
/-- Given a signed measure `s` and a negative measurable set `i`, `to_measure_of_le_zero`
provides the measure, mapping measurable sets `j` to `-s (i ∩ j)`. -/
def to_measure_of_le_zero (s : signed_measure α) (i : set α) (hi₁ : measurable_set i)
(hi₂ : s ≤[i] 0) : measure α :=
to_measure_of_zero_le (-s) i hi₁ $ (@neg_zero (vector_measure α ℝ) _) ▸ neg_le_neg _ _ hi₁ hi₂
lemma to_measure_of_le_zero_apply (hi : s ≤[i] 0)
(hi₁ : measurable_set i) (hj₁ : measurable_set j) :
s.to_measure_of_le_zero i hi₁ hi j =
@coe ℝ≥0 ℝ≥0∞ _ ⟨-s (i ∩ j), neg_apply s (i ∩ j) ▸ nonneg_of_zero_le_restrict _
(zero_le_restrict_subset _ hi₁ (set.inter_subset_left _ _)
((@neg_zero (vector_measure α ℝ) _) ▸ neg_le_neg _ _ hi₁ hi))⟩ :=
begin
erw [to_measure_of_zero_le_apply],
{ simp },
{ assumption },
end
/-- `signed_measure.to_measure_of_zero_le` is a finite measure. -/
instance to_measure_of_zero_le_finite (hi : 0 ≤[i] s) (hi₁ : measurable_set i) :
is_finite_measure (s.to_measure_of_zero_le i hi₁ hi) :=
{ measure_univ_lt_top :=
begin
rw [to_measure_of_zero_le_apply s hi hi₁ measurable_set.univ],
exact ennreal.coe_lt_top,
end }
/-- `signed_measure.to_measure_of_le_zero` is a finite measure. -/
instance to_measure_of_le_zero_finite (hi : s ≤[i] 0) (hi₁ : measurable_set i) :
is_finite_measure (s.to_measure_of_le_zero i hi₁ hi) :=
{ measure_univ_lt_top :=
begin
rw [to_measure_of_le_zero_apply s hi hi₁ measurable_set.univ],
exact ennreal.coe_lt_top,
end }
lemma to_measure_of_zero_le_to_signed_measure (hs : 0 ≤[univ] s) :
(s.to_measure_of_zero_le univ measurable_set.univ hs).to_signed_measure = s :=
begin
ext i hi,
simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_zero_le_apply _ _ _ hi],
end
lemma to_measure_of_le_zero_to_signed_measure (hs : s ≤[univ] 0) :
(s.to_measure_of_le_zero univ measurable_set.univ hs).to_signed_measure = -s :=
begin
ext i hi,
simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_le_zero_apply _ _ _ hi],
end
end signed_measure
namespace measure
open vector_measure
variables (μ : measure α) [is_finite_measure μ]
lemma zero_le_to_signed_measure : 0 ≤ μ.to_signed_measure :=
begin
rw ← le_restrict_univ_iff_le,
refine restrict_le_restrict_of_subset_le _ _ (λ j hj₁ _, _),
simp only [measure.to_signed_measure_apply_measurable hj₁, coe_zero, pi.zero_apply,
ennreal.to_real_nonneg, vector_measure.coe_zero]
end
lemma to_signed_measure_to_measure_of_zero_le :
μ.to_signed_measure.to_measure_of_zero_le univ measurable_set.univ
((le_restrict_univ_iff_le _ _).2 (zero_le_to_signed_measure μ)) = μ :=
begin
refine measure.ext (λ i hi, _),
lift μ i to ℝ≥0 using (measure_lt_top _ _).ne with m hm,
simp [signed_measure.to_measure_of_zero_le_apply _ _ _ hi,
measure.to_signed_measure_apply_measurable hi, ← hm],
end
end measure
end measure_theory
|
671aca91d6bb802fd745954f787a957c46fb767c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/meta/coinductive_predicates.lean | 3470062a6941974a2b83a8f0aa7482b93f3820e5 | [] | 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,557 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl (CMU)
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.core
import Mathlib.PostPort
universes u
namespace Mathlib
theorem monotonicity.pi {α : Sort u} {p : α → Prop} {q : α → Prop} (h : ∀ (a : α), implies (p a) (q a)) : implies (∀ (a : α), p a) (∀ (a : α), q a) :=
fun (h' : ∀ (a : α), p a) (a : α) => h a (h' a)
theorem monotonicity.imp {p : Prop} {p' : Prop} {q : Prop} {q' : Prop} (h₁ : implies p' q') (h₂ : implies q p) : implies (p → p') (q → q') :=
fun (h : p → p') => h₁ ∘ h ∘ h₂
theorem monotonicity.const (p : Prop) : implies p p :=
id
theorem monotonicity.true (p : Prop) : implies p True :=
fun (_x : p) => trivial
theorem monotonicity.false (p : Prop) : implies False p :=
false.elim
theorem monotonicity.exists {α : Sort u} {p : α → Prop} {q : α → Prop} (h : ∀ (a : α), implies (p a) (q a)) : implies (∃ (a : α), p a) (∃ (a : α), q a) :=
exists_imp_exists h
theorem monotonicity.and {p : Prop} {p' : Prop} {q : Prop} {q' : Prop} (hp : implies p p') (hq : implies q q') : implies (p ∧ q) (p' ∧ q') :=
and.imp hp hq
theorem monotonicity.or {p : Prop} {p' : Prop} {q : Prop} {q' : Prop} (hp : implies p p') (hq : implies q q') : implies (p ∨ q) (p' ∨ q') :=
or.imp hp hq
theorem monotonicity.not {p : Prop} {q : Prop} (h : implies p q) : implies (¬q) (¬p) :=
mt h
|
c7b879f3ad58a688c56b4bbc21c9363296962966 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/ftree_brec.lean | 785915542ba67081c9e5c6fbbbfabe6c2ba9202d | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 5,000 | lean | import data.prod
inductive ftree (A : Type) (B : Type) : Type :=
| leafa : ftree A B
| node : (A → B → ftree A B) → (B → ftree A B) → ftree A B
set_option pp.universes true
check @ftree
namespace ftree
namespace manual
definition below.{l l₁ l₂} {A : Type.{l₁}} {B : Type.{l₂}} (C : ftree A B → Type.{l+1}) (t : ftree A B)
: Type.{max l₁ l₂ (l+1)} :=
@ftree.rec.{(max l₁ l₂ (l+1))+1 l₁ l₂}
A
B
(λ t : ftree A B, Type.{max l₁ l₂ (l+1)})
poly_unit.{max l₁ l₂ (l+1)}
(λ (f₁ : A → B → ftree A B)
(f₂ : B → ftree A B)
(r₁ : Π (a : A) (b : B), Type.{max l₁ l₂ (l+1)})
(r₂ : Π (b : B), Type.{max l₁ l₂ (l+1)}),
let fc₁ : Type.{max l₁ l₂ (l+1)} := Π (a : A) (b : B), C (f₁ a b) in
let fc₂ : Type.{max l₂ l+1} := Π (b : B), C (f₂ b) in
let fr₁ : Type.{max l₁ l₂ (l+1)} := Π (a : A) (b : B), r₁ a b in
let fr₂ : Type.{max l₁ l₂ (l+1)} := Π (b : B), r₂ b in
let p₁ : Type.{max l₁ l₂ (l+1)} := prod.{(max l₁ l₂ (l+1)) (max l₁ l₂ (l+1))} fc₁ fr₁ in
let p₂ : Type.{max l₁ l₂ (l+1)} := prod.{(max l₂ (l+1)) (max l₁ l₂ (l+1))} fc₂ fr₂ in
prod.{(max l₁ l₂ (l+1)) (max l₁ l₂ (l+1))} p₁ p₂)
t
definition pbelow.{l₁ l₂} {A : Type.{l₁}} {B : Type.{l₂}} (C : ftree A B → Prop) (t : ftree A B)
: Prop :=
@ftree.rec.{1 l₁ l₂}
A
B
(λ t : ftree A B, Prop)
true
(λ (f₁ : A → B → ftree A B)
(f₂ : B → ftree A B)
(r₁ : Π (a : A) (b : B), Prop)
(r₂ : Π (b : B), Prop),
let fc₁ : Prop := ∀ (a : A) (b : B), C (f₁ a b) in
let fc₂ : Prop := ∀ (b : B), C (f₂ b) in
let fr₁ : Prop := ∀ (a : A) (b : B), r₁ a b in
let fr₂ : Prop := ∀ (b : B), r₂ b in
let p₁ : Prop := fc₁ ∧ fr₁ in
let p₂ : Prop := fc₂ ∧ fr₂ in
p₁ ∧ p₂)
t
definition brec_on.{l l₁ l₂} {A : Type.{l₁}} {B : Type.{l₂}} {C : ftree A B → Type.{l+1}}
(t : ftree A B)
(F : Π (t : ftree A B), @below A B C t → C t)
: C t :=
have gen : prod.{(l+1) (max l₁ l₂ (l+1))} (C t) (@below A B C t), from
@ftree.rec.{(max l₁ l₂ (l+1)) l₁ l₂}
A
B
(λ t : ftree A B, prod.{(l+1) (max l₁ l₂ (l+1))} (C t) (@below A B C t))
(have b : @below A B C (leafa A B), from
poly_unit.star.{max l₁ l₂ (l+1)},
have c : C (leafa A B), from
F (leafa A B) b,
prod.mk.{(l+1) (max l₁ l₂ (l+1))} c b)
(λ (f₁ : A → B → ftree A B)
(f₂ : B → ftree A B)
(r₁ : Π (a : A) (b : B), prod.{(l+1) (max l₁ l₂ (l+1))} (C (f₁ a b)) (@below A B C (f₁ a b)))
(r₂ : Π (b : B), prod.{(l+1) (max l₁ l₂ (l+1))} (C (f₂ b)) (@below A B C (f₂ b))),
let fc₁ : Π (a : A) (b : B), C (f₁ a b) := λ (a : A) (b : B), prod.pr1 (r₁ a b) in
let fr₁ : Π (a : A) (b : B), @below A B C (f₁ a b) := λ (a : A) (b : B), prod.pr2 (r₁ a b) in
let fc₂ : Π (b : B), C (f₂ b) := λ (b : B), prod.pr1 (r₂ b) in
let fr₂ : Π (b : B), @below A B C (f₂ b) := λ (b : B), prod.pr2 (r₂ b) in
have b : @below A B C (node f₁ f₂), from
prod.mk (prod.mk fc₁ fr₁) (prod.mk fc₂ fr₂),
have c : C (node f₁ f₂), from
F (node f₁ f₂) b,
prod.mk c b)
t,
prod.pr1 gen
definition binduction_on.{l₁ l₂} {A : Type.{l₁}} {B : Type.{l₂}} {C : ftree A B → Prop}
(t : ftree A B)
(F : Π (t : ftree A B), @ftree.ibelow A B C t → C t)
: C t :=
have gen : C t ∧ @ftree.ibelow A B C t, from
@ftree.rec.{0 l₁ l₂}
A
B
(λ t : ftree A B, C t ∧ @ftree.ibelow A B C t)
(have b : @ftree.ibelow A B C (leafa A B), from
true.intro,
have c : C (leafa A B), from
F (leafa A B) b,
and.intro c b)
(λ (f₁ : A → B → ftree A B)
(f₂ : B → ftree A B)
(r₁ : ∀ (a : A) (b : B), C (f₁ a b) ∧ @ftree.ibelow A B C (f₁ a b))
(r₂ : ∀ (b : B), C (f₂ b) ∧ @ftree.ibelow A B C (f₂ b)),
let fc₁ : ∀ (a : A) (b : B), C (f₁ a b) := λ (a : A) (b : B), and.elim_left (r₁ a b) in
let fr₁ : ∀ (a : A) (b : B), @ftree.ibelow A B C (f₁ a b) := λ (a : A) (b : B), and.elim_right (r₁ a b) in
let fc₂ : ∀ (b : B), C (f₂ b) := λ (b : B), and.elim_left (r₂ b) in
let fr₂ : ∀ (b : B), @ftree.ibelow A B C (f₂ b) := λ (b : B), and.elim_right (r₂ b) in
have b : @ftree.ibelow A B C (node f₁ f₂), from
and.intro (and.intro fc₁ fr₁) (and.intro fc₂ fr₂),
have c : C (node f₁ f₂), from
F (node f₁ f₂) b,
and.intro c b)
t,
and.elim_left gen
end manual
end ftree
|
a42b38a9fc3376b0cd30d38eefb97ad96eba1442 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/algebra/algebra/basic.lean | 89dddf970e9568be9735a128b0bef020ecf50a50 | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 53,371 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import algebra.iterate_hom
import data.equiv.ring_aut
import algebra.module.basic
import linear_algebra.basic
import tactic.abel
/-!
# Algebras over commutative semirings
In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`,
and algebra equivalences `alg_equiv`.
We also define the 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 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`.
See note [reducible non-instances]. -/
@[reducible]
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`.
See note [reducible non-instances]. -/
@[reducible]
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]
/-- We keep this lemma private because it picks up the `algebra.to_has_scalar` instance
which we set to priority 0 shortly. See `smul_def` below for the public version. -/
private 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 [←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.
-- Unfortunately, leaving it in place causes deterministic timeouts later in mathlib.
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
/-- `mul_comm` for `algebra`s when one element is from the base ring. -/
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
/-- `mul_left_comm` for `algebra`s when one element is from the base ring. -/
theorem left_comm (x : A) (r : R) (y : A) :
x * (algebra_map R A r * y) = algebra_map R A r * (x * y) :=
by rw [← mul_assoc, ← commutes, mul_assoc]
/-- `mul_right_comm` for `algebra`s when one element is from the base ring. -/
theorem right_comm (x : A) (r : R) (y : A) :
(x * algebra_map R A r) * y = (x * y) * algebra_map R A r :=
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) = bit0 (r • (1 : A)) :=
by simp [bit0, add_smul]
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) = bit1 (r • (1 : A)) :=
by simp [bit1, add_smul]
lemma bit1_smul_one' : bit1 r • (1 : A) = r • 2 + 1 :=
by simp [bit1, bit0, 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_id : algebra_map R R = ring_hom.id _ := rfl
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
/-- 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.
See note [reducible non-instances]. -/
@[reducible]
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
end ring
end algebra
namespace no_zero_smul_divisors
variables {R A : Type*}
open algebra
section ring
variables [comm_ring R]
/-- 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 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 _)⟩
variables (R A)
lemma algebra_map_injective [ring A] [nontrivial A]
[algebra R A] [no_zero_smul_divisors R A] :
function.injective (algebra_map R A) :=
suffices function.injective (λ (c : R), c • (1 : A)),
by { convert this, ext, rw [algebra.smul_def, mul_one] },
smul_left_injective R one_ne_zero
variables {R A}
lemma iff_algebra_map_injective [ring A] [is_domain A] [algebra R A] :
no_zero_smul_divisors R A ↔ function.injective (algebra_map R A) :=
⟨@@no_zero_smul_divisors.algebra_map_injective R A _ _ _ _,
no_zero_smul_divisors.of_algebra_map_injective⟩
end ring
section field
variables [field R] [semiring A] [algebra R A]
@[priority 100] -- see note [lower instance priority]
instance algebra.no_zero_smul_divisors [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 no_zero_smul_divisors
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, opposite.rec $ λ 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 : algebra R (module.End R M) :=
algebra.of_module smul_mul_assoc (λ r f g, (smul_comm r f g).symm)
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
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) (λ _, A → B) := ⟨alg_hom.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
-- make the coercion the simp-normal form
@[simp] lemma to_ring_hom_eq_coe (f : A →ₐ[R] B) : f.to_ring_hom = 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_injective : @function.injective (A →ₐ[R] B) (A → B) coe_fn :=
by { intros φ₁ φ₂ H, cases φ₁, cases φ₂, congr, exact H }
theorem coe_fn_inj {φ₁ φ₂ : A →ₐ[R] B} : (φ₁ : A → B) = φ₂ ↔ φ₁ = φ₂ := coe_fn_injective.eq_iff
theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_injective $ 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_injective $ 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 →ₗ[R] 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_injective : function.injective (to_linear_map : _ → (A →ₗ[R] B)) :=
λ φ₁ φ₂ h, ext $ linear_map.congr_fun 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
@[simp] lemma to_linear_map_id : to_linear_map (alg_hom.id R A) = linear_map.id :=
linear_map.ext $ λ _, rfl
/-- Promote a `linear_map` to an `alg_hom` by supplying proofs about the behavior on `1` and `*`. -/
@[simps]
def of_linear_map (f : A →ₗ[R] B) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) :
A →ₐ[R] B :=
{ to_fun := f,
map_one' := map_one,
map_mul' := map_mul,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, f.map_smul, map_one],
.. f.to_add_monoid_hom }
@[simp] lemma of_linear_map_to_linear_map (map_one) (map_mul) :
of_linear_map φ.to_linear_map map_one map_mul = φ :=
by { ext, refl }
@[simp] lemma to_linear_map_of_linear_map (f : A →ₗ[R] B) (map_one) (map_mul) :
to_linear_map (of_linear_map f map_one map_mul) = f :=
by { ext, refl }
@[simp] lemma of_linear_map_id (map_one) (map_mul) :
of_linear_map linear_map.id map_one map_mul = alg_hom.id R A :=
ext $ λ _, rfl
lemma map_list_prod (s : list A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_list_prod s
section prod
/-- First projection as `alg_hom`. -/
def fst : A × B →ₐ[R] A :=
{ commutes' := λ r, rfl, .. ring_hom.fst A B}
/-- Second projection as `alg_hom`. -/
def snd : A × B →ₐ[R] B :=
{ commutes' := λ r, rfl, .. ring_hom.snd A B}
end prod
lemma algebra_map_eq_apply (f : A →ₐ[R] B) {y : R} {x : A} (h : algebra_map R A y = x) :
algebra_map R B y = f x :=
h ▸ (f.commutes _).symm
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₂) (λ _, A₁ → 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
@[simp] lemma to_ring_equiv_eq_coe : e.to_ring_equiv = e := rfl
@[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ ≃+* A₂)) :=
λ e₁ e₂ h, ext $ ring_equiv.congr_fun h
@[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
lemma coe_alg_hom_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ →ₐ[R] A₂)) :=
λ e₁ e₂ h, ext $ alg_hom.congr_fun h
/-- 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 symm_trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₃) :
(e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl
@[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₂ :=
{ to_fun := f,
inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
lemma coe_alg_hom_of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
↑(of_alg_hom f g h₁ h₂) = f := alg_hom.ext $ λ _, rfl
@[simp]
lemma of_alg_hom_coe_alg_hom (f : A₁ ≃ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
of_alg_hom ↑f g h₁ h₂ = f := ext $ λ _, rfl
lemma of_alg_hom_symm (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
(of_alg_hom f g h₁ h₂).symm = of_alg_hom g f h₂ h₁ := rfl
/-- 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. -/
@[simps apply] def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ :=
{ to_fun := e,
map_smul' := λ r x, by simp [algebra.smul_def],
inv_fun := e.symm,
.. e }
@[simp] lemma to_linear_equiv_refl :
(alg_equiv.refl : A₁ ≃ₐ[R] A₁).to_linear_equiv = linear_equiv.refl R A₁ := rfl
@[simp] lemma to_linear_equiv_symm (e : A₁ ≃ₐ[R] A₂) :
e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl
@[simp] lemma to_linear_equiv_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
(e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := rfl
theorem to_linear_equiv_injective : function.injective (to_linear_equiv : _ → (A₁ ≃ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_equiv.congr_fun 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_injective : function.injective (to_linear_map : _ → (A₁ →ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_map.congr_fun 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.
-/
@[simps apply]
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_symm :
(of_linear_equiv l map_mul commutes).symm = of_linear_equiv l.symm
((of_linear_equiv l map_mul commutes).symm.map_mul)
((of_linear_equiv l map_mul commutes).symm.commutes) :=
rfl
@[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 }
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
/-- The tautological action by `A₁ ≃ₐ[R] A₁` on `A₁`.
This generalizes `function.End.apply_mul_action`. -/
instance apply_mul_semiring_action : mul_semiring_action (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul := ($),
smul_zero := alg_equiv.map_zero,
smul_add := alg_equiv.map_add,
smul_one := alg_equiv.map_one,
smul_mul := alg_equiv.map_mul,
one_smul := λ _, rfl,
mul_smul := λ _ _ _, rfl }
@[simp] protected lemma smul_def (f : A₁ ≃ₐ[R] A₁) (a : A₁) : f • a = f a := rfl
instance apply_has_faithful_scalar : has_faithful_scalar (A₁ ≃ₐ[R] A₁) A₁ :=
⟨λ _ _, alg_equiv.ext⟩
instance apply_smul_comm_class : smul_comm_class R (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul_comm := λ r e a, (e.to_linear_equiv.map_smul r a).symm }
instance apply_smul_comm_class' : smul_comm_class (A₁ ≃ₐ[R] A₁) R A₁ :=
{ smul_comm := λ e r a, (e.to_linear_equiv.map_smul r a) }
@[simp] lemma algebra_map_eq_apply (e : A₁ ≃ₐ[R] A₂) {y : R} {x : A₁} :
(algebra_map R A₂ y = e x) ↔ (algebra_map R A₁ y = x) :=
⟨λ h, by simpa using e.symm.to_alg_hom.algebra_map_eq_apply h,
λ h, e.to_alg_hom.algebra_map_eq_apply h⟩
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 mul_semiring_action
variables {M G : Type*} (R A : Type*) [comm_semiring R] [semiring A] [algebra R A]
section
variables [monoid M] [mul_semiring_action M A] [smul_comm_class M R A]
/-- Each element of the monoid defines a algebra homomorphism.
This is a stronger version of `mul_semiring_action.to_ring_hom` and
`distrib_mul_action.to_linear_map`. -/
@[simps]
def to_alg_hom (m : M) : A →ₐ[R] A :=
alg_hom.mk' (mul_semiring_action.to_ring_hom _ _ m) (smul_comm _)
theorem to_alg_hom_injective [has_faithful_scalar M A] :
function.injective (mul_semiring_action.to_alg_hom R A : M → A →ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_hom.ext_iff.1 h r
end
section
variables [group G] [mul_semiring_action G A] [smul_comm_class G R A]
/-- Each element of the group defines a algebra equivalence.
This is a stronger version of `mul_semiring_action.to_ring_equiv` and
`distrib_mul_action.to_linear_equiv`. -/
@[simps]
def to_alg_equiv (g : G) : A ≃ₐ[R] A :=
{ .. mul_semiring_action.to_ring_equiv _ _ g,
.. mul_semiring_action.to_alg_hom R A g }
theorem to_alg_equiv_injective [has_faithful_scalar G A] :
function.injective (mul_semiring_action.to_alg_equiv R A : G → A ≃ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_equiv.ext_iff.1 h r
end
end mul_semiring_action
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 char_zero
variables {R : Type*} (S : Type*) [comm_semiring R] [semiring S] [algebra R S]
lemma of_algebra [char_zero S] : char_zero R :=
⟨begin
suffices : function.injective (algebra_map R S ∘ coe),
{ exact this.of_comp },
convert char_zero.cast_injective,
ext n,
rw [function.comp_app, ← (algebra_map ℕ _).eq_nat_cast, ← ring_hom.comp_apply,
ring_hom.eq_nat_cast],
all_goals { apply_instance }
end⟩
end char_zero
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
end algebra
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' := λ _ _, zsmul_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)) : R →+* Π i : I, 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}
variables (A B : Type*) [comm_semiring R] [semiring B] [algebra R B]
/-- `function.const` as an `alg_hom`. The name matches `pi.const_ring_hom`, `pi.const_monoid_hom`,
etc. -/
@[simps]
def const_alg_hom : B →ₐ[R] (A → B) :=
{ to_fun := function.const _,
commutes' := λ r, rfl,
.. pi.const_ring_hom A B}
/-- When `R` is commutative and permits an `algebra_map`, `pi.const_ring_hom` is equal to that
map. -/
@[simp] lemma const_ring_hom_eq_algebra_map : const_ring_hom A R = algebra_map R (A → R) :=
rfl
@[simp] lemma const_alg_hom_eq_algebra_of_id : const_alg_hom R A R = algebra.of_id R (A → R) :=
rfl
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
/-! 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
variables (R S M)
/-- 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
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
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {I : Type*}
variables [comm_semiring R] [semiring A] [semiring B]
variables [algebra R A] [algebra R B]
/-- `R`-algebra homomorphism between the function spaces `I → A` and `I → B`, induced by an
`R`-algebra homomorphism `f` between `A` and `B`. -/
@[simps] protected def comp_left (f : A →ₐ[R] B) (I : Type*) : (I → A) →ₐ[R] (I → B) :=
{ to_fun := λ h, f ∘ h,
commutes' := λ c, by { ext, exact f.commutes' c },
.. f.to_ring_hom.comp_left I }
end alg_hom
|
b1afd4b1733921bf31c4fbe2c95121f62ddebada | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/linear_algebra/basic.lean | 9a638bdbdfb76c6bbb1d8c33954cfed6767530b4 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 62,571 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard
Basics of linear algebra. This sets up the "categorical/lattice structure" of
modules, submodules, and linear maps.
-/
import algebra.pi_instances data.finsupp data.equiv.algebra order.order_iso
open function lattice
reserve infix ` ≃ₗ `:25
universes u v w x y z
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ε : Type z} {ι : Type x}
namespace finset
lemma smul_sum [ring γ] [add_comm_group β] [module γ β]
{s : finset α} {a : γ} {f : α → β} :
a • (s.sum f) = s.sum (λc, a • f c) :=
(finset.sum_hom ((•) a)).symm
end finset
namespace finsupp
lemma smul_sum [has_zero β] [ring γ] [add_comm_group δ] [module γ δ]
{v : α →₀ β} {c : γ} {h : α → β → δ} :
c • (v.sum h) = v.sum (λa b, c • h a b) :=
finset.smul_sum
end finsupp
namespace linear_map
section
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [add_comm_group ε]
variables [module α β] [module α γ] [module α δ] [module α ε]
variables (f g : β →ₗ[α] γ)
include α
@[simp] theorem comp_id : f.comp id = f :=
linear_map.ext $ λ x, rfl
@[simp] theorem id_comp : id.comp f = f :=
linear_map.ext $ λ x, rfl
theorem comp_assoc (g : γ →ₗ[α] δ) (h : δ →ₗ[α] ε) : (h.comp g).comp f = h.comp (g.comp f) :=
rfl
def cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h : ∀c, f c ∈ p) : γ →ₗ[α] p :=
by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp
@[simp] theorem cod_restrict_apply (p : submodule α β) (f : γ →ₗ[α] β) {h} (x : γ) :
(cod_restrict p f h x : β) = f x := rfl
@[simp] lemma comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) (g : δ →ₗ[α] β) :
(cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) :=
ext $ assume b, rfl
@[simp] lemma subtype_comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) :
p.subtype.comp (cod_restrict p f h) = f :=
ext $ assume b, rfl
def inverse (g : γ → β) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : γ →ₗ[α] β :=
by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact
⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂],
λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩
instance : has_zero (β →ₗ[α] γ) := ⟨⟨λ _, 0, by simp, by simp⟩⟩
@[simp] lemma zero_apply (x : β) : (0 : β →ₗ[α] γ) x = 0 := rfl
instance : has_neg (β →ₗ[α] γ) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩
@[simp] lemma neg_apply (x : β) : (- f) x = - f x := rfl
instance : has_add (β →ₗ[α] γ) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩
@[simp] lemma add_apply (x : β) : (f + g) x = f x + g x := rfl
instance : add_comm_group (β →ₗ[α] γ) :=
by refine {zero := 0, add := (+), neg := has_neg.neg, ..};
intros; ext; simp
instance linear_map.is_add_group_hom : is_add_group_hom f :=
{ map_add := f.add }
instance linear_map_apply_is_add_group_hom (a : β) :
is_add_group_hom (λ f : β →ₗ[α] γ, f a) :=
{ map_add := λ f g, linear_map.add_apply f g a }
lemma sum_apply [decidable_eq δ] (t : finset δ) (f : δ → β →ₗ[α] γ) (b : β) :
t.sum f b = t.sum (λd, f d b) :=
(@finset.sum_hom _ _ _ t f _ _ (λ g : β →ₗ[α] γ, g b) _).symm
@[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl
def smul_right (f : γ →ₗ[α] α) (x : β) : γ →ₗ[α] β :=
⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩.
@[simp] theorem smul_right_apply (f : γ →ₗ[α] α) (x : β) (c : γ) :
(smul_right f x : γ → β) c = f c • x := rfl
instance : has_one (β →ₗ[α] β) := ⟨linear_map.id⟩
instance : has_mul (β →ₗ[α] β) := ⟨linear_map.comp⟩
@[simp] lemma one_app (x : β) : (1 : β →ₗ[α] β) x = x := rfl
@[simp] lemma mul_app (A B : β →ₗ[α] β) (x : β) : (A * B) x = A (B x) := rfl
@[simp] theorem comp_zero : f.comp (0 : δ →ₗ[α] β) = 0 :=
ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero]
@[simp] theorem zero_comp : (0 : γ →ₗ[α] δ).comp f = 0 :=
rfl
section
variables (α β)
include β
instance endomorphism_ring : ring (β →ₗ[α] β) :=
by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..};
{ intros, apply linear_map.ext, simp }
end
section
variables (α β γ)
def fst : β × γ →ₗ[α] β := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩
def snd : β × γ →ₗ[α] γ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩
end
@[simp] theorem fst_apply (x : β × γ) : fst α β γ x = x.1 := rfl
@[simp] theorem snd_apply (x : β × γ) : snd α β γ x = x.2 := rfl
def pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : β →ₗ[α] γ × δ :=
⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩
@[simp] theorem pair_apply (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (x : β) :
pair f g x = (f x, g x) := rfl
@[simp] theorem fst_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) :
(fst α γ δ).comp (pair f g) = f := by ext; refl
@[simp] theorem snd_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) :
(snd α γ δ).comp (pair f g) = g := by ext; refl
@[simp] theorem pair_fst_snd : pair (fst α β γ) (snd α β γ) = linear_map.id :=
by ext; refl
section
variables (α β γ)
def inl : β →ₗ[α] β × γ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl]
def inr : γ →ₗ[α] β × γ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr]
end
@[simp] theorem inl_apply (x : β) : inl α β γ x = (x, 0) := rfl
@[simp] theorem inr_apply (x : γ) : inr α β γ x = (0, x) := rfl
def copair (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : β × γ →ₗ[α] δ :=
⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩
@[simp] theorem copair_apply (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (x : β) (y : γ) :
copair f g (x, y) = f x + g y := rfl
@[simp] theorem copair_inl (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) :
(copair f g).comp (inl α β γ) = f := by ext; simp
@[simp] theorem copair_inr (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) :
(copair f g).comp (inr α β γ) = g := by ext; simp
@[simp] theorem copair_inl_inr : copair (inl α β γ) (inr α β γ) = linear_map.id :=
by ext ⟨x, y⟩; simp
theorem fst_eq_copair : fst α β γ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp
theorem snd_eq_copair : snd α β γ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp
theorem inl_eq_pair : inl α β γ = pair linear_map.id 0 := rfl
theorem inr_eq_pair : inr α β γ = pair 0 linear_map.id := rfl
end
section comm_ring
variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
variables (f g : β →ₗ[α] γ)
include α
instance : has_scalar α (β →ₗ[α] γ) := ⟨λ a f,
⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩
@[simp] lemma smul_apply (a : α) (x : β) : (a • f) x = a • f x := rfl
instance : module α (β →ₗ[α] γ) :=
module.of_core $ by refine { smul := (•), ..};
intros; ext; simp [smul_add, add_smul, smul_smul]
def congr_right (f : γ →ₗ[α] δ) : (β →ₗ[α] γ) →ₗ[α] (β →ₗ[α] δ) :=
⟨linear_map.comp f,
λ _ _, linear_map.ext $ λ _, f.2 _ _,
λ _ _, linear_map.ext $ λ _, f.3 _ _⟩
theorem smul_comp (g : γ →ₗ[α] δ) (a : α) : (a • g).comp f = a • (g.comp f) :=
rfl
theorem comp_smul (g : γ →ₗ[α] δ) (a : α) : g.comp (a • f) = a • (g.comp f) :=
ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl
end comm_ring
end linear_map
namespace submodule
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
variables (p p' : submodule α β) (q q' : submodule α γ)
variables {r : α} {x y : β}
open set lattice
instance : partial_order (submodule α β) :=
partial_order.lift (coe : submodule α β → set β) (λ a b, ext') (by apply_instance)
lemma le_def {p p' : submodule α β} : p ≤ p' ↔ (p : set β) ⊆ p' := iff.rfl
lemma le_def' {p p' : submodule α β} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl
def of_le {p p' : submodule α β} (h : p ≤ p') : p →ₗ[α] p' :=
linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx
@[simp] theorem of_le_apply {p p' : submodule α β} (h : p ≤ p')
(x : p) : (of_le h x : β) = x := rfl
lemma subtype_comp_of_le (p q : submodule α β) (h : p ≤ q) :
(submodule.subtype q).comp (of_le h) = submodule.subtype p :=
by ext ⟨b, hb⟩; simp
instance : has_bot (submodule α β) :=
⟨by split; try {exact {0}}; simp {contextual := tt}⟩
@[simp] lemma bot_coe : ((⊥ : submodule α β) : set β) = {0} := rfl
section
variables (α)
@[simp] lemma mem_bot : x ∈ (⊥ : submodule α β) ↔ x = 0 := mem_singleton_iff
end
instance : order_bot (submodule α β) :=
{ bot := ⊥,
bot_le := λ p x, by simp {contextual := tt},
..submodule.partial_order }
instance : has_top (submodule α β) :=
⟨by split; try {exact set.univ}; simp⟩
@[simp] lemma top_coe : ((⊤ : submodule α β) : set β) = univ := rfl
@[simp] lemma mem_top : x ∈ (⊤ : submodule α β) := trivial
lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : α) = 1) : p = ⊥ :=
by ext x; simp [semimodule.eq_zero_of_zero_eq_one _ x zero_eq_one]
instance : order_top (submodule α β) :=
{ top := ⊤,
le_top := λ p x _, trivial,
..submodule.partial_order }
instance : has_Inf (submodule α β) :=
⟨λ S, {
carrier := ⋂ s ∈ S, ↑s,
zero := by simp,
add := by simp [add_mem] {contextual := tt},
smul := by simp [smul_mem] {contextual := tt} }⟩
private lemma Inf_le' {S : set (submodule α β)} {p} : p ∈ S → Inf S ≤ p :=
bInter_subset_of_mem
private lemma le_Inf' {S : set (submodule α β)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S :=
subset_bInter
instance : has_inf (submodule α β) :=
⟨λ p p', {
carrier := p ∩ p',
zero := by simp,
add := by simp [add_mem] {contextual := tt},
smul := by simp [smul_mem] {contextual := tt} }⟩
instance : complete_lattice (submodule α β) :=
{ sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha,
le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb,
sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩,
inf := (⊓),
le_inf := λ a b c, subset_inter,
inf_le_left := λ a b, inter_subset_left _ _,
inf_le_right := λ a b, inter_subset_right _ _,
Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t},
le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs,
Sup_le := λ s p hs, Inf_le' hs,
Inf := Inf,
le_Inf := λ s a, le_Inf',
Inf_le := λ s a, Inf_le',
..submodule.lattice.order_top,
..submodule.lattice.order_bot }
instance : add_comm_monoid (submodule α β) :=
{ add := (⊔),
add_assoc := λ _ _ _, sup_assoc,
zero := ⊥,
zero_add := λ _, bot_sup_eq,
add_zero := λ _, sup_bot_eq,
add_comm := λ _ _, sup_comm }
@[simp] lemma add_eq_sup (M N : submodule α β) : M + N = M ⊔ N := rfl
@[simp] lemma zero_eq_bot : (0 : submodule α β) = ⊥ := rfl
lemma eq_top_iff' {p : submodule α β} : p = ⊤ ↔ ∀ x, x ∈ p :=
eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩
@[simp] theorem inf_coe : (p ⊓ p' : set β) = p ∩ p' := rfl
@[simp] theorem mem_inf {p p' : submodule α β} :
x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
@[simp] theorem Inf_coe (P : set (submodule α β)) : (↑(Inf P) : set β) = ⋂ p ∈ P, ↑p := rfl
@[simp] theorem infi_coe {ι} (p : ι → submodule α β) :
(↑⨅ i, p i : set β) = ⋂ i, ↑(p i) :=
by rw [infi, Inf_coe]; ext a; simp; exact
⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩
@[simp] theorem mem_infi {ι} (p : ι → submodule α β) :
x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i :=
by rw [← mem_coe, infi_coe, mem_Inter]; refl
theorem disjoint_def {p p' : submodule α β} :
disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:β) :=
show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set β)) ↔ _, by simp
/-- The pushforward -/
def map (f : β →ₗ[α] γ) (p : submodule α β) : submodule α γ :=
{ carrier := f '' p,
zero := ⟨0, p.zero_mem, f.map_zero⟩,
add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩;
exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩,
smul := by rintro a _ ⟨b, hb, rfl⟩;
exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ }
lemma map_coe (f : β →ₗ[α] γ) (p : submodule α β) :
(map f p : set γ) = f '' p := rfl
@[simp] lemma mem_map {f : β →ₗ[α] γ} {p : submodule α β} {x : γ} :
x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl
theorem mem_map_of_mem {f : β →ₗ[α] γ} {p : submodule α β} {r} (h : r ∈ p) : f r ∈ map f p :=
set.mem_image_of_mem _ h
lemma map_id : map linear_map.id p = p :=
submodule.ext $ λ a, by simp
lemma map_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α β) :
map (g.comp f) p = map g (map f p) :=
submodule.ext' $ by simp [map_coe]; rw ← image_comp
lemma map_mono {f : β →ₗ[α] γ} {p p' : submodule α β} : p ≤ p' → map f p ≤ map f p' :=
image_subset _
@[simp] lemma map_zero : map (0 : β →ₗ[α] γ) p = ⊥ :=
have ∃ (x : β), x ∈ p := ⟨0, p.zero_mem⟩,
ext $ by simp [this, eq_comm]
/-- The pullback -/
def comap (f : β →ₗ[α] γ) (p : submodule α γ) : submodule α β :=
{ carrier := f ⁻¹' p,
zero := by simp,
add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂],
smul := λ a x h, by simp [p.smul_mem _ h] }
@[simp] lemma comap_coe (f : β →ₗ[α] γ) (p : submodule α γ) :
(comap f p : set β) = f ⁻¹' p := rfl
@[simp] lemma mem_comap {f : β →ₗ[α] γ} {p : submodule α γ} :
x ∈ comap f p ↔ f x ∈ p := iff.rfl
lemma comap_id : comap linear_map.id p = p :=
submodule.ext' rfl
lemma comap_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α δ) :
comap (g.comp f) p = comap f (comap g p) := rfl
lemma comap_mono {f : β →ₗ[α] γ} {q q' : submodule α γ} : q ≤ q' → comap f q ≤ comap f q' :=
preimage_mono
lemma map_le_iff_le_comap {f : β →ₗ[α] γ} {p : submodule α β} {q : submodule α γ} :
map f p ≤ q ↔ p ≤ comap f q := image_subset_iff
lemma gc_map_comap (f : β →ₗ[α] γ) : galois_connection (map f) (comap f)
| p q := map_le_iff_le_comap
@[simp] lemma map_bot (f : β →ₗ[α] γ) : map f ⊥ = ⊥ :=
(gc_map_comap f).l_bot
@[simp] lemma map_sup (f : β →ₗ[α] γ) : map f (p ⊔ p') = map f p ⊔ map f p' :=
(gc_map_comap f).l_sup
@[simp] lemma map_supr {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α β) :
map f (⨆i, p i) = (⨆i, map f (p i)) :=
(gc_map_comap f).l_supr
@[simp] lemma comap_top (f : β →ₗ[α] γ) : comap f ⊤ = ⊤ := rfl
@[simp] lemma comap_inf (f : β →ₗ[α] γ) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl
@[simp] lemma comap_infi {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α γ) :
comap f (⨅i, p i) = (⨅i, comap f (p i)) :=
(gc_map_comap f).u_infi
@[simp] lemma comap_zero : comap (0 : β →ₗ[α] γ) q = ⊤ :=
ext $ by simp
lemma map_comap_le (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) ≤ q :=
(gc_map_comap f).l_u_le _
lemma le_comap_map (f : β →ₗ[α] γ) (p : submodule α β) : p ≤ comap f (map f p) :=
(gc_map_comap f).le_u_l _
--TODO(Mario): is there a way to prove this from order properties?
lemma map_inf_eq_map_inf_comap {f : β →ₗ[α] γ}
{p : submodule α β} {p' : submodule α γ} :
map f p ⊓ p' = map f (p ⊓ comap f p') :=
le_antisymm
(by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩)
(le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right))
lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' :=
ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩
lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule α β)), b = 0
| ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot α).1 hb
section
variables (α)
def span (s : set β) : submodule α β := Inf {p | s ⊆ p}
end
variables {s t : set β}
lemma mem_span : x ∈ span α s ↔ ∀ p : submodule α β, s ⊆ p → x ∈ p :=
mem_bInter_iff
lemma subset_span : s ⊆ span α s :=
λ x h, mem_span.2 $ λ p hp, hp h
lemma span_le {p} : span α s ≤ p ↔ s ⊆ p :=
⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩
lemma span_mono (h : s ⊆ t) : span α s ≤ span α t :=
span_le.2 $ subset.trans h subset_span
lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span α s) : span α s = p :=
le_antisymm (span_le.2 h₁) h₂
@[simp] lemma span_eq : span α (p : set β) = p :=
span_eq_of_le _ (subset.refl _) subset_span
@[elab_as_eliminator] lemma span_induction {p : β → Prop} (h : x ∈ span α s)
(Hs : ∀ x ∈ s, p x) (H0 : p 0)
(H1 : ∀ x y, p x → p y → p (x + y))
(H2 : ∀ (a:α) x, p x → p (a • x)) : p x :=
(@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h
section
variables (α β)
protected def gi : galois_insertion (@span α β _ _ _) coe :=
{ choice := λ s _, span α s,
gc := λ s t, span_le,
le_l_u := λ s, subset_span,
choice_eq := λ s h, rfl }
end
@[simp] lemma span_empty : span α (∅ : set β) = ⊥ :=
(submodule.gi α β).gc.l_bot
@[simp] lemma span_univ : span α (univ : set β) = ⊤ :=
eq_top_iff.2 $ le_def.2 $ subset_span
lemma span_union (s t : set β) : span α (s ∪ t) = span α s ⊔ span α t :=
(submodule.gi α β).gc.l_sup
lemma span_Union {ι} (s : ι → set β) : span α (⋃ i, s i) = ⨆ i, span α (s i) :=
(submodule.gi α β).gc.l_supr
@[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι)
(S : ι → submodule α β)
(H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) :
((supr S : submodule α β) : set β) = ⋃ i, S i :=
begin
refine subset.antisymm _ (Union_subset $ le_supr S),
rw [show supr S = ⨆ i, span α (S i), by simp, ← span_Union],
unfreezeI,
refine λ x hx, span_induction hx (λ _, id) _ _ _,
{ cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ },
{ simp, intros x y i hi j hj,
rcases H i j with ⟨k, ik, jk⟩,
exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ },
{ simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ },
end
lemma mem_supr_of_mem {ι : Sort*} {b : β} (p : ι → submodule α β) (i : ι) (h : b ∈ p i) :
b ∈ (⨆i, p i) :=
have p i ≤ (⨆i, p i) := le_supr p i,
@this b h
@[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι)
(S : ι → submodule α β)
(H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} :
x ∈ supr S ↔ ∃ i, x ∈ S i :=
by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl
theorem mem_Sup_of_directed {s : set (submodule α β)}
{z} (hzs : z ∈ Sup s) (x ∈ s)
(hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) :
∃ y ∈ s, z ∈ y :=
begin
haveI := classical.dec, rw Sup_eq_supr at hzs,
have : ∃ (i : submodule α β), z ∈ ⨆ (H : i ∈ s), i,
{ refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs,
by_cases his : i ∈ s; by_cases hjs : j ∈ s,
{ rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩,
exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik),
le_supr_of_le hks (supr_le $ λ _, hjk)⟩ },
{ exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ },
{ exact ⟨j, supr_le $ his.elim, le_refl _⟩ },
{ exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } },
cases this with N hzn, by_cases hns : N ∈ s,
{ have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _),
exact ⟨N, hns, this hzn⟩ },
{ have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim,
cases (mem_bot α).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ }
end
section
variables {p p'}
lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x :=
⟨λ h, begin
rw [← span_eq p, ← span_eq p', ← span_union] at h,
apply span_induction h,
{ rintro y (h | h),
{ exact ⟨y, h, 0, by simp, by simp⟩ },
{ exact ⟨0, by simp, y, h, by simp⟩ } },
{ exact ⟨0, by simp, 0, by simp⟩ },
{ rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩,
exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ },
{ rintro a _ ⟨y, hy, z, hz, rfl⟩,
exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ }
end,
by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _
((le_sup_left : p ≤ p ⊔ p') hy)
((le_sup_right : p' ≤ p ⊔ p') hz)⟩
end
lemma mem_span_singleton {y : β} : x ∈ span α ({y} : set β) ↔ ∃ a:α, a • y = x :=
⟨λ h, begin
apply span_induction h,
{ rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ },
{ exact ⟨0, by simp⟩ },
{ rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩,
exact ⟨a + b, by simp [add_smul]⟩ },
{ rintro a _ ⟨b, rfl⟩,
exact ⟨a * b, by simp [smul_smul]⟩ }
end,
by rintro ⟨a, y, rfl⟩; exact
smul_mem _ _ (subset_span $ by simp)⟩
lemma span_singleton_eq_range (y : β) : (span α ({y} : set β) : set β) = range ((• y) : α → β) :=
set.ext $ λ x, mem_span_singleton
lemma mem_span_insert {y} : x ∈ span α (insert y s) ↔ ∃ (a:α) (z ∈ span α s), x = a • y + z :=
begin
rw [← union_singleton, span_union, mem_sup],
simp [mem_span_singleton], split,
{ rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ },
{ rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ }
end
lemma mem_span_insert' {y} : x ∈ span α (insert y s) ↔ ∃(a:α), x + a • y ∈ span α s :=
begin
rw mem_span_insert, split,
{ rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ },
{ rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ }
end
lemma span_insert_eq_span (h : x ∈ span α s) : span α (insert x s) = span α s :=
span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _)
lemma span_span : span α (span α s : set β) = span α s := span_eq _
lemma span_eq_bot : span α (s : set β) = ⊥ ↔ ∀ x ∈ s, (x:β) = 0 :=
eq_bot_iff.trans ⟨
λ H x h, (mem_bot α).1 $ H $ subset_span h,
λ H, span_le.2 (λ x h, (mem_bot α).2 $ H x h)⟩
lemma span_singleton_eq_bot : span α ({x} : set β) = ⊥ ↔ x = 0 :=
span_eq_bot.trans $ by simp
@[simp] lemma span_image (f : β →ₗ[α] γ) : span α (f '' s) = map f (span α s) :=
span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $
span_le.2 $ image_subset_iff.1 subset_span
lemma linear_eq_on (s : set β) {f g : β →ₗ[α] γ} (H : ∀x∈s, f x = g x) {x} (h : x ∈ span α s) :
f x = g x :=
by apply span_induction h H; simp {contextual := tt}
def prod : submodule α (β × γ) :=
{ carrier := set.prod p q,
zero := ⟨zero_mem _, zero_mem _⟩,
add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩;
exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩,
smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩;
exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ }
@[simp] lemma prod_coe :
(prod p q : set (β × γ)) = set.prod p q := rfl
@[simp] lemma mem_prod {p : submodule α β} {q : submodule α γ} {x : β × γ} :
x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod
lemma span_prod_le (s : set β) (t : set γ) :
span α (set.prod s t) ≤ prod (span α s) (span α t) :=
span_le.2 $ set.prod_mono subset_span subset_span
@[simp] lemma prod_top : (prod ⊤ ⊤ : submodule α (β × γ)) = ⊤ :=
by ext; simp
@[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule α (β × γ)) = ⊥ :=
by ext ⟨x, y⟩; simp [prod.zero_eq_mk]
lemma prod_mono {p p' : submodule α β} {q q' : submodule α γ} :
p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono
@[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') :=
ext' set.prod_inter_prod
@[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') :=
begin
refine le_antisymm (sup_le
(prod_mono le_sup_left le_sup_left)
(prod_mono le_sup_right le_sup_right)) _,
simp [le_def'], intros xx yy hxx hyy,
rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩,
rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩,
refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩
end
-- TODO(Mario): Factor through add_subgroup
def quotient_rel : setoid β :=
⟨λ x y, x - y ∈ p, λ x, by simp,
λ x y h, by simpa using neg_mem _ h,
λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩
def quotient : Type* := quotient (quotient_rel p)
namespace quotient
def mk {p : submodule α β} : β → quotient p := quotient.mk'
@[simp] theorem mk_eq_mk {p : submodule α β} (x : β) : (quotient.mk x : quotient p) = mk x := rfl
@[simp] theorem mk'_eq_mk {p : submodule α β} (x : β) : (quotient.mk' x : quotient p) = mk x := rfl
@[simp] theorem quot_mk_eq_mk {p : submodule α β} (x : β) : (quot.mk _ x : quotient p) = mk x := rfl
protected theorem eq {x y : β} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq'
instance : has_zero (quotient p) := ⟨mk 0⟩
@[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl
@[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p :=
by simpa using (quotient.eq p : mk x = 0 ↔ _)
instance : has_add (quotient p) :=
⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $
λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩
@[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl
instance : has_neg (quotient p) :=
⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $
λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩
@[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl
instance : add_comm_group (quotient p) :=
by refine {zero := 0, add := (+), neg := has_neg.neg, ..};
repeat {rintro ⟨⟩};
simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm]
instance : has_scalar α (quotient p) :=
⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $
λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩
@[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl
instance : module α (quotient p) :=
module.of_core $ by refine {smul := (•), ..};
repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul,
-mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm]
instance {α β} {R:discrete_field α} [add_comm_group β] [vector_space α β]
(p : submodule α β) : vector_space α (quotient p) := {}
end quotient
end submodule
namespace submodule
variables [discrete_field α]
variables [add_comm_group β] [vector_space α β]
variables [add_comm_group γ] [vector_space α γ]
lemma comap_smul (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) (h : a ≠ 0) :
p.comap (a • f) = p.comap f :=
by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply]
lemma map_smul (f : β →ₗ[α] γ) (p : submodule α β) (a : α) (h : a ≠ 0) :
p.map (a • f) = p.map f :=
le_antisymm
begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end
begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end
set_option class.instance_max_depth 40
lemma comap_smul' (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) :
p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) :=
by by_cases a = 0; simp [h, comap_smul]
lemma map_smul' (f : β →ₗ[α] γ) (p : submodule α β) (a : α) :
p.map (a • f) = (⨆ h : a ≠ 0, p.map f) :=
by by_cases a = 0; simp [h, map_smul]
end submodule
namespace linear_map
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
include α
open submodule
@[simp] lemma finsupp_sum {α β γ δ} [ring α] [add_comm_group β] [module α β]
[add_comm_group γ] [module α γ] [has_zero δ]
(f : β →ₗ[α] γ) {t : ι →₀ δ} {g : ι → δ → β} :
f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum
theorem map_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h p') :
submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) :=
submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext]
theorem comap_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf p') :
submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') :=
submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩
def range (f : β →ₗ[α] γ) : submodule α γ := map f ⊤
theorem range_coe (f : β →ₗ[α] γ) : (range f : set γ) = set.range f := set.image_univ
@[simp] theorem mem_range {f : β →ₗ[α] γ} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x :=
(set.ext_iff _ _).1 (range_coe f).
@[simp] theorem range_id : range (linear_map.id : β →ₗ[α] β) = ⊤ := map_id _
theorem range_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) = map g (range f) :=
map_comp _ _ _
theorem range_comp_le_range (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) ≤ range g :=
by rw range_comp; exact map_mono le_top
theorem range_eq_top {f : β →ₗ[α] γ} : range f = ⊤ ↔ surjective f :=
by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective]
lemma range_le_iff_comap {f : β →ₗ[α] γ} {p : submodule α γ} : range f ≤ p ↔ comap f p = ⊤ :=
by rw [range, map_le_iff_le_comap, eq_top_iff]
lemma map_le_range {f : β →ₗ[α] γ} {p : submodule α β} : map f p ≤ range f :=
map_mono le_top
lemma sup_range_inl_inr :
(inl α β γ).range ⊔ (inr α β γ).range = ⊤ :=
begin
refine eq_top_iff'.2 (λ x, mem_sup.2 _),
rcases x with ⟨x₁, x₂⟩ ,
have h₁ : prod.mk x₁ (0 : γ) ∈ (inl α β γ).range,
by simp,
have h₂ : prod.mk (0 : β) x₂ ∈ (inr α β γ).range,
by simp,
use [⟨x₁, 0⟩, h₁, ⟨0, x₂⟩, h₂],
simp
end
def ker (f : β →ₗ[α] γ) : submodule α β := comap f ⊥
@[simp] theorem mem_ker {f : β →ₗ[α] γ} {y} : y ∈ ker f ↔ f y = 0 := mem_bot α
@[simp] theorem ker_id : ker (linear_map.id : β →ₗ[α] β) = ⊥ := rfl
theorem ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker (g.comp f) = comap f (ker g) := rfl
theorem ker_le_ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker f ≤ ker (g.comp f) :=
by rw ker_comp; exact comap_mono bot_le
theorem sub_mem_ker_iff {f : β →ₗ[α] γ} {x y} : x - y ∈ f.ker ↔ f x = f y :=
by rw [mem_ker, map_sub, sub_eq_zero]
theorem disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} :
disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 :=
by simp [disjoint_def]
theorem disjoint_ker' {f : β →ₗ[α] γ} {p : submodule α β} :
disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y :=
disjoint_ker.trans
⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]),
λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩
theorem inj_of_disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β}
{s : set β} (h : s ⊆ p) (hd : disjoint p (ker f)) :
∀ x y ∈ s, f x = f y → x = y :=
λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy)
lemma disjoint_inl_inr : disjoint (inl α β γ).range (inr α β γ).range :=
by simp [disjoint_def, @eq_comm β 0, @eq_comm γ 0] {contextual := tt}; intros; refl
theorem ker_eq_bot {f : β →ₗ[α] γ} : ker f = ⊥ ↔ injective f :=
by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤
theorem ker_eq_bot' {f : β →ₗ[α] γ} :
ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) :=
have h : (∀ m ∈ (⊤ : submodule α β), f m = 0 → m = 0) ↔ (∀ m, f m = 0 → m = 0),
from ⟨λ h m, h m mem_top, λ h m _, h m⟩,
by simpa [h, disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤
lemma le_ker_iff_map {f : β →ₗ[α] γ} {p : submodule α β} : p ≤ ker f ↔ map f p = ⊥ :=
by rw [ker, eq_bot_iff, map_le_iff_le_comap]
lemma ker_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) :
ker (cod_restrict p f hf) = ker f :=
by rw [ker, comap_cod_restrict, map_bot]; refl
lemma range_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) :
range (cod_restrict p f hf) = comap p.subtype f.range :=
map_cod_restrict _ _ _ _
lemma map_comap_eq (f : β →ₗ[α] γ) (q : submodule α γ) :
map f (comap f q) = range f ⊓ q :=
le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $
by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩
lemma map_comap_eq_self {f : β →ₗ[α] γ} {q : submodule α γ} (h : q ≤ range f) :
map f (comap f q) = q :=
by rw [map_comap_eq, inf_of_le_right h]
lemma comap_map_eq (f : β →ₗ[α] γ) (p : submodule α β) :
comap f (map f p) = p ⊔ ker f :=
begin
refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)),
rintro x ⟨y, hy, e⟩,
exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩
end
lemma comap_map_eq_self {f : β →ₗ[α] γ} {p : submodule α β} (h : ker f ≤ p) :
comap f (map f p) = p :=
by rw [comap_map_eq, sup_of_le_left h]
@[simp] theorem ker_zero : ker (0 : β →ₗ[α] γ) = ⊤ :=
eq_top_iff'.2 $ λ x, by simp
@[simp] theorem range_zero : range (0 : β →ₗ[α] γ) = ⊥ :=
submodule.map_zero _
theorem ker_eq_top {f : β →ₗ[α] γ} : ker f = ⊤ ↔ f = 0 :=
⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩
lemma range_le_bot_iff (f : β →ₗ[α] γ) : range f ≤ ⊥ ↔ f = 0 :=
by rw [range_le_iff_comap]; exact ker_eq_top
theorem map_le_map_iff {f : β →ₗ[α] γ} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' :=
⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩
theorem map_injective {f : β →ₗ[α] γ} (hf : ker f = ⊥) : injective (map f) :=
λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h))
theorem comap_le_comap_iff {f : β →ₗ[α] γ} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' :=
⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩
theorem comap_injective {f : β →ₗ[α] γ} (hf : range f = ⊤) : injective (comap f) :=
λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h))
theorem map_copair_prod (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (p : submodule α β) (q : submodule α γ) :
map (copair f g) (p.prod q) = map f p ⊔ map g q :=
begin
refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)),
{ rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩,
exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ },
{ exact λ x hx, ⟨(x, 0), by simp [hx]⟩ },
{ exact λ x hx, ⟨(0, x), by simp [hx]⟩ }
end
theorem comap_pair_prod (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (p : submodule α γ) (q : submodule α δ) :
comap (pair f g) (p.prod q) = comap f p ⊓ comap g q :=
submodule.ext $ λ x, iff.rfl
theorem prod_eq_inf_comap (p : submodule α β) (q : submodule α γ) :
p.prod q = p.comap (linear_map.fst α β γ) ⊓ q.comap (linear_map.snd α β γ) :=
submodule.ext $ λ x, iff.rfl
theorem prod_eq_sup_map (p : submodule α β) (q : submodule α γ) :
p.prod q = p.map (linear_map.inl α β γ) ⊔ q.map (linear_map.inr α β γ) :=
by rw [← map_copair_prod, copair_inl_inr, map_id]
lemma span_inl_union_inr {s : set β} {t : set γ} :
span α (prod.inl '' s ∪ prod.inr '' t) = (span α s).prod (span α t) :=
by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl
lemma ker_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) :
ker (pair f g) = ker f ⊓ ker g :=
by rw [ker, ← prod_bot, comap_pair_prod]; refl
end linear_map
namespace linear_map
variables [discrete_field α]
variables [add_comm_group β] [vector_space α β]
variables [add_comm_group γ] [vector_space α γ]
lemma ker_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : ker (a • f) = ker f :=
submodule.comap_smul f _ a h
lemma ker_smul' (f : β →ₗ[α] γ) (a : α) : ker (a • f) = ⨅(h : a ≠ 0), ker f :=
submodule.comap_smul' f _ a
lemma range_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : range (a • f) = range f :=
submodule.map_smul f _ a h
lemma range_smul' (f : β →ₗ[α] γ) (a : α) : range (a • f) = ⨆(h : a ≠ 0), range f :=
submodule.map_smul' f _ a
end linear_map
namespace is_linear_map
lemma is_linear_map_add {α β : Type*} [ring α] [add_comm_group β] [module α β]:
is_linear_map α (λ (x : β × β), x.1 + x.2) :=
begin
apply is_linear_map.mk,
{ intros x y,
simp },
{ intros x y,
simp [smul_add] }
end
lemma is_linear_map_sub {α β : Type*} [ring α] [add_comm_group β] [module α β]:
is_linear_map α (λ (x : β × β), x.1 - x.2) :=
begin
apply is_linear_map.mk,
{ intros x y,
simp },
{ intros x y,
simp [smul_add] }
end
end is_linear_map
namespace submodule
variables {R:ring α} [add_comm_group β] [add_comm_group γ] [module α β] [module α γ]
variables (p p' : submodule α β) (q : submodule α γ)
include R
open linear_map
@[simp] theorem map_top (f : β →ₗ[α] γ) : map f ⊤ = range f := rfl
@[simp] theorem comap_bot (f : β →ₗ[α] γ) : comap f ⊥ = ker f := rfl
@[simp] theorem ker_subtype : p.subtype.ker = ⊥ :=
ker_eq_bot.2 $ λ x y, subtype.eq'
@[simp] theorem range_subtype : p.subtype.range = p :=
by simpa using map_comap_subtype p ⊤
lemma map_subtype_le (p' : submodule α p) : map p.subtype p' ≤ p :=
by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range)
@[simp] theorem ker_of_le (p p' : submodule α β) (h : p ≤ p') : (of_le h).ker = ⊥ :=
by rw [of_le, ker_cod_restrict, ker_subtype]
lemma range_of_le (p q : submodule α β) (h : p ≤ q) : (of_le h).range = comap q.subtype p :=
by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype]
lemma disjoint_iff_comap_eq_bot (p q : submodule α β) :
disjoint p q ↔ comap p.subtype q = ⊥ :=
by rw [eq_bot_iff, ← map_le_map_iff p.ker_subtype, map_bot, map_comap_subtype]; refl
/-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/
def map_subtype.order_iso :
((≤) : submodule α p → submodule α p → Prop) ≃o
((≤) : {p' : submodule α β // p' ≤ p} → {p' : submodule α β // p' ≤ p} → Prop) :=
{ to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩,
inv_fun := λ q, comap p.subtype q,
left_inv := λ p', comap_map_eq_self $ by simp,
right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq],
ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm }
def map_subtype.le_order_embedding :
((≤) : submodule α p → submodule α p → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) :=
(order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _)
@[simp] lemma map_subtype_embedding_eq (p' : submodule α p) :
map_subtype.le_order_embedding p p' = map p.subtype p' := rfl
def map_subtype.lt_order_embedding :
((<) : submodule α p → submodule α p → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) :=
(map_subtype.le_order_embedding p).lt_embedding_of_le_embedding
@[simp] theorem map_inl : p.map (inl α β γ) = prod p ⊥ :=
by ext ⟨x, y⟩; simp [and.left_comm, eq_comm]
@[simp] theorem map_inr : q.map (inr α β γ) = prod ⊥ q :=
by ext ⟨x, y⟩; simp [and.left_comm, eq_comm]
@[simp] theorem comap_fst : p.comap (fst α β γ) = prod p ⊤ :=
by ext ⟨x, y⟩; simp
@[simp] theorem comap_snd : q.comap (snd α β γ) = prod ⊤ q :=
by ext ⟨x, y⟩; simp
@[simp] theorem prod_comap_inl : (prod p q).comap (inl α β γ) = p := by ext; simp
@[simp] theorem prod_comap_inr : (prod p q).comap (inr α β γ) = q := by ext; simp
@[simp] theorem prod_map_fst : (prod p q).map (fst α β γ) = p :=
by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)]
@[simp] theorem prod_map_snd : (prod p q).map (snd α β γ) = q :=
by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)]
@[simp] theorem ker_inl : (inl α β γ).ker = ⊥ :=
by rw [ker, ← prod_bot, prod_comap_inl]
@[simp] theorem ker_inr : (inr α β γ).ker = ⊥ :=
by rw [ker, ← prod_bot, prod_comap_inr]
@[simp] theorem range_fst : (fst α β γ).range = ⊤ :=
by rw [range, ← prod_top, prod_map_fst]
@[simp] theorem range_snd : (snd α β γ).range = ⊤ :=
by rw [range, ← prod_top, prod_map_snd]
def mkq : β →ₗ[α] p.quotient := ⟨quotient.mk, by simp, by simp⟩
@[simp] theorem mkq_apply (x : β) : p.mkq x = quotient.mk x := rfl
def liftq (f : β →ₗ[α] γ) (h : p ≤ f.ker) : p.quotient →ₗ[α] γ :=
⟨λ x, _root_.quotient.lift_on' x f $
λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab,
by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y,
by rintro a ⟨x⟩; exact f.map_smul a x⟩
@[simp] theorem liftq_apply (f : β →ₗ[α] γ) {h} (x : β) :
p.liftq f h (quotient.mk x) = f x := rfl
@[simp] theorem liftq_mkq (f : β →ₗ[α] γ) (h) : (p.liftq f h).comp p.mkq = f :=
by ext; refl
@[simp] theorem range_mkq : p.mkq.range = ⊤ :=
eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩
@[simp] theorem ker_mkq : p.mkq.ker = p :=
by ext; simp
lemma le_comap_mkq (p' : submodule α p.quotient) : p ≤ comap p.mkq p' :=
by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p')
@[simp] theorem mkq_map_self : map p.mkq p = ⊥ :=
by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _
@[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' :=
by simp [comap_map_eq, sup_comm]
def mapq (f : β →ₗ[α] γ) (h : p ≤ comap f q) : p.quotient →ₗ[α] q.quotient :=
p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h
@[simp] theorem mapq_apply (f : β →ₗ[α] γ) {h} (x : β) :
mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl
theorem mapq_mkq (f : β →ₗ[α] γ) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f :=
by ext x; refl
theorem comap_liftq (f : β →ₗ[α] γ) (h) :
q.comap (p.liftq f h) = (q.comap f).map (mkq p) :=
le_antisymm
(by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩)
(by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _)
theorem map_liftq (f : β →ₗ[α] γ) (h) (q : submodule α (quotient p)) :
q.map (p.liftq f h) = (q.comap p.mkq).map f :=
le_antisymm
(by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩)
(by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩)
theorem ker_liftq (f : β →ₗ[α] γ) (h) :
ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _
theorem range_liftq (f : β →ₗ[α] γ) (h) :
range (p.liftq f h) = range f := map_liftq _ _ _ _
theorem ker_liftq_eq_bot (f : β →ₗ[α] γ) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ :=
by rw [ker_liftq, le_antisymm h h', mkq_map_self]
/-- Correspondence Theorem -/
def comap_mkq.order_iso :
((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≃o
((≤) : {p' : submodule α β // p ≤ p'} → {p' : submodule α β // p ≤ p'} → Prop) :=
{ to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩,
inv_fun := λ q, map p.mkq q,
left_inv := λ p', map_comap_eq_self $ by simp,
right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq],
ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm }
def comap_mkq.le_order_embedding :
((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) :=
(order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _)
@[simp] lemma comap_mkq_embedding_eq (p' : submodule α p.quotient) :
comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl
def comap_mkq.lt_order_embedding :
((<) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) :=
(comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding
end submodule
section
set_option old_structure_cmd true
structure linear_equiv (α : Type u) (β : Type v) (γ : Type w)
[ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ]
extends β →ₗ[α] γ, β ≃ γ
end
infix ` ≃ₗ ` := linear_equiv _
notation β ` ≃ₗ[`:50 α `] ` γ := linear_equiv α β γ
namespace linear_equiv
section ring
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
include α
instance : has_coe (β ≃ₗ[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩
@[simp] theorem coe_apply (e : β ≃ₗ[α] γ) (b : β) : (e : β →ₗ[α] γ) b = e b := rfl
lemma to_equiv_injective : function.injective (to_equiv : (β ≃ₗ[α] γ) → β ≃ γ) :=
λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h)
@[extensionality] lemma ext {f g : β ≃ₗ[α] γ} (h : (f : β → γ) = g) : f = g :=
to_equiv_injective (equiv.eq_of_to_fun_eq h)
section
variable (β)
def refl : β ≃ₗ[α] β := { .. linear_map.id, .. equiv.refl β }
end
def symm (e : β ≃ₗ[α] γ) : γ ≃ₗ[α] β :=
{ .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv,
.. e.to_equiv.symm }
def trans (e₁ : β ≃ₗ[α] γ) (e₂ : γ ≃ₗ[α] δ) : β ≃ₗ[α] δ :=
{ .. e₂.to_linear_map.comp e₁.to_linear_map,
.. e₁.to_equiv.trans e₂.to_equiv }
@[simp] theorem apply_symm_apply (e : β ≃ₗ[α] γ) (c : γ) : e (e.symm c) = c := e.6 c
@[simp] theorem symm_apply_apply (e : β ≃ₗ[α] γ) (b : β) : e.symm (e b) = b := e.5 b
noncomputable def of_bijective
(f : β →ₗ[α] γ) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : β ≃ₗ[α] γ :=
{ ..f, ..@equiv.of_bijective _ _ f
⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ }
@[simp] theorem of_bijective_apply (f : β →ₗ[α] γ) {hf₁ hf₂} (x : β) :
of_bijective f hf₁ hf₂ x = f x := rfl
def of_linear (f : β →ₗ[α] γ) (g : γ →ₗ[α] β)
(h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : β ≃ₗ[α] γ :=
{ inv_fun := g,
left_inv := linear_map.ext_iff.1 h₂,
right_inv := linear_map.ext_iff.1 h₁,
..f }
@[simp] theorem of_linear_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂}
(x : β) : of_linear f g h₁ h₂ x = f x := rfl
@[simp] theorem of_linear_symm_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂}
(x : γ) : (of_linear f g h₁ h₂).symm x = g x := rfl
@[simp] protected theorem ker (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).ker = ⊥ :=
linear_map.ker_eq_bot.2 f.to_equiv.injective
@[simp] protected theorem range (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).range = ⊤ :=
linear_map.range_eq_top.2 f.to_equiv.surjective
def of_top (p : submodule α β) (h : p = ⊤) : p ≃ₗ[α] β :=
{ inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩,
left_inv := λ ⟨x, h⟩, rfl,
right_inv := λ x, rfl,
.. p.subtype }
@[simp] theorem of_top_apply (p : submodule α β) {h} (x : p) :
of_top p h x = x := rfl
@[simp] theorem of_top_symm_apply (p : submodule α β) {h} (x : β) :
↑((of_top p h).symm x) = x := rfl
lemma eq_bot_of_equiv (p : submodule α β) (e : p ≃ₗ[α] (⊥ : submodule α γ)) :
p = ⊥ :=
begin
refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot α).2 _),
have := e.symm_apply_apply ⟨b, hb⟩,
rw [← e.coe_apply, submodule.eq_zero_of_bot_submodule ((e : p →ₗ[α] (⊥ : submodule α γ)) ⟨b, hb⟩),
← e.symm.coe_apply, linear_map.map_zero] at this,
exact congr_arg (coe : p → β) this.symm
end
end ring
section comm_ring
variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
include α
open linear_map
set_option class.instance_max_depth 39
def smul_of_unit (a : units α) : β ≃ₗ[α] β :=
of_linear ((a:α) • 1 : β →ₗ β) (((a⁻¹ : units α) : α) • 1 : β →ₗ β)
(by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl)
(by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl)
def congr_right (f : γ ≃ₗ[α] δ) : (β →ₗ[α] γ) ≃ₗ (β →ₗ δ) :=
of_linear
f.to_linear_map.congr_right
f.symm.to_linear_map.congr_right
(linear_map.ext $ λ _, linear_map.ext $ λ _, f.6 _)
(linear_map.ext $ λ _, linear_map.ext $ λ _, f.5 _)
end comm_ring
section field
variables [field α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
variable (β)
open linear_map
def smul_of_ne_zero (a : α) (ha : a ≠ 0) : β ≃ₗ[α] β :=
smul_of_unit $ units.mk0 a ha
end field
end linear_equiv
namespace equiv
variables [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ]
def to_linear_equiv (e : β ≃ γ) (h : is_linear_map α (e : β → γ)) : β ≃ₗ[α] γ :=
{ add := h.add, smul := h.smul, .. e}
end equiv
namespace linear_map
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
variables (f : β →ₗ[α] γ)
/-- First Isomorphism Law -/
noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[α] f.range :=
have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right,
let F : f.ker.quotient →ₗ[α] f.range :=
f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩)
(λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in
{ inv_fun := λx, submodule.quotient.mk (classical.some (hr x)),
left_inv := by rintro ⟨x⟩; exact
(submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $
classical.some_spec $ hr $ F $ submodule.quotient.mk x),
right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x),
.. F }
open submodule
def sup_quotient_to_quotient_inf (p p' : submodule α β) :
(comap p.subtype (p ⊓ p')).quotient →ₗ[α] (comap (p ⊔ p').subtype p').quotient :=
(comap p.subtype (p ⊓ p')).liftq
((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin
rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype],
exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end
set_option class.instance_max_depth 41
/-- Second Isomorphism Law -/
noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule α β) :
(comap p.subtype (p ⊓ p')).quotient ≃ₗ[α] (comap (p ⊔ p').subtype p').quotient :=
{ .. sup_quotient_to_quotient_inf p p',
.. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from
@equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin
constructor,
{ rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot],
rw [ker_comp, ker_mkq],
rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ },
rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'],
rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩,
use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2,
change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff]
end }
section prod
def prod {α β γ δ : Type*} [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
[module α β] [module α γ] [module α δ]
(f₁ : β →ₗ[α] γ) (f₂ : β →ₗ[α] δ) : β →ₗ[α] (γ × δ) :=
{ to_fun := λx, (f₁ x, f₂ x),
add := λx y, begin
change (f₁ (x + y), f₂ (x+y)) = (f₁ x, f₂ x) + (f₁ y, f₂ y),
simp only [linear_map.map_add],
refl
end,
smul := λc x, by simp only [linear_map.map_smul] }
lemma is_linear_map_prod_iso {α β γ δ : Type*} [comm_ring α] [add_comm_group β] [add_comm_group γ]
[add_comm_group δ] [module α β] [module α γ] [module α δ] :
is_linear_map α (λ(p : (β →ₗ[α] γ) × (β →ₗ[α] δ)), (linear_map.prod p.1 p.2 : (β →ₗ[α] (γ × δ)))) :=
⟨λu v, rfl, λc u, rfl⟩
def scalar_prod_space_iso {α β γ : Type*} [comm_ring α] [add_comm_group β] [add_comm_group γ]
[module α β] [module α γ] (c : β →ₗ[α] α) (f : γ) : β →ₗ[α] γ :=
{ to_fun := λx, (c x) • f,
add := λx y, begin
change c (x + y) • f = (c x) • f + (c y) • f,
simp [add_smul],
end,
smul := λa x, by simp [smul_smul] }
end prod
section pi
universe i
variables {φ : ι → Type i}
variables [∀i, add_comm_group (φ i)] [∀i, module α (φ i)]
/-- `pi` construction for linear functions. From a family of linear functions it produces a linear
function into a family of modules. -/
def pi (f : Πi, γ →ₗ[α] φ i) : γ →ₗ[α] (Πi, φ i) :=
⟨λc i, f i c,
assume c d, funext $ assume i, (f i).add _ _, assume c d, funext $ assume i, (f i).smul _ _⟩
@[simp] lemma pi_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i : ι) :
pi f c i = f i c := rfl
lemma ker_pi (f : Πi, γ →ₗ[α] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) :=
by ext c; simp [funext_iff]; refl
lemma pi_eq_zero (f : Πi, γ →ₗ[α] φ i) : pi f = 0 ↔ (∀i, f i = 0) :=
by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩
lemma pi_zero : pi (λi, 0 : Πi, γ →ₗ[α] φ i) = 0 :=
by ext; refl
lemma pi_comp (f : Πi, γ →ₗ[α] φ i) (g : δ →ₗ[α] γ) : (pi f).comp g = pi (λi, (f i).comp g) :=
rfl
/-- Linear projection -/
def proj (i : ι) : (Πi, φ i) →ₗ[α] φ i :=
⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩
@[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[α] φ i) b = b i := rfl
lemma proj_pi (f : Πi, γ →ₗ[α] φ i) (i : ι) : (proj i).comp (pi f) = f i :=
ext $ assume c, rfl
lemma infi_ker_proj : (⨅i, ker (proj i) : submodule α (Πi, φ i)) = ⊥ :=
bot_unique $ submodule.le_def'.2 $ assume a h,
begin
simp only [mem_infi, mem_ker, proj_apply] at h,
exact (mem_bot _).2 (funext $ assume i, h i)
end
section
variables (α φ)
def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)]
(hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) :
(⨅i ∈ J, ker (proj i) : submodule α (Πi, φ i)) ≃ₗ[α] (Πi:I, φ i) :=
begin
refine linear_equiv.of_linear
(pi $ λi, (proj (i:ι)).comp (submodule.subtype _))
(cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _,
{ assume b,
simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply],
assume j hjJ,
have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩,
rw [dif_neg this, zero_apply] },
{ simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.val_prop'],
ext b ⟨j, hj⟩, refl },
{ ext ⟨b, hb⟩,
apply subtype.coe_ext.2,
ext j,
have hb : ∀i ∈ J, b i = 0,
{ simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb },
simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply],
split_ifs,
{ rw [dif_pos h], refl },
{ rw [dif_neg h],
exact (hb _ $ (hu trivial).resolve_left h).symm } }
end
end
section
variable [decidable_eq ι]
/-- `diag i j` is the identity map if `i = j` otherwise it is the constant 0 map. -/
def diag (i j : ι) : φ i →ₗ[α] φ j :=
@function.update ι (λj, φ i →ₗ[α] φ j) _ 0 i id j
lemma update_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i j : ι) (b : γ →ₗ[α] φ i) :
(update f i b j) c = update (λi, f i c) i (b c) j :=
begin
by_cases j = i,
{ rw [h, update_same, update_same] },
{ rw [update_noteq h, update_noteq h] }
end
end
section
variable [decidable_eq ι]
variables (α φ)
/-- Standard basis -/
def std_basis (i : ι) : φ i →ₗ[α] (Πi, φ i) := pi (diag i)
lemma std_basis_apply (i : ι) (b : φ i) : std_basis α φ i b = update 0 i b :=
by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl
@[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis α φ i b i = b :=
by rw [std_basis_apply, update_same]
lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis α φ i b j = 0 :=
by rw [std_basis_apply, update_noteq h]; refl
lemma ker_std_basis (i : ι) : ker (std_basis α φ i) = ⊥ :=
ker_eq_bot.2 $ assume f g hfg,
have std_basis α φ i f i = std_basis α φ i g i := hfg ▸ rfl,
by simpa only [std_basis_same]
lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis α φ j) = diag j i :=
by rw [std_basis, proj_pi]
lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis α φ i) = id :=
by ext b; simp
lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis α φ j) = 0 :=
by ext b; simp [std_basis_ne α φ _ _ h]
lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) :
(⨆i∈I, range (std_basis α φ i)) ≤ (⨅i∈J, ker (proj i)) :=
begin
refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _),
simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi],
assume b hb j hj,
have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩,
rw [proj_std_basis_ne α φ j i this.symm, zero_apply]
end
lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) :
(⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis α φ i)) :=
submodule.le_def'.2
begin
assume b hb,
simp only [mem_infi, mem_ker, proj_apply] at hb,
rw ← show I.sum (λi, std_basis α φ i (b i)) = b,
{ ext i,
rw [pi.finset_sum_apply, ← std_basis_same α φ i (b i)],
refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _,
assume hiI,
rw [std_basis_same],
exact hb _ ((hu trivial).resolve_left hiI) },
exact sum_mem _ (assume i hiI, mem_supr_of_mem _ i $ mem_supr_of_mem _ hiI $
linear_map.mem_range.2 ⟨_, rfl⟩)
end
lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι}
(hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) :
(⨆i∈I, range (std_basis α φ i)) = (⨅i∈J, ker (proj i)) :=
begin
refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _,
have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [finset.coe_to_finset] },
refine le_trans (infi_ker_proj_le_supr_range_std_basis α φ this) (supr_le_supr $ assume i, _),
rw [← finset.mem_coe, finset.coe_to_finset],
exact le_refl _
end
lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis α φ i)) = ⊤ :=
have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty],
begin
apply top_unique,
convert (infi_ker_proj_le_supr_range_std_basis α φ this),
exact infi_emptyset.symm,
exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis α φ i)) $ finset.mem_univ i).symm)
end
lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) :
disjoint (⨆i∈I, range (std_basis α φ i)) (⨆i∈J, range (std_basis α φ i)) :=
begin
refine disjoint_mono
(supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I)
(supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _,
simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply,
funext_iff],
rintros b ⟨hI, hJ⟩ i,
classical,
by_cases hiI : i ∈ I,
{ by_cases hiJ : i ∈ J,
{ exact (h ⟨hiI, hiJ⟩).elim },
{ exact hJ i hiJ } },
{ exact hI i hiI }
end
end
end pi
variables (α β)
instance automorphism_group : group (β ≃ₗ[α] β) :=
{ mul := λ f g, g.trans f,
one := linear_equiv.refl β,
inv := λ f, f.symm,
mul_assoc := λ f g h, by {ext, refl},
mul_one := λ f, by {ext, refl},
one_mul := λ f, by {ext, refl},
mul_left_inv := λ f, by {ext, exact f.left_inv x} }
instance automorphism_group.to_linear_map_is_monoid_hom :
is_monoid_hom (linear_equiv.to_linear_map : (β ≃ₗ[α] β) → (β →ₗ[α] β)) :=
{ map_one := rfl,
map_mul := λ f g, rfl }
/-- The group of invertible linear maps from `β` to itself -/
def general_linear_group := units (β →ₗ[α] β)
namespace general_linear_group
variables {α β}
instance : group (general_linear_group α β) := by delta general_linear_group; apply_instance
def to_linear_equiv (f : general_linear_group α β) : (β ≃ₗ[α] β) :=
{ inv_fun := f.inv.to_fun,
left_inv := λ m, show (f.inv * f.val) m = m,
by erw f.inv_val; simp,
right_inv := λ m, show (f.val * f.inv) m = m,
by erw f.val_inv; simp,
..f.val }
def of_linear_equiv (f : (β ≃ₗ[α] β)) : general_linear_group α β :=
{ val := f,
inv := f.symm,
val_inv := linear_map.ext $ λ _, f.apply_symm_apply _,
inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ }
variables (α β)
def general_linear_equiv : general_linear_group α β ≃* (β ≃ₗ[α] β) :=
{ to_fun := to_linear_equiv,
inv_fun := of_linear_equiv,
left_inv := λ f,
begin
delta to_linear_equiv of_linear_equiv,
cases f with f f_inv, cases f, cases f_inv,
congr
end,
right_inv := λ f,
begin
delta to_linear_equiv of_linear_equiv,
cases f,
congr
end,
map_mul' := λ x y, by {ext, refl} }
@[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group α β) :
((general_linear_equiv α β).to_equiv f).to_linear_map = f.val :=
by {ext, refl}
end general_linear_group
end linear_map
|
45c91f8b4dae5f66a1567027d0e67d8405eb6312 | 137c667471a40116a7afd7261f030b30180468c2 | /src/group_theory/subgroup.lean | 7962a6d73bbb48b6a78bf5dadb9fb8d2d70f322c | [
"Apache-2.0"
] | permissive | bragadeesh153/mathlib | 46bf814cfb1eecb34b5d1549b9117dc60f657792 | b577bb2cd1f96eb47031878256856020b76f73cd | refs/heads/master | 1,687,435,188,334 | 1,626,384,207,000 | 1,626,384,207,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 80,920 | lean | /-
Copyright (c) 2020 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import group_theory.submonoid
import algebra.group.conj
import algebra.pointwise
import order.atoms
/-!
# Subgroups
This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled
form (unbundled subgroups are in `deprecated/subgroups.lean`).
We prove subgroups of a group form a complete lattice, and results about images and preimages of
subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms.
There are also theorems about the subgroups generated by an element or a subset of a group,
defined both inductively and as the infimum of the set of subgroups containing a given
element/subset.
Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration.
## Main definitions
Notation used here:
- `G N` are `group`s
- `A` is an `add_group`
- `H K` are `subgroup`s of `G` or `add_subgroup`s of `A`
- `x` is an element of type `G` or type `A`
- `f g : N →* G` are group homomorphisms
- `s k` are sets of elements of type `G`
Definitions in the file:
* `subgroup G` : the type of subgroups of a group `G`
* `add_subgroup A` : the type of subgroups of an additive group `A`
* `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice
* `subgroup.closure k` : the minimal subgroup that includes the set `k`
* `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G`
* `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set
* `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K`
is a subgroup of `G × N`
* `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup
* `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G`
such that `f x = 1`
* `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that
`f x = g x` form a subgroup of `G`
* `is_simple_group G` : a class indicating that a group has exactly two normal subgroups
## Implementation notes
Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a subgroup's underlying set.
## Tags
subgroup, subgroups
-/
open_locale big_operators
variables {G : Type*} [group G]
variables {A : Type*} [add_group A]
set_option old_structure_cmd true
/-- A subgroup of a group `G` is a subset containing 1, closed under multiplication
and closed under multiplicative inverse. -/
structure subgroup (G : Type*) [group G] extends submonoid G :=
(inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier)
/-- An additive subgroup of an additive group `G` is a subset containing 0, closed
under addition and additive inverse. -/
structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:=
(neg_mem' {x} : x ∈ carrier → -x ∈ carrier)
attribute [to_additive] subgroup
attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid
/-- Reinterpret a `subgroup` as a `submonoid`. -/
add_decl_doc subgroup.to_submonoid
/-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/
add_decl_doc add_subgroup.to_add_submonoid
namespace subgroup
@[to_additive]
instance : set_like (subgroup G) G :=
⟨subgroup.carrier, λ p q h, by cases p; cases q; congr'⟩
@[simp, to_additive]
lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
/-- See Note [custom simps projection] -/
@[to_additive "See Note [custom simps projection]"]
def simps.coe (S : subgroup G) : set G := S
initialize_simps_projections subgroup (carrier → coe)
initialize_simps_projections add_subgroup (carrier → coe)
@[simp, to_additive]
lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl
@[simp, to_additive]
lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl
@[to_additive]
instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K :=
show fintype {g : G // g ∈ K}, from infer_instance
@[to_additive]
theorem to_submonoid_injective :
function.injective (to_submonoid : subgroup G → submonoid G) :=
λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h)
@[simp, to_additive]
theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q :=
to_submonoid_injective.eq_iff
@[mono, to_additive] lemma to_submonoid_strict_mono :
strict_mono (to_submonoid : subgroup G → submonoid G) := λ _ _, id
@[mono, to_additive]
lemma to_submonoid_mono : monotone (to_submonoid : subgroup G → submonoid G) :=
to_submonoid_strict_mono.monotone
@[simp, to_additive]
lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≤ q.to_submonoid ↔ p ≤ q :=
iff.rfl
end subgroup
/-!
### Conversion to/from `additive`/`multiplicative`
-/
section mul_add
/-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/
@[simps]
def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) :=
{ to_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid },
inv_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/
abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G :=
subgroup.to_add_subgroup.symm
/-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`.
-/
@[simps]
def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) :=
{ to_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid },
inv_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`.
-/
abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A :=
add_subgroup.to_subgroup.symm
end mul_add
namespace subgroup
variables (H K : subgroup G)
/-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional
equalities.-/
@[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one.
Useful to fix definitional equalities"]
protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G :=
{ carrier := s,
one_mem' := hs.symm ▸ K.one_mem',
mul_mem' := hs.symm ▸ K.mul_mem',
inv_mem' := hs.symm ▸ K.inv_mem' }
/-- Two subgroups are equal if they have the same elements. -/
@[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."]
theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h
attribute [ext] add_subgroup.ext
/-- A subgroup contains the group's 1. -/
@[to_additive "An `add_subgroup` contains the group's 0."]
theorem one_mem : (1 : G) ∈ H := H.one_mem'
/-- A subgroup is closed under multiplication. -/
@[to_additive "An `add_subgroup` is closed under addition."]
theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := λ hx hy, H.mul_mem' hx hy
/-- A subgroup is closed under inverse. -/
@[to_additive "An `add_subgroup` is closed under inverse."]
theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := λ hx, H.inv_mem' hx
/-- A subgroup is closed under division. -/
@[to_additive "An `add_subgroup` is closed under subtraction."]
theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H :=
by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy)
@[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H :=
⟨λ h, inv_inv x ▸ H.inv_mem h, H.inv_mem⟩
@[to_additive] lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H :=
by rw [← H.inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv]
@[simp, to_additive]
theorem inv_coe_set : (H : set G)⁻¹ = H :=
by { ext, simp, }
@[simp, to_additive]
lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G → Prop} :
(∃ (x : G), x ∈ K ∧ P x⁻¹) ↔ ∃ x ∈ K, P x :=
by split; { rintros ⟨x, x_in, hx⟩, exact ⟨x⁻¹, inv_mem K x_in, by simp [hx]⟩ }
@[to_additive]
lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H :=
⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), λ hb, H.mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H :=
⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩
/-- Product of a list of elements in a subgroup is in the subgroup. -/
@[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."]
lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K :=
K.to_submonoid.list_prod_mem
/-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/
@[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group`
is in the `add_subgroup`."]
lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) :
(∀ a ∈ g, a ∈ K) → g.prod ∈ K := K.to_submonoid.multiset_prod_mem g
/-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the
subgroup. -/
@[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset`
is in the `add_subgroup`."]
lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G)
{ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) :
∏ c in t, f c ∈ K :=
K.to_submonoid.prod_mem h
lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := K.to_submonoid.pow_mem hx
lemma gpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K
| (n : ℕ) := by { rw [gpow_coe_nat], exact pow_mem _ hx n }
| -[1+ n] := by { rw [gpow_neg_succ_of_nat], exact K.inv_mem (K.pow_mem hx n.succ) }
/-- Construct a subgroup from a nonempty set that is closed under division. -/
@[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"]
def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G :=
have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx,
have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 x one_mem hx,
{ carrier := s,
one_mem' := one_mem,
inv_mem' := inv_mem,
mul_mem' := λ x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) }
/-- A subgroup of a group inherits a multiplication. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an addition."]
instance has_mul : has_mul H := H.to_submonoid.has_mul
/-- A subgroup of a group inherits a 1. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a zero."]
instance has_one : has_one H := H.to_submonoid.has_one
/-- A subgroup of a group inherits an inverse. -/
@[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."]
instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩
/-- A subgroup of a group inherits a division -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."]
instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩
@[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl
@[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : G) = ↑x / ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl
attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero
add_subgroup.coe_neg add_subgroup.coe_mk
/-- A subgroup of a group inherits a group structure. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."]
instance to_group {G : Type*} [group G] (H : subgroup G) : group H :=
subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `comm_group` is a `comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."]
instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H :=
subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."]
instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) :
ordered_comm_group H :=
subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a
`linear_ordered_add_comm_group`."]
instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G]
(H : subgroup G) : linear_ordered_comm_group H :=
subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- The natural group hom from a subgroup of group `G` to `G`. -/
@[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."]
def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl
@[simp, norm_cast] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H ▸ monoid_hom.map_pow _ _ _
@[simp, norm_cast] lemma coe_gpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H ▸ monoid_hom.map_gpow _ _ _
/-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/
@[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."]
def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K :=
monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl)
@[simp, to_additive]
lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a :=
by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] }
@[simp, to_additive]
lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) :
K.subtype.comp (inclusion hH) = H.subtype :=
by { ext, simp }
/-- The subgroup `G` of the group `G`. -/
@[to_additive "The `add_subgroup G` of the `add_group G`."]
instance : has_top (subgroup G) :=
⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩
/-- The trivial subgroup `{1}` of an group `G`. -/
@[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."]
instance : has_bot (subgroup G) :=
⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩
@[to_additive]
instance : inhabited (subgroup G) := ⟨⊥⟩
@[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl
@[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x
@[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl
@[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl
@[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) :=
begin
rw set_like.ext'_iff,
simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and],
end
@[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = ⊥ :=
begin
rw subgroup.eq_bot_iff_forall,
intros y hy,
rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one],
end
@[to_additive] instance fintype_bot : fintype (⊥ : subgroup G) := ⟨{1},
by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩
@[simp] lemma _root_.add_subgroup.card_bot : fintype.card (⊥ : add_subgroup A) = 1 :=
fintype.card_eq_one_iff.2
⟨⟨(0 : A), set.mem_singleton 0⟩, λ ⟨y, hy⟩, subtype.eq $ add_subgroup.mem_bot.1 hy⟩
-- `@[to_additive]` doesn't work, because it converts the `1 : ℕ` to `0`.
@[simp] lemma card_bot : fintype.card (⊥ : subgroup G) = 1 :=
fintype.card_eq_one_iff.2
⟨⟨(1 : G), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩
@[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G]
(h : fintype.card H = fintype.card G) : H = ⊤ :=
begin
haveI : fintype (H : set G) := ‹fintype H›,
rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj,
← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card],
congr
end
@[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) :
nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) :=
subtype.nontrivial_iff_exists_ne (λ x, x ∈ H) (1 : H)
/-- A subgroup is either the trivial subgroup or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H :=
begin
classical,
by_cases h : ∀ x ∈ H, x = (1 : G),
{ left,
exact H.eq_bot_iff_forall.mpr h },
{ right,
push_neg at h,
simpa [nontrivial_iff_exists_ne_one] using h },
end
/-- A subgroup is either the trivial subgroup or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) :=
begin
convert H.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
/-- The inf of two subgroups is their intersection. -/
@[to_additive "The inf of two `add_subgroups`s is their intersection."]
instance : has_inf (subgroup G) :=
⟨λ H₁ H₂,
{ inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩,
.. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩
@[simp, to_additive]
lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl
@[simp, to_additive]
lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
@[to_additive]
instance : has_Inf (subgroup G) :=
⟨λ s,
{ inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h),
.. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩
@[simp, to_additive]
lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl
attribute [norm_cast] coe_Inf add_subgroup.coe_Inf
@[simp, to_additive]
lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff
@[to_additive]
lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i :=
by simp only [infi, mem_Inf, set.forall_range_iff]
@[simp, to_additive]
lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i :=
by simp only [infi, coe_Inf, set.bInter_range]
attribute [norm_cast] coe_infi add_subgroup.coe_infi
/-- Subgroups of a group form a complete lattice. -/
@[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."]
instance : complete_lattice (subgroup G) :=
{ bot := (⊥),
bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem,
top := (⊤),
le_top := λ S x hx, mem_top x,
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 (subgroup G) $ λ s, is_glb.of_image
(λ H K, show (H : set G) ≤ K ↔ H ≤ K, from set_like.coe_subset_coe) is_glb_binfi }
@[to_additive]
lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
@[to_additive]
lemma mem_supr_of_mem {ι : Type*} {S : ι → subgroup G} (i : ι) :
∀ {x : G}, x ∈ S i → x ∈ supr S :=
show S i ≤ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G}
(hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S :=
show s ≤ Sup S, from le_Sup hs
@[to_additive]
lemma subsingleton_iff : subsingleton G ↔ subsingleton (subgroup G) :=
⟨ λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩,
λ h, by exactI ⟨λ x y,
have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i,
(this x).trans (this y).symm⟩⟩
@[to_additive]
lemma nontrivial_iff : nontrivial G ↔ nontrivial (subgroup G) :=
not_iff_not.mp (
(not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans
not_nontrivial_iff_subsingleton.symm)
@[to_additive]
instance [subsingleton G] : unique (subgroup G) :=
⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mp ‹_›) a _⟩
@[to_additive]
instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mp ‹_›
@[to_additive] lemma eq_top_iff' : H = ⊤ ↔ ∀ x : G, x ∈ H :=
eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩
/-- The `subgroup` generated by a set. -/
@[to_additive "The `add_subgroup` generated by a set"]
def closure (k : set G) : subgroup G := Inf {K | k ⊆ K}
variable {k : set G}
@[to_additive]
lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K :=
mem_Inf
/-- The subgroup generated by a set includes the set. -/
@[simp, to_additive "The `add_subgroup` generated by a set includes the set."]
lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx
open set
/-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/
@[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"]
lemma closure_le : closure k ≤ K ↔ k ⊆ K :=
⟨subset.trans subset_closure, λ h, Inf_le h⟩
@[to_additive]
lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K :=
le_antisymm ((closure_le $ K).2 h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and
is preserved under multiplication and inverse, then `p` holds for all elements of the closure
of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements
of the additive closure of `k`."]
lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹) : p x :=
(@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h
attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction
/-- An induction principle on elements of the subtype `subgroup.closure`.
If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse,
then `p` holds for all elements `x : closure k`.
The difference with `subgroup.closure_induction` is that this acts on the subtype.
-/
@[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`.
If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation,
then `p` holds for all elements `x : closure k`.
The difference with `add_subgroup.closure_induction` is that this acts on the subtype."]
lemma closure_induction' (k : set G) {p : closure k → Prop}
(Hk : ∀ x (h : x ∈ k), p ⟨x, subset_closure h⟩)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹)
(x : closure k) :
p x :=
subtype.rec_on x $ λ x hx, begin
refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p ⟨x, hx⟩), hc),
exact closure_induction hx
(λ x hx, ⟨subset_closure hx, Hk x hx⟩)
⟨one_mem _, H1⟩
(λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy,
⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩)
(λ x hx, exists.elim hx $ λ hx' hx, ⟨inv_mem _ hx', Hinv _ hx⟩),
end
attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction'
variable (G)
/-- `closure` forms a Galois insertion with the coercion to set. -/
@[to_additive "`closure` forms a Galois insertion with the coercion to set."]
protected def gi : galois_insertion (@closure G _) coe :=
{ choice := λ s _, closure s,
gc := λ s t, @closure_le _ _ t s,
le_l_u := λ s, subset_closure,
choice_eq := λ s h, rfl }
variable {G}
/-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`. -/
@[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`"]
lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k :=
(subgroup.gi G).gc.monotone_l h'
/-- Closure of a subgroup `K` equals `K`. -/
@[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"]
lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K
@[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ :=
(subgroup.gi G).gc.l_bot
@[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ :=
@coe_top G _ ▸ closure_eq ⊤
@[to_additive]
lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t :=
(subgroup.gi G).gc.l_sup
@[to_additive]
lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(subgroup.gi G).gc.l_supr
@[to_additive]
lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) :
closure S = ⊥ ↔ S ⊆ {1} :=
by { rw [← le_bot_iff], exact closure_le _}
/-- The subgroup generated by an element of a group equals the set of integer number powers of
the element. -/
lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y :=
begin
refine ⟨λ hy, closure_induction hy _ _ _ _,
λ ⟨n, hn⟩, hn ▸ gpow_mem _ (subset_closure $ mem_singleton x) n⟩,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact ⟨1, gpow_one x⟩ },
{ exact ⟨0, gpow_zero x⟩ },
{ rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩,
exact ⟨n + m, gpow_add x n m⟩ },
rintros _ ⟨n, rfl⟩,
exact ⟨-n, gpow_neg x n⟩
end
lemma closure_singleton_one : closure ({1} : set G) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ ⊆ closure S :=
begin
intros s hs,
rw [set_like.mem_coe, ←subgroup.inv_mem_iff],
exact subset_closure (mem_inv.mp hs),
end
@[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S :=
begin
refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _),
{ exact inv_subset_closure S },
{ simpa only [set.inv_inv] using inv_subset_closure S⁻¹ },
end
@[to_additive]
lemma closure_to_submonoid (S : set G) :
(closure S).to_submonoid = submonoid.closure (S ∪ S⁻¹) :=
begin
refine le_antisymm _ (submonoid.closure_le.2 _),
{ intros x hx,
refine closure_induction hx (λ x hx, submonoid.closure_mono (subset_union_left S S⁻¹)
(submonoid.subset_closure hx)) (submonoid.one_mem _) (λ x y hx hy, submonoid.mul_mem _ hx hy)
(λ x hx, _),
rwa [←submonoid.mem_closure_inv, set.union_inv, set.inv_inv, set.union_comm] },
{ simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] }
end
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of
`k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of
the closure of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k` and their negation, and is preserved under addition, then `p` holds for all
elements of the additive closure of `k`."]
lemma closure_induction'' {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (Hk_inv : ∀ x ∈ k, p x⁻¹) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y)) : p x :=
begin
rw [← mem_to_submonoid, closure_to_submonoid k] at h,
refine submonoid.closure_induction h (λ x hx, _) H1 (λ x y hx hy, Hmul x y hx hy),
{ rw [mem_union, mem_inv] at hx,
cases hx with mem invmem,
{ exact Hk x mem },
{ rw [← inv_inv x],
exact Hk_inv _ invmem } },
end
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K)
{x : G} :
x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩,
suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i,
by simpa only [closure_Union, closure_eq (K _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _),
{ exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hK i j with ⟨k, hki, hkj⟩,
exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ },
rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) :
((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty)
(hK : directed_on (≤) K) {x : G} :
x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s :=
begin
haveI : nonempty K := Kne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk]
end
variables {N : Type*} [group N] {P : Type*} [group P]
/-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def comap {N : Type*} [group N] (f : G →* N)
(H : subgroup N) : subgroup G :=
{ carrier := (f ⁻¹' H),
inv_mem' := λ a ha,
show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha,
.. H.to_submonoid.comap f }
@[simp, to_additive]
lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl
@[simp, to_additive]
lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl
@[to_additive]
lemma comap_mono {f : G →* N} {K K' : subgroup N} : K ≤ K' → comap f K ≤ comap f K' :=
preimage_mono
@[to_additive]
lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) :
(K.comap g).comap f = K.comap (g.comp f) :=
rfl
/-- The image of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def map (f : G →* N) (H : subgroup G) : subgroup N :=
{ carrier := (f '' H),
inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ },
.. H.to_submonoid.map f }
@[simp, to_additive]
lemma coe_map (f : G →* N) (K : subgroup G) :
(K.map f : set N) = f '' K := rfl
@[simp, to_additive]
lemma mem_map {f : G →* N} {K : subgroup G} {y : N} :
y ∈ K.map f ↔ ∃ x ∈ K, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : G →* N) {K : subgroup G} {x : G} (hx : x ∈ K) : f x ∈ K.map f :=
mem_image_of_mem f hx
@[to_additive]
lemma apply_coe_mem_map (f : G →* N) (K : subgroup G) (x : K) : f x ∈ K.map f :=
mem_map_of_mem f x.prop
@[to_additive]
lemma map_mono {f : G →* N} {K K' : subgroup G} : K ≤ K' → map f K ≤ map f K' :=
image_subset _
@[to_additive]
lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) :=
set_like.coe_injective $ image_image _ _ _
@[to_additive]
lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} :
K.map f ≤ H ↔ K ≤ H.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) :=
λ _ _, map_le_iff_le_comap
@[to_additive]
lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive] lemma comap_sup_comap_le
(H K : subgroup N) (f : G →* N) : comap f H ⊔ comap f K ≤ comap f (H ⊔ K) :=
monotone.le_map_sup (λ _ _, comap_mono) H K
@[to_additive] lemma supr_comap_le {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(⨆ i, (s i).comap f) ≤ (supr s).comap f :=
monotone.le_map_supr (λ _ _, comap_mono)
@[to_additive]
lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[to_additive] lemma map_inf_le (H K : subgroup G) (f : G →* N) :
map f (H ⊓ K) ≤ map f H ⊓ map f K :=
le_inf (map_mono inf_le_left) (map_mono inf_le_right)
@[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G →* N) (hf : function.injective f) :
map f (H ⊓ K) = map f H ⊓ map f K :=
begin
rw ← set_like.coe_set_eq,
simp [set.image_inter hf],
end
@[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp, to_additive]
lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K :=
ext $ λ x, and_iff_right_of_imp (λ _, x.prop)
@[simp, to_additive]
lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H :=
ext $ λ x, and_iff_left_of_imp (λ _, x.prop)
/-- For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`. -/
@[to_additive "For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`."]
def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype
@[to_additive] lemma coe_subgroup_of (H K : subgroup G) :
(H.subgroup_of K : set K) = K.subtype ⁻¹' H := rfl
@[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} :
h ∈ H.subgroup_of K ↔ (h : G) ∈ H :=
iff.rfl
@[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) :
(H.subgroup_of K).map K.subtype = H ⊓ K := set_like.ext'
begin
convert set.image_preimage_eq_inter_range,
simp only [subtype.range_coe_subtype, coe_subtype, coe_inf],
refl,
end
/-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/
@[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K`
as an `add_subgroup` of `A × B`."]
def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) :=
{ inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩,
.. submonoid.prod H.to_submonoid K.to_submonoid}
@[to_additive coe_prod]
lemma coe_prod (H : subgroup G) (K : subgroup N) :
(H.prod K : set (G × N)) = (H : set G).prod (K : set N) := rfl
@[to_additive mem_prod]
lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} :
p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl
@[to_additive prod_mono]
lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) :=
λ s s' hs t t' ht, set.prod_mono hs ht
@[to_additive prod_mono_right]
lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) :=
prod_mono (le_refl K)
@[to_additive prod_mono_left]
lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) :=
λ s₁ s₂ hs, prod_mono hs (le_refl H)
@[to_additive prod_top]
lemma prod_top (K : subgroup G) :
K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (H : subgroup N) :
(⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ :=
set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk]
/-- Product of subgroups is isomorphic to their product as groups. -/
@[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product
as additive groups"]
def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K :=
{ map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K }
/-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/
structure normal : Prop :=
(conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H)
attribute [class] normal
end subgroup
namespace add_subgroup
/-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/
structure normal (H : add_subgroup A) : Prop :=
(conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H)
attribute [to_additive add_subgroup.normal] subgroup.normal
attribute [class] normal
end add_subgroup
namespace subgroup
variables {H K : subgroup G}
@[priority 100, to_additive]
instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal :=
⟨by simp [mul_comm, mul_left_comm]⟩
namespace normal
variable (nH : H.normal)
@[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H :=
have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa
@[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H :=
⟨nH.mem_comm, nH.mem_comm⟩
end normal
@[priority 100, to_additive]
instance bot_normal : normal (⊥ : subgroup G) := ⟨by simp⟩
@[priority 100, to_additive]
instance top_normal : normal (⊤ : subgroup G) := ⟨λ _ _, mem_top⟩
variable (G)
/-- The center of a group `G` is the set of elements that commute with everything in `G` -/
@[to_additive "The center of a group `G` is the set of elements that commute with everything in
`G`"]
def center : subgroup G :=
{ carrier := {z | ∀ g, g * z = z * g},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ g, g * a = a * g) (hb : ∀ g, g * b = b * g) g,
by assoc_rw [ha, hb g],
inv_mem' := λ a (ha : ∀ g, g * a = a * g) g,
by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] }
variable {G}
@[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl
@[priority 100, to_additive]
instance center_normal : (center G).normal :=
⟨begin
assume n hn g h,
assoc_rw [hn (h * g), hn g],
simp
end⟩
variables {G} (H)
/-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/
@[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."]
def normalizer : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
-- variant for sets.
-- TODO should this replace `normalizer`?
/-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/
@[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy
`g+S-g=S`."]
def set_normalizer (S : set G) : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G}
(h : ∀ n, n ∈ S → x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S :=
by haveI := classical.prop_decidable;
haveI := set.fintype_image S (λ n, x * n * x⁻¹); exact
λ n, ⟨h n, λ h₁,
have heq : (λ n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le
(λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective S conj_injective),
have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' S := heq.symm ▸ h₁,
let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩
variable {H}
@[to_additive] lemma mem_normalizer_iff {g : G} :
g ∈ normalizer H ↔ ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl
@[to_additive] lemma le_normalizer : H ≤ normalizer H :=
λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH]
@[priority 100, to_additive]
instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal :=
⟨λ x xH g, by simpa using (g.2 x).1 xH⟩
open_locale classical
@[to_additive]
lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer :=
λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩,
λ yH, by simpa [mem_comap, mul_assoc] using
hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩
end subgroup
namespace group
variables {s : set G}
/-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of
the elements of `s`. -/
def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a
lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x :=
set.mem_bUnion_iff
theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s :=
λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩
theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) :
conjugates_of_set s ⊆ conjugates_of_set t :=
set.bUnion_subset_bUnion_left h
lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) :
conjugates_of a ⊆ N :=
by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c }
theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) :
conjugates_of_set s ⊆ N :=
set.bUnion_subset (λ x H, conjugates_subset_normal (h H))
/-- The set of conjugates of `s` is closed under conjugation. -/
lemma conj_mem_conjugates_of_set {x c : G} :
x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) :=
λ H,
begin
rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩,
exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ (is_conj_iff.2 ⟨c,rfl⟩)⟩,
end
end group
namespace subgroup
open group
variable {s : set G}
/-- The normal closure of a set `s` is the subgroup closure of all the conjugates of
elements of `s`. It is the smallest normal subgroup containing `s`. -/
def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s :=
subset_closure
theorem subset_normal_closure : s ⊆ normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H :=
λ _ h, subset_normal_closure h
/-- The normal closure of `s` is a normal subgroup. -/
instance normal_closure_normal : (normal_closure s).normal :=
⟨λ n h g,
begin
refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) },
{ simpa using (normal_closure s).one_mem },
{ rw ← conj_mul,
exact mul_mem _ ihx ihy },
{ rw ← conj_inv,
exact inv_mem _ ihx }
end⟩
/-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/
theorem normal_closure_le_normal {N : subgroup G} [N.normal]
(h : s ⊆ N) : normal_closure s ≤ N :=
begin
assume a w,
refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset h hx) },
{ exact subgroup.one_mem _ },
{ exact subgroup.mul_mem _ ihx ihy },
{ exact subgroup.inv_mem _ ihx }
end
lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N :=
⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t :=
normal_closure_le_normal (set.subset.trans h subset_normal_closure)
theorem normal_closure_eq_infi : normal_closure s =
⨅ (N : subgroup G) [normal N] (hs : s ⊆ N), N :=
le_antisymm
(le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal))))
(infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance)
(infi_le_of_le subset_normal_closure (le_refl _))))
@[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H :=
le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure)
@[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s :=
normal_closure_eq_self _
theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s :=
by simp only [subset_normal_closure, closure_le]
@[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} :
normal_closure ↑(closure s) = normal_closure s :=
le_antisymm (normal_closure_le_normal closure_le_normal_closure)
(normal_closure_mono subset_closure)
end subgroup
namespace add_subgroup
open set
lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) :
∀ n : ℤ, n • x ∈ H
| (n : ℕ) := by { rw [gsmul_coe_nat], exact add_submonoid.nsmul_mem H.to_add_submonoid hx n }
| -[1+ n] := begin
rw gsmul_neg_succ_of_nat,
apply H.neg_mem',
exact add_submonoid.nsmul_mem H.to_add_submonoid hx n.succ
end
/-- The `add_subgroup` generated by an element of an `add_group` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y ∈ closure ({x} : set A) ↔ ∃ n : ℤ, n • x = y :=
begin
refine ⟨λ hy, closure_induction hy _ _ _ _,
λ ⟨n, hn⟩, hn ▸ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact ⟨1, one_gsmul x⟩ },
{ exact ⟨0, zero_gsmul x⟩ },
{ rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩,
exact ⟨n + m, add_gsmul x n m⟩ },
{ rintros _ ⟨n, rfl⟩,
refine ⟨-n, neg_gsmul x n⟩ }
end
lemma closure_singleton_zero : closure ({0} : set A) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
variable (H : add_subgroup A)
@[simp] lemma coe_smul (x : H) (n : ℕ) : ((n • x : H) : A) = n • x :=
coe_subtype H ▸ add_monoid_hom.map_nsmul _ _ _
@[simp] lemma coe_gsmul (x : H) (n : ℤ) : ((n • x : H) : A) = n • x :=
coe_subtype H ▸ add_monoid_hom.map_gsmul _ _ _
attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow
attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow
end add_subgroup
namespace monoid_hom
variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G)
open subgroup
/-- The range of a monoid homomorphism from a group is a subgroup. -/
@[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."]
def range (f : G →* N) : subgroup N :=
subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff])
@[to_additive]
instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] :
decidable_pred (∈ f.range) :=
λ x, fintype.decidable_exists_fintype
@[simp, to_additive] lemma coe_range (f : G →* N) :
(f.range : set N) = set.range f := rfl
@[simp, to_additive] lemma mem_range {f : G →* N} {y : N} :
y ∈ f.range ↔ ∃ x, f x = y :=
iff.rfl
@[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f :=
by ext; simp
/-- The canonical surjective group homomorphism `G →* f(G)` induced by a group
homomorphism `G →* N`. -/
@[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group
homomorphism `G →+ N`."]
def range_restrict (f : G →* N) : G →* f.range :=
monoid_hom.mk' (λ g, ⟨f g, ⟨g, rfl⟩⟩) $ λ a b, by {ext, exact f.map_mul' _ _}
@[simp, to_additive]
lemma coe_range_restrict (f : G →* N) (g : G) : (f.range_restrict g : N) = f g := rfl
@[to_additive]
lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range :=
by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f
@[to_additive]
lemma range_top_iff_surjective {N} [group N] {f : G →* N} :
f.range = (⊤ : subgroup N) ↔ function.surjective f :=
set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid homomorphism is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."]
lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) :
f.range = (⊤ : subgroup N) :=
range_top_iff_surjective.2 hf
@[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H :=
by { rw [range_eq_map, ← set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp }
/-- Restriction of a group hom to a subgroup of the domain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."]
def restrict (f : G →* N) (H : subgroup G) : H →* N :=
f.comp H.subtype
@[simp, to_additive]
lemma restrict_apply {H : subgroup G} (f : G →* N) (x : H) :
f.restrict H x = f (x : G) := rfl
/-- Restriction of a group hom to a subgroup of the codomain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."]
def cod_restrict (f : G →* N) (S : subgroup N) (h : ∀ x, f x ∈ S) : G →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
@[simp, to_additive]
lemma cod_restrict_apply {G : Type*} [group G] {N : Type*} [group N] (f : G →* N)
(S : subgroup N) (h : ∀ (x : G), f x ∈ S) {x : G} :
f.cod_restrict S h x = ⟨f x, h x⟩ := rfl
/-- Computable alternative to `monoid_hom.of_injective`. -/
def of_left_inverse {f : G →* N} {g : N →* G} (h : function.left_inverse g f) : G ≃* f.range :=
{ to_fun := f.range_restrict,
inv_fun := g ∘ f.range.subtype,
left_inv := h,
right_inv := by
{ rintros ⟨x, y, rfl⟩,
apply subtype.ext,
rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] },
.. f.range_restrict }
@[simp] lemma of_left_inverse_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : G) :
↑(of_left_inverse h x) = f x := rfl
@[simp] lemma of_left_inverse_symm_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : f.range) :
(of_left_inverse h).symm x = g x := rfl
/-- The range of an injective group homomorphism is isomorphic to its domain. -/
noncomputable def of_injective {f : G →* N} (hf : function.injective f) : G ≃* f.range :=
(mul_equiv.of_bijective (f.cod_restrict f.range (λ x, ⟨x, rfl⟩))
⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩)
lemma of_injective_apply {f : G →* N} (hf : function.injective f) {x : G} :
↑(of_injective hf x) = f x := rfl
/-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that
`f x = 1` -/
@[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements
such that `f x = 0`"]
def ker (f : G →* N) := (⊥ : subgroup N).comap f
@[to_additive]
lemma mem_ker (f : G →* N) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl
@[to_additive]
lemma coe_ker (f : G →* N) : (f.ker : set G) = (f : G → N) ⁻¹' {1} := rfl
@[to_additive]
lemma eq_iff (f : G →* N) {x y : G} : f x = f y ↔ y⁻¹ * x ∈ f.ker :=
by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm]
@[to_additive]
instance decidable_mem_ker [decidable_eq N] (f : G →* N) :
decidable_pred (∈ f.ker) :=
λ x, decidable_of_iff (f x = 1) f.mem_ker
@[to_additive]
lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl
@[simp, to_additive] lemma comap_bot (f : G →* N) :
(⊥ : subgroup N).comap f = f.ker := rfl
@[to_additive] lemma range_restrict_ker (f : G →* N) : ker (range_restrict f) = ker f :=
begin
ext,
change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1,
simp only [],
end
@[simp, to_additive]
lemma ker_one : (1 : G →* N).ker = ⊤ :=
by { ext, simp [mem_ker] }
@[to_additive] lemma ker_eq_bot_iff (f : G →* N) : f.ker = ⊥ ↔ function.injective f :=
begin
split,
{ intros h x y hxy,
rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy },
{ exact λ h, le_bot_iff.mp (λ x hx, h (hx.trans f.map_one.symm)) },
end
@[to_additive]
lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N']
(f : G →* N) (g : G' →* N') (S : subgroup N) (S' : subgroup N') :
(S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) :=
set_like.coe_injective $ set.preimage_prod_map_prod f g _ _
@[to_additive]
lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G →* N) (g : G' →* N') :
(prod_map f g).ker = f.ker.prod g.ker :=
begin
dsimp only [ker],
rw [←prod_map_comap_prod, bot_prod_bot],
end
/-- The subgroup of elements `x : G` such that `f x = g x` -/
@[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"]
def eq_locus (f g : G →* N) : subgroup G :=
{ inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx],
.. eq_mlocus f g}
/-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/
@[to_additive]
lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s ≤ f.eq_locus g, from (closure_le _).2 h
@[to_additive]
lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) :
f = g :=
ext $ λ x, h trivial
@[to_additive]
lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) :
f = g :=
eq_of_eq_on_top $ hs ▸ eq_on_closure h
@[to_additive]
lemma gclosure_preimage_le (f : G →* N) (s : set N) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
(closure_le _).2 $ λ x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx
/-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup
generated by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals
the `add_subgroup` generated by the image of the set."]
lemma map_closure (f : G →* N) (s : set G) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s)
(gclosure_preimage_le _ _))
((closure_le _).2 $ set.image_subset _ subset_closure)
end monoid_hom
namespace subgroup
variables {N : Type*} [group N] (H : subgroup G)
@[to_additive] lemma map_eq_bot_iff {f : G →* N} : H.map f = ⊥ ↔ H ≤ f.ker :=
begin
rw eq_bot_iff,
split,
{ exact λ h x hx, h ⟨x, hx, rfl⟩ },
{ intros h x hx,
obtain ⟨y, hy, rfl⟩ := hx,
exact h hy },
end
@[to_additive]
lemma map_eq_bot_iff_of_injective {f : G →* N} (hf : function.injective f) : H.map f = ⊥ ↔ H = ⊥ :=
by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff]
end subgroup
namespace subgroup
open monoid_hom
variables {N : Type*} [group N] (f : G →* N)
@[to_additive]
lemma map_le_range (H : subgroup G) : map f H ≤ f.range :=
(range_eq_map f).symm ▸ map_mono le_top
@[to_additive]
lemma ker_le_comap (H : subgroup N) : f.ker ≤ comap f H :=
comap_mono bot_le
@[to_additive]
lemma map_comap_le (H : subgroup N) : map f (comap f H) ≤ H :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma le_comap_map (H : subgroup G) : H ≤ comap f (map f H) :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_eq (H : subgroup N) :
map f (comap f H) = f.range ⊓ H :=
set_like.ext' begin
convert set.image_preimage_eq_inter_range,
simp [set.inter_comm],
end
@[to_additive]
lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H ⊔ f.ker :=
begin
refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)),
intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx,
rcases hx with ⟨y, hy, hy'⟩,
have : y⁻¹ * x ∈ f.ker, { rw mem_ker, simp [hy'] },
convert mul_mem _ (mem_sup_left hy) (mem_sup_right this),
simp,
end
@[to_additive]
lemma map_comap_eq_self {f : G →* N} {H : subgroup N} (h : H ≤ f.range) :
map f (comap f H) = H :=
by rwa [map_comap_eq, inf_eq_right]
@[to_additive]
lemma map_comap_eq_self_of_surjective {f : G →* N} (h : function.surjective f) (H : subgroup N) :
map f (comap f H) = H :=
map_comap_eq_self ((range_top_of_surjective _ h).symm ▸ le_top)
@[to_additive]
lemma comap_injective {f : G →* N} (h : function.surjective f) : function.injective (comap f) :=
λ K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL }
@[to_additive]
lemma comap_map_eq_self {f : G →* N} {H : subgroup G} (h : f.ker ≤ H) :
comap f (map f H) = H :=
by rwa [comap_map_eq, sup_eq_left]
@[to_additive]
lemma comap_map_eq_self_of_injective {f : G →* N} (h : function.injective f) (H : subgroup G) :
comap f (map f H) = H :=
comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm ▸ bot_le)
@[to_additive]
lemma map_injective {f : G →* N} (h : function.injective f) : function.injective (map f) :=
λ K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL }
@[to_additive]
lemma map_eq_comap_of_inverse {f : G →* N} {g : N →* G} (hl : function.left_inverse g f)
(hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H :=
set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr]
/-- Given `f(A) = f(B)`, `ker f ≤ A`, and `ker f ≤ B`, deduce that `A = B` -/
@[to_additive] lemma map_injective_of_ker_le
{H K : subgroup G} (hH : f.ker ≤ H) (hK : f.ker ≤ K) (hf : map f H = map f K) :
H = K :=
begin
apply_fun comap f at hf,
rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf,
end
@[to_additive] lemma comap_sup_eq
(H K : subgroup N) (hf : function.surjective f):
comap f H ⊔ comap f K = comap f (H ⊔ K) :=
begin
have : map f (comap f H ⊔ comap f K) = map f (comap f (H ⊔ K)),
{ simp [subgroup.map_comap_eq, map_sup, f.range_top_of_surjective hf], },
refine map_injective_of_ker_le f _ _ this,
{ calc f.ker ≤ comap f H : ker_le_comap f _
... ≤ comap f H ⊔ comap f K : le_sup_left, },
exact ker_le_comap _ _,
end
end subgroup
namespace monoid_hom
variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃]
variables (f : G₁ →* G₂) (f_inv : G₂ → G₁)
/-- Auxiliary definition used to define `lift_of_right_inverse` -/
@[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"]
def lift_of_right_inverse_aux
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) :
G₂ →* G₃ :=
{ to_fun := λ b, g (f_inv b),
map_one' := hg (hf 1),
map_mul' :=
begin
intros x y,
rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul],
simp only [hf _],
end }
@[simp, to_additive]
lemma lift_of_right_inverse_aux_comp_apply
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) :
(f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x :=
begin
dsimp [lift_of_right_inverse_aux],
rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one],
simp only [hf _],
end
/-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `φ`
* such that `φ.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+* G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`.
See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \
f | \ g
| \
v \⌟
G₂----> G₃
∃!φ
```
-/
@[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `φ`
* such that `φ.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+ G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+ G₃` satisfies `hg : f.ker ≤ g.ker`.
See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \\
f | \\ g
| \\
v \\⌟
G₂----> G₃
∃!φ
```"]
def lift_of_right_inverse
(hf : function.right_inverse f_inv f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
{ to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2,
inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩,
left_inv := λ g, by {
ext,
simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk,
subtype.val_eq_coe], },
right_inv := λ φ, by {
ext b,
simp [lift_of_right_inverse_aux, hf b], } }
/-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right
inverse is available, that uses `function.surj_inv`. -/
@[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no
computable right inverse is available."]
noncomputable abbreviation lift_of_surjective
(hf : function.surjective f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf)
@[simp, to_additive]
lemma lift_of_right_inverse_comp_apply
(hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) (x : G₁) :
(f.lift_of_right_inverse f_inv hf g) (f x) = g x :=
f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x
@[simp, to_additive]
lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f)
(g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) :
(f.lift_of_right_inverse f_inv hf g).comp f = g :=
monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g
@[to_additive]
lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ →* G₃)
(hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) :
h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) :=
begin
simp_rw ←hh,
exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm,
end
end monoid_hom
variables {N : Type*} [group N]
-- Here `H.normal` is an explicit argument so we can use dot notation with `comap`.
@[to_additive]
lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) :
(H.comap f).normal :=
⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩
@[priority 100, to_additive]
instance subgroup.normal_comap {H : subgroup N}
[nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _
@[priority 100, to_additive]
instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal :=
by rw [monoid_hom.ker]; apply_instance
@[priority 100, to_additive]
instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] :
((H ⊓ N).comap H.subtype).normal :=
⟨λ x hx g, begin
simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx,
simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap],
exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩,
end⟩
namespace subgroup
/-- The subgroup generated by an element. -/
def gpowers (g : G) : subgroup G :=
subgroup.copy (gpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl
@[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩
lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl
lemma gpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : gpowers a ≤ K :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.gpow_mem h i end
lemma mem_gpowers_iff {g h : G} :
h ∈ gpowers g ↔ ∃ (k : ℤ), g ^ k = h :=
iff.rfl
end subgroup
namespace add_subgroup
/-- The subgroup generated by an element. -/
def gmultiples (a : A) : add_subgroup A :=
add_subgroup.copy (gmultiples_hom A a).range (set.range ((• a) : ℤ → A)) rfl
@[simp] lemma mem_gmultiples (a : A) : a ∈ gmultiples a := ⟨1, one_gsmul _⟩
lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl
lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a ∈ B) : gmultiples a ≤ B :=
@subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h
lemma mem_gmultiples_iff {a b : A} :
b ∈ add_subgroup.gmultiples a ↔ ∃ (k : ℤ), k • a = b :=
iff.rfl
attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers
attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers
attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure
attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom
attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset
attribute [to_additive add_subgroup.mem_gmultiples_iff] subgroup.mem_gpowers_iff
end add_subgroup
lemma int.mem_gmultiples_iff {a b : ℤ} :
b ∈ add_subgroup.gmultiples a ↔ a ∣ b :=
exists_congr (λ k, by rw [mul_comm, eq_comm, ← smul_eq_mul])
lemma of_mul_image_gpowers_eq_gmultiples_of_mul { x : G } :
additive.of_mul '' ((subgroup.gpowers x) : set G) = add_subgroup.gmultiples (additive.of_mul x) :=
begin
ext y,
split,
{ rintro ⟨z, ⟨m, hm⟩, hz2⟩,
use m,
simp only,
rwa [← of_mul_gpow, hm] },
{ rintros ⟨n, hn⟩,
refine ⟨x ^ n, ⟨n, rfl⟩, _⟩,
rwa of_mul_gpow }
end
lemma of_add_image_gmultiples_eq_gpowers_of_add {x : A} :
multiplicative.of_add '' ((add_subgroup.gmultiples x) : set A) =
subgroup.gpowers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_gpowers_eq_gmultiples_of_mul,
end
namespace mul_equiv
variables {H K : subgroup G}
/-- Makes the identity isomorphism from a proof two subgroups of a multiplicative
group are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof
two subgroups of an additive group are equal."]
def subgroup_congr (h : H = K) : H ≃* K :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h }
end mul_equiv
-- TODO : ↥(⊤ : subgroup H) ≃* H ?
namespace subgroup
variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C}
@[to_additive]
lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
⟨λ h, begin
rw [← closure_eq s, ← closure_eq t, ← closure_union] at h,
apply closure_induction h,
{ rintro y (h | h),
{ exact ⟨y, h, 1, t.one_mem, by simp⟩ },
{ exact ⟨1, s.one_mem, y, h, by simp⟩ } },
{ exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ },
{ rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩,
exact ⟨_, mul_mem _ hy₁ hy₂, _, mul_mem _ hz₁ hz₂, by simp [mul_assoc]; cc⟩ },
{ rintro _ ⟨y, hy, z, hz, rfl⟩,
exact ⟨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ }
end,
by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem _
((le_sup_left : s ≤ s ⊔ t) hy)
((le_sup_right : t ≤ s ⊔ t) hz)⟩
@[to_additive]
lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x :=
mem_sup.trans $ by simp only [set_like.exists, coe_mk]
@[to_additive]
instance : is_modular_lattice (subgroup C) :=
⟨λ x y z xz a ha, begin
rw [mem_inf, mem_sup] at ha,
rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩,
rw mem_sup,
refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩,
rw ← inv_mul_cancel_left b c,
apply z.mul_mem (z.inv_mem (xz hb)) haz,
end⟩
end subgroup
section
variables (G) (A)
/-- A `group` is simple when it has exactly two normal `subgroup`s. -/
class is_simple_group extends nontrivial G : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : subgroup G, H.normal → H = ⊥ ∨ H = ⊤)
/-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/
class is_simple_add_group extends nontrivial A : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : add_subgroup A, H.normal → H = ⊥ ∨ H = ⊤)
attribute [to_additive] is_simple_group
variables {G} {A}
@[to_additive]
lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) :
H = ⊥ ∨ H = ⊤ :=
is_simple_group.eq_bot_or_eq_top_of_normal H Hn
namespace is_simple_group
@[to_additive]
instance {C : Type*} [comm_group C] [is_simple_group C] :
is_simple_lattice (subgroup C) :=
⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩
open subgroup
@[to_additive]
lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G]
[nontrivial H] (f : G →* H) (hf : function.surjective f) :
is_simple_group H :=
⟨nontrivial.exists_pair_ne, λ H iH, begin
refine ((iH.comap f).eq_bot_or_eq_top).imp (λ h, _) (λ h, _),
{ rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] },
{ rw [←comap_top f] at h, exact comap_injective hf h }
end⟩
end is_simple_group
end
namespace subgroup
section pointwise
@[to_additive]
lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T :=
Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem
(set_like.le_def.mp le_sup_left $ subset_closure hs)
(set_like.le_def.mp le_sup_right $ subset_closure ht)
@[to_additive]
lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) :=
le_antisymm
(sup_le
(λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩)
(λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩))
(by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le)
@[to_additive]
private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (H : set G) * N,
one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩,
⟨h * h', h'⁻¹ * n * h' * n',
H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨h, n, hh, hn, hx⟩,
⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h,
by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ }
/-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition)
when `N` is normal."]
lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N :=
set.subset.antisymm
(show H ⊔ N ≤ mul_normal_aux H N,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure H N).symm ▸ subset_closure)
@[to_additive]
private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (N : set G) * H,
one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩,
⟨n * (h * n' * h⁻¹), h * h',
N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨n, h, hn, hh, hx⟩,
⟨h⁻¹ * n⁻¹ * h, h⁻¹,
by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh,
by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ }
/-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition)
when `N` is normal."]
lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H :=
set.subset.antisymm
(show N ⊔ H ≤ normal_mul_aux N H,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure N H).symm ▸ subset_closure)
@[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A ≤ C) :
(A : set G) * ↑(B ⊓ C) = (A * B) ⊓ C :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, hy, ⟨hzB, hzC⟩, rfl⟩,
refine ⟨_, mul_mem C (h hy) hzC⟩,
exact ⟨y, z, hy, hzB, rfl⟩ },
rintros ⟨⟨y, z, hy, hz, rfl⟩, hyz⟩,
refine ⟨y, z, hy, ⟨hz, _⟩, rfl⟩,
suffices : y⁻¹ * (y * z) ∈ C, { simpa },
exact mul_mem C (inv_mem C (h hy)) hyz
end
@[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C ≤ A) :
((A ⊓ B : subgroup G) : set G) * C = A ⊓ (B * C) :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, ⟨hyA, hyB⟩, hz, rfl⟩,
refine ⟨mul_mem A hyA (h hz), _⟩,
exact ⟨y, z, hyB, hz, rfl⟩ },
rintros ⟨hyz, y, z, hy, hz, rfl⟩,
refine ⟨y, z, ⟨_, hy⟩, hz, rfl⟩,
suffices : (y * z) * z⁻¹ ∈ A, { simpa },
exact mul_mem A hyz (inv_mem A (h hz))
end
end pointwise
section subgroup_normal
@[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H ≤ K) :
(H.subgroup_of K).normal ↔ ∀ h k, h ∈ H → k ∈ K → k * h * k⁻¹ ∈ H :=
⟨λ hN h k hH hK, hN.conj_mem ⟨h, hHK hH⟩ hH ⟨k, hK⟩,
λ hN, { conj_mem := λ h hm k, (hN h.1 k.1 hm k.2) }⟩
@[to_additive] instance prod_subgroup_of_prod_normal
{H₁ K₁ : subgroup G} {H₂ K₂ : subgroup N}
[h₁ : (H₁.subgroup_of K₁).normal] [h₂ : (H₂.subgroup_of K₂).normal] :
((H₁.prod H₂).subgroup_of (K₁.prod K₂)).normal :=
{ conj_mem := λ n hgHK g,
⟨h₁.conj_mem ⟨(n : G × N).fst, (mem_prod.mp n.2).1⟩
hgHK.1 ⟨(g : G × N).fst, (mem_prod.mp g.2).1⟩,
h₂.conj_mem ⟨(n : G × N).snd, (mem_prod.mp n.2).2⟩
hgHK.2 ⟨(g : G × N).snd, (mem_prod.mp g.2).2⟩⟩ }
@[to_additive] instance prod_normal
(H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] :
(H.prod K).normal :=
{ conj_mem := λ n hg g,
⟨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst,
hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.snd⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_right
(A B' B : subgroup G) (hB : B' ≤ B) [hN : (B'.subgroup_of B).normal] :
((A ⊓ B').subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨mul_mem A (mul_mem A (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem A (mem_inf.1 g.2).1),
(normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_left
{A' A : subgroup G} (B : subgroup G) (hA : A' ≤ A) [hN : (A'.subgroup_of A).normal] :
((A' ⊓ B).subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1,
mul_mem B (mul_mem B (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem B (mem_inf.1 g.2).2)⟩ }
instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H ⊔ K).normal :=
{ conj_mem := λ n hmem g,
begin
change n ∈ ↑(H ⊔ K) at hmem,
change g * n * g⁻¹ ∈ ↑(H ⊔ K),
rw [normal_mul, set.mem_mul] at *,
rcases hmem with ⟨h, k, hh, hk, rfl⟩,
refine ⟨g * h * g⁻¹, g * k * g⁻¹, hH.conj_mem h hh g, hK.conj_mem k hk g, _⟩,
simp
end }
@[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] :
(H ⊓ K).normal :=
{ conj_mem := λ n hmem g,
by { rw mem_inf at *, exact ⟨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 g⟩ } }
@[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A ≤ B) (hA' : A' ≤ B) :
(A ⊔ A').subgroup_of B = A.subgroup_of B ⊔ A'.subgroup_of B :=
begin
refine map_injective_of_ker_le B.subtype
(ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _,
{ simp only [subgroup_of, map_comap_eq, map_sup, subtype_range],
rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] },
end
@[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G}
(hK : H ≤ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b ∈ K) (h : a * b ∈ H) :
b * a ∈ H :=
begin
have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb,
rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this,
end
end subgroup_normal
end subgroup
namespace is_conj
open subgroup
lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal]
{g g' : G} {hg : g ∈ N} {hg' : g' ∈ N} (hc : is_conj g g')
(ht : normal_closure ({⟨g, hg⟩} : set N) = ⊤) :
normal_closure ({⟨g', hg'⟩} : set N) = ⊤ :=
begin
obtain ⟨c, rfl⟩ := is_conj_iff.1 hc,
have h : ∀ x : N, (mul_aut.conj c) x ∈ N,
{ rintro ⟨x, hx⟩,
exact hn.conj_mem _ hx c },
have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h),
{ rintro ⟨x, hx⟩,
refine ⟨⟨c⁻¹ * x * c, _⟩, _⟩,
{ have h := hn.conj_mem _ hx c⁻¹,
rwa [inv_inv] at h },
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply,
coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, ← mul_assoc, mul_inv_self, one_mul],
rw [mul_assoc, mul_inv_self, mul_one] },
have ht' := map_mono (eq_top_iff.1 ht),
rw [← monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht',
refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))),
rw [set.singleton_subset_iff, set_like.mem_coe],
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk,
monoid_hom.restrict_apply, mem_comap],
exact subset_normal_closure (set.mem_singleton _),
end
end is_conj
/-! ### Actions by `subgroup`s
These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`.
-/
section actions
namespace subgroup
variables {α β : Type*}
/-- The action by a subgroup is the action by the underlying group. -/
@[to_additive /-"The additive action by an add_subgroup is the action by the underlying
add_group. "-/]
instance [mul_action G α] (S : subgroup G) : mul_action S α :=
S.to_submonoid.mul_action
@[to_additive]
lemma smul_def [mul_action G α] {S : subgroup G} (g : S) (m : α) : g • m = (g : G) • m := rfl
@[to_additive]
instance smul_comm_class_left
[mul_action G β] [has_scalar α β] [smul_comm_class G α β] (S : subgroup G) :
smul_comm_class S α β :=
S.to_submonoid.smul_comm_class_left
@[to_additive]
instance smul_comm_class_right
[has_scalar α β] [mul_action G β] [smul_comm_class α G β] (S : subgroup G) :
smul_comm_class α S β :=
S.to_submonoid.smul_comm_class_right
/-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/
instance
[has_scalar α β] [mul_action G α] [mul_action G β] [is_scalar_tower G α β] (S : subgroup G) :
is_scalar_tower S α β :=
S.to_submonoid.is_scalar_tower
/-- The action by a subgroup is the action by the underlying group. -/
instance [add_monoid α] [distrib_mul_action G α] (S : subgroup G) : distrib_mul_action S α :=
S.to_submonoid.distrib_mul_action
end subgroup
end actions
|
5815be4d3398838bf4822c1c3e3a185ee415c09a | 7b89826c26634aa18c0110f1634f73027851edfe | /logic_and_proof/src/04-02.lean | 1b37b38f4ddc4e148ebb0b6e82829b5e4dd5395d | [
"MIT"
] | permissive | marcofavorito/leanings | b7642344d8c9012a1cec74a804c5884297880c4d | 581b83be66ff4f8dd946fb6a1bb045d2ddf91076 | refs/heads/master | 1,672,310,991,244 | 1,603,031,766,000 | 1,603,031,766,000 | 279,163,004 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 575 | lean | variables A B C D: Prop
variable h : A ∧ ¬ B
#check A ∧ ¬ B → C
#check h
#check and.left h
#check and.right h
#check and.intro (and.right h) (and.left h)
variable h1 : A → B → C
variable h2 : D → A
variable h3 : D
variable h4 : B
#check h2 h3
#check h1 (h2 h3)
#check h1 (h2 h3) h4
#check assume h : A, and.intro h h
#check (assume h : A ∧ ¬ B, and.intro (and.right h) (and.left h))
#check (λ h: A ∧ ¬B, and.intro (and.right h) (and.left h))
-- #check (λ h: A ∧ B, (A ∧ B, h))
#check assume h : A, (¬h)
-- #check (λ h: A ∨ B, and.right h)
|
6daade33761057bf86d5e8c08e474f8bd3f79bdb | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/prop_logic_lean_summary/unnamed_223.lean | 7c8fdcacde61b1174d53aab4db18a67b156bc547 | [] | no_license | gihanmarasingha/mth1001_sphinx | 190a003269ba5e54717b448302a27ca26e31d491 | 05126586cbf5786e521be1ea2ef5b4ba3c44e74a | refs/heads/master | 1,672,913,933,677 | 1,604,516,583,000 | 1,604,516,583,000 | 309,245,750 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 170 | lean | variables p q : Prop
-- BEGIN
example (h : p → q) (k : p) : q :=
begin
apply h, -- This is a backward proof that changes the goal to proving p.
exact k,
end
-- END |
b64e41d1da2d64f30bedfb5d4ddf34fbd77e2b01 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/linear_algebra/dimension.lean | abb6d78cbf8c7c2c6ea8ff247148924647c6b422 | [
"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 | 53,560 | 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, Sander Dahmen, Scott Morrison
-/
import linear_algebra.dfinsupp
import linear_algebra.std_basis
import linear_algebra.isomorphisms
import set_theory.cofinality
import linear_algebra.invariant_basis_number
/-!
# Dimension of modules and vector spaces
## Main definitions
* The rank of a module is defined as `module.rank : cardinal`.
This is defined as the supremum of the cardinalities of linearly independent subsets.
* The rank of a linear map is defined as the rank of its range.
## Main statements
* `linear_map.dim_le_of_injective`: the source of an injective linear map has dimension
at most that of the target.
* `linear_map.dim_le_of_surjective`: the target of a surjective linear map has dimension
at most that of that source.
* `basis_fintype_of_finite_spans`:
the existence of a finite spanning set implies that any basis is finite.
* `infinite_basis_le_maximal_linear_independent`:
if `b` is an infinite basis for a module `M`,
and `s` is a maximal linearly independent set,
then the cardinality of `b` is bounded by the cardinality of `s`.
For modules over rings satisfying the rank condition
* `basis.le_span`:
the cardinality of a basis is bounded by the cardinality of any spanning set
For modules over rings satisfying the strong rank condition
* `linear_independent_le_span`:
For any linearly independent family `v : ι → M`
and any finite spanning set `w : set M`,
the cardinality of `ι` is bounded by the cardinality of `w`.
* `linear_independent_le_basis`:
If `b` is a basis for a module `M`,
and `s` is a linearly independent set,
then the cardinality of `s` is bounded by the cardinality of `b`.
For modules over rings with invariant basis number
(including all commutative rings and all noetherian rings)
* `mk_eq_mk_of_basis`: the dimension theorem, any two bases of the same vector space have the same
cardinality.
For vector spaces (i.e. modules over a field), we have
* `dim_quotient_add_dim`: if `V₁` is a submodule of `V`, then
`module.rank (V/V₁) + module.rank V₁ = module.rank V`.
* `dim_range_add_dim_ker`: the rank-nullity theorem.
## Implementation notes
There is a naming discrepancy: most of the theorem names refer to `dim`,
even though the definition is of `module.rank`.
This reflects that `module.rank` was originally called `dim`, and only defined for vector spaces.
Many theorems in this file are not universe-generic when they relate dimensions
in different universes. They should be as general as they can be without
inserting `lift`s. The types `V`, `V'`, ... all live in different universes,
and `V₁`, `V₂`, ... all live in the same universe.
-/
noncomputable theory
universes u v v' v'' u₁' w w'
variables {K : Type u} {V V₁ V₂ V₃ : Type v} {V' V'₁ : Type v'} {V'' : Type v''}
variables {ι : Type w} {ι' : Type w'} {η : Type u₁'} {φ : η → Type*}
open_locale classical big_operators cardinal
open basis submodule function set
section module
section
variables [semiring K] [add_comm_monoid V] [module K V]
include K
variables (K V)
/-- The rank of a module, defined as a term of type `cardinal`.
We define this as the supremum of the cardinalities of linearly independent subsets.
For a free module over any ring satisfying the strong rank condition
(e.g. left-noetherian rings, commutative rings, and in particular division rings and fields),
this is the same as the dimension of the space (i.e. the cardinality of any basis).
In particular this agrees with the usual notion of the dimension of a vector space.
The definition is marked as protected to avoid conflicts with `_root_.rank`,
the rank of a linear map.
-/
protected def module.rank : cardinal :=
cardinal.sup.{v v} (λ ι : {s : set V // linear_independent K (coe : s → V)}, #ι.1)
end
section
variables {R : Type u} [ring R]
variables {M : Type v} [add_comm_group M] [module R M]
variables {M' : Type v'} [add_comm_group M'] [module R M']
variables {M₁ : Type v} [add_comm_group M₁] [module R M₁]
theorem linear_map.lift_dim_le_of_injective (f : M →ₗ[R] M') (i : injective f) :
cardinal.lift.{v'} (module.rank R M) ≤ cardinal.lift.{ v} (module.rank R M') :=
begin
dsimp [module.rank],
fapply cardinal.lift_sup_le_lift_sup',
{ rintro ⟨s, li⟩,
use f '' s,
convert (li.map' f (linear_map.ker_eq_bot.mpr i)).comp
(equiv.set.image ⇑f s i).symm (equiv.injective _),
ext ⟨-, ⟨x, ⟨h, rfl⟩⟩⟩,
simp, },
{ rintro ⟨s, li⟩,
exact cardinal.lift_mk_le'.mpr ⟨(equiv.set.image f s i).to_embedding⟩, }
end
theorem linear_map.dim_le_of_injective (f : M →ₗ[R] M₁) (i : injective f) :
module.rank R M ≤ module.rank R M₁ :=
cardinal.lift_le.1 (f.lift_dim_le_of_injective i)
theorem dim_le {n : ℕ}
(H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) :
module.rank R M ≤ n :=
begin
apply cardinal.sup_le.mpr,
rintro ⟨s, li⟩,
exact linear_independent_bounded_of_finset_linear_independent_bounded H _ li,
end
lemma lift_dim_range_le (f : M →ₗ[R] M') :
cardinal.lift.{v} (module.rank R f.range) ≤ cardinal.lift.{v'} (module.rank R M) :=
begin
dsimp [module.rank],
apply cardinal.lift_sup_le,
rintro ⟨s, li⟩,
apply le_trans,
swap 2,
apply cardinal.lift_le.mpr,
refine (cardinal.le_sup _ ⟨range_splitting f '' s, _⟩),
{ apply linear_independent.of_comp f.range_restrict,
convert li.comp (equiv.set.range_splitting_image_equiv f s) (equiv.injective _) using 1, },
{ exact (cardinal.lift_mk_eq'.mpr ⟨equiv.set.range_splitting_image_equiv f s⟩).ge, },
end
lemma dim_range_le (f : M →ₗ[R] M₁) : module.rank R f.range ≤ module.rank R M :=
by simpa using lift_dim_range_le f
lemma lift_dim_map_le (f : M →ₗ[R] M') (p : submodule R M) :
cardinal.lift.{v} (module.rank R (p.map f)) ≤ cardinal.lift.{v'} (module.rank R p) :=
begin
have h := lift_dim_range_le (f.comp (submodule.subtype p)),
rwa [linear_map.range_comp, range_subtype] at h,
end
lemma dim_map_le (f : M →ₗ[R] M₁) (p : submodule R M) : module.rank R (p.map f) ≤ module.rank R p :=
by simpa using lift_dim_map_le f p
lemma dim_le_of_submodule (s t : submodule R M) (h : s ≤ t) :
module.rank R s ≤ module.rank R t :=
(of_le h).dim_le_of_injective $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq,
subtype.eq $ show x = y, from subtype.ext_iff_val.1 eq
/-- Two linearly equivalent vector spaces have the same dimension, a version with different
universes. -/
theorem linear_equiv.lift_dim_eq (f : M ≃ₗ[R] M') :
cardinal.lift.{v'} (module.rank R M) = cardinal.lift.{v} (module.rank R M') :=
begin
apply le_antisymm,
{ exact f.to_linear_map.lift_dim_le_of_injective f.injective, },
{ exact f.symm.to_linear_map.lift_dim_le_of_injective f.symm.injective, },
end
/-- Two linearly equivalent vector spaces have the same dimension. -/
theorem linear_equiv.dim_eq (f : M ≃ₗ[R] M₁) :
module.rank R M = module.rank R M₁ :=
cardinal.lift_inj.1 f.lift_dim_eq
lemma dim_eq_of_injective (f : M →ₗ[R] M₁) (h : injective f) :
module.rank R M = module.rank R f.range :=
(linear_equiv.of_injective f h).dim_eq
/-- Pushforwards of submodules along a `linear_equiv` have the same dimension. -/
lemma linear_equiv.dim_map_eq (f : M ≃ₗ[R] M₁) (p : submodule R M) :
module.rank R (p.map (f : M →ₗ[R] M₁)) = module.rank R p :=
(f.of_submodule p).dim_eq.symm
variables (R M)
@[simp] lemma dim_top : module.rank R (⊤ : submodule R M) = module.rank R M :=
begin
have : (⊤ : submodule R M) ≃ₗ[R] M := linear_equiv.of_top ⊤ rfl,
rw this.dim_eq,
end
variables {R M}
lemma dim_range_of_surjective (f : M →ₗ[R] M') (h : surjective f) :
module.rank R f.range = module.rank R M' :=
by rw [linear_map.range_eq_top.2 h, dim_top]
lemma dim_submodule_le (s : submodule R M) : module.rank R s ≤ module.rank R M :=
begin
rw ←dim_top R M,
exact dim_le_of_submodule _ _ le_top,
end
lemma linear_map.dim_le_of_surjective (f : M →ₗ[R] M₁) (h : surjective f) :
module.rank R M₁ ≤ module.rank R M :=
begin
rw ←dim_range_of_surjective f h,
apply dim_range_le,
end
theorem dim_quotient_le (p : submodule R M) :
module.rank R p.quotient ≤ module.rank R M :=
(mkq p).dim_le_of_surjective (surjective_quot_mk _)
variables [nontrivial R]
lemma {m} cardinal_lift_le_dim_of_linear_independent
{ι : Type w} {v : ι → M} (hv : linear_independent R v) :
cardinal.lift.{(max v m)} (#ι) ≤ cardinal.lift.{(max w m)} (module.rank R M) :=
begin
apply le_trans,
{ exact cardinal.lift_mk_le.mpr
⟨(equiv.of_injective _ hv.injective).to_embedding⟩, },
{ simp only [cardinal.lift_le],
apply le_trans,
swap,
exact cardinal.le_sup _ ⟨range v, hv.coe_range⟩,
exact le_refl _, },
end
lemma cardinal_lift_le_dim_of_linear_independent'
{ι : Type w} {v : ι → M} (hv : linear_independent R v) :
cardinal.lift.{v} (#ι) ≤ cardinal.lift.{w} (module.rank R M) :=
cardinal_lift_le_dim_of_linear_independent.{u v w 0} hv
lemma cardinal_le_dim_of_linear_independent
{ι : Type v} {v : ι → M} (hv : linear_independent R v) :
#ι ≤ module.rank R M :=
by simpa using cardinal_lift_le_dim_of_linear_independent hv
lemma cardinal_le_dim_of_linear_independent'
{s : set M} (hs : linear_independent R (λ x, x : s → M)) :
#s ≤ module.rank R M :=
cardinal_le_dim_of_linear_independent hs
variables (R M)
@[simp] lemma dim_punit : module.rank R punit = 0 :=
begin
apply le_bot_iff.mp,
apply cardinal.sup_le.mpr,
rintro ⟨s, li⟩,
apply le_bot_iff.mpr,
apply cardinal.mk_emptyc_iff.mpr,
simp only [subtype.coe_mk],
by_contradiction h,
have ne : s.nonempty := ne_empty_iff_nonempty.mp h,
simpa using linear_independent.ne_zero (⟨_, ne.some_mem⟩ : s) li,
end
@[simp] lemma dim_bot : module.rank R (⊥ : submodule R M) = 0 :=
begin
have : (⊥ : submodule R M) ≃ₗ[R] punit := bot_equiv_punit,
rw [this.dim_eq, dim_punit],
end
variables {R M}
/--
Over any nontrivial ring, the existence of a finite spanning set implies that any basis is finite.
-/
-- One might hope that a finite spanning set implies that any linearly independent set is finite.
-- While this is true over a division ring
-- (simply because any linearly independent set can be extended to a basis),
-- I'm not certain what more general statements are possible.
def basis_fintype_of_finite_spans (w : set M) [fintype w] (s : span R w = ⊤)
{ι : Type w} (b : basis ι R M) : fintype ι :=
begin
-- We'll work by contradiction, assuming `ι` is infinite.
apply fintype_of_not_infinite _,
introI i,
-- Let `S` be the union of the supports of `x ∈ w` expressed as linear combinations of `b`.
-- This is a finite set since `w` is finite.
let S : finset ι := finset.univ.sup (λ x : w, (b.repr x).support),
let bS : set M := b '' S,
have h : ∀ x ∈ w, x ∈ span R bS,
{ intros x m,
rw [←b.total_repr x, finsupp.span_image_eq_map_total, submodule.mem_map],
use b.repr x,
simp only [and_true, eq_self_iff_true, finsupp.mem_supported],
change (b.repr x).support ≤ S,
convert (finset.le_sup (by simp : (⟨x, m⟩ : w) ∈ finset.univ)),
refl, },
-- Thus this finite subset of the basis elements spans the entire module.
have k : span R bS = ⊤ := eq_top_iff.2 (le_trans s.ge (span_le.2 h)),
-- Now there is some `x : ι` not in `S`, since `ι` is infinite.
obtain ⟨x, nm⟩ := infinite.exists_not_mem_finset S,
-- However it must be in the span of the finite subset,
have k' : b x ∈ span R bS, { rw k, exact mem_top, },
-- giving the desire contradiction.
refine b.linear_independent.not_mem_span_image _ k',
exact nm,
end
/--
Over any ring `R`, if `b` is a basis for a module `M`,
and `s` is a maximal linearly independent set,
then the union of the supports of `x ∈ s` (when written out in the basis `b`) is all of `b`.
-/
-- From [Les familles libres maximales d'un module ont-elles le meme cardinal?][lazarus1973]
lemma union_support_maximal_linear_independent_eq_range_basis
{ι : Type w} (b : basis ι R M)
{κ : Type w'} (v : κ → M) (i : linear_independent R v) (m : i.maximal) :
(⋃ k, ((b.repr (v k)).support : set ι)) = univ :=
begin
-- If that's not the case,
by_contradiction h,
simp only [←ne.def, ne_univ_iff_exists_not_mem, mem_Union, not_exists_not,
finsupp.mem_support_iff, finset.mem_coe] at h,
-- We have some basis element `b b'` which is not in the support of any of the `v i`.
obtain ⟨b', w⟩ := h,
-- Using this, we'll construct a linearly independent family strictly larger than `v`,
-- by also using this `b b'`.
let v' : option κ → M := λ o, o.elim (b b') v,
have r : range v ⊆ range v',
{ rintro - ⟨k, rfl⟩,
use some k,
refl, },
have r' : b b' ∉ range v,
{ rintro ⟨k, p⟩,
simpa [w] using congr_arg (λ m, (b.repr m) b') p, },
have r'' : range v ≠ range v',
{ intro e,
have p : b b' ∈ range v', { use none, refl, },
rw ←e at p,
exact r' p, },
have inj' : injective v',
{ rintros (_|k) (_|k) z,
{ refl, },
{ exfalso, exact r' ⟨k, z.symm⟩, },
{ exfalso, exact r' ⟨k, z⟩, },
{ congr, exact i.injective z, }, },
-- The key step in the proof is checking that this strictly larger family is linearly independent.
have i' : linear_independent R (coe : range v' → M),
{ rw [linear_independent_subtype_range inj', linear_independent_iff],
intros l z,
rw [finsupp.total_option] at z,
simp only [v', option.elim] at z,
change _ + finsupp.total κ M R v l.some = 0 at z,
-- We have some linear combination of `b b'` and the `v i`, which we want to show is trivial.
-- We'll first show the coefficient of `b b'` is zero,
-- by expressing the `v i` in the basis `b`, and using that the `v i` have no `b b'` term.
have l₀ : l none = 0,
{ rw ←eq_neg_iff_add_eq_zero at z,
replace z := eq_neg_of_eq_neg z,
apply_fun (λ x, b.repr x b') at z,
simp only [repr_self, linear_equiv.map_smul, mul_one, finsupp.single_eq_same, pi.neg_apply,
finsupp.smul_single', linear_equiv.map_neg, finsupp.coe_neg] at z,
erw finsupp.congr_fun (finsupp.apply_total R (b.repr : M →ₗ[R] ι →₀ R) v l.some) b' at z,
simpa [finsupp.total_apply, w] using z, },
-- Then all the other coefficients are zero, because `v` is linear independent.
have l₁ : l.some = 0,
{ rw [l₀, zero_smul, zero_add] at z,
exact linear_independent_iff.mp i _ z, },
-- Finally we put those facts together to show the linear combination is trivial.
ext (_|a),
{ simp only [l₀, finsupp.coe_zero, pi.zero_apply], },
{ erw finsupp.congr_fun l₁ a,
simp only [finsupp.coe_zero, pi.zero_apply], }, },
dsimp [linear_independent.maximal] at m,
specialize m (range v') i' r,
exact r'' m,
end
/--
Over any ring `R`, if `b` is an infinite basis for a module `M`,
and `s` is a maximal linearly independent set,
then the cardinality of `b` is bounded by the cardinality of `s`.
-/
lemma infinite_basis_le_maximal_linear_independent'
{ι : Type w} (b : basis ι R M) [infinite ι]
{κ : Type w'} (v : κ → M) (i : linear_independent R v) (m : i.maximal) :
cardinal.lift.{w'} (#ι) ≤ cardinal.lift.{w} (#κ) :=
begin
let Φ := λ k : κ, (b.repr (v k)).support,
have w₁ : #ι ≤ #(set.range Φ),
{ apply cardinal.le_range_of_union_finset_eq_top,
exact union_support_maximal_linear_independent_eq_range_basis b v i m, },
have w₂ :
cardinal.lift.{w'} (#(set.range Φ)) ≤ cardinal.lift.{w} (#κ) :=
cardinal.mk_range_le_lift,
exact (cardinal.lift_le.mpr w₁).trans w₂,
end
/--
Over any ring `R`, if `b` is an infinite basis for a module `M`,
and `s` is a maximal linearly independent set,
then the cardinality of `b` is bounded by the cardinality of `s`.
-/
-- (See `infinite_basis_le_maximal_linear_independent'` for the more general version
-- where the index types can live in different universes.)
lemma infinite_basis_le_maximal_linear_independent
{ι : Type w} (b : basis ι R M) [infinite ι]
{κ : Type w} (v : κ → M) (i : linear_independent R v) (m : i.maximal) :
#ι ≤ #κ :=
cardinal.lift_le.mp (infinite_basis_le_maximal_linear_independent' b v i m)
lemma complete_lattice.independent.subtype_ne_bot_le_rank [no_zero_smul_divisors R M]
{V : ι → submodule R M} (hV : complete_lattice.independent V) :
cardinal.lift.{v} (#{i : ι // V i ≠ ⊥}) ≤ cardinal.lift.{w} (module.rank R M) :=
begin
set I := {i : ι // V i ≠ ⊥},
have hI : ∀ i : I, ∃ v ∈ V i, v ≠ (0:M),
{ intros i,
rw ← submodule.ne_bot_iff,
exact i.prop },
choose v hvV hv using hI,
have : linear_independent R v,
{ exact (hV.comp _ subtype.coe_injective).linear_independent _ hvV hv },
exact cardinal_lift_le_dim_of_linear_independent' this
end
end
section rank_zero
variables {R : Type u} {M : Type v}
variables [ring R] [nontrivial R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M]
lemma dim_zero_iff_forall_zero : module.rank R M = 0 ↔ ∀ x : M, x = 0 :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ contrapose! h,
obtain ⟨x, hx⟩ := h,
suffices : 1 ≤ module.rank R M,
{ intro h, exact lt_irrefl _ (lt_of_lt_of_le cardinal.zero_lt_one (h ▸ this)) },
suffices : linear_independent R (λ (y : ({x} : set M)), ↑y),
{ simpa using (cardinal_le_dim_of_linear_independent this), },
exact linear_independent_singleton hx },
{ have : (⊤ : submodule R M) = ⊥,
{ ext x, simp [h x] },
rw [←dim_top, this, dim_bot] }
end
lemma dim_zero_iff : module.rank R M = 0 ↔ subsingleton M :=
dim_zero_iff_forall_zero.trans (subsingleton_iff_forall_eq 0).symm
lemma dim_pos_iff_exists_ne_zero : 0 < module.rank R M ↔ ∃ x : M, x ≠ 0 :=
begin
rw ←not_iff_not,
simpa using dim_zero_iff_forall_zero
end
lemma dim_pos_iff_nontrivial : 0 < module.rank R M ↔ nontrivial M :=
dim_pos_iff_exists_ne_zero.trans (nontrivial_iff_exists_ne 0).symm
lemma dim_pos [h : nontrivial M] : 0 < module.rank R M :=
dim_pos_iff_nontrivial.2 h
end rank_zero
section invariant_basis_number
variables {R : Type u} [ring R] [invariant_basis_number R]
variables {M : Type v} [add_comm_group M] [module R M]
/-- The dimension theorem: if `v` and `v'` are two bases, their index types
have the same cardinalities. -/
theorem mk_eq_mk_of_basis (v : basis ι R M) (v' : basis ι' R M) :
cardinal.lift.{w'} (#ι) = cardinal.lift.{w} (#ι') :=
begin
haveI := nontrivial_of_invariant_basis_number R,
by_cases h : #ι < ω,
{ -- `v` is a finite basis, so by `basis_fintype_of_finite_spans` so is `v'`.
haveI : fintype ι := (cardinal.lt_omega_iff_fintype.mp h).some,
haveI : fintype (range v) := set.fintype_range ⇑v,
haveI := basis_fintype_of_finite_spans _ v.span_eq v',
-- We clean up a little:
rw [cardinal.fintype_card, cardinal.fintype_card],
simp only [cardinal.lift_nat_cast, cardinal.nat_cast_inj],
-- Now we can use invariant basis number to show they have the same cardinality.
apply card_eq_of_lequiv R,
exact (((finsupp.linear_equiv_fun_on_fintype R R ι).symm.trans v.repr.symm) ≪≫ₗ
v'.repr) ≪≫ₗ (finsupp.linear_equiv_fun_on_fintype R R ι'), },
{ -- `v` is an infinite basis,
-- so by `infinite_basis_le_maximal_linear_independent`, `v'` is at least as big,
-- and then applying `infinite_basis_le_maximal_linear_independent` again
-- we see they have the same cardinality.
simp only [not_lt] at h,
haveI : infinite ι := cardinal.infinite_iff.mpr h,
have w₁ :=
infinite_basis_le_maximal_linear_independent' v _ v'.linear_independent v'.maximal,
haveI : infinite ι' := cardinal.infinite_iff.mpr (begin
apply cardinal.lift_le.{w' w}.mp,
have p := (cardinal.lift_le.mpr h).trans w₁,
rw cardinal.lift_omega at ⊢ p,
exact p,
end),
have w₂ :=
infinite_basis_le_maximal_linear_independent' v' _ v.linear_independent v.maximal,
exact le_antisymm w₁ w₂, }
end
/-- Given two basis indexed by `ι` and `ι'` of an `R`-module, where `R` satisfies the invariant
basis number property, an equiv `ι ≃ ι' `. -/
def basis.index_equiv (v : basis ι R M) (v' : basis ι' R M) : ι ≃ ι' :=
nonempty.some (cardinal.lift_mk_eq.1 (cardinal.lift_max.2 (mk_eq_mk_of_basis v v')))
theorem mk_eq_mk_of_basis' {ι' : Type w} (v : basis ι R M) (v' : basis ι' R M) :
#ι = #ι' :=
cardinal.lift_inj.1 $ mk_eq_mk_of_basis v v'
end invariant_basis_number
section rank_condition
variables {R : Type u} [ring R] [rank_condition R]
variables {M : Type v} [add_comm_group M] [module R M]
/--
An auxiliary lemma for `basis.le_span`.
If `R` satisfies the rank condition,
then for any finite basis `b : basis ι R M`,
and any finite spanning set `w : set M`,
the cardinality of `ι` is bounded by the cardinality of `w`.
-/
lemma basis.le_span'' {ι : Type*} [fintype ι] (b : basis ι R M)
{w : set M} [fintype w] (s : span R w = ⊤) :
fintype.card ι ≤ fintype.card w :=
begin
-- We construct an surjective linear map `(w → R) →ₗ[R] (ι → R)`,
-- by expressing a linear combination in `w` as a linear combination in `ι`.
fapply card_le_of_surjective' R,
{ exact b.repr.to_linear_map.comp (finsupp.total w M R coe), },
{ apply surjective.comp,
apply linear_equiv.surjective,
rw [←linear_map.range_eq_top, finsupp.range_total],
simpa using s, },
end
/--
Another auxiliary lemma for `basis.le_span`, which does not require assuming the basis is finite,
but still assumes we have a finite spanning set.
-/
lemma basis_le_span' {ι : Type*} (b : basis ι R M)
{w : set M} [fintype w] (s : span R w = ⊤) :
#ι ≤ fintype.card w :=
begin
haveI := nontrivial_of_invariant_basis_number R,
haveI := basis_fintype_of_finite_spans w s b,
rw cardinal.fintype_card ι,
simp only [cardinal.nat_cast_le],
exact basis.le_span'' b s,
end
/--
If `R` satisfies the rank condition,
then the cardinality of any basis is bounded by the cardinality of any spanning set.
-/
-- Note that if `R` satisfies the strong rank condition,
-- this also follows from `linear_independent_le_span` below.
theorem basis.le_span {J : set M} (v : basis ι R M)
(hJ : span R J = ⊤) : #(range v) ≤ #J :=
begin
haveI := nontrivial_of_invariant_basis_number R,
cases le_or_lt ω (#J) with oJ oJ,
{ have := cardinal.mk_range_eq_of_injective v.injective,
let S : J → set ι := λ j, ↑(v.repr j).support,
let S' : J → set M := λ j, v '' S j,
have hs : range v ⊆ ⋃ j, S' j,
{ intros b hb,
rcases mem_range.1 hb with ⟨i, hi⟩,
have : span R J ≤ comap v.repr.to_linear_map (finsupp.supported R R (⋃ j, S j)) :=
span_le.2 (λ j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩),
rw hJ at this,
replace : v.repr (v i) ∈ (finsupp.supported R R (⋃ j, S j)) := this trivial,
rw [v.repr_self, finsupp.mem_supported,
finsupp.support_single_ne_zero one_ne_zero] at this,
{ subst b,
rcases mem_Union.1 (this (finset.mem_singleton_self _)) with ⟨j, hj⟩,
exact mem_Union.2 ⟨j, (mem_image _ _ _).2 ⟨i, hj, rfl⟩⟩ },
{ apply_instance } },
refine le_of_not_lt (λ IJ, _),
suffices : #(⋃ j, S' j) < #(range v),
{ exact not_le_of_lt this ⟨set.embedding_of_subset _ _ hs⟩ },
refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk
(cardinal.sum_le_sum _ (λ _, cardinal.omega) _)) _,
{ exact λ j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ (finset.finite_to_set _).image _) },
{ rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } },
{ haveI : fintype J := (cardinal.lt_omega_iff_fintype.mp oJ).some,
rw [←cardinal.lift_le, cardinal.mk_range_eq_of_injective v.injective, cardinal.fintype_card J],
convert cardinal.lift_le.{w v}.2 (basis_le_span' v hJ),
simp, },
end
end rank_condition
section strong_rank_condition
variables {R : Type u} [ring R] [strong_rank_condition R]
variables {M : Type v} [add_comm_group M] [module R M]
open submodule
-- An auxiliary lemma for `linear_independent_le_span'`,
-- with the additional assumption that the linearly independent family is finite.
lemma linear_independent_le_span_aux'
{ι : Type*} [fintype ι] (v : ι → M) (i : linear_independent R v)
(w : set M) [fintype w] (s : range v ≤ span R w) :
fintype.card ι ≤ fintype.card w :=
begin
-- We construct an injective linear map `(ι → R) →ₗ[R] (w → R)`,
-- by thinking of `f : ι → R` as a linear combination of the finite family `v`,
-- and expressing that (using the axiom of choice) as a linear combination over `w`.
-- We can do this linearly by constructing the map on a basis.
fapply card_le_of_injective' R,
{ apply finsupp.total,
exact λ i, span.repr R w ⟨v i, s (mem_range_self i)⟩, },
{ intros f g h,
apply_fun finsupp.total w M R coe at h,
simp only [finsupp.total_total, submodule.coe_mk, span.finsupp_total_repr] at h,
rw [←sub_eq_zero, ←linear_map.map_sub] at h,
exact sub_eq_zero.mp (linear_independent_iff.mp i _ h), },
end
/--
If `R` satisfies the strong rank condition,
then any linearly independent family `v : ι → M`
contained in the span of some finite `w : set M`,
is itself finite.
-/
def linear_independent_fintype_of_le_span_fintype
{ι : Type*} (v : ι → M) (i : linear_independent R v)
(w : set M) [fintype w] (s : range v ≤ span R w) : fintype ι :=
fintype_of_finset_card_le (fintype.card w) (λ t, begin
let v' := λ x : (t : set ι), v x,
have i' : linear_independent R v' := i.comp _ subtype.val_injective,
have s' : range v' ≤ span R w := (range_comp_subset_range _ _).trans s,
simpa using linear_independent_le_span_aux' v' i' w s',
end)
/--
If `R` satisfies the strong rank condition,
then for any linearly independent family `v : ι → M`
contained in the span of some finite `w : set M`,
the cardinality of `ι` is bounded by the cardinality of `w`.
-/
lemma linear_independent_le_span' {ι : Type*} (v : ι → M) (i : linear_independent R v)
(w : set M) [fintype w] (s : range v ≤ span R w) :
#ι ≤ fintype.card w :=
begin
haveI : fintype ι := linear_independent_fintype_of_le_span_fintype v i w s,
rw cardinal.fintype_card,
simp only [cardinal.nat_cast_le],
exact linear_independent_le_span_aux' v i w s,
end
/--
If `R` satisfies the strong rank condition,
then for any linearly independent family `v : ι → M`
and any finite spanning set `w : set M`,
the cardinality of `ι` is bounded by the cardinality of `w`.
-/
lemma linear_independent_le_span {ι : Type*} (v : ι → M) (i : linear_independent R v)
(w : set M) [fintype w] (s : span R w = ⊤) :
#ι ≤ fintype.card w :=
begin
apply linear_independent_le_span' v i w,
rw s,
exact le_top,
end
/-- A linearly-independent family of vectors in a module over a ring satisfying the strong rank
condition must be finite if the module is Noetherian. -/
noncomputable def fintype_of_is_noetherian_linear_independent [is_noetherian R M]
{v : ι → M} (hi : linear_independent R v) : fintype ι :=
begin
have hfg : (⊤ : submodule R M).fg,
{ exact is_noetherian_def.mp infer_instance ⊤, },
rw submodule.fg_def at hfg,
choose s hs hs' using hfg,
haveI : fintype s := hs.fintype,
apply linear_independent_fintype_of_le_span_fintype v hi s,
simp only [hs', set.subset_univ, submodule.top_coe, set.le_eq_subset],
end
/-- A linearly-independent subset of a module over a ring satisfying the strong rank condition
must be finite if the module is Noetherian. -/
lemma finite_of_is_noetherian_linear_independent [is_noetherian R M]
{s : set M} (hi : linear_independent R (coe : s → M)) : s.finite :=
⟨fintype_of_is_noetherian_linear_independent hi⟩
/--
An auxiliary lemma for `linear_independent_le_basis`:
we handle the case where the basis `b` is infinite.
-/
lemma linear_independent_le_infinite_basis
{ι : Type*} (b : basis ι R M) [infinite ι]
{κ : Type*} (v : κ → M) (i : linear_independent R v) :
#κ ≤ #ι :=
begin
by_contradiction,
simp only [not_le] at h,
have w : #(finset ι) = #ι :=
cardinal.mk_finset_eq_mk (cardinal.infinite_iff.mp ‹infinite ι›),
rw ←w at h,
let Φ := λ k : κ, (b.repr (v k)).support,
obtain ⟨s, w : infinite ↥(Φ ⁻¹' {s})⟩ := cardinal.exists_infinite_fiber Φ h
(by { rw [cardinal.infinite_iff, w], exact (cardinal.infinite_iff.mp ‹infinite ι›), }),
let v' := λ k : Φ ⁻¹' {s}, v k,
have i' : linear_independent R v' := i.comp _ subtype.val_injective,
have w' : fintype (Φ ⁻¹' {s}),
{ apply linear_independent_fintype_of_le_span_fintype v' i' (s.image b),
rintros m ⟨⟨p,⟨rfl⟩⟩,rfl⟩,
simp only [set_like.mem_coe, subtype.coe_mk, finset.coe_image],
apply basis.mem_span_repr_support, },
exactI w.false,
end
/--
Over any ring `R` satisfying the strong rank condition,
if `b` is a basis for a module `M`,
and `s` is a linearly independent set,
then the cardinality of `s` is bounded by the cardinality of `b`.
-/
lemma linear_independent_le_basis
{ι : Type*} (b : basis ι R M)
{κ : Type*} (v : κ → M) (i : linear_independent R v) :
#κ ≤ #ι :=
begin
-- We split into cases depending on whether `ι` is infinite.
cases fintype_or_infinite ι; resetI,
{ -- When `ι` is finite, we have `linear_independent_le_span`,
rw cardinal.fintype_card ι,
haveI : nontrivial R := nontrivial_of_invariant_basis_number R,
rw fintype.card_congr (equiv.of_injective b b.injective),
exact linear_independent_le_span v i (range b) b.span_eq, },
{ -- and otherwise we have `linear_indepedent_le_infinite_basis`.
exact linear_independent_le_infinite_basis b v i, },
end
/-- In an `n`-dimensional space, the rank is at most `m`. -/
lemma basis.card_le_card_of_linear_independent_aux
{R : Type*} [ring R] [strong_rank_condition R]
(n : ℕ) {m : ℕ} (v : fin m → fin n → R) :
linear_independent R v → m ≤ n :=
λ h, by simpa using (linear_independent_le_basis (pi.basis_fun R (fin n)) v h)
/--
Over any ring `R` satisfying the strong rank condition,
if `b` is an infinite basis for a module `M`,
then every maximal linearly independent set has the same cardinality as `b`.
This proof (along with some of the lemmas above) comes from
[Les familles libres maximales d'un module ont-elles le meme cardinal?][lazarus1973]
-/
-- When the basis is not infinite this need not be true!
lemma maximal_linear_independent_eq_infinite_basis
{ι : Type*} (b : basis ι R M) [infinite ι]
{κ : Type*} (v : κ → M) (i : linear_independent R v) (m : i.maximal) :
#κ = #ι :=
begin
apply le_antisymm,
{ exact linear_independent_le_basis b v i, },
{ haveI : nontrivial R := nontrivial_of_invariant_basis_number R,
exact infinite_basis_le_maximal_linear_independent b v i m, }
end
theorem basis.mk_eq_dim'' {ι : Type v} (v : basis ι R M) :
#ι = module.rank R M :=
begin
haveI := nontrivial_of_invariant_basis_number R,
apply le_antisymm,
{ transitivity,
swap,
apply cardinal.le_sup,
exact ⟨set.range v, by { convert v.reindex_range.linear_independent, ext, simp }⟩,
exact (cardinal.mk_range_eq v v.injective).ge, },
{ apply cardinal.sup_le.mpr,
rintro ⟨s, li⟩,
apply linear_independent_le_basis v _ li, },
end
-- By this stage we want to have a complete API for `module.rank`,
-- so we set it `irreducible` here, to keep ourselves honest.
attribute [irreducible] module.rank
theorem basis.mk_range_eq_dim (v : basis ι R M) :
#(range v) = module.rank R M :=
v.reindex_range.mk_eq_dim''
/-- If a vector space has a finite basis, then its dimension (seen as a cardinal) is equal to the
cardinality of the basis. -/
lemma dim_eq_card_basis {ι : Type w} [fintype ι] (h : basis ι R M) :
module.rank R M = fintype.card ι :=
by {haveI := nontrivial_of_invariant_basis_number R,
rw [←h.mk_range_eq_dim, cardinal.fintype_card, set.card_range_of_injective h.injective] }
lemma basis.card_le_card_of_linear_independent {ι : Type*} [fintype ι]
(b : basis ι R M) {ι' : Type*} [fintype ι'] {v : ι' → M} (hv : linear_independent R v) :
fintype.card ι' ≤ fintype.card ι :=
begin
letI := nontrivial_of_invariant_basis_number R,
simpa [dim_eq_card_basis b, cardinal.fintype_card] using
cardinal_lift_le_dim_of_linear_independent' hv
end
lemma basis.card_le_card_of_submodule (N : submodule R M) [fintype ι] (b : basis ι R M)
[fintype ι'] (b' : basis ι' R N) : fintype.card ι' ≤ fintype.card ι :=
b.card_le_card_of_linear_independent (b'.linear_independent.map' N.subtype N.ker_subtype)
lemma basis.card_le_card_of_le
{N O : submodule R M} (hNO : N ≤ O) [fintype ι] (b : basis ι R O) [fintype ι']
(b' : basis ι' R N) : fintype.card ι' ≤ fintype.card ι :=
b.card_le_card_of_linear_independent
(b'.linear_independent.map' (submodule.of_le hNO) (N.ker_of_le O _))
theorem basis.mk_eq_dim (v : basis ι R M) :
cardinal.lift.{v} (#ι) = cardinal.lift.{w} (module.rank R M) :=
begin
haveI := nontrivial_of_invariant_basis_number R,
rw [←v.mk_range_eq_dim, cardinal.mk_range_eq_of_injective v.injective]
end
theorem {m} basis.mk_eq_dim' (v : basis ι R M) :
cardinal.lift.{(max v m)} (#ι) = cardinal.lift.{(max w m)} (module.rank R M) :=
by simpa using v.mk_eq_dim
/-- If a module has a finite dimension, all bases are indexed by a finite type. -/
lemma basis.nonempty_fintype_index_of_dim_lt_omega {ι : Type*}
(b : basis ι R M) (h : module.rank R M < cardinal.omega) :
nonempty (fintype ι) :=
by rwa [← cardinal.lift_lt, ← b.mk_eq_dim,
-- ensure `omega` has the correct universe
cardinal.lift_omega, ← cardinal.lift_omega.{u_1 v},
cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h
/-- If a module has a finite dimension, all bases are indexed by a finite type. -/
noncomputable def basis.fintype_index_of_dim_lt_omega {ι : Type*}
(b : basis ι R M) (h : module.rank R M < cardinal.omega) :
fintype ι :=
classical.choice (b.nonempty_fintype_index_of_dim_lt_omega h)
/-- If a module has a finite dimension, all bases are indexed by a finite set. -/
lemma basis.finite_index_of_dim_lt_omega {ι : Type*} {s : set ι}
(b : basis s R M) (h : module.rank R M < cardinal.omega) :
s.finite :=
finite_def.2 (b.nonempty_fintype_index_of_dim_lt_omega h)
lemma dim_span {v : ι → M} (hv : linear_independent R v) :
module.rank R ↥(span R (range v)) = #(range v) :=
begin
haveI := nontrivial_of_invariant_basis_number R,
rw [←cardinal.lift_inj, ← (basis.span hv).mk_eq_dim,
cardinal.mk_range_eq_of_injective (@linear_independent.injective ι R M v _ _ _ _ hv)]
end
lemma dim_span_set {s : set M} (hs : linear_independent R (λ x, x : s → M)) :
module.rank R ↥(span R s) = #s :=
by { rw [← @set_of_mem_eq _ s, ← subtype.range_coe_subtype], exact dim_span hs }
/-- If `N` is a submodule in a free, finitely generated module,
do induction on adjoining a linear independent element to a submodule. -/
def submodule.induction_on_rank [is_domain R] [fintype ι] (b : basis ι R M)
(P : submodule R M → Sort*) (ih : ∀ (N : submodule R M),
(∀ (N' ≤ N) (x ∈ N), (∀ (c : R) (y ∈ N'), c • x + y = (0 : M) → c = 0) → P N') →
P N)
(N : submodule R M) : P N :=
submodule.induction_on_rank_aux b P ih (fintype.card ι) N (λ s hs hli,
by simpa using b.card_le_card_of_linear_independent hli)
/-- If `S` a finite-dimensional ring extension of `R` which is free as an `R`-module,
then the rank of an ideal `I` of `S` over `R` is the same as the rank of `S`.
-/
lemma ideal.rank_eq {R S : Type*} [comm_ring R] [strong_rank_condition R] [ring S] [is_domain S]
[algebra R S] {n m : Type*} [fintype n] [fintype m]
(b : basis n R S) {I : ideal S} (hI : I ≠ ⊥) (c : basis m R I) :
fintype.card m = fintype.card n :=
begin
obtain ⟨a, ha⟩ := submodule.nonzero_mem_of_bot_lt (bot_lt_iff_ne_bot.mpr hI),
have : linear_independent R (λ i, b i • a),
{ have hb := b.linear_independent,
rw fintype.linear_independent_iff at ⊢ hb,
intros g hg,
apply hb g,
simp only [← smul_assoc, ← finset.sum_smul, smul_eq_zero] at hg,
exact hg.resolve_right ha },
exact le_antisymm
(b.card_le_card_of_linear_independent (c.linear_independent.map' (submodule.subtype I)
(linear_map.ker_eq_bot.mpr subtype.coe_injective)))
(c.card_le_card_of_linear_independent this),
end
variables (R)
lemma dim_self : module.rank R R = 1 :=
by rw [←cardinal.lift_inj, ← (basis.singleton punit R).mk_eq_dim, cardinal.mk_punit]
end strong_rank_condition
section division_ring
variables [division_ring K] [add_comm_group V] [module K V] [add_comm_group V₁] [module K V₁]
variables {K V}
/-- If a vector space has a finite dimension, the index set of `basis.of_vector_space` is finite. -/
lemma basis.finite_of_vector_space_index_of_dim_lt_omega (h : module.rank K V < cardinal.omega) :
(basis.of_vector_space_index K V).finite :=
finite_def.2 $ (basis.of_vector_space K V).nonempty_fintype_index_of_dim_lt_omega h
variables [add_comm_group V'] [module K V']
/-- Two vector spaces are isomorphic if they have the same dimension. -/
theorem nonempty_linear_equiv_of_lift_dim_eq
(cond : cardinal.lift.{v'} (module.rank K V) = cardinal.lift.{v} (module.rank K V')) :
nonempty (V ≃ₗ[K] V') :=
begin
let B := basis.of_vector_space K V,
let B' := basis.of_vector_space K V',
have : cardinal.lift.{v' v} (#_) = cardinal.lift.{v v'} (#_),
by rw [B.mk_eq_dim'', cond, B'.mk_eq_dim''],
exact (cardinal.lift_mk_eq.{v v' 0}.1 this).map (B.equiv B')
end
/-- Two vector spaces are isomorphic if they have the same dimension. -/
theorem nonempty_linear_equiv_of_dim_eq (cond : module.rank K V = module.rank K V₁) :
nonempty (V ≃ₗ[K] V₁) :=
nonempty_linear_equiv_of_lift_dim_eq $ congr_arg _ cond
section
variables (V V' V₁)
/-- Two vector spaces are isomorphic if they have the same dimension. -/
def linear_equiv.of_lift_dim_eq
(cond : cardinal.lift.{v'} (module.rank K V) = cardinal.lift.{v} (module.rank K V')) :
V ≃ₗ[K] V' :=
classical.choice (nonempty_linear_equiv_of_lift_dim_eq cond)
/-- Two vector spaces are isomorphic if they have the same dimension. -/
def linear_equiv.of_dim_eq (cond : module.rank K V = module.rank K V₁) : V ≃ₗ[K] V₁ :=
classical.choice (nonempty_linear_equiv_of_dim_eq cond)
end
/-- Two vector spaces are isomorphic if and only if they have the same dimension. -/
theorem linear_equiv.nonempty_equiv_iff_lift_dim_eq :
nonempty (V ≃ₗ[K] V') ↔
cardinal.lift.{v'} (module.rank K V) = cardinal.lift.{v} (module.rank K V') :=
⟨λ ⟨h⟩, linear_equiv.lift_dim_eq h, λ h, nonempty_linear_equiv_of_lift_dim_eq h⟩
/-- Two vector spaces are isomorphic if and only if they have the same dimension. -/
theorem linear_equiv.nonempty_equiv_iff_dim_eq :
nonempty (V ≃ₗ[K] V₁) ↔ module.rank K V = module.rank K V₁ :=
⟨λ ⟨h⟩, linear_equiv.dim_eq h, λ h, nonempty_linear_equiv_of_dim_eq h⟩
-- TODO how far can we generalise this?
-- When `s` is finite, we could prove this for any ring satisfying the strong rank condition
-- using `linear_independent_le_span'`
lemma dim_span_le (s : set V) : module.rank K (span K s) ≤ #s :=
begin
obtain ⟨b, hb, hsab, hlib⟩ := exists_linear_independent K s,
convert cardinal.mk_le_mk_of_subset hb,
rw [← hsab, dim_span_set hlib]
end
lemma dim_span_of_finset (s : finset V) :
module.rank K (span K (↑s : set V)) < cardinal.omega :=
calc module.rank K (span K (↑s : set V)) ≤ #(↑s : set V) : dim_span_le ↑s
... = s.card : by rw [cardinal.finset_card, finset.coe_sort_coe]
... < cardinal.omega : cardinal.nat_lt_omega _
theorem dim_prod : module.rank K (V × V₁) = module.rank K V + module.rank K V₁ :=
begin
let b := basis.of_vector_space K V,
let c := basis.of_vector_space K V₁,
rw [← cardinal.lift_inj,
← (basis.prod b c).mk_eq_dim,
cardinal.lift_add, ← cardinal.mk_ulift,
← b.mk_eq_dim, ← c.mk_eq_dim,
← cardinal.mk_ulift, ← cardinal.mk_ulift,
cardinal.add_def (ulift _)],
exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2
⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm ⟩),
end
section fintype
variable [fintype η]
variables [∀i, add_comm_group (φ i)] [∀i, module K (φ i)]
open linear_map
lemma dim_pi : module.rank K (Πi, φ i) = cardinal.sum (λi, module.rank K (φ i)) :=
begin
let b := assume i, basis.of_vector_space K (φ i),
let this : basis (Σ j, _) K (Π j, φ j) := pi.basis b,
rw [←cardinal.lift_inj, ← this.mk_eq_dim],
simp [λ i, (b i).mk_range_eq_dim.symm, cardinal.sum_mk]
end
lemma dim_fun {V η : Type u} [fintype η] [add_comm_group V] [module K V] :
module.rank K (η → V) = fintype.card η * module.rank K V :=
by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card]
lemma dim_fun_eq_lift_mul :
module.rank K (η → V) = (fintype.card η : cardinal.{max u₁' v}) *
cardinal.lift.{u₁'} (module.rank K V) :=
by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast]
lemma dim_fun' : module.rank K (η → K) = fintype.card η :=
by rw [dim_fun_eq_lift_mul, dim_self, cardinal.lift_one, mul_one, cardinal.nat_cast_inj]
lemma dim_fin_fun (n : ℕ) : module.rank K (fin n → K) = n :=
by simp [dim_fun']
end fintype
end division_ring
section field
variables [field K] [add_comm_group V] [module K V] [add_comm_group V₁] [module K V₁]
variables [add_comm_group V'] [module K V']
variables {K V}
theorem dim_quotient_add_dim (p : submodule K V) :
module.rank K p.quotient + module.rank K p = module.rank K V :=
by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq
/-- rank-nullity theorem -/
theorem dim_range_add_dim_ker (f : V →ₗ[K] V₁) :
module.rank K f.range + module.rank K f.ker = module.rank K V :=
begin
haveI := λ (p : submodule K V), classical.dec_eq p.quotient,
rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim]
end
lemma dim_eq_of_surjective (f : V →ₗ[K] V₁) (h : surjective f) :
module.rank K V = module.rank K V₁ + module.rank K f.ker :=
by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h]
section
variables [add_comm_group V₂] [module K V₂]
variables [add_comm_group V₃] [module K V₃]
open linear_map
/-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq`. -/
lemma dim_add_dim_split
(db : V₂ →ₗ[K] V) (eb : V₃ →ₗ[K] V) (cd : V₁ →ₗ[K] V₂) (ce : V₁ →ₗ[K] V₃)
(hde : ⊤ ≤ db.range ⊔ eb.range)
(hgd : ker cd = ⊥)
(eq : db.comp cd = eb.comp ce)
(eq₂ : ∀d e, db d = eb e → (∃c, cd c = d ∧ ce c = e)) :
module.rank K V + module.rank K V₁ = module.rank K V₂ + module.rank K V₃ :=
have hf : surjective (coprod db eb),
begin
refine (range_eq_top.1 $ top_unique $ _),
rwa [← map_top, ← prod_top, map_coprod_prod, ←range_eq_map, ←range_eq_map]
end,
begin
conv {to_rhs, rw [← dim_prod, dim_eq_of_surjective _ hf] },
congr' 1,
apply linear_equiv.dim_eq,
refine linear_equiv.of_bijective _ _ _,
{ refine cod_restrict _ (prod cd (- ce)) _,
{ assume c,
simp only [add_eq_zero_iff_eq_neg, linear_map.prod_apply, mem_ker,
coprod_apply, neg_neg, map_neg, neg_apply],
exact linear_map.ext_iff.1 eq c } },
{ rw [← ker_eq_bot, ker_cod_restrict, ker_prod, hgd, bot_inf_eq] },
{ rw [← range_eq_top, eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap,
map_top, range_subtype],
rintros ⟨d, e⟩,
have h := eq₂ d (-e),
simp only [add_eq_zero_iff_eq_neg, linear_map.prod_apply, mem_ker, set_like.mem_coe,
prod.mk.inj_iff, coprod_apply, map_neg, neg_apply, linear_map.mem_range] at ⊢ h,
assume hde,
rcases h hde with ⟨c, h₁, h₂⟩,
refine ⟨c, h₁, _⟩,
rw [h₂, _root_.neg_neg] }
end
lemma dim_sup_add_dim_inf_eq (s t : submodule K V) :
module.rank K (s ⊔ t : submodule K V) + module.rank K (s ⊓ t : submodule K V) =
module.rank K s + module.rank K t :=
dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right)
begin
rw [← map_le_map_iff' (ker_subtype $ s ⊔ t), map_sup, map_top,
← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le,
range_subtype, range_subtype, range_subtype],
exact le_refl _
end
(ker_of_le _ _ _)
begin ext ⟨x, hx⟩, refl end
begin
rintros ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ eq,
have : b₁ = b₂ := congr_arg subtype.val eq,
subst this,
exact ⟨⟨b₁, hb₁, hb₂⟩, rfl, rfl⟩
end
lemma dim_add_le_dim_add_dim (s t : submodule K V) :
module.rank K (s ⊔ t : submodule K V) ≤ module.rank K s + module.rank K t :=
by { rw [← dim_sup_add_dim_inf_eq], exact self_le_add_right _ _ }
end
lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s ≠ ⊥) : ∃ b : V, b ∈ s ∧ b ≠ 0 :=
begin
classical,
by_contradiction hex,
have : ∀x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex },
exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs)
end
lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : 0 < module.rank K s) :
∃ b : V, b ∈ s ∧ b ≠ 0 :=
exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [eq, dim_bot] at h; exact lt_irrefl _ h
section rank
-- TODO This definition, and some of the results about it, could be generalized to arbitrary rings.
/-- `rank f` is the rank of a `linear_map f`, defined as the dimension of `f.range`. -/
def rank (f : V →ₗ[K] V') : cardinal := module.rank K f.range
lemma rank_le_domain (f : V →ₗ[K] V₁) : rank f ≤ module.rank K V :=
by { rw [← dim_range_add_dim_ker f], exact self_le_add_right _ _ }
lemma rank_le_range (f : V →ₗ[K] V₁) : rank f ≤ module.rank K V₁ :=
dim_submodule_le _
lemma rank_add_le (f g : V →ₗ[K] V') : rank (f + g) ≤ rank f + rank g :=
calc rank (f + g) ≤ module.rank K (f.range ⊔ g.range : submodule K V') :
begin
refine dim_le_of_submodule _ _ _,
exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $
assume x, show f x + g x ∈ (f.range ⊔ g.range : submodule K V'), from
mem_sup.2 ⟨_, ⟨x, rfl⟩, _, ⟨x, rfl⟩, rfl⟩)
end
... ≤ rank f + rank g : dim_add_le_dim_add_dim _ _
@[simp] lemma rank_zero : rank (0 : V →ₗ[K] V') = 0 :=
by rw [rank, linear_map.range_zero, dim_bot]
lemma rank_finset_sum_le {η} (s : finset η) (f : η → V →ₗ[K] V') :
rank (∑ d in s, f d) ≤ ∑ d in s, rank (f d) :=
@finset.sum_hom_rel _ _ _ _ _ (λa b, rank a ≤ b) f (λ d, rank (f d)) s (le_of_eq rank_zero)
(λ i g c h, le_trans (rank_add_le _ _) (add_le_add_left h _))
variables [add_comm_group V''] [module K V'']
lemma rank_comp_le1 (g : V →ₗ[K] V') (f : V' →ₗ[K] V'') : rank (f.comp g) ≤ rank f :=
begin
refine dim_le_of_submodule _ _ _,
rw [linear_map.range_comp],
exact linear_map.map_le_range,
end
variables [add_comm_group V'₁] [module K V'₁]
lemma rank_comp_le2 (g : V →ₗ[K] V') (f : V' →ₗ[K] V'₁) : rank (f.comp g) ≤ rank g :=
by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _
end rank
-- TODO The remainder of this file could be generalized to arbitrary rings.
/-- The `ι` indexed basis on `V`, where `ι` is an empty type and `V` is zero-dimensional.
See also `finite_dimensional.fin_basis`.
-/
def basis.of_dim_eq_zero {ι : Type*} [is_empty ι] (hV : module.rank K V = 0) :
basis ι K V :=
begin
haveI : subsingleton V := dim_zero_iff.1 hV,
exact basis.empty _
end
@[simp] lemma basis.of_dim_eq_zero_apply {ι : Type*} [is_empty ι]
(hV : module.rank K V = 0) (i : ι) :
basis.of_dim_eq_zero hV i = 0 :=
rfl
lemma le_dim_iff_exists_linear_independent {c : cardinal} :
c ≤ module.rank K V ↔ ∃ s : set V, #s = c ∧ linear_independent K (coe : s → V) :=
begin
split,
{ intro h,
let t := basis.of_vector_space K V,
rw [← t.mk_eq_dim'', cardinal.le_mk_iff_exists_subset] at h,
rcases h with ⟨s, hst, hsc⟩,
exact ⟨s, hsc, (of_vector_space_index.linear_independent K V).mono hst⟩ },
{ rintro ⟨s, rfl, si⟩,
exact cardinal_le_dim_of_linear_independent si }
end
lemma le_dim_iff_exists_linear_independent_finset {n : ℕ} :
↑n ≤ module.rank K V ↔
∃ s : finset V, s.card = n ∧ linear_independent K (coe : (s : set V) → V) :=
begin
simp only [le_dim_iff_exists_linear_independent, cardinal.mk_eq_nat_iff_finset],
split,
{ rintro ⟨s, ⟨t, rfl, rfl⟩, si⟩,
exact ⟨t, rfl, si⟩ },
{ rintro ⟨s, rfl, si⟩,
exact ⟨s, ⟨s, rfl, rfl⟩, si⟩ }
end
lemma le_rank_iff_exists_linear_independent {c : cardinal} {f : V →ₗ[K] V'} :
c ≤ rank f ↔
∃ s : set V, cardinal.lift.{v'} (#s) = cardinal.lift.{v} c ∧
linear_independent K (λ x : s, f x) :=
begin
rcases f.range_restrict.exists_right_inverse_of_surjective f.range_range_restrict with ⟨g, hg⟩,
have fg : left_inverse f.range_restrict g, from linear_map.congr_fun hg,
refine ⟨λ h, _, _⟩,
{ rcases le_dim_iff_exists_linear_independent.1 h with ⟨s, rfl, si⟩,
refine ⟨g '' s, cardinal.mk_image_eq_lift _ _ fg.injective, _⟩,
replace fg : ∀ x, f (g x) = x, by { intro x, convert congr_arg subtype.val (fg x) },
replace si : linear_independent K (λ x : s, f (g x)),
by simpa only [fg] using si.map' _ (ker_subtype _),
exact si.image_of_comp s g f },
{ rintro ⟨s, hsc, si⟩,
have : linear_independent K (λ x : s, f.range_restrict x),
from linear_independent.of_comp (f.range.subtype) (by convert si),
convert cardinal_le_dim_of_linear_independent this.image,
rw [← cardinal.lift_inj, ← hsc, cardinal.mk_image_eq_of_inj_on_lift],
exact inj_on_iff_injective.2 this.injective }
end
lemma le_rank_iff_exists_linear_independent_finset {n : ℕ} {f : V →ₗ[K] V'} :
↑n ≤ rank f ↔ ∃ s : finset V, s.card = n ∧ linear_independent K (λ x : (s : set V), f x) :=
begin
simp only [le_rank_iff_exists_linear_independent, cardinal.lift_nat_cast,
cardinal.lift_eq_nat_iff, cardinal.mk_eq_nat_iff_finset],
split,
{ rintro ⟨s, ⟨t, rfl, rfl⟩, si⟩,
exact ⟨t, rfl, si⟩ },
{ rintro ⟨s, rfl, si⟩,
exact ⟨s, ⟨s, rfl, rfl⟩, si⟩ }
end
/-- A vector space has dimension at most `1` if and only if there is a
single vector of which all vectors are multiples. -/
lemma dim_le_one_iff : module.rank K V ≤ 1 ↔ ∃ v₀ : V, ∀ v, ∃ r : K, r • v₀ = v :=
begin
let b := basis.of_vector_space K V,
split,
{ intro hd,
rw [← b.mk_eq_dim'', cardinal.le_one_iff_subsingleton, subsingleton_coe] at hd,
rcases eq_empty_or_nonempty (of_vector_space_index K V) with hb | ⟨⟨v₀, hv₀⟩⟩,
{ use 0,
have h' : ∀ v : V, v = 0, { simpa [hb, submodule.eq_bot_iff] using b.span_eq.symm },
intro v,
simp [h' v] },
{ use v₀,
have h' : (K ∙ v₀) = ⊤, { simpa [hd.eq_singleton_of_mem hv₀] using b.span_eq },
intro v,
have hv : v ∈ (⊤ : submodule K V) := mem_top,
rwa [←h', mem_span_singleton] at hv } },
{ rintros ⟨v₀, hv₀⟩,
have h : (K ∙ v₀) = ⊤,
{ ext, simp [mem_span_singleton, hv₀] },
rw [←dim_top, ←h],
convert dim_span_le _,
simp }
end
/-- A submodule has dimension at most `1` if and only if there is a
single vector in the submodule such that the submodule is contained in
its span. -/
lemma dim_submodule_le_one_iff (s : submodule K V) : module.rank K s ≤ 1 ↔ ∃ v₀ ∈ s, s ≤ K ∙ v₀ :=
begin
simp_rw [dim_le_one_iff, le_span_singleton_iff],
split,
{ rintro ⟨⟨v₀, hv₀⟩, h⟩,
use [v₀, hv₀],
intros v hv,
obtain ⟨r, hr⟩ := h ⟨v, hv⟩,
use r,
simp_rw [subtype.ext_iff, coe_smul, submodule.coe_mk] at hr,
exact hr },
{ rintro ⟨v₀, hv₀, h⟩,
use ⟨v₀, hv₀⟩,
rintro ⟨v, hv⟩,
obtain ⟨r, hr⟩ := h v hv,
use r,
simp_rw [subtype.ext_iff, coe_smul, submodule.coe_mk],
exact hr }
end
/-- A submodule has dimension at most `1` if and only if there is a
single vector, not necessarily in the submodule, such that the
submodule is contained in its span. -/
lemma dim_submodule_le_one_iff' (s : submodule K V) : module.rank K s ≤ 1 ↔ ∃ v₀, s ≤ K ∙ v₀ :=
begin
rw dim_submodule_le_one_iff,
split,
{ rintros ⟨v₀, hv₀, h⟩,
exact ⟨v₀, h⟩ },
{ rintros ⟨v₀, h⟩,
by_cases hw : ∃ w : V, w ∈ s ∧ w ≠ 0,
{ rcases hw with ⟨w, hw, hw0⟩,
use [w, hw],
rcases mem_span_singleton.1 (h hw) with ⟨r', rfl⟩,
have h0 : r' ≠ 0,
{ rintro rfl,
simpa using hw0 },
rwa span_singleton_smul_eq _ h0 },
{ push_neg at hw,
rw ←submodule.eq_bot_iff at hw,
simp [hw] } }
end
end field
end module
|
7d79874370683ac1c1a56db2e30ea66cb30dc954 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/category_theory/limits/shapes/terminal.lean | 056d469ad4df444dc80bbdfa5e8ffdbdb1b93268 | [
"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 | 3,329 | 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.limits.shapes.finite_products
import category_theory.pempty
/-!
# Initial and terminal objects in a category.
-/
universes v u
open category_theory
namespace category_theory.limits
variables (C : Type u) [𝒞 : category.{v} C]
include 𝒞
/-- A category has a terminal object if it has a limit over the empty diagram. -/
-- Use `has_terminal_of_unique` to construct instances.
class has_terminal :=
(has_limits_of_shape : has_limits_of_shape.{v} pempty C)
/-- A category has an initial object if it has a colimit over the empty diagram. -/
-- Use `has_initial_of_unique` to construct instances.
class has_initial :=
(has_colimits_of_shape : has_colimits_of_shape.{v} pempty C)
attribute [instance] has_terminal.has_limits_of_shape has_initial.has_colimits_of_shape
@[priority 100] -- see Note [lower instance priority]
instance [has_finite_products.{v} C] : has_terminal.{v} C :=
{ has_limits_of_shape :=
{ has_limit := λ F,
has_limit_of_equivalence_comp ((functor.empty.{v} (discrete pempty.{v+1})).as_equivalence.symm) } }
@[priority 100] -- see Note [lower instance priority]
instance [has_finite_coproducts.{v} C] : has_initial.{v} C :=
{ has_colimits_of_shape :=
{ has_colimit := λ F, has_colimit_of_equivalence_comp ((functor.empty (discrete pempty)).as_equivalence.symm) } }
abbreviation terminal [has_terminal.{v} C] : C := limit (functor.empty C)
abbreviation initial [has_initial.{v} C] : C := colimit (functor.empty C)
notation `⊤_` C:20 := terminal C
notation `⊥_` C:20 := initial C
section
variables {C}
/-- We can more explicitly show that a category has a terminal object by specifying the object,
and showing there is a unique morphism to it from any other object. -/
def has_terminal_of_unique (X : C) [h : Π Y : C, unique (Y ⟶ X)] : has_terminal.{v} C :=
{ has_limits_of_shape :=
{ has_limit := λ F,
{ cone := { X := X, π := { app := pempty.rec _ } },
is_limit := { lift := λ s, (h s.X).default } } } }
/-- We can more explicitly show that a category has an initial object by specifying the object,
and showing there is a unique morphism from it to any other object. -/
def has_initial_of_unique (X : C) [h : Π Y : C, unique (X ⟶ Y)] : has_initial.{v} C :=
{ has_colimits_of_shape :=
{ has_colimit := λ F,
{ cocone := { X := X, ι := { app := pempty.rec _ } },
is_colimit := { desc := λ s, (h s.X).default } } } }
/-- The map from an object to the terminal object. -/
abbreviation terminal.from [has_terminal.{v} C] (P : C) : P ⟶ ⊤_ C :=
limit.lift (functor.empty C) { X := P, π := by tidy }.
/-- The map to an object from the initial object. -/
abbreviation initial.to [has_initial.{v} C] (P : C) : ⊥_ C ⟶ P :=
colimit.desc (functor.empty C) { X := P, ι := by tidy }.
instance unique_to_terminal [has_terminal.{v} C] (P : C) : unique (P ⟶ ⊤_ C) :=
{ default := terminal.from P,
uniq := λ m, by { apply limit.hom_ext, rintro ⟨⟩ } }
instance unique_from_initial [has_initial.{v} C] (P : C) : unique (⊥_ C ⟶ P) :=
{ default := initial.to P,
uniq := λ m, by { apply colimit.hom_ext, rintro ⟨⟩ } }
end
end category_theory.limits
|
1b8ccf2c3a5220d3551ea0e0ca20e80d6a4ef0ea | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/algebra/module.lean | 3867b2dc45bb5e4cdeacdc52ce03b3b835f8764d | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 4,912 | lean | /-
Copyright (c) 2015 Nathaniel Thomas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nathaniel Thomas, Jeremy Avigad
Modules and vector spaces over a ring.
-/
import algebra.field
structure has_scalar [class] (F V : Type) :=
(smul : F → V → V)
infixl ` • `:73 := has_scalar.smul
/- modules over a ring -/
structure left_module [class] (R M : Type) [ringR : ring R]
extends has_scalar R M, add_comm_group M :=
(smul_left_distrib : ∀ (r : R) (x y : M), smul r (add x y) = (add (smul r x) (smul r y)))
(smul_right_distrib : ∀ (r s : R) (x : M), smul (ring.add r s) x = (add (smul r x) (smul s x)))
(mul_smul : ∀ r s x, smul (mul r s) x = smul r (smul s x))
(one_smul : ∀ x, smul one x = x)
section left_module
variables {R M : Type}
variable [ringR : ring R]
variable [moduleRM : left_module R M]
include ringR moduleRM
-- Note: the anonymous include does not work in the propositions below.
proposition smul_left_distrib (a : R) (u v : M) : a • (u + v) = a • u + a • v :=
!left_module.smul_left_distrib
proposition smul_right_distrib (a b : R) (u : M) : (a + b) • u = a • u + b • u :=
!left_module.smul_right_distrib
proposition mul_smul (a : R) (b : R) (u : M) : (a * b) • u = a • (b • u) :=
!left_module.mul_smul
proposition one_smul (u : M) : (1 : R) • u = u := !left_module.one_smul
proposition zero_smul (u : M) : (0 : R) • u = 0 :=
have (0 : R) • u + 0 • u = 0 • u + 0, by rewrite [-smul_right_distrib, *add_zero],
!add.left_cancel this
proposition smul_zero (a : R) : a • (0 : M) = 0 :=
have a • (0:M) + a • 0 = a • 0 + 0, by rewrite [-smul_left_distrib, *add_zero],
!add.left_cancel this
proposition neg_smul (a : R) (u : M) : (-a) • u = - (a • u) :=
eq_neg_of_add_eq_zero (by rewrite [-smul_right_distrib, add.left_inv, zero_smul])
proposition neg_one_smul (u : M) : -(1 : R) • u = -u :=
by rewrite [neg_smul, one_smul]
proposition smul_neg (a : R) (u : M) : a • (-u) = -(a • u) :=
by rewrite [-neg_one_smul, -mul_smul, mul_neg_one_eq_neg, neg_smul]
proposition smul_sub_left_distrib (a : R) (u v : M) : a • (u - v) = a • u - a • v :=
by rewrite [sub_eq_add_neg, smul_left_distrib, smul_neg]
proposition sub_smul_right_distrib (a b : R) (v : M) : (a - b) • v = a • v - b • v :=
by rewrite [sub_eq_add_neg, smul_right_distrib, neg_smul]
end left_module
/- linear maps -/
structure is_linear_map [class] (R : Type) {M₁ M₂ : Type}
[smul₁ : has_scalar R M₁] [smul₂ : has_scalar R M₂]
[add₁ : has_add M₁] [add₂ : has_add M₂]
(T : M₁ → M₂) :=
(additive : ∀ u v : M₁, T (u + v) = T u + T v)
(homogeneous : ∀ a : R, ∀ u : M₁, T (a • u) = a • T u)
proposition linear_map_additive (R : Type) {M₁ M₂ : Type}
[smul₁ : has_scalar R M₁] [smul₂ : has_scalar R M₂]
[add₁ : has_add M₁] [add₂ : has_add M₂]
(T : M₁ → M₂) [linT : is_linear_map R T] (u v : M₁) :
T (u + v) = T u + T v :=
is_linear_map.additive smul₁ smul₂ _ _ T u v
proposition linear_map_homogeneous {R M₁ M₂ : Type}
[smul₁ : has_scalar R M₁] [smul₂ : has_scalar R M₂]
[add₁ : has_add M₁] [add₂ : has_add M₂]
(T : M₁ → M₂) [linT : is_linear_map R T] (a : R) (u : M₁) :
T (a • u) = a • T u :=
is_linear_map.homogeneous smul₁ smul₂ _ _ T a u
proposition is_linear_map_id [instance] (R : Type) {M : Type}
[smulRM : has_scalar R M] [has_addM : has_add M] :
is_linear_map R (id : M → M) :=
is_linear_map.mk (take u v, rfl) (take a u, rfl)
section is_linear_map
variables {R M₁ M₂ : Type}
variable [ringR : ring R]
variable [moduleRM₁ : left_module R M₁]
variable [moduleRM₂ : left_module R M₂]
include ringR moduleRM₁ moduleRM₂
variable T : M₁ → M₂
variable [is_linear_mapT : is_linear_map R T]
include is_linear_mapT
proposition linear_map_zero : T 0 = 0 :=
calc
T 0 = T ((0 : R) • 0) : zero_smul
... = (0 : R) • T 0 : linear_map_homogeneous T
... = 0 : zero_smul
proposition linear_map_neg (u : M₁) : T (-u) = -(T u) :=
by rewrite [-neg_one_smul, linear_map_homogeneous T, neg_one_smul]
proposition linear_map_smul_add_smul (a b : R) (u v : M₁) :
T (a • u + b • v) = a • T u + b • T v :=
by rewrite [linear_map_additive R T, *linear_map_homogeneous T]
end is_linear_map
/- vector spaces -/
structure vector_space [class] (F V : Type) [fieldF : field F]
extends left_module F V
/- an example -/
section
variables (F V : Type)
variables [field F] [vector_spaceFV : vector_space F V]
variable T : V → V
variable [is_linear_map F T]
include vector_spaceFV
example (a b : F) (u v : V) : T (a • u + b • v) = a • T u + b • T v :=
!linear_map_smul_add_smul
end
|
6695bfe9a27396bd584586e9016862f3af442ac2 | aa2345b30d710f7e75f13157a35845ee6d48c017 | /ring_theory/localization.lean | 88d3cc4f477a821b039c414f2bb861feff379f1e | [
"Apache-2.0"
] | permissive | CohenCyril/mathlib | 5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe | a12d5a192f5923016752f638d19fc1a51610f163 | refs/heads/master | 1,586,031,957,957 | 1,541,432,824,000 | 1,541,432,824,000 | 156,246,337 | 0 | 0 | Apache-2.0 | 1,541,434,514,000 | 1,541,434,513,000 | null | UTF-8 | Lean | false | false | 8,762 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import tactic.ring data.quot ring_theory.ideals group_theory.submonoid
universe u
namespace localization
variables (α : Type u) [comm_ring α] (S : set α) [is_submonoid S]
def r : α × S → α × S → Prop :=
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩, ∃ t ∈ S, (s₁ * r₂ - s₂ * r₁) * t = 0
local infix ≈ := r α S
section
variables {α S}
theorem r_of_eq : ∀{a₀ a₁ : α × S} (h : a₀.2.1 * a₁.1 = a₁.2.1 * a₀.1), a₀ ≈ a₁
| ⟨r₀, s₀, hs₀⟩ ⟨r₁, s₁, hs₁⟩ h := ⟨1, is_submonoid.one_mem S, by simp [h] at h ⊢⟩
end
theorem refl (x : α × S) : x ≈ x := r_of_eq rfl
theorem symm : ∀ (x y : α × S), x ≈ y → y ≈ x :=
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩, ⟨t, hts,
calc (s₂ * r₁ - s₁ * r₂) * t = -((s₁ * r₂ - s₂ * r₁) * t) : by simp [add_mul]
... = 0 : by rw ht; simp⟩
theorem trans : ∀ (x y z : α × S), x ≈ y → y ≈ z → x ≈ z :=
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨t, hts, ht⟩ ⟨t', hts', ht'⟩,
⟨s₂ * t' * t, is_submonoid.mul_mem (is_submonoid.mul_mem hs₂ hts') hts,
calc (s₁ * r₃ - s₃ * r₁) * (s₂ * t' * t) =
t' * s₃ * ((s₁ * r₂ - s₂ * r₁) * t) + t * s₁ * ((s₂ * r₃ - s₃ * r₂) * t') :
by simp [mul_left_comm, mul_add, mul_comm]
... = 0 : by rw [ht, ht']; simp⟩
instance : setoid (α × S) :=
⟨r α S, refl α S, symm α S, trans α S⟩
def loc := quotient $ localization.setoid α S
instance : has_add (loc α S) :=
⟨quotient.lift₂
(λ x y : α × S, (⟦⟨x.2 * y.1 + y.2 * x.1, _, is_submonoid.mul_mem x.2.2 y.2.2⟩⟧ : loc α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩,
quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅,
calc (s₁ * s₂ * (s₃ * r₄ + s₄ * r₃) - s₃ * s₄ * (s₁ * r₂ + s₂ * r₁)) * (t₆ * t₅) =
s₁ * s₃ * ((s₂ * r₄ - s₄ * r₂) * t₆) * t₅ + s₂ * s₄ * ((s₁ * r₃ - s₃ * r₁) * t₅) * t₆ : by ring
... = 0 : by rw [ht₆, ht₅]; simp⟩⟩
instance : has_neg (loc α S) :=
⟨quotient.lift (λ x : α × S, (⟦⟨-x.1, x.2⟩⟧ : loc α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩,
quotient.sound ⟨t, hts,
calc (s₁ * -r₂ - s₂ * -r₁) * t = -((s₁ * r₂ - s₂ * r₁) * t) : by ring
... = 0 : by rw ht; simp⟩⟩
instance : has_mul (loc α S) :=
⟨quotient.lift₂
(λ x y : α × S, (⟦⟨x.1 * y.1, _, is_submonoid.mul_mem x.2.2 y.2.2⟩⟧ : loc α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩,
quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅,
calc ((s₁ * s₂) * (r₃ * r₄) - (s₃ * s₄) * (r₁ * r₂)) * (t₆ * t₅) =
t₆ * ((s₁ * r₃ - s₃ * r₁) * t₅) * r₂ * s₄ + t₅ * ((s₂ * r₄ - s₄ * r₂) * t₆) * r₃ * s₁ :
by simp [mul_left_comm, mul_add, mul_comm]
... = 0 : by rw [ht₅, ht₆]; simp⟩⟩
def of_comm_ring : α → loc α S := λ r, ⟦⟨r, 1, is_submonoid.one_mem S⟩⟧
instance : comm_ring (loc α S) :=
by refine
{ add := has_add.add,
add_assoc := λ m n k, quotient.induction_on₃ m n k _,
zero := of_comm_ring α S 0,
zero_add := quotient.ind _,
add_zero := quotient.ind _,
neg := has_neg.neg,
add_left_neg := quotient.ind _,
add_comm := quotient.ind₂ _,
mul := has_mul.mul,
mul_assoc := λ m n k, quotient.induction_on₃ m n k _,
one := of_comm_ring α S 1,
one_mul := quotient.ind _,
mul_one := quotient.ind _,
left_distrib := λ m n k, quotient.induction_on₃ m n k _,
right_distrib := λ m n k, quotient.induction_on₃ m n k _,
mul_comm := quotient.ind₂ _ };
{ intros,
try {rcases a with ⟨r₁, s₁, hs₁⟩},
try {rcases b with ⟨r₂, s₂, hs₂⟩},
try {rcases c with ⟨r₃, s₃, hs₃⟩},
refine (quotient.sound $ r_of_eq _),
simp [mul_left_comm, mul_add, mul_comm] }
instance : is_ring_hom (of_comm_ring α S) :=
{ map_add := λ x y, quotient.sound $ by simp,
map_mul := λ x y, quotient.sound $ by simp,
map_one := rfl }
variable {α}
def away (x : α) := loc α (powers x)
instance away.comm_ring (x : α) : comm_ring (away x) :=
localization.comm_ring α (powers x)
section at_prime
variables (P : ideal α) [hp : ideal.is_prime P]
include hp
instance prime.is_submonoid :
is_submonoid (set.compl ↑P) :=
{ one_mem := P.ne_top_iff_one.1 hp.1,
mul_mem := λ x y hnx hny hxy, or.cases_on (hp.2 hxy) hnx hny }
def at_prime := loc α (set.compl P)
instance at_prime.comm_ring : comm_ring (at_prime P) :=
localization.comm_ring α (set.compl P)
instance at_prime.local_ring : is_local_ring (at_prime P) :=
local_of_nonunits_ideal
(λ hze,
let ⟨t, hts, ht⟩ := quotient.exact hze in
hts $ have htz : t = 0, by simpa using ht,
suffices (0:α) ∈ P, by rwa htz,
P.zero_mem)
(begin
rintro ⟨⟨r₁, s₁, hs₁⟩⟩ ⟨⟨r₂, s₂, hs₂⟩⟩ hx hy hu,
rcases is_unit_iff_exists_inv.1 hu with ⟨⟨⟨r₃, s₃, hs₃⟩⟩, hz⟩,
rcases quotient.exact hz with ⟨t, hts, ht⟩,
simp at ht,
have : ∀ {r s hs}, (⟦⟨r, s, hs⟩⟧ : at_prime P) ∈ nonunits (at_prime P) → r ∈ P,
{ haveI := classical.dec,
exact λ r s hs, not_imp_comm.1 (λ nr,
is_unit_iff_exists_inv.2 ⟨⟦⟨s, r, nr⟩⟧,
quotient.sound $ r_of_eq $ by simp [mul_comm]⟩) },
have hr₃ := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right hts,
have := (ideal.add_mem_iff_left _ _).1 hr₃,
{ exact not_or (mt hp.mem_or_mem (not_or hs₁ hs₂)) hs₃ (hp.mem_or_mem this) },
{ exact P.neg_mem (P.mul_mem_right
(P.add_mem (P.mul_mem_left (this hy)) (P.mul_mem_left (this hx)))) }
end)
end at_prime
variable (α)
def non_zero_divisors : set α := {x | ∀ z, z * x = 0 → z = 0}
instance non_zero_divisors.is_submonoid : is_submonoid (non_zero_divisors α) :=
{ one_mem := λ z hz, by simpa using hz,
mul_mem := λ x₁ x₂ hx₁ hx₂ z hz,
have z * x₁ * x₂ = 0, by rwa mul_assoc,
hx₁ z $ hx₂ (z * x₁) this }
def quotient_ring := loc α (non_zero_divisors α)
instance quotient_ring.comm_ring : comm_ring (quotient_ring α) :=
localization.comm_ring α (non_zero_divisors α)
section quotient_ring
variables {β : Type u} [integral_domain β] [decidable_eq β]
lemma ne_zero_of_mem_non_zero_divisors {x : β}
(hm : x ∈ localization.non_zero_divisors β) : x ≠ 0 | hz :=
zero_ne_one (hm 1 (by simpa)).symm
lemma eq_zero_of_ne_zero_of_mul_eq_zero {x y : β} :
x ≠ 0 → y * x = 0 → y = 0 :=
λ hnx hxy, or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma mem_non_zero_divisors_of_ne_zero {x : β} :
x ≠ 0 → x ∈ localization.non_zero_divisors β :=
λ hnx z, eq_zero_of_ne_zero_of_mul_eq_zero hnx
variable (β)
def inv_aux : β × (non_zero_divisors β) → quotient_ring β :=
λ ⟨r, s, hs⟩, if h : r = 0 then 0 else ⟦⟨s, r, mem_non_zero_divisors_of_ne_zero h⟩⟧
instance : has_inv (quotient_ring β) :=
⟨quotient.lift (inv_aux β) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩,
begin
have hrs : s₁ * r₂ = 0 + s₂ * r₁,
from sub_eq_iff_eq_add.1 (hts _ ht),
by_cases hr₁ : r₁ = 0;
by_cases hr₂ : r₂ = 0;
simp [hr₁, hr₂] at hrs; simp [inv_aux, hr₁, hr₂],
{ exfalso,
exact ne_zero_of_mem_non_zero_divisors hs₁ hrs },
{ exfalso,
exact ne_zero_of_mem_non_zero_divisors hs₂ hrs },
{ apply r_of_eq,
simpa [mul_comm] using hrs.symm }
end⟩
def quotient_ring.field.of_integral_domain : field (quotient_ring β) :=
by refine
{ inv := has_inv.inv,
zero_ne_one := λ hzo,
let ⟨t, hts, ht⟩ := quotient.exact hzo in
zero_ne_one (by simpa using hts _ ht : 0 = 1),
mul_inv_cancel := quotient.ind _,
inv_mul_cancel := quotient.ind _,
..localization.comm_ring β _ };
{ intros x hnx,
rcases x with ⟨x, z, hz⟩,
have : x ≠ 0,
from assume hx, hnx (quotient.sound $ r_of_eq $ by simp [hx]),
simp [has_inv.inv, inv_aux, inv_aux._match_1, this],
exact (quotient.sound $ r_of_eq $ by simp; ring) }
end quotient_ring
end localization
|
0e3eba688ce2dd4064d8318abc206b2a5d9108de | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/order/rel_iso.lean | e4381f4c3e62c17ef29f8a3bf7ed5172343cf104 | [
"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 | 34,817 | 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 algebra.group.defs
import logic.embedding
import order.rel_classes
/-!
# Relation homomorphisms, embeddings, isomorphisms
This file defines relation homomorphisms, embeddings, isomorphisms and order embeddings and
isomorphisms.
## Main declarations
* `rel_hom`: Relation homomorphism. A `rel_hom r s` is a function `f : α → β` such that
`r a b → s (f a) (f b)`.
* `rel_embedding`: Relation embedding. A `rel_embedding r s` is an embedding `f : α ↪ β` such that
`r a b ↔ s (f a) (f b)`.
* `rel_iso`: Relation isomorphism. A `rel_iso r s` is an equivalence `f : α ≃ β` such that
`r a b ↔ s (f a) (f b)`.
* `order_embedding`: Relation embedding. An `order_embedding α β` is an embedding `f : α ↪ β` such
that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_embedding α β (≤) (≤)`.
* `order_iso`: Relation isomorphism. An `order_iso α β` is an equivalence `f : α ≃ β` such that
`a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_iso α β (≤) (≤)`.
* `sum_lex_congr`, `prod_lex_congr`: Creates a relation homomorphism between two `sum_lex` or two
`prod_lex` from relation homomorphisms between their arguments.
## Notation
* `→r`: `rel_hom`
* `↪r`: `rel_embedding`
* `≃r`: `rel_iso`
* `↪o`: `order_embedding`
* `≃o`: `order_iso`
-/
open function
universes u v w
variables {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop}
/-- A relation homomorphism with respect to a given pair of relations `r` and `s`
is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/
@[nolint has_inhabited_instance]
structure rel_hom {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) :=
(to_fun : α → β)
(map_rel' : ∀ {a b}, r a b → s (to_fun a) (to_fun b))
infix ` →r `:25 := rel_hom
namespace rel_hom
instance : has_coe_to_fun (r →r s) := ⟨λ _, α → β, λ o, o.to_fun⟩
initialize_simps_projections rel_hom (to_fun → apply)
theorem map_rel (f : r →r s) : ∀ {a b}, r a b → s (f a) (f b) := f.map_rel'
@[simp] theorem coe_fn_mk (f : α → β) (o) :
(@rel_hom.mk _ _ r s f o : α → β) = f := rfl
@[simp] theorem coe_fn_to_fun (f : r →r s) : (f.to_fun : α → β) = f := rfl
/-- The map `coe_fn : (r →r s) → (α → β)` is injective. -/
theorem coe_fn_injective : @function.injective (r →r s) (α → β) coe_fn
| ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ h := by { congr, exact h }
@[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g :=
coe_fn_injective (funext h)
theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
/-- Identity map is a relation homomorphism. -/
@[refl, simps] protected def id (r : α → α → Prop) : r →r r :=
⟨λ x, x, λ a b x, x⟩
/-- Composition of two relation homomorphisms is a relation homomorphism. -/
@[trans, simps] protected def comp (g : s →r t) (f : r →r s) : r →r t :=
⟨λ x, g (f x), λ a b h, g.2 (f.2 h)⟩
/-- A relation homomorphism is also a relation homomorphism between dual relations. -/
protected def swap (f : r →r s) : swap r →r swap s :=
⟨f, λ a b, f.map_rel⟩
/-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/
def preimage (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s := ⟨f, λ a b, id⟩
protected theorem is_irrefl : ∀ (f : r →r s) [is_irrefl β s], is_irrefl α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o h)⟩
protected theorem is_asymm : ∀ (f : r →r s) [is_asymm β s], is_asymm α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o h₁) (o h₂)⟩
protected theorem acc (f : r →r s) (a : α) : acc s (f a) → acc r a :=
begin
generalize h : f a = b, intro ac,
induction ac with _ H IH generalizing a, subst h,
exact ⟨_, λ a' h, IH (f a') (f.map_rel h) _ rfl⟩
end
protected theorem well_founded : ∀ (f : r →r s) (h : well_founded s), well_founded r
| f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩
lemma map_inf {α β : Type*} [semilattice_inf α] [linear_order β]
(a : ((<) : β → β → Prop) →r ((<) : α → α → Prop)) (m n : β) : a (m ⊓ n) = a m ⊓ a n :=
begin
symmetry, cases le_or_lt n m with h,
{ rw [inf_eq_right.mpr h, inf_eq_right], exact strict_mono.monotone (λ x y, a.map_rel) h, },
{ rw [inf_eq_left.mpr (le_of_lt h), inf_eq_left], exact le_of_lt (a.map_rel h), },
end
lemma map_sup {α β : Type*} [semilattice_sup α] [linear_order β]
(a : ((>) : β → β → Prop) →r ((>) : α → α → Prop)) (m n : β) : a (m ⊔ n) = a m ⊔ a n :=
begin
symmetry, cases le_or_lt m n with h,
{ rw [sup_eq_right.mpr h, sup_eq_right], exact strict_mono.monotone (λ x y, a.swap.map_rel) h, },
{ rw [sup_eq_left.mpr (le_of_lt h), sup_eq_left], exact le_of_lt (a.map_rel h), },
end
end rel_hom
/-- An increasing function is injective -/
lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r]
[is_irrefl β s] (f : α → β) (hf : ∀ {x y}, r x y → s (f x) (f y)) : injective f :=
begin
intros x y hxy,
rcases trichotomous_of r x y with h | h | h,
have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this,
exact h,
have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this
end
/-- An increasing function is injective -/
lemma rel_hom.injective_of_increasing [is_trichotomous α r]
[is_irrefl β s] (f : r →r s) : injective f :=
injective_of_increasing r s f (λ x y, f.map_rel)
theorem surjective.well_founded_iff {f : α → β} (hf : surjective f)
(o : ∀ {a b}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s :=
iff.intro (begin
apply rel_hom.well_founded,
refine rel_hom.mk _ _,
{exact classical.some hf.has_right_inverse},
intros a b h, apply o.2, convert h,
iterate 2 { apply classical.some_spec hf.has_right_inverse },
end) (rel_hom.well_founded ⟨f, λ _ _, o.1⟩)
/-- A relation embedding with respect to a given pair of relations `r` and `s`
is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/
structure rel_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β :=
(map_rel_iff' : ∀ {a b}, s (to_embedding a) (to_embedding b) ↔ r a b)
infix ` ↪r `:25 := rel_embedding
/-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`.
This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/
abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] :=
@rel_embedding α β (≤) (≤)
infix ` ↪o `:25 := order_embedding
/-- The induced relation on a subtype is an embedding under the natural inclusion. -/
definition subtype.rel_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) :
((subtype.val : subtype p → X) ⁻¹'o r) ↪r r :=
⟨embedding.subtype p, λ x y, iff.rfl⟩
theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop}
(hs : equivalence s) : equivalence (f ⁻¹'o s) :=
⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩
namespace rel_embedding
/-- A relation embedding is also a relation homomorphism -/
def to_rel_hom (f : r ↪r s) : (r →r s) :=
{ to_fun := f.to_embedding.to_fun,
map_rel' := λ x y, (map_rel_iff' f).mpr }
instance : has_coe (r ↪r s) (r →r s) := ⟨to_rel_hom⟩
-- see Note [function coercion]
instance : has_coe_to_fun (r ↪r s) := ⟨λ _, α → β, λ o, o.to_embedding⟩
/-- 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 : r ↪r s) : α → β := h
initialize_simps_projections rel_embedding (to_embedding_to_fun → apply, -to_embedding)
@[simp] lemma to_rel_hom_eq_coe (f : r ↪r s) : f.to_rel_hom = f := rfl
@[simp] lemma coe_coe_fn (f : r ↪r s) : ((f : r →r s) : α → β) = f := rfl
theorem injective (f : r ↪r s) : injective f := f.inj'
theorem map_rel_iff (f : r ↪r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff'
@[simp] theorem coe_fn_mk (f : α ↪ β) (o) :
(@rel_embedding.mk _ _ r s f o : α → β) = f := rfl
@[simp] theorem coe_fn_to_embedding (f : r ↪r s) : (f.to_embedding : α → β) = f := rfl
/-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. -/
theorem coe_fn_injective : @function.injective (r ↪r s) (α → β) coe_fn
| ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by { congr, exact h }
@[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g :=
coe_fn_injective (funext h)
theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
/-- Identity map is a relation embedding. -/
@[refl, simps] protected def refl (r : α → α → Prop) : r ↪r r :=
⟨embedding.refl _, λ a b, iff.rfl⟩
/-- Composition of two relation embeddings is a relation embedding. -/
@[trans] protected def trans (f : r ↪r s) (g : s ↪r t) : r ↪r t :=
⟨f.1.trans g.1, λ a b, by simp [f.map_rel_iff, g.map_rel_iff]⟩
instance (r : α → α → Prop) : inhabited (r ↪r r) := ⟨rel_embedding.refl _⟩
theorem trans_apply (f : r ↪r s) (g : s ↪r t) (a : α) : (f.trans g) a = g (f a) := rfl
@[simp] theorem coe_trans (f : r ↪r s) (g : s ↪r t) : ⇑(f.trans g) = g ∘ f := rfl
/-- A relation embedding is also a relation embedding between dual relations. -/
protected def swap (f : r ↪r s) : swap r ↪r swap s :=
⟨f.to_embedding, λ a b, f.map_rel_iff⟩
/-- If `f` is injective, then it is a relation embedding from the
preimage relation of `s` to `s`. -/
def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ↪r s := ⟨f, λ a b, iff.rfl⟩
theorem eq_preimage (f : r ↪r s) : r = f ⁻¹'o s :=
by { ext a b, exact f.map_rel_iff.symm }
protected theorem is_irrefl (f : r ↪r s) [is_irrefl β s] : is_irrefl α r :=
⟨λ a, mt f.map_rel_iff.2 (irrefl (f a))⟩
protected theorem is_refl (f : r ↪r s) [is_refl β s] : is_refl α r :=
⟨λ a, f.map_rel_iff.1 $ refl _⟩
protected theorem is_symm (f : r ↪r s) [is_symm β s] : is_symm α r :=
⟨λ a b, imp_imp_imp f.map_rel_iff.2 f.map_rel_iff.1 symm⟩
protected theorem is_asymm (f : r ↪r s) [is_asymm β s] : is_asymm α r :=
⟨λ a b h₁ h₂, asymm (f.map_rel_iff.2 h₁) (f.map_rel_iff.2 h₂)⟩
protected theorem is_antisymm : ∀ (f : r ↪r s) [is_antisymm β s], is_antisymm α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.2 h₁) (o.2 h₂))⟩
protected theorem is_trans : ∀ (f : r ↪r s) [is_trans β s], is_trans α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.1 (H _ _ _ (o.2 h₁) (o.2 h₂))⟩
protected theorem is_total : ∀ (f : r ↪r s) [is_total β s], is_total α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).1 (H _ _)⟩
protected theorem is_preorder : ∀ (f : r ↪r s) [is_preorder β s], is_preorder α r
| f H := by exactI {..f.is_refl, ..f.is_trans}
protected theorem is_partial_order : ∀ (f : r ↪r s) [is_partial_order β s], is_partial_order α r
| f H := by exactI {..f.is_preorder, ..f.is_antisymm}
protected theorem is_linear_order : ∀ (f : r ↪r s) [is_linear_order β s], is_linear_order α r
| f H := by exactI {..f.is_partial_order, ..f.is_total}
protected theorem is_strict_order : ∀ (f : r ↪r s) [is_strict_order β s], is_strict_order α r
| f H := by exactI {..f.is_irrefl, ..f.is_trans}
protected theorem is_trichotomous : ∀ (f : r ↪r s) [is_trichotomous β s], is_trichotomous α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff o)).1 (H _ _)⟩
protected theorem is_strict_total_order' :
∀ (f : r ↪r s) [is_strict_total_order' β s], is_strict_total_order' α r
| f H := by exactI {..f.is_trichotomous, ..f.is_strict_order}
protected theorem acc (f : r ↪r s) (a : α) : acc s (f a) → acc r a :=
begin
generalize h : f a = b, intro ac,
induction ac with _ H IH generalizing a, subst h,
exact ⟨_, λ a' h, IH (f a') (f.map_rel_iff.2 h) _ rfl⟩
end
protected theorem well_founded : ∀ (f : r ↪r s) (h : well_founded s), well_founded r
| f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩
protected theorem is_well_order : ∀ (f : r ↪r s) [is_well_order β s], is_well_order α r
| f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'}
/--
To define an relation embedding from an antisymmetric relation `r` to a reflexive relation `s` it
suffices to give a function together with a proof that it satisfies `s (f a) (f b) ↔ r a b`.
-/
def of_map_rel_iff (f : α → β) [is_antisymm α r] [is_refl β s]
(hf : ∀ a b, s (f a) (f b) ↔ r a b) : r ↪r s :=
{ to_fun := f,
inj' := λ x y h, antisymm ((hf _ _).1 (h ▸ refl _)) ((hf _ _).1 (h ▸ refl _)),
map_rel_iff' := hf }
@[simp]
lemma of_map_rel_iff_coe (f : α → β) [is_antisymm α r] [is_refl β s]
(hf : ∀ a b, s (f a) (f b) ↔ r a b) :
⇑(of_map_rel_iff f hf : r ↪r s) = f :=
rfl
/-- It suffices to prove `f` is monotone between strict relations
to show it is a relation embedding. -/
def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β)
(H : ∀ a b, r a b → s (f a) (f b)) : r ↪r s :=
begin
haveI := @is_asymm.is_irrefl β s _,
refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨λ h, _, H _ _⟩⟩,
{ refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _;
exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) },
{ refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)),
{ subst e, exact irrefl _ h },
{ exact asymm (H _ _ h') h } }
end
@[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) :
(@of_monotone _ _ r s _ _ f H : α → β) = f := rfl
/-- Embeddings of partial orders that preserve `<` also preserve `≤`. -/
def order_embedding_of_lt_embedding [partial_order α] [partial_order β]
(f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) :
α ↪o β :=
{ map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective.eq_iff] }, .. f }
@[simp]
lemma order_embedding_of_lt_embedding_apply [partial_order α] [partial_order β]
{f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)} {x : α} :
order_embedding_of_lt_embedding f x = f x := rfl
end rel_embedding
namespace order_embedding
variables [preorder α] [preorder β] (f : α ↪o β)
/-- `<` is preserved by order embeddings of preorders. -/
def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) :=
{ map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff], .. f }
@[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl
@[simp] theorem le_iff_le {a b} : (f a) ≤ (f b) ↔ a ≤ b := f.map_rel_iff
@[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b :=
f.lt_embedding.map_rel_iff
@[simp] lemma eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff
protected theorem monotone : monotone f := λ x y, f.le_iff_le.2
protected theorem strict_mono : strict_mono f := λ x y, f.lt_iff_lt.2
protected theorem acc (a : α) : acc (<) (f a) → acc (<) a :=
f.lt_embedding.acc a
protected theorem well_founded :
well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) :=
f.lt_embedding.well_founded
protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) :=
f.lt_embedding.is_well_order
/-- An order embedding is also an order embedding between dual orders. -/
protected def dual : order_dual α ↪o order_dual β :=
⟨f.to_embedding, λ a b, f.map_rel_iff⟩
/--
To define an order embedding from a partial order to a preorder it suffices to give a function
together with a proof that it satisfies `f a ≤ f b ↔ a ≤ b`.
-/
def of_map_le_iff {α β} [partial_order α] [preorder β] (f : α → β)
(hf : ∀ a b, f a ≤ f b ↔ a ≤ b) : α ↪o β :=
rel_embedding.of_map_rel_iff f hf
@[simp] lemma coe_of_map_le_iff {α β} [partial_order α] [preorder β] {f : α → β} (h) :
⇑(of_map_le_iff f h) = f := rfl
/-- A strictly monotone map from a linear order is an order embedding. --/
def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β)
(h : strict_mono f) : α ↪o β :=
of_map_le_iff f (λ _ _, h.le_iff_le)
@[simp] lemma coe_of_strict_mono {α β} [linear_order α] [preorder β] {f : α → β}
(h : strict_mono f) : ⇑(of_strict_mono f h) = f := rfl
/-- Embedding of a subtype into the ambient type as an `order_embedding`. -/
@[simps {fully_applied := ff}] def subtype (p : α → Prop) : subtype p ↪o α :=
⟨embedding.subtype p, λ x y, iff.rfl⟩
end order_embedding
/-- A relation isomorphism is an equivalence that is also a relation embedding. -/
structure rel_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β :=
(map_rel_iff' : ∀ {a b}, s (to_equiv a) (to_equiv b) ↔ r a b)
infix ` ≃r `:25 := rel_iso
/-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`.
This definition is an abbreviation of `rel_iso (≤) (≤)`. -/
abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤)
infix ` ≃o `:25 := order_iso
namespace rel_iso
/-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion
but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s`
in the target type. -/
def to_rel_embedding (f : r ≃r s) : r ↪r s :=
⟨f.to_equiv.to_embedding, f.map_rel_iff'⟩
instance : has_coe (r ≃r s) (r ↪r s) := ⟨to_rel_embedding⟩
-- see Note [function coercion]
instance : has_coe_to_fun (r ≃r s) := ⟨λ _, α → β, λ f, f⟩
@[simp] lemma to_rel_embedding_eq_coe (f : r ≃r s) : f.to_rel_embedding = f := rfl
@[simp] lemma coe_coe_fn (f : r ≃r s) : ((f : r ↪r s) : α → β) = f := rfl
theorem map_rel_iff (f : r ≃r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff'
@[simp] theorem coe_fn_mk (f : α ≃ β) (o : ∀ ⦃a b⦄, s (f a) (f b) ↔ r a b) :
(rel_iso.mk f o : α → β) = f := rfl
@[simp] theorem coe_fn_to_equiv (f : r ≃r s) : (f.to_equiv : α → β) = f := rfl
theorem to_equiv_injective : injective (to_equiv : (r ≃r s) → α ≃ β)
| ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h }
/-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse
`function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/
theorem coe_fn_injective : @function.injective (r ≃r s) (α → β) coe_fn :=
equiv.coe_fn_injective.comp to_equiv_injective
@[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g :=
coe_fn_injective (funext h)
theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
/-- Inverse map of a relation isomorphism is a relation isomorphism. -/
@[symm] protected def symm (f : r ≃r s) : s ≃r r :=
⟨f.to_equiv.symm, λ a b, by erw [← f.map_rel_iff, f.1.apply_symm_apply, f.1.apply_symm_apply]⟩
/-- 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 : r ≃r s) : α → β := h
/-- See Note [custom simps projection]. -/
def simps.symm_apply (h : r ≃r s) : β → α := h.symm
initialize_simps_projections rel_iso
(to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv)
/-- Identity map is a relation isomorphism. -/
@[refl, simps apply] protected def refl (r : α → α → Prop) : r ≃r r :=
⟨equiv.refl _, λ a b, iff.rfl⟩
/-- Composition of two relation isomorphisms is a relation isomorphism. -/
@[trans, simps apply] protected def trans (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t :=
⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, f₂.map_rel_iff.trans f₁.map_rel_iff⟩
instance (r : α → α → Prop) : inhabited (r ≃r r) := ⟨rel_iso.refl _⟩
@[simp] lemma default_def (r : α → α → Prop) : default (r ≃r r) = rel_iso.refl r := rfl
/-- a relation isomorphism is also a relation isomorphism between dual relations. -/
protected def swap (f : r ≃r s) : (swap r) ≃r (swap s) :=
⟨f.to_equiv, λ _ _, f.map_rel_iff⟩
@[simp] theorem coe_fn_symm_mk (f o) : ((@rel_iso.mk _ _ r s f o).symm : β → α) = f.symm :=
rfl
@[simp] theorem apply_symm_apply (e : r ≃r s) (x : β) : e (e.symm x) = x :=
e.to_equiv.apply_symm_apply x
@[simp] theorem symm_apply_apply (e : r ≃r s) (x : α) : e.symm (e x) = x :=
e.to_equiv.symm_apply_apply x
theorem rel_symm_apply (e : r ≃r s) {x y} : r x (e.symm y) ↔ s (e x) y :=
by rw [← e.map_rel_iff, e.apply_symm_apply]
theorem symm_apply_rel (e : r ≃r s) {x y} : r (e.symm x) y ↔ s x (e y) :=
by rw [← e.map_rel_iff, e.apply_symm_apply]
protected lemma bijective (e : r ≃r s) : bijective e := e.to_equiv.bijective
protected lemma injective (e : r ≃r s) : injective e := e.to_equiv.injective
protected lemma surjective (e : r ≃r s) : surjective e := e.to_equiv.surjective
@[simp] lemma range_eq (e : r ≃r s) : set.range e = set.univ := e.surjective.range_eq
@[simp] lemma eq_iff_eq (f : r ≃r s) {a b} : f a = f b ↔ a = b :=
f.injective.eq_iff
/-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/
protected def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃r s := ⟨f, λ a b, iff.rfl⟩
/-- A surjective relation embedding is a relation isomorphism. -/
@[simps apply]
noncomputable def of_surjective (f : r ↪r s) (H : surjective f) : r ≃r s :=
⟨equiv.of_bijective f ⟨f.injective, H⟩, λ a b, f.map_rel_iff⟩
/--
Given relation isomorphisms `r₁ ≃r s₁` and `r₂ ≃r s₂`, construct a relation isomorphism for the
lexicographic orders on the sum.
-/
def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂}
(e₁ : @rel_iso α₁ β₁ r₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) :
sum.lex r₁ r₂ ≃r sum.lex s₁ s₂ :=
⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b,
by cases e₁ with f hf; cases e₂ with g hg;
cases a; cases b; simp [hf, hg]⟩
/--
Given relation isomorphisms `r₁ ≃r s₁` and `r₂ ≃r s₂`, construct a relation isomorphism for the
lexicographic orders on the product.
-/
def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂}
(e₁ : @rel_iso α₁ β₁ r₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) :
prod.lex r₁ r₂ ≃r prod.lex s₁ s₂ :=
⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv,
λ a b, by simp [prod.lex_def, e₁.map_rel_iff, e₂.map_rel_iff]⟩
instance : group (r ≃r r) :=
{ one := rel_iso.refl r,
mul := λ f₁ f₂, f₂.trans f₁,
inv := rel_iso.symm,
mul_assoc := λ f₁ f₂ f₃, rfl,
one_mul := λ f, ext $ λ _, rfl,
mul_one := λ f, ext $ λ _, rfl,
mul_left_inv := λ f, ext f.symm_apply_apply }
@[simp] lemma coe_one : ⇑(1 : r ≃r r) = id := rfl
@[simp] lemma coe_mul (e₁ e₂ : r ≃r r) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl
lemma mul_apply (e₁ e₂ : r ≃r r) (x : α) : (e₁ * e₂) x = e₁ (e₂ x) := rfl
@[simp] lemma inv_apply_self (e : r ≃r r) (x) : e⁻¹ (e x) = x := e.symm_apply_apply x
@[simp] lemma apply_inv_self (e : r ≃r r) (x) : e (e⁻¹ x) = x := e.apply_symm_apply x
end rel_iso
namespace order_iso
section has_le
variables [has_le α] [has_le β] [has_le γ]
/-- Reinterpret an order isomorphism as an order embedding. -/
def to_order_embedding (e : α ≃o β) : α ↪o β :=
e.to_rel_embedding
@[simp] lemma coe_to_order_embedding (e : α ≃o β) :
⇑(e.to_order_embedding) = e := rfl
protected lemma bijective (e : α ≃o β) : bijective e := e.to_equiv.bijective
protected lemma injective (e : α ≃o β) : injective e := e.to_equiv.injective
protected lemma surjective (e : α ≃o β) : surjective e := e.to_equiv.surjective
@[simp] lemma range_eq (e : α ≃o β) : set.range e = set.univ := e.surjective.range_eq
@[simp] lemma apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y :=
e.to_equiv.apply_eq_iff_eq
/-- Identity order isomorphism. -/
def refl (α : Type*) [has_le α] : α ≃o α := rel_iso.refl (≤)
@[simp] lemma coe_refl : ⇑(refl α) = id := rfl
lemma refl_apply (x : α) : refl α x = x := rfl
@[simp] lemma refl_to_equiv : (refl α).to_equiv = equiv.refl α := rfl
/-- Inverse of an order isomorphism. -/
def symm (e : α ≃o β) : β ≃o α := e.symm
@[simp] lemma apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x :=
e.to_equiv.apply_symm_apply x
@[simp] lemma symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x :=
e.to_equiv.symm_apply_apply x
@[simp] lemma symm_refl (α : Type*) [has_le α] : (refl α).symm = refl α := rfl
lemma apply_eq_iff_eq_symm_apply (e : α ≃o β) (x : α) (y : β) : e x = y ↔ x = e.symm y :=
e.to_equiv.apply_eq_iff_eq_symm_apply
theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x :=
e.to_equiv.symm_apply_eq
@[simp] lemma symm_symm (e : α ≃o β) : e.symm.symm = e := by { ext, refl }
lemma symm_injective : injective (symm : (α ≃o β) → (β ≃o α)) :=
λ e e' h, by rw [← e.symm_symm, h, e'.symm_symm]
@[simp] lemma to_equiv_symm (e : α ≃o β) : e.to_equiv.symm = e.symm.to_equiv := rfl
@[simp] lemma symm_image_image (e : α ≃o β) (s : set α) : e.symm '' (e '' s) = s :=
e.to_equiv.symm_image_image s
@[simp] lemma image_symm_image (e : α ≃o β) (s : set β) : e '' (e.symm '' s) = s :=
e.to_equiv.image_symm_image s
lemma image_eq_preimage (e : α ≃o β) (s : set α) : e '' s = e.symm ⁻¹' s :=
e.to_equiv.image_eq_preimage s
@[simp] lemma preimage_symm_preimage (e : α ≃o β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s :=
e.to_equiv.preimage_symm_preimage s
@[simp] lemma symm_preimage_preimage (e : α ≃o β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s :=
e.to_equiv.symm_preimage_preimage s
@[simp] lemma image_preimage (e : α ≃o β) (s : set β) : e '' (e ⁻¹' s) = s :=
e.to_equiv.image_preimage s
@[simp] lemma preimage_image (e : α ≃o β) (s : set α) : e ⁻¹' (e '' s) = s :=
e.to_equiv.preimage_image s
/-- Composition of two order isomorphisms is an order isomorphism. -/
@[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := e.trans e'
@[simp] lemma coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl
lemma trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl
@[simp] lemma refl_trans (e : α ≃o β) : (refl α).trans e = e := by { ext x, refl }
@[simp] lemma trans_refl (e : α ≃o β) : e.trans (refl β) = e := by { ext x, refl }
end has_le
open set
section le
variables [has_le α] [has_le β] [has_le γ]
@[simp] lemma le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff
lemma le_symm_apply (e : α ≃o β) {x : α} {y : β} : x ≤ e.symm y ↔ e x ≤ y :=
e.rel_symm_apply
lemma symm_apply_le (e : α ≃o β) {x : α} {y : β} : e.symm y ≤ x ↔ y ≤ e x :=
e.symm_apply_rel
end le
variables [preorder α] [preorder β] [preorder γ]
protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone
protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono
@[simp] lemma lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y :=
e.to_order_embedding.lt_iff_lt
/-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders,
it suffices to prove `cmp a (g b) = cmp (f a) b`. --/
def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α)
(h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β :=
have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] },
{ to_fun := f,
inv_fun := g,
left_inv := λ a, (gf a).symm,
right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] },
map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert (h _ _).symm, apply gf } }
/-- Order isomorphism between two equal sets. -/
def set_congr (s t : set α) (h : s = t) : s ≃o t :=
{ to_equiv := equiv.set_congr h,
map_rel_iff' := λ x y, iff.rfl }
/-- Order isomorphism between `univ : set α` and `α`. -/
def set.univ : (set.univ : set α) ≃o α :=
{ to_equiv := equiv.set.univ α,
map_rel_iff' := λ x y, iff.rfl }
/-- Order isomorphism between `α → β` and `β`, where `α` has a unique element. -/
@[simps to_equiv apply] def fun_unique (α β : Type*) [unique α] [preorder β] :
(α → β) ≃o β :=
{ to_equiv := equiv.fun_unique α β,
map_rel_iff' := λ f g, by simp [pi.le_def, unique.forall_iff] }
@[simp] lemma fun_unique_symm_apply {α β : Type*} [unique α] [preorder β] :
((fun_unique α β).symm : β → α → β) = function.const α := rfl
end order_iso
namespace equiv
variables [preorder α] [preorder β]
/-- If `e` is an equivalence with monotone forward and inverse maps, then `e` is an
order isomorphism. -/
def to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) :
α ≃o β :=
⟨e, λ x y, ⟨λ h, by simpa only [e.symm_apply_apply] using h₂ h, λ h, h₁ h⟩⟩
@[simp] lemma coe_to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) :
⇑(e.to_order_iso h₁ h₂) = e := rfl
@[simp] lemma to_order_iso_to_equiv (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) :
(e.to_order_iso h₁ h₂).to_equiv = e := rfl
end equiv
/-- If a function `f` is strictly monotone on a set `s`, then it defines an order isomorphism
between `s` and its image. -/
protected noncomputable def strict_mono_on.order_iso {α β} [linear_order α] [preorder β]
(f : α → β) (s : set α) (hf : strict_mono_on f s) :
s ≃o f '' s :=
{ to_equiv := hf.inj_on.bij_on_image.equiv _,
map_rel_iff' := λ x y, hf.le_iff_le x.2 y.2 }
/-- A strictly monotone function from a linear order is an order isomorphism between its domain and
its range. -/
protected noncomputable def strict_mono.order_iso {α β} [linear_order α] [preorder β] (f : α → β)
(h_mono : strict_mono f) : α ≃o set.range f :=
{ to_equiv := equiv.of_injective f h_mono.injective,
map_rel_iff' := λ a b, h_mono.le_iff_le }
/-- A strictly monotone surjective function from a linear order is an order isomorphism. -/
noncomputable def strict_mono.order_iso_of_surjective {α β} [linear_order α] [preorder β]
(f : α → β) (h_mono : strict_mono f) (h_surj : surjective f) : α ≃o β :=
(h_mono.order_iso f).trans $ (order_iso.set_congr _ _ h_surj.range_eq).trans order_iso.set.univ
/-- `subrel r p` is the inherited relation on a subset. -/
def subrel (r : α → α → Prop) (p : set α) : p → p → Prop :=
(coe : p → α) ⁻¹'o r
@[simp] theorem subrel_val (r : α → α → Prop) (p : set α)
{a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl
namespace subrel
/-- The relation embedding from the inherited relation on a subset. -/
protected def rel_embedding (r : α → α → Prop) (p : set α) :
subrel r p ↪r r := ⟨embedding.subtype _, λ a b, iff.rfl⟩
@[simp] theorem rel_embedding_apply (r : α → α → Prop) (p a) :
subrel.rel_embedding r p a = a.1 := rfl
instance (r : α → α → Prop) [is_well_order α r]
(p : set α) : is_well_order p (subrel r p) :=
rel_embedding.is_well_order (subrel.rel_embedding r p)
end subrel
/-- Restrict the codomain of a relation embedding. -/
def rel_embedding.cod_restrict (p : set β) (f : r ↪r s) (H : ∀ a, f a ∈ p) : r ↪r subrel s p :=
⟨f.to_embedding.cod_restrict p H, f.map_rel_iff'⟩
@[simp] theorem rel_embedding.cod_restrict_apply (p) (f : r ↪r s) (H a) :
rel_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
/-- An order isomorphism is also an order isomorphism between dual orders. -/
protected def order_iso.dual [preorder α] [preorder β] (f : α ≃o β) :
order_dual α ≃o order_dual β := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩
section lattice_isos
lemma order_iso.map_bot' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β}
(hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y :=
by { refine le_antisymm _ (hy _), rw [← f.apply_symm_apply y, f.map_rel_iff], apply hx }
lemma order_iso.map_bot [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ :=
f.map_bot' (λ _, bot_le) (λ _, bot_le)
lemma order_iso.map_top' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β}
(hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y :=
f.dual.map_bot' hx hy
lemma order_iso.map_top [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ :=
f.dual.map_bot
lemma order_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β]
(f : α ↪o β) (x y : α) :
f (x ⊓ y) ≤ f x ⊓ f y :=
f.monotone.map_inf_le x y
lemma order_iso.map_inf [semilattice_inf α] [semilattice_inf β]
(f : α ≃o β) (x y : α) :
f (x ⊓ y) = f x ⊓ f y :=
begin
refine (f.to_order_embedding.map_inf_le x y).antisymm _,
simpa [← f.symm.le_iff_le] using f.symm.to_order_embedding.map_inf_le (f x) (f y)
end
lemma order_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β]
(f : α ↪o β) (x y : α) :
f x ⊔ f y ≤ f (x ⊔ y) :=
f.monotone.le_map_sup x y
lemma order_iso.map_sup [semilattice_sup α] [semilattice_sup β]
(f : α ≃o β) (x y : α) :
f (x ⊔ y) = f x ⊔ f y :=
f.dual.map_inf x y
section bounded_lattice
variables [bounded_lattice α] [bounded_lattice β] (f : α ≃o β)
include f
lemma order_iso.is_compl {x y : α} (h : is_compl x y) : is_compl (f x) (f y) :=
⟨by { rw [← f.map_bot, ← f.map_inf, f.map_rel_iff], exact h.1 },
by { rw [← f.map_top, ← f.map_sup, f.map_rel_iff], exact h.2 }⟩
theorem order_iso.is_compl_iff {x y : α} :
is_compl x y ↔ is_compl (f x) (f y) :=
⟨f.is_compl, λ h, begin
rw [← f.symm_apply_apply x, ← f.symm_apply_apply y],
exact f.symm.is_compl h,
end⟩
lemma order_iso.is_complemented
[is_complemented α] : is_complemented β :=
⟨λ x, begin
obtain ⟨y, hy⟩ := exists_is_compl (f.symm x),
rw ← f.symm_apply_apply y at hy,
refine ⟨f y, f.symm.is_compl_iff.2 hy⟩,
end⟩
theorem order_iso.is_complemented_iff :
is_complemented α ↔ is_complemented β :=
⟨by { introI, exact f.is_complemented }, by { introI, exact f.symm.is_complemented }⟩
end bounded_lattice
end lattice_isos
|
aed259bcb5b671c20d40239d153f44226356898c | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/field_theory/mv_polynomial.lean | d76a8918d879d384ef0a11319363aca228e19b03 | [
"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 | 9,329 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
Multivariate functions of the form `α^n → α` are isomorphic to multivariate polynomials in
`n` variables.
-/
import linear_algebra.finsupp_vector_space field_theory.finite data.mv_polynomial
noncomputable theory
open_locale classical
open set linear_map submodule
namespace mv_polynomial
universes u v
variables {σ : Type u} {α : Type v}
instance [field α] : vector_space α (mv_polynomial σ α) :=
finsupp.vector_space _ _
section
variables (σ α) [field α] (m : ℕ)
def restrict_total_degree : submodule α (mv_polynomial σ α) :=
finsupp.supported _ _ {n | n.sum (λn e, e) ≤ m }
lemma mem_restrict_total_degree (p : mv_polynomial σ α) :
p ∈ restrict_total_degree σ α m ↔ p.total_degree ≤ m :=
begin
rw [total_degree, finset.sup_le_iff],
refl
end
end
section
variables (σ α)
def restrict_degree (m : ℕ) [field α] : submodule α (mv_polynomial σ α) :=
finsupp.supported _ _ {n | ∀i, n i ≤ m }
end
lemma mem_restrict_degree [field α] (p : mv_polynomial σ α) (n : ℕ) :
p ∈ restrict_degree σ α n ↔ (∀s ∈ p.support, ∀i, (s : σ →₀ ℕ) i ≤ n) :=
begin
rw [restrict_degree, finsupp.mem_supported],
refl
end
lemma mem_restrict_degree_iff_sup [field α] (p : mv_polynomial σ α) (n : ℕ) :
p ∈ restrict_degree σ α n ↔ ∀i, p.degrees.count i ≤ n :=
begin
simp only [mem_restrict_degree, degrees, multiset.count_sup, finsupp.count_to_multiset,
finset.sup_le_iff],
exact ⟨assume h n s hs, h s hs n, assume h s hs n, h n s hs⟩
end
lemma map_range_eq_map {β : Type*}
[comm_ring α] [comm_ring β] (p : mv_polynomial σ α)
(f : α → β) [is_semiring_hom f]:
finsupp.map_range f (is_semiring_hom.map_zero f) p = p.map f :=
begin
rw [← finsupp.sum_single p, finsupp.sum, finsupp.map_range_finset_sum,
← p.support.sum_hom (map f)],
{ refine finset.sum_congr rfl (assume n _, _),
rw [finsupp.map_range_single, ← monomial, ← monomial, map_monomial] },
apply_instance
end
section
variables (σ α)
lemma is_basis_monomials [field α] :
is_basis α ((λs, (monomial s 1 : mv_polynomial σ α))) :=
suffices is_basis α (λ (sa : Σ _, unit), (monomial sa.1 1 : mv_polynomial σ α)),
begin
apply is_basis.comp this (λ (s : σ →₀ ℕ), ⟨s, punit.star⟩),
split,
{ intros x y hxy,
simpa using hxy },
{ intros x,
rcases x with ⟨x₁, x₂⟩,
use x₁,
rw punit_eq punit.star x₂ }
end,
begin
apply finsupp.is_basis_single (λ _ _, (1 : α)),
intro _,
apply is_basis_singleton_one,
end
end
end mv_polynomial
namespace mv_polynomial
universe u
variables (σ : Type u) (α : Type u) [field α]
open_locale classical
lemma dim_mv_polynomial : vector_space.dim α (mv_polynomial σ α) = cardinal.mk (σ →₀ ℕ) :=
by rw [← cardinal.lift_inj, ← (is_basis_monomials σ α).mk_eq_dim]
end mv_polynomial
namespace mv_polynomial
variables {α : Type*} {σ : Type*}
variables [field α] [fintype α] [fintype σ]
def indicator (a : σ → α) : mv_polynomial σ α :=
finset.univ.prod (λn, 1 - (X n - C (a n))^(fintype.card α - 1))
lemma eval_indicator_apply_eq_one (a : σ → α) :
eval a (indicator a) = 1 :=
have 0 < fintype.card α - 1,
begin
rw [← finite_field.card_units, fintype.card_pos_iff],
exact ⟨1⟩
end,
by simp only [indicator, (finset.univ.prod_hom (eval a)).symm, eval_sub,
is_ring_hom.map_one (eval a), is_semiring_hom.map_pow (eval a), eval_X, eval_C,
sub_self, zero_pow this, sub_zero, finset.prod_const_one]
lemma eval_indicator_apply_eq_zero (a b : σ → α) (h : a ≠ b) :
eval a (indicator b) = 0 :=
have ∃i, a i ≠ b i, by rwa [(≠), function.funext_iff, not_forall] at h,
begin
rcases this with ⟨i, hi⟩,
simp only [indicator, (finset.univ.prod_hom (eval a)).symm, eval_sub,
is_ring_hom.map_one (eval a), is_semiring_hom.map_pow (eval a), eval_X, eval_C,
sub_self, finset.prod_eq_zero_iff],
refine ⟨i, finset.mem_univ _, _⟩,
rw [finite_field.pow_card_sub_one_eq_one, sub_self],
rwa [(≠), sub_eq_zero],
end
lemma degrees_indicator (c : σ → α) :
degrees (indicator c) ≤ finset.univ.sum (λs:σ, add_monoid.smul (fintype.card α - 1) {s}) :=
begin
rw [indicator],
refine le_trans (degrees_prod _ _) (finset.sum_le_sum $ assume s hs, _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_one, ← bot_eq_zero, bot_sup_eq],
refine le_trans (degrees_pow _ _) (add_monoid.smul_le_smul_of_le_right _ _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_C, ← bot_eq_zero, sup_bot_eq],
exact degrees_X _
end
set_option class.instance_max_depth 50
lemma indicator_mem_restrict_degree (c : σ → α) :
indicator c ∈ restrict_degree σ α (fintype.card α - 1) :=
begin
rw [mem_restrict_degree_iff_sup, indicator],
assume n,
refine le_trans (multiset.count_le_of_le _ $ degrees_indicator _) (le_of_eq _),
rw [← finset.univ.sum_hom (multiset.count n)],
simp only [is_add_monoid_hom.map_smul (multiset.count n), multiset.singleton_eq_singleton,
add_monoid.smul_eq_mul, nat.cast_id],
transitivity,
refine finset.sum_eq_single n _ _,
{ assume b hb ne, rw [multiset.count_cons_of_ne ne.symm, multiset.count_zero, mul_zero] },
{ assume h, exact (h $ finset.mem_univ _).elim },
{ rw [multiset.count_cons_self, multiset.count_zero, mul_one] }
end
section
variables (α σ)
def evalₗ : mv_polynomial σ α →ₗ[α] (σ → α) → α :=
⟨ λp e, p.eval e,
assume p q, funext $ assume e, eval_add,
assume a p, funext $ assume e, by rw [smul_eq_C_mul, eval_mul, eval_C]; refl ⟩
end
section
lemma evalₗ_apply (p : mv_polynomial σ α) (e : σ → α) : evalₗ α σ p e = p.eval e :=
rfl
end
lemma map_restrict_dom_evalₗ : (restrict_degree σ α (fintype.card α - 1)).map (evalₗ α σ) = ⊤ :=
begin
refine top_unique (submodule.le_def'.2 $ assume e _, mem_map.2 _),
refine ⟨finset.univ.sum (λn:σ → α, e n • indicator n), _, _⟩,
{ exact sum_mem _ (assume c _, smul_mem _ _ (indicator_mem_restrict_degree _)) },
{ ext n,
simp only [linear_map.map_sum, @pi.finset_sum_apply (σ → α) (λ_, α) _ _ _ _ _,
pi.smul_apply, linear_map.map_smul],
simp only [evalₗ_apply],
transitivity,
refine finset.sum_eq_single n _ _,
{ assume b _ h,
rw [eval_indicator_apply_eq_zero _ _ h.symm, smul_zero] },
{ assume h, exact (h $ finset.mem_univ n).elim },
{ rw [eval_indicator_apply_eq_one, smul_eq_mul, mul_one] } }
end
end mv_polynomial
namespace mv_polynomial
universe u
variables (σ : Type u) (α : Type u) [fintype σ] [field α] [fintype α]
@[derive [add_comm_group, vector_space α, inhabited]]
def R : Type u := restrict_degree σ α (fintype.card α - 1)
noncomputable instance decidable_restrict_degree (m : ℕ) :
decidable_pred (λn, n ∈ {n : σ →₀ ℕ | ∀i, n i ≤ m }) :=
by simp only [set.mem_set_of_eq]; apply_instance
set_option class.instance_max_depth 60
lemma dim_R : vector_space.dim α (R σ α) = fintype.card (σ → α) :=
calc vector_space.dim α (R σ α) =
vector_space.dim α (↥{s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card α - 1} →₀ α) :
linear_equiv.dim_eq
(finsupp.supported_equiv_finsupp {s : σ →₀ ℕ | ∀n:σ, s n ≤ fintype.card α - 1 })
... = cardinal.mk {s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card α - 1} :
by rw [finsupp.dim_eq, dim_of_field, mul_one]
... = cardinal.mk {s : σ → ℕ | ∀ (n : σ), s n < fintype.card α } :
begin
refine quotient.sound ⟨equiv.subtype_congr finsupp.equiv_fun_on_fintype $ assume f, _⟩,
refine forall_congr (assume n, nat.le_sub_right_iff_add_le _),
exact fintype.card_pos_iff.2 ⟨0⟩
end
... = cardinal.mk (σ → {n // n < fintype.card α}) :
quotient.sound ⟨@equiv.subtype_pi_equiv_pi σ (λ_, ℕ) (λs n, n < fintype.card α)⟩
... = cardinal.mk (σ → fin (fintype.card α)) :
quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) (equiv.fin_equiv_subtype _).symm⟩
... = cardinal.mk (σ → α) :
begin
refine (trunc.induction_on (fintype.equiv_fin α) $ assume (e : α ≃ fin (fintype.card α)), _),
refine quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) e.symm⟩
end
... = fintype.card (σ → α) : cardinal.fintype_card _
def evalᵢ : R σ α →ₗ[α] (σ → α) → α :=
((evalₗ α σ).comp (restrict_degree σ α (fintype.card α - 1)).subtype)
lemma range_evalᵢ : (evalᵢ σ α).range = ⊤ :=
begin
rw [evalᵢ, linear_map.range_comp, range_subtype],
exact map_restrict_dom_evalₗ
end
lemma ker_evalₗ : (evalᵢ σ α).ker = ⊥ :=
begin
refine injective_of_surjective _ _ _ (range_evalᵢ _ _),
{ rw [dim_R], exact cardinal.nat_lt_omega _ },
{ rw [dim_R, dim_fun, dim_of_field, mul_one] }
end
lemma eq_zero_of_eval_eq_zero (p : mv_polynomial σ α)
(h : ∀v:σ → α, p.eval v = 0) (hp : p ∈ restrict_degree σ α (fintype.card α - 1)) :
p = 0 :=
let p' : R σ α := ⟨p, hp⟩ in
have p' ∈ (evalᵢ σ α).ker := by rw [mem_ker]; ext v; exact h v,
show p'.1 = (0 : R σ α).1,
begin
rw [ker_evalₗ, mem_bot] at this,
rw [this]
end
end mv_polynomial
|
404aa0120c297b4476882056184e682fa33e51ce | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /06_Inductive_Types.org.15.lean | 828c61ac01eca594ed1de48321536279e665d60d | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 272 | lean | /- page 82 -/
import standard
import data.bool data.prod data.nat data.prod data.sum
open prod sum nat bool
-- BEGIN
definition sum_example (s : ℕ + ℕ) : ℕ :=
sum.cases_on s (λ n, 2 * n) (λ n, 2 * n + 1)
eval sum_example (inl 3)
eval sum_example (inr 3)
-- END
|
d05bccfdc024eb6880a34f6fc2e43b783cf7a486 | 618003631150032a5676f229d13a079ac875ff77 | /src/data/stream/basic.lean | 50f7a32b31833054cc9a46a4079e3a61360a0ae0 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 389 | lean | /-
Copyright (c) 2020 Gabriel Ebner, Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Simon Hudon
-/
import tactic.ext
import data.stream
/-!
# Additional instances and attributes for streams
-/
attribute [ext] stream.ext
instance {α} [inhabited α] : inhabited (stream α) :=
⟨stream.const (default _)⟩
|
51a1c13868c9541534f09df8c211a42d7125577b | efa51dd2edbbbbd6c34bd0ce436415eb405832e7 | /20170116_POPL/super/assoc.lean | c2bb3ee152734eb6d5edb82ba85ceda11da6b01a | [
"Apache-2.0"
] | permissive | leanprover/presentations | dd031a05bcb12c8855676c77e52ed84246bd889a | 3ce2d132d299409f1de269fa8e95afa1333d644e | refs/heads/master | 1,688,703,388,796 | 1,686,838,383,000 | 1,687,465,742,000 | 29,750,158 | 12 | 9 | Apache-2.0 | 1,540,211,670,000 | 1,422,042,683,000 | Lean | UTF-8 | Lean | false | false | 908 | lean | import tools.super
-- super can prove statements about associative-commutative function symbols,
-- if we tell it to use the right lemmas:
example (x y z w : ℕ) : x + y + z + w = (w + z) + (y + x) :=
by super nat.add_assoc nat.add_comm
-- (Internally, this works in a very similar way to the sorting by rewriting
-- described in `../assoc/ac_by_simp.lean`.)
-- We can also prove existential statements using super:
example (x y z w : ℕ) : ∃a, x + y + z + w = a + (y + x) :=
by super with nat.add_assoc nat.add_comm
-- This support also works with other types than ℕ.
-- Let us show the well-known fact that if a monoid has right inverses, they are
-- also left inverses (i.e., it is a group):
example {α} [monoid α] [has_inv α]
(right_inverse : ∀x : α, x * x⁻¹ = 1) :
∀x : α, x⁻¹ * x = 1 :=
by super with monoid.mul_assoc monoid.mul_one
|
cd47cbb251be5e7e4aa2711a4ce3448e994c4d47 | 54f4ad05b219d444b709f56c2f619dd87d14ec29 | /my_project/src/dependent_example.lean | 4e8701fe17287e4831e016a05a3e67880ae8568a | [] | no_license | yizhou7/learning-lean | 8efcf838c7276e235a81bd291f467fa43ce56e0a | 91fb366c624df6e56e19555b2e482ce767cd8224 | refs/heads/master | 1,675,649,087,737 | 1,609,022,281,000 | 1,609,022,281,000 | 272,072,779 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 987 | lean | universe u
inductive vector (α : Type u) : ℕ → Type u
| nil {} : vector 0
| cons : Π {n}, α → vector n → vector (n+1)
namespace vector
def head {α : Type} : Π {n}, vector α (n+1) → α
| n (cons x _) := x
def tail {α : Type} : Π {n}, vector α (n+1) → vector α n
| n (cons _ xs) := xs
def map {α β : Type} (f : α → β) : Π {n}, vector α (n) → vector β (n)
| 0 nil := nil
| n (cons x xs) := cons (f x) (map xs)
def concat {α : Type} : Π {n}, vector α n → Π {m}, vector α m → vector α (n + m)
| 0 nil m ys := begin
rw (nat.zero_add m),
exact ys,
end
| (nat.succ n) (cons x xs) m ys :=
begin
rw (nat.succ_add n m),
exact (cons x (concat xs ys)),
end
def filter {α : Type} (f : α → Prop) [d : decidable_pred f] : Π {n}, vector α n → Σ {m}, vector α m
| 0 nil := sigma.mk 0 nil
| (nat.succ n) (cons x xs) :=
begin
exact let p := filter xs in
if (f x) then (sigma.mk (p.1 + 1) (cons x p.2))
else p,
end
end vector |
805841184908a2fc2bed898ada14a45d185ae15f | 26bff4ed296b8373c92b6b025f5d60cdf02104b9 | /library/algebra/ring.lean | 4c51bffafe7ad834cc3d3ee70acb2858f9dd5848 | [
"Apache-2.0"
] | permissive | guiquanz/lean | b8a878ea24f237b84b0e6f6be2f300e8bf028229 | 242f8ba0486860e53e257c443e965a82ee342db3 | refs/heads/master | 1,526,680,092,098 | 1,427,492,833,000 | 1,427,493,281,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,469 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.ring
Authors: Jeremy Avigad, Leonardo de Moura
Structures with multiplicative and additive components, including semirings, rings, and fields.
The development is modeled after Isabelle's library.
-/
import logic.eq logic.connectives data.unit data.sigma data.prod
import algebra.function algebra.binary algebra.group
open eq eq.ops
namespace algebra
variable {A : Type}
/- auxiliary classes -/
structure distrib [class] (A : Type) extends has_mul A, has_add A :=
(left_distrib : ∀a b c, mul a (add b c) = add (mul a b) (mul a c))
(right_distrib : ∀a b c, mul (add a b) c = add (mul a c) (mul b c))
theorem left_distrib [s : distrib A] (a b c : A) : a * (b + c) = a * b + a * c :=
!distrib.left_distrib
theorem right_distrib [s: distrib A] (a b c : A) : (a + b) * c = a * c + b * c :=
!distrib.right_distrib
structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A :=
(zero_mul : ∀a, mul zero a = zero)
(mul_zero : ∀a, mul a zero = zero)
theorem zero_mul [s : mul_zero_class A] (a : A) : 0 * a = 0 := !mul_zero_class.zero_mul
theorem mul_zero [s : mul_zero_class A] (a : A) : a * 0 = 0 := !mul_zero_class.mul_zero
structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A :=
(zero_ne_one : zero ≠ one)
theorem zero_ne_one [s: zero_ne_one_class A] : 0 ≠ 1 := @zero_ne_one_class.zero_ne_one A s
/- semiring -/
structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A,
mul_zero_class A
section semiring
variables [s : semiring A] (a b c : A)
include s
theorem ne_zero_of_mul_ne_zero_right {a b : A} (H : a * b ≠ 0) : a ≠ 0 :=
assume H1 : a = 0,
have H2 : a * b = 0, from H1⁻¹ ▸ zero_mul b,
H H2
theorem ne_zero_of_mul_ne_zero_left {a b : A} (H : a * b ≠ 0) : b ≠ 0 :=
assume H1 : b = 0,
have H2 : a * b = 0, from H1⁻¹ ▸ mul_zero a,
H H2
end semiring
/- comm semiring -/
structure comm_semiring [class] (A : Type) extends semiring A, comm_semigroup A
-- TODO: we could also define a cancelative comm_semiring, i.e. satisfying
-- c ≠ 0 → c * a = c * b → a = b.
section comm_semiring
variables [s : comm_semiring A] (a b c : A)
include s
definition dvd (a b : A) : Prop := ∃c, b = a * c
notation ( a | b ) := dvd a b
theorem dvd.intro {a b c : A} (H : a * c = b) : (a | b) :=
exists.intro _ H⁻¹
theorem dvd.intro_left {a b c : A} (H : c * a = b) : (a | b) :=
dvd.intro (!mul.comm ▸ H)
theorem exists_eq_mul_right_of_dvd {a b : A} (H : (a | b)) : ∃c, b = a * c := H
theorem dvd.elim {P : Prop} {a b : A} (H₁ : (a | b)) (H₂ : ∀c, b = a * c → P) : P :=
exists.elim H₁ H₂
theorem exists_eq_mul_left_of_dvd {a b : A} (H : (a | b)) : ∃c, b = c * a :=
dvd.elim H (take c, assume H1 : b = a * c, exists.intro c (H1 ⬝ !mul.comm))
theorem dvd.elim_left {P : Prop} {a b : A} (H₁ : (a | b)) (H₂ : ∀c, b = c * a → P) : P :=
exists.elim (exists_eq_mul_left_of_dvd H₁) (take c, assume H₃ : b = c * a, H₂ c H₃)
theorem dvd.refl : (a | a) := dvd.intro !mul_one
theorem dvd.trans {a b c : A} (H₁ : (a | b)) (H₂ : (b | c)) : (a | c) :=
dvd.elim H₁
(take d, assume H₃ : b = a * d,
dvd.elim H₂
(take e, assume H₄ : c = b * e,
dvd.intro
(show a * (d * e) = c, by rewrite ⟨-mul.assoc, -H₃, H₄⟩)))
theorem eq_zero_of_zero_dvd {a : A} (H : (0 | a)) : a = 0 :=
dvd.elim H (take c, assume H' : a = 0 * c, H' ⬝ !zero_mul)
theorem dvd_zero : (a | 0) := dvd.intro !mul_zero
theorem one_dvd : (1 | a) := dvd.intro !one_mul
theorem dvd_mul_right : (a | a * b) := dvd.intro rfl
theorem dvd_mul_left : (a | b * a) := mul.comm a b ▸ dvd_mul_right a b
theorem dvd_mul_of_dvd_left {a b : A} (H : (a | b)) (c : A) : (a | b * c) :=
dvd.elim H
(take d,
assume H₁ : b = a * d,
dvd.intro
(show a * (d * c) = b * c, from by rewrite ⟨-mul.assoc, H₁⟩))
theorem dvd_mul_of_dvd_right {a b : A} (H : (a | b)) (c : A) : (a | c * b) :=
!mul.comm ▸ (dvd_mul_of_dvd_left H _)
theorem mul_dvd_mul {a b c d : A} (dvd_ab : (a | b)) (dvd_cd : (c | d)) : (a * c | b * d) :=
dvd.elim dvd_ab
(take e, assume Haeb : b = a * e,
dvd.elim dvd_cd
(take f, assume Hcfd : d = c * f,
dvd.intro
(show a * c * (e * f) = b * d,
by rewrite [mul.assoc, {c*_}mul.left_comm, -mul.assoc, Haeb, Hcfd])))
theorem dvd_of_mul_right_dvd {a b c : A} (H : (a * b | c)) : (a | c) :=
dvd.elim H (take d, assume Habdc : c = a * b * d, dvd.intro (!mul.assoc⁻¹ ⬝ Habdc⁻¹))
theorem dvd_of_mul_left_dvd {a b c : A} (H : (a * b | c)) : (b | c) :=
dvd_of_mul_right_dvd (mul.comm a b ▸ H)
theorem dvd_add {a b c : A} (Hab : (a | b)) (Hac : (a | c)) : (a | b + c) :=
dvd.elim Hab
(take d, assume Hadb : b = a * d,
dvd.elim Hac
(take e, assume Haec : c = a * e,
dvd.intro (show a * (d + e) = b + c,
by rewrite [left_distrib, -Hadb, -Haec])))
end comm_semiring
/- ring -/
structure ring [class] (A : Type) extends add_comm_group A, monoid A, distrib A
theorem ring.mul_zero [s : ring A] (a : A) : a * 0 = 0 :=
have H : a * 0 + 0 = a * 0 + a * 0, from calc
a * 0 + 0 = a * 0 : by rewrite add_zero
... = a * (0 + 0) : by rewrite add_zero
... = a * 0 + a * 0 : by rewrite {a*_}ring.left_distrib,
show a * 0 = 0, from (add.left_cancel H)⁻¹
theorem ring.zero_mul [s : ring A] (a : A) : 0 * a = 0 :=
have H : 0 * a + 0 = 0 * a + 0 * a, from calc
0 * a + 0 = 0 * a : by rewrite add_zero
... = (0 + 0) * a : by rewrite add_zero
... = 0 * a + 0 * a : by rewrite {_*a}ring.right_distrib,
show 0 * a = 0, from (add.left_cancel H)⁻¹
definition ring.to_semiring [instance] [coercion] [reducible] [s : ring A] : semiring A :=
⦃ semiring, s,
mul_zero := ring.mul_zero,
zero_mul := ring.zero_mul ⦄
section
variables [s : ring A] (a b c d e : A)
include s
theorem neg_mul_eq_neg_mul : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin
rewrite [-right_distrib, add.right_inv, zero_mul]
end
theorem neg_mul_eq_mul_neg : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin
rewrite [-left_distrib, add.right_inv, mul_zero]
end
theorem neg_mul_neg : -a * -b = a * b :=
calc
-a * -b = -(a * -b) : by rewrite -neg_mul_eq_neg_mul
... = - -(a * b) : by rewrite -neg_mul_eq_mul_neg
... = a * b : by rewrite neg_neg
theorem neg_mul_comm : -a * b = a * -b := !neg_mul_eq_neg_mul⁻¹ ⬝ !neg_mul_eq_mul_neg
theorem neg_eq_neg_one_mul : -a = -1 * a :=
calc
-a = -(1 * a) : by rewrite one_mul
... = -1 * a : by rewrite neg_mul_eq_neg_mul
theorem mul_sub_left_distrib : a * (b - c) = a * b - a * c :=
calc
a * (b - c) = a * b + a * -c : left_distrib
... = a * b + - (a * c) : by rewrite -neg_mul_eq_mul_neg
... = a * b - a * c : rfl
theorem mul_sub_right_distrib : (a - b) * c = a * c - b * c :=
calc
(a - b) * c = a * c + -b * c : right_distrib
... = a * c + - (b * c) : by rewrite neg_mul_eq_neg_mul
... = a * c - b * c : rfl
-- TODO: can calc mode be improved to make this easier?
-- TODO: there is also the other direction. It will be easier when we
-- have the simplifier.
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d :=
calc
a * e + c = b * e + d ↔ a * e + c = d + b * e : by rewrite {b*e+_}add.comm
... ↔ a * e + c - b * e = d : iff.symm !sub_eq_iff_eq_add
... ↔ a * e - b * e + c = d : by rewrite sub_add_eq_add_sub
... ↔ (a - b) * e + c = d : by rewrite mul_sub_right_distrib
theorem mul_neg_one_eq_neg : a * (-1) = -a :=
have H : a + a * -1 = 0, from calc
a + a * -1 = a * 1 + a * -1 : mul_one
... = a * (1 + -1) : left_distrib
... = a * 0 : add.right_inv
... = 0 : mul_zero,
symm (neg_eq_of_add_eq_zero H)
theorem mul_ne_zero_imp_ne_zero {a b} (H : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 :=
have Ha : a ≠ 0, from
(assume Ha1 : a = 0,
have H1 : a * b = 0, by rewrite [Ha1, zero_mul],
absurd H1 H),
have Hb : b ≠ 0, from
(assume Hb1 : b = 0,
have H1 : a * b = 0, by rewrite [Hb1, mul_zero],
absurd H1 H),
and.intro Ha Hb
end
structure comm_ring [class] (A : Type) extends ring A, comm_semigroup A
definition comm_ring.to_comm_semiring [instance] [coercion] [reducible] [s : comm_ring A] : comm_semiring A :=
⦃ comm_semiring, s,
mul_zero := mul_zero,
zero_mul := zero_mul ⦄
section
variables [s : comm_ring A] (a b c d e : A)
include s
-- TODO: wait for the simplifier
theorem mul_self_sub_mul_self_eq : a * a - b * b = (a + b) * (a - b) := sorry
theorem mul_self_sub_one_eq : a * a - 1 = (a + 1) * (a - 1) :=
mul_one 1 ▸ mul_self_sub_mul_self_eq a 1
theorem dvd_neg_iff_dvd : (a | -b) ↔ (a | b) :=
iff.intro
(assume H : (a | -b),
dvd.elim H
(take c, assume H' : -b = a * c,
dvd.intro
(show a * -c = b,
by rewrite [-neg_mul_eq_mul_neg, -H', neg_neg])))
(assume H : (a | b),
dvd.elim H
(take c, assume H' : b = a * c,
dvd.intro
(show a * -c = -b,
by rewrite [-neg_mul_eq_mul_neg, -H'])))
theorem neg_dvd_iff_dvd : (-a | b) ↔ (a | b) :=
iff.intro
(assume H : (-a | b),
dvd.elim H
(take c, assume H' : b = -a * c,
dvd.intro
(show a * -c = b, by rewrite [-neg_mul_comm, H'])))
(assume H : (a | b),
dvd.elim H
(take c, assume H' : b = a * c,
dvd.intro
(show -a * -c = b, by rewrite [neg_mul_neg, H'])))
theorem dvd_sub (H₁ : (a | b)) (H₂ : (a | c)) : (a | b - c) :=
dvd_add H₁ (iff.elim_right !dvd_neg_iff_dvd H₂)
end
/- integral domains -/
structure no_zero_divisors [class] (A : Type) extends has_mul A, has_zero A :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀a b, mul a b = zero → a = zero ∨ b = zero)
theorem eq_zero_or_eq_zero_of_mul_eq_zero {A : Type} [s : no_zero_divisors A] {a b : A}
(H : a * b = 0) :
a = 0 ∨ b = 0 := !no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero H
structure integral_domain [class] (A : Type) extends comm_ring A, no_zero_divisors A
section
variables [s : integral_domain A] (a b c d e : A)
include s
theorem mul_ne_zero {a b : A} (H1 : a ≠ 0) (H2 : b ≠ 0) : a * b ≠ 0 :=
assume H : a * b = 0,
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero H) (assume H3, H1 H3) (assume H4, H2 H4)
theorem mul.cancel_right {a b c : A} (Ha : a ≠ 0) (H : b * a = c * a) : b = c :=
have H1 : b * a - c * a = 0, from iff.mp !eq_iff_sub_eq_zero H,
have H2 : (b - c) * a = 0, using H1, by rewrite [mul_sub_right_distrib, H1],
have H3 : b - c = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha,
iff.elim_right !eq_iff_sub_eq_zero H3
theorem mul.cancel_left {a b c : A} (Ha : a ≠ 0) (H : a * b = a * c) : b = c :=
have H1 : a * b - a * c = 0, from iff.mp !eq_iff_sub_eq_zero H,
have H2 : a * (b - c) = 0, using H1, by rewrite [mul_sub_left_distrib, H1],
have H3 : b - c = 0, from or_resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha,
iff.elim_right !eq_iff_sub_eq_zero H3
-- TODO: do we want the iff versions?
-- TODO: wait for simplifier
theorem mul_self_eq_mul_self_iff (a b : A) : a * a = b * b ↔ a = b ∨ a = -b := sorry
theorem mul_self_eq_one_iff (a : A) : a * a = 1 ↔ a = 1 ∨ a = -1 := sorry
-- TODO: c - b * c → c = 0 ∨ b = 1 and variants
theorem dvd_of_mul_dvd_mul_left {a b c : A} (Ha : a ≠ 0) (Hdvd : (a * b | a * c)) : (b | c) :=
dvd.elim Hdvd
(take d,
assume H : a * c = a * b * d,
have H1 : b * d = c, from mul.cancel_left Ha (mul.assoc a b d ▸ H⁻¹),
dvd.intro H1)
theorem dvd_of_mul_dvd_mul_right {a b c : A} (Ha : a ≠ 0) (Hdvd : (b * a | c * a)) : (b | c) :=
dvd.elim Hdvd
(take d,
assume H : c * a = b * a * d,
have H1 : b * d * a = c * a, from by rewrite [mul.right_comm, -H],
have H2 : b * d = c, from mul.cancel_right Ha H1,
dvd.intro H2)
end
end algebra
|
030adc582cfebf77b4933593f1aad21b477bcf03 | 6b7c9c6393bac7cb1c64582a1c62597e24f5bb80 | /src/tactic/gptf/utils/derive_has_to_format.lean | 9058a4f3ba5e4f2a851f92ce5b00dfcee759f488 | [
"Apache-2.0"
] | permissive | alreadydone/lean-gptf | 56a7d9cbd9400af72fb143d60c8774b8cfbc09cb | b4ab1eb2da0178f3dcdc49771d9fed6b50e35d98 | refs/heads/master | 1,679,371,993,063 | 1,614,479,778,000 | 1,614,479,778,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,016 | lean | /-
Copyright (c) 2021 Jesse Michael Han. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Jesse Michael Han
derive has_to_format
-/
import tactic
meta def expr.join (tp : expr) : list expr → tactic expr := λ xs,
xs.foldr (λ e₁ acc, do {acc ← acc, tactic.to_expr ``(@list.cons %%tp %%e₁ %%acc)}) (tactic.to_expr ``(list.nil))
meta def format.join_using : format → list format → format := λ x xs,
format.join $ list.intercalate [x] (pure <$> xs)
meta def format.apply_constructor : format → list format → format := λ f fs,
match fs with
| [] := f
| fs := format.join_using " " [f, format.join ["(", format.join_using " " fs, ")"]]
end
open tactic
namespace tactic
namespace interactive
meta def mk_to_format (type : name) : tactic unit := do {
ls ← local_context,
(x::_) ← tactic.intro_lst [`arg],
et ← infer_type x,
xs ← tactic.induction x,
xs.mmap' $ λ ⟨c, args, _⟩, do
(args', rec_call) ← args.mpartition $ λ e, do {
e' ← tactic.to_expr ``(format),
bnot <$> e'.occurs <$> tactic.infer_type e
},
args'' ← args'.mmap (λ a, flip prod.mk a <$> (et.occurs <$> tactic.infer_type a)),
let fn : list (bool × expr) → state_t (list expr) tactic (list expr) := λ args'', do {
let pop : state_t (list expr) tactic (option expr) := do {
xs ← get,
match xs with
| (a::as) := modify (λ _, as) *> pure (some a)
| [] := pure none
end
},
args''.mmap (λ ⟨b, a⟩, if b then do (some x) ← pop, pure x else state_t.lift $ do
a_tp ← infer_type a,
_inst ← mk_app ``has_to_format [a_tp] >>= mk_instance,
tactic.to_expr ``(@has_to_format.to_format _ (%%_inst) %%a))
},
args''' ← prod.fst <$> (fn args'').run rec_call,
c ← tactic.resolve_constant c,
nm_tp ← to_expr ``(name),
nm_fmt ← mk_app ``has_to_format [nm_tp] >>= mk_instance,
fs ← expr.join `(format) args''',
result ← to_expr ``(format.apply_constructor (@has_to_format.to_format %%nm_tp %%nm_fmt %%c) %%fs),
tactic.exact result
}
meta def derive_has_to_format (pre : option name) : tactic unit := do {
vs ← local_context,
`(has_to_format %%f) ← target,
env ← get_env,
let n := f.get_app_fn.const_name,
d ← get_decl n,
refine ``( { to_format := _ } ),
tgt ← target,
extract_def (with_prefix pre n <.> "to_format") ff $ mk_to_format n
}
meta def has_to_format_derive_handler' (nspace : option name := none) : derive_handler :=
higher_order_derive_handler ``has_to_format (derive_has_to_format nspace) [] nspace
@[derive_handler]
meta def has_to_format_derive_handler : derive_handler :=
guard_class ``has_to_format has_to_format_derive_handler'
inductive my_nat : Type
| zero : my_nat
| succ : my_nat → my_nat
attribute [derive [has_to_format]] my_nat -- whee
end interactive
end tactic
section instances
attribute [derive has_to_format] interactive.loc -- :D
end instances
|
16ba178df98659c5bd8a053f02c0fc3fdf6a78c6 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/topology/sheaves/forget.lean | 1d6224e13158e781583085398cdad19c53885568 | [
"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 | 8,054 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.sheaves.sheaf
import category_theory.limits.preserves.shapes.products
import category_theory.limits.types
/-!
# Checking the sheaf condition on the underlying presheaf of types.
If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits
(we assume all limits exist in both `C` and `D`),
then checking the sheaf condition for a presheaf `F : presheaf C X`
is equivalent to checking the sheaf condition for `F ⋙ G`.
The important special case is when
`C` is a concrete category with a forgetful functor
that preserves limits and reflects isomorphisms.
Then to check the sheaf condition it suffices
to check it on the underlying sheaf of types.
## References
* https://stacks.math.columbia.edu/tag/0073
-/
noncomputable theory
open category_theory
open category_theory.limits
open topological_space
open opposite
namespace Top
namespace presheaf
namespace sheaf_condition
open sheaf_condition_equalizer_products
universes v u₁ u₂
variables {C : Type u₁} [category.{v} C] [has_limits C]
variables {D : Type u₂} [category.{v} D] [has_limits D]
variables (G : C ⥤ D) [preserves_limits G]
variables {X : Top.{v}} (F : presheaf C X)
variables {ι : Type v} (U : ι → opens X)
local attribute [reducible] diagram left_res right_res
/--
When `G` preserves limits, the sheaf condition diagram for `F` composed with `G` is
naturally isomorphic to the sheaf condition diagram for `F ⋙ G`.
-/
def diagram_comp_preserves_limits :
diagram F U ⋙ G ≅ diagram (F ⋙ G) U :=
begin
fapply nat_iso.of_components,
rintro ⟨j⟩,
exact (preserves_product.iso _ _),
exact (preserves_product.iso _ _),
rintros ⟨⟩ ⟨⟩ ⟨⟩,
{ ext, simp, dsimp, simp, }, -- non-terminal `simp`, but `squeeze_simp` fails
{ ext,
simp only [limit.lift_π, functor.comp_map, map_lift_pi_comparison, fan.mk_π_app,
preserves_product.iso_hom, parallel_pair_map_left, functor.map_comp,
category.assoc],
dsimp, simp, },
{ ext,
simp only [limit.lift_π, functor.comp_map, parallel_pair_map_right, fan.mk_π_app,
preserves_product.iso_hom, map_lift_pi_comparison, functor.map_comp,
category.assoc],
dsimp, simp, },
{ ext, simp, dsimp, simp, },
end
local attribute [reducible] res
/--
When `G` preserves limits, the image under `G` of the sheaf condition fork for `F`
is the sheaf condition fork for `F ⋙ G`,
postcomposed with the inverse of the natural isomorphism `diagram_comp_preserves_limits`.
-/
def map_cone_fork : G.map_cone (fork F U) ≅
(cones.postcompose (diagram_comp_preserves_limits G F U).inv).obj (fork (F ⋙ G) U) :=
cones.ext (iso.refl _) (λ j,
begin
dsimp, simp [diagram_comp_preserves_limits], cases j; dsimp,
{ rw iso.eq_comp_inv,
ext,
simp, dsimp, simp, },
{ rw iso.eq_comp_inv,
ext,
simp, -- non-terminal `simp`, but `squeeze_simp` fails
dsimp,
simp only [limit.lift_π, fan.mk_π_app, ←G.map_comp, limit.lift_π_assoc, fan.mk_π_app] }
end)
end sheaf_condition
universes v u₁ u₂
open sheaf_condition sheaf_condition_equalizer_products
variables {C : Type u₁} [category.{v} C] {D : Type u₂} [category.{v} D]
variables (G : C ⥤ D)
variables [reflects_isomorphisms G]
variables [has_limits C] [has_limits D] [preserves_limits G]
variables {X : Top.{v}} (F : presheaf C X)
/--
If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits
(we assume all limits exist in both `C` and `D`),
then checking the sheaf condition for a presheaf `F : presheaf C X`
is equivalent to checking the sheaf condition for `F ⋙ G`.
The important special case is when
`C` is a concrete category with a forgetful functor
that preserves limits and reflects isomorphisms.
Then to check the sheaf condition it suffices to check it on the underlying sheaf of types.
Another useful example is the forgetful functor `TopCommRing ⥤ Top`.
See https://stacks.math.columbia.edu/tag/0073.
In fact we prove a stronger version with arbitrary complete target category.
-/
lemma is_sheaf_iff_is_sheaf_comp :
presheaf.is_sheaf F ↔ presheaf.is_sheaf (F ⋙ G) :=
begin
split,
{ intros S ι U,
-- We have that the sheaf condition fork for `F` is a limit fork,
obtain ⟨t₁⟩ := S U,
-- and since `G` preserves limits, the image under `G` of this fork is a limit fork too.
have t₂ := @preserves_limit.preserves _ _ _ _ _ _ _ G _ _ t₁,
-- As we established above, that image is just the sheaf condition fork
-- for `F ⋙ G` postcomposed with some natural isomorphism,
have t₃ := is_limit.of_iso_limit t₂ (map_cone_fork G F U),
-- and as postcomposing by a natural isomorphism preserves limit cones,
have t₄ := is_limit.postcompose_inv_equiv _ _ t₃,
-- we have our desired conclusion.
exact ⟨t₄⟩, },
{ intros S ι U,
refine ⟨_⟩,
-- Let `f` be the universal morphism from `F.obj U` to the equalizer
-- of the sheaf condition fork, whatever it is.
-- Our goal is to show that this is an isomorphism.
let f := equalizer.lift _ (w F U),
-- If we can do that,
suffices : is_iso (G.map f),
{ resetI,
-- we have that `f` itself is an isomorphism, since `G` reflects isomorphisms
haveI : is_iso f := is_iso_of_reflects_iso f G,
-- TODO package this up as a result elsewhere:
apply is_limit.of_iso_limit (limit.is_limit _),
apply iso.symm,
fapply cones.ext,
exact (as_iso f),
rintro ⟨_|_⟩; { dsimp [f], simp, }, },
{ -- Returning to the task of shwoing that `G.map f` is an isomorphism,
-- we note that `G.map f` is almost but not quite (see below) a morphism
-- from the sheaf condition cone for `F ⋙ G` to the
-- image under `G` of the equalizer cone for the sheaf condition diagram.
let c := fork (F ⋙ G) U,
obtain ⟨hc⟩ := S U,
let d := G.map_cone (equalizer.fork (left_res F U) (right_res F U)),
have hd : is_limit d := preserves_limit.preserves (limit.is_limit _),
-- Since both of these are limit cones
-- (`c` by our hypothesis `S`, and `d` because `G` preserves limits),
-- we hope to be able to conclude that `f` is an isomorphism.
-- We say "not quite" above because `c` and `d` don't quite have the same shape:
-- we need to postcompose by the natural isomorphism `diagram_comp_preserves_limits`
-- introduced above.
let d' := (cones.postcompose (diagram_comp_preserves_limits G F U).hom).obj d,
have hd' : is_limit d' :=
(is_limit.postcompose_hom_equiv (diagram_comp_preserves_limits G F U) d).symm hd,
-- Now everything works: we verify that `f` really is a morphism between these cones:
let f' : c ⟶ d' :=
fork.mk_hom (G.map f)
begin
dsimp only [c, d, d', f, diagram_comp_preserves_limits, res],
dunfold fork.ι,
ext1 j,
dsimp,
simp only [category.assoc, ←functor.map_comp_assoc, equalizer.lift_ι,
map_lift_pi_comparison_assoc],
dsimp [res], simp,
end,
-- conclude that it is an isomorphism,
-- just because it's a morphism between two limit cones.
haveI : is_iso f' := is_limit.hom_is_iso hc hd' f',
-- A cone morphism is an isomorphism exactly if the morphism between the cone points is,
-- so we're done!
exact is_iso.of_iso ((cones.forget _).map_iso (as_iso f')) }, },
end
/-!
As an example, we now have everything we need to check the sheaf condition
for a presheaf of commutative rings, merely by checking the sheaf condition
for the underlying sheaf of types.
```
import algebra.category.CommRing.limits
example (X : Top) (F : presheaf CommRing X) (h : presheaf.is_sheaf (F ⋙ (forget CommRing))) :
F.is_sheaf :=
(is_sheaf_iff_is_sheaf_comp (forget CommRing) F).mpr h
```
-/
end presheaf
end Top
|
785fe25e16b99317462b705b9011c65df9be3cb3 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/algebra/group_with_zero/basic.lean | 814528e576f9ac9099c22e06c8275b14fc417f2a | [
"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 | 41,785 | 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 logic.nontrivial
import algebra.group.units_hom
import algebra.group.inj_surj
import algebra.group_with_zero.defs
/-!
# Groups 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
Various lemmas about `group_with_zero` and `comm_group_with_zero`.
To reduce import dependencies, the type-classes themselves are in
`algebra.group_with_zero.defs`.
## 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
open_locale classical
open function
variables {M₀ G₀ M₀' G₀' : Type*}
mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to
reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are
division-free."
attribute [field_simps] mul_div_assoc'
section
section mul_zero_class
variables [mul_zero_class M₀] {a b : M₀}
/-- Pullback a `mul_zero_class` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀' → M₀)
(hf : injective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) :
mul_zero_class M₀' :=
{ mul := (*),
zero := 0,
zero_mul := λ a, hf $ by simp only [mul, zero, zero_mul],
mul_zero := λ a, hf $ by simp only [mul, zero, mul_zero] }
/-- Pushforward a `mul_zero_class` instance along an surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀ → M₀')
(hf : surjective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) :
mul_zero_class M₀' :=
{ mul := (*),
zero := 0,
mul_zero := hf.forall.2 $ λ x, by simp only [← zero, ← mul, mul_zero],
zero_mul := hf.forall.2 $ λ x, by simp only [← zero, ← mul, zero_mul] }
lemma mul_eq_zero_of_left (h : a = 0) (b : M₀) : a * b = 0 := h.symm ▸ zero_mul b
lemma mul_eq_zero_of_right (a : M₀) (h : b = 0) : a * b = 0 := h.symm ▸ mul_zero a
lemma left_ne_zero_of_mul : a * b ≠ 0 → a ≠ 0 := mt (λ h, mul_eq_zero_of_left h b)
lemma right_ne_zero_of_mul : a * b ≠ 0 → b ≠ 0 := mt (mul_eq_zero_of_right a)
lemma ne_zero_and_ne_zero_of_mul (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 :=
⟨left_ne_zero_of_mul h, right_ne_zero_of_mul h⟩
lemma mul_eq_zero_of_ne_zero_imp_eq_zero {a b : M₀} (h : a ≠ 0 → b = 0) :
a * b = 0 :=
if ha : a = 0 then by rw [ha, zero_mul] else by rw [h ha, mul_zero]
end mul_zero_class
/-- Pushforward a `no_zero_divisors` instance along an injective function. -/
protected lemma function.injective.no_zero_divisors [has_mul M₀] [has_zero M₀]
[has_mul M₀'] [has_zero M₀'] [no_zero_divisors M₀']
(f : M₀ → M₀') (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) :
no_zero_divisors M₀ :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ x y H,
have f x * f y = 0, by rw [← mul, H, zero],
(eq_zero_or_eq_zero_of_mul_eq_zero this).imp (λ H, hf $ by rwa zero) (λ H, hf $ by rwa zero) }
lemma eq_zero_of_mul_self_eq_zero [has_mul M₀] [has_zero M₀] [no_zero_divisors M₀]
{a : M₀} (h : a * a = 0) :
a = 0 :=
(eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id
section
variables [mul_zero_class M₀] [no_zero_divisors M₀] {a b : M₀}
/-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them
equals zero. -/
@[simp] theorem mul_eq_zero : a * b = 0 ↔ a = 0 ∨ b = 0 :=
⟨eq_zero_or_eq_zero_of_mul_eq_zero,
λo, o.elim (λ h, mul_eq_zero_of_left h b) (mul_eq_zero_of_right a)⟩
/-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them
equals zero. -/
@[simp] theorem zero_eq_mul : 0 = a * b ↔ a = 0 ∨ b = 0 :=
by rw [eq_comm, mul_eq_zero]
/-- If `α` has no zero divisors, then the product of two elements is nonzero iff both of them
are nonzero. -/
theorem mul_ne_zero_iff : a * b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 :=
(not_congr mul_eq_zero).trans not_or_distrib
@[field_simps] theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 :=
mul_ne_zero_iff.2 ⟨ha, hb⟩
/-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` equals zero iff so is
`b * a`. -/
theorem mul_eq_zero_comm : a * b = 0 ↔ b * a = 0 :=
mul_eq_zero.trans $ (or_comm _ _).trans mul_eq_zero.symm
/-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` is nonzero iff so is
`b * a`. -/
theorem mul_ne_zero_comm : a * b ≠ 0 ↔ b * a ≠ 0 :=
not_congr mul_eq_zero_comm
lemma mul_self_eq_zero : a * a = 0 ↔ a = 0 := by simp
lemma zero_eq_mul_self : 0 = a * a ↔ a = 0 := by simp
end
end
section
variables [mul_zero_one_class M₀]
/-- Pullback a `mul_zero_one_class` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀']
(f : M₀' → M₀)
(hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) :
mul_zero_one_class M₀' :=
{ ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul }
/-- Pushforward a `mul_zero_one_class` instance along an surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀']
(f : M₀ → M₀')
(hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) :
mul_zero_one_class M₀' :=
{ ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul }
/-- In a monoid with zero, if zero equals one, then zero is the only element. -/
lemma eq_zero_of_zero_eq_one (h : (0 : M₀) = 1) (a : M₀) : a = 0 :=
by rw [← mul_one a, ← h, mul_zero]
/-- In a monoid with zero, if zero equals one, then zero is the unique element.
Somewhat arbitrarily, we define the default element to be `0`.
All other elements will be provably equal to it, but not necessarily definitionally equal. -/
def unique_of_zero_eq_one (h : (0 : M₀) = 1) : unique M₀ :=
{ default := 0, uniq := eq_zero_of_zero_eq_one h }
/-- In a monoid with zero, zero equals one if and only if all elements of that semiring
are equal. -/
theorem subsingleton_iff_zero_eq_one : (0 : M₀) = 1 ↔ subsingleton M₀ :=
⟨λ h, @unique.subsingleton _ (unique_of_zero_eq_one h), λ h, @subsingleton.elim _ h _ _⟩
alias subsingleton_iff_zero_eq_one ↔ subsingleton_of_zero_eq_one _
lemma eq_of_zero_eq_one (h : (0 : M₀) = 1) (a b : M₀) : a = b :=
@subsingleton.elim _ (subsingleton_of_zero_eq_one h) a b
/-- In a monoid with zero, either zero and one are nonequal, or zero is the only element. -/
lemma zero_ne_one_or_forall_eq_0 : (0 : M₀) ≠ 1 ∨ (∀a:M₀, a = 0) :=
not_or_of_imp eq_zero_of_zero_eq_one
end
section
variables [mul_zero_one_class M₀] [nontrivial M₀] {a b : M₀}
/-- In a nontrivial monoid with zero, zero and one are different. -/
@[simp] lemma zero_ne_one : 0 ≠ (1:M₀) :=
begin
assume h,
rcases exists_pair_ne M₀ with ⟨x, y, hx⟩,
apply hx,
calc x = 1 * x : by rw [one_mul]
... = 0 : by rw [← h, zero_mul]
... = 1 * y : by rw [← h, zero_mul]
... = y : by rw [one_mul]
end
@[simp] lemma one_ne_zero : (1:M₀) ≠ 0 :=
zero_ne_one.symm
lemma ne_zero_of_eq_one {a : M₀} (h : a = 1) : a ≠ 0 :=
calc a = 1 : h
... ≠ 0 : one_ne_zero
lemma left_ne_zero_of_mul_eq_one (h : a * b = 1) : a ≠ 0 :=
left_ne_zero_of_mul $ ne_zero_of_eq_one h
lemma right_ne_zero_of_mul_eq_one (h : a * b = 1) : b ≠ 0 :=
right_ne_zero_of_mul $ ne_zero_of_eq_one h
/-- Pullback a `nontrivial` instance along a function sending `0` to `0` and `1` to `1`. -/
protected lemma pullback_nonzero [has_zero M₀'] [has_one M₀']
(f : M₀' → M₀) (zero : f 0 = 0) (one : f 1 = 1) : nontrivial M₀' :=
⟨⟨0, 1, mt (congr_arg f) $ by { rw [zero, one], exact zero_ne_one }⟩⟩
end
section semigroup_with_zero
/-- Pullback a `semigroup_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.semigroup_with_zero
[has_zero M₀'] [has_mul M₀'] [semigroup_with_zero M₀] (f : M₀' → M₀) (hf : injective f)
(zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) :
semigroup_with_zero M₀' :=
{ .. hf.mul_zero_class f zero mul,
.. ‹has_zero M₀'›,
.. hf.semigroup f mul }
/-- Pushforward a `semigroup_with_zero` class along an surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.semigroup_with_zero
[semigroup_with_zero M₀] [has_zero M₀'] [has_mul M₀'] (f : M₀ → M₀') (hf : surjective f)
(zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) :
semigroup_with_zero M₀' :=
{ .. hf.mul_zero_class f zero mul,
.. ‹has_zero M₀'›,
.. hf.semigroup f mul }
end semigroup_with_zero
section monoid_with_zero
/-- Pullback a `monoid_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀']
[monoid_with_zero M₀]
(f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
monoid_with_zero M₀' :=
{ .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul }
/-- Pushforward a `monoid_with_zero` class along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀']
[monoid_with_zero M₀]
(f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
monoid_with_zero M₀' :=
{ .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul }
/-- Pullback a `monoid_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀']
[comm_monoid_with_zero M₀]
(f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
comm_monoid_with_zero M₀' :=
{ .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul }
/-- Pushforward a `monoid_with_zero` class along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀']
[comm_monoid_with_zero M₀]
(f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
comm_monoid_with_zero M₀' :=
{ .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul }
variables [monoid_with_zero M₀]
namespace units
/-- An element of the unit group of a nonzero monoid with zero represented as an element
of the monoid is nonzero. -/
@[simp] lemma ne_zero [nontrivial M₀] (u : units M₀) :
(u : M₀) ≠ 0 :=
left_ne_zero_of_mul_eq_one u.mul_inv
-- We can't use `mul_eq_zero` + `units.ne_zero` in the next two lemmas because we don't assume
-- `nonzero M₀`.
@[simp] lemma mul_left_eq_zero (u : units M₀) {a : M₀} : a * u = 0 ↔ a = 0 :=
⟨λ h, by simpa using mul_eq_zero_of_left h ↑u⁻¹, λ h, mul_eq_zero_of_left h u⟩
@[simp] lemma mul_right_eq_zero (u : units M₀) {a : M₀} : ↑u * a = 0 ↔ a = 0 :=
⟨λ h, by simpa using mul_eq_zero_of_right ↑u⁻¹ h, mul_eq_zero_of_right u⟩
end units
namespace is_unit
lemma ne_zero [nontrivial M₀] {a : M₀} (ha : is_unit a) : a ≠ 0 := let ⟨u, hu⟩ :=
ha in hu ▸ u.ne_zero
lemma mul_right_eq_zero {a b : M₀} (ha : is_unit a) : a * b = 0 ↔ b = 0 :=
let ⟨u, hu⟩ := ha in hu ▸ u.mul_right_eq_zero
lemma mul_left_eq_zero {a b : M₀} (hb : is_unit b) : a * b = 0 ↔ a = 0 :=
let ⟨u, hu⟩ := hb in hu ▸ u.mul_left_eq_zero
end is_unit
@[simp] theorem is_unit_zero_iff : is_unit (0 : M₀) ↔ (0:M₀) = 1 :=
⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0,
λ h, @is_unit_of_subsingleton _ _ (subsingleton_of_zero_eq_one h) 0⟩
@[simp] theorem not_is_unit_zero [nontrivial M₀] : ¬ is_unit (0 : M₀) :=
mt is_unit_zero_iff.1 zero_ne_one
variable (M₀)
end monoid_with_zero
section cancel_monoid_with_zero
variables [cancel_monoid_with_zero M₀] {a b c : M₀}
@[priority 10] -- see Note [lower instance priority]
instance cancel_monoid_with_zero.to_no_zero_divisors : no_zero_divisors M₀ :=
⟨λ a b ab0, by { by_cases a = 0, { left, exact h }, right,
apply cancel_monoid_with_zero.mul_left_cancel_of_ne_zero h, rw [ab0, mul_zero], }⟩
lemma mul_left_inj' (hc : c ≠ 0) : a * c = b * c ↔ a = b := ⟨mul_right_cancel₀ hc, λ h, h ▸ rfl⟩
lemma mul_right_inj' (ha : a ≠ 0) : a * b = a * c ↔ b = c := ⟨mul_left_cancel₀ ha, λ h, h ▸ rfl⟩
@[simp] lemma mul_eq_mul_right_iff : a * c = b * c ↔ a = b ∨ c = 0 :=
by by_cases hc : c = 0; [simp [hc], simp [mul_left_inj', hc]]
@[simp] lemma mul_eq_mul_left_iff : a * b = a * c ↔ b = c ∨ a = 0 :=
by by_cases ha : a = 0; [simp [ha], simp [mul_right_inj', ha]]
/-- Pullback a `monoid_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.cancel_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀']
(f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
cancel_monoid_with_zero M₀' :=
{ mul_left_cancel_of_ne_zero := λ x y z hx H, hf $ mul_left_cancel₀ ((hf.ne_iff' zero).2 hx) $
by erw [← mul, ← mul, H]; refl,
mul_right_cancel_of_ne_zero := λ x y z hx H, hf $ mul_right_cancel₀ ((hf.ne_iff' zero).2 hx) $
by erw [← mul, ← mul, H]; refl,
.. hf.monoid f one mul, .. hf.mul_zero_class f zero mul }
/-- An element of a `cancel_monoid_with_zero` fixed by right multiplication by an element other
than one must be zero. -/
theorem eq_zero_of_mul_eq_self_right (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 :=
classical.by_contradiction $ λ ha, h₁ $ mul_left_cancel₀ ha $ h₂.symm ▸ (mul_one a).symm
/-- An element of a `cancel_monoid_with_zero` fixed by left multiplication by an element other
than one must be zero. -/
theorem eq_zero_of_mul_eq_self_left (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 :=
classical.by_contradiction $ λ ha, h₁ $ mul_right_cancel₀ ha $ h₂.symm ▸ (one_mul a).symm
end cancel_monoid_with_zero
section comm_cancel_monoid_with_zero
variables [comm_cancel_monoid_with_zero M₀] {a b c : M₀}
/-- Pullback a `comm_cancel_monoid_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_cancel_monoid_with_zero
[has_zero M₀'] [has_mul M₀'] [has_one M₀']
(f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
comm_cancel_monoid_with_zero M₀' :=
{ .. hf.comm_monoid_with_zero f zero one mul,
.. hf.cancel_monoid_with_zero f zero one mul }
end comm_cancel_monoid_with_zero
section group_with_zero
variables [group_with_zero G₀] {a b c g h x : G₀}
alias div_eq_mul_inv ← division_def
/-- Pullback a `group_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀']
[has_inv G₀'] [has_div G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹)
(div : ∀ x y, f (x / y) = f x / f y) :
group_with_zero G₀' :=
{ inv_zero := hf $ by erw [inv, zero, inv_zero],
mul_inv_cancel := λ x hx, hf $ by erw [one, mul, inv, mul_inv_cancel ((hf.ne_iff' zero).2 hx)],
.. hf.monoid_with_zero f zero one mul,
.. hf.div_inv_monoid f one mul inv div,
.. pullback_nonzero f zero one, }
/-- Pushforward a `group_with_zero` class along an surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀']
[has_inv G₀'] [has_div G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f)
(zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y)
(inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) :
group_with_zero G₀' :=
{ inv_zero := by erw [← zero, ← inv, inv_zero],
mul_inv_cancel := hf.forall.2 $ λ x hx,
by erw [← inv, ← mul, mul_inv_cancel (mt (congr_arg f) $ trans_rel_left ne hx zero.symm)];
exact one,
exists_pair_ne := ⟨0, 1, h01⟩,
.. hf.monoid_with_zero f zero one mul,
.. hf.div_inv_monoid f one mul inv div }
@[simp] lemma mul_inv_cancel_right₀ (h : b ≠ 0) (a : G₀) :
(a * b) * b⁻¹ = a :=
calc (a * b) * b⁻¹ = a * (b * b⁻¹) : mul_assoc _ _ _
... = a : by simp [h]
@[simp] lemma mul_inv_cancel_left₀ (h : a ≠ 0) (b : G₀) :
a * (a⁻¹ * b) = b :=
calc a * (a⁻¹ * b) = (a * a⁻¹) * b : (mul_assoc _ _ _).symm
... = b : by simp [h]
lemma inv_ne_zero (h : a ≠ 0) : a⁻¹ ≠ 0 :=
assume a_eq_0, by simpa [a_eq_0] using mul_inv_cancel h
@[simp] lemma inv_mul_cancel (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]
lemma group_with_zero.mul_left_injective (h : x ≠ 0) :
function.injective (λ y, x * y) :=
λ y y' w, by simpa only [←mul_assoc, inv_mul_cancel h, one_mul] using congr_arg (λ y, x⁻¹ * y) w
lemma group_with_zero.mul_right_injective (h : x ≠ 0) :
function.injective (λ y, y * x) :=
λ y y' w, by simpa only [mul_assoc, mul_inv_cancel h, mul_one] using congr_arg (λ y, y * x⁻¹) w
@[simp] lemma inv_mul_cancel_right₀ (h : b ≠ 0) (a : G₀) :
(a * b⁻¹) * b = a :=
calc (a * b⁻¹) * b = a * (b⁻¹ * b) : mul_assoc _ _ _
... = a : by simp [h]
@[simp] lemma inv_mul_cancel_left₀ (h : a ≠ 0) (b : G₀) :
a⁻¹ * (a * b) = b :=
calc a⁻¹ * (a * b) = (a⁻¹ * a) * b : (mul_assoc _ _ _).symm
... = b : by simp [h]
@[simp] lemma inv_one : 1⁻¹ = (1:G₀) :=
calc 1⁻¹ = 1 * 1⁻¹ : by rw [one_mul]
... = (1:G₀) : by simp
@[simp] lemma inv_inv₀ (a : G₀) : a⁻¹⁻¹ = a :=
begin
by_cases h : a = 0, { simp [h] },
calc a⁻¹⁻¹ = a * (a⁻¹ * a⁻¹⁻¹) : by simp [h]
... = a : by simp [inv_ne_zero h]
end
/-- Multiplying `a` by itself and then by its inverse results in `a`
(whether or not `a` is zero). -/
@[simp] lemma mul_self_mul_inv (a : G₀) : a * a * a⁻¹ = a :=
begin
by_cases h : a = 0,
{ rw [h, inv_zero, mul_zero] },
{ rw [mul_assoc, mul_inv_cancel h, mul_one] }
end
/-- Multiplying `a` by its inverse and then by itself results in `a`
(whether or not `a` is zero). -/
@[simp] lemma mul_inv_mul_self (a : G₀) : a * a⁻¹ * a = a :=
begin
by_cases h : a = 0,
{ rw [h, inv_zero, mul_zero] },
{ rw [mul_inv_cancel h, one_mul] }
end
/-- Multiplying `a⁻¹` by `a` twice results in `a` (whether or not `a`
is zero). -/
@[simp] lemma inv_mul_mul_self (a : G₀) : a⁻¹ * a * a = a :=
begin
by_cases h : a = 0,
{ rw [h, inv_zero, mul_zero] },
{ rw [inv_mul_cancel h, one_mul] }
end
/-- Multiplying `a` by itself and then dividing by itself results in
`a` (whether or not `a` is zero). -/
@[simp] lemma mul_self_div_self (a : G₀) : a * a / a = a :=
by rw [div_eq_mul_inv, mul_self_mul_inv a]
/-- Dividing `a` by itself and then multiplying by itself results in
`a` (whether or not `a` is zero). -/
@[simp] lemma div_self_mul_self (a : G₀) : a / a * a = a :=
by rw [div_eq_mul_inv, mul_inv_mul_self a]
lemma inv_involutive₀ : function.involutive (has_inv.inv : G₀ → G₀) :=
inv_inv₀
lemma eq_inv_of_mul_right_eq_one (h : a * b = 1) :
b = a⁻¹ :=
by rw [← inv_mul_cancel_left₀ (left_ne_zero_of_mul_eq_one h) b, h, mul_one]
lemma eq_inv_of_mul_left_eq_one (h : a * b = 1) :
a = b⁻¹ :=
by rw [← mul_inv_cancel_right₀ (right_ne_zero_of_mul_eq_one h) a, h, one_mul]
lemma inv_injective₀ : function.injective (@has_inv.inv G₀ _) :=
inv_involutive₀.injective
@[simp] lemma inv_inj₀ : g⁻¹ = h⁻¹ ↔ g = h := inv_injective₀.eq_iff
/-- This is the analogue of `inv_eq_iff_inv_eq` for `group_with_zero`.
It could also be named `inv_eq_iff_inv_eq'`. -/
lemma inv_eq_iff : g⁻¹ = h ↔ h⁻¹ = g :=
by rw [← inv_inj₀, eq_comm, inv_inv₀]
/-- This is the analogue of `eq_inv_iff_eq_inv` for `group_with_zero`.
It could also be named `eq_inv_iff_eq_inv'`. -/
lemma eq_inv_iff : a = b⁻¹ ↔ b = a⁻¹ :=
by rw [eq_comm, inv_eq_iff, eq_comm]
@[simp] lemma inv_eq_one₀ : g⁻¹ = 1 ↔ g = 1 :=
by rw [inv_eq_iff, inv_one, eq_comm]
lemma eq_mul_inv_iff_mul_eq₀ (hc : c ≠ 0) : a = b * c⁻¹ ↔ a * c = b :=
by split; rintro rfl; [rw inv_mul_cancel_right₀ hc, rw mul_inv_cancel_right₀ hc]
lemma eq_inv_mul_iff_mul_eq₀ (hb : b ≠ 0) : a = b⁻¹ * c ↔ b * a = c :=
by split; rintro rfl; [rw mul_inv_cancel_left₀ hb, rw inv_mul_cancel_left₀ hb]
lemma inv_mul_eq_iff_eq_mul₀ (ha : a ≠ 0) : a⁻¹ * b = c ↔ b = a * c :=
by rw [eq_comm, eq_inv_mul_iff_mul_eq₀ ha, eq_comm]
lemma mul_inv_eq_iff_eq_mul₀ (hb : b ≠ 0) : a * b⁻¹ = c ↔ a = c * b :=
by rw [eq_comm, eq_mul_inv_iff_mul_eq₀ hb, eq_comm]
lemma mul_inv_eq_one₀ (hb : b ≠ 0) : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_inv_eq_iff_eq_mul₀ hb, one_mul]
lemma inv_mul_eq_one₀ (ha : a ≠ 0) : a⁻¹ * b = 1 ↔ a = b :=
by rw [inv_mul_eq_iff_eq_mul₀ ha, mul_one, eq_comm]
lemma mul_eq_one_iff_eq_inv₀ (hb : b ≠ 0) : a * b = 1 ↔ a = b⁻¹ :=
by { convert mul_inv_eq_one₀ (inv_ne_zero hb), rw [inv_inv₀] }
lemma mul_eq_one_iff_inv_eq₀ (ha : a ≠ 0) : a * b = 1 ↔ a⁻¹ = b :=
by { convert inv_mul_eq_one₀ (inv_ne_zero ha), rw [inv_inv₀] }
end group_with_zero
namespace units
variables [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 mk0_one (h := one_ne_zero) :
mk0 (1 : G₀) h = 1 :=
by { ext, refl }
@[simp] lemma coe_mk0 {a : G₀} (h : a ≠ 0) : (mk0 a h : G₀) = a := rfl
@[simp] lemma mk0_coe (u : units G₀) (h : (u : G₀) ≠ 0) : mk0 (u : G₀) h = u :=
units.ext rfl
@[simp, norm_cast] lemma coe_inv' (u : units G₀) : ((u⁻¹ : units G₀) : G₀) = u⁻¹ :=
eq_inv_of_mul_left_eq_one u.inv_mul
@[simp] lemma mul_inv' (u : units G₀) : (u : G₀) * u⁻¹ = 1 := mul_inv_cancel u.ne_zero
@[simp] lemma inv_mul' (u : units G₀) : (u⁻¹ : G₀) * u = 1 := inv_mul_cancel u.ne_zero
@[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⟩
@[simp] lemma exists_iff_ne_zero {x : G₀} : (∃ u : units G₀, ↑u = x) ↔ x ≠ 0 :=
⟨λ ⟨u, hu⟩, hu ▸ u.ne_zero, assume hx, ⟨mk0 x hx, rfl⟩⟩
instance : can_lift G₀ (units G₀) :=
{ coe := coe,
cond := (≠ 0),
prf := λ x, exists_iff_ne_zero.mpr }
end units
section group_with_zero
variables [group_with_zero G₀]
lemma is_unit.mk0 (x : G₀) (hx : x ≠ 0) : is_unit x := (units.mk0 x hx).is_unit
lemma is_unit_iff_ne_zero {x : G₀} : is_unit x ↔ x ≠ 0 :=
units.exists_iff_ne_zero
@[priority 10] -- see Note [lower instance priority]
instance group_with_zero.no_zero_divisors : no_zero_divisors G₀ :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h,
begin
contrapose! h,
exact ((units.mk0 a h.1) * (units.mk0 b h.2)).ne_zero
end,
.. (‹_› : group_with_zero G₀) }
@[priority 10] -- see Note [lower instance priority]
instance group_with_zero.cancel_monoid_with_zero : cancel_monoid_with_zero G₀ :=
{ mul_left_cancel_of_ne_zero := λ x y z hx h,
by rw [← inv_mul_cancel_left₀ hx y, h, inv_mul_cancel_left₀ hx z],
mul_right_cancel_of_ne_zero := λ x y z hy h,
by rw [← mul_inv_cancel_right₀ hy x, h, mul_inv_cancel_right₀ hy z],
.. (‹_› : group_with_zero G₀) }
-- Can't be put next to the other `mk0` lemmas becuase it depends on the
-- `no_zero_divisors` instance, which depends on `mk0`.
@[simp] lemma units.mk0_mul (x y : G₀) (hxy) :
units.mk0 (x * y) hxy =
units.mk0 x (mul_ne_zero_iff.mp hxy).1 * units.mk0 y (mul_ne_zero_iff.mp hxy).2 :=
by { ext, refl }
lemma mul_inv_rev₀ (x y : G₀) : (x * y)⁻¹ = y⁻¹ * x⁻¹ :=
begin
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
@[simp] lemma div_self {a : G₀} (h : a ≠ 0) : a / a = 1 :=
by rw [div_eq_mul_inv, mul_inv_cancel h]
@[simp] lemma div_one (a : G₀) : a / 1 = a :=
by simp [div_eq_mul_inv a 1]
@[simp] lemma zero_div (a : G₀) : 0 / a = 0 :=
by rw [div_eq_mul_inv, zero_mul]
@[simp] lemma div_zero (a : G₀) : a / 0 = 0 :=
by rw [div_eq_mul_inv, inv_zero, mul_zero]
@[simp] lemma div_mul_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a / b * b = a :=
by rw [div_eq_mul_inv, inv_mul_cancel_right₀ h a]
lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a :=
classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a)
@[simp] lemma mul_div_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a * b / b = a :=
by rw [div_eq_mul_inv, mul_inv_cancel_right₀ h a]
lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a :=
classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a)
local attribute [simp] div_eq_mul_inv mul_comm mul_assoc mul_left_comm
@[simp] lemma div_self_mul_self' (a : G₀) : a / (a * a) = a⁻¹ :=
calc a / (a * a) = a⁻¹⁻¹ * a⁻¹ * a⁻¹ : by simp [mul_inv_rev₀]
... = a⁻¹ : inv_mul_mul_self _
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 :=
by simpa only [one_div] using inv_ne_zero h
lemma eq_one_div_of_mul_eq_one {a b : G₀} (h : a * b = 1) : b = 1 / a :=
by simpa only [one_div] using eq_inv_of_mul_right_eq_one h
lemma eq_one_div_of_mul_eq_one_left {a b : G₀} (h : b * a = 1) : b = 1 / a :=
by simpa only [one_div] using eq_inv_of_mul_left_eq_one h
@[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]
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]
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]
@[simp] lemma zero_eq_inv {a : G₀} : 0 = a⁻¹ ↔ 0 = a :=
eq_comm.trans $ inv_eq_zero.trans 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 :=
by simpa only [div_eq_mul_inv] using congr_arg ((*) a) u.coe_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 :=
by rw [div_eq_mul_inv, mul_inv_rev₀, div_eq_mul_inv, inv_inv₀]
lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ :=
by rw [mul_inv_rev₀, div_eq_mul_inv]
lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 :=
by { rw div_eq_mul_inv, exact mul_ne_zero ha (inv_ne_zero hb) }
@[simp] lemma div_eq_zero_iff : a / b = 0 ↔ a = 0 ∨ b = 0:=
by simp [div_eq_mul_inv]
lemma div_ne_zero_iff : a / b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 :=
(not_congr div_eq_zero_iff).trans not_or_distrib
lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b :=
by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_left_inj]
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]⟩
lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b :=
by rw [eq_comm, div_eq_iff_mul_eq hc]
lemma div_eq_of_eq_mul {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : y = z * x) : y / x = z :=
(div_eq_iff_mul_eq hx).2 h.symm
lemma eq_div_of_mul_eq {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : z * x = y) : z = y / x :=
eq.symm $ div_eq_of_eq_mul hx h.symm
lemma eq_of_div_eq_one (h : a / b = 1) : a = b :=
begin
by_cases hb : b = 0,
{ rw [hb, div_zero] at h,
exact eq_of_zero_eq_one h a b },
{ rwa [div_eq_iff_mul_eq hb, one_mul, eq_comm] at h }
end
lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b :=
⟨eq_of_div_eq_one, λ h, h.symm ▸ div_self hb⟩
lemma div_mul_left {a b : G₀} (hb : b ≠ 0) : b / (a * b) = 1 / a :=
by simp only [div_eq_mul_inv, mul_inv_rev₀, mul_inv_cancel_left₀ hb, one_mul]
lemma mul_div_mul_right (a b : G₀) {c : G₀} (hc : c ≠ 0) :
(a * c) / (b * c) = a / b :=
by simp only [div_eq_mul_inv, mul_inv_rev₀, mul_assoc, mul_inv_cancel_left₀ hc]
lemma mul_mul_div (a : G₀) {b : G₀} (hb : b ≠ 0) : a = a * b * (1 / b) :=
by simp [hb]
end group_with_zero
section comm_group_with_zero -- comm
variables [comm_group_with_zero G₀] {a b c : G₀}
@[priority 10] -- see Note [lower instance priority]
instance comm_group_with_zero.comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero G₀ :=
{ ..group_with_zero.cancel_monoid_with_zero, ..comm_group_with_zero.to_comm_monoid_with_zero G₀ }
/-- Pullback a `comm_group_with_zero` class along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀']
[has_inv G₀'] [has_div G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹)
(div : ∀ x y, f (x / y) = f x / f y) :
comm_group_with_zero G₀' :=
{ .. hf.group_with_zero f zero one mul inv div, .. hf.comm_semigroup f mul }
/-- Pushforward a `comm_group_with_zero` class along a surjective function. -/
protected def function.surjective.comm_group_with_zero [has_zero G₀'] [has_mul G₀']
[has_one G₀'] [has_inv G₀'] [has_div G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f)
(zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹)
(div : ∀ x y, f (x / y) = f x / f y) :
comm_group_with_zero G₀' :=
{ .. hf.group_with_zero h01 f zero one mul inv div, .. hf.comm_semigroup f mul }
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 :=
by rw [mul_comm, div_mul_left ha]
lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b :=
by rw [mul_comm, mul_div_cancel_of_imp h]
lemma mul_div_cancel_left {a : G₀} (b : G₀) (ha : a ≠ 0) : a * b / a = b :=
mul_div_cancel_left_of_imp $ λ h, (ha h).elim
lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a :=
by rw [mul_comm, div_mul_cancel_of_imp h]
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, mul_inv₀]
lemma mul_div_mul_left (a b : G₀) {c : G₀} (hc : c ≠ 0) :
(c * a) / (c * b) = a / b :=
by rw [mul_comm c, mul_comm c, mul_div_mul_right _ _ hc]
@[field_simps] 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]
@[field_simps] 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]
@[field_simps] 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]
/-- Dividing `a` by the result of dividing `a` by itself results in
`a` (whether or not `a` is zero). -/
@[simp] lemma div_div_self (a : G₀) : a / (a / a) = a :=
begin
rw div_div_eq_mul_div,
exact mul_self_div_self a
end
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, ((one_div_ne_zero ha) h).elim)
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 [comm_group_with_zero G₀] {a b c d : G₀}
lemma div_eq_inv_mul : a / b = b⁻¹ * a :=
by rw [div_eq_mul_inv, 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, div_eq_mul_inv]
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]
@[field_simps] 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_left_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]
@[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b :=
(div_eq_iff_mul_eq hb).trans eq_comm
@[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a :=
eq_div_iff_mul_eq 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
namespace semiconj_by
@[simp] lemma zero_right [mul_zero_class G₀] (a : G₀) : semiconj_by a 0 0 :=
by simp only [semiconj_by, mul_zero, zero_mul]
@[simp] lemma zero_left [mul_zero_class G₀] (x y : G₀) : semiconj_by 0 x y :=
by simp only [semiconj_by, mul_zero, zero_mul]
variables [group_with_zero G₀] {a x y x' y' : G₀}
@[simp] lemma inv_symm_left_iff₀ : semiconj_by a⁻¹ x y ↔ semiconj_by a y x :=
classical.by_cases
(λ ha : a = 0, by simp only [ha, inv_zero, semiconj_by.zero_left])
(λ ha, @units_inv_symm_left_iff _ _ (units.mk0 a ha) _ _)
lemma inv_symm_left₀ (h : semiconj_by a x y) : semiconj_by a⁻¹ y x :=
semiconj_by.inv_symm_left_iff₀.2 h
lemma inv_right₀ (h : semiconj_by a x y) : semiconj_by a x⁻¹ y⁻¹ :=
begin
by_cases ha : a = 0,
{ simp only [ha, zero_left] },
by_cases hx : x = 0,
{ subst x,
simp only [semiconj_by, mul_zero, @eq_comm _ _ (y * a), mul_eq_zero] at h,
simp [h.resolve_right ha] },
{ have := mul_ne_zero ha hx,
rw [h.eq, mul_ne_zero_iff] at this,
exact @units_inv_right _ _ _ (units.mk0 x hx) (units.mk0 y this.1) h },
end
@[simp] lemma inv_right_iff₀ : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y :=
⟨λ h, inv_inv₀ x ▸ inv_inv₀ y ▸ h.inv_right₀, inv_right₀⟩
lemma div_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x / x') (y / y') :=
by { rw [div_eq_mul_inv, div_eq_mul_inv], exact h.mul_right h'.inv_right₀ }
end semiconj_by
namespace commute
@[simp] theorem zero_right [mul_zero_class G₀] (a : G₀) :commute a 0 := semiconj_by.zero_right a
@[simp] theorem zero_left [mul_zero_class G₀] (a : G₀) : commute 0 a := semiconj_by.zero_left a a
variables [group_with_zero G₀] {a b c : G₀}
@[simp] theorem inv_left_iff₀ : commute a⁻¹ b ↔ commute a b :=
semiconj_by.inv_symm_left_iff₀
theorem inv_left₀ (h : commute a b) : commute a⁻¹ b := inv_left_iff₀.2 h
@[simp] theorem inv_right_iff₀ : commute a b⁻¹ ↔ commute a b :=
semiconj_by.inv_right_iff₀
theorem inv_right₀ (h : commute a b) : commute a b⁻¹ := inv_right_iff₀.2 h
theorem inv_inv₀ (h : commute a b) : commute a⁻¹ b⁻¹ := h.inv_left₀.inv_right₀
@[simp] theorem div_right (hab : commute a b) (hac : commute a c) :
commute a (b / c) :=
hab.div_right hac
@[simp] theorem div_left (hac : commute a c) (hbc : commute b c) :
commute (a / b) c :=
by { rw div_eq_mul_inv, exact hac.mul_left hbc.inv_left₀ }
end commute
namespace monoid_with_zero_hom
variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero M₀] [nontrivial M₀]
section monoid_with_zero
variables (f : monoid_with_zero_hom G₀ M₀) {a : G₀}
lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 :=
⟨λ hfa ha, hfa $ ha.symm ▸ f.map_zero, λ ha, ((is_unit.mk0 a ha).map f.to_monoid_hom).ne_zero⟩
@[simp] lemma map_eq_zero : f a = 0 ↔ a = 0 :=
not_iff_not.1 f.map_ne_zero
end monoid_with_zero
section group_with_zero
variables (f : monoid_with_zero_hom G₀ G₀') (a b : G₀)
/-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/
@[simp] lemma map_inv : f a⁻¹ = (f a)⁻¹ :=
begin
by_cases h : a = 0, by simp [h],
apply eq_inv_of_mul_left_eq_one,
rw [← f.map_mul, inv_mul_cancel h, f.map_one]
end
@[simp] lemma map_div : f (a / b) = f a / f b :=
by simpa only [div_eq_mul_inv] using ((f.map_mul _ _).trans $ _root_.congr_arg _ $ f.map_inv b)
end group_with_zero
end monoid_with_zero_hom
@[simp] lemma monoid_hom.map_units_inv {M G₀ : Type*} [monoid M] [group_with_zero G₀]
(f : M →* G₀) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ :=
by rw [← units.coe_map, ← units.coe_map, ← units.coe_inv', monoid_hom.map_inv]
section noncomputable_defs
variables {M : Type*} [nontrivial M]
/-- Constructs a `group_with_zero` structure on a `monoid_with_zero`
consisting only of units and 0. -/
noncomputable def group_with_zero_of_is_unit_or_eq_zero [hM : monoid_with_zero M]
(h : ∀ (a : M), is_unit a ∨ a = 0) : group_with_zero M :=
{ inv := λ a, if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹,
inv_zero := dif_pos rfl,
mul_inv_cancel := λ a h0, by {
change a * (if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹) = 1,
rw [dif_neg h0, units.mul_inv_eq_iff_eq_mul, one_mul, is_unit.unit_spec] },
exists_pair_ne := nontrivial.exists_pair_ne,
.. hM }
/-- Constructs a `comm_group_with_zero` structure on a `comm_monoid_with_zero`
consisting only of units and 0. -/
noncomputable def comm_group_with_zero_of_is_unit_or_eq_zero [hM : comm_monoid_with_zero M]
(h : ∀ (a : M), is_unit a ∨ a = 0) : comm_group_with_zero M :=
{ .. (group_with_zero_of_is_unit_or_eq_zero h), .. hM }
end noncomputable_defs
|
6eeea4480814e348c4fae1451341ed576a8d1b2c | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/init/trunc.hlean | e1e01f13901d6484f9eba5acc8910bb07df2d055 | [
"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 | 12,435 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Floris van Doorn
Definition of is_trunc (n-truncatedness)
Ported from Coq HoTT.
-/
prelude
import .nat .logic .equiv .pathover
open eq nat sigma unit sigma.ops
--set_option class.force_new true
/- Truncation levels -/
inductive trunc_index : Type₀ :=
| minus_two : trunc_index
| succ : trunc_index → trunc_index
open trunc_index
/-
notation for trunc_index is -2, -1, 0, 1, ...
from 0 and up this comes from a coercion from num to trunc_index (via ℕ)
-/
notation `ℕ₋₂` := trunc_index -- input using \N-2
definition has_zero_trunc_index [instance] [priority 2000] : has_zero ℕ₋₂ :=
has_zero.mk (succ (succ minus_two))
definition has_one_trunc_index [instance] [priority 2000] : has_one ℕ₋₂ :=
has_one.mk (succ (succ (succ minus_two)))
namespace trunc_index
notation `-1` := trunc_index.succ trunc_index.minus_two -- ISSUE: -1 gets printed as -2.+1?
notation `-2` := trunc_index.minus_two
postfix `.+1`:(max+1) := trunc_index.succ
postfix `.+2`:(max+1) := λn, (n .+1 .+1)
--addition, where we add two to the result
definition add_plus_two [reducible] (n m : ℕ₋₂) : ℕ₋₂ :=
trunc_index.rec_on m n (λ k l, l .+1)
infix ` +2+ `:65 := trunc_index.add_plus_two
-- addition of trunc_indices, where results smaller than -2 are changed to -2
protected definition add (n m : ℕ₋₂) : ℕ₋₂ :=
trunc_index.cases_on m
(trunc_index.cases_on n -2 (λn', (trunc_index.cases_on n' -2 id)))
(λm', trunc_index.cases_on m'
(trunc_index.cases_on n -2 id)
(add_plus_two n))
/- we give a weird name to the reflexivity step to avoid overloading le.refl
(which can be used if types.trunc is imported) -/
inductive le (a : ℕ₋₂) : ℕ₋₂ → Type :=
| tr_refl : le a a
| step : Π {b}, le a b → le a (b.+1)
end trunc_index
definition has_le_trunc_index [instance] [priority 2000] : has_le ℕ₋₂ :=
has_le.mk trunc_index.le
attribute trunc_index.add [reducible]
definition has_add_trunc_index [instance] [priority 2000] : has_add ℕ₋₂ :=
has_add.mk trunc_index.add
namespace trunc_index
definition sub_two [reducible] (n : ℕ) : ℕ₋₂ :=
nat.rec_on n -2 (λ n k, k.+1)
definition add_two [reducible] (n : ℕ₋₂) : ℕ :=
trunc_index.rec_on n nat.zero (λ n k, nat.succ k)
postfix `.-2`:(max+1) := sub_two
postfix `.-1`:(max+1) := λn, (n .-2 .+1)
definition of_nat [coercion] [reducible] (n : ℕ) : ℕ₋₂ :=
n.-2.+2
definition succ_le_succ {n m : ℕ₋₂} (H : n ≤ m) : n.+1 ≤ m.+1 :=
by induction H with m H IH; apply le.tr_refl; exact le.step IH
definition minus_two_le (n : ℕ₋₂) : -2 ≤ n :=
by induction n with n IH; apply le.tr_refl; exact le.step IH
end trunc_index open trunc_index
namespace is_trunc
export [notation] [coercion] trunc_index
/- truncated types -/
/-
Just as in Coq HoTT we define an internal version of contractibility and is_trunc, but we only
use `is_trunc` and `is_contr`
-/
structure contr_internal (A : Type) :=
(center : A)
(center_eq : Π(a : A), center = a)
definition is_trunc_internal (n : ℕ₋₂) : Type → Type :=
trunc_index.rec_on n
(λA, contr_internal A)
(λn trunc_n A, (Π(x y : A), trunc_n (x = y)))
end is_trunc open is_trunc
structure is_trunc [class] (n : ℕ₋₂) (A : Type) :=
(to_internal : is_trunc_internal n A)
open nat num trunc_index
namespace is_trunc
abbreviation is_contr := is_trunc -2
abbreviation is_prop := is_trunc -1
abbreviation is_set := is_trunc 0
variables {A B : Type}
definition is_trunc_succ_intro (A : Type) (n : ℕ₋₂) [H : ∀x y : A, is_trunc n (x = y)]
: is_trunc n.+1 A :=
is_trunc.mk (λ x y, !is_trunc.to_internal)
definition is_trunc_eq [instance] [priority 1200]
(n : ℕ₋₂) [H : is_trunc (n.+1) A] (x y : A) : is_trunc n (x = y) :=
is_trunc.mk (is_trunc.to_internal (n.+1) A x y)
/- contractibility -/
definition is_contr.mk (center : A) (center_eq : Π(a : A), center = a) : is_contr A :=
is_trunc.mk (contr_internal.mk center center_eq)
definition center (A : Type) [H : is_contr A] : A :=
contr_internal.center (is_trunc.to_internal -2 A)
definition center_eq [H : is_contr A] (a : A) : !center = a :=
contr_internal.center_eq (is_trunc.to_internal -2 A) a
definition eq_of_is_contr [H : is_contr A] (x y : A) : x = y :=
(center_eq x)⁻¹ ⬝ (center_eq y)
definition prop_eq_of_is_contr {A : Type} [H : is_contr A] {x y : A} (p q : x = y) : p = q :=
have K : ∀ (r : x = y), eq_of_is_contr x y = r, from (λ r, eq.rec_on r !con.left_inv),
(K p)⁻¹ ⬝ K q
theorem is_contr_eq {A : Type} [H : is_contr A] (x y : A) : is_contr (x = y) :=
is_contr.mk !eq_of_is_contr (λ p, !prop_eq_of_is_contr)
local attribute is_contr_eq [instance]
/- truncation is upward close -/
-- n-types are also (n+1)-types
theorem is_trunc_succ [instance] [priority 900] (A : Type) (n : ℕ₋₂)
[H : is_trunc n A] : is_trunc (n.+1) A :=
trunc_index.rec_on n
(λ A (H : is_contr A), !is_trunc_succ_intro)
(λ n IH A (H : is_trunc (n.+1) A), @is_trunc_succ_intro _ _ (λ x y, IH _ _))
A H
--in the proof the type of H is given explicitly to make it available for class inference
theorem is_trunc_of_le.{l} (A : Type.{l}) {n m : ℕ₋₂} (Hnm : n ≤ m)
[Hn : is_trunc n A] : is_trunc m A :=
begin
induction Hnm with m Hnm IH,
{ exact Hn},
{ exact _}
end
definition is_trunc_of_imp_is_trunc {n : ℕ₋₂} (H : A → is_trunc (n.+1) A)
: is_trunc (n.+1) A :=
@is_trunc_succ_intro _ _ (λx y, @is_trunc_eq _ _ (H x) x y)
definition is_trunc_of_imp_is_trunc_of_le {n : ℕ₋₂} (Hn : -1 ≤ n) (H : A → is_trunc n A)
: is_trunc n A :=
begin
cases Hn with n' Hn': apply is_trunc_of_imp_is_trunc H
end
-- these must be definitions, because we need them to compute sometimes
definition is_trunc_of_is_contr (A : Type) (n : ℕ₋₂) [H : is_contr A] : is_trunc n A :=
trunc_index.rec_on n H (λn H, _)
definition is_trunc_succ_of_is_prop (A : Type) (n : ℕ₋₂) [H : is_prop A]
: is_trunc (n.+1) A :=
is_trunc_of_le A (show -1 ≤ n.+1, from succ_le_succ (minus_two_le n))
definition is_trunc_succ_succ_of_is_set (A : Type) (n : ℕ₋₂) [H : is_set A]
: is_trunc (n.+2) A :=
is_trunc_of_le A (show 0 ≤ n.+2, from succ_le_succ (succ_le_succ (minus_two_le n)))
/- props -/
definition is_prop.elim [H : is_prop A] (x y : A) : x = y :=
!center
definition is_contr_of_inhabited_prop {A : Type} [H : is_prop A] (x : A) : is_contr A :=
is_contr.mk x (λy, !is_prop.elim)
theorem is_prop_of_imp_is_contr {A : Type} (H : A → is_contr A) : is_prop A :=
@is_trunc_succ_intro A -2
(λx y,
have H2 : is_contr A, from H x,
!is_contr_eq)
theorem is_prop.mk {A : Type} (H : ∀x y : A, x = y) : is_prop A :=
is_prop_of_imp_is_contr (λ x, is_contr.mk x (H x))
theorem is_prop_elim_self {A : Type} {H : is_prop A} (x : A) : is_prop.elim x x = idp :=
!is_prop.elim
/- sets -/
theorem is_set.mk (A : Type) (H : ∀(x y : A) (p q : x = y), p = q) : is_set A :=
@is_trunc_succ_intro _ _ (λ x y, is_prop.mk (H x y))
definition is_set.elim [H : is_set A] ⦃x y : A⦄ (p q : x = y) : p = q :=
!is_prop.elim
/- instances -/
definition is_contr_sigma_eq [instance] [priority 800] {A : Type} (a : A)
: is_contr (Σ(x : A), a = x) :=
is_contr.mk (sigma.mk a idp) (λp, sigma.rec_on p (λ b q, eq.rec_on q idp))
definition is_contr_sigma_eq' [instance] [priority 800] {A : Type} (a : A)
: is_contr (Σ(x : A), x = a) :=
is_contr.mk (sigma.mk a idp) (λp, sigma.rec_on p (λ b q, eq.rec_on q idp))
definition ap_pr1_center_eq_sigma_eq {A : Type} {a x : A} (p : a = x)
: ap pr₁ (center_eq ⟨x, p⟩) = p :=
by induction p; reflexivity
definition ap_pr1_center_eq_sigma_eq' {A : Type} {a x : A} (p : x = a)
: ap pr₁ (center_eq ⟨x, p⟩) = p⁻¹ :=
by induction p; reflexivity
definition is_contr_unit : is_contr unit :=
is_contr.mk star (λp, unit.rec_on p idp)
definition is_prop_empty : is_prop empty :=
is_prop.mk (λx, !empty.elim x)
local attribute is_contr_unit is_prop_empty [instance]
definition is_trunc_unit [instance] (n : ℕ₋₂) : is_trunc n unit :=
!is_trunc_of_is_contr
definition is_trunc_empty [instance] (n : ℕ₋₂) : is_trunc (n.+1) empty :=
!is_trunc_succ_of_is_prop
/- interaction with equivalences -/
section
open is_equiv equiv
definition is_contr_is_equiv_closed (f : A → B) [Hf : is_equiv f] [HA: is_contr A]
: (is_contr B) :=
is_contr.mk (f (center A)) (λp, eq_of_eq_inv !center_eq)
definition is_contr_equiv_closed (H : A ≃ B) [HA: is_contr A] : is_contr B :=
is_contr_is_equiv_closed (to_fun H)
definition equiv_of_is_contr_of_is_contr [HA : is_contr A] [HB : is_contr B] : A ≃ B :=
equiv.mk
(λa, center B)
(is_equiv.adjointify (λa, center B) (λb, center A) center_eq center_eq)
theorem is_trunc_is_equiv_closed (n : ℕ₋₂) (f : A → B) [H : is_equiv f]
[HA : is_trunc n A] : is_trunc n B :=
begin
revert A HA B f H, induction n with n IH: intros,
{ exact is_contr_is_equiv_closed f},
{ apply is_trunc_succ_intro, intro x y,
exact IH (f⁻¹ x = f⁻¹ y) _ (x = y) (ap f⁻¹)⁻¹ !is_equiv_inv}
end
definition is_trunc_is_equiv_closed_rev (n : ℕ₋₂) (f : A → B) [H : is_equiv f]
[HA : is_trunc n B] : is_trunc n A :=
is_trunc_is_equiv_closed n f⁻¹
definition is_trunc_equiv_closed (n : ℕ₋₂) (f : A ≃ B) [HA : is_trunc n A]
: is_trunc n B :=
is_trunc_is_equiv_closed n (to_fun f)
definition is_trunc_equiv_closed_rev (n : ℕ₋₂) (f : A ≃ B) [HA : is_trunc n B]
: is_trunc n A :=
is_trunc_is_equiv_closed n (to_inv f)
definition is_equiv_of_is_prop [constructor] [HA : is_prop A] [HB : is_prop B]
(f : A → B) (g : B → A) : is_equiv f :=
is_equiv.mk f g (λb, !is_prop.elim) (λa, !is_prop.elim) (λa, !is_set.elim)
definition equiv_of_is_prop [constructor] [HA : is_prop A] [HB : is_prop B]
(f : A → B) (g : B → A) : A ≃ B :=
equiv.mk f (is_equiv_of_is_prop f g)
definition equiv_of_iff_of_is_prop [unfold 5] [HA : is_prop A] [HB : is_prop B] (H : A ↔ B) : A ≃ B :=
equiv_of_is_prop (iff.elim_left H) (iff.elim_right H)
/- truncatedness of lift -/
definition is_trunc_lift [instance] [priority 1450] (A : Type) (n : ℕ₋₂)
[H : is_trunc n A] : is_trunc n (lift A) :=
is_trunc_equiv_closed _ !equiv_lift
end
/- interaction with the Unit type -/
open equiv
/- A contractible type is equivalent to unit. -/
variable (A)
definition equiv_unit_of_is_contr [H : is_contr A] : A ≃ unit :=
equiv.MK (λ (x : A), ⋆)
(λ (u : unit), center A)
(λ (u : unit), unit.rec_on u idp)
(λ (x : A), center_eq x)
/- interaction with pathovers -/
variable {A}
variables {C : A → Type}
{a a₂ : A} (p : a = a₂)
(c : C a) (c₂ : C a₂)
definition is_prop.elimo [H : is_prop (C a)] : c =[p] c₂ :=
pathover_of_eq_tr !is_prop.elim
definition is_trunc_pathover [instance]
(n : ℕ₋₂) [H : is_trunc (n.+1) (C a)] : is_trunc n (c =[p] c₂) :=
is_trunc_equiv_closed_rev n !pathover_equiv_eq_tr
variables {p c c₂}
theorem is_set.elimo (q q' : c =[p] c₂) [H : is_set (C a)] : q = q' :=
!is_prop.elim
-- TODO: port "Truncated morphisms"
/- truncated universe -/
end is_trunc
structure trunctype (n : ℕ₋₂) :=
(carrier : Type)
(struct : is_trunc n carrier)
notation n `-Type` := trunctype n
abbreviation Prop := -1-Type
abbreviation Set := 0-Type
attribute trunctype.carrier [coercion]
attribute trunctype.struct [instance] [priority 1400]
protected abbreviation Prop.mk := @trunctype.mk -1
protected abbreviation Set.mk := @trunctype.mk (-1.+1)
protected definition trunctype.mk' [constructor] (n : ℕ₋₂) (A : Type) [H : is_trunc n A]
: n-Type :=
trunctype.mk A H
namespace is_trunc
definition tlift.{u v} [constructor] {n : ℕ₋₂} (A : trunctype.{u} n)
: trunctype.{max u v} n :=
trunctype.mk (lift A) !is_trunc_lift
end is_trunc
|
f88b48e5bb74a4a8c15dc9e965456c629c1998c5 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/polynomial/derivative.lean | 7f8bccce59b7fb72af2398b659ab86e8ec28eeb9 | [
"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,935 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import algebra.hom.iterate
import data.polynomial.eval
/-!
# The derivative map on polynomials
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main definitions
* `polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map.
-/
noncomputable theory
open finset
open_locale big_operators classical polynomial
namespace polynomial
universes u v w y z
variables {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {A : Type z} {a b : R} {n : ℕ}
section derivative
section semiring
variables [semiring R]
/-- `derivative p` is the formal derivative of the polynomial `p` -/
def derivative : R[X] →ₗ[R] R[X] :=
{ to_fun := λ p, p.sum (λ n a, C (a * n) * X^(n-1)),
map_add' := λ p q, by rw sum_add_index;
simp only [add_mul, forall_const, ring_hom.map_add,
eq_self_iff_true, zero_mul, ring_hom.map_zero],
map_smul' := λ a p, by dsimp; rw sum_smul_index;
simp only [mul_sum, ← C_mul', mul_assoc, coeff_C_mul, ring_hom.map_mul, forall_const,
zero_mul, ring_hom.map_zero, sum] }
lemma derivative_apply (p : R[X]) :
derivative p = p.sum (λn a, C (a * n) * X^(n - 1)) := rfl
lemma coeff_derivative (p : R[X]) (n : ℕ) :
coeff (derivative p) n = coeff p (n + 1) * (n + 1) :=
begin
rw [derivative_apply],
simp only [coeff_X_pow, coeff_sum, coeff_C_mul],
rw [sum, finset.sum_eq_single (n + 1)],
simp only [nat.add_succ_sub_one, add_zero, mul_one, if_true, eq_self_iff_true], norm_cast,
{ assume b, cases b,
{ intros, rw [nat.cast_zero, mul_zero, zero_mul], },
{ intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } },
{ rw [if_pos (add_tsub_cancel_right n 1).symm, mul_one, nat.cast_add, nat.cast_one,
mem_support_iff],
intro h, push_neg at h, simp [h], },
end
@[simp]
lemma derivative_zero : derivative (0 : R[X]) = 0 :=
derivative.map_zero
@[simp]
lemma iterate_derivative_zero {k : ℕ} : derivative^[k] (0 : R[X]) = 0 :=
begin
induction k with k ih,
{ simp, },
{ simp [ih], },
end
@[simp]
lemma derivative_monomial (a : R) (n : ℕ) : derivative (monomial n a) = monomial (n - 1) (a * n) :=
by { rw [derivative_apply, sum_monomial_index, C_mul_X_pow_eq_monomial], simp }
lemma derivative_C_mul_X (a : R) : derivative (C a * X) = C a :=
by simpa only [C_mul_X_eq_monomial, derivative_monomial, nat.cast_one, mul_one]
lemma derivative_C_mul_X_pow (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X ^ (n - 1) :=
by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial]
lemma derivative_C_mul_X_sq (a : R) : derivative (C a * X ^ 2) = C (a * 2) * X :=
by rw [derivative_C_mul_X_pow, nat.cast_two, pow_one]
@[simp] lemma derivative_X_pow (n : ℕ) : derivative (X ^ n : R[X]) = C ↑n * X ^ (n - 1) :=
by convert derivative_C_mul_X_pow (1 : R) n; simp
@[simp] lemma derivative_X_sq : derivative (X ^ 2 : R[X]) = C 2 * X :=
by rw [derivative_X_pow, nat.cast_two, pow_one]
@[simp] lemma derivative_C {a : R} : derivative (C a) = 0 :=
by simp [derivative_apply]
lemma derivative_of_nat_degree_zero {p : R[X]} (hp : p.nat_degree = 0) : p.derivative = 0 :=
by rw [eq_C_of_nat_degree_eq_zero hp, derivative_C]
@[simp] lemma derivative_X : derivative (X : R[X]) = 1 :=
(derivative_monomial _ _).trans $ by simp
@[simp] lemma derivative_one : derivative (1 : R[X]) = 0 :=
derivative_C
@[simp] lemma derivative_bit0 {a : R[X]} : derivative (bit0 a) = bit0 (derivative a) :=
by simp [bit0]
@[simp] lemma derivative_bit1 {a : R[X]} : derivative (bit1 a) = bit0 (derivative a) :=
by simp [bit1]
@[simp] lemma derivative_add {f g : R[X]} : derivative (f + g) = derivative f + derivative g :=
derivative.map_add f g
@[simp] lemma derivative_X_add_C (c : R) : (X + C c).derivative = 1 :=
by rw [derivative_add, derivative_X, derivative_C, add_zero]
@[simp] lemma iterate_derivative_add {f g : R[X]} {k : ℕ} :
derivative^[k] (f + g) = (derivative^[k] f) + (derivative^[k] g) :=
derivative.to_add_monoid_hom.iterate_map_add _ _ _
@[simp] lemma derivative_sum {s : finset ι} {f : ι → R[X]} :
derivative (∑ b in s, f b) = ∑ b in s, derivative (f b) :=
derivative.map_sum
@[simp] lemma derivative_smul {S : Type*} [monoid S]
[distrib_mul_action S R] [is_scalar_tower S R R]
(s : S) (p : R[X]) : derivative (s • p) = s • derivative p :=
derivative.map_smul_of_tower s p
@[simp] lemma iterate_derivative_smul {S : Type*} [monoid S]
[distrib_mul_action S R] [is_scalar_tower S R R]
(s : S) (p : R[X]) (k : ℕ) :
derivative^[k] (s • p) = s • (derivative^[k] p) :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih], },
end
@[simp]
lemma iterate_derivative_C_mul (a : R) (p : R[X]) (k : ℕ) :
derivative^[k] (C a * p) = C a * (derivative^[k] p) :=
by simp_rw [← smul_eq_C_mul, iterate_derivative_smul]
theorem of_mem_support_derivative {p : R[X]} {n : ℕ} (h : n ∈ p.derivative.support) :
n + 1 ∈ p.support :=
mem_support_iff.2 $ λ (h1 : p.coeff (n+1) = 0), mem_support_iff.1 h $
show p.derivative.coeff n = 0, by rw [coeff_derivative, h1, zero_mul]
theorem degree_derivative_lt {p : R[X]} (hp : p ≠ 0) : p.derivative.degree < p.degree :=
(finset.sup_lt_iff $ bot_lt_iff_ne_bot.2 $ mt degree_eq_bot.1 hp).2 $ λ n hp, lt_of_lt_of_le
(with_bot.some_lt_some.2 n.lt_succ_self) $ finset.le_sup $ of_mem_support_derivative hp
theorem degree_derivative_le {p : R[X]} : p.derivative.degree ≤ p.degree :=
if H : p = 0 then le_of_eq $ by rw [H, derivative_zero] else (degree_derivative_lt H).le
theorem nat_degree_derivative_lt {p : R[X]} (hp : p.nat_degree ≠ 0) :
p.derivative.nat_degree < p.nat_degree :=
begin
cases eq_or_ne p.derivative 0 with hp' hp',
{ rw [hp', polynomial.nat_degree_zero],
exact hp.bot_lt },
{ rw nat_degree_lt_nat_degree_iff hp',
exact degree_derivative_lt (λ h, hp (h.symm ▸ nat_degree_zero)) }
end
lemma nat_degree_derivative_le (p : R[X]) : p.derivative.nat_degree ≤ p.nat_degree - 1 :=
begin
by_cases p0 : p.nat_degree = 0,
{ simp [p0, derivative_of_nat_degree_zero] },
{ exact nat.le_pred_of_lt (nat_degree_derivative_lt p0) }
end
@[simp] lemma derivative_nat_cast {n : ℕ} : derivative (n : R[X]) = 0 :=
begin
rw ← map_nat_cast C n,
exact derivative_C,
end
lemma iterate_derivative_eq_zero {p : R[X]} {x : ℕ} (hx : p.nat_degree < x) :
polynomial.derivative^[x] p = 0 :=
begin
induction h : p.nat_degree using nat.strong_induction_on with _ ih generalizing p x,
subst h,
obtain ⟨t, rfl⟩ := nat.exists_eq_succ_of_ne_zero (pos_of_gt hx).ne',
rw [function.iterate_succ_apply],
by_cases hp : p.nat_degree = 0,
{ rw [derivative_of_nat_degree_zero hp, iterate_derivative_zero] },
have := nat_degree_derivative_lt hp,
exact ih _ this (this.trans_le $ nat.le_of_lt_succ hx) rfl
end
@[simp] lemma iterate_derivative_C {k} (h : 0 < k) : (derivative^[k] (C a : R[X])) = 0 :=
iterate_derivative_eq_zero $ (nat_degree_C _).trans_lt h
@[simp] lemma iterate_derivative_one {k} (h : 0 < k) : (derivative^[k] (1 : R[X])) = 0 :=
iterate_derivative_C h
@[simp] lemma iterate_derivative_X {k} (h : 1 < k) : (derivative^[k] (X : R[X])) = 0 :=
iterate_derivative_eq_zero $ nat_degree_X_le.trans_lt h
theorem nat_degree_eq_zero_of_derivative_eq_zero [no_zero_smul_divisors ℕ R] {f : R[X]}
(h : f.derivative = 0) : f.nat_degree = 0 :=
begin
rcases eq_or_ne f 0 with rfl | hf,
{ exact nat_degree_zero },
rw nat_degree_eq_zero_iff_degree_le_zero,
by_contra' f_nat_degree_pos,
rw [←nat_degree_pos_iff_degree_pos] at f_nat_degree_pos,
let m := f.nat_degree - 1,
have hm : m + 1 = f.nat_degree := tsub_add_cancel_of_le f_nat_degree_pos,
have h2 := coeff_derivative f m,
rw polynomial.ext_iff at h,
rw [h m, coeff_zero, ← nat.cast_add_one, ← nsmul_eq_mul', eq_comm, smul_eq_zero] at h2,
replace h2 := h2.resolve_left m.succ_ne_zero,
rw [hm, ←leading_coeff, leading_coeff_eq_zero] at h2,
exact hf h2
end
theorem eq_C_of_derivative_eq_zero [no_zero_smul_divisors ℕ R] {f : R[X]} (h : f.derivative = 0) :
f = C (f.coeff 0) :=
eq_C_of_nat_degree_eq_zero $ nat_degree_eq_zero_of_derivative_eq_zero h
@[simp] lemma derivative_mul {f g : R[X]} :
derivative (f * g) = derivative f * g + f * derivative g :=
calc derivative (f * g) = f.sum (λ n a, g.sum (λ m b, (n + m) • (C (a * b) * X ^ ((n + m) - 1)))) :
begin
rw mul_eq_sum_sum,
transitivity, exact derivative_sum,
transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum },
apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm,
transitivity,
{ exact congr_arg _ C_mul_X_pow_eq_monomial.symm },
dsimp, rw [← smul_mul_assoc, smul_C, nsmul_eq_mul'], exact derivative_C_mul_X_pow _ _
end
... = f.sum (λn a, g.sum (λm b,
(n • (C a * X^(n - 1))) * (C b * X^m) + (C a * X^n) * (m • (C b * X^(m - 1))))) :
sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm,
by cases n; cases m; simp_rw [add_smul, mul_smul_comm, smul_mul_assoc,
X_pow_mul_assoc, ← mul_assoc, ← C_mul, mul_assoc, ← pow_add];
simp only [nat.add_succ, nat.succ_add, nat.succ_sub_one, zero_smul, add_comm]
... = derivative f * g + f * derivative g :
begin
conv { to_rhs, congr,
{ rw [← sum_C_mul_X_pow_eq g] },
{ rw [← sum_C_mul_X_pow_eq f] } },
simp only [sum, sum_add_distrib, finset.mul_sum, finset.sum_mul, derivative_apply],
simp_rw [← smul_mul_assoc, smul_C, nsmul_eq_mul'],
end
lemma derivative_eval (p : R[X]) (x : R) :
p.derivative.eval x = p.sum (λ n a, (a * n) * x ^ (n-1)) :=
by simp_rw [derivative_apply, eval_sum, eval_mul_X_pow, eval_C]
@[simp]
theorem derivative_map [semiring S] (p : R[X]) (f : R →+* S) :
(p.map f).derivative = p.derivative.map f :=
begin
let n := max p.nat_degree ((map f p).nat_degree),
rw [derivative_apply, derivative_apply],
rw [sum_over_range' _ _ (n + 1) ((le_max_left _ _).trans_lt (lt_add_one _))],
rw [sum_over_range' _ _ (n + 1) ((le_max_right _ _).trans_lt (lt_add_one _))],
simp only [polynomial.map_sum, polynomial.map_mul, polynomial.map_C, map_mul, coeff_map,
map_nat_cast, polynomial.map_nat_cast, polynomial.map_pow, map_X],
all_goals { intro n, rw [zero_mul, C_0, zero_mul], }
end
@[simp]
theorem iterate_derivative_map [semiring S] (p : R[X]) (f : R →+* S) (k : ℕ):
polynomial.derivative^[k] (p.map f) = (polynomial.derivative^[k] p).map f :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp only [ih, function.iterate_succ, polynomial.derivative_map, function.comp_app], },
end
lemma derivative_nat_cast_mul {n : ℕ} {f : R[X]} :
(↑n * f).derivative = n * f.derivative :=
by simp
@[simp] lemma iterate_derivative_nat_cast_mul {n k : ℕ} {f : R[X]} :
derivative^[k] (n * f) = n * (derivative^[k] f) :=
by induction k with k ih generalizing f; simp*
lemma mem_support_derivative [no_zero_smul_divisors ℕ R]
(p : R[X]) (n : ℕ) :
n ∈ (derivative p).support ↔ n + 1 ∈ p.support :=
suffices ¬p.coeff (n + 1) * (n + 1 : ℕ) = 0 ↔ coeff p (n + 1) ≠ 0,
by simpa only [mem_support_iff, coeff_derivative, ne.def, nat.cast_succ],
by { rw [← nsmul_eq_mul', smul_eq_zero], simp only [nat.succ_ne_zero, false_or] }
@[simp] lemma degree_derivative_eq [no_zero_smul_divisors ℕ R]
(p : R[X]) (hp : 0 < nat_degree p) :
degree (derivative p) = (nat_degree p - 1 : ℕ) :=
begin
have h0 : p ≠ 0,
{ contrapose! hp,
simp [hp] },
apply le_antisymm,
{ rw derivative_apply,
apply le_trans (degree_sum_le _ _) (finset.sup_le (λ n hn, _)),
apply le_trans (degree_C_mul_X_pow_le _ _) (with_bot.coe_le_coe.2 (tsub_le_tsub_right _ _)),
apply le_nat_degree_of_mem_supp _ hn },
{ refine le_sup _,
rw [mem_support_derivative, tsub_add_cancel_of_le, mem_support_iff],
{ show ¬ leading_coeff p = 0,
rw [leading_coeff_eq_zero],
assume h, rw [h, nat_degree_zero] at hp,
exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), },
exact hp }
end
lemma coeff_iterate_derivative_as_prod_Ico {k} (p : R[X]) :
∀ m : ℕ, (derivative^[k] p).coeff m = (∏ i in Ico m.succ (m + k.succ), i) • (p.coeff (m + k)) :=
begin
induction k with k ih,
{ simp only [add_zero, forall_const, one_smul, Ico_self, eq_self_iff_true,
function.iterate_zero_apply, prod_empty] },
{ intro m, rw [function.iterate_succ_apply', coeff_derivative, ih (m+1), ← nat.cast_add_one,
← nsmul_eq_mul', smul_smul, mul_comm],
apply congr_arg2,
{ have set_eq : (Ico m.succ (m + k.succ.succ)) = (Ico (m + 1).succ (m + 1 + k.succ)) ∪ {m+1},
{ simp_rw [← nat.Ico_succ_singleton, union_comm, nat.succ_eq_add_one, add_comm (k + 1),
add_assoc],
rw [Ico_union_Ico_eq_Ico]; simp_rw [add_le_add_iff_left, le_add_self], },
rw [set_eq, prod_union, prod_singleton],
{ rw [disjoint_singleton_right, mem_Ico],
exact λ h, (nat.lt_succ_self _).not_le h.1 } },
{ exact congr_arg _ (nat.succ_add m k) } },
end
lemma coeff_iterate_derivative_as_prod_range {k} (p : R[X]) :
∀ m : ℕ, (derivative^[k] p).coeff m = (∏ i in range k, (m + k - i)) • p.coeff (m + k) :=
begin
induction k with k ih,
{ simp },
intro m,
calc (derivative^[k + 1] p).coeff m
= (∏ i in range k, (m + k.succ - i)) • p.coeff (m + k.succ) * (m + 1) :
by rw [function.iterate_succ_apply', coeff_derivative, ih m.succ, nat.succ_add, nat.add_succ]
... = ((∏ i in range k, (m + k.succ - i)) * (m + 1)) • p.coeff (m + k.succ) :
by rw [← nat.cast_add_one, ← nsmul_eq_mul', smul_smul, mul_comm]
... = (∏ i in range k.succ, (m + k.succ - i)) • p.coeff (m + k.succ) :
by rw [prod_range_succ, add_tsub_assoc_of_le k.le_succ, nat.succ_sub le_rfl, tsub_self]
end
lemma iterate_derivative_mul {n} (p q : R[X]) :
derivative^[n] (p * q) =
∑ k in range n.succ,
n.choose k • ((derivative^[n - k] p) * (derivative^[k] q)) :=
begin
induction n with n IH,
{ simp },
calc derivative^[n + 1] (p * q)
= (∑ (k : ℕ) in range n.succ,
(n.choose k) • ((derivative^[n - k] p) * (derivative^[k] q))).derivative :
by rw [function.iterate_succ_apply', IH]
... = ∑ (k : ℕ) in range n.succ,
(n.choose k) • ((derivative^[n - k + 1] p) * (derivative^[k] q)) +
∑ (k : ℕ) in range n.succ,
(n.choose k) • ((derivative^[n - k] p) * (derivative^[k + 1] q)) :
by simp_rw [derivative_sum, derivative_smul, derivative_mul, function.iterate_succ_apply',
smul_add, sum_add_distrib]
... = (∑ (k : ℕ) in range n.succ,
(n.choose k.succ) • ((derivative^[n - k] p) * (derivative^[k + 1] q)) +
1 • ((derivative^[n + 1] p) * (derivative^[0] q))) +
∑ (k : ℕ) in range n.succ,
(n.choose k) • ((derivative^[n - k] p) * (derivative^[k + 1] q)) : _
... = ∑ (k : ℕ) in range n.succ,
(n.choose k) • ((derivative^[n - k] p) * (derivative^[k + 1] q)) +
∑ (k : ℕ) in range n.succ,
(n.choose k.succ) • ((derivative^[n - k] p) * (derivative^[k + 1] q)) +
1 • ((derivative^[n + 1] p) * (derivative^[0] q)) :
by rw [add_comm, add_assoc]
... = ∑ (i : ℕ) in range n.succ,
((n+1).choose (i+1)) • ((derivative^[n + 1 - (i + 1)] p) * (derivative^[i + 1] q)) +
1 • ((derivative^[n + 1] p) * (derivative^[0] q)) :
by simp_rw [nat.choose_succ_succ, nat.succ_sub_succ, add_smul, sum_add_distrib]
... = ∑ (k : ℕ) in range n.succ.succ,
(n.succ.choose k) • (derivative^[n.succ - k] p * (derivative^[k] q)) :
by rw [sum_range_succ' _ n.succ, nat.choose_zero_right, tsub_zero],
congr,
refine (sum_range_succ' _ _).trans (congr_arg2 (+) _ _),
{ rw [sum_range_succ, nat.choose_succ_self, zero_smul, add_zero],
refine sum_congr rfl (λ k hk, _),
rw mem_range at hk,
congr,
rw [tsub_add_eq_add_tsub (nat.succ_le_of_lt hk), nat.succ_sub_succ] },
{ rw [nat.choose_zero_right, tsub_zero] },
end
end semiring
section comm_semiring
variables [comm_semiring R]
theorem derivative_pow_succ (p : R[X]) (n : ℕ) :
(p ^ (n + 1)).derivative = C ↑(n + 1) * (p ^ n) * p.derivative :=
nat.rec_on n (by rw [pow_one, nat.cast_one, C_1, one_mul, pow_zero, one_mul]) $ λ n ih,
by rw [pow_succ', derivative_mul, ih, nat.add_one, mul_right_comm, nat.cast_add n.succ, C_add,
add_mul, add_mul, pow_succ', ← mul_assoc, nat.cast_one, C_1, one_mul]
theorem derivative_pow (p : R[X]) (n : ℕ) :
(p ^ n).derivative = C ↑n * (p ^ (n - 1)) * p.derivative :=
nat.cases_on n (by rw [pow_zero, derivative_one, nat.cast_zero, C_0, zero_mul, zero_mul]) $ λ n,
by rw [p.derivative_pow_succ n, n.succ_sub_one, n.cast_succ]
theorem derivative_sq (p : R[X]) : (p ^ 2).derivative = C 2 * p * p.derivative :=
by rw [derivative_pow_succ, nat.cast_two, pow_one]
theorem dvd_iterate_derivative_pow (f : R[X]) (n : ℕ) {m : ℕ} (c : R) (hm : m ≠ 0) :
(n : R) ∣ eval c (derivative^[m] (f ^ n)) :=
begin
obtain ⟨m, rfl⟩ := nat.exists_eq_succ_of_ne_zero hm,
rw [function.iterate_succ_apply, derivative_pow, mul_assoc, C_eq_nat_cast,
iterate_derivative_nat_cast_mul, eval_mul, eval_nat_cast],
exact dvd_mul_right _ _,
end
lemma iterate_derivative_X_pow_eq_nat_cast_mul (n k : ℕ) :
(derivative^[k] (X ^ n : R[X])) = ↑(nat.desc_factorial n k) * X ^ (n - k) :=
begin
induction k with k ih,
{ rw [function.iterate_zero_apply, tsub_zero, nat.desc_factorial_zero, nat.cast_one, one_mul] },
{ rw [function.iterate_succ_apply', ih, derivative_nat_cast_mul, derivative_X_pow, C_eq_nat_cast,
nat.succ_eq_add_one, nat.desc_factorial_succ, nat.sub_sub, nat.cast_mul, ←mul_assoc,
mul_comm ↑(nat.desc_factorial _ _)] },
end
lemma iterate_derivative_X_pow_eq_C_mul (n k : ℕ) :
(derivative^[k] (X ^ n : R[X])) = C ↑(nat.desc_factorial n k) * X ^ (n - k) :=
by rw [iterate_derivative_X_pow_eq_nat_cast_mul n k, C_eq_nat_cast]
lemma iterate_derivative_X_pow_eq_smul (n : ℕ) (k : ℕ) :
(derivative^[k] (X ^ n : R[X])) = (nat.desc_factorial n k : R) • X ^ (n - k) :=
by rw [iterate_derivative_X_pow_eq_C_mul n k, smul_eq_C_mul]
lemma derivative_X_add_C_pow (c : R) (m : ℕ) :
((X + C c) ^ m).derivative = C ↑m * (X + C c) ^ (m - 1) :=
by rw [derivative_pow, derivative_X_add_C, mul_one]
lemma derivative_X_add_C_sq (c : R) : ((X + C c) ^ 2).derivative = C 2 * (X + C c) :=
by rw [derivative_sq, derivative_X_add_C, mul_one]
lemma iterate_derivative_X_add_pow (n k : ℕ) (c : R) : derivative^[k] ((X + C c) ^ n) =
↑(∏ i in finset.range k, (n - i)) * (X + C c) ^ (n - k) :=
begin
induction k with k IH,
{ rw [function.iterate_zero_apply, finset.range_zero, finset.prod_empty, nat.cast_one, one_mul,
tsub_zero] },
{ simp only [function.iterate_succ_apply', IH, derivative_mul, zero_mul, derivative_nat_cast,
zero_add, finset.prod_range_succ, C_eq_nat_cast, nat.sub_sub, ←mul_assoc,
derivative_X_add_C_pow, nat.succ_eq_add_one, nat.cast_mul] },
end
lemma derivative_comp (p q : R[X]) : (p.comp q).derivative = q.derivative * p.derivative.comp q :=
begin
apply polynomial.induction_on' p,
{ intros p₁ p₂ h₁ h₂, simp [h₁, h₂, mul_add], },
{ intros n r,
simp only [derivative_pow, derivative_mul, monomial_comp, derivative_monomial, derivative_C,
zero_mul, C_eq_nat_cast, zero_add, ring_hom.map_mul],
-- is there a tactic for this? (a multiplicative `abel`):
rw [mul_comm (derivative q)],
simp only [mul_assoc], }
end
/-- Chain rule for formal derivative of polynomials. -/
theorem derivative_eval₂_C (p q : R[X]) :
(p.eval₂ C q).derivative = p.derivative.eval₂ C q * q.derivative :=
polynomial.induction_on p
(λ r, by rw [eval₂_C, derivative_C, eval₂_zero, zero_mul])
(λ p₁ p₂ ih₁ ih₂, by rw [eval₂_add, derivative_add, ih₁, ih₂, derivative_add, eval₂_add, add_mul])
(λ n r ih, by rw [pow_succ', ← mul_assoc, eval₂_mul, eval₂_X, derivative_mul, ih,
@derivative_mul _ _ _ X, derivative_X, mul_one, eval₂_add, @eval₂_mul _ _ _ _ X, eval₂_X,
add_mul, mul_right_comm])
theorem derivative_prod {s : multiset ι} {f : ι → R[X]} :
(multiset.map f s).prod.derivative =
(multiset.map (λ i, (multiset.map f (s.erase i)).prod * (f i).derivative) s).sum :=
begin
refine multiset.induction_on s (by simp) (λ i s h, _),
rw [multiset.map_cons, multiset.prod_cons, derivative_mul, multiset.map_cons _ i s,
multiset.sum_cons, multiset.erase_cons_head, mul_comm (f i).derivative],
congr,
rw [h, ← add_monoid_hom.coe_mul_left, (add_monoid_hom.mul_left (f i)).map_multiset_sum _,
add_monoid_hom.coe_mul_left],
simp only [function.comp_app, multiset.map_map],
refine congr_arg _ (multiset.map_congr rfl (λ j hj, _)),
rw [← mul_assoc, ← multiset.prod_cons, ← multiset.map_cons],
by_cases hij : i = j,
{ simp [hij, ← multiset.prod_cons, ← multiset.map_cons, multiset.cons_erase hj] },
{ simp [hij] }
end
end comm_semiring
section ring
variables [ring R]
@[simp] lemma derivative_neg (f : R[X]) : derivative (-f) = - derivative f :=
linear_map.map_neg derivative f
@[simp] lemma iterate_derivative_neg {f : R[X]} {k : ℕ} :
derivative^[k] (-f) = - (derivative^[k] f) :=
(@derivative R _).to_add_monoid_hom.iterate_map_neg _ _
@[simp] lemma derivative_sub {f g : R[X]} : derivative (f - g) = derivative f - derivative g :=
linear_map.map_sub derivative f g
@[simp] lemma derivative_X_sub_C (c : R) : (X - C c).derivative = 1 :=
by rw [derivative_sub, derivative_X, derivative_C, sub_zero]
@[simp] lemma iterate_derivative_sub {k : ℕ} {f g : R[X]} :
derivative^[k] (f - g) = (derivative^[k] f) - (derivative^[k] g) :=
by induction k with k ih generalizing f g; simp*
@[simp] lemma derivative_int_cast {n : ℤ} : derivative (n : R[X]) = 0 :=
begin
rw ← C_eq_int_cast n,
exact derivative_C,
end
lemma derivative_int_cast_mul {n : ℤ} {f : R[X]} :
(↑n * f).derivative = n * f.derivative :=
by simp
@[simp] lemma iterate_derivative_int_cast_mul {n : ℤ} {k : ℕ} {f : R[X]} :
derivative^[k] (↑n * f) = n * (derivative^[k] f) :=
by induction k with k ih generalizing f; simp*
end ring
section comm_ring
variables [comm_ring R]
lemma derivative_comp_one_sub_X (p : R[X]) :
(p.comp (1 - X)).derivative = -p.derivative.comp (1 - X) :=
by simp [derivative_comp]
@[simp]
lemma iterate_derivative_comp_one_sub_X (p : R[X]) (k : ℕ) :
derivative^[k] (p.comp (1 - X)) = (-1) ^ k * (derivative^[k] p).comp (1 - X) :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih p.derivative, iterate_derivative_neg, derivative_comp, pow_succ], },
end
lemma eval_multiset_prod_X_sub_C_derivative {S : multiset R} {r : R} (hr : r ∈ S) :
eval r (multiset.map (λ a, X - C a) S).prod.derivative =
(multiset.map (λ a, r - a) (S.erase r)).prod :=
begin
nth_rewrite 0 [← multiset.cons_erase hr],
simpa using (eval_ring_hom r).map_multiset_prod (multiset.map (λ a, X - C a) (S.erase r)),
end
lemma derivative_X_sub_C_pow (c : R) (m : ℕ) :
((X - C c) ^ m).derivative = C ↑m * (X - C c) ^ (m - 1) :=
by rw [derivative_pow, derivative_X_sub_C, mul_one]
lemma derivative_X_sub_C_sq (c : R) : ((X - C c) ^ 2).derivative = C 2 * (X - C c) :=
by rw [derivative_sq, derivative_X_sub_C, mul_one]
lemma iterate_derivative_X_sub_pow (n k : ℕ) (c : R) :
(derivative^[k] ((X - C c) ^ n)) = (↑(∏ i in finset.range k, (n - i))) * (X - C c) ^ (n - k) :=
by simp_rw [sub_eq_add_neg, ←C_neg, iterate_derivative_X_add_pow]
end comm_ring
end derivative
end polynomial
|
b4afd3e36a25005e2c4aa1bd387941e913c8c825 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/category_theory/functor/flat.lean | 675ccdea6503812593486a0e01bf96ed5450e938 | [
"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 | 16,378 | lean | /-
Copyright (c) 2021 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import category_theory.limits.filtered_colimit_commutes_finite_limit
import category_theory.limits.preserves.functor_category
import category_theory.limits.preserves.shapes.equalizers
import category_theory.limits.bicones
import category_theory.limits.comma
import category_theory.limits.preserves.finite
import category_theory.limits.shapes.finite_limits
/-!
# Representably flat functors
We define representably flat functors as functors such that the category of structured arrows
over `X` is cofiltered for each `X`. This concept is also known as flat functors as in [Elephant]
Remark C2.3.7, and this name is suggested by Mike Shulman in
https://golem.ph.utexas.edu/category/2011/06/flat_functors_and_morphisms_of.html to avoid
confusion with other notions of flatness.
This definition is equivalent to left exact functors (functors that preserves finite limits) when
`C` has all finite limits.
## Main results
* `flat_of_preserves_finite_limits`: If `F : C ⥤ D` preserves finite limits and `C` has all finite
limits, then `F` is flat.
* `preserves_finite_limits_of_flat`: If `F : C ⥤ D` is flat, then it preserves all finite limits.
* `preserves_finite_limits_iff_flat`: If `C` has all finite limits,
then `F` is flat iff `F` is left_exact.
* `Lan_preserves_finite_limits_of_flat`: If `F : C ⥤ D` is a flat functor between small categories,
then the functor `Lan F.op` between presheaves of sets preserves all finite limits.
* `flat_iff_Lan_flat`: If `C`, `D` are small and `C` has all finite limits, then `F` is flat iff
`Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)` is flat.
* `preserves_finite_limits_iff_Lan_preserves_finite_limits`: If `C`, `D` are small and `C` has all
finite limits, then `F` preserves finite limits iff `Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)`
does.
-/
universes w v₁ v₂ v₃ u₁ u₂ u₃
open category_theory
open category_theory.limits
open opposite
namespace category_theory
namespace structured_arrow_cone
open structured_arrow
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D]
variables {J : Type w} [small_category J]
variables {K : J ⥤ C} (F : C ⥤ D) (c : cone K)
/--
Given a cone `c : cone K` and a map `f : X ⟶ c.X`, we can construct a cone of structured
arrows over `X` with `f` as the cone point. This is the underlying diagram.
-/
@[simps]
def to_diagram : J ⥤ structured_arrow c.X K :=
{ obj := λ j, structured_arrow.mk (c.π.app j),
map := λ j k g, structured_arrow.hom_mk g (by simpa) }
/-- Given a diagram of `structured_arrow X F`s, we may obtain a cone with cone point `X`. -/
@[simps]
def diagram_to_cone {X : D} (G : J ⥤ structured_arrow X F) : cone (G ⋙ proj X F ⋙ F) :=
{ X := X, π := { app := λ j, (G.obj j).hom } }
/--
Given a cone `c : cone K` and a map `f : X ⟶ F.obj c.X`, we can construct a cone of structured
arrows over `X` with `f` as the cone point.
-/
@[simps]
def to_cone {X : D} (f : X ⟶ F.obj c.X) :
cone (to_diagram (F.map_cone c) ⋙ map f ⋙ pre _ K F) :=
{ X := mk f, π := { app := λ j, hom_mk (c.π.app j) rfl,
naturality' := λ j k g, by { ext, dsimp, simp } } }
end structured_arrow_cone
section representably_flat
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
variables {E : Type u₃} [category.{v₃} E]
/--
A functor `F : C ⥤ D` is representably-flat functor if the comma category `(X/F)`
is cofiltered for each `X : C`.
-/
class representably_flat (F : C ⥤ D) : Prop :=
(cofiltered : ∀ (X : D), is_cofiltered (structured_arrow X F))
attribute [instance] representably_flat.cofiltered
local attribute [instance] is_cofiltered.nonempty
instance representably_flat.id : representably_flat (𝟭 C) :=
begin
constructor,
intro X,
haveI : nonempty (structured_arrow X (𝟭 C)) := ⟨structured_arrow.mk (𝟙 _)⟩,
rsufficesI : is_cofiltered_or_empty (structured_arrow X (𝟭 C)),
{ constructor },
constructor,
{ intros Y Z,
use structured_arrow.mk (𝟙 _),
use structured_arrow.hom_mk Y.hom (by erw [functor.id_map, category.id_comp]),
use structured_arrow.hom_mk Z.hom (by erw [functor.id_map, category.id_comp]) },
{ intros Y Z f g,
use structured_arrow.mk (𝟙 _),
use structured_arrow.hom_mk Y.hom (by erw [functor.id_map, category.id_comp]),
ext,
transitivity Z.hom; simp }
end
instance representably_flat.comp (F : C ⥤ D) (G : D ⥤ E)
[representably_flat F] [representably_flat G] : representably_flat (F ⋙ G) :=
begin
constructor,
intro X,
haveI : nonempty (structured_arrow X (F ⋙ G)),
{ have f₁ : structured_arrow X G := nonempty.some infer_instance,
have f₂ : structured_arrow f₁.right F := nonempty.some infer_instance,
exact ⟨structured_arrow.mk (f₁.hom ≫ G.map f₂.hom)⟩ },
rsufficesI : is_cofiltered_or_empty (structured_arrow X (F ⋙ G)),
{ constructor },
constructor,
{ intros Y Z,
let W := @is_cofiltered.min (structured_arrow X G) _ _
(structured_arrow.mk Y.hom) (structured_arrow.mk Z.hom),
let Y' : W ⟶ _ := is_cofiltered.min_to_left _ _,
let Z' : W ⟶ _ := is_cofiltered.min_to_right _ _,
let W' := @is_cofiltered.min (structured_arrow W.right F) _ _
(structured_arrow.mk Y'.right) (structured_arrow.mk Z'.right),
let Y'' : W' ⟶ _ := is_cofiltered.min_to_left _ _,
let Z'' : W' ⟶ _ := is_cofiltered.min_to_right _ _,
use structured_arrow.mk (W.hom ≫ G.map W'.hom),
use structured_arrow.hom_mk Y''.right (by simp [← G.map_comp]),
use structured_arrow.hom_mk Z''.right (by simp [← G.map_comp]) },
{ intros Y Z f g,
let W := @is_cofiltered.eq (structured_arrow X G) _ _
(structured_arrow.mk Y.hom) (structured_arrow.mk Z.hom)
(structured_arrow.hom_mk (F.map f.right) (structured_arrow.w f))
(structured_arrow.hom_mk (F.map g.right) (structured_arrow.w g)),
let h : W ⟶ _ := is_cofiltered.eq_hom _ _,
let h_cond : h ≫ _ = h ≫ _ := is_cofiltered.eq_condition _ _,
let W' := @is_cofiltered.eq (structured_arrow W.right F) _ _
(structured_arrow.mk h.right) (structured_arrow.mk (h.right ≫ F.map f.right))
(structured_arrow.hom_mk f.right rfl)
(structured_arrow.hom_mk g.right (congr_arg comma_morphism.right h_cond).symm),
let h' : W' ⟶ _ := is_cofiltered.eq_hom _ _,
let h'_cond : h' ≫ _ = h' ≫ _ := is_cofiltered.eq_condition _ _,
use structured_arrow.mk (W.hom ≫ G.map W'.hom),
use structured_arrow.hom_mk h'.right (by simp [← G.map_comp]),
ext,
exact (congr_arg comma_morphism.right h'_cond : _) }
end
end representably_flat
section has_limit
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D]
local attribute [instance] has_finite_limits_of_has_finite_limits_of_size
@[priority 100]
instance cofiltered_of_has_finite_limits [has_finite_limits C] : is_cofiltered C :=
{ cocone_objs := λ A B, ⟨limits.prod A B, limits.prod.fst, limits.prod.snd, trivial⟩,
cocone_maps := λ A B f g, ⟨equalizer f g, equalizer.ι f g, equalizer.condition f g⟩,
nonempty := ⟨⊤_ C⟩ }
lemma flat_of_preserves_finite_limits [has_finite_limits C] (F : C ⥤ D)
[preserves_finite_limits F] : representably_flat F := ⟨λ X,
begin
haveI : has_finite_limits (structured_arrow X F) :=
begin
apply has_finite_limits_of_has_finite_limits_of_size.{v₁} (structured_arrow X F),
intros J sJ fJ, resetI, constructor
end,
apply_instance
end⟩
namespace preserves_finite_limits_of_flat
open structured_arrow
open structured_arrow_cone
variables {J : Type v₁} [small_category J] [fin_category J] {K : J ⥤ C}
variables (F : C ⥤ D) [representably_flat F] {c : cone K} (hc : is_limit c) (s : cone (K ⋙ F))
include hc
/--
(Implementation).
Given a limit cone `c : cone K` and a cone `s : cone (K ⋙ F)` with `F` representably flat,
`s` can factor through `F.map_cone c`.
-/
noncomputable def lift : s.X ⟶ F.obj c.X :=
let s' := is_cofiltered.cone (to_diagram s ⋙ structured_arrow.pre _ K F) in
s'.X.hom ≫ (F.map $ hc.lift $
(cones.postcompose ({ app := λ X, 𝟙 _, naturality' := by simp }
: (to_diagram s ⋙ pre s.X K F) ⋙ proj s.X F ⟶ K)).obj $
(structured_arrow.proj s.X F).map_cone s')
lemma fac (x : J) : lift F hc s ≫ (F.map_cone c).π.app x = s.π.app x :=
by simpa [lift, ←functor.map_comp]
local attribute [simp] eq_to_hom_map
lemma uniq {K : J ⥤ C} {c : cone K} (hc : is_limit c)
(s : cone (K ⋙ F)) (f₁ f₂ : s.X ⟶ F.obj c.X)
(h₁ : ∀ (j : J), f₁ ≫ (F.map_cone c).π.app j = s.π.app j)
(h₂ : ∀ (j : J), f₂ ≫ (F.map_cone c).π.app j = s.π.app j) : f₁ = f₂ :=
begin
-- We can make two cones over the diagram of `s` via `f₁` and `f₂`.
let α₁ : to_diagram (F.map_cone c) ⋙ map f₁ ⟶ to_diagram s :=
{ app := λ X, eq_to_hom (by simp [←h₁]), naturality' := λ _ _ _, by { ext, simp } },
let α₂ : to_diagram (F.map_cone c) ⋙ map f₂ ⟶ to_diagram s :=
{ app := λ X, eq_to_hom (by simp [←h₂]), naturality' := λ _ _ _, by { ext, simp } },
let c₁ : cone (to_diagram s ⋙ pre s.X K F) :=
(cones.postcompose (whisker_right α₁ (pre s.X K F) : _)).obj (to_cone F c f₁),
let c₂ : cone (to_diagram s ⋙ pre s.X K F) :=
(cones.postcompose (whisker_right α₂ (pre s.X K F) : _)).obj (to_cone F c f₂),
-- The two cones can then be combined and we may obtain a cone over the two cones since
-- `structured_arrow s.X F` is cofiltered.
let c₀ := is_cofiltered.cone (bicone_mk _ c₁ c₂),
let g₁ : c₀.X ⟶ c₁.X := c₀.π.app (bicone.left),
let g₂ : c₀.X ⟶ c₂.X := c₀.π.app (bicone.right),
-- Then `g₁.right` and `g₂.right` are two maps from the same cone into the `c`.
have : ∀ (j : J), g₁.right ≫ c.π.app j = g₂.right ≫ c.π.app j,
{ intro j,
injection c₀.π.naturality (bicone_hom.left j) with _ e₁,
injection c₀.π.naturality (bicone_hom.right j) with _ e₂,
simpa using e₁.symm.trans e₂ },
have : c.extend g₁.right = c.extend g₂.right,
{ unfold cone.extend, congr' 1, ext x, apply this },
-- And thus they are equal as `c` is the limit.
have : g₁.right = g₂.right,
calc g₁.right = hc.lift (c.extend g₁.right) : by { apply hc.uniq (c.extend _), tidy }
... = hc.lift (c.extend g₂.right) : by { congr, exact this }
... = g₂.right : by { symmetry, apply hc.uniq (c.extend _), tidy },
-- Finally, since `fᵢ` factors through `F(gᵢ)`, the result follows.
calc f₁ = 𝟙 _ ≫ f₁ : by simp
... = c₀.X.hom ≫ F.map g₁.right : g₁.w
... = c₀.X.hom ≫ F.map g₂.right : by rw this
... = 𝟙 _ ≫ f₂ : g₂.w.symm
... = f₂ : by simp
end
end preserves_finite_limits_of_flat
/-- Representably flat functors preserve finite limits. -/
noncomputable
def preserves_finite_limits_of_flat (F : C ⥤ D) [representably_flat F] :
preserves_finite_limits F :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size,
intros J _ _, constructor,
intros K, constructor,
intros c hc,
exactI { lift := preserves_finite_limits_of_flat.lift F hc,
fac' := preserves_finite_limits_of_flat.fac F hc,
uniq' := λ s m h, by
{ apply preserves_finite_limits_of_flat.uniq F hc,
exact h,
exact preserves_finite_limits_of_flat.fac F hc s } }
end
/--
If `C` is finitely cocomplete, then `F : C ⥤ D` is representably flat iff it preserves
finite limits.
-/
noncomputable
def preserves_finite_limits_iff_flat [has_finite_limits C] (F : C ⥤ D) :
representably_flat F ≃ preserves_finite_limits F :=
{ to_fun := λ _, by exactI preserves_finite_limits_of_flat F,
inv_fun := λ _, by exactI flat_of_preserves_finite_limits F,
left_inv := λ _, proof_irrel _ _,
right_inv := λ x, by { cases x, unfold preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr } }
end has_limit
section small_category
variables {C D : Type u₁} [small_category C] [small_category D] (E : Type u₂) [category.{u₁} E]
/--
(Implementation)
The evaluation of `Lan F` at `X` is the colimit over the costructured arrows over `X`.
-/
noncomputable
def Lan_evaluation_iso_colim (F : C ⥤ D) (X : D)
[∀ (X : D), has_colimits_of_shape (costructured_arrow F X) E] :
Lan F ⋙ (evaluation D E).obj X ≅
((whiskering_left _ _ E).obj (costructured_arrow.proj F X)) ⋙ colim :=
nat_iso.of_components (λ G, colim.map_iso (iso.refl _))
begin
intros G H i,
ext,
simp only [functor.comp_map, colimit.ι_desc_assoc, functor.map_iso_refl, evaluation_obj_map,
whiskering_left_obj_map, category.comp_id, Lan_map_app, category.assoc],
erw [colimit.ι_pre_assoc (Lan.diagram F H X) (costructured_arrow.map j.hom),
category.id_comp, category.comp_id, colimit.ι_map],
rcases j with ⟨j_left, ⟨⟨⟩⟩, j_hom⟩,
congr,
rw [costructured_arrow.map_mk, category.id_comp, costructured_arrow.mk]
end
variables [concrete_category.{u₁} E] [has_limits E] [has_colimits E]
variables [reflects_limits (forget E)] [preserves_filtered_colimits (forget E)]
variables [preserves_limits (forget E)]
/--
If `F : C ⥤ D` is a representably flat functor between small categories, then the functor
`Lan F.op` that takes presheaves over `C` to presheaves over `D` preserves finite limits.
-/
noncomputable
instance Lan_preserves_finite_limits_of_flat (F : C ⥤ D) [representably_flat F] :
preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros J _ _, resetI,
apply preserves_limits_of_shape_of_evaluation (Lan F.op : (Cᵒᵖ ⥤ E) ⥤ (Dᵒᵖ ⥤ E)) J,
intro K,
haveI : is_filtered (costructured_arrow F.op K) :=
is_filtered.of_equivalence (structured_arrow_op_equivalence F (unop K)),
exact preserves_limits_of_shape_of_nat_iso (Lan_evaluation_iso_colim _ _ _).symm,
end
instance Lan_flat_of_flat (F : C ⥤ D) [representably_flat F] :
representably_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) := flat_of_preserves_finite_limits _
variable [has_finite_limits C]
noncomputable
instance Lan_preserves_finite_limits_of_preserves_finite_limits (F : C ⥤ D)
[preserves_finite_limits F] : preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) :=
begin
haveI := flat_of_preserves_finite_limits F,
apply_instance
end
lemma flat_iff_Lan_flat (F : C ⥤ D) :
representably_flat F ↔ representably_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)) :=
⟨λ H, by exactI infer_instance, λ H,
begin
resetI,
haveI := preserves_finite_limits_of_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)),
haveI : preserves_finite_limits F :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros, resetI, apply preserves_limit_of_Lan_presesrves_limit
end,
apply flat_of_preserves_finite_limits
end⟩
/--
If `C` is finitely complete, then `F : C ⥤ D` preserves finite limits iff
`Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)` preserves finite limits.
-/
noncomputable
def preserves_finite_limits_iff_Lan_preserves_finite_limits (F : C ⥤ D) :
preserves_finite_limits F ≃ preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)) :=
{ to_fun := λ _, by exactI infer_instance,
inv_fun := λ _,
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros, resetI, apply preserves_limit_of_Lan_presesrves_limit
end,
left_inv := λ x,
begin
cases x, unfold preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr
end,
right_inv := λ x,
begin
cases x,
unfold preserves_finite_limits_of_flat,
congr,
unfold category_theory.Lan_preserves_finite_limits_of_preserves_finite_limits
category_theory.Lan_preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr
end }
end small_category
end category_theory
|
2c8a7b02f937b15ab8d0552f78811882fe0ce5d3 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/uni_bug1.lean | 1942c9d6e1e026525d0aa6fe0487c03c43310753 | [
"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 | 175 | lean | --
open nat prod
constant R : nat → nat → Prop
constant f (a b : nat) (H : R a b) : nat
axiom Rtrue (a b : nat) : R a b
check f 1 0 (Rtrue (pr1 (prod.mk 1 (0:nat))) 0)
|
d9392628ed0c5d3a59dc3dc0c941f0bf8acc35f9 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/print_thm.lean | 66dac3d5afb055096436d0feae479f4789d60bc5 | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 101 | lean | open nat
theorem simple (a : nat) : a ≥ 0 :=
zero_le a
print simple
reveal simple
print simple
|
a581c98fb984b34d103abfe08a9d03a277dfdb95 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/subst1.lean | 8aedd50c3740d892fb8999393943536f70f9f3e0 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 631 | lean | new_frontend
set_option trace.Meta.Tactic.subst true
theorem tst1 (x y z : Nat) : y = z → x = x → x = y → x = z := by
intros h1 h2 h3
subst x
assumption
theorem tst2 (x y z : Nat) : y = z → x = z + y → x = z + z := by
intros h1 h2
subst h1
subst h2
exact rfl
def BV (n : Nat) : Type := Unit
theorem tst3 (n m : Nat) (v : BV n) (w : BV m) (h1 : n = m) (h2 : forall (v1 v2 : BV n), v1 = v2) : v = cast (congrArg BV h1) w := by
subst h1
apply h2
theorem tst4 (n m : Nat) (v : BV n) (w : BV m) (h1 : n = m) (h2 : forall (v1 v2 : BV n), v1 = v2) : v = cast (congrArg BV h1.symm) w := by
subst n
apply h2
|
dbb9e7361a220ae2cef2119a37124a072a0ca680 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/Reparen.lean | 8ef804e6832e2aa1951695b41d8982be12334291 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,015 | lean | /-! Reprint file after removing all parentheses and then passing it through the parenthesizer -/
import Lean.Parser
new_frontend
open Lean
open Lean.Format
def unparenAux (parens body : Syntax) : Syntax :=
match parens.getHeadInfo, body.getHeadInfo, body.getTailInfo, parens.getTailInfo with
| some bi', some bi, some ti, some ti' => (body.setHeadInfo { bi with leading := bi'.leading }).setTailInfo { ti with trailing := ti'.trailing }
| _, _, _, _ => body
partial def unparen : Syntax → Syntax
-- don't remove parentheses in syntax quotations, they might be semantically significant
| stx => if stx.isOfKind `Lean.Parser.Term.stxQuot then stx
else match_syntax stx with
| `(($stx')) => unparenAux stx $ unparen stx'
| `(level|($stx')) => unparenAux stx $ unparen stx'
| _ => stx.modifyArgs $ Array.map unparen
unsafe def main (args : List String) : IO Unit := do
let (debug, f) : Bool × String := match args with
| [f, "-d"] => (true, f)
| [f] => (false, f)
| _ => panic! "usage: file [-d]";
let env ← mkEmptyEnvironment;
let stx ← Lean.Parser.parseFile env args.head!;
let header := stx.getArg 0;
let some s ← pure header.reprint | throw $ IO.userError "header reprint failed";
IO.print s;
let cmds := (stx.getArg 1).getArgs;
cmds.forM $ fun cmd => do
let cmd := unparen cmd;
let (cmd, _) ← («finally» (PrettyPrinter.parenthesizeCommand cmd) printTraces).toIO { options := Options.empty.setBool `trace.PrettyPrinter.parenthesize debug } { env := env };
let some s ← pure cmd.reprint | throw $ IO.userError "cmd reprint failed";
IO.print s
#eval main ["../../../src/Init/Core.lean"]
def check (stx : Syntax) : CoreM Unit := do
let stx' := unparen stx;
let stx' ← PrettyPrinter.parenthesizeTerm stx';
let f ← PrettyPrinter.formatTerm stx';
IO.println f;
when (stx != stx') $
throwError "reparenthesization failed"
open Lean
syntax:80 term " ^~ " term:80 : term
syntax:70 term " *~ " term:71 : term
#eval check $ Unhygienic.run `(((1 + 2) *~ 3) ^~ 4)
|
0e329c040309f7ddc5dbe59c09974a1fd89cd88a | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/as_is_elab.lean | 34b8de8451370dbee23b17acf6963b7c9dc396f0 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 627 | lean | open function
variable x : list nat
#check x^.map (+1)
#check x^.foldl (+) 0
def f (l : list (nat × nat)) : list nat :=
l^.map (λ ⟨a, b⟩, a + b)
example : [(1,2), (3,4)]^.map (uncurry (+)) = [3, 7] :=
rfl
example : [(1,2), (3,4)]^.map (λ ⟨a, b⟩, a + b) = [3, 7] :=
rfl
instance decidable_uncurry_pred{α} (p : α → α → Prop) [decidable_rel p] : decidable_pred (uncurry p) :=
λ a, by { cases a; dsimp [uncurry]; apply_instance }
example : [(1,2), (4,3), (3, 2), (0, 2), (5, 4)]^.filter (uncurry (>)) = [(4,3), (3,2), (5, 4)] :=
rfl
example : [(1,2), (4,3)]^.foldl (λ v ⟨a, b⟩, v + a) 0 = 5 :=
rfl
|
8edfd8486796368ebd6d404f9830b8723b0f6336 | 13d50f9487a2afddb5e1ae1bbe68f7870f70e79a | /Demo.lean | b0e4bbdb4a2840219828f3da0fb2b640b24356a0 | [] | no_license | gebner/lean4-mathlib-import | 09a09d9cc30738bcc253e919ab3485e13b8f992d | 719c0558dfa9c4ec201aa40f4786d5f1c1e4bd1e | refs/heads/master | 1,649,553,984,859 | 1,584,121,837,000 | 1,584,121,837,000 | 238,557,672 | 4 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 111 | lean | import Import.Mathlib
new_frontend
set_option pp.all true
#check (deriv : (real → real) → (real → real)) |
d7b54fbfbc09cfb34730dc8a24cbb6096bf5e7c8 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Init/Data/String/Extra.lean | bcb125052c4e6eb248002fa64765d18fc3c550e0 | [
"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 | 2,314 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import Init.Control.Except
import Init.Data.ByteArray
import Init.SimpLemmas
import Init.Data.Nat.Linear
import Init.Util
import Init.WFTactics
namespace String
/-- Interpret the string as the decimal representation of a natural number.
Panics if the string is not a string of digits. -/
def toNat! (s : String) : Nat :=
if s.isNat then
s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0
else
panic! "Nat expected"
/--
Convert a [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoded `ByteArray` string to `String`.
The result is unspecified if `a` is not properly UTF-8 encoded.
-/
@[extern "lean_string_from_utf8_unchecked"]
opaque fromUTF8Unchecked (a : @& ByteArray) : String
/-- Convert the given `String` to a [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoded byte array. -/
@[extern "lean_string_to_utf8"]
opaque toUTF8 (a : @& String) : ByteArray
theorem Iterator.sizeOf_next_lt_of_hasNext (i : String.Iterator) (h : i.hasNext) : sizeOf i.next < sizeOf i := by
cases i; rename_i s pos; simp [Iterator.next, Iterator.sizeOf_eq]; simp [Iterator.hasNext] at h
exact Nat.sub_lt_sub_left h (String.lt_next s pos)
macro_rules | `(tactic| decreasing_trivial) => `(tactic| apply String.Iterator.sizeOf_next_lt_of_hasNext; assumption)
theorem Iterator.sizeOf_next_lt_of_atEnd (i : String.Iterator) (h : ¬ i.atEnd = true) : sizeOf i.next < sizeOf i :=
have h : i.hasNext := decide_eq_true <| Nat.gt_of_not_le <| mt decide_eq_true h
sizeOf_next_lt_of_hasNext i h
macro_rules | `(tactic| decreasing_trivial) => `(tactic| apply String.Iterator.sizeOf_next_lt_of_atEnd; assumption)
namespace Iterator
/-- Advance the given iterator until the predicate returns true or the end of the string is reached. -/
@[specialize] def find (it : Iterator) (p : Char → Bool) : Iterator :=
if it.atEnd then it
else if p it.curr then it
else find it.next p
@[specialize] def foldUntil (it : Iterator) (init : α) (f : α → Char → Option α) : α × Iterator :=
if it.atEnd then
(init, it)
else if let some a := f init it.curr then
foldUntil it.next a f
else
(init, it)
end Iterator
end String
|
596d5ea0de7f2a30faeaad488e3560c92275dc45 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/data/set/function.lean | 2bfbfe6d88859b34e59dd98ca4b78dbf3220d91b | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 12,421 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad, Andrew Zipperer, Haitao Zhang, Minchao Wu
Functions over sets.
-/
import data.set.basic logic.function
open function
namespace set
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
/- maps to -/
/-- `maps_to f a b` means that the image of `a` is contained in `b`. -/
@[reducible] def maps_to (f : α → β) (a : set α) (b : set β) : Prop := a ⊆ f ⁻¹' b
theorem maps_to' (f : α → β) (a : set α) (b : set β) : maps_to f a b ↔ f '' a ⊆ b :=
image_subset_iff.symm
theorem maps_to_of_eq_on {f1 f2 : α → β} {a : set α} {b : set β} (h₁ : eq_on f1 f2 a)
(h₂ : maps_to f1 a b) :
maps_to f2 a b :=
λ x h, by rw [mem_preimage, ← h₁ _ h]; exact h₂ h
theorem maps_to_comp {g : β → γ} {f : α → β} {a : set α} {b : set β} {c : set γ}
(h₁ : maps_to g b c) (h₂ : maps_to f a b) : maps_to (g ∘ f) a c :=
λ x h, h₁ (h₂ h)
theorem maps_to_univ (f : α → β) (a) : maps_to f a univ :=
λ x h, trivial
theorem maps_to_image (f : α → β) (a : set α) : maps_to f a (f '' a) :=
by rw maps_to'
theorem maps_to_range (f : α → β) (a : set α) : maps_to f a (range f) :=
by rw [← image_univ, maps_to']; exact image_subset _ (subset_univ _)
theorem image_subset_of_maps_to_of_subset {f : α → β} {a c : set α} {b : set β} (h₁ : maps_to f a b)
(h₂ : c ⊆ a) :
f '' c ⊆ b :=
λ y hy, let ⟨x, hx, heq⟩ := hy in
by rw [←heq]; apply h₁; apply h₂; assumption
theorem image_subset_of_maps_to {f : α → β} {a : set α} {b : set β} (h : maps_to f a b) :
f '' a ⊆ b :=
image_subset_of_maps_to_of_subset h (subset.refl _)
/- injectivity -/
/-- `f` is injective on `a` if the restriction of `f` to `a` is injective. -/
@[reducible] def inj_on (f : α → β) (a : set α) : Prop :=
∀⦃x1 x2 : α⦄, x1 ∈ a → x2 ∈ a → f x1 = f x2 → x1 = x2
theorem inj_on_empty (f : α → β) : inj_on f ∅ :=
λ _ _ h₁ _ _, false.elim h₁
theorem inj_on_of_eq_on {f1 f2 : α → β} {a : set α} (h₁ : eq_on f1 f2 a)
(h₂ : inj_on f1 a) :
inj_on f2 a :=
λ _ _ h₁' h₂' heq, by apply h₂ h₁' h₂'; rw [h₁, heq, ←h₁]; repeat {assumption}
theorem inj_on_of_inj_on_of_subset {f : α → β} {a b : set α} (h₁ : inj_on f b) (h₂ : a ⊆ b) :
inj_on f a :=
λ _ _ h₁' h₂' heq, h₁ (h₂ h₁') (h₂ h₂') heq
lemma injective_iff_inj_on_univ {f : α → β} : injective f ↔ inj_on f univ :=
iff.intro (λ h _ _ _ _ heq, h heq) (λ h _ _ heq, h trivial trivial heq)
lemma inj_on_of_injective {α β : Type*} {f : α → β} (s : set α) (h : injective f) :
inj_on f s :=
inj_on_of_inj_on_of_subset (injective_iff_inj_on_univ.1 h) (subset_univ s)
theorem inj_on_comp {g : β → γ} {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to f a b) (h₂ : inj_on g b) (h₃: inj_on f a) :
inj_on (g ∘ f) a :=
λ _ _ h₁' h₂' heq,
by apply h₃ h₁' h₂'; apply h₂; repeat {apply h₁, assumption}; assumption
lemma inj_on_comp_of_injective_left {g : β → γ} {f : α → β} {a : set α} (hg : injective g)
(hf : inj_on f a) : inj_on (g ∘ f) a :=
inj_on_comp (maps_to_univ _ _) (injective_iff_inj_on_univ.mp hg) hf
lemma inj_on_iff_injective {f : α → β} {s : set α} : inj_on f s ↔ injective (λ x:s, f x.1) :=
⟨λ H a b h, subtype.eq $ H a.2 b.2 h,
λ H a b as bs h, congr_arg subtype.val $ @H ⟨a, as⟩ ⟨b, bs⟩ h⟩
lemma inv_fun_on_image [inhabited α] {β : Type v} {s t : set α} {f : α → β}
(h : inj_on f s) (ht : t ⊆ s) : (inv_fun_on f s) '' (f '' t) = t :=
begin
have A : ∀z, z ∈ t → ((inv_fun_on f s) ∘ f) z = z := λz hz, inv_fun_on_eq' h (ht hz),
rw ← image_comp,
ext,
simp [A] {contextual := tt}
end
lemma inj_on_preimage {f : α → β} {B : set (set β)} (hB : B ⊆ powerset (range f)) :
inj_on (preimage f) B :=
begin
intros s t hs ht hst,
rw [←image_preimage_eq_of_subset (hB hs), ←image_preimage_eq_of_subset (hB ht), hst]
end
lemma subset_image_iff {s : set α} {t : set β} (f : α → β) :
t ⊆ f '' s ↔ ∃u⊆s, t = f '' u ∧ inj_on f u :=
begin
split,
{ assume h, choose g hg using h,
refine ⟨ {a | ∃ b (h : b ∈ t), g h = a }, _, set.ext $ assume b, ⟨_, _⟩, _⟩,
{ rintros a ⟨b, hb, rfl⟩,
exact (hg hb).1 },
{ rintros hb,
exact ⟨g hb, ⟨b, hb, rfl⟩, (hg hb).2⟩ },
{ rintros ⟨c, ⟨b, hb, rfl⟩, rfl⟩,
rwa (hg hb).2 },
{ rintros a₁ a₂ ⟨b₁, h₁, rfl⟩ ⟨b₂, h₂, rfl⟩ eq,
rw [(hg h₁).2, (hg h₂).2] at eq,
subst eq } },
{ rintros ⟨u, hu, rfl, _⟩,
exact image_subset _ hu }
end
lemma subset_range_iff {s : set β} (f : α → β) :
s ⊆ set.range f ↔ ∃u, s = f '' u ∧ inj_on f u :=
by rw [← image_univ, subset_image_iff]; simp
/- surjectivity -/
/-- `f` is surjective from `a` to `b` if `b` is contained in the image of `a`. -/
@[reducible] def surj_on (f : α → β) (a : set α) (b : set β) : Prop := b ⊆ f '' a
theorem surj_on_of_eq_on {f1 f2 : α → β} {a : set α} {b : set β} (h₁ : eq_on f1 f2 a)
(h₂ : surj_on f1 a b) :
surj_on f2 a b :=
λ _ h, let ⟨x, hx⟩ := h₂ h in
⟨x, hx.left, by rw [←h₁ _ hx.left]; exact hx.right⟩
theorem surj_on_comp {g : β → γ} {f : α → β} {a : set α} {b : set β} {c : set γ}
(h₁ : surj_on g b c) (h₂ : surj_on f a b) :
surj_on (g ∘ f) a c :=
λ z h, let ⟨y, hy⟩ := h₁ h, ⟨x, hx⟩ := h₂ hy.left in
⟨x, hx.left, calc g (f x) = g y : by rw [hx.right]
... = z : hy.right⟩
lemma surjective_iff_surj_on_univ {f : α → β} : surjective f ↔ surj_on f univ univ :=
by simp [surjective, surj_on, subset_def]
lemma surj_on_iff_surjective {f : α → β} {s : set α} : surj_on f s univ ↔ surjective (λ x:s, f x.1) :=
⟨λ H b, let ⟨a, as, e⟩ := @H b trivial in ⟨⟨a, as⟩, e⟩,
λ H b _, let ⟨⟨a, as⟩, e⟩ := H b in ⟨a, as, e⟩⟩
lemma image_eq_of_maps_to_of_surj_on {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to f a b) (h₂ : surj_on f a b) :
f '' a = b :=
eq_of_subset_of_subset (image_subset_of_maps_to h₁) h₂
/- bijectivity -/
/-- `f` is bijective from `a` to `b` if `f` is injective on `a` and `f '' a = b`. -/
@[reducible] def bij_on (f : α → β) (a : set α) (b : set β) : Prop :=
maps_to f a b ∧ inj_on f a ∧ surj_on f a b
lemma maps_to_of_bij_on {f : α → β} {a : set α} {b : set β} (h : bij_on f a b) :
maps_to f a b :=
h.left
lemma inj_on_of_bij_on {f : α → β} {a : set α} {b : set β} (h : bij_on f a b) :
inj_on f a :=
h.right.left
lemma surj_on_of_bij_on {f : α → β} {a : set α} {b : set β} (h : bij_on f a b) :
surj_on f a b :=
h.right.right
lemma bij_on.mk {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to f a b) (h₂ : inj_on f a) (h₃ : surj_on f a b) :
bij_on f a b :=
⟨h₁, h₂, h₃⟩
theorem bij_on_of_eq_on {f1 f2 : α → β} {a : set α} {b : set β} (h₁ : eq_on f1 f2 a)
(h₂ : bij_on f1 a b) : bij_on f2 a b :=
let ⟨map, inj, surj⟩ := h₂ in
⟨maps_to_of_eq_on h₁ map, inj_on_of_eq_on h₁ inj, surj_on_of_eq_on h₁ surj⟩
lemma image_eq_of_bij_on {f : α → β} {a : set α} {b : set β} (h : bij_on f a b) :
f '' a = b :=
image_eq_of_maps_to_of_surj_on h.left h.right.right
theorem bij_on_comp {g : β → γ} {f : α → β} {a : set α} {b : set β} {c : set γ}
(h₁ : bij_on g b c) (h₂: bij_on f a b) :
bij_on (g ∘ f) a c :=
let ⟨gmap, ginj, gsurj⟩ := h₁, ⟨fmap, finj, fsurj⟩ := h₂ in
⟨maps_to_comp gmap fmap, inj_on_comp fmap ginj finj, surj_on_comp gsurj fsurj⟩
lemma bijective_iff_bij_on_univ {f : α → β} : bijective f ↔ bij_on f univ univ :=
iff.intro
(λ h, let ⟨inj, surj⟩ := h in
⟨maps_to_univ f _, iff.mp injective_iff_inj_on_univ inj, iff.mp surjective_iff_surj_on_univ surj⟩)
(λ h, let ⟨map, inj, surj⟩ := h in
⟨iff.mpr injective_iff_inj_on_univ inj, iff.mpr surjective_iff_surj_on_univ surj⟩)
/- left inverse -/
/-- `g` is a left inverse to `f` on `a` means that `g (f x) = x` for all `x ∈ a`. -/
@[reducible] def left_inv_on (g : β → α) (f : α → β) (a : set α) : Prop :=
∀ x ∈ a, g (f x) = x
theorem left_inv_on_of_eq_on_left {g1 g2 : β → α} {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to f a b) (h₂ : eq_on g1 g2 b) (h₃ : left_inv_on g1 f a) : left_inv_on g2 f a :=
λ x h,
calc
g2 (f x) = g1 (f x) : eq.symm $ h₂ _ (h₁ h)
... = x : h₃ _ h
theorem left_inv_on_of_eq_on_right {g : β → α} {f1 f2 : α → β} {a : set α}
(h₁ : eq_on f1 f2 a) (h₂ : left_inv_on g f1 a) : left_inv_on g f2 a :=
λ x h,
calc
g (f2 x) = g (f1 x) : congr_arg g (h₁ _ h).symm
... = x : h₂ _ h
theorem inj_on_of_left_inv_on {g : β → α} {f : α → β} {a : set α} (h : left_inv_on g f a) :
inj_on f a :=
λ x₁ x₂ h₁ h₂ heq,
calc
x₁ = g (f x₁) : eq.symm $ h _ h₁
... = g (f x₂) : congr_arg g heq
... = x₂ : h _ h₂
theorem left_inv_on_comp {f' : β → α} {g' : γ → β} {g : β → γ} {f : α → β}
{a : set α} {b : set β} (h₁ : maps_to f a b)
(h₂ : left_inv_on f' f a) (h₃ : left_inv_on g' g b) : left_inv_on (f' ∘ g') (g ∘ f) a :=
λ x h,
calc
(f' ∘ g') ((g ∘ f) x) = f' (f x) : congr_arg f' (h₃ _ (h₁ h))
... = x : h₂ _ h
/- right inverse -/
/-- `g` is a right inverse to `f` on `b` if `f (g x) = x` for all `x ∈ b`. -/
@[reducible] def right_inv_on (g : β → α) (f : α → β) (b : set β) : Prop :=
left_inv_on f g b
theorem right_inv_on_of_eq_on_left {g1 g2 : β → α} {f : α → β} {a : set α} {b : set β}
(h₁ : eq_on g1 g2 b) (h₂ : right_inv_on g1 f b) : right_inv_on g2 f b :=
left_inv_on_of_eq_on_right h₁ h₂
theorem right_inv_on_of_eq_on_right {g : β → α} {f1 f2 : α → β} {a : set α} {b : set β}
(h₁ : maps_to g b a) (h₂ : eq_on f1 f2 a) (h₃ : right_inv_on g f1 b) : right_inv_on g f2 b :=
left_inv_on_of_eq_on_left h₁ h₂ h₃
theorem surj_on_of_right_inv_on {g : β → α} {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to g b a) (h₂ : right_inv_on g f b) :
surj_on f a b :=
λ y h, ⟨g y, h₁ h, h₂ _ h⟩
theorem right_inv_on_comp {f' : β → α} {g' : γ → β} {g : β → γ} {f : α → β}
{c : set γ} {b : set β} (g'cb : maps_to g' c b)
(h₁ : right_inv_on f' f b) (h₂ : right_inv_on g' g c) : right_inv_on (f' ∘ g') (g ∘ f) c :=
left_inv_on_comp g'cb h₂ h₁
theorem right_inv_on_of_inj_on_of_left_inv_on {f : α → β} {g : β → α} {a : set α} {b : set β}
(h₁ : maps_to f a b) (h₂ : maps_to g b a) (h₃ : inj_on f a) (h₄ : left_inv_on f g b) :
right_inv_on f g a :=
λ x h, h₃ (h₂ $ h₁ h) h (h₄ _ (h₁ h))
theorem eq_on_of_left_inv_of_right_inv {g₁ g₂ : β → α} {f : α → β} {a : set α} {b : set β}
(h₁ : maps_to g₂ b a) (h₂ : left_inv_on g₁ f a) (h₃ : right_inv_on g₂ f b) : eq_on g₁ g₂ b :=
λ y h,
calc
g₁ y = (g₁ ∘ f ∘ g₂) y : congr_arg g₁ (h₃ _ h).symm
... = g₂ y : h₂ _ (h₁ h)
theorem left_inv_on_of_surj_on_right_inv_on {f : α → β} {g : β → α} {a : set α} {b : set β}
(h₁ : surj_on f a b) (h₂ : right_inv_on f g a) :
left_inv_on f g b :=
λ y h, let ⟨x, hx, heq⟩ := h₁ h in
calc
(f ∘ g) y = (f ∘ g ∘ f) x : congr_arg (f ∘ g) heq.symm
... = f x : congr_arg f (h₂ _ hx)
... = y : heq
/- inverses -/
/-- `g` is an inverse to `f` viewed as a map from `a` to `b` -/
@[reducible] def inv_on (g : β → α) (f : α → β) (a : set α) (b : set β) : Prop :=
left_inv_on g f a ∧ right_inv_on g f b
theorem bij_on_of_inv_on {g : β → α} {f : α → β} {a : set α} {b : set β} (h₁ : maps_to f a b)
(h₂ : maps_to g b a) (h₃ : inv_on g f a b) : bij_on f a b :=
⟨h₁, inj_on_of_left_inv_on h₃.left, surj_on_of_right_inv_on h₂ h₃.right⟩
lemma range_restrict {α : Type*} {β : Type*} (f : α → β) (p : set α) :
range (restrict f p) = f '' (p : set α) :=
by { ext x, simp [restrict], refl }
end set
|
5a7016a0e5c1ea1a5fee382a07edc2c21d6c84d8 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /src/Init/Lean/Util/Path.lean | d032ba6fea1d03806d6ea648bb60d21f4f17506b | [
"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 | 4,980 | 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, Sebastian Ullrich
Management of the Lean search path (`LEAN_PATH`), which is a list of
`pkg=path` mappings from package name to root path. An import `A.B.C`
given an `A=path` entry resolves to `path/B/C.olean`. A package-only
import `A` is normalized to `A.Default`. For the input file, we also
need the reverse direction of finding a (unique) module path from a
file path.
-/
prelude
import Init.System.IO
import Init.System.FilePath
import Init.Data.Array
import Init.Data.List.Control
import Init.Data.HashMap
import Init.Data.Nat.Control
import Init.Lean.Data.Name
namespace Lean
open System.FilePath (pathSeparator extSeparator)
private def pathSep : String := toString pathSeparator
def realPathNormalized (fname : String) : IO String := do
fname ← IO.realPath fname;
pure (System.FilePath.normalizePath fname)
abbrev SearchPath := HashMap String String
def mkSearchPathRef : IO (IO.Ref SearchPath) :=
IO.mkRef ∅
@[init mkSearchPathRef]
constant searchPathRef : IO.Ref SearchPath := arbitrary _
def parseSearchPath (path : String) (sp : SearchPath := ∅) : IO SearchPath := do
let ps := System.FilePath.splitSearchPath path;
sp ← ps.foldlM (fun (sp : SearchPath) s => match s.splitOn "=" with
| [pkg, path] => do
condM (IO.isDir path)
(pure $ sp.insert pkg path)
(throw $ IO.userError $ "invalid search path, '" ++ path ++ "' is not a directory")
| _ => throw $ IO.userError $ "ill-formed search path entry '" ++ s ++ "', should be of form 'pkg=path'")
sp;
pure sp
def getBuiltinSearchPath : IO SearchPath := do
appDir ← IO.appDir;
let libDir := appDir ++ pathSep ++ ".." ++ pathSep ++ "src" ++ pathSep ++ "Init";
libDirExists ← IO.isDir libDir;
if libDirExists then do
path ← realPathNormalized libDir;
pure $ HashMap.empty.insert "Init" path
else do
let installedLibDir := appDir ++ pathSep ++ ".." ++ pathSep ++ "lib" ++ pathSep ++ "lean" ++ pathSep ++ "Init";
installedLibDirExists ← IO.isDir installedLibDir;
if installedLibDirExists then do
path ← realPathNormalized installedLibDir;
pure $ HashMap.empty.insert "Init" path
else
pure ∅
def addSearchPathFromEnv (sp : SearchPath) : IO SearchPath := do
val ← IO.getEnv "LEAN_PATH";
match val with
| none => pure sp
| some val => parseSearchPath val sp
@[export lean_init_search_path]
def initSearchPath (path : Option String := "") : IO Unit :=
match path with
| some path => parseSearchPath path >>= searchPathRef.set
| none => do
sp ← getBuiltinSearchPath;
sp ← addSearchPathFromEnv sp;
searchPathRef.set sp
def modPathToFilePath : Name → String
| Name.str Name.anonymous h _ => h
| Name.str p h _ => modPathToFilePath p ++ pathSep ++ h
| Name.anonymous => ""
| Name.num p _ _ => panic! "ill-formed import"
/- Given `A.B.C, return ("A", `B.C). -/
def splitAtRoot : Name → String × Name
| Name.str Name.anonymous s _ => (s, Name.anonymous)
| Name.str n s _ =>
let (pkg, path) := splitAtRoot n;
(pkg, mkNameStr path s)
| _ => panic! "ill-formed import"
def findOLean (mod : Name) : IO String := do
sp ← searchPathRef.get;
let (pkg, path) := splitAtRoot mod;
some root ← pure $ sp.find? pkg
| throw $ IO.userError $ "unknown package '" ++ pkg ++ "'";
let fname := root ++ pathSep ++ modPathToFilePath path ++ ".olean";
pure fname
def findAtSearchPath (fname : String) : IO (Option (String × String)) := do
fname ← realPathNormalized fname;
sp ← searchPathRef.get;
results ← sp.foldM (fun results pkg path => do
path ← realPathNormalized path;
pure $ if String.isPrefixOf path fname then (pkg, path) :: results else results) [];
match results with
| [res] => pure res
| [] => pure none
| _ => throw (IO.userError ("file '" ++ fname ++ "' is contained in multiple packages: " ++ ", ".intercalate (results.map Prod.fst)))
@[export lean_module_name_of_file]
def moduleNameOfFileName (fname : String) : IO (Option Name) := do
some (pkg, path) ← findAtSearchPath fname
| pure none;
fname ← realPathNormalized fname;
let fnameSuffix := fname.drop path.length;
let fnameSuffix := if fnameSuffix.get 0 == pathSeparator then fnameSuffix.drop 1 else fnameSuffix;
some extPos ← pure (fnameSuffix.revPosOf '.')
| throw (IO.userError ("failed to convert file '" ++ fname ++ "' to module name, extension is missing"));
let modNameStr := fnameSuffix.extract 0 extPos;
let extStr := fnameSuffix.extract (extPos + 1) fnameSuffix.bsize;
let parts := modNameStr.splitOn pathSep;
let modName := parts.foldl mkNameStr pkg;
pure modName
-- normalize `A` to `A.Default`
@[export lean_normalize_module_name]
def normalizeModuleName : Name → Name
| Name.str Name.anonymous pkg _ => mkNameSimple pkg ++ "Default"
| m => m
end Lean
|
b9dd5a9798eee4ceeebc3c980d937cde1cdcd318 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Server/Utils.lean | 4827739d4205ddcbea5a90394c100ab05f3f5f89 | [
"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,042 | lean | /-
Copyright (c) 2020 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Marc Huisinga
-/
import Lean.Data.Lsp.Communication
import Lean.Data.Lsp.Diagnostics
import Lean.Data.Lsp.Extra
import Lean.Data.Lsp.TextSync
import Lean.Server.InfoUtils
namespace IO
def throwServerError (err : String) : IO α :=
throw (userError err)
namespace FS.Stream
/-- Chains two streams by creating a new stream s.t. writing to it
just writes to `a` but reading from it also duplicates the read output
into `b`, c.f. `a | tee b` on Unix.
NB: if `a` is written to but this stream is never read from,
the output will *not* be duplicated. Use this if you only care
about the data that was actually read. -/
def chainRight (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream :=
{ a with
flush := a.flush *> b.flush
read := fun sz => do
let bs ← a.read sz
b.write bs
if flushEagerly then b.flush
pure bs
getLine := do
let ln ← a.getLine
b.putStr ln
if flushEagerly then b.flush
pure ln }
/-- Like `tee a | b` on Unix. See `chainOut`. -/
def chainLeft (a : Stream) (b : Stream) (flushEagerly : Bool := false) : Stream :=
{ b with
flush := a.flush *> b.flush
write := fun bs => do
a.write bs
if flushEagerly then a.flush
b.write bs
putStr := fun s => do
a.putStr s
if flushEagerly then a.flush
b.putStr s }
/-- Prefixes all written outputs with `pre`. -/
def withPrefix (a : Stream) (pre : String) : Stream :=
{ a with
write := fun bs => do
a.putStr pre
a.write bs
putStr := fun s =>
a.putStr (pre ++ s) }
end FS.Stream
end IO
namespace Lean.Server
structure DocumentMeta where
uri : Lsp.DocumentUri
version : Nat
text : FileMap
deriving Inhabited
def DocumentMeta.mkInputContext (doc : DocumentMeta) : Parser.InputContext where
input := doc.text.source
fileName := (System.Uri.fileUriToPath? doc.uri).getD doc.uri |>.toString
fileMap := doc.text
def replaceLspRange (text : FileMap) (r : Lsp.Range) (newText : String) : FileMap :=
let start := text.lspPosToUtf8Pos r.start
let «end» := text.lspPosToUtf8Pos r.«end»
let pre := text.source.extract 0 start
let post := text.source.extract «end» text.source.endPos
(pre ++ newText ++ post).toFileMap
open IO
/-- Duplicates an I/O stream to a log file `fName` in LEAN_SERVER_LOG_DIR
if that envvar is set. -/
def maybeTee (fName : String) (isOut : Bool) (h : FS.Stream) : IO FS.Stream := do
match (← IO.getEnv "LEAN_SERVER_LOG_DIR") with
| none => pure h
| some logDir =>
IO.FS.createDirAll logDir
let hTee ← FS.Handle.mk (System.mkFilePath [logDir, fName]) FS.Mode.write
let hTee := FS.Stream.ofHandle hTee
pure $ if isOut then
hTee.chainLeft h true
else
h.chainRight hTee true
open Lsp
/-- Returns the document contents with the change applied. -/
def applyDocumentChange (oldText : FileMap) : (change : Lsp.TextDocumentContentChangeEvent) → FileMap
| TextDocumentContentChangeEvent.rangeChange (range : Range) (newText : String) =>
replaceLspRange oldText range newText
| TextDocumentContentChangeEvent.fullChange (newText : String) =>
newText.toFileMap
/-- Returns the document contents with all changes applied. -/
def foldDocumentChanges (changes : Array Lsp.TextDocumentContentChangeEvent) (oldText : FileMap) : FileMap :=
changes.foldl applyDocumentChange oldText
def publishDiagnostics (m : DocumentMeta) (diagnostics : Array Lsp.Diagnostic) (hOut : FS.Stream) : IO Unit :=
hOut.writeLspNotification {
method := "textDocument/publishDiagnostics"
param := {
uri := m.uri
version? := m.version
diagnostics := diagnostics
: PublishDiagnosticsParams
}
}
def publishProgress (m : DocumentMeta) (processing : Array LeanFileProgressProcessingInfo) (hOut : FS.Stream) : IO Unit :=
hOut.writeLspNotification {
method := "$/lean/fileProgress"
param := {
textDocument := { uri := m.uri, version? := m.version }
processing
: LeanFileProgressParams
}
}
def publishProgressAtPos (m : DocumentMeta) (pos : String.Pos) (hOut : FS.Stream) (kind : LeanFileProgressKind := LeanFileProgressKind.processing) : IO Unit :=
publishProgress m #[{ range := ⟨m.text.utf8PosToLspPos pos, m.text.utf8PosToLspPos m.text.source.endPos⟩, kind := kind }] hOut
def publishProgressDone (m : DocumentMeta) (hOut : FS.Stream) : IO Unit :=
publishProgress m #[] hOut
-- TODO: should return a request ID (or task?) when we add response handling
def applyWorkspaceEdit (params : ApplyWorkspaceEditParams) (hOut : FS.Stream) : IO Unit :=
hOut.writeLspRequest ⟨"workspace/applyEdit", "workspace/applyEdit", params⟩
end Lean.Server
def String.Range.toLspRange (text : Lean.FileMap) (r : String.Range) : Lean.Lsp.Range :=
⟨text.utf8PosToLspPos r.start, text.utf8PosToLspPos r.stop⟩
|
e2c8a3cb21d3f37ed7080510abdb95e90b173f02 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/palindrome.lean | aab0958efe135ef679315bc58fab08a190d4c926 | [
"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 | 2,008 | lean | /-
Copyright (c) 2020 Google LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Wong
-/
import data.list.basic
/-!
# Palindromes
This module defines *palindromes*, lists which are equal to their reverse.
The main result is the `palindrome` inductive type, and its associated `palindrome.rec_on` induction
principle. Also provided are conversions to and from other equivalent definitions.
## References
* [Pierre Castéran, *On palindromes*][casteran]
[casteran]: https://www.labri.fr/perso/casteran/CoqArt/inductive-prop-chap/palindrome.html
## Tags
palindrome, reverse, induction
-/
open list
variables {α : Type*}
/--
`palindrome l` asserts that `l` is a palindrome. This is defined inductively:
* The empty list is a palindrome;
* A list with one element is a palindrome;
* Adding the same element to both ends of a palindrome results in a bigger palindrome.
-/
inductive palindrome : list α → Prop
| nil : palindrome []
| singleton : ∀ x, palindrome [x]
| cons_concat : ∀ x {l}, palindrome l → palindrome (x :: (l ++ [x]))
namespace palindrome
lemma reverse_eq {l : list α} (p : palindrome l) : reverse l = l :=
palindrome.rec_on p rfl (λ _, rfl) (λ x l p h, by simp [h])
lemma of_reverse_eq {l : list α} : reverse l = l → palindrome l :=
begin
refine bidirectional_rec_on l (λ _, palindrome.nil) (λ a _, palindrome.singleton a) _,
intros x l y hp hr,
rw [reverse_cons, reverse_append] at hr,
rw head_eq_of_cons_eq hr,
have : palindrome l, from hp (append_inj_left' (tail_eq_of_cons_eq hr) rfl),
exact palindrome.cons_concat x this
end
lemma iff_reverse_eq {l : list α} : palindrome l ↔ reverse l = l :=
iff.intro reverse_eq of_reverse_eq
lemma append_reverse (l : list α) : palindrome (l ++ reverse l) :=
by { apply of_reverse_eq, rw [reverse_append, reverse_reverse] }
instance [decidable_eq α] (l : list α) : decidable (palindrome l) :=
decidable_of_iff' _ iff_reverse_eq
end palindrome
|
df80946bf162635b477e11689202ca7c55f261a4 | febba19712b2aefe4d6c7fb0230b8d7e272c98c4 | /src/formula.lean | c4abb5ee372be17aa5754a789f85f70637ea8525 | [] | no_license | hcheval/formalized-proof-mining | e6eb980feb644ae269f43d9af93ac584b7a47a17 | 216cc73fccd84900a1ba7eaae5f73732496d6afe | refs/heads/master | 1,689,621,410,792 | 1,629,912,272,000 | 1,629,912,272,000 | 399,855,572 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,928 | lean | import types
inductive formula (ι : Type) (gri : ground_interpretation ι)
| prime (p : Prop) [decidable p] : formula
| conjunction : formula → formula → formula
| disjunction : formula → formula → formula
| implication : formula → formula → formula
| universal {σ : type ι gri} : (∥σ∥ → formula) → formula
| existential {σ : type ι gri} : (∥σ∥ → formula) → formula
inductive restricted_formula (ι : Type) (gri : ground_interpretation ι)
section basics
namespace formula
infixr `⟹` : 45 := implication
infix `⋀` : 50 := conjunction
infix `⋁` : 50 := disjunction
notation `universal'` := @universal _ _
notation `existential'` := @existential _ _
-- how does this actually work?
notation `∀∀` binders `,` r:(scoped A, universal A) := r
notation `∃∃` binders `,` r:(scoped A, existential A) := r
@[reducible, simp] def falsum (ι : Type) (gri : ground_interpretation ι) : formula ι gri := @prime ι gri false _
@[reducible, simp] def falsum' {ι : Type} {gri : ground_interpretation ι} : formula ι gri := falsum _ _
variables {ι : Type} {gri : ground_interpretation ι}
local notation `𝕋` := type ι gri
local notation `𝔽` := formula ι gri
instance : has_bot 𝔽 := ⟨falsum ι gri⟩
def negation (A : 𝔽) := A ⟹ falsum ι gri
prefix `∼` : 90 := negation
def equivalence (A B : 𝔽) := A ⟹ B ⋀ B ⟹ A
infixl `⇔` : 15 := equivalence
@[simp]
def eqext (greq : Π {i : ι}, ∥𝕏 i∥ → ∥𝕏 i∥ → 𝔽) : Π {σ : 𝕋}, ∥σ∥ → ∥σ∥ → 𝔽
| 𝕆 x y := prime $ x = y
| (𝕏 i) x y := greq x y
| (σ ↣ τ) x y := ∀∀ z : ∥σ∥ , (eqext (x z) (y z))
| (σ ⊗ τ) x y := eqext x.1 y.1 ⋀ eqext x.2 y.2
@[simp, pp_nodot]
def interpret : 𝔽 → Prop
| (@prime _ _ p _) := p
| (A ⋀ B) := A.interpret ∧ B.interpret
| (A ⋁ B) := A.interpret ∨ B.interpret
| (A ⟹ B) := A.interpret → B.interpret
| (universal' σ A) := ∀ x : ∥σ∥, (A x).interpret
| (existential' σ A) := ∃ x : ∥σ∥, (A x).interpret
-- @[simp]
-- lemma prime_interpret (p : Prop) [decidable p] : ∥(prime p : 𝔽)∥ ↔ p :=
-- by split; intros; simpa
notation `∥` A `∥` := interpret A
end formula
end basics
section kinds_of_formulas
variables {ι : Type} {gri : ground_interpretation ι}
local notation `𝕋` := type ι gri
local notation `𝔽` := formula ι gri
variables {greq : Π {i : ι}, ∥𝕏 i // gri∥ → ∥𝕏 i // gri∥ → 𝔽}
namespace formula
@[simp]
def is_qf : 𝔽 → Prop
| (@prime _ _ _ _) := true
| (A ⋀ B) := and A.is_qf B.is_qf
| (A ⋁ B) := and A.is_qf B.is_qf
| (A ⟹ B) := and A.is_qf B.is_qf
| (universal' σ A) := false
| (existential' σ A) := false
@[simp]
def is_qf_disj_free : 𝔽 → Prop
| (@prime _ _ _ _) := true
| (A ⋀ B) := and A.is_qf_disj_free B.is_qf_disj_free
| (A ⋁ B) := false
| (A ⟹ B) := and A.is_qf_disj_free B.is_qf_disj_free
| (universal' σ A) := false
| (existential' σ A) := false
inductive purely_univ : 𝔽 → Type
| of_qf (A : 𝔽) : A.is_qf → purely_univ A
| of_univ {σ : 𝕋} (A : ∥σ∥ → 𝔽) : (∀ x : ∥σ∥, purely_univ (A x)) → purely_univ (universal' σ A)
def is_purely_univ : 𝔽 → Prop
| (@prime _ _ _ _) := true
| (A ⋀ B) := A.is_qf ∧ B.is_qf
| (A ⋁ B) := A.is_qf ∧ B.is_qf
| (A ⟹ B) := A.is_qf ∧ B.is_qf
| (universal' σ A) := ∀ x, is_purely_univ (A x)
| (existential' σ A) := false
inductive purely_univ_disj_free : 𝔽 → Type
| of_qf_disj_free (A : 𝔽) : A.is_qf_disj_free → purely_univ_disj_free A
| of_univ {σ : 𝕋} (A : ∥σ∥ → 𝔽) : (∀ x : ∥σ∥, purely_univ_disj_free (A x)) → purely_univ_disj_free (universal' σ A)
end formula
end kinds_of_formulas
|
5bb25786fc4a6d5cccf98483f18d2311e735f9c4 | ecea1a568b8a7bdfef1206845f7dc2e62b85b6b3 | /library/init/data/string/lemmas.lean | ab943f051dfb070830428c182e7beaa6f3716082 | [
"Apache-2.0"
] | permissive | ndcroos/lean | b1b6de42ee4d3ded05829373e505e09614d4acdf | 6919d231413705c892eec02bdacd7d7d916524c8 | refs/heads/master | 1,625,911,732,192 | 1,507,549,407,000 | 1,507,549,407,000 | 106,106,359 | 0 | 0 | null | 1,507,388,410,000 | 1,507,388,410,000 | null | UTF-8 | Lean | false | false | 989 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import init.meta
namespace string
lemma empty_ne_str (c : char) (s : string) : empty ≠ str s c :=
begin cases s, unfold str empty, intro h, injection h, contradiction end
lemma str_ne_empty (c : char) (s : string) : str s c ≠ empty :=
begin cases s, unfold str empty, intro h, injection h, contradiction end
lemma str_ne_str_left {c₁ c₂ : char} (s₁ s₂ : string) : c₁ ≠ c₂ → str s₁ c₁ ≠ str s₂ c₂ :=
begin cases s₁, cases s₂, intros h₁ h₂, unfold str at h₂, injection h₂ with h, injection h, contradiction end
lemma str_ne_str_right (c₁ c₂ : char) {s₁ s₂ : string} : s₁ ≠ s₂ → str s₁ c₁ ≠ str s₂ c₂ :=
begin cases s₁, cases s₂, intros h₁ h₂, unfold str at h₂, injection h₂ with h, injection h, subst data, contradiction end
end string
|
4e3e48eb92b8f3933b3e855af820bac759254341 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Elab/Tactic/Split.lean | 5615c48a0aa7a6e402b675f84b050eb42b7deb5a | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 1,457 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Split
import Lean.Elab.Tactic.Basic
import Lean.Elab.Tactic.Location
namespace Lean.Elab.Tactic
open Meta
@[builtinTactic Lean.Parser.Tactic.split] def evalSplit : Tactic := fun stx => do
unless stx[1].isNone do
throwError "'split' tactic, term to split is not supported yet"
let loc := expandOptLocation stx[2]
match loc with
| Location.targets hyps simplifyTarget =>
if (hyps.size > 0 && simplifyTarget) || hyps.size > 1 then
throwErrorAt stx[2] "'split' tactic failed, select a single target to split"
if simplifyTarget then
liftMetaTactic fun mvarId => do
let some mvarIds ← splitTarget? mvarId | Meta.throwTacticEx `split mvarId ""
return mvarIds
else
let fvarId ← getFVarId hyps[0]!
liftMetaTactic fun mvarId => do
let some mvarIds ← splitLocalDecl? mvarId fvarId | Meta.throwTacticEx `split mvarId ""
return mvarIds
| Location.wildcard =>
liftMetaTactic fun mvarId => do
let fvarIds ← mvarId.getNondepPropHyps
for fvarId in fvarIds do
if let some mvarIds ← splitLocalDecl? mvarId fvarId then
return mvarIds
let some mvarIds ← splitTarget? mvarId | Meta.throwTacticEx `split mvarId ""
return mvarIds
end Lean.Elab.Tactic
|
ff1f4b7eff067639633bfc997e796c855d819b49 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/multiset/locally_finite.lean | 8873513dbf22368fa3a802f1496dc299489747ea | [
"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 | 8,413 | 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 data.finset.locally_finite
/-!
# Intervals as multisets
This file provides basic results about all the `multiset.Ixx`, which are defined in
`order.locally_finite`.
-/
variables {α : Type*}
namespace multiset
section preorder
variables [preorder α] [locally_finite_order α] {a b c : α}
lemma nodup_Icc : (Icc a b).nodup := finset.nodup _
lemma nodup_Ico : (Ico a b).nodup := finset.nodup _
lemma nodup_Ioc : (Ioc a b).nodup := finset.nodup _
lemma nodup_Ioo : (Ioo a b).nodup := finset.nodup _
@[simp] lemma Icc_eq_zero_iff : Icc a b = 0 ↔ ¬a ≤ b :=
by rw [Icc, finset.val_eq_zero, finset.Icc_eq_empty_iff]
@[simp] lemma Ico_eq_zero_iff : Ico a b = 0 ↔ ¬a < b :=
by rw [Ico, finset.val_eq_zero, finset.Ico_eq_empty_iff]
@[simp] lemma Ioc_eq_zero_iff : Ioc a b = 0 ↔ ¬a < b :=
by rw [Ioc, finset.val_eq_zero, finset.Ioc_eq_empty_iff]
@[simp] lemma Ioo_eq_zero_iff [densely_ordered α] : Ioo a b = 0 ↔ ¬a < b :=
by rw [Ioo, finset.val_eq_zero, finset.Ioo_eq_empty_iff]
alias Icc_eq_zero_iff ↔ _ Icc_eq_zero
alias Ico_eq_zero_iff ↔ _ Ico_eq_zero
alias Ioc_eq_zero_iff ↔ _ Ioc_eq_zero
@[simp] lemma Ioo_eq_zero (h : ¬a < b) : Ioo a b = 0 :=
eq_zero_iff_forall_not_mem.2 $ λ x hx, h ((mem_Ioo.1 hx).1.trans (mem_Ioo.1 hx).2)
@[simp] lemma Icc_eq_zero_of_lt (h : b < a) : Icc a b = 0 := Icc_eq_zero h.not_le
@[simp] lemma Ico_eq_zero_of_le (h : b ≤ a) : Ico a b = 0 := Ico_eq_zero h.not_lt
@[simp] lemma Ioc_eq_zero_of_le (h : b ≤ a) : Ioc a b = 0 := Ioc_eq_zero h.not_lt
@[simp] lemma Ioo_eq_zero_of_le (h : b ≤ a) : Ioo a b = 0 := Ioo_eq_zero h.not_lt
variables (a)
@[simp] lemma Ico_self : Ico a a = 0 := by rw [Ico, finset.Ico_self, finset.empty_val]
@[simp] lemma Ioc_self : Ioc a a = 0 := by rw [Ioc, finset.Ioc_self, finset.empty_val]
@[simp] lemma Ioo_self : Ioo a a = 0 := by rw [Ioo, finset.Ioo_self, finset.empty_val]
variables {a b c}
lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := finset.left_mem_Icc
lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := finset.left_mem_Ico
lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := finset.right_mem_Icc
lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := finset.right_mem_Ioc
@[simp] lemma left_not_mem_Ioc : a ∉ Ioc a b := finset.left_not_mem_Ioc
@[simp] lemma left_not_mem_Ioo : a ∉ Ioo a b := finset.left_not_mem_Ioo
@[simp] lemma right_not_mem_Ico : b ∉ Ico a b := finset.right_not_mem_Ico
@[simp] lemma right_not_mem_Ioo : b ∉ Ioo a b := finset.right_not_mem_Ioo
lemma Ico_filter_lt_of_le_left [decidable_pred (< c)] (hca : c ≤ a) :
(Ico a b).filter (λ x, x < c) = ∅ :=
by { rw [Ico, ←finset.filter_val, finset.Ico_filter_lt_of_le_left hca], refl }
lemma Ico_filter_lt_of_right_le [decidable_pred (< c)] (hbc : b ≤ c) :
(Ico a b).filter (λ x, x < c) = Ico a b :=
by rw [Ico, ←finset.filter_val, finset.Ico_filter_lt_of_right_le hbc]
lemma Ico_filter_lt_of_le_right [decidable_pred (< c)] (hcb : c ≤ b) :
(Ico a b).filter (λ x, x < c) = Ico a c :=
by { rw [Ico, ←finset.filter_val, finset.Ico_filter_lt_of_le_right hcb], refl }
lemma Ico_filter_le_of_le_left [decidable_pred ((≤) c)] (hca : c ≤ a) :
(Ico a b).filter (λ x, c ≤ x) = Ico a b :=
by rw [Ico, ←finset.filter_val, finset.Ico_filter_le_of_le_left hca]
lemma Ico_filter_le_of_right_le [decidable_pred ((≤) b)] :
(Ico a b).filter (λ x, b ≤ x) = ∅ :=
by { rw [Ico, ←finset.filter_val, finset.Ico_filter_le_of_right_le], refl }
lemma Ico_filter_le_of_left_le [decidable_pred ((≤) c)] (hac : a ≤ c) :
(Ico a b).filter (λ x, c ≤ x) = Ico c b :=
by { rw [Ico, ←finset.filter_val, finset.Ico_filter_le_of_left_le hac], refl }
end preorder
section partial_order
variables [partial_order α] [locally_finite_order α] {a b : α}
@[simp] lemma Icc_self (a : α) : Icc a a = {a} := by rw [Icc, finset.Icc_self, finset.singleton_val]
lemma Ico_cons_right (h : a ≤ b) : b ::ₘ (Ico a b) = Icc a b :=
by { classical,
rw [Ico, ←finset.insert_val_of_not_mem right_not_mem_Ico, finset.Ico_insert_right h], refl }
lemma Ioo_cons_left (h : a < b) : a ::ₘ (Ioo a b) = Ico a b :=
by { classical,
rw [Ioo, ←finset.insert_val_of_not_mem left_not_mem_Ioo, finset.Ioo_insert_left h], refl }
lemma Ico_disjoint_Ico {a b c d : α} (h : b ≤ c) : (Ico a b).disjoint (Ico c d) :=
λ x hab hbc, by { rw mem_Ico at hab hbc, exact hab.2.not_le (h.trans hbc.1) }
@[simp] lemma Ico_inter_Ico_of_le [decidable_eq α] {a b c d : α} (h : b ≤ c) :
Ico a b ∩ Ico c d = 0 :=
multiset.inter_eq_zero_iff_disjoint.2 $ Ico_disjoint_Ico h
lemma Ico_filter_le_left {a b : α} [decidable_pred (≤ a)] (hab : a < b) :
(Ico a b).filter (λ x, x ≤ a) = {a} :=
by { rw [Ico, ←finset.filter_val, finset.Ico_filter_le_left hab], refl }
lemma card_Ico_eq_card_Icc_sub_one (a b : α) : (Ico a b).card = (Icc a b).card - 1 :=
finset.card_Ico_eq_card_Icc_sub_one _ _
lemma card_Ioc_eq_card_Icc_sub_one (a b : α) : (Ioc a b).card = (Icc a b).card - 1 :=
finset.card_Ioc_eq_card_Icc_sub_one _ _
lemma card_Ioo_eq_card_Ico_sub_one (a b : α) : (Ioo a b).card = (Ico a b).card - 1 :=
finset.card_Ioo_eq_card_Ico_sub_one _ _
lemma card_Ioo_eq_card_Icc_sub_two (a b : α) : (Ioo a b).card = (Icc a b).card - 2 :=
finset.card_Ioo_eq_card_Icc_sub_two _ _
end partial_order
section linear_order
variables [linear_order α] [locally_finite_order α] {a b c d : α}
lemma Ico_subset_Ico_iff {a₁ b₁ a₂ b₂ : α} (h : a₁ < b₁) :
Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ :=
finset.Ico_subset_Ico_iff h
lemma Ico_add_Ico_eq_Ico {a b c : α} (hab : a ≤ b) (hbc : b ≤ c) :
Ico a b + Ico b c = Ico a c :=
by rw [add_eq_union_iff_disjoint.2 (Ico_disjoint_Ico le_rfl), Ico, Ico, Ico, ←finset.union_val,
finset.Ico_union_Ico_eq_Ico hab hbc]
lemma Ico_inter_Ico : Ico a b ∩ Ico c d = Ico (max a c) (min b d) :=
by rw [Ico, Ico, Ico, ←finset.inter_val, finset.Ico_inter_Ico]
@[simp] lemma Ico_filter_lt (a b c : α) : (Ico a b).filter (λ x, x < c) = Ico a (min b c) :=
by rw [Ico, Ico, ←finset.filter_val, finset.Ico_filter_lt]
@[simp] lemma Ico_filter_le (a b c : α) : (Ico a b).filter (λ x, c ≤ x) = Ico (max a c) b :=
by rw [Ico, Ico, ←finset.filter_val, finset.Ico_filter_le]
@[simp] lemma Ico_sub_Ico_left (a b c : α) : Ico a b - Ico a c = Ico (max a c) b :=
by rw [Ico, Ico, Ico, ←finset.sdiff_val, finset.Ico_diff_Ico_left]
@[simp] lemma Ico_sub_Ico_right (a b c : α) : Ico a b - Ico c b = Ico a (min b c) :=
by rw [Ico, Ico, Ico, ←finset.sdiff_val, finset.Ico_diff_Ico_right]
end linear_order
section ordered_cancel_add_comm_monoid
variables [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [locally_finite_order α]
lemma map_add_left_Icc (a b c : α) : (Icc a b).map ((+) c) = Icc (c + a) (c + b) :=
by { classical, rw [Icc, Icc, ←finset.image_add_left_Icc, finset.image_val,
((finset.nodup _).map $ add_right_injective c).dedup] }
lemma map_add_left_Ico (a b c : α) : (Ico a b).map ((+) c) = Ico (c + a) (c + b) :=
by { classical, rw [Ico, Ico, ←finset.image_add_left_Ico, finset.image_val,
((finset.nodup _).map $ add_right_injective c).dedup] }
lemma map_add_left_Ioc (a b c : α) : (Ioc a b).map ((+) c) = Ioc (c + a) (c + b) :=
by { classical, rw [Ioc, Ioc, ←finset.image_add_left_Ioc, finset.image_val,
((finset.nodup _).map $ add_right_injective c).dedup] }
lemma map_add_left_Ioo (a b c : α) : (Ioo a b).map ((+) c) = Ioo (c + a) (c + b) :=
by { classical, rw [Ioo, Ioo, ←finset.image_add_left_Ioo, finset.image_val,
((finset.nodup _).map $ add_right_injective c).dedup] }
lemma map_add_right_Icc (a b c : α) : (Icc a b).map (λ x, x + c) = Icc (a + c) (b + c) :=
by { simp_rw add_comm _ c, exact map_add_left_Icc _ _ _ }
lemma map_add_right_Ico (a b c : α) : (Ico a b).map (λ x, x + c) = Ico (a + c) (b + c) :=
by { simp_rw add_comm _ c, exact map_add_left_Ico _ _ _ }
lemma map_add_right_Ioc (a b c : α) : (Ioc a b).map (λ x, x + c) = Ioc (a + c) (b + c) :=
by { simp_rw add_comm _ c, exact map_add_left_Ioc _ _ _ }
lemma map_add_right_Ioo (a b c : α) : (Ioo a b).map (λ x, x + c) = Ioo (a + c) (b + c) :=
by { simp_rw add_comm _ c, exact map_add_left_Ioo _ _ _ }
end ordered_cancel_add_comm_monoid
end multiset
|
2db2a1426e64d7c0e36129240fe530eb2610066b | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/linear_algebra/dfinsupp.lean | 3d0a611f8523f11c34fa58ec69f9d52516027bd0 | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,160 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau
-/
import data.finsupp.to_dfinsupp
import linear_algebra.basis
/-!
# Properties of the module `Π₀ i, M i`
Given an indexed collection of `R`-modules `M i`, the `R`-module structure on `Π₀ i, M i`
is defined in `data.dfinsupp`.
In this file we define `linear_map` versions of various maps:
* `dfinsupp.lsingle a : M →ₗ[R] Π₀ i, M i`: `dfinsupp.single a` as a linear map;
* `dfinsupp.lmk s : (Π i : (↑s : set ι), M i) →ₗ[R] Π₀ i, M i`: `dfinsupp.single a` as a linear map;
* `dfinsupp.lapply i : (Π₀ i, M i) →ₗ[R] M`: the map `λ f, f i` as a linear map;
* `dfinsupp.lsum`: `dfinsupp.sum` or `dfinsupp.lift_add_hom` as a `linear_map`;
## Implementation notes
This file should try to mirror `linear_algebra.finsupp` where possible. The API of `finsupp` is
much more developed, but many lemmas in that file should be eligible to copy over.
## Tags
function with finite support, module, linear algebra
-/
variables {ι : Type*} {R : Type*} {S : Type*} {M : ι → Type*} {N : Type*}
variables [dec_ι : decidable_eq ι]
namespace dfinsupp
variables [semiring R] [Π i, add_comm_monoid (M i)] [Π i, module R (M i)]
variables [add_comm_monoid N] [module R N]
include dec_ι
/-- `dfinsupp.mk` as a `linear_map`. -/
def lmk (s : finset ι) : (Π i : (↑s : set ι), M i) →ₗ[R] Π₀ i, M i :=
{ to_fun := mk s, map_add' := λ _ _, mk_add, map_smul' := λ c x, mk_smul c x}
/-- `dfinsupp.single` as a `linear_map` -/
def lsingle (i) : M i →ₗ[R] Π₀ i, M i :=
{ to_fun := single i, map_smul' := single_smul, .. dfinsupp.single_add_hom _ _ }
/-- Two `R`-linear maps from `Π₀ i, M i` which agree on each `single i x` agree everywhere. -/
lemma lhom_ext ⦃φ ψ : (Π₀ i, M i) →ₗ[R] N⦄
(h : ∀ i x, φ (single i x) = ψ (single i x)) :
φ = ψ :=
linear_map.to_add_monoid_hom_injective $ add_hom_ext h
/-- Two `R`-linear maps from `Π₀ i, M i` which agree on each `single i x` agree everywhere.
See note [partially-applied ext lemmas].
After apply this lemma, if `M = R` then it suffices to verify `φ (single a 1) = ψ (single a 1)`. -/
@[ext] lemma lhom_ext' ⦃φ ψ : (Π₀ i, M i) →ₗ[R] N⦄
(h : ∀ i, φ.comp (lsingle i) = ψ.comp (lsingle i)) :
φ = ψ :=
lhom_ext $ λ i, linear_map.congr_fun (h i)
omit dec_ι
/-- Interpret `λ (f : Π₀ i, M i), f i` as a linear map. -/
def lapply (i : ι) : (Π₀ i, M i) →ₗ[R] M i :=
{ to_fun := λ f, f i,
map_add' := λ f g, add_apply f g i,
map_smul' := λ c f, smul_apply c f i}
include dec_ι
@[simp] lemma lmk_apply (s : finset ι) (x) : (lmk s : _ →ₗ[R] Π₀ i, M i) x = mk s x := rfl
@[simp] lemma lsingle_apply (i : ι) (x : M i) : (lsingle i : _ →ₗ[R] _) x = single i x := rfl
omit dec_ι
@[simp] lemma lapply_apply (i : ι) (f : Π₀ i, M i) : (lapply i : _ →ₗ[R] _) f = f i := rfl
section lsum
/-- Typeclass inference can't find `dfinsupp.add_comm_monoid` without help for this case.
This instance allows it to be found where it is needed on the LHS of the colon in
`dfinsupp.module_of_linear_map`. -/
instance add_comm_monoid_of_linear_map : add_comm_monoid (Π₀ (i : ι), M i →ₗ[R] N) :=
@dfinsupp.add_comm_monoid _ (λ i, M i →ₗ[R] N) _
/-- Typeclass inference can't find `dfinsupp.module` without help for this case.
This is needed to define `dfinsupp.lsum` below.
The cause seems to be an inability to unify the `Π i, add_comm_monoid (M i →ₗ[R] N)` instance that
we have with the `Π i, has_zero (M i →ₗ[R] N)` instance which appears as a parameter to the
`dfinsupp` type. -/
instance module_of_linear_map [semiring S] [module S N] [smul_comm_class R S N] :
module S (Π₀ (i : ι), M i →ₗ[R] N) :=
@dfinsupp.module _ _ (λ i, M i →ₗ[R] N) _ _ _
variables (S)
include dec_ι
/-- The `dfinsupp` version of `finsupp.lsum`.
See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/
@[simps]
def lsum [semiring S] [module S N] [smul_comm_class R S N] :
(Π i, M i →ₗ[R] N) ≃ₗ[S] ((Π₀ i, M i) →ₗ[R] N) :=
{ to_fun := λ F, {
to_fun := sum_add_hom (λ i, (F i).to_add_monoid_hom),
map_add' := (lift_add_hom (λ i, (F i).to_add_monoid_hom)).map_add,
map_smul' := λ c f, by {
dsimp,
apply dfinsupp.induction f,
{ rw [smul_zero, add_monoid_hom.map_zero, smul_zero] },
{ intros a b f ha hb hf,
rw [smul_add, add_monoid_hom.map_add, add_monoid_hom.map_add, smul_add, hf, ←single_smul,
sum_add_hom_single, sum_add_hom_single, linear_map.to_add_monoid_hom_coe,
linear_map.map_smul], } } },
inv_fun := λ F i, F.comp (lsingle i),
left_inv := λ F, by { ext x y, simp },
right_inv := λ F, by { ext x y, simp },
map_add' := λ F G, by { ext x y, simp },
map_smul' := λ c F, by { ext, simp } }
/-- While `simp` can prove this, it is often convenient to avoid unfolding `lsum` into `sum_add_hom`
with `dfinsupp.lsum_apply_apply`. -/
lemma lsum_single [semiring S] [module S N] [smul_comm_class R S N]
(F : Π i, M i →ₗ[R] N) (i) (x : M i) :
lsum S F (single i x) = F i x :=
sum_add_hom_single _ _ _
end lsum
/-! ### Bundled versions of `dfinsupp.map_range`
The names should match the equivalent bundled `finsupp.map_range` definitions.
-/
section map_range
variables {β β₁ β₂: ι → Type*}
variables [Π i, add_comm_monoid (β i)] [Π i, add_comm_monoid (β₁ i)] [Π i, add_comm_monoid (β₂ i)]
variables [Π i, module R (β i)] [Π i, module R (β₁ i)] [Π i, module R (β₂ i)]
lemma map_range_smul (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0)
(r : R) (hf' : ∀ i x, f i (r • x) = r • f i x) (g : Π₀ i, β₁ i):
map_range f hf (r • g) = r • map_range f hf g :=
begin
ext,
simp only [map_range_apply f, coe_smul, pi.smul_apply, hf']
end
/-- `dfinsupp.map_range` as an `linear_map`. -/
@[simps apply]
def map_range.linear_map (f : Π i, β₁ i →ₗ[R] β₂ i) : (Π₀ i, β₁ i) →ₗ[R] (Π₀ i, β₂ i) :=
{ to_fun := map_range (λ i x, f i x) (λ i, (f i).map_zero),
map_smul' := λ r, map_range_smul _ _ _ (λ i, (f i).map_smul r),
.. map_range.add_monoid_hom (λ i, (f i).to_add_monoid_hom) }
@[simp]
lemma map_range.linear_map_id :
map_range.linear_map (λ i, (linear_map.id : (β₂ i) →ₗ[R] _)) = linear_map.id :=
linear_map.ext map_range_id
lemma map_range.linear_map_comp (f : Π i, β₁ i →ₗ[R] β₂ i) (f₂ : Π i, β i →ₗ[R] β₁ i):
map_range.linear_map (λ i, (f i).comp (f₂ i)) =
(map_range.linear_map f).comp (map_range.linear_map f₂) :=
linear_map.ext $ map_range_comp (λ i x, f i x) (λ i x, f₂ i x) _ _ _
/-- `dfinsupp.map_range.linear_map` as an `linear_equiv`. -/
@[simps apply]
def map_range.linear_equiv (e : Π i, β₁ i ≃ₗ[R] β₂ i) : (Π₀ i, β₁ i) ≃ₗ[R] (Π₀ i, β₂ i) :=
{ to_fun := map_range (λ i x, e i x) (λ i, (e i).map_zero),
inv_fun := map_range (λ i x, (e i).symm x) (λ i, (e i).symm.map_zero),
.. map_range.add_equiv (λ i, (e i).to_add_equiv),
.. map_range.linear_map (λ i, (e i).to_linear_map) }
@[simp]
lemma map_range.linear_equiv_refl :
(map_range.linear_equiv $ λ i, linear_equiv.refl R (β₁ i)) = linear_equiv.refl _ _ :=
linear_equiv.ext map_range_id
lemma map_range.linear_equiv_trans (f : Π i, β i ≃ₗ[R] β₁ i) (f₂ : Π i, β₁ i ≃ₗ[R] β₂ i):
map_range.linear_equiv (λ i, (f i).trans (f₂ i)) =
(map_range.linear_equiv f).trans (map_range.linear_equiv f₂) :=
linear_equiv.ext $ map_range_comp (λ i x, f₂ i x) (λ i x, f i x) _ _ _
@[simp]
lemma map_range.linear_equiv_symm (e : Π i, β₁ i ≃ₗ[R] β₂ i) :
(map_range.linear_equiv e).symm = map_range.linear_equiv (λ i, (e i).symm) := rfl
end map_range
section basis
/-- The direct sum of free modules is free.
Note that while this is stated for `dfinsupp` not `direct_sum`, the types are defeq. -/
noncomputable def basis {η : ι → Type*} (b : Π i, basis (η i) R (M i)) :
basis (Σ i, η i) R (Π₀ i, M i) :=
basis.of_repr ((map_range.linear_equiv (λ i, (b i).repr)).trans
(sigma_finsupp_lequiv_dfinsupp R).symm)
end basis
end dfinsupp
include dec_ι
namespace submodule
variables [semiring R] [add_comm_monoid N] [module R N]
open dfinsupp
lemma dfinsupp_sum_mem {β : ι → Type*} [Π i, has_zero (β i)]
[Π i (x : β i), decidable (x ≠ 0)] (S : submodule R N)
(f : Π₀ i, β i) (g : Π i, β i → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) : f.sum g ∈ S :=
S.to_add_submonoid.dfinsupp_sum_mem f g h
lemma dfinsupp_sum_add_hom_mem {β : ι → Type*} [Π i, add_zero_class (β i)]
(S : submodule R N) (f : Π₀ i, β i) (g : Π i, β i →+ N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) :
dfinsupp.sum_add_hom g f ∈ S :=
S.to_add_submonoid.dfinsupp_sum_add_hom_mem f g h
/-- The supremum of a family of submodules is equal to the range of `dfinsupp.lsum`; that is
every element in the `supr` can be produced from taking a finite number of non-zero elements
of `p i`, coercing them to `N`, and summing them. -/
lemma supr_eq_range_dfinsupp_lsum (p : ι → submodule R N) :
supr p = (dfinsupp.lsum ℕ (λ i, (p i).subtype)).range :=
begin
apply le_antisymm,
{ apply supr_le _,
intros i y hy,
exact ⟨dfinsupp.single i ⟨y, hy⟩, dfinsupp.sum_add_hom_single _ _ _⟩, },
{ rintros x ⟨v, rfl⟩,
exact dfinsupp_sum_add_hom_mem _ v _ (λ i _, (le_supr p i : p i ≤ _) (v i).prop) }
end
/-- The bounded supremum of a family of commutative additive submonoids is equal to the range of
`dfinsupp.sum_add_hom` composed with `dfinsupp.filter_add_monoid_hom`; that is, every element in the
bounded `supr` can be produced from taking a finite number of non-zero elements from the `S i` that
satisfy `p i`, coercing them to `γ`, and summing them. -/
lemma bsupr_eq_range_dfinsupp_lsum (p : ι → Prop)
[decidable_pred p] (S : ι → submodule R N) :
(⨆ i (h : p i), S i) =
((dfinsupp.lsum ℕ (λ i, (S i).subtype)).comp (dfinsupp.filter_linear_map R _ p)).range :=
begin
apply le_antisymm,
{ apply bsupr_le _,
intros i hi y hy,
refine ⟨dfinsupp.single i ⟨y, hy⟩, _⟩,
rw [linear_map.comp_apply, filter_linear_map_apply, filter_single_pos _ _ hi],
exact dfinsupp.sum_add_hom_single _ _ _, },
{ rintros x ⟨v, rfl⟩,
refine dfinsupp_sum_add_hom_mem _ _ _ (λ i hi, _),
refine mem_supr_of_mem i _,
by_cases hp : p i,
{ simp [hp], },
{ simp [hp] }, }
end
lemma mem_supr_iff_exists_dfinsupp (p : ι → submodule R N) (x : N) :
x ∈ supr p ↔ ∃ f : Π₀ i, p i, dfinsupp.lsum ℕ (λ i, (p i).subtype) f = x :=
set_like.ext_iff.mp (supr_eq_range_dfinsupp_lsum p) x
/-- A variant of `submodule.mem_supr_iff_exists_dfinsupp` with the RHS fully unfolded. -/
lemma mem_supr_iff_exists_dfinsupp' (p : ι → submodule R N) [Π i (x : p i), decidable (x ≠ 0)]
(x : N) :
x ∈ supr p ↔ ∃ f : Π₀ i, p i, f.sum (λ i xi, ↑xi) = x :=
begin
rw mem_supr_iff_exists_dfinsupp,
simp_rw [dfinsupp.lsum_apply_apply, dfinsupp.sum_add_hom_apply],
congr',
end
lemma mem_bsupr_iff_exists_dfinsupp (p : ι → Prop) [decidable_pred p] (S : ι → submodule R N)
(x : N) :
x ∈ (⨆ i (h : p i), S i) ↔
∃ f : Π₀ i, S i, dfinsupp.lsum ℕ (λ i, (S i).subtype) (f.filter p) = x :=
set_like.ext_iff.mp (bsupr_eq_range_dfinsupp_lsum p S) x
end submodule
namespace complete_lattice
open dfinsupp
section semiring
variables [semiring R] [add_comm_monoid N] [module R N]
/-- Independence of a family of submodules can be expressed as a quantifier over `dfinsupp`s.
This is an intermediate result used to prove
`complete_lattice.independent_of_dfinsupp_lsum_injective` and
`complete_lattice.independent.dfinsupp_lsum_injective`. -/
lemma independent_iff_forall_dfinsupp (p : ι → submodule R N) :
independent p ↔
∀ i (x : p i) (v : Π₀ (i : ι), ↥(p i)), lsum ℕ (λ i, (p i).subtype) (erase i v) = x → x = 0 :=
begin
simp_rw [complete_lattice.independent_def, submodule.disjoint_def,
submodule.mem_bsupr_iff_exists_dfinsupp, exists_imp_distrib, filter_ne_eq_erase],
apply forall_congr (λ i, _),
refine subtype.forall'.trans _,
simp_rw submodule.coe_eq_zero,
refl,
end
/- If `dfinsupp.lsum` applied with `submodule.subtype` is injective then the submodules are
independent. -/
lemma independent_of_dfinsupp_lsum_injective (p : ι → submodule R N)
(h : function.injective (lsum ℕ (λ i, (p i).subtype))) :
independent p :=
begin
rw independent_iff_forall_dfinsupp,
intros i x v hv,
replace hv : lsum ℕ (λ i, (p i).subtype) (erase i v) = lsum ℕ (λ i, (p i).subtype) (single i x),
{ simpa only [lsum_single] using hv, },
have := dfinsupp.ext_iff.mp (h hv) i,
simpa [eq_comm] using this,
end
/-- Combining `dfinsupp.lsum` with `linear_map.to_span_singleton` is the same as `finsupp.total` -/
lemma lsum_comp_map_range_to_span_singleton
[Π (m : R), decidable (m ≠ 0)]
(p : ι → submodule R N) {v : ι → N} (hv : ∀ (i : ι), v i ∈ p i) :
((lsum ℕ) (λ i, (p i).subtype) : _ →ₗ[R] _).comp
((map_range.linear_map
(λ i, linear_map.to_span_singleton R ↥(p i) ⟨v i, hv i⟩) : _ →ₗ[R] _).comp
(finsupp_lequiv_dfinsupp R : (ι →₀ R) ≃ₗ[R] _).to_linear_map) =
finsupp.total ι N R v :=
by { ext, simp }
end semiring
section ring
variables [ring R] [add_comm_group N] [module R N]
/-- The canonical map out of a direct sum of a family of submodules is injective when the submodules
are `complete_lattice.independent`.
Note that this is not generally true for `[semiring R]`, for instance when `A` is the
`ℕ`-submodules of the positive and negative integers.
See `counterexamples/direct_sum_is_internal.lean` for a proof of this fact. -/
lemma independent.dfinsupp_lsum_injective {p : ι → submodule R N}
(h : independent p) : function.injective (lsum ℕ (λ i, (p i).subtype)) :=
begin
-- simplify everything down to binders over equalities in `N`
rw independent_iff_forall_dfinsupp at h,
suffices : (lsum ℕ (λ i, (p i).subtype)).ker = ⊥,
{ -- Lean can't find this without our help
letI : add_comm_group (Π₀ i, p i) := @dfinsupp.add_comm_group _ (λ i, p i) _,
rw linear_map.ker_eq_bot at this, exact this },
rw linear_map.ker_eq_bot',
intros m hm,
ext i : 1,
-- split `m` into the piece at `i` and the pieces elsewhere, to match `h`
rw [dfinsupp.zero_apply, ←neg_eq_zero],
refine h i (-m i) m _,
rwa [←erase_add_single i m, linear_map.map_add, lsum_single, submodule.subtype_apply,
add_eq_zero_iff_eq_neg, ←submodule.coe_neg] at hm,
end
/-- A family of submodules over an additive group are independent if and only iff `dfinsupp.lsum`
applied with `submodule.subtype` is injective.
Note that this is not generally true for `[semiring R]`; see
`complete_lattice.independent.dfinsupp_lsum_injective` for details. -/
lemma independent_iff_dfinsupp_lsum_injective (p : ι → submodule R N) :
independent p ↔ function.injective (lsum ℕ (λ i, (p i).subtype)) :=
⟨independent.dfinsupp_lsum_injective, independent_of_dfinsupp_lsum_injective p⟩
omit dec_ι
/-- If a family of submodules is `independent`, then a choice of nonzero vector from each submodule
forms a linearly independent family. -/
lemma independent.linear_independent [no_zero_smul_divisors R N] (p : ι → submodule R N)
(hp : complete_lattice.independent p) {v : ι → N} (hv : ∀ i, v i ∈ p i) (hv' : ∀ i, v i ≠ 0) :
linear_independent R v :=
begin
classical,
rw linear_independent_iff,
intros l hl,
let a := dfinsupp.map_range.linear_map
(λ i, linear_map.to_span_singleton R (p i) (⟨v i, hv i⟩)) l.to_dfinsupp,
have ha : a = 0,
{ apply hp.dfinsupp_lsum_injective,
rwa ←lsum_comp_map_range_to_span_singleton _ hv at hl },
ext i,
apply smul_left_injective R (hv' i),
have : l i • v i = a i := rfl,
simp [this, ha],
end
end ring
end complete_lattice
|
3bfc4565a7b04ed4c9f570b906a5c47a16b1b5db | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /library/theories/combinatorics/choose.lean | c3ac5076bbd40f4c74675d7115159abe4c5f5e38 | [
"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 | 8,505 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad
Binomial coefficients, "n choose k".
-/
import data.nat.div data.nat.fact data.finset
open decidable
namespace nat
/- choose -/
definition choose : ℕ → ℕ → ℕ
| 0 0 := 1
| 0 (succ k) := 0
| (succ n) 0 := 1
| (succ n) (succ k) := choose n (succ k) + choose n k
theorem choose_zero_right (n : ℕ) : choose n 0 = 1 :=
nat.cases_on n rfl (take m, rfl)
theorem choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl
theorem choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n (succ k) + choose n k :=
rfl
theorem choose_eq_zero_of_lt {n : ℕ} : ∀{k : ℕ}, n < k → choose n k = 0 :=
nat.induction_on n
(take k, assume H : 0 < k,
obtain k' (H : k = succ k'), from exists_eq_succ_of_pos H,
by rewrite H)
(take n',
assume IH: ∀ k, n' < k → choose n' k = 0,
take k,
suppose succ n' < k,
obtain k' (keq : k = succ k'), from exists_eq_succ_of_lt this,
have n' < k', by rewrite keq at this; apply lt_of_succ_lt_succ this,
by rewrite [keq, choose_succ_succ, IH _ this, IH _ (lt.trans this !lt_succ_self)])
theorem choose_self (n : ℕ) : choose n n = 1 :=
begin
induction n with [n, ih],
{apply rfl},
rewrite [choose_succ_succ, ih, choose_eq_zero_of_lt !lt_succ_self]
end
theorem choose_succ_self (n : ℕ) : choose (succ n) n = succ n :=
begin
induction n with [n, ih],
{apply rfl},
rewrite [choose_succ_succ, ih, choose_self, add.comm]
end
theorem choose_one_right (n : ℕ) : choose n 1 = n :=
begin
induction n with [n, ih],
{apply rfl},
change choose (succ n) (succ 0) = succ n,
krewrite [choose_succ_succ, ih, choose_zero_right]
end
theorem choose_pos {n : ℕ} : ∀ {k : ℕ}, k ≤ n → choose n k > 0 :=
begin
induction n with [n, ih],
{intros [k, H],
have k = 0, from eq_of_le_of_ge H !zero_le,
subst k, rewrite choose_zero_right; apply zero_lt_one},
intro k,
cases k with k,
{intros, rewrite [choose_zero_right], apply zero_lt_one},
suppose succ k ≤ succ n,
have k ≤ n, from le_of_succ_le_succ this,
by rewrite [choose_succ_succ]; apply add_pos_right (ih this)
end
-- A key identity. The proof is subtle.
theorem succ_mul_choose_eq (n : ℕ) :
∀ k, succ n * (choose n k) = choose (succ n) (succ k) * succ k :=
begin
induction n with [n, ih],
{intro k,
cases k with k',
{krewrite [*choose_self, one_mul, mul_one]},
{have H : 1 < succ (succ k'), from succ_lt_succ !zero_lt_succ,
krewrite [one_mul, choose_zero_succ, choose_eq_zero_of_lt H, zero_mul]}},
intro k,
cases k with k',
{krewrite [choose_zero_right, choose_one_right]},
rewrite [choose_succ_succ (succ n), right_distrib, -ih (succ k')],
rewrite [choose_succ_succ at {1}, left_distrib, *succ_mul (succ n), mul_succ, -ih k'],
rewrite [*add.assoc, add.left_comm (choose n _)]
end
theorem choose_mul_fact_mul_fact {n : ℕ} :
∀ {k : ℕ}, k ≤ n → choose n k * fact k * fact (n - k) = fact n :=
begin
induction n using nat.strong_induction_on with [n, ih],
cases n with n,
{intro k H, have k = 0, from eq_zero_of_le_zero H, rewrite this},
intro k,
intro H, -- k ≤ n,
cases k with k,
{rewrite [choose_zero_right, fact_zero, *one_mul]},
have k ≤ n, from le_of_succ_le_succ H,
show choose (succ n) (succ k) * fact (succ k) * fact (succ n - succ k) = fact (succ n), from
begin
rewrite [succ_sub_succ, fact_succ, -mul.assoc, -succ_mul_choose_eq],
rewrite [fact_succ n, -ih n !lt_succ_self this, *mul.assoc]
end
end
theorem choose_def_alt {n k : ℕ} (H : k ≤ n) : choose n k = fact n / (fact k * fact (n -k)) :=
eq.symm (nat.div_eq_of_eq_mul_left (mul_pos !fact_pos !fact_pos)
(by rewrite [-mul.assoc, choose_mul_fact_mul_fact H]))
theorem fact_mul_fact_dvd_fact {n k : ℕ} (H : k ≤ n) : fact k * fact (n - k) ∣ fact n :=
by rewrite [-choose_mul_fact_mul_fact H, mul.assoc]; apply dvd_mul_left
open finset
/- the number of subsets of s of size k is n choose k -/
section card_subsets
variables {A : Type} [deceqA : decidable_eq A]
include deceqA
private theorem aux₀ (s : finset A) : {t ∈ powerset s | card t = 0} = '{∅} :=
ext (take t, iff.intro
(assume H,
have t = ∅, from eq_empty_of_card_eq_zero (of_mem_sep H),
show t ∈ '{ ∅ }, by rewrite [this, mem_singleton_iff])
(assume H,
have t = ∅, by rewrite mem_singleton_iff at H; assumption,
by substvars; exact mem_sep_of_mem !empty_mem_powerset rfl))
private theorem aux₁ (k : ℕ) : {t ∈ powerset (∅ : finset A) | card t = succ k} = ∅ :=
eq_empty_of_forall_not_mem (take t, assume H,
have t ∈ powerset ∅, from mem_of_mem_sep H,
have t = ∅, by rewrite [powerset_empty at this, mem_singleton_iff at this]; assumption,
have card (∅ : finset A) = succ k, by rewrite -this; apply of_mem_sep H,
nat.no_confusion this)
private theorem aux₂ {a : A} {s t : finset A} (anins : a ∉ s) (tpows : t ∈ powerset s) : a ∉ t :=
suppose a ∈ t,
have a ∈ s, from mem_of_subset_of_mem (subset_of_mem_powerset tpows) this,
anins this
private theorem aux₃ {a : A} {s t : finset A} (anins : a ∉ s) (k : ℕ) :
t ∈ (insert a) ' (powerset s) ∧ card t = succ k ↔
t ∈ (insert a) ' {t' ∈ powerset s | card t' = k} :=
iff.intro
(assume H,
obtain H' cardt, from H,
obtain t' [(t'pows : t' ∈ powerset s) (teq : insert a t' = t)], from exists_of_mem_image H',
have aint : a ∈ t, by rewrite -teq; apply mem_insert,
have anint' : a ∉ t', from
(assume aint',
have a ∈ s, from mem_of_subset_of_mem (subset_of_mem_powerset t'pows) aint',
anins this),
have t' = erase a t, by rewrite [-teq, erase_insert (aux₂ anins t'pows)],
have card t' = k, by rewrite [this, card_erase_of_mem aint, cardt],
mem_image (mem_sep_of_mem t'pows this) teq)
(assume H,
obtain t' [Ht' (teq : insert a t' = t)], from exists_of_mem_image H,
have t'pows : t' ∈ powerset s, from mem_of_mem_sep Ht',
have cardt' : card t' = k, from of_mem_sep Ht',
and.intro
(show t ∈ (insert a) ' (powerset s), from mem_image t'pows teq)
(show card t = succ k,
by rewrite [-teq, card_insert_of_not_mem (aux₂ anins t'pows), cardt']))
private theorem aux₄ {a : A} {s : finset A} (anins : a ∉ s) (k : ℕ) :
{t ∈ powerset (insert a s)| card t = succ k} =
{t ∈ powerset s | card t = succ k} ∪ (insert a) ' {t ∈ powerset s | card t = k} :=
begin
apply ext, intro t,
rewrite [powerset_insert anins, mem_union_iff, *mem_sep_iff, mem_union_iff, and.right_distrib,
aux₃ anins]
end
private theorem aux₅ {a : A} {s : finset A} (anins : a ∉ s) (k : ℕ) :
{t ∈ powerset s | card t = succ k} ∩ (insert a) ' {t ∈ powerset s | card t = k} = ∅ :=
inter_eq_empty
(take t, assume Ht₁ Ht₂,
have tpows : t ∈ powerset s, from mem_of_mem_sep Ht₁,
have anint : a ∉ t, from aux₂ anins tpows,
obtain t' [Ht' (teq : insert a t' = t)], from exists_of_mem_image Ht₂,
have aint : a ∈ t, by rewrite -teq; apply mem_insert,
show false, from anint aint)
private theorem aux₆ {a : A} {s : finset A} (anins : a ∉ s) (k : ℕ) :
card ((insert a) ' {t ∈ powerset s | card t = k}) = card {t ∈ powerset s | card t = k} :=
have set.inj_on (insert a) (ts {t ∈ powerset s| card t = k}), from
take t₁ t₂, assume Ht₁ Ht₂,
assume Heq : insert a t₁ = insert a t₂,
have t₁ ∈ powerset s, from mem_of_mem_sep Ht₁,
have anint₁ : a ∉ t₁, from aux₂ anins this,
have t₂ ∈ powerset s, from mem_of_mem_sep Ht₂,
have anint₂ : a ∉ t₂, from aux₂ anins this,
calc
t₁ = erase a (insert a t₁) : by rewrite (erase_insert anint₁)
... = erase a (insert a t₂) : Heq
... = t₂ : by rewrite (erase_insert anint₂),
card_image_eq_of_inj_on this
theorem card_subsets (s : finset A) : ∀k, card {t ∈ powerset s | card t = k} = choose (card s) k :=
begin
induction s with a s anins ih,
{intro k,
cases k with k,
{rewrite aux₀},
rewrite aux₁},
intro k,
cases k with k,
{rewrite [aux₀, choose_zero_right]},
rewrite [*(card_insert_of_not_mem anins), aux₄ anins, card_union_of_disjoint (aux₅ anins k),
aux₆ anins k, *ih]
end
end card_subsets
end nat
|
42ffeef7030d3e967e72dbcece295e753d2886ea | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/algebra/ordered_field.lean | f289102de1c68194e65ccb89032d51af48f73e8d | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 22,354 | lean | /-
Copyright (c) 2014 Robert Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Lewis
-/
import algebra.ordered_ring algebra.field
open eq eq.ops
structure linear_ordered_field [class] (A : Type) extends linear_ordered_ring A, field A
section linear_ordered_field
variable {A : Type}
variables [s : linear_ordered_field A] {a b c d : A}
include s
-- helpers for following
theorem mul_zero_lt_mul_inv_of_pos (H : 0 < a) : a * 0 < a * (1 / a) :=
calc
a * 0 = 0 : mul_zero
... < 1 : zero_lt_one
... = a * a⁻¹ : mul_inv_cancel (ne.symm (ne_of_lt H))
... = a * (1 / a) : inv_eq_one_div
theorem mul_zero_lt_mul_inv_of_neg (H : a < 0) : a * 0 < a * (1 / a) :=
calc
a * 0 = 0 : mul_zero
... < 1 : zero_lt_one
... = a * a⁻¹ : mul_inv_cancel (ne_of_lt H)
... = a * (1 / a) : inv_eq_one_div
theorem one_div_pos_of_pos (H : 0 < a) : 0 < 1 / a :=
lt_of_mul_lt_mul_left (mul_zero_lt_mul_inv_of_pos H) (le_of_lt H)
theorem one_div_neg_of_neg (H : a < 0) : 1 / a < 0 :=
gt_of_mul_lt_mul_neg_left (mul_zero_lt_mul_inv_of_neg H) (le_of_lt H)
theorem le_mul_of_ge_one_right (Hb : b ≥ 0) (H : a ≥ 1) : b ≤ b * a :=
mul_one _ ▸ (mul_le_mul_of_nonneg_left H Hb)
theorem lt_mul_of_gt_one_right (Hb : b > 0) (H : a > 1) : b < b * a :=
mul_one _ ▸ (mul_lt_mul_of_pos_left H Hb)
theorem one_le_div_iff_le (a : A) {b : A} (Hb : b > 0) : 1 ≤ a / b ↔ b ≤ a :=
have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb),
iff.intro
(assume H : 1 ≤ a / b,
calc
b = b : refl
... ≤ b * (a / b) : le_mul_of_ge_one_right (le_of_lt Hb) H
... = a : mul_div_cancel' Hb')
(assume H : b ≤ a,
have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc
1 = b * (1 / b) : mul_one_div_cancel Hb'
... ≤ a * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt Hbinv)
... = a / b : div_eq_mul_one_div)
theorem le_of_one_le_div (Hb : b > 0) (H : 1 ≤ a / b) : b ≤ a :=
(iff.mp (!one_le_div_iff_le Hb)) H
theorem one_le_div_of_le (Hb : b > 0) (H : b ≤ a) : 1 ≤ a / b :=
(iff.mpr (!one_le_div_iff_le Hb)) H
theorem one_lt_div_iff_lt (a : A) {b : A} (Hb : b > 0) : 1 < a / b ↔ b < a :=
have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb),
iff.intro
(assume H : 1 < a / b,
calc
b < b * (a / b) : lt_mul_of_gt_one_right Hb H
... = a : mul_div_cancel' Hb')
(assume H : b < a,
have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc
1 = b * (1 / b) : mul_one_div_cancel Hb'
... < a * (1 / b) : mul_lt_mul_of_pos_right H Hbinv
... = a / b : div_eq_mul_one_div)
theorem lt_of_one_lt_div (Hb : b > 0) (H : 1 < a / b) : b < a :=
(iff.mp (!one_lt_div_iff_lt Hb)) H
theorem one_lt_div_of_lt (Hb : b > 0) (H : b < a) : 1 < a / b :=
(iff.mpr (!one_lt_div_iff_lt Hb)) H
theorem exists_lt (a : A) : ∃ x, x < a :=
have H : a - 1 < a, from add_lt_of_le_of_neg (le.refl _) zero_gt_neg_one,
exists.intro _ H
theorem exists_gt (a : A) : ∃ x, x > a :=
have H : a + 1 > a, from lt_add_of_le_of_pos (le.refl _) zero_lt_one,
exists.intro _ H
-- the following theorems amount to four iffs, for <, ≤, ≥, >.
theorem mul_le_of_le_div (Hc : 0 < c) (H : a ≤ b / c) : a * c ≤ b :=
!div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_le_mul_of_nonneg_right H (le_of_lt Hc)
theorem le_div_of_mul_le (Hc : 0 < c) (H : a * c ≤ b) : a ≤ b / c :=
calc
a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc))
... ≤ b * (1 / c) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc))
... = b / c : div_eq_mul_one_div
theorem mul_lt_of_lt_div (Hc : 0 < c) (H : a < b / c) : a * c < b :=
!div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_lt_mul_of_pos_right H Hc
theorem lt_div_of_mul_lt (Hc : 0 < c) (H : a * c < b) : a < b / c :=
calc
a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc))
... < b * (1 / c) : mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc)
... = b / c : div_eq_mul_one_div
theorem mul_le_of_div_le_of_neg (Hc : c < 0) (H : b / c ≤ a) : a * c ≤ b :=
!div_mul_cancel (ne_of_lt Hc) ▸ mul_le_mul_of_nonpos_right H (le_of_lt Hc)
theorem div_le_of_mul_le_of_neg (Hc : c < 0) (H : a * c ≤ b) : b / c ≤ a :=
calc
a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc)
... ≥ b * (1 / c) : mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc))
... = b / c : div_eq_mul_one_div
theorem mul_lt_of_gt_div_of_neg (Hc : c < 0) (H : a > b / c) : a * c < b :=
!div_mul_cancel (ne_of_lt Hc) ▸ mul_lt_mul_of_neg_right H Hc
theorem div_lt_of_mul_lt_of_pos (Hc : c > 0) (H : b < a * c) : b / c < a :=
calc
a = a * c * (1 / c) : !mul_mul_div (ne_of_gt Hc)
... > b * (1 / c) : mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc)
... = b / c : div_eq_mul_one_div
theorem div_lt_of_mul_gt_of_neg (Hc : c < 0) (H : a * c < b) : b / c < a :=
calc
a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc)
... > b * (1 / c) : mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc)
... = b / c : div_eq_mul_one_div
theorem div_le_of_le_mul (Hb : b > 0) (H : a ≤ b * c) : a / b ≤ c :=
calc
a / b = a * (1 / b) : div_eq_mul_one_div
... ≤ (b * c) * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hb))
... = (b * c) / b : div_eq_mul_one_div
... = c : mul_div_cancel_left (ne.symm (ne_of_lt Hb))
theorem le_mul_of_div_le (Hc : c > 0) (H : a / c ≤ b) : a ≤ b * c :=
calc
a = a / c * c : !div_mul_cancel (ne.symm (ne_of_lt Hc))
... ≤ b * c : mul_le_mul_of_nonneg_right H (le_of_lt Hc)
-- following these in the isabelle file, there are 8 biconditionals for the above with - signs
-- skipping for now
theorem mul_sub_mul_div_mul_neg (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c < b / d) :
(a * d - b * c) / (c * d) < 0 :=
have H1 : a / c - b / d < 0, from calc
a / c - b / d < b / d - b / d : sub_lt_sub_right H
... = 0 : sub_self,
calc
0 > a / c - b / d : H1
... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd
... = (a * d - b * c) / (c * d) : mul.comm
theorem mul_sub_mul_div_mul_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c ≤ b / d) :
(a * d - b * c) / (c * d) ≤ 0 :=
have H1 : a / c - b / d ≤ 0, from calc
a / c - b / d ≤ b / d - b / d : sub_le_sub_right H
... = 0 : sub_self,
calc
0 ≥ a / c - b / d : H1
... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd
... = (a * d - b * c) / (c * d) : mul.comm
theorem div_lt_div_of_mul_sub_mul_div_neg (Hc : c ≠ 0) (Hd : d ≠ 0)
(H : (a * d - b * c) / (c * d) < 0) : a / c < b / d :=
assert H1 : (a * d - c * b) / (c * d) < 0, by rewrite [mul.comm c b]; exact H,
assert H2 : a / c - b / d < 0, by rewrite [!div_sub_div Hc Hd]; exact H1,
assert H3 : a / c - b / d + b / d < 0 + b / d, from add_lt_add_right H2 _,
begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end
theorem div_le_div_of_mul_sub_mul_div_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0)
(H : (a * d - b * c) / (c * d) ≤ 0) : a / c ≤ b / d :=
assert H1 : (a * d - c * b) / (c * d) ≤ 0, by rewrite [mul.comm c b]; exact H,
assert H2 : a / c - b / d ≤ 0, by rewrite [!div_sub_div Hc Hd]; exact H1,
assert H3 : a / c - b / d + b / d ≤ 0 + b / d, from add_le_add_right H2 _,
begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end
theorem div_pos_of_pos_of_pos (Ha : 0 < a) (Hb : 0 < b) : 0 < a / b :=
begin
rewrite div_eq_mul_one_div,
apply mul_pos,
exact Ha,
apply one_div_pos_of_pos,
exact Hb
end
theorem div_nonneg_of_nonneg_of_pos (Ha : 0 ≤ a) (Hb : 0 < b) : 0 ≤ a / b :=
begin
rewrite div_eq_mul_one_div,
apply mul_nonneg,
exact Ha,
apply le_of_lt,
apply one_div_pos_of_pos,
exact Hb
end
theorem div_neg_of_neg_of_pos (Ha : a < 0) (Hb : 0 < b) : a / b < 0:=
begin
rewrite div_eq_mul_one_div,
apply mul_neg_of_neg_of_pos,
exact Ha,
apply one_div_pos_of_pos,
exact Hb
end
theorem div_nonpos_of_nonpos_of_pos (Ha : a ≤ 0) (Hb : 0 < b) : a / b ≤ 0 :=
begin
rewrite div_eq_mul_one_div,
apply mul_nonpos_of_nonpos_of_nonneg,
exact Ha,
apply le_of_lt,
apply one_div_pos_of_pos,
exact Hb
end
theorem div_neg_of_pos_of_neg (Ha : 0 < a) (Hb : b < 0) : a / b < 0 :=
begin
rewrite div_eq_mul_one_div,
apply mul_neg_of_pos_of_neg,
exact Ha,
apply one_div_neg_of_neg,
exact Hb
end
theorem div_nonpos_of_nonneg_of_neg (Ha : 0 ≤ a) (Hb : b < 0) : a / b ≤ 0 :=
begin
rewrite div_eq_mul_one_div,
apply mul_nonpos_of_nonneg_of_nonpos,
exact Ha,
apply le_of_lt,
apply one_div_neg_of_neg,
exact Hb
end
theorem div_pos_of_neg_of_neg (Ha : a < 0) (Hb : b < 0) : 0 < a / b :=
begin
rewrite div_eq_mul_one_div,
apply mul_pos_of_neg_of_neg,
exact Ha,
apply one_div_neg_of_neg,
exact Hb
end
theorem div_nonneg_of_nonpos_of_neg (Ha : a ≤ 0) (Hb : b < 0) : 0 ≤ a / b :=
begin
rewrite div_eq_mul_one_div,
apply mul_nonneg_of_nonpos_of_nonpos,
exact Ha,
apply le_of_lt,
apply one_div_neg_of_neg,
exact Hb
end
theorem div_lt_div_of_lt_of_pos (H : a < b) (Hc : 0 < c) : a / c < b / c :=
begin
rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div],
exact mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc)
end
theorem div_le_div_of_le_of_pos (H : a ≤ b) (Hc : 0 < c) : a / c ≤ b / c :=
begin
rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div],
exact mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc))
end
theorem div_lt_div_of_lt_of_neg (H : b < a) (Hc : c < 0) : a / c < b / c :=
begin
rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div],
exact mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc)
end
theorem div_le_div_of_le_of_neg (H : b ≤ a) (Hc : c < 0) : a / c ≤ b / c :=
begin
rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div],
exact mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc))
end
theorem two_pos : (1 : A) + 1 > 0 :=
add_pos zero_lt_one zero_lt_one
theorem one_add_one_ne_zero : 1 + 1 ≠ (0:A) :=
ne.symm (ne_of_lt two_pos)
theorem two_ne_zero : 2 ≠ (0:A) :=
by unfold bit0; apply one_add_one_ne_zero
theorem add_halves (a : A) : a / 2 + a / 2 = a :=
calc
a / 2 + a / 2 = (a + a) / 2 : by rewrite div_add_div_same
... = (a * 1 + a * 1) / 2 : by rewrite mul_one
... = (a * (1 + 1)) / 2 : by rewrite left_distrib
... = (a * 2) / 2 : by rewrite one_add_one_eq_two
... = a : by rewrite [@mul_div_cancel A _ _ _ two_ne_zero]
theorem sub_self_div_two (a : A) : a - a / 2 = a / 2 :=
by rewrite [-{a}add_halves at {1}, add_sub_cancel]
theorem add_midpoint {a b : A} (H : a < b) : a + (b - a) / 2 < b :=
begin
rewrite [-div_sub_div_same, sub_eq_add_neg, {b / 2 + _}add.comm, -add.assoc, -sub_eq_add_neg],
apply add_lt_of_lt_sub_right,
rewrite *sub_self_div_two,
apply div_lt_div_of_lt_of_pos H two_pos
end
theorem div_two_sub_self (a : A) : a / 2 - a = - (a / 2) :=
by rewrite [-{a}add_halves at {2}, sub_add_eq_sub_sub, sub_self, zero_sub]
theorem add_self_div_two (a : A) : (a + a) / 2 = a :=
symm (iff.mpr (!eq_div_iff_mul_eq (ne_of_gt (add_pos zero_lt_one zero_lt_one)))
(by krewrite [left_distrib, *mul_one]))
theorem two_gt_one : (2:A) > 1 :=
calc (2:A) = 1+1 : one_add_one_eq_two
... > 1+0 : add_lt_add_left zero_lt_one
... = 1 : add_zero
theorem two_ge_one : (2:A) ≥ 1 :=
le_of_lt two_gt_one
theorem four_pos : (4 : A) > 0 := add_pos two_pos two_pos
theorem mul_le_mul_of_mul_div_le (H : a * (b / c) ≤ d) (Hc : c > 0) : b * a ≤ d * c :=
begin
rewrite [-mul_div_assoc at H, mul.comm b],
apply le_mul_of_div_le Hc H
end
theorem div_two_lt_of_pos (H : a > 0) : a / (1 + 1) < a :=
have Ha : a / (1 + 1) > 0, from div_pos_of_pos_of_pos H (add_pos zero_lt_one zero_lt_one),
calc
a / (1 + 1) < a / (1 + 1) + a / (1 + 1) : lt_add_of_pos_left Ha
... = a : add_halves
theorem div_mul_le_div_mul_of_div_le_div_pos {e : A} (Hb : b ≠ 0) (Hd : d ≠ 0) (H : a / b ≤ c / d)
(He : e > 0) : a / (b * e) ≤ c / (d * e) :=
begin
rewrite [!field.div_mul_eq_div_mul_one_div Hb (ne_of_gt He),
!field.div_mul_eq_div_mul_one_div Hd (ne_of_gt He)],
apply mul_le_mul_of_nonneg_right H,
apply le_of_lt,
apply one_div_pos_of_pos He
end
theorem exists_add_lt_and_pos_of_lt (H : b < a) : ∃ c : A, b + c < a ∧ c > 0 :=
exists.intro ((a - b) / (1 + 1))
(and.intro (assert H2 : a + a > (b + b) + (a - b), from calc
a + a > b + a : add_lt_add_right H
... = b + a + b - b : add_sub_cancel
... = b + b + a - b : add.right_comm
... = (b + b) + (a - b) : add_sub,
assert H3 : (a + a) / 2 > ((b + b) + (a - b)) / 2,
from div_lt_div_of_lt_of_pos H2 two_pos,
by rewrite [one_add_one_eq_two, sub_eq_add_neg, add_self_div_two at H3, -div_add_div_same at H3, add_self_div_two at H3];
exact H3)
(div_pos_of_pos_of_pos (iff.mpr !sub_pos_iff_lt H) two_pos))
theorem ge_of_forall_ge_sub {a b : A} (H : ∀ ε : A, ε > 0 → a ≥ b - ε) : a ≥ b :=
begin
apply le_of_not_gt,
intro Hb,
cases exists_add_lt_and_pos_of_lt Hb with [c, Hc],
let Hc' := H c (and.right Hc),
apply (not_le_of_gt (and.left Hc)) (iff.mpr !le_add_iff_sub_right_le Hc')
end
end linear_ordered_field
structure discrete_linear_ordered_field [class] (A : Type) extends linear_ordered_field A,
decidable_linear_ordered_comm_ring A :=
(inv_zero : inv zero = zero)
section discrete_linear_ordered_field
variable {A : Type}
variables [s : discrete_linear_ordered_field A] {a b c : A}
include s
definition dec_eq_of_dec_lt : ∀ x y : A, decidable (x = y) :=
take x y,
decidable.by_cases
(assume H : x < y, decidable.inr (ne_of_lt H))
(assume H : ¬ x < y,
decidable.by_cases
(assume H' : y < x, decidable.inr (ne.symm (ne_of_lt H')))
(assume H' : ¬ y < x,
decidable.inl (le.antisymm (le_of_not_gt H') (le_of_not_gt H))))
definition discrete_linear_ordered_field.to_discrete_field [trans_instance] [reducible]
: discrete_field A :=
⦃ discrete_field, s, has_decidable_eq := dec_eq_of_dec_lt⦄
theorem pos_of_one_div_pos (H : 0 < 1 / a) : 0 < a :=
have H1 : 0 < 1 / (1 / a), from one_div_pos_of_pos H,
have H2 : 1 / a ≠ 0, from
(assume H3 : 1 / a = 0,
have H4 : 1 / (1 / a) = 0, from H3⁻¹ ▸ !div_zero,
absurd H4 (ne.symm (ne_of_lt H1))),
(division_ring.one_div_one_div (ne_zero_of_one_div_ne_zero H2)) ▸ H1
theorem neg_of_one_div_neg (H : 1 / a < 0) : a < 0 :=
have H1 : 0 < - (1 / a), from neg_pos_of_neg H,
have Ha : a ≠ 0, from ne_zero_of_one_div_ne_zero (ne_of_lt H),
have H2 : 0 < 1 / (-a), from (division_ring.one_div_neg_eq_neg_one_div Ha)⁻¹ ▸ H1,
have H3 : 0 < -a, from pos_of_one_div_pos H2,
neg_of_neg_pos H3
theorem le_of_one_div_le_one_div (H : 0 < a) (Hl : 1 / a ≤ 1 / b) : b ≤ a :=
have Hb : 0 < b, from pos_of_one_div_pos (calc
0 < 1 / a : one_div_pos_of_pos H
... ≤ 1 / b : Hl),
have H' : 1 ≤ a / b, from (calc
1 = a / a : div_self (ne.symm (ne_of_lt H))
... = a * (1 / a) : div_eq_mul_one_div
... ≤ a * (1 / b) : mul_le_mul_of_nonneg_left Hl (le_of_lt H)
... = a / b : div_eq_mul_one_div
), le_of_one_le_div Hb H'
theorem le_of_one_div_le_one_div_of_neg (H : b < 0) (Hl : 1 / a ≤ 1 / b) : b ≤ a :=
assert Ha : a ≠ 0, from ne_of_lt (neg_of_one_div_neg (calc
1 / a ≤ 1 / b : Hl
... < 0 : one_div_neg_of_neg H)),
have H' : -b > 0, from neg_pos_of_neg H,
have Hl' : - (1 / b) ≤ - (1 / a), from neg_le_neg Hl,
have Hl'' : 1 / - b ≤ 1 / - a, from calc
1 / -b = - (1 / b) : by rewrite [division_ring.one_div_neg_eq_neg_one_div (ne_of_lt H)]
... ≤ - (1 / a) : Hl'
... = 1 / -a : by rewrite [division_ring.one_div_neg_eq_neg_one_div Ha],
le_of_neg_le_neg (le_of_one_div_le_one_div H' Hl'')
theorem lt_of_one_div_lt_one_div (H : 0 < a) (Hl : 1 / a < 1 / b) : b < a :=
have Hb : 0 < b, from pos_of_one_div_pos (calc
0 < 1 / a : one_div_pos_of_pos H
... < 1 / b : Hl),
have H : 1 < a / b, from (calc
1 = a / a : div_self (ne.symm (ne_of_lt H))
... = a * (1 / a) : div_eq_mul_one_div
... < a * (1 / b) : mul_lt_mul_of_pos_left Hl H
... = a / b : div_eq_mul_one_div),
lt_of_one_lt_div Hb H
theorem lt_of_one_div_lt_one_div_of_neg (H : b < 0) (Hl : 1 / a < 1 / b) : b < a :=
have H1 : b ≤ a, from le_of_one_div_le_one_div_of_neg H (le_of_lt Hl),
have Hn : b ≠ a, from
(assume Hn' : b = a,
have Hl' : 1 / a = 1 / b, from Hn' ▸ refl _,
absurd Hl' (ne_of_lt Hl)),
lt_of_le_of_ne H1 Hn
theorem one_div_lt_one_div_of_lt (Ha : 0 < a) (H : a < b) : 1 / b < 1 / a :=
lt_of_not_ge
(assume H',
absurd H (not_lt_of_ge (le_of_one_div_le_one_div Ha H')))
theorem one_div_le_one_div_of_le (Ha : 0 < a) (H : a ≤ b) : 1 / b ≤ 1 / a :=
le_of_not_gt
(assume H',
absurd H (not_le_of_gt (lt_of_one_div_lt_one_div Ha H')))
theorem one_div_lt_one_div_of_lt_of_neg (Hb : b < 0) (H : a < b) : 1 / b < 1 / a :=
lt_of_not_ge
(assume H',
absurd H (not_lt_of_ge (le_of_one_div_le_one_div_of_neg Hb H')))
theorem one_div_le_one_div_of_le_of_neg (Hb : b < 0) (H : a ≤ b) : 1 / b ≤ 1 / a :=
le_of_not_gt
(assume H',
absurd H (not_le_of_gt (lt_of_one_div_lt_one_div_of_neg Hb H')))
theorem one_div_le_of_one_div_le_of_pos (Ha : a > 0) (H : 1 / a ≤ b) : 1 / b ≤ a :=
begin
rewrite -(one_div_one_div a),
apply one_div_le_one_div_of_le,
apply one_div_pos_of_pos,
repeat assumption
end
theorem one_div_le_of_one_div_le_of_neg (Ha : b < 0) (H : 1 / a ≤ b) : 1 / b ≤ a :=
begin
rewrite -(one_div_one_div a),
apply one_div_le_one_div_of_le_of_neg,
repeat assumption
end
theorem one_lt_one_div (H1 : 0 < a) (H2 : a < 1) : 1 < 1 / a :=
one_div_one ▸ one_div_lt_one_div_of_lt H1 H2
theorem one_le_one_div (H1 : 0 < a) (H2 : a ≤ 1) : 1 ≤ 1 / a :=
one_div_one ▸ one_div_le_one_div_of_le H1 H2
theorem one_div_lt_neg_one (H1 : a < 0) (H2 : -1 < a) : 1 / a < -1 :=
one_div_neg_one_eq_neg_one ▸ one_div_lt_one_div_of_lt_of_neg H1 H2
theorem one_div_le_neg_one (H1 : a < 0) (H2 : -1 ≤ a) : 1 / a ≤ -1 :=
one_div_neg_one_eq_neg_one ▸ one_div_le_one_div_of_le_of_neg H1 H2
theorem div_lt_div_of_pos_of_lt_of_pos (Hb : 0 < b) (H : b < a) (Hc : 0 < c) : c / a < c / b :=
begin
apply iff.mp !sub_neg_iff_lt,
rewrite [div_eq_mul_one_div, {c / b}div_eq_mul_one_div, -mul_sub_left_distrib],
apply mul_neg_of_pos_of_neg,
exact Hc,
apply iff.mpr !sub_neg_iff_lt,
apply one_div_lt_one_div_of_lt,
repeat assumption
end
theorem div_mul_le_div_mul_of_div_le_div_pos' {d e : A} (H : a / b ≤ c / d)
(He : e > 0) : a / (b * e) ≤ c / (d * e) :=
begin
rewrite [2 div_mul_eq_div_mul_one_div],
apply mul_le_mul_of_nonneg_right H,
apply le_of_lt,
apply one_div_pos_of_pos He
end
theorem abs_one_div (a : A) : abs (1 / a) = 1 / abs a :=
if H : a > 0 then
by rewrite [abs_of_pos H, abs_of_pos (one_div_pos_of_pos H)]
else
(if H' : a < 0 then
by rewrite [abs_of_neg H', abs_of_neg (one_div_neg_of_neg H'),
-(division_ring.one_div_neg_eq_neg_one_div (ne_of_lt H'))]
else
assert Heq : a = 0, from eq_of_le_of_ge (le_of_not_gt H) (le_of_not_gt H'),
by rewrite [Heq, div_zero, *abs_zero, div_zero])
theorem sign_eq_div_abs (a : A) : sign a = a / (abs a) :=
decidable.by_cases
(suppose a = 0, by subst a; rewrite [zero_div, sign_zero])
(suppose a ≠ 0,
have abs a ≠ 0, from assume H, this (eq_zero_of_abs_eq_zero H),
!eq_div_of_mul_eq this !eq_sign_mul_abs⁻¹)
theorem add_quarters (a : A) : a / 4 + a / 4 = a / 2 :=
have H4 [visible] : (4 : A) = 2 * 2, by norm_num,
calc
a / 4 + a / 4 = (a + a) / (2 * 2) : by rewrite [-H4, div_add_div_same]
... = (a * 1 + a * 1) / (2 * 2) : by rewrite mul_one
... = (a * (1 + 1)) / (2 * 2) : by rewrite left_distrib
... = (a * 2) / (2 * 2) : rfl
... = ((a * 2) / 2) / 2 : by rewrite -div_div_eq_div_mul
... = a / 2 : by rewrite (mul_div_cancel a two_ne_zero)
lemma div_two_add_div_four_lt {a : A} (H : a > 0) : a / 2 + a / 4 < a :=
begin
replace (4 : A) with (2 : A) + 2,
have Hne : (2 + 2 : A) ≠ 0, from ne_of_gt four_pos,
krewrite (div_add_div _ _ two_ne_zero Hne),
have Hnum : (2 + 2 + 2) / (2 * (2 + 2)) = (3 : A) / 4, by norm_num,
rewrite [{2 * a}mul.comm, -left_distrib, mul_div_assoc, -mul_one a at {2}], krewrite Hnum,
apply mul_lt_mul_of_pos_left,
apply div_lt_of_mul_lt_of_pos,
apply four_pos,
rewrite one_mul,
replace (3 : A) with (2 : A) + 1,
replace (4 : A) with (2 : A) + 2,
apply add_lt_add_left,
apply two_gt_one,
exact H
end
end discrete_linear_ordered_field
|
08af2cf5a2196171d354afe0c975ad61d4bcb637 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/list/nat_antidiagonal.lean | 0ec67b3cdc549cd20540fd7eb6879e5447036eb2 | [
"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,191 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.list.range
/-!
# Antidiagonals in ℕ × ℕ as lists
This file defines the antidiagonals of ℕ × ℕ as lists: the `n`-th antidiagonal is the list of
pairs `(i, j)` such that `i + j = n`. This is useful for polynomial multiplication and more
generally for sums going from `0` to `n`.
## Notes
Files `data.multiset.nat_antidiagonal` and `data.finset.nat_antidiagonal` successively turn the
`list` definition we have here into `multiset` and `finset`.
-/
open list function nat
namespace list
namespace nat
/-- The antidiagonal of a natural number `n` is the list of pairs `(i, j)` such that `i + j = n`. -/
def antidiagonal (n : ℕ) : list (ℕ × ℕ) :=
(range (n+1)).map (λ i, (i, n - i))
/-- A pair (i, j) is contained in the antidiagonal of `n` if and only if `i + j = n`. -/
@[simp] lemma mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} :
x ∈ antidiagonal n ↔ x.1 + x.2 = n :=
begin
rw [antidiagonal, mem_map], split,
{ rintros ⟨i, hi, rfl⟩, rw [mem_range, lt_succ_iff] at hi, exact add_tsub_cancel_of_le hi },
{ rintro rfl, refine ⟨x.fst, _, _⟩,
{ rw [mem_range, add_assoc, lt_add_iff_pos_right], exact zero_lt_succ _ },
{ exact prod.ext rfl (add_tsub_cancel_left _ _) } }
end
/-- The length of the antidiagonal of `n` is `n + 1`. -/
@[simp] lemma length_antidiagonal (n : ℕ) : (antidiagonal n).length = n+1 :=
by rw [antidiagonal, length_map, length_range]
/-- The antidiagonal of `0` is the list `[(0, 0)]` -/
@[simp] lemma antidiagonal_zero : antidiagonal 0 = [(0, 0)] :=
rfl
/-- The antidiagonal of `n` does not contain duplicate entries. -/
lemma nodup_antidiagonal (n : ℕ) : nodup (antidiagonal n) :=
(nodup_range _).map (@left_inverse.injective ℕ (ℕ × ℕ) prod.fst (λ i, (i, n-i)) $ λ i, rfl)
@[simp] lemma antidiagonal_succ {n : ℕ} :
antidiagonal (n + 1) = (0, n + 1) :: ((antidiagonal n).map (prod.map nat.succ id)) :=
begin
simp only [antidiagonal, range_succ_eq_map, map_cons, true_and, nat.add_succ_sub_one, add_zero,
id.def, eq_self_iff_true, tsub_zero, map_map, prod.map_mk],
apply congr (congr rfl _) rfl,
ext; simp,
end
lemma antidiagonal_succ' {n : ℕ} :
antidiagonal (n + 1) = ((antidiagonal n).map (prod.map id nat.succ)) ++ [(n + 1, 0)] :=
begin
simp only [antidiagonal, range_succ, add_tsub_cancel_left, map_append,
append_assoc, tsub_self, singleton_append, map_map, map],
congr' 1,
apply map_congr,
simp [le_of_lt, nat.succ_eq_add_one, nat.sub_add_comm] { contextual := tt },
end
lemma antidiagonal_succ_succ' {n : ℕ} :
antidiagonal (n + 2) =
(0, n + 2) :: ((antidiagonal n).map (prod.map nat.succ nat.succ)) ++ [(n + 2, 0)] :=
by { rw antidiagonal_succ', simpa }
lemma map_swap_antidiagonal {n : ℕ} :
(antidiagonal n).map prod.swap = (antidiagonal n).reverse :=
begin
rw [antidiagonal, map_map, prod.swap, ← list.map_reverse,
range_eq_range', reverse_range', ← range_eq_range', map_map],
apply map_congr,
simp [nat.sub_sub_self, lt_succ_iff] { contextual := tt },
end
end nat
end list
|
c1894424251f8ad02d77a605845b7947ee3579bc | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /stage0/src/Lean/Elab/Tactic/Rewrite.lean | 70a7061cb14e0f15b5e9ca3486c05c950a75bfd4 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 3,769 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Rewrite
import Lean.Meta.Tactic.Replace
import Lean.Elab.Tactic.Basic
import Lean.Elab.Tactic.ElabTerm
import Lean.Elab.Tactic.Location
namespace Lean.Elab.Tactic
open Meta
private def expand (kind : SyntaxNodeKind) (token : String) (stx : Syntax) : Syntax := do
let rwRules := stx[1][1].getSepArgs
let loc := stx[2]
let mut newTacs := #[]
for i in [:rwRules.size] do
let rwRule := rwRules[i]
-- We use `stx` as "position provider" for the first rule
-- Without this change, we don't get correct information when we hover over `rw`/`rewrite`/`erewrite` with multiple rewrites
let ref ← if i == 0 then stx else pure rwRule
newTacs := newTacs.push <| Syntax.node kind #[mkAtomFrom ref token, rwRule, loc]
return mkNullNode newTacs
@[builtinMacro Lean.Parser.Tactic.rewriteSeq] def expandRewriteTactic : Macro := fun stx =>
return expand ``Parser.Tactic.rewrite "rewrite " stx
@[builtinMacro Lean.Parser.Tactic.erewriteSeq] def expandERewriteTactic : Macro := fun stx =>
return expand ``Parser.Tactic.erewrite "erewrite " stx
def rewriteTarget (stx : Syntax) (symm : Bool) (mode : TransparencyMode) : TacticM Unit := do
Term.withSynthesize <| withMainContext do
let e ← elabTerm stx none true
let r ← rewrite (← getMainGoal) (← getMainTarget) e symm (mode := mode)
let mvarId' ← replaceTargetEq (← getMainGoal) r.eNew r.eqProof
replaceMainGoal (mvarId' :: r.mvarIds)
def rewriteLocalDeclFVarId (stx : Syntax) (symm : Bool) (fvarId : FVarId) (mode : TransparencyMode) : TacticM Unit := do
Term.withSynthesize <| withMainContext do
let e ← elabTerm stx none true
let localDecl ← getLocalDecl fvarId
let rwResult ← rewrite (← getMainGoal) localDecl.type e symm (mode := mode)
let replaceResult ← replaceLocalDecl (← getMainGoal) fvarId rwResult.eNew rwResult.eqProof
replaceMainGoal (replaceResult.mvarId :: rwResult.mvarIds)
def rewriteLocalDecl (stx : Syntax) (symm : Bool) (userName : Name) (mode : TransparencyMode) : TacticM Unit :=
withMainContext do
let localDecl ← getLocalDeclFromUserName userName
rewriteLocalDeclFVarId stx symm localDecl.fvarId mode
def rewriteAll (stx : Syntax) (symm : Bool) (mode : TransparencyMode) : TacticM Unit := do
let worked ← tryTactic <| rewriteTarget stx symm mode
withMainContext do
let mut worked := worked
-- We must traverse backwards because `replaceLocalDecl` uses the revert/intro idiom
for fvarId in (← getLCtx).getFVarIds.reverse do
worked := worked || (← tryTactic <| rewriteLocalDeclFVarId stx symm fvarId mode)
unless worked do
let mvarId ← getMainGoal
throwTacticEx `rewrite mvarId "did not find instance of the pattern in the current goal"
def evalRewriteCore (mode : TransparencyMode) : Tactic := fun stx => do
let rule := stx[1]
let symm := !rule[0].isNone
let term := rule[1]
let loc := expandOptLocation stx[2]
match loc with
| Location.targets hyps type =>
hyps.forM (rewriteLocalDecl term symm · mode)
if type then
rewriteTarget term symm mode
| Location.wildcard => rewriteAll term symm mode
/-
```
def rwRule := leading_parser optional (unicodeSymbol "←" "<-") >> termParser
def «rewrite» := leading_parser "rewrite" >> rwRule >> optional location
```
-/
@[builtinTactic Lean.Parser.Tactic.rewrite] def evalRewrite : Tactic :=
evalRewriteCore TransparencyMode.instances
@[builtinTactic Lean.Parser.Tactic.erewrite] def evalERewrite : Tactic :=
evalRewriteCore TransparencyMode.default
end Lean.Elab.Tactic
|
848ae28277a3cc66a406916c09a5f2fd316b692b | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/number_theory/lucas_lehmer.lean | c502ed324605bf50447d60e73dc40720e2462461 | [
"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 | 17,252 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Scott Morrison, Ainsley Pahljina
-/
import tactic.ring_exp
import tactic.interval_cases
import data.nat.parity
import data.zmod.basic
import group_theory.order_of_element
import ring_theory.fintype
/-!
# The Lucas-Lehmer test for Mersenne primes.
We define `lucas_lehmer_residue : Π p : ℕ, zmod (2^p - 1)`, and
prove `lucas_lehmer_residue p = 0 → prime (mersenne p)`.
We construct a tactic `lucas_lehmer.run_test`, which iteratively certifies the arithmetic
required to calculate the residue, and enables us to prove
```
example : prime (mersenne 127) :=
lucas_lehmer_sufficiency _ (by norm_num) (by lucas_lehmer.run_test)
```
## TODO
- Show reverse implication.
- Speed up the calculations using `n ≡ (n % 2^p) + (n / 2^p) [MOD 2^p - 1]`.
- Find some bigger primes!
## History
This development began as a student project by Ainsley Pahljina,
and was then cleaned up for mathlib by Scott Morrison.
The tactic for certified computation of Lucas-Lehmer residues was provided by Mario Carneiro.
-/
/-- The Mersenne numbers, 2^p - 1. -/
def mersenne (p : ℕ) : ℕ := 2^p - 1
lemma mersenne_pos {p : ℕ} (h : 0 < p) : 0 < mersenne p :=
begin
dsimp [mersenne],
calc 0 < 2^1 - 1 : by norm_num
... ≤ 2^p - 1 : nat.pred_le_pred (nat.pow_le_pow_of_le_right (nat.succ_pos 1) h)
end
@[simp]
lemma succ_mersenne (k : ℕ) : mersenne k + 1 = 2 ^ k :=
begin
rw [mersenne, nat.sub_add_cancel],
exact one_le_pow_of_one_le (by norm_num) k
end
namespace lucas_lehmer
open nat
/-!
We now define three(!) different versions of the recurrence
`s (i+1) = (s i)^2 - 2`.
These versions take values either in `ℤ`, in `zmod (2^p - 1)`, or
in `ℤ` but applying `% (2^p - 1)` at each step.
They are each useful at different points in the proof,
so we take a moment setting up the lemmas relating them.
-/
/-- The recurrence `s (i+1) = (s i)^2 - 2` in `ℤ`. -/
def s : ℕ → ℤ
| 0 := 4
| (i+1) := (s i)^2 - 2
/-- The recurrence `s (i+1) = (s i)^2 - 2` in `zmod (2^p - 1)`. -/
def s_zmod (p : ℕ) : ℕ → zmod (2^p - 1)
| 0 := 4
| (i+1) := (s_zmod i)^2 - 2
/-- The recurrence `s (i+1) = ((s i)^2 - 2) % (2^p - 1)` in `ℤ`. -/
def s_mod (p : ℕ) : ℕ → ℤ
| 0 := 4 % (2^p - 1)
| (i+1) := ((s_mod i)^2 - 2) % (2^p - 1)
lemma mersenne_int_ne_zero (p : ℕ) (w : 0 < p) : (2^p - 1 : ℤ) ≠ 0 :=
begin
apply ne_of_gt, simp only [gt_iff_lt, sub_pos],
exact_mod_cast nat.one_lt_two_pow p w,
end
lemma s_mod_nonneg (p : ℕ) (w : 0 < p) (i : ℕ) : 0 ≤ s_mod p i :=
begin
cases i; dsimp [s_mod],
{ trivial, },
{ apply int.mod_nonneg, exact mersenne_int_ne_zero p w },
end
lemma s_mod_mod (p i : ℕ) : s_mod p i % (2^p - 1) = s_mod p i :=
by cases i; simp [s_mod]
lemma s_mod_lt (p : ℕ) (w : 0 < p) (i : ℕ) : s_mod p i < 2^p - 1 :=
begin
rw ←s_mod_mod,
convert int.mod_lt _ _,
{ refine (abs_of_nonneg _).symm,
simp only [sub_nonneg, ge_iff_le],
exact_mod_cast nat.one_le_two_pow p, },
{ exact mersenne_int_ne_zero p w, },
end
lemma s_zmod_eq_s (p' : ℕ) (i : ℕ) : s_zmod (p'+2) i = (s i : zmod (2^(p'+2) - 1)):=
begin
induction i with i ih,
{ dsimp [s, s_zmod], norm_num, },
{ push_cast [s, s_zmod, ih] },
end
-- These next two don't make good `norm_cast` lemmas.
lemma int.coe_nat_pow_pred (b p : ℕ) (w : 0 < b) : ((b^p - 1 : ℕ) : ℤ) = (b^p - 1 : ℤ) :=
begin
have : 1 ≤ b^p := nat.one_le_pow p b w,
push_cast [this],
end
lemma int.coe_nat_two_pow_pred (p : ℕ) : ((2^p - 1 : ℕ) : ℤ) = (2^p - 1 : ℤ) :=
int.coe_nat_pow_pred 2 p dec_trivial
lemma s_zmod_eq_s_mod (p : ℕ) (i : ℕ) : s_zmod p i = (s_mod p i : zmod (2^p - 1)) :=
by induction i; push_cast [←int.coe_nat_two_pow_pred p, s_mod, s_zmod, *]
/-- The Lucas-Lehmer residue is `s p (p-2)` in `zmod (2^p - 1)`. -/
def lucas_lehmer_residue (p : ℕ) : zmod (2^p - 1) := s_zmod p (p-2)
lemma residue_eq_zero_iff_s_mod_eq_zero (p : ℕ) (w : 1 < p) :
lucas_lehmer_residue p = 0 ↔ s_mod p (p-2) = 0 :=
begin
dsimp [lucas_lehmer_residue],
rw s_zmod_eq_s_mod p,
split,
{ -- We want to use that fact that `0 ≤ s_mod p (p-2) < 2^p - 1`
-- and `lucas_lehmer_residue p = 0 → 2^p - 1 ∣ s_mod p (p-2)`.
intro h,
simp [zmod.int_coe_zmod_eq_zero_iff_dvd] at h,
apply int.eq_zero_of_dvd_of_nonneg_of_lt _ _ h; clear h,
apply s_mod_nonneg _ (nat.lt_of_succ_lt w),
convert s_mod_lt _ (nat.lt_of_succ_lt w) (p-2),
push_cast [nat.one_le_two_pow p],
refl, },
{ intro h, rw h, simp, },
end
/--
A Mersenne number `2^p-1` is prime if and only if
the Lucas-Lehmer residue `s p (p-2) % (2^p - 1)` is zero.
-/
@[derive decidable_pred]
def lucas_lehmer_test (p : ℕ) : Prop := lucas_lehmer_residue p = 0
/-- `q` is defined as the minimum factor of `mersenne p`, bundled as an `ℕ+`. -/
def q (p : ℕ) : ℕ+ := ⟨nat.min_fac (mersenne p), nat.min_fac_pos (mersenne p)⟩
instance fact_pnat_pos (q : ℕ+) : fact (0 < (q : ℕ)) :=
q.2
/-- We construct the ring `X q` as ℤ/qℤ + √3 ℤ/qℤ. -/
-- It would be nice to define this as (ℤ/qℤ)[x] / (x^2 - 3),
-- obtaining the ring structure for free,
-- but that seems to be more trouble than it's worth;
-- if it were easy to make the definition,
-- cardinality calculations would be somewhat more involved, too.
@[derive [add_comm_group, decidable_eq, fintype, inhabited]]
def X (q : ℕ+) : Type := (zmod q) × (zmod q)
namespace X
variable {q : ℕ+}
@[ext]
lemma ext {x y : X q} (h₁ : x.1 = y.1) (h₂ : x.2 = y.2) : x = y :=
begin
cases x, cases y,
congr; assumption
end
@[simp] lemma add_fst (x y : X q) : (x + y).1 = x.1 + y.1 := rfl
@[simp] lemma add_snd (x y : X q) : (x + y).2 = x.2 + y.2 := rfl
@[simp] lemma neg_fst (x : X q) : (-x).1 = -x.1 := rfl
@[simp] lemma neg_snd (x : X q) : (-x).2 = -x.2 := rfl
instance : has_mul (X q) :=
{ mul := λ x y, (x.1*y.1 + 3*x.2*y.2, x.1*y.2 + x.2*y.1) }
@[simp] lemma mul_fst (x y : X q) : (x * y).1 = x.1 * y.1 + 3 * x.2 * y.2 := rfl
@[simp] lemma mul_snd (x y : X q) : (x * y).2 = x.1 * y.2 + x.2 * y.1 := rfl
instance : has_one (X q) :=
{ one := ⟨1,0⟩ }
@[simp] lemma one_fst : (1 : X q).1 = 1 := rfl
@[simp] lemma one_snd : (1 : X q).2 = 0 := rfl
@[simp] lemma bit0_fst (x : X q) : (bit0 x).1 = bit0 x.1 := rfl
@[simp] lemma bit0_snd (x : X q) : (bit0 x).2 = bit0 x.2 := rfl
@[simp] lemma bit1_fst (x : X q) : (bit1 x).1 = bit1 x.1 := rfl
@[simp] lemma bit1_snd (x : X q) : (bit1 x).2 = bit0 x.2 := by { dsimp [bit1], simp, }
instance : monoid (X q) :=
{ mul_assoc := λ x y z, by { ext; { dsimp, ring }, },
one := ⟨1,0⟩,
one_mul := λ x, by { ext; simp, },
mul_one := λ x, by { ext; simp, },
..(infer_instance : has_mul (X q)) }
lemma left_distrib (x y z : X q) : x * (y + z) = x * y + x * z :=
by { ext; { dsimp, ring }, }
lemma right_distrib (x y z : X q) : (x + y) * z = x * z + y * z :=
by { ext; { dsimp, ring }, }
instance : ring (X q) :=
{ left_distrib := left_distrib,
right_distrib := right_distrib,
..(infer_instance : add_comm_group (X q)),
..(infer_instance : monoid (X q)) }
instance : comm_ring (X q) :=
{ mul_comm := λ x y, by { ext; { dsimp, ring }, },
..(infer_instance : ring (X q))}
instance [fact (1 < (q : ℕ))] : nontrivial (X q) :=
⟨⟨0, 1, λ h, by { injection h with h1 _, exact zero_ne_one h1 } ⟩⟩
@[simp]
lemma nat_coe_fst (n : ℕ) : (n : X q).fst = (n : zmod q) :=
begin
induction n,
{ refl, },
{ dsimp, simp only [add_left_inj], exact n_ih, }
end
@[simp]
lemma nat_coe_snd (n : ℕ) : (n : X q).snd = (0 : zmod q) :=
begin
induction n,
{ refl, },
{ dsimp, simp only [add_zero], exact n_ih, }
end
@[simp]
lemma int_coe_fst (n : ℤ) : (n : X q).fst = (n : zmod q) :=
by { induction n; simp, }
@[simp]
lemma int_coe_snd (n : ℤ) : (n : X q).snd = (0 : zmod q) :=
by { induction n; simp, }
@[norm_cast]
lemma coe_mul (n m : ℤ) : ((n * m : ℤ) : X q) = (n : X q) * (m : X q) :=
by { ext; simp; ring }
@[norm_cast]
lemma coe_nat (n : ℕ) : ((n : ℤ) : X q) = (n : X q) :=
by { ext; simp, }
/-- The cardinality of `X` is `q^2`. -/
lemma X_card : fintype.card (X q) = q^2 :=
begin
dsimp [X],
rw [fintype.card_prod, zmod.card q],
ring,
end
/-- There are strictly fewer than `q^2` units, since `0` is not a unit. -/
lemma units_card (w : 1 < q) : fintype.card (units (X q)) < q^2 :=
begin
haveI : fact (1 < (q : ℕ)) := w,
convert card_units_lt (X q),
rw X_card,
end
/-- We define `ω = 2 + √3`. -/
def ω : X q := (2, 1)
/-- We define `ωb = 2 - √3`, which is the inverse of `ω`. -/
def ωb : X q := (2, -1)
lemma ω_mul_ωb (q : ℕ+) : (ω : X q) * ωb = 1 :=
begin
dsimp [ω, ωb],
ext; simp; ring,
end
lemma ωb_mul_ω (q : ℕ+) : (ωb : X q) * ω = 1 :=
begin
dsimp [ω, ωb],
ext; simp; ring,
end
/-- A closed form for the recurrence relation. -/
lemma closed_form (i : ℕ) : (s i : X q) = (ω : X q)^(2^i) + (ωb : X q)^(2^i) :=
begin
induction i with i ih,
{ dsimp [s, ω, ωb],
ext; { simp; refl, }, },
{ calc (s (i + 1) : X q) = ((s i)^2 - 2 : ℤ) : rfl
... = ((s i : X q)^2 - 2) : by push_cast
... = (ω^(2^i) + ωb^(2^i))^2 - 2 : by rw ih
... = (ω^(2^i))^2 + (ωb^(2^i))^2 + 2*(ωb^(2^i)*ω^(2^i)) - 2 : by ring
... = (ω^(2^i))^2 + (ωb^(2^i))^2 :
by rw [←mul_pow ωb ω, ωb_mul_ω, one_pow, mul_one, add_sub_cancel]
... = ω^(2^(i+1)) + ωb^(2^(i+1)) : by rw [←pow_mul, ←pow_mul, pow_succ'] }
end
end X
open X
/-!
Here and below, we introduce `p' = p - 2`, in order to avoid using subtraction in `ℕ`.
-/
/-- If `1 < p`, then `q p`, the smallest prime factor of `mersenne p`, is more than 2. -/
lemma two_lt_q (p' : ℕ) : 2 < q (p'+2) := begin
by_contradiction,
simp at a,
interval_cases q (p'+2); clear a,
{ -- If q = 1, we get a contradiction from 2^p = 2
dsimp [q] at h, injection h with h', clear h,
simp [mersenne] at h',
exact lt_irrefl 2
(calc 2 ≤ p'+2 : nat.le_add_left _ _
... < 2^(p'+2) : nat.lt_two_pow _
... = 2 : nat.pred_inj (nat.one_le_two_pow _) dec_trivial h'), },
{ -- If q = 2, we get a contradiction from 2 ∣ 2^p - 1
dsimp [q] at h, injection h with h', clear h,
rw [mersenne, pnat.one_coe, nat.min_fac_eq_two_iff, pow_succ] at h',
exact nat.two_not_dvd_two_mul_sub_one (nat.one_le_two_pow _) h', }
end
theorem ω_pow_formula (p' : ℕ) (h : lucas_lehmer_residue (p'+2) = 0) :
∃ (k : ℤ), (ω : X (q (p'+2)))^(2^(p'+1)) =
k * (mersenne (p'+2)) * ((ω : X (q (p'+2)))^(2^p')) - 1 :=
begin
dsimp [lucas_lehmer_residue] at h,
rw s_zmod_eq_s p' at h,
simp [zmod.int_coe_zmod_eq_zero_iff_dvd] at h,
cases h with k h,
use k,
replace h := congr_arg (λ (n : ℤ), (n : X (q (p'+2)))) h, -- coercion from ℤ to X q
dsimp at h,
rw closed_form at h,
replace h := congr_arg (λ x, ω^2^p' * x) h,
dsimp at h,
have t : 2^p' + 2^p' = 2^(p'+1) := by ring_exp,
rw [mul_add, ←pow_add ω, t, ←mul_pow ω ωb (2^p'), ω_mul_ωb, one_pow] at h,
rw [mul_comm, coe_mul] at h,
rw [mul_comm _ (k : X (q (p'+2)))] at h,
replace h := eq_sub_of_add_eq h,
exact_mod_cast h,
end
/-- `q` is the minimum factor of `mersenne p`, so `M p = 0` in `X q`. -/
theorem mersenne_coe_X (p : ℕ) : (mersenne p : X (q p)) = 0 :=
begin
ext; simp [mersenne, q, zmod.nat_coe_zmod_eq_zero_iff_dvd, -pow_pos],
apply nat.min_fac_dvd,
end
theorem ω_pow_eq_neg_one (p' : ℕ) (h : lucas_lehmer_residue (p'+2) = 0) :
(ω : X (q (p'+2)))^(2^(p'+1)) = -1 :=
begin
cases ω_pow_formula p' h with k w,
rw [mersenne_coe_X] at w,
simpa using w,
end
theorem ω_pow_eq_one (p' : ℕ) (h : lucas_lehmer_residue (p'+2) = 0) :
(ω : X (q (p'+2)))^(2^(p'+2)) = 1 :=
calc (ω : X (q (p'+2)))^2^(p'+2)
= (ω^(2^(p'+1)))^2 : by rw [←pow_mul, ←pow_succ']
... = (-1)^2 : by rw ω_pow_eq_neg_one p' h
... = 1 : by simp
/-- `ω` as an element of the group of units. -/
def ω_unit (p : ℕ) : units (X (q p)) :=
{ val := ω,
inv := ωb,
val_inv := by simp [ω_mul_ωb],
inv_val := by simp [ωb_mul_ω], }
@[simp] lemma ω_unit_coe (p : ℕ) : (ω_unit p : X (q p)) = ω := rfl
/-- The order of `ω` in the unit group is exactly `2^p`. -/
theorem order_ω (p' : ℕ) (h : lucas_lehmer_residue (p'+2) = 0) :
order_of (ω_unit (p'+2)) = 2^(p'+2) :=
begin
apply nat.eq_prime_pow_of_dvd_least_prime_pow, -- the order of ω divides 2^p
{ norm_num, },
{ intro o,
have ω_pow := order_of_dvd_iff_pow_eq_one.1 o,
replace ω_pow := congr_arg (units.coe_hom (X (q (p'+2))) : units (X (q (p'+2))) → X (q (p'+2))) ω_pow,
simp at ω_pow,
have h : (1 : zmod (q (p'+2))) = -1 :=
congr_arg (prod.fst) ((ω_pow.symm).trans (ω_pow_eq_neg_one p' h)),
haveI : fact (2 < (q (p'+2) : ℕ)) := two_lt_q _,
apply zmod.neg_one_ne_one h.symm, },
{ apply order_of_dvd_iff_pow_eq_one.2,
apply units.ext,
push_cast,
exact ω_pow_eq_one p' h, }
end
lemma order_ineq (p' : ℕ) (h : lucas_lehmer_residue (p'+2) = 0) : 2^(p'+2) < (q (p'+2) : ℕ)^2 :=
calc 2^(p'+2) = order_of (ω_unit (p'+2)) : (order_ω p' h).symm
... ≤ fintype.card (units (X _)) : order_of_le_card_univ
... < (q (p'+2) : ℕ)^2 : units_card (nat.lt_of_succ_lt (two_lt_q _))
end lucas_lehmer
export lucas_lehmer (lucas_lehmer_test lucas_lehmer_residue)
open lucas_lehmer
theorem lucas_lehmer_sufficiency (p : ℕ) (w : 1 < p) : lucas_lehmer_test p → (mersenne p).prime :=
begin
let p' := p - 2,
have z : p = p' + 2 := (nat.sub_eq_iff_eq_add w).mp rfl,
have w : 1 < p' + 2 := (nat.lt_of_sub_eq_succ rfl),
contrapose,
intros a t,
rw z at a,
rw z at t,
have h₁ := order_ineq p' t,
have h₂ := nat.min_fac_sq_le_self (mersenne_pos (nat.lt_of_succ_lt w)) a,
have h := lt_of_lt_of_le h₁ h₂,
exact not_lt_of_ge (nat.sub_le _ _) h,
end
-- Here we calculate the residue, very inefficiently, using `dec_trivial`. We can do much better.
example : (mersenne 5).prime := lucas_lehmer_sufficiency 5 (by norm_num) dec_trivial
-- Next we use `norm_num` to calculate each `s p i`.
namespace lucas_lehmer
open tactic
meta instance nat_pexpr : has_to_pexpr ℕ := ⟨pexpr.of_expr ∘ λ n, reflect n⟩
meta instance int_pexpr : has_to_pexpr ℤ := ⟨pexpr.of_expr ∘ λ n, reflect n⟩
lemma s_mod_succ {p a i b c}
(h1 : (2^p - 1 : ℤ) = a)
(h2 : s_mod p i = b)
(h3 : (b * b - 2) % a = c) :
s_mod p (i+1) = c :=
by { dsimp [s_mod, mersenne], rw [h1, h2, pow_two, h3] }
/--
Given a goal of the form `lucas_lehmer_test p`,
attempt to do the calculation using `norm_num` to certify each step.
-/
meta def run_test : tactic unit :=
do `(lucas_lehmer_test %%p) ← target,
`[dsimp [lucas_lehmer_test]],
`[rw lucas_lehmer.residue_eq_zero_iff_s_mod_eq_zero, swap, norm_num],
p ← eval_expr ℕ p,
-- Calculate the candidate Mersenne prime
let M : ℤ := 2^p - 1,
t ← to_expr ``(2^%%p - 1 = %%M),
v ← to_expr ``(by norm_num : 2^%%p - 1 = %%M),
w ← assertv `w t v,
-- Unfortunately this creates something like `w : 2^5 - 1 = int.of_nat 31`.
-- We could make a better `has_to_pexpr ℤ` instance, or just:
`[simp only [int.coe_nat_zero, int.coe_nat_succ,
int.of_nat_eq_coe, zero_add, int.coe_nat_bit1] at w],
-- base case
t ← to_expr ``(s_mod %%p 0 = 4),
v ← to_expr ``(by norm_num [lucas_lehmer.s_mod] : s_mod %%p 0 = 4),
h ← assertv `h t v,
-- step case, repeated p-2 times
iterate_exactly (p-2) `[replace h := lucas_lehmer.s_mod_succ w h (by { norm_num, refl })],
-- now close the goal
h ← get_local `h,
exact h
end lucas_lehmer
/-- We verify that the tactic works to prove `127.prime`. -/
example : (mersenne 7).prime := lucas_lehmer_sufficiency _ (by norm_num) (by lucas_lehmer.run_test).
/-!
This implementation works successfully to prove `(2^127 - 1).prime`,
and all the Mersenne primes up to this point appear in [archive/examples/mersenne_primes.lean].
`(2^127 - 1).prime` takes about 5 minutes to run (depending on your CPU!),
and unfortunately the next Mersenne prime `(2^521 - 1)`,
which was the first "computer era" prime,
is out of reach with the current implementation.
There's still low hanging fruit available to do faster computations
based on the formula
n ≡ (n % 2^p) + (n / 2^p) [MOD 2^p - 1]
and the fact that `% 2^p` and `/ 2^p` can be very efficient on the binary representation.
Someone should do this, too!
-/
lemma modeq_mersenne (n k : ℕ) : k ≡ ((k / 2^n) + (k % 2^n)) [MOD 2^n - 1] :=
-- See https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/help.20finding.20a.20lemma/near/177698446
begin
conv in k {rw [← nat.mod_add_div k (2^n), add_comm]},
refine nat.modeq.modeq_add _ (by refl),
conv {congr, skip, skip, rw ← one_mul (k/2^n)},
refine nat.modeq.modeq_mul _ (by refl),
symmetry,
rw [nat.modeq.modeq_iff_dvd, int.coe_nat_sub],
exact pow_pos (show 0 < 2, from dec_trivial) _
end
-- It's hard to know what the limiting factor for large Mersenne primes would be.
-- In the purely computational world, I think it's the squaring operation in `s`.
|
9ab8777013ad195a5636dc8ef56bf8f01823b06a | 54d7e71c3616d331b2ec3845d31deb08f3ff1dea | /tests/lean/run/at_at_bug.lean | f27860ec1ca8983361e82b35ffc692af05527a9d | [
"Apache-2.0"
] | permissive | pachugupta/lean | 6f3305c4292288311cc4ab4550060b17d49ffb1d | 0d02136a09ac4cf27b5c88361750e38e1f485a1a | refs/heads/master | 1,611,110,653,606 | 1,493,130,117,000 | 1,493,167,649,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 384 | lean |
example (a b : nat) (p : nat → nat → Prop) (h₁ : p a b) (h₂ : a = b) : p b b :=
@@eq.subst (λ x, p x b) h₂ h₁
set_option pp.all true
variable my_has_add : has_add nat
#check @@add my_has_add 0 1
local notation h1 `▸[` m `]` h2 := @@eq.subst m h1 h2
example (a b : nat) (p : nat → nat → Prop) (h₁ : p a b) (h₂ : a = b) : p b b :=
h₂ ▸[λ x, p x b] h₁
|
882b93679016c42056a21adf766afb611c67538c | 94e33a31faa76775069b071adea97e86e218a8ee | /src/model_theory/finitely_generated.lean | dce01734c2433fa64f1fa2f081e9aba8f6e14440 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 9,379 | lean | /-
Copyright (c) 2022 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import model_theory.substructures
/-!
# Finitely Generated First-Order Structures
This file defines what it means for a first-order (sub)structure to be finitely or countably
generated, similarly to other finitely-generated objects in the algebra library.
## Main Definitions
* `first_order.language.substructure.fg` indicates that a substructure is finitely generated.
* `first_order.language.Structure.fg` indicates that a structure is finitely generated.
* `first_order.language.substructure.cg` indicates that a substructure is countably generated.
* `first_order.language.Structure.cg` indicates that a structure is countably generated.
## TODO
Develop a more unified definition of finite generation using the theory of closure operators, or use
this definition of finite generation to define the others.
-/
open_locale first_order
open set
namespace first_order
namespace language
open Structure
variables {L : language} {M : Type*} [L.Structure M]
namespace substructure
/-- A substructure of `M` is finitely generated if it is the closure of a finite subset of `M`. -/
def fg (N : L.substructure M) : Prop := ∃ S : finset M, closure L ↑S = N
theorem fg_def {N : L.substructure M} :
N.fg ↔ ∃ S : set M, S.finite ∧ closure L S = N :=
⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin
rintro ⟨t', h, rfl⟩,
rcases finite.exists_finset_coe h with ⟨t, rfl⟩,
exact ⟨t, rfl⟩
end⟩
lemma fg_iff_exists_fin_generating_family {N : L.substructure M} :
N.fg ↔ ∃ (n : ℕ) (s : fin n → M), closure L (range s) = N :=
begin
rw fg_def,
split,
{ rintros ⟨S, Sfin, hS⟩,
obtain ⟨n, f, rfl⟩ := Sfin.fin_embedding,
exact ⟨n, f, hS⟩, },
{ rintros ⟨n, s, hs⟩,
refine ⟨range s, finite_range s, hs⟩ },
end
theorem fg_bot : (⊥ : L.substructure M).fg :=
⟨∅, by rw [finset.coe_empty, closure_empty]⟩
theorem fg_closure {s : set M} (hs : s.finite) : fg (closure L s) :=
⟨hs.to_finset, by rw [hs.coe_to_finset]⟩
theorem fg_closure_singleton (x : M) : fg (closure L ({x} : set M)) :=
fg_closure (finite_singleton x)
theorem fg.sup {N₁ N₂ : L.substructure M}
(hN₁ : N₁.fg) (hN₂ : N₂.fg) : (N₁ ⊔ N₂).fg :=
let ⟨t₁, ht₁⟩ := fg_def.1 hN₁, ⟨t₂, ht₂⟩ := fg_def.1 hN₂ in
fg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [closure_union, ht₁.2, ht₂.2]⟩
theorem fg.map {N : Type*} [L.Structure N] (f : M →[L] N) {s : L.substructure M} (hs : s.fg) :
(s.map f).fg :=
let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, ht.1.image _, by rw [closure_image, ht.2]⟩
theorem fg.of_map_embedding {N : Type*} [L.Structure N] (f : M ↪[L] N) {s : L.substructure M}
(hs : (s.map f.to_hom).fg) : s.fg :=
begin
rcases hs with ⟨t, h⟩,
rw fg_def,
refine ⟨f ⁻¹' t, t.finite_to_set.preimage (f.injective.inj_on _), _⟩,
have hf : function.injective f.to_hom := f.injective,
refine map_injective_of_injective hf _,
rw [← h, map_closure, embedding.coe_to_hom, image_preimage_eq_of_subset],
intros x hx,
have h' := subset_closure hx,
rw h at h',
exact hom.map_le_range h'
end
/-- A substructure of `M` is countably generated if it is the closure of a countable subset of `M`.
-/
def cg (N : L.substructure M) : Prop := ∃ S : set M, S.countable ∧ closure L S = N
theorem cg_def {N : L.substructure M} :
N.cg ↔ ∃ S : set M, S.countable ∧ closure L S = N := iff.refl _
theorem fg.cg {N : L.substructure M} (h : N.fg) : N.cg :=
begin
obtain ⟨s, hf, rfl⟩ := fg_def.1 h,
refine ⟨s, hf.countable, rfl⟩,
end
lemma cg_iff_empty_or_exists_nat_generating_family {N : L.substructure M} :
N.cg ↔ (↑N = (∅ : set M)) ∨ ∃ (s : ℕ → M), closure L (range s) = N :=
begin
rw cg_def,
split,
{ rintros ⟨S, Scount, hS⟩,
cases eq_empty_or_nonempty ↑N with h h,
{ exact or.intro_left _ h },
obtain ⟨f, h'⟩ := (Scount.union (set.countable_singleton h.some)).exists_surjective
(singleton_nonempty h.some).inr,
refine or.intro_right _ ⟨f, _⟩,
rw [← h', closure_union, hS, sup_eq_left, closure_le],
exact singleton_subset_iff.2 h.some_mem },
{ intro h,
cases h with h h,
{ refine ⟨∅, countable_empty, closure_eq_of_le (empty_subset _) _⟩,
rw [← set_like.coe_subset_coe, h],
exact empty_subset _ },
{ obtain ⟨f, rfl⟩ := h,
exact ⟨range f, countable_range _, rfl⟩ } },
end
theorem cg_bot : (⊥ : L.substructure M).cg := fg_bot.cg
theorem cg_closure {s : set M} (hs : s.countable) : cg (closure L s) :=
⟨s, hs, rfl⟩
theorem cg_closure_singleton (x : M) : cg (closure L ({x} : set M)) := (fg_closure_singleton x).cg
theorem cg.sup {N₁ N₂ : L.substructure M}
(hN₁ : N₁.cg) (hN₂ : N₂.cg) : (N₁ ⊔ N₂).cg :=
let ⟨t₁, ht₁⟩ := cg_def.1 hN₁, ⟨t₂, ht₂⟩ := cg_def.1 hN₂ in
cg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [closure_union, ht₁.2, ht₂.2]⟩
theorem cg.map {N : Type*} [L.Structure N] (f : M →[L] N) {s : L.substructure M} (hs : s.cg) :
(s.map f).cg :=
let ⟨t, ht⟩ := cg_def.1 hs in cg_def.2 ⟨f '' t, ht.1.image _, by rw [closure_image, ht.2]⟩
theorem cg.of_map_embedding {N : Type*} [L.Structure N] (f : M ↪[L] N) {s : L.substructure M}
(hs : (s.map f.to_hom).cg) : s.cg :=
begin
rcases hs with ⟨t, h1, h2⟩,
rw cg_def,
refine ⟨f ⁻¹' t, h1.preimage f.injective, _⟩,
have hf : function.injective f.to_hom := f.injective,
refine map_injective_of_injective hf _,
rw [← h2, map_closure, embedding.coe_to_hom, image_preimage_eq_of_subset],
intros x hx,
have h' := subset_closure hx,
rw h2 at h',
exact hom.map_le_range h'
end
theorem cg_iff_countable [L.countable_functions] {s : L.substructure M} :
s.cg ↔ nonempty (encodable s) :=
begin
refine ⟨_, λ h, ⟨s, h, s.closure_eq⟩⟩,
rintro ⟨s, h, rfl⟩,
exact h.substructure_closure L
end
end substructure
open substructure
namespace Structure
variables (L) (M)
/-- A structure is finitely generated if it is the closure of a finite subset. -/
class fg : Prop := (out : (⊤ : L.substructure M).fg)
/-- A structure is countably generated if it is the closure of a countable subset. -/
class cg : Prop := (out : (⊤ : L.substructure M).cg)
variables {L M}
lemma fg_def : fg L M ↔ (⊤ : L.substructure M).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩
/-- An equivalent expression of `Structure.fg` in terms of `set.finite` instead of `finset`. -/
lemma fg_iff : fg L M ↔ ∃ S : set M, S.finite ∧ closure L S = (⊤ : L.substructure M) :=
by rw [fg_def, substructure.fg_def]
lemma fg.range {N : Type*} [L.Structure N] (h : fg L M) (f : M →[L] N) :
f.range.fg :=
begin
rw [hom.range_eq_map],
exact (fg_def.1 h).map f,
end
lemma fg.map_of_surjective {N : Type*} [L.Structure N] (h : fg L M) (f : M →[L] N)
(hs : function.surjective f) :
fg L N :=
begin
rw ← hom.range_eq_top at hs,
rw [fg_def, ← hs],
exact h.range f,
end
lemma cg_def : cg L M ↔ (⊤ : L.substructure M).cg := ⟨λ h, h.1, λ h, ⟨h⟩⟩
/-- An equivalent expression of `Structure.cg`. -/
lemma cg_iff : cg L M ↔ ∃ S : set M, S.countable ∧ closure L S = (⊤ : L.substructure M) :=
by rw [cg_def, substructure.cg_def]
lemma cg.range {N : Type*} [L.Structure N] (h : cg L M) (f : M →[L] N) :
f.range.cg :=
begin
rw [hom.range_eq_map],
exact (cg_def.1 h).map f,
end
lemma cg.map_of_surjective {N : Type*} [L.Structure N] (h : cg L M) (f : M →[L] N)
(hs : function.surjective f) :
cg L N :=
begin
rw ← hom.range_eq_top at hs,
rw [cg_def, ← hs],
exact h.range f,
end
lemma cg_iff_countable [L.countable_functions] :
cg L M ↔ nonempty (encodable M) :=
by rw [cg_def, cg_iff_countable, cardinal.encodable_iff, cardinal.encodable_iff,
top_equiv.to_equiv.cardinal_eq]
lemma fg.cg (h : fg L M) : cg L M :=
cg_def.2 (fg_def.1 h).cg
@[priority 100] instance cg_of_fg [h : fg L M] : cg L M := h.cg
end Structure
lemma equiv.fg_iff {N : Type*} [L.Structure N] (f : M ≃[L] N) :
Structure.fg L M ↔ Structure.fg L N :=
⟨λ h, h.map_of_surjective f.to_hom f.to_equiv.surjective,
λ h, h.map_of_surjective f.symm.to_hom f.to_equiv.symm.surjective⟩
lemma substructure.fg_iff_Structure_fg (S : L.substructure M) :
S.fg ↔ Structure.fg L S :=
begin
rw Structure.fg_def,
refine ⟨λ h, fg.of_map_embedding S.subtype _, λ h, _⟩,
{ rw [← hom.range_eq_map, range_subtype],
exact h },
{ have h := h.map S.subtype.to_hom,
rw [← hom.range_eq_map, range_subtype] at h,
exact h }
end
lemma equiv.cg_iff {N : Type*} [L.Structure N] (f : M ≃[L] N) :
Structure.cg L M ↔ Structure.cg L N :=
⟨λ h, h.map_of_surjective f.to_hom f.to_equiv.surjective,
λ h, h.map_of_surjective f.symm.to_hom f.to_equiv.symm.surjective⟩
lemma substructure.cg_iff_Structure_cg (S : L.substructure M) :
S.cg ↔ Structure.cg L S :=
begin
rw Structure.cg_def,
refine ⟨λ h, cg.of_map_embedding S.subtype _, λ h, _⟩,
{ rw [← hom.range_eq_map, range_subtype],
exact h },
{ have h := h.map S.subtype.to_hom,
rw [← hom.range_eq_map, range_subtype] at h,
exact h }
end
end language
end first_order
|
f4a7516e17d2d26e967ee40dd939dbc03c847644 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /scripts/lean_version.lean | bb5b6a9e037f9dc9a7ad49372729b84818ca6049 | [
"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 | 265 | lean | -- Print the current version of lean, using logic copied from leanpkg.
import system.io
def lean_version_string_core :=
let (major, minor, patch) := lean.version in
sformat!("{major}.{minor}.{patch}")
def main : io unit :=
io.put_str_ln lean_version_string_core
|
cfbbd0041c8bd53441b95286402ef3c70e9f5f5c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/lazy_list/basic_auto.lean | e1d0a5a4dac89271e3747b80eed749ce93cb3cab | [] | 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,798 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
Traversable instance for lazy_lists.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.control.traversable.equiv
import Mathlib.control.traversable.instances
import Mathlib.Lean3Lib.data.lazy_list
import Mathlib.PostPort
universes u_1 u u_2 u_3
namespace Mathlib
/-!
## Definitions on lazy lists
This file contains various definitions and proofs on lazy lists.
TODO: move the `lazy_list.lean` file from core to mathlib.
-/
namespace thunk
/-- Creates a thunk with a (non-lazy) constant value. -/
def mk {α : Type u_1} (x : α) : thunk α := fun (_x : Unit) => x
protected instance decidable_eq {α : Type u} [DecidableEq α] : DecidableEq (thunk α) := sorry
end thunk
namespace lazy_list
/-- Isomorphism between strict and lazy lists. -/
def list_equiv_lazy_list (α : Type u_1) : List α ≃ lazy_list α :=
equiv.mk of_list to_list sorry sorry
protected instance inhabited {α : Type u} : Inhabited (lazy_list α) := { default := nil }
protected instance decidable_eq {α : Type u} [DecidableEq α] : DecidableEq (lazy_list α) := sorry
/-- Traversal of lazy lists using an applicative effect. -/
protected def traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u}
(f : α → m β) : lazy_list α → m (lazy_list β) :=
sorry
protected instance traversable : traversable lazy_list := traversable.mk lazy_list.traverse
protected instance is_lawful_traversable : is_lawful_traversable lazy_list :=
equiv.is_lawful_traversable' list_equiv_lazy_list sorry sorry sorry
/-- `init xs`, if `xs` non-empty, drops the last element of the list.
Otherwise, return the empty list. -/
def init {α : Type u_1} : lazy_list α → lazy_list α := sorry
/-- Return the first object contained in the list that satisfies
predicate `p` -/
def find {α : Type u_1} (p : α → Prop) [decidable_pred p] : lazy_list α → Option α := sorry
/-- `interleave xs ys` creates a list where elements of `xs` and `ys` alternate. -/
def interleave {α : Type u_1} : lazy_list α → lazy_list α → lazy_list α := sorry
/-- `interleave_all (xs::ys::zs::xss)` creates a list where elements of `xs`, `ys`
and `zs` and the rest alternate. Every other element of the resulting list is taken from
`xs`, every fourth is taken from `ys`, every eighth is taken from `zs` and so on. -/
def interleave_all {α : Type u_1} : List (lazy_list α) → lazy_list α := sorry
/-- Monadic bind operation for `lazy_list`. -/
protected def bind {α : Type u_1} {β : Type u_2} : lazy_list α → (α → lazy_list β) → lazy_list β :=
sorry
/-- Reverse the order of a `lazy_list`.
It is done by converting to a `list` first because reversal involves evaluating all
the list and if the list is all evaluated, `list` is a better representation for
it than a series of thunks. -/
def reverse {α : Type u_1} (xs : lazy_list α) : lazy_list α := of_list (list.reverse (to_list xs))
protected instance monad : Monad lazy_list := sorry
theorem append_nil {α : Type u_1} (xs : lazy_list α) : (append xs fun (_ : Unit) => nil) = xs :=
sorry
theorem append_assoc {α : Type u_1} (xs : lazy_list α) (ys : lazy_list α) (zs : lazy_list α) :
(append (append xs fun (_ : Unit) => ys) fun (_ : Unit) => zs) =
append xs fun (_ : Unit) => append ys fun (_ : Unit) => zs :=
sorry
theorem append_bind {α : Type u_1} {β : Type u_2} (xs : lazy_list α) (ys : thunk (lazy_list α))
(f : α → lazy_list β) :
lazy_list.bind (append xs ys) f =
append (lazy_list.bind xs f) fun (_ : Unit) => lazy_list.bind (ys Unit.unit) f :=
sorry
protected instance is_lawful_monad : is_lawful_monad lazy_list := sorry
/-- Try applying function `f` to every element of a `lazy_list` and
return the result of the first attempt that succeeds. -/
def mfirst {m : Type u_1 → Type u_2} [alternative m] {α : Type u_3} {β : Type u_1} (f : α → m β) :
lazy_list α → m β :=
sorry
/-- Membership in lazy lists -/
protected def mem {α : Type u_1} (x : α) : lazy_list α → Prop := sorry
protected instance has_mem {α : outParam (Type u_1)} : has_mem α (lazy_list α) :=
has_mem.mk lazy_list.mem
protected instance mem.decidable {α : Type u_1} [DecidableEq α] (x : α) (xs : lazy_list α) :
Decidable (x ∈ xs) :=
sorry
@[simp] theorem mem_nil {α : Type u_1} (x : α) : x ∈ nil ↔ False := iff.rfl
@[simp] theorem mem_cons {α : Type u_1} (x : α) (y : α) (ys : thunk (lazy_list α)) :
x ∈ cons y ys ↔ x = y ∨ x ∈ ys Unit.unit :=
iff.rfl
theorem forall_mem_cons {α : Type u_1} {p : α → Prop} {a : α} {l : thunk (lazy_list α)} :
(∀ (x : α), x ∈ cons a l → p x) ↔ p a ∧ ∀ (x : α), x ∈ l Unit.unit → p x :=
sorry
/-! ### map for partial functions -/
/-- Partial map. If `f : Π a, p a → β` is a partial function defined on
`a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`
but is defined only when all members of `l` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {α : Type u_1} {β : Type u_2} {p : α → Prop} (f : (a : α) → p a → β)
(l : lazy_list α) : (∀ (a : α), a ∈ l → p a) → lazy_list β :=
sorry
/-- "Attach" the proof that the elements of `l` are in `l` to produce a new `lazy_list`
with the same elements but in the type `{x // x ∈ l}`. -/
def attach {α : Type u_1} (l : lazy_list α) : lazy_list (Subtype fun (x : α) => x ∈ l) :=
pmap Subtype.mk l sorry
protected instance has_repr {α : Type u_1} [has_repr α] : has_repr (lazy_list α) :=
has_repr.mk fun (xs : lazy_list α) => repr (to_list xs)
end Mathlib |
2eaddb4d659b0ecf6ded6529bb106c44b359568a | 7617a8700fc3eebd9200f1e620ee65b3565d2eff | /src/util/trace.lean | 62bc396e373a23b9b717be647158034b21dd960c | [] | no_license | digama0/vc0 | 9747eee791c5c2f58eb53264ca6263ee53de0f87 | b8b192c8c139e0b5a25a7284b93ed53cdf7fd7a5 | refs/heads/master | 1,626,186,940,436 | 1,592,385,972,000 | 1,592,385,972,000 | 158,556,766 | 5 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,197 | lean | /-
trace.lean
Author: Mario Carneiro
This file defines a coinductive datatype for "traces", which are possible
program behaviors including I/O. Because Lean does not yet support coinductive
datatypes, we have to define this one manually (one would like to define
`trace := gfp trace1` and skip all the lemmas).
A trace is coinductively defined by `trace1 α := roption (η ⊕ (Σ i:ι, o i → α))`.
Here `η` is the type of possible final states (including stuck states and
error states if applicable), `ι` is the type of I/O inputs (possible functions
to call and arguments to pass), and `o i` is the type of I/O return values
(dependent on the input). The trace is really a tree because the I/O output is
nondeterministic; the program behavior describes what will happen for any
return value. The `roption` handles program divergence; if the program does
not terminate then no trace will be produced.
-/
import util.basic
namespace vc0
section
variables (η : Type) {ι : Type} (o : ι → Type)
def trace1 (α : Type) : Type := roption (η ⊕ (Σ i, o i → α))
variables {η o}
def trace1.out {α} (t : trace1 η o α) : roption (Σ i, o i → α) :=
t.bind $ λ x, match x with
| (sum.inl _) := roption.none
| (sum.inr x) := roption.some x
end
theorem trace1.mem_out {α} (t : trace1 η o α) {x} : x ∈ t.out ↔
(sum.inr x : η ⊕ (Σ i, o i → α)) ∈ (by exact t : roption _) :=
roption.mem_bind_iff.trans
⟨by rintro ⟨_|x, ⟨h, e⟩, ⟨⟩, rfl⟩; exact ⟨_, e⟩,
λ h, ⟨sum.inr x, h, trivial, rfl⟩⟩
def trace1.fst {α} (t : trace1 η o α) : roption ι :=
sigma.fst <$> trace1.out t
def trace1.snd {α} (t : trace1 η o α) : ∀ i ∈ t.fst, o i → α :=
roption.mem_cases _ (by exact λ h, ((trace1.out t).get h).2)
theorem trace1.fst_snd_mem {α} (t : trace1 η o α) : ∀ i h,
(⟨i, trace1.snd t i h⟩ : Σ i, o i → α) ∈ t.out :=
roption.mem_cases _ begin
change ∀ h : t.out.dom, (⟨(t.out.get h).1, (t.out.get h).2⟩ : Σ i, o i → α) ∈ t.out,
intro h, rw sigma.eta, apply roption.get_mem
end
theorem trace1.snd_eq {α} (t : trace1 η o α) (i h g)
(H : (⟨i, g⟩ : Σ i, o i → α) ∈ t.out) : trace1.snd t i h = g :=
by simpa using roption.mem_unique (t.fst_snd_mem i h) H
instance trace1.functor : functor (trace1 η o) :=
{ map := λ α β f, roption.map $ sum.map id $ sigma.map id $ λ i, (∘) f }
instance trace1.is_lawful_functor : is_lawful_functor (trace1 η o) :=
{ id_map := λ α, roption.map_id' $ by rintro (_|⟨i, g⟩); refl,
comp_map := λ α β γ g h t, begin
refine eq.trans _ (roption.map_map _ _ _).symm,
simp [(<$>)], congr, ext ⟨i, g⟩; refl
end }
def trace1.pmap {α β} (t : trace1 η o α)
(f : ∀ x:Σ i, o i → α, x ∈ t.out → o x.1 → α → β) : trace1 η o β :=
⟨t.dom, λ h, begin
cases e : t.get h with a x,
{ exact sum.inl a },
{ exact sum.inr ⟨x.1, λ b, f _ (t.mem_out.2 ⟨h, e⟩) b (x.2 b)⟩ }
end⟩
theorem trace1.pmap_inl {α β} (t : trace1 η o α) (f h)
{a} (H : t.get h = sum.inl a) :
(t.pmap f : trace1 η o β).get h = sum.inl a :=
by simp [trace1.pmap, H]
theorem trace1.pmap_inr {α β} (t : trace1 η o α) (f h)
{x} (H : t.get h = sum.inr x) :
(t.pmap f : trace1 η o β).get h =
sum.inr ⟨x.1, λ b, f _ (t.mem_out.2 ⟨h, H⟩) b (x.2 b)⟩ :=
by simp [trace1.pmap, H]
def trace1.out_map {α β} (f : α → β) (t : trace1 η o α) :
(f <$> t : trace1 η o β).out = (sigma.map id $ λ i, (∘) f) <$> t.out :=
roption.ext $ λ x, by simp [trace1.mem_out, (<$>), sum.map]; refl
theorem trace1.fst_map {α β} (f : α → β) (t : trace1 η o α) :
(f <$> t : trace1 η o β).fst = t.fst :=
show sigma.fst <$> trace1.out _ = sigma.fst <$> _, begin
rw [trace1.out_map, ← comp_map],
congr' 1, ext ⟨a, b⟩, refl
end
theorem trace1.snd_map {α β} (f : α → β) (t : trace1 η o α) (i h h' b) :
(f <$> t : trace1 η o β).snd i h b = f (t.snd i h' b) :=
begin
have := (f <$> t).fst_snd_mem i h,
simp [t.out_map f] at this,
rcases this with ⟨i, g, h₁, h₂⟩,
injection h₂ with h₃ h₄, subst h₃, simp at h₄,
rw [← h₄, ← t.snd_eq _ h' _ h₁]
end
variables (η o)
def traceN : ℕ → Type → Type
| 0 α := α
| (n+1) α := traceN n (trace1 η o α)
variables {η o}
def traceN.map : ∀ (n) {α β}, (α → β) → traceN η o n α → traceN η o n β
| 0 α β f := f
| (n+1) α β f := @traceN.map n _ _ ((<$>) f)
instance traceN.functor (n) : functor (traceN η o n) :=
{ map := @traceN.map _ _ _ n }
instance traceN.is_lawful_functor : ∀ n, is_lawful_functor (traceN η o n)
| 0 := by split; intros; refl
| (n+1) := begin
split; intros,
{ show (<$>) id <$> x = x,
rw [(funext (@id_map (trace1 η o) _ _ _) : (<$>) id = id),
@id_map _ _ (traceN.is_lawful_functor n)] },
{ change ((<$>) (h ∘ g) <$> x : traceN η o n (trace1 η o γ)) =
(<$>) h <$> (<$>) g <$> x,
rw [(funext (@comp_map (trace1 η o) _ _ _ _ _ _ _) :
(<$>) (h ∘ g) = (<$>) h ∘ (<$>) g),
@comp_map _ _ (traceN.is_lawful_functor n)] },
end
def trace1.forall₂ {α β} (R : α → β → Prop) : trace1 η o α → trace1 η o β → Prop :=
roption.forall₂ $ sum.forall₂ eq $ sigma.forall₂ $ λ i f g, ∀ x, R (f x) (g x)
def traceN.forall₂ : ∀ {n α β}, (α → β → Prop) → traceN η o n α → traceN η o n β → Prop
| 0 α β R := R
| (n+1) α β R := @traceN.forall₂ n _ _ (trace1.forall₂ R)
def traceN.fst : ∀ {n α}, traceN η o (n+1) α → roption ι
| 0 α t := trace1.fst t
| (n+1) α t := @traceN.fst n _ t
def traceN.out : ∀ {n α}, traceN η o (n+1) α → roption (Σ i, o i → traceN η o n α)
| 0 α := trace1.out
| (n+1) α := @traceN.out n _
def traceN.snd : ∀ {n α} (t : traceN η o (n+1) α) (i ∈ t.fst), o i → traceN η o n α
| 0 α t := trace1.snd t
| (n+1) α t := @traceN.snd n _ t
theorem traceN.fst_map : ∀ n {α β} (f : α → β) (t : traceN η o (n+1) α),
(f <$> t : traceN η o (n+1) β).fst = t.fst
| 0 α β f := trace1.fst_map _
| (n+1) α β f := traceN.fst_map n _
theorem traceN.snd_map : ∀ n {α β} (f : α → β) (t : traceN η o (n+1) α) (i h h' b),
(f <$> t : traceN η o (n+1) β).snd i h b = f <$> t.snd i h' b
| 0 α β f := trace1.snd_map _
| (n+1) α β f := traceN.snd_map n _
theorem traceN.snd_map' (n) {α β} (f : α → β) (t : traceN η o (n+1) α) (i h b) :
(f <$> t : traceN η o (n+1) β).snd i h b = f <$> t.snd i (by rwa t.fst_map _ _ at h) b :=
traceN.snd_map _ _ _ _ _ _ _
theorem traceN.out_map : ∀ n {α β} (f : α → β) (t : traceN η o n (trace1 η o α)),
(f <$> t : traceN η o (n+1) β).out =
(sigma.map id $ by exact λ i g x, f <$> g x) <$> t.out
| 0 α β f t := trace1.out_map _ _
| (n+1) α β f t := traceN.out_map n _ _
variables (η o)
def trace : Type := -- gfp trace1
{f : ∀ n, traceN η o n unit // ∀ n, f n = (λ _, ()) <$> f (n+1) }
variables {η o}
theorem trace_fst_aux (f : ∀ n, traceN η o n unit)
(H : ∀ n, f n = (λ _, ()) <$> f (n+1)) :
∀ n, (f (n+1)).fst = (f 1).fst
| 0 := rfl
| (n+1) := by rw [← trace_fst_aux n, H (n+1), traceN.fst_map]; refl
theorem trace_map_aux (f : ∀ n, traceN η o n unit)
(H : ∀ n, f n = (λ _, ()) <$> f (n+1)) :
∀ n, sigma.fst <$> (f (n+1)).out = sigma.fst <$> (f 1).out
| 0 := rfl
| (n+1) := begin
rw [← trace_map_aux n, H (n+1), traceN.out_map, ← comp_map],
congr, ext ⟨i, b⟩, refl
end
def trace.destruct : trace η o → trace1 η o (trace η o)
| ⟨f, H⟩ := trace1.pmap (f 1) (λ x i b _, begin
have hx : ∀ n, x.1 ∈ traceN.fst (f (n + 1)),
{ intro n, rwa trace_fst_aux _ H n, exact roption.mem_map _ i },
have := λ n, traceN.out (f (n+1)),
refine ⟨λ n, traceN.snd (f (n+1)) _ (hx n) b, λ n, _⟩,
refine eq.trans _ ((f (n+1+1)).snd_map' n (λ _, ()) _ _ b),
{ rw traceN.fst_map, exact hx (n+1) },
{ congr, apply H }
end)
def traceN.iter {α} (F : α → trace1 η o α) (a : α) : ∀ n, traceN η o n α
| 0 := a
| (n+1) := (F <$> traceN.iter n : traceN η o n (trace1 η o α))
theorem traceN.iter_fst {α} (F : α → trace1 η o α) (a : α) :
∀ n, (traceN.iter F a (n+1)).fst = (F a).fst
| 0 := rfl
| (n+1) := (traceN.fst_map _ _ _).trans (traceN.iter_fst n)
theorem traceN.iter_snd' {α} (F : α → trace1 η o α) (a : α) : ∀ n i h b,
(traceN.iter F a (n+1)).snd i h b =
traceN.iter F ((F a).snd i (by rwa traceN.iter_fst at h) b) n
| 0 i h b := rfl
| (n+1) i h b := (traceN.snd_map' _ _ _ i h b).trans (congr_arg _ (traceN.iter_snd' n _ _ _))
theorem traceN.iter_snd {α} (F : α → trace1 η o α) (a : α) (n i h h' b) :
(traceN.iter F a (n+1)).snd i h b = traceN.iter F ((F a).snd i h' b) n :=
traceN.iter_snd' _ _ _ _ _ _
def trace.corec {α} (F : α → trace1 η o α) (a : α) : trace η o :=
⟨λ n, (λ _, ()) <$> traceN.iter F a n, λ n,
by rw traceN.iter; exact
eq.trans (by refl) ((comp_map _ _ _).trans (comp_map _ _ _))⟩
theorem trace.corec_eq {α} (F : α → trace1 η o α) (a : α) :
trace.destruct (trace.corec F a) = trace.corec F <$> F a :=
roption.ext' iff.rfl $ λ h₁ h₂, begin
change (F a).dom at h₁,
change h₂ with h₁, clear h₂,
change (trace1.pmap _ _).get _ = sum.map _ _ _,
have e : ((λ _, ()) <$> traceN.iter F a 1).get h₁ =
sum.map id (sigma.map id (λ _ _ _, ())) ((F a).get h₁) := rfl,
cases e' : (F a).get h₁; rw e' at e,
{ rw trace1.pmap_inl _ _ _ e, refl },
{ rw trace1.pmap_inr _ _ _ e, cases val with i g,
simp! [sigma.map], ext b,
simp [trace.corec], ext n,
suffices : ∀ h, traceN.snd ((λ _, ()) <$> traceN.iter F a (n+1))
i h b = (λ _, ()) <$> traceN.iter F (g b) n, {apply this},
intro,
rw traceN.fst_map at h,
rw [traceN.snd_map _ _ _ _ _ h, traceN.iter_snd', trace1.snd_eq],
exact (trace1.mem_out _).2 ⟨_, e'⟩ },
end
/-
theorem trace.eq_of_bisim (R : trace η o → trace η o → Prop)
(H : ∀ t₁ t₂, R t₁ t₂ → trace1.forall₂ R t₁.destruct t₂.destruct) :
∀ t₁ t₂, R t₁ t₂ → t₁ = t₂ :=
sorry
-/
end
end vc0
|
567263f24885df989db4cc086e59b2a16b260ccf | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/data/setoid/basic.lean | 246ec8c1533a82917822f704e6f170158bfc5819 | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,341 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston, Bryan Gin-ge Chen
-/
import order.galois_connection
/-!
# Equivalence relations
This file defines the complete lattice of equivalence relations on a type, results about the
inductively defined equivalence closure of a binary relation, and the analogues of some isomorphism
theorems for quotients of arbitrary types.
## Implementation notes
The function `rel` and lemmas ending in ' make it easier to talk about different
equivalence relations on the same type.
The complete lattice instance for equivalence relations could have been defined by lifting
the Galois insertion of equivalence relations on α into binary relations on α, and then using
`complete_lattice.copy` to define a complete lattice instance with more appropriate
definitional equalities (a similar example is `filter.complete_lattice` in
`order/filter/basic.lean`). This does not save space, however, and is less clear.
Partitions are not defined as a separate structure here; users are encouraged to
reason about them using the existing `setoid` and its infrastructure.
## Tags
setoid, equivalence, iseqv, relation, equivalence relation
-/
variables {α : Type*} {β : Type*}
/-- A version of `setoid.r` that takes the equivalence relation as an explicit argument. -/
def setoid.rel (r : setoid α) : α → α → Prop := @setoid.r _ r
/-- A version of `quotient.eq'` compatible with `setoid.rel`, to make rewriting possible. -/
lemma quotient.eq_rel {r : setoid α} {x y} : ⟦x⟧ = ⟦y⟧ ↔ r.rel x y := quotient.eq'
namespace setoid
@[ext] lemma ext' {r s : setoid α} (H : ∀ a b, r.rel a b ↔ s.rel a b) :
r = s := ext H
lemma ext_iff {r s : setoid α} : r = s ↔ ∀ a b, r.rel a b ↔ s.rel a b :=
⟨λ h a b, h ▸ iff.rfl, ext'⟩
/-- Two equivalence relations are equal iff their underlying binary operations are equal. -/
theorem eq_iff_rel_eq {r₁ r₂ : setoid α} : r₁ = r₂ ↔ r₁.rel = r₂.rel :=
⟨λ h, h ▸ rfl, λ h, setoid.ext' $ λ x y, h ▸ iff.rfl⟩
/-- Defining `≤` for equivalence relations. -/
instance : has_le (setoid α) := ⟨λ r s, ∀ ⦃x y⦄, r.rel x y → s.rel x y⟩
theorem le_def {r s : setoid α} : r ≤ s ↔ ∀ {x y}, r.rel x y → s.rel x y := iff.rfl
@[refl] lemma refl' (r : setoid α) (x) : r.rel x x := r.2.1 x
@[symm] lemma symm' (r : setoid α) : ∀ {x y}, r.rel x y → r.rel y x := λ _ _ h, r.2.2.1 h
@[trans] lemma trans' (r : setoid α) : ∀ {x y z}, r.rel x y → r.rel y z → r.rel x z :=
λ _ _ _ hx, r.2.2.2 hx
/-- The kernel of a function is an equivalence relation. -/
def ker (f : α → β) : setoid α :=
⟨λ x y, f x = f y, ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h, h.trans⟩⟩
/-- The kernel of the quotient map induced by an equivalence relation r equals r. -/
@[simp] lemma ker_mk_eq (r : setoid α) : ker (@quotient.mk _ r) = r :=
ext' $ λ x y, quotient.eq
lemma ker_def {f : α → β} {x y : α} : (ker f).rel x y ↔ f x = f y := iff.rfl
/-- Given types `α`, `β`, the product of two equivalence relations `r` on `α` and `s` on `β`:
`(x₁, x₂), (y₁, y₂) ∈ α × β` are related by `r.prod s` iff `x₁` is related to `y₁`
by `r` and `x₂` is related to `y₂` by `s`. -/
protected def prod (r : setoid α) (s : setoid β) : setoid (α × β) :=
{ r := λ x y, r.rel x.1 y.1 ∧ s.rel x.2 y.2,
iseqv := ⟨λ x, ⟨r.refl' x.1, s.refl' x.2⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩ }
/-- The infimum of two equivalence relations. -/
instance : has_inf (setoid α) :=
⟨λ r s, ⟨λ x y, r.rel x y ∧ s.rel x y, ⟨λ x, ⟨r.refl' x, s.refl' x⟩,
λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩⟩⟩
/-- The infimum of 2 equivalence relations r and s is the same relation as the infimum
of the underlying binary operations. -/
lemma inf_def {r s : setoid α} : (r ⊓ s).rel = r.rel ⊓ s.rel := rfl
theorem inf_iff_and {r s : setoid α} {x y} :
(r ⊓ s).rel x y ↔ r.rel x y ∧ s.rel x y := iff.rfl
/-- The infimum of a set of equivalence relations. -/
instance : has_Inf (setoid α) :=
⟨λ S, ⟨λ x y, ∀ r ∈ S, rel r x y,
⟨λ x r hr, r.refl' x, λ _ _ h r hr, r.symm' $ h r hr,
λ _ _ _ h1 h2 r hr, r.trans' (h1 r hr) $ h2 r hr⟩⟩⟩
/-- The underlying binary operation of the infimum of a set of equivalence relations
is the infimum of the set's image under the map to the underlying binary operation. -/
theorem Inf_def {s : set (setoid α)} : (Inf s).rel = Inf (rel '' s) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
instance : partial_order (setoid α) :=
{ le := (≤),
lt := λ r s, r ≤ s ∧ ¬s ≤ r,
le_refl := λ _ _ _, id,
le_trans := λ _ _ _ hr hs _ _ h, hs $ hr h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ r s h1 h2, setoid.ext' $ λ x y, ⟨λ h, h1 h, λ h, h2 h⟩ }
/-- The complete lattice of equivalence relations on a type, with bottom element `=`
and top element the trivial equivalence relation. -/
instance complete_lattice : complete_lattice (setoid α) :=
{ inf := has_inf.inf,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ h1 h2 _ _ h, ⟨h1 h, h2 h⟩,
top := ⟨λ _ _, true, ⟨λ _, trivial, λ _ _ h, h, λ _ _ _ h1 h2, h1⟩⟩,
le_top := λ _ _ _ _, trivial,
bot := ⟨(=), ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h1 h2, h1.trans h2⟩⟩,
bot_le := λ r x y h, h ▸ r.2.1 x,
.. complete_lattice_of_Inf (setoid α) $ assume s,
⟨λ r hr x y h, h _ hr, λ r hr x y h r' hr', hr hr' h⟩ }
/-- The inductively defined equivalence closure of a binary relation r is the infimum
of the set of all equivalence relations containing r. -/
theorem eqv_gen_eq (r : α → α → Prop) :
eqv_gen.setoid r = Inf {s : setoid α | ∀ ⦃x y⦄, r x y → s.rel x y} :=
le_antisymm
(λ _ _ H, eqv_gen.rec (λ _ _ h _ hs, hs h) (refl' _)
(λ _ _ _, symm' _) (λ _ _ _ _ _, trans' _) H)
(Inf_le $ λ _ _ h, eqv_gen.rel _ _ h)
/-- The supremum of two equivalence relations r and s is the equivalence closure of the binary
relation `x is related to y by r or s`. -/
lemma sup_eq_eqv_gen (r s : setoid α) :
r ⊔ s = eqv_gen.setoid (λ x y, r.rel x y ∨ s.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of 2 equivalence relations r and s is the equivalence closure of the
supremum of the underlying binary operations. -/
lemma sup_def {r s : setoid α} : r ⊔ s = eqv_gen.setoid (r.rel ⊔ s.rel) :=
by rw sup_eq_eqv_gen; refl
/-- The supremum of a set S of equivalence relations is the equivalence closure of the binary
relation `there exists r ∈ S relating x and y`. -/
lemma Sup_eq_eqv_gen (S : set (setoid α)) :
Sup S = eqv_gen.setoid (λ x y, ∃ r : setoid α, r ∈ S ∧ r.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [upper_bounds, le_def, and_imp, exists_imp_distrib],
ext,
exact ⟨λ H x y r hr, H hr, λ H r hr x y, H r hr⟩
end
/-- The supremum of a set of equivalence relations is the equivalence closure of the
supremum of the set's image under the map to the underlying binary operation. -/
lemma Sup_def {s : set (setoid α)} : Sup s = eqv_gen.setoid (Sup (rel '' s)) :=
begin
rw [Sup_eq_eqv_gen, Sup_image],
congr' with x y,
simp only [supr_apply, supr_Prop_eq, exists_prop]
end
/-- The equivalence closure of an equivalence relation r is r. -/
@[simp] lemma eqv_gen_of_setoid (r : setoid α) : eqv_gen.setoid r.r = r :=
le_antisymm (by rw eqv_gen_eq; exact Inf_le (λ _ _, id)) eqv_gen.rel
/-- Equivalence closure is idempotent. -/
@[simp] lemma eqv_gen_idem (r : α → α → Prop) :
eqv_gen.setoid (eqv_gen.setoid r).rel = eqv_gen.setoid r :=
eqv_gen_of_setoid _
/-- The equivalence closure of a binary relation r is contained in any equivalence
relation containing r. -/
theorem eqv_gen_le {r : α → α → Prop} {s : setoid α} (h : ∀ x y, r x y → s.rel x y) :
eqv_gen.setoid r ≤ s :=
by rw eqv_gen_eq; exact Inf_le h
/-- Equivalence closure of binary relations is monotonic. -/
theorem eqv_gen_mono {r s : α → α → Prop} (h : ∀ x y, r x y → s x y) :
eqv_gen.setoid r ≤ eqv_gen.setoid s :=
eqv_gen_le $ λ _ _ hr, eqv_gen.rel _ _ $ h _ _ hr
/-- There is a Galois insertion of equivalence relations on α into binary relations
on α, with equivalence closure the lower adjoint. -/
def gi : @galois_insertion (α → α → Prop) (setoid α) _ _ eqv_gen.setoid rel :=
{ choice := λ r h, eqv_gen.setoid r,
gc := λ r s, ⟨λ H _ _ h, H $ eqv_gen.rel _ _ h, λ H, eqv_gen_of_setoid s ▸ eqv_gen_mono H⟩,
le_l_u := λ x, (eqv_gen_of_setoid x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
open function
/-- A function from α to β is injective iff its kernel is the bottom element of the complete lattice
of equivalence relations on α. -/
theorem injective_iff_ker_bot (f : α → β) :
injective f ↔ ker f = ⊥ :=
(@eq_bot_iff (setoid α) _ (ker f)).symm
/-- The elements related to x ∈ α by the kernel of f are those in the preimage of f(x) under f. -/
lemma ker_iff_mem_preimage {f : α → β} {x y} : (ker f).rel x y ↔ x ∈ f ⁻¹' {f y} :=
iff.rfl
/-- Equivalence between functions `α → β` such that `r x y → f x = f y` and functions
`quotient r → β`. -/
def lift_equiv (r : setoid α) : {f : α → β // r ≤ ker f} ≃ (quotient r → β) :=
{ to_fun := λ f, quotient.lift (f : α → β) f.2,
inv_fun := λ f, ⟨f ∘ quotient.mk, λ x y h, by simp [ker_def, quotient.sound h]⟩,
left_inv := λ ⟨f, hf⟩, subtype.eq $ funext $ λ x, rfl,
right_inv := λ f, funext $ λ x, quotient.induction_on' x $ λ x, rfl }
/-- The uniqueness part of the universal property for quotients of an arbitrary type. -/
theorem lift_unique {r : setoid α} {f : α → β} (H : r ≤ ker f) (g : quotient r → β)
(Hg : f = g ∘ quotient.mk) : quotient.lift f H = g :=
begin
ext ⟨x⟩,
erw [quotient.lift_beta f H, Hg],
refl
end
/-- Given a map f from α to β, the natural map from the quotient of α by the kernel of f is
injective. -/
lemma ker_lift_injective (f : α → β) : injective (@quotient.lift _ _ (ker f) f (λ _ _ h, h)) :=
λ x y, quotient.induction_on₂' x y $ λ a b h, quotient.sound' h
/-- Given a map f from α to β, the kernel of f is the unique equivalence relation on α whose
induced map from the quotient of α to β is injective. -/
lemma ker_eq_lift_of_injective {r : setoid α} (f : α → β) (H : ∀ x y, r.rel x y → f x = f y)
(h : injective (quotient.lift f H)) : ker f = r :=
le_antisymm
(λ x y hk, quotient.exact $ h $ show quotient.lift f H ⟦x⟧ = quotient.lift f H ⟦y⟧, from hk)
H
variables (r : setoid α) (f : α → β)
/-- The first isomorphism theorem for sets: the quotient of α by the kernel of a function f
bijects with f's image. -/
noncomputable def quotient_ker_equiv_range :
quotient (ker f) ≃ set.range f :=
equiv.of_bijective (@quotient.lift _ (set.range f) (ker f)
(λ x, ⟨f x, set.mem_range_self x⟩) $ λ _ _ h, subtype.ext_val h)
⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨@quotient.mk _ (ker f) z, by rw quotient.lift_beta; exact subtype.ext_iff_val.2 hz⟩⟩
/-- The quotient of α by the kernel of a surjective function f bijects with f's codomain. -/
noncomputable def quotient_ker_equiv_of_surjective (hf : surjective f) :
quotient (ker f) ≃ β :=
(quotient_ker_equiv_range f).trans $ equiv.subtype_univ_equiv hf
variables {r f}
/-- Given a function `f : α → β` and equivalence relation `r` on `α`, the equivalence
closure of the relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are
related to the elements of `f⁻¹(y)` by `r`.' -/
def map (r : setoid α) (f : α → β) : setoid β :=
eqv_gen.setoid $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b
/-- Given a surjective function f whose kernel is contained in an equivalence relation r, the
equivalence relation on f's codomain defined by x ≈ y ↔ the elements of f⁻¹(x) are related to
the elements of f⁻¹(y) by r. -/
def map_of_surjective (r) (f : α → β) (h : ker f ≤ r) (hf : surjective f) :
setoid β :=
⟨λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b,
⟨λ x, let ⟨y, hy⟩ := hf x in ⟨y, y, hy, hy, r.refl' y⟩,
λ _ _ ⟨x, y, hx, hy, h⟩, ⟨y, x, hy, hx, r.symm' h⟩,
λ _ _ _ ⟨x, y, hx, hy, h₁⟩ ⟨y', z, hy', hz, h₂⟩,
⟨x, z, hx, hz, r.trans' h₁ $ r.trans' (h $ by rwa ←hy' at hy) h₂⟩⟩⟩
/-- A special case of the equivalence closure of an equivalence relation r equalling r. -/
lemma map_of_surjective_eq_map (h : ker f ≤ r) (hf : surjective f) :
map r f = map_of_surjective r f h hf :=
by rw ←eqv_gen_of_setoid (map_of_surjective r f h hf); refl
/-- Given a function `f : α → β`, an equivalence relation `r` on `β` induces an equivalence
relation on `α` defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `r`'. -/
def comap (f : α → β) (r : setoid β) : setoid α :=
⟨λ x y, r.rel (f x) (f y), ⟨λ _, r.refl' _, λ _ _ h, r.symm' h, λ _ _ _ h1, r.trans' h1⟩⟩
/-- Given a map `f : N → M` and an equivalence relation `r` on `β`, the equivalence relation
induced on `α` by `f` equals the kernel of `r`'s quotient map composed with `f`. -/
lemma comap_eq {f : α → β} {r : setoid β} : comap f r = ker (@quotient.mk _ r ∘ f) :=
ext $ λ x y, show _ ↔ ⟦_⟧ = ⟦_⟧, by rw quotient.eq; refl
/-- The second isomorphism theorem for sets. -/
noncomputable def comap_quotient_equiv (f : α → β) (r : setoid β) :
quotient (comap f r) ≃ set.range (@quotient.mk _ r ∘ f) :=
(quotient.congr_right $ ext_iff.1 comap_eq).trans $ quotient_ker_equiv_range $ quotient.mk ∘ f
variables (r f)
/-- The third isomorphism theorem for sets. -/
def quotient_quotient_equiv_quotient (s : setoid α) (h : r ≤ s) :
quotient (ker (quot.map_right h)) ≃ quotient s :=
{ to_fun := λ x, quotient.lift_on' x (λ w, quotient.lift_on' w (@quotient.mk _ s) $
λ x y H, quotient.sound $ h H) $ λ x y, quotient.induction_on₂' x y $ λ w z H,
show @quot.mk _ _ _ = @quot.mk _ _ _, from H,
inv_fun := λ x, quotient.lift_on' x
(λ w, @quotient.mk _ (ker $ quot.map_right h) $ @quotient.mk _ r w) $
λ x y H, quotient.sound' $ show @quot.mk _ _ _ = @quot.mk _ _ _, from quotient.sound H,
left_inv := λ x, quotient.induction_on' x $ λ y, quotient.induction_on' y $
λ w, by show ⟦_⟧ = _; refl,
right_inv := λ x, quotient.induction_on' x $ λ y, by show ⟦_⟧ = _; refl }
variables {r f}
open quotient
/-- Given an equivalence relation `r` on `α`, the order-preserving bijection between the set of
equivalence relations containing `r` and the equivalence relations on the quotient of `α` by `r`. -/
def correspondence (r : setoid α) : {s // r ≤ s} ≃o setoid (quotient r) :=
{ to_fun := λ s, map_of_surjective s.1 quotient.mk ((ker_mk_eq r).symm ▸ s.2) exists_rep,
inv_fun := λ s, ⟨comap quotient.mk s, λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw eq_rel.2 h⟩,
left_inv := λ s, subtype.ext_iff_val.2 $ ext' $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
s.1.trans' (s.1.symm' $ s.2 $ eq_rel.1 hx) $ s.1.trans' H $ s.2 $ eq_rel.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ s, let Hm : ker quotient.mk ≤ comap quotient.mk s :=
λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw (@eq_rel _ r x y).2 ((ker_mk_eq r) ▸ h) in
ext' $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
quotient.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
map_rel_iff' := λ s t, ⟨λ h x y hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨x, y, rfl, rfl, hs⟩ in
t.1.trans' (t.1.symm' $ t.2 $ eq_rel.1 hx) $ t.1.trans' ht $ t.2 $ eq_rel.1 hy,
λ h x y hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩⟩ }
end setoid
|
43cd0f74a680553f2fc1aeed5debb96563f2674c | 6e41ee3ac9b96e8980a16295cc21f131e731884f | /library/init/reserved_notation.lean | 973d6d2787a31150b6e43028425db5dfdd2dbb9c | [
"Apache-2.0"
] | permissive | EgbertRijke/lean | 3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3 | 4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183 | refs/heads/master | 1,610,834,871,476 | 1,422,159,801,000 | 1,422,159,801,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,011 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: general_notation
Authors: Leonardo de Moura, Jeremy Avigad
-/
prelude
import init.datatypes
notation `assume` binders `,` r:(scoped f, f) := r
notation `take` binders `,` r:(scoped f, f) := r
/-
Global declarations of right binding strength
If a module reassigns these, it will be incompatible with other modules that adhere to these
conventions.
When hovering over a symbol, use "C-u C-x =" to see how to input it.
-/
/- Logical operations and relations -/
definition std.prec.max : num := 1024 -- reflects max precedence used internally
definition std.prec.arrow : num := 25
reserve prefix `¬`:40
reserve prefix `~`:40
reserve infixr `∧`:35
reserve infixr `/\`:35
reserve infixr `\/`:30
reserve infixr `∨`:30
reserve infix `<->`:25
reserve infix `↔`:25
reserve infix `=`:50
reserve infix `≠`:50
reserve infix `≈`:50
reserve infix `∼`:50
reserve infixr `∘`:60 -- input with \comp
reserve postfix `⁻¹`:100 --input with \sy or \-1 or \inv
reserve infixl `⬝`:75
reserve infixr `▸`:75
/- types and type constructors -/
reserve infixl `⊎`:25
reserve infixl `×`:30
/- arithmetic operations -/
reserve infixl `+`:65
reserve infixl `-`:65
reserve infixl `*`:70
reserve infixl `div`:70
reserve infixl `mod`:70
reserve prefix `-`:100
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
/- set operations -/
reserve infix `∈`:50
reserve infix `∉`:50
reserve infixl `∩`:70
reserve infixl `∪`:65
/- other symbols -/
precedence `|`:55
reserve notation | a:55 |
reserve infixl `++`:65
reserve infixr `::`:65
-- Yet another trick to anotate an expression with a type
definition is_typeof (A : Type) (a : A) : A := a
notation `typeof` t `:` T := is_typeof T t
|
6cc0c255075bebbc14a6ebeab1e8490ed12df38d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/homology/opposite.lean | 7613e58ab6a0d9be8116dfa11d0c7af3061652f4 | [
"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,240 | lean | /-
Copyright (c) 2022 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Amelia Livingston
-/
import category_theory.abelian.opposite
import category_theory.abelian.homology
import algebra.homology.additive
/-!
# Opposite categories of complexes
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Given a preadditive category `V`, the opposite of its category of chain complexes is equivalent to
the category of cochain complexes of objects in `Vᵒᵖ`. We define this equivalence, and another
analagous equivalence (for a general category of homological complexes with a general
complex shape).
We then show that when `V` is abelian, if `C` is a homological complex, then the homology of
`op(C)` is isomorphic to `op` of the homology of `C` (and the analagous result for `unop`).
## Implementation notes
It is convenient to define both `op` and `op_symm`; this is because given a complex shape `c`,
`c.symm.symm` is not defeq to `c`.
## Tags
opposite, chain complex, cochain complex, homology, cohomology, homological complex
-/
noncomputable theory
open opposite category_theory category_theory.limits
section
variables {V : Type*} [category V] [abelian V]
lemma image_to_kernel_op {X Y Z : V} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0) :
image_to_kernel g.op f.op (by rw [←op_comp, w, op_zero]) = ((image_subobject_iso _)
≪≫ (image_op_op _).symm).hom ≫ (cokernel.desc f (factor_thru_image g)
(by rw [←cancel_mono (image.ι g), category.assoc, image.fac, w, zero_comp])).op
≫ ((kernel_subobject_iso _) ≪≫ (kernel_op_op _)).inv :=
begin
ext,
simpa only [iso.trans_hom, iso.symm_hom, iso.trans_inv, kernel_op_op_inv, category.assoc,
image_to_kernel_arrow, kernel_subobject_arrow', kernel.lift_ι, ←op_comp, cokernel.π_desc,
←image_subobject_arrow, ←image_unop_op_inv_comp_op_factor_thru_image g.op],
end
lemma image_to_kernel_unop {X Y Z : Vᵒᵖ} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0) :
image_to_kernel g.unop f.unop (by rw [←unop_comp, w, unop_zero]) = ((image_subobject_iso _)
≪≫ (image_unop_unop _).symm).hom ≫ (cokernel.desc f (factor_thru_image g)
(by rw [←cancel_mono (image.ι g), category.assoc, image.fac, w, zero_comp])).unop
≫ ((kernel_subobject_iso _) ≪≫ (kernel_unop_unop _)).inv :=
begin
ext,
dunfold image_unop_unop,
simp only [iso.trans_hom, iso.symm_hom, iso.trans_inv, kernel_unop_unop_inv, category.assoc,
image_to_kernel_arrow, kernel_subobject_arrow', kernel.lift_ι, cokernel.π_desc,
iso.unop_inv, ←unop_comp, factor_thru_image_comp_image_unop_op_inv, quiver.hom.unop_op,
image_subobject_arrow],
end
/-- Given `f, g` with `f ≫ g = 0`, the homology of `g.op, f.op` is the opposite of the homology of
`f, g`. -/
def homology_op {X Y Z : V} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0) :
homology g.op f.op (by rw [←op_comp, w, op_zero]) ≅ opposite.op (homology f g w) :=
cokernel_iso_of_eq (image_to_kernel_op _ _ w) ≪≫ (cokernel_epi_comp _ _)
≪≫ cokernel_comp_is_iso _ _ ≪≫ cokernel_op_op _ ≪≫ ((homology_iso_kernel_desc _ _ _)
≪≫ (kernel_iso_of_eq (by ext; simp only [image.fac, cokernel.π_desc, cokernel.π_desc_assoc]))
≪≫ (kernel_comp_mono _ (image.ι g))).op
/-- Given morphisms `f, g` in `Vᵒᵖ` with `f ≫ g = 0`, the homology of `g.unop, f.unop` is the
opposite of the homology of `f, g`. -/
def homology_unop {X Y Z : Vᵒᵖ} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0) :
homology g.unop f.unop (by rw [←unop_comp, w, unop_zero]) ≅ opposite.unop (homology f g w) :=
cokernel_iso_of_eq (image_to_kernel_unop _ _ w) ≪≫ (cokernel_epi_comp _ _)
≪≫ cokernel_comp_is_iso _ _ ≪≫ cokernel_unop_unop _
≪≫ ((homology_iso_kernel_desc _ _ _)
≪≫ (kernel_iso_of_eq (by ext; simp only [image.fac, cokernel.π_desc, cokernel.π_desc_assoc]))
≪≫ (kernel_comp_mono _ (image.ι g))).unop
end
namespace homological_complex
variables {ι V : Type*} [category V] {c : complex_shape ι}
section
variables [preadditive V]
/-- Sends a complex `X` with objects in `V` to the corresponding complex with objects in `Vᵒᵖ`. -/
@[simps] protected def op (X : homological_complex V c) : homological_complex Vᵒᵖ c.symm :=
{ X := λ i, op (X.X i),
d := λ i j, (X.d j i).op,
shape' := λ i j hij, by { rw [X.shape j i hij, op_zero], },
d_comp_d' := by { intros, rw [← op_comp, X.d_comp_d, op_zero], } }
/-- Sends a complex `X` with objects in `V` to the corresponding complex with objects in `Vᵒᵖ`. -/
@[simps] protected def op_symm (X : homological_complex V c.symm) : homological_complex Vᵒᵖ c :=
{ X := λ i, op (X.X i),
d := λ i j, (X.d j i).op,
shape' := λ i j hij, by { rw [X.shape j i hij, op_zero], },
d_comp_d' := by { intros, rw [← op_comp, X.d_comp_d, op_zero], } }
/-- Sends a complex `X` with objects in `Vᵒᵖ` to the corresponding complex with objects in `V`. -/
@[simps] protected def unop (X : homological_complex Vᵒᵖ c) : homological_complex V c.symm :=
{ X := λ i, unop (X.X i),
d := λ i j, (X.d j i).unop,
shape' := λ i j hij, by { rw [X.shape j i hij, unop_zero], },
d_comp_d' := by { intros, rw [← unop_comp, X.d_comp_d, unop_zero], } }
/-- Sends a complex `X` with objects in `Vᵒᵖ` to the corresponding complex with objects in `V`. -/
@[simps] protected def unop_symm (X : homological_complex Vᵒᵖ c.symm) : homological_complex V c :=
{ X := λ i, unop (X.X i),
d := λ i j, (X.d j i).unop,
shape' := λ i j hij, by { rw [X.shape j i hij, unop_zero], },
d_comp_d' := by { intros, rw [← unop_comp, X.d_comp_d, unop_zero], } }
variables (V c)
/-- Auxilliary definition for `op_equivalence`. -/
@[simps] def op_functor : (homological_complex V c)ᵒᵖ ⥤ homological_complex Vᵒᵖ c.symm :=
{ obj := λ X, (unop X).op,
map := λ X Y f,
{ f := λ i, (f.unop.f i).op,
comm' := λ i j hij, by simp only [op_d, ← op_comp, f.unop.comm] }, }
/-- Auxilliary definition for `op_equivalence`. -/
@[simps] def op_inverse : homological_complex Vᵒᵖ c.symm ⥤ (homological_complex V c)ᵒᵖ :=
{ obj := λ X, op X.unop_symm,
map := λ X Y f, quiver.hom.op
{ f := λ i, (f.f i).unop,
comm' := λ i j hij, by simp only [unop_symm_d, ←unop_comp, f.comm], }}
/-- Auxilliary definition for `op_equivalence`. -/
def op_unit_iso : 𝟭 (homological_complex V c)ᵒᵖ ≅ op_functor V c ⋙ op_inverse V c :=
nat_iso.of_components (λ X, (homological_complex.hom.iso_of_components (λ i, iso.refl _)
(λ i j hij, by simp only [iso.refl_hom, category.id_comp, unop_symm_d, op_d, quiver.hom.unop_op,
category.comp_id]) : (opposite.unop X).op.unop_symm ≅ unop X).op)
begin
intros X Y f,
refine quiver.hom.unop_inj _,
ext,
simp only [quiver.hom.unop_op, functor.id_map, iso.op_hom, functor.comp_map,
unop_comp, comp_f, hom.iso_of_components_hom_f],
erw [category.id_comp, category.comp_id (f.unop.f x)],
end
/-- Auxilliary definition for `op_equivalence`. -/
def op_counit_iso : op_inverse V c ⋙ op_functor V c ≅ 𝟭 (homological_complex Vᵒᵖ c.symm) :=
nat_iso.of_components (λ X, homological_complex.hom.iso_of_components (λ i, iso.refl _)
(λ i j hij, by simpa only [iso.refl_hom, category.id_comp, category.comp_id]))
begin
intros X Y f,
ext,
simpa only [quiver.hom.unop_op, quiver.hom.op_unop, functor.comp_map, functor.id_map,
iso.refl_hom, category.id_comp, category.comp_id, comp_f, hom.iso_of_components_hom_f],
end
/-- Given a category of complexes with objects in `V`, there is a natural equivalence between its
opposite category and a category of complexes with objects in `Vᵒᵖ`. -/
@[simps] def op_equivalence : (homological_complex V c)ᵒᵖ ≌ homological_complex Vᵒᵖ c.symm :=
{ functor := op_functor V c,
inverse := op_inverse V c,
unit_iso := op_unit_iso V c,
counit_iso := op_counit_iso V c,
functor_unit_iso_comp' :=
begin
intro X,
ext,
simp only [op_unit_iso, op_counit_iso, nat_iso.of_components_hom_app, iso.op_hom,
comp_f, op_functor_map_f, quiver.hom.unop_op, hom.iso_of_components_hom_f],
exact category.comp_id _,
end }
/-- Auxilliary definition for `unop_equivalence`. -/
@[simps] def unop_functor : (homological_complex Vᵒᵖ c)ᵒᵖ ⥤ homological_complex V c.symm :=
{ obj := λ X, (unop X).unop,
map := λ X Y f,
{ f := λ i, (f.unop.f i).unop,
comm' := λ i j hij, by simp only [unop_d, ← unop_comp, f.unop.comm] }, }
/-- Auxilliary definition for `unop_equivalence`. -/
@[simps] def unop_inverse : homological_complex V c.symm ⥤ (homological_complex Vᵒᵖ c)ᵒᵖ :=
{ obj := λ X, op X.op_symm,
map := λ X Y f, quiver.hom.op
{ f := λ i, (f.f i).op,
comm' := λ i j hij, by simp only [op_symm_d, ←op_comp, f.comm], }}
/-- Auxilliary definition for `unop_equivalence`. -/
def unop_unit_iso : 𝟭 (homological_complex Vᵒᵖ c)ᵒᵖ ≅ unop_functor V c ⋙ unop_inverse V c :=
nat_iso.of_components (λ X, (homological_complex.hom.iso_of_components (λ i, iso.refl _)
(λ i j hij, by simp only [iso.refl_hom, category.id_comp, unop_symm_d, op_d, quiver.hom.unop_op,
category.comp_id]) : (opposite.unop X).op.unop_symm ≅ unop X).op)
begin
intros X Y f,
refine quiver.hom.unop_inj _,
ext,
simp only [quiver.hom.unop_op, functor.id_map, iso.op_hom, functor.comp_map,
unop_comp, comp_f, hom.iso_of_components_hom_f],
erw [category.id_comp, category.comp_id (f.unop.f x)],
end
/-- Auxilliary definition for `unop_equivalence`. -/
def unop_counit_iso : unop_inverse V c ⋙ unop_functor V c ≅ 𝟭 (homological_complex V c.symm) :=
nat_iso.of_components (λ X, homological_complex.hom.iso_of_components (λ i, iso.refl _)
(λ i j hij, by simpa only [iso.refl_hom, category.id_comp, category.comp_id]))
begin
intros X Y f,
ext,
simpa only [quiver.hom.unop_op, quiver.hom.op_unop, functor.comp_map, functor.id_map,
iso.refl_hom, category.id_comp, category.comp_id, comp_f, hom.iso_of_components_hom_f],
end
/-- Given a category of complexes with objects in `Vᵒᵖ`, there is a natural equivalence between its
opposite category and a category of complexes with objects in `V`. -/
@[simps] def unop_equivalence : (homological_complex Vᵒᵖ c)ᵒᵖ ≌ homological_complex V c.symm :=
{ functor := unop_functor V c,
inverse := unop_inverse V c,
unit_iso := unop_unit_iso V c,
counit_iso := unop_counit_iso V c,
functor_unit_iso_comp' :=
begin
intro X,
ext,
simp only [op_unit_iso, op_counit_iso, nat_iso.of_components_hom_app, iso.op_hom,
comp_f, op_functor_map_f, quiver.hom.unop_op, hom.iso_of_components_hom_f],
exact category.comp_id _,
end }
variables {V c}
instance op_functor_additive : (@op_functor ι V _ c _).additive := {}
instance unop_functor_additive : (@unop_functor ι V _ c _).additive := {}
end
variables [abelian V] (C : homological_complex V c) (i : ι)
/-- Auxilliary tautological definition for `homology_op`. -/
def homology_op_def :
C.op.homology i ≅ _root_.homology (C.d_from i).op (C.d_to i).op
(by rw [←op_comp, C.d_to_comp_d_from i, op_zero]) := iso.refl _
/-- Given a complex `C` of objects in `V`, the `i`th homology of its 'opposite' complex (with
objects in `Vᵒᵖ`) is the opposite of the `i`th homology of `C`. -/
def homology_op : C.op.homology i ≅ opposite.op (C.homology i) :=
homology_op_def _ _ ≪≫ homology_op _ _ _
/-- Auxilliary tautological definition for `homology_unop`. -/
def homology_unop_def (C : homological_complex Vᵒᵖ c) :
C.unop.homology i ≅ _root_.homology (C.d_from i).unop (C.d_to i).unop
(by rw [←unop_comp, C.d_to_comp_d_from i, unop_zero]) := iso.refl _
/-- Given a complex `C` of objects in `Vᵒᵖ`, the `i`th homology of its 'opposite' complex (with
objects in `V`) is the opposite of the `i`th homology of `C`. -/
def homology_unop (C : homological_complex Vᵒᵖ c) :
C.unop.homology i ≅ opposite.unop (C.homology i) :=
homology_unop_def _ _ ≪≫ homology_unop _ _ _
end homological_complex
|
79b31ab94f41a0cd5d92e681434bfee5a9eb42a8 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/information_theory/hamming.lean | 541c080cd68dc4f039876e1032c5f40764c374ca | [
"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 | 14,137 | lean | /-
Copyright (c) 2022 Wrenna Robson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wrenna Robson
-/
import analysis.normed.group.basic
/-!
# Hamming spaces
The Hamming metric counts the number of places two members of a (finite) Pi type
differ. The Hamming norm is the same as the Hamming metric over additive groups, and
counts the number of places a member of a (finite) Pi type differs from zero.
This is a useful notion in various applications, but in particular it is relevant
in coding theory, in which it is fundamental for defining the minimum distance of a
code.
## Main definitions
* `hamming_dist x y`: the Hamming distance between `x` and `y`, the number of entries which differ.
* `hamming_norm x`: the Hamming norm of `x`, the number of non-zero entries.
* `hamming β`: a type synonym for `Π i, β i` with `dist` and `norm` provided by the above.
* `hamming.to_hamming`, `hamming.of_hamming`: functions for casting between `hamming β` and
`Π i, β i`.
* `hamming.normed_add_comm_group`: the Hamming norm forms a normed group on `hamming β`.
-/
section hamming_dist_norm
open finset function
variables {α ι : Type*} {β : ι → Type*} [fintype ι] [Π i, decidable_eq (β i)]
variables {γ : ι → Type*} [Π i, decidable_eq (γ i)]
/-- The Hamming distance function to the naturals. -/
def hamming_dist (x y : Π i, β i) : ℕ := (univ.filter (λ i, x i ≠ y i)).card
/-- Corresponds to `dist_self`. -/
@[simp] lemma hamming_dist_self (x : Π i, β i) : hamming_dist x x = 0 :=
by { rw [hamming_dist, card_eq_zero, filter_eq_empty_iff], exact λ _ _ H, H rfl }
/-- Corresponds to `dist_nonneg`. -/
lemma hamming_dist_nonneg {x y : Π i, β i} : 0 ≤ hamming_dist x y := zero_le _
/-- Corresponds to `dist_comm`. -/
lemma hamming_dist_comm (x y : Π i, β i) : hamming_dist x y = hamming_dist y x :=
by simp_rw [hamming_dist, ne_comm]
/-- Corresponds to `dist_triangle`. -/
lemma hamming_dist_triangle (x y z : Π i, β i) :
hamming_dist x z ≤ hamming_dist x y + hamming_dist y z :=
begin
classical, simp_rw hamming_dist, refine le_trans (card_mono _) (card_union_le _ _),
rw ← filter_or, refine monotone_filter_right _ _, intros i h,
by_contra' H, exact h (eq.trans H.1 H.2)
end
/-- Corresponds to `dist_triangle_left`. -/
lemma hamming_dist_triangle_left (x y z : Π i, β i) :
hamming_dist x y ≤ hamming_dist z x + hamming_dist z y :=
by { rw hamming_dist_comm z, exact hamming_dist_triangle _ _ _ }
/-- Corresponds to `dist_triangle_right`. -/
lemma hamming_dist_triangle_right (x y z : Π i, β i) :
hamming_dist x y ≤ hamming_dist x z + hamming_dist y z :=
by { rw hamming_dist_comm y, exact hamming_dist_triangle _ _ _ }
/-- Corresponds to `swap_dist`. -/
theorem swap_hamming_dist : swap (@hamming_dist _ β _ _) = hamming_dist :=
by { funext x y, exact hamming_dist_comm _ _ }
/-- Corresponds to `eq_of_dist_eq_zero`. -/
lemma eq_of_hamming_dist_eq_zero {x y : Π i, β i} : hamming_dist x y = 0 → x = y :=
by simp_rw [hamming_dist, card_eq_zero, filter_eq_empty_iff, not_not,
funext_iff, mem_univ, forall_true_left, imp_self]
/-- Corresponds to `dist_eq_zero`. -/
@[simp] lemma hamming_dist_eq_zero {x y : Π i, β i} : hamming_dist x y = 0 ↔ x = y :=
⟨eq_of_hamming_dist_eq_zero, (λ H, by {rw H, exact hamming_dist_self _})⟩
/-- Corresponds to `zero_eq_dist`. -/
@[simp] lemma hamming_zero_eq_dist {x y : Π i, β i} : 0 = hamming_dist x y ↔ x = y :=
by rw [eq_comm, hamming_dist_eq_zero]
/-- Corresponds to `dist_ne_zero`. -/
lemma hamming_dist_ne_zero {x y : Π i, β i} : hamming_dist x y ≠ 0 ↔ x ≠ y :=
hamming_dist_eq_zero.not
/-- Corresponds to `dist_pos`. -/
@[simp] lemma hamming_dist_pos {x y : Π i, β i} : 0 < hamming_dist x y ↔ x ≠ y :=
by rw [←hamming_dist_ne_zero, iff_not_comm, not_lt, le_zero_iff]
@[simp] lemma hamming_dist_lt_one {x y : Π i, β i} : hamming_dist x y < 1 ↔ x = y :=
by rw [nat.lt_one_iff, hamming_dist_eq_zero]
lemma hamming_dist_le_card_fintype {x y : Π i, β i} :
hamming_dist x y ≤ fintype.card ι := card_le_univ _
lemma hamming_dist_comp_le_hamming_dist (f : Π i, γ i → β i) {x y : Π i, γ i} :
hamming_dist (λ i, f i (x i)) (λ i, f i (y i)) ≤ hamming_dist x y :=
card_mono (monotone_filter_right _ $ λ i H1 H2, H1 $ congr_arg (f i) H2)
lemma hamming_dist_comp (f : Π i, γ i → β i) {x y : Π i, γ i} (hf : Π i, injective (f i)) :
hamming_dist (λ i, f i (x i)) (λ i, f i (y i)) = hamming_dist x y :=
begin
refine le_antisymm (hamming_dist_comp_le_hamming_dist _) _,
exact card_mono (monotone_filter_right _ $ λ i H1 H2, H1 $ hf i H2)
end
lemma hamming_dist_smul_le_hamming_dist [Π i, has_smul α (β i)] {k : α} {x y : Π i, β i} :
hamming_dist (k • x) (k • y) ≤ hamming_dist x y :=
hamming_dist_comp_le_hamming_dist $ λ i, (•) k
/-- Corresponds to `dist_smul` with the discrete norm on `α`. -/
lemma hamming_dist_smul [Π i, has_smul α (β i)] {k : α} {x y : Π i, β i}
(hk : Π i, is_smul_regular (β i) k) : hamming_dist (k • x) (k • y) = hamming_dist x y :=
hamming_dist_comp (λ i, (•) k) hk
section has_zero
variables [Π i, has_zero (β i)] [Π i, has_zero (γ i)]
/-- The Hamming weight function to the naturals. -/
def hamming_norm (x : Π i, β i) : ℕ := (univ.filter (λ i, x i ≠ 0)).card
/-- Corresponds to `dist_zero_right`. -/
@[simp] lemma hamming_dist_zero_right (x : Π i, β i) : hamming_dist x 0 = hamming_norm x := rfl
/-- Corresponds to `dist_zero_left`. -/
@[simp] lemma hamming_dist_zero_left : hamming_dist (0 : Π i, β i) = hamming_norm :=
funext $ λ x, by rw [hamming_dist_comm, hamming_dist_zero_right]
/-- Corresponds to `norm_nonneg`. -/
@[simp] lemma hamming_norm_nonneg {x : Π i, β i} : 0 ≤ hamming_norm x := zero_le _
/-- Corresponds to `norm_zero`. -/
@[simp] lemma hamming_norm_zero : hamming_norm (0 : Π i, β i) = 0 := hamming_dist_self _
/-- Corresponds to `norm_eq_zero`. -/
@[simp] lemma hamming_norm_eq_zero {x : Π i, β i} : hamming_norm x = 0 ↔ x = 0 :=
hamming_dist_eq_zero
/-- Corresponds to `norm_ne_zero_iff`. -/
lemma hamming_norm_ne_zero_iff {x : Π i, β i} : hamming_norm x ≠ 0 ↔ x ≠ 0 :=
hamming_norm_eq_zero.not
/-- Corresponds to `norm_pos_iff`. -/
@[simp] lemma hamming_norm_pos_iff {x : Π i, β i} : 0 < hamming_norm x ↔ x ≠ 0 := hamming_dist_pos
@[simp] lemma hamming_norm_lt_one {x : Π i, β i} : hamming_norm x < 1 ↔ x = 0 := hamming_dist_lt_one
lemma hamming_norm_le_card_fintype {x : Π i, β i} : hamming_norm x ≤ fintype.card ι :=
hamming_dist_le_card_fintype
lemma hamming_norm_comp_le_hamming_norm (f : Π i, γ i → β i) {x : Π i, γ i} (hf : Π i, f i 0 = 0) :
hamming_norm (λ i, f i (x i)) ≤ hamming_norm x :=
by {convert hamming_dist_comp_le_hamming_dist f, simp_rw hf, refl}
lemma hamming_norm_comp (f : Π i, γ i → β i) {x : Π i, γ i} (hf₁ : Π i, injective (f i))
(hf₂ : Π i, f i 0 = 0) : hamming_norm (λ i, f i (x i)) = hamming_norm x :=
by {convert hamming_dist_comp f hf₁, simp_rw hf₂, refl}
lemma hamming_norm_smul_le_hamming_norm [has_zero α] [Π i, smul_with_zero α (β i)] {k : α}
{x : Π i, β i} : hamming_norm (k • x) ≤ hamming_norm x :=
hamming_norm_comp_le_hamming_norm (λ i (c : β i), k • c) (λ i, by simp_rw smul_zero')
lemma hamming_norm_smul [has_zero α] [Π i, smul_with_zero α (β i)] {k : α}
(hk : ∀ i, is_smul_regular (β i) k) (x : Π i, β i) : hamming_norm (k • x) = hamming_norm x :=
hamming_norm_comp (λ i (c : β i), k • c) hk (λ i, by simp_rw smul_zero')
end has_zero
/-- Corresponds to `dist_eq_norm`. -/
lemma hamming_dist_eq_hamming_norm [Π i, add_group (β i)] (x y : Π i, β i) :
hamming_dist x y = hamming_norm (x - y) :=
by simp_rw [hamming_norm, hamming_dist, pi.sub_apply, sub_ne_zero]
end hamming_dist_norm
/-! ### The `hamming` type synonym -/
/-- Type synonym for a Pi type which inherits the usual algebraic instances, but is equipped with
the Hamming metric and norm, instead of `pi.normed_add_comm_group` which uses the sup norm. -/
def hamming {ι : Type*} (β : ι → Type*) : Type* := Π i, β i
namespace hamming
variables {α ι : Type*} {β : ι → Type*}
/-! Instances inherited from normal Pi types. -/
instance [Π i, inhabited (β i)] : inhabited (hamming β) := ⟨λ i, default⟩
instance [decidable_eq ι] [fintype ι] [Π i, fintype (β i)] : fintype (hamming β) := pi.fintype
instance [inhabited ι] [∀ i, nonempty (β i)] [nontrivial (β default)] :
nontrivial (hamming β) := pi.nontrivial
instance [fintype ι] [Π i, decidable_eq (β i)] : decidable_eq (hamming β) :=
fintype.decidable_pi_fintype
instance [Π i, has_zero (β i)] : has_zero (hamming β) := pi.has_zero
instance [Π i, has_neg (β i)] : has_neg (hamming β) := pi.has_neg
instance [Π i, has_add (β i)] : has_add (hamming β) := pi.has_add
instance [Π i, has_sub (β i)] : has_sub (hamming β) := pi.has_sub
instance [Π i, has_smul α (β i)] : has_smul α (hamming β) := pi.has_smul
instance [has_zero α] [Π i, has_zero (β i)] [Π i, smul_with_zero α (β i)] :
smul_with_zero α (hamming β) := pi.smul_with_zero _
instance [Π i, add_monoid (β i)] : add_monoid (hamming β) := pi.add_monoid
instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (hamming β) := pi.add_comm_monoid
instance [Π i, add_comm_group (β i)] : add_comm_group (hamming β) := pi.add_comm_group
instance (α) [semiring α] (β : ι → Type*) [Π i, add_comm_monoid (β i)]
[Π i, module α (β i)] : module α (hamming β) := pi.module _ _ _
/-! API to/from the type synonym. -/
/-- `to_hamming` is the identity function to the `hamming` of a type. -/
@[pattern] def to_hamming : (Π i, β i) ≃ hamming β := equiv.refl _
/-- `of_hamming` is the identity function from the `hamming` of a type. -/
@[pattern] def of_hamming : hamming β ≃ Π i, β i := equiv.refl _
@[simp] lemma to_hamming_symm_eq : (@to_hamming _ β).symm = of_hamming := rfl
@[simp] lemma of_hamming_symm_eq : (@of_hamming _ β).symm = to_hamming := rfl
@[simp] lemma to_hamming_of_hamming (x : hamming β) : to_hamming (of_hamming x) = x := rfl
@[simp] lemma of_hamming_to_hamming (x : Π i, β i) : of_hamming (to_hamming x) = x := rfl
@[simp] lemma to_hamming_inj {x y : Π i, β i} : to_hamming x = to_hamming y ↔ x = y := iff.rfl
@[simp] lemma of_hamming_inj {x y : hamming β} : of_hamming x = of_hamming y ↔ x = y := iff.rfl
@[simp] lemma to_hamming_zero [Π i, has_zero (β i)] : to_hamming (0 : Π i, β i) = 0 := rfl
@[simp] lemma of_hamming_zero [Π i, has_zero (β i)] : of_hamming (0 : hamming β) = 0 := rfl
@[simp] lemma to_hamming_neg [Π i, has_neg (β i)] {x : Π i, β i} :
to_hamming (-x) = - to_hamming x := rfl
@[simp] lemma of_hamming_neg [Π i, has_neg (β i)] {x : hamming β} :
of_hamming (-x) = - of_hamming x := rfl
@[simp] lemma to_hamming_add [Π i, has_add (β i)] {x y : Π i, β i} :
to_hamming (x + y) = to_hamming x + to_hamming y := rfl
@[simp] lemma of_hamming_add [Π i, has_add (β i)] {x y : hamming β} :
of_hamming (x + y) = of_hamming x + of_hamming y := rfl
@[simp] lemma to_hamming_sub [Π i, has_sub (β i)] {x y : Π i, β i} :
to_hamming (x - y) = to_hamming x - to_hamming y := rfl
@[simp] lemma of_hamming_sub [Π i, has_sub (β i)] {x y : hamming β} :
of_hamming (x - y) = of_hamming x - of_hamming y := rfl
@[simp] lemma to_hamming_smul [Π i, has_smul α (β i)] {r : α} {x : Π i, β i} :
to_hamming (r • x) = r • to_hamming x := rfl
@[simp] lemma of_hamming_smul [Π i, has_smul α (β i)] {r : α} {x : hamming β} :
of_hamming (r • x) = r • of_hamming x := rfl
section
/-! Instances equipping `hamming` with `hamming_norm` and `hamming_dist`. -/
variables [fintype ι] [Π i, decidable_eq (β i)]
instance : has_dist (hamming β) := ⟨λ x y, hamming_dist (of_hamming x) (of_hamming y)⟩
@[simp, push_cast] lemma dist_eq_hamming_dist (x y : hamming β) :
dist x y = hamming_dist (of_hamming x) (of_hamming y) := rfl
instance : pseudo_metric_space (hamming β) :=
{ dist_self := by { push_cast, exact_mod_cast hamming_dist_self },
dist_comm := by { push_cast, exact_mod_cast hamming_dist_comm },
dist_triangle := by { push_cast, exact_mod_cast hamming_dist_triangle },
to_uniform_space := ⊥,
uniformity_dist := uniformity_dist_of_mem_uniformity _ _ $ λ s, begin
push_cast,
split,
{ refine λ hs, ⟨1, zero_lt_one, λ _ _ hab, _⟩,
rw_mod_cast [hamming_dist_lt_one] at hab,
rw [of_hamming_inj, ← mem_id_rel] at hab,
exact hs hab },
{ rintros ⟨_, hε, hs⟩ ⟨_, _⟩ hab,
rw mem_id_rel at hab,
rw hab,
refine hs (lt_of_eq_of_lt _ hε),
exact_mod_cast hamming_dist_self _ }
end,
to_bornology := ⟨⊥, bot_le⟩,
cobounded_sets := begin
ext,
push_cast,
refine iff_of_true (filter.mem_sets.mpr filter.mem_bot) ⟨fintype.card ι, λ _ _ _ _, _⟩,
exact_mod_cast hamming_dist_le_card_fintype
end,
..hamming.has_dist }
@[simp, push_cast] lemma nndist_eq_hamming_dist (x y : hamming β) :
nndist x y = hamming_dist (of_hamming x) (of_hamming y) := rfl
instance : metric_space (hamming β) :=
{ eq_of_dist_eq_zero :=
by { push_cast, exact_mod_cast @eq_of_hamming_dist_eq_zero _ _ _ _ },
..hamming.pseudo_metric_space }
instance [Π i, has_zero (β i)] : has_norm (hamming β) := ⟨λ x, hamming_norm (of_hamming x)⟩
@[simp, push_cast] lemma norm_eq_hamming_norm [Π i, has_zero (β i)] (x : hamming β) :
∥x∥ = hamming_norm (of_hamming x) := rfl
instance [Π i, add_comm_group (β i)] : seminormed_add_comm_group (hamming β) :=
{ dist_eq := by { push_cast, exact_mod_cast hamming_dist_eq_hamming_norm }, ..pi.add_comm_group }
@[simp, push_cast] lemma nnnorm_eq_hamming_norm [Π i, add_comm_group (β i)] (x : hamming β) :
∥x∥₊ = hamming_norm (of_hamming x) := rfl
instance [Π i, add_comm_group (β i)] : normed_add_comm_group (hamming β) :=
{ ..hamming.seminormed_add_comm_group }
end
end hamming
|
120787f5e62c097d1d51d14b7eef58f744dc3f1b | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/tactic/replacer.lean | 7c70029224bd4a8cfbade7ea6effb03b7eace939 | [
"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 | 4,762 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
A mechanism for defining tactics for use in auto params, whose
meaning is defined incrementally through attributes.
-/
import tactic.core data.string.defs data.list.defs
namespace tactic
meta def replacer_core {α : Type} [reflected α]
(ntac : name) (eval : ∀ β [reflected β], expr → tactic β) :
list name → tactic α
| [] := fail ("no implementation defined for " ++ to_string ntac)
| (n::ns) := do d ← get_decl n, let t := d.type,
tac ← do { mk_const n >>= eval (tactic α) } <|>
do { tac ← mk_const n >>= eval (tactic α → tactic α),
return (tac (replacer_core ns)) } <|>
do { tac ← mk_const n >>= eval (option (tactic α) → tactic α),
return (tac (guard (ns ≠ []) >> some (replacer_core ns))) },
tac
meta def replacer (ntac : name) {α : Type} [reflected α]
(F : Type → Type) (eF : ∀ β, reflected β → reflected (F β))
(R : ∀ β, F β → β) : tactic α :=
attribute.get_instances ntac >>= replacer_core ntac
(λ β eβ e, R β <$> @eval_expr' (F β) (eF β eβ) e)
meta def mk_replacer₁ : expr → nat → expr × expr
| (expr.pi n bi d b) i :=
let (e₁, e₂) := mk_replacer₁ b (i+1) in
(expr.pi n bi d e₁, (`(expr.pi n bi d) : expr) e₂)
| _ i := (expr.var i, expr.var 0)
meta def mk_replacer₂ (ntac : name) (v : expr × expr) : expr → nat → option expr
| (expr.pi n bi d b) i := do
b' ← mk_replacer₂ b (i+1),
some (expr.lam n bi d b')
| `(tactic %%β) i := some $
(expr.const ``replacer []).mk_app [
reflect ntac, β, reflect β,
expr.lam `γ binder_info.default `(Type) v.1,
expr.lam `γ binder_info.default `(Type) $
expr.lam `eγ binder_info.inst_implicit ((`(@reflected Type) : expr) β) v.2,
expr.lam `γ binder_info.default `(Type) $
expr.lam `f binder_info.default v.1 $
(list.range i).foldr (λ i e', e' (expr.var (i+2))) (expr.var 0)
]
| _ i := none
meta def mk_replacer (ntac : name) (e : expr) : tactic expr :=
mk_replacer₂ ntac (mk_replacer₁ e 0) e 0
meta def valid_types : expr → list expr
| (expr.pi n bi d b) := expr.pi n bi d <$> valid_types b
| `(tactic %%β) := [`(tactic.{0} %%β),
`(tactic.{0} %%β → tactic.{0} %%β),
`(option (tactic.{0} %%β) → tactic.{0} %%β)]
| _ := []
meta def replacer_attr (ntac : name) : user_attribute :=
{ name := ntac,
descr :=
"Replaces the definition of `" ++ to_string ntac ++ "`. This should be " ++
"applied to a definition with the type `tactic unit`, which will be " ++
"called whenever `" ++ to_string ntac ++ "` is called. The definition " ++
"can optionally have an argument of type `tactic unit` or " ++
"`option (tactic unit)` which refers to the previous definition, if any.",
after_set := some $ λ n _ _, do
d ← get_decl n,
base ← get_decl ntac,
guardb ((valid_types base.type).any (=ₐ d.type))
<|> fail format!"incorrect type for @[{ntac}]" }
/-- Define a new replaceable tactic. -/
meta def def_replacer (ntac : name) (ty : expr) : tactic unit :=
let nattr := ntac <.> "attr" in do
add_meta_definition nattr []
`(user_attribute) `(replacer_attr %%(reflect ntac)),
set_basic_attribute `user_attribute nattr tt,
v ← mk_replacer ntac ty,
add_meta_definition ntac [] ty v,
add_doc_string ntac $
"The `" ++ to_string ntac ++ "` tactic is a \"replaceable\" " ++
"tactic, which means that its meaning is defined by tactics that " ++
"are defined later with the `@[" ++ to_string ntac ++ "]` attribute. " ++
"It is intended for use with `auto_param`s for structure fields."
open interactive lean.parser
/-- Define a new replaceable tactic. -/
@[user_command] meta def def_replacer_cmd (meta_info : decl_meta_info)
(_ : parse $ tk "def_replacer") : lean.parser unit :=
do ntac ← ident,
ty ← optional (tk ":" *> types.texpr),
match ty with
| (some p) := do t ← to_expr p, def_replacer ntac t
| none := def_replacer ntac `(tactic unit)
end
meta def unprime : name → tactic name
| nn@(name.mk_string s n) :=
let s' := s.over_list (list.take_while (≠ ''')) in
if s'.length < s.length then pure (name.mk_string s' n)
else fail format!"expecting primed name: {nn}"
| n := fail format!"invalid name: {n}"
@[user_attribute] meta def replaceable_attr : user_attribute :=
{ name := `replaceable,
descr := "make definition replaceable in dependent modules",
after_set := some $ λ n' _ _,
do { n ← unprime n',
d ← get_decl n',
«def_replacer» n d.type,
(replacer_attr n).set n' () tt } }
end tactic
|
75cd1c04a2ecaa7e35cd4e1a0fd9d99109d3d106 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/data/bool.lean | d30c3c3968be0d57a9fae57c896c7f79c7f5d341 | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,091 | 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
-/
/-!
# booleans
This file proves various trivial lemmas about booleans and their
relation to decidable propositions.
## Notations
This file introduces the notation `!b` for `bnot b`, the boolean "not".
## Tags
bool, boolean, De Morgan
-/
prefix `!`:90 := bnot
namespace bool
-- TODO: duplicate of a lemma in core
theorem coe_sort_tt : coe_sort.{1 1} tt = true := coe_sort_tt
-- TODO: duplicate of a lemma in core
theorem coe_sort_ff : coe_sort.{1 1} ff = false := coe_sort_ff
-- TODO: duplicate of a lemma in core
theorem to_bool_true {h} : @to_bool true h = tt :=
to_bool_true_eq_tt h
-- TODO: duplicate of a lemma in core
theorem to_bool_false {h} : @to_bool false h = ff :=
to_bool_false_eq_ff h
@[simp] theorem to_bool_coe (b:bool) {h} : @to_bool b h = b :=
(show _ = to_bool b, by congr).trans (by cases b; refl)
theorem coe_to_bool (p : Prop) [decidable p] : to_bool p ↔ p := to_bool_iff _
@[simp] lemma of_to_bool_iff {p : Prop} [decidable p] : to_bool p ↔ p :=
⟨of_to_bool_true, _root_.to_bool_true⟩
@[simp] lemma tt_eq_to_bool_iff {p : Prop} [decidable p] : tt = to_bool p ↔ p :=
eq_comm.trans of_to_bool_iff
@[simp] lemma ff_eq_to_bool_iff {p : Prop} [decidable p] : ff = to_bool p ↔ ¬ p :=
eq_comm.trans (to_bool_ff_iff _)
@[simp] theorem to_bool_not (p : Prop) [decidable p] : to_bool (¬ p) = bnot (to_bool p) :=
by by_cases p; simp *
@[simp] theorem to_bool_and (p q : Prop) [decidable p] [decidable q] :
to_bool (p ∧ q) = p && q :=
by by_cases p; by_cases q; simp *
@[simp] theorem to_bool_or (p q : Prop) [decidable p] [decidable q] :
to_bool (p ∨ q) = p || q :=
by by_cases p; by_cases q; simp *
@[simp] theorem to_bool_eq {p q : Prop} [decidable p] [decidable q] :
to_bool p = to_bool q ↔ (p ↔ q) :=
⟨λ h, (coe_to_bool p).symm.trans $ by simp [h], to_bool_congr⟩
lemma not_ff : ¬ ff := by simp
@[simp] theorem default_bool : default bool = ff := rfl
theorem dichotomy (b : bool) : b = ff ∨ b = tt :=
by cases b; simp
@[simp] theorem forall_bool {p : bool → Prop} : (∀ b, p b) ↔ p ff ∧ p tt :=
⟨λ h, by simp [h], λ ⟨h₁, h₂⟩ b, by cases b; assumption⟩
@[simp] theorem exists_bool {p : bool → Prop} : (∃ b, p b) ↔ p ff ∨ p tt :=
⟨λ ⟨b, h⟩, by cases b; [exact or.inl h, exact or.inr h],
λ h, by cases h; exact ⟨_, h⟩⟩
/-- If `p b` is decidable for all `b : bool`, then `∀ b, p b` is decidable -/
instance decidable_forall_bool {p : bool → Prop} [∀ b, decidable (p b)] : decidable (∀ b, p b) :=
decidable_of_decidable_of_iff and.decidable forall_bool.symm
/-- If `p b` is decidable for all `b : bool`, then `∃ b, p b` is decidable -/
instance decidable_exists_bool {p : bool → Prop} [∀ b, decidable (p b)] : decidable (∃ b, p b) :=
decidable_of_decidable_of_iff or.decidable exists_bool.symm
@[simp] theorem cond_ff {α} (t e : α) : cond ff t e = e := rfl
@[simp] theorem cond_tt {α} (t e : α) : cond tt t e = t := rfl
@[simp] theorem cond_to_bool {α} (p : Prop) [decidable p] (t e : α) :
cond (to_bool p) t e = if p then t else e :=
by by_cases p; simp *
theorem coe_bool_iff : ∀ {a b : bool}, (a ↔ b) ↔ a = b := dec_trivial
theorem eq_tt_of_ne_ff : ∀ {a : bool}, a ≠ ff → a = tt := dec_trivial
theorem eq_ff_of_ne_tt : ∀ {a : bool}, a ≠ tt → a = ff := dec_trivial
theorem bor_comm : ∀ a b, a || b = b || a := dec_trivial
@[simp] theorem bor_assoc : ∀ a b c, (a || b) || c = a || (b || c) := dec_trivial
theorem bor_left_comm : ∀ a b c, a || (b || c) = b || (a || c) := dec_trivial
theorem bor_inl {a b : bool} (H : a) : a || b :=
by simp [H]
theorem bor_inr {a b : bool} (H : b) : a || b :=
by simp [H]
theorem band_comm : ∀ a b, a && b = b && a := dec_trivial
@[simp] theorem band_assoc : ∀ a b c, (a && b) && c = a && (b && c) := dec_trivial
theorem band_left_comm : ∀ a b c, a && (b && c) = b && (a && c) := dec_trivial
theorem band_elim_left : ∀ {a b : bool}, a && b → a := dec_trivial
theorem band_intro : ∀ {a b : bool}, a → b → a && b := dec_trivial
theorem band_elim_right : ∀ {a b : bool}, a && b → b := dec_trivial
@[simp] theorem bnot_false : bnot ff = tt := rfl
@[simp] theorem bnot_true : bnot tt = ff := rfl
theorem eq_tt_of_bnot_eq_ff : ∀ {a : bool}, bnot a = ff → a = tt := dec_trivial
theorem eq_ff_of_bnot_eq_tt : ∀ {a : bool}, bnot a = tt → a = ff := dec_trivial
theorem bxor_comm : ∀ a b, bxor a b = bxor b a := dec_trivial
@[simp] theorem bxor_assoc : ∀ a b c, bxor (bxor a b) c = bxor a (bxor b c) := dec_trivial
theorem bxor_left_comm : ∀ a b c, bxor a (bxor b c) = bxor b (bxor a c) := dec_trivial
@[simp] theorem bxor_bnot_left : ∀ a, bxor (!a) a = tt := dec_trivial
@[simp] theorem bxor_bnot_right : ∀ a, bxor a (!a) = tt := dec_trivial
@[simp] theorem bxor_bnot_bnot : ∀ a b, bxor (!a) (!b) = bxor a b := dec_trivial
@[simp] theorem bxor_ff_left : ∀ a, bxor ff a = a := dec_trivial
@[simp] theorem bxor_ff_right : ∀ a, bxor a ff = a := dec_trivial
lemma bxor_iff_ne : ∀ {x y : bool}, bxor x y = tt ↔ x ≠ y := dec_trivial
/-! ### De Morgan's laws for booleans-/
@[simp] lemma bnot_band : ∀ (a b : bool), !(a && b) = !a || !b := dec_trivial
@[simp] lemma bnot_bor : ∀ (a b : bool), !(a || b) = !a && !b := dec_trivial
lemma bnot_inj : ∀ {a b : bool}, !a = !b → a = b := dec_trivial
end bool
instance : linear_order bool :=
{ le := λ a b, a = ff ∨ b = tt,
le_refl := dec_trivial,
le_trans := dec_trivial,
le_antisymm := dec_trivial,
le_total := dec_trivial,
decidable_le := infer_instance,
decidable_eq := infer_instance,
decidable_lt := infer_instance }
namespace bool
@[simp] lemma ff_le {x : bool} : ff ≤ x := or.intro_left _ rfl
@[simp] lemma le_tt {x : bool} : x ≤ tt := or.intro_right _ rfl
@[simp] lemma ff_lt_tt : ff < tt := lt_of_le_of_ne ff_le ff_ne_tt
/-- convert a `bool` to a `ℕ`, `false -> 0`, `true -> 1` -/
def to_nat (b : bool) : ℕ :=
cond b 1 0
/-- convert a `ℕ` to a `bool`, `0 -> false`, everything else -> `true` -/
def of_nat (n : ℕ) : bool :=
to_bool (n ≠ 0)
lemma of_nat_le_of_nat {n m : ℕ} (h : n ≤ m) : of_nat n ≤ of_nat m :=
begin
simp [of_nat];
cases nat.decidable_eq n 0;
cases nat.decidable_eq m 0;
simp only [to_bool],
{ subst m, have h := le_antisymm h (nat.zero_le _),
contradiction },
{ left, refl }
end
lemma to_nat_le_to_nat {b₀ b₁ : bool} (h : b₀ ≤ b₁) : to_nat b₀ ≤ to_nat b₁ :=
by cases h; subst h; [cases b₁, cases b₀]; simp [to_nat,nat.zero_le]
lemma of_nat_to_nat (b : bool) : of_nat (to_nat b) = b :=
by cases b; simp only [of_nat,to_nat]; exact dec_trivial
@[simp] lemma injective_iff {α : Sort*} {f : bool → α} : function.injective f ↔ f ff ≠ f tt :=
⟨λ Hinj Heq, ff_ne_tt (Hinj Heq),
λ H x y hxy, by { cases x; cases y, exacts [rfl, (H hxy).elim, (H hxy.symm).elim, rfl] }⟩
end bool
|
66e87dd4aa779716c1ad0a9ad64e7d4f4dee0bd4 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/full_subcategory.lean | 235c6fa4e74432abc62877ffa82cd7c5ef963632 | [
"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,990 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Reid Barton
-/
import category_theory.groupoid
namespace category_theory
universes v u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
section induced
/- Induced categories.
Given a category D and a function F : C → D from a type C to the
objects of D, there is an essentially unique way to give C a
category structure such that F becomes a fully faithful functor,
namely by taking Hom_C(X, Y) = Hom_D(FX, FY). We call this the
category induced from D along F.
As a special case, if C is a subtype of D, this produces the full
subcategory of D on the objects belonging to C. In general the
induced category is equivalent to the full subcategory of D on the
image of F.
-/
/-
It looks odd to make D an explicit argument of `induced_category`,
when it is determined by the argument F anyways. The reason to make D
explicit is in order to control its syntactic form, so that instances
like `induced_category.has_forget₂` (elsewhere) refer to the correct
form of D. This is used to set up several algebraic categories like
def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid)
-- not `induced_category (bundled monoid) (bundled.map @comm_monoid.to_monoid)`,
-- even though `Mon = bundled monoid`!
-/
variables {C : Type u₁} (D : Type u₂) [category.{v} D]
variables (F : C → D)
include F
/--
`induced_category D F`, where `F : C → D`, is a typeclass synonym for `C`,
which provides a category structure so that the morphisms `X ⟶ Y` are the morphisms
in `D` from `F X` to `F Y`.
-/
@[nolint has_inhabited_instance unused_arguments]
def induced_category : Type u₁ := C
variables {D}
instance induced_category.has_coe_to_sort [has_coe_to_sort D] :
has_coe_to_sort (induced_category D F) :=
⟨_, λ c, ↥(F c)⟩
instance induced_category.category : category.{v} (induced_category D F) :=
{ hom := λ X Y, F X ⟶ F Y,
id := λ X, 𝟙 (F X),
comp := λ _ _ _ f g, f ≫ g }
/--
The forgetful functor from an induced category to the original category,
forgetting the extra data.
-/
@[simps] def induced_functor : induced_category D F ⥤ D :=
{ obj := F, map := λ x y f, f }
instance induced_category.full : full (induced_functor F) :=
{ preimage := λ x y f, f }
instance induced_category.faithful : faithful (induced_functor F) := {}
end induced
instance induced_category.groupoid {C : Type u₁} (D : Type u₂) [groupoid.{v} D] (F : C → D) :
groupoid.{v} (induced_category D F) :=
{ inv := λ X Y f, groupoid.inv f,
inv_comp' := λ X Y f, groupoid.inv_comp f,
comp_inv' := λ X Y f, groupoid.comp_inv f,
.. induced_category.category F }
section full_subcategory
/- A full subcategory is the special case of an induced category with F = subtype.val. -/
variables {C : Type u₂} [category.{v} C]
variables (Z : C → Prop)
/--
The category structure on a subtype; morphisms just ignore the property.
See https://stacks.math.columbia.edu/tag/001D. We do not define 'strictly full' subcategories.
-/
instance full_subcategory : category.{v} {X : C // Z X} :=
induced_category.category subtype.val
/--
The forgetful functor from a full subcategory into the original category
("forgetting" the condition).
-/
def full_subcategory_inclusion : {X : C // Z X} ⥤ C :=
induced_functor subtype.val
@[simp] lemma full_subcategory_inclusion.obj {X} :
(full_subcategory_inclusion Z).obj X = X.val := rfl
@[simp] lemma full_subcategory_inclusion.map {X Y} {f : X ⟶ Y} :
(full_subcategory_inclusion Z).map f = f := rfl
instance full_subcategory.full : full (full_subcategory_inclusion Z) :=
induced_category.full subtype.val
instance full_subcategory.faithful : faithful (full_subcategory_inclusion Z) :=
induced_category.faithful subtype.val
end full_subcategory
end category_theory
|
6da5b2b3f12e41379ec00e6796f15a53f68d11c2 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/topology/algebra/multilinear.lean | 35e778cb8db626043c6df69d46187d6be2c971a7 | [
"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 | 10,486 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.algebra.module linear_algebra.multilinear
/-!
# Continuous multilinear maps
We define continuous multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are multilinear
and continuous, by extending the space of multilinear maps with a continuity assumption.
Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type, and all these
spaces are also topological spaces.
## Main definitions
* `continuous_multilinear_map R M₁ M₂` is the space of continuous multilinear maps from
`Π(i : ι), M₁ i` to `M₂`. We show that it is an `R`-module.
## Implementation notes
We mostly follow the API of multilinear maps.
## Notation
We introduce the notation `M [×n]→L[R] M'` for the space of continuous `n`-multilinear maps from
`M^n` to `M'`. This is a particular case of the general notion (where we allow varying dependent
types as the arguments of our continuous multilinear maps), but arguably the most important one,
especially when defining iterated derivatives.
-/
open function fin set
universes u v w w₁ w₂ w₃ w₄
variables {R : Type u} {ι : Type v} {n : ℕ}
{M : fin n.succ → Type w} {M₁ : ι → Type w₁} {M₂ : Type w₂} {M₃ : Type w₃} {M₄ : Type w₄}
[decidable_eq ι]
/-- Continuous multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂`
are modules over `R` with a topological structure. In applications, there will be compatibility
conditions between the algebraic and the topological structures, but this is not needed for the
definition. -/
structure continuous_multilinear_map (R : Type u) {ι : Type v} (M₁ : ι → Type w₁) (M₂ : Type w₂)
[decidable_eq ι] [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)]
[module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂]
extends multilinear_map R M₁ M₂ :=
(cont : continuous to_fun)
notation M `[×`:25 n `]→L[`:25 R `] ` M' := continuous_multilinear_map R (λ (i : fin n), M) M'
namespace continuous_multilinear_map
section ring
variables [ring R]
[∀i, add_comm_group (M i)] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂]
[add_comm_group M₃] [add_comm_group M₄]
[∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] [module R M₄]
[∀i, topological_space (M i)] [∀i, topological_space (M₁ i)] [topological_space M₂]
[topological_space M₃] [topological_space M₄]
(f f' : continuous_multilinear_map R M₁ M₂)
instance : has_coe_to_fun (continuous_multilinear_map R M₁ M₂) :=
⟨_, λ f, f.to_multilinear_map.to_fun⟩
@[ext] theorem ext {f f' : continuous_multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' :=
by { cases f, cases f', congr, ext x, exact H x }
@[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x + y)) = f (update m i x) + f (update m i y) :=
f.add m i x y
@[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) :
f (update m i (c • x)) = c • f (update m i x) :=
f.smul m i c x
@[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x - y)) = f (update m i x) - f (update m i y) :=
f.to_multilinear_map.map_sub _ _ _ _
lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 :=
f.to_multilinear_map.map_coord_zero i h
@[simp] lemma map_zero [nonempty ι] : f 0 = 0 :=
f.to_multilinear_map.map_zero
instance : has_zero (continuous_multilinear_map R M₁ M₂) :=
⟨{ cont := continuous_const, ..(0 : multilinear_map R M₁ M₂) }⟩
instance : inhabited (continuous_multilinear_map R M₁ M₂) := ⟨0⟩
@[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : continuous_multilinear_map R M₁ M₂) m = 0 := rfl
section topological_add_group
variable [topological_add_group M₂]
instance : has_add (continuous_multilinear_map R M₁ M₂) :=
⟨λ f f', {cont := f.cont.add f'.cont, ..(f.to_multilinear_map + f'.to_multilinear_map)}⟩
@[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl
instance : has_neg (continuous_multilinear_map R M₁ M₂) :=
⟨λ f, {cont := f.cont.neg, ..(-f.to_multilinear_map)}⟩
@[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl
instance : add_comm_group (continuous_multilinear_map R M₁ M₂) :=
by refine {zero := 0, add := (+), neg := has_neg.neg, ..};
intros; ext; simp [add_comm, add_left_comm]
@[simp] lemma sub_apply (m : Πi, M₁ i) : (f - f') m = f m - f' m := rfl
end topological_add_group
/-- If `f` is a continuous multilinear map, then `f.to_continuous_linear_map m i` is the continuous
linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the
`i`-th coordinate. -/
def to_continuous_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →L[R] M₂ :=
{ cont := f.cont.comp continuous_update, ..(f.to_multilinear_map.to_linear_map m i) }
/-- The cartesian product of two continuous multilinear maps, as a continuous multilinear map. -/
def prod (f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) :
continuous_multilinear_map R M₁ (M₂ × M₃) :=
{ cont := f.cont.prod_mk g.cont,
.. f.to_multilinear_map.prod g.to_multilinear_map }
@[simp] lemma prod_apply
(f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) (m : Πi, M₁ i) :
(f.prod g) m = (f m, g m) := rfl
/- If `R` and `M₃` are implicit in the next definition, Lean is never able to inder them, even
given `g` and `f`. Therefore, we make them explicit. -/
variables (R M₃)
/-- If `g` is continuous multilinear and `f` is continuous linear, then `g (f m₁, ..., f mₙ)` is
again a continuous multilinear map, that we call `g.comp_continuous_linear_map f`. -/
def comp_continuous_linear_map
(g : continuous_multilinear_map R (λ (i : ι), M₃) M₄) (f : M₂ →L[R] M₃) :
continuous_multilinear_map R (λ (i : ι), M₂) M₄ :=
{ cont := g.cont.comp $ continuous_pi $ λj, f.cont.comp $ continuous_apply _,
.. g.to_multilinear_map.comp_linear_map f.to_linear_map }
variables {R M₃}
/-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one
can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the
additivity of a multilinear map along the first variable. -/
lemma cons_add (f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) :
f (cons (x+y) m) = f (cons x m) + f (cons y m) :=
f.to_multilinear_map.cons_add m x y
/-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one
can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the
multiplicativity of a multilinear map along the first variable. -/
lemma cons_smul
(f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) :
f (cons (c • x) m) = c • f (cons x m) :=
f.to_multilinear_map.cons_smul m c x
end ring
section comm_ring
variables [comm_ring R]
[∀i, add_comm_group (M₁ i)] [add_comm_group M₂]
[∀i, module R (M₁ i)] [module R M₂]
[∀i, topological_space (M₁ i)] [topological_space M₂]
(f : continuous_multilinear_map R M₁ M₂)
lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) :
f (s.piecewise (λ i, c i • m i) m) = s.prod c • f m :=
f.to_multilinear_map.map_piecewise_smul _ _ _
/-- Multiplicativity of a continuous multilinear map along all coordinates at the same time,
writing `f (λ i, c i • m i)` as `univ.prod c • f m`. -/
lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) :
f (λ i, c i • m i) = finset.univ.prod c • f m :=
f.to_multilinear_map.map_smul_univ _ _
lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) :
f (t.piecewise (m + m') m') = t.powerset.sum (λ s, f (s.piecewise m m')) :=
f.to_multilinear_map.map_piecewise_add _ _ _
/-- Additivity of a continuous multilinear map along all coordinates at the same time,
writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/
lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) :
f (m + m') = (finset.univ : finset (finset ι)).sum (λ s, f (s.piecewise m m')) :=
f.to_multilinear_map.map_add_univ _ _
variables [topological_space R] [topological_module R M₂]
instance : has_scalar R (continuous_multilinear_map R M₁ M₂) :=
⟨λ c f, { cont := continuous.smul continuous_const f.cont, .. c • f.to_multilinear_map }⟩
@[simp] lemma smul_apply (c : R) (m : Πi, M₁ i) : (c • f) m = c • f m := rfl
/-- The space of continuous multilinear maps is a module over `R`, for the pointwise addition and
scalar multiplication. -/
instance [topological_add_group M₂] : module R (continuous_multilinear_map R M₁ M₂) :=
module.of_core $ by refine { smul := (•), ..};
intros; ext; simp [smul_add, add_smul, smul_smul]
/-- Linear map version of the map `to_multilinear_map` associating to a continuous multilinear map
the corresponding multilinear map. -/
def to_multilinear_map_linear [topological_add_group M₂] :
(continuous_multilinear_map R M₁ M₂) →ₗ[R] (multilinear_map R M₁ M₂) :=
{ to_fun := λ f, f.to_multilinear_map,
add := λ f g, rfl,
smul := λ c f, rfl }
end comm_ring
end continuous_multilinear_map
namespace continuous_linear_map
variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃]
[∀i, module R (M₁ i)] [module R M₂] [module R M₃]
[∀i, topological_space (M₁ i)] [topological_space M₂] [topological_space M₃]
/-- Composing a continuous multilinear map with a continuous linear map gives again a
continuous multilinear map. -/
def comp_continuous_multilinear_map (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) :
continuous_multilinear_map R M₁ M₃ :=
{ cont := g.cont.comp f.cont,
.. g.to_linear_map.comp_multilinear_map f.to_multilinear_map }
@[simp] lemma comp_continuous_multilinear_map_coe (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) :
((g.comp_continuous_multilinear_map f) : (Πi, M₁ i) → M₃) =
(g : M₂ → M₃) ∘ (f : (Πi, M₁ i) → M₂) :=
by { ext m, refl }
end continuous_linear_map
|
6ff438527d22b9183567f8e804e4dc463e2162b5 | 8e6cad62ec62c6c348e5faaa3c3f2079012bdd69 | /src/field_theory/separable.lean | 2e4c15c2b995e9a9d18d64fabaa7db8ff4974d7e | [
"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 | 25,692 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau.
-/
import algebra.polynomial.big_operators
import field_theory.minpoly
import field_theory.splitting_field
import field_theory.tower
import algebra.squarefree
/-!
# Separable polynomials
We define a polynomial to be separable if it is coprime with its derivative. We prove basic
properties about separable polynomials here.
## Main definitions
* `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative.
* `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a
commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`.
* `polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`.
-/
universes u v w
open_locale classical big_operators
open finset
namespace polynomial
section comm_semiring
variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S]
/-- A polynomial is separable iff it is coprime with its derivative. -/
def separable (f : polynomial R) : Prop :=
is_coprime f f.derivative
lemma separable_def (f : polynomial R) :
f.separable ↔ is_coprime f f.derivative :=
iff.rfl
lemma separable_def' (f : polynomial R) :
f.separable ↔ ∃ a b : polynomial R, a * f + b * f.derivative = 1 :=
iff.rfl
lemma separable_one : (1 : polynomial R).separable :=
is_coprime_one_left
lemma separable_X_add_C (a : R) : (X + C a).separable :=
by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero],
exact is_coprime_one_right }
lemma separable_X : (X : polynomial R).separable :=
by { rw [separable_def, derivative_X], exact is_coprime_one_right }
lemma separable_C (r : R) : (C r).separable ↔ is_unit r :=
by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C]
lemma separable.of_mul_left {f g : polynomial R} (h : (f * g).separable) : f.separable :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this)
end
lemma separable.of_mul_right {f g : polynomial R} (h : (f * g).separable) : g.separable :=
by { rw mul_comm at h, exact h.of_mul_left }
lemma separable.of_dvd {f g : polynomial R} (hf : f.separable) (hfg : g ∣ f) : g.separable :=
by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf }
lemma separable_gcd_left {F : Type*} [field F] {f : polynomial F}
(hf : f.separable) (g : polynomial F) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g)
lemma separable_gcd_right {F : Type*} [field F] {g : polynomial F}
(f : polynomial F) (hg : g.separable) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g)
lemma separable.is_coprime {f g : polynomial R} (h : (f * g).separable) : is_coprime f g :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this)
end
theorem separable.of_pow' {f : polynomial R} :
∀ {n : ℕ} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0
| 0 := λ h, or.inr $ or.inr rfl
| 1 := λ h, or.inr $ or.inl ⟨pow_one f ▸ h, rfl⟩
| (n+2) := λ h, or.inl $ is_coprime_self.1 h.is_coprime.of_mul_right_left
theorem separable.of_pow {f : polynomial R} (hf : ¬is_unit f) {n : ℕ} (hn : n ≠ 0)
(hfs : (f ^ n).separable) : f.separable ∧ n = 1 :=
(hfs.of_pow'.resolve_left hf).resolve_right hn
theorem separable.map {p : polynomial R} (h : p.separable) {f : R →+* S} : (p.map f).separable :=
let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f,
by rw [derivative_map, ← map_mul, ← map_mul, ← map_add, H, map_one]⟩
variables (R) (p q : ℕ)
/-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/
noncomputable def expand : polynomial R →ₐ[R] polynomial R :=
{ commutes' := λ r, eval₂_C _ _,
.. (eval₂_ring_hom C (X ^ p) : polynomial R →+* polynomial R) }
lemma coe_expand : (expand R p : polynomial R → polynomial R) = eval₂ C (X ^ p) := rfl
variables {R}
lemma expand_eq_sum {f : polynomial R} :
expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) :=
by { dsimp [expand, eval₂], refl, }
@[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _
@[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _
@[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r :=
by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul]
theorem expand_expand (f : polynomial R) : expand R p (expand R q f) = expand R (p * q) f :=
polynomial.induction_on f (λ r, by simp_rw expand_C)
(λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X,
alg_hom.map_pow, expand_X, pow_mul])
theorem expand_mul (f : polynomial R) : expand R (p * q) f = expand R p (expand R q f) :=
(expand_expand p q f).symm
@[simp] theorem expand_one (f : polynomial R) : expand R 1 f = f :=
polynomial.induction_on f
(λ r, by rw expand_C)
(λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one])
theorem expand_pow (f : polynomial R) : expand R (p ^ q) f = (expand R p ^[q] f) :=
nat.rec_on q (by rw [pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih,
by rw [function.iterate_succ_apply', pow_succ, expand_mul, ih]
theorem derivative_expand (f : polynomial R) :
(expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) :=
by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one]
theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 :=
begin
simp only [expand_eq_sum],
simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, finsupp.sum],
split_ifs with h,
{ rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl], refl,
{ intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] },
{ intro hn, rw finsupp.not_mem_support_iff.1 hn, split_ifs; refl } },
{ rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, },
end
@[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (n * p) = f.coeff n :=
by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp]
@[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (p * n) = f.coeff n :=
by rw [mul_comm, coeff_expand_mul hp]
theorem expand_eq_map_domain (p : ℕ) (f : polynomial R) :
expand R p f = f.map_domain (*p) :=
polynomial.induction_on' f (λ p q hp hq, by simp [*, finsupp.map_domain_add]) $
λ n a, by simp_rw [expand_monomial, monomial_def, finsupp.map_domain_single]
theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : polynomial R} :
expand R p f = expand R p g ↔ f = g :=
⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩
theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : polynomial R} : expand R p f = 0 ↔ f = 0 :=
by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero]
theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : polynomial R} {r : R} :
expand R p f = C r ↔ f = C r :=
by rw [← expand_C, expand_inj hp, expand_C]
theorem nat_degree_expand (p : ℕ) (f : polynomial R) :
(expand R p f).nat_degree = f.nat_degree * p :=
begin
cases p.eq_zero_or_pos with hp hp,
{ rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] },
by_cases hf : f = 0,
{ rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] },
have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf,
rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1],
refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _,
{ rw coeff_expand hp, split_ifs with hpn,
{ rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn,
rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn },
{ refl } },
{ refine le_degree_of_ne_zero _,
rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf }
end
theorem map_expand {p : ℕ} (hp : 0 < p) {f : R →+* S} {q : polynomial R} :
map f (expand R p q) = expand S p (map f q) :=
by { ext, rw [coeff_map, coeff_expand hp, coeff_expand hp], split_ifs; simp, }
end comm_semiring
section comm_ring
variables {R : Type u} [comm_ring R]
lemma separable_X_sub_C {x : R} : separable (X - C x) :=
by simpa only [sub_eq_add_neg, C_neg] using separable_X_add_C (-x)
lemma separable.mul {f g : polynomial R} (hf : f.separable) (hg : g.separable)
(h : is_coprime f g) : (f * g).separable :=
by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left
((h.symm.mul_right hg).mul_add_right_right _) }
lemma separable_prod' {ι : Sort*} {f : ι → polynomial R} {s : finset ι} :
(∀x∈s, ∀y∈s, x ≠ y → is_coprime (f x) (f y)) → (∀x∈s, (f x).separable) →
(∏ x in s, f x).separable :=
finset.induction_on s (λ _ _, separable_one) $ λ a s has ih h1 h2, begin
simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has,
exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ λ i his, h1.1.2 i his $
ne.symm $ ne_of_mem_of_not_mem his has)
end
lemma separable_prod {ι : Sort*} [fintype ι] {f : ι → polynomial R}
(h1 : pairwise (is_coprime on f)) (h2 : ∀ x, (f x).separable) : (∏ x, f x).separable :=
separable_prod' (λ x hx y hy hxy, h1 x y hxy) (λ x hx, h2 x)
lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ι : Sort*} {f : ι → R} {s : finset ι}
(hfs : (∏ i in s, (X - C (f i))).separable)
{x y : ι} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y :=
begin
by_contra hxy,
rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy),
prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← pow_two] at hfs,
cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C) two_ne_zero).2
end
lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ι : Sort*} [fintype ι] {f : ι → R}
(hfs : (∏ i, (X - C (f i))).separable) : function.injective f :=
λ x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy
lemma is_unit_of_self_mul_dvd_separable {p q : polynomial R}
(hp : p.separable) (hq : q * q ∣ p) : is_unit q :=
begin
obtain ⟨p, rfl⟩ := hq,
apply is_coprime_self.mp,
have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)),
{ simp only [← mul_assoc, mul_add],
convert hp,
rw [derivative_mul, derivative_mul],
ring },
exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this)
end
end comm_ring
section integral_domain
variables (R : Type u) [integral_domain R]
theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) :
is_local_ring_hom (↑(expand R p) : polynomial R →+* polynomial R) :=
begin
refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1,
have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1),
rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2,
rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C]
end
end integral_domain
section field
variables {F : Type u} [field F] {K : Type v} [field K]
theorem separable_iff_derivative_ne_zero {f : polynomial F} (hf : irreducible f) :
f.separable ↔ f.derivative ≠ 0 :=
⟨λ h1 h2, hf.not_unit $ is_coprime_zero_right.1 $ h2 ▸ h1,
λ h, is_coprime_of_dvd (mt and.right h) $ λ g hg1 hg2 ⟨p, hg3⟩ hg4,
let ⟨u, hu⟩ := (hf.is_unit_or_is_unit hg3).resolve_left hg1 in
have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd },
not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt h⟩
theorem separable_map (f : F →+* K) {p : polynomial F} : (p.map f).separable ↔ p.separable :=
by simp_rw [separable_def, derivative_map, is_coprime_map]
section char_p
variables (p : ℕ) [hp : fact p.prime]
include hp
/-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/
noncomputable def contract (f : polynomial F) : polynomial F :=
⟨f.support.preimage (*p) $ λ _ _ _ _, (nat.mul_left_inj hp.pos).1,
λ n, f.coeff (n * p),
λ n, by { rw [finset.mem_preimage, finsupp.mem_support_iff], refl }⟩
theorem coeff_contract (f : polynomial F) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := rfl
theorem of_irreducible_expand {f : polynomial F} (hf : irreducible (expand F p f)) :
irreducible f :=
@@of_irreducible_map _ _ _ (is_local_ring_hom_expand F hp.pos) hf
theorem of_irreducible_expand_pow {f : polynomial F} {n : ℕ} :
irreducible (expand F (p ^ n) f) → irreducible f :=
nat.rec_on n (λ hf, by rwa [pow_zero, expand_one] at hf) $ λ n ih hf,
ih $ of_irreducible_expand p $ by rwa [expand_expand]
variables [HF : char_p F p]
include HF
theorem expand_char (f : polynomial F) :
map (frobenius F p) (expand F p f) = f ^ p :=
begin
refine f.induction_on' (λ a b ha hb, _) (λ n a, _),
{ rw [alg_hom.map_add, map_add, ha, hb, add_pow_char], },
{ rw [expand_monomial, map_monomial, single_eq_C_mul_X, single_eq_C_mul_X,
mul_pow, ← C.map_pow, frobenius_def],
ring_exp }
end
theorem map_expand_pow_char (f : polynomial F) (n : ℕ) :
map ((frobenius F p) ^ n) (expand F (p ^ n) f) = f ^ (p ^ n) :=
begin
induction n, {simp [ring_hom.one_def]},
symmetry,
rw [pow_succ', pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def, ← map_map, mul_comm,
expand_mul, ← map_expand (nat.prime.pos hp)],
end
theorem expand_contract {f : polynomial F} (hf : f.derivative = 0) :
expand F p (contract p f) = f :=
begin
ext n, rw [coeff_expand hp.pos, coeff_contract], split_ifs with h,
{ rw nat.div_mul_cancel h },
{ cases n, { exact absurd (dvd_zero p) h },
have := coeff_derivative f n, rw [hf, coeff_zero, zero_eq_mul] at this, cases this, { rw this },
rw [← nat.cast_succ, char_p.cast_eq_zero_iff F p] at this,
exact absurd this h }
end
theorem separable_or {f : polynomial F} (hf : irreducible f) : f.separable ∨
¬f.separable ∧ ∃ g : polynomial F, irreducible g ∧ expand F p g = f :=
if H : f.derivative = 0 then or.inr
⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H],
contract p f,
by haveI := is_local_ring_hom_expand F hp.pos; exact
of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H at hf),
expand_contract p H⟩
else or.inl $ (separable_iff_derivative_ne_zero hf).2 H
theorem exists_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0) :
∃ (n : ℕ) (g : polynomial F), g.separable ∧ expand F (p ^ n) g = f :=
begin
generalize hn : f.nat_degree = N, unfreezingI { revert f },
apply nat.strong_induction_on N, intros N ih f hf hf0 hn,
rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩,
{ refine ⟨0, f, h, _⟩, rw [pow_zero, expand_one] },
{ cases N with N,
{ rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn,
rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1,
rw [h1, C_0] at hn, exact absurd hn hf0 },
have hg1 : g.nat_degree * p = N.succ,
{ rwa [← nat_degree_expand, hgf] },
have hg2 : g.nat_degree ≠ 0,
{ intro this, rw [this, zero_mul] at hg1, cases hg1 },
have hg3 : g.nat_degree < N.succ,
{ rw [← mul_one g.nat_degree, ← hg1],
exact nat.mul_lt_mul_of_pos_left hp.one_lt (nat.pos_of_ne_zero hg2) },
have hg4 : g ≠ 0,
{ rintro rfl, exact hg2 nat_degree_zero },
rcases ih _ hg3 hg hg4 rfl with ⟨n, g, hg5, rfl⟩, refine ⟨n+1, g, hg5, _⟩,
rw [← hgf, expand_expand, pow_succ] }
end
theorem is_unit_or_eq_zero_of_separable_expand {f : polynomial F} (n : ℕ)
(hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 :=
begin
rw or_iff_not_imp_right, intro hn,
have hf2 : (expand F (p ^ n) f).derivative = 0,
{ by rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero,
zero_pow (nat.pos_of_ne_zero hn), zero_mul, mul_zero] },
rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf, rcases hf with ⟨r, hr, hrf⟩,
rw [eq_comm, expand_eq_C (pow_pos hp.pos _)] at hrf,
rwa [hrf, is_unit_C]
end
theorem unique_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0)
(n₁ : ℕ) (g₁ : polynomial F) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f)
(n₂ : ℕ) (g₂ : polynomial F) (hg₂ : g₂.separable) (hgf₂ : expand F (p ^ n₂) g₂ = f) :
n₁ = n₂ ∧ g₁ = g₂ :=
begin
revert g₁ g₂, wlog hn : n₁ ≤ n₂ := le_total n₁ n₂ using [n₁ n₂, n₂ n₁] tactic.skip,
unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩,
rw [← hgf₁, pow_add, expand_mul, expand_inj (pow_pos hp.pos n₁)] at hgf₂, subst hgf₂,
subst hgf₁,
rcases is_unit_or_eq_zero_of_separable_expand p k hg₁ with h | rfl,
{ rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩,
simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 },
{ rw [add_zero, pow_zero, expand_one], split; refl } },
exact λ g₁ g₂ hg₁ hgf₁ hg₂ hgf₂, let ⟨hn, hg⟩ :=
this g₂ g₁ hg₂ hgf₂ hg₁ hgf₁ in ⟨hn.symm, hg.symm⟩
end
end char_p
lemma separable_prod_X_sub_C_iff' {ι : Sort*} {f : ι → F} {s : finset ι} :
(∏ i in s, (X - C (f i))).separable ↔ (∀ (x ∈ s) (y ∈ s), f x = f y → x = y) :=
⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy,
λ H, by { rw ← prod_attach, exact separable_prod' (λ x hx y hy hxy,
@pairwise_coprime_X_sub _ _ { x // x ∈ s } (λ x, f x)
(λ x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy)
(λ _ _, separable_X_sub_C) }⟩
lemma separable_prod_X_sub_C_iff {ι : Sort*} [fintype ι] {f : ι → F} :
(∏ i, (X - C (f i))).separable ↔ function.injective f :=
separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff]
section splits
open_locale big_operators
variables {i : F →+* K}
lemma not_unit_X_sub_C (a : F) : ¬ is_unit (X - C a) :=
λ h, have one_eq_zero : (1 : with_bot ℕ) = 0, by simpa using degree_eq_zero_of_is_unit h,
one_ne_zero (option.some_injective _ one_eq_zero)
lemma nodup_of_separable_prod {s : multiset F}
(hs : separable (multiset.map (λ a, X - C a) s).prod) : s.nodup :=
begin
rw multiset.nodup_iff_ne_cons_cons,
rintros a t rfl,
refine not_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _),
simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _)
end
lemma multiplicity_le_one_of_separable {p q : polynomial F} (hq : ¬ is_unit q)
(hsep : separable p) : multiplicity q p ≤ 1 :=
begin
contrapose! hq,
apply is_unit_of_self_mul_dvd_separable hsep,
rw ← pow_two,
apply multiplicity.pow_dvd_of_le_multiplicity,
exact_mod_cast (enat.add_one_le_of_lt hq)
end
lemma separable.squarefree {p : polynomial F} (hsep : separable p) : squarefree p :=
begin
rw multiplicity.squarefree_iff_multiplicity_le_one p,
intro f,
by_cases hunit : is_unit f,
{ exact or.inr hunit },
exact or.inl (multiplicity_le_one_of_separable hunit hsep)
end
/--If `n ≠ 0` in `F`, then ` X ^ n - a` is separable for any `a ≠ 0`. -/
lemma separable_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) :
separable (X ^ n - C a) :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ exfalso,
rw hzero at hn,
exact hn (refl 0) },
apply (separable_def' (X ^ n - C a)).2,
use [-C (a⁻¹), (C ((a⁻¹) * (↑n)⁻¹) * X)],
have mul_pow_sub : X * X ^ (n - 1) = X ^ n,
{ nth_rewrite 0 [←pow_one X],
rw pow_mul_pow_sub X (nat.succ_le_iff.mpr hpos) },
rw [derivative_sub, derivative_C, sub_zero, derivative_pow X n, derivative_X, mul_one],
have hcalc : C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n)) = C a⁻¹ * (X ^ n),
{ calc C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n))
= C a⁻¹ * C ((↑n)⁻¹) * (C ↑n * (X ^ n)) : by rw [C_mul, C_eq_nat_cast]
... = C a⁻¹ * (C ((↑n)⁻¹) * C ↑n) * (X ^ n) : by ring
... = C a⁻¹ * C ((↑n)⁻¹ * ↑n) * (X ^ n) : by rw [← C_mul]
... = C a⁻¹ * C 1 * (X ^ n) : by field_simp [hn]
... = C a⁻¹ * (X ^ n) : by rw [C_1, mul_one] },
calc -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * X * (↑n * X ^ (n - 1))
= -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * (↑n * (X * X ^ (n - 1))) : by ring
... = -C a⁻¹ * (X ^ n - C a) + C a⁻¹ * (X ^ n) : by rw [mul_pow_sub, hcalc]
... = C a⁻¹ * C a : by ring
... = (1 : polynomial F) : by rw [← C_mul, inv_mul_cancel ha, C_1]
end
/--If `n ≠ 0` in `F`, then ` X ^ n - a` is squarefree for any `a ≠ 0`. -/
lemma squarefree_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) :
squarefree (X ^ n - C a) :=
(separable_X_pow_sub_C a hn ha).squarefree
lemma root_multiplicity_le_one_of_separable {p : polynomial F} (hp : p ≠ 0)
(hsep : separable p) (x : F) : root_multiplicity x p ≤ 1 :=
begin
rw [root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_le_coe, enat.coe_get],
exact multiplicity_le_one_of_separable (not_unit_X_sub_C _) hsep
end
lemma count_roots_le_one {p : polynomial F} (hsep : separable p) (x : F) :
p.roots.count x ≤ 1 :=
begin
by_cases hp : p = 0,
{ simp [hp] },
rw count_roots hp,
exact root_multiplicity_le_one_of_separable hp hsep x
end
lemma nodup_roots {p : polynomial F} (hsep : separable p) :
p.roots.nodup :=
multiset.nodup_iff_count_le_one.mpr (count_roots_le_one hsep)
lemma eq_X_sub_C_of_separable_of_root_eq {x : F} {h : polynomial F} (h_ne_zero : h ≠ 0)
(h_sep : h.separable) (h_root : h.eval x = 0) (h_splits : splits i h)
(h_roots : ∀ y ∈ (h.map i).roots, y = i x) : h = (C (leading_coeff h)) * (X - C x) :=
begin
apply polynomial.eq_X_sub_C_of_splits_of_single_root i h_splits,
apply finset.mk.inj,
{ change _ = {i x},
rw finset.eq_singleton_iff_unique_mem,
split,
{ apply finset.mem_mk.mpr,
rw mem_roots (show h.map i ≠ 0, by exact map_ne_zero h_ne_zero),
rw [is_root.def,←eval₂_eq_eval_map,eval₂_hom,h_root],
exact ring_hom.map_zero i },
{ exact h_roots } },
{ exact nodup_roots (separable.map h_sep) },
end
end splits
end field
end polynomial
open polynomial
theorem irreducible.separable {F : Type u} [field F] [char_zero F] {f : polynomial F}
(hf : irreducible f) : f.separable :=
begin
rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq], rintro ⟨⟩,
rw [pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff],
refine λ hf1, hf.not_unit _, rw [hf1, is_unit_C, is_unit_iff_ne_zero],
intro hf2, rw [hf2, C_0] at hf1, exact absurd hf1 hf.ne_zero
end
-- TODO: refactor to allow transcendental extensions?
-- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions
/-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff
the minimal polynomial of every `x : K` is separable. -/
class is_separable (F K : Sort*) [field F] [field K] [algebra F K] : Prop :=
(is_integral' (x : K) : is_integral F x)
(separable' (x : K) : (minpoly F x).separable)
theorem is_separable.is_integral {F K} [field F] [field K] [algebra F K] (h : is_separable F K) :
∀ x : K, is_integral F x := is_separable.is_integral'
theorem is_separable.separable {F K} [field F] [field K] [algebra F K] (h : is_separable F K) :
∀ x : K, (minpoly F x).separable := is_separable.separable'
theorem is_separable_iff {F K} [field F] [field K] [algebra F K] : is_separable F K ↔
∀ x : K, is_integral F x ∧ (minpoly F x).separable :=
⟨λ h x, ⟨h.is_integral x, h.separable x⟩, λ h, ⟨λ x, (h x).1, λ x, (h x).2⟩⟩
instance is_separable_self (F : Type*) [field F] : is_separable F F :=
⟨λ x, is_integral_algebra_map, λ x, by { rw minpoly.eq_X_sub_C', exact separable_X_sub_C }⟩
section is_separable_tower
variables (F K E : Type*) [field F] [field K] [field E] [algebra F K] [algebra F E]
[algebra K E] [is_scalar_tower F K E]
lemma is_separable_tower_top_of_is_separable [h : is_separable F E] : is_separable K E :=
⟨λ x, is_integral_of_is_scalar_tower x (h.is_integral x),
λ x, (h.separable x).map.of_dvd (minpoly.dvd_map_of_is_scalar_tower _ _ _)⟩
lemma is_separable_tower_bot_of_is_separable [h : is_separable F E] : is_separable F K :=
is_separable_iff.2 $ λ x, begin
refine (is_separable_iff.1 h (algebra_map K E x)).imp
is_integral_tower_bot_of_is_integral_field (λ hs, _),
obtain ⟨q, hq⟩ := minpoly.dvd F x
(is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero_field
(minpoly.aeval F ((algebra_map K E) x))),
rw hq at hs,
exact hs.of_mul_left
end
variables {E}
lemma is_separable.of_alg_hom (E' : Type*) [field E'] [algebra F E']
(f : E →ₐ[F] E') [is_separable F E'] : is_separable F E :=
begin
letI : algebra E E' := ring_hom.to_algebra f.to_ring_hom,
haveI : is_scalar_tower F E E' := is_scalar_tower.of_algebra_map_eq (λ x, (f.commutes x).symm),
exact is_separable_tower_bot_of_is_separable F E E',
end
end is_separable_tower
|
864dfa436ec5f5eb7bd75f01a8199b8c4c565a8b | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/pp_parens.lean | 331eb00ff3452e138720d12f69aef613c1fc4486 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 584 | lean | set_option pp.parens true
#check 1 + 2 * 3 + 4
#check @nat.add_assoc
#check [1,2,3]++[3,4,5]
#check ∀ (x y z : ℤ), x - (-3) = -2 + x
#check ∀ (p : ℕ → ℕ → Prop), p (1 + 2) 3
#check ∀ (f : ℕ → ℕ → ℕ), f 2 (f 3 4 + 1) + 2 = f 1 2
def Union {ι β : Type*} (s : ι → set β) : set β := {x : β | ∃ i, x ∈ s i}
notation `⋃` binders `, ` r:(scoped f, Union f) := r
#check (set.univ : set ℕ) = ⋃(n:ℕ), {m : ℕ | m < n}
-- subtype notation is handled specially by the pretty printer, so isn't parenthesized
#check {x : ℕ // x ∈ set.univ}
|
0b68f88583ed0a7b4fa344894c46d47c2b0adc2d | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/run/full.lean | dd6f1857d31080ff7d37683d2215e2699cc709b2 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 106 | lean | namespace foo
constant x : num
#check x
#check x
set_option pp.full_names true
#check x
end foo
|
6f21c433e894fb320c3306ff5b5f864b7fc58652 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/tactic/lint/basic.lean | 31ad2ea8db30813f9e9d2db68454cba40d7babe9 | [
"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 | 4,094 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner
-/
import tactic.core
/-!
# Basic linter types and attributes
This file defines the basic types and attributes used by the linting
framework. A linter essentially consists of a function
`declaration → tactic (option string)`, this function together with some
metadata is stored in the `linter` structure. We define two attributes:
* `@[linter]` applies to a declaration of type `linter` and adds it to the default linter set.
* `@[nolint linter_name]` omits the tagged declaration from being checked by
the linter with name `linter_name`.
-/
open tactic
setup_tactic_parser
section
local attribute [semireducible] reflected
/--
We store the list of nolint names as `@id (list name) (Prop simp_nf doc_blame has_coe_t)`
See Note [user attribute parameters]
-/
private meta def reflect_name_list : has_reflect (list name) | ns :=
`(id %%(expr.mk_app `(Prop) $ ns.map (flip expr.const [])) : list name)
private meta def parse_name_list (e : expr) : list name :=
e.app_arg.get_app_args.map expr.const_name
local attribute [instance] reflect_name_list
/-- Defines the user attribute `nolint` for skipping `#lint` -/
@[user_attribute]
meta def nolint_attr : user_attribute (name_map (list name)) (list name) :=
{ name := "nolint",
descr := "Do not report this declaration in any of the tests of `#lint`",
after_set := some $ λ n _ _, (do
ls@(_::_) ← parse_name_list <$> nolint_attr.get_param_untyped n
| fail "you need to specify at least one linter to disable",
skip),
cache_cfg := {
dependencies := [],
mk_cache := list.mfoldl
(λ cache d, native.rb_map.insert cache d <$>
parse_name_list <$> nolint_attr.get_param_untyped d)
mk_name_map },
parser := ident* }
end
add_tactic_doc
{ name := "nolint",
category := doc_category.attr,
decl_names := [`nolint_attr],
tags := ["linting"] }
/-- `should_be_linted linter decl` returns true if `decl` should be checked
using `linter`, i.e., if there is no `nolint` attribute. -/
meta def should_be_linted (linter : name) (decl : name) : tactic bool := do
c ← nolint_attr.get_cache,
pure $ linter ∉ (c.find decl).get_or_else []
/--
A linting test for the `#lint` command.
`test` defines a test to perform on every declaration. It should never fail. Returning `none`
signifies a passing test. Returning `some msg` reports a failing test with error `msg`.
`no_errors_found` is the message printed when all tests are negative, and `errors_found` is printed
when at least one test is positive.
If `is_fast` is false, this test will be omitted from `#lint-`.
If `auto_decls` is true, this test will also be executed on automatically generated declarations.
-/
meta structure linter :=
(test : declaration → tactic (option string))
(no_errors_found : string)
(errors_found : string)
(is_fast : bool := tt)
(auto_decls : bool)
/-- Takes a list of names that resolve to declarations of type `linter`,
and produces a list of linters. -/
meta def get_linters (l : list name) : tactic (list (name × linter)) :=
l.mmap (λ n, prod.mk n.last <$> (mk_const n >>= eval_expr linter)
<|> fail format!"invalid linter: {n}")
/-- Defines the user attribute `linter` for adding a linter to the default set.
Linters should be defined in the `linter` namespace.
A linter `linter.my_new_linter` is referred to as `my_new_linter` (without the `linter` namespace)
when used in `#lint`.
-/
@[user_attribute]
meta def linter_attr : user_attribute unit unit :=
{ name := "linter",
descr := "Use this declaration as a linting test in #lint",
after_set := some $ λ nm _ _,
mk_const nm >>= infer_type >>= unify `(linter) }
add_tactic_doc
{ name := "linter",
category := doc_category.attr,
decl_names := [`linter_attr],
tags := ["linting"] }
|
5cb036361dc92a214d43575fa317d3aca6c58011 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/replacer.lean | d436aeecb6f114cc441fffdef83bdea49698f8b2 | [
"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 | 939 | lean | import tactic.replacer
open tactic
def_replacer sneaky
example : true :=
begin
success_if_fail { sneaky },
trivial
end
@[sneaky] meta def sneaky' : tactic unit := `[skip]
example : true :=
begin
sneaky,
guard_target true,
trivial
end
@[sneaky] meta def sneaky'' := `[trivial]
example : true :=
begin
sneaky
end
@[sneaky] meta def sneaky''' (old : tactic unit) := old >> `[trivial]
example : true ∧ true :=
begin
split,
sneaky
end
def_replacer transform : ℕ → tactic ℕ
run_cmd success_if_fail (transform 1)
@[transform] meta def transform' (n : ℕ) : tactic ℕ :=
return (n+1)
run_cmd do n ← transform 2, guard (n = 3)
@[transform] meta def transform'' (n : ℕ) : tactic ℕ :=
return (n * n)
run_cmd do n ← transform 2, guard (n = 4)
@[transform] meta def transform''' (n : ℕ) (old : tactic ℕ) : tactic ℕ :=
do n' ← old, return (n' * n')
run_cmd do n ← transform 2, guard (n = 16)
|
91dbb55bd2eba2e99b01f688d8e3924d37a3d8d3 | 0845ae2ca02071debcfd4ac24be871236c01784f | /tests/compiler/reusebug.lean | bf4a0ddb7b7ecaca1c9b135a3fa9ed6f5752079f | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 721 | lean | inductive Expr
| Val : Int → Expr
| Var : String → Expr
| Add : Expr → Expr → Expr
| Mul : Expr → Expr → Expr
open Expr
def Expr.toString : Expr → String
| (Val n) := toString n
| (Var x) := x
| (Add f g) := "(" ++ Expr.toString f ++ " + " ++ Expr.toString g ++ ")"
| (Mul f g) := "(" ++ Expr.toString f ++ " * " ++ Expr.toString g ++ ")"
instance : HasToString Expr :=
⟨Expr.toString⟩
partial def addAux : Expr → Expr → Expr
| f (Add (Val n) g) := addAux (Val n) (addAux f g)
| f g := Add f g
def add (a b : Expr) : Expr :=
addAux a b
def main (xs : List String) : IO UInt32 :=
do let x := Var "x";
IO.println (add (Val 1) (Add (Mul (Val 2) x) x));
pure 0
|
71bffd01b22a6122c58cf69a8f8415b4d4a8b73b | 87fd6b43d22688237c02b87c30d2a524f53bab24 | /src/game/sets/sets_level04.lean | 9efb99dabb5323f773b8a59131204ec26b307eda | [
"Apache-2.0"
] | permissive | grthomson/real-number-game | 66142fedf0987db90f66daed52f9c8b42b70f909 | 8ddc15fdddc241c246653f7bb341df36e4e880a8 | refs/heads/master | 1,668,059,330,605 | 1,592,873,454,000 | 1,592,873,454,000 | 262,025,764 | 0 | 0 | null | 1,588,849,107,000 | 1,588,849,106,000 | null | UTF-8 | Lean | false | false | 1,910 | lean | import game.sets.sets_level03 -- hide
namespace xena -- hide
open_locale classical -- hide
variable X : Type --hide
/-
# Chapter 1 : Sets
## Level 4
-/
/- Axiom : eq_iff :
A = B ↔ ∀ x : X, x ∈ A ↔ x ∈ B
-/
/-
To prove that two sets are equal, one needs to use the axiom
of extensionality: two sets are equal if and only if they have
the same elements.
We have called this axiom `eq_iff`. In mathlib it is called `ext_iff`.
```
lemma eq_iff : A = B ↔ ∀ x : X, x ∈ A ↔ x ∈ B
```
-/
/- Hint : Hint
To prove the theorem below, remember that you can use `split` to
change the goal into two goals, corresponding to the left-right and
right-left implication, respectively. For the first goal, after
`intro H,` the equality of the two sets can be manipulated
using `rw eq_iff`.
-/
/-
## A word on coding style
After a `split` statement, one goal turns into two. A good programming style
would be to use `{}` brackets to work on each goal individually, like this:
```
begin
split,
{ insert,
proof of,
first goal
},
{ insert,
proof of,
second goal
}
end
```
This way you only ever have one goal to work on, and your code becomes
easier to read.
-/
/- Lemma
If $A$ and $B$ are sets of any type $X$, then
$$ A \subseteq B \iff A \cup B = B.$$
-/
theorem subset_iff_union_eq (A : set X) (B : set X) : A ⊆ B ↔ A ∪ B = B :=
begin
rw subset_iff,
split,
{ intro h,
rw eq_iff,
intro x,
rw mem_union_iff,
-- can't rewrite under a binder
specialize h x, -- or replace h := h x,
tauto! },
{ intro h,
intros x hA,
rw ←h,
rw mem_union_iff,
tauto!
}
end
-- begin hide
-- theorem subset_iff_union_eq' (A : set X) (B : set X) : A ⊆ B ↔ A ∪ B = B :=
-- begin
-- rw subset_iff,
-- rw eq_iff,
-- apply forall_congr,
-- intro x,
-- rw mem_union_iff,
-- tauto!,
-- end
-- end hide
end xena --hide
|
79ddbd2f802ff1f14462bf38dc90c15fd505918c | 80746c6dba6a866de5431094bf9f8f841b043d77 | /src/analysis/normed_space/bounded_linear_maps.lean | 22bb51a10b413adf60687a24913818caf7a864ef | [
"Apache-2.0"
] | permissive | leanprover-fork/mathlib-backup | 8b5c95c535b148fca858f7e8db75a76252e32987 | 0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0 | refs/heads/master | 1,585,156,056,139 | 1,548,864,430,000 | 1,548,864,438,000 | 143,964,213 | 0 | 0 | Apache-2.0 | 1,550,795,966,000 | 1,533,705,322,000 | Lean | UTF-8 | Lean | false | false | 5,894 | 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
Continuous linear functions -- functions between normed vector spaces which are bounded and linear.
-/
import algebra.field
import tactic.norm_num
import analysis.normed_space.basic
@[simp] lemma mul_inv_eq' {α} [discrete_field α] (a b : α) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
classical.by_cases (assume : a = 0, by simp [this]) $ assume ha,
classical.by_cases (assume : b = 0, by simp [this]) $ assume hb,
mul_inv_eq hb ha
noncomputable theory
local attribute [instance] classical.prop_decidable
local notation f ` →_{`:50 a `} `:0 b := filter.tendsto f (nhds a) (nhds b)
open filter (tendsto)
open metric
variables {k : Type*} [normed_field k]
variables {E : Type*} [normed_space k E]
variables {F : Type*} [normed_space k F]
variables {G : Type*} [normed_space k G]
structure is_bounded_linear_map {k : Type*}
[normed_field k] {E : Type*} [normed_space k E] {F : Type*} [normed_space k F] (L : E → F)
extends is_linear_map k L : Prop :=
(bound : ∃ M, M > 0 ∧ ∀ x : E, ∥ L x ∥ ≤ M * ∥ x ∥)
include k
lemma is_linear_map.with_bound
{L : E → F} (hf : is_linear_map k L) (M : ℝ) (h : ∀ x : E, ∥ L x ∥ ≤ M * ∥ x ∥) :
is_bounded_linear_map L :=
⟨ hf, classical.by_cases
(assume : M ≤ 0, ⟨1, zero_lt_one, assume x,
le_trans (h x) $ mul_le_mul_of_nonneg_right (le_trans this zero_le_one) (norm_nonneg x)⟩)
(assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩
namespace is_bounded_linear_map
lemma zero : is_bounded_linear_map (λ (x:E), (0:F)) :=
(0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl]
lemma id : is_bounded_linear_map (λ (x:E), x) :=
linear_map.id.is_linear.with_bound 1 $ by simp [le_refl]
lemma smul {f : E → F} (c : k) : is_bounded_linear_map f → is_bounded_linear_map (λ e, c • f e)
| ⟨hf, ⟨M, hM, h⟩⟩ := (c • hf.mk' f).is_linear.with_bound (∥c∥ * M) $ assume x,
calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x)
... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (h x) (norm_nonneg c)
... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm
lemma neg {f : E → F} (hf : is_bounded_linear_map f) : is_bounded_linear_map (λ e, -f e) :=
begin
rw show (λ e, -f e) = (λ e, (-1 : k) • f e), { funext, simp },
exact smul (-1) hf
end
lemma add {f : E → F} {g : E → F} :
is_bounded_linear_map f → is_bounded_linear_map g → is_bounded_linear_map (λ e, f e + g e)
| ⟨hlf, Mf, hMf, hf⟩ ⟨hlg, Mg, hMg, hg⟩ := (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ assume x,
calc ∥f x + g x∥ ≤ ∥f x∥ + ∥g x∥ : norm_triangle _ _
... ≤ Mf * ∥x∥ + Mg * ∥x∥ : add_le_add (hf x) (hg x)
... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul
lemma sub {f : E → F} {g : E → F} (hf : is_bounded_linear_map f) (hg : is_bounded_linear_map g) :
is_bounded_linear_map (λ e, f e - g e) := add hf (neg hg)
lemma comp {f : E → F} {g : F → G} :
is_bounded_linear_map g → is_bounded_linear_map f → is_bounded_linear_map (g ∘ f)
| ⟨hlg, Mg, hMg, hg⟩ ⟨hlf, Mf, hMf, hf⟩ := ((hlg.mk' _).comp (hlf.mk' _)).is_linear.with_bound (Mg * Mf) $ assume x,
calc ∥g (f x)∥ ≤ Mg * ∥f x∥ : hg _
... ≤ Mg * (Mf * ∥x∥) : mul_le_mul_of_nonneg_left (hf _) (le_of_lt hMg)
... = Mg * Mf * ∥x∥ : (mul_assoc _ _ _).symm
lemma tendsto {L : E → F} (x : E) : is_bounded_linear_map L → L →_{x} (L x)
| ⟨hL, M, hM, h_ineq⟩ := tendsto_iff_norm_tendsto_zero.2 $
squeeze_zero (assume e, norm_nonneg _)
(assume e, calc ∥L e - L x∥ = ∥hL.mk' L (e - x)∥ : by rw (hL.mk' _).map_sub e x; refl
... ≤ M*∥e-x∥ : h_ineq (e-x))
(suffices (λ (e : E), M * ∥e - x∥) →_{x} (M * 0), by simpa,
tendsto_mul tendsto_const_nhds (lim_norm _))
lemma continuous {L : E → F} (hL : is_bounded_linear_map L) : continuous L :=
continuous_iff_tendsto.2 $ assume x, hL.tendsto x
lemma lim_zero_bounded_linear_map {L : E → F} (H : is_bounded_linear_map L) : (L →_{0} 0) :=
(H.1.mk' _).map_zero ▸ continuous_iff_tendsto.1 H.continuous 0
end is_bounded_linear_map
-- Next lemma is stated for real normed space but it would work as soon as the base field is an extension of ℝ
lemma bounded_continuous_linear_map
{E : Type*} [normed_space ℝ E] {F : Type*} [normed_space ℝ F] {L : E → F}
(lin : is_linear_map ℝ L) (cont : continuous L) : is_bounded_linear_map L :=
let ⟨δ, δ_pos, hδ⟩ := exists_delta_of_continuous cont zero_lt_one 0 in
have HL0 : L 0 = 0, from (lin.mk' _).map_zero,
have H : ∀{a}, ∥a∥ ≤ δ → ∥L a∥ < 1, by simpa only [HL0, dist_zero_right] using hδ,
lin.with_bound (δ⁻¹) $ assume x,
classical.by_cases (assume : x = 0, by simp only [this, HL0, norm_zero, mul_zero]) $
assume h : x ≠ 0,
let p := ∥x∥ * δ⁻¹, q := p⁻¹ in
have p_inv : p⁻¹ = δ*∥x∥⁻¹, by simp,
have norm_x_pos : ∥x∥ > 0 := (norm_pos_iff x).2 h,
have norm_x : ∥x∥ ≠ 0 := mt (norm_eq_zero x).1 h,
have p_pos : p > 0 := mul_pos norm_x_pos (inv_pos δ_pos),
have p0 : _ := ne_of_gt p_pos,
have q_pos : q > 0 := inv_pos p_pos,
have q0 : _ := ne_of_gt q_pos,
have ∥p⁻¹ • x∥ = δ := calc
∥p⁻¹ • x∥ = abs p⁻¹ * ∥x∥ : by rw norm_smul; refl
... = p⁻¹ * ∥x∥ : by rw [abs_of_nonneg $ le_of_lt q_pos]
... = δ : by simp [mul_assoc, inv_mul_cancel norm_x],
calc ∥L x∥ = (p * q) * ∥L x∥ : begin dsimp [q], rw [mul_inv_cancel p0, one_mul] end
... = p * ∥L (q • x)∥ : by simp [lin.smul, norm_smul, real.norm_eq_abs, abs_of_pos q_pos, mul_assoc]
... ≤ p * 1 : mul_le_mul_of_nonneg_left (le_of_lt $ H $ le_of_eq $ this) (le_of_lt p_pos)
... = δ⁻¹ * ∥x∥ : by rw [mul_one, mul_comm]
|
14ba39a7c232463b322b77c2794e17aa95ec1fc1 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/lean/run/inline_fn.lean | 630429df0cdfba1507f30afe4b8b272e4fe3621d | [
"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 | 192 | lean | def f {α : Type} [HasAdd α] (x : α) :=
x + x + x
def h : Nat → Nat
| 0 => 10
| n+1 => n * h n
def g1 (x : Nat) :=
inline f x
def g2 (x : Nat) :=
inline h x
def g3 :=
inline h 2
|
0a265f99a013fe83d3d4e1f6bc4f6023938698a2 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Elab/PreDefinition/WF/PackDomain.lean | 4289ffb3d4cfd7010e5d96587cdd76b8b17cfca4 | [
"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 | 7,699 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.PreDefinition.Basic
namespace Lean.Elab.WF
open Meta
/--
Given a (dependent) tuple `t` (using `PSigma`) of the given arity.
Return an array containing its "elements".
Example: `mkTupleElems a 4` returns `#[a.1, a.2.1, a.2.2.1, a.2.2.2]`.
-/
private def mkTupleElems (t : Expr) (arity : Nat) : Array Expr := Id.run do
let mut result := #[]
let mut t := t
for _ in [:arity - 1] do
result := result.push (mkProj ``PSigma 0 t)
t := mkProj ``PSigma 1 t
result.push t
/-- Create a unary application by packing the given arguments using `PSigma.mk` -/
partial def mkUnaryArg (type : Expr) (args : Array Expr) : MetaM Expr := do
go 0 type
where
go (i : Nat) (type : Expr) : MetaM Expr := do
if i < args.size - 1 then
let arg := args[i]!
assert! type.isAppOfArity ``PSigma 2
let us := type.getAppFn.constLevels!
let α := type.appFn!.appArg!
let β := type.appArg!
assert! β.isLambda
let type := β.bindingBody!.instantiate1 arg
let rest ← go (i+1) type
return mkApp4 (mkConst ``PSigma.mk us) α β arg rest
else
return args[i]!
private partial def mkPSigmaCasesOn (y : Expr) (codomain : Expr) (xs : Array Expr) (value : Expr) : MetaM Expr := do
let mvar ← mkFreshExprSyntheticOpaqueMVar codomain
let rec go (mvarId : MVarId) (y : FVarId) (ys : Array Expr) : MetaM Unit := do
if ys.size < xs.size - 1 then
let xDecl ← xs[ys.size]!.fvarId!.getDecl
let xDecl' ← xs[ys.size + 1]!.fvarId!.getDecl
let #[s] ← mvarId.cases y #[{ varNames := [xDecl.userName, xDecl'.userName] }] | unreachable!
go s.mvarId s.fields[1]!.fvarId! (ys.push s.fields[0]!)
else
let ys := ys.push (mkFVar y)
mvarId.assign (value.replaceFVars xs ys)
go mvar.mvarId! y.fvarId! #[]
instantiateMVars mvar
/--
Convert the given pre-definitions into unary functions.
We "pack" the arguments using `PSigma`.
-/
partial def packDomain (fixedPrefix : Nat) (preDefs : Array PreDefinition) : MetaM (Array PreDefinition) := do
let mut preDefsNew := #[]
let mut arities := #[]
let mut modified := false
for preDef in preDefs do
let (preDefNew, arity, modifiedCurr) ← lambdaTelescope preDef.value fun xs _ => do
if xs.size == fixedPrefix then
throwError "well-founded recursion cannot be used, '{preDef.declName}' does not take any (non-fixed) arguments"
let arity := xs.size
if arity > fixedPrefix + 1 then
let bodyType ← instantiateForall preDef.type xs
let mut d ← inferType xs.back
let ys : Array Expr := xs[:fixedPrefix]
let xs : Array Expr := xs[fixedPrefix:]
for x in xs.pop.reverse do
d ← mkAppOptM ``PSigma #[some (← inferType x), some (← mkLambdaFVars #[x] d)]
withLocalDeclD (← mkFreshUserName `_x) d fun tuple => do
let elems := mkTupleElems tuple xs.size
let codomain := bodyType.replaceFVars xs elems
let preDefNew:= { preDef with
declName := preDef.declName ++ `_unary
type := (← mkForallFVars (ys.push tuple) codomain)
}
addAsAxiom preDefNew
return (preDefNew, arity, true)
else
return (preDef, arity, false)
modified := modified || modifiedCurr
arities := arities.push arity
preDefsNew := preDefsNew.push preDefNew
if !modified then
return preDefs
-- Update values
for i in [:preDefs.size] do
let preDef := preDefs[i]!
let preDefNew := preDefsNew[i]!
let valueNew ← lambdaTelescope preDef.value fun xs body => do
let ys : Array Expr := xs[:fixedPrefix]
let xs : Array Expr := xs[fixedPrefix:]
let type ← instantiateForall preDefNew.type ys
forallBoundedTelescope type (some 1) fun z codomain => do
let z := z[0]!
let newBody ← mkPSigmaCasesOn z codomain xs body
mkLambdaFVars (ys.push z) (← packApplications newBody arities preDefsNew)
let isBad (e : Expr) : Bool :=
match isAppOfPreDef? e with
| none => false
| some i => e.getAppNumArgs > fixedPrefix + 1 || preDefsNew[i]!.declName != preDefs[i]!.declName
if let some bad := valueNew.find? isBad then
if let some i := isAppOfPreDef? bad then
throwErrorAt preDef.ref "well-founded recursion cannot be used, function '{preDef.declName}' contains application of function '{preDefs[i]!.declName}' with #{bad.getAppNumArgs} argument(s), but function has arity {arities[i]!}"
preDefsNew := preDefsNew.set! i { preDefNew with value := valueNew }
return preDefsNew
where
/-- Return `some i` if `e` is a `preDefs[i]` application -/
isAppOfPreDef? (e : Expr) : Option Nat := do
let f := e.getAppFn
guard f.isConst
preDefs.findIdx? (·.declName == f.constName!)
packApplications (e : Expr) (arities : Array Nat) (preDefsNew : Array PreDefinition) : MetaM Expr := do
let pack (e : Expr) (funIdx : Nat) : MetaM Expr := do
let f := e.getAppFn
let args := e.getAppArgs
let fNew := mkConst preDefsNew[funIdx]!.declName f.constLevels!
let fNew := mkAppN fNew args[:fixedPrefix]
let Expr.forallE _ d .. ← inferType fNew | unreachable!
let argNew ← mkUnaryArg d args[fixedPrefix:]
return mkApp fNew argNew
let rec
visit (e : Expr) : MonadCacheT ExprStructEq Expr MetaM Expr := do
checkCache { val := e : ExprStructEq } fun _ => Meta.withIncRecDepth do
match e with
| Expr.lam n d b c =>
withLocalDecl n c (← visit d) fun x => do
mkLambdaFVars (usedLetOnly := false) #[x] (← visit (b.instantiate1 x))
| Expr.forallE n d b c =>
withLocalDecl n c (← visit d) fun x => do
mkForallFVars (usedLetOnly := false) #[x] (← visit (b.instantiate1 x))
| Expr.letE n t v b _ =>
withLetDecl n (← visit t) (← visit v) fun x => do
mkLambdaFVars (usedLetOnly := false) #[x] (← visit (b.instantiate1 x))
| Expr.proj n i s .. => return mkProj n i (← visit s)
| Expr.mdata d b => return mkMData d (← visit b)
| Expr.app .. => visitApp e
| Expr.const .. => visitApp e
| e => return e,
visitApp (e : Expr) : MonadCacheT ExprStructEq Expr MetaM Expr := e.withApp fun f args => do
let args ← args.mapM visit
if let some funIdx := isAppOfPreDef? f then
let numArgs := args.size
let arity := arities[funIdx]!
if numArgs < arity then
-- Partial application
let extra := arity - numArgs
withDefault do forallBoundedTelescope (← inferType e) extra fun xs _ => do
if xs.size != extra then
return (mkAppN f args) -- It will fail later
else
mkLambdaFVars xs (← pack (mkAppN (mkAppN f args) xs) funIdx)
else if numArgs > arity then
-- Over application
let r ← pack (mkAppN f args[:arity]) funIdx
let rType ← inferType r
-- Make sure the new auxiliary definition has only one argument.
withLetDecl (← mkFreshUserName `aux) rType r fun aux =>
mkLetFVars #[aux] (mkAppN aux args[arity:])
else
pack (mkAppN f args) funIdx
else if args.isEmpty then
return f
else
return mkAppN (← visit f) args
visit e |>.run
end Lean.Elab.WF
|
ff21e264694f2bb154b8e902d57eb0c01c176518 | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2019/lectures/injsurj.lean | 6c656c7d707b1379eaebd5fb73273c65878848e4 | [] | 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 | 499 | lean | open function
example
(X Y Z : Type)
(f : X → Y)
(g : Y → Z)
(f_inj : injective f)
(g_inj : injective g) :
injective (g ∘ f) :=
begin
unfold injective at *,
intros p q,
intro h,
apply f_inj,
apply g_inj,
exact h,
end
example
(X Y Z : Type)
(f : X → Y)
(g : Y → Z)
(f_surj : surjective f)
(g_surj : surjective g) :
surjective (g ∘ f) :=
begin
intro z,
cases (g_surj z) with y hy,
cases (f_surj y) with x hx,
existsi x,
rw ←hy,
rw ←hx,
end |
cf8bd1f6ab0f9ec356df2945649cb0072b2257ff | c777c32c8e484e195053731103c5e52af26a25d1 | /src/topology/fiber_bundle/trivialization.lean | 84a15ebb82f14ab3cdea625770377077d5465e79 | [
"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 | 31,417 | 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.bundle
import topology.algebra.order.field
import topology.local_homeomorph
/-!
# Trivializations
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main definitions
### Basic definitions
* `trivialization F p` : structure extending local homeomorphisms, defining a local
trivialization of a topological space `Z` with projection `p` and fiber `F`.
* `pretrivialization F proj` : trivialization as a local equivalence, mainly used when the
topology on the total space has not yet been defined.
### Operations on bundles
We provide the following operations on `trivialization`s.
* `trivialization.comp_homeomorph`: given a local trivialization `e` of a fiber bundle
`p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle
`p ∘ h`.
## Implementation notes
Previously, in mathlib, there was a structure `topological_vector_bundle.trivialization` which
extended another structure `topological_fiber_bundle.trivialization` by a linearity hypothesis. As
of PR #17359, we have changed this to a single structure `trivialization` (no namespace), together
with a mixin class `trivialization.is_linear`.
This permits all the *data* of a vector bundle to be held at the level of fiber bundles, so that the
same trivializations can underlie an object's structure as (say) a vector bundle over `ℂ` and as a
vector bundle over `ℝ`, as well as its structure simply as a fiber bundle.
This might be a little surprising, given the general trend of the library to ever-increased
bundling. But in this case the typical motivation for more bundling does not apply: there is no
algebraic or order structure on the whole type of linear (say) trivializations of a bundle.
Indeed, since trivializations only have meaning on their base sets (taking junk values outside), the
type of linear trivializations is not even particularly well-behaved.
-/
open topological_space filter set bundle
open_locale topology classical bundle
variables {ι : Type*} {B : Type*} {F : Type*} {E : B → Type*}
variables (F) {Z : Type*} [topological_space B] [topological_space F] {proj : Z → B}
/-- This structure contains the information left for a local trivialization (which is implemented
below as `trivialization F proj`) if the total space has not been given a topology, but we
have a topology on both the fiber and the base space. Through the construction
`topological_fiber_prebundle F proj` it will be possible to promote a
`pretrivialization F proj` to a `trivialization F proj`. -/
@[ext, nolint has_nonempty_instance]
structure pretrivialization (proj : Z → B) extends local_equiv Z (B × F) :=
(open_target : is_open target)
(base_set : set B)
(open_base_set : is_open base_set)
(source_eq : source = proj ⁻¹' base_set)
(target_eq : target = base_set ×ˢ univ)
(proj_to_fun : ∀ p ∈ source, (to_fun p).1 = proj p)
namespace pretrivialization
instance : has_coe_to_fun (pretrivialization F proj) (λ _, Z → (B × F)) := ⟨λ e, e.to_fun⟩
variables {F} (e : pretrivialization F proj) {x : Z}
@[simp, mfld_simps] lemma coe_coe : ⇑e.to_local_equiv = e := rfl
@[simp, mfld_simps] lemma coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex
lemma mem_source : x ∈ e.source ↔ proj x ∈ e.base_set := by rw [e.source_eq, mem_preimage]
lemma coe_fst' (ex : proj x ∈ e.base_set) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex)
protected lemma eq_on : eq_on (prod.fst ∘ e) proj e.source := λ x hx, e.coe_fst hx
lemma mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst ex).symm rfl
lemma mk_proj_snd' (ex : proj x ∈ e.base_set) : (proj x, (e x).2) = e x :=
prod.ext (e.coe_fst' ex).symm rfl
/-- Composition of inverse and coercion from the subtype of the target. -/
def set_symm : e.target → Z := e.target.restrict e.to_local_equiv.symm
lemma mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set :=
by rw [e.target_eq, prod_univ, mem_preimage]
lemma proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.to_local_equiv.symm x) = x.1 :=
begin
have := (e.coe_fst (e.to_local_equiv.map_target hx)).symm,
rwa [← e.coe_coe, e.to_local_equiv.right_inv hx] at this
end
lemma proj_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) :
proj (e.to_local_equiv.symm (b, x)) = b :=
e.proj_symm_apply (e.mem_target.2 hx)
lemma proj_surj_on_base_set [nonempty F] : set.surj_on proj e.source e.base_set :=
λ b hb, let ⟨y⟩ := ‹nonempty F› in ⟨e.to_local_equiv.symm (b, y),
e.to_local_equiv.map_target $ e.mem_target.2 hb, e.proj_symm_apply' hb⟩
lemma apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.to_local_equiv.symm x) = x :=
e.to_local_equiv.right_inv hx
lemma apply_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) :
e (e.to_local_equiv.symm (b, x)) = (b, x) :=
e.apply_symm_apply (e.mem_target.2 hx)
lemma symm_apply_apply {x : Z} (hx : x ∈ e.source) : e.to_local_equiv.symm (e x) = x :=
e.to_local_equiv.left_inv hx
@[simp, mfld_simps] lemma symm_apply_mk_proj {x : Z} (ex : x ∈ e.source) :
e.to_local_equiv.symm (proj x, (e x).2) = x :=
by rw [← e.coe_fst ex, prod.mk.eta, ← e.coe_coe, e.to_local_equiv.left_inv ex]
@[simp, mfld_simps] lemma preimage_symm_proj_base_set :
(e.to_local_equiv.symm ⁻¹' (proj ⁻¹' e.base_set)) ∩ e.target = e.target :=
begin
refine inter_eq_right_iff_subset.mpr (λ x hx, _),
simp only [mem_preimage, local_equiv.inv_fun_as_coe, e.proj_symm_apply hx],
exact e.mem_target.mp hx,
end
@[simp, mfld_simps] lemma preimage_symm_proj_inter (s : set B) :
(e.to_local_equiv.symm ⁻¹' (proj ⁻¹' s)) ∩ e.base_set ×ˢ univ = (s ∩ e.base_set) ×ˢ univ :=
begin
ext ⟨x, y⟩,
suffices : x ∈ e.base_set → (proj (e.to_local_equiv.symm (x, y)) ∈ s ↔ x ∈ s),
by simpa only [prod_mk_mem_set_prod_eq, mem_inter_iff, and_true, mem_univ, and.congr_left_iff],
intro h,
rw [e.proj_symm_apply' h]
end
lemma target_inter_preimage_symm_source_eq (e f : pretrivialization F proj) :
f.target ∩ (f.to_local_equiv.symm) ⁻¹' e.source = (e.base_set ∩ f.base_set) ×ˢ univ :=
by rw [inter_comm, f.target_eq, e.source_eq, f.preimage_symm_proj_inter]
lemma trans_source (e f : pretrivialization F proj) :
(f.to_local_equiv.symm.trans e.to_local_equiv).source = (e.base_set ∩ f.base_set) ×ˢ univ :=
by rw [local_equiv.trans_source, local_equiv.symm_source, e.target_inter_preimage_symm_source_eq]
lemma symm_trans_symm (e e' : pretrivialization F proj) :
(e.to_local_equiv.symm.trans e'.to_local_equiv).symm =
e'.to_local_equiv.symm.trans e.to_local_equiv :=
by rw [local_equiv.trans_symm_eq_symm_trans_symm, local_equiv.symm_symm]
lemma symm_trans_source_eq (e e' : pretrivialization F proj) :
(e.to_local_equiv.symm.trans e'.to_local_equiv).source = (e.base_set ∩ e'.base_set) ×ˢ univ :=
by rw [local_equiv.trans_source, e'.source_eq, local_equiv.symm_source, e.target_eq, inter_comm,
e.preimage_symm_proj_inter, inter_comm]
lemma symm_trans_target_eq (e e' : pretrivialization F proj) :
(e.to_local_equiv.symm.trans e'.to_local_equiv).target = (e.base_set ∩ e'.base_set) ×ˢ univ :=
by rw [← local_equiv.symm_source, symm_trans_symm, symm_trans_source_eq, inter_comm]
variables {B F} (e' : pretrivialization F (π E)) {x' : total_space E} {b : B} {y : E b}
lemma coe_mem_source : ↑y ∈ e'.source ↔ b ∈ e'.base_set := e'.mem_source
@[simp, mfld_simps] lemma coe_coe_fst (hb : b ∈ e'.base_set) : (e' y).1 = b :=
e'.coe_fst (e'.mem_source.2 hb)
lemma mk_mem_target {x : B} {y : F} : (x, y) ∈ e'.target ↔ x ∈ e'.base_set :=
e'.mem_target
lemma symm_coe_proj {x : B} {y : F} (e' : pretrivialization F (π E)) (h : x ∈ e'.base_set) :
(e'.to_local_equiv.symm (x, y)).1 = x :=
e'.proj_symm_apply' h
section has_zero
variables [∀ x, has_zero (E x)]
/-- A fiberwise inverse to `e`. This is the function `F → E b` that induces a local inverse
`B × F → total_space E` of `e` on `e.base_set`. It is defined to be `0` outside `e.base_set`. -/
protected noncomputable def symm (e : pretrivialization F (π E)) (b : B) (y : F) : E b :=
if hb : b ∈ e.base_set
then cast (congr_arg E (e.proj_symm_apply' hb)) (e.to_local_equiv.symm (b, y)).2
else 0
lemma symm_apply (e : pretrivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
e.symm b y = cast (congr_arg E (e.symm_coe_proj hb)) (e.to_local_equiv.symm (b, y)).2 :=
dif_pos hb
lemma symm_apply_of_not_mem (e : pretrivialization F (π E)) {b : B} (hb : b ∉ e.base_set) (y : F) :
e.symm b y = 0 :=
dif_neg hb
lemma coe_symm_of_not_mem (e : pretrivialization F (π E)) {b : B} (hb : b ∉ e.base_set) :
(e.symm b : F → E b) = 0 :=
funext $ λ y, dif_neg hb
lemma mk_symm (e : pretrivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
total_space_mk b (e.symm b y) = e.to_local_equiv.symm (b, y) :=
by rw [e.symm_apply hb, total_space.mk_cast, total_space.eta]
lemma symm_proj_apply (e : pretrivialization F (π E)) (z : total_space E)
(hz : z.proj ∈ e.base_set) : e.symm z.proj (e z).2 = z.2 :=
by rw [e.symm_apply hz, cast_eq_iff_heq, e.mk_proj_snd' hz,
e.symm_apply_apply (e.mem_source.mpr hz)]
lemma symm_apply_apply_mk (e : pretrivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : E b) :
e.symm b (e (total_space_mk b y)).2 = y :=
e.symm_proj_apply (total_space_mk b y) hb
lemma apply_mk_symm (e : pretrivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
e (total_space_mk b (e.symm b y)) = (b, y) :=
by rw [e.mk_symm hb, e.apply_symm_apply (e.mk_mem_target.mpr hb)]
end has_zero
end pretrivialization
variables [topological_space Z] [topological_space (total_space E)]
/--
A structure extending local homeomorphisms, defining a local trivialization of a projection
`proj : Z → B` with fiber `F`, as a local homeomorphism between `Z` and `B × F` defined between two
sets of the form `proj ⁻¹' base_set` and `base_set × F`, acting trivially on the first coordinate.
-/
@[ext, nolint has_nonempty_instance]
structure trivialization (proj : Z → B)
extends local_homeomorph Z (B × F) :=
(base_set : set B)
(open_base_set : is_open base_set)
(source_eq : source = proj ⁻¹' base_set)
(target_eq : target = base_set ×ˢ univ)
(proj_to_fun : ∀ p ∈ source, (to_local_homeomorph p).1 = proj p)
namespace trivialization
variables {F} (e : trivialization F proj) {x : Z}
/-- Natural identification as a `pretrivialization`. -/
def to_pretrivialization : pretrivialization F proj := { ..e }
instance : has_coe_to_fun (trivialization F proj) (λ _, Z → B × F) := ⟨λ e, e.to_fun⟩
instance : has_coe (trivialization F proj) (pretrivialization F proj) :=
⟨to_pretrivialization⟩
lemma to_pretrivialization_injective :
function.injective (λ e : trivialization F proj, e.to_pretrivialization) :=
by { intros e e', rw [pretrivialization.ext_iff, trivialization.ext_iff,
← local_homeomorph.to_local_equiv_injective.eq_iff], exact id }
@[simp, mfld_simps] lemma coe_coe : ⇑e.to_local_homeomorph = e := rfl
@[simp, mfld_simps] lemma coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex
protected lemma eq_on : eq_on (prod.fst ∘ e) proj e.source := λ x hx, e.coe_fst hx
lemma mem_source : x ∈ e.source ↔ proj x ∈ e.base_set := by rw [e.source_eq, mem_preimage]
lemma coe_fst' (ex : proj x ∈ e.base_set) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex)
lemma mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst ex).symm rfl
lemma mk_proj_snd' (ex : proj x ∈ e.base_set) : (proj x, (e x).2) = e x :=
prod.ext (e.coe_fst' ex).symm rfl
lemma source_inter_preimage_target_inter (s : set (B × F)) :
e.source ∩ (e ⁻¹' (e.target ∩ s)) = e.source ∩ (e ⁻¹' s) :=
e.to_local_homeomorph.source_inter_preimage_target_inter s
@[simp, mfld_simps] lemma coe_mk (e : local_homeomorph Z (B × F)) (i j k l m) (x : Z) :
(trivialization.mk e i j k l m : trivialization F proj) x = e x := rfl
lemma mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set :=
e.to_pretrivialization.mem_target
lemma map_target {x : B × F} (hx : x ∈ e.target) : e.to_local_homeomorph.symm x ∈ e.source :=
e.to_local_homeomorph.map_target hx
lemma proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.to_local_homeomorph.symm x) = x.1 :=
e.to_pretrivialization.proj_symm_apply hx
lemma proj_symm_apply' {b : B} {x : F}
(hx : b ∈ e.base_set) : proj (e.to_local_homeomorph.symm (b, x)) = b :=
e.to_pretrivialization.proj_symm_apply' hx
lemma proj_surj_on_base_set [nonempty F] : set.surj_on proj e.source e.base_set :=
e.to_pretrivialization.proj_surj_on_base_set
lemma apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.to_local_homeomorph.symm x) = x :=
e.to_local_homeomorph.right_inv hx
lemma apply_symm_apply'
{b : B} {x : F} (hx : b ∈ e.base_set) : e (e.to_local_homeomorph.symm (b, x)) = (b, x) :=
e.to_pretrivialization.apply_symm_apply' hx
@[simp, mfld_simps] lemma symm_apply_mk_proj (ex : x ∈ e.source) :
e.to_local_homeomorph.symm (proj x, (e x).2) = x :=
e.to_pretrivialization.symm_apply_mk_proj ex
lemma symm_trans_source_eq (e e' : trivialization F proj) :
(e.to_local_equiv.symm.trans e'.to_local_equiv).source = (e.base_set ∩ e'.base_set) ×ˢ univ :=
pretrivialization.symm_trans_source_eq e.to_pretrivialization e'
lemma symm_trans_target_eq (e e' : trivialization F proj) :
(e.to_local_equiv.symm.trans e'.to_local_equiv).target = (e.base_set ∩ e'.base_set) ×ˢ univ :=
pretrivialization.symm_trans_target_eq e.to_pretrivialization e'
lemma coe_fst_eventually_eq_proj (ex : x ∈ e.source) : prod.fst ∘ e =ᶠ[𝓝 x] proj :=
mem_nhds_iff.2 ⟨e.source, λ y hy, e.coe_fst hy, e.open_source, ex⟩
lemma coe_fst_eventually_eq_proj' (ex : proj x ∈ e.base_set) : prod.fst ∘ e =ᶠ[𝓝 x] proj :=
e.coe_fst_eventually_eq_proj (e.mem_source.2 ex)
lemma map_proj_nhds (ex : x ∈ e.source) : map proj (𝓝 x) = 𝓝 (proj x) :=
by rw [← e.coe_fst ex, ← map_congr (e.coe_fst_eventually_eq_proj ex), ← map_map, ← e.coe_coe,
e.to_local_homeomorph.map_nhds_eq ex, map_fst_nhds]
lemma preimage_subset_source {s : set B} (hb : s ⊆ e.base_set) : proj ⁻¹' s ⊆ e.source :=
λ p hp, e.mem_source.mpr (hb hp)
lemma image_preimage_eq_prod_univ {s : set B} (hb : s ⊆ e.base_set) :
e '' (proj ⁻¹' s) = s ×ˢ univ :=
subset.antisymm (image_subset_iff.mpr (λ p hp,
⟨(e.proj_to_fun p (e.preimage_subset_source hb hp)).symm ▸ hp, trivial⟩)) (λ p hp,
let hp' : p ∈ e.target := e.mem_target.mpr (hb hp.1) in
⟨e.inv_fun p, mem_preimage.mpr ((e.proj_symm_apply hp').symm ▸ hp.1), e.apply_symm_apply hp'⟩)
/-- The preimage of a subset of the base set is homeomorphic to the product with the fiber. -/
def preimage_homeomorph {s : set B} (hb : s ⊆ e.base_set) : proj ⁻¹' s ≃ₜ s × F :=
(e.to_local_homeomorph.homeomorph_of_image_subset_source (e.preimage_subset_source hb)
(e.image_preimage_eq_prod_univ hb)).trans
((homeomorph.set.prod s univ).trans ((homeomorph.refl s).prod_congr (homeomorph.set.univ F)))
@[simp] lemma preimage_homeomorph_apply {s : set B} (hb : s ⊆ e.base_set) (p : proj ⁻¹' s) :
e.preimage_homeomorph hb p = (⟨proj p, p.2⟩, (e p).2) :=
prod.ext (subtype.ext (e.proj_to_fun p (e.mem_source.mpr (hb p.2)))) rfl
@[simp] lemma preimage_homeomorph_symm_apply {s : set B} (hb : s ⊆ e.base_set) (p : s × F) :
(e.preimage_homeomorph hb).symm p = ⟨e.symm (p.1, p.2), ((e.preimage_homeomorph hb).symm p).2⟩ :=
rfl
/-- The source is homeomorphic to the product of the base set with the fiber. -/
def source_homeomorph_base_set_prod : e.source ≃ₜ e.base_set × F :=
(homeomorph.set_congr e.source_eq).trans (e.preimage_homeomorph subset_rfl)
@[simp] lemma source_homeomorph_base_set_prod_apply (p : e.source) :
e.source_homeomorph_base_set_prod p = (⟨proj p, e.mem_source.mp p.2⟩, (e p).2) :=
e.preimage_homeomorph_apply subset_rfl ⟨p, e.mem_source.mp p.2⟩
@[simp] lemma source_homeomorph_base_set_prod_symm_apply (p : e.base_set × F) :
e.source_homeomorph_base_set_prod.symm p =
⟨e.symm (p.1, p.2), (e.source_homeomorph_base_set_prod.symm p).2⟩ :=
rfl
/-- Each fiber of a trivialization is homeomorphic to the specified fiber. -/
def preimage_singleton_homeomorph {b : B} (hb : b ∈ e.base_set) : proj ⁻¹' {b} ≃ₜ F :=
(e.preimage_homeomorph (set.singleton_subset_iff.mpr hb)).trans (((homeomorph.homeomorph_of_unique
({b} : set B) punit).prod_congr (homeomorph.refl F)).trans (homeomorph.punit_prod F))
@[simp] lemma preimage_singleton_homeomorph_apply {b : B} (hb : b ∈ e.base_set)
(p : proj ⁻¹' {b}) : e.preimage_singleton_homeomorph hb p = (e p).2 :=
rfl
@[simp] lemma preimage_singleton_homeomorph_symm_apply {b : B} (hb : b ∈ e.base_set) (p : F) :
(e.preimage_singleton_homeomorph hb).symm p =
⟨e.symm (b, p), by rw [mem_preimage, e.proj_symm_apply' hb, mem_singleton_iff]⟩ :=
rfl
/-- In the domain of a bundle trivialization, the projection is continuous-/
lemma continuous_at_proj (ex : x ∈ e.source) : continuous_at proj x :=
(e.map_proj_nhds ex).le
/-- Composition of a `trivialization` and a `homeomorph`. -/
protected def comp_homeomorph {Z' : Type*} [topological_space Z'] (h : Z' ≃ₜ Z) :
trivialization F (proj ∘ h) :=
{ to_local_homeomorph := h.to_local_homeomorph.trans e.to_local_homeomorph,
base_set := e.base_set,
open_base_set := e.open_base_set,
source_eq := by simp [e.source_eq, preimage_preimage],
target_eq := by simp [e.target_eq],
proj_to_fun := λ p hp,
have hp : h p ∈ e.source, by simpa using hp,
by simp [hp] }
/-- Read off the continuity of a function `f : Z → X` at `z : Z` by transferring via a
trivialization of `Z` containing `z`. -/
lemma continuous_at_of_comp_right {X : Type*} [topological_space X] {f : Z → X} {z : Z}
(e : trivialization F proj) (he : proj z ∈ e.base_set)
(hf : continuous_at (f ∘ e.to_local_equiv.symm) (e z)) :
continuous_at f z :=
begin
have hez : z ∈ e.to_local_equiv.symm.target,
{ rw [local_equiv.symm_target, e.mem_source],
exact he },
rwa [e.to_local_homeomorph.symm.continuous_at_iff_continuous_at_comp_right hez,
local_homeomorph.symm_symm]
end
/-- Read off the continuity of a function `f : X → Z` at `x : X` by transferring via a
trivialization of `Z` containing `f x`. -/
lemma continuous_at_of_comp_left {X : Type*} [topological_space X] {f : X → Z} {x : X}
(e : trivialization F proj) (hf_proj : continuous_at (proj ∘ f) x) (he : proj (f x) ∈ e.base_set)
(hf : continuous_at (e ∘ f) x) :
continuous_at f x :=
begin
rw e.to_local_homeomorph.continuous_at_iff_continuous_at_comp_left,
{ exact hf },
rw [e.source_eq, ← preimage_comp],
exact hf_proj.preimage_mem_nhds (e.open_base_set.mem_nhds he),
end
variables {E} (e' : trivialization F (π E)) {x' : total_space E} {b : B} {y : E b}
protected lemma continuous_on : continuous_on e' e'.source := e'.continuous_to_fun
lemma coe_mem_source : ↑y ∈ e'.source ↔ b ∈ e'.base_set := e'.mem_source
lemma open_target : is_open e'.target :=
by { rw e'.target_eq, exact e'.open_base_set.prod is_open_univ }
@[simp, mfld_simps] lemma coe_coe_fst (hb : b ∈ e'.base_set) : (e' y).1 = b :=
e'.coe_fst (e'.mem_source.2 hb)
lemma mk_mem_target {y : F} : (b, y) ∈ e'.target ↔ b ∈ e'.base_set :=
e'.to_pretrivialization.mem_target
lemma symm_apply_apply {x : total_space E} (hx : x ∈ e'.source) :
e'.to_local_homeomorph.symm (e' x) = x :=
e'.to_local_equiv.left_inv hx
@[simp, mfld_simps] lemma symm_coe_proj {x : B} {y : F}
(e : trivialization F (π E)) (h : x ∈ e.base_set) :
(e.to_local_homeomorph.symm (x, y)).1 = x := e.proj_symm_apply' h
section has_zero
variables [∀ x, has_zero (E x)]
/-- A fiberwise inverse to `e'`. The function `F → E x` that induces a local inverse
`B × F → total_space E` of `e'` on `e'.base_set`. It is defined to be `0` outside `e'.base_set`. -/
protected noncomputable def symm (e : trivialization F (π E)) (b : B) (y : F) : E b :=
e.to_pretrivialization.symm b y
lemma symm_apply (e : trivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
e.symm b y = cast (congr_arg E (e.symm_coe_proj hb)) (e.to_local_homeomorph.symm (b, y)).2 :=
dif_pos hb
lemma symm_apply_of_not_mem (e : trivialization F (π E)) {b : B} (hb : b ∉ e.base_set) (y : F) :
e.symm b y = 0 :=
dif_neg hb
lemma mk_symm (e : trivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
total_space_mk b (e.symm b y) = e.to_local_homeomorph.symm (b, y) :=
e.to_pretrivialization.mk_symm hb y
lemma symm_proj_apply (e : trivialization F (π E)) (z : total_space E)
(hz : z.proj ∈ e.base_set) : e.symm z.proj (e z).2 = z.2 :=
e.to_pretrivialization.symm_proj_apply z hz
lemma symm_apply_apply_mk (e : trivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : E b) :
e.symm b (e (total_space_mk b y)).2 = y :=
e.symm_proj_apply (total_space_mk b y) hb
lemma apply_mk_symm (e : trivialization F (π E)) {b : B} (hb : b ∈ e.base_set) (y : F) :
e (total_space_mk b (e.symm b y)) = (b, y) :=
e.to_pretrivialization.apply_mk_symm hb y
lemma continuous_on_symm (e : trivialization F (π E)) :
continuous_on (λ z : B × F, total_space_mk z.1 (e.symm z.1 z.2)) (e.base_set ×ˢ univ) :=
begin
have : ∀ (z : B × F) (hz : z ∈ e.base_set ×ˢ (univ : set F)),
total_space_mk z.1 (e.symm z.1 z.2) = e.to_local_homeomorph.symm z,
{ rintro x ⟨hx : x.1 ∈ e.base_set, _⟩, simp_rw [e.mk_symm hx, prod.mk.eta] },
refine continuous_on.congr _ this,
rw [← e.target_eq],
exact e.to_local_homeomorph.continuous_on_symm
end
end has_zero
/-- If `e` is a `trivialization` of `proj : Z → B` with fiber `F` and `h` is a homeomorphism
`F ≃ₜ F'`, then `e.trans_fiber_homeomorph h` is the trivialization of `proj` with the fiber `F'`
that sends `p : Z` to `((e p).1, h (e p).2)`. -/
def trans_fiber_homeomorph {F' : Type*} [topological_space F']
(e : trivialization F proj) (h : F ≃ₜ F') : trivialization F' proj :=
{ to_local_homeomorph := e.to_local_homeomorph.trans_homeomorph $ (homeomorph.refl _).prod_congr h,
base_set := e.base_set,
open_base_set := e.open_base_set,
source_eq := e.source_eq,
target_eq := by simp [e.target_eq, prod_univ, preimage_preimage],
proj_to_fun := e.proj_to_fun }
@[simp] lemma trans_fiber_homeomorph_apply {F' : Type*} [topological_space F']
(e : trivialization F proj) (h : F ≃ₜ F') (x : Z) :
e.trans_fiber_homeomorph h x = ((e x).1, h (e x).2) :=
rfl
/-- Coordinate transformation in the fiber induced by a pair of bundle trivializations. See also
`trivialization.coord_change_homeomorph` for a version bundled as `F ≃ₜ F`. -/
def coord_change (e₁ e₂ : trivialization F proj) (b : B) (x : F) : F :=
(e₂ $ e₁.to_local_homeomorph.symm (b, x)).2
lemma mk_coord_change
(e₁ e₂ : trivialization F proj) {b : B}
(h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) (x : F) :
(b, e₁.coord_change e₂ b x) = e₂ (e₁.to_local_homeomorph.symm (b, x)) :=
begin
refine prod.ext _ rfl,
rw [e₂.coe_fst', ← e₁.coe_fst', e₁.apply_symm_apply' h₁],
{ rwa [e₁.proj_symm_apply' h₁] },
{ rwa [e₁.proj_symm_apply' h₁] }
end
lemma coord_change_apply_snd
(e₁ e₂ : trivialization F proj) {p : Z}
(h : proj p ∈ e₁.base_set) :
e₁.coord_change e₂ (proj p) (e₁ p).snd = (e₂ p).snd :=
by rw [coord_change, e₁.symm_apply_mk_proj (e₁.mem_source.2 h)]
lemma coord_change_same_apply
(e : trivialization F proj) {b : B} (h : b ∈ e.base_set) (x : F) :
e.coord_change e b x = x :=
by rw [coord_change, e.apply_symm_apply' h]
lemma coord_change_same
(e : trivialization F proj) {b : B} (h : b ∈ e.base_set) :
e.coord_change e b = id :=
funext $ e.coord_change_same_apply h
lemma coord_change_coord_change
(e₁ e₂ e₃ : trivialization F proj) {b : B}
(h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) (x : F) :
e₂.coord_change e₃ b (e₁.coord_change e₂ b x) = e₁.coord_change e₃ b x :=
begin
rw [coord_change, e₁.mk_coord_change _ h₁ h₂, ← e₂.coe_coe,
e₂.to_local_homeomorph.left_inv, coord_change],
rwa [e₂.mem_source, e₁.proj_symm_apply' h₁]
end
lemma continuous_coord_change (e₁ e₂ : trivialization F proj) {b : B}
(h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) :
continuous (e₁.coord_change e₂ b) :=
begin
refine continuous_snd.comp (e₂.to_local_homeomorph.continuous_on.comp_continuous
(e₁.to_local_homeomorph.continuous_on_symm.comp_continuous _ _) _),
{ exact continuous_const.prod_mk continuous_id },
{ exact λ x, e₁.mem_target.2 h₁ },
{ intro x,
rwa [e₂.mem_source, e₁.proj_symm_apply' h₁] }
end
/-- Coordinate transformation in the fiber induced by a pair of bundle trivializations,
as a homeomorphism. -/
protected def coord_change_homeomorph
(e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) :
F ≃ₜ F :=
{ to_fun := e₁.coord_change e₂ b,
inv_fun := e₂.coord_change e₁ b,
left_inv := λ x, by simp only [*, coord_change_coord_change, coord_change_same_apply],
right_inv := λ x, by simp only [*, coord_change_coord_change, coord_change_same_apply],
continuous_to_fun := e₁.continuous_coord_change e₂ h₁ h₂,
continuous_inv_fun := e₂.continuous_coord_change e₁ h₂ h₁ }
@[simp] lemma coord_change_homeomorph_coe
(e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) :
⇑(e₁.coord_change_homeomorph e₂ h₁ h₂) = e₁.coord_change e₂ b :=
rfl
variables {F} {B' : Type*} [topological_space B']
lemma is_image_preimage_prod (e : trivialization F proj) (s : set B) :
e.to_local_homeomorph.is_image (proj ⁻¹' s) (s ×ˢ univ) :=
λ x hx, by simp [e.coe_fst', hx]
/-- Restrict a `trivialization` to an open set in the base. `-/
protected def restr_open (e : trivialization F proj) (s : set B)
(hs : is_open s) : trivialization F proj :=
{ to_local_homeomorph := ((e.is_image_preimage_prod s).symm.restr
(is_open.inter e.open_target (hs.prod is_open_univ))).symm,
base_set := e.base_set ∩ s,
open_base_set := is_open.inter e.open_base_set hs,
source_eq := by simp [e.source_eq],
target_eq := by simp [e.target_eq, prod_univ],
proj_to_fun := λ p hp, e.proj_to_fun p hp.1 }
section piecewise
lemma frontier_preimage (e : trivialization F proj) (s : set B) :
e.source ∩ frontier (proj ⁻¹' s) = proj ⁻¹' (e.base_set ∩ frontier s) :=
by rw [← (e.is_image_preimage_prod s).frontier.preimage_eq, frontier_prod_univ_eq,
(e.is_image_preimage_prod _).preimage_eq, e.source_eq, preimage_inter]
/-- Given two bundle trivializations `e`, `e'` of `proj : Z → B` and a set `s : set B` such that
the base sets of `e` and `e'` intersect `frontier s` on the same set and `e p = e' p` whenever
`proj p ∈ e.base_set ∩ frontier s`, `e.piecewise e' s Hs Heq` is the bundle trivialization over
`set.ite s e.base_set e'.base_set` that is equal to `e` on `proj ⁻¹ s` and is equal to `e'`
otherwise. -/
noncomputable def piecewise (e e' : trivialization F proj) (s : set B)
(Hs : e.base_set ∩ frontier s = e'.base_set ∩ frontier s)
(Heq : eq_on e e' $ proj ⁻¹' (e.base_set ∩ frontier s)) :
trivialization F proj :=
{ to_local_homeomorph := e.to_local_homeomorph.piecewise e'.to_local_homeomorph
(proj ⁻¹' s) (s ×ˢ univ) (e.is_image_preimage_prod s) (e'.is_image_preimage_prod s)
(by rw [e.frontier_preimage, e'.frontier_preimage, Hs])
(by rwa e.frontier_preimage),
base_set := s.ite e.base_set e'.base_set,
open_base_set := e.open_base_set.ite e'.open_base_set Hs,
source_eq := by simp [e.source_eq, e'.source_eq],
target_eq := by simp [e.target_eq, e'.target_eq, prod_univ],
proj_to_fun := by rintro p (⟨he, hs⟩|⟨he, hs⟩); simp * }
/-- Given two bundle trivializations `e`, `e'` of a topological fiber bundle `proj : Z → B`
over a linearly ordered base `B` and a point `a ∈ e.base_set ∩ e'.base_set` such that
`e` equals `e'` on `proj ⁻¹' {a}`, `e.piecewise_le_of_eq e' a He He' Heq` is the bundle
trivialization over `set.ite (Iic a) e.base_set e'.base_set` that is equal to `e` on points `p`
such that `proj p ≤ a` and is equal to `e'` otherwise. -/
noncomputable def piecewise_le_of_eq [linear_order B] [order_topology B]
(e e' : trivialization F proj) (a : B) (He : a ∈ e.base_set) (He' : a ∈ e'.base_set)
(Heq : ∀ p, proj p = a → e p = e' p) :
trivialization F proj :=
e.piecewise e' (Iic a)
(set.ext $ λ x, and.congr_left_iff.2 $ λ hx,
by simp [He, He', mem_singleton_iff.1 (frontier_Iic_subset _ hx)])
(λ p hp, Heq p $ frontier_Iic_subset _ hp.2)
/-- Given two bundle trivializations `e`, `e'` of a topological fiber bundle `proj : Z → B` over a
linearly ordered base `B` and a point `a ∈ e.base_set ∩ e'.base_set`, `e.piecewise_le e' a He He'`
is the bundle trivialization over `set.ite (Iic a) e.base_set e'.base_set` that is equal to `e` on
points `p` such that `proj p ≤ a` and is equal to `((e' p).1, h (e' p).2)` otherwise, where
`h = `e'.coord_change_homeomorph e _ _` is the homeomorphism of the fiber such that
`h (e' p).2 = (e p).2` whenever `e p = a`. -/
noncomputable def piecewise_le [linear_order B] [order_topology B]
(e e' : trivialization F proj) (a : B) (He : a ∈ e.base_set) (He' : a ∈ e'.base_set) :
trivialization F proj :=
e.piecewise_le_of_eq (e'.trans_fiber_homeomorph (e'.coord_change_homeomorph e He' He))
a He He' $ by { unfreezingI {rintro p rfl },
ext1,
{ simp [e.coe_fst', e'.coe_fst', *] },
{ simp [e'.coord_change_apply_snd, *] } }
/-- Given two bundle trivializations `e`, `e'` over disjoint sets, `e.disjoint_union e' H` is the
bundle trivialization over the union of the base sets that agrees with `e` and `e'` over their
base sets. -/
noncomputable def disjoint_union (e e' : trivialization F proj)
(H : disjoint e.base_set e'.base_set) :
trivialization F proj :=
{ to_local_homeomorph := e.to_local_homeomorph.disjoint_union e'.to_local_homeomorph
(by { rw [e.source_eq, e'.source_eq], exact H.preimage _, })
(by { rw [e.target_eq, e'.target_eq, disjoint_iff_inf_le],
intros x hx, exact H.le_bot ⟨hx.1.1, hx.2.1⟩ }),
base_set := e.base_set ∪ e'.base_set,
open_base_set := is_open.union e.open_base_set e'.open_base_set,
source_eq := congr_arg2 (∪) e.source_eq e'.source_eq,
target_eq := (congr_arg2 (∪) e.target_eq e'.target_eq).trans union_prod.symm,
proj_to_fun :=
begin
rintro p (hp|hp'),
{ show (e.source.piecewise e e' p).1 = proj p,
rw [piecewise_eq_of_mem, e.coe_fst]; exact hp },
{ show (e.source.piecewise e e' p).1 = proj p,
rw [piecewise_eq_of_not_mem, e'.coe_fst hp'],
simp only [e.source_eq, e'.source_eq] at hp' ⊢,
exact λ h, H.le_bot ⟨h, hp'⟩ }
end }
end piecewise
end trivialization
|
ed220afa99681ce9d2d266389979c38a106180ad | 49bd2218ae088932d847f9030c8dbff1c5607bb7 | /src/analysis/calculus/tangent_cone.lean | 78146a41a857f761cc5f7c549662f92f6e670cdb | [
"Apache-2.0"
] | permissive | FredericLeRoux/mathlib | e8f696421dd3e4edc8c7edb3369421c8463d7bac | 3645bf8fb426757e0a20af110d1fdded281d286e | refs/heads/master | 1,607,062,870,732 | 1,578,513,186,000 | 1,578,513,186,000 | 231,653,181 | 0 | 0 | Apache-2.0 | 1,578,080,327,000 | 1,578,080,326,000 | null | UTF-8 | Lean | false | false | 18,505 | 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 analysis.convex analysis.normed_space.bounded_linear_maps analysis.specific_limits
/-!
# Tangent cone
In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s`
ensuring that, if a function has two derivatives, then they have to coincide. As a direct
definition of this fact (quantifying on all target types and all functions) would depend on
universes, we use a more intrinsic definition: if all the possible tangent directions to the set
`s` at the point `x` span a dense subset of the whole subset, it is easy to check that the
derivative has to be unique.
Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`,
and express `unique_diff_within_at` and `unique_diff_on` in terms of it.
One should however think of this definition as an implementation detail: the only reason to
introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness
of the derivative. This is why their names reflect their uses, and not how they are defined.
## Implementation details
Note that this file is imported by `deriv.lean`. Hence, derivatives are not defined yet. The
property of uniqueness of the derivative is therefore proved in `deriv.lean`, but based on the
properties of the tangent cone we prove here.
-/
variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E]
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
variables {G : Type*} [normed_group G] [normed_space ℝ G]
set_option class.instance_max_depth 50
open filter set
open_locale topological_space
/-- The set of all tangent directions to the set `s` at the point `x`. -/
def tangent_cone_at (s : set E) (x : E) : set E :=
{y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), {n:ℕ | x + d n ∈ s} ∈ (at_top : filter ℕ) ∧
(tendsto (λn, ∥c n∥) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))}
/-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space.
The main role of this property is to ensure that the differential within `s` at `x` is unique,
hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `deriv.lean`.
To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which
is automatic when `E` is not `0`-dimensional).
-/
def unique_diff_within_at (s : set E) (x : E) : Prop :=
closure ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E) = univ ∧ x ∈ closure s
/-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of
the whole space. The main role of this property is to ensure that the differential along `s` is
unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in
`deriv.lean`. -/
def unique_diff_on (s : set E) : Prop :=
∀x ∈ s, unique_diff_within_at 𝕜 s x
variables {𝕜} {x y : E} {s t : set E}
section tangent_cone
/- This section is devoted to the properties of the tangent cone. -/
open normed_field
lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ :=
begin
refine univ_subset_iff.1 (λy hy, _),
rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩,
refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem_sets' (λn, mem_univ _), _, _⟩,
{ simp only [norm_pow],
exact tendsto_pow_at_top_at_top_of_gt_1 hw },
{ convert tendsto_const_nhds,
ext n,
have : w ^ n * (w ^ n)⁻¹ = 1,
{ apply mul_inv_cancel,
apply pow_ne_zero,
simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm },
rw [smul_smul, this, one_smul] }
end
lemma tangent_cone_mono (h : s ⊆ t) :
tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x :=
begin
rintros y ⟨c, d, ds, ctop, clim⟩,
exact ⟨c, d, mem_sets_of_superset ds (λn hn, h hn), ctop, clim⟩
end
/-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone,
the sequence `d` tends to 0 at infinity. -/
lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E}
(hc : tendsto (λn, ∥c n∥) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) :
tendsto d l (𝓝 0) :=
begin
have A : tendsto (λn, ∥c n∥⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc,
have B : tendsto (λn, ∥c n • d n∥) l (𝓝 ∥y∥) :=
(continuous_norm.tendsto _).comp hd,
have C : tendsto (λn, ∥c n∥⁻¹ * ∥c n • d n∥) l (𝓝 (0 * ∥y∥)) := A.mul B,
rw zero_mul at C,
have : {n | ∥c n∥⁻¹ * ∥c n • d n∥ = ∥d n∥} ∈ l,
{ apply mem_sets_of_superset (ne_mem_of_tendsto_norm_at_top hc 0) (λn hn, _),
rw [mem_set_of_eq, norm_smul, ← mul_assoc, inv_mul_cancel, one_mul],
rwa [ne.def, norm_eq_zero] },
have D : tendsto (λ n, ∥d n∥) l (𝓝 0) :=
tendsto.congr' this C,
rw tendsto_zero_iff_norm_tendsto_zero,
exact D
end
/-- Intersecting with a neighborhood of the point does not change the tangent cone. -/
lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) :
tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x :=
begin
refine subset.antisymm (tangent_cone_mono (inter_subset_left _ _)) _,
rintros y ⟨c, d, ds, ctop, clim⟩,
refine ⟨c, d, _, ctop, clim⟩,
have : {n : ℕ | x + d n ∈ t} ∈ at_top,
{ have : tendsto (λn, x + d n) at_top (𝓝 (x + 0)) :=
tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim),
rw add_zero at this,
exact mem_map.1 (this ht) },
exact inter_mem_sets ds this
end
/-- The tangent cone of a product contains the tangent cone of its left factor. -/
lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) :
set.prod (tangent_cone_at 𝕜 s x) {(0 : F)} ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
begin
rintros ⟨v, w⟩ ⟨⟨c, d, hd, hc, hy⟩, hw⟩,
have : w = 0, by simpa using hw,
rw this,
have : ∀n, ∃d', y + d' ∈ t ∧ ∥c n • d'∥ ≤ ((1:ℝ)/2)^n,
{ assume n,
have c_pos : 0 < 1 + ∥c n∥ :=
add_pos_of_pos_of_nonneg zero_lt_one (norm_nonneg _),
rcases metric.mem_closure_iff'.1 ht ((1 + ∥c n∥)⁻¹ * (1/2)^n) _ with ⟨z, z_pos, hz⟩,
refine ⟨z - y, _, _⟩,
{ convert z_pos, abel },
{ rw [norm_smul, ← dist_eq_norm, dist_comm],
calc ∥c n∥ * dist y z ≤ (1 + ∥c n∥) * ((1 + ∥c n∥)⁻¹ * (1/2)^n) :
begin
apply mul_le_mul _ (le_of_lt hz) dist_nonneg (le_of_lt c_pos),
simp only [zero_le_one, le_add_iff_nonneg_left]
end
... = (1/2)^n :
begin
rw [← mul_assoc, mul_inv_cancel, one_mul],
exact ne_of_gt c_pos
end },
{ apply mul_pos (inv_pos c_pos) (pow_pos _ _),
norm_num } },
choose d' hd' using this,
refine ⟨c, λn, (d n, d' n), _, hc, _⟩,
show {n : ℕ | (x, y) + (d n, d' n) ∈ set.prod s t} ∈ at_top,
{ apply filter.mem_sets_of_superset hd,
assume n hn,
simp at hn,
simp [hn, (hd' n).1] },
{ apply tendsto_prod_mk_nhds hy,
change tendsto (λ (n : ℕ), c n • d' n) at_top (𝓝 0),
rw tendsto_zero_iff_norm_tendsto_zero,
refine squeeze_zero (λn, norm_nonneg _) (λn, (hd' n).2) _,
apply tendsto_pow_at_top_nhds_0_of_lt_1; norm_num }
end
/-- The tangent cone of a product contains the tangent cone of its right factor. -/
lemma subset_tangent_cone_prod_right {t : set F} {y : F}
(hs : x ∈ closure s) :
set.prod {(0 : E)} (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
begin
rintros ⟨v, w⟩ ⟨hv, ⟨c, d, hd, hc, hy⟩⟩,
have : v = 0, by simpa using hv,
rw this,
have : ∀n, ∃d', x + d' ∈ s ∧ ∥c n • d'∥ ≤ ((1:ℝ)/2)^n,
{ assume n,
have c_pos : 0 < 1 + ∥c n∥ :=
add_pos_of_pos_of_nonneg zero_lt_one (norm_nonneg _),
rcases metric.mem_closure_iff'.1 hs ((1 + ∥c n∥)⁻¹ * (1/2)^n) _ with ⟨z, z_pos, hz⟩,
refine ⟨z - x, _, _⟩,
{ convert z_pos, abel },
{ rw [norm_smul, ← dist_eq_norm, dist_comm],
calc ∥c n∥ * dist x z ≤ (1 + ∥c n∥) * ((1 + ∥c n∥)⁻¹ * (1/2)^n) :
begin
apply mul_le_mul _ (le_of_lt hz) dist_nonneg (le_of_lt c_pos),
simp only [zero_le_one, le_add_iff_nonneg_left]
end
... = (1/2)^n :
begin
rw [← mul_assoc, mul_inv_cancel, one_mul],
exact ne_of_gt c_pos
end },
{ apply mul_pos (inv_pos c_pos) (pow_pos _ _),
norm_num } },
choose d' hd' using this,
refine ⟨c, λn, (d' n, d n), _, hc, _⟩,
show {n : ℕ | (x, y) + (d' n, d n) ∈ set.prod s t} ∈ at_top,
{ apply filter.mem_sets_of_superset hd,
assume n hn,
simp at hn,
simp [hn, (hd' n).1] },
{ apply tendsto_prod_mk_nhds _ hy,
change tendsto (λ (n : ℕ), c n • d' n) at_top (𝓝 0),
rw tendsto_zero_iff_norm_tendsto_zero,
refine squeeze_zero (λn, norm_nonneg _) (λn, (hd' n).2) _,
apply tendsto_pow_at_top_nhds_0_of_lt_1; norm_num }
end
/-- If a subset of a real vector space contains a segment, then the direction of this
segment belongs to the tangent cone at its endpoints. -/
lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment x y ⊆ s) :
y - x ∈ tangent_cone_at ℝ s x :=
begin
let w : ℝ := 2,
let c := λn:ℕ, (2:ℝ)^n,
let d := λn:ℕ, (c n)⁻¹ • (y-x),
refine ⟨c, d, filter.univ_mem_sets' (λn, h _), _, _⟩,
show x + d n ∈ segment x y,
{ refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩,
{ rw inv_nonneg, apply pow_nonneg, norm_num },
{ apply inv_le_one, apply one_le_pow_of_one_le, norm_num },
{ simp only [d], abel } },
show filter.tendsto (λ (n : ℕ), ∥c n∥) filter.at_top filter.at_top,
{ have : (λ (n : ℕ), ∥c n∥) = c,
by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) },
rw this,
exact tendsto_pow_at_top_at_top_of_gt_1 (by norm_num) },
show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)),
{ have : (λ (n : ℕ), c n • d n) = (λn, y - x),
{ ext n,
simp only [d, smul_smul],
rw [mul_inv_cancel, one_smul],
exact pow_ne_zero _ (by norm_num) },
rw this,
apply tendsto_const_nhds }
end
end tangent_cone
section unique_diff
/- This section is devoted to properties of the predicates `unique_diff_within_at` and
`unique_diff_on`. -/
lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x :=
by { rw [unique_diff_within_at, tangent_cone_univ], simp }
lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) :=
λx hx, unique_diff_within_at_univ
lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) :
unique_diff_within_at 𝕜 t x :=
begin
unfold unique_diff_within_at at *,
rw [← univ_subset_iff, ← h.1],
exact ⟨closure_mono (submodule.span_mono (tangent_cone_mono st)), closure_mono st h.2⟩
end
lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
begin
have : x ∈ closure (s ∩ t) ↔ x ∈ closure s,
{ split,
{ assume h, exact closure_mono (inter_subset_left _ _) h },
{ assume h,
rw mem_closure_iff_nhds at ⊢ h,
assume u hu,
rw [inter_comm s t, ← inter_assoc],
exact h _ (filter.inter_mem_sets hu ht) } },
rw [unique_diff_within_at, unique_diff_within_at, tangent_cone_inter_nhds ht, this]
end
lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter ht).2 hs
lemma unique_diff_within_at_inter' (ht : t ∈ nhds_within x s) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
begin
split,
{ exact λH, H.mono (inter_subset_left _ _) },
{ assume H,
rw mem_nhds_within at ht,
rcases ht with ⟨u, u_open, xu, us⟩,
have : u ∈ 𝓝 x := mem_nhds_sets u_open xu,
rw ← unique_diff_within_at_inter this at H,
apply H.mono,
exact λ p ⟨ps, pu⟩, ⟨ps, us ⟨pu, ps⟩⟩ }
end
lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ nhds_within x s) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter' ht).2 hs
lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x :=
begin
have := unique_diff_within_at_univ.inter (mem_nhds_sets hs xs),
rwa univ_inter at this
end
lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) :=
λx hx, (hs x hx.1).inter (mem_nhds_sets ht hx.2)
lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s :=
λx hx, is_open.unique_diff_within_at hs hx
/-- The product of two sets of unique differentiability at points `x` and `y` has unique
differentiability at `(x, y)`. -/
lemma unique_diff_within_at.prod {t : set F} {y : F}
(hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) :
unique_diff_within_at 𝕜 (set.prod s t) (x, y) :=
begin
rw [unique_diff_within_at, ← univ_subset_iff] at ⊢ hs ht,
split,
{ assume v _,
rw metric.mem_closure_iff',
assume ε ε_pos,
rcases v with ⟨v₁, v₂⟩,
rcases metric.mem_closure_iff'.1 (hs.1 (mem_univ v₁)) ε ε_pos with ⟨w₁, w₁_mem, h₁⟩,
rcases metric.mem_closure_iff'.1 (ht.1 (mem_univ v₂)) ε ε_pos with ⟨w₂, w₂_mem, h₂⟩,
have I₁ : (w₁, (0 : F)) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)),
{ apply submodule.span_induction w₁_mem,
{ assume w hw,
have : (w, (0 : F)) ∈ (set.prod (tangent_cone_at 𝕜 s x) {(0 : F)}),
{ rw mem_prod,
simp [hw],
apply mem_insert },
have : (w, (0 : F)) ∈ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
subset_tangent_cone_prod_left ht.2 this,
exact submodule.subset_span this },
{ exact submodule.zero_mem _ },
{ assume a b ha hb,
have : (a, (0 : F)) + (b, (0 : F)) = (a + b, (0 : F)), by simp,
rw ← this,
exact submodule.add_mem _ ha hb },
{ assume c a ha,
have : c • (0 : F) = (0 : F), by simp,
rw ← this,
exact submodule.smul_mem _ _ ha } },
have I₂ : ((0 : E), w₂) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)),
{ apply submodule.span_induction w₂_mem,
{ assume w hw,
have : ((0 : E), w) ∈ (set.prod {(0 : E)} (tangent_cone_at 𝕜 t y)),
{ rw mem_prod,
simp [hw],
apply mem_insert },
have : ((0 : E), w) ∈ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
subset_tangent_cone_prod_right hs.2 this,
exact submodule.subset_span this },
{ exact submodule.zero_mem _ },
{ assume a b ha hb,
have : ((0 : E), a) + ((0 : E), b) = ((0 : E), a + b), by simp,
rw ← this,
exact submodule.add_mem _ ha hb },
{ assume c a ha,
have : c • (0 : E) = (0 : E), by simp,
rw ← this,
exact submodule.smul_mem _ _ ha } },
have I : (w₁, w₂) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)),
{ have : (w₁, (0 : F)) + ((0 : E), w₂) = (w₁, w₂), by simp,
rw ← this,
exact submodule.add_mem _ I₁ I₂ },
refine ⟨(w₁, w₂), I, _⟩,
simp [dist, h₁, h₂] },
{ simp [closure_prod_eq, mem_prod_iff, hs.2, ht.2] }
end
/-- The product of two sets of unique differentiability is a set of unique differentiability. -/
lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) :
unique_diff_on 𝕜 (set.prod s t) :=
λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2)
/-- In a real vector space, a convex set with nonempty interior is a set of unique
differentiability. -/
theorem unique_diff_on_convex {s : set G} (conv : convex s) (hs : interior s ≠ ∅) :
unique_diff_on ℝ s :=
begin
assume x xs,
have A : ∀v, ∃a∈ tangent_cone_at ℝ s x, ∃b∈ tangent_cone_at ℝ s x, ∃δ>(0:ℝ), δ • v = b-a,
{ assume v,
rcases ne_empty_iff_exists_mem.1 hs with ⟨y, hy⟩,
have ys : y ∈ s := interior_subset hy,
have : ∃(δ : ℝ), 0<δ ∧ y + δ • v ∈ s,
{ by_cases h : ∥v∥ = 0,
{ exact ⟨1, zero_lt_one, by simp [(norm_eq_zero _).1 h, ys]⟩ },
{ rcases mem_interior.1 hy with ⟨u, us, u_open, yu⟩,
rcases metric.is_open_iff.1 u_open y yu with ⟨ε, εpos, hε⟩,
let δ := (ε/2) / ∥v∥,
have δpos : 0 < δ := div_pos (half_pos εpos) (lt_of_le_of_ne (norm_nonneg _) (ne.symm h)),
have : y + δ • v ∈ s,
{ apply us (hε _),
rw [metric.mem_ball, dist_eq_norm],
calc ∥(y + δ • v) - y ∥ = ∥δ • v∥ : by {congr' 1, abel }
... = ∥δ∥ * ∥v∥ : norm_smul _ _
... = δ * ∥v∥ : by simp only [norm, abs_of_nonneg (le_of_lt δpos)]
... = ε /2 : div_mul_cancel _ h
... < ε : half_lt_self εpos },
exact ⟨δ, δpos, this⟩ } },
rcases this with ⟨δ, δpos, hδ⟩,
refine ⟨y-x, _, (y + δ • v) - x, _, δ, δpos, by abel⟩,
exact mem_tangent_cone_of_segment_subset (convex_segment_iff.1 conv x y xs ys),
exact mem_tangent_cone_of_segment_subset (convex_segment_iff.1 conv x _ xs hδ) },
have B : ∀v:G, v ∈ submodule.span ℝ (tangent_cone_at ℝ s x),
{ assume v,
rcases A v with ⟨a, ha, b, hb, δ, hδ, h⟩,
have : v = δ⁻¹ • (b - a),
by { rw [← h, smul_smul, inv_mul_cancel, one_smul], exact (ne_of_gt hδ) },
rw this,
exact submodule.smul_mem _ _
(submodule.sub_mem _ (submodule.subset_span hb) (submodule.subset_span ha)) },
refine ⟨univ_subset_iff.1 (λv hv, subset_closure (B v)), subset_closure xs⟩
end
/-- The real interval `[0, 1]` is a set of unique differentiability. -/
lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) :=
begin
apply unique_diff_on_convex (convex_Icc 0 1),
have : (1/(2:ℝ)) ∈ interior (Icc (0:ℝ) 1) :=
mem_interior.2 ⟨Ioo (0:ℝ) 1, Ioo_subset_Icc_self, is_open_Ioo, by norm_num, by norm_num⟩,
exact ne_empty_of_mem this,
end
end unique_diff
|
c5c5afcc0fef88f94f32b24be68689be9ca2afe0 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/simpStarHyp.lean | ee409c90442c956ea053e8587fe55e25f7785064 | [
"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 | 217 | lean | theorem ex1 (h₁ : a = 0) (h₂ : a + b = 0 + 0) : b + 0 = 0 := by
simp [h₁, h₂] at h₁ h₂
simp [h₂]
theorem ex2 (h₁ : a = 0) (h₂ : a + b = 0 + 0) : b + 0 = 0 := by
simp [h₁] at *
simp [h₂]
|
2540c1bb6b2a5d36fae55008ab649b7a37859d28 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/topology/instances/complex.lean | 0f3120aa8bd23cb60dafa45de4f67dc768930b3b | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 6,038 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Topology of the complex numbers.
-/
import data.complex.basic topology.metric_space.basic topology.instances.real
noncomputable theory
open filter metric
open_locale topological_space
namespace complex
-- TODO(Mario): these proofs are all copied from analysis/real. Generalize
-- to normed fields
instance : metric_space ℂ :=
{ dist := λx y, (x - y).abs,
dist_self := by simp [abs_zero],
eq_of_dist_eq_zero := by simp [add_neg_eq_zero],
dist_comm := assume x y, complex.abs_sub _ _,
dist_triangle := assume x y z, complex.abs_sub_le _ _ _ }
theorem dist_eq (x y : ℂ) : dist x y = (x - y).abs := rfl
theorem uniform_continuous_add : uniform_continuous (λp : ℂ × ℂ, p.1 + p.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩
theorem uniform_continuous_neg : uniform_continuous (@has_neg.neg ℂ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [dist_eq] using h⟩
instance : uniform_add_group ℂ :=
uniform_add_group.mk' uniform_continuous_add uniform_continuous_neg
instance : topological_add_group ℂ := by apply_instance
lemma uniform_continuous_inv (s : set ℂ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) :
uniform_continuous (λp:s, p.1⁻¹) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in
⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩
lemma uniform_continuous_abs : uniform_continuous (abs : ℂ → ℝ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _)⟩
lemma continuous_abs : continuous (abs : ℂ → ℝ) :=
uniform_continuous_abs.continuous
lemma tendsto_inv {r : ℂ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) :=
by rw ← abs_pos at r0; exact
tendsto_of_uniform_continuous_subtype
(uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h))
(mem_nhds_sets (continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0))
lemma continuous_inv' : continuous (λa:{r:ℂ // r ≠ 0}, a.val⁻¹) :=
continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩,
tendsto.comp (tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _)
lemma continuous_inv {α} [topological_space α] {f : α → ℂ} (h : ∀a, f a ≠ 0) (hf : continuous f) :
continuous (λa, (f a)⁻¹) :=
show continuous ((has_inv.inv ∘ @subtype.val ℂ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩),
from continuous_inv'.comp (continuous_subtype_mk _ hf)
lemma uniform_continuous_mul_const {x : ℂ} : uniform_continuous ((*) x) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, begin
cases no_top (abs x) with y xy,
have y0 := lt_of_le_of_lt (abs_nonneg _) xy,
refine ⟨_, div_pos ε0 y0, λ a b h, _⟩,
rw [dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)],
exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0
end
lemma uniform_continuous_mul (s : set (ℂ × ℂ))
{r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℂ × ℂ).1 < r₁ ∧ abs x.2 < r₂) :
uniform_continuous (λp:s, p.1.1 * p.1.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h,
let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩
protected lemma continuous_mul : continuous (λp : ℂ × ℂ, p.1 * p.2) :=
continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩,
tendsto_of_uniform_continuous_subtype
(uniform_continuous_mul
({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1})
(λ x, id))
(mem_nhds_sets
(is_open_prod
(continuous_abs _ $ is_open_gt' (abs a₁ + 1))
(continuous_abs _ $ is_open_gt' (abs a₂ + 1)))
⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩)
local attribute [semireducible] real.le
lemma uniform_continuous_re : uniform_continuous re :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_re_le_abs _)⟩)
lemma continuous_re : continuous re := uniform_continuous_re.continuous
lemma uniform_continuous_im : uniform_continuous im :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_im_le_abs _)⟩)
lemma continuous_im : continuous im := uniform_continuous_im.continuous
lemma uniform_continuous_of_real : uniform_continuous of_real :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _,
by rw [real.dist_eq, complex.dist_eq, of_real_eq_coe, of_real_eq_coe, ← of_real_sub, abs_of_real];
exact id⟩)
lemma continuous_of_real : continuous of_real := uniform_continuous_of_real.continuous
instance : topological_ring ℂ :=
{ continuous_mul := complex.continuous_mul, ..complex.topological_add_group }
instance : topological_semiring ℂ := by apply_instance
def real_prod_homeo : homeomorph ℂ (ℝ × ℝ) :=
{ to_equiv := real_prod_equiv,
continuous_to_fun := continuous.prod_mk continuous_re continuous_im,
continuous_inv_fun := show continuous (λ p : ℝ × ℝ, complex.mk p.1 p.2),
by simp only [mk_eq_add_mul_I]; exact
continuous_add
(continuous_of_real.comp continuous_fst)
(continuous_mul (continuous_of_real.comp continuous_snd) continuous_const) }
instance : proper_space ℂ :=
⟨λx r, begin
refine real_prod_homeo.symm.compact_preimage.1
(compact_of_is_closed_subset
(compact_prod _ _ (proper_space.compact_ball x.re r) (proper_space.compact_ball x.im r))
(continuous_iff_is_closed.1 real_prod_homeo.symm.continuous _ is_closed_ball) _),
exact λ p h, ⟨
le_trans (abs_re_le_abs (⟨p.1, p.2⟩ - x)) h,
le_trans (abs_im_le_abs (⟨p.1, p.2⟩ - x)) h⟩
end⟩
end complex
|
f6d1edc45a126133f7e60656c44111942bc14886 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/pldi/ptreq.lean | f24558a6c209c01ff94ecc504f3dae1cb69effae | [
"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 | 1,634 | lean | namespace Demo
@[extern "lean_ptr_addr"]
unsafe def ptrAddrUnsafe {α : Type} (a : @& α) : USize := 0
@[inline] unsafe def withPtrEqUnsafe {α : Type} (a b : α) (k : Unit → Bool) (h : a = b → k () = true) : Bool :=
if ptrAddrUnsafe a == ptrAddrUnsafe b then true else k ()
@[implemented_by withPtrEqUnsafe]
def withPtrEq {α : Type} (a b : α) (k : Unit → Bool) (h : a = b → k () = true) : Bool :=
k ()
/-
class inductive Decidable (p : Prop)
| isFalse (h : ¬p) : Decidable
| isTrue (h : p) : Decidable
-/
/-- `withPtrEq` for `DecidableEq` -/
@[inline] def withPtrEqDecEq {α : Type} (a b : α) (k : Unit → Decidable (a = b)) : Decidable (a = b) :=
let b := withPtrEq a b (fun _ => toBoolUsing (k ())) (toBoolUsingEqTrue (k ()));
condEq b
(fun h => isTrue (ofBoolUsingEqTrue h))
(fun h => isFalse (ofBoolUsingEqFalse h))
end Demo
/-
abbrev DecidableEq (α : Sort u) :=
∀ (a b : α), Decidable (a = b)
-/
def List.decEqAux {α} [s : DecidableEq α] : DecidableEq (List α)
| [], [] => isTrue rfl
| a::as, b::bs =>
match s a b with
| isFalse n => isFalse fun h => List.noConfusion h fun h₁ _ => absurd h₁ n
| isTrue h₁ =>
match List.decEqAux as bs with
-- match withPtrEqDecEq as bs (fun _ => List.decEqAux as bs with)
| isFalse n => isFalse fun h => List.noConfusion h fun _ h₁ => absurd h₁ n
| isTrue h₂ => isTrue (h₁ ▸ h₂ ▸ rfl)
| (_::_), [] => isFalse fun h => List.noConfusion h
| [], (_::_) => isFalse fun h => List.noConfusion h
def List.decEq {α} [DecidableEq α] : DecidableEq (List α) :=
fun a b => withPtrEqDecEq a b (fun _ => List.decEqAux a b)
|
4da383ed77056a72d84691e0483c9be35ac72c23 | 19cc34575500ee2e3d4586c15544632aa07a8e66 | /src/topology/basic.lean | d062c79cf13be11432f29699980ba45c0c04b1af | [
"Apache-2.0"
] | permissive | LibertasSpZ/mathlib | b9fcd46625eb940611adb5e719a4b554138dade6 | 33f7870a49d7cc06d2f3036e22543e6ec5046e68 | refs/heads/master | 1,672,066,539,347 | 1,602,429,158,000 | 1,602,429,158,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 48,651 | 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
alias dense_iff_inter_open ↔ dense.inter_open_nonempty _
@[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
theorem is_open_iff_ultrafilter {s : set α} :
is_open s ↔ (∀ (x ∈ s) (l : filter α), is_ultrafilter l → l ≤ 𝓝 x → s ∈ l) :=
by simp_rw [is_open_iff_mem_nhds, @mem_iff_ultrafilter _ (𝓝 _)]
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 le_nhds_Lim {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 tendsto_nhds_lim {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) :
tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) :=
le_nhds_Lim 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
|
e6599f44eb30094adca1259d27d714622affd90c | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/measure/finite_measure_weak_convergence.lean | 48b58a3c8b60a290643ff2ba145bbec8bd9246ab | [
"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 | 20,895 | lean | /-
Copyright (c) 2021 Kalle Kytölä. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kalle Kytölä
-/
import measure_theory.measure.measure_space
import measure_theory.integral.bochner
import topology.continuous_function.bounded
import topology.algebra.module.weak_dual
/-!
# Weak convergence of (finite) measures
This file defines the topology of weak convergence of finite measures and probability measures
on topological spaces. The topology of weak convergence is the coarsest topology w.r.t. which
for every bounded continuous `ℝ≥0`-valued function `f`, the integration of `f` against the
measure is continuous.
TODOs:
* Prove that an equivalent definition of the topologies is obtained requiring continuity of
integration of bounded continuous `ℝ`-valued functions instead.
* Include the portmanteau theorem on characterizations of weak convergence of (Borel) probability
measures.
## Main definitions
The main definitions are the
* types `finite_measure α` and `probability_measure α` with topologies of weak convergence;
* `to_weak_dual_bcnn : finite_measure α → (weak_dual ℝ≥0 (α →ᵇ ℝ≥0))`
allowing to interpret a finite measure as a continuous linear functional on the space of
bounded continuous nonnegative functions on `α`. This is used for the definition of the
topology of weak convergence.
## Main results
* Finite measures `μ` on `α` give rise to continuous linear functionals on the space of
bounded continuous nonnegative functions on `α` via integration:
`to_weak_dual_bcnn : finite_measure α → (weak_dual ℝ≥0 (α →ᵇ ℝ≥0))`.
* `tendsto_iff_forall_lintegral_tendsto`: Convergence of finite measures and probability measures
is characterized by the convergence of integrals of all bounded continuous (nonnegative)
functions. This essentially shows that the given definition of topology corresponds to the
common textbook definition of weak convergence of measures.
TODO:
* Portmanteau theorem.
## Notations
No new notation is introduced.
## Implementation notes
The topology of weak convergence of finite Borel measures will be defined using a mapping from
`finite_measure α` to `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)`, inheriting the topology from the latter.
The current implementation of `finite_measure α` and `probability_measure α` is directly as
subtypes of `measure α`, and the coercion to a function is the composition `ennreal.to_nnreal`
and the coercion to function of `measure α`. Another alternative would be to use a bijection
with `vector_measure α ℝ≥0` as an intermediate step. The choice of implementation should not have
drastic downstream effects, so it can be changed later if appropriate.
Potential advantages of using the `nnreal`-valued vector measure alternative:
* The coercion to function would avoid need to compose with `ennreal.to_nnreal`, the
`nnreal`-valued API could be more directly available.
Potential drawbacks of the vector measure alternative:
* The coercion to function would lose monotonicity, as non-measurable sets would be defined to
have measure 0.
* No integration theory directly. E.g., the topology definition requires `lintegral` w.r.t.
a coercion to `measure α` in any case.
## References
* [Billingsley, *Convergence of probability measures*][billingsley1999]
## Tags
weak convergence of measures, finite measure, probability measure
-/
noncomputable theory
open measure_theory
open set
open filter
open bounded_continuous_function
open_locale topological_space ennreal nnreal bounded_continuous_function
namespace measure_theory
variables {α : Type*} [measurable_space α]
/-- Finite measures are defined as the subtype of measures that have the property of being finite
measures (i.e., their total mass is finite). -/
def finite_measure (α : Type*) [measurable_space α] : Type* :=
{μ : measure α // is_finite_measure μ}
namespace finite_measure
/-- A finite measure can be interpreted as a measure. -/
instance : has_coe (finite_measure α) (measure_theory.measure α) := coe_subtype
instance is_finite_measure (μ : finite_measure α) :
is_finite_measure (μ : measure α) := μ.prop
instance : has_coe_to_fun (finite_measure α) (λ _, set α → ℝ≥0) :=
⟨λ μ s, (μ s).to_nnreal⟩
lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : finite_measure α) :
(ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl
@[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : finite_measure α) (s : set α) :
(ν s : ℝ≥0∞) = (ν : measure α) s := ennreal.coe_to_nnreal (measure_lt_top ↑ν s).ne
@[simp] lemma val_eq_to_measure (ν : finite_measure α) : ν.val = (ν : measure α) := rfl
lemma coe_injective : function.injective (coe : finite_measure α → measure α) :=
subtype.coe_injective
/-- The (total) mass of a finite measure `μ` is `μ univ`, i.e., the cast to `nnreal` of
`(μ : measure α) univ`. -/
def mass (μ : finite_measure α) : ℝ≥0 := μ univ
@[simp] lemma ennreal_mass {μ : finite_measure α} :
(μ.mass : ℝ≥0∞) = (μ : measure α) univ := ennreal_coe_fn_eq_coe_fn_to_measure μ set.univ
instance has_zero : has_zero (finite_measure α) :=
{ zero := ⟨0, measure_theory.is_finite_measure_zero⟩ }
instance : inhabited (finite_measure α) := ⟨0⟩
instance : has_add (finite_measure α) :=
{ add := λ μ ν, ⟨μ + ν, measure_theory.is_finite_measure_add⟩ }
variables {R : Type*} [has_scalar R ℝ≥0] [has_scalar R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0∞]
[is_scalar_tower R ℝ≥0∞ ℝ≥0∞]
instance : has_scalar R (finite_measure α) :=
{ smul := λ (c : R) μ, ⟨c • μ, measure_theory.is_finite_measure_smul_of_nnreal_tower⟩, }
@[simp, norm_cast] lemma coe_zero : (coe : finite_measure α → measure α) 0 = 0 := rfl
@[simp, norm_cast] lemma coe_add (μ ν : finite_measure α) : ↑(μ + ν) = (↑μ + ↑ν : measure α) := rfl
@[simp, norm_cast] lemma coe_smul (c : R) (μ : finite_measure α) :
↑(c • μ) = (c • ↑μ : measure α) := rfl
@[simp, norm_cast] lemma coe_fn_zero :
(⇑(0 : finite_measure α) : set α → ℝ≥0) = (0 : set α → ℝ≥0) := by { funext, refl, }
@[simp, norm_cast] lemma coe_fn_add (μ ν : finite_measure α) :
(⇑(μ + ν) : set α → ℝ≥0) = (⇑μ + ⇑ν : set α → ℝ≥0) :=
by { funext, simp [← ennreal.coe_eq_coe], }
@[simp, norm_cast] lemma coe_fn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] (c : R) (μ : finite_measure α) :
(⇑(c • μ) : set α → ℝ≥0) = c • (⇑μ : set α → ℝ≥0) :=
by { funext, simp [← ennreal.coe_eq_coe, ennreal.coe_smul], }
instance : add_comm_monoid (finite_measure α) :=
finite_measure.coe_injective.add_comm_monoid coe coe_zero coe_add (λ _ _, coe_smul _ _)
/-- Coercion is an `add_monoid_hom`. -/
@[simps]
def coe_add_monoid_hom : finite_measure α →+ measure α :=
{ to_fun := coe, map_zero' := coe_zero, map_add' := coe_add }
instance {α : Type*} [measurable_space α] : module ℝ≥0 (finite_measure α) :=
function.injective.module _ coe_add_monoid_hom finite_measure.coe_injective coe_smul
variables [topological_space α]
/-- The pairing of a finite (Borel) measure `μ` with a nonnegative bounded continuous
function is obtained by (Lebesgue) integrating the (test) function against the measure.
This is `finite_measure.test_against_nn`. -/
def test_against_nn (μ : finite_measure α) (f : α →ᵇ ℝ≥0) : ℝ≥0 :=
(∫⁻ x, f x ∂(μ : measure α)).to_nnreal
lemma _root_.bounded_continuous_function.nnreal.to_ennreal_comp_measurable {α : Type*}
[topological_space α] [measurable_space α] [opens_measurable_space α] (f : α →ᵇ ℝ≥0) :
measurable (λ x, (f x : ℝ≥0∞)) :=
measurable_coe_nnreal_ennreal.comp f.continuous.measurable
lemma lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : finite_measure α) (f : α →ᵇ ℝ≥0) :
∫⁻ x, f x ∂(μ : measure α) < ∞ :=
begin
apply is_finite_measure.lintegral_lt_top_of_bounded_to_ennreal,
use nndist f 0,
intros x,
have key := bounded_continuous_function.nnreal.upper_bound f x,
rw ennreal.coe_le_coe,
have eq : nndist f 0 = ⟨dist f 0, dist_nonneg⟩,
{ ext,
simp only [real.coe_to_nnreal', max_eq_left_iff, subtype.coe_mk, coe_nndist], },
rwa eq at key,
end
@[simp] lemma test_against_nn_coe_eq {μ : finite_measure α} {f : α →ᵇ ℝ≥0} :
(μ.test_against_nn f : ℝ≥0∞) = ∫⁻ x, f x ∂(μ : measure α) :=
ennreal.coe_to_nnreal (lintegral_lt_top_of_bounded_continuous_to_nnreal μ f).ne
lemma test_against_nn_const (μ : finite_measure α) (c : ℝ≥0) :
μ.test_against_nn (bounded_continuous_function.const α c) = c * μ.mass :=
by simp [← ennreal.coe_eq_coe]
lemma test_against_nn_mono (μ : finite_measure α)
{f g : α →ᵇ ℝ≥0} (f_le_g : (f : α → ℝ≥0) ≤ g) :
μ.test_against_nn f ≤ μ.test_against_nn g :=
begin
simp only [←ennreal.coe_le_coe, test_against_nn_coe_eq],
apply lintegral_mono,
exact λ x, ennreal.coe_mono (f_le_g x),
end
variables [opens_measurable_space α]
lemma test_against_nn_add (μ : finite_measure α) (f₁ f₂ : α →ᵇ ℝ≥0) :
μ.test_against_nn (f₁ + f₂) = μ.test_against_nn f₁ + μ.test_against_nn f₂ :=
begin
simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_add, ennreal.coe_add,
pi.add_apply, test_against_nn_coe_eq],
apply lintegral_add;
exact bounded_continuous_function.nnreal.to_ennreal_comp_measurable _,
end
lemma test_against_nn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] [pseudo_metric_space R] [has_zero R]
[has_bounded_smul R ℝ≥0]
(μ : finite_measure α) (c : R) (f : α →ᵇ ℝ≥0) :
μ.test_against_nn (c • f) = c • μ.test_against_nn f :=
begin
simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_smul,
test_against_nn_coe_eq, ennreal.coe_smul],
simp_rw [←smul_one_smul ℝ≥0∞ c (f _ : ℝ≥0∞), ←smul_one_smul ℝ≥0∞ c (lintegral _ _ : ℝ≥0∞),
smul_eq_mul],
exact @lintegral_const_mul _ _ (μ : measure α) (c • 1) _
(bounded_continuous_function.nnreal.to_ennreal_comp_measurable f),
end
lemma test_against_nn_lipschitz_estimate (μ : finite_measure α) (f g : α →ᵇ ℝ≥0) :
μ.test_against_nn f ≤ μ.test_against_nn g + (nndist f g) * μ.mass :=
begin
simp only [←μ.test_against_nn_const (nndist f g), ←test_against_nn_add, ←ennreal.coe_le_coe,
bounded_continuous_function.coe_add, const_apply, ennreal.coe_add, pi.add_apply,
coe_nnreal_ennreal_nndist, test_against_nn_coe_eq],
apply lintegral_mono,
have le_dist : ∀ x, dist (f x) (g x) ≤ nndist f g :=
bounded_continuous_function.dist_coe_le_dist,
intros x,
have le' : f(x) ≤ g(x) + nndist f g,
{ apply (nnreal.le_add_nndist (f x) (g x)).trans,
rw add_le_add_iff_left,
exact dist_le_coe.mp (le_dist x), },
have le : (f(x) : ℝ≥0∞) ≤ (g(x) : ℝ≥0∞) + (nndist f g),
by { rw ←ennreal.coe_add, exact ennreal.coe_mono le', },
rwa [coe_nnreal_ennreal_nndist] at le,
end
lemma test_against_nn_lipschitz (μ : finite_measure α) :
lipschitz_with μ.mass (λ (f : α →ᵇ ℝ≥0), μ.test_against_nn f) :=
begin
rw lipschitz_with_iff_dist_le_mul,
intros f₁ f₂,
suffices : abs (μ.test_against_nn f₁ - μ.test_against_nn f₂ : ℝ) ≤ μ.mass * (dist f₁ f₂),
{ rwa nnreal.dist_eq, },
apply abs_le.mpr,
split,
{ have key' := μ.test_against_nn_lipschitz_estimate f₂ f₁,
rw mul_comm at key',
suffices : ↑(μ.test_against_nn f₂) ≤ ↑(μ.test_against_nn f₁) + ↑(μ.mass) * dist f₁ f₂,
{ linarith, },
have key := nnreal.coe_mono key',
rwa [nnreal.coe_add, nnreal.coe_mul, nndist_comm] at key, },
{ have key' := μ.test_against_nn_lipschitz_estimate f₁ f₂,
rw mul_comm at key',
suffices : ↑(μ.test_against_nn f₁) ≤ ↑(μ.test_against_nn f₂) + ↑(μ.mass) * dist f₁ f₂,
{ linarith, },
have key := nnreal.coe_mono key',
rwa [nnreal.coe_add, nnreal.coe_mul] at key, },
end
/-- Finite measures yield elements of the `weak_dual` of bounded continuous nonnegative
functions via `finite_measure.test_against_nn`, i.e., integration. -/
def to_weak_dual_bcnn (μ : finite_measure α) :
weak_dual ℝ≥0 (α →ᵇ ℝ≥0) :=
{ to_fun := λ f, μ.test_against_nn f,
map_add' := test_against_nn_add μ,
map_smul' := test_against_nn_smul μ,
cont := μ.test_against_nn_lipschitz.continuous, }
@[simp] lemma coe_to_weak_dual_bcnn (μ : finite_measure α) :
⇑μ.to_weak_dual_bcnn = μ.test_against_nn := rfl
@[simp] lemma to_weak_dual_bcnn_apply (μ : finite_measure α) (f : α →ᵇ ℝ≥0) :
μ.to_weak_dual_bcnn f = (∫⁻ x, f x ∂(μ : measure α)).to_nnreal := rfl
/-- The topology of weak convergence on `finite_measures α` is inherited (induced) from the weak-*
topology on `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)` via the function `finite_measures.to_weak_dual_bcnn`. -/
instance : topological_space (finite_measure α) :=
topological_space.induced to_weak_dual_bcnn infer_instance
lemma to_weak_dual_bcnn_continuous :
continuous (@finite_measure.to_weak_dual_bcnn α _ _ _) :=
continuous_induced_dom
/- Integration of (nonnegative bounded continuous) test functions against finite Borel measures
depends continuously on the measure. -/
lemma continuous_test_against_nn_eval (f : α →ᵇ ℝ≥0) :
continuous (λ (μ : finite_measure α), μ.test_against_nn f) :=
(by apply (weak_bilin.eval_continuous _ _).comp to_weak_dual_bcnn_continuous :
continuous ((λ φ : weak_dual ℝ≥0 (α →ᵇ ℝ≥0), φ f) ∘ to_weak_dual_bcnn))
lemma tendsto_iff_weak_star_tendsto {γ : Type*} {F : filter γ}
{μs : γ → finite_measure α} {μ : finite_measure α} :
tendsto μs F (𝓝 μ) ↔ tendsto (λ i, (μs(i)).to_weak_dual_bcnn) F (𝓝 μ.to_weak_dual_bcnn) :=
inducing.tendsto_nhds_iff ⟨rfl⟩
theorem tendsto_iff_forall_test_against_nn_tendsto {γ : Type*} {F : filter γ}
{μs : γ → finite_measure α} {μ : finite_measure α} :
tendsto μs F (𝓝 μ) ↔
∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (μs(i)).to_weak_dual_bcnn f) F (𝓝 (μ.to_weak_dual_bcnn f)) :=
by { rw [tendsto_iff_weak_star_tendsto, tendsto_iff_forall_eval_tendsto_top_dual_pairing], refl, }
theorem tendsto_iff_forall_lintegral_tendsto {γ : Type*} {F : filter γ}
{μs : γ → finite_measure α} {μ : finite_measure α} :
tendsto μs F (𝓝 μ) ↔
∀ (f : α →ᵇ ℝ≥0),
tendsto (λ i, (∫⁻ x, (f x) ∂(μs(i) : measure α))) F (𝓝 ((∫⁻ x, (f x) ∂(μ : measure α)))) :=
begin
rw tendsto_iff_forall_test_against_nn_tendsto,
simp_rw [to_weak_dual_bcnn_apply _ _, ←test_against_nn_coe_eq,
ennreal.tendsto_coe, ennreal.to_nnreal_coe],
end
end finite_measure
/-- Probability measures are defined as the subtype of measures that have the property of being
probability measures (i.e., their total mass is one). -/
def probability_measure (α : Type*) [measurable_space α] : Type* :=
{μ : measure α // is_probability_measure μ}
namespace probability_measure
instance [inhabited α] : inhabited (probability_measure α) :=
⟨⟨measure.dirac default, measure.dirac.is_probability_measure⟩⟩
/-- A probability measure can be interpreted as a measure. -/
instance : has_coe (probability_measure α) (measure_theory.measure α) := coe_subtype
instance : has_coe_to_fun (probability_measure α) (λ _, set α → ℝ≥0) :=
⟨λ μ s, (μ s).to_nnreal⟩
instance (μ : probability_measure α) : is_probability_measure (μ : measure α) := μ.prop
lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : probability_measure α) :
(ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl
@[simp] lemma val_eq_to_measure (ν : probability_measure α) : ν.val = (ν : measure α) := rfl
lemma coe_injective : function.injective (coe : probability_measure α → measure α) :=
subtype.coe_injective
@[simp] lemma coe_fn_univ (ν : probability_measure α) : ν univ = 1 :=
congr_arg ennreal.to_nnreal ν.prop.measure_univ
/-- A probability measure can be interpreted as a finite measure. -/
def to_finite_measure (μ : probability_measure α) : finite_measure α := ⟨μ, infer_instance⟩
@[simp] lemma coe_comp_to_finite_measure_eq_coe (ν : probability_measure α) :
(ν.to_finite_measure : measure α) = (ν : measure α) := rfl
@[simp] lemma coe_fn_comp_to_finite_measure_eq_coe_fn (ν : probability_measure α) :
(ν.to_finite_measure : set α → ℝ≥0) = (ν : set α → ℝ≥0) := rfl
@[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : probability_measure α) (s : set α) :
(ν s : ℝ≥0∞) = (ν : measure α) s :=
by { rw [← coe_fn_comp_to_finite_measure_eq_coe_fn,
finite_measure.ennreal_coe_fn_eq_coe_fn_to_measure], refl, }
@[simp] lemma mass_to_finite_measure (μ : probability_measure α) :
μ.to_finite_measure.mass = 1 := μ.coe_fn_univ
variables [topological_space α]
lemma lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : probability_measure α) (f : α →ᵇ ℝ≥0) :
∫⁻ x, f x ∂(μ : measure α) < ∞ :=
μ.to_finite_measure.lintegral_lt_top_of_bounded_continuous_to_nnreal f
variables [opens_measurable_space α]
lemma test_against_nn_lipschitz (μ : probability_measure α) :
lipschitz_with 1 (λ (f : α →ᵇ ℝ≥0), μ.to_finite_measure.test_against_nn f) :=
μ.mass_to_finite_measure ▸ μ.to_finite_measure.test_against_nn_lipschitz
/-- The topology of weak convergence on `probability_measures α`. This is inherited (induced) from
the weak-* topology on `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)` via the function
`probability_measures.to_weak_dual_bcnn`. -/
instance : topological_space (probability_measure α) :=
topological_space.induced to_finite_measure infer_instance
lemma to_finite_measure_continuous :
continuous (to_finite_measure : probability_measure α → finite_measure α) :=
continuous_induced_dom
/-- Probability measures yield elements of the `weak_dual` of bounded continuous nonnegative
functions via `finite_measure.test_against_nn`, i.e., integration. -/
def to_weak_dual_bcnn : probability_measure α → weak_dual ℝ≥0 (α →ᵇ ℝ≥0) :=
finite_measure.to_weak_dual_bcnn ∘ to_finite_measure
@[simp] lemma coe_to_weak_dual_bcnn (μ : probability_measure α) :
⇑μ.to_weak_dual_bcnn = μ.to_finite_measure.test_against_nn := rfl
@[simp] lemma to_weak_dual_bcnn_apply (μ : probability_measure α) (f : α →ᵇ ℝ≥0) :
μ.to_weak_dual_bcnn f = (∫⁻ x, f x ∂(μ : measure α)).to_nnreal := rfl
lemma to_weak_dual_bcnn_continuous :
continuous (λ (μ : probability_measure α), μ.to_weak_dual_bcnn) :=
finite_measure.to_weak_dual_bcnn_continuous.comp to_finite_measure_continuous
/- Integration of (nonnegative bounded continuous) test functions against Borel probability
measures depends continuously on the measure. -/
lemma continuous_test_against_nn_eval (f : α →ᵇ ℝ≥0) :
continuous (λ (μ : probability_measure α), μ.to_finite_measure.test_against_nn f) :=
(finite_measure.continuous_test_against_nn_eval f).comp to_finite_measure_continuous
/- The canonical mapping from probability measures to finite measures is an embedding. -/
lemma to_finite_measure_embedding (α : Type*)
[measurable_space α] [topological_space α] [opens_measurable_space α] :
embedding (to_finite_measure : probability_measure α → finite_measure α) :=
{ induced := rfl,
inj := λ μ ν h, subtype.eq (by convert congr_arg coe h) }
lemma tendsto_nhds_iff_to_finite_measures_tendsto_nhds {δ : Type*}
(F : filter δ) {μs : δ → probability_measure α} {μ₀ : probability_measure α} :
tendsto μs F (𝓝 μ₀) ↔ tendsto (to_finite_measure ∘ μs) F (𝓝 (μ₀.to_finite_measure)) :=
embedding.tendsto_nhds_iff (probability_measure.to_finite_measure_embedding α)
/-- The usual definition of weak convergence of probability measures is given in terms of sequences
of probability measures: it is the requirement that the integrals of all continuous bounded
functions against members of the sequence converge. This version is a characterization using
nonnegative bounded continuous functions. -/
theorem tendsto_iff_forall_lintegral_tendsto {γ : Type*} {F : filter γ}
{μs : γ → probability_measure α} {μ : probability_measure α} :
tendsto μs F (𝓝 μ) ↔
∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (∫⁻ x, (f x) ∂(μs(i) : measure α))) F
(𝓝 ((∫⁻ x, (f x) ∂(μ : measure α)))) :=
begin
rw tendsto_nhds_iff_to_finite_measures_tendsto_nhds,
exact finite_measure.tendsto_iff_forall_lintegral_tendsto,
end
end probability_measure
end measure_theory
|
13729cc1d4c84398150d70e8d69b1a9ce22d99fb | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/coe1.lean | fa47b9aa9f66234f683894945fb4ca343845522f | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 905 | lean | namespace hidden
#check if tt then "a" else "b"
/- Remark: in the standard library nat_to_int and int_to_real are has_lift instances
instead of has_coe. -/
constant int : Type
constant real : Type
constant nat_to_int : has_coe nat int
constant int_to_real : has_coe int real
attribute [instance] nat_to_int int_to_real
constant sine : real → real
constants n m : nat
constants i j : int
constants x y : real
#check sine x
#check sine n
#check sine i
constant int_has_add : has_add int
constant real_has_add : has_add real
attribute [instance] int_has_add real_has_add
#check x + i
/- The following one does not work because the implicit argument ?A of add is bound to int
when x is processed. -/
#check i + x -- FAIL
/- The workaround is to use the explicit lift -/
#check ↑i + x
#check x + n
#check n + x -- FAIL
#check ↑n + x
#check (i:real) + x
#check (n:real) + x
end hidden
|
ca18b7cf3b4b8ad94a3fc7494fa6864db7f776ff | 437dc96105f48409c3981d46fb48e57c9ac3a3e4 | /src/field_theory/finite.lean | 18d4b4b00d4ab902288acae373bca82d41f83f2e | [
"Apache-2.0"
] | permissive | dan-c-k/mathlib | 08efec79bd7481ee6da9cc44c24a653bff4fbe0d | 96efc220f6225bc7a5ed8349900391a33a38cc56 | refs/heads/master | 1,658,082,847,093 | 1,589,013,201,000 | 1,589,013,201,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,138 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Joey van Langen, Casper Putz
-/
import data.polynomial
import data.equiv.ring
import data.zmod.basic
import group_theory.order_of_element
import linear_algebra.basis
universes u v
variables {K : Type u} {R : Type v}
open function finset polynomial nat
section
variables [integral_domain R] (S : set (units R)) [is_subgroup S] [fintype S]
lemma card_nth_roots_subgroup_units [decidable_eq R] {n : ℕ} (hn : 0 < n) (a : S) :
(univ.filter (λ b : S, b ^ n = a)).card ≤ (nth_roots n ((a : units R) : R)).card :=
card_le_card_of_inj_on (λ a, ((a : units R) : R))
(by simp [mem_nth_roots hn, (units.coe_pow _ _).symm, -units.coe_pow, units.ext_iff.symm, subtype.coe_ext])
(by simp [units.ext_iff.symm, subtype.coe_ext.symm])
instance subgroup_units_cyclic : is_cyclic S :=
by haveI := classical.dec_eq R; exact
is_cyclic_of_card_pow_eq_one_le
(λ n hn, le_trans (card_nth_roots_subgroup_units S hn 1) (card_nth_roots _ _))
end
namespace finite_field
def field_of_integral_domain [fintype R] [decidable_eq R] [integral_domain R] :
field R :=
{ inv := λ a, if h : a = 0 then 0
else fintype.bij_inv (show function.bijective (* a),
from fintype.injective_iff_bijective.1 $ λ _ _, (domain.mul_right_inj h).1) 1,
mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by rw [dif_neg ha, mul_comm];
exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1,
inv_zero := dif_pos rfl,
..show integral_domain R, by apply_instance }
section polynomial
variables [fintype R] [integral_domain R]
open finset polynomial
/-- The cardinality of a field is at most n times the cardinality of the image of a degree n
polynomial -/
lemma card_image_polynomial_eval [decidable_eq R] {p : polynomial R} (hp : 0 < p.degree) :
fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card :=
finset.card_le_mul_card_image _ _
(λ a _, calc _ = (p - C a).roots.card : congr_arg card
(by simp [finset.ext, mem_roots_sub_C hp, -sub_eq_add_neg])
... ≤ _ : card_roots_sub_C' hp)
/-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/
lemma exists_root_sum_quadratic {f g : polynomial R} (hf2 : degree f = 2)
(hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 :=
by letI := classical.dec_eq R; exact
suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))),
begin
simp only [disjoint_left, mem_image] at this,
push_neg at this,
rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩,
exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩
end,
assume hd : disjoint _ _,
lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $
calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card
≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_of_subset (subset_univ _))
... = fintype.card R + fintype.card R : two_mul _
... < nat_degree f * (univ.image (λ x : R, eval x f)).card +
nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card :
add_lt_add_of_lt_of_le
(lt_of_le_of_ne
(card_image_polynomial_eval (by rw hf2; exact dec_trivial))
(mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR])))
(card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial))
... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card :
by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2,
nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add]
end polynomial
section
variables [field K] [fintype K]
lemma card_units : fintype.card (units K) = fintype.card K - 1 :=
begin
classical,
rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : K)⟩)],
haveI := set_fintype {a : K | a ≠ 0},
haveI := set_fintype (@set.univ K),
rw [fintype.card_congr (equiv.units_equiv_ne_zero _),
← @set.card_insert _ _ {a : K | a ≠ 0} _ (not_not.2 (eq.refl (0 : K)))
(set.fintype_insert _ _), fintype.card_congr (equiv.set.univ K).symm],
congr; simp [set.ext_iff, classical.em]
end
instance : is_cyclic (units K) :=
by haveI := classical.dec_eq K;
haveI := set_fintype (@set.univ (units K)); exact
let ⟨g, hg⟩ := is_cyclic.exists_generator (@set.univ (units K)) in
⟨⟨g, λ x, let ⟨n, hn⟩ := hg ⟨x, trivial⟩ in ⟨n, by rw [← is_subgroup.coe_gpow, hn]; refl⟩⟩⟩
lemma prod_univ_units_id_eq_neg_one :
univ.prod (λ x, x) = (-1 : units K) :=
begin
classical,
have : ((@univ (units K) _).erase (-1)).prod (λ x, x) = 1,
from prod_involution (λ x _, x⁻¹) (by simp)
(λ a, by simp [units.inv_eq_self_iff] {contextual := tt})
(λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt})
(by simp),
rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _),
this, mul_one]
end
theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) :=
begin
haveI hp : fact p.prime := char_p.char_is_prime K p,
have V : vector_space (zmod p) K, from { .. (zmod.cast_hom p K).to_module },
obtain ⟨n, h⟩ := @vector_space.card_fintype _ _ _ _ V _ _,
rw zmod.card at h,
refine ⟨⟨n, _⟩, hp, h⟩,
apply or.resolve_left (nat.eq_zero_or_pos n),
rintro rfl,
rw nat.pow_zero at h,
have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) },
exact absurd this zero_ne_one,
end
theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) :=
let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩
end
lemma pow_card_sub_one_eq_one [field K] [fintype K] (a : K) (ha : a ≠ 0) :
a ^ (fintype.card K - 1) = 1 :=
calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) :
by rw [units.coe_pow, units.coe_mk0]
... = 1 : by { classical, rw [← card_units, pow_card_eq_one], refl }
end finite_field
namespace zmod
open finite_field
lemma sum_two_squares (p : ℕ) [hp : fact p.prime] (x : zmod p) :
∃ a b : zmod p, a^2 + b^2 = x :=
begin
cases hp.eq_two_or_odd with hp2 hp_odd,
{ unfreezeI, subst p, revert x, exact dec_trivial },
let f : polynomial (zmod p) := X^2,
let g : polynomial (zmod p) := X^2 - C x,
obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 :=
@exists_root_sum_quadratic _ _ _ f g
(degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]),
refine ⟨a, b, _⟩,
rw ← sub_eq_zero,
simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab,
end
end zmod
namespace char_p
lemma sum_two_squares (R : Type*) [integral_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) :
∃ a b : ℕ, (a^2 + b^2 : R) = x :=
begin
haveI := char_is_prime_of_pos R p,
obtain ⟨a, b, hab⟩ := zmod.sum_two_squares p x,
refine ⟨a.val, b.val, _⟩,
have := congr_arg (zmod.cast_hom p R) hab,
simpa only [zmod.cast_int_cast, zmod.cast_hom_apply, zmod.cast_add,
zmod.nat_cast_val, _root_.pow_two, zmod.cast_mul]
end
end char_p
open_locale nat
open zmod
/-- The Fermat-Euler totient theorem. `nat.modeq.pow_totient` is an alternative statement
of the same theorem. -/
@[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 :=
by rw [← card_units_eq_totient, pow_card_eq_one]
/-- The Fermat-Euler totient theorem. `zmod.pow_totient` is an alternative statement
of the same theorem. -/
lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] :=
begin
cases n, {simp},
rw ← zmod.eq_iff_modeq_nat,
let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h,
have := zmod.pow_totient x',
apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this,
simpa only [-zmod.pow_totient, succ_eq_add_one, cast_pow, units.coe_one,
nat.cast_one, cast_unit_of_coprime, units.coe_pow],
end
|
4c040e0e8381167936841403c24d9139477d10ae | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/meta/tagged_format.lean | 22af61985df0f5017cdb48b484fe76aabea57428 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 2,518 | lean | /-
Copyright (c) E.W.Ayers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: E.W.Ayers
-/
prelude
import init.meta.tactic
import init.meta.expr_address
import init.control
universe u
/-- An alternative to format that keeps structural information stored as a tag. -/
meta inductive tagged_format (α : Type u)
| tag : α → tagged_format → tagged_format
| compose : tagged_format → tagged_format → tagged_format
| group : tagged_format → tagged_format
| nest : nat → tagged_format → tagged_format
| highlight : format.color → tagged_format → tagged_format
| of_format : format → tagged_format
namespace tagged_format
variables {α β : Type u}
protected meta def map (f : α → β) : tagged_format α → tagged_format β
| (compose x y) := compose (map x) (map y)
| (group x) := group $ map x
| (nest i x) := nest i $ map x
| (highlight c x) := highlight c $ map x
| (of_format x) := of_format x
| (tag a x) := tag (f a) (map x)
meta instance is_functor: functor tagged_format :=
{ map := @tagged_format.map }
meta def m_untag {t : Type → Type} [monad t] (f : α → format → t format) : tagged_format α → t format
| (compose x y) := pure format.compose <*> m_untag x <*> m_untag y
| (group x) := pure format.group <*> m_untag x
| (nest i x) := pure (format.nest i) <*> m_untag x
| (highlight c x) := pure format.highlight <*> m_untag x <*> pure c
| (of_format x) := pure $ x
| (tag a x) := m_untag x >>= f a
meta def untag (f : α → format → format) : tagged_format α → format :=
@m_untag _ id _ f
meta instance has_to_fmt : has_to_format (tagged_format α) :=
⟨tagged_format.untag (λ a f, f)⟩
end tagged_format
/-- tagged_format with information about subexpressions. -/
meta def eformat := tagged_format (expr.address × expr)
/-- A special version of pp which also preserves expression boundary information.
On a tag ⟨e,a⟩, note that the given expr `e` is _not_ necessarily the subexpression of the root
expression that `tactic_state.pp_tagged` was called with. For example if the subexpression is
under a binder then all of the `expr.var 0`s will be replaced with a local constant not in
the local context with the name and type set to that of the binder.-/
meta constant tactic_state.pp_tagged : tactic_state → expr → eformat
meta def tactic.pp_tagged : expr → tactic eformat
| e := tactic.read >>= λ ts, pure $ tactic_state.pp_tagged ts e
|
b3f722555730879507fb64f7b4226318d0212fd5 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /data/finset.lean | 729216321bdca6e8a8cd03232b5a98e1ce00370c | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 60,377 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro
Finite sets.
-/
import logic.embedding order.boolean_algebra algebra.order_functions
data.multiset data.sigma.basic data.set.lattice
open multiset subtype nat lattice
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `finset α` is the type of finite sets of elements of `α`. It is implemented
as a multiset (a list up to permutation) which has no duplicate elements. -/
structure finset (α : Type*) :=
(val : multiset α)
(nodup : nodup val)
namespace finset
theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t
| ⟨s, _⟩ ⟨t, _⟩ h := by congr; assumption
@[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t :=
⟨eq_of_veq, congr_arg _⟩
@[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 :=
erase_dup_eq_self.2 s.2
end finset
namespace finset
instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α)
| s₁ s₂ := decidable_of_iff _ val_inj
/- membership -/
instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩
theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl
@[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl
instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) :=
multiset.decidable_mem _ _
/- set coercion -/
/-- Convert a finset to a set in the natural way. -/
def to_set (s : finset α) : set α := {x | x ∈ s}
instance : has_lift (finset α) (set α) := ⟨to_set⟩
@[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl
@[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl
/- extensionality -/
theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ :=
val_inj.symm.trans $ nodup_ext s₁.2 s₂.2
@[extensionality]
theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ :=
ext.2
@[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ :=
(set.ext_iff _ _).trans ext.symm
/- subset -/
instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩
theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl
@[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _
theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans
theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset
theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩
theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl
@[simp] theorem coe_subset {s₁ s₂ : finset α} :
(↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2
instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩
instance : partial_order (finset α) :=
{ le := (⊆),
lt := (⊂),
le_refl := subset.refl,
le_trans := @subset.trans _,
le_antisymm := @subset.antisymm _ }
@[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl
@[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ :=
show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁,
by simp [set.ssubset_iff_subset_not_subset] {contextual := tt}
@[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ :=
and_congr val_le_iff $ not_congr val_le_iff
/- empty -/
protected def empty : finset α := ⟨0, nodup_zero⟩
instance : has_emptyc (finset α) := ⟨finset.empty⟩
instance : inhabited (finset α) := ⟨∅⟩
@[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl
@[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id
@[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅
| e := not_mem_empty a $ e ▸ h
@[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _
theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ :=
eq_of_veq (eq_zero_of_forall_not_mem H)
lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s :=
⟨λ h, by simp [h], λ h, eq_empty_of_forall_not_mem h⟩
@[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅
theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero
theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s :=
exists_mem_of_ne_zero (mt val_eq_zero.1 h)
@[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) :=
by simp [set.ext_iff]
/-- `singleton a` is the set `{a}` containing `a` and nothing else. -/
def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩
local prefix `ι`:90 := singleton
@[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a :=
by simp [singleton]
theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := by simp
theorem mem_singleton_self (a : α) : a ∈ ι a := by simp
theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b :=
⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩
@[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _)
@[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) :=
by simp [set.ext_iff]
/- insert -/
section decidable_eq
variables [decidable_eq α]
/-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/
instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩
@[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl
theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl
@[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl
theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) :=
by simp [erase_dup_cons]
theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 :=
by rw [insert_val, ndinsert_of_not_mem h]
@[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert
theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := by simp
theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := by simp *
theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s :=
(mem_insert.1 h).resolve_left
@[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) :=
by simp [set.ext_iff]
@[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s :=
eq_of_veq $ ndinsert_of_mem h
@[simp] theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) :=
ext.2 $ by simp [or.left_comm]
@[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s :=
ext.2 $ by simp
@[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ :=
ne_empty_of_mem (mem_insert_self a s)
theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp [subset_iff, or_imp_distrib, forall_and_distrib]
theorem subset_insert [h : decidable_eq α] (a : α) (s : finset α) : s ⊆ insert a s :=
λ b, mem_insert_of_mem
theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t :=
insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩
lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) :=
iff.intro
(assume ⟨h₁, h₂⟩,
have ∃a, a ∈ t ∧ a ∉ s, by simpa [finset.subset_iff, classical.not_forall] using h₂,
let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩)
(assume ⟨a, hat, has⟩,
let ⟨h₁, h₂⟩ := insert_subset.mp has in
⟨h₂, assume h, hat $ h h₁⟩)
lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s :=
ssubset_iff.mpr ⟨a, h, subset.refl _⟩
@[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α]
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s
| ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin
cases nodup_cons.1 nd with m nd',
rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)],
{ exact h₂ (by exact m) (IH nd') },
{ rw [insert_val, ndinsert_of_not_mem m] }
end) nd
@[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α]
(s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s :=
finset.induction h₁ h₂ s
@[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl
@[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl
@[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a :=
by simp [singleton]
/- union -/
/-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/
instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩
theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl
@[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 :=
ndunion_eq_union s₁.2
@[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion
theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := by simp *
theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := by simp *
theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ :=
by simp [not_or_distrib]
@[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := by simp [set.ext_iff]
theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ :=
val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩)
theorem subset_union_left {s₁ s₂ : finset α} : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _
theorem subset_union_right {s₁ s₂ : finset α} : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _
@[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := by simp [ext, or_comm]
instance : is_commutative (finset α) (∪) := ⟨union_comm⟩
@[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
by simp [ext, or_comm, or.left_comm]
instance : is_associative (finset α) (∪) := ⟨union_assoc⟩
@[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ by simp
instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩
theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) :=
ext.2 $ by simp [or_comm, or.left_comm]
theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by simp
@[simp] theorem union_self (s : finset α) : s ∪ s = s := by simp
@[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := by simp [ext]
@[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := by simp [ext]
theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := by simp [ext, or_comm, or.left_comm]
@[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) :=
by simp [ext, or_comm, or.left_comm]
@[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) :=
by simp [ext, or.left_comm]
theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t :=
by simp [ext]
/- inter -/
/-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/
instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩
theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl
@[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 :=
ndinter_eq_inter s₁.2
@[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter
theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1
theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2
theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ :=
and_imp.1 mem_inter.2
theorem inter_subset_left {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left
theorem inter_subset_right {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right
theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ :=
by simp [subset_iff] {contextual:=tt}; finish
@[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := by simp [set.ext_iff]
@[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ by simp [and_comm]
@[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ by simp [and_comm, and.left_comm]
@[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ by simp [and.left_comm]
@[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ by simp [and.left_comm]
@[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ by simp
@[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ by simp
@[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ by simp
@[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) :
insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) :=
ext.2 $ by simp; intro x; constructor; finish
@[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) :
s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) :=
by rw [inter_comm, insert_inter_of_mem h, inter_comm]
@[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) :
insert a s₁ ∩ s₂ = s₁ ∩ s₂ :=
ext.2 $ assume a', by by_cases h' : a' = a; simp [mem_inter, mem_insert, h, h', and_comm]
@[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) :
s₁ ∩ insert a s₂ = s₁ ∩ s₂ :=
by rw [inter_comm, insert_inter_of_not_mem h, inter_comm]
@[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} : a ∈ s → ι a ∩ s = ι a :=
show a ∈ s → insert a ∅ ∩ s = insert a ∅, by simp {contextual := tt}
@[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} : a ∉ s → ι a ∩ s = ∅ :=
show a ∉ s → insert a ∅ ∩ s = ∅, by simp {contextual := tt}
@[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a :=
by rw [inter_comm, singleton_inter_of_mem h]
@[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ :=
by rw [inter_comm, singleton_inter_of_not_mem h]
/- lattice laws -/
instance : lattice (finset α) :=
{ sup := (∪),
sup_le := assume a b c, union_subset,
le_sup_left := assume a b, subset_union_left,
le_sup_right := assume a b, subset_union_right,
inf := (∩),
le_inf := assume a b c, subset_inter,
inf_le_left := assume a b, inter_subset_left,
inf_le_right := assume a b, inter_subset_right,
..finset.partial_order }
@[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl
@[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl
instance : semilattice_inf_bot (finset α) :=
{ bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice }
instance : distrib_lattice (finset α) :=
{ le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c,
by simp [subset_iff, and_imp, or_imp_distrib] {contextual:=tt},
..finset.lattice.lattice }
theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) :=
ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish
theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) :=
ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish
theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) :=
ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish
theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) :=
ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish
/- erase -/
/-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are
not equal to `a`. -/
def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩
@[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl
@[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s :=
mem_erase_iff_of_nodup s.2
theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := by simp
@[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl
theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp {contextual:=tt}
theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase
theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp {contextual:=tt}
theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s :=
ext.2 $ assume x, by simp; constructor; finish
theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s :=
ext.2 $ assume x, by simp; constructor; finish
theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a :=
val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h
theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _
@[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) :=
by simp [set.ext_iff, and_comm]
lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s :=
calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h
theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h
theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t :=
by simp [subset_iff, or_iff_not_imp_left]; exact forall_congr (λ x, forall_swap)
theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s :=
subset_insert_iff.1 $ subset.refl _
theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) :=
subset_insert_iff.2 $ subset.refl _
/- sdiff -/
/-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/
instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩
@[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} :
a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2
@[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ :=
ext.2 $ λ a, by simpa [or_and_distrib_left, dec_em] using or_iff_right_of_imp (@h a)
@[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ :=
(union_comm _ _).trans (sdiff_union_of_subset h)
@[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ :=
ext.2 $ by simp {contextual := tt}
@[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ :=
by simp
theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ :=
by simpa [subset_iff] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂)
@[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) :=
by simp [set.ext_iff]
end decidable_eq
/- attach -/
/-- `attach s` takes the elements of `s` and forms a new set of elements of the
subtype `{x // x ∈ s}`. -/
def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩
@[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl
@[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _
@[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl
section decidable_pi_exists
variables {s : finset α}
instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∀a (h : a ∈ s), p a h) :=
multiset.decidable_dforall_multiset
/-- decidable equality for functions whose domain is bounded by finsets -/
instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] :
decidable_eq (Πa∈s, β a) :=
multiset.decidable_eq_pi_multiset
instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∃a (h : a ∈ s), p a h) :=
multiset.decidable_dexists_multiset
end decidable_pi_exists
/- filter -/
section filter
variables {p q : α → Prop} [decidable_pred p] [decidable_pred q]
/-- `filter p s` is the set of elements of `s` that satisfy `p`. -/
def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α :=
⟨_, nodup_filter p s.2⟩
@[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl
@[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter
@[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _
theorem filter_filter (s : finset α) :
(s.filter p).filter q = s.filter (λa, p a ∧ q a) :=
ext.2 $ assume a, by simp [and_comm, and.left_comm]
@[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ :=
ext.2 $ assume a, by simp
lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s :=
eq_of_veq $ filter_congr H
variable [decidable_eq α]
theorem filter_union (s₁ s₂ : finset α) :
(s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=
ext.2 $ by simp [or_and_distrib_right]
theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
ext.2 $ by simp [and_or_distrib_left]
theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q :=
ext.2 $ by simp [and_comm, and.left_comm]
theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p :=
ext.2 $ by simpa [and_comm] using λ a, and_congr_right $
λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm
theorem sdiff_eq_filter (s₁ s₂ : finset α) :
s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ by simp
theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s :=
by simp [filter_not]
theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ :=
by simp [filter_not]
@[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) :=
by simp [set.ext_iff]
end filter
/- range -/
section range
variables {n m l : ℕ}
/-- `range n` is the set of integers less than `n`. -/
def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩
@[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl
@[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range
@[simp] theorem range_zero : range 0 = ∅ := rfl
@[simp] theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ by simp
@[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self
@[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset
theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n :=
finset.induction_on s ⟨0, by simp⟩ $ λ a s ha ⟨n, hn⟩,
⟨max (a + 1) n, insert_subset.2
⟨by simpa using le_max_left (a+1) n, subset.trans hn (by simp [le_max_right])⟩⟩
end range
/- useful rules for calculations with quantifiers -/
theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false :=
by simp
theorem exists_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) :=
by simp [or_and_distrib_right, exists_or_distrib]
theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true :=
by simp
theorem forall_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) :=
by simp [or_imp_distrib, forall_and_distrib]
end finset
namespace option
/-- Construct an empty or singleton finset from an `option` -/
def to_finset (o : option α) : finset α :=
match o with
| none := ∅
| some a := finset.singleton a
end
@[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl
@[simp] theorem to_finset_some {a : α} : (some a).to_finset = finset.singleton a := rfl
@[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o :=
by cases o; simp [eq_comm]
end option
/- erase_dup on list and multiset -/
namespace multiset
variable [decidable_eq α]
/-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/
def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩
@[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl
theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset :=
finset.val_inj.1 (erase_dup_eq_self.2 n).symm
@[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s :=
mem_erase_dup
@[simp] lemma to_finset_cons (a : α) (s : multiset α) :
to_finset (a :: s) = insert a (to_finset s) :=
finset.eq_of_veq erase_dup_cons
end multiset
namespace list
variable [decidable_eq α]
/-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/
def to_finset (l : list α) : finset α := multiset.to_finset l
@[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl
theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset :=
multiset.to_finset_eq n
@[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l :=
mem_erase_dup
@[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ :=
rfl
@[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) :=
finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h]
end list
namespace finset
section map
open function
def map (f : α ↪ β) (s : finset α) : finset β :=
⟨s.1.map f, nodup_map f.2 s.2⟩
@[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl
@[simp] theorem map_empty (f : α ↪ β) (s : finset α) : (∅ : finset α).map f = ∅ := rfl
variables {f : α ↪ β} {s : finset α}
@[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := by simp [mem_def]
@[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.map f :=
mem_map.2 ⟨_, h, rfl⟩
theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} :
s.to_finset.map f = (s.map f).to_finset := ext.2 $ by simp
theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ by simp [embedding.refl]
theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) :=
eq_of_veq $ by simp [erase_dup_map_erase_dup_eq]
theorem map_subset_map {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.map f ⊆ s₂.map f :=
by simp [subset_def, map_subset_map h]
theorem map_filter {p : β → Prop} [decidable_pred p] :
(s.map f).filter p = (s.filter (p ∘ f)).map f :=
ext.2 $ λ b, by simp; rw ← exists_and_distrib_right;
refine exists_congr (λ a, (and_congr_right $ λ e, _).trans and.right_comm);
simp [e.2.symm]
theorem map_union [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f :=
ext.2 $ by simp [mem_map, or_and_distrib_right, exists_or_distrib]
theorem map_inter [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f :=
ext.2 $ by simp [mem_map]; exact λ b,
⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩,
λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, f.2 (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩.
@[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) :=
ext.2 $ by simp [mem_map, eq_comm]
@[simp] theorem map_insert [decidable_eq α] [decidable_eq β]
(f : α ↪ β) (a : α) (s : finset α) :
(insert a s).map f = insert (f a) (s.map f) :=
by simp [insert_eq, map_union]
@[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s :=
eq_of_veq $ by simp [embedding.subtype]; rw attach_val; simp [multiset.attach_map_val]
end map
section image
variables [decidable_eq β]
/-- `image f s` is the forward image of `s` under `f`. -/
def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset
@[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl
@[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl
variables {f : α → β} {s : finset α}
@[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp [mem_def]
@[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f :=
mem_image.2 ⟨_, h, rfl⟩
@[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := by simp [set.ext_iff]
theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ by simp
@[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f :=
multiset.erase_dup_eq_self.2 (nodup_map_on H s.2)
theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ by simp
theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) :=
eq_of_veq $ by simp [erase_dup_map_erase_dup_eq]
theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f :=
by simp [subset_def, multiset.map_subset_map h]
theorem image_filter {p : β → Prop} [decidable_pred p] :
(s.image f).filter p = (s.filter (p ∘ f)).image f :=
ext.2 $ λ b, by simp [and_comm]; rw ← exists_and_distrib_left; exact
exists_congr (λ a, and.left_comm.trans $ and_congr_right $ λ e, by simp [e.symm])
theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f :=
ext.2 $ by simp [mem_image, or_and_distrib_right, exists_or_distrib]
theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f :=
ext.2 $ by simp [mem_image]; exact λ b,
⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩,
λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩.
@[simp] theorem image_singleton [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (f a) :=
ext.2 $ by simp [mem_image, eq_comm]
@[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) :
(insert a s).image f = insert (f a) (s.image f) :=
by simp [insert_eq, image_union]
@[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s :=
eq_of_veq $ by simp [multiset.attach_map_val]
@[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} :
attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s})
((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) :=
begin
apply eq_of_veq,
dsimp,
rw [attach_ndinsert, multiset.erase_dup_eq_self.2],
{ refl },
apply nodup_map_on,
exact assume ⟨a', _⟩ _ ⟨b', _⟩ _ h, by simp at h; simp [h],
exact multiset.nodup_attach.2 s.2
end
theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f :=
eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm
lemma image_const [decidable_eq β] {s : finset α} (h : s ≠ ∅) (b : β) : s.image (λa, b) = singleton b :=
ext.2 $ assume b', by simp [exists_mem_of_ne_empty h, eq_comm]
end image
/- card -/
section card
/-- `card s` is the cardinality (number of elements) of `s`. -/
def card (s : finset α) : nat := s.1.card
theorem card_def (s : finset α) : s.card = s.1.card := rfl
@[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl
@[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ :=
card_eq_zero.trans val_eq_zero
theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ :=
pos_iff_ne_zero.trans $ not_congr card_eq_zero
@[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 :=
by simpa [card] using congr_arg multiset.card (ndinsert_of_not_mem h)
theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 :=
by by_cases a ∈ s; simp [h, nat.le_add_right]
@[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _
theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem
theorem card_range (n : ℕ) : card (range n) = n := card_range n
theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach
theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α}
(H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s :=
by simp [card, image_val_of_inj_on H]
theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α)
(H : function.injective f) : card (image f s) = card s :=
card_image_of_inj_on $ λ x _ y _ h, H h
lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ}
(f : ∀i, i < n → α)
(hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s)
(f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) :
card s = n :=
have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a,
from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩,
assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩,
have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)),
by simpa [ext],
calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) :
by rw [this]
... = card ((range n).attach) :
card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq,
subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
... = card (range n) : card_attach
... = n : card_range n
lemma card_eq_succ [decidable_eq α] {s : finset α} {a : α} {n : ℕ} :
s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) :=
iff.intro
(assume eq,
have card s > 0, from eq.symm ▸ nat.zero_lt_succ _,
let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in
⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp [eq, card_erase_of_mem has]⟩)
(assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat)
theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t :=
multiset.card_le_of_le ∘ val_le_iff.mpr
theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t :=
eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂
lemma card_lt_card [decidable_eq α] {s t : finset α} (h : s ⊂ t) : s.card < t.card :=
card_lt_of_lt (val_lt_iff.2 h)
lemma card_le_card_of_inj_on [decidable_eq α] [decidable_eq β] {s : finset α} {t : finset β}
(f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) :
card s ≤ card t :=
calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj]
... ≤ card t : card_le_of_subset $
assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end
lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α}
(f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s :=
calc n = card (range n) : (card_range n).symm
... ≤ card s : card_le_card_of_inj_on f
(by simp; assumption)
(by simp; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂)
@[elab_as_eliminator] lemma strong_induction_on {p : finset α → Sort*} :
∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s
| ⟨s, nd⟩ ih := multiset.strong_induction_on s
(λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd
@[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop}
(s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s :=
finset.strong_induction_on s $ λ s,
finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $
λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
end card
section bind
variables [decidable_eq β] {s : finset α} {t : α → finset β}
/-- `bind s t` is the union of `t x` over `x ∈ s` -/
protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset
@[simp] theorem bind_val (s : finset α) (t : α → finset β) :
(s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl
@[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl
@[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a :=
by simp [mem_def]
@[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t :=
ext.2 $ by simp [or_and_distrib_right, exists_or_distrib]
@[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a :=
show (insert a ∅ : finset α).bind t = t a, by simp
theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} :
(s.image f).bind t = s.bind (λa, t (f a)) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp {contextual := tt})
theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} :
(s.bind t).image f = s.bind (λa, (t a).image f) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp [image_union] {contextual := tt})
theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) :
(s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) :=
ext.2 $ by simp
lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ :=
have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a),
from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩,
by simpa [finset.subset_iff]
lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f :=
finset.ext.mpr $ by simp [eq_comm]
end bind
section prod
variables {s : finset α} {t : finset β}
/-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/
protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩
@[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl
@[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product
theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) :
s.product t = s.bind (λa, t.image $ λb, (a, b)) :=
ext.2 $ by simp [and.left_comm]
@[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t :=
multiset.card_product _ _
end prod
section sigma
variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)}
/-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/
protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) :=
⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩
@[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma
theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} :
s₁ ⊆ s₂ → (∀a, t₁ a ⊆ t₂ a) → s₁.sigma t₁ ⊆ s₂.sigma t₂ :=
by simp [subset_iff, mem_sigma] {contextual := tt}
theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) :
s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) :=
ext.2 $ by simp [and.left_comm]
end sigma
section pi
variables {δ : α → Type*} [decidable_eq α]
def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) :=
⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩
@[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) :
(s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl
@[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} :
f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) :=
mem_pi _ _ _
def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a :=
multiset.pi.empty β a h
def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' :=
multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h)
@[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) :
pi.cons s a b f a h = b :=
multiset.pi.cons_same _
lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') :
pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) :=
multiset.pi.cons_ne _ _
lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) :
function.injective (pi.cons s a b) :=
assume e₁ e₂ eq,
@multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $
funext $ assume e, funext $ assume h,
have pi.cons s a b e₁ e (by simpa using h) = pi.cons s a b e₂ e (by simpa using h),
by rw [eq],
this
@[simp] lemma pi_empty {t : Πa:α, finset (δ a)} :
pi (∅ : finset α) t = singleton (pi.empty δ) := rfl
@[simp] lemma pi_insert [∀a, decidable_eq (δ a)]
{s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) :
pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) :=
begin
apply eq_of_veq,
rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2,
refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) =
erase_dup ((t a).1.bind $ λ b,
erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $
λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha),
subst s', rw pi_cons,
congr, funext b,
rw multiset.erase_dup_eq_self.2,
exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2,
end
end pi
section powerset
def powerset (s : finset α) : finset (finset α) :=
⟨s.1.powerset.pmap finset.mk
(λ t h, nodup_of_le (mem_powerset.1 h) s.2),
nodup_pmap (λ a ha b hb, congr_arg finset.val)
(nodup_powerset.2 s.2)⟩
@[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t :=
by cases s; simp [powerset]; rw ← val_le_iff
@[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s :=
mem_powerset.2 (empty_subset _)
@[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s :=
mem_powerset.2 (subset.refl _)
@[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t :=
⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _),
λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩
@[simp] theorem card_powerset (s : finset α) :
card (powerset s) = 2 ^ card s :=
(card_pmap _ _ _).trans (card_powerset s.1)
end powerset
section subtype
variables [decidable_eq α]
protected def subtype (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) :=
(s.filter p).attach.image $ λ⟨a, ha⟩, ⟨a, (mem_filter.1 ha).2⟩
@[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} :
∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s
| ⟨a, ha⟩ := by simp [finset.subtype, ha]
end subtype
section fold
variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op]
local notation a * b := op a b
include hc ha
/-- `fold op b f s` folds the commutative associative operation `op` over the
`f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/
def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b
variables {op} {f : α → β} {b : β} {s : finset α} {a : α}
@[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl
@[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f :=
by simp [fold, ndinsert_of_not_mem h]
@[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b :=
by simp [fold]
@[simp] theorem fold_image [decidable_eq α] [decidable_eq γ] {g : γ → α} {s : finset γ}
(H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) :=
by simp [fold, image_val_of_inj_on H, map_map]
@[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g :=
by rw [fold, fold, map_congr H]
theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} :
s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g :=
by simp [fold, fold_distrib]
theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op']
{m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) :
s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) :=
by rw [fold, fold, ← fold_hom op hm, multiset.map_map]
theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} :
(s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f :=
by unfold fold; rw [← fold_add op, ← map_add, union_val,
inter_val, union_add_inter, map_add, hc.comm, fold_add]
@[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] :
(insert a s).fold op b f = f a * s.fold op b f :=
by haveI := classical.prop_decidable;
rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq,
fold_erase_dup_idem op]; simp [fold]
end fold
section sup
variables [semilattice_sup_bot α]
/-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/
def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma sup_val : s.sup f = (s.1.map f).sup := rfl
@[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ :=
fold_empty
@[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f :=
fold_insert_idem
@[simp] lemma sup_singleton [decidable_eq β] {b : β} : ({b} : finset β).sup f = f b :=
calc _ = f b ⊔ (∅:finset β).sup f : sup_insert
... = f b : by simp
lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f :=
finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc)
lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp [-sup_le_iff, sup_le_sup] {contextual := tt})
lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f :=
by letI := classical.dec_eq β; from
calc f b ≤ f b ⊔ s.sup f : le_sup_left
... = (insert b s).sup f : by simp
... = s.sup f : by simp [hb]
lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp {contextual := tt})
lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) :=
iff.intro (assume h b hb, le_trans (le_sup hb) h) sup_le
lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f :=
sup_le $ assume b hb, le_sup (h hb)
end sup
section inf
variables [semilattice_inf_top α]
/-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/
def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma inf_val : s.inf f = (s.1.map f).inf := rfl
@[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ :=
fold_empty
@[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f :=
fold_insert_idem
@[simp] lemma inf_singleton [decidable_eq β] {b : β} : ({b} : finset β).inf f = f b :=
calc _ = f b ⊓ (∅:finset β).inf f : inf_insert
... = f b : by simp
lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f :=
finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc)
lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp [inf_le_inf] {contextual := tt})
lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b :=
by letI := classical.dec_eq β; from
calc f b ≥ f b ⊓ s.inf f : inf_le_left
... = (insert b s).inf f : by simp
... = s.inf f : by simp [hb]
lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp {contextual := tt})
lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ (∀b ∈ s, a ≤ f b) :=
iff.intro (assume h b hb, le_trans h (inf_le hb)) le_inf
lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f :=
le_inf $ assume b hb, inf_le (h hb)
end inf
/- max and min of finite sets -/
section max_min
variables [decidable_linear_order α]
protected def max : finset α → option α :=
fold (option.lift_or_get max) none some
theorem max_eq_sup_with_bot (s : finset α) :
s.max = @sup (with_bot α) α _ s some := rfl
@[simp] theorem max_empty : (∅ : finset α).max = none :=
by simp [finset.max]
@[simp] theorem max_insert {a : α} {s : finset α} :
(insert a s).max = option.lift_or_get max (some a) s.max :=
by simp [finset.max, fold_insert_idem]
@[simp] theorem max_singleton {a : α} : finset.max {a} = some a :=
by simp [finset.max, option.lift_or_get]
@[simp] theorem max_singleton' {a : α} : finset.max (singleton a) = some a :=
max_singleton
theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max :=
(@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem max_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.max :=
let ⟨a, ha⟩ := exists_mem_of_ne_empty h in max_of_mem ha
theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ :=
⟨λ h, by_contradiction $
λ hs, let ⟨a, ha⟩ := max_of_ne_empty hs in by simpa [h] using ha,
λ h, h.symm ▸ max_empty⟩
theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s :=
finset.induction_on s (by simp)
(λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice max_choice (some b) s.max with q q; simp [q] at h,
{ exact absurd h p },
{ exact mem_insert_of_mem (ih h) } }
end)
theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b :=
by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
protected def min : finset α → option α :=
fold (option.lift_or_get min) none some
theorem min_eq_inf_with_top (s : finset α) :
s.min = @inf (with_top α) α _ s some := rfl
@[simp] theorem min_empty : (∅ : finset α).min = none :=
by simp [finset.min]
@[simp] theorem min_insert {a : α} {s : finset α} :
(insert a s).min = option.lift_or_get min (some a) s.min :=
by simp [finset.min, fold_insert_idem]
@[simp] theorem min_singleton {a : α} : finset.min {a} = some a :=
by simp [finset.min, option.lift_or_get]
theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min :=
(@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem min_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.min :=
let ⟨a, ha⟩ := exists_mem_of_ne_empty h in min_of_mem ha
theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ :=
⟨λ h, by_contradiction $
λ hs, let ⟨a, ha⟩ := min_of_ne_empty hs in by simpa [h] using ha,
λ h, h.symm ▸ min_empty⟩
theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s :=
finset.induction_on s (by simp) $
λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice min_choice (some b) s.min with q q; simp [q] at h,
{ exact absurd h p },
{ exact mem_insert_of_mem (ih h) } }
end
theorem le_min_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b :=
by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
end max_min
section sort
variables (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r]
/-- `sort s` constructs a sorted list from the unordered set `s`.
(Uses merge sort algorithm.) -/
def sort (s : finset α) : list α := sort r s.1
@[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) :=
sort_sorted _ _
@[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 :=
sort_eq _ _
@[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup :=
(by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s))
@[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s :=
list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s)
@[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s :=
multiset.mem_sort _
end sort
section disjoint
variable [decidable_eq α]
theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t :=
by simp [_root_.disjoint, subset_iff]; refl
theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 :=
disjoint_left
theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ :=
disjoint_iff
theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s :=
by rw [disjoint.comm, disjoint_left]
theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp [disjoint_left, imp_not_comm]
theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t :=
disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁))
theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t :=
disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁))
@[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left
@[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right
@[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s :=
by simp [disjoint_left]; refl
@[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s :=
by rw disjoint.comm; simp
@[simp] theorem disjoint_insert_left {a : α} {s t : finset α} :
disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t :=
by simp [disjoint_left, or_imp_distrib, forall_and_distrib]; refl
@[simp] theorem disjoint_insert_right {a : α} {s t : finset α} :
disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t :=
disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm]
@[simp] theorem disjoint_union_left {s t u : finset α} :
disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp [disjoint_left, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right {s t u : finset α} :
disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp [disjoint_right, or_imp_distrib, forall_and_distrib]
@[simp] theorem card_disjoint_union {s t : finset α} :
disjoint s t → card (s ∪ t) = card s + card t :=
finset.induction_on s (by simp) $ by simp {contextual := tt}
end disjoint
theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) :
list.sorted (<) (sort (≤) s) :=
(sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _)
instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩
def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) :=
⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩
@[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} :
a ∈ s.attach_fin h ↔ a.1 ∈ s :=
⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁,
λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩
@[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) :
(s.attach_fin h).card = s.card := multiset.card_pmap _ _ _
end finset
namespace list
variable [decidable_eq α]
theorem to_finset_card_of_nodup {l : list α} : l.nodup → l.to_finset.card = l.length :=
begin
induction l,
case list.nil { simp },
case list.cons : _ _ ih {
intros nd,
simp at nd,
simp [finset.card_insert_of_not_mem ((not_iff_not_of_iff mem_to_finset).mpr nd.1),
ih nd.2]
}
end
end list
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.