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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
c9b8911769a0e6c0e82c5c984fd222fe3cf6b97b | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/MonadEnv.lean | a6170d685dcf06902410403d6db3202a76d6ca5e | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 7,175 | 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.Environment
import Lean.Exception
import Lean.Declaration
import Lean.Util.FindExpr
import Lean.AuxRecursor
namespace Lean
def setEnv [MonadEnv m] (env : Environment) : m Unit :=
modifyEnv fun _ => env
def isInductive [Monad m] [MonadEnv m] (declName : Name) : m Bool := do
match (← getEnv).find? declName with
| some (ConstantInfo.inductInfo ..) => return true
| _ => return false
def isInductivePredicate [Monad m] [MonadEnv m] (declName : Name) : m Bool := do
match (← getEnv).find? declName with
| some (ConstantInfo.inductInfo { type := type, ..}) => return visit type
| _ => return false
where
visit : Expr → Bool
| Expr.sort u .. => u == levelZero
| Expr.forallE _ _ b _ => visit b
| _ => false
def isRecCore (env : Environment) (declName : Name) : Bool :=
match env.find? declName with
| some (ConstantInfo.recInfo ..) => return true
| _ => return false
def isRec [Monad m] [MonadEnv m] (declName : Name) : m Bool :=
return isRecCore (← getEnv) declName
@[inline] def withoutModifyingEnv [Monad m] [MonadEnv m] [MonadFinally m] {α : Type} (x : m α) : m α := do
let env ← getEnv
try x finally setEnv env
@[inline] def matchConst [Monad m] [MonadEnv m] (e : Expr) (failK : Unit → m α) (k : ConstantInfo → List Level → m α) : m α := do
match e with
| Expr.const constName us _ => do
match (← getEnv).find? constName with
| some cinfo => k cinfo us
| none => failK ()
| _ => failK ()
@[inline] def matchConstInduct [Monad m] [MonadEnv m] (e : Expr) (failK : Unit → m α) (k : InductiveVal → List Level → m α) : m α :=
matchConst e failK fun cinfo us =>
match cinfo with
| ConstantInfo.inductInfo val => k val us
| _ => failK ()
@[inline] def matchConstCtor [Monad m] [MonadEnv m] (e : Expr) (failK : Unit → m α) (k : ConstructorVal → List Level → m α) : m α :=
matchConst e failK fun cinfo us =>
match cinfo with
| ConstantInfo.ctorInfo val => k val us
| _ => failK ()
@[inline] def matchConstRec [Monad m] [MonadEnv m] (e : Expr) (failK : Unit → m α) (k : RecursorVal → List Level → m α) : m α :=
matchConst e failK fun cinfo us =>
match cinfo with
| ConstantInfo.recInfo val => k val us
| _ => failK ()
def hasConst [Monad m] [MonadEnv m] (constName : Name) : m Bool := do
return (← getEnv).contains constName
private partial def mkAuxNameAux (env : Environment) (base : Name) (i : Nat) : Name :=
let candidate := base.appendIndexAfter i
if env.contains candidate then
mkAuxNameAux env base (i+1)
else
candidate
def mkAuxName [Monad m] [MonadEnv m] (baseName : Name) (idx : Nat) : m Name := do
return mkAuxNameAux (← getEnv) baseName idx
def getConstInfo [Monad m] [MonadEnv m] [MonadError m] (constName : Name) : m ConstantInfo := do
match (← getEnv).find? constName with
| some info => pure info
| none => throwError "unknown constant '{mkConst constName}'"
def mkConstWithLevelParams [Monad m] [MonadEnv m] [MonadError m] (constName : Name) : m Expr := do
let info ← getConstInfo constName
mkConst constName (info.levelParams.map mkLevelParam)
def getConstInfoInduct [Monad m] [MonadEnv m] [MonadError m] (constName : Name) : m InductiveVal := do
match (← getConstInfo constName) with
| ConstantInfo.inductInfo v => pure v
| _ => throwError "'{mkConst constName}' is not a inductive type"
def getConstInfoCtor [Monad m] [MonadEnv m] [MonadError m] (constName : Name) : m ConstructorVal := do
match (← getConstInfo constName) with
| ConstantInfo.ctorInfo v => pure v
| _ => throwError "'{mkConst constName}' is not a constructor"
def getConstInfoRec [Monad m] [MonadEnv m] [MonadError m] (constName : Name) : m RecursorVal := do
match (← getConstInfo constName) with
| ConstantInfo.recInfo v => pure v
| _ => throwError "'{mkConst constName}' is not a recursor"
@[inline] def matchConstStruct [Monad m] [MonadEnv m] [MonadError m] (e : Expr) (failK : Unit → m α) (k : InductiveVal → List Level → ConstructorVal → m α) : m α :=
matchConstInduct e failK fun ival us => do
if ival.isRec then failK ()
else match ival.ctors with
| [ctor] =>
match (← getConstInfo ctor) with
| ConstantInfo.ctorInfo cval => k ival us cval
| _ => failK ()
| _ => failK ()
def addDecl [Monad m] [MonadEnv m] [MonadError m] [MonadOptions m] (decl : Declaration) : m Unit := do
match (← getEnv).addDecl decl with
| Except.ok env => setEnv env
| Except.error ex => throwKernelException ex
private def supportedRecursors :=
#[``Empty.rec, ``False.rec, ``Eq.rec, ``Eq.recOn, ``Eq.casesOn, ``False.casesOn, ``Empty.casesOn, ``And.rec, ``And.casesOn]
/- This is a temporary workaround for generating better error messages for the compiler. It can be deleted after we
rewrite the remaining parts of the compiler in Lean. -/
private def checkUnsupported [Monad m] [MonadEnv m] [MonadError m] (decl : Declaration) : m Unit := do
let env ← getEnv
decl.forExprM fun e =>
let unsupportedRecursor? := e.find? fun
| Expr.const declName .. =>
((isAuxRecursor env declName && !isCasesOnRecursor env declName) || isRecCore env declName)
&& !supportedRecursors.contains declName
| _ => false
match unsupportedRecursor? with
| some (Expr.const declName ..) => throwError "code generator does not support recursor '{declName}' yet, consider using 'match ... with' and/or structural recursion"
| _ => pure ()
def compileDecl [Monad m] [MonadEnv m] [MonadError m] [MonadOptions m] (decl : Declaration) : m Unit := do
match (← getEnv).compileDecl (← getOptions) decl with
| Except.ok env => setEnv env
| Except.error (KernelException.other msg) =>
checkUnsupported decl -- Generate nicer error message for unsupported recursors and axioms
throwError msg
| Except.error ex =>
throwKernelException ex
def addAndCompile [Monad m] [MonadEnv m] [MonadError m] [MonadOptions m] (decl : Declaration) : m Unit := do
addDecl decl;
compileDecl decl
unsafe def evalConst [Monad m] [MonadEnv m] [MonadError m] [MonadOptions m] (α) (constName : Name) : m α := do
ofExcept <| (← getEnv).evalConst α (← getOptions) constName
unsafe def evalConstCheck [Monad m] [MonadEnv m] [MonadError m] [MonadOptions m] (α) (typeName : Name) (constName : Name) : m α := do
ofExcept <| (← getEnv).evalConstCheck α (← getOptions) typeName constName
def findModuleOf? [Monad m] [MonadEnv m] [MonadError m] (declName : Name) : m (Option Name) := do
discard <| getConstInfo declName -- ensure declaration exists
match (← getEnv).getModuleIdxFor? declName with
| none => return none
| some modIdx => return some ((← getEnv).allImportedModuleNames[modIdx])
end Lean
|
65e8cf470769ba2e114ec5e95cd2c29d51d8c561 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Init/Control/Reader.lean | eddf6c602f6e39438a7abb45711749bc45956cbc | [
"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,052 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
The Reader monad transformer for passing immutable State.
-/
prelude
import Init.Control.Basic
import Init.Control.Id
import Init.Control.Except
namespace ReaderT
@[inline] protected def orElse [Alternative m] (x₁ : ReaderT ρ m α) (x₂ : Unit → ReaderT ρ m α) : ReaderT ρ m α :=
fun s => x₁ s <|> x₂ () s
@[inline] protected def failure [Alternative m] : ReaderT ρ m α :=
fun _ => failure
instance [Alternative m] [Monad m] : Alternative (ReaderT ρ m) where
failure := ReaderT.failure
orElse := ReaderT.orElse
end ReaderT
instance : MonadControl m (ReaderT ρ m) where
stM := id
liftWith f ctx := f fun x => x ctx
restoreM x _ := x
instance ReaderT.tryFinally [MonadFinally m] [Monad m] : MonadFinally (ReaderT ρ m) where
tryFinally' x h ctx := tryFinally' (x ctx) (fun a? => h a? ctx)
@[reducible] def Reader (ρ : Type u) := ReaderT ρ Id
|
b8835bc38c1dda040c850500a2251752f69135fb | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/measure_theory/constructions/polish.lean | 63cf523501aca03eea562c4cb2ecd9cc24997be2 | [
"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 | 47,777 | lean | /-
Copyright (c) 2022 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Felix Weilacher
-/
import data.real.cardinality
import topology.perfect
import measure_theory.constructions.borel_space.basic
/-!
# The Borel sigma-algebra on Polish spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We discuss several results pertaining to the relationship between the topology and the Borel
structure on Polish spaces.
## Main definitions and results
First, we define the class of analytic sets and establish its basic properties.
* `measure_theory.analytic_set s`: a set in a topological space is analytic if it is the continuous
image of a Polish space. Equivalently, it is empty, or the image of `ℕ → ℕ`.
* `measure_theory.analytic_set.image_of_continuous`: a continuous image of an analytic set is
analytic.
* `measurable_set.analytic_set`: in a Polish space, any Borel-measurable set is analytic.
Then, we show Lusin's theorem that two disjoint analytic sets can be separated by Borel sets.
* `measurably_separable s t` states that there exists a measurable set containing `s` and disjoint
from `t`.
* `analytic_set.measurably_separable` shows that two disjoint analytic sets are separated by a
Borel set.
We then prove the Lusin-Souslin theorem that a continuous injective image of a Borel subset of
a Polish space is Borel. The proof of this nontrivial result relies on the above results on
analytic sets.
* `measurable_set.image_of_continuous_on_inj_on` asserts that, if `s` is a Borel measurable set in
a Polish space, then the image of `s` under a continuous injective map is still Borel measurable.
* `continuous.measurable_embedding` states that a continuous injective map on a Polish space
is a measurable embedding for the Borel sigma-algebra.
* `continuous_on.measurable_embedding` is the same result for a map restricted to a measurable set
on which it is continuous.
* `measurable.measurable_embedding` states that a measurable injective map from a Polish space
to a second-countable topological space is a measurable embedding.
* `is_clopenable_iff_measurable_set`: in a Polish space, a set is clopenable (i.e., it can be made
open and closed by using a finer Polish topology) if and only if it is Borel-measurable.
We use this to prove several versions of the Borel isomorphism theorem.
* `polish_space.measurable_equiv_of_not_countable` : Any two uncountable Polish spaces
are Borel isomorphic.
* `polish_space.equiv.measurable_equiv` : Any two Polish spaces of the same cardinality
are Borel isomorphic.
-/
open set function polish_space pi_nat topological_space metric filter
open_locale topology measure_theory filter
variables {α : Type*} [topological_space α] {ι : Type*}
namespace measure_theory
/-! ### Analytic sets -/
/-- An analytic set is a set which is the continuous image of some Polish space. There are several
equivalent characterizations of this definition. For the definition, we pick one that avoids
universe issues: a set is analytic if and only if it is a continuous image of `ℕ → ℕ` (or if it
is empty). The above more usual characterization is given
in `analytic_set_iff_exists_polish_space_range`.
Warning: these are analytic sets in the context of descriptive set theory (which is why they are
registered in the namespace `measure_theory`). They have nothing to do with analytic sets in the
context of complex analysis. -/
@[irreducible] def analytic_set (s : set α) : Prop :=
s = ∅ ∨ ∃ (f : (ℕ → ℕ) → α), continuous f ∧ range f = s
lemma analytic_set_empty : analytic_set (∅ : set α) :=
begin
rw analytic_set,
exact or.inl rfl
end
lemma analytic_set_range_of_polish_space
{β : Type*} [topological_space β] [polish_space β] {f : β → α} (f_cont : continuous f) :
analytic_set (range f) :=
begin
casesI is_empty_or_nonempty β,
{ rw range_eq_empty,
exact analytic_set_empty },
{ rw analytic_set,
obtain ⟨g, g_cont, hg⟩ : ∃ (g : (ℕ → ℕ) → β), continuous g ∧ surjective g :=
exists_nat_nat_continuous_surjective β,
refine or.inr ⟨f ∘ g, f_cont.comp g_cont, _⟩,
rwa hg.range_comp }
end
/-- The image of an open set under a continuous map is analytic. -/
lemma _root_.is_open.analytic_set_image {β : Type*} [topological_space β] [polish_space β]
{s : set β} (hs : is_open s) {f : β → α} (f_cont : continuous f) :
analytic_set (f '' s) :=
begin
rw image_eq_range,
haveI : polish_space s := hs.polish_space,
exact analytic_set_range_of_polish_space (f_cont.comp continuous_subtype_coe),
end
/-- A set is analytic if and only if it is the continuous image of some Polish space. -/
theorem analytic_set_iff_exists_polish_space_range {s : set α} :
analytic_set s ↔ ∃ (β : Type) (h : topological_space β) (h' : @polish_space β h) (f : β → α),
@continuous _ _ h _ f ∧ range f = s :=
begin
split,
{ assume h,
rw analytic_set at h,
cases h,
{ refine ⟨empty, by apply_instance, by apply_instance, empty.elim, continuous_bot, _⟩,
rw h,
exact range_eq_empty _ },
{ exact ⟨ℕ → ℕ, by apply_instance, by apply_instance, h⟩ } },
{ rintros ⟨β, h, h', f, f_cont, f_range⟩,
resetI,
rw ← f_range,
exact analytic_set_range_of_polish_space f_cont }
end
/-- The continuous image of an analytic set is analytic -/
lemma analytic_set.image_of_continuous_on {β : Type*} [topological_space β]
{s : set α} (hs : analytic_set s) {f : α → β} (hf : continuous_on f s) :
analytic_set (f '' s) :=
begin
rcases analytic_set_iff_exists_polish_space_range.1 hs with ⟨γ, γtop, γpolish, g, g_cont, gs⟩,
resetI,
have : f '' s = range (f ∘ g), by rw [range_comp, gs],
rw this,
apply analytic_set_range_of_polish_space,
apply hf.comp_continuous g_cont (λ x, _),
rw ← gs,
exact mem_range_self _
end
lemma analytic_set.image_of_continuous {β : Type*} [topological_space β]
{s : set α} (hs : analytic_set s) {f : α → β} (hf : continuous f) :
analytic_set (f '' s) :=
hs.image_of_continuous_on hf.continuous_on
/-- A countable intersection of analytic sets is analytic. -/
theorem analytic_set.Inter [hι : nonempty ι] [countable ι] [t2_space α]
{s : ι → set α} (hs : ∀ n, analytic_set (s n)) :
analytic_set (⋂ n, s n) :=
begin
unfreezingI { rcases hι with ⟨i₀⟩ },
/- For the proof, write each `s n` as the continuous image under a map `f n` of a
Polish space `β n`. The product space `γ = Π n, β n` is also Polish, and so is the subset
`t` of sequences `x n` for which `f n (x n)` is independent of `n`. The set `t` is Polish, and the
range of `x ↦ f 0 (x 0)` on `t` is exactly `⋂ n, s n`, so this set is analytic. -/
choose β hβ h'β f f_cont f_range using λ n, analytic_set_iff_exists_polish_space_range.1 (hs n),
resetI,
let γ := Π n, β n,
let t : set γ := ⋂ n, {x | f n (x n) = f i₀ (x i₀)},
have t_closed : is_closed t,
{ apply is_closed_Inter,
assume n,
exact is_closed_eq ((f_cont n).comp (continuous_apply n))
((f_cont i₀).comp (continuous_apply i₀)) },
haveI : polish_space t := t_closed.polish_space,
let F : t → α := λ x, f i₀ ((x : γ) i₀),
have F_cont : continuous F :=
(f_cont i₀).comp ((continuous_apply i₀).comp continuous_subtype_coe),
have F_range : range F = ⋂ (n : ι), s n,
{ apply subset.antisymm,
{ rintros y ⟨x, rfl⟩,
apply mem_Inter.2 (λ n, _),
have : f n ((x : γ) n) = F x := (mem_Inter.1 x.2 n : _),
rw [← this, ← f_range n],
exact mem_range_self _ },
{ assume y hy,
have A : ∀ n, ∃ (x : β n), f n x = y,
{ assume n,
rw [← mem_range, f_range n],
exact mem_Inter.1 hy n },
choose x hx using A,
have xt : x ∈ t,
{ apply mem_Inter.2 (λ n, _),
simp [hx] },
refine ⟨⟨x, xt⟩, _⟩,
exact hx i₀ } },
rw ← F_range,
exact analytic_set_range_of_polish_space F_cont,
end
/-- A countable union of analytic sets is analytic. -/
theorem analytic_set.Union [countable ι] {s : ι → set α} (hs : ∀ n, analytic_set (s n)) :
analytic_set (⋃ n, s n) :=
begin
/- For the proof, write each `s n` as the continuous image under a map `f n` of a
Polish space `β n`. The union space `γ = Σ n, β n` is also Polish, and the map `F : γ → α` which
coincides with `f n` on `β n` sends it to `⋃ n, s n`. -/
choose β hβ h'β f f_cont f_range using λ n, analytic_set_iff_exists_polish_space_range.1 (hs n),
resetI,
let γ := Σ n, β n,
let F : γ → α := by { rintros ⟨n, x⟩, exact f n x },
have F_cont : continuous F := continuous_sigma f_cont,
have F_range : range F = ⋃ n, s n,
{ rw [range_sigma_eq_Union_range],
congr,
ext1 n,
rw ← f_range n },
rw ← F_range,
exact analytic_set_range_of_polish_space F_cont,
end
theorem _root_.is_closed.analytic_set [polish_space α] {s : set α} (hs : is_closed s) :
analytic_set s :=
begin
haveI : polish_space s := hs.polish_space,
rw ← @subtype.range_val α s,
exact analytic_set_range_of_polish_space continuous_subtype_coe,
end
/-- Given a Borel-measurable set in a Polish space, there exists a finer Polish topology making
it clopen. This is in fact an equivalence, see `is_clopenable_iff_measurable_set`. -/
lemma _root_.measurable_set.is_clopenable [polish_space α] [measurable_space α] [borel_space α]
{s : set α} (hs : measurable_set s) :
is_clopenable s :=
begin
revert s,
apply measurable_set.induction_on_open,
{ exact λ u hu, hu.is_clopenable },
{ exact λ u hu h'u, h'u.compl },
{ exact λ f f_disj f_meas hf, is_clopenable.Union hf }
end
theorem _root_.measurable_set.analytic_set
{α : Type*} [t : topological_space α] [polish_space α] [measurable_space α] [borel_space α]
{s : set α} (hs : measurable_set s) :
analytic_set s :=
begin
/- For a short proof (avoiding measurable induction), one sees `s` as a closed set for a finer
topology `t'`. It is analytic for this topology. As the identity from `t'` to `t` is continuous
and the image of an analytic set is analytic, it follows that `s` is also analytic for `t`. -/
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ t' : topological_space α, t' ≤ t ∧ @polish_space α t' ∧ is_closed[t'] s ∧ is_open[t'] s :=
hs.is_clopenable,
have A := @is_closed.analytic_set α t' t'_polish s s_closed,
convert @analytic_set.image_of_continuous α t' α t s A id (continuous_id_of_le t't),
simp only [id.def, image_id'],
end
/-- Given a Borel-measurable function from a Polish space to a second-countable space, there exists
a finer Polish topology on the source space for which the function is continuous. -/
lemma _root_.measurable.exists_continuous {α β : Type*}
[t : topological_space α] [polish_space α] [measurable_space α] [borel_space α]
[tβ : topological_space β] [measurable_space β] [opens_measurable_space β]
{f : α → β} [second_countable_topology (range f)] (hf : measurable f) :
∃ (t' : topological_space α), t' ≤ t ∧ @continuous α β t' tβ f ∧ @polish_space α t' :=
begin
obtain ⟨b, b_count, -, hb⟩ :
∃ b : set (set (range f)), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b :=
exists_countable_basis (range f),
haveI : countable b := b_count.to_subtype,
have : ∀ (s : b), is_clopenable (range_factorization f ⁻¹' s),
{ assume s,
apply measurable_set.is_clopenable,
exact hf.subtype_mk (hb.is_open s.2).measurable_set },
choose T Tt Tpolish Tclosed Topen using this,
obtain ⟨t', t'T, t't, t'_polish⟩ :
∃ (t' : topological_space α), (∀ i, t' ≤ T i) ∧ (t' ≤ t) ∧ @polish_space α t' :=
exists_polish_space_forall_le T Tt Tpolish,
letI := t', -- not needed in Lean 4
refine ⟨t', t't, _, t'_polish⟩,
have : @continuous _ _ t' _ (range_factorization f) :=
hb.continuous _ (λ s hs, t'T ⟨s, hs⟩ _ (Topen ⟨s, hs⟩)),
exact continuous_subtype_coe.comp this
end
/-- The image of a measurable set in a Polish space under a measurable map is an analytic set. -/
theorem _root_.measurable_set.analytic_set_image {X Y : Type*}
[topological_space X] [polish_space X] [measurable_space X] [borel_space X]
[topological_space Y] [measurable_space Y] [opens_measurable_space Y]
{f : X → Y} [second_countable_topology (range f)] {s : set X} (hs : measurable_set s)
(hf : measurable f) : analytic_set (f '' s) :=
begin
borelize X,
rcases hf.exists_continuous with ⟨τ', hle, hfc, hτ'⟩,
letI m' : measurable_space X := @borel _ τ',
haveI b' : borel_space X := ⟨rfl⟩,
have hle := borel_anti hle,
exact (hle _ hs).analytic_set.image_of_continuous hfc
end
/-! ### Separating sets with measurable sets -/
/-- Two sets `u` and `v` in a measurable space are measurably separable if there
exists a measurable set containing `u` and disjoint from `v`.
This is mostly interesting for Borel-separable sets. -/
def measurably_separable {α : Type*} [measurable_space α] (s t : set α) : Prop :=
∃ u, s ⊆ u ∧ disjoint t u ∧ measurable_set u
lemma measurably_separable.Union [countable ι]
{α : Type*} [measurable_space α] {s t : ι → set α}
(h : ∀ m n, measurably_separable (s m) (t n)) :
measurably_separable (⋃ n, s n) (⋃ m, t m) :=
begin
choose u hsu htu hu using h,
refine ⟨⋃ m, (⋂ n, u m n), _, _, _⟩,
{ refine Union_subset (λ m, subset_Union_of_subset m _),
exact subset_Inter (λ n, hsu m n) },
{ simp_rw [disjoint_Union_left, disjoint_Union_right],
assume n m,
apply disjoint.mono_right _ (htu m n),
apply Inter_subset },
{ refine measurable_set.Union (λ m, _),
exact measurable_set.Inter (λ n, hu m n) }
end
/-- The hard part of the Lusin separation theorem saying that two disjoint analytic sets are
contained in disjoint Borel sets (see the full statement in `analytic_set.measurably_separable`).
Here, we prove this when our analytic sets are the ranges of functions from `ℕ → ℕ`.
-/
lemma measurably_separable_range_of_disjoint [t2_space α] [measurable_space α]
[opens_measurable_space α] {f g : (ℕ → ℕ) → α} (hf : continuous f) (hg : continuous g)
(h : disjoint (range f) (range g)) :
measurably_separable (range f) (range g) :=
begin
/- We follow [Kechris, *Classical Descriptive Set Theory* (Theorem 14.7)][kechris1995].
If the ranges are not Borel-separated, then one can find two cylinders of length one whose images
are not Borel-separated, and then two smaller cylinders of length two whose images are not
Borel-separated, and so on. One thus gets two sequences of cylinders, that decrease to two
points `x` and `y`. Their images are different by the disjointness assumption, hence contained
in two disjoint open sets by the T2 property. By continuity, long enough cylinders around `x`
and `y` have images which are separated by these two disjoint open sets, a contradiction.
-/
by_contra hfg,
have I : ∀ n x y, (¬(measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n))))
→ ∃ x' y', x' ∈ cylinder x n ∧ y' ∈ cylinder y n ∧
¬(measurably_separable (f '' (cylinder x' (n+1))) (g '' (cylinder y' (n+1)))),
{ assume n x y,
contrapose!,
assume H,
rw [← Union_cylinder_update x n, ← Union_cylinder_update y n, image_Union, image_Union],
refine measurably_separable.Union (λ i j, _),
exact H _ _ (update_mem_cylinder _ _ _) (update_mem_cylinder _ _ _) },
-- consider the set of pairs of cylinders of some length whose images are not Borel-separated
let A := {p : ℕ × (ℕ → ℕ) × (ℕ → ℕ) //
¬(measurably_separable (f '' (cylinder p.2.1 p.1)) (g '' (cylinder p.2.2 p.1)))},
-- for each such pair, one can find longer cylinders whose images are not Borel-separated either
have : ∀ (p : A), ∃ (q : A), q.1.1 = p.1.1 + 1 ∧ q.1.2.1 ∈ cylinder p.1.2.1 p.1.1
∧ q.1.2.2 ∈ cylinder p.1.2.2 p.1.1,
{ rintros ⟨⟨n, x, y⟩, hp⟩,
rcases I n x y hp with ⟨x', y', hx', hy', h'⟩,
exact ⟨⟨⟨n+1, x', y'⟩, h'⟩, rfl, hx', hy'⟩ },
choose F hFn hFx hFy using this,
let p0 : A := ⟨⟨0, λ n, 0, λ n, 0⟩, by simp [hfg]⟩,
-- construct inductively decreasing sequences of cylinders whose images are not separated
let p : ℕ → A := λ n, F^[n] p0,
have prec : ∀ n, p (n+1) = F (p n) := λ n, by simp only [p, iterate_succ'],
-- check that at the `n`-th step we deal with cylinders of length `n`
have pn_fst : ∀ n, (p n).1.1 = n,
{ assume n,
induction n with n IH,
{ refl },
{ simp only [prec, hFn, IH] } },
-- check that the cylinders we construct are indeed decreasing, by checking that the coordinates
-- are stationary.
have Ix : ∀ m n, m + 1 ≤ n → (p n).1.2.1 m = (p (m+1)).1.2.1 m,
{ assume m,
apply nat.le_induction,
{ refl },
assume n hmn IH,
have I : (F (p n)).val.snd.fst m = (p n).val.snd.fst m,
{ apply hFx (p n) m,
rw pn_fst,
exact hmn },
rw [prec, I, IH] },
have Iy : ∀ m n, m + 1 ≤ n → (p n).1.2.2 m = (p (m+1)).1.2.2 m,
{ assume m,
apply nat.le_induction,
{ refl },
assume n hmn IH,
have I : (F (p n)).val.snd.snd m = (p n).val.snd.snd m,
{ apply hFy (p n) m,
rw pn_fst,
exact hmn },
rw [prec, I, IH] },
-- denote by `x` and `y` the limit points of these two sequences of cylinders.
set x : ℕ → ℕ := λ n, (p (n+1)).1.2.1 n with hx,
set y : ℕ → ℕ := λ n, (p (n+1)).1.2.2 n with hy,
-- by design, the cylinders around these points have images which are not Borel-separable.
have M : ∀ n, ¬(measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n))),
{ assume n,
convert (p n).2 using 3,
{ rw [pn_fst, ← mem_cylinder_iff_eq, mem_cylinder_iff],
assume i hi,
rw hx,
exact (Ix i n hi).symm },
{ rw [pn_fst, ← mem_cylinder_iff_eq, mem_cylinder_iff],
assume i hi,
rw hy,
exact (Iy i n hi).symm } },
-- consider two open sets separating `f x` and `g y`.
obtain ⟨u, v, u_open, v_open, xu, yv, huv⟩ :
∃ u v : set α, is_open u ∧ is_open v ∧ f x ∈ u ∧ g y ∈ v ∧ disjoint u v,
{ apply t2_separation,
exact disjoint_iff_forall_ne.1 h _ (mem_range_self _) _ (mem_range_self _) },
letI : metric_space (ℕ → ℕ) := metric_space_nat_nat,
obtain ⟨εx, εxpos, hεx⟩ : ∃ (εx : ℝ) (H : εx > 0), metric.ball x εx ⊆ f ⁻¹' u,
{ apply metric.mem_nhds_iff.1,
exact hf.continuous_at.preimage_mem_nhds (u_open.mem_nhds xu) },
obtain ⟨εy, εypos, hεy⟩ : ∃ (εy : ℝ) (H : εy > 0), metric.ball y εy ⊆ g ⁻¹' v,
{ apply metric.mem_nhds_iff.1,
exact hg.continuous_at.preimage_mem_nhds (v_open.mem_nhds yv) },
obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2 : ℝ)^n < min εx εy :=
exists_pow_lt_of_lt_one (lt_min εxpos εypos) (by norm_num),
-- for large enough `n`, these open sets separate the images of long cylinders around `x` and `y`
have B : measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n)),
{ refine ⟨u, _, _, u_open.measurable_set⟩,
{ rw image_subset_iff,
apply subset.trans _ hεx,
assume z hz,
rw mem_cylinder_iff_dist_le at hz,
exact hz.trans_lt (hn.trans_le (min_le_left _ _)) },
{ refine disjoint.mono_left _ huv.symm,
change g '' cylinder y n ⊆ v,
rw image_subset_iff,
apply subset.trans _ hεy,
assume z hz,
rw mem_cylinder_iff_dist_le at hz,
exact hz.trans_lt (hn.trans_le (min_le_right _ _)) } },
-- this is a contradiction.
exact M n B
end
/-- The Lusin separation theorem: if two analytic sets are disjoint, then they are contained in
disjoint Borel sets. -/
theorem analytic_set.measurably_separable [t2_space α] [measurable_space α]
[opens_measurable_space α] {s t : set α} (hs : analytic_set s) (ht : analytic_set t)
(h : disjoint s t) :
measurably_separable s t :=
begin
rw analytic_set at hs ht,
rcases hs with rfl|⟨f, f_cont, rfl⟩,
{ refine ⟨∅, subset.refl _, by simp, measurable_set.empty⟩ },
rcases ht with rfl|⟨g, g_cont, rfl⟩,
{ exact ⟨univ, subset_univ _, by simp, measurable_set.univ⟩ },
exact measurably_separable_range_of_disjoint f_cont g_cont h,
end
/-- **Suslin's Theorem**: in a Hausdorff topological space, an analytic set with an analytic
complement is measurable. -/
theorem analytic_set.measurable_set_of_compl [t2_space α] [measurable_space α]
[opens_measurable_space α] {s : set α} (hs : analytic_set s) (hsc : analytic_set (sᶜ)) :
measurable_set s :=
begin
rcases hs.measurably_separable hsc disjoint_compl_right with ⟨u, hsu, hdu, hmu⟩,
obtain rfl : s = u := hsu.antisymm (disjoint_compl_left_iff_subset.1 hdu),
exact hmu
end
end measure_theory
/-!
### Measurability of preimages under measurable maps
-/
namespace measurable
variables {X Y β : Type*}
[topological_space X] [polish_space X] [measurable_space X] [borel_space X]
[topological_space Y] [t2_space Y] [measurable_space Y] [opens_measurable_space Y]
[measurable_space β]
/-- If `f : X → Y` is a surjective Borel measurable map from a Polish space to a topological space
with second countable topology, then the preimage of a set `s` is measurable if and only if the set
is measurable.
One implication is the definition of measurability, the other one heavily relies on `X` being a
Polish space. -/
theorem measurable_set_preimage_iff_of_surjective [second_countable_topology Y] {f : X → Y}
(hf : measurable f) (hsurj : surjective f) {s : set Y} :
measurable_set (f ⁻¹' s) ↔ measurable_set s :=
begin
refine ⟨λ h, _, λ h, hf h⟩,
apply measure_theory.analytic_set.measurable_set_of_compl,
{ rw [← image_preimage_eq s hsurj],
exact h.analytic_set_image hf },
{ rw [← image_preimage_eq (sᶜ) hsurj],
exact h.compl.analytic_set_image hf }
end
theorem map_measurable_space_eq [second_countable_topology Y] {f : X → Y} (hf : measurable f)
(hsurj : surjective f) : measurable_space.map f ‹measurable_space X› = ‹measurable_space Y› :=
measurable_space.ext $ λ _, hf.measurable_set_preimage_iff_of_surjective hsurj
theorem map_measurable_space_eq_borel [second_countable_topology Y] {f : X → Y} (hf : measurable f)
(hsurj : surjective f) : measurable_space.map f ‹measurable_space X› = borel Y :=
begin
have := hf.mono le_rfl opens_measurable_space.borel_le,
letI := borel Y, haveI : borel_space Y := ⟨rfl⟩,
exact this.map_measurable_space_eq hsurj
end
theorem borel_space_codomain [second_countable_topology Y] {f : X → Y} (hf : measurable f)
(hsurj : surjective f) : borel_space Y :=
⟨(hf.map_measurable_space_eq hsurj).symm.trans $ hf.map_measurable_space_eq_borel hsurj⟩
/-- If `f : X → Y` is a Borel measurable map from a Polish space to a topological space with second
countable topology, then the preimage of a set `s` is measurable if and only if the set is
measurable in `set.range f`. -/
theorem measurable_set_preimage_iff_preimage_coe {f : X → Y} [second_countable_topology (range f)]
(hf : measurable f) {s : set Y} :
measurable_set (f ⁻¹' s) ↔ measurable_set (coe ⁻¹' s : set (range f)) :=
have hf' : measurable (range_factorization f) := hf.subtype_mk,
by rw [← hf'.measurable_set_preimage_iff_of_surjective surjective_onto_range]; refl
/-- If `f : X → Y` is a Borel measurable map from a Polish space to a topological space with second
countable topology and the range of `f` is measurable, then the preimage of a set `s` is measurable
if and only if the intesection with `set.range f` is measurable. -/
theorem measurable_set_preimage_iff_inter_range {f : X → Y} [second_countable_topology (range f)]
(hf : measurable f) (hr : measurable_set (range f)) {s : set Y} :
measurable_set (f ⁻¹' s) ↔ measurable_set (s ∩ range f) :=
begin
rw [hf.measurable_set_preimage_iff_preimage_coe,
← (measurable_embedding.subtype_coe hr).measurable_set_image, subtype.image_preimage_coe]
end
/-- If `f : X → Y` is a Borel measurable map from a Polish space to a topological space with second
countable topology, then for any measurable space `β` and `g : Y → β`, the composition `g ∘ f` is
measurable if and only if the restriction of `g` to the range of `f` is measurable. -/
theorem measurable_comp_iff_restrict {f : X → Y} [second_countable_topology (range f)]
(hf : measurable f) {g : Y → β} :
measurable (g ∘ f) ↔ measurable (restrict (range f) g) :=
forall₂_congr $ λ s _,
@measurable.measurable_set_preimage_iff_preimage_coe _ _ _ _ _ _ _ _ _ _ _ _ hf (g ⁻¹' s)
/-- If `f : X → Y` is a surjective Borel measurable map from a Polish space to a topological space
with second countable topology, then for any measurable space `α` and `g : Y → α`, the composition
`g ∘ f` is measurable if and only if `g` is measurable. -/
theorem measurable_comp_iff_of_surjective [second_countable_topology Y] {f : X → Y}
(hf : measurable f) (hsurj : surjective f) {g : Y → β} :
measurable (g ∘ f) ↔ measurable g :=
forall₂_congr $ λ s _,
@measurable.measurable_set_preimage_iff_of_surjective _ _ _ _ _ _ _ _ _ _ _ _ hf hsurj (g ⁻¹' s)
end measurable
theorem continuous.map_eq_borel {X Y : Type*}
[topological_space X] [polish_space X] [measurable_space X] [borel_space X]
[topological_space Y] [t2_space Y] [second_countable_topology Y]
{f : X → Y} (hf : continuous f) (hsurj : surjective f) :
measurable_space.map f ‹measurable_space X› = borel Y :=
begin
borelize Y,
exact hf.measurable.map_measurable_space_eq hsurj
end
theorem continuous.map_borel_eq {X Y : Type*} [topological_space X] [polish_space X]
[topological_space Y] [t2_space Y] [second_countable_topology Y]
{f : X → Y} (hf : continuous f) (hsurj : surjective f) :
measurable_space.map f (borel X) = borel Y :=
begin
borelize X,
exact hf.map_eq_borel hsurj
end
instance quotient.borel_space {X : Type*} [topological_space X] [polish_space X]
[measurable_space X] [borel_space X] {s : setoid X} [t2_space (quotient s)]
[second_countable_topology (quotient s)] : borel_space (quotient s) :=
⟨continuous_quotient_mk.map_eq_borel (surjective_quotient_mk _)⟩
@[to_additive]
instance quotient_group.borel_space {G : Type*} [topological_space G] [polish_space G]
[group G] [topological_group G] [measurable_space G] [borel_space G]
{N : subgroup G} [N.normal] [is_closed (N : set G)] : borel_space (G ⧸ N) :=
by haveI := polish_space.second_countable G; exact quotient.borel_space
namespace measure_theory
/-! ### Injective images of Borel sets -/
variables {γ : Type*} [tγ : topological_space γ] [polish_space γ]
include tγ
/-- The Lusin-Souslin theorem: the range of a continuous injective function defined on a Polish
space is Borel-measurable. -/
theorem measurable_set_range_of_continuous_injective {β : Type*}
[topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{f : γ → β} (f_cont : continuous f) (f_inj : injective f) :
measurable_set (range f) :=
begin
/- We follow [Fremlin, *Measure Theory* (volume 4, 423I)][fremlin_vol4].
Let `b = {s i}` be a countable basis for `α`. When `s i` and `s j` are disjoint, their images are
disjoint analytic sets, hence by the separation theorem one can find a Borel-measurable set
`q i j` separating them.
Let `E i = closure (f '' s i) ∩ ⋂ j, q i j \ q j i`. It contains `f '' (s i)` and it is
measurable. Let `F n = ⋃ E i`, where the union is taken over those `i` for which `diam (s i)`
is bounded by some number `u n` tending to `0` with `n`.
We claim that `range f = ⋂ F n`, from which the measurability is obvious. The inclusion `⊆` is
straightforward. To show `⊇`, consider a point `x` in the intersection. For each `n`, it belongs
to some `E i` with `diam (s i) ≤ u n`. Pick a point `y i ∈ s i`. We claim that for such `i`
and `j`, the intersection `s i ∩ s j` is nonempty: if it were empty, then thanks to the
separating set `q i j` in the definition of `E i` one could not have `x ∈ E i ∩ E j`.
Since these two sets have small diameter, it follows that `y i` and `y j` are close.
Thus, `y` is a Cauchy sequence, converging to a limit `z`. We claim that `f z = x`, completing
the proof.
Otherwise, one could find open sets `v` and `w` separating `f z` from `x`. Then, for large `n`,
the image `f '' (s i)` would be included in `v` by continuity of `f`, so its closure would be
contained in the closure of `v`, and therefore it would be disjoint from `w`. This is a
contradiction since `x` belongs both to this closure and to `w`. -/
letI := upgrade_polish_space γ,
obtain ⟨b, b_count, b_nonempty, hb⟩ :
∃ b : set (set γ), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b := exists_countable_basis γ,
haveI : encodable b := b_count.to_encodable,
let A := {p : b × b // disjoint (p.1 : set γ) p.2},
-- for each pair of disjoint sets in the topological basis `b`, consider Borel sets separating
-- their images, by injectivity of `f` and the Lusin separation theorem.
have : ∀ (p : A), ∃ (q : set β), f '' (p.1.1 : set γ) ⊆ q ∧ disjoint (f '' (p.1.2 : set γ)) q
∧ measurable_set q,
{ assume p,
apply analytic_set.measurably_separable ((hb.is_open p.1.1.2).analytic_set_image f_cont)
((hb.is_open p.1.2.2).analytic_set_image f_cont),
exact disjoint.image p.2 (f_inj.inj_on univ) (subset_univ _) (subset_univ _) },
choose q hq1 hq2 q_meas using this,
-- define sets `E i` and `F n` as in the proof sketch above
let E : b → set β := λ s, closure (f '' s) ∩
(⋂ (t : b) (ht : disjoint s.1 t.1), q ⟨(s, t), ht⟩ \ q ⟨(t, s), ht.symm⟩),
obtain ⟨u, u_anti, u_pos, u_lim⟩ :
∃ (u : ℕ → ℝ), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) :=
exists_seq_strict_anti_tendsto (0 : ℝ),
let F : ℕ → set β := λ n, ⋃ (s : b) (hs : bounded s.1 ∧ diam s.1 ≤ u n), E s,
-- it is enough to show that `range f = ⋂ F n`, as the latter set is obviously measurable.
suffices : range f = ⋂ n, F n,
{ have E_meas : ∀ (s : b), measurable_set (E s),
{ assume b,
refine is_closed_closure.measurable_set.inter _,
refine measurable_set.Inter (λ s, _),
exact measurable_set.Inter (λ hs, (q_meas _).diff (q_meas _)) },
have F_meas : ∀ n, measurable_set (F n),
{ assume n,
refine measurable_set.Union (λ s, _),
exact measurable_set.Union (λ hs, E_meas _) },
rw this,
exact measurable_set.Inter (λ n, F_meas n) },
-- we check both inclusions.
apply subset.antisymm,
-- we start with the easy inclusion `range f ⊆ ⋂ F n`. One just needs to unfold the definitions.
{ rintros x ⟨y, rfl⟩,
apply mem_Inter.2 (λ n, _),
obtain ⟨s, sb, ys, hs⟩ : ∃ (s : set γ) (H : s ∈ b), y ∈ s ∧ s ⊆ ball y (u n / 2),
{ apply hb.mem_nhds_iff.1,
exact ball_mem_nhds _ (half_pos (u_pos n)) },
have diam_s : diam s ≤ u n,
{ apply (diam_mono hs bounded_ball).trans,
convert diam_ball (half_pos (u_pos n)).le,
ring },
refine mem_Union.2 ⟨⟨s, sb⟩, _⟩,
refine mem_Union.2 ⟨⟨metric.bounded.mono hs bounded_ball, diam_s⟩, _⟩,
apply mem_inter (subset_closure (mem_image_of_mem _ ys)),
refine mem_Inter.2 (λ t, mem_Inter.2 (λ ht, ⟨_, _⟩)),
{ apply hq1,
exact mem_image_of_mem _ ys },
{ apply disjoint_left.1 (hq2 ⟨(t, ⟨s, sb⟩), ht.symm⟩),
exact mem_image_of_mem _ ys } },
-- Now, let us prove the harder inclusion `⋂ F n ⊆ range f`.
{ assume x hx,
-- pick for each `n` a good set `s n` of small diameter for which `x ∈ E (s n)`.
have C1 : ∀ n, ∃ (s : b) (hs : bounded s.1 ∧ diam s.1 ≤ u n), x ∈ E s :=
λ n, by simpa only [mem_Union] using mem_Inter.1 hx n,
choose s hs hxs using C1,
have C2 : ∀ n, (s n).1.nonempty,
{ assume n,
rw nonempty_iff_ne_empty,
assume hn,
have := (s n).2,
rw hn at this,
exact b_nonempty this },
-- choose a point `y n ∈ s n`.
choose y hy using C2,
have I : ∀ m n, ((s m).1 ∩ (s n).1).nonempty,
{ assume m n,
rw ← not_disjoint_iff_nonempty_inter,
by_contra' h,
have A : x ∈ q ⟨(s m, s n), h⟩ \ q ⟨(s n, s m), h.symm⟩,
{ have := mem_Inter.1 (hxs m).2 (s n), exact (mem_Inter.1 this h : _) },
have B : x ∈ q ⟨(s n, s m), h.symm⟩ \ q ⟨(s m, s n), h⟩,
{ have := mem_Inter.1 (hxs n).2 (s m), exact (mem_Inter.1 this h.symm : _) },
exact A.2 B.1 },
-- the points `y n` are nearby, and therefore they form a Cauchy sequence.
have cauchy_y : cauchy_seq y,
{ have : tendsto (λ n, 2 * u n) at_top (𝓝 0), by simpa only [mul_zero] using u_lim.const_mul 2,
apply cauchy_seq_of_le_tendsto_0' (λ n, 2 * u n) (λ m n hmn, _) this,
rcases I m n with ⟨z, zsm, zsn⟩,
calc dist (y m) (y n) ≤ dist (y m) z + dist z (y n) : dist_triangle _ _ _
... ≤ u m + u n :
add_le_add ((dist_le_diam_of_mem (hs m).1 (hy m) zsm).trans (hs m).2)
((dist_le_diam_of_mem (hs n).1 zsn (hy n)).trans (hs n).2)
... ≤ 2 * u m : by linarith [u_anti.antitone hmn] },
haveI : nonempty γ := ⟨y 0⟩,
-- let `z` be its limit.
let z := lim at_top y,
have y_lim : tendsto y at_top (𝓝 z) := cauchy_y.tendsto_lim,
suffices : f z = x, by { rw ← this, exact mem_range_self _ },
-- assume for a contradiction that `f z ≠ x`.
by_contra' hne,
-- introduce disjoint open sets `v` and `w` separating `f z` from `x`.
obtain ⟨v, w, v_open, w_open, fzv, xw, hvw⟩ := t2_separation hne,
obtain ⟨δ, δpos, hδ⟩ : ∃ δ > (0 : ℝ), ball z δ ⊆ f ⁻¹' v,
{ apply metric.mem_nhds_iff.1,
exact f_cont.continuous_at.preimage_mem_nhds (v_open.mem_nhds fzv) },
obtain ⟨n, hn⟩ : ∃ n, u n + dist (y n) z < δ,
{ have : tendsto (λ n, u n + dist (y n) z) at_top (𝓝 0),
by simpa only [add_zero] using u_lim.add (tendsto_iff_dist_tendsto_zero.1 y_lim),
exact ((tendsto_order.1 this).2 _ δpos).exists },
-- for large enough `n`, the image of `s n` is contained in `v`, by continuity of `f`.
have fsnv : f '' (s n) ⊆ v,
{ rw image_subset_iff,
apply subset.trans _ hδ,
assume a ha,
calc dist a z ≤ dist a (y n) + dist (y n) z : dist_triangle _ _ _
... ≤ u n + dist (y n) z :
add_le_add_right ((dist_le_diam_of_mem (hs n).1 ha (hy n)).trans (hs n).2) _
... < δ : hn },
-- as `x` belongs to the closure of `f '' (s n)`, it belongs to the closure of `v`.
have : x ∈ closure v := closure_mono fsnv (hxs n).1,
-- this is a contradiction, as `x` is supposed to belong to `w`, which is disjoint from
-- the closure of `v`.
exact disjoint_left.1 (hvw.closure_left w_open) this xw }
end
theorem _root_.is_closed.measurable_set_image_of_continuous_on_inj_on
{β : Type*} [topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{s : set γ} (hs : is_closed s) {f : γ → β} (f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
rw image_eq_range,
haveI : polish_space s := is_closed.polish_space hs,
apply measurable_set_range_of_continuous_injective,
{ rwa continuous_on_iff_continuous_restrict at f_cont },
{ rwa inj_on_iff_injective at f_inj }
end
variables [measurable_space γ] [hγb : borel_space γ]
{β : Type*} [tβ : topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{s : set γ} {f : γ → β}
include tβ hγb
/-- The Lusin-Souslin theorem: if `s` is Borel-measurable in a Polish space, then its image under
a continuous injective map is also Borel-measurable. -/
theorem _root_.measurable_set.image_of_continuous_on_inj_on
(hs : measurable_set s) (f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @polish_space γ t' ∧ is_closed[t'] s ∧
is_open[t'] s := hs.is_clopenable,
exact @is_closed.measurable_set_image_of_continuous_on_inj_on γ t' t'_polish β _ _ _ _ s
s_closed f (f_cont.mono_dom t't) f_inj,
end
/-- The Lusin-Souslin theorem: if `s` is Borel-measurable in a Polish space, then its image under
a measurable injective map taking values in a second-countable topological space
is also Borel-measurable. -/
theorem _root_.measurable_set.image_of_measurable_inj_on [second_countable_topology β]
(hs : measurable_set s) (f_meas : measurable f) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
-- for a finer Polish topology, `f` is continuous. Therefore, one may apply the corresponding
-- result for continuous maps.
obtain ⟨t', t't, f_cont, t'_polish⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @continuous γ β t' tβ f ∧ @polish_space γ t' :=
f_meas.exists_continuous,
have M : measurable_set[@borel γ t'] s :=
@continuous.measurable γ γ t' (@borel γ t')
(@borel_space.opens_measurable γ t' (@borel γ t') (by { constructor, refl }))
tγ _ _ _ (continuous_id_of_le t't) s hs,
exact @measurable_set.image_of_continuous_on_inj_on γ t' t'_polish
(@borel γ t') (by { constructor, refl }) β _ _ _ _ s f M
(@continuous.continuous_on γ β t' tβ f s f_cont) f_inj,
end
/-- An injective continuous function on a Polish space is a measurable embedding. -/
theorem _root_.continuous.measurable_embedding (f_cont : continuous f) (f_inj : injective f) :
measurable_embedding f :=
{ injective := f_inj,
measurable := f_cont.measurable,
measurable_set_image' := λ u hu,
hu.image_of_continuous_on_inj_on f_cont.continuous_on (f_inj.inj_on _) }
/-- If `s` is Borel-measurable in a Polish space and `f` is continuous injective on `s`, then
the restriction of `f` to `s` is a measurable embedding. -/
theorem _root_.continuous_on.measurable_embedding (hs : measurable_set s)
(f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_embedding (s.restrict f) :=
{ injective := inj_on_iff_injective.1 f_inj,
measurable := (continuous_on_iff_continuous_restrict.1 f_cont).measurable,
measurable_set_image' :=
begin
assume u hu,
have A : measurable_set ((coe : s → γ) '' u) :=
(measurable_embedding.subtype_coe hs).measurable_set_image.2 hu,
have B : measurable_set (f '' ((coe : s → γ) '' u)) :=
A.image_of_continuous_on_inj_on (f_cont.mono (subtype.coe_image_subset s u))
(f_inj.mono ((subtype.coe_image_subset s u))),
rwa ← image_comp at B,
end }
/-- An injective measurable function from a Polish space to a second-countable topological space
is a measurable embedding. -/
theorem _root_.measurable.measurable_embedding [second_countable_topology β]
(f_meas : measurable f) (f_inj : injective f) :
measurable_embedding f :=
{ injective := f_inj,
measurable := f_meas,
measurable_set_image' := λ u hu, hu.image_of_measurable_inj_on f_meas (f_inj.inj_on _) }
omit tβ
/-- In a Polish space, a set is clopenable if and only if it is Borel-measurable. -/
lemma is_clopenable_iff_measurable_set :
is_clopenable s ↔ measurable_set s :=
begin
-- we already know that a measurable set is clopenable. Conversely, assume that `s` is clopenable.
refine ⟨λ hs, _, λ hs, hs.is_clopenable⟩,
-- consider a finer topology `t'` in which `s` is open and closed.
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @polish_space γ t' ∧ is_closed[t'] s ∧
is_open[t'] s := hs,
-- the identity is continuous from `t'` to `tγ`.
have C : @continuous γ γ t' tγ id := continuous_id_of_le t't,
-- therefore, it is also a measurable embedding, by the Lusin-Souslin theorem
have E := @continuous.measurable_embedding γ t' t'_polish (@borel γ t') (by { constructor, refl })
γ tγ (polish_space.t2_space γ) _ _ id C injective_id,
-- the set `s` is measurable for `t'` as it is closed.
have M : @measurable_set γ (@borel γ t') s :=
@is_closed.measurable_set γ s t' (@borel γ t')
(@borel_space.opens_measurable γ t' (@borel γ t') (by { constructor, refl })) s_closed,
-- therefore, its image under the measurable embedding `id` is also measurable for `tγ`.
convert E.measurable_set_image.2 M,
simp only [id.def, image_id'],
end
omit hγb
/-- The set of points for which a measurable sequence of functions converges is measurable. -/
@[measurability] lemma measurable_set_exists_tendsto
[hγ : opens_measurable_space γ] [countable ι] {l : filter ι}
[l.is_countably_generated] {f : ι → β → γ} (hf : ∀ i, measurable (f i)) :
measurable_set {x | ∃ c, tendsto (λ n, f n x) l (𝓝 c)} :=
begin
by_cases hl : l.ne_bot,
swap, { rw not_ne_bot at hl, simp [hl] },
letI := upgrade_polish_space γ,
rcases l.exists_antitone_basis with ⟨u, hu⟩,
simp_rw ← cauchy_map_iff_exists_tendsto,
change measurable_set {x | _ ∧ _},
have : ∀ x, ((map (λ i, f i x) l) ×ᶠ (map (λ i, f i x) l)).has_antitone_basis
(λ n, ((λ i, f i x) '' u n) ×ˢ ((λ i, f i x) '' u n)) := λ x, hu.map.prod hu.map,
simp_rw [and_iff_right (hl.map _), filter.has_basis.le_basis_iff (this _).to_has_basis
metric.uniformity_basis_dist_inv_nat_succ, set.set_of_forall],
refine measurable_set.bInter set.countable_univ (λ K _, _),
simp_rw set.set_of_exists,
refine measurable_set.bUnion set.countable_univ (λ N hN, _),
simp_rw [prod_image_image_eq, image_subset_iff, prod_subset_iff, set.set_of_forall],
exact measurable_set.bInter (to_countable (u N)) (λ i _,
measurable_set.bInter (to_countable (u N)) (λ j _,
measurable_set_lt (measurable.dist (hf i) (hf j)) measurable_const)),
end
end measure_theory
/-! ### The Borel Isomorphism Theorem -/
/-Note: Move to topology/metric_space/polish when porting. -/
@[priority 50]
instance polish_of_countable [h : countable α] [discrete_topology α] : polish_space α :=
begin
obtain ⟨f, hf⟩ := h.exists_injective_nat,
have : closed_embedding f,
{ apply closed_embedding_of_continuous_injective_closed continuous_of_discrete_topology hf,
exact λ t _, is_closed_discrete _, },
exact this.polish_space,
end
namespace polish_space
/-Note: This is to avoid a loop in TC inference. When ported to Lean 4, this will not
be necessary, and `second_countable_of_polish` should probably
just be added as an instance soon after the definition of `polish_space`.-/
private lemma second_countable_of_polish [h : polish_space α] : second_countable_topology α :=
h.second_countable
local attribute [-instance] polish_space_of_complete_second_countable
local attribute [instance] second_countable_of_polish
variables {β : Type*} [topological_space β] [polish_space α] [polish_space β]
variables [measurable_space α] [measurable_space β] [borel_space α] [borel_space β]
/-- If two Polish spaces admit Borel measurable injections to one another,
then they are Borel isomorphic.-/
noncomputable
def borel_schroeder_bernstein
{f : α → β} {g : β → α}
(fmeas : measurable f) (finj : function.injective f)
(gmeas : measurable g) (ginj : function.injective g) :
α ≃ᵐ β :=
(fmeas.measurable_embedding finj).schroeder_bernstein (gmeas.measurable_embedding ginj)
/-- Any uncountable Polish space is Borel isomorphic to the Cantor space `ℕ → bool`.-/
noncomputable
def measurable_equiv_nat_bool_of_not_countable (h : ¬ countable α) : α ≃ᵐ (ℕ → bool) :=
begin
apply nonempty.some,
obtain ⟨f, -, fcts, finj⟩ := is_closed_univ.exists_nat_bool_injection_of_not_countable
(by rwa [← countable_coe_iff, (equiv.set.univ _).countable_iff]),
obtain ⟨g, gmeas, ginj⟩ :=
measurable_space.measurable_injection_nat_bool_of_countably_generated α,
exact ⟨borel_schroeder_bernstein gmeas ginj fcts.measurable finj⟩,
end
/-- The **Borel Isomorphism Theorem**: Any two uncountable Polish spaces are Borel isomorphic.-/
noncomputable
def measurable_equiv_of_not_countable (hα : ¬ countable α) (hβ : ¬ countable β ) : α ≃ᵐ β :=
(measurable_equiv_nat_bool_of_not_countable hα).trans
(measurable_equiv_nat_bool_of_not_countable hβ).symm
/-- The **Borel Isomorphism Theorem**: If two Polish spaces have the same cardinality,
they are Borel isomorphic.-/
noncomputable
def equiv.measurable_equiv (e : α ≃ β) : α ≃ᵐ β :=
begin
by_cases h : countable α,
{ letI := h,
letI := countable.of_equiv α e,
use e; apply measurable_of_countable, },
refine measurable_equiv_of_not_countable h _,
rwa e.countable_iff at h,
end
end polish_space
namespace measure_theory
-- todo after the port: move to topology/metric_space/polish
instance [polish_space α] : polish_space (univ : set α) := is_closed_univ.polish_space
variables (α) [measurable_space α] [polish_space α] [borel_space α]
lemma exists_nat_measurable_equiv_range_coe_fin_of_finite [finite α] :
∃ n : ℕ, nonempty (α ≃ᵐ range (coe : fin n → ℝ)) :=
begin
obtain ⟨n, ⟨n_equiv⟩⟩ := finite.exists_equiv_fin α,
refine ⟨n, ⟨polish_space.equiv.measurable_equiv (n_equiv.trans _)⟩⟩,
exact equiv.of_injective _ (nat.cast_injective.comp fin.val_injective),
end
lemma measurable_equiv_range_coe_nat_of_infinite_of_countable [infinite α] [countable α] :
nonempty (α ≃ᵐ range (coe : ℕ → ℝ)) :=
begin
haveI : polish_space (range (coe : ℕ → ℝ)),
{ exact nat.closed_embedding_coe_real.is_closed_map.closed_range.polish_space, },
refine ⟨polish_space.equiv.measurable_equiv _⟩,
refine (nonempty_equiv_of_countable.some : α ≃ ℕ).trans _,
exact equiv.of_injective coe nat.cast_injective,
end
/-- Any Polish Borel space is measurably equivalent to a subset of the reals. -/
theorem exists_subset_real_measurable_equiv : ∃ s : set ℝ, measurable_set s ∧ nonempty (α ≃ᵐ s) :=
begin
by_cases hα : countable α,
{ casesI finite_or_infinite α,
{ obtain ⟨n, h_nonempty_equiv⟩ := exists_nat_measurable_equiv_range_coe_fin_of_finite α,
refine ⟨_, _, h_nonempty_equiv⟩,
letI : measurable_space (fin n) := borel (fin n),
haveI : borel_space (fin n) := ⟨rfl⟩,
refine measurable_embedding.measurable_set_range _,
{ apply_instance, },
{ exact continuous_of_discrete_topology.measurable_embedding
(nat.cast_injective.comp fin.val_injective), }, },
{ refine ⟨_, _, measurable_equiv_range_coe_nat_of_infinite_of_countable α⟩,
refine measurable_embedding.measurable_set_range _,
{ apply_instance, },
{ exact continuous_of_discrete_topology.measurable_embedding nat.cast_injective, }, }, },
{ refine ⟨univ, measurable_set.univ,
⟨(polish_space.measurable_equiv_of_not_countable hα _ : α ≃ᵐ (univ : set ℝ))⟩⟩,
rw countable_coe_iff,
exact cardinal.not_countable_real, }
end
/-- Any Polish Borel space embeds measurably into the reals. -/
theorem exists_measurable_embedding_real : ∃ (f : α → ℝ), measurable_embedding f :=
begin
obtain ⟨s, hs, ⟨e⟩⟩ := exists_subset_real_measurable_equiv α,
exact ⟨coe ∘ e, (measurable_embedding.subtype_coe hs).comp e.measurable_embedding⟩,
end
end measure_theory
|
6ae54de2cdda1a57b2575ec9e203d61374e4f1c9 | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/group_theory/coset.lean | 8b313d207d57475cd70072dbbc4cbfffcb8ea0d2 | [
"Apache-2.0"
] | permissive | ntzwq/mathlib | ca50b21079b0a7c6781c34b62199a396dd00cee2 | 36eec1a98f22df82eaccd354a758ef8576af2a7f | refs/heads/master | 1,675,193,391,478 | 1,607,822,996,000 | 1,607,822,996,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,402 | lean | /-
Copyright (c) 2018 Mitchell Rowett. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mitchell Rowett, Scott Morrison
-/
import group_theory.subgroup
open set function
variable {α : Type*}
/-- The left coset `a*s` corresponding to an element `a : α` and a subset `s : set α` -/
@[to_additive left_add_coset "The left coset `a+s` corresponding to an element `a : α`
and a subset `s : set α`"]
def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s
/-- The right coset `s*a` corresponding to an element `a : α` and a subset `s : set α` -/
@[to_additive right_add_coset "The right coset `s+a` corresponding to an element `a : α`
and a subset `s : set α`"]
def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s
localized "infix ` *l `:70 := left_coset" in coset
localized "infix ` +l `:70 := left_add_coset" in coset
localized "infix ` *r `:70 := right_coset" in coset
localized "infix ` +r `:70 := right_add_coset" in coset
section coset_mul
variable [has_mul α]
@[to_additive mem_left_add_coset]
lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s :=
mem_image_of_mem (λ b : α, a * b) hxS
@[to_additive mem_right_add_coset]
lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a :=
mem_image_of_mem (λ b : α, b * a) hxS
/-- Equality of two left cosets `a*s` and `b*s` -/
@[to_additive left_add_coset_equiv "Equality of two left cosets `a+s` and `b+s`"]
def left_coset_equiv (s : set α) (a b : α) := a *l s = b *l s
@[to_additive left_add_coset_equiv_rel]
lemma left_coset_equiv_rel (s : set α) : equivalence (left_coset_equiv s) :=
mk_equivalence (left_coset_equiv s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans)
end coset_mul
section coset_semigroup
variable [semigroup α]
@[simp] lemma left_coset_assoc (s : set α) (a b : α) : a *l (b *l s) = (a * b) *l s :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
attribute [to_additive left_add_coset_assoc] left_coset_assoc
@[simp] lemma right_coset_assoc (s : set α) (a b : α) : s *r a *r b = s *r (a * b) :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
attribute [to_additive right_add_coset_assoc] right_coset_assoc
@[to_additive left_add_coset_right_add_coset]
lemma left_coset_right_coset (s : set α) (a b : α) : a *l s *r b = a *l (s *r b) :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
end coset_semigroup
section coset_monoid
variables [monoid α] (s : set α)
@[simp] lemma one_left_coset : 1 *l s = s :=
set.ext $ by simp [left_coset]
attribute [to_additive zero_left_add_coset] one_left_coset
@[simp] lemma right_coset_one : s *r 1 = s :=
set.ext $ by simp [right_coset]
attribute [to_additive right_add_coset_zero] right_coset_one
end coset_monoid
section coset_submonoid
open submonoid
variables [monoid α] (s : submonoid α)
@[to_additive mem_own_left_add_coset]
lemma mem_own_left_coset (a : α) : a ∈ a *l s :=
suffices a * 1 ∈ a *l s, by simpa,
mem_left_coset a (one_mem s)
@[to_additive mem_own_right_add_coset]
lemma mem_own_right_coset (a : α) : a ∈ (s : set α) *r a :=
suffices 1 * a ∈ (s : set α) *r a, by simpa,
mem_right_coset a (one_mem s)
@[to_additive mem_left_add_coset_left_add_coset]
lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s :=
by rw [←submonoid.mem_coe, ←ha]; exact mem_own_left_coset s a
@[to_additive mem_right_add_coset_right_add_coset]
lemma mem_right_coset_right_coset {a : α} (ha : (s : set α) *r a = s) : a ∈ s :=
by rw [←submonoid.mem_coe, ←ha]; exact mem_own_right_coset s a
end coset_submonoid
section coset_group
variables [group α] {s : set α} {x : α}
@[to_additive mem_left_add_coset_iff]
lemma mem_left_coset_iff (a : α) : x ∈ a *l s ↔ a⁻¹ * x ∈ s :=
iff.intro
(assume ⟨b, hb, eq⟩, by simp [eq.symm, hb])
(assume h, ⟨a⁻¹ * x, h, by simp⟩)
@[to_additive mem_right_add_coset_iff]
lemma mem_right_coset_iff (a : α) : x ∈ s *r a ↔ x * a⁻¹ ∈ s :=
iff.intro
(assume ⟨b, hb, eq⟩, by simp [eq.symm, hb])
(assume h, ⟨x * a⁻¹, h, by simp⟩)
end coset_group
section coset_subgroup
open subgroup
variables [group α] (s : subgroup α)
@[to_additive left_add_coset_mem_left_add_coset]
lemma left_coset_mem_left_coset {a : α} (ha : a ∈ s) : a *l s = s :=
set.ext $ by simp [mem_left_coset_iff, mul_mem_cancel_left s (s.inv_mem ha)]
@[to_additive right_add_coset_mem_right_add_coset]
lemma right_coset_mem_right_coset {a : α} (ha : a ∈ s) : (s : set α) *r a = s :=
set.ext $ assume b, by simp [mem_right_coset_iff, mul_mem_cancel_right s (s.inv_mem ha)]
@[to_additive normal_of_eq_add_cosets]
theorem normal_of_eq_cosets (N : s.normal) (g : α) : g *l s = s *r g :=
set.ext $ assume a, by simp [mem_left_coset_iff, mem_right_coset_iff]; rw [N.mem_comm_iff]
@[to_additive eq_add_cosets_of_normal]
theorem eq_cosets_of_normal (h : ∀ g : α, g *l s = s *r g) : s.normal :=
⟨assume a ha g, show g * a * g⁻¹ ∈ (s : set α),
by rw [← mem_right_coset_iff, ← h]; exact mem_left_coset g ha⟩
@[to_additive normal_iff_eq_add_cosets]
theorem normal_iff_eq_cosets : s.normal ↔ ∀ g : α, g *l s = s *r g :=
⟨@normal_of_eq_cosets _ _ s, eq_cosets_of_normal s⟩
end coset_subgroup
run_cmd to_additive.map_namespace `quotient_group `quotient_add_group
namespace quotient_group
/-- The equivalence relation corresponding to the partition of a group by left cosets
of a subgroup.-/
@[to_additive "The equivalence relation corresponding to the partition of a group by left cosets
of a subgroup."]
def left_rel [group α] (s : subgroup α) : setoid α :=
⟨λ x y, x⁻¹ * y ∈ s,
assume x, by simp [s.one_mem],
assume x y hxy,
have (x⁻¹ * y)⁻¹ ∈ s, from s.inv_mem hxy,
by simpa using this,
assume x y z hxy hyz,
have x⁻¹ * y * (y⁻¹ * z) ∈ s, from s.mul_mem hxy hyz,
by simpa [mul_assoc] using this⟩
/-- `quotient s` is the quotient type representing the left cosets of `s`.
If `s` is a normal subgroup, `quotient s` is a group -/
def quotient [group α] (s : subgroup α) : Type* := quotient (left_rel s)
section
open_locale classical
noncomputable instance [group α] [fintype α] (s : subgroup α) : fintype (quotient_group.quotient s) :=
quotient.fintype _
end
end quotient_group
namespace quotient_add_group
/-- `quotient s` is the quotient type representing the left cosets of `s`.
If `s` is a normal subgroup, `quotient s` is a group -/
def quotient [add_group α] (s : add_subgroup α) : Type* := quotient (left_rel s)
end quotient_add_group
attribute [to_additive quotient_add_group.quotient] quotient_group.quotient
namespace quotient_group
variables [group α] {s : subgroup α}
/-- The canonical map from a group `α` to the quotient `α/s`. -/
@[to_additive "The canonical map from an `add_group` `α` to the quotient `α/s`."]
def mk (a : α) : quotient s :=
quotient.mk' a
@[elab_as_eliminator, to_additive]
lemma induction_on {C : quotient s → Prop} (x : quotient s)
(H : ∀ z, C (quotient_group.mk z)) : C x :=
quotient.induction_on' x H
@[to_additive]
instance : has_coe_t α (quotient s) := ⟨mk⟩ -- note [use has_coe_t]
@[elab_as_eliminator, to_additive]
lemma induction_on' {C : quotient s → Prop} (x : quotient s)
(H : ∀ z : α, C z) : C x :=
quotient.induction_on' x H
@[to_additive]
instance (s : subgroup α) : inhabited (quotient s) :=
⟨((1 : α) : quotient s)⟩
@[to_additive quotient_add_group.eq]
protected lemma eq {a b : α} : (a : quotient s) = b ↔ a⁻¹ * b ∈ s :=
quotient.eq'
@[to_additive]
lemma eq_class_eq_left_coset (s : subgroup α) (g : α) :
{x : α | (x : quotient s) = g} = left_coset g s :=
set.ext $ λ z, by { rw [mem_left_coset_iff, set.mem_set_of_eq, eq_comm, quotient_group.eq], simp }
@[to_additive]
lemma preimage_image_coe (N : subgroup α) (s : set α) :
coe ⁻¹' ((coe : α → quotient N) '' s) = ⋃ x : N, (λ y : α, y * x) '' s :=
begin
ext x,
simp only [quotient_group.eq, subgroup.exists, exists_prop, set.mem_preimage, set.mem_Union,
set.mem_image, subgroup.coe_mk, ← eq_inv_mul_iff_mul_eq],
exact ⟨λ ⟨y, hs, hN⟩, ⟨_, hN, y, hs, rfl⟩, λ ⟨z, hN, y, hs, hyz⟩, ⟨y, hs, hyz ▸ hN⟩⟩
end
end quotient_group
namespace subgroup
open quotient_group
variables [group α] {s : subgroup α}
/-- The natural bijection between the cosets `g*s` and `s` -/
@[to_additive "The natural bijection between the cosets `g+s` and `s`"]
def left_coset_equiv_subgroup (g : α) : left_coset g s ≃ s :=
⟨λ x, ⟨g⁻¹ * x.1, (mem_left_coset_iff _).1 x.2⟩,
λ x, ⟨g * x.1, x.1, x.2, rfl⟩,
λ ⟨x, hx⟩, subtype.eq $ by simp,
λ ⟨g, hg⟩, subtype.eq $ by simp⟩
/-- A (non-canonical) bijection between a group `α` and the product `(α/s) × s` -/
@[to_additive "A (non-canonical) bijection between an add_group `α` and the product `(α/s) × s`"]
noncomputable def group_equiv_quotient_times_subgroup :
α ≃ quotient s × s :=
calc α ≃ Σ L : quotient s, {x : α // (x : quotient s) = L} :
(equiv.sigma_preimage_equiv quotient_group.mk).symm
... ≃ Σ L : quotient s, left_coset (quotient.out' L) s :
equiv.sigma_congr_right (λ L,
begin
rw ← eq_class_eq_left_coset,
show _root_.subtype (λ x : α, quotient.mk' x = L) ≃ _root_.subtype (λ x : α, quotient.mk' x = quotient.mk' _),
simp [-quotient.eq'],
end)
... ≃ Σ L : quotient s, s :
equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _)
... ≃ quotient s × s :
equiv.sigma_equiv_prod _ _
end subgroup
namespace quotient_group
variables [group α]
-- FIXME -- why is there no `to_additive`?
/-- If `s` is a subgroup of the group `α`, and `t` is a subset of `α/s`, then
there is a (typically non-canonical) bijection between the preimage of `t` in
`α` and the product `s × t`. -/
noncomputable def preimage_mk_equiv_subgroup_times_set
(s : subgroup α) (t : set (quotient s)) : quotient_group.mk ⁻¹' t ≃ s × t :=
have h : ∀ {x : quotient s} {a : α}, x ∈ t → a ∈ s →
(quotient.mk' (quotient.out' x * a) : quotient s) = quotient.mk' (quotient.out' x) :=
λ x a hx ha, quotient.sound' (show (quotient.out' x * a)⁻¹ * quotient.out' x ∈ s,
from (s.inv_mem_iff).1 $
by rwa [mul_inv_rev, inv_inv, ← mul_assoc, inv_mul_self, one_mul]),
{ to_fun := λ ⟨a, ha⟩, ⟨⟨(quotient.out' (quotient.mk' a))⁻¹ * a,
@quotient.exact' _ (left_rel s) _ _ $ (quotient.out_eq' _)⟩,
⟨quotient.mk' a, ha⟩⟩,
inv_fun := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, ⟨quotient.out' x * a, show quotient.mk' _ ∈ t,
by simp [h hx ha, hx]⟩,
left_inv := λ ⟨a, ha⟩, subtype.eq $ show _ * _ = a, by simp,
right_inv := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, show (_, _) = _, by simp [h hx ha] }
end quotient_group
/--
We use the class `has_coe_t` instead of `has_coe` if the first argument is a variable,
or if the second argument is a variable not occurring in the first.
Using `has_coe` would cause looping of type-class inference. See
<https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/remove.20all.20instances.20with.20variable.20domain>
-/
library_note "use has_coe_t"
|
b012bedb02df499be08a7fbb0955a3317bec44c3 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/linear_algebra/prod.lean | a2898792568a73b0c0a7569ff5a9cd812ee2f8c8 | [
"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 | 30,199 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Eric Wieser
-/
import linear_algebra.span
import order.partial_sups
import algebra.algebra.basic
/-! ### Products of modules
This file defines constructors for linear maps whose domains or codomains are products.
It contains theorems relating these to each other, as well as to `submodule.prod`, `submodule.map`,
`submodule.comap`, `linear_map.range`, and `linear_map.ker`.
## Main definitions
- products in the domain:
- `linear_map.fst`
- `linear_map.snd`
- `linear_map.coprod`
- `linear_map.prod_ext`
- products in the codomain:
- `linear_map.inl`
- `linear_map.inr`
- `linear_map.prod`
- products in both domain and codomain:
- `linear_map.prod_map`
- `linear_equiv.prod_map`
- `linear_equiv.skew_prod`
-/
universes u v w x y z u' v' w' y'
variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'}
variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x}
variables {M₅ M₆ : Type*}
section prod
namespace linear_map
variables (S : Type*) [semiring R] [semiring S]
variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄]
variables [add_comm_monoid M₅] [add_comm_monoid M₆]
variables [module R M] [module R M₂] [module R M₃] [module R M₄]
variables [module R M₅] [module R M₆]
variables (f : M →ₗ[R] M₂)
section
variables (R M M₂)
/-- The first projection of a product is a linear map. -/
def fst : M × M₂ →ₗ[R] M := { to_fun := prod.fst, map_add' := λ x y, rfl, map_smul' := λ x y, rfl }
/-- The second projection of a product is a linear map. -/
def snd : M × M₂ →ₗ[R] M₂ := { to_fun := prod.snd, map_add' := λ x y, rfl, map_smul' := λ x y, rfl }
end
@[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl
@[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl
theorem fst_surjective : function.surjective (fst R M M₂) := λ x, ⟨(x, 0), rfl⟩
theorem snd_surjective : function.surjective (snd R M M₂) := λ x, ⟨(0, x), rfl⟩
/-- The prod of two linear maps is a linear map. -/
@[simps] def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (M →ₗ[R] M₂ × M₃) :=
{ to_fun := pi.prod f g,
map_add' := λ x y, by simp only [pi.prod, prod.mk_add_mk, map_add],
map_smul' := λ c x, by simp only [pi.prod, prod.smul_mk, map_smul, ring_hom.id_apply] }
lemma coe_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ⇑(f.prod g) = pi.prod f g := rfl
@[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) :
(fst R M₂ M₃).comp (prod f g) = f := by ext; refl
@[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) :
(snd R M₂ M₃).comp (prod f g) = g := by ext; refl
@[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = linear_map.id :=
fun_like.coe_injective pi.prod_fst_snd
/-- Taking the product of two maps with the same domain is equivalent to taking the product of
their codomains.
See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/
@[simps] def prod_equiv
[module S M₂] [module S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] :
((M →ₗ[R] M₂) × (M →ₗ[R] M₃)) ≃ₗ[S] (M →ₗ[R] M₂ × M₃) :=
{ to_fun := λ f, f.1.prod f.2,
inv_fun := λ f, ((fst _ _ _).comp f, (snd _ _ _).comp f),
left_inv := λ f, by ext; refl,
right_inv := λ f, by ext; refl,
map_add' := λ a b, rfl,
map_smul' := λ r a, rfl }
section
variables (R M M₂)
/-- The left injection into a product is a linear map. -/
def inl : M →ₗ[R] M × M₂ := prod linear_map.id 0
/-- The right injection into a product is a linear map. -/
def inr : M₂ →ₗ[R] M × M₂ := prod 0 linear_map.id
theorem range_inl : range (inl R M M₂) = ker (snd R M M₂) :=
begin
ext x,
simp only [mem_ker, mem_range],
split,
{ rintros ⟨y, rfl⟩, refl },
{ intro h, exact ⟨x.fst, prod.ext rfl h.symm⟩ }
end
theorem ker_snd : ker (snd R M M₂) = range (inl R M M₂) :=
eq.symm $ range_inl R M M₂
theorem range_inr : range (inr R M M₂) = ker (fst R M M₂) :=
begin
ext x,
simp only [mem_ker, mem_range],
split,
{ rintros ⟨y, rfl⟩, refl },
{ intro h, exact ⟨x.snd, prod.ext h.symm rfl⟩ }
end
theorem ker_fst : ker (fst R M M₂) = range (inr R M M₂) :=
eq.symm $ range_inr R M M₂
end
@[simp] theorem coe_inl : (inl R M M₂ : M → M × M₂) = λ x, (x, 0) := rfl
theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl
@[simp] theorem coe_inr : (inr R M M₂ : M₂ → M × M₂) = prod.mk 0 := rfl
theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl
theorem inl_eq_prod : inl R M M₂ = prod linear_map.id 0 := rfl
theorem inr_eq_prod : inr R M M₂ = prod 0 linear_map.id := rfl
theorem inl_injective : function.injective (inl R M M₂) :=
λ _, by simp
theorem inr_injective : function.injective (inr R M M₂) :=
λ _, by simp
/-- The coprod function `λ x : M × M₂, f x.1 + g x.2` is a linear map. -/
def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ :=
f.comp (fst _ _ _) + g.comp (snd _ _ _)
@[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M × M₂) :
coprod f g x = f x.1 + g x.2 := rfl
@[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) :
(coprod f g).comp (inl R M M₂) = f :=
by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply]
@[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) :
(coprod f g).comp (inr R M M₂) = g :=
by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply]
@[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = linear_map.id :=
by ext; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply,
inl_apply, inr_apply, zero_add]
theorem comp_coprod (f : M₃ →ₗ[R] M₄) (g₁ : M →ₗ[R] M₃) (g₂ : M₂ →ₗ[R] M₃) :
f.comp (g₁.coprod g₂) = (f.comp g₁).coprod (f.comp g₂) :=
ext $ λ x, f.map_add (g₁ x.1) (g₂ x.2)
theorem fst_eq_coprod : fst R M M₂ = coprod linear_map.id 0 := by ext; simp
theorem snd_eq_coprod : snd R M M₂ = coprod 0 linear_map.id := by ext; simp
@[simp] theorem coprod_comp_prod (f : M₂ →ₗ[R] M₄) (g : M₃ →ₗ[R] M₄)
(f' : M →ₗ[R] M₂) (g' : M →ₗ[R] M₃) :
(f.coprod g).comp (f'.prod g') = f.comp f' + g.comp g' :=
rfl
@[simp]
lemma coprod_map_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (S : submodule R M)
(S' : submodule R M₂) :
(submodule.prod S S').map (linear_map.coprod f g) = S.map f ⊔ S'.map g :=
set_like.coe_injective $ begin
simp only [linear_map.coprod_apply, submodule.coe_sup, submodule.map_coe],
rw [←set.image2_add, set.image2_image_left, set.image2_image_right],
exact set.image_prod (λ m m₂, f m + g m₂),
end
/-- Taking the product of two maps with the same codomain is equivalent to taking the product of
their domains.
See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/
@[simps] def coprod_equiv [module S M₃] [smul_comm_class R S M₃] :
((M →ₗ[R] M₃) × (M₂ →ₗ[R] M₃)) ≃ₗ[S] (M × M₂ →ₗ[R] M₃) :=
{ to_fun := λ f, f.1.coprod f.2,
inv_fun := λ f, (f.comp (inl _ _ _), f.comp (inr _ _ _)),
left_inv := λ f, by simp only [prod.mk.eta, coprod_inl, coprod_inr],
right_inv := λ f, by simp only [←comp_coprod, comp_id, coprod_inl_inr],
map_add' := λ a b,
by { ext, simp only [prod.snd_add, add_apply, coprod_apply, prod.fst_add, add_add_add_comm] },
map_smul' := λ r a,
by { dsimp, ext, simp only [smul_add, smul_apply, prod.smul_snd, prod.smul_fst,
coprod_apply] } }
theorem prod_ext_iff {f g : M × M₂ →ₗ[R] M₃} :
f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) :=
(coprod_equiv ℕ).symm.injective.eq_iff.symm.trans prod.ext_iff
/--
Split equality of linear maps from a product into linear maps over each component, to allow `ext`
to apply lemmas specific to `M →ₗ M₃` and `M₂ →ₗ M₃`.
See note [partially-applied ext lemmas]. -/
@[ext] theorem prod_ext {f g : M × M₂ →ₗ[R] M₃}
(hl : f.comp (inl _ _ _) = g.comp (inl _ _ _))
(hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) :
f = g :=
prod_ext_iff.2 ⟨hl, hr⟩
/-- `prod.map` of two linear maps. -/
def prod_map (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : (M × M₂) →ₗ[R] (M₃ × M₄) :=
(f.comp (fst R M M₂)).prod (g.comp (snd R M M₂))
@[simp] theorem prod_map_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) :
f.prod_map g x = (f x.1, g x.2) := rfl
lemma prod_map_comap_prod (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) (S : submodule R M₂)
(S' : submodule R M₄) :
(submodule.prod S S').comap (linear_map.prod_map f g) = (S.comap f).prod (S'.comap g) :=
set_like.coe_injective $ set.preimage_prod_map_prod f g _ _
lemma ker_prod_map (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) :
(linear_map.prod_map f g).ker = submodule.prod f.ker g.ker :=
begin
dsimp only [ker],
rw [←prod_map_comap_prod, submodule.prod_bot],
end
@[simp]
lemma prod_map_id : (id : M →ₗ[R] M).prod_map (id : M₂ →ₗ[R] M₂) = id :=
linear_map.ext $ λ _, prod.mk.eta
@[simp]
lemma prod_map_one : (1 : M →ₗ[R] M).prod_map (1 : M₂ →ₗ[R] M₂) = 1 :=
linear_map.ext $ λ _, prod.mk.eta
lemma prod_map_comp (f₁₂ : M →ₗ[R] M₂) (f₂₃ : M₂ →ₗ[R] M₃) (g₁₂ : M₄ →ₗ[R] M₅) (g₂₃ : M₅ →ₗ[R] M₆) :
f₂₃.prod_map g₂₃ ∘ₗ f₁₂.prod_map g₁₂ = (f₂₃ ∘ₗ f₁₂).prod_map (g₂₃ ∘ₗ g₁₂) := rfl
lemma prod_map_mul (f₁₂ : M →ₗ[R] M) (f₂₃ : M →ₗ[R] M) (g₁₂ : M₂ →ₗ[R] M₂) (g₂₃ : M₂ →ₗ[R] M₂) :
f₂₃.prod_map g₂₃ * f₁₂.prod_map g₁₂ = (f₂₃ * f₁₂).prod_map (g₂₃ * g₁₂) := rfl
lemma prod_map_add (f₁ : M →ₗ[R] M₃) (f₂ : M →ₗ[R] M₃) (g₁ : M₂ →ₗ[R] M₄) (g₂ : M₂ →ₗ[R] M₄) :
(f₁ + f₂).prod_map (g₁ + g₂) = f₁.prod_map g₁ + f₂.prod_map g₂ := rfl
@[simp] lemma prod_map_zero :
(0 : M →ₗ[R] M₂).prod_map (0 : M₃ →ₗ[R] M₄) = 0 := rfl
@[simp] lemma prod_map_smul
[module S M₃] [module S M₄] [smul_comm_class R S M₃] [smul_comm_class R S M₄]
(s : S) (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : prod_map (s • f) (s • g) = s • prod_map f g := rfl
variables (R M M₂ M₃ M₄)
/-- `linear_map.prod_map` as a `linear_map` -/
@[simps]
def prod_map_linear
[module S M₃] [module S M₄] [smul_comm_class R S M₃] [smul_comm_class R S M₄] :
((M →ₗ[R] M₃) × (M₂ →ₗ[R] M₄)) →ₗ[S] ((M × M₂) →ₗ[R] (M₃ × M₄)) :=
{ to_fun := λ f, prod_map f.1 f.2,
map_add' := λ _ _, rfl,
map_smul' := λ _ _, rfl}
/-- `linear_map.prod_map` as a `ring_hom` -/
@[simps]
def prod_map_ring_hom : (M →ₗ[R] M) × (M₂ →ₗ[R] M₂) →+* ((M × M₂) →ₗ[R] (M × M₂)) :=
{ to_fun := λ f, prod_map f.1 f.2,
map_one' := prod_map_one,
map_zero' := rfl,
map_add' := λ _ _, rfl,
map_mul' := λ _ _, rfl }
variables {R M M₂ M₃ M₄}
section map_mul
variables {A : Type*} [non_unital_non_assoc_semiring A] [module R A]
variables {B : Type*} [non_unital_non_assoc_semiring B] [module R B]
lemma inl_map_mul (a₁ a₂ : A) : linear_map.inl R A B (a₁ * a₂) =
linear_map.inl R A B a₁ * linear_map.inl R A B a₂ :=
prod.ext rfl (by simp)
lemma inr_map_mul (b₁ b₂ : B) : linear_map.inr R A B (b₁ * b₂) =
linear_map.inr R A B b₁ * linear_map.inr R A B b₂ :=
prod.ext (by simp) rfl
end map_mul
end linear_map
end prod
namespace linear_map
variables (R M M₂)
variables [comm_semiring R]
variables [add_comm_monoid M] [add_comm_monoid M₂]
variables [module R M] [module R M₂]
/-- `linear_map.prod_map` as an `algebra_hom` -/
@[simps]
def prod_map_alg_hom : (module.End R M) × (module.End R M₂) →ₐ[R] module.End R (M × M₂) :=
{ commutes' := λ _, rfl, ..prod_map_ring_hom R M M₂ }
end linear_map
namespace linear_map
open submodule
variables [semiring R]
[add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄]
[module R M] [module R M₂] [module R M₃] [module R M₄]
lemma range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) :
(f.coprod g).range = f.range ⊔ g.range :=
submodule.ext $ λ x, by simp [mem_sup]
lemma is_compl_range_inl_inr : is_compl (inl R M M₂).range (inr R M M₂).range :=
begin
split,
{ rintros ⟨_, _⟩ ⟨⟨x, hx⟩, ⟨y, hy⟩⟩,
simp only [prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊢,
exact ⟨hy.1.symm, hx.2.symm⟩ },
{ rintros ⟨x, y⟩ -,
simp only [mem_sup, mem_range, exists_prop],
refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, _⟩,
simp }
end
lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ :=
is_compl_range_inl_inr.sup_eq_top
lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range :=
by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl
theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃)
(p : submodule R M) (q : submodule R M₂) :
map (coprod f g) (p.prod q) = map f p ⊔ map g q :=
begin
refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)),
{ rw set_like.le_def, rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩,
exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ },
{ exact λ x hx, ⟨(x, 0), by simp [hx]⟩ },
{ exact λ x hx, ⟨(0, x), by simp [hx]⟩ }
end
theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃)
(p : submodule R M₂) (q : submodule R M₃) :
comap (prod f g) (p.prod q) = comap f p ⊓ comap g q :=
submodule.ext $ λ x, iff.rfl
theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) :
p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) :=
submodule.ext $ λ x, iff.rfl
theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) :
p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) :=
by rw [← map_coprod_prod, coprod_inl_inr, map_id]
lemma span_inl_union_inr {s : set M} {t : set M₂} :
span R (inl R M M₂ '' s ∪ inr R M M₂ '' t) = (span R s).prod (span R t) :=
by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]
@[simp] lemma ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) :
ker (prod f g) = ker f ⊓ ker g :=
by rw [ker, ← prod_bot, comap_prod_prod]; refl
lemma range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) :
range (prod f g) ≤ (range f).prod (range g) :=
begin
simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod,
exists_imp_distrib],
rintro _ x rfl,
exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩
end
lemma ker_prod_ker_le_ker_coprod {M₂ : Type*} [add_comm_group M₂] [module R M₂]
{M₃ : Type*} [add_comm_group M₃] [module R M₃]
(f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) :
(ker f).prod (ker g) ≤ ker (f.coprod g) :=
by { rintros ⟨y, z⟩, simp {contextual := tt} }
lemma ker_coprod_of_disjoint_range {M₂ : Type*} [add_comm_group M₂] [module R M₂]
{M₃ : Type*} [add_comm_group M₃] [module R M₃]
(f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (hd : disjoint f.range g.range) :
ker (f.coprod g) = (ker f).prod (ker g) :=
begin
apply le_antisymm _ (ker_prod_ker_le_ker_coprod f g),
rintros ⟨y, z⟩ h,
simp only [mem_ker, mem_prod, coprod_apply] at h ⊢,
have : f y ∈ f.range ⊓ g.range,
{ simp only [true_and, mem_range, mem_inf, exists_apply_eq_apply],
use -z,
rwa [eq_comm, map_neg, ← sub_eq_zero, sub_neg_eq_add] },
rw [hd.eq_bot, mem_bot] at this,
rw [this] at h,
simpa [this] using h,
end
end linear_map
namespace submodule
open linear_map
variables [semiring R]
variables [add_comm_monoid M] [add_comm_monoid M₂]
variables [module R M] [module R M₂]
lemma sup_eq_range (p q : submodule R M) : p ⊔ q = (p.subtype.coprod q.subtype).range :=
submodule.ext $ λ x, by simp [submodule.mem_sup, set_like.exists]
variables (p : submodule R M) (q : submodule R M₂)
@[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ :=
by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot,
exists_eq_left', mem_prod] }
@[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q :=
by ext ⟨x, y⟩; simp [and.left_comm, eq_comm]
@[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ :=
by ext ⟨x, y⟩; simp
@[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q :=
by ext ⟨x, y⟩; simp
@[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp
@[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp
@[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p :=
by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)]
@[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q :=
by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)]
@[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ :=
by rw [ker, ← prod_bot, prod_comap_inl]
@[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ :=
by rw [ker, ← prod_bot, prod_comap_inr]
@[simp] theorem range_fst : (fst R M M₂).range = ⊤ :=
by rw [range_eq_map, ← prod_top, prod_map_fst]
@[simp] theorem range_snd : (snd R M M₂).range = ⊤ :=
by rw [range_eq_map, ← prod_top, prod_map_snd]
variables (R M M₂)
/-- `M` as a submodule of `M × N`. -/
def fst : submodule R (M × M₂) := (⊥ : submodule R M₂).comap (linear_map.snd R M M₂)
/-- `M` as a submodule of `M × N` is isomorphic to `M`. -/
@[simps] def fst_equiv : submodule.fst R M M₂ ≃ₗ[R] M :=
{ to_fun := λ x, x.1.1,
inv_fun := λ m, ⟨⟨m, 0⟩, by tidy⟩,
map_add' := by simp,
map_smul' := by simp,
left_inv := by tidy,
right_inv := by tidy, }
lemma fst_map_fst : (submodule.fst R M M₂).map (linear_map.fst R M M₂) = ⊤ :=
by tidy
lemma fst_map_snd : (submodule.fst R M M₂).map (linear_map.snd R M M₂) = ⊥ :=
by { tidy, exact 0, }
/-- `N` as a submodule of `M × N`. -/
def snd : submodule R (M × M₂) := (⊥ : submodule R M).comap (linear_map.fst R M M₂)
/-- `N` as a submodule of `M × N` is isomorphic to `N`. -/
@[simps] def snd_equiv : submodule.snd R M M₂ ≃ₗ[R] M₂ :=
{ to_fun := λ x, x.1.2,
inv_fun := λ n, ⟨⟨0, n⟩, by tidy⟩,
map_add' := by simp,
map_smul' := by simp,
left_inv := by tidy,
right_inv := by tidy, }
lemma snd_map_fst : (submodule.snd R M M₂).map (linear_map.fst R M M₂) = ⊥ :=
by { tidy, exact 0, }
lemma snd_map_snd : (submodule.snd R M M₂).map (linear_map.snd R M M₂) = ⊤ :=
by tidy
lemma fst_sup_snd : submodule.fst R M M₂ ⊔ submodule.snd R M M₂ = ⊤ :=
begin
rw eq_top_iff,
rintro ⟨m, n⟩ -,
rw [show (m, n) = (m, 0) + (0, n), by simp],
apply submodule.add_mem (submodule.fst R M M₂ ⊔ submodule.snd R M M₂),
{ exact submodule.mem_sup_left (submodule.mem_comap.mpr (by simp)), },
{ exact submodule.mem_sup_right (submodule.mem_comap.mpr (by simp)), },
end
lemma fst_inf_snd : submodule.fst R M M₂ ⊓ submodule.snd R M M₂ = ⊥ := by tidy
lemma le_prod_iff {p₁ : submodule R M} {p₂ : submodule R M₂} {q : submodule R (M × M₂)} :
q ≤ p₁.prod p₂ ↔ map (linear_map.fst R M M₂) q ≤ p₁ ∧ map (linear_map.snd R M M₂) q ≤ p₂ :=
begin
split,
{ intros h,
split,
{ rintros x ⟨⟨y1,y2⟩, ⟨hy1,rfl⟩⟩, exact (h hy1).1 },
{ rintros x ⟨⟨y1,y2⟩, ⟨hy1,rfl⟩⟩, exact (h hy1).2 }, },
{ rintros ⟨hH, hK⟩ ⟨x1, x2⟩ h, exact ⟨hH ⟨_ , h, rfl⟩, hK ⟨ _, h, rfl⟩⟩, }
end
lemma prod_le_iff {p₁ : submodule R M} {p₂ : submodule R M₂} {q : submodule R (M × M₂)} :
p₁.prod p₂ ≤ q ↔ map (linear_map.inl R M M₂) p₁ ≤ q ∧ map (linear_map.inr R M M₂) p₂ ≤ q :=
begin
split,
{ intros h,
split,
{ rintros _ ⟨x, hx, rfl⟩, apply h, exact ⟨hx, zero_mem p₂⟩, },
{ rintros _ ⟨x, hx, rfl⟩, apply h, exact ⟨zero_mem p₁, hx⟩, }, },
{ rintros ⟨hH, hK⟩ ⟨x1, x2⟩ ⟨h1, h2⟩,
have h1' : (linear_map.inl R _ _) x1 ∈ q, { apply hH, simpa using h1, },
have h2' : (linear_map.inr R _ _) x2 ∈ q, { apply hK, simpa using h2, },
simpa using add_mem h1' h2', }
end
lemma prod_eq_bot_iff {p₁ : submodule R M} {p₂ : submodule R M₂} :
p₁.prod p₂ = ⊥ ↔ p₁ = ⊥ ∧ p₂ = ⊥ :=
by simp only [eq_bot_iff, prod_le_iff, (gc_map_comap _).le_iff_le, comap_bot, ker_inl, ker_inr]
lemma prod_eq_top_iff {p₁ : submodule R M} {p₂ : submodule R M₂} :
p₁.prod p₂ = ⊤ ↔ p₁ = ⊤ ∧ p₂ = ⊤ :=
by simp only [eq_top_iff, le_prod_iff, ← (gc_map_comap _).le_iff_le, map_top, range_fst, range_snd]
end submodule
namespace linear_equiv
/-- Product of modules is commutative up to linear isomorphism. -/
@[simps apply]
def prod_comm (R M N : Type*) [semiring R] [add_comm_monoid M] [add_comm_monoid N]
[module R M] [module R N] : (M × N) ≃ₗ[R] (N × M) :=
{ to_fun := prod.swap,
map_smul' := λ r ⟨m, n⟩, rfl,
..add_equiv.prod_comm }
section
variables [semiring R]
variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄]
variables {module_M : module R M} {module_M₂ : module R M₂}
variables {module_M₃ : module R M₃} {module_M₄ : module R M₄}
variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄)
/-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/
protected def prod :
(M × M₃) ≃ₗ[R] (M₂ × M₄) :=
{ map_add' := λ x y, prod.ext (e₁.map_add _ _) (e₂.map_add _ _),
map_smul' := λ c x, prod.ext (e₁.map_smulₛₗ c _) (e₂.map_smulₛₗ c _),
.. equiv.prod_congr e₁.to_equiv e₂.to_equiv }
lemma prod_symm : (e₁.prod e₂).symm = e₁.symm.prod e₂.symm := rfl
@[simp] lemma prod_apply (p) :
e₁.prod e₂ p = (e₁ p.1, e₂ p.2) := rfl
@[simp, norm_cast] lemma coe_prod :
(e₁.prod e₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = (e₁ : M →ₗ[R] M₂).prod_map (e₂ : M₃ →ₗ[R] M₄) := rfl
end
section
variables [semiring R]
variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_group M₄]
variables {module_M : module R M} {module_M₂ : module R M₂}
variables {module_M₃ : module R M₃} {module_M₄ : module R M₄}
variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄)
/-- Equivalence given by a block lower diagonal matrix. `e₁` and `e₂` are diagonal square blocks,
and `f` is a rectangular block below the diagonal. -/
protected def skew_prod (f : M →ₗ[R] M₄) :
(M × M₃) ≃ₗ[R] M₂ × M₄ :=
{ inv_fun := λ p : M₂ × M₄, (e₁.symm p.1, e₂.symm (p.2 - f (e₁.symm p.1))),
left_inv := λ p, by simp,
right_inv := λ p, by simp,
.. ((e₁ : M →ₗ[R] M₂).comp (linear_map.fst R M M₃)).prod
((e₂ : M₃ →ₗ[R] M₄).comp (linear_map.snd R M M₃) +
f.comp (linear_map.fst R M M₃)) }
@[simp] lemma skew_prod_apply (f : M →ₗ[R] M₄) (x) :
e₁.skew_prod e₂ f x = (e₁ x.1, e₂ x.2 + f x.1) := rfl
@[simp] lemma skew_prod_symm_apply (f : M →ₗ[R] M₄) (x) :
(e₁.skew_prod e₂ f).symm x = (e₁.symm x.1, e₂.symm (x.2 - f (e₁.symm x.1))) := rfl
end
end linear_equiv
namespace linear_map
open submodule
variables [ring R]
variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃]
variables [module R M] [module R M₂] [module R M₃]
/-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of
`prod f g` is equal to the product of `range f` and `range g`. -/
lemma range_prod_eq {f : M →ₗ[R] M₂} {g : M →ₗ[R] M₃} (h : ker f ⊔ ker g = ⊤) :
range (prod f g) = (range f).prod (range g) :=
begin
refine le_antisymm (f.range_prod_le g) _,
simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib,
and_imp, prod.forall, pi.prod],
rintros _ _ x rfl y rfl,
simp only [prod.mk.inj_iff, ← sub_mem_ker_iff],
have : y - x ∈ ker f ⊔ ker g, { simp only [h, mem_top] },
rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩,
refine ⟨x' + x, _, _⟩,
{ rwa add_sub_cancel },
{ rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub,
add_sub_cancel'] }
end
end linear_map
namespace linear_map
/-!
## Tunnels and tailings
Some preliminary work for establishing the strong rank condition for noetherian rings.
Given a morphism `f : M × N →ₗ[R] M` which is `i : injective f`,
we can find an infinite decreasing `tunnel f i n` of copies of `M` inside `M`,
and sitting beside these, an infinite sequence of copies of `N`.
We picturesquely name these as `tailing f i n` for each individual copy of `N`,
and `tailings f i n` for the supremum of the first `n+1` copies:
they are the pieces left behind, sitting inside the tunnel.
By construction, each `tailing f i (n+1)` is disjoint from `tailings f i n`;
later, when we assume `M` is noetherian, this implies that `N` must be trivial,
and establishes the strong rank condition for any left-noetherian ring.
-/
section tunnel
-- (This doesn't work over a semiring: we need to use that `submodule R M` is a modular lattice,
-- which requires cancellation.)
variables [ring R]
variables {N : Type*} [add_comm_group M] [module R M] [add_comm_group N] [module R N]
open function
/-- An auxiliary construction for `tunnel`.
The composition of `f`, followed by the isomorphism back to `K`,
followed by the inclusion of this submodule back into `M`. -/
def tunnel_aux (f : M × N →ₗ[R] M) (Kφ : Σ K : submodule R M, K ≃ₗ[R] M) :
M × N →ₗ[R] M :=
(Kφ.1.subtype.comp Kφ.2.symm.to_linear_map).comp f
lemma tunnel_aux_injective
(f : M × N →ₗ[R] M) (i : injective f) (Kφ : Σ K : submodule R M, K ≃ₗ[R] M) :
injective (tunnel_aux f Kφ) :=
(subtype.val_injective.comp Kφ.2.symm.injective).comp i
noncomputable theory
/-- Auxiliary definition for `tunnel`. -/
-- Even though we have `noncomputable theory`,
-- we get an error without another `noncomputable` here.
noncomputable def tunnel' (f : M × N →ₗ[R] M) (i : injective f) :
ℕ → Σ (K : submodule R M), K ≃ₗ[R] M
| 0 := ⟨⊤, linear_equiv.of_top ⊤ rfl⟩
| (n+1) :=
⟨(submodule.fst R M N).map (tunnel_aux f (tunnel' n)),
((submodule.fst R M N).equiv_map_of_injective _ (tunnel_aux_injective f i (tunnel' n))).symm.trans
(submodule.fst_equiv R M N)⟩
/--
Give an injective map `f : M × N →ₗ[R] M` we can find a nested sequence of submodules
all isomorphic to `M`.
-/
def tunnel (f : M × N →ₗ[R] M) (i : injective f) : ℕ →o (submodule R M)ᵒᵈ :=
⟨λ n, (tunnel' f i n).1, monotone_nat_of_le_succ (λ n, begin
dsimp [tunnel', tunnel_aux],
rw [submodule.map_comp, submodule.map_comp],
apply submodule.map_subtype_le,
end)⟩
/--
Give an injective map `f : M × N →ₗ[R] M` we can find a sequence of submodules
all isomorphic to `N`.
-/
def tailing (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : submodule R M :=
(submodule.snd R M N).map (tunnel_aux f (tunnel' f i n))
/-- Each `tailing f i n` is a copy of `N`. -/
def tailing_linear_equiv (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : tailing f i n ≃ₗ[R] N :=
((submodule.snd R M N).equiv_map_of_injective _
(tunnel_aux_injective f i (tunnel' f i n))).symm.trans (submodule.snd_equiv R M N)
lemma tailing_le_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
tailing f i n ≤ tunnel f i n :=
begin
dsimp [tailing, tunnel_aux],
rw [submodule.map_comp, submodule.map_comp],
apply submodule.map_subtype_le,
end
lemma tailing_disjoint_tunnel_succ (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
disjoint (tailing f i n) (tunnel f i (n+1)) :=
begin
rw disjoint_iff,
dsimp [tailing, tunnel, tunnel'],
rw [submodule.map_inf_eq_map_inf_comap,
submodule.comap_map_eq_of_injective (tunnel_aux_injective _ i _), inf_comm,
submodule.fst_inf_snd, submodule.map_bot],
end
lemma tailing_sup_tunnel_succ_le_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
tailing f i n ⊔ tunnel f i (n+1) ≤ tunnel f i n :=
begin
dsimp [tailing, tunnel, tunnel', tunnel_aux],
rw [←submodule.map_sup, sup_comm, submodule.fst_sup_snd, submodule.map_comp, submodule.map_comp],
apply submodule.map_subtype_le,
end
/-- The supremum of all the copies of `N` found inside the tunnel. -/
def tailings (f : M × N →ₗ[R] M) (i : injective f) : ℕ → submodule R M :=
partial_sups (tailing f i)
@[simp] lemma tailings_zero (f : M × N →ₗ[R] M) (i : injective f) :
tailings f i 0 = tailing f i 0 :=
by simp [tailings]
@[simp] lemma tailings_succ (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
tailings f i (n+1) = tailings f i n ⊔ tailing f i (n+1) :=
by simp [tailings]
lemma tailings_disjoint_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
disjoint (tailings f i n) (tunnel f i (n+1)) :=
begin
induction n with n ih,
{ simp only [tailings_zero],
apply tailing_disjoint_tunnel_succ, },
{ simp only [tailings_succ],
refine disjoint.disjoint_sup_left_of_disjoint_sup_right _ _,
apply tailing_disjoint_tunnel_succ,
apply disjoint.mono_right _ ih,
apply tailing_sup_tunnel_succ_le_tunnel, },
end
lemma tailings_disjoint_tailing (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) :
disjoint (tailings f i n) (tailing f i (n+1)) :=
disjoint.mono_right (tailing_le_tunnel f i _) (tailings_disjoint_tunnel f i _)
end tunnel
end linear_map
|
83a915ad4d7ad189a5f05083f3723a764e916508 | 675b8263050a5d74b89ceab381ac81ce70535688 | /src/analysis/normed_space/dual.lean | 6a5270793328614fbd12af313da1e586c2c3964a | [
"Apache-2.0"
] | permissive | vozor/mathlib | 5921f55235ff60c05f4a48a90d616ea167068adf | f7e728ad8a6ebf90291df2a4d2f9255a6576b529 | refs/heads/master | 1,675,607,702,231 | 1,609,023,279,000 | 1,609,023,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,869 | lean | /-
Copyright (c) 2020 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth, Frédéric Dupuis
-/
import analysis.normed_space.hahn_banach
import analysis.normed_space.inner_product
/-!
# The topological dual of a normed space
In this file we define the topological dual of a normed space, and the bounded linear map from
a normed space into its double dual.
We also prove that, for base field `𝕜` with `[is_R_or_C 𝕜]`, this map is an isometry.
We then consider inner product spaces, with base field over `ℝ` (the corresponding results for `ℂ`
will require the definition of conjugate-linear maps). We define `to_dual_map`, a continuous linear
map from `E` to its dual, which maps an element `x` of the space to `λ y, ⟪x, y⟫`. We check
(`to_dual_map_isometry`) that this map is an isometry onto its image, and particular is injective.
We also define `to_dual'` as the function taking taking a vector to its dual for a base field `𝕜`
with `[is_R_or_C 𝕜]`; this is a function and not a linear map.
Finally, under the hypothesis of completeness (i.e., for Hilbert spaces), we prove the Fréchet-Riesz
representation (`to_dual_map_eq_top`), which states the surjectivity: every element of the dual
of a Hilbert space `E` has the form `λ u, ⟪x, u⟫` for some `x : E`. This permits the map
`to_dual_map` to be upgraded to an (isometric) continuous linear equivalence, `to_dual`, between a
Hilbert space and its dual.
## References
* [M. Einsiedler and T. Ward, *Functional Analysis, Spectral Theory, and Applications*]
[EinsiedlerWard2017]
## Tags
dual, Fréchet-Riesz
-/
noncomputable theory
open_locale classical
universes u v
namespace normed_space
section general
variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
variables (E : Type*) [normed_group E] [normed_space 𝕜 E]
/-- The topological dual of a normed space `E`. -/
@[derive [has_coe_to_fun, normed_group, normed_space 𝕜]] def dual := E →L[𝕜] 𝕜
instance : inhabited (dual 𝕜 E) := ⟨0⟩
/-- The inclusion of a normed space in its double (topological) dual. -/
def inclusion_in_double_dual' (x : E) : (dual 𝕜 (dual 𝕜 E)) :=
linear_map.mk_continuous
{ to_fun := λ f, f x,
map_add' := by simp,
map_smul' := by simp }
∥x∥
(λ f, by { rw mul_comm, exact f.le_op_norm x } )
@[simp] lemma dual_def (x : E) (f : dual 𝕜 E) :
((inclusion_in_double_dual' 𝕜 E) x) f = f x := rfl
lemma double_dual_bound (x : E) : ∥(inclusion_in_double_dual' 𝕜 E) x∥ ≤ ∥x∥ :=
begin
apply continuous_linear_map.op_norm_le_bound,
{ simp },
{ intros f, rw mul_comm, exact f.le_op_norm x, }
end
/-- The inclusion of a normed space in its double (topological) dual, considered
as a bounded linear map. -/
def inclusion_in_double_dual : E →L[𝕜] (dual 𝕜 (dual 𝕜 E)) :=
linear_map.mk_continuous
{ to_fun := λ (x : E), (inclusion_in_double_dual' 𝕜 E) x,
map_add' := λ x y, by { ext, simp },
map_smul' := λ (c : 𝕜) x, by { ext, simp } }
1
(λ x, by { convert double_dual_bound _ _ _, simp } )
end general
section bidual_isometry
variables {𝕜 : Type v} [is_R_or_C 𝕜]
{E : Type u} [normed_group E] [normed_space 𝕜 E]
/-- If one controls the norm of every `f x`, then one controls the norm of `x`.
Compare `continuous_linear_map.op_norm_le_bound`. -/
lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), ∥f x∥ ≤ M * ∥f∥) :
∥x∥ ≤ M :=
begin
classical,
by_cases h : x = 0,
{ simp only [h, hMp, norm_zero] },
{ obtain ⟨f, hf⟩ : ∃ g : E →L[𝕜] 𝕜, _ := exists_dual_vector x h,
calc ∥x∥ = ∥norm' 𝕜 x∥ : (norm_norm' _ _ _).symm
... = ∥f x∥ : by rw hf.2
... ≤ M * ∥f∥ : hM f
... = M : by rw [hf.1, mul_one] }
end
/-- The inclusion of a normed space in its double dual is an isometry onto its image.-/
lemma inclusion_in_double_dual_isometry (x : E) : ∥inclusion_in_double_dual 𝕜 E x∥ = ∥x∥ :=
begin
apply le_antisymm,
{ exact double_dual_bound 𝕜 E x },
{ rw continuous_linear_map.norm_def,
apply real.lb_le_Inf _ continuous_linear_map.bounds_nonempty,
rintros c ⟨hc1, hc2⟩,
exact norm_le_dual_bound x hc1 hc2 },
end
end bidual_isometry
end normed_space
namespace inner_product_space
open is_R_or_C continuous_linear_map
section is_R_or_C
variables (𝕜 : Type*)
variables {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E]
local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y
local postfix `†`:90 := @is_R_or_C.conj 𝕜 _
/--
Given some `x` in an inner product space, we can define its dual as the continuous linear map
`λ y, ⟪x, y⟫`. Consider using `to_dual` or `to_dual_map` instead in the real case.
-/
def to_dual' : E →+ normed_space.dual 𝕜 E :=
{ to_fun := λ x, linear_map.mk_continuous
{ to_fun := λ y, ⟪x, y⟫,
map_add' := λ _ _, inner_add_right,
map_smul' := λ _ _, inner_smul_right }
∥x∥
(λ y, by { rw [is_R_or_C.norm_eq_abs], exact abs_inner_le_norm _ _ }),
map_zero' := by { ext z, simp },
map_add' := λ x y, by { ext z, simp [inner_add_left] } }
@[simp] lemma to_dual'_apply {x y : E} : to_dual' 𝕜 x y = ⟪x, y⟫ := rfl
/-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/
@[simp] lemma norm_to_dual'_apply (x : E) : ∥to_dual' 𝕜 x∥ = ∥x∥ :=
begin
refine le_antisymm _ _,
{ exact linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ },
{ cases eq_or_lt_of_le (norm_nonneg x) with h h,
{ have : x = 0 := norm_eq_zero.mp (eq.symm h),
simp [this] },
{ refine (mul_le_mul_right h).mp _,
calc ∥x∥ * ∥x∥ = ∥x∥ ^ 2 : by ring
... = re ⟪x, x⟫ : norm_sq_eq_inner _
... ≤ abs ⟪x, x⟫ : re_le_abs _
... = ∥to_dual' 𝕜 x x∥ : by simp [norm_eq_abs]
... ≤ ∥to_dual' 𝕜 x∥ * ∥x∥ : le_op_norm (to_dual' 𝕜 x) x } }
end
variables (E)
lemma to_dual'_isometry : isometry (@to_dual' 𝕜 E _ _) :=
add_monoid_hom.isometry_of_norm _ (norm_to_dual'_apply 𝕜)
/--
Fréchet-Riesz representation: any `ℓ` in the dual of a Hilbert space `E` is of the form
`λ u, ⟪y, u⟫` for some `y : E`, i.e. `to_dual'` is surjective.
-/
lemma to_dual'_surjective [complete_space E] : function.surjective (@to_dual' 𝕜 E _ _) :=
begin
intros ℓ,
set Y := ker ℓ with hY,
by_cases htriv : Y = ⊤,
{ have hℓ : ℓ = 0,
{ have h' := linear_map.ker_eq_top.mp htriv,
rw [←coe_zero] at h',
apply coe_injective,
exact h' },
exact ⟨0, by simp [hℓ]⟩ },
{ have Ycomplete := is_complete_ker ℓ,
rw [submodule.eq_top_iff_orthogonal_eq_bot Ycomplete, ←hY] at htriv,
change Y.orthogonal ≠ ⊥ at htriv,
rw [submodule.ne_bot_iff] at htriv,
obtain ⟨z : E, hz : z ∈ Y.orthogonal, z_ne_0 : z ≠ 0⟩ := htriv,
refine ⟨((ℓ z)† / ⟪z, z⟫) • z, _⟩,
ext x,
have h₁ : (ℓ z) • x - (ℓ x) • z ∈ Y,
{ rw [mem_ker, map_sub, map_smul, map_smul, algebra.id.smul_eq_mul, algebra.id.smul_eq_mul,
mul_comm],
exact sub_self (ℓ x * ℓ z) },
have h₂ : (ℓ z) * ⟪z, x⟫ = (ℓ x) * ⟪z, z⟫,
{ have h₃ := calc
0 = ⟪z, (ℓ z) • x - (ℓ x) • z⟫ : by { rw [(Y.mem_orthogonal' z).mp hz], exact h₁ }
... = ⟪z, (ℓ z) • x⟫ - ⟪z, (ℓ x) • z⟫ : by rw [inner_sub_right]
... = (ℓ z) * ⟪z, x⟫ - (ℓ x) * ⟪z, z⟫ : by simp [inner_smul_right],
exact sub_eq_zero.mp (eq.symm h₃) },
have h₄ := calc
⟪((ℓ z)† / ⟪z, z⟫) • z, x⟫ = (ℓ z) / ⟪z, z⟫ * ⟪z, x⟫
: by simp [inner_smul_left, conj_div, conj_conj]
... = (ℓ z) * ⟪z, x⟫ / ⟪z, z⟫
: by rw [←div_mul_eq_mul_div]
... = (ℓ x) * ⟪z, z⟫ / ⟪z, z⟫
: by rw [h₂]
... = ℓ x
: begin
have : ⟪z, z⟫ ≠ 0,
{ change z = 0 → false at z_ne_0,
rwa ←inner_self_eq_zero at z_ne_0 },
field_simp [this]
end,
exact h₄ }
end
end is_R_or_C
section real
variables {F : Type*} [inner_product_space ℝ F]
/-- In a real inner product space `F`, the function that takes a vector `x` in `F` to its dual
`λ y, ⟪x, y⟫` is a continuous linear map. If the space is complete (i.e. is a Hilbert space),
consider using `to_dual` instead. -/
-- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps)
def to_dual_map : F →L[ℝ] (normed_space.dual ℝ F) :=
linear_map.mk_continuous
{ to_fun := to_dual' ℝ,
map_add' := λ x y, by { ext, simp [inner_add_left] },
map_smul' := λ c x, by { ext, simp [inner_smul_left] } }
1
(λ x, by simp only [norm_to_dual'_apply, one_mul, linear_map.coe_mk])
@[simp] lemma to_dual_map_apply {x y : F} : to_dual_map x y = ⟪x, y⟫_ℝ := rfl
/-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/
@[simp] lemma norm_to_dual_map_apply (x : F) : ∥to_dual_map x∥ = ∥x∥ := norm_to_dual'_apply _ _
lemma to_dual_map_isometry : isometry (@to_dual_map F _) :=
add_monoid_hom.isometry_of_norm _ norm_to_dual_map_apply
lemma to_dual_map_injective : function.injective (@to_dual_map F _) :=
to_dual_map_isometry.injective
@[simp] lemma ker_to_dual_map : (@to_dual_map F _).ker = ⊥ :=
linear_map.ker_eq_bot.mpr to_dual_map_injective
@[simp] lemma to_dual_map_eq_iff_eq {x y : F} : to_dual_map x = to_dual_map y ↔ x = y :=
((linear_map.ker_eq_bot).mp (@ker_to_dual_map F _)).eq_iff
variables [complete_space F]
/--
Fréchet-Riesz representation: any `ℓ` in the dual of a real Hilbert space `F` is of the form
`λ u, ⟪y, u⟫` for some `y` in `F`. See `inner_product_space.to_dual` for the continuous linear
equivalence thus induced.
-/
-- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps)
lemma range_to_dual_map : (@to_dual_map F _).range = ⊤ :=
linear_map.range_eq_top.mpr (to_dual'_surjective ℝ F)
/--
Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to
its dual is a continuous linear equivalence. -/
def to_dual : F ≃L[ℝ] (normed_space.dual ℝ F) :=
continuous_linear_equiv.of_isometry to_dual_map.to_linear_map to_dual_map_isometry range_to_dual_map
/--
Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to
its dual is an isometry. -/
def isometric.to_dual : F ≃ᵢ normed_space.dual ℝ F :=
{ to_equiv := to_dual.to_linear_equiv.to_equiv,
isometry_to_fun := to_dual'_isometry ℝ F}
@[simp] lemma to_dual_apply {x y : F} : to_dual x y = ⟪x, y⟫_ℝ := rfl
@[simp] lemma to_dual_eq_iff_eq {x y : F} : to_dual x = to_dual y ↔ x = y :=
(@to_dual F _ _).injective.eq_iff
lemma to_dual_eq_iff_eq' {x x' : F} : (∀ y : F, ⟪x, y⟫_ℝ = ⟪x', y⟫_ℝ) ↔ x = x' :=
begin
split,
{ intros h,
have : to_dual x = to_dual x' → x = x' := to_dual_eq_iff_eq.mp,
apply this,
simp_rw [←to_dual_apply] at h,
ext z,
exact h z },
{ rintros rfl y,
refl }
end
@[simp] lemma norm_to_dual_apply (x : F) : ∥to_dual x∥ = ∥x∥ := norm_to_dual_map_apply x
/-- In a Hilbert space, the norm of a vector in the dual space is the norm of its corresponding
primal vector. -/
lemma norm_to_dual_symm_apply (ℓ : normed_space.dual ℝ F) : ∥to_dual.symm ℓ∥ = ∥ℓ∥ :=
begin
have : ℓ = to_dual (to_dual.symm ℓ) := by simp only [continuous_linear_equiv.apply_symm_apply],
conv_rhs { rw [this] },
refine eq.symm (norm_to_dual_apply _),
end
end real
end inner_product_space
|
2265d179ffd287dbcfc205b5585cec50a5584f0c | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/data/zsqrtd/basic.lean | 47e675b88e774a4c380a829c07ec24171c4c0cd0 | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 29,790 | 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.associated
import tactic.ring
/-- The ring of integers adjoined with a square root of `d`.
These have the form `a + b √d` where `a b : ℤ`. The components
are called `re` and `im` by analogy to the negative `d` case,
but of course both parts are real here since `d` is nonnegative. -/
structure zsqrtd (d : ℤ) :=
(re : ℤ)
(im : ℤ)
prefix `ℤ√`:100 := zsqrtd
namespace zsqrtd
section
parameters {d : ℤ}
instance : decidable_eq ℤ√d :=
by tactic.mk_dec_eq_instance
theorem ext : ∀ {z w : ℤ√d}, z = w ↔ z.re = w.re ∧ z.im = w.im
| ⟨x, y⟩ ⟨x', y'⟩ := ⟨λ h, by injection h; split; assumption,
λ ⟨h₁, h₂⟩, by congr; assumption⟩
/-- Convert an integer to a `ℤ√d` -/
def of_int (n : ℤ) : ℤ√d := ⟨n, 0⟩
theorem of_int_re (n : ℤ) : (of_int n).re = n := rfl
theorem of_int_im (n : ℤ) : (of_int n).im = 0 := rfl
/-- The zero of the ring -/
def zero : ℤ√d := of_int 0
instance : has_zero ℤ√d := ⟨zsqrtd.zero⟩
@[simp] theorem zero_re : (0 : ℤ√d).re = 0 := rfl
@[simp] theorem zero_im : (0 : ℤ√d).im = 0 := rfl
instance : inhabited ℤ√d := ⟨0⟩
/-- The one of the ring -/
def one : ℤ√d := of_int 1
instance : has_one ℤ√d := ⟨zsqrtd.one⟩
@[simp] theorem one_re : (1 : ℤ√d).re = 1 := rfl
@[simp] theorem one_im : (1 : ℤ√d).im = 0 := rfl
/-- The representative of `√d` in the ring -/
def sqrtd : ℤ√d := ⟨0, 1⟩
@[simp] theorem sqrtd_re : (sqrtd : ℤ√d).re = 0 := rfl
@[simp] theorem sqrtd_im : (sqrtd : ℤ√d).im = 1 := rfl
/-- Addition of elements of `ℤ√d` -/
def add : ℤ√d → ℤ√d → ℤ√d
| ⟨x, y⟩ ⟨x', y'⟩ := ⟨x + x', y + y'⟩
instance : has_add ℤ√d := ⟨zsqrtd.add⟩
@[simp] theorem add_def (x y x' y' : ℤ) :
(⟨x, y⟩ + ⟨x', y'⟩ : ℤ√d) = ⟨x + x', y + y'⟩ := rfl
@[simp] theorem add_re : ∀ z w : ℤ√d, (z + w).re = z.re + w.re
| ⟨x, y⟩ ⟨x', y'⟩ := rfl
@[simp] theorem add_im : ∀ z w : ℤ√d, (z + w).im = z.im + w.im
| ⟨x, y⟩ ⟨x', y'⟩ := rfl
@[simp] theorem bit0_re (z) : (bit0 z : ℤ√d).re = bit0 z.re := add_re _ _
@[simp] theorem bit0_im (z) : (bit0 z : ℤ√d).im = bit0 z.im := add_im _ _
@[simp] theorem bit1_re (z) : (bit1 z : ℤ√d).re = bit1 z.re := by simp [bit1]
@[simp] theorem bit1_im (z) : (bit1 z : ℤ√d).im = bit0 z.im := by simp [bit1]
/-- Negation in `ℤ√d` -/
def neg : ℤ√d → ℤ√d
| ⟨x, y⟩ := ⟨-x, -y⟩
instance : has_neg ℤ√d := ⟨zsqrtd.neg⟩
@[simp] theorem neg_re : ∀ z : ℤ√d, (-z).re = -z.re
| ⟨x, y⟩ := rfl
@[simp] theorem neg_im : ∀ z : ℤ√d, (-z).im = -z.im
| ⟨x, y⟩ := rfl
/-- Multiplication in `ℤ√d` -/
def mul : ℤ√d → ℤ√d → ℤ√d
| ⟨x, y⟩ ⟨x', y'⟩ := ⟨x * x' + d * y * y', x * y' + y * x'⟩
instance : has_mul ℤ√d := ⟨zsqrtd.mul⟩
@[simp] theorem mul_re : ∀ z w : ℤ√d, (z * w).re = z.re * w.re + d * z.im * w.im
| ⟨x, y⟩ ⟨x', y'⟩ := rfl
@[simp] theorem mul_im : ∀ z w : ℤ√d, (z * w).im = z.re * w.im + z.im * w.re
| ⟨x, y⟩ ⟨x', y'⟩ := rfl
instance : comm_ring ℤ√d := by refine
{ add := (+),
zero := 0,
neg := has_neg.neg,
mul := (*),
sub := λ a b, a + -b,
one := 1, ..};
{ intros, simp [ext, add_mul, mul_add, add_comm, add_left_comm, mul_comm, mul_left_comm] }
instance : add_comm_monoid ℤ√d := by apply_instance
instance : add_monoid ℤ√d := by apply_instance
instance : monoid ℤ√d := by apply_instance
instance : comm_monoid ℤ√d := by apply_instance
instance : comm_semigroup ℤ√d := by apply_instance
instance : semigroup ℤ√d := by apply_instance
instance : add_comm_semigroup ℤ√d := by apply_instance
instance : add_semigroup ℤ√d := by apply_instance
instance : comm_semiring ℤ√d := by apply_instance
instance : semiring ℤ√d := by apply_instance
instance : ring ℤ√d := by apply_instance
instance : distrib ℤ√d := by apply_instance
/-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/
def conj : ℤ√d → ℤ√d
| ⟨x, y⟩ := ⟨x, -y⟩
@[simp] theorem conj_re : ∀ z : ℤ√d, (conj z).re = z.re
| ⟨x, y⟩ := rfl
@[simp] theorem conj_im : ∀ z : ℤ√d, (conj z).im = -z.im
| ⟨x, y⟩ := rfl
/-- `conj` as an `add_monoid_hom`. -/
def conj_hom : ℤ√d →+ ℤ√d :=
{ to_fun := conj,
map_add' := λ ⟨a, ai⟩ ⟨b, bi⟩, ext.mpr ⟨rfl, neg_add _ _⟩,
map_zero' := ext.mpr ⟨rfl, neg_zero⟩ }
@[simp] lemma conj_zero : conj (0 : ℤ√d) = 0 :=
conj_hom.map_zero
@[simp] lemma conj_one : conj (1 : ℤ√d) = 1 :=
by simp only [zsqrtd.ext, zsqrtd.conj_re, zsqrtd.conj_im, zsqrtd.one_im, neg_zero, eq_self_iff_true,
and_self]
@[simp] lemma conj_neg (x : ℤ√d) : (-x).conj = -x.conj :=
conj_hom.map_neg x
@[simp] lemma conj_add (x y : ℤ√d) : (x + y).conj = x.conj + y.conj :=
conj_hom.map_add x y
@[simp] lemma conj_sub (x y : ℤ√d) : (x - y).conj = x.conj - y.conj :=
conj_hom.map_sub x y
@[simp] lemma conj_conj {d : ℤ} (x : ℤ√d) : x.conj.conj = x :=
by simp only [ext, true_and, conj_re, eq_self_iff_true, neg_neg, conj_im]
instance : nontrivial ℤ√d :=
⟨⟨0, 1, dec_trivial⟩⟩
@[simp] theorem coe_nat_re (n : ℕ) : (n : ℤ√d).re = n :=
by induction n; simp *
@[simp] theorem coe_nat_im (n : ℕ) : (n : ℤ√d).im = 0 :=
by induction n; simp *
theorem coe_nat_val (n : ℕ) : (n : ℤ√d) = ⟨n, 0⟩ :=
by simp [ext]
@[simp] theorem coe_int_re (n : ℤ) : (n : ℤ√d).re = n :=
by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq]
@[simp] theorem coe_int_im (n : ℤ) : (n : ℤ√d).im = 0 :=
by cases n; simp *
theorem coe_int_val (n : ℤ) : (n : ℤ√d) = ⟨n, 0⟩ :=
by simp [ext]
instance : char_zero ℤ√d :=
{ cast_injective := λ m n, by simp [ext] }
@[simp] theorem of_int_eq_coe (n : ℤ) : (of_int n : ℤ√d) = n :=
by simp [ext, of_int_re, of_int_im]
@[simp] theorem smul_val (n x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ :=
by simp [ext]
@[simp] theorem muld_val (x y : ℤ) : sqrtd * ⟨x, y⟩ = ⟨d * y, x⟩ :=
by simp [ext]
@[simp] theorem dmuld : sqrtd * sqrtd = d :=
by simp [ext]
@[simp] theorem smuld_val (n x y : ℤ) : sqrtd * (n : ℤ√d) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ :=
by simp [ext]
theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd * y :=
by simp [ext]
theorem mul_conj {x y : ℤ} : (⟨x, y⟩ * conj ⟨x, y⟩ : ℤ√d) = x * x - d * y * y :=
by simp [ext, sub_eq_add_neg, mul_comm]
theorem conj_mul {a b : ℤ√d} : conj (a * b) = conj a * conj b :=
by { simp [ext], ring }
protected lemma coe_int_add (m n : ℤ) : (↑(m + n) : ℤ√d) = ↑m + ↑n :=
(int.cast_ring_hom _).map_add _ _
protected lemma coe_int_sub (m n : ℤ) : (↑(m - n) : ℤ√d) = ↑m - ↑n :=
(int.cast_ring_hom _).map_sub _ _
protected lemma coe_int_mul (m n : ℤ) : (↑(m * n) : ℤ√d) = ↑m * ↑n :=
(int.cast_ring_hom _).map_mul _ _
protected lemma coe_int_inj {m n : ℤ} (h : (↑m : ℤ√d) = ↑n) : m = n :=
by simpa using congr_arg re h
/-- Read `sq_le a c b d` as `a √c ≤ b √d` -/
def sq_le (a c b d : ℕ) : Prop := c*a*a ≤ d*b*b
theorem sq_le_of_le {c d x y z w : ℕ} (xz : z ≤ x) (yw : y ≤ w) (xy : sq_le x c y d) :
sq_le z c w d :=
le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $
le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _))
theorem sq_le_add_mixed {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) :
c * (x * z) ≤ d * (y * w) :=
nat.mul_self_le_mul_self_iff.2 $
by simpa [mul_comm, mul_left_comm] using
mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _)
theorem sq_le_add {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) :
sq_le (x + z) c (y + w) d :=
begin
have xz := sq_le_add_mixed xy zw,
simp [sq_le, mul_assoc] at xy zw,
simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *]
end
theorem sq_le_cancel {c d x y z w : ℕ} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) :
sq_le z c w d :=
begin
apply le_of_not_gt,
intro l,
refine not_le_of_gt _ h,
simp [sq_le, mul_add, mul_comm, mul_left_comm, add_assoc],
have hm := sq_le_add_mixed zw (le_of_lt l),
simp [sq_le, mul_assoc] at l zw,
exact lt_of_le_of_lt (add_le_add_right zw _)
(add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _)
end
theorem sq_le_smul {c d x y : ℕ} (n : ℕ) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d :=
by simpa [sq_le, mul_left_comm, mul_assoc] using
nat.mul_le_mul_left (n * n) xy
theorem sq_le_mul {d x y z w : ℕ} :
(sq_le x 1 y d → sq_le z 1 w d → sq_le (x * w + y * z) d (x * z + d * y * w) 1) ∧
(sq_le x 1 y d → sq_le w d z 1 → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧
(sq_le y d x 1 → sq_le z 1 w d → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧
(sq_le y d x 1 → sq_le w d z 1 → sq_le (x * w + y * z) d (x * z + d * y * w) 1) :=
by refine ⟨_, _, _, _⟩; {
intros xy zw,
have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy))
(sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)),
refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _),
convert this,
simp only [one_mul, int.coe_nat_add, int.coe_nat_mul],
ring }
/-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d ≥ 0`;
we are interested in the case `c = 1` but this is more symmetric -/
def nonnegg (c d : ℕ) : ℤ → ℤ → Prop
| (a : ℕ) (b : ℕ) := true
| (a : ℕ) -[1+ b] := sq_le (b+1) c a d
| -[1+ a] (b : ℕ) := sq_le (a+1) d b c
| -[1+ a] -[1+ b] := false
theorem nonnegg_comm {c d : ℕ} {x y : ℤ} : nonnegg c d x y = nonnegg d c y x :=
by induction x; induction y; refl
theorem nonnegg_neg_pos {c d} : Π {a b : ℕ}, nonnegg c d (-a) b ↔ sq_le a d b c
| 0 b := ⟨by simp [sq_le, nat.zero_le], λa, trivial⟩
| (a+1) b := by rw ← int.neg_succ_of_nat_coe; refl
theorem nonnegg_pos_neg {c d} {a b : ℕ} : nonnegg c d a (-b) ↔ sq_le b c a d :=
by rw nonnegg_comm; exact nonnegg_neg_pos
theorem nonnegg_cases_right {c d} {a : ℕ} :
Π {b : ℤ}, (Π x : ℕ, b = -x → sq_le x c a d) → nonnegg c d a b
| (b:nat) h := trivial
| -[1+ b] h := h (b+1) rfl
theorem nonnegg_cases_left {c d} {b : ℕ} {a : ℤ} (h : Π x : ℕ, a = -x → sq_le x d b c) :
nonnegg c d a b :=
cast nonnegg_comm (nonnegg_cases_right h)
section norm
def norm (n : ℤ√d) : ℤ := n.re * n.re - d * n.im * n.im
@[simp] lemma norm_zero : norm 0 = 0 := by simp [norm]
@[simp] lemma norm_one : norm 1 = 1 := by simp [norm]
@[simp] lemma norm_int_cast (n : ℤ) : norm n = n * n := by simp [norm]
@[simp] lemma norm_nat_cast (n : ℕ) : norm n = n * n := norm_int_cast n
@[simp] lemma norm_mul (n m : ℤ√d) : norm (n * m) = norm n * norm m :=
by { simp only [norm, mul_im, mul_re], ring }
lemma norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * n.conj :=
by cases n; simp [norm, conj, zsqrtd.ext, mul_comm, sub_eq_add_neg]
@[simp] lemma norm_neg (x : ℤ√d) : (-x).norm = x.norm :=
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_neg, neg_mul_eq_neg_mul_symm,
mul_neg_eq_neg_mul_symm, neg_neg]
@[simp] lemma norm_conj (x : ℤ√d) : x.conj.norm = x.norm :=
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_conj, mul_comm]
instance : is_monoid_hom norm :=
{ map_one := norm_one, map_mul := norm_mul }
lemma norm_nonneg (hd : d ≤ 0) (n : ℤ√d) : 0 ≤ n.norm :=
add_nonneg (mul_self_nonneg _)
(by rw [mul_assoc, neg_mul_eq_neg_mul];
exact (mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _)))
lemma norm_eq_one_iff {x : ℤ√d} : x.norm.nat_abs = 1 ↔ is_unit x :=
⟨λ h, is_unit_iff_dvd_one.2 $
(le_total 0 (norm x)).cases_on
(λ hx, show x ∣ 1, from ⟨x.conj,
by rwa [← int.coe_nat_inj', int.nat_abs_of_nonneg hx,
← @int.cast_inj (ℤ√d) _ _, norm_eq_mul_conj, eq_comm] at h⟩)
(λ hx, show x ∣ 1, from ⟨- x.conj,
by rwa [← int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx,
← @int.cast_inj (ℤ√d) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg,
eq_comm] at h⟩),
λ h, let ⟨y, hy⟩ := is_unit_iff_dvd_one.1 h in begin
have := congr_arg (int.nat_abs ∘ norm) hy,
rw [function.comp_app, function.comp_app, norm_mul, int.nat_abs_mul,
norm_one, int.nat_abs_one, eq_comm, nat.mul_eq_one_iff] at this,
exact this.1
end⟩
end norm
end
section
parameter {d : ℕ}
/-- Nonnegativity of an element of `ℤ√d`. -/
def nonneg : ℤ√d → Prop | ⟨a, b⟩ := nonnegg d 1 a b
protected def le (a b : ℤ√d) : Prop := nonneg (b - a)
instance : has_le ℤ√d := ⟨zsqrtd.le⟩
protected def lt (a b : ℤ√d) : Prop := ¬(b ≤ a)
instance : has_lt ℤ√d := ⟨zsqrtd.lt⟩
instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) :=
by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance
instance decidable_nonneg : Π (a : ℤ√d), decidable (nonneg a)
| ⟨a, b⟩ := zsqrtd.decidable_nonnegg _ _ _ _
instance decidable_le (a b : ℤ√d) : decidable (a ≤ b) := decidable_nonneg _
theorem nonneg_cases : Π {a : ℤ√d}, nonneg a → ∃ x y : ℕ, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩
| ⟨(x : ℕ), (y : ℕ)⟩ h := ⟨x, y, or.inl rfl⟩
| ⟨(x : ℕ), -[1+ y]⟩ h := ⟨x, y+1, or.inr $ or.inl rfl⟩
| ⟨-[1+ x], (y : ℕ)⟩ h := ⟨x+1, y, or.inr $ or.inr rfl⟩
| ⟨-[1+ x], -[1+ y]⟩ h := false.elim h
lemma nonneg_add_lem {x y z w : ℕ} (xy : nonneg ⟨x, -y⟩) (zw : nonneg ⟨-z, w⟩) :
nonneg (⟨x, -y⟩ + ⟨-z, w⟩) :=
have nonneg ⟨int.sub_nat_nat x z, int.sub_nat_nat w y⟩, from int.sub_nat_nat_elim x z
(λm n i, sq_le y d m 1 → sq_le n 1 w d → nonneg ⟨i, int.sub_nat_nat w y⟩)
(λj k, int.sub_nat_nat_elim w y
(λm n i, sq_le n d (k + j) 1 → sq_le k 1 m d → nonneg ⟨int.of_nat j, i⟩)
(λm n xy zw, trivial)
(λm n xy zw, sq_le_cancel zw xy))
(λj k, int.sub_nat_nat_elim w y
(λm n i, sq_le n d k 1 → sq_le (k + j + 1) 1 m d → nonneg ⟨-[1+ j], i⟩)
(λm n xy zw, sq_le_cancel xy zw)
(λm n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) (le_refl _) xy) in
have k + j + 1 ≤ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t),
absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy)
(nonnegg_neg_pos.1 zw),
show nonneg ⟨_, _⟩, by rw [neg_add_eq_sub];
rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this
theorem nonneg_add {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) :=
begin
rcases nonneg_cases ha with ⟨x, y, rfl|rfl|rfl⟩;
rcases nonneg_cases hb with ⟨z, w, rfl|rfl|rfl⟩; dsimp [add, nonneg] at ha hb ⊢,
{ trivial },
{ refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp [add_comm, *]))) },
{ apply nat.le_add_left } },
{ refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp [add_comm, *]))) },
{ apply nat.le_add_left } },
{ refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) },
{ apply nat.le_add_right } },
{ simpa [add_comm] using
nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) },
{ exact nonneg_add_lem ha hb },
{ refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ z (by simp *))) },
{ apply nat.le_add_right } },
{ rw [add_comm, add_comm ↑y], exact nonneg_add_lem hb ha },
{ simpa [add_comm] using
nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) },
end
theorem le_refl (a : ℤ√d) : a ≤ a := show nonneg (a - a), by simp
protected theorem le_trans {a b c : ℤ√d} (ab : a ≤ b) (bc : b ≤ c) : a ≤ c :=
have nonneg (b - a + (c - b)), from nonneg_add ab bc,
by simpa [sub_add_sub_cancel']
theorem nonneg_iff_zero_le {a : ℤ√d} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp
theorem le_of_le_le {x y z w : ℤ} (xz : x ≤ z) (yw : y ≤ w) : (⟨x, y⟩ : ℤ√d) ≤ ⟨z, w⟩ :=
show nonneg ⟨z - x, w - y⟩, from
match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, ⟨a, rfl⟩, ⟨b, rfl⟩ :=
trivial end
theorem le_arch (a : ℤ√d) : ∃n : ℕ, a ≤ n :=
let ⟨x, y, (h : a ≤ ⟨x, y⟩)⟩ := show ∃x y : ℕ, nonneg (⟨x, y⟩ + -a), from match -a with
| ⟨int.of_nat x, int.of_nat y⟩ := ⟨0, 0, trivial⟩
| ⟨int.of_nat x, -[1+ y]⟩ := ⟨0, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩
| ⟨-[1+ x], int.of_nat y⟩ := ⟨x+1, 0, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩
| ⟨-[1+ x], -[1+ y]⟩ := ⟨x+1, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩
end in begin
refine ⟨x + d*y, zsqrtd.le_trans h _⟩,
rw [← int.cast_coe_nat, ← of_int_eq_coe],
change nonneg ⟨(↑x + d*y) - ↑x, 0-↑y⟩,
cases y with y,
{ simp },
have h : ∀y, sq_le y d (d * y) 1 := λ y,
by simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_right (y * y) (nat.le_mul_self d),
rw [show (x:ℤ) + d * nat.succ y - x = d * nat.succ y, by simp],
exact h (y+1)
end
protected theorem nonneg_total : Π (a : ℤ√d), nonneg a ∨ nonneg (-a)
| ⟨(x : ℕ), (y : ℕ)⟩ := or.inl trivial
| ⟨-[1+ x], -[1+ y]⟩ := or.inr trivial
| ⟨0, -[1+ y]⟩ := or.inr trivial
| ⟨-[1+ x], 0⟩ := or.inr trivial
| ⟨(x+1:ℕ), -[1+ y]⟩ := nat.le_total
| ⟨-[1+ x], (y+1:ℕ)⟩ := nat.le_total
protected theorem le_total (a b : ℤ√d) : a ≤ b ∨ b ≤ a :=
let t := nonneg_total (b - a) in by rw [show -(b-a) = a-b, from neg_sub b a] at t; exact t
instance : preorder ℤ√d :=
{ le := zsqrtd.le,
le_refl := zsqrtd.le_refl,
le_trans := @zsqrtd.le_trans,
lt := zsqrtd.lt,
lt_iff_le_not_le := λ a b,
(and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm }
protected theorem add_le_add_left (a b : ℤ√d) (ab : a ≤ b) (c : ℤ√d) : c + a ≤ c + b :=
show nonneg _, by rw add_sub_add_left_eq_sub; exact ab
protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b :=
by simpa using zsqrtd.add_le_add_left _ _ h (-c)
protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b :=
λ h', h (zsqrtd.le_of_add_le_add_left _ _ _ h')
theorem nonneg_smul {a : ℤ√d} {n : ℕ} (ha : nonneg a) : nonneg (n * a) :=
by rw ← int.cast_coe_nat; exact match a, nonneg_cases ha, ha with
| ._, ⟨x, y, or.inl rfl⟩, ha := by rw smul_val; trivial
| ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by rw smul_val; simpa using
nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha)
| ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by rw smul_val; simpa using
nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha)
end
theorem nonneg_muld {a : ℤ√d} (ha : nonneg a) : nonneg (sqrtd * a) :=
by refine match a, nonneg_cases ha, ha with
| ._, ⟨x, y, or.inl rfl⟩, ha := trivial
| ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by simp; apply nonnegg_neg_pos.2;
simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha)
| ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by simp; apply nonnegg_pos_neg.2;
simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha)
end
theorem nonneg_mul_lem {x y : ℕ} {a : ℤ√d} (ha : nonneg a) : nonneg (⟨x, y⟩ * a) :=
have (⟨x, y⟩ * a : ℤ√d) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc];
refl,
by rw this; exact nonneg_add (nonneg_smul ha) (nonneg_muld $ nonneg_smul ha)
theorem nonneg_mul {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) :=
match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with
| ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := trivial
| ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := nonneg_mul_lem hb
| ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := nonneg_mul_lem hb
| ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb :=
by rw mul_comm; exact nonneg_mul_lem ha
| ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb :=
by rw mul_comm; exact nonneg_mul_lem ha
| ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb :=
by rw [calc (⟨-x, y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl
... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp [add_comm]]; exact
nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb))
| ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb :=
by rw [calc (⟨-x, y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl
... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp [add_comm]]; exact
nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb))
| ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb :=
by rw [calc (⟨x, -y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl
... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp [add_comm]]; exact
nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb))
| ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb :=
by rw [calc (⟨x, -y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl
... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp [add_comm]]; exact
nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb))
end
protected theorem mul_nonneg (a b : ℤ√d) : 0 ≤ a → 0 ≤ b → 0 ≤ a * b :=
by repeat {rw ← nonneg_iff_zero_le}; exact nonneg_mul
theorem not_sq_le_succ (c d y) (h : 0 < c) : ¬sq_le (y + 1) c 0 d :=
not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _
/-- A nonsquare is a natural number that is not equal to the square of an
integer. This is implemented as a typeclass because it's a necessary condition
for much of the Pell equation theory. -/
class nonsquare (x : ℕ) : Prop := (ns [] : ∀n : ℕ, x ≠ n*n)
parameter [dnsq : nonsquare d]
include dnsq
theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0)
theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 ∧ y = 0 :=
let g := x.gcd y in or.elim g.eq_zero_or_pos
(λH, ⟨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right H⟩)
(λgpos, false.elim $
let ⟨m, n, co, (hx : x = m * g), (hy : y = n * g)⟩ := nat.exists_coprime gpos in
begin
rw [hx, hy] at h,
have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos)
(by simpa [mul_comm, mul_left_comm] using h),
have co2 := let co1 := co.mul_right co in co1.mul co1,
exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $
co2.dvd_of_dvd_mul_right $ by simp [this])
end)
theorem divides_sq_eq_zero_z {x y : ℤ} (h : x * x = d * y * y) : x = 0 ∧ y = 0 :=
by rw [mul_assoc, ← int.nat_abs_mul_self, ← int.nat_abs_mul_self, ← int.coe_nat_mul, ← mul_assoc]
at h;
exact let ⟨h1, h2⟩ := divides_sq_eq_zero (int.coe_nat_inj h) in
⟨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2⟩
theorem not_divides_square (x y) : (x + 1) * (x + 1) ≠ d * (y + 1) * (y + 1) :=
λe, by have t := (divides_sq_eq_zero e).left; contradiction
theorem nonneg_antisymm : Π {a : ℤ√d}, nonneg a → nonneg (-a) → a = 0
| ⟨0, 0⟩ xy yx := rfl
| ⟨-[1+ x], -[1+ y]⟩ xy yx := false.elim xy
| ⟨(x+1:nat), (y+1:nat)⟩ xy yx := false.elim yx
| ⟨-[1+ x], 0⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial)
| ⟨(x+1:nat), 0⟩ xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial)
| ⟨0, -[1+ y]⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos)
| ⟨0, (y+1:nat)⟩ _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos)
| ⟨(x+1:nat), -[1+ y]⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) :=
let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_square _ _)
| ⟨-[1+ x], (y+1:nat)⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) :=
let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_square _ _)
theorem le_antisymm {a b : ℤ√d} (ab : a ≤ b) (ba : b ≤ a) : a = b :=
eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab)
instance : linear_order ℤ√d :=
{ le_antisymm := @zsqrtd.le_antisymm,
le_total := zsqrtd.le_total,
decidable_le := zsqrtd.decidable_le,
..zsqrtd.preorder }
protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Π {a b : ℤ√d}, a * b = 0 → a = 0 ∨ b = 0
| ⟨x, y⟩ ⟨z, w⟩ h := by injection h with h1 h2; exact
have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero h1,
have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero h2,
have fin : x*x = d*y*y → (⟨x, y⟩:ℤ√d) = 0, from
λe, match x, y, divides_sq_eq_zero_z e with ._, ._, ⟨rfl, rfl⟩ := rfl end,
if z0 : z = 0 then if w0 : w = 0 then
or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end)
else
or.inl $ fin $ mul_right_cancel' w0 $ calc
x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm]
... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm]
else
or.inl $ fin $ mul_right_cancel' z0 $ calc
x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm]
... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm]
instance : integral_domain ℤ√d :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero,
.. zsqrtd.comm_ring, .. zsqrtd.nontrivial }
protected theorem mul_pos (a b : ℤ√d) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := λab,
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero
(le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0))))
(λe, ne_of_gt a0 e)
(λe, ne_of_gt b0 e)
instance : linear_ordered_comm_ring ℤ√d :=
{ add_le_add_left := @zsqrtd.add_le_add_left,
mul_pos := @zsqrtd.mul_pos,
zero_le_one := dec_trivial,
.. zsqrtd.comm_ring, .. zsqrtd.linear_order, .. zsqrtd.nontrivial }
instance : linear_ordered_semiring ℤ√d := by apply_instance
instance : ordered_semiring ℤ√d := by apply_instance
end
lemma norm_eq_zero {d : ℤ} (h_nonsquare : ∀ n : ℤ, d ≠ n*n) (a : ℤ√d) :
norm a = 0 ↔ a = 0 :=
begin
refine ⟨λ ha, ext.mpr _, λ h, by rw [h, norm_zero]⟩,
delta norm at ha,
rw sub_eq_zero at ha,
by_cases h : 0 ≤ d,
{ obtain ⟨d', rfl⟩ := int.eq_coe_of_zero_le h,
haveI : nonsquare d' := ⟨λ n h, h_nonsquare n $ by exact_mod_cast h⟩,
exact divides_sq_eq_zero_z ha, },
{ push_neg at h,
suffices : a.re * a.re = 0,
{ rw eq_zero_of_mul_self_eq_zero this at ha ⊢,
simpa only [true_and, or_self_right, zero_re, zero_im, eq_self_iff_true,
zero_eq_mul, mul_zero, mul_eq_zero, h.ne, false_or, or_self] using ha },
apply _root_.le_antisymm _ (mul_self_nonneg _),
rw [ha, mul_assoc],
exact mul_nonpos_of_nonpos_of_nonneg h.le (mul_self_nonneg _) }
end
variables {R : Type} [comm_ring R]
@[ext] lemma hom_ext {d : ℤ} (f g : ℤ√d →+* R) (h : f sqrtd = g sqrtd) : f = g :=
begin
ext ⟨x_re, x_im⟩,
simp [decompose, h],
end
/-- The unique `ring_hom` from `ℤ√d` to a ring `R`, constructed by replacing `√d` with the provided
root. Conversely, this associates to every mapping `ℤ√d →+* R` a value of `√d` in `R`. -/
@[simps]
def lift {d : ℤ} : {r : R // r * r = ↑d} ≃ (ℤ√d →+* R) :=
{ to_fun := λ r,
{ to_fun := λ a, a.1 + a.2*(r : R),
map_zero' := by simp,
map_add' := λ a b, by { simp, ring, },
map_one' := by simp,
map_mul' := λ a b, by {
have : (a.re + a.im * r : R) * (b.re + b.im * r) =
a.re * b.re + (a.re * b.im + a.im * b.re) * r + a.im * b.im * (r * r) := by ring,
simp [this, r.prop],
ring, } },
inv_fun := λ f, ⟨f sqrtd, by rw [←f.map_mul, dmuld, ring_hom.map_int_cast]⟩,
left_inv := λ r, by { ext, simp },
right_inv := λ f, by { ext, simp } }
/-- `lift r` is injective if `d` is non-square, and R has characteristic zero (that is, the map from
`ℤ` into `R` is injective). -/
lemma lift_injective [char_zero R] {d : ℤ} (r : {r : R // r * r = ↑d}) (hd : ∀ n : ℤ, d ≠ n*n) :
function.injective (lift r) :=
(lift r).injective_iff.mpr $ λ a ha,
begin
have h_inj : function.injective (coe : ℤ → R) := int.cast_injective,
suffices : lift r a.norm = 0,
{ simp only [coe_int_re, add_zero, lift_apply_apply, coe_int_im, int.cast_zero, zero_mul] at this,
rwa [← int.cast_zero, h_inj.eq_iff, norm_eq_zero hd] at this },
rw [norm_eq_mul_conj, ring_hom.map_mul, ha, zero_mul]
end
end zsqrtd
|
2e93754f7adb9f32ae4611a360c2d673d65e4b08 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/set/intervals/basic.lean | af601cd479b825f77cb83be7b2a107734974d66c | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 59,751 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne
-/
import algebra.order.group
import order.rel_iso
/-!
# Intervals
In any preorder `α`, we define intervals (which on each side can be either infinite, open, or
closed) using the following naming conventions:
- `i`: infinite
- `o`: open
- `c`: closed
Each interval has the name `I` + letter for left side + letter for right side. For instance,
`Ioc a b` denotes the inverval `(a, b]`.
This file contains these definitions, and basic facts on inclusion, intersection, difference of
intervals (where the precise statements may depend on the properties of the order, in particular
for some statements it should be `linear_order` or `densely_ordered`).
TODO: This is just the beginning; a lot of rules are missing
-/
variables {α β : Type*}
namespace set
open set
open order_dual (to_dual of_dual)
section preorder
variables [preorder α] {a a₁ a₂ b b₁ b₂ c x : α}
/-- Left-open right-open interval -/
def Ioo (a b : α) := {x | a < x ∧ x < b}
/-- Left-closed right-open interval -/
def Ico (a b : α) := {x | a ≤ x ∧ x < b}
/-- Left-infinite right-open interval -/
def Iio (a : α) := {x | x < a}
/-- Left-closed right-closed interval -/
def Icc (a b : α) := {x | a ≤ x ∧ x ≤ b}
/-- Left-infinite right-closed interval -/
def Iic (b : α) := {x | x ≤ b}
/-- Left-open right-closed interval -/
def Ioc (a b : α) := {x | a < x ∧ x ≤ b}
/-- Left-closed right-infinite interval -/
def Ici (a : α) := {x | a ≤ x}
/-- Left-open right-infinite interval -/
def Ioi (a : α) := {x | a < x}
lemma Ioo_def (a b : α) : {x | a < x ∧ x < b} = Ioo a b := rfl
lemma Ico_def (a b : α) : {x | a ≤ x ∧ x < b} = Ico a b := rfl
lemma Iio_def (a : α) : {x | x < a} = Iio a := rfl
lemma Icc_def (a b : α) : {x | a ≤ x ∧ x ≤ b} = Icc a b := rfl
lemma Iic_def (b : α) : {x | x ≤ b} = Iic b := rfl
lemma Ioc_def (a b : α) : {x | a < x ∧ x ≤ b} = Ioc a b := rfl
lemma Ici_def (a : α) : {x | a ≤ x} = Ici a := rfl
lemma Ioi_def (a : α) : {x | a < x} = Ioi a := rfl
@[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := iff.rfl
@[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := iff.rfl
@[simp] lemma mem_Iio : x ∈ Iio b ↔ x < b := iff.rfl
@[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl
@[simp] lemma mem_Iic : x ∈ Iic b ↔ x ≤ b := iff.rfl
@[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := iff.rfl
@[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := iff.rfl
@[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := iff.rfl
instance decidable_mem_Ioo [decidable (a < x ∧ x < b)] : decidable (x ∈ Ioo a b) := by assumption
instance decidable_mem_Ico [decidable (a ≤ x ∧ x < b)] : decidable (x ∈ Ico a b) := by assumption
instance decidable_mem_Iio [decidable (x < b)] : decidable (x ∈ Iio b) := by assumption
instance decidable_mem_Icc [decidable (a ≤ x ∧ x ≤ b)] : decidable (x ∈ Icc a b) := by assumption
instance decidable_mem_Iic [decidable (x ≤ b)] : decidable (x ∈ Iic b) := by assumption
instance decidable_mem_Ioc [decidable (a < x ∧ x ≤ b)] : decidable (x ∈ Ioc a b) := by assumption
instance decidable_mem_Ici [decidable (a ≤ x)] : decidable (x ∈ Ici a) := by assumption
instance decidable_mem_Ioi [decidable (a < x)] : decidable (x ∈ Ioi a) := by assumption
@[simp] lemma left_mem_Ioo : a ∈ Ioo a b ↔ false := by simp [lt_irrefl]
@[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl]
@[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl]
@[simp] lemma left_mem_Ioc : a ∈ Ioc a b ↔ false := by simp [lt_irrefl]
lemma left_mem_Ici : a ∈ Ici a := by simp
@[simp] lemma right_mem_Ioo : b ∈ Ioo a b ↔ false := by simp [lt_irrefl]
@[simp] lemma right_mem_Ico : b ∈ Ico a b ↔ false := by simp [lt_irrefl]
@[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl]
@[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl]
lemma right_mem_Iic : a ∈ Iic a := by simp
@[simp] lemma dual_Ici : Ici (to_dual a) = of_dual ⁻¹' Iic a := rfl
@[simp] lemma dual_Iic : Iic (to_dual a) = of_dual ⁻¹' Ici a := rfl
@[simp] lemma dual_Ioi : Ioi (to_dual a) = of_dual ⁻¹' Iio a := rfl
@[simp] lemma dual_Iio : Iio (to_dual a) = of_dual ⁻¹' Ioi a := rfl
@[simp] lemma dual_Icc : Icc (to_dual a) (to_dual b) = of_dual ⁻¹' Icc b a :=
set.ext $ λ x, and_comm _ _
@[simp] lemma dual_Ioc : Ioc (to_dual a) (to_dual b) = of_dual ⁻¹' Ico b a :=
set.ext $ λ x, and_comm _ _
@[simp] lemma dual_Ico : Ico (to_dual a) (to_dual b) = of_dual ⁻¹' Ioc b a :=
set.ext $ λ x, and_comm _ _
@[simp] lemma dual_Ioo : Ioo (to_dual a) (to_dual b) = of_dual ⁻¹' Ioo b a :=
set.ext $ λ x, and_comm _ _
@[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b :=
⟨λ ⟨x, hx⟩, hx.1.trans hx.2, λ h, ⟨a, left_mem_Icc.2 h⟩⟩
@[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b :=
⟨λ ⟨x, hx⟩, hx.1.trans_lt hx.2, λ h, ⟨a, left_mem_Ico.2 h⟩⟩
@[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b :=
⟨λ ⟨x, hx⟩, hx.1.trans_le hx.2, λ h, ⟨b, right_mem_Ioc.2 h⟩⟩
@[simp] lemma nonempty_Ici : (Ici a).nonempty := ⟨a, left_mem_Ici⟩
@[simp] lemma nonempty_Iic : (Iic a).nonempty := ⟨a, right_mem_Iic⟩
@[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b :=
⟨λ ⟨x, ha, hb⟩, ha.trans hb, exists_between⟩
@[simp] lemma nonempty_Ioi [no_max_order α] : (Ioi a).nonempty := exists_gt a
@[simp] lemma nonempty_Iio [no_min_order α] : (Iio a).nonempty := exists_lt a
lemma nonempty_Icc_subtype (h : a ≤ b) : nonempty (Icc a b) :=
nonempty.to_subtype (nonempty_Icc.mpr h)
lemma nonempty_Ico_subtype (h : a < b) : nonempty (Ico a b) :=
nonempty.to_subtype (nonempty_Ico.mpr h)
lemma nonempty_Ioc_subtype (h : a < b) : nonempty (Ioc a b) :=
nonempty.to_subtype (nonempty_Ioc.mpr h)
/-- An interval `Ici a` is nonempty. -/
instance nonempty_Ici_subtype : nonempty (Ici a) :=
nonempty.to_subtype nonempty_Ici
/-- An interval `Iic a` is nonempty. -/
instance nonempty_Iic_subtype : nonempty (Iic a) :=
nonempty.to_subtype nonempty_Iic
lemma nonempty_Ioo_subtype [densely_ordered α] (h : a < b) : nonempty (Ioo a b) :=
nonempty.to_subtype (nonempty_Ioo.mpr h)
/-- In an order without maximal elements, the intervals `Ioi` are nonempty. -/
instance nonempty_Ioi_subtype [no_max_order α] : nonempty (Ioi a) :=
nonempty.to_subtype nonempty_Ioi
/-- In an order without minimal elements, the intervals `Iio` are nonempty. -/
instance nonempty_Iio_subtype [no_min_order α] : nonempty (Iio a) :=
nonempty.to_subtype nonempty_Iio
instance [no_min_order α] : no_min_order (Iio a) :=
⟨λ a, let ⟨b, hb⟩ := exists_lt (a : α) in ⟨⟨b, lt_trans hb a.2⟩, hb⟩⟩
instance [no_min_order α] : no_min_order (Iic a) :=
⟨λ a, let ⟨b, hb⟩ := exists_lt (a : α) in ⟨⟨b, hb.le.trans a.2⟩, hb⟩⟩
instance [no_max_order α] : no_max_order (Ioi a) :=
order_dual.no_max_order (Iio (to_dual a))
instance [no_max_order α] : no_max_order (Ici a) :=
order_dual.no_max_order (Iic (to_dual a))
@[simp] lemma Icc_eq_empty (h : ¬a ≤ b) : Icc a b = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans hb)
@[simp] lemma Ico_eq_empty (h : ¬a < b) : Ico a b = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans_lt hb)
@[simp] lemma Ioc_eq_empty (h : ¬a < b) : Ioc a b = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans_le hb)
@[simp] lemma Ioo_eq_empty (h : ¬a < b) : Ioo a b = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans hb)
@[simp] lemma Icc_eq_empty_of_lt (h : b < a) : Icc a b = ∅ :=
Icc_eq_empty h.not_le
@[simp] lemma Ico_eq_empty_of_le (h : b ≤ a) : Ico a b = ∅ :=
Ico_eq_empty h.not_lt
@[simp] lemma Ioc_eq_empty_of_le (h : b ≤ a) : Ioc a b = ∅ :=
Ioc_eq_empty h.not_lt
@[simp] lemma Ioo_eq_empty_of_le (h : b ≤ a) : Ioo a b = ∅ :=
Ioo_eq_empty h.not_lt
@[simp] lemma Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty $ lt_irrefl _
@[simp] lemma Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty $ lt_irrefl _
@[simp] lemma Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty $ lt_irrefl _
lemma Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a :=
⟨λ h, h $ left_mem_Ici, λ h x hx, h.trans hx⟩
lemma Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici αᵒᵈ _ _ _
lemma Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a :=
⟨λ h, h left_mem_Ici, λ h x hx, h.trans_le hx⟩
lemma Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b :=
⟨λ h, h right_mem_Iic, λ h x hx, lt_of_le_of_lt hx h⟩
lemma Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) :
Ioo a₁ b₁ ⊆ Ioo a₂ b₂ :=
λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans_lt hx₁, hx₂.trans_le h₂⟩
lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b :=
Ioo_subset_Ioo h le_rfl
lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ :=
Ioo_subset_Ioo le_rfl h
lemma Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) :
Ico a₁ b₁ ⊆ Ico a₂ b₂ :=
λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans hx₁, hx₂.trans_le h₂⟩
lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b :=
Ico_subset_Ico h le_rfl
lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ :=
Ico_subset_Ico le_rfl h
lemma Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) :
Icc a₁ b₁ ⊆ Icc a₂ b₂ :=
λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans hx₁, le_trans hx₂ h₂⟩
lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b :=
Icc_subset_Icc h le_rfl
lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ :=
Icc_subset_Icc le_rfl h
lemma Icc_subset_Ioo (ha : a₂ < a₁) (hb : b₁ < b₂) :
Icc a₁ b₁ ⊆ Ioo a₂ b₂ :=
λ x hx, ⟨ha.trans_le hx.1, hx.2.trans_lt hb⟩
lemma Icc_subset_Ici_self : Icc a b ⊆ Ici a := λ x, and.left
lemma Icc_subset_Iic_self : Icc a b ⊆ Iic b := λ x, and.right
lemma Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := λ x, and.right
lemma Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) :
Ioc a₁ b₁ ⊆ Ioc a₂ b₂ :=
λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans_lt hx₁, hx₂.trans h₂⟩
lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b :=
Ioc_subset_Ioc h le_rfl
lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ :=
Ioc_subset_Ioc le_rfl h
lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b :=
λ x, and.imp_left h₁.trans_le
lemma Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ :=
λ x, and.imp_right $ λ h', h'.trans_lt h
lemma Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ :=
λ x, and.imp_right $ λ h₂, h₂.trans_lt h₁
lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ x, and.imp_left le_of_lt
lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := λ x, and.imp_right le_of_lt
lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := λ x, and.imp_right le_of_lt
lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := λ x, and.imp_left le_of_lt
lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b :=
subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self
lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := λ x, and.right
lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := λ x, and.right
lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := λ x, and.left
lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := λ x, and.left
lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := λ x hx, le_of_lt hx
lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := λ x hx, le_of_lt hx
lemma Ico_subset_Ici_self : Ico a b ⊆ Ici a := λ x, and.left
lemma Ioi_ssubset_Ici_self : Ioi a ⊂ Ici a := ⟨Ioi_subset_Ici_self, λ h, lt_irrefl a (h le_rfl)⟩
lemma Iio_ssubset_Iic_self : Iio a ⊂ Iic a := @Ioi_ssubset_Ici_self αᵒᵈ _ _
lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ :=
⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩,
λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans hx, hx'.trans h'⟩⟩
lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ :=
⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩,
λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans_le hx, hx'.trans_lt h'⟩⟩
lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ :=
⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩,
λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans hx, hx'.trans_lt h'⟩⟩
lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ :=
⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩,
λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans_le hx, hx'.trans h'⟩⟩
lemma Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ :=
⟨λ h, h ⟨h₁, le_rfl⟩, λ h x ⟨hx, hx'⟩, hx'.trans_lt h⟩
lemma Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ :=
⟨λ h, h ⟨le_rfl, h₁⟩, λ h x ⟨hx, hx'⟩, h.trans_le hx⟩
lemma Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ :=
⟨λ h, h ⟨h₁, le_rfl⟩, λ h x ⟨hx, hx'⟩, hx'.trans h⟩
lemma Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) :
Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ :=
⟨λ h, h ⟨le_rfl, h₁⟩, λ h x ⟨hx, hx'⟩, h.trans hx⟩
lemma Icc_ssubset_Icc_left (hI : a₂ ≤ b₂) (ha : a₂ < a₁) (hb : b₁ ≤ b₂) :
Icc a₁ b₁ ⊂ Icc a₂ b₂ :=
(ssubset_iff_of_subset (Icc_subset_Icc (le_of_lt ha) hb)).mpr
⟨a₂, left_mem_Icc.mpr hI, not_and.mpr (λ f g, lt_irrefl a₂ (ha.trans_le f))⟩
lemma Icc_ssubset_Icc_right (hI : a₂ ≤ b₂) (ha : a₂ ≤ a₁) (hb : b₁ < b₂) :
Icc a₁ b₁ ⊂ Icc a₂ b₂ :=
(ssubset_iff_of_subset (Icc_subset_Icc ha (le_of_lt hb))).mpr
⟨b₂, right_mem_Icc.mpr hI, (λ f, lt_irrefl b₁ (hb.trans_le f.2))⟩
/-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need
the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/
lemma Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a :=
λ x hx, h.trans_lt hx
/-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need
the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/
lemma Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a :=
subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self
/-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need
the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/
lemma Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b :=
λ x hx, lt_of_lt_of_le hx h
/-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need
the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/
lemma Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b :=
subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self
lemma Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl
lemma Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl
lemma Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl
lemma Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl
lemma Iic_inter_Ici : Iic a ∩ Ici b = Icc b a := inter_comm _ _
lemma Iio_inter_Ici : Iio a ∩ Ici b = Ico b a := inter_comm _ _
lemma Iic_inter_Ioi : Iic a ∩ Ioi b = Ioc b a := inter_comm _ _
lemma Iio_inter_Ioi : Iio a ∩ Ioi b = Ioo b a := inter_comm _ _
lemma mem_Icc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Icc a b := Ioo_subset_Icc_self h
lemma mem_Ico_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ico a b := Ioo_subset_Ico_self h
lemma mem_Ioc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ioc a b := Ioo_subset_Ioc_self h
lemma mem_Icc_of_Ico (h : x ∈ Ico a b) : x ∈ Icc a b := Ico_subset_Icc_self h
lemma mem_Icc_of_Ioc (h : x ∈ Ioc a b) : x ∈ Icc a b := Ioc_subset_Icc_self h
lemma mem_Ici_of_Ioi (h : x ∈ Ioi a) : x ∈ Ici a := Ioi_subset_Ici_self h
lemma mem_Iic_of_Iio (h : x ∈ Iio a) : x ∈ Iic a := Iio_subset_Iic_self h
lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ ¬a ≤ b :=
by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Icc]
lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ ¬a < b :=
by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ico]
lemma Ioc_eq_empty_iff : Ioc a b = ∅ ↔ ¬a < b :=
by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioc]
lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ ¬a < b :=
by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioo]
lemma _root_.is_top.Iic_eq (h : is_top a) : Iic a = univ := eq_univ_of_forall h
lemma _root_.is_bot.Ici_eq (h : is_bot a) : Ici a = univ := eq_univ_of_forall h
lemma _root_.is_max.Ioi_eq (h : is_max a) : Ioi a = ∅ := eq_empty_of_subset_empty $ λ b, h.not_lt
lemma _root_.is_min.Iio_eq (h : is_min a) : Iio a = ∅ := eq_empty_of_subset_empty $ λ b, h.not_lt
lemma Iic_inter_Ioc_of_le (h : a ≤ c) : Iic a ∩ Ioc b c = Ioc b a :=
ext $ λ x, ⟨λ H, ⟨H.2.1, H.1⟩, λ H, ⟨H.2, H.1, H.2.trans h⟩⟩
end preorder
section partial_order
variables [partial_order α] {a b c : α}
@[simp] lemma Icc_self (a : α) : Icc a a = {a} :=
set.ext $ by simp [Icc, le_antisymm_iff, and_comm]
@[simp] lemma Icc_eq_singleton_iff : Icc a b = {c} ↔ a = c ∧ b = c :=
begin
refine ⟨λ h, _, _⟩,
{ have hab : a ≤ b := nonempty_Icc.1 (h.symm.subst $ singleton_nonempty c),
exact ⟨eq_of_mem_singleton $ h.subst $ left_mem_Icc.2 hab,
eq_of_mem_singleton $ h.subst $ right_mem_Icc.2 hab⟩ },
{ rintro ⟨rfl, rfl⟩,
exact Icc_self _ }
end
@[simp] lemma Icc_diff_left : Icc a b \ {a} = Ioc a b :=
ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm, and.right_comm]
@[simp] lemma Icc_diff_right : Icc a b \ {b} = Ico a b :=
ext $ λ x, by simp [lt_iff_le_and_ne, and_assoc]
@[simp] lemma Ico_diff_left : Ico a b \ {a} = Ioo a b :=
ext $ λ x, by simp [and.right_comm, ← lt_iff_le_and_ne, eq_comm]
@[simp] lemma Ioc_diff_right : Ioc a b \ {b} = Ioo a b :=
ext $ λ x, by simp [and_assoc, ← lt_iff_le_and_ne]
@[simp] lemma Icc_diff_both : Icc a b \ {a, b} = Ioo a b :=
by rw [insert_eq, ← diff_diff, Icc_diff_left, Ioc_diff_right]
@[simp] lemma Ici_diff_left : Ici a \ {a} = Ioi a :=
ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm]
@[simp] lemma Iic_diff_right : Iic a \ {a} = Iio a :=
ext $ λ x, by simp [lt_iff_le_and_ne]
@[simp] lemma Ico_diff_Ioo_same (h : a < b) : Ico a b \ Ioo a b = {a} :=
by rw [← Ico_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Ico.2 h)]
@[simp] lemma Ioc_diff_Ioo_same (h : a < b) : Ioc a b \ Ioo a b = {b} :=
by rw [← Ioc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Ioc.2 h)]
@[simp] lemma Icc_diff_Ico_same (h : a ≤ b) : Icc a b \ Ico a b = {b} :=
by rw [← Icc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Icc.2 h)]
@[simp] lemma Icc_diff_Ioc_same (h : a ≤ b) : Icc a b \ Ioc a b = {a} :=
by rw [← Icc_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Icc.2 h)]
@[simp] lemma Icc_diff_Ioo_same (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} :=
by { rw [← Icc_diff_both, diff_diff_cancel_left], simp [insert_subset, h] }
@[simp] lemma Ici_diff_Ioi_same : Ici a \ Ioi a = {a} :=
by rw [← Ici_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 left_mem_Ici)]
@[simp] lemma Iic_diff_Iio_same : Iic a \ Iio a = {a} :=
by rw [← Iic_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 right_mem_Iic)]
@[simp] lemma Ioi_union_left : Ioi a ∪ {a} = Ici a := ext $ λ x, by simp [eq_comm, le_iff_eq_or_lt]
@[simp] lemma Iio_union_right : Iio a ∪ {a} = Iic a := ext $ λ x, le_iff_lt_or_eq.symm
lemma Ioo_union_left (hab : a < b) : Ioo a b ∪ {a} = Ico a b :=
by rw [← Ico_diff_left, diff_union_self,
union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Ico.2 hab)]
lemma Ioo_union_right (hab : a < b) : Ioo a b ∪ {b} = Ioc a b :=
by simpa only [dual_Ioo, dual_Ico] using Ioo_union_left hab.dual
lemma Ioc_union_left (hab : a ≤ b) : Ioc a b ∪ {a} = Icc a b :=
by rw [← Icc_diff_left, diff_union_self,
union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Icc.2 hab)]
lemma Ico_union_right (hab : a ≤ b) : Ico a b ∪ {b} = Icc a b :=
by simpa only [dual_Ioc, dual_Icc] using Ioc_union_left hab.dual
@[simp] lemma Ico_insert_right (h : a ≤ b) : insert b (Ico a b) = Icc a b :=
by rw [insert_eq, union_comm, Ico_union_right h]
@[simp] lemma Ioc_insert_left (h : a ≤ b) : insert a (Ioc a b) = Icc a b :=
by rw [insert_eq, union_comm, Ioc_union_left h]
@[simp] lemma Ioo_insert_left (h : a < b) : insert a (Ioo a b) = Ico a b :=
by rw [insert_eq, union_comm, Ioo_union_left h]
@[simp] lemma Ioo_insert_right (h : a < b) : insert b (Ioo a b) = Ioc a b :=
by rw [insert_eq, union_comm, Ioo_union_right h]
@[simp] lemma Iio_insert : insert a (Iio a) = Iic a := ext $ λ _, le_iff_eq_or_lt.symm
@[simp] lemma Ioi_insert : insert a (Ioi a) = Ici a :=
ext $ λ _, (or_congr_left' eq_comm).trans le_iff_eq_or_lt.symm
lemma mem_Ici_Ioi_of_subset_of_subset {s : set α} (ho : Ioi a ⊆ s) (hc : s ⊆ Ici a) :
s ∈ ({Ici a, Ioi a} : set (set α)) :=
classical.by_cases
(λ h : a ∈ s, or.inl $ subset.antisymm hc $ by rw [← Ioi_union_left, union_subset_iff]; simp *)
(λ h, or.inr $ subset.antisymm (λ x hx, lt_of_le_of_ne (hc hx) (λ heq, h $ heq.symm ▸ hx)) ho)
lemma mem_Iic_Iio_of_subset_of_subset {s : set α} (ho : Iio a ⊆ s) (hc : s ⊆ Iic a) :
s ∈ ({Iic a, Iio a} : set (set α)) :=
@mem_Ici_Ioi_of_subset_of_subset αᵒᵈ _ a s ho hc
lemma mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset {s : set α} (ho : Ioo a b ⊆ s) (hc : s ⊆ Icc a b) :
s ∈ ({Icc a b, Ico a b, Ioc a b, Ioo a b} : set (set α)) :=
begin
classical,
by_cases ha : a ∈ s; by_cases hb : b ∈ s,
{ refine or.inl (subset.antisymm hc _),
rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha,
← Icc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho },
{ refine (or.inr $ or.inl $ subset.antisymm _ _),
{ rw [← Icc_diff_right],
exact subset_diff_singleton hc hb },
{ rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha] at ho } },
{ refine (or.inr $ or.inr $ or.inl $ subset.antisymm _ _),
{ rw [← Icc_diff_left],
exact subset_diff_singleton hc ha },
{ rwa [← Ioc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho } },
{ refine (or.inr $ or.inr $ or.inr $ subset.antisymm _ ho),
rw [← Ico_diff_left, ← Icc_diff_right],
apply_rules [subset_diff_singleton] }
end
lemma eq_left_or_mem_Ioo_of_mem_Ico {x : α} (hmem : x ∈ Ico a b) :
x = a ∨ x ∈ Ioo a b :=
hmem.1.eq_or_gt.imp_right $ λ h, ⟨h, hmem.2⟩
lemma eq_right_or_mem_Ioo_of_mem_Ioc {x : α} (hmem : x ∈ Ioc a b) :
x = b ∨ x ∈ Ioo a b :=
hmem.2.eq_or_lt.imp_right $ and.intro hmem.1
lemma eq_endpoints_or_mem_Ioo_of_mem_Icc {x : α} (hmem : x ∈ Icc a b) :
x = a ∨ x = b ∨ x ∈ Ioo a b :=
hmem.1.eq_or_gt.imp_right $ λ h, eq_right_or_mem_Ioo_of_mem_Ioc ⟨h, hmem.2⟩
lemma _root_.is_max.Ici_eq (h : is_max a) : Ici a = {a} :=
eq_singleton_iff_unique_mem.2 ⟨left_mem_Ici, λ b, h.eq_of_ge⟩
lemma _root_.is_min.Iic_eq (h : is_min a) : Iic a = {a} := h.to_dual.Ici_eq
end partial_order
section order_top
@[simp] lemma Ici_top [partial_order α] [order_top α] : Ici (⊤ : α) = {⊤} := is_max_top.Ici_eq
variables [preorder α] [order_top α] {a : α}
@[simp] lemma Ioi_top : Ioi (⊤ : α) = ∅ := is_max_top.Ioi_eq
@[simp] lemma Iic_top : Iic (⊤ : α) = univ := is_top_top.Iic_eq
@[simp] lemma Icc_top : Icc a ⊤ = Ici a := by simp [← Ici_inter_Iic]
@[simp] lemma Ioc_top : Ioc a ⊤ = Ioi a := by simp [← Ioi_inter_Iic]
end order_top
section order_bot
@[simp] lemma Iic_bot [partial_order α] [order_bot α] : Iic (⊥ : α) = {⊥} :=
is_min_bot.Iic_eq
variables [preorder α] [order_bot α] {a : α}
@[simp] lemma Iio_bot : Iio (⊥ : α) = ∅ := is_min_bot.Iio_eq
@[simp] lemma Ici_bot : Ici (⊥ : α) = univ := is_bot_bot.Ici_eq
@[simp] lemma Icc_bot : Icc ⊥ a = Iic a := by simp [← Ici_inter_Iic]
@[simp] lemma Ico_bot : Ico ⊥ a = Iio a := by simp [← Ici_inter_Iio]
end order_bot
lemma Icc_bot_top [partial_order α] [bounded_order α] : Icc (⊥ : α) ⊤ = univ := by simp
section linear_order
variables [linear_order α] {a a₁ a₂ b b₁ b₂ c d : α}
lemma not_mem_Ici : c ∉ Ici a ↔ c < a := not_le
lemma not_mem_Iic : c ∉ Iic b ↔ b < c := not_le
lemma not_mem_Icc_of_lt (ha : c < a) : c ∉ Icc a b :=
not_mem_subset Icc_subset_Ici_self $ not_mem_Ici.mpr ha
lemma not_mem_Icc_of_gt (hb : b < c) : c ∉ Icc a b :=
not_mem_subset Icc_subset_Iic_self $ not_mem_Iic.mpr hb
lemma not_mem_Ico_of_lt (ha : c < a) : c ∉ Ico a b :=
not_mem_subset Ico_subset_Ici_self $ not_mem_Ici.mpr ha
lemma not_mem_Ioc_of_gt (hb : b < c) : c ∉ Ioc a b :=
not_mem_subset Ioc_subset_Iic_self $ not_mem_Iic.mpr hb
lemma not_mem_Ioi : c ∉ Ioi a ↔ c ≤ a := not_lt
lemma not_mem_Iio : c ∉ Iio b ↔ b ≤ c := not_lt
lemma not_mem_Ioc_of_le (ha : c ≤ a) : c ∉ Ioc a b :=
not_mem_subset Ioc_subset_Ioi_self $ not_mem_Ioi.mpr ha
lemma not_mem_Ico_of_ge (hb : b ≤ c) : c ∉ Ico a b :=
not_mem_subset Ico_subset_Iio_self $ not_mem_Iio.mpr hb
lemma not_mem_Ioo_of_le (ha : c ≤ a) : c ∉ Ioo a b :=
not_mem_subset Ioo_subset_Ioi_self $ not_mem_Ioi.mpr ha
lemma not_mem_Ioo_of_ge (hb : b ≤ c) : c ∉ Ioo a b :=
not_mem_subset Ioo_subset_Iio_self $ not_mem_Iio.mpr hb
@[simp] lemma compl_Iic : (Iic a)ᶜ = Ioi a := ext $ λ _, not_le
@[simp] lemma compl_Ici : (Ici a)ᶜ = Iio a := ext $ λ _, not_le
@[simp] lemma compl_Iio : (Iio a)ᶜ = Ici a := ext $ λ _, not_lt
@[simp] lemma compl_Ioi : (Ioi a)ᶜ = Iic a := ext $ λ _, not_lt
@[simp] lemma Ici_diff_Ici : Ici a \ Ici b = Ico a b :=
by rw [diff_eq, compl_Ici, Ici_inter_Iio]
@[simp] lemma Ici_diff_Ioi : Ici a \ Ioi b = Icc a b :=
by rw [diff_eq, compl_Ioi, Ici_inter_Iic]
@[simp] lemma Ioi_diff_Ioi : Ioi a \ Ioi b = Ioc a b :=
by rw [diff_eq, compl_Ioi, Ioi_inter_Iic]
@[simp] lemma Ioi_diff_Ici : Ioi a \ Ici b = Ioo a b :=
by rw [diff_eq, compl_Ici, Ioi_inter_Iio]
@[simp] lemma Iic_diff_Iic : Iic b \ Iic a = Ioc a b :=
by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iic]
@[simp] lemma Iio_diff_Iic : Iio b \ Iic a = Ioo a b :=
by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iio]
@[simp] lemma Iic_diff_Iio : Iic b \ Iio a = Icc a b :=
by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iic]
@[simp] lemma Iio_diff_Iio : Iio b \ Iio a = Ico a b :=
by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iio]
lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) :
Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ :=
⟨λ h, have a₂ ≤ a₁ ∧ a₁ < b₂ := h ⟨le_rfl, h₁⟩,
⟨this.1, le_of_not_lt $ λ h', lt_irrefl b₂ (h ⟨this.2.le, h'⟩).2⟩,
λ ⟨h₁, h₂⟩, Ico_subset_Ico h₁ h₂⟩
lemma Ioc_subset_Ioc_iff (h₁ : a₁ < b₁) :
Ioc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ b₁ ≤ b₂ ∧ a₂ ≤ a₁ :=
by { convert @Ico_subset_Ico_iff αᵒᵈ _ b₁ b₂ a₁ a₂ h₁; exact (@dual_Ico α _ _ _).symm }
lemma Ioo_subset_Ioo_iff [densely_ordered α] (h₁ : a₁ < b₁) :
Ioo a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ :=
⟨λ h, begin
rcases exists_between h₁ with ⟨x, xa, xb⟩,
split; refine le_of_not_lt (λ h', _),
{ have ab := (h ⟨xa, xb⟩).1.trans xb,
exact lt_irrefl _ (h ⟨h', ab⟩).1 },
{ have ab := xa.trans (h ⟨xa, xb⟩).2,
exact lt_irrefl _ (h ⟨ab, h'⟩).2 }
end, λ ⟨h₁, h₂⟩, Ioo_subset_Ioo h₁ h₂⟩
lemma Ico_eq_Ico_iff (h : a₁ < b₁ ∨ a₂ < b₂) : Ico a₁ b₁ = Ico a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ :=
⟨λ e, begin
simp [subset.antisymm_iff] at e, simp [le_antisymm_iff],
cases h; simp [Ico_subset_Ico_iff h] at e;
[ rcases e with ⟨⟨h₁, h₂⟩, e'⟩, rcases e with ⟨e', ⟨h₁, h₂⟩⟩ ];
have := (Ico_subset_Ico_iff $ h₁.trans_lt $ h.trans_le h₂).1 e';
tauto
end, λ ⟨h₁, h₂⟩, by rw [h₁, h₂]⟩
open_locale classical
@[simp] lemma Ioi_subset_Ioi_iff : Ioi b ⊆ Ioi a ↔ a ≤ b :=
begin
refine ⟨λ h, _, λ h, Ioi_subset_Ioi h⟩,
by_contradiction ba,
exact lt_irrefl _ (h (not_le.mp ba))
end
@[simp] lemma Ioi_subset_Ici_iff [densely_ordered α] : Ioi b ⊆ Ici a ↔ a ≤ b :=
begin
refine ⟨λ h, _, λ h, Ioi_subset_Ici h⟩,
by_contradiction ba,
obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := exists_between (not_le.mp ba),
exact lt_irrefl _ (ca.trans_le (h bc))
end
@[simp] lemma Iio_subset_Iio_iff : Iio a ⊆ Iio b ↔ a ≤ b :=
begin
refine ⟨λ h, _, λ h, Iio_subset_Iio h⟩,
by_contradiction ab,
exact lt_irrefl _ (h (not_le.mp ab))
end
@[simp] lemma Iio_subset_Iic_iff [densely_ordered α] : Iio a ⊆ Iic b ↔ a ≤ b :=
by rw [←diff_eq_empty, Iio_diff_Iic, Ioo_eq_empty_iff, not_lt]
/-! ### Unions of adjacent intervals -/
/-! #### Two infinite intervals -/
lemma Iic_union_Ioi_of_le (h : a ≤ b) : Iic b ∪ Ioi a = univ :=
eq_univ_of_forall $ λ x, (h.lt_or_le x).symm
lemma Iio_union_Ici_of_le (h : a ≤ b) : Iio b ∪ Ici a = univ :=
eq_univ_of_forall $ λ x, (h.le_or_lt x).symm
lemma Iic_union_Ici_of_le (h : a ≤ b) : Iic b ∪ Ici a = univ :=
eq_univ_of_forall $ λ x, (h.le_or_le x).symm
lemma Iio_union_Ioi_of_lt (h : a < b) : Iio b ∪ Ioi a = univ :=
eq_univ_of_forall $ λ x, (h.lt_or_lt x).symm
@[simp] lemma Iic_union_Ici : Iic a ∪ Ici a = univ := Iic_union_Ici_of_le le_rfl
@[simp] lemma Iio_union_Ici : Iio a ∪ Ici a = univ := Iio_union_Ici_of_le le_rfl
@[simp] lemma Iic_union_Ioi : Iic a ∪ Ioi a = univ := Iic_union_Ioi_of_le le_rfl
@[simp] lemma Iio_union_Ioi : Iio a ∪ Ioi a = {a}ᶜ := ext $ λ x, lt_or_lt_iff_ne
/-! #### A finite and an infinite interval -/
lemma Ioo_union_Ioi' (h₁ : c < b) :
Ioo a b ∪ Ioi c = Ioi (min a c) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ioo, mem_Ioi, min_lt_iff],
by_cases hc : c < x,
{ tauto },
{ have hxb : x < b := (le_of_not_gt hc).trans_lt h₁,
tauto },
end
lemma Ioo_union_Ioi (h : c < max a b) :
Ioo a b ∪ Ioi c = Ioi (min a c) :=
begin
cases le_total a b with hab hab; simp [hab] at h,
{ exact Ioo_union_Ioi' h },
{ rw min_comm,
simp [*, min_eq_left_of_lt] },
end
lemma Ioi_subset_Ioo_union_Ici : Ioi a ⊆ Ioo a b ∪ Ici b :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)
@[simp] lemma Ioo_union_Ici_eq_Ioi (h : a < b) : Ioo a b ∪ Ici b = Ioi a :=
subset.antisymm (λ x hx, hx.elim and.left h.trans_le) Ioi_subset_Ioo_union_Ici
lemma Ici_subset_Ico_union_Ici : Ici a ⊆ Ico a b ∪ Ici b :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)
@[simp] lemma Ico_union_Ici_eq_Ici (h : a ≤ b) : Ico a b ∪ Ici b = Ici a :=
subset.antisymm (λ x hx, hx.elim and.left h.trans) Ici_subset_Ico_union_Ici
lemma Ico_union_Ici' (h₁ : c ≤ b) :
Ico a b ∪ Ici c = Ici (min a c) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ico, mem_Ici, min_le_iff],
by_cases hc : c ≤ x,
{ tauto },
{ have hxb : x < b := (lt_of_not_ge hc).trans_le h₁,
tauto },
end
lemma Ico_union_Ici (h : c ≤ max a b) :
Ico a b ∪ Ici c = Ici (min a c) :=
begin
cases le_total a b with hab hab; simp [hab] at h,
{ exact Ico_union_Ici' h },
{ simp [*] },
end
lemma Ioi_subset_Ioc_union_Ioi : Ioi a ⊆ Ioc a b ∪ Ioi b :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)
@[simp] lemma Ioc_union_Ioi_eq_Ioi (h : a ≤ b) : Ioc a b ∪ Ioi b = Ioi a :=
subset.antisymm (λ x hx, hx.elim and.left h.trans_lt) Ioi_subset_Ioc_union_Ioi
lemma Ioc_union_Ioi' (h₁ : c ≤ b) :
Ioc a b ∪ Ioi c = Ioi (min a c) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ioc, mem_Ioi, min_lt_iff],
by_cases hc : c < x,
{ tauto },
{ have hxb : x ≤ b := (le_of_not_gt hc).trans h₁,
tauto },
end
lemma Ioc_union_Ioi (h : c ≤ max a b) :
Ioc a b ∪ Ioi c = Ioi (min a c) :=
begin
cases le_total a b with hab hab; simp [hab] at h,
{ exact Ioc_union_Ioi' h },
{ simp [*] },
end
lemma Ici_subset_Icc_union_Ioi : Ici a ⊆ Icc a b ∪ Ioi b :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)
@[simp] lemma Icc_union_Ioi_eq_Ici (h : a ≤ b) : Icc a b ∪ Ioi b = Ici a :=
subset.antisymm (λ x hx, hx.elim and.left $ λ hx', h.trans $ le_of_lt hx') Ici_subset_Icc_union_Ioi
lemma Ioi_subset_Ioc_union_Ici : Ioi a ⊆ Ioc a b ∪ Ici b :=
subset.trans Ioi_subset_Ioo_union_Ici (union_subset_union_left _ Ioo_subset_Ioc_self)
@[simp] lemma Ioc_union_Ici_eq_Ioi (h : a < b) : Ioc a b ∪ Ici b = Ioi a :=
subset.antisymm (λ x hx, hx.elim and.left h.trans_le) Ioi_subset_Ioc_union_Ici
lemma Ici_subset_Icc_union_Ici : Ici a ⊆ Icc a b ∪ Ici b :=
subset.trans Ici_subset_Ico_union_Ici (union_subset_union_left _ Ico_subset_Icc_self)
@[simp] lemma Icc_union_Ici_eq_Ici (h : a ≤ b) : Icc a b ∪ Ici b = Ici a :=
subset.antisymm (λ x hx, hx.elim and.left h.trans) Ici_subset_Icc_union_Ici
lemma Icc_union_Ici' (h₁ : c ≤ b) :
Icc a b ∪ Ici c = Ici (min a c) :=
begin
ext1 x,
simp_rw [mem_union, mem_Icc, mem_Ici, min_le_iff],
by_cases hc : c ≤ x,
{ tauto },
{ have hxb : x ≤ b := (le_of_not_ge hc).trans h₁,
tauto },
end
lemma Icc_union_Ici (h : c ≤ max a b) :
Icc a b ∪ Ici c = Ici (min a c) :=
begin
cases le_or_lt a b with hab hab; simp [hab] at h,
{ exact Icc_union_Ici' h },
{ cases h,
{ simp [*] },
{ have hca : c ≤ a := h.trans hab.le,
simp [*] } },
end
/-! #### An infinite and a finite interval -/
lemma Iic_subset_Iio_union_Icc : Iic b ⊆ Iio a ∪ Icc a b :=
λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)
@[simp] lemma Iio_union_Icc_eq_Iic (h : a ≤ b) : Iio a ∪ Icc a b = Iic b :=
subset.antisymm (λ x hx, hx.elim (λ hx, (le_of_lt hx).trans h) and.right)
Iic_subset_Iio_union_Icc
lemma Iio_subset_Iio_union_Ico : Iio b ⊆ Iio a ∪ Ico a b :=
λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)
@[simp] lemma Iio_union_Ico_eq_Iio (h : a ≤ b) : Iio a ∪ Ico a b = Iio b :=
subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_lt_of_le hx' h) and.right) Iio_subset_Iio_union_Ico
lemma Iio_union_Ico' (h₁ : c ≤ b) :
Iio b ∪ Ico c d = Iio (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Iio, mem_Ico, lt_max_iff],
by_cases hc : c ≤ x,
{ tauto },
{ have hxb : x < b := (lt_of_not_ge hc).trans_le h₁,
tauto },
end
lemma Iio_union_Ico (h : min c d ≤ b) :
Iio b ∪ Ico c d = Iio (max b d) :=
begin
cases le_total c d with hcd hcd; simp [hcd] at h,
{ exact Iio_union_Ico' h },
{ simp [*] },
end
lemma Iic_subset_Iic_union_Ioc : Iic b ⊆ Iic a ∪ Ioc a b :=
λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)
@[simp] lemma Iic_union_Ioc_eq_Iic (h : a ≤ b) : Iic a ∪ Ioc a b = Iic b :=
subset.antisymm (λ x hx, hx.elim (λ hx', le_trans hx' h) and.right) Iic_subset_Iic_union_Ioc
lemma Iic_union_Ioc' (h₁ : c < b) :
Iic b ∪ Ioc c d = Iic (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Iic, mem_Ioc, le_max_iff],
by_cases hc : c < x,
{ tauto },
{ have hxb : x ≤ b := (le_of_not_gt hc).trans h₁.le,
tauto },
end
lemma Iic_union_Ioc (h : min c d < b) :
Iic b ∪ Ioc c d = Iic (max b d) :=
begin
cases le_total c d with hcd hcd; simp [hcd] at h,
{ exact Iic_union_Ioc' h },
{ rw max_comm,
simp [*, max_eq_right_of_lt h] },
end
lemma Iio_subset_Iic_union_Ioo : Iio b ⊆ Iic a ∪ Ioo a b :=
λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)
@[simp] lemma Iic_union_Ioo_eq_Iio (h : a < b) : Iic a ∪ Ioo a b = Iio b :=
subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_le_of_lt hx' h) and.right) Iio_subset_Iic_union_Ioo
lemma Iio_union_Ioo' (h₁ : c < b) :
Iio b ∪ Ioo c d = Iio (max b d) :=
begin
ext x,
cases lt_or_le x b with hba hba,
{ simp [hba, h₁] },
{ simp only [mem_Iio, mem_union_eq, mem_Ioo, lt_max_iff],
refine or_congr iff.rfl ⟨and.right, _⟩,
exact λ h₂, ⟨h₁.trans_le hba, h₂⟩ },
end
lemma Iio_union_Ioo (h : min c d < b) :
Iio b ∪ Ioo c d = Iio (max b d) :=
begin
cases le_total c d with hcd hcd; simp [hcd] at h,
{ exact Iio_union_Ioo' h },
{ rw max_comm,
simp [*, max_eq_right_of_lt h] },
end
lemma Iic_subset_Iic_union_Icc : Iic b ⊆ Iic a ∪ Icc a b :=
subset.trans Iic_subset_Iic_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self)
@[simp] lemma Iic_union_Icc_eq_Iic (h : a ≤ b) : Iic a ∪ Icc a b = Iic b :=
subset.antisymm (λ x hx, hx.elim (λ hx', le_trans hx' h) and.right) Iic_subset_Iic_union_Icc
lemma Iic_union_Icc' (h₁ : c ≤ b) :
Iic b ∪ Icc c d = Iic (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Iic, mem_Icc, le_max_iff],
by_cases hc : c ≤ x,
{ tauto },
{ have hxb : x ≤ b := (le_of_not_ge hc).trans h₁,
tauto },
end
lemma Iic_union_Icc (h : min c d ≤ b) :
Iic b ∪ Icc c d = Iic (max b d) :=
begin
cases le_or_lt c d with hcd hcd; simp [hcd] at h,
{ exact Iic_union_Icc' h },
{ cases h,
{ have hdb : d ≤ b := hcd.le.trans h,
simp [*] },
{ simp [*] } },
end
lemma Iio_subset_Iic_union_Ico : Iio b ⊆ Iic a ∪ Ico a b :=
subset.trans Iio_subset_Iic_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self)
@[simp] lemma Iic_union_Ico_eq_Iio (h : a < b) : Iic a ∪ Ico a b = Iio b :=
subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_le_of_lt hx' h) and.right) Iio_subset_Iic_union_Ico
/-! #### Two finite intervals, `I?o` and `Ic?` -/
lemma Ioo_subset_Ioo_union_Ico : Ioo a c ⊆ Ioo a b ∪ Ico b c :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ioo_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Ico b c = Ioo a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_le h₂⟩) (λ hx, ⟨h₁.trans_le hx.1, hx.2⟩))
Ioo_subset_Ioo_union_Ico
lemma Ico_subset_Ico_union_Ico : Ico a c ⊆ Ico a b ∪ Ico b c :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ico_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Ico b c = Ico a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_le h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩))
Ico_subset_Ico_union_Ico
lemma Ico_union_Ico' (h₁ : c ≤ b) (h₂ : a ≤ d) :
Ico a b ∪ Ico c d = Ico (min a c) (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ico, min_le_iff, lt_max_iff],
by_cases hc : c ≤ x; by_cases hd : x < d,
{ tauto },
{ have hax : a ≤ x := h₂.trans (le_of_not_gt hd),
tauto },
{ have hxb : x < b := (lt_of_not_ge hc).trans_le h₁,
tauto },
{ tauto },
end
lemma Ico_union_Ico (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) :
Ico a b ∪ Ico c d = Ico (min a c) (max b d) :=
begin
cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp [hab, hcd] at h₁ h₂,
{ exact Ico_union_Ico' h₂ h₁ },
all_goals { simp [*] },
end
lemma Icc_subset_Ico_union_Icc : Icc a c ⊆ Ico a b ∪ Icc b c :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ico_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Icc b c = Icc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.le.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩))
Icc_subset_Ico_union_Icc
lemma Ioc_subset_Ioo_union_Icc : Ioc a c ⊆ Ioo a b ∪ Icc b c :=
λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ioo_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Icc b c = Ioc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.le.trans h₂⟩)
(λ hx, ⟨h₁.trans_le hx.1, hx.2⟩))
Ioc_subset_Ioo_union_Icc
/-! #### Two finite intervals, `I?c` and `Io?` -/
lemma Ioo_subset_Ioc_union_Ioo : Ioo a c ⊆ Ioc a b ∪ Ioo b c :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ioc_union_Ioo_eq_Ioo (h₁ : a ≤ b) (h₂ : b < c) : Ioc a b ∪ Ioo b c = Ioo a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩) (λ hx, ⟨h₁.trans_lt hx.1, hx.2⟩))
Ioo_subset_Ioc_union_Ioo
lemma Ico_subset_Icc_union_Ioo : Ico a c ⊆ Icc a b ∪ Ioo b c :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Icc_union_Ioo_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ioo b c = Ico a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩)
(λ hx, ⟨h₁.trans hx.1.le, hx.2⟩))
Ico_subset_Icc_union_Ioo
lemma Icc_subset_Icc_union_Ioc : Icc a c ⊆ Icc a b ∪ Ioc b c :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Icc_union_Ioc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Ioc b c = Icc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1.le, hx.2⟩))
Icc_subset_Icc_union_Ioc
lemma Ioc_subset_Ioc_union_Ioc : Ioc a c ⊆ Ioc a b ∪ Ioc b c :=
λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)
@[simp] lemma Ioc_union_Ioc_eq_Ioc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans_lt hx.1, hx.2⟩))
Ioc_subset_Ioc_union_Ioc
lemma Ioc_union_Ioc' (h₁ : c ≤ b) (h₂ : a ≤ d) :
Ioc a b ∪ Ioc c d = Ioc (min a c) (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ioc, min_lt_iff, le_max_iff],
by_cases hc : c < x; by_cases hd : x ≤ d,
{ tauto },
{ have hax : a < x := h₂.trans_lt (lt_of_not_ge hd),
tauto },
{ have hxb : x ≤ b := (le_of_not_gt hc).trans h₁,
tauto },
{ tauto },
end
lemma Ioc_union_Ioc (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) :
Ioc a b ∪ Ioc c d = Ioc (min a c) (max b d) :=
begin
cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp [hab, hcd] at h₁ h₂,
{ exact Ioc_union_Ioc' h₂ h₁ },
all_goals { simp [*] },
end
/-! #### Two finite intervals with a common point -/
lemma Ioo_subset_Ioc_union_Ico : Ioo a c ⊆ Ioc a b ∪ Ico b c :=
subset.trans Ioo_subset_Ioc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self)
@[simp] lemma Ioc_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b < c) : Ioc a b ∪ Ico b c = Ioo a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx', ⟨hx'.1, hx'.2.trans_lt h₂⟩) (λ hx', ⟨h₁.trans_le hx'.1, hx'.2⟩))
Ioo_subset_Ioc_union_Ico
lemma Ico_subset_Icc_union_Ico : Ico a c ⊆ Icc a b ∪ Ico b c :=
subset.trans Ico_subset_Icc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self)
@[simp] lemma Icc_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ico b c = Ico a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩))
Ico_subset_Icc_union_Ico
lemma Icc_subset_Icc_union_Icc : Icc a c ⊆ Icc a b ∪ Icc b c :=
subset.trans Icc_subset_Icc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self)
@[simp] lemma Icc_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Icc b c = Icc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩))
Icc_subset_Icc_union_Icc
lemma Icc_union_Icc' (h₁ : c ≤ b) (h₂ : a ≤ d) :
Icc a b ∪ Icc c d = Icc (min a c) (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Icc, min_le_iff, le_max_iff],
by_cases hc : c ≤ x; by_cases hd : x ≤ d,
{ tauto },
{ have hax : a ≤ x := h₂.trans (le_of_not_ge hd),
tauto },
{ have hxb : x ≤ b := (le_of_not_ge hc).trans h₁,
tauto },
{ tauto }
end
/--
We cannot replace `<` by `≤` in the hypotheses.
Otherwise for `b < a = d < c` the l.h.s. is `∅` and the r.h.s. is `{a}`.
-/
lemma Icc_union_Icc (h₁ : min a b < max c d) (h₂ : min c d < max a b) :
Icc a b ∪ Icc c d = Icc (min a c) (max b d) :=
begin
cases le_or_lt a b with hab hab; cases le_or_lt c d with hcd hcd;
simp only [min_eq_left, min_eq_right, max_eq_left, max_eq_right, min_eq_left_of_lt,
min_eq_right_of_lt, max_eq_left_of_lt, max_eq_right_of_lt, hab, hcd] at h₁ h₂,
{ exact Icc_union_Icc' h₂.le h₁.le },
all_goals { simp [*, min_eq_left_of_lt, max_eq_left_of_lt, min_eq_right_of_lt,
max_eq_right_of_lt] },
end
lemma Ioc_subset_Ioc_union_Icc : Ioc a c ⊆ Ioc a b ∪ Icc b c :=
subset.trans Ioc_subset_Ioc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self)
@[simp] lemma Ioc_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioc a b ∪ Icc b c = Ioc a c :=
subset.antisymm
(λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans_le hx.1, hx.2⟩))
Ioc_subset_Ioc_union_Icc
lemma Ioo_union_Ioo' (h₁ : c < b) (h₂ : a < d) :
Ioo a b ∪ Ioo c d = Ioo (min a c) (max b d) :=
begin
ext1 x,
simp_rw [mem_union, mem_Ioo, min_lt_iff, lt_max_iff],
by_cases hc : c < x; by_cases hd : x < d,
{ tauto },
{ have hax : a < x := h₂.trans_le (le_of_not_lt hd),
tauto },
{ have hxb : x < b := (le_of_not_lt hc).trans_lt h₁,
tauto },
{ tauto }
end
lemma Ioo_union_Ioo (h₁ : min a b < max c d) (h₂ : min c d < max a b) :
Ioo a b ∪ Ioo c d = Ioo (min a c) (max b d) :=
begin
cases le_total a b with hab hab; cases le_total c d with hcd hcd;
simp only [min_eq_left, min_eq_right, max_eq_left, max_eq_right, hab, hcd] at h₁ h₂,
{ exact Ioo_union_Ioo' h₂ h₁ },
all_goals
{ simp [*, min_eq_left_of_lt, min_eq_right_of_lt, max_eq_left_of_lt, max_eq_right_of_lt,
le_of_lt h₂, le_of_lt h₁] },
end
end linear_order
section lattice
section inf
variables [semilattice_inf α]
@[simp] lemma Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) :=
by { ext x, simp [Iic] }
@[simp] lemma Ioc_inter_Iic (a b c : α) : Ioc a b ∩ Iic c = Ioc a (b ⊓ c) :=
by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, inter_assoc, Iic_inter_Iic]
end inf
section sup
variables [semilattice_sup α]
@[simp] lemma Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) :=
by { ext x, simp [Ici] }
@[simp] lemma Ico_inter_Ici (a b c : α) : Ico a b ∩ Ici c = Ico (a ⊔ c) b :=
by rw [← Ici_inter_Iio, ← Ici_inter_Iio, ← Ici_inter_Ici, inter_right_comm]
end sup
section both
variables [lattice α] {a b c a₁ a₂ b₁ b₂ : α}
lemma Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) :=
by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_refl
@[simp] lemma Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) :
Icc a b ∩ Icc b c = {b} :=
by rw [Icc_inter_Icc, sup_of_le_right hab, inf_of_le_left hbc, Icc_self]
end both
end lattice
section linear_order
variables [linear_order α] {a a₁ a₂ b b₁ b₂ c d : α}
@[simp] lemma Ioi_inter_Ioi : Ioi a ∩ Ioi b = Ioi (a ⊔ b) := ext $ λ _, sup_lt_iff.symm
@[simp] lemma Iio_inter_Iio : Iio a ∩ Iio b = Iio (a ⊓ b) := ext $ λ _, lt_inf_iff.symm
lemma Ico_inter_Ico : Ico a₁ b₁ ∩ Ico a₂ b₂ = Ico (a₁ ⊔ a₂) (b₁ ⊓ b₂) :=
by simp only [Ici_inter_Iio.symm, Ici_inter_Ici.symm, Iio_inter_Iio.symm]; ac_refl
lemma Ioc_inter_Ioc : Ioc a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (a₁ ⊔ a₂) (b₁ ⊓ b₂) :=
by simp only [Ioi_inter_Iic.symm, Ioi_inter_Ioi.symm, Iic_inter_Iic.symm]; ac_refl
lemma Ioo_inter_Ioo : Ioo a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (a₁ ⊔ a₂) (b₁ ⊓ b₂) :=
by simp only [Ioi_inter_Iio.symm, Ioi_inter_Ioi.symm, Iio_inter_Iio.symm]; ac_refl
lemma Ioc_inter_Ioo_of_left_lt (h : b₁ < b₂) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioc (max a₁ a₂) b₁ :=
ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _),
and_iff_left_iff_imp.2 (λ h', lt_of_le_of_lt h' h)]
lemma Ioc_inter_Ioo_of_right_le (h : b₂ ≤ b₁) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (max a₁ a₂) b₂ :=
ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _),
and_iff_right_iff_imp.2 (λ h', ((le_of_lt h').trans h))]
lemma Ioo_inter_Ioc_of_left_le (h : b₁ ≤ b₂) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioo (max a₁ a₂) b₁ :=
by rw [inter_comm, Ioc_inter_Ioo_of_right_le h, max_comm]
lemma Ioo_inter_Ioc_of_right_lt (h : b₂ < b₁) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (max a₁ a₂) b₂ :=
by rw [inter_comm, Ioc_inter_Ioo_of_left_lt h, max_comm]
@[simp] lemma Ico_diff_Iio : Ico a b \ Iio c = Ico (max a c) b :=
by rw [diff_eq, compl_Iio, Ico_inter_Ici, sup_eq_max]
@[simp] lemma Ioc_diff_Ioi : Ioc a b \ Ioi c = Ioc a (min b c) :=
ext $ by simp [iff_def] {contextual:=tt}
@[simp] lemma Ioc_inter_Ioi : Ioc a b ∩ Ioi c = Ioc (a ⊔ c) b :=
by rw [← Ioi_inter_Iic, inter_assoc, inter_comm, inter_assoc, Ioi_inter_Ioi, inter_comm,
Ioi_inter_Iic, sup_comm]
@[simp] lemma Ico_inter_Iio : Ico a b ∩ Iio c = Ico a (min b c) :=
ext $ by simp [iff_def] {contextual:=tt}
@[simp] lemma Ioc_diff_Iic : Ioc a b \ Iic c = Ioc (max a c) b :=
by rw [diff_eq, compl_Iic, Ioc_inter_Ioi, sup_eq_max]
@[simp] lemma Ioc_union_Ioc_right : Ioc a b ∪ Ioc a c = Ioc a (max b c) :=
by rw [Ioc_union_Ioc, min_self]; exact (min_le_left _ _).trans (le_max_left _ _)
@[simp] lemma Ioc_union_Ioc_left : Ioc a c ∪ Ioc b c = Ioc (min a b) c :=
by rw [Ioc_union_Ioc, max_self]; exact (min_le_right _ _).trans (le_max_right _ _)
@[simp] lemma Ioc_union_Ioc_symm : Ioc a b ∪ Ioc b a = Ioc (min a b) (max a b) :=
by { rw max_comm, apply Ioc_union_Ioc; rw max_comm; exact min_le_max }
@[simp] lemma Ioc_union_Ioc_union_Ioc_cycle :
Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc (min a (min b c)) (max a (max b c)) :=
begin
rw [Ioc_union_Ioc, Ioc_union_Ioc],
ac_refl,
all_goals { solve_by_elim [min_le_of_left_le, min_le_of_right_le, le_max_of_le_left,
le_max_of_le_right, le_refl] { max_depth := 5 }}
end
end linear_order
/-!
### Closed intervals in `α × β`
-/
section prod
variables [preorder α] [preorder β]
@[simp] lemma Iic_prod_Iic (a : α) (b : β) : Iic a ×ˢ Iic b = Iic (a, b) := rfl
@[simp] lemma Ici_prod_Ici (a : α) (b : β) : Ici a ×ˢ Ici b = Ici (a, b) := rfl
lemma Ici_prod_eq (a : α × β) : Ici a = Ici a.1 ×ˢ Ici a.2 := rfl
lemma Iic_prod_eq (a : α × β) : Iic a = Iic a.1 ×ˢ Iic a.2 := rfl
@[simp] lemma Icc_prod_Icc (a₁ a₂ : α) (b₁ b₂ : β) :
Icc a₁ a₂ ×ˢ Icc b₁ b₂ = Icc (a₁, b₁) (a₂, b₂) :=
by { ext ⟨x, y⟩, simp [and.assoc, and_comm, and.left_comm] }
lemma Icc_prod_eq (a b : α × β) :
Icc a b = Icc a.1 b.1 ×ˢ Icc a.2 b.2 :=
by simp
end prod
/-! ### Lemmas about membership of arithmetic operations -/
section ordered_comm_group
variables [ordered_comm_group α] {a b c d : α}
/-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/
@[to_additive] lemma inv_mem_Icc_iff : a⁻¹ ∈ set.Icc c d ↔ a ∈ set.Icc (d⁻¹) (c⁻¹) :=
(and_comm _ _).trans $ and_congr inv_le' le_inv'
@[to_additive] lemma inv_mem_Ico_iff : a⁻¹ ∈ set.Ico c d ↔ a ∈ set.Ioc (d⁻¹) (c⁻¹) :=
(and_comm _ _).trans $ and_congr inv_lt' le_inv'
@[to_additive] lemma inv_mem_Ioc_iff : a⁻¹ ∈ set.Ioc c d ↔ a ∈ set.Ico (d⁻¹) (c⁻¹) :=
(and_comm _ _).trans $ and_congr inv_le' lt_inv'
@[to_additive] lemma inv_mem_Ioo_iff : a⁻¹ ∈ set.Ioo c d ↔ a ∈ set.Ioo (d⁻¹) (c⁻¹) :=
(and_comm _ _).trans $ and_congr inv_lt' lt_inv'
end ordered_comm_group
section ordered_add_comm_group
variables [ordered_add_comm_group α] {a b c d : α}
/-! `add_mem_Ixx_iff_left` -/
lemma add_mem_Icc_iff_left : a + b ∈ set.Icc c d ↔ a ∈ set.Icc (c - b) (d - b) :=
(and_congr sub_le_iff_le_add le_sub_iff_add_le).symm
lemma add_mem_Ico_iff_left : a + b ∈ set.Ico c d ↔ a ∈ set.Ico (c - b) (d - b) :=
(and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm
lemma add_mem_Ioc_iff_left : a + b ∈ set.Ioc c d ↔ a ∈ set.Ioc (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm
lemma add_mem_Ioo_iff_left : a + b ∈ set.Ioo c d ↔ a ∈ set.Ioo (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm
/-! `add_mem_Ixx_iff_right` -/
lemma add_mem_Icc_iff_right : a + b ∈ set.Icc c d ↔ b ∈ set.Icc (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm
lemma add_mem_Ico_iff_right : a + b ∈ set.Ico c d ↔ b ∈ set.Ico (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm
lemma add_mem_Ioc_iff_right : a + b ∈ set.Ioc c d ↔ b ∈ set.Ioc (c - a) (d - a) :=
(and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm
lemma add_mem_Ioo_iff_right : a + b ∈ set.Ioo c d ↔ b ∈ set.Ioo (c - a) (d - a) :=
(and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm
/-! `sub_mem_Ixx_iff_left` -/
lemma sub_mem_Icc_iff_left : a - b ∈ set.Icc c d ↔ a ∈ set.Icc (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_le_iff_le_add
lemma sub_mem_Ico_iff_left : a - b ∈ set.Ico c d ↔ a ∈ set.Ico (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_lt_iff_lt_add
lemma sub_mem_Ioc_iff_left : a - b ∈ set.Ioc c d ↔ a ∈ set.Ioc (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_le_iff_le_add
lemma sub_mem_Ioo_iff_left : a - b ∈ set.Ioo c d ↔ a ∈ set.Ioo (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add
/-! `sub_mem_Ixx_iff_right` -/
lemma sub_mem_Icc_iff_right : a - b ∈ set.Icc c d ↔ b ∈ set.Icc (a - d) (a - c) :=
(and_comm _ _).trans $ and_congr sub_le le_sub
lemma sub_mem_Ico_iff_right : a - b ∈ set.Ico c d ↔ b ∈ set.Ioc (a - d) (a - c) :=
(and_comm _ _).trans $ and_congr sub_lt le_sub
lemma sub_mem_Ioc_iff_right : a - b ∈ set.Ioc c d ↔ b ∈ set.Ico (a - d) (a - c) :=
(and_comm _ _).trans $ and_congr sub_le lt_sub
lemma sub_mem_Ioo_iff_right : a - b ∈ set.Ioo c d ↔ b ∈ set.Ioo (a - d) (a - c) :=
(and_comm _ _).trans $ and_congr sub_lt lt_sub
-- I think that symmetric intervals deserve attention and API: they arise all the time,
-- for instance when considering metric balls in `ℝ`.
lemma mem_Icc_iff_abs_le {R : Type*} [linear_ordered_add_comm_group R] {x y z : R} :
|x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) :=
abs_le.trans $ (and_comm _ _).trans $ and_congr sub_le neg_le_sub_iff_le_add
end ordered_add_comm_group
section linear_ordered_add_comm_group
variables [linear_ordered_add_comm_group α]
/-- If we remove a smaller interval from a larger, the result is nonempty -/
lemma nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) :
nonempty ↥(Ico x (x + dx) \ Ico y (y + dy)) :=
begin
cases lt_or_le x y with h' h',
{ use x, simp [*, not_le.2 h'] },
{ use max x (x + dy), simp [*, le_refl] }
end
end linear_ordered_add_comm_group
end set
open set
namespace order_iso
section preorder
variables [preorder α] [preorder β]
@[simp] lemma preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' (Iic b) = Iic (e.symm b) :=
by { ext x, simp [← e.le_iff_le] }
@[simp] lemma preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' (Ici b) = Ici (e.symm b) :=
by { ext x, simp [← e.le_iff_le] }
@[simp] lemma preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' (Iio b) = Iio (e.symm b) :=
by { ext x, simp [← e.lt_iff_lt] }
@[simp] lemma preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' (Ioi b) = Ioi (e.symm b) :=
by { ext x, simp [← e.lt_iff_lt] }
@[simp] lemma preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' (Icc a b) = Icc (e.symm a) (e.symm b) :=
by simp [← Ici_inter_Iic]
@[simp] lemma preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' (Ico a b) = Ico (e.symm a) (e.symm b) :=
by simp [← Ici_inter_Iio]
@[simp] lemma preimage_Ioc (e : α ≃o β) (a b : β) : e ⁻¹' (Ioc a b) = Ioc (e.symm a) (e.symm b) :=
by simp [← Ioi_inter_Iic]
@[simp] lemma preimage_Ioo (e : α ≃o β) (a b : β) : e ⁻¹' (Ioo a b) = Ioo (e.symm a) (e.symm b) :=
by simp [← Ioi_inter_Iio]
@[simp] lemma image_Iic (e : α ≃o β) (a : α) : e '' (Iic a) = Iic (e a) :=
by rw [e.image_eq_preimage, e.symm.preimage_Iic, e.symm_symm]
@[simp] lemma image_Ici (e : α ≃o β) (a : α) : e '' (Ici a) = Ici (e a) :=
e.dual.image_Iic a
@[simp] lemma image_Iio (e : α ≃o β) (a : α) : e '' (Iio a) = Iio (e a) :=
by rw [e.image_eq_preimage, e.symm.preimage_Iio, e.symm_symm]
@[simp] lemma image_Ioi (e : α ≃o β) (a : α) : e '' (Ioi a) = Ioi (e a) :=
e.dual.image_Iio a
@[simp] lemma image_Ioo (e : α ≃o β) (a b : α) : e '' (Ioo a b) = Ioo (e a) (e b) :=
by rw [e.image_eq_preimage, e.symm.preimage_Ioo, e.symm_symm]
@[simp] lemma image_Ioc (e : α ≃o β) (a b : α) : e '' (Ioc a b) = Ioc (e a) (e b) :=
by rw [e.image_eq_preimage, e.symm.preimage_Ioc, e.symm_symm]
@[simp] lemma image_Ico (e : α ≃o β) (a b : α) : e '' (Ico a b) = Ico (e a) (e b) :=
by rw [e.image_eq_preimage, e.symm.preimage_Ico, e.symm_symm]
@[simp] lemma image_Icc (e : α ≃o β) (a b : α) : e '' (Icc a b) = Icc (e a) (e b) :=
by rw [e.image_eq_preimage, e.symm.preimage_Icc, e.symm_symm]
end preorder
/-- Order isomorphism between `Iic (⊤ : α)` and `α` when `α` has a top element -/
def Iic_top [preorder α] [order_top α] : set.Iic (⊤ : α) ≃o α :=
{ map_rel_iff' := λ x y, by refl,
.. (@equiv.subtype_univ_equiv α (set.Iic (⊤ : α)) (λ x, le_top)), }
/-- Order isomorphism between `Ici (⊥ : α)` and `α` when `α` has a bottom element -/
def Ici_bot [preorder α] [order_bot α] : set.Ici (⊥ : α) ≃o α :=
{ map_rel_iff' := λ x y, by refl,
.. (@equiv.subtype_univ_equiv α (set.Ici (⊥ : α)) (λ x, bot_le)) }
end order_iso
/-! ### Lemmas about intervals in dense orders -/
section dense
variables (α) [preorder α] [densely_ordered α] {x y : α}
instance : no_min_order (set.Ioo x y) :=
⟨λ ⟨a, ha₁, ha₂⟩, begin
rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, hb₁, hb₂.trans ha₂⟩, hb₂⟩
end⟩
instance : no_min_order (set.Ioc x y) :=
⟨λ ⟨a, ha₁, ha₂⟩, begin
rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, hb₁, hb₂.le.trans ha₂⟩, hb₂⟩
end⟩
instance : no_min_order (set.Ioi x) :=
⟨λ ⟨a, ha⟩, begin
rcases exists_between ha with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, hb₁⟩, hb₂⟩
end⟩
instance : no_max_order (set.Ioo x y) :=
⟨λ ⟨a, ha₁, ha₂⟩, begin
rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, ha₁.trans hb₁, hb₂⟩, hb₁⟩
end⟩
instance : no_max_order (set.Ico x y) :=
⟨λ ⟨a, ha₁, ha₂⟩, begin
rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, ha₁.trans hb₁.le, hb₂⟩, hb₁⟩
end⟩
instance : no_max_order (set.Iio x) :=
⟨λ ⟨a, ha⟩, begin
rcases exists_between ha with ⟨b, hb₁, hb₂⟩,
exact ⟨⟨b, hb₂⟩, hb₁⟩
end⟩
end dense
|
b5d6d05ac9904806562844eb83cc62a9c2182a2f | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/ring_theory/polynomial/gauss_lemma.lean | 91cbbaa6d63532c34812e55d63bde38850e5200a | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,907 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import ring_theory.int.basic
import ring_theory.localization
/-!
# Gauss's Lemma
Gauss's Lemma is one of a few results pertaining to irreducibility of primitive polynomials.
## Main Results
- `polynomial.is_primitive.irreducible_iff_irreducible_map_fraction_map`:
A primitive polynomial is irreducible iff it is irreducible in a fraction field.
- `polynomial.is_primitive.int.irreducible_iff_irreducible_map_cast`:
A primitive polynomial over `ℤ` is irreducible iff it is irreducible over `ℚ`.
- `polynomial.is_primitive.dvd_iff_fraction_map_dvd_fraction_map`:
Two primitive polynomials divide each other iff they do in a fraction field.
- `polynomial.is_primitive.int.dvd_iff_map_cast_dvd_map_cast`:
Two primitive polynomials over `ℤ` divide each other if they do in `ℚ`.
-/
open_locale non_zero_divisors
variables {R : Type*} [integral_domain R]
namespace polynomial
section gcd_monoid
variable [gcd_monoid R]
section
variables {S : Type*} [integral_domain S] {φ : R →+* S} (hinj : function.injective φ)
variables {f : polynomial R} (hf : f.is_primitive)
include hinj hf
lemma is_primitive.is_unit_iff_is_unit_map_of_injective :
is_unit f ↔ is_unit (map φ f) :=
begin
refine ⟨(map_ring_hom φ).is_unit_map, λ h, _⟩,
rcases is_unit_iff.1 h with ⟨_, ⟨u, rfl⟩, hu⟩,
have hdeg := degree_C u.ne_zero,
rw [hu, degree_map' hinj] at hdeg,
rw [eq_C_of_degree_eq_zero hdeg, is_primitive_iff_content_eq_one,
content_C, normalize_eq_one] at hf,
rwa [eq_C_of_degree_eq_zero hdeg, is_unit_C],
end
lemma is_primitive.irreducible_of_irreducible_map_of_injective (h_irr : irreducible (map φ f)) :
irreducible f :=
begin
refine ⟨λ h, h_irr.not_unit (is_unit.map ((map_ring_hom φ).to_monoid_hom) h), _⟩,
intros a b h,
rcases h_irr.is_unit_or_is_unit (by rw [h, map_mul]) with hu | hu,
{ left,
rwa (hf.is_primitive_of_dvd (dvd.intro _ h.symm)).is_unit_iff_is_unit_map_of_injective hinj },
right,
rwa (hf.is_primitive_of_dvd (dvd.intro_left _ h.symm)).is_unit_iff_is_unit_map_of_injective hinj
end
end
section fraction_map
variables {K : Type*} [field K] [algebra R K] [is_fraction_ring R K]
lemma is_primitive.is_unit_iff_is_unit_map {p : polynomial R} (hp : p.is_primitive) :
is_unit p ↔ is_unit (p.map (algebra_map R K)) :=
hp.is_unit_iff_is_unit_map_of_injective (is_fraction_ring.injective _ _)
open is_localization
lemma is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part
{p : polynomial K} (h0 : p ≠ 0) (h : is_unit (integer_normalization R⁰ p).prim_part) :
is_unit p :=
begin
rcases is_unit_iff.1 h with ⟨_, ⟨u, rfl⟩, hu⟩,
obtain ⟨⟨c, c0⟩, hc⟩ := integer_normalization_map_to_map R⁰ p,
rw [subtype.coe_mk, algebra.smul_def, algebra_map_apply] at hc,
apply is_unit_of_mul_is_unit_right,
rw [← hc, (integer_normalization R⁰ p).eq_C_content_mul_prim_part, ← hu,
← ring_hom.map_mul, is_unit_iff],
refine ⟨algebra_map R K ((integer_normalization R⁰ p).content * ↑u),
is_unit_iff_ne_zero.2 (λ con, _), by simp⟩,
replace con := (algebra_map R K).injective_iff.1 (is_fraction_ring.injective _ _) _ con,
rw [mul_eq_zero, content_eq_zero_iff, is_fraction_ring.integer_normalization_eq_zero_iff] at con,
rcases con with con | con,
{ apply h0 con },
{ apply units.ne_zero _ con },
end
/-- **Gauss's Lemma** states that a primitive polynomial is irreducible iff it is irreducible in the
fraction field. -/
theorem is_primitive.irreducible_iff_irreducible_map_fraction_map
{p : polynomial R} (hp : p.is_primitive) :
irreducible p ↔ irreducible (p.map (algebra_map R K)) :=
begin
refine ⟨λ hi, ⟨λ h, hi.not_unit (hp.is_unit_iff_is_unit_map.2 h), λ a b hab, _⟩,
hp.irreducible_of_irreducible_map_of_injective (is_fraction_ring.injective _ _)⟩,
obtain ⟨⟨c, c0⟩, hc⟩ := integer_normalization_map_to_map R⁰ a,
obtain ⟨⟨d, d0⟩, hd⟩ := integer_normalization_map_to_map R⁰ b,
rw [algebra.smul_def, algebra_map_apply, subtype.coe_mk] at hc hd,
rw mem_non_zero_divisors_iff_ne_zero at c0 d0,
have hcd0 : c * d ≠ 0 := mul_ne_zero c0 d0,
rw [ne.def, ← C_eq_zero] at hcd0,
have h1 : C c * C d * p = integer_normalization R⁰ a * integer_normalization R⁰ b,
{ apply map_injective (algebra_map R K) (is_fraction_ring.injective _ _) _,
rw [map_mul, map_mul, map_mul, hc, hd, map_C, map_C, hab],
ring },
obtain ⟨u, hu⟩ : associated (c * d) (content (integer_normalization R⁰ a) *
content (integer_normalization R⁰ b)),
{ rw [← dvd_dvd_iff_associated, ← normalize_eq_normalize_iff, normalize.map_mul,
normalize.map_mul, normalize_content, normalize_content,
← mul_one (normalize c * normalize d), ← hp.content_eq_one, ← content_C, ← content_C,
← content_mul, ← content_mul, ← content_mul, h1] },
rw [← ring_hom.map_mul, eq_comm,
(integer_normalization R⁰ a).eq_C_content_mul_prim_part,
(integer_normalization R⁰ b).eq_C_content_mul_prim_part, mul_assoc,
mul_comm _ (C _ * _), ← mul_assoc, ← mul_assoc, ← ring_hom.map_mul, ← hu, ring_hom.map_mul,
mul_assoc, mul_assoc, ← mul_assoc (C ↑u)] at h1,
have h0 : (a ≠ 0) ∧ (b ≠ 0),
{ classical,
rw [ne.def, ne.def, ← decidable.not_or_iff_and_not, ← mul_eq_zero, ← hab],
intro con,
apply hp.ne_zero (map_injective (algebra_map R K) (is_fraction_ring.injective _ _) _),
simp [con] },
rcases hi.is_unit_or_is_unit (mul_left_cancel' hcd0 h1).symm with h | h,
{ right,
apply is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part h0.2
(is_unit_of_mul_is_unit_right h) },
{ left,
apply is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part h0.1 h },
end
lemma is_primitive.dvd_of_fraction_map_dvd_fraction_map {p q : polynomial R}
(hp : p.is_primitive) (hq : q.is_primitive)
(h_dvd : p.map (algebra_map R K) ∣ q.map (algebra_map R K)) : p ∣ q :=
begin
rcases h_dvd with ⟨r, hr⟩,
obtain ⟨⟨s, s0⟩, hs⟩ := integer_normalization_map_to_map R⁰ r,
rw [subtype.coe_mk, algebra.smul_def, algebra_map_apply] at hs,
have h : p ∣ q * C s,
{ use (integer_normalization R⁰ r),
apply map_injective (algebra_map R K) (is_fraction_ring.injective _ _),
rw [map_mul, map_mul, hs, hr, mul_assoc, mul_comm r],
simp },
rw [← hp.dvd_prim_part_iff_dvd, prim_part_mul, hq.prim_part_eq,
associated.dvd_iff_dvd_right] at h,
{ exact h },
{ symmetry,
rcases is_unit_prim_part_C s with ⟨u, hu⟩,
use u,
rw hu },
iterate 2 { apply mul_ne_zero hq.ne_zero,
rw [ne.def, C_eq_zero],
contrapose! s0,
simp [s0, mem_non_zero_divisors_iff_ne_zero] }
end
variables (K)
lemma is_primitive.dvd_iff_fraction_map_dvd_fraction_map {p q : polynomial R}
(hp : p.is_primitive) (hq : q.is_primitive) :
(p ∣ q) ↔ (p.map (algebra_map R K) ∣ q.map (algebra_map R K)) :=
⟨λ ⟨a,b⟩, ⟨a.map (algebra_map R K), b.symm ▸ map_mul (algebra_map R K)⟩,
λ h, hp.dvd_of_fraction_map_dvd_fraction_map hq h⟩
end fraction_map
/-- **Gauss's Lemma** for `ℤ` states that a primitive integer polynomial is irreducible iff it is
irreducible over `ℚ`. -/
theorem is_primitive.int.irreducible_iff_irreducible_map_cast
{p : polynomial ℤ} (hp : p.is_primitive) :
irreducible p ↔ irreducible (p.map (int.cast_ring_hom ℚ)) :=
hp.irreducible_iff_irreducible_map_fraction_map
lemma is_primitive.int.dvd_iff_map_cast_dvd_map_cast (p q : polynomial ℤ)
(hp : p.is_primitive) (hq : q.is_primitive) :
(p ∣ q) ↔ (p.map (int.cast_ring_hom ℚ) ∣ q.map (int.cast_ring_hom ℚ)) :=
hp.dvd_iff_fraction_map_dvd_fraction_map ℚ hq
end gcd_monoid
end polynomial
|
e0d0a288001670aea42186aa187bcae338f27319 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/CompilerSimp.lean | 6288c2bb730bcc0924cae03606942f20eeb426cf | [
"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 | 654 | lean | import Lean.Compiler.Main
import Lean.Compiler.LCNF.Testing
import Lean.Elab.Do
open Lean
open Lean.Compiler.LCNF
-- Run compilation twice to avoid the output caused by the inliner
#eval Compiler.compile #[``Lean.Meta.synthInstance, ``Lean.Elab.Term.Do.elabDo]
@[cpass]
def simpFixTest : PassInstaller := Testing.assertIsAtFixPoint |>.install `simp `simpFix
@[cpass]
def simpReaderTest : PassInstaller :=
Testing.assertDoesNotContainConstAfter `ReaderT.bind "simp did not inline ReaderT.bind" |>.install `simp `simpInlinesBinds
set_option trace.Compiler.test true in
#eval Compiler.compile #[``Lean.Meta.synthInstance, ``Lean.Elab.Term.Do.elabDo]
|
497e5fb25593501b91f1f79c991804f9312bcdc7 | 54d7e71c3616d331b2ec3845d31deb08f3ff1dea | /library/init/meta/smt/interactive.lean | e4f9c308807d2cca1b4fc059b446c9b4663ba94d | [
"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 | 10,257 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.smt.smt_tactic init.meta.interactive
import init.meta.smt.rsimp
namespace smt_tactic
meta def save_info (p : pos) : smt_tactic unit :=
do (ss, ts) ← smt_tactic.read,
tactic.save_info_thunk p (λ _, smt_state.to_format ss ts)
meta def skip : smt_tactic unit :=
return ()
meta def solve_goals : smt_tactic unit :=
repeat close
meta def step {α : Type} (tac : smt_tactic α) : smt_tactic unit :=
tac >> solve_goals
meta def istep {α : Type} (line : nat) (col : nat) (tac : smt_tactic α) : smt_tactic unit :=
λ ss ts, (@scope_trace _ line col ((tac >> solve_goals) ss ts)).clamp_pos line col
meta def execute (tac : smt_tactic unit) : tactic unit :=
using_smt tac
meta def execute_with (cfg : smt_config) (tac : smt_tactic unit) : tactic unit :=
using_smt tac cfg
namespace interactive
open lean.parser
open interactive
open interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many
meta def itactic : Type :=
smt_tactic unit
meta def intros : parse ident* → smt_tactic unit
| [] := smt_tactic.intros
| hs := smt_tactic.intro_lst hs
/--
Try to close main goal by using equalities implied by the congruence
closure module.
-/
meta def close : smt_tactic unit :=
smt_tactic.close
/--
Produce new facts using heuristic lemma instantiation based on E-matching.
This tactic tries to match patterns from lemmas in the main goal with terms
in the main goal. The set of lemmas is populated with theorems
tagged with the attribute specified at smt_config.em_attr, and lemmas
added using tactics such as `smt_tactic.add_lemmas`.
The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`.
-/
meta def ematch : smt_tactic unit :=
smt_tactic.ematch
meta def apply (q : parse texpr) : smt_tactic unit :=
tactic.interactive.apply q
meta def fapply (q : parse texpr) : smt_tactic unit :=
tactic.interactive.fapply q
meta def apply_instance : smt_tactic unit :=
tactic.apply_instance
meta def change (q : parse texpr) : smt_tactic unit :=
tactic.interactive.change q
meta def exact (q : parse texpr) : smt_tactic unit :=
tactic.interactive.exact q
meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : smt_tactic unit :=
do e ← tactic.to_expr_strict q,
smt_tactic.assert h e
meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : smt_tactic unit :=
do e ← tactic.to_expr_strict q,
smt_tactic.define h e
meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : smt_tactic unit :=
do t ← tactic.to_expr_strict q₁,
v ← tactic.to_expr_strict ``(%%q₂ : %%t),
smt_tactic.assertv h t v
meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : smt_tactic unit :=
do t ← tactic.to_expr_strict q₁,
v ← tactic.to_expr_strict ``(%%q₂ : %%t),
smt_tactic.definev h t v
meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.note h p
meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.pose h p
meta def add_fact (q : parse texpr) : smt_tactic unit :=
do h ← tactic.get_unused_name `h none,
p ← tactic.to_expr_strict q,
smt_tactic.note h p
meta def trace_state : smt_tactic unit :=
smt_tactic.trace_state
meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit :=
tactic.trace a
meta def destruct (q : parse texpr) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.destruct p
meta def by_cases (q : parse texpr) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.by_cases p
meta def by_contradiction : smt_tactic unit :=
smt_tactic.by_contradiction
meta def by_contra : smt_tactic unit :=
smt_tactic.by_contradiction
open tactic (resolve_name transparency to_expr)
private meta def report_invalid_em_lemma {α : Type} (n : name) : tactic α :=
fail ("invalid ematch lemma '" ++ to_string n ++ "'")
private meta def add_lemma_name (md : transparency) (lhs_lemma : bool) (n : name) (ref : expr) : smt_tactic unit :=
do
p ← resolve_name n,
match p.to_raw_expr with
| expr.const n _ := (add_ematch_lemma_from_decl_core md lhs_lemma n >> tactic.save_const_type_info n ref) <|> report_invalid_em_lemma n
| _ := (do e ← to_expr p, add_ematch_lemma_core md lhs_lemma e >> try (tactic.save_type_info e ref)) <|> report_invalid_em_lemma n
end
private meta def add_lemma_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) : smt_tactic unit :=
let e := pexpr.to_raw_expr p in
match e with
| (expr.const c []) := add_lemma_name md lhs_lemma c e
| (expr.local_const c _ _ _) := add_lemma_name md lhs_lemma c e
| _ := do new_e ← to_expr p, add_ematch_lemma_core md lhs_lemma new_e
end
private meta def add_lemma_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → smt_tactic unit
| [] := return ()
| (p::ps) := add_lemma_pexpr md lhs_lemma p >> add_lemma_pexprs ps
meta def add_lemma (l : parse qexpr_list_or_texpr) : smt_tactic unit :=
add_lemma_pexprs reducible ff l
meta def add_lhs_lemma (l : parse qexpr_list_or_texpr) : smt_tactic unit :=
add_lemma_pexprs reducible tt l
private meta def add_eqn_lemmas_for_core (md : transparency) : list name → smt_tactic unit
| [] := return ()
| (c::cs) := do
p ← resolve_name c,
match p.to_raw_expr with
| expr.const n _ := add_ematch_eqn_lemmas_for_core md n >> add_eqn_lemmas_for_core cs
| _ := fail $ "'" ++ to_string c ++ "' is not a constant"
end
meta def add_eqn_lemmas_for (ids : parse ident*) : smt_tactic unit :=
add_eqn_lemmas_for_core reducible ids
meta def add_eqn_lemmas (ids : parse ident*) : smt_tactic unit :=
add_eqn_lemmas_for ids
private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name) (hs : hinst_lemmas) (ref : expr) : smt_tactic hinst_lemmas :=
do
p ← resolve_name n,
match p.to_raw_expr with
| expr.const n _ :=
(do h ← hinst_lemma.mk_from_decl_core md n lhs_lemma, tactic.save_const_type_info n ref, return $ hs.add h)
<|>
(do hs₁ ← mk_ematch_eqn_lemmas_for_core md n, tactic.save_const_type_info n ref, return $ hs.merge hs₁)
<|>
report_invalid_em_lemma n
| _ :=
(do e ← to_expr p, h ← hinst_lemma.mk_core md e lhs_lemma, try (tactic.save_type_info e ref), return $ hs.add h)
<|>
report_invalid_em_lemma n
end
private meta def add_hinst_lemma_from_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas :=
let e := pexpr.to_raw_expr p in
match e with
| (expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs e
| (expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs e
| _ := do new_e ← to_expr p, h ← hinst_lemma.mk_core md new_e lhs_lemma, return $ hs.add h
end
private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → hinst_lemmas → smt_tactic hinst_lemmas
| [] hs := return hs
| (p::ps) hs := do hs₁ ← add_hinst_lemma_from_pexpr md lhs_lemma p hs, add_hinst_lemmas_from_pexprs ps hs₁
meta def ematch_using (l : parse qexpr_list_or_texpr) : smt_tactic unit :=
do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk,
smt_tactic.ematch_using hs
/-- Try the given tactic, and do nothing if it fails. -/
meta def try (t : itactic) : smt_tactic unit :=
smt_tactic.try t
/-- Keep applying the given tactic until it fails. -/
meta def repeat (t : itactic) : smt_tactic unit :=
smt_tactic.repeat t
/-- Apply the given tactic to all remaining goals. -/
meta def all_goals (t : itactic) : smt_tactic unit :=
smt_tactic.all_goals t
meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list)
(revert : parse $ (tk "generalizing" *> ident*)?) : smt_tactic unit :=
slift (tactic.interactive.induction p rec_name ids revert)
open tactic
/-- Simplify the target type of the main goal. -/
meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit :=
tactic.interactive.simp hs attr_names ids [] cfg
/-- Simplify the target type of the main goal using simplification lemmas and the current set of hypotheses. -/
meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit :=
tactic.interactive.simp_using_hs hs attr_names ids cfg
meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit :=
simp_using_hs hs attr_names ids cfg
meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : smt_tactic unit :=
tactic.interactive.dsimp es attr_names ids []
meta def rsimp : smt_tactic unit :=
do ccs ← to_cc_state, _root_.rsimp.rsimplify_goal ccs
meta def add_simp_lemmas : smt_tactic unit :=
get_hinst_lemmas_for_attr `rsimp_attr >>= add_lemmas
/-- Keep applying heuristic instantiation until the current goal is solved, or it fails. -/
meta def eblast : smt_tactic unit :=
smt_tactic.eblast
/-- Keep applying heuristic instantiation using the given lemmas until the current goal is solved, or it fails. -/
meta def eblast_using (l : parse qexpr_list_or_texpr) : smt_tactic unit :=
do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk,
smt_tactic.repeat (smt_tactic.ematch_using hs >> smt_tactic.try smt_tactic.close)
meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : smt_tactic unit :=
do e ← to_expr p, guard (expr.alpha_eqv t e)
meta def guard_target (p : parse texpr) : smt_tactic unit :=
do t ← target, guard_expr_eq t p
end interactive
end smt_tactic
|
01971cd6d6469cffd40b605948a4335e3aaa2b9b | 19cc34575500ee2e3d4586c15544632aa07a8e66 | /src/algebra/group_with_zero_power.lean | b22d53db579220a1409331449824760f9c0a5a26 | [
"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 | 8,382 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group_power
/-!
# Powers of elements of groups with an adjoined zero element
In this file we define integer power functions for groups with an adjoined zero element.
This generalises the integer power function on a division ring.
-/
@[simp] lemma zero_pow' {M : Type*} [monoid_with_zero M] :
∀ n : ℕ, n ≠ 0 → (0 : M) ^ n = 0
| 0 h := absurd rfl h
| (k+1) h := zero_mul _
@[simp] lemma zero_pow_eq_zero {M : Type*} [monoid_with_zero M] [nontrivial M] {n : ℕ} :
(0 : M) ^ n = 0 ↔ 0 < n :=
begin
split; intro h,
{ rw [nat.pos_iff_ne_zero], rintro rfl, simpa using h },
{ exact zero_pow' n h.ne.symm }
end
theorem pow_eq_zero' {M : Type*} [monoid_with_zero M] [no_zero_divisors M]
{a : M} {n : ℕ} (H : a ^ n = 0) : a = 0 :=
begin
induction n with n ih,
{ rw pow_zero at H,
rw [← mul_one a, H, mul_zero] },
exact or.cases_on (mul_eq_zero.1 H) id ih
end
@[field_simps] theorem pow_ne_zero' {M : Type*} [monoid_with_zero M] [no_zero_divisors M]
{a : M} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 :=
mt pow_eq_zero' h
section group_with_zero
variables {G₀ : Type*} [group_with_zero G₀]
section nat_pow
@[simp, field_simps] theorem inv_pow' (a : G₀) (n : ℕ) : (a⁻¹) ^ n = (a ^ n)⁻¹ :=
by induction n with n ih; [exact inv_one.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev']]
theorem pow_sub' (a : G₀) {m n : ℕ} (ha : a ≠ 0) (h : n ≤ m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ :=
have h1 : m - n + n = m, from nat.sub_add_cancel h,
have h2 : a ^ (m - n) * a ^ n = a ^ m, by rw [←pow_add, h1],
eq_div_of_mul_eq (pow_ne_zero' _ ha) h2
theorem pow_inv_comm' (a : G₀) (m n : ℕ) : (a⁻¹) ^ m * a ^ n = a ^ n * (a⁻¹) ^ m :=
(commute.refl a).inv_left'.pow_pow m n
end nat_pow
end group_with_zero
section int_pow
open int
variables {G₀ : Type*} [group_with_zero G₀]
/--
The power operation in a group with zero.
This extends `monoid.pow` to negative integers
with the definition `a ^ (-n) = (a ^ n)⁻¹`.
-/
def fpow (a : G₀) : ℤ → G₀
| (of_nat n) := a ^ n
| -[1+n] := (a ^ (nat.succ n))⁻¹
@[priority 10] instance : has_pow G₀ ℤ := ⟨fpow⟩
@[simp] theorem fpow_coe_nat (a : G₀) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl
theorem fpow_of_nat (a : G₀) (n : ℕ) : a ^ of_nat n = a ^ n := rfl
@[simp] theorem fpow_neg_succ_of_nat (a : G₀) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl
local attribute [ematch] le_of_lt
@[simp] theorem fpow_zero (a : G₀) : a ^ (0:ℤ) = 1 := rfl
@[simp] theorem fpow_one (a : G₀) : a ^ (1:ℤ) = a := mul_one _
@[simp] theorem one_fpow : ∀ (n : ℤ), (1 : G₀) ^ n = 1
| (n : ℕ) := one_pow _
| -[1+ n] := show _⁻¹=(1:G₀), by rw [one_pow, inv_one]
lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → (0 : G₀) ^ z = 0
| (of_nat n) h := zero_pow' _ $ by rintro rfl; exact h rfl
| -[1+n] h := show (0*0 ^ n)⁻¹ = (0 : G₀), by simp
@[simp] theorem fpow_neg (a : G₀) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹
| (n+1:ℕ) := rfl
| 0 := inv_one.symm
| -[1+ n] := (inv_inv' _).symm
theorem fpow_neg_one (x : G₀) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x
theorem inv_fpow (a : G₀) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹
| (n : ℕ) := inv_pow' a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow' a (n+1)
lemma fpow_add_one {a : G₀} (ha : a ≠ 0) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a
| (of_nat n) := by simp [← int.coe_nat_succ, pow_succ']
| -[1+0] := by simp [int.neg_succ_of_nat_eq, ha]
| -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, fpow_neg, neg_add, neg_add_cancel_right, fpow_neg,
← int.coe_nat_succ, fpow_coe_nat, fpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev', mul_assoc,
inv_mul_cancel ha, mul_one]
lemma fpow_sub_one {a : G₀} (ha : a ≠ 0) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ :=
calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : by rw [mul_assoc, mul_inv_cancel ha, mul_one]
... = a^n * a⁻¹ : by rw [← fpow_add_one ha, sub_add_cancel]
lemma fpow_add {a : G₀} (ha : a ≠ 0) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n :=
begin
induction n using int.induction_on with n ihn n ihn,
case hz : { simp },
{ simp only [← add_assoc, fpow_add_one ha, ihn, mul_assoc] },
{ rw [fpow_sub_one ha, ← mul_assoc, ← ihn, ← fpow_sub_one ha, add_sub_assoc] }
end
theorem fpow_one_add {a : G₀} (h : a ≠ 0) (i : ℤ) : a ^ (1 + i) = a * a ^ i :=
by rw [fpow_add h, fpow_one]
theorem semiconj_by.fpow_right {a x y : G₀} (h : semiconj_by a x y) :
∀ m : ℤ, semiconj_by a (x^m) (y^m)
| (n : ℕ) := h.pow_right n
| -[1+n] := (h.pow_right (n + 1)).inv_right'
theorem commute.fpow_right {a b : G₀} (h : commute a b) : ∀ m : ℤ, commute a (b^m) :=
h.fpow_right
theorem commute.fpow_left {a b : G₀} (h : commute a b) (m : ℤ) : commute (a^m) b :=
(h.symm.fpow_right m).symm
theorem commute.fpow_fpow {a b : G₀} (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) :=
(h.fpow_left m).fpow_right n
theorem commute.fpow_self (a : G₀) (n : ℤ) : commute (a^n) a := (commute.refl a).fpow_left n
theorem commute.self_fpow (a : G₀) (n : ℤ) : commute a (a^n) := (commute.refl a).fpow_right n
theorem commute.fpow_fpow_self (a : G₀) (m n : ℤ) : commute (a^m) (a^n) := (commute.refl a).fpow_fpow m n
theorem fpow_mul (a : G₀) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n
| (m : ℕ) (n : ℕ) := pow_mul _ _ _
| (m : ℕ) -[1+ n] := (fpow_neg _ (m * n.succ)).trans $
show (a ^ (m * n.succ))⁻¹ = _, by rw pow_mul; refl
| -[1+ m] (n : ℕ) := (fpow_neg _ (m.succ * n)).trans $
show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow']; refl
| -[1+ m] -[1+ n] := (pow_mul a m.succ n.succ).trans $
show _ = (_⁻¹ ^ _)⁻¹, by rw [inv_pow', inv_inv']
theorem fpow_mul' (a : G₀) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, fpow_mul]
@[simp, norm_cast] lemma units.coe_gpow' (u : units G₀) :
∀ (n : ℤ), ((u ^ n : units G₀) : G₀) = u ^ n
| (n : ℕ) := u.coe_pow n
| -[1+k] := by rw [gpow_neg_succ_of_nat, fpow_neg_succ_of_nat, units.coe_inv', u.coe_pow]
lemma fpow_ne_zero_of_ne_zero {a : G₀} (ha : a ≠ 0) : ∀ (z : ℤ), a ^ z ≠ 0
| (of_nat n) := pow_ne_zero' _ ha
| -[1+n] := inv_ne_zero $ pow_ne_zero' _ ha
lemma fpow_sub {a : G₀} (ha : a ≠ 0) (z1 z2 : ℤ) : a ^ (z1 - z2) = a ^ z1 / a ^ z2 :=
by rw [sub_eq_add_neg, fpow_add ha, fpow_neg]; refl
lemma commute.mul_fpow {a b : G₀} (h : commute a b) :
∀ (i : ℤ), (a * b) ^ i = (a ^ i) * (b ^ i)
| (n : ℕ) := h.mul_pow n
| -[1+n] := by simp [h.mul_pow, (h.pow_pow _ _).eq, mul_inv_rev']
lemma mul_fpow {G₀ : Type*} [comm_group_with_zero G₀] (a b : G₀) (m : ℤ):
(a * b) ^ m = (a ^ m) * (b ^ m) :=
(commute.all a b).mul_fpow m
lemma fpow_eq_zero {x : G₀} {n : ℤ} (h : x ^ n = 0) : x = 0 :=
classical.by_contradiction $ λ hx, fpow_ne_zero_of_ne_zero hx n h
lemma fpow_ne_zero {x : G₀} (n : ℤ) : x ≠ 0 → x ^ n ≠ 0 :=
mt fpow_eq_zero
theorem fpow_neg_mul_fpow_self (n : ℤ) {x : G₀} (h : x ≠ 0) :
x ^ (-n) * x ^ n = 1 :=
begin
rw [fpow_neg],
exact inv_mul_cancel (fpow_ne_zero n h)
end
theorem one_div_pow {a : G₀} (n : ℕ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_pow']
theorem one_div_fpow {a : G₀} (n : ℤ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_fpow]
end int_pow
section
variables {G₀ : Type*} [comm_group_with_zero G₀]
@[simp] theorem div_pow (a b : G₀) (n : ℕ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_pow, inv_pow']
@[simp] theorem div_fpow (a : G₀) {b : G₀} (n : ℤ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_fpow, inv_fpow]
lemma div_sq_cancel {a : G₀} (ha : a ≠ 0) (b : G₀) : a ^ 2 * b / a = a * b :=
by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
end
/-- If a monoid homomorphism `f` between two `group_with_zero`s maps `0` to `0`, then it maps `x^n`,
`n : ℤ`, to `(f x)^n`. -/
lemma monoid_hom.map_fpow {G₀ G₀' : Type*} [group_with_zero G₀] [group_with_zero G₀']
(f : G₀ →* G₀') (h0 : f 0 = 0) (x : G₀) :
∀ n : ℤ, f (x ^ n) = f x ^ n
| (n : ℕ) := f.map_pow x n
| -[1+n] := (f.map_inv' h0 _).trans $ congr_arg _ $ f.map_pow x _
|
b99985df3f916dd42f357971bdec7964ee35d1aa | 96338d06deb5f54f351493a71d6ecf6c546089a2 | /priv/Lean/BiCat.lean | 8ab7d81e8ada00364365fb70f9fa03f4dcbd7e27 | [] | no_license | silky/exe | 5f9e4eea772d74852a1a2fac57d8d20588282d2b | e81690d6e16f2a83c105cce446011af6ae905b81 | refs/heads/master | 1,609,385,766,412 | 1,472,164,223,000 | 1,472,164,223,000 | 66,610,224 | 1 | 0 | null | 1,472,178,919,000 | 1,472,178,919,000 | null | UTF-8 | Lean | false | false | 1,900 | lean | /- BiCat.lean -/
import Setoid
import Cat
import Functor
namespace EXE
namespace Bicat
-- carrier of a bicategory: type of morphisms
abbreviation HomType (Ob : Type) : Type := Π(Dom Cod : Ob), CatType
-- print HomType
-- structure of a bicategory (operations of degree 0)
section withHom
variables {Ob : Type} (Hom : HomType Ob)
abbreviation IdType : Type :=
Π{a : Ob}, Hom a a
abbreviation MulType : Type :=
Π{a b c : Ob}, Hom b c ⟶ Hom a b ⟶ Hom a c
end withHom
-- extra structure of bicategory (operations of degree 1)
section withIdMul
variables {Ob : Type} {Hom : HomType Ob}
variables (Id : IdType Hom) (Mul : MulType Hom)
abbreviation UnitLMor : Type :=
Π{a b : Ob}, Π(f : Hom a b), (Mul $$ Id $$ f) ⇒(Hom a b)⇒ f
abbreviation UnitRMor : Type :=
Π{a b : Ob}, Π(f : Hom a b), (Mul $$ f $$ Id) ⇒(Hom a b)⇒ f
abbreviation UnitCMor : Type :=
Π{a : Ob}, (Mul $$ Id $$ Id) ⇒(Hom a a)⇒ (@Id a)
abbreviation UnitLInvMor : Type :=
Π{a b : Ob}, Π(f : Hom a b), f ⇒(Hom a b)⇒ (Mul $$ Id $$ f)
abbreviation UnitRInvMor : Type :=
Π{a b : Ob}, Π(f : Hom a b), f ⇒(Hom a b)⇒ (Mul $$ f $$ Id)
abbreviation UnitCInvMor : Type :=
Π{a : Ob}, (@Id a) ⇒(Hom a a)⇒ (Mul $$ Id $$ Id)
abbreviation AssocMor : Type :=
Π{a b c d : Ob}, Π(f : Hom c d), Π(g : Hom b c), Π(h : Hom a b),
(Mul $$ (Mul $$ f $$ g) $$ h) ⇒(Hom a d)⇒ (Mul $$ f $$ (Mul $$ g $$ h))
abbreviation AssocInvMor : Type :=
∀{a b c d : Ob}, Π(f : Hom c d), Π(g : Hom b c), Π(h : Hom a b),
(Mul $$ f $$ (Mul $$ g $$ h)) ⇒(Hom a d)⇒ (Mul $$ (Mul $$ f $$ g) $$ h)
end withIdMul
end Bicat
end EXE
|
e37a8cc0d3c8d989384b5222713a929abd949bcb | 367134ba5a65885e863bdc4507601606690974c1 | /src/category_theory/sites/canonical.lean | d9911a10fdc5d7fe022fe1648c690d7bd7c04299 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 9,654 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.sites.sheaf_of_types
/-!
# The canonical topology on a category
We define the finest (largest) Grothendieck topology for which a given presheaf `P` is a sheaf.
This is well defined since if `P` is a sheaf for a topology `J`, then it is a sheaf for any
coarser (smaller) topology. Nonetheless we define the topology explicitly by specifying its sieves:
A sieve `S` on `X` is covering for `finest_topology_single P` iff
for any `f : Y ⟶ X`, `P` satisfies the sheaf axiom for `S.pullback f`.
Showing that this is a genuine Grothendieck topology (namely that it satisfies the transitivity
axiom) forms the bulk of this file.
This generalises to a set of presheaves, giving the topology `finest_topology Ps` which is the
finest topology for which every presheaf in `Ps` is a sheaf.
Using `Ps` as the set of representable presheaves defines the `canonical_topology`: the finest
topology for which every representable is a sheaf.
A Grothendieck topology is called `subcanonical` if it is smaller than the canonical topology,
equivalently it is subcanonical iff every representable presheaf is a sheaf.
## References
* https://ncatlab.org/nlab/show/canonical+topology
* https://ncatlab.org/nlab/show/subcanonical+coverage
* https://stacks.math.columbia.edu/tag/00Z9
* https://math.stackexchange.com/a/358709/
-/
universes v u
namespace category_theory
open category_theory category limits sieve classical
variables {C : Type u} [category.{v} C]
namespace sheaf
variables {P : Cᵒᵖ ⥤ Type v}
variables {X Y : C} {S : sieve X} {R : presieve X}
variables (J J₂ : grothendieck_topology C)
/--
To show `P` is a sheaf for the binding of `U` with `B`, it suffices to show that `P` is a sheaf for
`U`, that `P` is a sheaf for each sieve in `B`, and that it is separated for any pullback of any
sieve in `B`.
This is mostly an auxiliary lemma to show `is_sheaf_for_trans`.
Adapted from [Elephant], Lemma C2.1.7(i) with suggestions as mentioned in
https://math.stackexchange.com/a/358709/
-/
lemma is_sheaf_for_bind (P : Cᵒᵖ ⥤ Type v) (U : sieve X)
(B : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, U f → sieve Y)
(hU : presieve.is_sheaf_for P U)
(hB : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : U f), presieve.is_sheaf_for P (B hf))
(hB' : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (h : U f) ⦃Z⦄ (g : Z ⟶ Y),
presieve.is_separated_for P ((B h).pullback g)) :
presieve.is_sheaf_for P (sieve.bind U B) :=
begin
intros s hs,
let y : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : U f), presieve.family_of_elements P (B hf) :=
λ Y f hf Z g hg, s _ (presieve.bind_comp _ _ hg),
have hy : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : U f), (y hf).compatible,
{ intros Y f H Y₁ Y₂ Z g₁ g₂ f₁ f₂ hf₁ hf₂ comm,
apply hs,
apply reassoc_of comm },
let t : presieve.family_of_elements P U := λ Y f hf, (hB hf).amalgamate (y hf) (hy hf),
have ht : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : U f), (y hf).is_amalgamation (t f hf) :=
λ Y f hf, (hB hf).is_amalgamation _,
have hT : t.compatible,
{ rw presieve.compatible_iff_sieve_compatible,
intros Z W f h hf,
apply (hB (U.downward_closed hf h)).is_separated_for.ext,
intros Y l hl,
apply (hB' hf (l ≫ h)).ext,
intros M m hm,
have : bind U B (m ≫ l ≫ h ≫ f),
{ have : bind U B _ := presieve.bind_comp f hf hm,
simpa using this },
transitivity s (m ≫ l ≫ h ≫ f) this,
{ have := ht (U.downward_closed hf h) _ ((B _).downward_closed hl m),
rw [op_comp, functor_to_types.map_comp_apply] at this,
rw this,
change s _ _ = s _ _,
simp },
{ have : s _ _ = _ := (ht hf _ hm).symm,
simp only [assoc] at this,
rw this,
simp } },
refine ⟨hU.amalgamate t hT, _, _⟩,
{ rintro Z _ ⟨Y, f, g, hg, hf, rfl⟩,
rw [op_comp, functor_to_types.map_comp_apply, presieve.is_sheaf_for.valid_glue _ _ _ hg],
apply ht hg _ hf },
{ intros y hy,
apply hU.is_separated_for.ext,
intros Y f hf,
apply (hB hf).is_separated_for.ext,
intros Z g hg,
rw [←functor_to_types.map_comp_apply, ←op_comp, hy _ (presieve.bind_comp _ _ hg),
hU.valid_glue _ _ hf, ht hf _ hg] }
end
/--
Given two sieves `R` and `S`, to show that `P` is a sheaf for `S`, we can show:
* `P` is a sheaf for `R`
* `P` is a sheaf for the pullback of `S` along any arrow in `R`
* `P` is separated for the pullback of `R` along any arrow in `S`.
This is mostly an auxiliary lemma to construct `finest_topology`.
Adapted from [Elephant], Lemma C2.1.7(ii) with suggestions as mentioned in
https://math.stackexchange.com/a/358709
-/
lemma is_sheaf_for_trans (P : Cᵒᵖ ⥤ Type v) (R S : sieve X)
(hR : presieve.is_sheaf_for P R)
(hR' : ∀ ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : S f), presieve.is_separated_for P (R.pullback f))
(hS : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄ (hf : R f), presieve.is_sheaf_for P (S.pullback f)) :
presieve.is_sheaf_for P S :=
begin
have : (bind R (λ Y f hf, S.pullback f) : presieve X) ≤ S,
{ rintros Z f ⟨W, f, g, hg, (hf : S _), rfl⟩,
apply hf },
apply presieve.is_sheaf_for_subsieve_aux P this,
apply is_sheaf_for_bind _ _ _ hR hS,
{ intros Y f hf Z g,
dsimp,
rw ← pullback_comp,
apply (hS (R.downward_closed hf _)).is_separated_for },
{ intros Y f hf,
have : (sieve.pullback f (bind R (λ T (k : T ⟶ X) (hf : R k), pullback k S))) = R.pullback f,
{ ext Z g,
split,
{ rintro ⟨W, k, l, hl, _, comm⟩,
rw [pullback_apply, ← comm],
simp [hl] },
{ intro a,
refine ⟨Z, 𝟙 Z, _, a, _⟩,
simp [hf] } },
rw this,
apply hR' hf },
end
/--
Construct the finest (largest) Grothendieck topology for which the given presheaf is a sheaf.
This is a special case of https://stacks.math.columbia.edu/tag/00Z9, but following a different
proof (see the comments there).
-/
def finest_topology_single (P : Cᵒᵖ ⥤ Type v) : grothendieck_topology C :=
{ sieves := λ X S, ∀ Y (f : Y ⟶ X), presieve.is_sheaf_for P (S.pullback f),
top_mem' := λ X Y f,
begin
rw sieve.pullback_top,
exact presieve.is_sheaf_for_top_sieve P,
end,
pullback_stable' := λ X Y S f hS Z g,
begin
rw ← pullback_comp,
apply hS,
end,
transitive' := λ X S hS R hR Z g,
begin
-- This is the hard part of the construction, showing that the given set of sieves satisfies
-- the transitivity axiom.
refine is_sheaf_for_trans P (pullback g S) _ (hS Z g) _ _,
{ intros Y f hf,
rw ← pullback_comp,
apply (hS _ _).is_separated_for },
{ intros Y f hf,
have := hR hf _ (𝟙 _),
rw [pullback_id, pullback_comp] at this,
apply this },
end }
/--
Construct the finest (largest) Grothendieck topology for which all the given presheaves are sheaves.
This is equal to the construction of https://stacks.math.columbia.edu/tag/00Z9.
-/
def finest_topology (Ps : set (Cᵒᵖ ⥤ Type v)) : grothendieck_topology C :=
Inf (finest_topology_single '' Ps)
/-- Check that if `P ∈ Ps`, then `P` is indeed a sheaf for the finest topology on `Ps`. -/
lemma sheaf_for_finest_topology (Ps : set (Cᵒᵖ ⥤ Type v)) (h : P ∈ Ps) :
presieve.is_sheaf (finest_topology Ps) P :=
λ X S hS, by simpa using hS _ ⟨⟨_, _, ⟨_, h, rfl⟩, rfl⟩, rfl⟩ _ (𝟙 _)
/--
Check that if each `P ∈ Ps` is a sheaf for `J`, then `J` is a subtopology of `finest_topology Ps`.
-/
lemma le_finest_topology (Ps : set (Cᵒᵖ ⥤ Type v)) (J : grothendieck_topology C)
(hJ : ∀ P ∈ Ps, presieve.is_sheaf J P) : J ≤ finest_topology Ps :=
begin
rintro X S hS _ ⟨⟨_, _, ⟨P, hP, rfl⟩, rfl⟩, rfl⟩,
intros Y f, -- this can't be combined with the previous because the `subst` is applied at the end
exact hJ P hP (S.pullback f) (J.pullback_stable f hS),
end
/--
The `canonical_topology` on a category is the finest (largest) topology for which every
representable presheaf is a sheaf.
See https://stacks.math.columbia.edu/tag/00ZA
-/
def canonical_topology (C : Type u) [category.{v} C] : grothendieck_topology C :=
finest_topology (set.range yoneda.obj)
/-- `yoneda.obj X` is a sheaf for the canonical topology. -/
lemma is_sheaf_yoneda_obj (X : C) : presieve.is_sheaf (canonical_topology C) (yoneda.obj X) :=
λ Y S hS, sheaf_for_finest_topology _ (set.mem_range_self _) _ hS
/-- A representable functor is a sheaf for the canonical topology. -/
lemma is_sheaf_of_representable (P : Cᵒᵖ ⥤ Type v) [representable P] :
presieve.is_sheaf (canonical_topology C) P :=
presieve.is_sheaf_iso (canonical_topology C) representable.w (is_sheaf_yoneda_obj _)
/--
A subcanonical topology is a topology which is smaller than the canonical topology.
Equivalently, a topology is subcanonical iff every representable is a sheaf.
-/
def subcanonical (J : grothendieck_topology C) : Prop :=
J ≤ canonical_topology C
namespace subcanonical
/-- If every functor `yoneda.obj X` is a `J`-sheaf, then `J` is subcanonical. -/
lemma of_yoneda_is_sheaf (J : grothendieck_topology C)
(h : ∀ X, presieve.is_sheaf J (yoneda.obj X)) :
subcanonical J :=
le_finest_topology _ _ (by { rintro P ⟨X, rfl⟩, apply h })
/-- If `J` is subcanonical, then any representable is a `J`-sheaf. -/
lemma is_sheaf_of_representable {J : grothendieck_topology C} (hJ : subcanonical J)
(P : Cᵒᵖ ⥤ Type v) [representable P] :
presieve.is_sheaf J P :=
presieve.is_sheaf_of_le _ hJ (is_sheaf_of_representable P)
end subcanonical
end sheaf
end category_theory
|
f50e7b8f023865c8ea58a1185c6bea3ca33149b9 | 83c8119e3298c0bfc53fc195c41a6afb63d01513 | /library/init/algebra/group.lean | f78268c2f75dbd9a2903c0e0b753a5e4bcd1329f | [
"Apache-2.0"
] | permissive | anfelor/lean | 584b91c4e87a6d95f7630c2a93fb082a87319ed0 | 31cfc2b6bf7d674f3d0f73848b842c9c9869c9f1 | refs/heads/master | 1,610,067,141,310 | 1,585,992,232,000 | 1,585,992,232,000 | 251,683,543 | 0 | 0 | Apache-2.0 | 1,585,676,570,000 | 1,585,676,569,000 | null | UTF-8 | Lean | false | false | 16,395 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
prelude
import init.logic init.algebra.classes init.meta init.meta.decl_cmds init.meta.smt.rsimp
/- Make sure instances defined in this file have lower priority than the ones
defined for concrete structures -/
set_option default_priority 100
set_option old_structure_cmd true
universe u
variables {α : Type u}
class semigroup (α : Type u) extends has_mul α :=
(mul_assoc : ∀ a b c : α, a * b * c = a * (b * c))
class comm_semigroup (α : Type u) extends semigroup α :=
(mul_comm : ∀ a b : α, a * b = b * a)
class left_cancel_semigroup (α : Type u) extends semigroup α :=
(mul_left_cancel : ∀ a b c : α, a * b = a * c → b = c)
class right_cancel_semigroup (α : Type u) extends semigroup α :=
(mul_right_cancel : ∀ a b c : α, a * b = c * b → a = c)
class monoid (α : Type u) extends semigroup α, has_one α :=
(one_mul : ∀ a : α, 1 * a = a) (mul_one : ∀ a : α, a * 1 = a)
class comm_monoid (α : Type u) extends monoid α, comm_semigroup α
class group (α : Type u) extends monoid α, has_inv α :=
(mul_left_inv : ∀ a : α, a⁻¹ * a = 1)
class comm_group (α : Type u) extends group α, comm_monoid α
lemma mul_assoc [semigroup α] : ∀ a b c : α, a * b * c = a * (b * c) :=
semigroup.mul_assoc
instance semigroup_to_is_associative [semigroup α] : is_associative α (*) :=
⟨mul_assoc⟩
lemma mul_comm [comm_semigroup α] : ∀ a b : α, a * b = b * a :=
comm_semigroup.mul_comm
instance comm_semigroup_to_is_commutative [comm_semigroup α] : is_commutative α (*) :=
⟨mul_comm⟩
lemma mul_left_comm [comm_semigroup α] : ∀ a b c : α, a * (b * c) = b * (a * c) :=
left_comm has_mul.mul mul_comm mul_assoc
local attribute [simp] mul_assoc
lemma mul_right_comm [comm_semigroup α] : ∀ a b c : α, a * b * c = a * c * b :=
right_comm has_mul.mul mul_comm mul_assoc
lemma mul_left_cancel [left_cancel_semigroup α] {a b c : α} : a * b = a * c → b = c :=
left_cancel_semigroup.mul_left_cancel a b c
lemma mul_right_cancel [right_cancel_semigroup α] {a b c : α} : a * b = c * b → a = c :=
right_cancel_semigroup.mul_right_cancel a b c
lemma mul_left_cancel_iff [left_cancel_semigroup α] {a b c : α} : a * b = a * c ↔ b = c :=
⟨mul_left_cancel, congr_arg _⟩
lemma mul_right_cancel_iff [right_cancel_semigroup α] {a b c : α} : b * a = c * a ↔ b = c :=
⟨mul_right_cancel, congr_arg _⟩
@[simp] lemma one_mul [monoid α] : ∀ a : α, 1 * a = a :=
monoid.one_mul
@[simp] lemma mul_one [monoid α] : ∀ a : α, a * 1 = a :=
monoid.mul_one
@[simp] lemma mul_left_inv [group α] : ∀ a : α, a⁻¹ * a = 1 :=
group.mul_left_inv
def inv_mul_self := @mul_left_inv
@[simp] lemma inv_mul_cancel_left [group α] (a b : α) : a⁻¹ * (a * b) = b :=
by rw [← mul_assoc, mul_left_inv, one_mul]
@[simp] lemma inv_mul_cancel_right [group α] (a b : α) : a * b⁻¹ * b = a :=
by simp
@[simp] lemma inv_eq_of_mul_eq_one [group α] {a b : α} (h : a * b = 1) : a⁻¹ = b :=
by rw [← mul_one a⁻¹, ←h, ←mul_assoc, mul_left_inv, one_mul]
@[simp] lemma one_inv [group α] : 1⁻¹ = (1 : α) :=
inv_eq_of_mul_eq_one (one_mul 1)
@[simp] lemma inv_inv [group α] (a : α) : (a⁻¹)⁻¹ = a :=
inv_eq_of_mul_eq_one (mul_left_inv a)
@[simp] lemma mul_right_inv [group α] (a : α) : a * a⁻¹ = 1 :=
have a⁻¹⁻¹ * a⁻¹ = 1, by rw mul_left_inv,
by rwa [inv_inv] at this
def mul_inv_self := @mul_right_inv
lemma inv_inj [group α] {a b : α} (h : a⁻¹ = b⁻¹) : a = b :=
have a = a⁻¹⁻¹, by simp,
begin rw this, simp [h] end
lemma group.mul_left_cancel [group α] {a b c : α} (h : a * b = a * c) : b = c :=
have a⁻¹ * (a * b) = b, by simp,
begin simp [h] at this, rw this end
lemma group.mul_right_cancel [group α] {a b c : α} (h : a * b = c * b) : a = c :=
have a * b * b⁻¹ = a, by simp,
begin simp [h] at this, rw this end
instance group.to_left_cancel_semigroup [s : group α] : left_cancel_semigroup α :=
{ mul_left_cancel := @group.mul_left_cancel α s, ..s }
instance group.to_right_cancel_semigroup [s : group α] : right_cancel_semigroup α :=
{ mul_right_cancel := @group.mul_right_cancel α s, ..s }
lemma mul_inv_cancel_left [group α] (a b : α) : a * (a⁻¹ * b) = b :=
by rw [← mul_assoc, mul_right_inv, one_mul]
lemma mul_inv_cancel_right [group α] (a b : α) : a * b * b⁻¹ = a :=
by rw [mul_assoc, mul_right_inv, mul_one]
@[simp] lemma mul_inv_rev [group α] (a b : α) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one begin rw [mul_assoc, ← mul_assoc b, mul_right_inv, one_mul, mul_right_inv] end
lemma eq_inv_of_eq_inv [group α] {a b : α} (h : a = b⁻¹) : b = a⁻¹ :=
by simp [h]
lemma eq_inv_of_mul_eq_one [group α] {a b : α} (h : a * b = 1) : a = b⁻¹ :=
have a⁻¹ = b, from inv_eq_of_mul_eq_one h,
by simp [this.symm]
lemma eq_mul_inv_of_mul_eq [group α] {a b c : α} (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm]
lemma eq_inv_mul_of_mul_eq [group α] {a b c : α} (h : b * a = c) : a = b⁻¹ * c :=
by simp [h.symm]
lemma inv_mul_eq_of_eq_mul [group α] {a b c : α} (h : b = a * c) : a⁻¹ * b = c :=
by simp [h]
lemma mul_inv_eq_of_eq_mul [group α] {a b c : α} (h : a = c * b) : a * b⁻¹ = c :=
by simp [h]
lemma eq_mul_of_mul_inv_eq [group α] {a b c : α} (h : a * c⁻¹ = b) : a = b * c :=
by simp [h.symm]
lemma eq_mul_of_inv_mul_eq [group α] {a b c : α} (h : b⁻¹ * a = c) : a = b * c :=
by simp [h.symm, mul_inv_cancel_left]
lemma mul_eq_of_eq_inv_mul [group α] {a b c : α} (h : b = a⁻¹ * c) : a * b = c :=
by rw [h, mul_inv_cancel_left]
lemma mul_eq_of_eq_mul_inv [group α] {a b c : α} (h : a = c * b⁻¹) : a * b = c :=
by simp [h]
lemma mul_inv [comm_group α] (a b : α) : (a * b)⁻¹ = a⁻¹ * b⁻¹ :=
by rw [mul_inv_rev, mul_comm]
/- αdditive "sister" structures.
Example, add_semigroup mirrors semigroup.
These structures exist just to help automation.
In an alternative design, we could have the binary operation as an
extra argument for semigroup, monoid, group, etc. However, the lemmas
would be hard to index since they would not contain any constant.
For example, mul_assoc would be
lemma mul_assoc {α : Type u} {op : α → α → α} [semigroup α op] :
∀ a b c : α, op (op a b) c = op a (op b c) :=
semigroup.mul_assoc
The simplifier cannot effectively use this lemma since the pattern for
the left-hand-side would be
?op (?op ?a ?b) ?c
Remark: we use a tactic for transporting theorems from the multiplicative fragment
to the additive one.
-/
class add_semigroup (α : Type u) extends has_add α :=
(add_assoc : ∀ a b c : α, a + b + c = a + (b + c))
class add_comm_semigroup (α : Type u) extends add_semigroup α :=
(add_comm : ∀ a b : α, a + b = b + a)
class add_left_cancel_semigroup (α : Type u) extends add_semigroup α :=
(add_left_cancel : ∀ a b c : α, a + b = a + c → b = c)
class add_right_cancel_semigroup (α : Type u) extends add_semigroup α :=
(add_right_cancel : ∀ a b c : α, a + b = c + b → a = c)
class add_monoid (α : Type u) extends add_semigroup α, has_zero α :=
(zero_add : ∀ a : α, 0 + a = a) (add_zero : ∀ a : α, a + 0 = a)
class add_comm_monoid (α : Type u) extends add_monoid α, add_comm_semigroup α
class add_group (α : Type u) extends add_monoid α, has_neg α :=
(add_left_neg : ∀ a : α, -a + a = 0)
class add_comm_group (α : Type u) extends add_group α, add_comm_monoid α
open tactic
meta def transport_with_dict (dict : name_map name) (src : name) (tgt : name) : command :=
copy_decl_using dict src tgt
>> copy_attribute `reducible src tgt tt
>> copy_attribute `simp src tgt tt
>> copy_attribute `instance src tgt tt
/- Transport multiplicative to additive -/
meta def transport_multiplicative_to_additive (ls : list (name × name)) : command :=
let dict := native.rb_map.of_list ls in
ls.foldl (λ t ⟨src, tgt⟩, do
env ← get_env,
if (env.get tgt).to_bool = ff
then t >> transport_with_dict dict src tgt
else t)
skip
run_cmd transport_multiplicative_to_additive
[/- map operations -/
(`has_mul.mul, `has_add.add), (`has_one.one, `has_zero.zero), (`has_inv.inv, `has_neg.neg),
(`has_mul, `has_add), (`has_one, `has_zero), (`has_inv, `has_neg),
/- map constructors -/
(`has_mul.mk, `has_add.mk), (`has_one, `has_zero.mk), (`has_inv, `has_neg.mk),
/- map structures -/
(`semigroup, `add_semigroup),
(`monoid, `add_monoid),
(`group, `add_group),
(`comm_semigroup, `add_comm_semigroup),
(`comm_monoid, `add_comm_monoid),
(`comm_group, `add_comm_group),
(`left_cancel_semigroup, `add_left_cancel_semigroup),
(`right_cancel_semigroup, `add_right_cancel_semigroup),
(`left_cancel_semigroup.mk, `add_left_cancel_semigroup.mk),
(`right_cancel_semigroup.mk, `add_right_cancel_semigroup.mk),
/- map instances -/
(`semigroup.to_has_mul, `add_semigroup.to_has_add),
(`monoid.to_has_one, `add_monoid.to_has_zero),
(`group.to_has_inv, `add_group.to_has_neg),
(`comm_semigroup.to_semigroup, `add_comm_semigroup.to_add_semigroup),
(`monoid.to_semigroup, `add_monoid.to_add_semigroup),
(`comm_monoid.to_monoid, `add_comm_monoid.to_add_monoid),
(`comm_monoid.to_comm_semigroup, `add_comm_monoid.to_add_comm_semigroup),
(`group.to_monoid, `add_group.to_add_monoid),
(`comm_group.to_group, `add_comm_group.to_add_group),
(`comm_group.to_comm_monoid, `add_comm_group.to_add_comm_monoid),
(`left_cancel_semigroup.to_semigroup, `add_left_cancel_semigroup.to_add_semigroup),
(`right_cancel_semigroup.to_semigroup, `add_right_cancel_semigroup.to_add_semigroup),
/- map projections -/
(`semigroup.mul_assoc, `add_semigroup.add_assoc),
(`comm_semigroup.mul_comm, `add_comm_semigroup.add_comm),
(`left_cancel_semigroup.mul_left_cancel, `add_left_cancel_semigroup.add_left_cancel),
(`right_cancel_semigroup.mul_right_cancel, `add_right_cancel_semigroup.add_right_cancel),
(`monoid.one_mul, `add_monoid.zero_add),
(`monoid.mul_one, `add_monoid.add_zero),
(`group.mul_left_inv, `add_group.add_left_neg),
(`group.mul, `add_group.add),
(`group.mul_assoc, `add_group.add_assoc),
/- map lemmas -/
(`mul_assoc, `add_assoc),
(`mul_comm, `add_comm),
(`mul_left_comm, `add_left_comm),
(`mul_right_comm, `add_right_comm),
(`one_mul, `zero_add),
(`mul_one, `add_zero),
(`mul_left_inv, `add_left_neg),
(`mul_left_cancel, `add_left_cancel),
(`mul_right_cancel, `add_right_cancel),
(`mul_left_cancel_iff, `add_left_cancel_iff),
(`mul_right_cancel_iff, `add_right_cancel_iff),
(`inv_mul_cancel_left, `neg_add_cancel_left),
(`inv_mul_cancel_right, `neg_add_cancel_right),
(`eq_inv_mul_of_mul_eq, `eq_neg_add_of_add_eq),
(`inv_eq_of_mul_eq_one, `neg_eq_of_add_eq_zero),
(`inv_inv, `neg_neg),
(`mul_right_inv, `add_right_neg),
(`mul_inv_cancel_left, `add_neg_cancel_left),
(`mul_inv_cancel_right, `add_neg_cancel_right),
(`mul_inv_rev, `neg_add_rev),
(`mul_inv, `neg_add),
(`inv_inj, `neg_inj),
(`group.mul_left_cancel, `add_group.add_left_cancel),
(`group.mul_right_cancel, `add_group.add_right_cancel),
(`group.to_left_cancel_semigroup, `add_group.to_left_cancel_add_semigroup),
(`group.to_right_cancel_semigroup, `add_group.to_right_cancel_add_semigroup),
(`eq_inv_of_eq_inv, `eq_neg_of_eq_neg),
(`eq_inv_of_mul_eq_one, `eq_neg_of_add_eq_zero),
(`eq_mul_inv_of_mul_eq, `eq_add_neg_of_add_eq),
(`inv_mul_eq_of_eq_mul, `neg_add_eq_of_eq_add),
(`mul_inv_eq_of_eq_mul, `add_neg_eq_of_eq_add),
(`eq_mul_of_mul_inv_eq, `eq_add_of_add_neg_eq),
(`eq_mul_of_inv_mul_eq, `eq_add_of_neg_add_eq),
(`mul_eq_of_eq_inv_mul, `add_eq_of_eq_neg_add),
(`mul_eq_of_eq_mul_inv, `add_eq_of_eq_add_neg),
(`one_inv, `neg_zero)
]
instance add_semigroup_to_is_eq_associative [add_semigroup α] : is_associative α (+) :=
⟨add_assoc⟩
instance add_comm_semigroup_to_is_eq_commutative [add_comm_semigroup α] : is_commutative α (+) :=
⟨add_comm⟩
local attribute [simp] add_assoc add_comm add_left_comm
def neg_add_self := @add_left_neg
def add_neg_self := @add_right_neg
def eq_of_add_eq_add_left := @add_left_cancel
def eq_of_add_eq_add_right := @add_right_cancel
@[reducible] protected def algebra.sub [add_group α] (a b : α) : α :=
a + -b
instance add_group_has_sub [add_group α] : has_sub α :=
⟨algebra.sub⟩
local attribute [simp]
lemma sub_eq_add_neg [add_group α] (a b : α) : a - b = a + -b :=
rfl
lemma sub_self [add_group α] (a : α) : a - a = 0 :=
add_right_neg a
lemma sub_add_cancel [add_group α] (a b : α) : a - b + b = a :=
neg_add_cancel_right a b
lemma add_sub_cancel [add_group α] (a b : α) : a + b - b = a :=
add_neg_cancel_right a b
lemma add_sub_assoc [add_group α] (a b c : α) : a + b - c = a + (b - c) :=
by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg]
lemma eq_of_sub_eq_zero [add_group α] {a b : α} (h : a - b = 0) : a = b :=
have 0 + b = b, by rw zero_add,
have (a - b) + b = b, by rwa h,
by rwa [sub_eq_add_neg, neg_add_cancel_right] at this
lemma sub_eq_zero_of_eq [add_group α] {a b : α} (h : a = b) : a - b = 0 :=
by rw [h, sub_self]
lemma sub_eq_zero_iff_eq [add_group α] {a b : α} : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, sub_eq_zero_of_eq⟩
lemma zero_sub [add_group α] (a : α) : 0 - a = -a :=
zero_add (-a)
lemma sub_zero [add_group α] (a : α) : a - 0 = a :=
by rw [sub_eq_add_neg, neg_zero, add_zero]
lemma sub_ne_zero_of_ne [add_group α] {a b : α} (h : a ≠ b) : a - b ≠ 0 :=
begin
intro hab,
apply h,
apply eq_of_sub_eq_zero hab
end
lemma sub_neg_eq_add [add_group α] (a b : α) : a - (-b) = a + b :=
by rw [sub_eq_add_neg, neg_neg]
lemma neg_sub [add_group α] (a b : α) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, add_right_neg])
lemma add_sub [add_group α] (a b c : α) : a + (b - c) = a + b - c :=
by simp
lemma sub_add_eq_sub_sub_swap [add_group α] (a b c : α) : a - (b + c) = a - c - b :=
by simp
lemma add_sub_add_right_eq_sub [add_group α] (a b c : α) : (a + c) - (b + c) = a - b :=
by rw [sub_add_eq_sub_sub_swap]; simp
lemma eq_sub_of_add_eq [add_group α] {a b c : α} (h : a + c = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add [add_group α] {a b c : α} (h : a = c + b) : a - b = c :=
by simp [h]
lemma eq_add_of_sub_eq [add_group α] {a b c : α} (h : a - c = b) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub [add_group α] {a b c : α} (h : a = c - b) : a + b = c :=
by simp [h]
lemma sub_add_eq_sub_sub [add_comm_group α] (a b c : α) : a - (b + c) = a - b - c :=
by simp
lemma neg_add_eq_sub [add_comm_group α] (a b : α) : -a + b = b - a :=
by simp
lemma sub_add_eq_add_sub [add_comm_group α] (a b c : α) : a - b + c = a + c - b :=
by simp
lemma sub_sub [add_comm_group α] (a b c : α) : a - b - c = a - (b + c) :=
by simp
lemma sub_add [add_comm_group α] (a b c : α) : a - b + c = a - (b - c) :=
by simp
lemma add_sub_add_left_eq_sub [add_comm_group α] (a b c : α) : (c + a) - (c + b) = a - b :=
by simp
lemma eq_sub_of_add_eq' [add_comm_group α] {a b c : α} (h : c + a = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add' [add_comm_group α] {a b c : α} (h : a = b + c) : a - b = c :=
begin simp [h], rw [add_left_comm], simp end
lemma eq_add_of_sub_eq' [add_comm_group α] {a b c : α} (h : a - b = c) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub' [add_comm_group α] {a b c : α} (h : b = c - a) : a + b = c :=
begin simp [h], rw [add_comm c, add_neg_cancel_left] end
lemma sub_sub_self [add_comm_group α] (a b : α) : a - (a - b) = b :=
begin simp, rw [add_comm b, add_neg_cancel_left] end
lemma add_sub_comm [add_comm_group α] (a b c d : α) : a + b - (c + d) = (a - c) + (b - d) :=
by simp
lemma sub_eq_sub_add_sub [add_comm_group α] (a b c : α) : a - b = c - b + (a - c) :=
begin simp, rw [add_left_comm c], simp end
lemma neg_neg_sub_neg [add_comm_group α] (a b : α) : - (-a - -b) = a - b :=
by simp
/- The following lemmas generate too many instances for rsimp -/
attribute [no_rsimp]
mul_assoc mul_comm mul_left_comm
add_assoc add_comm add_left_comm
|
673f82b9d3d1c4663737423692522b9f48b75822 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/over.lean | fdd8918063f6c8b57ed8cde7d527998d358b9050 | [
"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 | 11,774 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Bhavik Mehta
-/
import category_theory.structured_arrow
import category_theory.punit
import category_theory.functor.reflects_isomorphisms
import category_theory.epi_mono
/-!
# Over and under categories
Over (and under) categories are special cases of comma categories.
* If `L` is the identity functor and `R` is a constant functor, then `comma L R` is the "slice" or
"over" category over the object `R` maps to.
* Conversely, if `L` is a constant functor and `R` is the identity functor, then `comma L R` is the
"coslice" or "under" category under the object `L` maps to.
## Tags
comma, slice, coslice, over, under
-/
namespace category_theory
universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes].
variables {T : Type u₁} [category.{v₁} T]
/--
The over category has as objects arrows in `T` with codomain `X` and as morphisms commutative
triangles.
See <https://stacks.math.columbia.edu/tag/001G>.
-/
@[derive category]
def over (X : T) := costructured_arrow (𝟭 T) X
-- Satisfying the inhabited linter
instance over.inhabited [inhabited T] : inhabited (over (default : T)) :=
{ default :=
{ left := default,
hom := 𝟙 _ } }
namespace over
variables {X : T}
@[ext] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V}
(h : f.left = g.left) : f = g :=
by tidy
@[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy
@[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl
@[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).left = f.left ≫ g.left := rfl
@[simp, reassoc] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom :=
by have := f.w; tidy
/-- To give an object in the over category, it suffices to give a morphism with codomain `X`. -/
@[simps]
def mk {X Y : T} (f : Y ⟶ X) : over X :=
costructured_arrow.mk f
/-- We can set up a coercion from arrows with codomain `X` to `over X`. This most likely should not
be a global instance, but it is sometimes useful. -/
def coe_from_hom {X Y : T} : has_coe (Y ⟶ X) (over X) :=
{ coe := mk }
section
local attribute [instance] coe_from_hom
@[simp] lemma coe_hom {X Y : T} (f : Y ⟶ X) : (f : over X).hom = f := rfl
end
/-- To give a morphism in the over category, it suffices to give an arrow fitting in a commutative
triangle. -/
@[simps]
def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) :
U ⟶ V :=
costructured_arrow.hom_mk f w
/--
Construct an isomorphism in the over category given isomorphisms of the objects whose forward
direction gives a commutative triangle.
-/
@[simps]
def iso_mk {f g : over X} (hl : f.left ≅ g.left) (hw : hl.hom ≫ g.hom = f.hom . obviously) :
f ≅ g :=
costructured_arrow.iso_mk hl hw
section
variable (X)
/--
The forgetful functor mapping an arrow to its domain.
See <https://stacks.math.columbia.edu/tag/001G>.
-/
def forget : over X ⥤ T := comma.fst _ _
end
@[simp] lemma forget_obj {U : over X} : (forget X).obj U = U.left := rfl
@[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : (forget X).map f = f.left := rfl
/-- The natural cocone over the forgetful functor `over X ⥤ T` with cocone point `X`. -/
@[simps] def forget_cocone (X : T) : limits.cocone (forget X) :=
{ X := X, ι := { app := comma.hom } }
/--
A morphism `f : X ⟶ Y` induces a functor `over X ⥤ over Y` in the obvious way.
See <https://stacks.math.columbia.edu/tag/001G>.
-/
def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ discrete.nat_trans (λ _, f)
section
variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V}
@[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl
@[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl
/-- Mapping by the identity morphism is just the identity functor. -/
def map_id : map (𝟙 Y) ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy)
/-- Mapping by the composite morphism `f ≫ g` is the same as mapping by `f` then by `g`. -/
def map_comp {Y Z : T} (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map f ⋙ map g :=
nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy)
end
instance forget_reflects_iso : reflects_isomorphisms (forget X) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨over.hom_mk (inv ((forget X).map f))
((as_iso ((forget X).map f)).inv_comp_eq.2 (over.w f).symm),
by tidy⟩⟩ }
instance forget_faithful : faithful (forget X) := {}.
/--
If `k.left` is an epimorphism, then `k` is an epimorphism. In other words, `over.forget X` reflects
epimorphisms.
The converse does not hold without additional assumptions on the underlying category.
-/
-- TODO: Show the converse holds if `T` has binary products or pushouts.
lemma epi_of_epi_left {f g : over X} (k : f ⟶ g) [hk : epi k.left] : epi k :=
faithful_reflects_epi (forget X) hk
/--
If `k.left` is a monomorphism, then `k` is a monomorphism. In other words, `over.forget X` reflects
monomorphisms.
The converse of `category_theory.over.mono_left_of_mono`.
This lemma is not an instance, to avoid loops in type class inference.
-/
lemma mono_of_mono_left {f g : over X} (k : f ⟶ g) [hk : mono k.left] : mono k :=
faithful_reflects_mono (forget X) hk
/--
If `k` is a monomorphism, then `k.left` is a monomorphism. In other words, `over.forget X` preserves
monomorphisms.
The converse of `category_theory.over.mono_of_mono_left`.
-/
instance mono_left_of_mono {f g : over X} (k : f ⟶ g) [mono k] : mono k.left :=
begin
refine ⟨λ (Y : T) l m a, _⟩,
let l' : mk (m ≫ f.hom) ⟶ f := hom_mk l (by { dsimp, rw [←over.w k, reassoc_of a] }),
suffices : l' = hom_mk m,
{ apply congr_arg comma_morphism.left this },
rw ← cancel_mono k,
ext,
apply a,
end
section iterated_slice
variables (f : over X)
/-- Given f : Y ⟶ X, this is the obvious functor from (T/X)/f to T/Y -/
@[simps]
def iterated_slice_forward : over f ⥤ over f.left :=
{ obj := λ α, over.mk α.hom.left,
map := λ α β κ, over.hom_mk κ.left.left (by { rw auto_param_eq, rw ← over.w κ, refl }) }
/-- Given f : Y ⟶ X, this is the obvious functor from T/Y to (T/X)/f -/
@[simps]
def iterated_slice_backward : over f.left ⥤ over f :=
{ obj := λ g, mk (hom_mk g.hom : mk (g.hom ≫ f.hom) ⟶ f),
map := λ g h α, hom_mk (hom_mk α.left (w_assoc α f.hom)) (over_morphism.ext (w α)) }
/-- Given f : Y ⟶ X, we have an equivalence between (T/X)/f and T/Y -/
@[simps]
def iterated_slice_equiv : over f ≌ over f.left :=
{ functor := iterated_slice_forward f,
inverse := iterated_slice_backward f,
unit_iso :=
nat_iso.of_components
(λ g, over.iso_mk (over.iso_mk (iso.refl _) (by tidy)) (by tidy))
(λ X Y g, by { ext, dsimp, simp }),
counit_iso :=
nat_iso.of_components
(λ g, over.iso_mk (iso.refl _) (by tidy))
(λ X Y g, by { ext, dsimp, simp }) }
lemma iterated_slice_forward_forget :
iterated_slice_forward f ⋙ forget f.left = forget f ⋙ forget X :=
rfl
lemma iterated_slice_backward_forget_forget :
iterated_slice_backward f ⋙ forget f ⋙ forget X = forget f.left :=
rfl
end iterated_slice
section
variables {D : Type u₂} [category.{v₂} D]
/-- A functor `F : T ⥤ D` induces a functor `over X ⥤ over (F.obj X)` in the obvious way. -/
@[simps]
def post (F : T ⥤ D) : over X ⥤ over (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ left := F.map f.left,
w' := by tidy; erw [← F.map_comp, w] } }
end
end over
/-- The under category has as objects arrows with domain `X` and as morphisms commutative
triangles. -/
@[derive category]
def under (X : T) := structured_arrow X (𝟭 T)
-- Satisfying the inhabited linter
instance under.inhabited [inhabited T] : inhabited (under (default : T)) :=
{ default :=
{ right := default,
hom := 𝟙 _ } }
namespace under
variables {X : T}
@[ext] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V}
(h : f.right = g.right) : f = g :=
by tidy
@[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy
@[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl
@[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).right = f.right ≫ g.right := rfl
@[simp, reassoc] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom :=
by have := f.w; tidy
/-- To give an object in the under category, it suffices to give an arrow with domain `X`. -/
@[simps]
def mk {X Y : T} (f : X ⟶ Y) : under X :=
structured_arrow.mk f
/-- To give a morphism in the under category, it suffices to give a morphism fitting in a
commutative triangle. -/
@[simps]
def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) :
U ⟶ V :=
structured_arrow.hom_mk f w
/--
Construct an isomorphism in the over category given isomorphisms of the objects whose forward
direction gives a commutative triangle.
-/
def iso_mk {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) : f ≅ g :=
structured_arrow.iso_mk hr hw
@[simp]
lemma iso_mk_hom_right {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) :
(iso_mk hr hw).hom.right = hr.hom := rfl
@[simp]
lemma iso_mk_inv_right {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) :
(iso_mk hr hw).inv.right = hr.inv := rfl
section
variables (X)
/-- The forgetful functor mapping an arrow to its domain. -/
def forget : under X ⥤ T := comma.snd _ _
end
@[simp] lemma forget_obj {U : under X} : (forget X).obj U = U.right := rfl
@[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : (forget X).map f = f.right := rfl
/-- The natural cone over the forgetful functor `under X ⥤ T` with cone point `X`. -/
@[simps] def forget_cone (X : T) : limits.cone (forget X) :=
{ X := X, π := { app := comma.hom } }
/-- A morphism `X ⟶ Y` induces a functor `under Y ⥤ under X` in the obvious way. -/
def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ discrete.nat_trans (λ _, f)
section
variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V}
@[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl
@[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl
/-- Mapping by the identity morphism is just the identity functor. -/
def map_id : map (𝟙 Y) ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy)
/-- Mapping by the composite morphism `f ≫ g` is the same as mapping by `f` then by `g`. -/
def map_comp {Y Z : T} (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f :=
nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy)
end
instance forget_reflects_iso : reflects_isomorphisms (forget X) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨under.hom_mk (inv ((under.forget X).map f)) ((is_iso.comp_inv_eq _).2 (under.w f).symm),
by tidy⟩⟩ }
instance forget_faithful : faithful (forget X) := {}.
section
variables {D : Type u₂} [category.{v₂} D]
/-- A functor `F : T ⥤ D` induces a functor `under X ⥤ under (F.obj X)` in the obvious way. -/
@[simps]
def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ right := F.map f.right,
w' := by tidy; erw [← F.map_comp, w] } }
end
end under
end category_theory
|
254d11df6c3492593e114dc445bae6b39226e490 | 2c096fdfecf64e46ea7bc6ce5521f142b5926864 | /src/Init/NotationExtra.lean | 5cf84a9d59c799dcaf6f5840c95d27665698de69 | [
"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 | Kha/lean4 | 1005785d2c8797ae266a303968848e5f6ce2fe87 | b99e11346948023cd6c29d248cd8f3e3fb3474cf | refs/heads/master | 1,693,355,498,027 | 1,669,080,461,000 | 1,669,113,138,000 | 184,748,176 | 0 | 0 | Apache-2.0 | 1,665,995,520,000 | 1,556,884,930,000 | Lean | UTF-8 | Lean | false | false | 16,550 | 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
import Init.Data.ToString
namespace Lean
macro "Macro.trace[" id:ident "]" s:interpolatedStr(term) : term =>
`(Macro.trace $(quote id.getId.eraseMacroScopes) (s! $s))
-- Auxiliary parsers and functions for declaring notation with binders
syntax unbracketedExplicitBinders := binderIdent+ (" : " term)?
syntax bracketedExplicitBinders := "(" withoutPosition(binderIdent+ " : " term) ")"
syntax explicitBinders := bracketedExplicitBinders+ <|> unbracketedExplicitBinders
open TSyntax.Compat in
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 : $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.unbracketedExplicitBinders 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 patternIgnore(" =?= " <|> " ≟ ") term
syntax unifConstraintElem := colGe unifConstraint ", "?
syntax (docComment)? attrKind "unif_hint " (ident)? bracketedBinder* " where " withPosition(unifConstraintElem*) patternIgnore("|-" <|> "⊢ ") unifConstraint : command
macro_rules
| `($[$doc?:docComment]? $kind:attrKind unif_hint $(n)? $bs* where $[$cs₁ ≟ $cs₂]* |- $t₁ ≟ $t₂) => do
let mut body ← `($t₁ = $t₂)
for (c₁, c₂) in cs₁.zip cs₂ |>.reverse do
body ← `($c₁ = $c₂ → $body)
let hint : Ident ← `(hint)
`($[$doc?:docComment]? @[$kind unification_hint] def $(n.getD hint) $bs* : Sort _ := $body)
end Lean
open Lean
section
open TSyntax.Compat
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
end
-- enforce indentation of calc steps so we know when to stop parsing them
syntax calcStep := ppIndent(colGe term " := " withPosition(term))
/-- Step-wise reasoning over transitive relations.
```
calc
a = b := pab
b = c := pbc
...
y = z := pyz
```
proves `a = z` from the given step-wise proofs. `=` can be replaced with any
relation implementing the typeclass `Trans`. Instead of repeating the right-
hand sides, subsequent left-hand sides can be replaced with `_`.
`calc` has term mode and tactic mode variants. This is the term mode variant.
See [Theorem Proving in Lean 4][tpil4] for more information.
[tpil4]: https://leanprover.github.io/theorem_proving_in_lean4/quantifiers_and_equality.html#calculational-proofs
-/
syntax (name := calc) "calc" ppLine withPosition(calcStep) ppLine withPosition((calcStep ppLine)*) : term
/-- Step-wise reasoning over transitive relations.
```
calc
a = b := pab
b = c := pbc
...
y = z := pyz
```
proves `a = z` from the given step-wise proofs. `=` can be replaced with any
relation implementing the typeclass `Trans`. Instead of repeating the right-
hand sides, subsequent left-hand sides can be replaced with `_`.
`calc` has term mode and tactic mode variants. This is the tactic mode variant,
which supports an additional feature: it works even if the goal is `a = z'`
for some other `z'`; in this case it will not close the goal but will instead
leave a subgoal proving `z = z'`.
See [Theorem Proving in Lean 4][tpil4] for more information.
[tpil4]: https://leanprover.github.io/theorem_proving_in_lean4/quantifiers_and_equality.html#calculational-proofs
-/
syntax (name := calcTactic) "calc" ppLine withPosition(calcStep) ppLine withPosition((calcStep ppLine)*) : tactic
@[app_unexpander Unit.unit] def unexpandUnit : Lean.PrettyPrinter.Unexpander
| `($(_)) => `(())
@[app_unexpander List.nil] def unexpandListNil : Lean.PrettyPrinter.Unexpander
| `($(_)) => `([])
@[app_unexpander List.cons] def unexpandListCons : Lean.PrettyPrinter.Unexpander
| `($(_) $x []) => `([$x])
| `($(_) $x [$xs,*]) => `([$x, $xs,*])
| _ => throw ()
@[app_unexpander List.toArray] def unexpandListToArray : Lean.PrettyPrinter.Unexpander
| `($(_) [$xs,*]) => `(#[$xs,*])
| _ => throw ()
@[app_unexpander Prod.mk] def unexpandProdMk : Lean.PrettyPrinter.Unexpander
| `($(_) $x ($y, $ys,*)) => `(($x, $y, $ys,*))
| `($(_) $x $y) => `(($x, $y))
| _ => throw ()
@[app_unexpander ite] def unexpandIte : Lean.PrettyPrinter.Unexpander
| `($(_) $c $t $e) => `(if $c then $t else $e)
| _ => throw ()
@[app_unexpander sorryAx] def unexpandSorryAx : Lean.PrettyPrinter.Unexpander
| `($(_) _) => `(sorry)
| `($(_) _ _) => `(sorry)
| _ => throw ()
@[app_unexpander Eq.ndrec] def unexpandEqNDRec : Lean.PrettyPrinter.Unexpander
| `($(_) $m $h) => `($h ▸ $m)
| _ => throw ()
@[app_unexpander Eq.rec] def unexpandEqRec : Lean.PrettyPrinter.Unexpander
| `($(_) $m $h) => `($h ▸ $m)
| _ => throw ()
@[app_unexpander Exists] def unexpandExists : Lean.PrettyPrinter.Unexpander
| `($(_) fun $x:ident => ∃ $xs:binderIdent*, $b) => `(∃ $x:ident $xs:binderIdent*, $b)
| `($(_) fun $x:ident => $b) => `(∃ $x:ident, $b)
| `($(_) fun ($x:ident : $t) => $b) => `(∃ ($x:ident : $t), $b)
| _ => throw ()
@[app_unexpander Sigma] def unexpandSigma : Lean.PrettyPrinter.Unexpander
| `($(_) fun ($x:ident : $t) => $b) => `(($x:ident : $t) × $b)
| _ => throw ()
@[app_unexpander PSigma] def unexpandPSigma : Lean.PrettyPrinter.Unexpander
| `($(_) fun ($x:ident : $t) => $b) => `(($x:ident : $t) ×' $b)
| _ => throw ()
@[app_unexpander Subtype] def unexpandSubtype : Lean.PrettyPrinter.Unexpander
| `($(_) fun ($x:ident : $type) => $p) => `({ $x : $type // $p })
| `($(_) fun $x:ident => $p) => `({ $x // $p })
| _ => throw ()
@[app_unexpander TSyntax] def unexpandTSyntax : Lean.PrettyPrinter.Unexpander
| `($f [$k]) => `($f $k)
| _ => throw ()
@[app_unexpander TSyntaxArray] def unexpandTSyntaxArray : Lean.PrettyPrinter.Unexpander
| `($f [$k]) => `($f $k)
| _ => throw ()
@[app_unexpander Syntax.TSepArray] def unexpandTSepArray : Lean.PrettyPrinter.Unexpander
| `($f [$k] $sep) => `($f $k $sep)
| _ => throw ()
@[app_unexpander GetElem.getElem] def unexpandGetElem : Lean.PrettyPrinter.Unexpander
| `($_ $array $index $_) => `($array[$index])
| _ => throw ()
@[app_unexpander getElem!] def unexpandGetElem! : Lean.PrettyPrinter.Unexpander
| `($_ $array $index) => `($array[$index]!)
| _ => throw ()
@[app_unexpander getElem?] def unexpandGetElem? : Lean.PrettyPrinter.Unexpander
| `($_ $array $index) => `($array[$index]?)
| _ => throw ()
@[app_unexpander Name.mkStr1] def unexpandMkStr1 : Lean.PrettyPrinter.Unexpander
| `($(_) $a:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr2] def unexpandMkStr2 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr3] def unexpandMkStr3 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr4] def unexpandMkStr4 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str $a4:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}.{a4.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr5] def unexpandMkStr5 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str $a4:str $a5:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}.{a4.getString}.{a5.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr6] def unexpandMkStr6 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str $a4:str $a5:str $a6:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}.{a4.getString}.{a5.getString}.{a6.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr7] def unexpandMkStr7 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str $a4:str $a5:str $a6:str $a7:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}.{a4.getString}.{a5.getString}.{a6.getString}.{a7.getString}"]
| _ => throw ()
@[app_unexpander Name.mkStr8] def unexpandMkStr8 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1:str $a2:str $a3:str $a4:str $a5:str $a6:str $a7:str $a8:str) => return mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{a1.getString}.{a2.getString}.{a3.getString}.{a4.getString}.{a5.getString}.{a6.getString}.{a7.getString}.{a8.getString}"]
| _ => throw ()
@[app_unexpander Array.empty] def unexpandArrayEmpty : Lean.PrettyPrinter.Unexpander
| _ => `(#[])
@[app_unexpander Array.mkArray0] def unexpandMkArray0 : Lean.PrettyPrinter.Unexpander
| _ => `(#[])
@[app_unexpander Array.mkArray1] def unexpandMkArray1 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1) => `(#[$a1])
| _ => throw ()
@[app_unexpander Array.mkArray2] def unexpandMkArray2 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2) => `(#[$a1, $a2])
| _ => throw ()
@[app_unexpander Array.mkArray3] def unexpandMkArray3 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3) => `(#[$a1, $a2, $a3])
| _ => throw ()
@[app_unexpander Array.mkArray4] def unexpandMkArray4 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3 $a4) => `(#[$a1, $a2, $a3, $a4])
| _ => throw ()
@[app_unexpander Array.mkArray5] def unexpandMkArray5 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3 $a4 $a5) => `(#[$a1, $a2, $a3, $a4, $a5])
| _ => throw ()
@[app_unexpander Array.mkArray6] def unexpandMkArray6 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3 $a4 $a5 $a6) => `(#[$a1, $a2, $a3, $a4, $a5, $a6])
| _ => throw ()
@[app_unexpander Array.mkArray7] def unexpandMkArray7 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3 $a4 $a5 $a6 $a7) => `(#[$a1, $a2, $a3, $a4, $a5, $a6, $a7])
| _ => throw ()
@[app_unexpander Array.mkArray8] def unexpandMkArray8 : Lean.PrettyPrinter.Unexpander
| `($(_) $a1 $a2 $a3 $a4 $a5 $a6 $a7 $a8) => `(#[$a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8])
| _ => throw ()
/--
Apply function extensionality and introduce new hypotheses.
The tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible to
```
|- ((fun x => ...) = (fun x => ...))
```
The variant `funext h₁ ... hₙ` applies `funext` `n` times, and uses the given identifiers to name the new hypotheses.
Patterns can be used like in the `intro` tactic. Example, given a goal
```
|- ((fun x : Nat × Bool => ...) = (fun x => ...))
```
`funext (a, b)` applies `funext` once and performs pattern matching on the newly introduced pair.
-/
syntax "funext " (colGt term:max)+ : tactic
macro_rules
| `(tactic|funext $x) => `(tactic| apply funext; intro $x:term)
| `(tactic|funext $x $xs*) => `(tactic| apply funext; intro $x:term; funext $xs*)
macro_rules
| `(%[ $[$x],* | $k ]) =>
if x.size < 8 then
x.foldrM (β := Term) (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
attribute [instance] C.mk
```
-/
syntax (name := Lean.Parser.Command.classAbbrev)
declModifiers "class " "abbrev " declId bracketedBinder* (":" term)?
":=" withPosition(group(colGe term ","?)*) : command
macro_rules
| `($mods:declModifiers class abbrev $id $params* $[: $ty]? := $[ $parents $[,]? ]*) =>
let ctor := mkIdentFrom id <| id.raw[0].getId.modifyBase (. ++ `mk)
`($mods:declModifiers class $id $params* extends $parents,* $[: $ty]?
attribute [instance] $ctor)
section
open Lean.Parser.Tactic
syntax cdotTk := patternIgnore("·" <|> ".")
/-- `· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/
syntax cdotTk ppHardSpace many1Indent(tactic ";"? ppLine) : tactic
macro_rules
| `(tactic| $cdot:cdotTk $[$tacs $[;%$sc]?]*) => do
let tacs ← tacs.zip sc |>.mapM fun
| (tac, none) => pure tac
| (tac, some sc) => `(tactic| ($tac; with_annotate_state $sc skip))
`(tactic| { with_annotate_state $cdot skip; $[$tacs]* })
end
/--
Similar to `first`, but succeeds only if one the given tactics solves the current goal.
-/
syntax (name := solve) "solve " withPosition((colGe "|" tacticSeq)+) : tactic
macro_rules
| `(tactic| solve $[| $ts]* ) => `(tactic| focus first $[| ($ts); done]*)
namespace Lean
/-! # `repeat` and `while` notation -/
inductive Loop where
| mk
@[inline]
partial def Loop.forIn {β : Type u} {m : Type u → Type v} [Monad m] (_ : Loop) (init : β) (f : Unit → β → m (ForInStep β)) : m β :=
let rec @[specialize] loop (b : β) : m β := do
match ← f () b with
| ForInStep.done b => pure b
| ForInStep.yield b => loop b
loop init
instance : ForIn m Loop Unit where
forIn := Loop.forIn
syntax "repeat " doSeq : doElem
macro_rules
| `(doElem| repeat $seq) => `(doElem| for _ in Loop.mk do $seq)
syntax "while " ident " : " termBeforeDo " do " doSeq : doElem
macro_rules
| `(doElem| while $h : $cond do $seq) => `(doElem| repeat if $h : $cond then $seq else break)
syntax "while " termBeforeDo " do " doSeq : doElem
macro_rules
| `(doElem| while $cond do $seq) => `(doElem| repeat if $cond then $seq else break)
syntax "repeat " doSeq " until " term : doElem
macro_rules
| `(doElem| repeat $seq until $cond) => `(doElem| repeat do $seq:doSeq; if $cond then break)
macro:50 e:term:51 " matches " p:sepBy1(term:51, "|") : term =>
`(((match $e:term with | $[$p:term]|* => true | _ => false) : Bool))
end Lean
|
42333a194f8eed52e39a13f52566efbb184427d9 | 29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f | /21_lecture.lean | f60e7a4240a80c070e5c7a25a44b36e1ba4042f5 | [] | no_license | KjellZijlemaker/Logical_Verification_VU | ced0ba95316a30e3c94ba8eebd58ea004fa6f53b | 4578b93bf1615466996157bb333c84122b201d99 | refs/heads/master | 1,585,966,086,108 | 1,549,187,704,000 | 1,549,187,704,000 | 155,690,284 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,857 | lean | /- Lecture 2.1: Functional Programming — Lists -/
/- Lists -/
namespace my_list
-- length of a list
def length {α : Type} : list α → ℕ
| [] := 0
| (x :: xs) := length xs + 1
#reduce length [2,3,1]
-- count elements in a list
def bcount {α : Type} (p : α → bool) : list α → ℕ
| [] := 0
| (x :: xs) :=
match p x with
| tt := bcount xs + 1
| ff := bcount xs
end
-- filter elements
def bfilter {α : Type} (p : α → bool) : list α → list α
| [] := []
| (x :: xs) :=
match p x with
| tt := x :: bfilter xs
| ff := bfilter xs
end
-- `n`th element (actually, `n + 1`st)
def nth {α : Type} : list α → ℕ → option α
| [] _ := none
| (x :: _) 0 := some x
| (_ :: xs) (n + 1) := nth xs n
lemma exists_nth {α : Type} :
∀(xs : list α) (n : ℕ), n < length xs → ∃a, nth xs n = some a
| [] _ h := false.elim (not_le_of_gt h (nat.zero_le _))
| (x :: _) 0 _ := ⟨x, rfl⟩
| (_ :: xs) (n + 1) h :=
have n_lt : n < length xs := lt_of_add_lt_add_right h,
have ih : ∃a, nth xs n = some a := exists_nth xs n n_lt,
by simp [nth, ih]
lemma lt_length_of_nth_some {α : Type} (a : α) :
∀(xs : list α) (n : ℕ), nth xs n = some a → n < length xs
| [] _ h := by contradiction
| (_ :: xs) 0 _ :=
show 0 < length xs + 1, from
lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one
| (_ :: xs) (n + 1) h :=
have ih : nth xs n = some a,
by simp [nth] at h; assumption,
show n + 1 < length xs + 1, from
add_lt_add_right (lt_length_of_nth_some _ _ ih) 1
-- tail of a list
def tail {α : Type} (xs : list α) : list α :=
match xs with
| [] := []
| _ :: xs := xs
end
-- head of a list
def head {α : Type} (xs : list α) : option α :=
match xs with
| [] := none
| x :: _ := some x
end
-- zip
def zip {α β : Type} : list α → list β → list (α × β)
| (x :: xs) (y :: ys) := (x, y) :: zip xs ys
| [] _ := []
| (_ :: _) [] := []
lemma map_zip {α α' β β' : Type} (f : α → α') (g : β → β') :
∀xs ys, list.map (λp : α × β, (f p.1, g p.2)) (zip xs ys) =
zip (list.map f xs) (list.map g ys)
| (x :: xs) (y :: ys) := begin simp [zip, list.map], exact (map_zip _ _) end
| [] _ := by refl
| (_ :: _) [] := by refl
lemma min_add_add (l m n : ℕ) :
min (l + m) (l + n) = l + min m n :=
have cancel : l + m ≤ l + n ↔ m ≤ n :=
begin
rw add_comm l,
rw add_comm l,
rw nat.add_le_add_iff_le_right
end,
by by_cases m ≤ n; simp [min, *]
lemma length_zip {α β : Type} :
∀(xs : list α) (ys : list β), length (zip xs ys) = min (length xs) (length ys)
| (x :: xs) (y :: ys) := by simp [zip, length, length_zip xs ys, min_add_add]
| [] _ := by refl
| (_ :: _) [] := by refl
end my_list
/- Type classes -/
namespace my_typeclass
class inhabited (α : Type) :=
(default_value : α)
instance : inhabited ℕ :=
⟨0⟩
instance prod_inhabited (α β) [inhabited α] [inhabited β] : inhabited (α × β) :=
⟨(inhabited.default_value α, inhabited.default_value β)⟩
#print instances inhabited
def nat_value : ℕ := inhabited.default_value ℕ
def pair_value : ℕ × ℕ := inhabited.default_value _
section
set_option pp.implicit true
#print nat_value
#print pair_value
end
-- `nth` for inhabited types
def inth {α : Type} [inhabited α] : list α → ℕ → α
| [] _ := inhabited.default_value α
| (x :: xs) 0 := x
| (x :: xs) (n + 1) := inth xs n
-- `head` for inhabited types
def ihead {α : Type} [inhabited α] (xs : list α) : α :=
match xs with
| [] := inhabited.default_value α
| x :: _ := x
end
lemma inth_eq_ihead {α : Type} [inhabited α] (xs : list α) :
inth xs 0 = ihead xs :=
match xs with
| [] := by refl
| _ :: _ := by refl
end
end my_typeclass
/- Decidable -/
#print decidable
#print decidable_eq
#print decidable_pred
#check ite
#check if_pos
#check if_neg
#check dite
#check dif_pos
#check dif_neg
lemma if_distrib {α β : Type} (f : α → β) (t e : α) (c : Prop) [decidable c] :
(if c then f t else f e) = f (if c then t else e) :=
by by_cases c; simp *
namespace my_list
def filter {α : Type} (p : α → Prop) [decidable_pred p] : list α → list α
| [] := []
| (x :: xs) := if p x then x :: filter xs else filter xs
#eval filter (λn : ℕ, 2 ≤ n ∧ n ≤ 10) [1, 2, 3, 4, 11, 15, 2]
section
set_option pp.implicit true
#check filter (λn : ℕ, 2 ≤ n ∧ n ≤ 10) [1, 2, 3, 4, 11, 15, 2]
end
/- **Optional**: Vectors as a dependent inductive type -/
inductive vec (α : Type) : ℕ → Type
| vnil {} : vec 0
| vcons (a : α) (n : ℕ) (v : vec n) : vec (n + 1)
export vec (vnil vcons)
#check vnil
#check vcons
def vec_to_list {α : Type} : ∀{n : ℕ}, vec α n → list α
| _ vnil := []
| _ (vcons a n v) := a :: vec_to_list v
def list_to_vec {α : Type} : ∀(l : list α) (n : ℕ), list.length l = n → vec α n
| [] 0 h := vec.vnil
| (x :: xs) (n + 1) h := vcons x n (list_to_vec xs n (nat.succ.inj h))
lemma length_vec_to_list {α : Type} :
∀{n : ℕ} (v : vec α n), list.length (vec_to_list v) = n
| _ vnil := by refl
| _ (vcons a n v) := congr_arg nat.succ (length_vec_to_list v)
lemma list_to_vec_vec_to_list {α : Type} :
∀{n : ℕ} (v : vec α n), list_to_vec (vec_to_list v) _ (length_vec_to_list _) = v
| _ vnil := by refl
| _ (vcons a n v) := by simp [vec_to_list, list_to_vec, list_to_vec_vec_to_list v]
lemma vec_to_list_list_to_vec {α : Type} :
∀(l : list α) (n : ℕ) (h : list.length l = n), vec_to_list (list_to_vec l n h) = l
| [] 0 h := by refl
| (x :: xs) (n + 1) h := by simp [list_to_vec, vec_to_list, vec_to_list_list_to_vec xs]
/- **Optional**: Vectors as a dependent quotient type -/
def vec2 (α : Type) (n : ℕ) : Type := { l : list α // list.length l = n }
def vec_to_vec2 {α : Type} {n : ℕ} (v : vec α n) : vec2 α n :=
⟨vec_to_list v, length_vec_to_list v⟩
def vec2_to_vec {α : Type} : ∀{n : ℕ}, vec2 α n → vec α n
| n ⟨l, hn⟩ := list_to_vec l n hn
lemma vec_to_vec2_vec2_to_vec {α : Type} :
∀{n : ℕ} (v : vec2 α n), vec_to_vec2 (vec2_to_vec v) = v
| _ ⟨l, rfl⟩ :=
begin
simp [vec2_to_vec, vec_to_vec2],
apply subtype.eq,
apply vec_to_list_list_to_vec
end
lemma vec2_to_vec_vec_to_vec2 {α : Type} {n : ℕ} (v : vec α n) :
vec2_to_vec (vec_to_vec2 v) = v :=
by simp [vec_to_vec2, vec2_to_vec, list_to_vec_vec_to_list]
def list_to_vec' {α : Type} : ∀(l : list α), vec α (list.length l)
| [] := vnil
| (x :: xs) := vcons x _ (list_to_vec' xs)
def vec2_to_vec' {α : Type} : ∀{n : ℕ}, vec2 α n → vec α n
| n ⟨l, hn⟩ :=
begin
rw ← hn,
exact list_to_vec' l
end
end my_list
|
21c4b7a39913c4deb67bdea9eb6033110bd46a5d | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Lean/Meta/Tactic/Replace.lean | 1bb22a3f53d14aa1fe794b7598b322bd89efc510 | [
"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 | 8,404 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.ForEachExpr
import Lean.Meta.AppBuilder
import Lean.Meta.MatchUtil
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.Assert
namespace Lean.Meta
/--
Convert the given goal `Ctx |- target` into `Ctx |- targetNew` using an equality proof `eqProof : target = targetNew`.
It assumes `eqProof` has type `target = targetNew` -/
def _root_.Lean.MVarId.replaceTargetEq (mvarId : MVarId) (targetNew : Expr) (eqProof : Expr) : MetaM MVarId :=
mvarId.withContext do
mvarId.checkNotAssigned `replaceTarget
let tag ← mvarId.getTag
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
let target ← mvarId.getType
let u ← getLevel target
let eq ← mkEq target targetNew
let newProof ← mkExpectedTypeHint eqProof eq
let val := mkAppN (Lean.mkConst `Eq.mpr [u]) #[target, targetNew, newProof, mvarNew]
mvarId.assign val
return mvarNew.mvarId!
@[deprecated MVarId.replaceTargetEq]
def replaceTargetEq (mvarId : MVarId) (targetNew : Expr) (eqProof : Expr) : MetaM MVarId :=
mvarId.replaceTargetEq targetNew eqProof
/--
Convert the given goal `Ctx | target` into `Ctx |- targetNew`. It assumes the goals are definitionally equal.
We use the proof term
```
@id target mvarNew
```
to create a checkpoint. -/
def _root_.Lean.MVarId.replaceTargetDefEq (mvarId : MVarId) (targetNew : Expr) : MetaM MVarId :=
mvarId.withContext do
mvarId.checkNotAssigned `change
let target ← mvarId.getType
if target == targetNew then
return mvarId
else
let tag ← mvarId.getTag
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
let newVal ← mkExpectedTypeHint mvarNew target
mvarId.assign newVal
return mvarNew.mvarId!
@[deprecated MVarId.replaceTargetDefEq]
def replaceTargetDefEq (mvarId : MVarId) (targetNew : Expr) : MetaM MVarId :=
mvarId.replaceTargetDefEq targetNew
private def replaceLocalDeclCore (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (eqProof : Expr) : MetaM AssertAfterResult :=
mvarId.withContext do
let localDecl ← fvarId.getDecl
let typeNewPr ← mkEqMP eqProof (mkFVar fvarId)
-- `typeNew` may contain variables that occur after `fvarId`.
-- Thus, we use the auxiliary function `findMaxFVar` to ensure `typeNew` is well-formed at the position we are inserting it.
let (_, localDecl') ← findMaxFVar typeNew |>.run localDecl
let result ← mvarId.assertAfter localDecl'.fvarId localDecl.userName typeNew typeNewPr
(do let mvarIdNew ← result.mvarId.clear fvarId
pure { result with mvarId := mvarIdNew })
<|> pure result
where
findMaxFVar (e : Expr) : StateRefT LocalDecl MetaM Unit :=
e.forEach' fun e => do
if e.isFVar then
let localDecl' ← e.fvarId!.getDecl
modify fun localDecl => if localDecl'.index > localDecl.index then localDecl' else localDecl
return false
else
return e.hasFVar
/--
Replace type of the local declaration with id `fvarId` with one with the same user-facing name, but with type `typeNew`.
This method assumes `eqProof` is a proof that type of `fvarId` is equal to `typeNew`.
This tactic actually adds a new declaration and (try to) clear the old one.
If the old one cannot be cleared, then at least its user-facing name becomes inaccessible.
Remark: the new declaration is added immediately after `fvarId`.
`typeNew` must be well-formed at `fvarId`, but `eqProof` may contain variables declared after `fvarId`. -/
abbrev _root_.Lean.MVarId.replaceLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (eqProof : Expr) : MetaM AssertAfterResult :=
replaceLocalDeclCore mvarId fvarId typeNew eqProof
@[deprecated MVarId.replaceLocalDecl]
abbrev replaceLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (eqProof : Expr) : MetaM AssertAfterResult :=
mvarId.replaceLocalDecl fvarId typeNew eqProof
/--
Replace the type of `fvarId` at `mvarId` with `typeNew`.
Remark: this method assumes that `typeNew` is definitionally equal to the current type of `fvarId`.
-/
def _root_.Lean.MVarId.replaceLocalDeclDefEq (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) : MetaM MVarId := do
mvarId.withContext do
let mvarDecl ← mvarId.getDecl
if typeNew == mvarDecl.type then
return mvarId
else
let lctxNew := (← getLCtx).modifyLocalDecl fvarId (·.setType typeNew)
let mvarNew ← mkFreshExprMVarAt lctxNew (← getLocalInstances) mvarDecl.type mvarDecl.kind mvarDecl.userName
mvarId.assign mvarNew
return mvarNew.mvarId!
@[deprecated MVarId.replaceLocalDeclDefEq]
def replaceLocalDeclDefEq (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) : MetaM MVarId := do
mvarId.replaceLocalDeclDefEq fvarId typeNew
/--
Replace the target type of `mvarId` with `typeNew`.
If `checkDefEq = false`, this method assumes that `typeNew` is definitionally equal to the current target type.
If `checkDefEq = true`, throw an error if `typeNew` is not definitionally equal to the current target type.
-/
def _root_.Lean.MVarId.change (mvarId : MVarId) (targetNew : Expr) (checkDefEq := true) : MetaM MVarId := mvarId.withContext do
let target ← mvarId.getType
if checkDefEq then
unless (← isDefEq target targetNew) do
throwTacticEx `change mvarId m!"given type{indentExpr targetNew}\nis not definitionally equal to{indentExpr target}"
mvarId.replaceTargetDefEq targetNew
@[deprecated MVarId.change]
def change (mvarId : MVarId) (targetNew : Expr) (checkDefEq := true) : MetaM MVarId := mvarId.withContext do
mvarId.change targetNew checkDefEq
/--
Replace the type of the free variable `fvarId` with `typeNew`.
If `checkDefEq = false`, this method assumes that `typeNew` is definitionally equal to `fvarId` type.
If `checkDefEq = true`, throw an error if `typeNew` is not definitionally equal to `fvarId` type.
-/
def _root_.Lean.MVarId.changeLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (checkDefEq := true) : MetaM MVarId := do
mvarId.checkNotAssigned `changeLocalDecl
let (xs, mvarId) ← mvarId.revert #[fvarId] true
mvarId.withContext do
let numReverted := xs.size
let target ← mvarId.getType
let check (typeOld : Expr) : MetaM Unit := do
if checkDefEq then
unless (← isDefEq typeNew typeOld) do
throwTacticEx `changeHypothesis mvarId m!"given type{indentExpr typeNew}\nis not definitionally equal to{indentExpr typeOld}"
let finalize (targetNew : Expr) : MetaM MVarId := do
let mvarId ← mvarId.replaceTargetDefEq targetNew
let (_, mvarId) ← mvarId.introNP numReverted
pure mvarId
match target with
| .forallE n d b c => do check d; finalize (mkForall n c typeNew b)
| .letE n t v b _ => do check t; finalize (mkLet n typeNew v b)
| _ => throwTacticEx `changeHypothesis mvarId "unexpected auxiliary target"
@[deprecated MVarId.changeLocalDecl]
def changeLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (checkDefEq := true) : MetaM MVarId := do
mvarId.changeLocalDecl fvarId typeNew checkDefEq
/--
Modify `mvarId` target type using `f`.
-/
def _root_.Lean.MVarId.modifyTarget (mvarId : MVarId) (f : Expr → MetaM Expr) : MetaM MVarId := do
mvarId.withContext do
mvarId.checkNotAssigned `modifyTarget
mvarId.change (← f (← mvarId.getType)) (checkDefEq := false)
@[deprecated modifyTarget]
def modifyTarget (mvarId : MVarId) (f : Expr → MetaM Expr) : MetaM MVarId := do
mvarId.modifyTarget f
/--
Modify `mvarId` target type left-hand-side using `f`.
Throw an error if target type is not an equality.
-/
def _root_.Lean.MVarId.modifyTargetEqLHS (mvarId : MVarId) (f : Expr → MetaM Expr) : MetaM MVarId := do
mvarId.modifyTarget fun target => do
if let some (_, lhs, rhs) ← matchEq? target then
mkEq (← f lhs) rhs
else
throwTacticEx `modifyTargetEqLHS mvarId m!"equality expected{indentExpr target}"
@[deprecated MVarId.modifyTargetEqLHS]
def modifyTargetEqLHS (mvarId : MVarId) (f : Expr → MetaM Expr) : MetaM MVarId := do
mvarId.modifyTargetEqLHS f
end Lean.Meta
|
a7dcb7d2a0f0e11d6ced6bc31483df7adbe5a07c | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/matrix/bilinear_form.lean | 9e198c8f1b5b017421074d6f0f14847bf8921fdf | [
"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 | 22,668 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Kexing Ying
-/
import linear_algebra.matrix.basis
import linear_algebra.matrix.nondegenerate
import linear_algebra.matrix.nonsingular_inverse
import linear_algebra.matrix.to_linear_equiv
import linear_algebra.bilinear_form
import linear_algebra.matrix.sesquilinear_form
/-!
# Bilinear form
This file defines the conversion between bilinear forms and matrices.
## Main definitions
* `matrix.to_bilin` given a basis define a bilinear form
* `matrix.to_bilin'` define the bilinear form on `n → R`
* `bilin_form.to_matrix`: calculate the matrix coefficients of a bilinear form
* `bilin_form.to_matrix'`: calculate the matrix coefficients of a bilinear form on `n → R`
## Notations
In this file we use the following type variables:
- `M`, `M'`, ... are modules over the semiring `R`,
- `M₁`, `M₁'`, ... are modules over the ring `R₁`,
- `M₂`, `M₂'`, ... are modules over the commutative semiring `R₂`,
- `M₃`, `M₃'`, ... are modules over the commutative ring `R₃`,
- `V`, ... is a vector space over the field `K`.
## Tags
bilinear_form, matrix, basis
-/
variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M]
variables {R₁ : Type*} {M₁ : Type*} [ring R₁] [add_comm_group M₁] [module R₁ M₁]
variables {R₂ : Type*} {M₂ : Type*} [comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂]
variables {R₃ : Type*} {M₃ : Type*} [comm_ring R₃] [add_comm_group M₃] [module R₃ M₃]
variables {V : Type*} {K : Type*} [field K] [add_comm_group V] [module K V]
variables {B : bilin_form R M} {B₁ : bilin_form R₁ M₁} {B₂ : bilin_form R₂ M₂}
section matrix
variables {n o : Type*}
open_locale big_operators
open bilin_form finset linear_map matrix
open_locale matrix
/-- The map from `matrix n n R` to bilinear forms on `n → R`.
This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/
def matrix.to_bilin'_aux [fintype n] (M : matrix n n R₂) : bilin_form R₂ (n → R₂) :=
{ bilin := λ v w, ∑ i j, v i * M i j * w j,
bilin_add_left := λ x y z, by simp only [pi.add_apply, add_mul, sum_add_distrib],
bilin_smul_left := λ a x y, by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_sum],
bilin_add_right := λ x y z, by simp only [pi.add_apply, mul_add, sum_add_distrib],
bilin_smul_right := λ a x y,
by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_left_comm, mul_sum] }
lemma matrix.to_bilin'_aux_std_basis [fintype n] [decidable_eq n] (M : matrix n n R₂)
(i j : n) : M.to_bilin'_aux (std_basis R₂ (λ _, R₂) i 1) (std_basis R₂ (λ _, R₂) j 1) = M i j :=
begin
rw [matrix.to_bilin'_aux, coe_fn_mk, sum_eq_single i, sum_eq_single j],
{ simp only [std_basis_same, std_basis_same, one_mul, mul_one] },
{ rintros j' - hj',
apply mul_eq_zero_of_right,
exact std_basis_ne R₂ (λ _, R₂) _ _ hj' 1 },
{ intros,
have := finset.mem_univ j,
contradiction },
{ rintros i' - hi',
refine finset.sum_eq_zero (λ j _, _),
apply mul_eq_zero_of_left,
apply mul_eq_zero_of_left,
exact std_basis_ne R₂ (λ _, R₂) _ _ hi' 1 },
{ intros,
have := finset.mem_univ i,
contradiction }
end
/-- The linear map from bilinear forms to `matrix n n R` given an `n`-indexed basis.
This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/
def bilin_form.to_matrix_aux (b : n → M₂) : bilin_form R₂ M₂ →ₗ[R₂] matrix n n R₂ :=
{ to_fun := λ B, of $ λ i j, B (b i) (b j),
map_add' := λ f g, rfl,
map_smul' := λ f g, rfl }
@[simp] lemma bilin_form.to_matrix_aux_apply (B : bilin_form R₂ M₂) (b : n → M₂) (i j : n) :
bilin_form.to_matrix_aux b B i j = B (b i) (b j) := rfl
variables [fintype n] [fintype o]
lemma to_bilin'_aux_to_matrix_aux [decidable_eq n] (B₂ : bilin_form R₂ (n → R₂)) :
matrix.to_bilin'_aux (bilin_form.to_matrix_aux (λ j, std_basis R₂ (λ _, R₂) j 1) B₂) = B₂ :=
begin
refine ext_basis (pi.basis_fun R₂ n) (λ i j, _),
rw [pi.basis_fun_apply, pi.basis_fun_apply, matrix.to_bilin'_aux_std_basis,
bilin_form.to_matrix_aux_apply]
end
section to_matrix'
/-! ### `to_matrix'` section
This section deals with the conversion between matrices and bilinear forms on `n → R₂`.
-/
variables [decidable_eq n] [decidable_eq o]
/-- The linear equivalence between bilinear forms on `n → R` and `n × n` matrices -/
def bilin_form.to_matrix' : bilin_form R₂ (n → R₂) ≃ₗ[R₂] matrix n n R₂ :=
{ inv_fun := matrix.to_bilin'_aux,
left_inv := by convert to_bilin'_aux_to_matrix_aux,
right_inv := λ M,
by { ext i j, simp only [to_fun_eq_coe, bilin_form.to_matrix_aux_apply,
matrix.to_bilin'_aux_std_basis] },
..bilin_form.to_matrix_aux (λ j, std_basis R₂ (λ _, R₂) j 1) }
@[simp] lemma bilin_form.to_matrix_aux_std_basis (B : bilin_form R₂ (n → R₂)) :
bilin_form.to_matrix_aux (λ j, std_basis R₂ (λ _, R₂) j 1) B =
bilin_form.to_matrix' B :=
rfl
/-- The linear equivalence between `n × n` matrices and bilinear forms on `n → R` -/
def matrix.to_bilin' : matrix n n R₂ ≃ₗ[R₂] bilin_form R₂ (n → R₂) :=
bilin_form.to_matrix'.symm
@[simp] lemma matrix.to_bilin'_aux_eq (M : matrix n n R₂) :
matrix.to_bilin'_aux M = matrix.to_bilin' M :=
rfl
lemma matrix.to_bilin'_apply (M : matrix n n R₂) (x y : n → R₂) :
matrix.to_bilin' M x y = ∑ i j, x i * M i j * y j := rfl
lemma matrix.to_bilin'_apply' (M : matrix n n R₂) (v w : n → R₂) :
matrix.to_bilin' M v w = matrix.dot_product v (M.mul_vec w) :=
begin
simp_rw [matrix.to_bilin'_apply, matrix.dot_product,
matrix.mul_vec, matrix.dot_product],
refine finset.sum_congr rfl (λ _ _, _),
rw finset.mul_sum,
refine finset.sum_congr rfl (λ _ _, _),
rw ← mul_assoc,
end
@[simp] lemma matrix.to_bilin'_std_basis (M : matrix n n R₂) (i j : n) :
matrix.to_bilin' M (std_basis R₂ (λ _, R₂) i 1) (std_basis R₂ (λ _, R₂) j 1) =
M i j :=
matrix.to_bilin'_aux_std_basis M i j
@[simp] lemma bilin_form.to_matrix'_symm :
(bilin_form.to_matrix'.symm : matrix n n R₂ ≃ₗ[R₂] _) = matrix.to_bilin' :=
rfl
@[simp] lemma matrix.to_bilin'_symm :
(matrix.to_bilin'.symm : _ ≃ₗ[R₂] matrix n n R₂) = bilin_form.to_matrix' :=
bilin_form.to_matrix'.symm_symm
@[simp] lemma matrix.to_bilin'_to_matrix' (B : bilin_form R₂ (n → R₂)) :
matrix.to_bilin' (bilin_form.to_matrix' B) = B :=
matrix.to_bilin'.apply_symm_apply B
@[simp] lemma bilin_form.to_matrix'_to_bilin' (M : matrix n n R₂) :
bilin_form.to_matrix' (matrix.to_bilin' M) = M :=
bilin_form.to_matrix'.apply_symm_apply M
@[simp] lemma bilin_form.to_matrix'_apply (B : bilin_form R₂ (n → R₂)) (i j : n) :
bilin_form.to_matrix' B i j =
B (std_basis R₂ (λ _, R₂) i 1) (std_basis R₂ (λ _, R₂) j 1) :=
rfl
@[simp] lemma bilin_form.to_matrix'_comp (B : bilin_form R₂ (n → R₂))
(l r : (o → R₂) →ₗ[R₂] (n → R₂)) :
(B.comp l r).to_matrix' = l.to_matrix'ᵀ ⬝ B.to_matrix' ⬝ r.to_matrix' :=
begin
ext i j,
simp only [bilin_form.to_matrix'_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply,
linear_map.to_matrix', linear_equiv.coe_mk, sum_mul],
rw sum_comm,
conv_lhs { rw ← bilin_form.sum_repr_mul_repr_mul (pi.basis_fun R₂ n) (l _) (r _) },
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros i' -,
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros j' -,
simp only [smul_eq_mul, pi.basis_fun_repr, mul_assoc, mul_comm, mul_left_comm,
pi.basis_fun_apply, of_apply] },
{ intros, simp only [zero_smul, smul_zero] } },
{ intros, simp only [zero_smul, finsupp.sum_zero] }
end
lemma bilin_form.to_matrix'_comp_left (B : bilin_form R₂ (n → R₂))
(f : (n → R₂) →ₗ[R₂] (n → R₂)) : (B.comp_left f).to_matrix' = f.to_matrix'ᵀ ⬝ B.to_matrix' :=
by simp only [bilin_form.comp_left, bilin_form.to_matrix'_comp, to_matrix'_id, matrix.mul_one]
lemma bilin_form.to_matrix'_comp_right (B : bilin_form R₂ (n → R₂))
(f : (n → R₂) →ₗ[R₂] (n → R₂)) : (B.comp_right f).to_matrix' = B.to_matrix' ⬝ f.to_matrix' :=
by simp only [bilin_form.comp_right, bilin_form.to_matrix'_comp, to_matrix'_id,
transpose_one, matrix.one_mul]
lemma bilin_form.mul_to_matrix'_mul (B : bilin_form R₂ (n → R₂))
(M : matrix o n R₂) (N : matrix n o R₂) :
M ⬝ B.to_matrix' ⬝ N = (B.comp Mᵀ.to_lin' N.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp, transpose_transpose, to_matrix'_to_lin']
lemma bilin_form.mul_to_matrix' (B : bilin_form R₂ (n → R₂)) (M : matrix n n R₂) :
M ⬝ B.to_matrix' = (B.comp_left Mᵀ.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp_left, transpose_transpose, to_matrix'_to_lin']
lemma bilin_form.to_matrix'_mul (B : bilin_form R₂ (n → R₂)) (M : matrix n n R₂) :
B.to_matrix' ⬝ M = (B.comp_right M.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp_right, to_matrix'_to_lin']
lemma matrix.to_bilin'_comp (M : matrix n n R₂) (P Q : matrix n o R₂) :
M.to_bilin'.comp P.to_lin' Q.to_lin' = (Pᵀ ⬝ M ⬝ Q).to_bilin' :=
bilin_form.to_matrix'.injective
(by simp only [bilin_form.to_matrix'_comp, bilin_form.to_matrix'_to_bilin', to_matrix'_to_lin'])
end to_matrix'
section to_matrix
/-! ### `to_matrix` section
This section deals with the conversion between matrices and bilinear forms on
a module with a fixed basis.
-/
variables [decidable_eq n] (b : basis n R₂ M₂)
/-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and
`n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/
noncomputable def bilin_form.to_matrix : bilin_form R₂ M₂ ≃ₗ[R₂] matrix n n R₂ :=
(bilin_form.congr b.equiv_fun).trans bilin_form.to_matrix'
/-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and
`n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/
noncomputable def matrix.to_bilin : matrix n n R₂ ≃ₗ[R₂] bilin_form R₂ M₂ :=
(bilin_form.to_matrix b).symm
@[simp] lemma bilin_form.to_matrix_apply (B : bilin_form R₂ M₂) (i j : n) :
bilin_form.to_matrix b B i j = B (b i) (b j) :=
by rw [bilin_form.to_matrix, linear_equiv.trans_apply, bilin_form.to_matrix'_apply, congr_apply,
b.equiv_fun_symm_std_basis, b.equiv_fun_symm_std_basis]
@[simp] lemma matrix.to_bilin_apply (M : matrix n n R₂) (x y : M₂) :
matrix.to_bilin b M x y = ∑ i j, b.repr x i * M i j * b.repr y j :=
begin
rw [matrix.to_bilin, bilin_form.to_matrix, linear_equiv.symm_trans_apply, ← matrix.to_bilin'],
simp only [congr_symm, congr_apply, linear_equiv.symm_symm, matrix.to_bilin'_apply,
basis.equiv_fun_apply]
end
-- Not a `simp` lemma since `bilin_form.to_matrix` needs an extra argument
lemma bilinear_form.to_matrix_aux_eq (B : bilin_form R₂ M₂) :
bilin_form.to_matrix_aux b B = bilin_form.to_matrix b B :=
ext (λ i j, by rw [bilin_form.to_matrix_apply, bilin_form.to_matrix_aux_apply])
@[simp] lemma bilin_form.to_matrix_symm :
(bilin_form.to_matrix b).symm = matrix.to_bilin b :=
rfl
@[simp] lemma matrix.to_bilin_symm :
(matrix.to_bilin b).symm = bilin_form.to_matrix b :=
(bilin_form.to_matrix b).symm_symm
lemma matrix.to_bilin_basis_fun :
matrix.to_bilin (pi.basis_fun R₂ n) = matrix.to_bilin' :=
by { ext M, simp only [matrix.to_bilin_apply, matrix.to_bilin'_apply, pi.basis_fun_repr] }
lemma bilin_form.to_matrix_basis_fun :
bilin_form.to_matrix (pi.basis_fun R₂ n) = bilin_form.to_matrix' :=
by { ext B, rw [bilin_form.to_matrix_apply, bilin_form.to_matrix'_apply,
pi.basis_fun_apply, pi.basis_fun_apply] }
@[simp] lemma matrix.to_bilin_to_matrix (B : bilin_form R₂ M₂) :
matrix.to_bilin b (bilin_form.to_matrix b B) = B :=
(matrix.to_bilin b).apply_symm_apply B
@[simp] lemma bilin_form.to_matrix_to_bilin (M : matrix n n R₂) :
bilin_form.to_matrix b (matrix.to_bilin b M) = M :=
(bilin_form.to_matrix b).apply_symm_apply M
variables {M₂' : Type*} [add_comm_monoid M₂'] [module R₂ M₂']
variables (c : basis o R₂ M₂')
variables [decidable_eq o]
-- Cannot be a `simp` lemma because `b` must be inferred.
lemma bilin_form.to_matrix_comp
(B : bilin_form R₂ M₂) (l r : M₂' →ₗ[R₂] M₂) :
bilin_form.to_matrix c (B.comp l r) =
(to_matrix c b l)ᵀ ⬝ bilin_form.to_matrix b B ⬝ to_matrix c b r :=
begin
ext i j,
simp only [bilin_form.to_matrix_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply,
linear_map.to_matrix', linear_equiv.coe_mk, sum_mul],
rw sum_comm,
conv_lhs { rw ← bilin_form.sum_repr_mul_repr_mul b },
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros i' -,
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros j' -,
simp only [smul_eq_mul, linear_map.to_matrix_apply,
basis.equiv_fun_apply, mul_assoc, mul_comm, mul_left_comm] },
{ intros, simp only [zero_smul, smul_zero] } },
{ intros, simp only [zero_smul, finsupp.sum_zero] }
end
lemma bilin_form.to_matrix_comp_left (B : bilin_form R₂ M₂) (f : M₂ →ₗ[R₂] M₂) :
bilin_form.to_matrix b (B.comp_left f) = (to_matrix b b f)ᵀ ⬝ bilin_form.to_matrix b B :=
by simp only [comp_left, bilin_form.to_matrix_comp b b, to_matrix_id, matrix.mul_one]
lemma bilin_form.to_matrix_comp_right (B : bilin_form R₂ M₂) (f : M₂ →ₗ[R₂] M₂) :
bilin_form.to_matrix b (B.comp_right f) = bilin_form.to_matrix b B ⬝ (to_matrix b b f) :=
by simp only [bilin_form.comp_right, bilin_form.to_matrix_comp b b, to_matrix_id,
transpose_one, matrix.one_mul]
@[simp]
lemma bilin_form.to_matrix_mul_basis_to_matrix (c : basis o R₂ M₂) (B : bilin_form R₂ M₂) :
(b.to_matrix c)ᵀ ⬝ bilin_form.to_matrix b B ⬝ b.to_matrix c = bilin_form.to_matrix c B :=
by rw [← linear_map.to_matrix_id_eq_basis_to_matrix, ← bilin_form.to_matrix_comp,
bilin_form.comp_id_id]
lemma bilin_form.mul_to_matrix_mul (B : bilin_form R₂ M₂)
(M : matrix o n R₂) (N : matrix n o R₂) :
M ⬝ bilin_form.to_matrix b B ⬝ N =
bilin_form.to_matrix c (B.comp (to_lin c b Mᵀ) (to_lin c b N)) :=
by simp only [B.to_matrix_comp b c, to_matrix_to_lin, transpose_transpose]
lemma bilin_form.mul_to_matrix (B : bilin_form R₂ M₂) (M : matrix n n R₂) :
M ⬝ bilin_form.to_matrix b B =
bilin_form.to_matrix b (B.comp_left (to_lin b b Mᵀ)) :=
by rw [B.to_matrix_comp_left b, to_matrix_to_lin, transpose_transpose]
lemma bilin_form.to_matrix_mul (B : bilin_form R₂ M₂) (M : matrix n n R₂) :
bilin_form.to_matrix b B ⬝ M =
bilin_form.to_matrix b (B.comp_right (to_lin b b M)) :=
by rw [B.to_matrix_comp_right b, to_matrix_to_lin]
lemma matrix.to_bilin_comp (M : matrix n n R₂) (P Q : matrix n o R₂) :
(matrix.to_bilin b M).comp (to_lin c b P) (to_lin c b Q) = matrix.to_bilin c (Pᵀ ⬝ M ⬝ Q) :=
(bilin_form.to_matrix c).injective
(by simp only [bilin_form.to_matrix_comp b c, bilin_form.to_matrix_to_bilin, to_matrix_to_lin])
end to_matrix
end matrix
section matrix_adjoints
open_locale matrix
variables {n : Type*} [fintype n]
variables (b : basis n R₃ M₃)
variables (J J₃ A A' : matrix n n R₃)
@[simp] lemma is_adjoint_pair_to_bilin' [decidable_eq n] :
bilin_form.is_adjoint_pair (matrix.to_bilin' J) (matrix.to_bilin' J₃)
(matrix.to_lin' A) (matrix.to_lin' A') ↔
matrix.is_adjoint_pair J J₃ A A' :=
begin
rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right,
have h : ∀ (B B' : bilin_form R₃ (n → R₃)), B = B' ↔
(bilin_form.to_matrix' B) = (bilin_form.to_matrix' B'),
{ intros B B',
split; intros h,
{ rw h },
{ exact bilin_form.to_matrix'.injective h } },
rw [h, bilin_form.to_matrix'_comp_left, bilin_form.to_matrix'_comp_right,
linear_map.to_matrix'_to_lin', linear_map.to_matrix'_to_lin',
bilin_form.to_matrix'_to_bilin', bilin_form.to_matrix'_to_bilin'],
refl,
end
@[simp] lemma is_adjoint_pair_to_bilin [decidable_eq n] :
bilin_form.is_adjoint_pair (matrix.to_bilin b J) (matrix.to_bilin b J₃)
(matrix.to_lin b b A) (matrix.to_lin b b A') ↔
matrix.is_adjoint_pair J J₃ A A' :=
begin
rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right,
have h : ∀ (B B' : bilin_form R₃ M₃), B = B' ↔
(bilin_form.to_matrix b B) = (bilin_form.to_matrix b B'),
{ intros B B',
split; intros h,
{ rw h },
{ exact (bilin_form.to_matrix b).injective h } },
rw [h, bilin_form.to_matrix_comp_left, bilin_form.to_matrix_comp_right,
linear_map.to_matrix_to_lin, linear_map.to_matrix_to_lin,
bilin_form.to_matrix_to_bilin, bilin_form.to_matrix_to_bilin],
refl,
end
lemma matrix.is_adjoint_pair_equiv' [decidable_eq n] (P : matrix n n R₃) (h : is_unit P) :
(Pᵀ ⬝ J ⬝ P).is_adjoint_pair (Pᵀ ⬝ J ⬝ P) A A' ↔
J.is_adjoint_pair J (P ⬝ A ⬝ P⁻¹) (P ⬝ A' ⬝ P⁻¹) :=
have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h,
begin
let u := P.nonsing_inv_unit h',
let v := Pᵀ.nonsing_inv_unit (P.is_unit_det_transpose h'),
let x := Aᵀ * Pᵀ * J,
let y := J * P * A',
suffices : x * ↑u = ↑v * y ↔ ↑v⁻¹ * x = y * ↑u⁻¹,
{ dunfold matrix.is_adjoint_pair,
repeat { rw matrix.transpose_mul, },
simp only [←matrix.mul_eq_mul, ←mul_assoc, P.transpose_nonsing_inv],
conv_lhs { to_rhs, rw [mul_assoc, mul_assoc], congr, skip, rw ←mul_assoc, },
conv_rhs { rw [mul_assoc, mul_assoc], conv { to_lhs, congr, skip, rw ←mul_assoc }, },
exact this, },
rw units.eq_mul_inv_iff_mul_eq, conv_rhs { rw mul_assoc, }, rw v.inv_mul_eq_iff_eq_mul,
end
variables [decidable_eq n]
/-- The submodule of pair-self-adjoint matrices with respect to bilinear forms corresponding to
given matrices `J`, `J₂`. -/
def pair_self_adjoint_matrices_submodule' : submodule R₃ (matrix n n R₃) :=
(bilin_form.is_pair_self_adjoint_submodule (matrix.to_bilin' J) (matrix.to_bilin' J₃)).map
((linear_map.to_matrix' : ((n → R₃) →ₗ[R₃] (n → R₃)) ≃ₗ[R₃] matrix n n R₃) :
((n → R₃) →ₗ[R₃] (n → R₃)) →ₗ[R₃] matrix n n R₃)
lemma mem_pair_self_adjoint_matrices_submodule' :
A ∈ (pair_self_adjoint_matrices_submodule J J₃) ↔ matrix.is_adjoint_pair J J₃ A A :=
by simp only [mem_pair_self_adjoint_matrices_submodule]
/-- The submodule of self-adjoint matrices with respect to the bilinear form corresponding to
the matrix `J`. -/
def self_adjoint_matrices_submodule' : submodule R₃ (matrix n n R₃) :=
pair_self_adjoint_matrices_submodule J J
lemma mem_self_adjoint_matrices_submodule' :
A ∈ self_adjoint_matrices_submodule J ↔ J.is_self_adjoint A :=
by simp only [mem_self_adjoint_matrices_submodule]
/-- The submodule of skew-adjoint matrices with respect to the bilinear form corresponding to
the matrix `J`. -/
def skew_adjoint_matrices_submodule' : submodule R₃ (matrix n n R₃) :=
pair_self_adjoint_matrices_submodule (-J) J
lemma mem_skew_adjoint_matrices_submodule' :
A ∈ skew_adjoint_matrices_submodule J ↔ J.is_skew_adjoint A :=
by simp only [mem_skew_adjoint_matrices_submodule]
end matrix_adjoints
namespace bilin_form
section det
open matrix
variables {A : Type*} [comm_ring A] [is_domain A] [module A M₃] (B₃ : bilin_form A M₃)
variables {ι : Type*} [decidable_eq ι] [fintype ι]
lemma _root_.matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin {M : matrix ι ι R₂}
(b : basis ι R₂ M₂) : M.to_bilin'.nondegenerate ↔ (matrix.to_bilin b M).nondegenerate :=
(nondegenerate_congr_iff b.equiv_fun.symm).symm
-- Lemmas transferring nondegeneracy between a matrix and its associated bilinear form
theorem _root_.matrix.nondegenerate.to_bilin' {M : matrix ι ι R₃} (h : M.nondegenerate) :
M.to_bilin'.nondegenerate :=
λ x hx, h.eq_zero_of_ortho $ λ y, by simpa only [to_bilin'_apply'] using hx y
@[simp] lemma _root_.matrix.nondegenerate_to_bilin'_iff {M : matrix ι ι R₃} :
M.to_bilin'.nondegenerate ↔ M.nondegenerate :=
⟨λ h v hv, h v $ λ w, (M.to_bilin'_apply' _ _).trans $ hv w, matrix.nondegenerate.to_bilin'⟩
theorem _root_.matrix.nondegenerate.to_bilin {M : matrix ι ι R₃} (h : M.nondegenerate)
(b : basis ι R₃ M₃) : (to_bilin b M).nondegenerate :=
(matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin b).mp h.to_bilin'
@[simp] lemma _root_.matrix.nondegenerate_to_bilin_iff {M : matrix ι ι R₃} (b : basis ι R₃ M₃) :
(to_bilin b M).nondegenerate ↔ M.nondegenerate :=
by rw [←matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin,
matrix.nondegenerate_to_bilin'_iff]
-- Lemmas transferring nondegeneracy between a bilinear form and its associated matrix
@[simp] theorem nondegenerate_to_matrix'_iff {B : bilin_form R₃ (ι → R₃)} :
B.to_matrix'.nondegenerate ↔ B.nondegenerate :=
matrix.nondegenerate_to_bilin'_iff.symm.trans $ (matrix.to_bilin'_to_matrix' B).symm ▸ iff.rfl
theorem nondegenerate.to_matrix' {B : bilin_form R₃ (ι → R₃)} (h : B.nondegenerate) :
B.to_matrix'.nondegenerate :=
nondegenerate_to_matrix'_iff.mpr h
@[simp] theorem nondegenerate_to_matrix_iff {B : bilin_form R₃ M₃} (b : basis ι R₃ M₃) :
(to_matrix b B).nondegenerate ↔ B.nondegenerate :=
(matrix.nondegenerate_to_bilin_iff b).symm.trans $ (matrix.to_bilin_to_matrix b B).symm ▸ iff.rfl
theorem nondegenerate.to_matrix {B : bilin_form R₃ M₃} (h : B.nondegenerate)
(b : basis ι R₃ M₃) : (to_matrix b B).nondegenerate :=
(nondegenerate_to_matrix_iff b).mpr h
-- Some shorthands for combining the above with `matrix.nondegenerate_of_det_ne_zero`
lemma nondegenerate_to_bilin'_iff_det_ne_zero {M : matrix ι ι A} :
M.to_bilin'.nondegenerate ↔ M.det ≠ 0 :=
by rw [matrix.nondegenerate_to_bilin'_iff, matrix.nondegenerate_iff_det_ne_zero]
theorem nondegenerate_to_bilin'_of_det_ne_zero' (M : matrix ι ι A) (h : M.det ≠ 0) :
M.to_bilin'.nondegenerate :=
nondegenerate_to_bilin'_iff_det_ne_zero.mpr h
lemma nondegenerate_iff_det_ne_zero {B : bilin_form A M₃}
(b : basis ι A M₃) : B.nondegenerate ↔ (to_matrix b B).det ≠ 0 :=
by rw [←matrix.nondegenerate_iff_det_ne_zero, nondegenerate_to_matrix_iff]
theorem nondegenerate_of_det_ne_zero (b : basis ι A M₃) (h : (to_matrix b B₃).det ≠ 0) :
B₃.nondegenerate :=
(nondegenerate_iff_det_ne_zero b).mpr h
end det
end bilin_form
|
c6e25623a3e7e6e3aab18cb1810b1ed8dbcc6abe | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/type_error_at_eval_expr.lean | 641fa2500f9bf0589f327ddf13e8640d7a034cd9 | [
"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 | 88 | lean | open tactic
run_cmd do
e ← to_expr ``([5] : list ℕ),
eval_expr ℕ e,
return ()
|
0b843202c699bfd2ddf59ea3cde24fc60abb05f6 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/linear_algebra/matrix.lean | fce5b94e24b8bbb5cd2dea453ac6174470e2e23c | [
"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 | 25,189 | 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, Casper Putz
-/
import linear_algebra.finite_dimensional
import linear_algebra.nonsingular_inverse
/-!
# Linear maps and matrices
This file defines the maps to send matrices to a linear map,
and to send linear maps between modules with a finite bases
to matrices. This defines a linear equivalence between linear maps
between finite-dimensional vector spaces and matrices indexed by
the respective bases.
Some results are proved about the linear map corresponding to a
diagonal matrix (range, ker and rank).
## Main definitions
to_lin, to_matrix, linear_equiv_matrix
## Tags
linear_map, matrix, linear_equiv, diagonal
-/
noncomputable theory
open set submodule
open_locale big_operators
universes u v w
variables {l m n : Type u} [fintype l] [fintype m] [fintype n]
namespace matrix
variables {R : Type v} [comm_ring R]
instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) :=
by unfold matrix; apply_instance
/-- Evaluation of matrices gives a linear map from matrix m n R to
linear maps (n → R) →ₗ[R] (m → R). -/
def eval : (matrix m n R) →ₗ[R] ((n → R) →ₗ[R] (m → R)) :=
begin
refine linear_map.mk₂ R mul_vec _ _ _ _,
{ assume M N v, funext x,
change ∑ y : n, (M x y + N x y) * v y = _,
simp only [_root_.add_mul, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change ∑ y : n, (c * M x y) * v y = _,
simp only [_root_.mul_assoc, finset.mul_sum.symm],
refl },
{ assume M v w, funext x,
change ∑ y : n, M x y * (v y + w y) = _,
simp [_root_.mul_add, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change ∑ y : n, M x y * (c * v y) = _,
rw [show (λy:n, M x y * (c * v y)) = (λy:n, c * (M x y * v y)), { funext n, ac_refl },
← finset.mul_sum],
refl }
end
/-- Evaluation of matrices gives a map from matrix m n R to
linear maps (n → R) →ₗ[R] (m → R). -/
def to_lin : matrix m n R → (n → R) →ₗ[R] (m → R) := eval.to_fun
theorem to_lin_of_equiv {p q : Type*} [fintype p] [fintype q] (e₁ : m ≃ p) (e₂ : n ≃ q)
(f : matrix p q R) : to_lin (λ i j, f (e₁ i) (e₂ j) : matrix m n R) =
linear_equiv.arrow_congr
(linear_map.fun_congr_left R R e₂)
(linear_map.fun_congr_left R R e₁)
(to_lin f) :=
linear_map.ext $ λ v, funext $ λ i,
calc ∑ j : n, f (e₁ i) (e₂ j) * v j
= ∑ j : n, f (e₁ i) (e₂ j) * v (e₂.symm (e₂ j)) : by simp_rw e₂.symm_apply_apply
... = ∑ k : q, f (e₁ i) k * v (e₂.symm k) : finset.sum_equiv e₂ (λ k, f (e₁ i) k * v (e₂.symm k))
lemma to_lin_add (M N : matrix m n R) : (M + N).to_lin = M.to_lin + N.to_lin :=
matrix.eval.map_add M N
@[simp] lemma to_lin_zero : (0 : matrix m n R).to_lin = 0 :=
matrix.eval.map_zero
@[simp] lemma to_lin_neg (M : matrix m n R) : (-M).to_lin = -M.to_lin :=
@linear_map.map_neg _ _ ((n → R) →ₗ[R] m → R) _ _ _ _ _ matrix.eval M
instance to_lin.is_add_monoid_hom :
@is_add_monoid_hom (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ to_lin :=
{ map_zero := to_lin_zero, map_add := to_lin_add }
@[simp] lemma to_lin_apply (M : matrix m n R) (v : n → R) :
(M.to_lin : (n → R) → (m → R)) v = mul_vec M v := rfl
lemma mul_to_lin (M : matrix m n R) (N : matrix n l R) :
(M.mul N).to_lin = M.to_lin.comp N.to_lin :=
by { ext, simp }
@[simp] lemma to_lin_one [decidable_eq n] : (1 : matrix n n R).to_lin = linear_map.id :=
by { ext, simp }
end matrix
namespace linear_map
variables {R : Type v} [comm_ring R]
/-- The linear map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/
def to_matrixₗ [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) →ₗ[R] matrix m n R :=
begin
refine linear_map.mk (λ f i j, f (λ n, ite (j = n) 1 0) i) _ _,
{ assume f g, simp only [add_apply], refl },
{ assume f g, simp only [smul_apply], refl }
end
/-- The map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/
def to_matrix [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) → matrix m n R := to_matrixₗ.to_fun
@[simp] lemma to_matrix_id [decidable_eq n] :
(@linear_map.id _ (n → R) _ _ _).to_matrix = 1 :=
by { ext, simp [to_matrix, to_matrixₗ, matrix.one_val, eq_comm] }
theorem to_matrix_of_equiv {p q : Type*} [fintype p] [fintype q] [decidable_eq n] [decidable_eq q]
(e₁ : m ≃ p) (e₂ : n ≃ q) (f : (q → R) →ₗ[R] (p → R)) (i j) :
to_matrix f (e₁ i) (e₂ j) = to_matrix (linear_equiv.arrow_congr
(linear_map.fun_congr_left R R e₂)
(linear_map.fun_congr_left R R e₁) f) i j :=
show f (λ k : q, ite (e₂ j = k) 1 0) (e₁ i) = f (λ k : q, ite (j = e₂.symm k) 1 0) (e₁ i),
by { congr' 1, ext, congr' 1, rw equiv.eq_symm_apply }
end linear_map
section linear_equiv_matrix
variables {R : Type v} [comm_ring R] [decidable_eq n]
open finsupp matrix linear_map
/-- to_lin is the left inverse of to_matrix. -/
lemma to_matrix_to_lin {f : (n → R) →ₗ[R] (m → R)} :
to_lin (to_matrix f) = f :=
begin
ext : 1,
-- Show that the two sides are equal by showing that they are equal on a basis
convert linear_eq_on (set.range _) _ (is_basis.mem_span (@pi.is_basis_fun R n _ _) _),
assume e he,
rw [@std_basis_eq_single R _ _ _ 1] at he,
cases (set.mem_range.mp he) with i h,
ext j,
change ∑ k, (f (λ l, ite (k = l) 1 0)) j * (e k) = _,
rw [←h],
conv_lhs { congr, skip, funext,
rw [mul_comm, ←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul],
rw [show _ = ite (i = k) (1:R) 0, by convert single_apply],
rw [show f (ite (i = k) (1:R) 0 • (λ l, ite (k = l) 1 0)) = ite (i = k) (f _) 0,
{ split_ifs, { rw [one_smul] }, { rw [zero_smul], exact linear_map.map_zero f } }] },
convert finset.sum_eq_single i _ _,
{ rw [if_pos rfl], convert rfl, ext, congr },
{ assume _ _ hbi, rw [if_neg $ ne.symm hbi], refl },
{ assume hi, exact false.elim (hi $ finset.mem_univ i) }
end
/-- to_lin is the right inverse of to_matrix. -/
lemma to_lin_to_matrix {M : matrix m n R} : to_matrix (to_lin M) = M :=
begin
ext,
change ∑ y, M i y * ite (j = y) 1 0 = M i j,
have h1 : (λ y, M i y * ite (j = y) 1 0) = (λ y, ite (j = y) (M i y) 0),
{ ext, split_ifs, exact mul_one _, exact mul_zero _ },
have h2 : ∑ y, ite (j = y) (M i y) 0 = ∑ y in {j}, ite (j = y) (M i y) 0,
{ refine (finset.sum_subset _ _).symm,
{ intros _ H, rwa finset.mem_singleton.1 H, exact finset.mem_univ _ },
{ exact λ _ _ H, if_neg (mt (finset.mem_singleton.2 ∘ eq.symm) H) } },
rw [h1, h2, finset.sum_singleton],
exact if_pos rfl
end
/-- Linear maps (n → R) →ₗ[R] (m → R) are linearly equivalent to matrix m n R. -/
def linear_equiv_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R :=
{ to_fun := to_matrix,
inv_fun := to_lin,
right_inv := λ _, to_lin_to_matrix,
left_inv := λ _, to_matrix_to_lin,
map_add' := to_matrixₗ.map_add,
map_smul' := to_matrixₗ.map_smul }
@[simp] lemma linear_equiv_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) :
linear_equiv_matrix' f = to_matrix f := rfl
/-- Given a basis of two modules M₁ and M₂ over a commutative ring R, we get a linear equivalence
between linear maps M₁ →ₗ M₂ and matrices over R indexed by the bases. -/
def linear_equiv_matrix {ι κ M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁]
[add_comm_group M₂] [module R M₂]
[fintype ι] [decidable_eq ι] [fintype κ]
{v₁ : ι → M₁} {v₂ : κ → M₂} (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) :
(M₁ →ₗ[R] M₂) ≃ₗ[R] matrix κ ι R :=
linear_equiv.trans (linear_equiv.arrow_congr (equiv_fun_basis hv₁) (equiv_fun_basis hv₂)) linear_equiv_matrix'
open_locale classical
theorem linear_equiv_matrix_range {ι κ M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁]
[add_comm_group M₂] [module R M₂]
[fintype ι] [fintype κ]
{v₁ : ι → M₁} {v₂ : κ → M₂} (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) (f : M₁ →ₗ[R] M₂) (k i) :
linear_equiv_matrix hv₁.range hv₂.range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ =
linear_equiv_matrix hv₁ hv₂ f k i :=
if H : (0 : R) = 1 then eq_of_zero_eq_one H _ _ else
begin
simp_rw [linear_equiv_matrix, linear_equiv.trans_apply, linear_equiv_matrix'_apply,
← equiv.of_injective_apply _ (hv₁.injective H), ← equiv.of_injective_apply _ (hv₂.injective H),
to_matrix_of_equiv, ← linear_equiv.trans_apply, linear_equiv.arrow_congr_trans], congr' 3;
refine function.left_inverse.injective linear_equiv.symm_symm _; ext x;
simp_rw [linear_equiv.symm_trans_apply, equiv_fun_basis_symm_apply, fun_congr_left_symm,
fun_congr_left_apply, fun_left_apply],
convert (finset.sum_equiv (equiv.of_injective _ (hv₁.injective H)) _).symm,
simp_rw [equiv.symm_apply_apply, equiv.of_injective_apply, subtype.coe_mk],
convert (finset.sum_equiv (equiv.of_injective _ (hv₂.injective H)) _).symm,
simp_rw [equiv.symm_apply_apply, equiv.of_injective_apply, subtype.coe_mk]
end
end linear_equiv_matrix
namespace matrix
open_locale matrix
lemma comp_to_matrix_mul {R : Type v} [comm_ring R] [decidable_eq l] [decidable_eq m]
(f : (m → R) →ₗ[R] (n → R)) (g : (l → R) →ₗ[R] (m → R)) :
(f.comp g).to_matrix = f.to_matrix ⬝ g.to_matrix :=
suffices (f.comp g) = (f.to_matrix ⬝ g.to_matrix).to_lin, by rw [this, to_lin_to_matrix],
by rw [mul_to_lin, to_matrix_to_lin, to_matrix_to_lin]
lemma linear_equiv_matrix_comp {R ι κ μ M₁ M₂ M₃ : Type*} [comm_ring R]
[add_comm_group M₁] [module R M₁]
[add_comm_group M₂] [module R M₂]
[add_comm_group M₃] [module R M₃]
[fintype ι] [decidable_eq ι] [fintype κ] [decidable_eq κ] [fintype μ]
{v₁ : ι → M₁} {v₂ : κ → M₂} {v₃ : μ → M₃}
(hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) (hv₃ : is_basis R v₃)
(f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) :
linear_equiv_matrix hv₁ hv₃ (f.comp g) =
linear_equiv_matrix hv₂ hv₃ f ⬝ linear_equiv_matrix hv₁ hv₂ g :=
by simp_rw [linear_equiv_matrix, linear_equiv.trans_apply, linear_equiv_matrix'_apply,
linear_equiv.arrow_congr_comp _ (equiv_fun_basis hv₂), comp_to_matrix_mul]
lemma linear_equiv_matrix_mul {R M ι : Type*} [comm_ring R]
[add_comm_group M] [module R M] [fintype ι] [decidable_eq ι]
{b : ι → M} (hb : is_basis R b) (f g : M →ₗ[R] M) :
linear_equiv_matrix hb hb (f * g) = linear_equiv_matrix hb hb f * linear_equiv_matrix hb hb g :=
linear_equiv_matrix_comp hb hb hb f g
section trace
variables (n) (R : Type v) (M : Type w) [semiring R] [add_comm_monoid M] [semimodule R M]
/--
The diagonal of a square matrix.
-/
def diag : (matrix n n M) →ₗ[R] n → M :=
{ to_fun := λ A i, A i i,
map_add' := by { intros, ext, refl, },
map_smul' := by { intros, ext, refl, } }
variables {n} {R} {M}
@[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl
@[simp] lemma diag_one [decidable_eq n] :
diag n R R 1 = λ i, 1 := by { dunfold diag, ext, simp [one_val_eq] }
@[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aᵀ = diag n R M A := rfl
variables (n) (R) (M)
/--
The trace of a square matrix.
-/
def trace : (matrix n n M) →ₗ[R] M :=
{ to_fun := λ A, ∑ i, diag n R M A i,
map_add' := by { intros, apply finset.sum_add_distrib, },
map_smul' := by { intros, simp [finset.smul_sum], } }
variables {n} {R} {M}
@[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = ∑ i, diag n R M A i := rfl
@[simp] lemma trace_one [decidable_eq n] :
trace n R R 1 = fintype.card n :=
have h : trace n R R 1 = ∑ i, diag n R R 1 i := rfl,
by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl
@[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aᵀ = trace n R M A := rfl
@[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) :
trace n R R (Aᵀ ⬝ Bᵀ) = trace m R R (A ⬝ B) := finset.sum_comm
lemma trace_mul_comm {S : Type v} [comm_ring S] (A : matrix m n S) (B : matrix n m S) :
trace n S S (B ⬝ A) = trace m S S (A ⬝ B) :=
by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul]
end trace
section ring
variables {R : Type v} [comm_ring R] [decidable_eq n]
open linear_map matrix
lemma proj_diagonal (i : n) (w : n → R) :
(proj i).comp (to_lin (diagonal w)) = (w i) • proj i :=
by ext j; simp [mul_vec_diagonal]
lemma diagonal_comp_std_basis (w : n → R) (i : n) :
(diagonal w).to_lin.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i :=
begin
ext a j,
simp only [linear_map.comp_apply, smul_apply, to_lin_apply, mul_vec_diagonal, smul_apply,
pi.smul_apply, smul_eq_mul],
by_cases i = j,
{ subst h },
{ rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] }
end
lemma diagonal_to_lin (w : n → R) :
(diagonal w).to_lin = linear_map.pi (λi, w i • linear_map.proj i) :=
by ext v j; simp [mul_vec_diagonal]
/-- An invertible matrix yields a linear equivalence from the free module to itself. -/
def to_linear_equiv (P : matrix n n R) (h : is_unit P) : (n → R) ≃ₗ[R] (n → R) :=
have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h,
{ inv_fun := P⁻¹.to_lin,
left_inv := λ v,
show (P⁻¹.to_lin.comp P.to_lin) v = v,
by rw [←matrix.mul_to_lin, P.nonsing_inv_mul h', matrix.to_lin_one, linear_map.id_apply],
right_inv := λ v,
show (P.to_lin.comp P⁻¹.to_lin) v = v,
by rw [←matrix.mul_to_lin, P.mul_nonsing_inv h', matrix.to_lin_one, linear_map.id_apply],
..P.to_lin }
@[simp] lemma to_linear_equiv_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv h) : module.End R (n → R)) = P.to_lin := rfl
@[simp] lemma to_linear_equiv_symm_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv h).symm : module.End R (n → R)) = P⁻¹.to_lin := rfl
end ring
section vector_space
variables {K : Type u} [field K] -- maybe try to relax the universe constraint
open linear_map matrix
lemma rank_vec_mul_vec (w : m → K) (v : n → K) :
rank (vec_mul_vec w v).to_lin ≤ 1 :=
begin
rw [vec_mul_vec_eq, mul_to_lin],
refine le_trans (rank_comp_le1 _ _) _,
refine le_trans (rank_le_domain _) _,
rw [dim_fun', ← cardinal.fintype_card],
exact le_refl _
end
lemma ker_diagonal_to_lin [decidable_eq m] (w : m → K) :
ker (diagonal w).to_lin = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) :=
begin
rw [← comap_bot, ← infi_ker_proj],
simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'],
have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self },
exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K)
(disjoint_compl {i | w i = 0}) this (finite.of_fintype _)).symm
end
lemma range_diagonal [decidable_eq m] (w : m → K) :
(diagonal w).to_lin.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) :=
begin
dsimp only [mem_set_of_eq],
rw [← map_top, ← supr_range_std_basis, map_supr],
congr, funext i,
rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul']
end
lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) :
rank (diagonal w).to_lin = fintype.card { i // w i ≠ 0 } :=
begin
have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self },
have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl {i | w i = 0}).symm,
have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _),
have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu,
rw [rank, range_diagonal, h₁, ←@dim_fun' K],
apply linear_equiv.dim_eq,
apply h₂,
end
end vector_space
section finite_dimensional
variables {R : Type v} [field R]
instance : finite_dimensional R (matrix m n R) :=
linear_equiv.finite_dimensional (linear_equiv.uncurry R m n).symm
/--
The dimension of the space of finite dimensional matrices
is the product of the number of rows and columns.
-/
@[simp] lemma findim_matrix :
finite_dimensional.findim R (matrix m n R) = fintype.card m * fintype.card n :=
by rw [@linear_equiv.findim_eq R (matrix m n R) _ _ _ _ _ _ (linear_equiv.uncurry R m n),
finite_dimensional.findim_fintype_fun_eq_card, fintype.card_prod]
end finite_dimensional
section reindexing
variables {l' m' n' : Type w} [fintype l'] [fintype m'] [fintype n']
variables {R : Type v}
/-- The natural map that reindexes a matrix's rows and columns with equivalent types is an
equivalence. -/
def reindex (eₘ : m ≃ m') (eₙ : n ≃ n') : matrix m n R ≃ matrix m' n' R :=
{ to_fun := λ M i j, M (eₘ.symm i) (eₙ.symm j),
inv_fun := λ M i j, M (eₘ i) (eₙ j),
left_inv := λ M, by simp,
right_inv := λ M, by simp, }
@[simp] lemma reindex_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
reindex eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) :=
rfl
@[simp] lemma reindex_symm_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) :
(reindex eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) :=
rfl
/-- The natural map that reindexes a matrix's rows and columns with equivalent types is a linear
equivalence. -/
def reindex_linear_equiv [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') :
matrix m n R ≃ₗ[R] matrix m' n' R :=
{ map_add' := λ M N, rfl,
map_smul' := λ M N, rfl,
..(reindex eₘ eₙ)}
@[simp] lemma reindex_linear_equiv_apply [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
reindex_linear_equiv eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) :=
rfl
@[simp] lemma reindex_linear_equiv_symm_apply [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) :
(reindex_linear_equiv eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) :=
rfl
lemma reindex_mul [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (eₗ : l ≃ l') (M : matrix m n R) (N : matrix n l R) :
(reindex_linear_equiv eₘ eₙ M) ⬝ (reindex_linear_equiv eₙ eₗ N) = reindex_linear_equiv eₘ eₗ (M ⬝ N) :=
begin
ext i j,
dsimp only [matrix.mul, matrix.dot_product],
rw [←finset.univ_map_equiv_to_embedding eₙ, finset.sum_map finset.univ eₙ.to_embedding],
simp,
end
/-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent
types is an equivalence of algebras. -/
def reindex_alg_equiv [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R :=
{ map_mul' := λ M N, by simp only [reindex_mul, linear_equiv.to_fun_apply, mul_eq_mul],
commutes' := λ r, by { ext, simp [algebra_map, algebra.to_ring_hom], by_cases h : i = j; simp [h], },
..(reindex_linear_equiv e e) }
@[simp] lemma reindex_alg_equiv_apply [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) (M : matrix m m R) :
reindex_alg_equiv e M = λ i j, M (e.symm i) (e.symm j) :=
rfl
@[simp] lemma reindex_alg_equiv_symm_apply [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) (M : matrix n n R) :
(reindex_alg_equiv e).symm M = λ i j, M (e i) (e j) :=
rfl
end reindexing
end matrix
namespace linear_map
open_locale matrix
/-- The trace of an endomorphism given a basis. -/
def trace_aux (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b) :
(M →ₗ[R] M) →ₗ[R] R :=
(matrix.trace ι R R).comp $ linear_equiv_matrix hb hb
@[simp] lemma trace_aux_def (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) :
trace_aux R hb f = matrix.trace ι R R (linear_equiv_matrix hb hb f) :=
rfl
theorem trace_aux_eq' (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b)
{κ : Type w} [fintype κ] [decidable_eq κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
linear_map.ext $ λ f,
calc matrix.trace ι R R (linear_equiv_matrix hb hb f)
= matrix.trace ι R R (linear_equiv_matrix hb hb ((linear_map.id.comp f).comp linear_map.id)) :
by rw [linear_map.id_comp, linear_map.comp_id]
... = matrix.trace ι R R (linear_equiv_matrix hc hb linear_map.id ⬝
linear_equiv_matrix hc hc f ⬝
linear_equiv_matrix hb hc linear_map.id) :
by rw [matrix.linear_equiv_matrix_comp _ hc, matrix.linear_equiv_matrix_comp _ hc]
... = matrix.trace κ R R (linear_equiv_matrix hc hc f ⬝
linear_equiv_matrix hb hc linear_map.id ⬝
linear_equiv_matrix hc hb linear_map.id) :
by rw [matrix.mul_assoc, matrix.trace_mul_comm]
... = matrix.trace κ R R (linear_equiv_matrix hc hc ((f.comp linear_map.id).comp linear_map.id)) :
by rw [matrix.linear_equiv_matrix_comp _ hb, matrix.linear_equiv_matrix_comp _ hc]
... = matrix.trace κ R R (linear_equiv_matrix hc hc f) :
by rw [linear_map.comp_id, linear_map.comp_id]
open_locale classical
theorem trace_aux_range (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] {b : ι → M} (hb : is_basis R b) :
trace_aux R hb.range = trace_aux R hb :=
linear_map.ext $ λ f, if H : 0 = 1 then eq_of_zero_eq_one H _ _ else
begin
change ∑ i : set.range b, _ = ∑ i : ι, _, simp_rw [matrix.diag_apply], symmetry,
convert finset.sum_equiv (equiv.of_injective _ $ hb.injective H) _, ext i,
exact (linear_equiv_matrix_range hb hb f i i).symm
end
/-- where `ι` and `κ` can reside in different universes -/
theorem trace_aux_eq (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type*} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b)
{κ : Type*} [fintype κ] [decidable_eq κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
calc trace_aux R hb
= trace_aux R hb.range : by { rw trace_aux_range R hb, congr }
... = trace_aux R hc.range : trace_aux_eq' _ _ _
... = trace_aux R hc : by { rw trace_aux_range R hc, congr }
/-- Trace of an endomorphism independent of basis. -/
def trace (R : Type u) [comm_ring R] (M : Type v) [add_comm_group M] [module R M] :
(M →ₗ[R] M) →ₗ[R] R :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M)
then trace_aux R (classical.some_spec H)
else 0
theorem trace_eq_matrix_trace (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [fintype ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) :
trace R M f = matrix.trace ι R R (linear_equiv_matrix hb hb f) :=
have ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M),
from ⟨finset.univ.image b,
by { rw [finset.coe_image, finset.coe_univ, set.image_univ], exact hb.range }⟩,
by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq }
theorem trace_mul_comm (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
(f g : M →ₗ[R] M) : trace R M (f * g) = trace R M (g * f) :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M) then let ⟨s, hb⟩ := H in
by { simp_rw [trace_eq_matrix_trace R hb, matrix.linear_equiv_matrix_mul], apply matrix.trace_mul_comm }
else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply]
end linear_map
/-- The natural equivalence between linear endomorphisms of finite free modules and square matrices
is compatible with the algebra structures. -/
def alg_equiv_matrix' {R : Type v} [comm_ring R] [decidable_eq n] :
module.End R (n → R) ≃ₐ[R] matrix n n R :=
{ map_mul' := matrix.comp_to_matrix_mul,
map_add' := linear_equiv_matrix'.map_add,
commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix = r • 1,
rw ←linear_map.to_matrix_id, refl, },
..linear_equiv_matrix' }
/-- A linear equivalence of two modules induces an equivalence of algebras of their
endomorphisms. -/
def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : M₁ ≃ₗ[R] M₂) :
module.End R M₁ ≃ₐ[R] module.End R M₂ :=
{ map_mul' := λ f g, by apply e.arrow_congr_comp,
map_add' := e.conj.map_add,
commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id,
rw [linear_equiv.map_smul, linear_equiv.conj_id], },
..e.conj }
/-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to
square matrices. -/
def alg_equiv_matrix {R : Type v} {M : Type w}
[comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] {b : n → M} (h : is_basis R b) :
module.End R M ≃ₐ[R] matrix n n R :=
(equiv_fun_basis h).alg_conj.trans alg_equiv_matrix'
|
f9107e1afb8f7fee0c46fb820bf0c46492859467 | 4c630d016e43ace8c5f476a5070a471130c8a411 | /order/filter.lean | 041b5a90e860e17f5eca6a07b7d302b6234fe1db | [
"Apache-2.0"
] | permissive | ngamt/mathlib | 9a510c391694dc43eec969914e2a0e20b272d172 | 58909bd424209739a2214961eefaa012fb8a18d2 | refs/heads/master | 1,585,942,993,674 | 1,540,739,585,000 | 1,540,916,815,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 90,156 | 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
Theory of filters on sets.
-/
import order.galois_connection order.zorn
import data.set.finite data.list
import category.applicative
open lattice set
universes u v w x y
local attribute [instance] classical.prop_decidable
namespace lattice
variables {α : Type u} {ι : Sort v}
section
variable [complete_lattice α]
lemma Inf_eq_finite_sets {s : set α} :
Inf s = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t) :=
le_antisymm
(le_infi $ assume t, le_infi $ assume ⟨_, h⟩, Inf_le_Inf h)
(le_Inf $ assume b h, infi_le_of_le {b} $ infi_le_of_le
(by simp only [h, finite_singleton, and_self, mem_set_of_eq,
singleton_subset_iff]) $ Inf_le $ by simp only [mem_singleton])
lemma infi_insert_finset {ι : Type v} {s : finset ι} {f : ι → α} {i : ι} :
(⨅j∈insert i s, f j) = f i ⊓ (⨅j∈s, f j) :=
by simp [infi_or, infi_inf_eq]
lemma infi_empty_finset {ι : Type v} {f : ι → α} : (⨅j∈(∅ : finset ι), f j) = ⊤ :=
by simp only [finset.not_mem_empty, infi_top, infi_false, eq_self_iff_true]
end
-- TODO: move
lemma inf_left_comm [semilattice_inf α] (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) :=
by rw [← inf_assoc, ← inf_assoc, @inf_comm α _ a]
def complete_lattice.copy (c : complete_lattice α)
(le : α → α → Prop) (eq_le : le = @complete_lattice.le α c)
(top : α) (eq_top : top = @complete_lattice.top α c)
(bot : α) (eq_bot : bot = @complete_lattice.bot α c)
(sup : α → α → α) (eq_sup : sup = @complete_lattice.sup α c)
(inf : α → α → α) (eq_inf : inf = @complete_lattice.inf α c)
(Sup : set α → α) (eq_Sup : Sup = @complete_lattice.Sup α c)
(Inf : set α → α) (eq_Inf : Inf = @complete_lattice.Inf α c) :
complete_lattice α :=
begin
refine { le := le, top := top, bot := bot, sup := sup, inf := inf, Sup := Sup, Inf := Inf, ..};
subst_vars,
exact @complete_lattice.le_refl α c,
exact @complete_lattice.le_trans α c,
exact @complete_lattice.le_antisymm α c,
exact @complete_lattice.le_sup_left α c,
exact @complete_lattice.le_sup_right α c,
exact @complete_lattice.sup_le α c,
exact @complete_lattice.inf_le_left α c,
exact @complete_lattice.inf_le_right α c,
exact @complete_lattice.le_inf α c,
exact @complete_lattice.le_top α c,
exact @complete_lattice.bot_le α c,
exact @complete_lattice.le_Sup α c,
exact @complete_lattice.Sup_le α c,
exact @complete_lattice.Inf_le α c,
exact @complete_lattice.le_Inf α c
end
end lattice
namespace set
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y}
theorem monotone_inter [preorder β] {f g : β → set α}
(hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ∩ (g x)) :=
assume a b h x ⟨h₁, h₂⟩, ⟨hf h h₁, hg h h₂⟩
theorem monotone_set_of [preorder α] {p : α → β → Prop}
(hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) :=
assume a a' h b, hp b h
end set
open set lattice
section order
variables {α : Type u} (r : α → α → Prop)
local infix `≼` : 50 := r
lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f)
(h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) :=
by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact
assume a₁ b₁ fb₁ a₂ b₂ fb₂,
let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂,
⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in
⟨x, ⟨z, xf⟩, xa₁, xa₂⟩
end order
theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α}
(h : zorn.chain (f ⁻¹'o r) c) :
directed r (λx:{a:α // a ∈ c}, f (x.val)) :=
assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases
(assume : a = b, by simp only [this, exists_prop, and_self, subtype.exists];
exact ⟨b, hb, refl _⟩)
(assume : a ≠ b, (h a ha b hb this).elim
(λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩)
(λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩))
structure filter (α : Type*) :=
(sets : set (set α))
(univ_sets : set.univ ∈ sets)
(sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets)
(inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets)
namespace filter
variables {α : Type u} {f g : filter α} {s t : set α}
lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g
| ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl
lemma filter_eq_iff : f = g ↔ f.sets = g.sets :=
⟨congr_arg _, filter_eq⟩
protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f.sets ↔ s ∈ g.sets :=
by rw [filter_eq_iff, ext_iff]
@[extensionality]
protected lemma ext : (∀ s, s ∈ f.sets ↔ s ∈ g.sets) → f = g :=
filter.ext_iff.2
lemma univ_mem_sets : univ ∈ f.sets :=
f.univ_sets
lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f.sets → x ⊆ y → y ∈ f.sets :=
f.sets_of_superset
lemma inter_mem_sets : ∀{s t}, s ∈ f.sets → t ∈ f.sets → s ∩ t ∈ f.sets :=
f.inter_sets
lemma univ_mem_sets' (h : ∀ a, a ∈ s): s ∈ f.sets :=
mem_sets_of_superset univ_mem_sets (assume x _, h x)
lemma mp_sets (hs : s ∈ f.sets) (h : {x | x ∈ s → x ∈ t} ∈ f.sets) : t ∈ f.sets :=
mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁
lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) :
(∀i∈is, s i ∈ f.sets) → (⋂i∈is, s i) ∈ f.sets :=
finite.induction_on hf
(assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff])
(assume i is _ hf hi hs,
have h₁ : s i ∈ f.sets, from hs i (by simp),
have h₂ : (⋂x∈is, s x) ∈ f.sets, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true],
by simp [inter_mem_sets h₁ h₂])
lemma exists_sets_subset_iff : (∃t∈f.sets, t ⊆ s) ↔ s ∈ f.sets :=
⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩
lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f.sets) :=
assume s t hst h, mem_sets_of_superset h hst
end filter
namespace tactic.interactive
open tactic interactive
/-- `filter [t1, ⋯, tn]` replaces a goal of the form `s ∈ f.sets`
and terms `h1 : t1 ∈ f.sets, ⋯, tn ∈ f.sets` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`.
`filter [t1, ⋯, tn] e` is a short form for `{ filter [t1, ⋯, tn], exact e }`.
-/
meta def filter_upwards
(s : parse types.pexpr_list)
(e' : parse $ optional types.texpr) : tactic unit :=
do
s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e),
eapplyc `filter.univ_mem_sets',
match e' with
| some e := interactive.exact e
| none := skip
end
end tactic.interactive
namespace filter
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section principal
/-- The principal filter of `s` is the collection of all supersets of `s`. -/
def principal (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := subset_univ s,
sets_of_superset := assume x y hx hy, subset.trans hx hy,
inter_sets := assume x y, subset_inter }
instance : inhabited (filter α) :=
⟨principal ∅⟩
@[simp] lemma mem_principal_sets {s t : set α} : s ∈ (principal t).sets ↔ t ⊆ s := iff.rfl
lemma mem_principal_self (s : set α) : s ∈ (principal s).sets := subset.refl _
end principal
section join
/-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/
def join (f : filter (filter α)) : filter α :=
{ sets := {s | {t : filter α | s ∈ t.sets} ∈ f.sets},
univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets,
sets_of_superset := assume x y hx xy,
mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy,
inter_sets := assume x y hx hy,
mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ }
@[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} :
s ∈ (join f).sets ↔ {t | s ∈ filter.sets t} ∈ f.sets := iff.rfl
end join
section lattice
instance : partial_order (filter α) :=
{ le := λf g, g.sets ⊆ f.sets,
le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁,
le_refl := assume a, subset.refl _,
le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ }
theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g.sets, x ∈ f.sets := iff.rfl
/-- `generate_sets g s`: `s` is in the filter closure of `g`. -/
inductive generate_sets (g : set (set α)) : set α → Prop
| basic {s : set α} : s ∈ g → generate_sets s
| univ {} : generate_sets univ
| superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t
| inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t)
/-- `generate g` is the smallest filter containing the sets `g`. -/
def generate (g : set (set α)) : filter α :=
{ sets := {s | generate_sets g s},
univ_sets := generate_sets.univ,
sets_of_superset := assume x y, generate_sets.superset,
inter_sets := assume s t, generate_sets.inter }
lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets :=
iff.intro
(assume h u hu, h $ generate_sets.basic $ hu)
(assume h u hu, hu.rec_on h univ_mem_sets
(assume x y _ hxy hx, mem_sets_of_superset hx hxy)
(assume x y _ _ hx hy, inter_mem_sets hx hy))
protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α :=
{ sets := s,
univ_sets := hs ▸ univ_mem_sets,
sets_of_superset := assume x y, hs ▸ mem_sets_of_superset,
inter_sets := assume x y, hs ▸ inter_mem_sets }
lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} :
filter.mk_of_closure s hs = generate s :=
filter.ext $ assume u, hs.symm ▸ iff.refl _
/- Galois insertion from sets of sets into a filters. -/
def gi_generate (α : Type*) :
@galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets :=
{ gc := assume s f, sets_iff_generate,
le_l_u := assume f u, generate_sets.basic,
choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
/-- The infimum of filters is the filter generated by intersections
of elements of the two filters. -/
instance : has_inf (filter α) := ⟨λf g : filter α,
{ sets := {s | ∃ (a ∈ f.sets) (b ∈ g.sets), a ∩ b ⊆ s },
univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩,
sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩,
inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩,
⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd,
calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl
... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩
@[simp] lemma mem_inf_sets {f g : filter α} {s : set α} :
s ∈ (f ⊓ g).sets ↔ ∃t₁∈f.sets, ∃t₂∈g.sets, t₁ ∩ t₂ ⊆ s := iff.rfl
lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f.sets) : s ∈ (f ⊓ g).sets :=
⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩
lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g.sets) : s ∈ (f ⊓ g).sets :=
⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩
lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α}
(hs : s ∈ f.sets) (ht : t ∈ g.sets) : s ∩ t ∈ (f ⊓ g).sets :=
inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht)
instance : has_top (filter α) :=
⟨{ sets := {s | ∀x, x ∈ s},
univ_sets := assume x, mem_univ x,
sets_of_superset := assume x y hx hxy a, hxy (hx a),
inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩
lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α).sets ↔ (∀x, x ∈ s) :=
iff.refl _
@[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α).sets ↔ s = univ :=
by rw [mem_top_sets_iff_forall, eq_univ_iff_forall]
section complete_lattice
/- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately,
we want to have different definitional equalities for the lattice operations. So we define them
upfront and change the lattice operations for the complete lattice instance. -/
private def original_complete_lattice : complete_lattice (filter α) :=
@order_dual.lattice.complete_lattice _ (gi_generate α).lift_complete_lattice
local attribute [instance] original_complete_lattice
instance : complete_lattice (filter α) := original_complete_lattice.copy
/- le -/ filter.partial_order.le rfl
/- top -/ (filter.lattice.has_top).1
(top_unique $ assume s hs, (eq_univ_of_forall hs).symm ▸ univ_mem_sets)
/- bot -/ _ rfl
/- sup -/ _ rfl
/- inf -/ (filter.lattice.has_inf).1
begin
ext f g : 2,
exact le_antisymm
(le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right))
(assume s ⟨a, ha, b, hb, hs⟩, mem_sets_of_superset (inter_mem_sets
(@inf_le_left (filter α) _ _ _ _ ha)
(@inf_le_right (filter α) _ _ _ _ hb)) hs)
end
/- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm)
/- Inf -/ _ rfl
end complete_lattice
lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl
lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets :=
(gi_generate α).gc.u_inf
lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) :=
(gi_generate α).gc.u_Inf
lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) :=
(gi_generate α).gc.u_infi
lemma generate_empty : filter.generate ∅ = (⊤ : filter α) :=
(gi_generate α).gc.l_bot
lemma generate_univ : filter.generate univ = (⊥ : filter α) :=
mk_of_closure_sets.symm
lemma generate_union {s t : set (set α)} :
filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t :=
(gi_generate α).gc.l_sup
lemma generate_Union {s : ι → set (set α)} :
filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) :=
(gi_generate α).gc.l_supr
@[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α).sets :=
trivial
@[simp] lemma mem_sup_sets {f g : filter α} {s : set α} :
s ∈ (f ⊔ g).sets ↔ s ∈ f.sets ∧ s ∈ g.sets :=
iff.rfl
@[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} :
x ∈ (Sup s).sets ↔ (∀f∈s, x ∈ (f:filter α).sets) :=
iff.rfl
@[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} :
x ∈ (supr f).sets ↔ (∀i, x ∈ (f i).sets) :=
by simp only [supr_sets_eq, iff_self, mem_Inter]
@[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f.sets :=
show (∀{t}, s ⊆ t → t ∈ f.sets) ↔ s ∈ f.sets,
from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩
lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t :=
by simp only [le_principal_iff, iff_self, mem_principal_sets]
lemma monotone_principal : monotone (principal : set α → filter α) :=
by simp only [monotone, principal_mono]; exact assume a b h, h
@[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t :=
by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl
@[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl
/- lattice equations -/
lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f.sets ↔ f = ⊥ :=
⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s),
assume : f = ⊥, this.symm ▸ mem_bot_sets⟩
lemma inhabited_of_mem_sets {f : filter α} {s : set α} (hf : f ≠ ⊥) (hs : s ∈ f.sets) :
∃x, x ∈ s :=
have ∅ ∉ f.sets, from assume h, hf $ empty_in_sets_eq_bot.mp h,
have s ≠ ∅, from assume h, this (h ▸ hs),
exists_mem_of_ne_empty this
lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ :=
empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩)
lemma forall_sets_neq_empty_iff_neq_bot {f : filter α} :
(∀ (s : set α), s ∈ f.sets → s ≠ ∅) ↔ f ≠ ⊥ :=
by
simp only [(@empty_in_sets_eq_bot α f).symm, ne.def];
exact ⟨assume h hs, h _ hs rfl, assume h s hs eq, h $ eq ▸ hs⟩
lemma mem_sets_of_neq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f.sets :=
have ∅ ∈ (f ⊓ principal (- s)).sets, from h.symm ▸ mem_bot_sets,
let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in
by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩
lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) :
(infi f).sets = (⋃ i, (f i).sets) :=
let ⟨i⟩ := ne, u := { filter .
sets := (⋃ i, (f i).sets),
univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩,
sets_of_superset := by simp only [mem_Union, exists_imp_distrib];
intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩,
inter_sets :=
begin
simp only [mem_Union, exists_imp_distrib],
assume x y a hx b hy,
rcases h a b with ⟨c, ha, hb⟩,
exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩
end } in
subset.antisymm
(show u ≤ infi f, from le_infi $ assume i, le_supr (λi, (f i).sets) i)
(Union_subset $ assume i, infi_le f i)
lemma infi_sets_eq' {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : ∃i, i ∈ s) :
(⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) :=
let ⟨i, hi⟩ := ne in
calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl
... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl
lemma Inf_sets_eq_finite {s : set (filter α)} :
(Inf s).sets = (⋃ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) :=
calc (Inf s).sets = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t).sets : by rw [lattice.Inf_eq_finite_sets]
... = (⨆ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) : infi_sets_eq'
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∪ y, ⟨finite_union hx₁ hy₁, union_subset hx₂ hy₂⟩,
Inf_le_Inf $ subset_union_left _ _, Inf_le_Inf $ subset_union_right _ _⟩)
⟨∅, by simp only [empty_subset, finite_empty, and_self, mem_set_of_eq]⟩
@[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq]
@[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq]
instance : bounded_distrib_lattice (filter α) :=
{ le_sup_inf :=
begin
assume x y z s,
simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp],
intros hs t₁ ht₁ t₂ ht₂ hts,
exact ⟨s ∪ t₁,
x.sets_of_superset hs $ subset_union_left _ _,
y.sets_of_superset ht₁ $ subset_union_right _ _,
s ∪ t₂,
x.sets_of_superset hs $ subset_union_left _ _,
z.sets_of_superset ht₂ $ subset_union_right _ _,
subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩
end,
..filter.lattice.complete_lattice }
private lemma infi_finite_distrib {s : set (filter α)} {f : filter α} (h : finite s) :
(⨅ a ∈ s, f ⊔ a) = f ⊔ (Inf s) :=
finite.induction_on h
(by simp only [mem_empty_eq, infi_false, infi_top, Inf_empty, sup_top_eq])
(by intros a s hn hs hi; rw [infi_insert, hi, ← sup_inf_left, Inf_insert])
/- the complementary version with ⨆ g∈s, f ⊓ g does not hold! -/
lemma binfi_sup_eq { f : filter α } {s : set (filter α)} : (⨅ g∈s, f ⊔ g) = f ⊔ Inf s :=
le_antisymm
begin
intros t h,
cases h with h₁ h₂,
rw [Inf_sets_eq_finite] at h₂,
simp only [and_assoc, exists_prop, mem_Union, mem_set_of_eq] at h₂,
rcases h₂ with ⟨s', hs', hs's, ht'⟩,
have ht : t ∈ (⨅ a ∈ s', f ⊔ a).sets,
{ rw [infi_finite_distrib], exact ⟨h₁, ht'⟩, exact hs' },
clear h₁ ht',
revert ht t,
change (⨅ a ∈ s, f ⊔ a) ≤ (⨅ a ∈ s', f ⊔ a),
apply infi_le_infi2 _,
exact assume i, ⟨i, infi_le_infi2 $ assume h, ⟨hs's h, le_refl _⟩⟩
end
(le_infi $ assume g, le_infi $ assume h, sup_le_sup (le_refl f) $ Inf_le h)
lemma infi_sup_eq { f : filter α } {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g :=
calc (⨅ x, f ⊔ g x) = (⨅ x (h : ∃i, g i = x), f ⊔ x) :
by simp only [infi_exists]; rw infi_comm; simp only [infi_infi_eq_right, eq_self_iff_true]
... = f ⊔ Inf {x | ∃i, g i = x} : binfi_sup_eq
... = f ⊔ infi g : by rw Inf_eq_infi; dsimp; simp only [infi_exists];
rw infi_comm; simp only [infi_infi_eq_right, eq_self_iff_true]
lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} :
∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⋂a∈s, p a) ⊆ t) :=
show ∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⨅a∈s, p a) ≤ t),
begin
refine finset.induction_on s _ _,
{ simp only [finset.not_mem_empty, false_implies_iff, lattice.infi_empty_finset, top_le_iff,
imp_true_iff, mem_top_sets, true_and, exists_const],
intros; refl },
{ intros a s has ih t,
simp only [ih, finset.forall_mem_insert, lattice.infi_insert_finset, mem_inf_sets,
exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt},
split,
{ intros t₁ ht₁ t₂ p hp ht₂ ht,
existsi function.update p a t₁,
have : ∀a'∈s, function.update p a t₁ a' = p a',
from assume a' ha',
have a' ≠ a, from assume h, has $ h ▸ ha',
function.update_noteq this,
have eq : (⨅j ∈ s, function.update p a t₁ j) = (⨅j ∈ s, p j),
begin congr, funext b, congr, funext h, apply this, assumption end,
simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt},
exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht },
from assume p hpa hp ht, ⟨p a, hpa, (⨅j∈s, p j), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ }
end
/- principal equations -/
@[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) :=
le_antisymm
(by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩)
(by simp [le_inf_iff, inter_subset_left, inter_subset_right])
@[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) :=
filter_eq $ set.ext $
by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets]
@[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter];
exact (@supr_le_iff (set α) _ _ _ _).symm
lemma principal_univ : principal (univ : set α) = ⊤ :=
top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true]
lemma principal_empty : principal (∅ : set α) = ⊥ :=
bot_unique $ assume s _, empty_subset _
@[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ :=
⟨assume h, principal_eq_iff_eq.mp $ by simp only [principal_empty, h, eq_self_iff_true],
assume h, by simp only [h, principal_empty, eq_self_iff_true]⟩
lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f.sets) : f ⊓ principal s = ⊥ :=
empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩
end lattice
section map
/-- The forward map of a filter -/
def map (m : α → β) (f : filter α) : filter β :=
{ sets := preimage m ⁻¹' f.sets,
univ_sets := univ_mem_sets,
sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st,
inter_sets := assume s t hs ht, inter_mem_sets hs ht }
@[simp] lemma map_principal {s : set α} {f : α → β} :
map f (principal s) = principal (set.image f s) :=
filter_eq $ set.ext $ assume a, image_subset_iff.symm
variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] lemma mem_map : t ∈ (map m f).sets ↔ {x | m x ∈ t} ∈ f.sets := iff.rfl
lemma image_mem_map (hs : s ∈ f.sets) : m '' s ∈ (map m f).sets :=
f.sets_of_superset hs $ subset_preimage_image m s
lemma mem_map_sets_iff : t ∈ (map m f).sets ↔ (∃s∈f.sets, m '' s ⊆ t) :=
iff.intro
(assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩)
(assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht)
@[simp] lemma map_id : filter.map id f = f :=
filter_eq $ rfl
@[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) :=
funext $ assume _, filter_eq $ rfl
@[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f :=
congr_fun (@@filter.map_compose m m') f
end map
section comap
/-- The inverse map of a filter -/
def comap (m : α → β) (f : filter β) : filter α :=
{ sets := { s | ∃t∈f.sets, m ⁻¹' t ⊆ s },
univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩,
sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab,
⟨a', ha', subset.trans ma'a ab⟩,
inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩,
⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ }
end comap
/-- The cofinite filter is the filter of subsets whose complements are finite. -/
def cofinite : filter α :=
{ sets := {s | finite (- s)},
univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq],
sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t),
finite_subset hs $ @lattice.neg_le_neg (set α) _ _ _ st,
inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)),
by simp only [compl_inter, finite_union, ht, hs, mem_set_of_eq] }
/-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
applicative instance. -/
def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
/-- The applicative sequentiation operation. This is not induced by the bind operation. -/
def seq (f : filter (α → β)) (g : filter α) : filter β :=
⟨{ s | ∃u∈f.sets, ∃t∈g.sets, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩,
assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
instance : has_pure filter := ⟨λ(α : Type u) x, principal {x}⟩
instance : has_bind filter := ⟨@filter.bind⟩
instance : has_seq filter := ⟨@filter.seq⟩
instance : functor filter := { map := @filter.map }
section
-- this section needs to be before applicative, otherwiese the wrong instance will be chosen
protected def monad : monad filter := { map := @filter.map }
local attribute [instance] filter.monad
protected def is_lawful_monad : is_lawful_monad filter :=
{ id_map := assume α f, filter_eq rfl,
pure_bind := assume α β a f, by simp only [bind, Sup_image, image_singleton,
join_principal_eq_Sup, lattice.Sup_singleton, map_principal, eq_self_iff_true],
bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
bind_pure_comp_eq_map := assume α β f x, filter_eq $
by simp only [bind, join, map, preimage, principal, set.subset_univ, eq_self_iff_true,
function.comp_app, mem_set_of_eq, singleton_subset_iff] }
end
instance : applicative filter := { map := @filter.map, seq := @filter.seq }
instance : alternative filter :=
{ failure := λα, ⊥,
orelse := λα x y, x ⊔ y }
@[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl
@[simp] lemma mem_pure {a : α} {s : set α} : a ∈ s → s ∈ (pure a : filter α).sets :=
by simp only [imp_self, pure_def, mem_principal_sets, singleton_subset_iff]; exact id
@[simp] lemma mem_pure_iff {a : α} {s : set α} : s ∈ (pure a : filter α).sets ↔ a ∈ s :=
by rw [pure_def, mem_principal_sets, set.singleton_subset_iff]
@[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl
@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
/- map and comap equations -/
section map
variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] theorem mem_comap_sets : s ∈ (comap m g).sets ↔ ∃t∈g.sets, m ⁻¹' t ⊆ s := iff.rfl
theorem preimage_mem_comap (ht : t ∈ g.sets) : m ⁻¹' t ∈ (comap m g).sets :=
⟨t, ht, subset.refl _⟩
lemma comap_id : comap id f = f :=
le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst)
lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f :=
le_antisymm
(assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩)
(assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩,
⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩)
@[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) :=
filter_eq $ set.ext $ assume s,
⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b,
assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩
lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g :=
⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩
lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) :=
assume f g, map_le_iff_le_comap
lemma map_mono (h : f₁ ≤ f₂) : map m f₁ ≤ map m f₂ := (gc_map_comap m).monotone_l h
lemma monotone_map : monotone (map m) | a b := map_mono
lemma comap_mono (h : g₁ ≤ g₂) : comap m g₁ ≤ comap m g₂ := (gc_map_comap m).monotone_u h
lemma monotone_comap : monotone (comap m) | a b := comap_mono
@[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot
@[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup
@[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) :=
(gc_map_comap m).l_supr
@[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top
@[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf
@[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) :=
(gc_map_comap m).u_infi
lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _
lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _
@[simp] lemma comap_bot : comap m ⊥ = ⊥ :=
bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩
lemma comap_supr {ι} {f : ι → filter β} {m : α → β} :
comap m (supr f) = (⨆i, comap m (f i)) :=
le_antisymm
(assume s hs,
have ∀i, ∃t, t ∈ (f i).sets ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs,
let ⟨t, ht⟩ := classical.axiom_of_choice this in
⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _),
begin
rw [preimage_Union, Union_subset_iff],
assume i,
exact (ht i).2
end⟩)
(supr_le $ assume i, monotone_comap $ le_supr _ _)
lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) :=
by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true]
lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ :=
le_antisymm
(assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩,
⟨t₁ ∪ t₂,
⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩,
union_subset hs₁ hs₂⟩)
(sup_le (comap_mono le_sup_left) (comap_mono le_sup_right))
lemma le_map_comap' {f : filter β} {m : α → β} {s : set β}
(hs : s ∈ f.sets) (hm : ∀b∈s, ∃a, m a = b) : f ≤ map m (comap m f) :=
assume t' ⟨t, ht, (sub : m ⁻¹' t ⊆ m ⁻¹' t')⟩,
by filter_upwards [ht, hs] assume x hxt hxs,
let ⟨y, hy⟩ := hm x hxs in
hy ▸ sub (show m y ∈ t, from hy.symm ▸ hxt)
lemma le_map_comap {f : filter β} {m : α → β} (hm : ∀x, ∃y, m y = x) : f ≤ map m (comap m f) :=
le_map_comap' univ_mem_sets (assume b _, hm b)
lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
comap m (map m f) = f :=
have ∀s, preimage m (image m s) = s,
from assume s, preimage_image_eq s h,
le_antisymm
(assume s hs, ⟨
image m s,
f.sets_of_superset hs $ by simp only [this, subset.refl],
by simp only [this, subset.refl]⟩)
le_comap_map
lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f ≤ map m g) : f ≤ g :=
assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)]
assume a has ⟨b, ⟨hbs, hb⟩, h⟩,
have b = a, from hm _ hbs _ has h,
this ▸ hb
lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) :
map m f ≤ map m g ↔ f ≤ g :=
iff.intro (le_of_map_le_map_inj' hsf hsg hm) map_mono
lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f = map m g) : f = g :=
le_antisymm
(le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h)
(le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm)
lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) :
f = g :=
have comap m (map m f) = comap m (map m g), by rw h,
by rwa [comap_map hm, comap_map hm] at this
lemma comap_neq_bot {f : filter β} {m : α → β}
(hm : ∀t∈f.sets, ∃a, m a ∈ t) : comap m f ≠ ⊥ :=
forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, t_s⟩,
let ⟨a, (ha : a ∈ preimage m t)⟩ := hm t ht in
neq_bot_of_le_neq_bot (ne_empty_of_mem ha) t_s
lemma comap_neq_bot_of_surj {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : ∀b, ∃a, m a = b) : comap m f ≠ ⊥ :=
comap_neq_bot $ assume t ht,
let
⟨b, (hx : b ∈ t)⟩ := inhabited_of_mem_sets hf ht,
⟨a, (ha : m a = b)⟩ := hm b
in ⟨a, ha.symm ▸ hx⟩
@[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ :=
⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id,
assume h, by simp only [h, eq_self_iff_true, map_bot]⟩
lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ :=
assume h, hf $ by rwa [map_eq_bot_iff] at h
lemma sInter_comap_sets (f : α → β) (F : filter β) :
⋂₀(comap f F).sets = ⋂ U ∈ F.sets, f ⁻¹' U :=
begin
ext x,
suffices : (∀ (A : set α) (B : set β), B ∈ F.sets → f ⁻¹' B ⊆ A → x ∈ A) ↔
∀ (B : set β), B ∈ F.sets → f x ∈ B,
by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter,
iff_self, mem_Inter, mem_preimage_eq, exists_imp_distrib],
split,
{ intros h U U_in,
simpa only [set.subset.refl, forall_prop_of_true, mem_preimage_eq] using h (f ⁻¹' U) U U_in },
{ intros h V U U_in f_U_V,
exact f_U_V (h U U_in) },
end
end map
lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f.sets) :
map m₁ f = map m₂ f :=
have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f.sets), map m₁ f ≤ map m₂ f,
begin
intros m₁ m₂ h s hs,
show {x | m₁ x ∈ s} ∈ f.sets,
filter_upwards [h, hs],
simp only [subset_def, mem_preimage_eq, mem_set_of_eq, forall_true_iff] {contextual := tt}
end,
le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm)
-- this is a generic rule for monotone functions:
lemma map_infi_le {f : ι → filter α} {m : α → β} :
map m (infi f) ≤ (⨅ i, map m (f i)) :=
le_infi $ assume i, map_mono $ infi_le _ _
lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) :
map m (infi f) = (⨅ i, map m (f i)) :=
le_antisymm
map_infi_le
(assume s (hs : preimage m s ∈ (infi f).sets),
have ∃i, preimage m s ∈ (f i).sets,
by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption,
let ⟨i, hi⟩ := this in
have (⨅ i, map m (f i)) ≤ principal s, from
infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption,
by simp only [filter.le_principal_iff] at this; assumption)
lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop}
(h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) :
map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) :=
let ⟨i, hi⟩ := ne in
calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true]
... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true]
lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f.sets) (htg : t ∈ g.sets)
(h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g :=
begin
refine le_antisymm
(le_inf (map_mono inf_le_left) (map_mono inf_le_right))
(assume s hs, _),
simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage_eq, mem_inf_sets] at hs ⊢,
rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩,
refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩,
{ filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ rw [image_inter_on],
{ refine image_subset_iff.2 _,
exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ },
{ exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } }
end
lemma map_inf {f g : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
map m (f ⊓ g) = map m f ⊓ map m g :=
map_inf' univ_mem_sets univ_mem_sets (assume x _ y _, h x y)
lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α}
(h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f :=
le_antisymm
(assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $
calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true]
... ⊆ preimage m b : preimage_mono h)
(assume b (hb : preimage m b ∈ f.sets),
⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩)
lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f :=
map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq
lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈f.sets, m '' s ∈ g.sets) :
g ≤ f.map m :=
assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _
section applicative
@[simp] lemma mem_pure_sets {a : α} {s : set α} :
s ∈ (pure a : filter α).sets ↔ a ∈ s :=
by simp only [iff_self, pure_def, mem_principal_sets, singleton_subset_iff]
lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α).sets :=
by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff]
@[simp] lemma pure_neq_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) :=
by simp only [pure, has_pure.pure, ne.def, not_false_iff, singleton_ne_empty, principal_eq_bot_iff]
lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ (f.seq g).sets ↔ (∃u∈f.sets, ∃t∈g.sets, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) :=
iff.refl _
lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ (f.seq g).sets ↔ (∃u∈f.sets, ∃t∈g.sets, set.seq u t ⊆ s) :=
by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self]
lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} :
s ∈ ((f.map m).seq g).sets ↔ (∃t u, t ∈ g.sets ∧ u ∈ f.sets ∧ ∀x∈u, ∀y∈t, m x y ∈ s) :=
iff.intro
(assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩)
(assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩)
lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α}
(hs : s ∈ f.sets) (ht : t ∈ g.sets): s.seq t ∈ (f.seq g).sets :=
⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩
lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β}
(hh : ∀t∈f.sets, ∀u∈g.sets, set.seq t u ∈ h.sets) : h ≤ seq f g :=
assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $
assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha
lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
(hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ :=
le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht)
@[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← singleton_seq, apply seq_mem_seq_sets _ hs,
simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff] },
{ rw mem_pure_sets at hs,
refine sets_of_superset (map g f) (image_mem_map ht) _,
rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ }
end
@[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) :=
le_antisymm
(le_principal_iff.2 $ sets_of_superset (map f (pure a)) (image_mem_map singleton_mem_pure_sets) $
by simp only [image_singleton, mem_singleton, singleton_subset_iff])
(le_map $ assume s, begin
simp only [mem_image, pure_def, mem_principal_sets, singleton_subset_iff],
exact assume has, ⟨a, has, rfl⟩
end)
@[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← seq_singleton, exact seq_mem_seq_sets hs
(by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff]) },
{ rw mem_pure_sets at ht,
refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _,
rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ }
end
@[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) :
seq h (seq g x) = seq (seq (map (∘) h) g) x :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩,
refine mem_sets_of_superset _
(set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)),
rw ← set.seq_seq,
exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) },
{ rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht),
rw set.seq_seq,
exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv }
end
lemma prod_map_seq_comm (f : filter α) (g : filter β) :
(map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw ← set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu },
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu }
end
instance : is_lawful_functor (filter : Type u → Type u) :=
{ id_map := assume α f, map_id,
comp_map := assume α β γ f g a, map_map.symm }
instance : is_lawful_applicative (filter : Type u → Type u) :=
{ pure_seq_eq_map := assume α β, pure_seq_eq_map,
map_pure := assume α β, map_pure,
seq_pure := assume α β, seq_pure,
seq_assoc := assume α β γ, seq_assoc }
instance : is_comm_applicative (filter : Type u → Type u) :=
⟨assume α β f g, prod_map_seq_comm f g⟩
lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) :
f <*> g = seq f g := rfl
end applicative
/- bind equations -/
section bind
@[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} :
s ∈ (bind f m).sets ↔ ∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets :=
calc s ∈ (bind f m).sets ↔ {a | s ∈ (m a).sets} ∈ f.sets : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq]
... ↔ (∃t ∈ f.sets, t ⊆ {a | s ∈ (m a).sets}) : exists_sets_subset_iff.symm
... ↔ (∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets) : iff.refl _
lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f.sets) :
bind f g ≤ bind f h :=
assume x h₂, show (_ ∈ f.sets), by filter_upwards [h₁, h₂] assume s gh' h', gh' h'
lemma bind_sup {f g : filter α} {h : α → filter β} :
bind (f ⊔ g) h = bind f h ⊔ bind g h :=
by simp only [bind, sup_join, map_sup, eq_self_iff_true]
lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) :
bind f h ≤ bind g h :=
assume s h', h₁ h'
lemma principal_bind {s : set α} {f : α → filter β} :
(bind (principal s) f) = (⨆x ∈ s, f x) :=
show join (map f (principal s)) = (⨆x ∈ s, f x),
by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true]
end bind
lemma infi_neq_bot_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥): (infi f) ≠ ⊥ :=
let ⟨x⟩ := hn in
assume h, have he: ∅ ∈ (infi f).sets, from h.symm ▸ mem_bot_sets,
classical.by_cases
(assume : nonempty ι,
have ∃i, ∅ ∈ (f i).sets,
by rw [infi_sets_eq hd this] at he; simp only [mem_Union] at he; assumption,
let ⟨i, hi⟩ := this in
hb i $ bot_unique $
assume s _, (f i).sets_of_superset hi $ empty_subset _)
(assume : ¬ nonempty ι,
have univ ⊆ (∅ : set α),
begin
rw [←principal_mono, principal_univ, principal_empty, ←h],
exact (le_infi $ assume i, false.elim $ this ⟨i⟩)
end,
this $ mem_univ x)
lemma infi_neq_bot_iff_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume neq_bot i eq_bot, neq_bot $ bot_unique $ infi_le_of_le i $ eq_bot ▸ le_refl _,
infi_neq_bot_of_directed hn hd⟩
lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ (f i).sets → s ∈ (⨅i, f i).sets :=
show (⨅i, f i) ≤ f i, from infi_le _ _
@[elab_as_eliminator]
lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ (infi f).sets) {p : set α → Prop}
(uni : p univ)
(ins : ∀{i s₁ s₂}, s₁ ∈ (f i).sets → p s₂ → p (s₁ ∩ s₂))
(upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s :=
begin
have hs' : s ∈ (Inf {a : filter α | ∃ (i : ι), a = f i}).sets := hs,
rw [Inf_sets_eq_finite] at hs',
simp only [mem_Union] at hs',
rcases hs' with ⟨is, ⟨fin_is, his⟩, hs⟩, revert his s,
refine finite.induction_on fin_is _ (λ fi is fi_ne_is fin_is ih, _); intros his s hs' hs,
{ rw [Inf_empty, mem_top_sets] at hs, simpa only [hs] },
{ rw [Inf_insert] at hs,
rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩,
rcases (his (mem_insert _ _) : ∃i, fi = f i) with ⟨i, rfl⟩,
have hs₂ : p s₂, from
have his : is ⊆ {x | ∃i, x = f i}, from assume i hi, his $ mem_insert_of_mem _ hi,
have infi f ≤ Inf is, from Inf_le_Inf his,
ih his (this hs₂) hs₂,
exact upw hs (ins hs₁ hs₂) }
end
/- tendsto -/
/-- `tendsto` is the generic "limit of a function" predicate.
`tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`,
the `f`-preimage of `a` is an `l₁` neighborhood. -/
def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂
lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ ∀ s ∈ l₂.sets, f ⁻¹' s ∈ l₁.sets := iff.rfl
lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f :=
map_le_iff_le_comap
lemma tendsto_cong {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : tendsto f₁ l₁ l₂) (hl : {x | f₁ x = f₂ x} ∈ l₁.sets) : tendsto f₂ l₁ l₂ :=
by rwa [tendsto, ←map_cong hl]
lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y :=
by simp only [tendsto, map_id, forall_true_iff] {contextual := tt}
lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x
lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ}
(hf : tendsto f x y) (hg : tendsto g y z) : tendsto (g ∘ f) x z :=
calc map (g ∘ f) x = map g (map f x) : by rw [map_map]
... ≤ map g y : map_mono hf
... ≤ z : hg
lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β}
(h : y ≤ x) : tendsto f x z → tendsto f y z :=
le_trans (map_mono h)
lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β}
(h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z :=
le_trans h₂ h₁
lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x)
lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ}
(h : tendsto (f ∘ g) x y) : tendsto f (map g x) y :=
by rwa [tendsto, map_map]
lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} :
tendsto f (map g x) y ↔ tendsto (f ∘ g) x y :=
by rw [tendsto, map_map]; refl
lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x :=
map_comap_le
lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} :
tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c :=
⟨assume h, h.comp tendsto_comap, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩
lemma tendsto_comap'' {m : α → β} {f : filter α} {g : filter β} (s : set α)
{i : γ → α} (hs : s ∈ f.sets) (hi : ∀a∈s, ∃c, i c = a)
(h : tendsto (m ∘ i) (comap i f) g) : tendsto m f g :=
have tendsto m (map i $ comap i $ f) g,
by rwa [tendsto, ←map_compose] at h,
le_trans (map_mono $ le_map_comap' hs hi) this
lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f :=
begin
refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ),
rw [comap_comap_comp, eq, comap_id],
exact le_refl _
end
lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g :=
begin
refine le_antisymm hφ (le_trans _ (map_mono hψ)),
rw [map_map, eq, map_id],
exact le_refl _
end
lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} :
tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ :=
by simp only [tendsto, lattice.le_inf_iff, iff_self]
lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_left) h
lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_right) h
lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} :
tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) :=
by simp only [tendsto, iff_self, lattice.le_infi_iff]
lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) :
tendsto f (x i) y → tendsto f (⨅i, x i) y :=
tendsto_le_left (infi_le _ _)
lemma tendsto_principal {f : α → β} {a : filter α} {s : set β} :
tendsto f a (principal s) ↔ {a | f a ∈ s} ∈ a.sets :=
by simp only [tendsto, le_principal_iff, mem_map, iff_self]
lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} :
tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t :=
by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl
lemma tendsto_pure_pure (f : α → β) (a : α) :
tendsto f (pure a) (pure (f a)) :=
show filter.map f (pure a) ≤ pure (f a),
by rw [filter.map_pure]; exact le_refl _
lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λa, b) a (pure b) :=
by simp [tendsto]; exact univ_mem_sets
section lift
/-- A variant on `bind` using a function `g` taking a set
instead of a member of `α`. -/
protected def lift (f : filter α) (g : set α → filter β) :=
⨅s ∈ f.sets, g s
variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β}
lemma lift_sets_eq (hg : monotone g) : (f.lift g).sets = (⋃t∈f.sets, (g t).sets) :=
infi_sets_eq'
(assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht,
hg $ inter_subset_left s t, hg $ inter_subset_right s t⟩)
⟨univ, univ_mem_sets⟩
lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f.sets) (hs : s ∈ (g t).sets) :
s ∈ (f.lift g).sets :=
le_principal_iff.mp $ show f.lift g ≤ principal s,
from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs
lemma mem_lift_sets (hg : monotone g) {s : set β} :
s ∈ (f.lift g).sets ↔ (∃t∈f.sets, s ∈ (g t).sets) :=
by rw [lift_sets_eq hg]; simp only [mem_Union]
lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α}
(hs : s ∈ f.sets) (hg : g s ≤ h) : f.lift g ≤ h :=
infi_le_of_le s $ infi_le_of_le hs $ hg
lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β}
(hh : ∀s∈f.sets, h ≤ g s) : h ≤ f.lift g :=
le_infi $ assume s, le_infi $ assume hs, hh s hs
lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ :=
infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩
lemma lift_mono' (hg : ∀s∈f.sets, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ :=
infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs
lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) :=
have monotone (map m ∘ g),
from monotone_comp hg monotone_map,
filter_eq $ set.ext $
by simp only [mem_lift_sets, hg, @mem_lift_sets _ _ f _ this, exists_prop, forall_const, mem_map, iff_self, function.comp_app]
lemma comap_lift_eq {m : γ → β} (hg : monotone g) : comap m (f.lift g) = f.lift (comap m ∘ g) :=
have monotone (comap m ∘ g),
from monotone_comp hg monotone_comap,
filter_eq $ set.ext begin
simp only [hg, @mem_lift_sets _ _ f _ this, comap, mem_lift_sets, mem_set_of_eq, exists_prop,
function.comp_apply],
exact λ s,
⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩,
λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩
end
theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) :
(comap m f).lift g = f.lift (g ∘ preimage m) :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs,
infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩)
(le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩,
infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub)
lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) :
(map m f).lift g = f.lift (g ∘ image m) :=
le_antisymm
(infi_le_infi2 $ assume s, ⟨image m s,
infi_le_infi2 $ assume hs, ⟨
f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h,
le_refl _⟩⟩)
(infi_le_infi2 $ assume t, ⟨preimage m t,
infi_le_infi2 $ assume ht, ⟨ht,
hg $ assume x, assume h : x ∈ m '' preimage m t,
let ⟨y, hy, h_eq⟩ := h in
show x ∈ t, from h_eq ▸ hy⟩⟩)
lemma lift_comm {g : filter β} {h : set α → set β → filter γ} :
f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj,
infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi)
(le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj,
infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi)
lemma lift_assoc {h : set β → filter γ} (hg : monotone g) :
(f.lift g).lift h = f.lift (λs, (g s).lift h) :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩)
(le_infi $ assume t, le_infi $ assume ht,
let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in
infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h')
lemma lift_lift_same_le_lift {g : set α → set α → filter β} :
f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) :=
le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs
lemma lift_lift_same_eq_lift {g : set α → set α → filter β}
(hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)):
f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) :=
le_antisymm
lift_lift_same_le_lift
(le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
infi_le_of_le (s ∩ t) $
infi_le_of_le (inter_mem_sets hs ht) $
calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _)
... ≤ g s t : hg₁ s (inter_subset_right _ _))
lemma lift_principal {s : set α} (hg : monotone g) :
(principal s).lift g = g s :=
le_antisymm
(infi_le_of_le s $ infi_le _ $ subset.refl _)
(le_infi $ assume t, le_infi $ assume hi, hg hi)
theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β}
(hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) :=
assume a b h, lift_mono (hf h) (hg h)
lemma lift_neq_bot_iff (hm : monotone g) : (f.lift g ≠ ⊥) ↔ (∀s∈f.sets, g s ≠ ⊥) :=
classical.by_cases
(assume hn : nonempty β,
calc f.lift g ≠ ⊥ ↔ (⨅s : { s // s ∈ f.sets}, g s.val) ≠ ⊥ :
by simp only [filter.lift, infi_subtype, iff_self, ne.def]
... ↔ (∀s:{ s // s ∈ f.sets}, g s.val ≠ ⊥) :
infi_neq_bot_iff_of_directed hn
(assume ⟨a, ha⟩ ⟨b, hb⟩, ⟨⟨a ∩ b, inter_mem_sets ha hb⟩,
hm $ inter_subset_left _ _, hm $ inter_subset_right _ _⟩)
... ↔ (∀s∈f.sets, g s ≠ ⊥) : ⟨assume h s hs, h ⟨s, hs⟩, assume h ⟨s, hs⟩, h s hs⟩)
(assume hn : ¬ nonempty β,
have h₁ : f.lift g = ⊥, from filter_eq_bot_of_not_nonempty hn,
have h₂ : ∀s, g s = ⊥, from assume s, filter_eq_bot_of_not_nonempty hn,
calc (f.lift g ≠ ⊥) ↔ false : by simp only [h₁, iff_self, eq_self_iff_true, not_true, ne.def]
... ↔ (∀s∈f.sets, false) : ⟨false.elim, assume h, h univ univ_mem_sets⟩
... ↔ (∀s∈f.sets, g s ≠ ⊥) : by simp only [h₂, iff_self, eq_self_iff_true, not_true, ne.def])
@[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g :=
le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g)
@[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} :
f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h :=
by simp only [filter.lift, infi_inf_eq, eq_self_iff_true]
@[simp] lemma lift_principal2 {f : filter α} : f.lift principal = f :=
le_antisymm
(assume s hs, mem_lift hs (mem_principal_self s))
(le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff])
lemma lift_infi {f : ι → filter α} {g : set α → filter β}
(hι : nonempty ι) (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) :=
le_antisymm
(le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _))
(assume s,
have g_mono : monotone g,
from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h,
have ∀t∈(infi f).sets, (⨅ (i : ι), filter.lift (f i) g) ≤ g t,
from assume t ht, infi_sets_induct ht
(let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets)
(assume i s₁ s₂ hs₁ hs₂,
@hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂)
(assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁),
begin
rw [lift_sets_eq g_mono],
simp only [mem_Union, exists_imp_distrib],
exact assume t ht hs, this t ht hs
end)
end lift
section lift'
/-- Specialize `lift` to functions `set α → set β`. This can be viewed as
a generalization of `comap`. -/
protected def lift' (f : filter α) (h : set α → set β) :=
f.lift (principal ∘ h)
variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β}
lemma mem_lift' {t : set α} (ht : t ∈ f.sets) : h t ∈ (f.lift' h).sets :=
le_principal_iff.mp $ show f.lift' h ≤ principal (h t),
from infi_le_of_le t $ infi_le_of_le ht $ le_refl _
lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h).sets ↔ (∃t∈f.sets, h t ⊆ s) :=
have monotone (principal ∘ h),
from assume a b h, principal_mono.mpr $ hh h,
by simp only [filter.lift', @mem_lift_sets α β f _ this, exists_prop, iff_self, mem_principal_sets, function.comp_app]
lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α}
(hs : s ∈ f.sets) (hg : principal (g s) ≤ h) : f.lift' g ≤ h :=
lift_le hs hg
lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ :=
lift_mono hf $ assume s, principal_mono.mpr $ hh s
lemma lift'_mono' (hh : ∀s∈f.sets, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ :=
infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs
lemma lift'_cong (hh : ∀s∈f.sets, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ :=
le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm)
lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) :=
calc map m (f.lift' h) = f.lift (map m ∘ principal ∘ h) :
map_lift_eq $ monotone_comp hh monotone_principal
... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true]
lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) :
(map m f).lift' g = f.lift' (g ∘ image m) :=
map_lift_eq2 $ monotone_comp hg monotone_principal
theorem comap_lift'_eq {m : γ → β} (hh : monotone h) :
comap m (f.lift' h) = f.lift' (preimage m ∘ h) :=
calc comap m (f.lift' h) = f.lift (comap m ∘ principal ∘ h) :
comap_lift_eq $ monotone_comp hh monotone_principal
... = f.lift' (preimage m ∘ h) : by simp only [(∘), filter.lift', comap_principal, eq_self_iff_true]
theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) :
(comap m f).lift' g = f.lift' (g ∘ preimage m) :=
comap_lift_eq2 $ monotone_comp hg monotone_principal
lemma lift'_principal {s : set α} (hh : monotone h) :
(principal s).lift' h = principal (h s) :=
lift_principal $ monotone_comp hh monotone_principal
lemma principal_le_lift' {t : set β} (hh : ∀s∈f.sets, t ⊆ h s) :
principal t ≤ f.lift' h :=
le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs)
theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β}
(hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) :=
assume a b h, lift'_mono (hf h) (hg h)
lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ}
(hg : monotone g) (hh : monotone h) :
(f.lift' g).lift h = f.lift (λs, h (g s)) :=
calc (f.lift' g).lift h = f.lift (λs, (principal (g s)).lift h) :
lift_assoc (monotone_comp hg monotone_principal)
... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true]
lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ}
(hg : monotone g) (hh : monotone h) :
(f.lift' g).lift' h = f.lift' (λs, h (g s)) :=
lift_lift'_assoc hg (monotone_comp hh monotone_principal)
lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ}
(hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) :=
lift_assoc hg
lemma lift_lift'_same_le_lift' {g : set α → set α → set β} :
f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) :=
lift_lift_same_le_lift
lemma lift_lift'_same_eq_lift' {g : set α → set α → set β}
(hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)):
f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) :=
lift_lift_same_eq_lift
(assume s, monotone_comp monotone_id $ monotone_comp (hg₁ s) monotone_principal)
(assume t, monotone_comp (hg₂ t) monotone_principal)
lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} :
f.lift' h ⊓ principal s = f.lift' (λt, h t ∩ s) :=
le_antisymm
(le_infi $ assume t, le_infi $ assume ht,
calc filter.lift' f h ⊓ principal s ≤ principal (h t) ⊓ principal s :
inf_le_inf (infi_le_of_le t $ infi_le _ ht) (le_refl _)
... = _ : by simp only [principal_eq_iff_eq, inf_principal, eq_self_iff_true, function.comp_app])
(le_inf
(le_infi $ assume t, le_infi $ assume ht,
infi_le_of_le t $ infi_le_of_le ht $
by simp only [le_principal_iff, inter_subset_left, mem_principal_sets, function.comp_app]; exact inter_subset_right _ _)
(infi_le_of_le univ $ infi_le_of_le univ_mem_sets $
by simp only [le_principal_iff, inter_subset_right, mem_principal_sets, function.comp_app]; exact inter_subset_left _ _))
lemma lift'_neq_bot_iff (hh : monotone h) : (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, h s ≠ ∅) :=
calc (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, principal (h s) ≠ ⊥) :
lift_neq_bot_iff (monotone_comp hh monotone_principal)
... ↔ (∀s∈f.sets, h s ≠ ∅) : by simp only [principal_eq_bot_iff, iff_self, ne.def, principal_eq_bot_iff]
@[simp] lemma lift'_id {f : filter α} : f.lift' id = f :=
lift_principal2
lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β}
(h_le : ∀s∈f.sets, h s ∈ g.sets) : g ≤ f.lift' h :=
le_infi $ assume s, le_infi $ assume hs, by simp only [h_le, le_principal_iff, function.comp_app]; exact h_le s hs
lemma lift_infi' {f : ι → filter α} {g : set α → filter β}
(hι : nonempty ι) (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) :=
le_antisymm
(le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _))
(assume s,
begin
rw [lift_sets_eq hg],
simp only [mem_Union, exists_imp_distrib, infi_sets_eq hf hι],
exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs
end)
lemma lift'_infi {f : ι → filter α} {g : set α → set β}
(hι : nonempty ι) (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) :=
lift_infi hι $ by simp only [principal_eq_iff_eq, inf_principal, function.comp_app]; apply assume s t, hg
theorem comap_eq_lift' {f : filter β} {m : α → β} :
comap m f = f.lift' (preimage m) :=
filter_eq $ set.ext $ by simp only [mem_lift'_sets, monotone_preimage, comap, exists_prop, forall_const, iff_self, mem_set_of_eq]
end lift'
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/- The product filter cannot be defined using the monad structure on filters. For example:
F := do {x <- seq, y <- top, return (x, y)}
hence:
s ∈ F <-> ∃n, [n..∞] × univ ⊆ s
G := do {y <- top, x <- seq, return (x, y)}
hence:
s ∈ G <-> ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s
Now ⋃i, [i..∞] × {i} is in G but not in F.
As product filter we want to have F as result.
-/
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f.sets) (ht : t ∈ g.sets) : set.prod s t ∈ (filter.prod f g).sets :=
inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ (filter.prod f g).sets ↔ (∃t₁∈f.sets, ∃t₂∈g.sets, set.prod t₁ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩,
⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩,
exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩,
⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩
end
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (filter.prod f g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (filter.prod f g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (filter.prod g h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) :
filter.prod (⨅i, f i) g = (⨅i, filter.prod (f i) g) :=
by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) :
filter.prod f (⨅i, g i) = (⨅i, filter.prod f (g i)) :=
by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
filter.prod f₁ g₁ ≤ filter.prod f₂ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
filter.prod (comap m₁ f₁) (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) :=
by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf]
lemma prod_comm' : filter.prod f g = comap (prod.swap) (filter.prod g f) :=
by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : filter.prod f g = map (λp:β×α, (p.2, p.1)) (filter.prod g f) :=
by rw [prod_comm', ← map_swap_eq_comap_swap]; refl
lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
filter.prod (map m₁ f₁) (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) :=
le_antisymm
(assume s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto_fst.comp (le_refl _)).prod_mk (tendsto_snd.comp (le_refl _)))
lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f.prod g) = (f.map (λa b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
assume s,
split,
exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g :=
have h : _ := map_prod id f g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
filter.prod f₁ g₁ ⊓ filter.prod f₂ g₂ = filter.prod (f₁ ⊓ f₂) (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, lattice.inf_left_comm]
@[simp] lemma prod_bot1 {f : filter α} : filter.prod f (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma prod_bot2 {g : filter β} : filter.prod (⊥ : filter α) g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
filter.prod (principal s) (principal t) = principal (set.prod s t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl
@[simp] lemma prod_pure_pure {a : α} {b : β} : filter.prod (pure a) (pure b) = pure (a, b) :=
by simp
lemma prod_def {f : filter α} {g : filter β} : f.prod g = (f.lift $ λs, g.lift' $ set.prod s) :=
have ∀(s:set α) (t : set β),
principal (set.prod s t) = (principal s).comap prod.fst ⊓ (principal t).comap prod.snd,
by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl,
begin
simp only [filter.lift', function.comp, this, -comap_principal, lift_inf, lift_const, lift_inf],
rw [← comap_lift_eq monotone_principal, ← comap_lift_eq monotone_principal],
simp only [filter.prod, lift_principal2, eq_self_iff_true]
end
lemma prod_same_eq : filter.prod f f = f.lift' (λt, set.prod t t) :=
by rw [prod_def];
from lift_lift'_same_eq_lift'
(assume s, set.monotone_prod monotone_const monotone_id)
(assume t, set.monotone_prod monotone_id monotone_const)
lemma mem_prod_same_iff {s : set (α×α)} :
s ∈ (filter.prod f f).sets ↔ (∃t∈f.sets, set.prod t t ⊆ s) :=
by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id
lemma prod_lift_lift {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂}
(hg₁ : monotone g₁) (hg₂ : monotone g₂) :
filter.prod (f₁.lift g₁) (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, filter.prod (g₁ s) (g₂ t))) :=
begin
simp only [prod_def],
rw [lift_assoc],
apply congr_arg, funext x,
rw [lift_comm],
apply congr_arg, funext y,
rw [lift'_lift_assoc],
exact hg₂,
exact hg₁
end
lemma prod_lift'_lift' {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂}
(hg₁ : monotone g₁) (hg₂ : monotone g₂) :
filter.prod (f₁.lift' g₁) (f₂.lift' g₂) = f₁.lift (λs, f₂.lift' (λt, set.prod (g₁ s) (g₂ t))) :=
begin
rw [prod_def, lift_lift'_assoc],
apply congr_arg, funext x,
rw [lift'_lift'_assoc],
exact hg₂,
exact set.monotone_prod monotone_const monotone_id,
exact hg₁,
exact (monotone_lift' monotone_const $ monotone_lam $
assume x, set.monotone_prod monotone_id monotone_const)
end
lemma prod_neq_bot {f : filter α} {g : filter β} : filter.prod f g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) :=
calc filter.prod f g ≠ ⊥ ↔ (∀s∈f.sets, g.lift' (set.prod s) ≠ ⊥) :
begin
rw [prod_def, lift_neq_bot_iff],
exact (monotone_lift' monotone_const $ monotone_lam $ assume s, set.monotone_prod monotone_id monotone_const)
end
... ↔ (∀s∈f.sets, ∀t∈g.sets, s ≠ ∅ ∧ t ≠ ∅) :
begin
apply forall_congr, intro s,
apply forall_congr, intro hs,
rw [lift'_neq_bot_iff],
apply forall_congr, intro t,
apply forall_congr, intro ht,
rw [set.prod_neq_empty_iff],
exact set.monotone_prod monotone_const monotone_id
end
... ↔ (∀s∈f.sets, s ≠ ∅) ∧ (∀t∈g.sets, t ≠ ∅) :
⟨assume h, ⟨assume s hs, (h s hs univ univ_mem_sets).left,
assume t ht, (h univ univ_mem_sets t ht).right⟩,
assume ⟨h₁, h₂⟩ s hs t ht, ⟨h₁ s hs, h₂ t ht⟩⟩
... ↔ _ : by simp only [forall_sets_neq_empty_iff_neq_bot]
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (filter.prod x y) z ↔
∀ W ∈ z.sets, ∃ U ∈ x.sets, ∃ V ∈ y.sets, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} :
filter.tendsto f (filter.prod x x) y ↔
∀ W ∈ y.sets, ∃ U ∈ x.sets, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W :=
by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self]
end prod
/- at_top and at_bot -/
/-- `at_top` is the filter representing the limit `→ ∞` on an ordered set.
It is generated by the collection of up-sets `{b | a ≤ b}`.
(The preorder need not have a top element for this to be well defined,
and indeed is trivial when a top element exists.) -/
def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b}
/-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set.
It is generated by the collection of down-sets `{b | b ≤ a}`.
(The preorder need not have a bottom element for this to be well defined,
and indeed is trivial when a bottom element exists.) -/
def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a}
lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ (@at_top α _).sets :=
mem_infi_sets a $ subset.refl _
@[simp] lemma at_top_ne_bot [inhabited α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ :=
infi_neq_bot_of_directed (by apply_instance)
(assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩)
(assume a, by simp only [principal_eq_bot_iff, ne.def, principal_eq_bot_iff]; exact ne_empty_of_mem (le_refl a))
@[simp] lemma mem_at_top_sets [inhabited α] [semilattice_sup α] {s : set α} :
s ∈ (at_top : filter α).sets ↔ ∃a:α, ∀b≥a, b ∈ s :=
iff.intro
(assume h, infi_sets_induct h ⟨default α, by simp only [forall_const, mem_univ, forall_true_iff]⟩
(assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b,
assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩)
(assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩))
(assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x)
lemma map_at_top_eq [inhabited α] [semilattice_sup α] {f : α → β} :
at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) :=
calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) :
map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) ⟨default α⟩
... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true]
lemma tendsto_at_top {α β} [partial_order β] (m : α → β) (f : filter α) :
tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f.sets) :=
by simp only [at_top, tendsto_infi, tendsto_principal]; refl
lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) :
tendsto (λs:finset γ, s.image j) at_top at_top :=
tendsto_infi.2 $ assume s, tendsto_infi' (s.image i) $ tendsto_principal_principal.2 $
assume t (ht : s.image i ⊆ t),
calc s = (s.image i).image j :
by simp only [finset.image_image, (∘), h]; exact finset.image_id.symm
... ⊆ t.image j : finset.image_subset_image ht
/- ultrafilter -/
section ultrafilter
open zorn
variables {f g : filter α}
/-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/
def ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g
lemma ultrafilter_pure {a : α} : ultrafilter (pure a) :=
⟨pure_neq_bot,
assume g hg ha,
have {a} ∈ g.sets, by simpa only [le_principal_iff, pure_def] using ha,
show ∀s∈g.sets, {a} ⊆ s, from classical.by_contradiction $
begin
simp only [classical.not_forall, not_imp, exists_imp_distrib, singleton_subset_iff],
exact assume s ⟨hs, hna⟩,
have {a} ∩ s ∈ g.sets, from inter_mem_sets ‹{a} ∈ g.sets› hs,
have ∅ ∈ g.sets, from mem_sets_of_superset this $
assume x ⟨hxa, hxs⟩, begin simp only [set.mem_singleton_iff] at hxa; simp only [hxa] at hxs, exact hna hxs end,
have g = ⊥, from empty_in_sets_eq_bot.mp this,
hg this
end⟩
lemma ultrafilter_unique (hg : ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g :=
le_antisymm h (hg.right _ hf h)
lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ ultrafilter u :=
let
τ := {f' // f' ≠ ⊥ ∧ f' ≤ f},
r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val,
⟨a, ha⟩ := inhabited_of_mem_sets h univ_mem_sets,
top : τ := ⟨f, h, le_refl f⟩,
sup : Π(c:set τ), chain r c → τ :=
λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val,
infi_neq_bot_of_directed ⟨a⟩
(directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb)
(assume ⟨⟨a, ha, _⟩, _⟩, ha),
infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩
in
have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc),
from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _),
have (∃ (u : τ), ∀ (a : τ), r u a → r a u),
from zorn (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁),
let ⟨uτ, hmin⟩ := this in
⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂,
hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩
lemma le_of_ultrafilter {g : filter α} (hf : ultrafilter f) (h : f ⊓ g ≠ ⊥) :
f ≤ g :=
le_of_inf_eq $ ultrafilter_unique hf h inf_le_left
lemma mem_or_compl_mem_of_ultrafilter (hf : ultrafilter f) (s : set α) :
s ∈ f.sets ∨ - s ∈ f.sets :=
classical.or_iff_not_imp_right.2 $ assume : - s ∉ f.sets,
have f ≤ principal s, from
le_of_ultrafilter hf $ assume h, this $ mem_sets_of_neq_bot $
by simp only [h, eq_self_iff_true, lattice.neg_neg],
by simp only [le_principal_iff] at this; assumption
lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : ultrafilter f) (h : s ∪ t ∈ f.sets) :
s ∈ f.sets ∨ t ∈ f.sets :=
(mem_or_compl_mem_of_ultrafilter hf s).imp_right
(assume : -s ∈ f.sets, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx)
lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : ultrafilter f) (hs : finite s)
: ⋃₀ s ∈ f.sets → ∃t∈s, t ∈ f.sets :=
finite.induction_on hs (by simp only [empty_in_sets_eq_bot, hf.left, mem_empty_eq, sUnion_empty,
forall_prop_of_false, exists_false, not_false_iff, exists_prop_of_false]) $
λ t s' ht' hs' ih, by simp only [exists_prop, mem_insert_iff, set.sUnion_insert]; exact
assume h, (mem_or_mem_of_ultrafilter hf h).elim
(assume : t ∈ f.sets, ⟨t, or.inl rfl, this⟩)
(assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩)
lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α}
(hf : ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f.sets) : ∃i∈is, s i ∈ f.sets :=
have his : finite (image s is), from finite_image s his,
have h : (⋃₀ image s is) ∈ f.sets, from by simp only [sUnion_image, set.sUnion_image]; assumption,
let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f.sets)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in
⟨i, hi, h_eq.symm ▸ ht⟩
lemma ultrafilter_of_split {f : filter α} (hf : f ≠ ⊥) (h : ∀s, s ∈ f.sets ∨ - s ∈ f.sets) :
ultrafilter f :=
⟨hf, assume g hg g_le s hs, (h s).elim id $
assume : - s ∈ f.sets,
have s ∩ -s ∈ g.sets, from inter_mem_sets hs (g_le this),
by simp only [empty_in_sets_eq_bot, hg, inter_compl_self] at this; contradiction⟩
lemma ultrafilter_map {f : filter α} {m : α → β} (h : ultrafilter f) : ultrafilter (map m f) :=
ultrafilter_of_split (by simp only [map_eq_bot_iff, h.left, ne.def, map_eq_bot_iff, not_false_iff]) $
assume s, show preimage m s ∈ f.sets ∨ - preimage m s ∈ f.sets,
from mem_or_compl_mem_of_ultrafilter h (preimage m s)
/-- Construct an ultrafilter extending a given filter.
The ultrafilter lemma is the assertion that such a filter exists;
we use the axiom of choice to pick one. -/
noncomputable def ultrafilter_of (f : filter α) : filter α :=
if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ ultrafilter u)
lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ ultrafilter (ultrafilter_of f) :=
begin
have h' := classical.epsilon_spec (exists_ultrafilter h),
simp only [ultrafilter_of, dif_neg, h, dif_neg, not_false_iff],
simp only at h',
assumption
end
lemma ultrafilter_of_le : ultrafilter_of f ≤ f :=
if h : f = ⊥ then by simp only [ultrafilter_of, dif_pos, h, dif_pos, eq_self_iff_true, le_bot_iff]; exact le_refl _
else (ultrafilter_of_spec h).left
lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : ultrafilter (ultrafilter_of f) :=
(ultrafilter_of_spec h).right
lemma ultrafilter_of_ultrafilter (h : ultrafilter f) : ultrafilter_of f = f :=
ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le
end ultrafilter
end filter
namespace filter
variables {α β γ : Type u} {f : β → filter α} {s : γ → set α}
open list
lemma mem_traverse_sets :
∀(fs : list β) (us : list γ),
forall₂ (λb c, s c ∈ (f b).sets) fs us → traverse s us ∈ (traverse f fs).sets
| [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _
| (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs)
lemma mem_traverse_sets_iff (fs : list β) (t : set (list α)) :
t ∈ (traverse f fs).sets ↔
(∃us:list (set α), forall₂ (λb (s : set α), s ∈ (f b).sets) fs us ∧ sequence us ⊆ t) :=
begin
split,
{ induction fs generalizing t,
case nil { simp only [sequence, pure_def, imp_self, forall₂_nil_left_iff, pure_def,
exists_eq_left, mem_principal_sets, set.pure_def, singleton_subset_iff, traverse_nil] },
case cons : b fs ih t {
assume ht,
rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩,
rcases ih v hv with ⟨us, hus, hu⟩,
exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } },
{ rintros ⟨us, hus, hs⟩,
exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs }
end
lemma sequence_mono :
∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs
| [] [] forall₂.nil := le_refl _
| (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs)
end filter |
8b3f8bab9eaae3270eb742cb0e235d321996be46 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /leanpkg/leanpkg/lean_version.lean | c1a7eddf6a908a0d432f656030b3230c93a7d1be | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 550 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Gabriel Ebner
-/
import leanpkg.config_vars
namespace leanpkg
open lean_version
def lean_version_string_core :=
major.repr ++ "." ++ minor.repr ++ "." ++ patch.repr
def lean_version_string :=
if is_release then
lean_version_string_core
else
"master"
def ui_lean_version_string :=
if is_release then
lean_version_string_core
else
"master (" ++ lean_version_string_core ++ ")"
end leanpkg |
3850ba1ffefc61b531d91fbd09b9ccf9e85657fb | 26bff4ed296b8373c92b6b025f5d60cdf02104b9 | /tests/lean/run/generalizes.lean | cb4b8d8e29aced854d32241691fc1d2b540c1cb0 | [
"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 | 159 | lean | import logic
theorem tst (A B : Type) (a : A) (b : B) : a == b → b == a :=
begin
generalizes (a, b, B),
intros (B', b, a, H),
apply (heq.symm H),
end
|
8f30bd85f17580a4ffd408e1cb1ec56d3dd61c47 | bb31430994044506fa42fd667e2d556327e18dfe | /src/topology/dense_embedding.lean | 261524bd0b5d815f81ccee3947ebe83b17522331 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 15,465 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import topology.separation
import topology.bases
/-!
# Dense embeddings
This file defines three properties of functions:
* `dense_range f` means `f` has dense image;
* `dense_inducing i` means `i` is also `inducing`;
* `dense_embedding e` means `e` is also an `embedding`.
The main theorem `continuous_extend` gives a criterion for a function
`f : X → Z` to a T₃ space Z to extend along a dense embedding
`i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only
has to be `dense_inducing` (not necessarily injective).
-/
noncomputable theory
open set filter
open_locale classical topological_space filter
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
/-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α`
is the one induced by `i` from the topology on `β`. -/
@[protect_proj] structure dense_inducing [topological_space α] [topological_space β] (i : α → β)
extends inducing i : Prop :=
(dense : dense_range i)
namespace dense_inducing
variables [topological_space α] [topological_space β]
variables {i : α → β} (di : dense_inducing i)
lemma nhds_eq_comap (di : dense_inducing i) :
∀ a : α, 𝓝 a = comap i (𝓝 $ i a) :=
di.to_inducing.nhds_eq_comap
protected lemma continuous (di : dense_inducing i) : continuous i :=
di.to_inducing.continuous
lemma closure_range : closure (range i) = univ :=
di.dense.closure_range
protected lemma preconnected_space [preconnected_space α] (di : dense_inducing i) :
preconnected_space β :=
di.dense.preconnected_space di.continuous
lemma closure_image_mem_nhds {s : set α} {a : α} (di : dense_inducing i) (hs : s ∈ 𝓝 a) :
closure (i '' s) ∈ 𝓝 (i a) :=
begin
rw [di.nhds_eq_comap a, ((nhds_basis_opens _).comap _).mem_iff] at hs,
rcases hs with ⟨U, ⟨haU, hUo⟩, sub : i ⁻¹' U ⊆ s⟩,
refine mem_of_superset (hUo.mem_nhds haU) _,
calc U ⊆ closure (i '' (i ⁻¹' U)) : di.dense.subset_closure_image_preimage_of_is_open hUo
... ⊆ closure (i '' s) : closure_mono (image_subset i sub)
end
lemma dense_image (di : dense_inducing i) {s : set α} : dense (i '' s) ↔ dense s :=
begin
refine ⟨λ H x, _, di.dense.dense_image di.continuous⟩,
rw [di.to_inducing.closure_eq_preimage_closure_image, H.closure_eq, preimage_univ],
trivial
end
/-- If `i : α → β` is a dense embedding with dense complement of the range, then any compact set in
`α` has empty interior. -/
lemma interior_compact_eq_empty [t2_space β] (di : dense_inducing i) (hd : dense (range i)ᶜ)
{s : set α} (hs : is_compact s) : interior s = ∅ :=
begin
refine eq_empty_iff_forall_not_mem.2 (λ x hx, _),
rw [mem_interior_iff_mem_nhds] at hx,
have := di.closure_image_mem_nhds hx,
rw (hs.image di.continuous).is_closed.closure_eq at this,
rcases hd.inter_nhds_nonempty this with ⟨y, hyi, hys⟩,
exact hyi (image_subset_range _ _ hys)
end
/-- The product of two dense inducings is a dense inducing -/
protected lemma prod [topological_space γ] [topological_space δ]
{e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) :
dense_inducing (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ induced := (de₁.to_inducing.prod_mk de₂.to_inducing).induced,
dense := de₁.dense.prod_map de₂.dense }
open topological_space
/-- If the domain of a `dense_inducing` map is a separable space, then so is the codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
di.dense.separable_space di.continuous
variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β}
/--
```
γ -f→ α
g↓ ↓e
δ -h→ β
```
-/
lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (di : dense_inducing i)
(H : tendsto h (𝓝 d) (𝓝 (i a))) (comm : h ∘ g = i ∘ f) : tendsto f (comap g (𝓝 d)) (𝓝 a) :=
begin
have lim1 : map g (comap g (𝓝 d)) ≤ 𝓝 d := map_comap_le,
replace lim1 : map h (map g (comap g (𝓝 d))) ≤ map h (𝓝 d) := map_mono lim1,
rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1,
have lim2 : comap i (map h (𝓝 d)) ≤ comap i (𝓝 (i a)) := comap_mono H,
rw ← di.nhds_eq_comap at lim2,
exact le_trans lim1 lim2,
end
protected lemma nhds_within_ne_bot (di : dense_inducing i) (b : β) :
ne_bot (𝓝[range i] b) :=
di.dense.nhds_within_ne_bot b
lemma comap_nhds_ne_bot (di : dense_inducing i) (b : β) : ne_bot (comap i (𝓝 b)) :=
comap_ne_bot $ λ s hs,
let ⟨_, ⟨ha, a, rfl⟩⟩ := mem_closure_iff_nhds.1 (di.dense b) s hs in ⟨a, ha⟩
variables [topological_space γ]
/-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends"
to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a
continuous extension, then `g` is the unique such extension. In general,
`g` might not be continuous or even extend `f`. -/
def extend (di : dense_inducing i) (f : α → γ) (b : β) : γ :=
@@lim _ ⟨f (di.dense.some b)⟩ (comap i (𝓝 b)) f
lemma extend_eq_of_tendsto [t2_space γ] {b : β} {c : γ} {f : α → γ}
(hf : tendsto f (comap i (𝓝 b)) (𝓝 c)) :
di.extend f b = c :=
by haveI := di.comap_nhds_ne_bot; exact hf.lim_eq
lemma extend_eq_at [t2_space γ] {f : α → γ} {a : α} (hf : continuous_at f a) :
di.extend f (i a) = f a :=
extend_eq_of_tendsto _ $ di.nhds_eq_comap a ▸ hf
lemma extend_eq_at' [t2_space γ] {f : α → γ} {a : α} (c : γ) (hf : tendsto f (𝓝 a) (𝓝 c)) :
di.extend f (i a) = f a :=
di.extend_eq_at (continuous_at_of_tendsto_nhds hf)
lemma extend_eq [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) :
di.extend f (i a) = f a :=
di.extend_eq_at hf.continuous_at
/-- Variation of `extend_eq` where we ask that `f` has a limit along `comap i (𝓝 b)` for each
`b : β`. This is a strictly stronger assumption than continuity of `f`, but in a lot of cases
you'd have to prove it anyway to use `continuous_extend`, so this avoids doing the work twice. -/
lemma extend_eq' [t2_space γ] {f : α → γ}
(di : dense_inducing i) (hf : ∀ b, ∃ c, tendsto f (comap i (𝓝 b)) (𝓝 c)) (a : α) :
di.extend f (i a) = f a :=
begin
rcases hf (i a) with ⟨b, hb⟩,
refine di.extend_eq_at' b _,
rwa ← di.to_inducing.nhds_eq_comap at hb,
end
lemma extend_unique_at [t2_space γ] {b : β} {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in comap i (𝓝 b), g (i x) = f x) (hg : continuous_at g b) :
di.extend f b = g b :=
begin
refine di.extend_eq_of_tendsto (λ s hs, mem_map.2 _),
suffices : ∀ᶠ (x : α) in comap i (𝓝 b), g (i x) ∈ s,
from hf.mp (this.mono $ λ x hgx hfx, hfx ▸ hgx),
clear hf f,
refine eventually_comap.2 ((hg.eventually hs).mono _),
rintros _ hxs x rfl,
exact hxs
end
lemma extend_unique [t2_space γ] {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ x, g (i x) = f x) (hg : continuous g) :
di.extend f = g :=
funext $ λ b, extend_unique_at di (eventually_of_forall hf) hg.continuous_at
lemma continuous_at_extend [t3_space γ] {b : β} {f : α → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in 𝓝 b, ∃c, tendsto f (comap i $ 𝓝 x) (𝓝 c)) :
continuous_at (di.extend f) b :=
begin
set φ := di.extend f,
haveI := di.comap_nhds_ne_bot,
suffices : ∀ V' ∈ 𝓝 (φ b), is_closed V' → φ ⁻¹' V' ∈ 𝓝 b,
by simpa [continuous_at, (closed_nhds_basis _).tendsto_right_iff],
intros V' V'_in V'_closed,
set V₁ := {x | tendsto f (comap i $ 𝓝 x) (𝓝 $ φ x)},
have V₁_in : V₁ ∈ 𝓝 b,
{ filter_upwards [hf],
rintros x ⟨c, hc⟩,
dsimp [V₁, φ],
rwa di.extend_eq_of_tendsto hc },
obtain ⟨V₂, V₂_in, V₂_op, hV₂⟩ : ∃ V₂ ∈ 𝓝 b, is_open V₂ ∧ ∀ x ∈ i ⁻¹' V₂, f x ∈ V',
{ simpa [and_assoc] using ((nhds_basis_opens' b).comap i).tendsto_left_iff.mp
(mem_of_mem_nhds V₁_in : b ∈ V₁) V' V'_in },
suffices : ∀ x ∈ V₁ ∩ V₂, φ x ∈ V',
{ filter_upwards [inter_mem V₁_in V₂_in] using this, },
rintros x ⟨x_in₁, x_in₂⟩,
have hV₂x : V₂ ∈ 𝓝 x := is_open.mem_nhds V₂_op x_in₂,
apply V'_closed.mem_of_tendsto x_in₁,
use V₂,
tauto,
end
lemma continuous_extend [t3_space γ] {f : α → γ} (di : dense_inducing i)
(hf : ∀b, ∃c, tendsto f (comap i (𝓝 b)) (𝓝 c)) : continuous (di.extend f) :=
continuous_iff_continuous_at.mpr $ assume b, di.continuous_at_extend $ univ_mem' hf
lemma mk'
(i : α → β)
(c : continuous i)
(dense : ∀x, x ∈ closure (range i))
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (i a), ∀ b, i b ∈ t → b ∈ s) :
dense_inducing i :=
{ induced := (induced_iff_nhds_eq i).2 $
λ a, le_antisymm (tendsto_iff_comap.1 $ c.tendsto _) (by simpa [filter.le_def] using H a),
dense := dense }
end dense_inducing
/-- A dense embedding is an embedding with dense image. -/
structure dense_embedding [topological_space α] [topological_space β] (e : α → β)
extends dense_inducing e : Prop :=
(inj : function.injective e)
theorem dense_embedding.mk'
[topological_space α] [topological_space β] (e : α → β)
(c : continuous e)
(dense : dense_range e)
(inj : function.injective e)
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (e a), ∀ b, e b ∈ t → b ∈ s) :
dense_embedding e :=
{ inj := inj,
..dense_inducing.mk' e c dense H}
namespace dense_embedding
open topological_space
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
variables {e : α → β} (de : dense_embedding e)
lemma inj_iff {x y} : e x = e y ↔ x = y := de.inj.eq_iff
lemma to_embedding : embedding e :=
{ induced := de.induced,
inj := de.inj }
/-- If the domain of a `dense_embedding` is a separable space, then so is its codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
de.to_dense_inducing.separable_space
/-- The product of two dense embeddings is a dense embedding. -/
protected lemma prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁)
(de₂ : dense_embedding e₂) :
dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩,
by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩,
..dense_inducing.prod de₁.to_dense_inducing de₂.to_dense_inducing }
/-- The dense embedding of a subtype inside its closure. -/
@[simps] def subtype_emb {α : Type*} (p : α → Prop) (e : α → β) (x : {x // p x}) :
{x // x ∈ closure (e '' {x | p x})} :=
⟨e x, subset_closure $ mem_image_of_mem e x.prop⟩
protected lemma subtype (p : α → Prop) : dense_embedding (subtype_emb p e) :=
{ dense := dense_iff_closure_eq.2 $
begin
ext ⟨x, hx⟩,
rw image_eq_range at hx,
simpa [closure_subtype, ← range_comp, (∘)],
end,
inj := (de.inj.comp subtype.coe_injective).cod_restrict _,
induced := (induced_iff_nhds_eq _).2 (assume ⟨x, hx⟩,
by simp [subtype_emb, nhds_subtype_eq_comap, de.to_inducing.nhds_eq_comap, comap_comap, (∘)]) }
lemma dense_image {s : set α} : dense (e '' s) ↔ dense s :=
de.to_dense_inducing.dense_image
end dense_embedding
lemma dense_embedding_id {α : Type*} [topological_space α] : dense_embedding (id : α → α) :=
{ dense := dense_range_id,
.. embedding_id }
lemma dense.dense_embedding_coe [topological_space α] {s : set α} (hs : dense s) :
dense_embedding (coe : s → α) :=
{ dense := hs.dense_range_coe,
.. embedding_subtype_coe }
lemma is_closed_property [topological_space β] {e : α → β} {p : β → Prop}
(he : dense_range e) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) :
∀b, p b :=
have univ ⊆ {b | p b},
from calc univ = closure (range e) : he.closure_range.symm
... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h
... = _ : hp.closure_eq,
assume b, this trivial
lemma is_closed_property2 [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) :
∀b₁ b₂, p b₁ b₂ :=
have ∀q:β×β, p q.1 q.2,
from is_closed_property (he.prod_map he) hp $ λ _, h _ _,
assume b₁ b₂, this ⟨b₁, b₂⟩
lemma is_closed_property3 [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) :
∀b₁ b₂ b₃, p b₁ b₂ b₃ :=
have ∀q:β×β×β, p q.1 q.2.1 q.2.2,
from is_closed_property (he.prod_map $ he.prod_map he) hp $ λ _, h _ _ _,
assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩
@[elab_as_eliminator]
lemma dense_range.induction_on [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop}
(b₀ : β) (hp : is_closed {b | p b}) (ih : ∀a:α, p $ e a) : p b₀ :=
is_closed_property he hp ih b₀
@[elab_as_eliminator]
lemma dense_range.induction_on₂ [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂))
(b₁ b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h _ _
@[elab_as_eliminator]
lemma dense_range.induction_on₃ [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃))
(b₁ b₂ b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h _ _ _
section
variables [topological_space β] [topological_space γ] [t2_space γ]
variables {f : α → β}
/-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/
lemma dense_range.equalizer (hfd : dense_range f)
{g h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) :
g = h :=
funext $ λ y, hfd.induction_on y (is_closed_eq hg hh) $ congr_fun H
end
-- Bourbaki GT III §3 no.4 Proposition 7 (generalised to any dense-inducing map to a T₃ space)
lemma filter.has_basis.has_basis_of_dense_inducing
[topological_space α] [topological_space β] [t3_space β]
{ι : Type*} {s : ι → set α} {p : ι → Prop} {x : α} (h : (𝓝 x).has_basis p s)
{f : α → β} (hf : dense_inducing f) :
(𝓝 (f x)).has_basis p $ λ i, closure $ f '' (s i) :=
begin
rw filter.has_basis_iff at h ⊢,
intros T,
refine ⟨λ hT, _, λ hT, _⟩,
{ obtain ⟨T', hT₁, hT₂, hT₃⟩ := exists_mem_nhds_is_closed_subset hT,
have hT₄ : f⁻¹' T' ∈ 𝓝 x,
{ rw hf.to_inducing.nhds_eq_comap x,
exact ⟨T', hT₁, subset.rfl⟩, },
obtain ⟨i, hi, hi'⟩ := (h _).mp hT₄,
exact ⟨i, hi, (closure_mono (image_subset f hi')).trans (subset.trans (closure_minimal
(image_subset_iff.mpr subset.rfl) hT₂) hT₃)⟩, },
{ obtain ⟨i, hi, hi'⟩ := hT,
suffices : closure (f '' s i) ∈ 𝓝 (f x), { filter_upwards [this] using hi', },
replace h := (h (s i)).mpr ⟨i, hi, subset.rfl⟩,
exact hf.closure_image_mem_nhds h, },
end
|
cc9d52fec319cc81ae386005777df545c914d078 | 8be24982c807641260370bd09243eac768750811 | /src/liouville_theorem.lean | fc8b885971e897fc919d97e71f508b5f4f414b81 | [] | no_license | jjaassoonn/transcendental | 8008813253af3aa80b5a5c56551317e7ab4246ab | 99bc6ea6089f04ed90a0f55f0533ebb7f47b22ff | refs/heads/master | 1,607,869,957,944 | 1,599,659,687,000 | 1,599,659,687,000 | 234,444,826 | 9 | 1 | null | 1,598,218,307,000 | 1,579,224,232,000 | HTML | UTF-8 | Lean | false | false | 53,343 | lean | import ring_theory.algebraic
import topology.algebra.polynomial
import analysis.calculus.mean_value
import small_things
noncomputable theory
open_locale classical
notation α`[X]` := polynomial α
notation `transcendental` x := ¬(is_algebraic ℤ x)
/--
- a number is transcendental ↔ a number is not algebraic
- a Liouville's number $x$ is a number such that
for every natural number, there is a rational number a/b such that 0 < |x - a/b| < 1/bⁿ
-/
def liouville_number (x : ℝ) := ∀ n : ℕ, ∃ a b : ℤ, b > 1 ∧ 0 < abs(x - a / b) ∧ abs(x - a / b) < 1/b^n
-- x ↦ |f(x)| is continuous
theorem abs_f_eval_around_α_continuous (f : ℝ[X]) (α : ℝ) : continuous_on (λ x : ℝ, (abs (f.eval x))) (set.Icc (α-1) (α+1)) :=
begin
have H : (λ x : ℝ, (abs (f.eval x))) = abs ∘ (λ x, f.eval x) := rfl,
rw H,
have H2 := polynomial.continuous_eval f,
have H3 := continuous.comp real.continuous_abs H2,
exact continuous.continuous_on H3
end
/--
The next two lemmas state coefficient (support resp.) of $f ∈ ℤ[T]$ is the same as $f ∈ ℝ[T]$.
-/
private lemma same_coeff (f : ℤ[X]) (n : ℕ): ℤembℝ (f.coeff n) = ((f.map ℤembℝ).coeff n) :=
begin
simp only [ring_hom.eq_int_cast, polynomial.coeff_map],
end
private lemma same_support (f : ℤ[X]) : f.support = (f.map ℤembℝ).support :=
begin
ext, split,
{
intro ha, replace ha : f.coeff a ≠ 0, exact finsupp.mem_support_iff.mp ha,
have ineq1 : ℤembℝ (f.coeff a) ≠ 0, norm_num, exact ha,
suffices : (polynomial.map ℤembℝ f).coeff a ≠ 0, exact finsupp.mem_support_iff.mpr this,
rwa [polynomial.coeff_map],
},
{
intro ha, replace ha : (polynomial.map ℤembℝ f).coeff a ≠ 0, exact finsupp.mem_support_iff.mp ha,
rw [<-same_coeff] at ha,
have ineq1 : f.coeff a ≠ 0, simp only [int.cast_eq_zero, ring_hom.eq_int_cast, ne.def] at ha, exact ha,
exact finsupp.mem_support_iff.mpr ineq1,
}
end
open_locale big_operators
private lemma sum_eq (S : finset ℕ) (f g : ℕ -> ℝ) : (∀ x ∈ S, f x = g x) -> ∑ i in S, f i = ∑ i in S, g i :=
λ h, @finset.sum_congr _ _ S S f g _ rfl h
private lemma eval_f_a_div_b (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
((f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ))) = ((1:ℝ)/(b:ℝ)^f.nat_degree) * (∑ i in f.support, (f.coeff i : ℝ)*(a:ℝ)^i*(b:ℝ)^(f.nat_degree - i)) :=
begin
rw [finset.mul_sum, polynomial.eval_map, polynomial.eval₂, finsupp.sum, sum_eq], intros i hi,
rw polynomial.apply_eq_coeff,
simp only [one_div, ring_hom.eq_int_cast, div_pow], rw [<-mul_assoc (↑b ^ f.nat_degree)⁻¹, <-mul_assoc (↑b ^ f.nat_degree)⁻¹],
field_simp,
suffices eq : ↑a ^ i / ↑b ^ i = ↑a ^ i * ↑b ^ (f.nat_degree - i) / ↑b ^ f.nat_degree,
{
have H := (@mul_left_inj' ℝ _ (↑a ^ i / ↑b ^ i) (↑a ^ i * ↑b ^ (f.nat_degree - i) / ↑b ^ f.nat_degree) ↑(f.coeff i) _).2 eq,
conv_lhs at H {rw mul_comm, rw <-mul_div_assoc}, conv_rhs at H {rw mul_comm}, rw H, ring,
have H := (f.3 i).1 hi, rw polynomial.coeff, norm_cast, exact H,
},
have eq1 := @fpow_sub ℝ _ (b:ℝ) _ (f.nat_degree - i) f.nat_degree,
have eq2 : (f.nat_degree:ℤ) - (i:ℤ) - (f.nat_degree:ℤ) = - i,
{
rw [sub_sub, add_comm, <-sub_sub], simp only [zero_sub, sub_self],
},
rw eq2 at eq1, rw [fpow_neg, inv_eq_one_div] at eq1,
have eqb1 : (b:ℝ) ^ (i:ℤ) = (b:ℝ) ^ i := by norm_num,
rw eqb1 at eq1,
have eq3 : (f.nat_degree : ℤ) - (i:ℤ) = int.of_nat (f.nat_degree - i),
{
norm_num, rw int.coe_nat_sub, by_contra rid, simp at rid,
have hi' := polynomial.coeff_eq_zero_of_nat_degree_lt rid,
have ineq2 := (f.3 i).1 hi, exact a_div_b_not_root (false.rec (polynomial.eval (↑a / ↑b) (polynomial.map ℤembℝ f) = 0) (ineq2 hi')),
},
have eqb2 : (b:ℝ) ^ ((f.nat_degree:ℤ) - (i:ℤ)) = (b:ℝ) ^ (f.nat_degree - i),
{
rw eq3, norm_num,
}, rw eqb2 at eq1,
have eqb3 : (b:ℝ)^(f.nat_degree:ℤ)= (b:ℝ)^f.nat_degree := by norm_num, rw eqb3 at eq1, conv_rhs {rw [mul_div_assoc, <-eq1]},
simp only [one_div], rw <-div_eq_mul_inv,
norm_cast, linarith,
end
private lemma cast1 (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
∑ i in f.support, (f.coeff i : ℝ)*(a:ℝ)^i*(b:ℝ)^(f.nat_degree - i) = (ℤembℝ (∑ i in f.support, (f.coeff i) * a^i * b ^ (f.nat_degree - i))) :=
begin
rw ring_hom.map_sum, rw sum_eq, intros i hi, norm_num,
end
private lemma ineq1 (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
∑ i in f.support, (f.coeff i) * a^i * b ^ (f.nat_degree - i) ≠ 0 :=
begin
suffices eq1 : ℤembℝ (∑ i in f.support, (f.coeff i) * a^i * b ^ (f.nat_degree - i)) ≠ 0,
{
intro rid, rw rid at eq1,
conv_rhs at eq1 {rw <-ℤembℝ_zero}, simpa only [],
},
intro rid,
have cast1 := cast1 f f_deg a b b_non_zero a_div_b_not_root, rw rid at cast1,
have eq2 := (@mul_right_inj' ℝ _ ((1:ℝ)/(b:ℝ)^f.nat_degree) _ _ _).2 cast1,
rw <-eval_f_a_div_b at eq2, simp only [mul_zero] at eq2, exact a_div_b_not_root eq2,
exact f_deg, exact b_non_zero, exact a_div_b_not_root,
intro rid, norm_num at rid, replace rid := pow_eq_zero rid, norm_cast at rid, linarith,
end
private lemma ineq2 (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
ℤembℝ (∑ i in f.support, (f.coeff i) * a^i * b ^ (f.nat_degree - i)) ≠ 0 :=
begin
have cast1 := cast1 f f_deg a b b_non_zero a_div_b_not_root,
have ineq1 := ineq1 f f_deg a b b_non_zero a_div_b_not_root,
norm_cast, intro rid, rw <-ℤembℝ_zero at rid, replace rid := ℤembℝ_inj rid,
exact a_div_b_not_root (false.rec (polynomial.eval (↑a / ↑b) (polynomial.map ℤembℝ f) = 0) (ineq1 rid)),
end
private lemma ineqb (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
abs (1/(b:ℝ)^f.nat_degree) = 1/(b:ℝ)^f.nat_degree :=
begin
rw abs_of_pos, norm_num, norm_cast, refine pow_pos _ f.nat_degree, exact b_non_zero,
end
private lemma abs_ℤembℝ (x : ℤ) : ℤembℝ (abs x) = abs (ℤembℝ x) := by simp only [ring_hom.eq_int_cast, int.cast_abs]
private lemma ineq3 (x : ℤ) (hx : x ≥ 1) : ℤembℝ x ≥ 1 :=
begin
simp only [ring_hom.eq_int_cast, ge_iff_le], norm_cast, exact hx,
end
/--
For an integer polynomial f of degree n > 1, if a/b is not a root of f then |f(a/b)| ≥ 1/bⁿ. (wlog b > 0).
If we write `f(T) = ∑ λᵢ Tⁱ`
This is because `|f(a/b)|=|∑ λᵢ aⁱ/bⁱ| = (1/bⁿ) |∑ λᵢ aⁱ b^(n-i)|`, but a/b is not a root, so `|∑ λᵢ aⁱ b^(n-i)| ≥ 1`.
- `ineq1` proves that if a/b is not a root of f then `∑ λᵢ aⁱ b^(n-i) ≠ 0`
-/
theorem abs_f_at_p_div_q_ge_1_div_q_pow_n (f : ℤ[X]) (f_deg : f.nat_degree > 1) (a b : ℤ) (b_non_zero : b > 0) (a_div_b_not_root : (f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ)) ≠ 0) :
@abs ℝ _ ((f.map ℤembℝ).eval ((a:ℝ)/(b:ℝ))) ≥ 1/(b:ℝ)^f.nat_degree :=
begin
have eval1 := eval_f_a_div_b f f_deg a b b_non_zero a_div_b_not_root, -- This is `f(a/b)=∑ λᵢ aⁱ/bⁱ`
have cast1 := cast1 f f_deg a b b_non_zero a_div_b_not_root, -- This is not maths, but casting types
have ineq1 := ineq1 f f_deg a b b_non_zero a_div_b_not_root, -- This is `∑ λᵢ aⁱ b^(n-i) ≠ 0`
have ineq2 := ineq2 f f_deg a b b_non_zero a_div_b_not_root, -- This is the same but casting types
rw [eval1, abs_mul, ineqb f f_deg a b b_non_zero a_div_b_not_root, cast1],
/-
To prove |f(a/b)| ≥ 1/bⁿ we just need to prove |∑ λᵢ aⁱ b^(n-i)| ≥ 1
-/
suffices ineq3 : abs (ℤembℝ (∑ (i : ℕ) in f.support, f.coeff i * a ^ i * b ^ (f.nat_degree - i))) ≥ 1,
{
rw ge_iff_le,
-- We are manipulating inequalities involving multiplication so we use mul_le_mul
-- mul_le_mul (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d
-- Here mul_le_mul needs two things to be positive and the positivity is proved after the main point is proved.
have H := @mul_le_mul ℝ _ 1 (1/(b:ℝ)^f.nat_degree) (abs (ℤembℝ (∑ (i : ℕ) in f.support, f.coeff i * a ^ i * b ^ (f.nat_degree - i)))) (1/(b:ℝ)^f.nat_degree) ineq3 _ _ _,
rw one_mul at H, rw mul_comm at H, exact H,
exact le_refl (1 / ↑b ^ polynomial.nat_degree f),
-- here is the proof that the things should be positive are positive. All are more or less trival
norm_num, replace H := @pow_nonneg ℝ _ b _ f.nat_degree, exact H,
norm_cast, exact ge_trans b_non_zero trivial, exact abs_nonneg _,
},
rw <-abs_ℤembℝ, apply ineq3,
have ineq4 : abs (∑ (i : ℕ) in f.support, f.coeff i * a ^ i * b ^ (f.nat_degree - i)) > 0 := abs_pos_iff.2 ineq1,
exact ineq4,
end
/--
This is the main lemma :
Let α be an irrational number which is a root of f(x) = Σ aⱼ Xᶨ ∈ Z[T] with f(x) ≢ 0.
Then there is a constant A = A(α) > 0 such that:
if a and b are integers with b > 0, then |α - a / b| > A / b^n
N.B. So neccessarily f has degree > 1, otherwise α is rational. But it doesn't hurt if we assume
f to be an integer polynomial of degree > 1.
-/
lemma about_irrational_root
(α : real) (hα : irrational α) (f : ℤ[X])
(f_deg : f.nat_degree > 1) (α_root : f_eval_on_ℝ f α = 0) :
∃ A : real, A > 0 ∧ ∀ a b : ℤ, b > 0 -> abs(α - a / b) > (A / b ^ (f.nat_degree)) :=
begin
have f_nonzero : f ≠ 0, -- f ∈ ℤ[T] is not zero
{
by_contra rid,
simp only [not_not] at rid, have f_nat_deg_zero : f.nat_degree = 0,
exact (congr_arg polynomial.nat_degree rid).trans rfl,
rw f_nat_deg_zero at f_deg, linarith,
},
generalize hfℝ: f.map ℤembℝ = f_ℝ,
have hfℝ_nonzero : f_ℝ ≠ 0, -- f ∈ ℝ[T] is not zero
{
by_contra absurd, simp only [not_not] at absurd, rw [polynomial.ext_iff] at absurd,
suffices : f = 0, exact f_nonzero this,
ext, replace absurd := absurd n, simp only [polynomial.coeff_zero] at absurd ⊢,
rw [<-hfℝ, polynomial.coeff_map, ℤembℝ] at absurd,
simp only [int.cast_eq_zero, ring_hom.eq_int_cast] at absurd, exact absurd,
},
generalize hDf: f_ℝ.derivative = Df_ℝ, -- We use Df_ℝ to denote the (formal) derivative of f ∈ ℝ[T]
have H := is_compact.exists_forall_ge (@compact_Icc (α-1) (α+1)) -- x ↦ abs |Df_ℝ| has a maximum on interval (α - 1, α + 1)
begin rw set.nonempty, use α, rw set.mem_Icc, split; linarith end
(abs_f_eval_around_α_continuous Df_ℝ α),
choose x_max hx_max using H, -- Say the maximum is attained at x_max with M = Df_ℝ(x_max)
generalize M_def: abs (Df_ℝ.eval x_max) = M,
have hM := hx_max.2, rw M_def at hM,
have M_non_zero : M ≠ 0, -- Then M is not zero :
{
by_contra absurd, -- Otherwise Df_ℝ(x) = 0 ∀ x ∈ (α-1, α+1)
simp only [not_not] at absurd, rw absurd at hM,
replace hM : ∀ (y : ℝ), y ∈ set.Icc (α - 1) (α + 1) → (polynomial.eval y Df_ℝ) = 0,
{
intros y hy,
have H := hM y hy, simp at H, rw H,
},
replace hM : Df_ℝ = 0 := f_zero_on_interval_f_zero Df_ℝ hM, -- Then Df_ℝ is the zero polyomial.
rename hM Df_ℝ_zero,
have f_ℝ_0 : f_ℝ.nat_degree = 0, -- Then f ∈ ℝ[x] has degree zero
{
have H := zero_deriv_imp_const_poly_ℝ f_ℝ _, exact H,
rw [<-hDf] at Df_ℝ_zero, assumption,
},
replace f_ℝ_0 := degree_0_constant f_ℝ f_ℝ_0, -- Then f ∈ ℝ[x] is constant polynomial
choose c hc using f_ℝ_0,
have absurd2 : c = 0, -- But since f(α) = 0, f ∈ ℝ[x] is the zero polynomial
{
rw [f_eval_on_ℝ, hfℝ, hc] at α_root, simp only [polynomial.eval_C] at α_root, assumption,
},
rw absurd2 at hc,
simp only [ring_hom.map_zero] at hc, rw <-hfℝ at hc,
replace hc : polynomial.map ℤembℝ f = polynomial.map ℤembℝ 0, simp only [polynomial.map_zero], exact hc,
exact f_nonzero (polynomial.map_injective ℤembℝ ℤembℝ_inj hc), -- But f ∈ ℤ[x] is not a zero polynomial.
},
have M_pos : M > 0, -- Since M is not zero, M > 0 being abs (sth.)
{
rw <-M_def at M_non_zero ⊢,
have H := abs_pos_iff.2 M_non_zero, simp only [abs_abs] at H, exact H,
},
generalize roots_def : f_ℝ.roots = f_roots,
generalize roots'_def : f_roots.erase α = f_roots', -- f_roots' is the root of f that is not α
generalize roots_distance_to_α : f_roots'.image (λ x, abs (α - x)) = distances,
generalize hdistances' : insert (1/M) (insert (1:ℝ) distances) = distances', -- distances' is the (finite) set {1, 1/M} ⋃ { |β - α| | β ∈ f_roots' }
have hnon_empty: distances'.nonempty, -- which is not empty, say 1/M ∈ distances'
{
rw <-hdistances',
rw [finset.nonempty], use (1/M), simp only [true_or, eq_self_iff_true, finset.mem_insert],
},
generalize hB : finset.min' distances' hnon_empty = B, -- Let B be the smallest of distances'.
have allpos : ∀ x : ℝ, x ∈ distances' -> x > 0, -- Every number in distances' is postive,
{
intros x hx, rw [<-hdistances', finset.mem_insert, finset.mem_insert] at hx,
cases hx, -- because it is either 1 / M which is positive
rw hx, simp only [one_div, gt_iff_lt, inv_pos], exact M_pos,
cases hx, -- or 1
rw hx, exact zero_lt_one, -- or |β - α| for some β which is a root of f but is not α.
rw [<-roots_distance_to_α] at hx, simp only [exists_prop, finset.mem_image] at hx,
choose α0 hα0 using hx,
rw [<-roots'_def, finset.mem_erase] at hα0,
rw <-(hα0.2), simp only [gt_iff_lt], apply (@abs_pos_iff ℝ _ (α - α0)).2,
rw sub_ne_zero, exact ne.symm hα0.1.1,
},
have B_pos : B > 0, -- Then B is positive.
{
have h := allpos (finset.min' distances' hnon_empty) (finset.min'_mem distances' hnon_empty),
rw <-hB, exact h,
},
generalize hA : B / 2 = A, -- Let A = B/2 > 0.
use A, split,
rw [<-hA], apply half_pos, exact B_pos, -- Then A is postive as B is postive
/- goal : ∀ (a b : ℤ), b > 0 → |α - a/b| > A / bⁿ where n is the degree of f.
We use proof by contradiction -/
by_contra absurd, simp only [gt_iff_lt, not_forall, not_lt, not_imp] at absurd,
choose a ha using absurd, -- So assume a and b are integers such that |α - a/b| ≤ A / b^n
choose b hb using ha,
have hb2 : b ^ f.nat_degree ≥ 1, -- Since b > 0 and n > 1, bⁿ ≥ 1 -- So A/bⁿ ≤ A
change b^ f.nat_degree > 0, apply pow_pos, exact hb.1,
have hb21 : abs (α - a / b) ≤ A, -- Then we also have |α - a/b| ≤ A
{
suffices H : (A / b ^ f.nat_degree) ≤ A,
exact le_trans hb.2 H,
apply (@div_le_iff ℝ _ A (b ^ f.nat_degree) A _).2,
apply (@le_mul_iff_one_le_right ℝ _ (b ^ f.nat_degree) A _).2, norm_cast at ⊢, exact hb2,
rw [<-hA], apply half_pos, assumption, norm_cast, linarith,
},
have hb22 : abs (α - a/b) < B, -- Since A > 0, |α - a/b| ≤ A < 2A = B ≤ 1
{
have H := half_lt_self B_pos, rw hA at H, exact gt_of_gt_of_ge H hb21,
},
have hab0 : (a/b:ℝ) ∈ set.Icc (α-1) (α+1), -- So a/b ∈ [α-1, α+1]
{
suffices : abs (α - a/b) ≤ 1,
rw [<-closed_ball_Icc, metric.mem_closed_ball, real.dist_eq, abs_sub], exact this,
suffices : B ≤ 1, linarith,
rw <-hB, refine finset.min'_le distances' hnon_empty 1 _,
rw [<-hdistances', finset.mem_insert, finset.mem_insert], tauto,
},
have hab1 : (a/b:ℝ) ≠ α, -- a/b is not α because α is irrational
have H := hα a b hb.1, rw sub_ne_zero at H, exact ne.symm H,
have hab2 : (a/b:ℝ) ∉ f_roots, -- a/b is not any root because
{
by_contra absurd, -- otherwise a/b ∈ f_roots' (roots of f other than α)
have H : (a/b:ℝ) ∈ f_roots',
rw [<-roots'_def, finset.mem_erase], exact ⟨hab1, absurd⟩,
have H2 : abs (α - a/b) ∈ distances', -- Then |α - a/b| is in {1, 1/M} ∪ {|β - α| | β ∈ f_roots' }
{
rw [<-hdistances', finset.mem_insert, finset.mem_insert], right, right,
rw [<-roots_distance_to_α, finset.mem_image], use (a/b:ℝ), split, exact H, refl,
},
have H3 := finset.min'_le distances' hnon_empty (abs (α - a/b)) H2, -- Then B > |α - a/b| ≥ B. A contradiction
rw hB at H3, linarith,
},
/- Since α ≠ a/b, either α > a/b or α < a/b, two cases essentially have the same proof. -/
have hab3 := ne_iff_lt_or_gt.1 hab1,
cases hab3,
{
have H := exists_deriv_eq_slope (λ x, f_ℝ.eval x) hab3 _ _, -- We mean value theorem in this case to find
choose x0 hx0 using H, -- an x₀ ∈ (a/b, α) such that Df_ℝ(x₀) = (f(α) - f(a/b))/(α - a/b),
have hx0r := hx0.2, -- since f(α) = 0, Df_ℝ(x₀) = - f(a/b) / (α - a/b).
rw [polynomial.deriv, hDf, <-hfℝ] at hx0r,
rw [f_eval_on_ℝ] at α_root, rw [α_root, hfℝ] at hx0r, simp only [zero_sub] at hx0r,
have Df_x0_nonzero : Df_ℝ.eval x0 ≠ 0, -- So Df_ℝ(x₀) is not zero. (a/b is not a root)
{
rw hx0r, intro rid, rw [neg_div, neg_eq_zero, div_eq_zero_iff] at rid, cases rid,
rw [<-roots_def, polynomial.mem_roots, polynomial.is_root] at hab2, exact hab2 rid,
exact hfℝ_nonzero, linarith,
},
have H2 : abs(α - a/b) = abs((f_ℝ.eval (a/b:ℝ)) / (Df_ℝ.eval x0)), -- So |α - a/b| = |f(a/b)/Df_ℝ(x₀)|
{
norm_num [hx0r],
rw [neg_div, div_neg, abs_neg, div_div_cancel'],
rw [<-roots_def] at hab2, by_contra absurd, simp only [not_not] at absurd,
have H := polynomial.mem_roots _, rw polynomial.is_root at H,
replace H := H.2 absurd, exact hab2 H,
exact hfℝ_nonzero,
},
have ineq' : polynomial.eval (a/b:ℝ) (polynomial.map ℤembℝ f) ≠ 0,
{
rw <-roots_def at hab2, intro rid, rw [hfℝ, <-polynomial.is_root, <-polynomial.mem_roots] at rid,
exact hab2 rid, exact hfℝ_nonzero,
},
have ineq : abs (α - a/b) ≥ 1/(M*b^(f.nat_degree)), -- By previous theorem |f(a/b)| ≥ 1/bⁿ and by definition -- |Df_ℝ(x₀)| ≤ M. [M is maximum of x ↦ abs(Df_ℝ x) on (α-1,α+1)
rw [H2, abs_div], -- and x₀ ∈ (α - 1, α + 1)]
have ineq := abs_f_at_p_div_q_ge_1_div_q_pow_n f f_deg a b hb.1 ineq', -- So |α - a/b| ≥ 1/(M*bⁿ) where n is degree of f
rw [<-hfℝ],
have ineq2 : abs (polynomial.eval x0 Df_ℝ) ≤ M,
{
have H := hM x0 _, exact H,
have h2 := @set.Ioo_subset_Icc_self ℝ _ (α-1) (α+1),
have h3 := (@set.Ioo_subset_Ioo_iff ℝ _ _ (α-1) _ (α+1) _ hab3).2 _,
have h4 : set.Ioo (↑a / ↑b) α ⊆ set.Icc (α-1) (α+1), exact set.subset.trans h3 h2,
exact set.mem_of_subset_of_mem h4 hx0.1, split,
rw set.mem_Icc at hab0, exact hab0.1, linarith,
},
have ineq3 := a_ge_b_a_div_c_ge_b_div_c _ _ (abs (polynomial.eval x0 Df_ℝ)) ineq _ _,
suffices : 1 / (b:ℝ) ^ f.nat_degree / abs (polynomial.eval x0 Df_ℝ) ≥ 1 / (M * ↑b ^ f.nat_degree),
linarith,
rw [div_div_eq_div_mul] at ineq3,
have ineq4 : 1 / ((b:ℝ) ^ f.nat_degree * abs (polynomial.eval x0 Df_ℝ)) ≥ 1 / (M * ↑b ^ f.nat_degree),
{
rw [ge_iff_le, one_div_le_one_div], conv_rhs {rw mul_comm},
have ineq := ((@mul_le_mul_left ℝ _ (abs (polynomial.eval x0 Df_ℝ)) M (↑b ^ f.nat_degree)) _).2 ineq2, exact ineq,
replace ineq := pow_pos hb.1 f.nat_degree, norm_cast, exact ineq, have ineq' : (b:ℝ) ^ f.nat_degree > 0, norm_cast,
exact pow_pos hb.1 f.nat_degree, exact (mul_pos M_pos ineq'),
apply mul_pos, norm_cast, exact pow_pos hb.1 f.nat_degree, rw abs_pos_iff, exact Df_x0_nonzero,
},
rw div_div_eq_div_mul, exact ineq4, have ineq5 := @div_nonneg ℝ _ 1 (↑b ^ f.nat_degree) _ _, exact ineq5, norm_cast,
exact bot_le, norm_cast, refine pow_nonneg (le_of_lt hb.1) f.nat_degree, rw [gt_iff_lt, abs_pos_iff], exact Df_x0_nonzero,
have ineq2 : 1/(M*b^(f.nat_degree)) > A / (b^f.nat_degree), -- Also 1/(M*bⁿ) > A/bⁿ since A < B ≤ 1/M
{
have ineq : A < B, rw [<-hA], exact @half_lt_self ℝ _ B B_pos,
have ineq2 : B ≤ 1/M, rw [<-hB], have H := finset.min'_le distances' hnon_empty (1/M) _, exact H,
rw [<-hdistances', finset.mem_insert], left, refl,
have ineq3 : A < 1/M, linarith,
rw [<-div_div_eq_div_mul], have ineq' := (@div_lt_div_right ℝ _ A (1/M) (↑b ^ f.nat_degree) _).2 ineq3,
rw <-gt_iff_lt at ineq', exact ineq', norm_cast, exact pow_pos hb.1 f.nat_degree,
},
have ineq3 : abs (α - a / b) > A / b ^ f.nat_degree := by linarith, -- So |α - a/b| > A/bⁿ
have ineq4 : abs (α - a / b) > abs (α - a / b) := by linarith, linarith, -- But we assumed |α - a/b| ≤ A/bⁿ. This is the desired contradiction
exact @polynomial.continuous_on ℝ _ (set.Icc (a/b:ℝ) α) f_ℝ, -- Since we used mean value theorem, we need to show continuity and differentiablity of x ↦ f(x),
exact @polynomial.differentiable_on ℝ _ (set.Ioo (a/b:ℝ) α) f_ℝ, -- these are built in.
},
/- The other case is similar, In fact I copied and pasted the above proof and exchanged positions of α and a/b. Then it worked. -/
have H := exists_deriv_eq_slope (λ x, f_ℝ.eval x) hab3 _ _,
choose x0 hx0 using H,
have hx0r := hx0.2,
rw [polynomial.deriv, hDf, <-hfℝ] at hx0r,
rw [f_eval_on_ℝ] at α_root, rw [α_root, hfℝ] at hx0r, simp only [sub_zero] at hx0r,
have Df_x0_nonzero : Df_ℝ.eval x0 ≠ 0,
{
rw hx0r, intro rid, rw [div_eq_zero_iff] at rid,
rw [<-roots_def, polynomial.mem_roots, polynomial.is_root] at hab2, cases rid, exact hab2 rid, linarith,
exact hfℝ_nonzero,
},
have H2 : abs(α - a/b) = abs((f_ℝ.eval (a/b:ℝ)) / (Df_ℝ.eval x0)),
{
norm_num [hx0r],
rw [div_div_cancel'], have : ↑a / ↑b - α = - (α - ↑a / ↑b), linarith, rw [this, abs_neg],
by_contra absurd, simp only [not_not] at absurd,
have H := polynomial.mem_roots _, rw polynomial.is_root at H,
replace H := H.2 absurd, rw roots_def at H, exact hab2 H,
exact hfℝ_nonzero,
},
have ineq' : polynomial.eval (a/b:ℝ) (polynomial.map ℤembℝ f) ≠ 0,
{
rw <-roots_def at hab2, intro rid, rw [hfℝ, <-polynomial.is_root, <-polynomial.mem_roots] at rid,
exact hab2 rid, exact hfℝ_nonzero,
},
have ineq : abs (α - a/b) ≥ 1/(M*b^(f.nat_degree)),
{
rw [H2, abs_div],
have ineq := abs_f_at_p_div_q_ge_1_div_q_pow_n f f_deg a b hb.1 ineq',
rw [<-hfℝ],
have ineq2 : abs (polynomial.eval x0 Df_ℝ) ≤ M,
{
have H := hM x0 _, exact H,
have h2 := @set.Ioo_subset_Icc_self ℝ _ (α-1) (α+1),
have h3 := (@set.Ioo_subset_Ioo_iff ℝ _ _ (α-1) _ (α+1) _ hab3).2 _,
have h4 : set.Ioo α (↑a / ↑b) ⊆ set.Icc (α-1) (α+1), exact set.subset.trans h3 h2,
exact set.mem_of_subset_of_mem h4 hx0.1, split,
rw set.mem_Icc at hab0, linarith,
exact (set.mem_Icc.1 hab0).2,
},
have ineq3 := a_ge_b_a_div_c_ge_b_div_c _ _ (abs (polynomial.eval x0 Df_ℝ)) ineq _ _,
suffices : 1 / ↑b ^ f.nat_degree / abs (polynomial.eval x0 Df_ℝ) ≥ 1 / (M * ↑b ^ f.nat_degree),
linarith,
rw [div_div_eq_div_mul] at ineq3,
have ineq4 : 1 / ((b:ℝ) ^ f.nat_degree * abs (polynomial.eval x0 Df_ℝ)) ≥ 1 / (M * ↑b ^ f.nat_degree),
{
rw [ge_iff_le, one_div_le_one_div], conv_rhs {rw mul_comm},
have ineq := ((@mul_le_mul_left ℝ _ (abs (polynomial.eval x0 Df_ℝ)) M (↑b ^ f.nat_degree)) _).2 ineq2, exact ineq,
replace ineq := pow_pos hb.1 f.nat_degree, norm_cast, exact ineq, have ineq' : (b:ℝ) ^ f.nat_degree > 0, norm_cast,
exact pow_pos hb.1 f.nat_degree, exact (mul_pos M_pos ineq'),
apply mul_pos, norm_cast, exact pow_pos hb.1 f.nat_degree, rw abs_pos_iff, exact Df_x0_nonzero,
},
rw div_div_eq_div_mul, exact ineq4, have ineq5 := @div_nonneg ℝ _ 1 (↑b ^ f.nat_degree) _ _, exact ineq5, norm_cast,
exact bot_le, norm_cast, refine pow_nonneg (le_of_lt hb.1) f.nat_degree, rw [gt_iff_lt, abs_pos_iff], exact Df_x0_nonzero,
},
have ineq2 : 1/(M*b^(f.nat_degree)) > A / (b^f.nat_degree),
{
have ineq : A < B, rw [<-hA], exact @half_lt_self ℝ _ B B_pos,
have ineq2 : B ≤ 1/M, rw [<-hB], have H := finset.min'_le distances' hnon_empty (1/M) _, exact H,
rw [<-hdistances', finset.mem_insert], left, refl,
have ineq3 : A < 1/M, linarith,
rw [<-div_div_eq_div_mul], have ineq' := (@div_lt_div_right ℝ _ A (1/M) (↑b ^ f.nat_degree) _).2 ineq3,
rw <-gt_iff_lt at ineq', exact ineq', norm_cast, exact pow_pos hb.1 f.nat_degree,
},
have ineq3 : abs (α - a / b) > A / b ^ f.nat_degree := by linarith,
have ineq4 : abs (α - a / b) > abs (α - a / b) := by linarith, linarith,
exact @polynomial.continuous_on ℝ _ (set.Icc α (a/b:ℝ)) f_ℝ,
exact @polynomial.differentiable_on ℝ _ (set.Ioo α (a/b:ℝ)) f_ℝ,
end
/--
We then prove that all liouville numbers cannot be rational. Hence we can apply the above lemma to any liouville number and show its
transcendence.
-/
lemma liouville_numbers_irrational: ∀ (x : real), (liouville_number x) -> irrational x :=
begin
intros x liouville_x a b hb rid, replace rid : x = ↑a / ↑b, linarith, -- Suppose x is a rational Liouville number: say x = a/b.
-- rw liouville_number at liouville_x,
generalize hn : b.nat_abs + 1 = n, -- Let n = |b| + 1. Then 2^(n-1) > b
have b_ineq : 2 ^ (n-1) > b,
{
rw <-hn, simp only [nat.add_succ_sub_one, add_zero, gt_iff_lt],
have triv : b = b.nat_abs, rw <-int.abs_eq_nat_abs, rw abs_of_pos, assumption,rw triv, simp,
have H := @nat.lt_pow_self 2 _ b.nat_abs, norm_cast, exact H, exact lt_add_one 1,
},
choose p hp using liouville_x n, -- Then there is a rational number p/q where q > 1
choose q hq using hp, rw rid at hq, -- such that 0 < |x- p/q| < 1/qⁿ
have q_pos : q > 0 := by linarith, -- i.e 0 < |(aq - bp)/bq| < 1/qⁿ
rw div_sub_div at hq,
rw abs_div at hq,
by_cases (abs ((a:ℝ) * (q:ℝ) - (b:ℝ) * (p:ℝ)) = 0), -- Then aq ≠ bp
{
rw h at hq, simp only [one_div, gt_iff_lt, euclidean_domain.zero_div, inv_pos] at hq, have hq1 := hq.1, have hq2 := hq.2, have hq21 := hq2.1, have hq22 := hq2.2, linarith,
},
{
have ineq2: (abs (a * q - b * p)) ≠ 0,
by_contra rid, simp only [abs_eq_zero, not_not] at rid, norm_cast at h,
simp only [abs_eq_zero] at h, exact h rid,
have ineq2':= abs_pos_iff.2 ineq2, rw [abs_abs] at ineq2',
replace ineq2' : 1 ≤ abs (a * q - b * p), linarith,
have ineq3 : 1 ≤ @abs ℝ _ (a * q - b * p), norm_cast, exact ineq2', -- Then |aq - bp| ≠ 0, then |aq-bp| ≥ 1
have eq : abs (↑b * ↑q) = (b:ℝ)*(q:ℝ), rw abs_of_pos, have eq' := mul_pos hb q_pos, norm_cast, exact eq',
rw eq at hq,
have ineq4 : 1 / (b * q : ℝ) ≤ (@abs ℝ _ (a * q - b * p)) / (b * q), -- So |(aq - bp)/bq| = |aq - bp|/(bq) ≥ 1/(bq)
{
rw div_le_div_iff, simp only [one_mul], have H := (@le_mul_iff_one_le_left ℝ _ _ (b * q) _).2 ineq3, exact H,
norm_cast, have eq' := mul_pos hb q_pos, exact eq', norm_cast, have eq' := mul_pos hb q_pos, exact eq', norm_cast, have eq' := mul_pos hb q_pos, exact eq',
},
have b_ineq' := @mul_lt_mul ℤ _ b q (2^(n-1)) q b_ineq _ _ _, -- But b < 2^(n-1) < q^(n-1)
have b_ineq'' : (b * q : ℝ) < (2:ℝ) ^ (n-1) * (q : ℝ), norm_cast, simp only [int.coe_nat_zero, int.coe_nat_pow, int.coe_nat_succ, zero_add], exact b_ineq',
have q_ineq1 : q ≥ 2, linarith,
have q_ineq2 := @pow_le_pow_of_le_left ℤ _ 2 q _ _ (n-1),
have q_ineq3 : 2 ^ (n - 1) * q ≤ q ^ (n - 1) * q, rw (mul_le_mul_right _), assumption, linarith,
have triv : q ^ (n - 1) * q = q ^ n, rw <-hn, simp only [nat.add_succ_sub_one, add_zero], rw pow_add, simp only [pow_one], rw triv at q_ineq3,
have b_ineq2 : b * q < q ^ n, linarith, -- So bq < qⁿ
have rid' := (@one_div_lt_one_div ℝ _ (q^n) (b*q) _ _).2 _,
have rid'' : @abs ℝ _ (a * q - b * p) / (b * q : ℝ) > 1 / q ^ n, linarith, -- Then |(aq - bp)/bq| > 1/qⁿ
have hq1 := hq.1, have hq2 := hq.2, have hq21 := hq2.1, have hq22 := hq2.2, -- This is the contradiction we are seeking for:
linarith, -- 1/qⁿ > |(aq - bp)/bq| > 1/qⁿ
/- other less important steps. We manipulated inequalities using multiplication and division, so we need to prove various things
to be non-negative or postive. The proves are all more or less trivial. -/
apply pow_pos, norm_cast, exact q_pos, norm_cast, apply mul_pos,
exact hb, exact q_pos,
norm_cast, exact b_ineq2,
norm_cast, exact bot_le,
exact q_ineq1,
exact le_refl q,
exact q_pos,
apply pow_nonneg, norm_cast, exact bot_le,
},
norm_cast, linarith, norm_cast, linarith,
end
theorem liouville_numbers_transcendental : ∀ x : ℝ, liouville_number x -> transcendental x :=
begin
intros x liouville_x, -- Let $x$ be any Liouville's number,
have irr_x : irrational x, exact liouville_numbers_irrational x liouville_x, -- Then by previous theorem, it is irrational.
intros rid, rw is_algebraic at rid, -- assume x is algebraic over ℤ,
choose f hf using rid, -- Let f be an integer polynomial who admitts x as a root.
have f_deg : f.nat_degree > 1, { -- Then f must have degree > 1, otherwise f have degree 0 or 1. --
by_contra rid, simp only [not_lt] at rid, replace rid := lt_or_eq_of_le rid, cases rid,
{ -- If f has degree 0 then f ∈ ℤ[T] = c for some c ∈ ℤ. So x is an irrational integer.
replace rid : f.nat_degree = 0, linarith, rw polynomial.nat_degree_eq_zero_iff_degree_le_zero at rid, rw polynomial.degree_le_zero_iff at rid,
rw rid at hf, simp only [polynomial.aeval_C] at hf, have hf1 := hf.1, have hf2 := hf.2, simp only [ring_hom.eq_int_cast, ne.def] at hf1,
simp only [int.cast_eq_zero, ring_hom.eq_int_cast] at hf2, rw hf2 at hf1, norm_cast at hf1,
},
{ -- If f has degree 1, then f = aT + b
have f_eq : f = polynomial.C (f.coeff 0) + (polynomial.C (f.coeff 1)) * polynomial.X,
{
ext,
rw [polynomial.coeff_add, polynomial.coeff_C, polynomial.coeff_C_mul, polynomial.coeff_X], split_ifs, linarith,
simp only [h, add_zero, mul_zero], simp only [h_1, mul_one, zero_add],
replace h : n > 0, exact nat.pos_of_ne_zero h, replace h : n ≥ 1, exact nat.succ_le_iff.mpr h,
replace h : 1 < n ∨ 1 = n, exact lt_or_eq_of_le h, cases h, rw polynomial.coeff_eq_zero_of_nat_degree_lt, simp only [add_zero, mul_zero],
rwa rid, exfalso, exact h_1 h,
},
rw f_eq at hf, simp only [alg_hom.map_add, polynomial.aeval_X, ne.def, polynomial.aeval_C, alg_hom.map_mul] at hf, rw irrational at irr_x,
by_cases ((f.coeff 1) > 0),
{
replace irr_x := irr_x (-(f.coeff 0)) (f.coeff 1) h, simp only [ne.def, int.cast_neg] at irr_x, rw neg_div at irr_x, rw sub_neg_eq_add at irr_x, rw add_comm at irr_x,
suffices : ↑(f.coeff 0) / ↑(f.coeff 1) + x = 0, exact irr_x this,
rw add_eq_zero_iff_eq_neg, rw div_eq_iff, have triv : -x * ↑(f.coeff 1) = - (x * (f.coeff 1)), exact norm_num.mul_neg_pos x ↑(polynomial.coeff f 1) (x * ↑(polynomial.coeff f 1)) rfl,
rw triv, rw <-add_eq_zero_iff_eq_neg, rw mul_comm, have hf2 := hf.2, simp [polynomial.aeval_def] at hf2, exact hf2,
norm_cast, linarith,
},
{
simp only [not_lt] at h, replace h := lt_or_eq_of_le h, cases h,
{
replace irr_x := irr_x (f.coeff 0) (-(f.coeff 1)) _, simp only [ne.def, int.cast_neg] at irr_x, rw div_neg at irr_x, rw sub_neg_eq_add at irr_x, rw add_comm at irr_x,
suffices suff : ↑(f.coeff 0) / ↑(f.coeff 1) + x = 0, exact irr_x suff,
rw add_eq_zero_iff_eq_neg, rw div_eq_iff, have triv : -x * ↑(f.coeff 1) = - (x * (f.coeff 1)), exact norm_num.mul_neg_pos x ↑(polynomial.coeff f 1) (x * ↑(polynomial.coeff f 1)) rfl,
rw triv, rw <-add_eq_zero_iff_eq_neg, rw mul_comm, exact hf.2, norm_cast, linarith, exact neg_pos.mpr h,
},
rw <-rid at h,
rw <-polynomial.leading_coeff at h,
rw polynomial.leading_coeff_eq_zero at h, rw h at rid, simp only [polynomial.nat_degree_zero, zero_ne_one] at rid, exact rid,
}
},
},
have about_root : f_eval_on_ℝ f x = 0,
{
rw f_eval_on_ℝ, have H := hf.2, rw [polynomial.aeval_def] at H,
rw [polynomial.eval, polynomial.eval₂_map], rw [polynomial.eval₂, finsupp.sum] at H ⊢, rw [<-H, sum_eq],
intros m hm, simp only [ring_hom.eq_int_cast, id.def],
},
choose A hA using about_irrational_root x irr_x f f_deg about_root, -- So we can apply the lemma about irrational root:
have A_pos := hA.1, -- There is an A > 0 such that for any integers a b with b > 0
have exists_r := pow_big_enough A A_pos, -- |x - a/b| > A/bⁿ where n is the degree of f.
choose r hr using exists_r, -- Let r ∈ ℕ such tht 1/A ≤ 2^r (equivalently 1/2^r ≤ A)
have hr' : 1/(2^r) ≤ A, rw [div_le_iff, mul_comm, <-div_le_iff], exact hr, exact A_pos, apply (pow_pos _), exact two_pos,
generalize hm : r + f.nat_degree = m, -- Let m := r + n
replace liouville_x := liouville_x m,
choose a ha using liouville_x, -- Since x is Liouville, there are integers a and b with b > 1
choose b hb using ha, -- such that 0 < |x - a/b| < 1/bᵐ = 1/bⁿ * 1/b^r.
have ineq := hb.2.2, rw <-hm at ineq, rw pow_add at ineq,
have eq1 := div_mul_eq_div_mul_one_div 1 ((b:ℝ) ^ r) ((b:ℝ)^f.nat_degree), rw eq1 at ineq,
have ineq2 : 1/((b:ℝ)^r) ≤ 1/((2:ℝ)^r), -- But 1/b^r ≤ 1/2^r ≤ A. So 0 < |x - a/b| < A/bⁿ.
{ -- This is the contradiction we seek by the choice of A.
apply (@one_div_le_one_div ℝ _ ((b:ℝ)^r) ((2:ℝ)^r) _ _).2,
suffices suff : 2 ^ r ≤ b^r, norm_cast, norm_num, exact suff,
have ineq' := @pow_le_pow_of_le_left ℝ _ 2 b _ _ r, norm_cast at ineq', norm_num at ineq', exact ineq',
linarith, norm_cast, linarith, norm_cast, apply pow_pos, linarith, apply pow_pos, linarith,
},
have ineq3 : 1 / ↑b ^ r ≤ A, exact le_trans ineq2 hr',
have ineq4 : 1 / ↑b ^ r * (1 / ↑b ^ f.nat_degree) ≤ A * (1 / ↑b ^ f.nat_degree),
{
have H := (@mul_le_mul_right ℝ _ (1 / ↑b ^ r) A (1 / ↑b ^ f.nat_degree) _).2 ineq3, exact H,
apply div_pos, linarith, norm_cast, apply pow_pos, linarith,
},
conv_rhs at ineq4 {rw <-mul_div_assoc, rw mul_one},
have ineq5 : abs (x - a / b:ℝ) < A / ↑b ^ f.nat_degree, linarith,
have rid := hA.2 a b _, linarith, linarith,
end
/-- ## define an example of Liouville number Σᵢ 1/2^(i!)
- In this section we explicitly define $\alpha := \sum_{i=0}^\infty ¼{1}{10^{i!}}$;
- We use comparison test to prove the convergence of α;
- Then we prove that α is indeed a Liouville number;
- From previous theorem we easily conclude α is transcendental.
-/
-- function n ↦ 1/10^n!
def ten_pow_n_fact_inverse (n : ℕ) : ℝ := (1/10)^n.fact
-- function n ↦ 1/10^n
def ten_pow_n_inverse (n : ℕ) : ℝ := (1/10)^n
-- 1/10^{n!} is nonnegative.
lemma ten_pow_n_fact_inverse_ge_0 (n : nat) : ten_pow_n_fact_inverse n ≥ 0 :=
begin
unfold ten_pow_n_fact_inverse,
simp only [one_div, inv_nonneg, ge_iff_le, inv_pow'], have h := le_of_lt (@pow_pos _ _ (10:real) _ n.fact),
norm_cast at h ⊢, exact h, norm_num,
end
-- n ≤ n!
lemma n_le_n_fact : ∀ n : nat, n ≤ n.fact
| 0 := by norm_num
| 1 := by norm_num
| (n+2) := begin
have H := n_le_n_fact n.succ,
conv_rhs {rw (nat.fact_succ n.succ)},
have ineq1 : n.succ.succ * n.succ ≤ n.succ.succ * n.succ.fact, {exact nat.mul_le_mul_left (nat.succ (nat.succ n)) (n_le_n_fact (nat.succ n))},
suffices ineq2 : n.succ.succ ≤ n.succ.succ * n.succ, {exact nat.le_trans ineq2 ineq1},
have H' : ∀ m : nat, m.succ.succ ≤ m.succ.succ * m.succ,
{
intro m, induction m with m hm,
norm_num, simp only [nat.mul_succ, zero_le, le_add_iff_nonneg_left] at hm ⊢,
},
exact H' n,
end
lemma ten_pow_n_fact_inverse_le_ten_pow_n_inverse (n : nat) : ten_pow_n_fact_inverse n ≤ ten_pow_n_inverse n :=
begin
simp only [ten_pow_n_fact_inverse, ten_pow_n_inverse, one_div, inv_pow'],
rw [inv_le], simp only [inv_inv'], apply pow_le_pow, linarith, apply n_le_n_fact, apply pow_pos,
linarith, rw inv_pos, apply pow_pos, linarith,
end
-- Σᵢ 1/10ⁱ exists because it is a geometric sequence with 1/10 < 1. The sum equals 10/9
theorem summable_ten_pow_n_inverse : summable ten_pow_n_inverse :=
begin
have H := @summable_geometric_of_abs_lt_1 (1/10:ℝ) _, have triv : ten_pow_n_inverse = (λ (n : ℕ), (1 / 10) ^ n) := rfl, rw triv, exact H,
rw abs_of_pos, linarith, linarith,
end
def β : ℝ := classical.some summable_ten_pow_n_inverse
theorem β_eq : (∑' (b : ℕ), ten_pow_n_inverse b) = (10 / 9:ℝ) :=
begin
have eq0 := @tsum_geometric_of_abs_lt_1 (1/10) _,
have eq1 : (∑' (n : ℕ), (1 / 10:ℝ) ^ n) = (∑' (b : ℕ), ten_pow_n_inverse b),
apply congr_arg, ext, rw ten_pow_n_inverse,
rw eq1 at eq0, rw eq0, norm_num, rw abs_of_pos, linarith, linarith,
end
-- Hence Σᵢ 1/10^i! exists by comparison test, call it α
theorem summable_ten_pow_n_fact_inverse : summable ten_pow_n_fact_inverse :=
begin
exact @summable_of_nonneg_of_le _ ten_pow_n_inverse ten_pow_n_fact_inverse ten_pow_n_fact_inverse_ge_0 ten_pow_n_fact_inverse_le_ten_pow_n_inverse summable_ten_pow_n_inverse,
end
-- define α to be Σᵢ 1/10^i!
def α := ∑' n, ten_pow_n_fact_inverse n
-- first k term
-- `α_k k` is the kth partial sum $\sum_{i=0}^k \frac{1}{10^{i!}}$.
notation `α_k` k := ∑ ii in finset.range (k+1), ten_pow_n_fact_inverse ii
-- `α_k k` is rational and can be written as $\frac{p}{10^{k!}}$ for some p ∈ ℕ.
theorem α_k_rat (k:ℕ) : ∃ (p : ℕ), (α_k k) = (p:ℝ) / ((10:ℝ) ^ k.fact) :=
begin
induction k with k IH,
simp only [ten_pow_n_fact_inverse, pow_one, finset.sum_singleton, finset.range_one, nat.fact_zero],
use 1, norm_cast,
choose pk hk using IH, -- Then the (k+1)th partial sum = p/10^{k!} + 10^{(k+1)!}
generalize hm : 10^((k+1).fact - k.fact) = m,
have eqm : 10^k.fact * m = 10^(k+1).fact, -- If we set m = 10^{(k+1)!-k!}, then pm+1 works. This is algebra.
rw <-hm, rw <-nat.pow_add, rw nat.add_sub_cancel', simp only [nat.fact_succ], rw le_mul_iff_one_le_left, exact inf_eq_left.mp rfl, exact nat.fact_pos k,
have eqm' : (10:ℝ)^k.fact * m = (10:ℝ)^(k+1).fact,
norm_cast, exact eqm,
generalize hp : pk * m + 1 = p,
use p, rw finset.sum_range_succ, rw hk,
rw [ten_pow_n_fact_inverse, div_pow], rw one_pow, rw nat.succ_eq_add_one,
rw <-eqm', rw <-hp, conv_rhs {simp only [one_div, nat.cast_add, nat.cast_one, nat.cast_mul], rw <-div_add_div_same, simp only [one_div]}, rw mul_div_mul_right, simp only [one_div], exact add_comm (10 ^ nat.fact k * ↑m)⁻¹ (↑pk / 10 ^ nat.fact k),
intro rid, rw <-hm at rid, norm_cast at rid, have eq := @nat.pow_pos 10 _ ((k + 1).fact - k.fact), linarith, linarith,
end
-- rest term `α_k_rest k` is $\sum_{i=0}^\infty \frac{1}{(i+k+1)!}=\sum_{i=k+1}^\infty \frac{1}{i!}$
notation `α_k_rest` k := ∑' ii, ten_pow_n_fact_inverse (ii + (k+1))
private lemma sum_ge_term (f : ℕ -> ℝ) (hf : ∀ x:ℕ, f x > 0) (hf' : summable f): (∑' i, f i) ≥ f 0 :=
begin
rw <-(@tsum_ite_eq ℝ ℕ _ _ _ 0 (f 0)),
generalize hfunc : (λ b' : ℕ, ite (b' = 0) (f 0) 0) = fn, simp only [ge_iff_le], apply tsum_le_tsum,
intro n, by_cases (n=0), split_ifs, exact le_of_eq (congr_arg f (eq.symm h)),
split_ifs, exact le_of_lt (hf n),
swap, dsimp only [], assumption, rw summable, use (f 0),
have H := has_sum_ite_eq 0 (f 0), exact H,
end
-- Since each summand in `α_k_rest k` is positive, the sum is positive.
theorem α_k_rest_pos (k : ℕ) : (α_k_rest k) > 0 :=
begin
generalize hfunc : (λ n:ℕ, ten_pow_n_fact_inverse (n + (k + 1))) = fn,
have ineq1 := sum_ge_term fn _ _,
have ineq2 : fn 0 > 0,
rw <-hfunc, simp only [gt_iff_lt, zero_add], rw ten_pow_n_fact_inverse, rw div_pow, apply div_pos, simp only [one_fpow, ne.def, triv, not_false_iff, one_ne_zero], exact zero_lt_one, apply pow_pos, linarith,
exact gt_of_ge_of_gt ineq1 ineq2,
intro n, rw <-hfunc, simp only [gt_iff_lt], rw ten_pow_n_fact_inverse, rw div_pow, apply div_pos, simp only [one_fpow, ne.def, triv, not_false_iff, one_ne_zero], exact zero_lt_one, apply pow_pos, linarith,
rw <-hfunc, exact (@summable_nat_add_iff ℝ _ _ _ ten_pow_n_fact_inverse (k+1)).2 summable_ten_pow_n_fact_inverse,
end
-- We also have for any k ∈ ℕ, α = (α_k k) + (α_k_rest k)
theorem α_truncate_wd (k : ℕ) : α = (α_k k) + α_k_rest k :=
begin
rw α,
have eq1 := @sum_add_tsum_nat_add ℝ _ _ _ _ ten_pow_n_fact_inverse (k+1) _,
rw eq1, exact summable_ten_pow_n_fact_inverse,
end
private lemma ten_pow_n_fact_gt_one (n : ℕ) : 10^(n.fact) > 1 :=
begin
induction n with n h,
simp only [gt_iff_lt, zero_le, one_le_bit1, nat.one_lt_bit0_iff, nat.fact_zero, nat.pow_one], simp only [gt_iff_lt, nat.fact_succ], rw nat.pow_mul,
have h' := @nat.lt_pow_self (10 ^ n.succ) _ n.fact,
have ineq := nat.fact_pos n, have ineq2 : 1 < (10 ^ n.succ) ^ n.fact := gt_of_gt_of_ge h' ineq, exact ineq2, exact nat.one_lt_pow' n 8,
end
private lemma nat.fact_succ' (n : ℕ) : n.succ.fact = n.fact * n.succ :=
begin
rw nat.fact_succ, ring,
end
-- Here we prove for any n ∈ ℕ, $\sum_{i} \left(\frac{1}{10^i}\times\frac{1}{10^{(n+1)!}}\right) \le \frac{2}{10^{(n+1)!}}$
private lemma lemma_ineq3 (n:ℕ) : (∑' (i:ℕ), (1/10:ℝ)^i * (1/10:ℝ)^(n+1).fact) ≤ (2/10^n.succ.fact:ℝ) :=
begin
rw tsum_mul_right, -- We factor out 1/10^{(n+1)!}
have eq1 := β_eq, unfold ten_pow_n_inverse at eq1, rw eq1, rw div_pow, rw one_pow, field_simp, rw <-nat.fact_succ,
rw div_le_div_iff, norm_cast, conv_rhs {rw <-nat.mul_assoc}, -- and use that what left is a geometric sum = 10/9
have triv : 2 * 9 = 18 := by norm_num, rw triv,
apply nat.mul_le_mul, linarith, linarith,
apply mul_pos, linarith, apply pow_pos, linarith, apply pow_pos, linarith,
have h := summable_ten_pow_n_inverse, have triv : ten_pow_n_inverse = (λ (b : ℕ), (1 / 10) ^ b) := rfl,
rwa <-triv,
end
-- We use induction to prove 2 < 10 ^ n!
private lemma aux_ineq (n:ℕ) : 2 < 10 ^ n.fact :=
begin
induction n with n ih, simp only [nat.succ_pos', one_lt_bit1, bit0_lt_bit0, nat.fact_zero, nat.pow_one],
rw [nat.fact_succ, nat.pow_mul],
have ineq : 10 ^ n.fact ≤ (10 ^ n.succ) ^ n.fact, rw nat.pow_le_iff_le_left,
have h : ∀ m : ℕ, 10 ≤ 10 ^ m.succ,
intro m, induction m with m hm, simp only [nat.pow_one], rw nat.pow_succ, linarith,
exact h n,
linarith [nat.fact_pos n], linarith,
end
-- $\frac{2}{10^{(n+1)!}} < \frac{1}{(10^{n!})^n}$
private lemma lemma_ineq4 (n:ℕ) : (2 / 10 ^ (n.fact * n.succ):ℝ) < (1 / ((10:ℝ) ^ n.fact) ^ n) :=
begin
rw div_lt_div_iff, rw one_mul, -- This is also algebra plus checking a lot of thing non-negative or positive.
conv_rhs {rw nat.succ_eq_add_one, rw mul_add, rw pow_add, rw mul_one, rw pow_mul, rw mul_comm},
apply mul_lt_mul,
norm_cast,
exact aux_ineq n,
linarith,
apply pow_pos, apply pow_pos, linarith,
apply pow_nonneg, linarith,
apply pow_pos, linarith,
apply pow_pos, apply pow_pos, linarith,
end
-- This is i + n! ≤ (i+n)!. Proved by induction on n.
private lemma ineq_i (i n : ℕ) : i + n.succ.fact ≤ (i + n.succ).fact :=
begin
induction n with n hn, simp only [add_zero, nat.fact_succ, nat.fact_one], rw <-nat.succ_eq_add_one, have h := (@nat.mul_le_mul_right 1 i.fact i.succ) _, rw [one_mul, mul_comm] at h, exact h,
replace h := nat.fact_pos i, exact h,
generalize hm : n.succ = m, rw hm at hn,
have triv : i + m.succ = (m+i).succ, rw nat.add_succ, rw add_comm, rw triv,
conv_rhs {rw nat.fact_succ, rw nat.succ_eq_add_one,},
have ineq1 : (m + i + 1) * (i + m.fact) ≤ (m + i + 1) * (m + i).fact,
apply mul_le_mul, linarith, rwa add_comm m, linarith, linarith,
suffices : i + m.succ.fact ≤ (m + i + 1) * (i + m.fact), exact le_trans this ineq1,
rw mul_add,
have ineq2 : (m + i + 1) * m.fact ≤ (m + i + 1) * i + (m + i + 1) * m.fact := nat.le_add_left _ _,
suffices : i + m.succ.fact ≤ (m + i + 1) * m.fact, exact le_trans this ineq2,
replace triv : (m + i + 1) = (m + 1 + i), ring, rw triv, rw add_mul, rw <-nat.succ_eq_add_one, rw <-nat.fact_succ, rw add_comm,
apply add_le_add, linarith,
replace triv := (@nat.mul_le_mul_right 1 m.fact i) _, simp only [one_mul] at triv, rw mul_comm at triv, exact triv,
replace triv := nat.fact_pos m, exact triv,
end
-- With all the auxilary inequalies, we can now prove α is a Liouville number.
-- Then α is a Liouville number hence a transcendental number.
theorem liouville_α : liouville_number α :=
begin
intro n, -- We fix n ∈ ℕ.
have lemma1 := α_k_rat n, -- we are going to prove 0 < |α - p/10^{n!}| < 1/10^{n! * n}
have lemma2 := α_truncate_wd n,
replace lemma2 : (α_k_rest n) = α - α_k n,
exact eq_sub_of_add_eq' (eq.symm lemma2), -- We know that the first n terms sums to p/10^{n!}
choose p hp using lemma1, -- for some p ∈ ℕ.
use p, use 10^(n.fact),
suffices : 0 < abs (α_k_rest n) ∧ abs (α_k_rest n) < 1/(10^ n.fact)^n,
{
split, norm_cast, exact ten_pow_n_fact_gt_one n,
rw [lemma2, hp] at this,
norm_cast at this,
tidy,
},
-- split, norm_cast, refine ten_pow_n_fact_gt_one n, -- and that 10^{n!} > 1
split, -- We first prove that 0 < |α - p/10^{n!}| or equivlanetly α ≠ p/10^{n!}
{ -- otherwise the rest term from i=n+1 to infinity sum to zero, but we proved it to be positive.
rw abs_pos_iff, intro rid, have α_k_rest_pos := α_k_rest_pos n, linarith,
},
{ -- next we prove |α - p/10^{n!}| < 1/10^{n! * n}
rw [abs_of_pos (α_k_rest_pos n)],
have ineq2 : (∑' (j : ℕ), ten_pow_n_fact_inverse (j + (n + 1))) ≤ (∑' (i:ℕ), (1/10:ℝ)^i * (1/10:ℝ)^(n+1).fact),
{ -- Since for all i ∈ ℕ, 1/10^{(i+n+1)!} ≤ 1/10ⁱ * 1/10^{(n+1)!}
apply tsum_le_tsum, intro i, -- [this is because of one of previous inequalities plus some inequality manipulation]
rw ten_pow_n_fact_inverse, field_simp, rw one_div_le_one_div, rw <-pow_add, apply pow_le_pow, linarith,
{ -- we have the rest of term sums to a number ≤ $\sum_i^\infty \frac{1}{10^i}\times\frac{1}{10^{(n+1)!}}$
rw <-nat.fact_succ, rw <-nat.succ_eq_add_one,
exact ineq_i _ _,
},
apply pow_pos, linarith, rw <-pow_add, apply pow_pos, linarith,
exact (@summable_nat_add_iff ℝ _ _ _ ten_pow_n_fact_inverse (n+1)).2 summable_ten_pow_n_fact_inverse,
{
have s0 : summable (λ (b : ℕ), (1 / 10:ℝ) ^ b),
{
have triv : (λ (b : ℕ), (1 / 10:ℝ) ^ b) = ten_pow_n_inverse, ext, rw ten_pow_n_inverse, rw triv,
exact summable_ten_pow_n_inverse,
},
apply (summable_mul_right_iff _).1 s0, have triv : (1 / 10:ℝ) ^ (n + 1).fact > 0, apply pow_pos, linarith, linarith,
}
}, -- by previous inequlity $\sum_i^\infty \frac{1}{10^i}\times\frac{1}{10^{(n+1)!}} < \frac{2}{10^{(n+1)!}} \le \frac{1}{10^{n!\times n}}$
have ineq3 : (∑' (i:ℕ), (1/10:ℝ)^i * (1/10:ℝ)^(n+1).fact) ≤ (2/10^n.succ.fact:ℝ) := lemma_ineq3 _,
rw nat.fact_succ' at ineq3, -- This what we want. So α is Liouville
have ineq4 : (2 / 10 ^ (n.fact * n.succ):ℝ) < (1 / ((10:ℝ) ^ n.fact) ^ n) := lemma_ineq4 _,
have ineq5 : (∑' (n_1 : ℕ), ten_pow_n_fact_inverse (n_1 + (n + 1))) < (1 / ((10:ℝ) ^ n.fact) ^ n),
{
apply @lt_of_le_of_lt ℝ _ (∑' (n_1 : ℕ), ten_pow_n_fact_inverse (n_1 + (n + 1))) (∑' (i : ℕ), (1 / 10) ^ i * (1 / 10) ^ (n + 1).fact) (1 / (10 ^ n.fact) ^ n),
exact ineq2, norm_cast at ineq2 ineq3 ineq4 ⊢, rw <-nat.succ_eq_add_one, rw nat.fact_succ', exact gt_of_gt_of_ge ineq4 ineq3,
},
tidy,
},
end
-- Then our general theory about Liouville number in particular applies to α giving us α transcendental
theorem transcendental_α : transcendental α := liouville_numbers_transcendental α liouville_α
|
4b164a0cde8b8f54fb4db576feabe86efcd7d8c0 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/char_p/char_and_card.lean | 52b303e3667def851f34c8adba22ab98988dc095 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,397 | lean | /-
Copyright (c) 2022 Michael Stoll. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Stoll
-/
import algebra.char_p.basic
import group_theory.perm.cycle.type
/-!
# Characteristic and cardinality
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We prove some results relating characteristic and cardinality of finite rings
## Tags
characterstic, cardinality, ring
-/
/-- A prime `p` is a unit in a commutative ring `R` of nonzero characterstic iff it does not divide
the characteristic. -/
lemma is_unit_iff_not_dvd_char_of_ring_char_ne_zero (R : Type*) [comm_ring R] (p : ℕ) [fact p.prime]
(hR : ring_char R ≠ 0) :
is_unit (p : R) ↔ ¬ p ∣ ring_char R :=
begin
have hch := char_p.cast_eq_zero R (ring_char R),
have hp : p.prime := fact.out p.prime,
split,
{ rintros h₁ ⟨q, hq⟩,
rcases is_unit.exists_left_inv h₁ with ⟨a, ha⟩,
have h₃ : ¬ ring_char R ∣ q :=
begin
rintro ⟨r, hr⟩,
rw [hr, ← mul_assoc, mul_comm p, mul_assoc] at hq,
nth_rewrite 0 ← mul_one (ring_char R) at hq,
exact nat.prime.not_dvd_one hp ⟨r, mul_left_cancel₀ hR hq⟩,
end,
have h₄ := mt (char_p.int_cast_eq_zero_iff R (ring_char R) q).mp,
apply_fun (coe : ℕ → R) at hq,
apply_fun ((*) a) at hq,
rw [nat.cast_mul, hch, mul_zero, ← mul_assoc, ha, one_mul] at hq,
norm_cast at h₄,
exact h₄ h₃ hq.symm, },
{ intro h,
rcases (hp.coprime_iff_not_dvd.mpr h).is_coprime with ⟨a, b, hab⟩,
apply_fun (coe : ℤ → R) at hab,
push_cast at hab,
rw [hch, mul_zero, add_zero, mul_comm] at hab,
exact is_unit_of_mul_eq_one (p : R) a hab, },
end
/-- A prime `p` is a unit in a finite commutative ring `R`
iff it does not divide the characteristic. -/
lemma is_unit_iff_not_dvd_char (R : Type*) [comm_ring R] (p : ℕ) [fact p.prime] [finite R] :
is_unit (p : R) ↔ ¬ p ∣ ring_char R :=
is_unit_iff_not_dvd_char_of_ring_char_ne_zero R p $ char_p.char_ne_zero_of_finite R (ring_char R)
/-- The prime divisors of the characteristic of a finite commutative ring are exactly
the prime divisors of its cardinality. -/
lemma prime_dvd_char_iff_dvd_card {R : Type*} [comm_ring R] [fintype R] (p : ℕ) [fact p.prime] :
p ∣ ring_char R ↔ p ∣ fintype.card R :=
begin
refine ⟨λ h, h.trans $ int.coe_nat_dvd.mp $ (char_p.int_cast_eq_zero_iff R (ring_char R)
(fintype.card R)).mp $ by exact_mod_cast char_p.cast_card_eq_zero R, λ h, _⟩,
by_contra h₀,
rcases exists_prime_add_order_of_dvd_card p h with ⟨r, hr⟩,
have hr₁ := add_order_of_nsmul_eq_zero r,
rw [hr, nsmul_eq_mul] at hr₁,
rcases is_unit.exists_left_inv ((is_unit_iff_not_dvd_char R p).mpr h₀) with ⟨u, hu⟩,
apply_fun ((*) u) at hr₁,
rw [mul_zero, ← mul_assoc, hu, one_mul] at hr₁,
exact mt add_monoid.order_of_eq_one_iff.mpr
(ne_of_eq_of_ne hr (nat.prime.ne_one (fact.out p.prime))) hr₁,
end
/-- A prime that does not divide the cardinality of a finite commutative ring `R`
is a unit in `R`. -/
lemma not_is_unit_prime_of_dvd_card {R : Type*} [comm_ring R] [fintype R] (p : ℕ) [fact p.prime]
(hp : p ∣ fintype.card R) : ¬ is_unit (p : R) :=
mt (is_unit_iff_not_dvd_char R p).mp (not_not.mpr ((prime_dvd_char_iff_dvd_card p).mpr hp))
|
0b50a7a190dd3a98838ed37daa4e8198da14d0b8 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/measure/mutually_singular.lean | 2df9de47e0ecd74040364e7359f89e8a6f47192b | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 4,047 | lean | /-
Copyright (c) 2021 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Yury Kudryashov
-/
import measure_theory.measure.measure_space
/-! # Mutually singular measures
Two measures `μ`, `ν` are said to be mutually singular (`measure_theory.measure.mutually_singular`,
localized notation `μ ⊥ₘ ν`) if there exists a measurable set `s` such that `μ s = 0` and
`ν sᶜ = 0`. The measurability of `s` is an unnecessary assumption (see
`measure_theory.measure.mutually_singular.mk`) but we keep it because this way `rcases (h : μ ⊥ₘ ν)`
gives us a measurable set and usually it is easy to prove measurability.
In this file we define the predicate `measure_theory.measure.mutually_singular` and prove basic
facts about it.
## Tags
measure, mutually singular
-/
open set
open_locale measure_theory nnreal ennreal
namespace measure_theory
namespace measure
variables {α : Type*} {m0 : measurable_space α} {μ μ₁ μ₂ ν ν₁ ν₂ : measure α}
/-- Two measures `μ`, `ν` are said to be mutually singular if there exists a measurable set `s`
such that `μ s = 0` and `ν sᶜ = 0`. -/
def mutually_singular {m0 : measurable_space α} (μ ν : measure α) : Prop :=
∃ (s : set α), measurable_set s ∧ μ s = 0 ∧ ν sᶜ = 0
localized "infix ` ⊥ₘ `:60 := measure_theory.measure.mutually_singular" in measure_theory
namespace mutually_singular
lemma mk {s t : set α} (hs : μ s = 0) (ht : ν t = 0) (hst : univ ⊆ s ∪ t) :
mutually_singular μ ν :=
begin
use [to_measurable μ s, measurable_set_to_measurable _ _, (measure_to_measurable _).trans hs],
refine measure_mono_null (λ x hx, (hst trivial).resolve_left $ λ hxs, hx _) ht,
exact subset_to_measurable _ _ hxs
end
@[simp] lemma zero_right : μ ⊥ₘ 0 := ⟨∅, measurable_set.empty, measure_empty, rfl⟩
@[symm] lemma symm (h : ν ⊥ₘ μ) : μ ⊥ₘ ν :=
let ⟨i, hi, his, hit⟩ := h in ⟨iᶜ, hi.compl, hit, (compl_compl i).symm ▸ his⟩
lemma comm : μ ⊥ₘ ν ↔ ν ⊥ₘ μ := ⟨λ h, h.symm, λ h, h.symm⟩
@[simp] lemma zero_left : 0 ⊥ₘ μ := zero_right.symm
lemma mono_ac (h : μ₁ ⊥ₘ ν₁) (hμ : μ₂ ≪ μ₁) (hν : ν₂ ≪ ν₁) : μ₂ ⊥ₘ ν₂ :=
let ⟨s, hs, h₁, h₂⟩ := h in ⟨s, hs, hμ h₁, hν h₂⟩
lemma mono (h : μ₁ ⊥ₘ ν₁) (hμ : μ₂ ≤ μ₁) (hν : ν₂ ≤ ν₁) : μ₂ ⊥ₘ ν₂ :=
h.mono_ac hμ.absolutely_continuous hν.absolutely_continuous
@[simp] lemma sum_left {ι : Type*} [encodable ι] {μ : ι → measure α} :
(sum μ) ⊥ₘ ν ↔ ∀ i, μ i ⊥ₘ ν :=
begin
refine ⟨λ h i, h.mono (le_sum _ _) le_rfl, λ H, _⟩,
choose s hsm hsμ hsν using H,
refine ⟨⋂ i, s i, measurable_set.Inter hsm, _, _⟩,
{ rw [sum_apply _ (measurable_set.Inter hsm), ennreal.tsum_eq_zero],
exact λ i, measure_mono_null (Inter_subset _ _) (hsμ i) },
{ rwa [compl_Inter, measure_Union_null_iff], }
end
@[simp] lemma sum_right {ι : Type*} [encodable ι] {ν : ι → measure α} :
μ ⊥ₘ sum ν ↔ ∀ i, μ ⊥ₘ ν i :=
comm.trans $ sum_left.trans $ forall_congr $ λ i, comm
@[simp] lemma add_left_iff : μ₁ + μ₂ ⊥ₘ ν ↔ μ₁ ⊥ₘ ν ∧ μ₂ ⊥ₘ ν :=
by rw [← sum_cond, sum_left, bool.forall_bool, cond, cond, and.comm]
@[simp] lemma add_right_iff : μ ⊥ₘ ν₁ + ν₂ ↔ μ ⊥ₘ ν₁ ∧ μ ⊥ₘ ν₂ :=
comm.trans $ add_left_iff.trans $ and_congr comm comm
lemma add_left (h₁ : ν₁ ⊥ₘ μ) (h₂ : ν₂ ⊥ₘ μ) : ν₁ + ν₂ ⊥ₘ μ :=
add_left_iff.2 ⟨h₁, h₂⟩
lemma add_right (h₁ : μ ⊥ₘ ν₁) (h₂ : μ ⊥ₘ ν₂) : μ ⊥ₘ ν₁ + ν₂ :=
add_right_iff.2 ⟨h₁, h₂⟩
lemma smul (r : ℝ≥0∞) (h : ν ⊥ₘ μ) : r • ν ⊥ₘ μ :=
h.mono_ac (absolutely_continuous.rfl.smul r) absolutely_continuous.rfl
lemma smul_nnreal (r : ℝ≥0) (h : ν ⊥ₘ μ) : r • ν ⊥ₘ μ := h.smul r
end mutually_singular
end measure
end measure_theory
|
81854cb7dad9d6cad56b640426a41ecb4381ebb8 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /06_Inductive_Types.org.41.lean | 104dc422102086696945c34e8e8715fafd211e55 | [] | 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 | 295 | lean | /- page 91 -/
import standard
namespace hide
-- BEGIN
inductive fin : nat → Type :=
| fz : Π n, fin (nat.succ n)
| fs : Π {n}, fin n → fin (nat.succ n)
-- END
open nat
check fin.fz 0
check fin.fz 42
check fin.fs (fin.fz 3)
/- Every n is in types >= n, and only those types -/
end hide
|
86f02890f62156f40f5b2fef6a034318275634b3 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /hott/hit/two_quotient.hlean | 7463a5f70661cac6a1a935fbe6206f270d53f513 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,771 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import homotopy.circle eq2 algebra.e_closure cubical.cube
open quotient eq circle sum sigma equiv function relation
/-
This files defines a general class of nonrecursive HITs using just quotients.
We can define any HIT X which has
- a single 0-constructor
f : A → X (for some type A)
- a single 1-constructor
e : Π{a a' : A}, R a a' → a = a' (for some (type-valued) relation R on A)
and furthermore has 2-constructors which are all of the form
p = p'
where p, p' are of the form
- refl (f a), for some a : A;
- e r, for some r : R a a';
- ap f q, where q : a = a';
- inverses of such paths;
- concatenations of such paths.
so an example 2-constructor could be (as long as it typechecks):
ap f q' ⬝ ((e r)⁻¹ ⬝ ap f q)⁻¹ ⬝ e r' = idp
-/
namespace simple_two_quotient
section
parameters {A : Type}
(R : A → A → Type)
local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R
parameter (Q : Π⦃a⦄, T a a → Type)
variables ⦃a a' : A⦄ {s : R a a'} {r : T a a}
local abbreviation B := A ⊎ Σ(a : A) (r : T a a), Q r
inductive pre_two_quotient_rel : B → B → Type :=
| pre_Rmk {} : Π⦃a a'⦄ (r : R a a'), pre_two_quotient_rel (inl a) (inl a')
--BUG: if {} not provided, the alias for pre_Rmk is wrong
definition pre_two_quotient := quotient pre_two_quotient_rel
open pre_two_quotient_rel
local abbreviation C := quotient pre_two_quotient_rel
protected definition j [constructor] (a : A) : C := class_of pre_two_quotient_rel (inl a)
protected definition pre_aux [constructor] (q : Q r) : C :=
class_of pre_two_quotient_rel (inr ⟨a, r, q⟩)
protected definition e (s : R a a') : j a = j a' := eq_of_rel _ (pre_Rmk s)
protected definition et (t : T a a') : j a = j a' := e_closure.elim e t
protected definition f [unfold 7] (q : Q r) : S¹ → C :=
circle.elim (j a) (et r)
protected definition pre_rec [unfold 8] {P : C → Type}
(Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q))
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') (x : C) : P x :=
begin
induction x with p,
{ induction p,
{ apply Pj},
{ induction a with a1 a2, induction a2, apply Pa}},
{ induction H, esimp, apply Pe},
end
protected definition pre_elim [unfold 8] {P : Type} (Pj : A → P)
(Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') (x : C)
: P :=
pre_rec Pj Pa (λa a' s, pathover_of_eq (Pe s)) x
protected theorem rec_e {P : C → Type}
(Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q))
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') ⦃a a' : A⦄ (s : R a a')
: apdo (pre_rec Pj Pa Pe) (e s) = Pe s :=
!rec_eq_of_rel
protected theorem elim_e {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P)
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (s : R a a')
: ap (pre_elim Pj Pa Pe) (e s) = Pe s :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (e s)),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑pre_elim,rec_e],
end
protected definition elim_et {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P)
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (t : T a a')
: ap (pre_elim Pj Pa Pe) (et t) = e_closure.elim Pe t :=
ap_e_closure_elim_h e (elim_e Pj Pa Pe) t
inductive simple_two_quotient_rel : C → C → Type :=
| Rmk {} : Π{a : A} {r : T a a} (q : Q r) (x : circle),
simple_two_quotient_rel (f q x) (pre_aux q)
open simple_two_quotient_rel
definition simple_two_quotient := quotient simple_two_quotient_rel
local abbreviation D := simple_two_quotient
local abbreviation i := class_of simple_two_quotient_rel
definition incl0 (a : A) : D := i (j a)
protected definition aux (q : Q r) : D := i (pre_aux q)
definition incl1 (s : R a a') : incl0 a = incl0 a' := ap i (e s)
definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t
-- "wrong" version inclt, which is ap i (p ⬝ q) instead of ap i p ⬝ ap i q
-- it is used in the proof, because incltw is easier to work with
protected definition incltw (t : T a a') : incl0 a = incl0 a' := ap i (et t)
protected definition inclt_eq_incltw (t : T a a') : inclt t = incltw t :=
(ap_e_closure_elim i e t)⁻¹
definition incl2' (q : Q r) (x : S¹) : i (f q x) = aux q :=
eq_of_rel simple_two_quotient_rel (Rmk q x)
protected definition incl2w (q : Q r) : incltw r = idp :=
(ap02 i (elim_loop (j a) (et r))⁻¹) ⬝
(ap_compose i (f q) loop)⁻¹ ⬝
ap_is_constant (incl2' q) loop ⬝
!con.right_inv
definition incl2 (q : Q r) : inclt r = idp :=
inclt_eq_incltw r ⬝ incl2w q
local attribute simple_two_quotient f i D incl0 aux incl1 incl2' inclt [reducible]
local attribute i aux incl0 [constructor]
protected definition elim {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
(x : D) : P :=
begin
induction x,
{ refine (pre_elim _ _ _ a),
{ exact P0},
{ intro a r q, exact P0 a},
{ exact P1}},
{ exact abstract begin induction H, induction x,
{ exact idpath (P0 a)},
{ unfold f, apply eq_pathover, apply hdeg_square,
exact abstract ap_compose (pre_elim P0 _ P1) (f q) loop ⬝
ap _ !elim_loop ⬝
!elim_et ⬝
P2 q ⬝
!ap_constant⁻¹ end
} end end},
end
local attribute elim [unfold 8]
protected definition elim_on {P : Type} (x : D) (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
: P :=
elim P0 P1 P2 x
definition elim_incl1 {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s :=
(ap_compose (elim P0 P1 P2) i (e s))⁻¹ ⬝ !elim_e
definition elim_inclt {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t :=
ap_e_closure_elim_h incl1 (elim_incl1 P2) t
protected definition elim_incltw {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (incltw t) = e_closure.elim P1 t :=
(ap_compose (elim P0 P1 P2) i (et t))⁻¹ ⬝ !elim_et
protected theorem elim_inclt_eq_elim_incltw {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a')
: elim_inclt P2 t = ap (ap (elim P0 P1 P2)) (inclt_eq_incltw t) ⬝ elim_incltw P2 t :=
begin
unfold [elim_inclt,elim_incltw,inclt_eq_incltw,et],
refine !ap_e_closure_elim_h_eq ⬝ _,
rewrite [ap_inv,-con.assoc],
xrewrite [eq_of_square (ap_ap_e_closure_elim i (elim P0 P1 P2) e t)⁻¹ʰ],
rewrite [↓incl1,con.assoc], apply whisker_left,
rewrite [↑[elim_et,elim_incl1],+ap_e_closure_elim_h_eq,con_inv,↑[i,function.compose]],
rewrite [-con.assoc (_ ⬝ _),con.assoc _⁻¹,con.left_inv,▸*,-ap_inv,-ap_con],
apply ap (ap _),
krewrite [-eq_of_homotopy3_inv,-eq_of_homotopy3_con]
end
definition elim_incl2' {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r) : ap (elim P0 P1 P2) (incl2' q base) = idpath (P0 a) :=
!elim_eq_of_rel
-- set_option pp.implicit true
protected theorem elim_incl2w {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r)
: square (ap02 (elim P0 P1 P2) (incl2w q)) (P2 q) (elim_incltw P2 r) idp :=
begin
esimp [incl2w,ap02],
rewrite [+ap_con (ap _),▸*],
xrewrite [-ap_compose (ap _) (ap i)],
rewrite [+ap_inv],
xrewrite [eq_top_of_square
((ap_compose_natural (elim P0 P1 P2) i (elim_loop (j a) (et r)))⁻¹ʰ⁻¹ᵛ ⬝h
(ap_ap_compose (elim P0 P1 P2) i (f q) loop)⁻¹ʰ⁻¹ᵛ ⬝h
ap_ap_is_constant (elim P0 P1 P2) (incl2' q) loop ⬝h
ap_con_right_inv_sq (elim P0 P1 P2) (incl2' q base)),
↑[elim_incltw]],
apply whisker_tl,
rewrite [ap_is_constant_eq],
xrewrite [naturality_apdo_eq (λx, !elim_eq_of_rel) loop],
rewrite [↑elim_2,rec_loop,square_of_pathover_concato_eq,square_of_pathover_eq_concato,
eq_of_square_vconcat_eq,eq_of_square_eq_vconcat],
apply eq_vconcat,
{ apply ap (λx, _ ⬝ eq_con_inv_of_con_eq ((_ ⬝ x ⬝ _)⁻¹ ⬝ _) ⬝ _),
transitivity _, apply ap eq_of_square,
apply to_right_inv !eq_pathover_equiv_square (hdeg_square (elim_1 P A R Q P0 P1 a r q P2)),
transitivity _, apply eq_of_square_hdeg_square,
unfold elim_1, reflexivity},
rewrite [+con_inv,whisker_left_inv,+inv_inv,-whisker_right_inv,
con.assoc (whisker_left _ _),con.assoc _ (whisker_right _ _),▸*,
whisker_right_con_whisker_left _ !ap_constant],
xrewrite [-con.assoc _ _ (whisker_right _ _)],
rewrite [con.assoc _ _ (whisker_left _ _),idp_con_whisker_left,▸*,
con.assoc _ !ap_constant⁻¹,con.left_inv],
xrewrite [eq_con_inv_of_con_eq_whisker_left,▸*],
rewrite [+con.assoc _ _ !con.right_inv,
right_inv_eq_idp (
(λ(x : ap (elim P0 P1 P2) (incl2' q base) = idpath
(elim P0 P1 P2 (class_of simple_two_quotient_rel (f q base)))), x)
(elim_incl2' P2 q)),
↑[whisker_left]],
xrewrite [con2_con_con2],
rewrite [idp_con,↑elim_incl2',con.left_inv,whisker_right_inv,↑whisker_right],
xrewrite [con.assoc _ _ (_ ◾ _)],
rewrite [con.left_inv,▸*,-+con.assoc,con.assoc _⁻¹,↑[elim,function.compose],con.left_inv,
▸*,↑j,con.left_inv,idp_con],
apply square_of_eq, reflexivity
end
theorem elim_incl2 {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r)
: square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 r) idp :=
begin
rewrite [↑incl2,↑ap02,ap_con,elim_inclt_eq_elim_incltw],
apply whisker_tl,
apply elim_incl2w
end
end
end simple_two_quotient
--attribute simple_two_quotient.j [constructor] --TODO
attribute /-simple_two_quotient.rec-/ simple_two_quotient.elim [unfold 8] [recursor 8]
--attribute simple_two_quotient.elim_type [unfold 9]
attribute /-simple_two_quotient.rec_on-/ simple_two_quotient.elim_on [unfold 5]
--attribute simple_two_quotient.elim_type_on [unfold 6]
namespace two_quotient
open e_closure simple_two_quotient
section
parameters {A : Type}
(R : A → A → Type)
local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R
parameter (Q : Π⦃a a'⦄, T a a' → T a a' → Type)
variables ⦃a a' : A⦄ {s : R a a'} {t t' : T a a'}
inductive two_quotient_Q : Π⦃a : A⦄, e_closure R a a → Type :=
| Qmk : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄, Q t t' → two_quotient_Q (t ⬝r t'⁻¹ʳ)
open two_quotient_Q
local abbreviation Q2 := two_quotient_Q
definition two_quotient := simple_two_quotient R Q2
definition incl0 (a : A) : two_quotient := incl0 _ _ a
definition incl1 (s : R a a') : incl0 a = incl0 a' := incl1 _ _ s
definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t
definition incl2 (q : Q t t') : inclt t = inclt t' :=
eq_of_con_inv_eq_idp (incl2 _ _ (Qmk R q))
protected definition elim {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
(x : two_quotient) : P :=
begin
induction x,
{ exact P0 a},
{ exact P1 s},
{ exact abstract [unfold 10] begin induction q with a a' t t' q,
rewrite [↑e_closure.elim],
apply con_inv_eq_idp, exact P2 q end end},
end
local attribute elim [unfold 8]
protected definition elim_on {P : Type} (x : two_quotient) (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
: P :=
elim P0 P1 P2 x
definition elim_incl1 {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s :=
!elim_incl1
definition elim_inclt {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t :=
!elim_inclt --ap_e_closure_elim_h incl1 (elim_incl1 P2) t
theorem elim_incl2 {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t')
: square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 t) (elim_inclt P2 t') :=
begin
rewrite [↑[incl2,elim],ap_eq_of_con_inv_eq_idp],
xrewrite [eq_top_of_square (elim_incl2 R Q2 P0 P1 (elim_1 A R Q P P0 P1 P2) (Qmk R q))],
-- esimp, --doesn't fold elim_inclt back. The following tactic is just a "custom esimp"
xrewrite [{simple_two_quotient.elim_inclt R Q2 (elim_1 A R Q P P0 P1 P2)
(t ⬝r t'⁻¹ʳ)}
idpath (ap_con (simple_two_quotient.elim R Q2 P0 P1 (elim_1 A R Q P P0 P1 P2))
(inclt t) (inclt t')⁻¹ ⬝
(simple_two_quotient.elim_inclt R Q2 (elim_1 A R Q P P0 P1 P2) t ◾
(ap_inv (simple_two_quotient.elim R Q2 P0 P1 (elim_1 A R Q P P0 P1 P2))
(inclt t') ⬝
inverse2 (simple_two_quotient.elim_inclt R Q2 (elim_1 A R Q P P0 P1 P2) t')))),▸*],
rewrite [-con.assoc _ _ (con_inv_eq_idp _),-con.assoc _ _ (_ ◾ _),con.assoc _ _ (ap_con _ _ _),
con.left_inv,↑whisker_left,con2_con_con2,-con.assoc (ap_inv _ _)⁻¹,
con.left_inv,+idp_con,eq_of_con_inv_eq_idp_con2],
xrewrite [to_left_inv !eq_equiv_con_inv_eq_idp (P2 q)],
apply top_deg_square
end
end
end two_quotient
--attribute two_quotient.j [constructor] --TODO
attribute /-two_quotient.rec-/ two_quotient.elim [unfold 8] [recursor 8]
--attribute two_quotient.elim_type [unfold 9]
attribute /-two_quotient.rec_on-/ two_quotient.elim_on [unfold 5]
--attribute two_quotient.elim_type_on [unfold 6]
|
7546a0ac3309e9b28203249bc1f81cc8189d0bb5 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/module/submodule/pointwise.lean | b119c8f629ff1e205062c3a3b27bb39366aa1294 | [
"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 | 7,867 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import group_theory.subgroup.pointwise
import linear_algebra.span
/-! # Pointwise instances on `submodule`s
This file provides:
* `submodule.has_pointwise_neg`
and the actions
* `submodule.pointwise_distrib_mul_action`
* `submodule.pointwise_mul_action_with_zero`
which matches the action of `mul_action_set`.
These actions are available in the `pointwise` locale.
## Implementation notes
Most of the lemmas in this file are direct copies of lemmas from
`group_theory/submonoid/pointwise.lean`.
-/
variables {α : Type*} {R : Type*} {M : Type*}
open_locale pointwise
namespace submodule
section neg
section semiring
variables [semiring R] [add_comm_group M] [module R M]
/-- The submodule with every element negated. Note if `R` is a ring and not just a semiring, this
is a no-op, as shown by `submodule.neg_eq_self`.
Recall that When `R` is the semiring corresponding to the nonnegative elements of `R'`,
`submodule R' M` is the type of cones of `M`. This instance reflects such cones about `0`.
This is available as an instance in the `pointwise` locale. -/
protected def has_pointwise_neg : has_neg (submodule R M) :=
{ neg := λ p,
{ carrier := -(p : set M),
smul_mem' := λ r m hm, set.mem_neg.2 $ smul_neg r m ▸ p.smul_mem r $ set.mem_neg.1 hm,
..(- p.to_add_submonoid) } }
localized "attribute [instance] submodule.has_pointwise_neg" in pointwise
open_locale pointwise
@[simp] lemma coe_set_neg (S : submodule R M) : ↑(-S) = -(S : set M) := rfl
@[simp] lemma neg_to_add_submonoid (S : submodule R M) :
(-S).to_add_submonoid = -S.to_add_submonoid := rfl
@[simp] lemma mem_neg {g : M} {S : submodule R M} : g ∈ -S ↔ -g ∈ S := iff.rfl
/-- `submodule.has_pointwise_neg` is involutive.
This is available as an instance in the `pointwise` locale. -/
protected def has_involutive_pointwise_neg : has_involutive_neg (submodule R M) :=
{ neg := has_neg.neg,
neg_neg := λ S, set_like.coe_injective $ neg_neg _ }
localized "attribute [instance] submodule.has_involutive_pointwise_neg" in pointwise
@[simp] lemma neg_le_neg (S T : submodule R M) : -S ≤ -T ↔ S ≤ T :=
set_like.coe_subset_coe.symm.trans set.neg_subset_neg
lemma neg_le (S T : submodule R M) : -S ≤ T ↔ S ≤ -T :=
set_like.coe_subset_coe.symm.trans set.neg_subset
/-- `submodule.has_pointwise_neg` as an order isomorphism. -/
def neg_order_iso : submodule R M ≃o submodule R M :=
{ to_equiv := equiv.neg _,
map_rel_iff' := neg_le_neg }
lemma closure_neg (s : set M) : span R (-s) = -(span R s) :=
begin
apply le_antisymm,
{ rw [span_le, coe_set_neg, ←set.neg_subset, neg_neg],
exact subset_span },
{ rw [neg_le, span_le, coe_set_neg, ←set.neg_subset],
exact subset_span }
end
@[simp]
lemma neg_inf (S T : submodule R M) : -(S ⊓ T) = (-S) ⊓ (-T) :=
set_like.coe_injective set.inter_neg
@[simp]
lemma neg_sup (S T : submodule R M) : -(S ⊔ T) = (-S) ⊔ (-T) :=
(neg_order_iso : submodule R M ≃o submodule R M).map_sup S T
@[simp]
lemma neg_bot : -(⊥ : submodule R M) = ⊥ :=
set_like.coe_injective $ (set.neg_singleton 0).trans $ congr_arg _ neg_zero
@[simp]
lemma neg_top : -(⊤ : submodule R M) = ⊤ :=
set_like.coe_injective $ set.neg_univ
@[simp]
lemma neg_infi {ι : Sort*} (S : ι → submodule R M) : -(⨅ i, S i) = ⨅ i, -S i :=
(neg_order_iso : submodule R M ≃o submodule R M).map_infi _
@[simp]
lemma neg_supr {ι : Sort*} (S : ι → submodule R M) : -(⨆ i, S i) = ⨆ i, -(S i) :=
(neg_order_iso : submodule R M ≃o submodule R M).map_supr _
end semiring
open_locale pointwise
@[simp] lemma neg_eq_self [ring R] [add_comm_group M] [module R M] (p : submodule R M) : -p = p :=
ext $ λ _, p.neg_mem_iff
end neg
variables [semiring R] [add_comm_monoid M] [module R M]
instance pointwise_add_comm_monoid : add_comm_monoid (submodule R M) :=
{ 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 (p q : submodule R M) : p + q = p ⊔ q := rfl
@[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl
instance : canonically_ordered_add_monoid (submodule R M) :=
{ zero := 0,
bot := ⊥,
add := (+),
add_le_add_left := λ a b, sup_le_sup_left,
exists_add_of_le := λ a b h, ⟨b, (sup_eq_right.2 h).symm⟩,
le_self_add := λ a b, le_sup_left,
..submodule.pointwise_add_comm_monoid,
..submodule.complete_lattice }
section
variables [monoid α] [distrib_mul_action α M] [smul_comm_class α R M]
/-- The action on a submodule corresponding to applying the action to every element.
This is available as an instance in the `pointwise` locale. -/
protected def pointwise_distrib_mul_action : distrib_mul_action α (submodule R M) :=
{ smul := λ a S, S.map (distrib_mul_action.to_linear_map R M a : M →ₗ[R] M),
one_smul := λ S,
(congr_arg (λ f : module.End R M, S.map f) (linear_map.ext $ by exact one_smul α)).trans
S.map_id,
mul_smul := λ a₁ a₂ S,
(congr_arg (λ f : module.End R M, S.map f) (linear_map.ext $ by exact mul_smul _ _)).trans
(S.map_comp _ _),
smul_zero := λ a, map_bot _,
smul_add := λ a S₁ S₂, map_sup _ _ _ }
localized "attribute [instance] submodule.pointwise_distrib_mul_action" in pointwise
open_locale pointwise
@[simp] lemma coe_pointwise_smul (a : α) (S : submodule R M) : ↑(a • S) = a • (S : set M) := rfl
@[simp] lemma pointwise_smul_to_add_submonoid (a : α) (S : submodule R M) :
(a • S).to_add_submonoid = a • S.to_add_submonoid := rfl
@[simp] lemma pointwise_smul_to_add_subgroup {R M : Type*}
[ring R] [add_comm_group M] [distrib_mul_action α M] [module R M] [smul_comm_class α R M]
(a : α) (S : submodule R M) :
(a • S).to_add_subgroup = a • S.to_add_subgroup := rfl
lemma smul_mem_pointwise_smul (m : M) (a : α) (S : submodule R M) : m ∈ S → a • m ∈ a • S :=
(set.smul_mem_smul_set : _ → _ ∈ a • (S : set M))
/-- See also `submodule.smul_bot`. -/
@[simp] lemma smul_bot' (a : α) : a • (⊥ : submodule R M) = ⊥ := map_bot _
/-- See also `submodule.smul_sup`. -/
lemma smul_sup' (a : α) (S T : submodule R M) : a • (S ⊔ T) = a • S ⊔ a • T := map_sup _ _ _
lemma smul_span (a : α) (s : set M) : a • span R s = span R (a • s) := map_span _ _
lemma span_smul (a : α) (s : set M) : span R (a • s) = a • span R s := eq.symm (span_image _).symm
instance pointwise_central_scalar [distrib_mul_action αᵐᵒᵖ M] [smul_comm_class αᵐᵒᵖ R M]
[is_central_scalar α M] :
is_central_scalar α (submodule R M) :=
⟨λ a S, congr_arg (λ f : module.End R M, S.map f) $ linear_map.ext $ by exact op_smul_eq_smul _⟩
@[simp] lemma smul_le_self_of_tower {α : Type*}
[semiring α] [module α R] [module α M] [smul_comm_class α R M] [is_scalar_tower α R M]
(a : α) (S : submodule R M) : a • S ≤ S :=
begin
rintro y ⟨x, hx, rfl⟩,
exact smul_of_tower_mem _ a hx,
end
end
section
variables [semiring α] [module α M] [smul_comm_class α R M]
/-- The action on a submodule corresponding to applying the action to every element.
This is available as an instance in the `pointwise` locale.
This is a stronger version of `submodule.pointwise_distrib_mul_action`. Note that `add_smul` does
not hold so this cannot be stated as a `module`. -/
protected def pointwise_mul_action_with_zero : mul_action_with_zero α (submodule R M) :=
{ zero_smul := λ S,
(congr_arg (λ f : M →ₗ[R] M, S.map f) (linear_map.ext $ by exact zero_smul α)).trans S.map_zero,
.. submodule.pointwise_distrib_mul_action }
localized "attribute [instance] submodule.pointwise_mul_action_with_zero" in pointwise
end
end submodule
|
463357aa4a573635285d8a645b81fa9bdee51e06 | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/data/nat/basic.lean | 1f80c2121ea97d7428a7b4903f7d89f58f719a58 | [
"Apache-2.0"
] | permissive | EdAyers/mathlib | 9ecfb2f14bd6caad748b64c9c131befbff0fb4e0 | ca5d4c1f16f9c451cf7170b10105d0051db79e1b | refs/heads/master | 1,626,189,395,845 | 1,555,284,396,000 | 1,555,284,396,000 | 144,004,030 | 0 | 0 | Apache-2.0 | 1,533,727,664,000 | 1,533,727,663,000 | null | UTF-8 | Lean | false | false | 38,082 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro
Basic operations on the natural numbers.
-/
import logic.basic algebra.ordered_ring data.option.basic
universes u v
namespace nat
variables {m n k : ℕ}
-- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding
-- during pattern matching. These lemmas package them back up as typeclass
-- mediated operations.
@[simp] theorem add_def {a b : ℕ} : nat.add a b = a + b := rfl
@[simp] theorem mul_def {a b : ℕ} : nat.mul a b = a * b := rfl
attribute [simp] nat.add_sub_cancel nat.add_sub_cancel_left
attribute [simp] nat.sub_self
theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m :=
⟨succ_inj, congr_arg _⟩
theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n :=
⟨le_of_succ_le_succ, succ_le_succ⟩
theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n :=
succ_le_succ_iff
lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n :=
⟨lt_of_succ_le, succ_le_of_lt⟩
theorem of_le_succ {n m : ℕ} (H : n ≤ m.succ) : n ≤ m ∨ n = m.succ :=
(lt_or_eq_of_le H).imp le_of_lt_succ id
@[elab_as_eliminator]
def le_rec_on {C : ℕ → Sort u} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π {k}, C k → C (k+1)) → C n → C m
| 0 H next x := eq.rec_on (eq_zero_of_le_zero H) x
| (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next $ le_rec_on h @next x) (λ h : n = m + 1, eq.rec_on h x)
theorem le_rec_on_self {C : ℕ → Sort u} {n} {h : n ≤ n} {next} (x : C n) : (le_rec_on h next x : C n) = x :=
by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl]
theorem le_rec_on_succ {C : ℕ → Sort u} {n m} (h1 : n ≤ m) {h2 : n ≤ m+1} {next} (x : C n) :
(le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) :=
by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] }
theorem le_rec_on_succ' {C : ℕ → Sort u} {n} {h : n ≤ n+1} {next} (x : C n) :
(le_rec_on h next x : C (n+1)) = next x :=
by rw [le_rec_on_succ (le_refl n), le_rec_on_self]
theorem le_rec_on_trans {C : ℕ → Sort u} {n m k} (hnm : n ≤ m) (hmk : m ≤ k) {next} (x : C n) :
(le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) :=
begin
induction hmk with k hmk ih, { rw le_rec_on_self },
rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ]
end
theorem le_rec_on_injective {C : ℕ → Sort u} {n m} (hnm : n ≤ m)
(next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.injective (next n)) :
function.injective (le_rec_on hnm next) :=
begin
induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H },
intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H)
end
theorem le_rec_on_surjective {C : ℕ → Sort u} {n m} (hnm : n ≤ m)
(next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.surjective (next n)) :
function.surjective (le_rec_on hnm next) :=
begin
induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self },
intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ
end
theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H]
theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) :=
by rw [← sub_one, nat.sub_sub, one_add]; refl
lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl
lemma one_le_of_lt {n m : ℕ} (h : n < m) : 1 ≤ m :=
lt_of_le_of_lt (nat.zero_le _) h
lemma le_pred_of_lt {n m : ℕ} (h : m < n) : m ≤ n - 1 :=
nat.sub_le_sub_right h 1
/-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/
@[simp] lemma pred_one_add (n : ℕ) : pred (1 + n) = n :=
by rw [add_comm, add_one, pred_succ]
theorem pos_iff_ne_zero : n > 0 ↔ n ≠ 0 :=
⟨ne_of_gt, nat.pos_of_ne_zero⟩
theorem pos_iff_ne_zero' : 0 < n ↔ n ≠ 0 := pos_iff_ne_zero
lemma one_lt_iff_ne_zero_and_ne_one : ∀ {n : ℕ}, 1 < n ↔ n ≠ 0 ∧ n ≠ 1
| 0 := dec_trivial
| 1 := dec_trivial
| (n+2) := dec_trivial
theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b :=
have h3 : a ≤ b, from le_of_lt_succ h1,
or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3))
protected theorem le_sub_add (n m : ℕ) : n ≤ n - m + m :=
or.elim (le_total n m)
(assume : n ≤ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end)
(assume : m ≤ n, begin rw (nat.sub_add_cancel this) end)
theorem sub_add_eq_max (n m : ℕ) : n - m + m = max n m :=
eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ λ k h₁ h₂,
by rw ← nat.sub_add_cancel h₂; exact
add_le_add_right (nat.sub_le_sub_right h₁ _) _
theorem sub_add_min (n m : ℕ) : n - m + min n m = n :=
(le_total n m).elim
(λ h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add])
(λ h, by rw [min_eq_right h, nat.sub_add_cancel h])
protected theorem add_sub_cancel' {n m : ℕ} (h : n ≥ m) : m + (n - m) = n :=
by rw [add_comm, nat.sub_add_cancel h]
protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n :=
begin rw [h, nat.add_sub_cancel_left] end
theorem sub_cancel {a b c : ℕ} (h₁ : a ≤ b) (h₂ : a ≤ c) (w : b - a = c - a) : b = c :=
by rw [←nat.sub_add_cancel h₁, ←nat.sub_add_cancel h₂, w]
lemma sub_sub_sub_cancel_right {a b c : ℕ} (h₂ : c ≤ b) : (a - c) - (b - c) = a - b :=
by rw [nat.sub_sub, ←nat.add_sub_assoc h₂, nat.add_sub_cancel_left]
theorem sub_min (n m : ℕ) : n - min n m = n - m :=
nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min]
protected theorem lt_of_sub_pos (h : n - m > 0) : m < n :=
lt_of_not_ge
(assume : m ≥ n,
have n - m = 0, from sub_eq_zero_of_le this,
begin rw this at h, exact lt_irrefl _ h end)
protected theorem lt_of_sub_lt_sub_right : m - k < n - k → m < n :=
lt_imp_lt_of_le_imp_le (λ h, nat.sub_le_sub_right h _)
protected theorem lt_of_sub_lt_sub_left : m - n < m - k → k < n :=
lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _)
protected theorem sub_lt_self (h₁ : m > 0) (h₂ : n > 0) : m - n < m :=
calc
m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos h₂]
... = pred m - pred n : by rw succ_sub_succ
... ≤ pred m : sub_le _ _
... < succ (pred m) : lt_succ_self _
... = m : succ_pred_eq_of_pos h₁
protected theorem le_sub_right_of_add_le (h : m + k ≤ n) : m ≤ n - k :=
by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k
protected theorem le_sub_left_of_add_le (h : k + m ≤ n) : m ≤ n - k :=
nat.le_sub_right_of_add_le (by rwa add_comm at h)
protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k :=
lt_of_succ_le $ nat.le_sub_right_of_add_le $
by rw succ_add; exact succ_le_of_lt h
protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k :=
nat.lt_sub_right_of_add_lt (by rwa add_comm at h)
protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n :=
@nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel)
protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n :=
by rw add_comm; exact nat.add_lt_of_lt_sub_right h
protected theorem le_add_of_sub_le_right : n - k ≤ m → n ≤ m + k :=
le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt
protected theorem le_add_of_sub_le_left : n - k ≤ m → n ≤ k + m :=
le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt
protected theorem lt_add_of_sub_lt_right : n - k < m → n < m + k :=
lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le
protected theorem lt_add_of_sub_lt_left : n - k < m → n < k + m :=
lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le
protected theorem sub_le_left_of_le_add : n ≤ k + m → n - k ≤ m :=
le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left
protected theorem sub_le_right_of_le_add : n ≤ m + k → n - k ≤ m :=
le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right
protected theorem sub_lt_left_iff_lt_add (H : n ≤ k) : k - n < m ↔ k < n + m :=
⟨nat.lt_add_of_sub_lt_left,
λ h₁,
have succ k ≤ n + m, from succ_le_of_lt h₁,
have succ (k - n) ≤ m, from
calc succ (k - n) = succ k - n : by rw (succ_sub H)
... ≤ n + m - n : nat.sub_le_sub_right this n
... = m : by rw nat.add_sub_cancel_left,
lt_of_succ_le this⟩
protected theorem le_sub_left_iff_add_le (H : m ≤ k) : n ≤ k - m ↔ m + n ≤ k :=
le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H)
protected theorem le_sub_right_iff_add_le (H : n ≤ k) : m ≤ k - n ↔ m + n ≤ k :=
by rw [nat.le_sub_left_iff_add_le H, add_comm]
protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k :=
⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩
protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k :=
by rw [nat.lt_sub_left_iff_add_lt, add_comm]
theorem sub_le_left_iff_le_add : m - n ≤ k ↔ m ≤ n + k :=
le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt
theorem sub_le_right_iff_le_add : m - k ≤ n ↔ m ≤ n + k :=
by rw [nat.sub_le_left_iff_le_add, add_comm]
protected theorem sub_lt_right_iff_lt_add (H : k ≤ m) : m - k < n ↔ m < n + k :=
by rw [nat.sub_lt_left_iff_lt_add H, add_comm]
protected theorem sub_le_sub_left_iff (H : k ≤ m) : m - n ≤ m - k ↔ k ≤ n :=
⟨λ h,
have k + (m - k) - n ≤ m - k, by rwa nat.add_sub_cancel' H,
nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this),
nat.sub_le_sub_left _⟩
protected theorem sub_lt_sub_right_iff (H : k ≤ m) : m - k < n - k ↔ m < n :=
lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H)
protected theorem sub_lt_sub_left_iff (H : n ≤ m) : m - n < m - k ↔ k < n :=
lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H)
protected theorem sub_le_iff : m - n ≤ k ↔ m - k ≤ n :=
nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm
protected lemma sub_le_self (n m : ℕ) : n - m ≤ n :=
nat.sub_le_left_of_le_add (nat.le_add_left _ _)
protected theorem sub_lt_iff (h₁ : n ≤ m) (h₂ : k ≤ m) : m - n < k ↔ m - k < n :=
(nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add h₂).symm
lemma pred_le_iff {n m : ℕ} : pred n ≤ m ↔ n ≤ succ m :=
@nat.sub_le_right_iff_le_add n m 1
lemma lt_pred_iff {n m : ℕ} : n < pred m ↔ succ n < m :=
@nat.lt_sub_right_iff_add_lt n 1 m
protected theorem mul_ne_zero {n m : ℕ} (n0 : n ≠ 0) (m0 : m ≠ 0) : n * m ≠ 0
| nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0
@[simp] protected theorem mul_eq_zero {a b : ℕ} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt})
@[simp] protected theorem zero_eq_mul {a b : ℕ} : 0 = a * b ↔ a = 0 ∨ b = 0 :=
by rw [eq_comm, nat.mul_eq_zero]
@[elab_as_eliminator]
protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n
| n := H n (λ m hm, strong_rec' m)
attribute [simp] nat.div_self
protected lemma div_le_of_le_mul' {m n : ℕ} {k} (h : m ≤ k * n) : m / k ≤ n :=
(eq_zero_or_pos k).elim
(λ k0, by rw [k0, nat.div_zero]; apply zero_le)
(λ k0, (decidable.mul_le_mul_left k0).1 $
calc k * (m / k)
≤ m % k + k * (m / k) : le_add_left _ _
... = m : mod_add_div _ _
... ≤ k * n : h)
protected lemma div_le_self' (m n : ℕ) : m / n ≤ m :=
(eq_zero_or_pos n).elim
(λ n0, by rw [n0, nat.div_zero]; apply zero_le)
(λ n0, nat.div_le_of_le_mul' $ calc
m = 1 * m : (one_mul _).symm
... ≤ n * m : mul_le_mul_right _ n0)
theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y :=
begin
revert x, refine nat.strong_rec' _ y,
clear y, intros y IH x,
cases decidable.lt_or_le y k with h h,
{ rw [div_eq_of_lt h],
cases x with x,
{ simp [zero_mul, zero_le] },
{ rw succ_mul,
exact iff_of_false (not_succ_le_zero _)
(not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } },
{ rw [div_eq_sub_div k0 h],
cases x with x,
{ simp [zero_mul, zero_le] },
{ rw [← add_one, nat.add_le_add_iff_le_right, succ_mul,
IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } }
end
theorem div_mul_le_self' (m n : ℕ) : m / n * n ≤ m :=
(nat.eq_zero_or_pos n).elim (λ n0, by simp [n0, zero_le]) $ λ n0,
(le_div_iff_mul_le' n0).1 (le_refl _)
theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k :=
lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0
protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k :=
(nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk,
(le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h
protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) :
a = b * c :=
by rw [← H2, nat.mul_div_cancel' H1]
protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : b > 0) (H' : b ∣ a) :
a / b = c ↔ a = b * c :=
⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩
protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : b > 0) (H' : b ∣ a) :
a / b = c ↔ a = c * b :=
by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2]
protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b :=
by rw [mul_comm,nat.div_mul_cancel Hd]
protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) :
n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k :=
⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩,
λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left];
simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩
lemma two_mul_odd_div_two {n : ℕ} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 :=
by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]}
lemma div_dvd_of_dvd {a b : ℕ} (h : b ∣ a) : (a / b) ∣ a :=
⟨b, (nat.div_mul_cancel h).symm⟩
protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b :=
nat.pos_of_ne_zero (λ h, lt_irrefl a
(calc a = a % b : by simpa [h] using (mod_add_div a b).symm
... < b : nat.mod_lt a hb
... ≤ a : hba))
protected theorem mul_right_inj {a b c : ℕ} (ha : a > 0) : b * a = c * a ↔ b = c :=
⟨nat.eq_of_mul_eq_mul_right ha, λ e, e ▸ rfl⟩
protected theorem mul_left_inj {a b c : ℕ} (ha : a > 0) : a * b = a * c ↔ b = c :=
⟨nat.eq_of_mul_eq_mul_left ha, λ e, e ▸ rfl⟩
protected lemma div_div_self : ∀ {a b : ℕ}, b ∣ a → 0 < a → a / (a / b) = b
| a 0 h₁ h₂ := by rw eq_zero_of_zero_dvd h₁; refl
| 0 b h₁ h₂ := absurd h₂ dec_trivial
| (a+1) (b+1) h₁ h₂ :=
(nat.mul_right_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $
by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁]
protected lemma div_lt_of_lt_mul {m n k : ℕ} (h : m < n * k) : m / n < k :=
lt_of_mul_lt_mul_left
(calc n * (m / n) ≤ m % n + n * (m / n) : nat.le_add_left _ _
... = m : mod_add_div _ _
... < n * k : h)
(nat.zero_le n)
protected lemma div_eq_zero_iff {a b : ℕ} (hb : 0 < b) : a / b = 0 ↔ a < b :=
⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb,
λ h, by rw [← nat.mul_left_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div,
mod_eq_of_lt h, mul_zero, add_zero]⟩
lemma mod_mul_right_div_self (a b c : ℕ) : a % (b * c) / b = (a / b) % c :=
if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc]
else by conv {to_rhs, rw ← mod_add_div a (b * c)};
rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left,
mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))]
lemma mod_mul_left_div_self (a b c : ℕ) : a % (c * b) / b = (a / b) % c :=
by rw [mul_comm c, mod_mul_right_div_self]
@[simp] protected theorem dvd_one {n : ℕ} : n ∣ 1 ↔ n = 1 :=
⟨eq_one_of_dvd_one, λ e, e.symm ▸ dvd_refl _⟩
protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m :=
(nat.dvd_add_iff_left h).symm
protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n :=
(nat.dvd_add_iff_right h).symm
protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : a > 0) : a * b ∣ a * c ↔ b ∣ c :=
exists_congr $ λ d, by rw [mul_assoc, nat.mul_left_inj ha]
protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : c > 0) : a * c ∣ b * c ↔ a ∣ b :=
exists_congr $ λ d, by rw [mul_right_comm, nat.mul_right_inj hc]
@[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n :=
(eq_zero_or_pos n).elim
(λ n0, by simp [n0])
(λ npos, mod_eq_of_lt (mod_lt _ npos))
theorem add_pos_left {m : ℕ} (h : m > 0) (n : ℕ) : m + n > 0 :=
calc
m + n > 0 + n : nat.add_lt_add_right h n
... = n : nat.zero_add n
... ≥ 0 : zero_le n
theorem add_pos_right (m : ℕ) {n : ℕ} (h : n > 0) : m + n > 0 :=
begin rw add_comm, exact add_pos_left h m end
theorem add_pos_iff_pos_or_pos (m n : ℕ) : m + n > 0 ↔ m > 0 ∨ n > 0 :=
iff.intro
begin
intro h,
cases m with m,
{simp [zero_add] at h, exact or.inr h},
exact or.inl (succ_pos _)
end
begin
intro h, cases h with mpos npos,
{ apply add_pos_left mpos },
apply add_pos_right _ npos
end
lemma add_eq_one_iff : ∀ {a b : ℕ}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0)
| 0 0 := dec_trivial
| 0 1 := dec_trivial
| 1 0 := dec_trivial
| 1 1 := dec_trivial
| (a+2) _ := by rw add_right_comm; exact dec_trivial
| _ (b+2) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp
lemma mul_eq_one_iff : ∀ {a b : ℕ}, a * b = 1 ↔ a = 1 ∧ b = 1
| 0 0 := dec_trivial
| 0 1 := dec_trivial
| 1 0 := dec_trivial
| (a+2) 0 := by simp
| 0 (b+2) := by simp
| (a+1) (b+1) := ⟨λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one,
(add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2],
by clear_aux_decl; finish⟩
lemma mul_right_eq_self_iff {a b : ℕ} (ha : 0 < a): a * b = a ↔ b = 1 :=
suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this,
nat.mul_left_inj ha
lemma mul_left_eq_self_iff {a b : ℕ} (hb : 0 < b): a * b = b ↔ a = 1 :=
by rw [mul_comm, nat.mul_right_eq_self_iff hb]
lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) :=
lt_succ_iff.trans le_iff_lt_or_eq
theorem le_zero_iff {i : ℕ} : i ≤ 0 ↔ i = 0 :=
⟨nat.eq_zero_of_le_zero, assume h, h ▸ le_refl i⟩
theorem le_add_one_iff {i j : ℕ} : i ≤ j + 1 ↔ (i ≤ j ∨ i = j + 1) :=
⟨assume h,
match nat.eq_or_lt_of_le h with
| or.inl h := or.inr h
| or.inr h := or.inl $ nat.le_of_succ_le_succ h
end,
or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩
theorem mul_self_inj {n m : ℕ} : n * n = m * m ↔ n = m :=
le_antisymm_iff.trans (le_antisymm_iff.trans
(and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm
instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) :
∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) :=
begin
induction n with n IH; intro; resetI,
{ exact is_true (λ n, dec_trivial) },
cases IH (λ k h, P k (lt_succ_of_lt h)) with h,
{ refine is_false (mt _ h), intros hn k h, apply hn },
by_cases p : P n (lt_succ_self n),
{ exact is_true (λ k h',
(lt_or_eq_of_le $ le_of_lt_succ h').elim (h _)
(λ e, match k, e, h' with _, rfl, h := p end)) },
{ exact is_false (mt (λ hn, hn _ _) p) }
end
instance decidable_forall_fin {n : ℕ} (P : fin n → Prop)
[H : decidable_pred P] : decidable (∀ i, P i) :=
decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩
instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop)
[H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) :=
decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h))
⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩
instance decidable_lo_hi (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x < hi → P x) :=
decidable_of_iff (∀ x < hi - lo, P (lo + x))
⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $
(not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh);
rwa [nat.add_sub_of_le hl] at this,
λal x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩
instance decidable_lo_hi_le (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x ≤ hi → P x) :=
decidable_of_iff (∀x, lo ≤ x → x < hi + 1 → P x) $
ball_congr $ λ x hl, imp_congr lt_succ_iff iff.rfl
protected theorem bit0_le {n m : ℕ} (h : n ≤ m) : bit0 n ≤ bit0 m :=
add_le_add h h
protected theorem bit1_le {n m : ℕ} (h : n ≤ m) : bit1 n ≤ bit1 m :=
succ_le_succ (add_le_add h h)
theorem bit_le : ∀ (b : bool) {n m : ℕ}, n ≤ m → bit b n ≤ bit b m
| tt n m h := nat.bit1_le h
| ff n m h := nat.bit0_le h
theorem bit_ne_zero (b) {n} (h : n ≠ 0) : bit b n ≠ 0 :=
by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _]
theorem bit0_le_bit : ∀ (b) {m n : ℕ}, m ≤ n → bit0 m ≤ bit b n
| tt m n h := le_of_lt $ nat.bit0_lt_bit1 h
| ff m n h := nat.bit0_le h
theorem bit_le_bit1 : ∀ (b) {m n : ℕ}, m ≤ n → bit b m ≤ bit1 n
| ff m n h := le_of_lt $ nat.bit0_lt_bit1 h
| tt m n h := nat.bit1_le h
theorem bit_lt_bit0 : ∀ (b) {n m : ℕ}, n < m → bit b n < bit0 m
| tt n m h := nat.bit1_lt_bit0 h
| ff n m h := nat.bit0_lt h
theorem bit_lt_bit (a b) {n m : ℕ} (h : n < m) : bit a n < bit b m :=
lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _))
/- partial subtraction -/
/-- Partial predecessor operation. Returns `ppred n = some m`
if `n = m + 1`, otherwise `none`. -/
@[simp] def ppred : ℕ → option ℕ
| 0 := none
| (n+1) := some n
/-- Partial subtraction operation. Returns `psub m n = some k`
if `m = n + k`, otherwise `none`. -/
@[simp] def psub (m : ℕ) : ℕ → option ℕ
| 0 := some m
| (n+1) := psub n >>= ppred
theorem pred_eq_ppred (n : ℕ) : pred n = (ppred n).get_or_else 0 :=
by cases n; refl
theorem sub_eq_psub (m : ℕ) : ∀ n, m - n = (psub m n).get_or_else 0
| 0 := rfl
| (n+1) := (pred_eq_ppred (m-n)).trans $
by rw [sub_eq_psub, psub]; cases psub m n; refl
@[simp] theorem ppred_eq_some {m : ℕ} : ∀ {n}, ppred n = some m ↔ succ m = n
| 0 := by split; intro h; contradiction
| (n+1) := by dsimp; split; intro h; injection h; subst n
@[simp] theorem ppred_eq_none : ∀ {n : ℕ}, ppred n = none ↔ n = 0
| 0 := by simp
| (n+1) := by dsimp; split; contradiction
theorem psub_eq_some {m : ℕ} : ∀ {n k}, psub m n = some k ↔ k + n = m
| 0 k := by simp [eq_comm]
| (n+1) k := by dsimp; apply option.bind_eq_some.trans; simp [psub_eq_some]
theorem psub_eq_none (m n : ℕ) : psub m n = none ↔ m < n :=
begin
cases s : psub m n; simp [eq_comm],
{ show m < n, refine lt_of_not_ge (λ h, _),
cases le.dest h with k e,
injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) },
{ show n ≤ m, rw ← psub_eq_some.1 s, apply le_add_left }
end
theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) :=
ppred_eq_some.2 $ succ_pred_eq_of_pos h
theorem psub_eq_sub {m n} (h : n ≤ m) : psub m n = some (m - n) :=
psub_eq_some.2 $ nat.sub_add_cancel h
theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k :=
by induction k; simp [*, add_succ, bind_assoc]
/- pow -/
attribute [simp] nat.pow_zero nat.pow_one
@[simp] lemma one_pow : ∀ n : ℕ, 1 ^ n = 1
| 0 := rfl
| (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow]
theorem pow_add (a m n : ℕ) : a^(m + n) = a^m * a^n :=
by induction n; simp [*, pow_succ, mul_assoc]
theorem pow_two (a : ℕ) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul
theorem pow_dvd_pow (a : ℕ) {m n : ℕ} (h : m ≤ n) : a^m ∣ a^n :=
by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right
theorem pow_dvd_pow_of_dvd {a b : ℕ} (h : a ∣ b) : ∀ n:ℕ, a^n ∣ b^n
| 0 := dvd_refl _
| (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h
theorem mul_pow (a b n : ℕ) : (a * b) ^ n = a ^ n * b ^ n :=
by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm]
protected theorem pow_mul (a b n : ℕ) : n ^ (a * b) = (n ^ a) ^ b :=
by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm]
theorem pow_pos {p : ℕ} (hp : p > 0) : ∀ n : ℕ, p ^ n > 0
| 0 := by simpa using zero_lt_one
| (k+1) := mul_pos (pow_pos _) hp
lemma pow_eq_mul_pow_sub (p : ℕ) {m n : ℕ} (h : m ≤ n) : p ^ m * p ^ (n - m) = p ^ n :=
by rw [←nat.pow_add, nat.add_sub_cancel' h]
lemma pow_lt_pow_succ {p : ℕ} (h : p > 1) (n : ℕ) : p^n < p^(n+1) :=
suffices p^n*1 < p^n*p, by simpa,
nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n)
lemma lt_pow_self {p : ℕ} (h : p > 1) : ∀ n : ℕ, n < p ^ n
| 0 := by simp [zero_lt_one]
| (n+1) := calc
n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _
... ≤ p ^ (n+1) : pow_lt_pow_succ h _
lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : p > 1) (hk : k > 1), ¬ p^k ∣ p
| (succ p) (succ k) hp hk h :=
have (succ p)^k * succ p ∣ 1 * succ p, by simpa,
have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this,
have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this,
have k < (succ p) ^ k, from lt_pow_self hp k,
have k < 1, by rwa [he] at this,
have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this,
have 1 > 1, by rwa [this] at hk,
absurd this dec_trivial
@[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) :=
by unfold bodd div2; cases bodd_div2 n; refl
@[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n
@[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n
@[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n
@[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n
/- iterate -/
section
variables {α : Sort*} (op : α → α)
@[simp] theorem iterate_zero (a : α) : op^[0] a = a := rfl
@[simp] theorem iterate_succ (n : ℕ) (a : α) : op^[succ n] a = (op^[n]) (op a) := rfl
theorem iterate_add : ∀ (m n : ℕ) (a : α), op^[m + n] a = (op^[m]) (op^[n] a)
| m 0 a := rfl
| m (succ n) a := iterate_add m n _
theorem iterate_succ' (n : ℕ) (a : α) : op^[succ n] a = op (op^[n] a) :=
by rw [← one_add, iterate_add]; refl
theorem iterate₀ {α : Type u} {op : α → α} {x : α} (H : op x = x) {n : ℕ} :
op^[n] x = x :=
by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]]
theorem iterate₁ {α : Type u} {β : Type v} {op : α → α} {op' : β → β} {op'' : α → β}
(H : ∀ x, op' (op'' x) = op'' (op x)) {n : ℕ} {x : α} :
op'^[n] (op'' x) = op'' (op^[n] x) :=
by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]]
theorem iterate₂ {α : Type u} {op : α → α} {op' : α → α → α} (H : ∀ x y, op (op' x y) = op' (op x) (op y)) {n : ℕ} {x y : α} :
op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) :=
by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]]
theorem iterate_cancel {α : Type u} {op op' : α → α} (H : ∀ x, op (op' x) = x) {n : ℕ} {x : α} : op^[n] (op'^[n] x) = x :=
by induction n; [refl, rwa [iterate_succ, iterate_succ', H]]
theorem iterate_inj {α : Type u} {op : α → α} (Hinj : function.injective op) (n : ℕ) (x y : α)
(H : (op^[n] x) = (op^[n] y)) : x = y :=
by induction n with n ih; simp only [iterate_zero, iterate_succ'] at H;
[exact H, exact ih (Hinj H)]
end
/- size and shift -/
theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 :=
by induction n; simp [shiftl', bit_ne_zero, *]
theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0
| 0 h := absurd rfl h
| (succ n) _ := nat.bit1_ne_zero _
@[simp] theorem size_zero : size 0 = 0 := rfl
@[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) :=
begin
rw size,
conv { to_lhs, rw [binary_rec], simp [h] },
rw div2_bit, refl
end
@[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) :=
@size_bit ff n (nat.bit0_ne_zero h)
@[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) :=
@size_bit tt n (nat.bit1_ne_zero n)
@[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0
@[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) :
size (shiftl' b m n) = size m + n :=
begin
induction n with n IH; simp [shiftl'] at h ⊢,
rw [size_bit h, nat.add_succ],
by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]],
rw s0 at h ⊢,
cases b, {exact absurd rfl h},
have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0,
rw [shiftl'_tt_eq_mul_pow] at this,
have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩),
subst m0,
simp at this,
have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ λ hn,
ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this),
subst n, refl
end
@[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) :
size (shiftl m n) = size m + n :=
size_shiftl' (shiftl'_ne_zero_left _ h _)
theorem lt_size_self (n : ℕ) : n < 2^size n :=
begin
rw [← one_shiftl],
have : ∀ {n}, n = 0 → n < shiftl 1 (size n) :=
λ n e, by subst e; exact dec_trivial,
apply binary_rec _ _ n, {apply this rfl},
intros b n IH,
by_cases bit b n = 0, {apply this h},
rw [size_bit h, shiftl_succ],
exact bit_lt_bit0 _ IH
end
theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n :=
⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h),
begin
rw [← one_shiftl], revert n,
apply binary_rec _ _ m,
{ intros n h, apply zero_le },
{ intros b m IH n h,
by_cases e : bit b m = 0, { rw e, apply zero_le },
rw [size_bit e],
cases n with n,
{ exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) },
{ apply succ_le_succ (IH _),
apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } }
end⟩
theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n :=
by rw [← not_lt, iff_not_comm, not_lt, size_le]
theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n :=
by rw lt_size; refl
theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 :=
by have := @size_pos n; simp [pos_iff_ne_zero'] at this;
exact not_iff_not.1 this
theorem size_pow {n : ℕ} : size (2^n) = n+1 :=
le_antisymm
(size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _))
(lt_size.2 $ le_refl _)
theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n :=
size_le.2 $ lt_of_le_of_lt h (lt_size_self _)
/- factorial -/
/-- `fact n` is the factorial of `n`. -/
@[simp] def fact : nat → nat
| 0 := 1
| (succ n) := succ n * fact n
@[simp] theorem fact_zero : fact 0 = 1 := rfl
@[simp] theorem fact_one : fact 1 = 1 := rfl
@[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl
theorem fact_pos : ∀ n, fact n > 0
| 0 := zero_lt_one
| (succ n) := mul_pos (succ_pos _) (fact_pos n)
theorem fact_ne_zero (n : ℕ) : fact n ≠ 0 := ne_of_gt (fact_pos _)
theorem fact_dvd_fact {m n} (h : m ≤ n) : fact m ∣ fact n :=
begin
induction n with n IH; simp,
{ have := eq_zero_of_le_zero h, subst m, simp },
{ cases eq_or_lt_of_le h with he hl,
{ subst m, simp },
{ apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } }
end
theorem dvd_fact : ∀ {m n}, m > 0 → m ≤ n → m ∣ fact n
| (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h)
theorem fact_le {m n} (h : m ≤ n) : fact m ≤ fact n :=
le_of_dvd (fact_pos _) (fact_dvd_fact h)
lemma fact_mul_pow_le_fact : ∀ {m n : ℕ}, m.fact * m.succ ^ n ≤ (m + n).fact
| m 0 := by simp
| m (n+1) :=
by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc];
exact mul_le_mul fact_mul_pow_le_fact
(nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _)
section find_greatest
/-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i`
exists -/
protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ
| 0 := 0
| (n + 1) := if P (n + 1) then n + 1 else find_greatest n
variables {P : ℕ → Prop} [decidable_pred P]
@[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl
@[simp] lemma find_greatest_eq : ∀{b}, P b → nat.find_greatest P b = b
| 0 h := rfl
| (n + 1) h := by simp [nat.find_greatest, h]
@[simp] lemma find_greatest_of_not {b} (h : ¬ P (b + 1)) :
nat.find_greatest P (b + 1) = nat.find_greatest P b :=
by simp [nat.find_greatest, h]
lemma find_greatest_spec_and_le :
∀{b m}, m ≤ b → P m → P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b
| 0 m hm hP :=
have m = 0, from le_antisymm hm (nat.zero_le _),
show P 0 ∧ m ≤ 0, from this ▸ ⟨hP, le_refl _⟩
| (b + 1) m hm hP :=
begin
by_cases h : P (b + 1),
{ simp [h, hm] },
{ have : m ≠ b + 1 := assume this, h $ this ▸ hP,
have : m ≤ b := (le_of_not_gt $ assume h : b + 1 ≤ m, this $ le_antisymm hm h),
have : P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b :=
find_greatest_spec_and_le this hP,
simp [h, this] }
end
lemma find_greatest_spec {b} : (∃m, m ≤ b ∧ P m) → P (nat.find_greatest P b)
| ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1
lemma find_greatest_le : ∀ {b}, nat.find_greatest P b ≤ b
| 0 := le_refl _
| (b + 1) :=
have nat.find_greatest P b ≤ b + 1, from le_trans find_greatest_le (nat.le_succ b),
by by_cases P (b + 1); simp [h, this]
lemma le_find_greatest {b m} (hmb : m ≤ b) (hm : P m) : m ≤ nat.find_greatest P b :=
(find_greatest_spec_and_le hmb hm).2
lemma find_greatest_is_greatest {P : ℕ → Prop} [decidable_pred P] {b} :
(∃ m, m ≤ b ∧ P m) → ∀ k, nat.find_greatest P b < k ∧ k ≤ b → ¬ P k
| ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk
end find_greatest
section div
lemma dvd_div_of_mul_dvd {a b c : ℕ} (h : a * b ∣ c) : b ∣ c / a :=
if ha : a = 0 then
by simp [ha]
else
have ha : a > 0, from nat.pos_of_ne_zero ha,
have h1 : ∃ d, c = a * b * d, from h,
let ⟨d, hd⟩ := h1 in
have hac : a ∣ c, from dvd_of_mul_right_dvd h,
have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd),
show ∃ d, c / a = b * d, from ⟨d, h2⟩
lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b :=
have h1 : ∃ d, b / c = a * d, from h,
have h2 : ∃ e, b = c * e, from hab,
let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in
have h3 : b = a * d * c, from
nat.eq_mul_of_div_eq_left hab hd,
show ∃ d, b = c * a * d, from ⟨d, by cc⟩
lemma div_mul_div {a b c d : ℕ} (hab : b ∣ a) (hcd : d ∣ c) :
(a / b) * (c / d) = (a * c) / (b * d) :=
have exi1 : ∃ x, a = b * x, from hab,
have exi2 : ∃ y, c = d * y, from hcd,
if hb : b = 0 then by simp [hb]
else have b > 0, from nat.pos_of_ne_zero hb,
if hd : d = 0 then by simp [hd]
else have d > 0, from nat.pos_of_ne_zero hd,
begin
cases exi1 with x hx, cases exi2 with y hy,
rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left],
symmetry,
apply nat.div_eq_of_eq_mul_left,
apply mul_pos,
repeat {assumption},
cc
end
lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k :=
have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn,
dvd_trans this hdiv
lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m :=
by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk
end div
lemma exists_eq_add_of_le : ∀ {m n : ℕ}, m ≤ n → ∃ k : ℕ, n = m + k
| 0 0 h := ⟨0, by simp⟩
| 0 (n+1) h := ⟨n+1, by simp⟩
| (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩
lemma exists_eq_add_of_lt : ∀ {m n : ℕ}, m < n → ∃ k : ℕ, n = m + k + 1
| 0 0 h := false.elim $ lt_irrefl _ h
| 0 (n+1) h := ⟨n, by simp⟩
| (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩
lemma with_bot.add_eq_zero_iff : ∀ {n m : with_bot ℕ}, n + m = 0 ↔ n = 0 ∧ m = 0
| none m := iff_of_false dec_trivial (λ h, absurd h.1 dec_trivial)
| n none := iff_of_false (by cases n; exact dec_trivial)
(λ h, absurd h.2 dec_trivial)
| (some n) (some m) := show (n + m : with_bot ℕ) = (0 : ℕ) ↔ (n : with_bot ℕ) = (0 : ℕ) ∧
(m : with_bot ℕ) = (0 : ℕ),
by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe,
with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)]
lemma with_bot.add_eq_one_iff : ∀ {n m : with_bot ℕ}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0)
| none none := dec_trivial
| none (some m) := dec_trivial
| (some n) none := iff_of_false dec_trivial (λ h, h.elim (λ h, absurd h.2 dec_trivial)
(λ h, absurd h.2 dec_trivial))
| (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe,
with_bot.coe_eq_coe]; simp
| (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe,
with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero]
-- induction
@[elab_as_eliminator] lemma le_induction {P : nat → Prop} {m} (h0 : P m) (h1 : ∀ n ≥ m, P n → P (n + 1)) :
∀ n ≥ m, P n :=
by apply nat.less_than_or_equal.rec h0; exact h1
end nat
|
e0a8dc58b559466d2acb235094cb6de2a3af0e28 | 947b78d97130d56365ae2ec264df196ce769371a | /stage0/src/Lean/Data/Lsp/InitShutdown.lean | 0b4e1180479d8324feecf5c8f1befff078f01e9e | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,048 | 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.Lsp.Capabilities
import Lean.Data.Lsp.Workspace
import Lean.Data.Json
/-! Functionality to do with initializing and shutting down
the server ("General Messages" section of LSP spec). -/
namespace Lean
namespace Lsp
open Json
structure ClientInfo :=
(name : String)
(version? : Option String := none)
instance ClientInfo.hasFromJson : HasFromJson ClientInfo :=
⟨fun j => do
name ← j.getObjValAs? String "name";
let version? := j.getObjValAs? String "version";
pure ⟨name, version?⟩⟩
inductive Trace
| off
| messages
| verbose
instance Trace.hasFromJson : HasFromJson Trace :=
⟨fun j => match j.getStr? with
| some "off" => Trace.off
| some "messages" => Trace.messages
| some "verbose" => Trace.verbose
| _ => none⟩
structure InitializeParams :=
(processId? : Option Int := none)
(clientInfo? : Option ClientInfo := none)
/- We don't support the deprecated rootPath
(rootPath? : Option String) -/
(rootUri? : Option String := none)
(initializationOptions? : Option Json := none)
(capabilities : ClientCapabilities)
/- If omitted, we default to off. -/
(trace : Trace := Trace.off)
(workspaceFolders? : Option (Array WorkspaceFolder) := none)
instance InitializeParams.hasFromJson : HasFromJson InitializeParams :=
⟨fun j => do
/- Many of these params can be null instead of not present.
For ease of implementation, we're liberal:
missing params, wrong json types and null all map to none,
even if LSP sometimes only allows some subset of these.
In cases where LSP makes a meaningful distinction
between different kinds of missing values, we'll
follow accordingly. -/
let processId? := j.getObjValAs? Int "processId";
let clientInfo? := j.getObjValAs? ClientInfo "clientInfo";
let rootUri? := j.getObjValAs? String "rootUri";
let initializationOptions? := j.getObjVal? "initializationOptions";
capabilities ← j.getObjValAs? ClientCapabilities "capabilities";
let trace := (j.getObjValAs? Trace "trace").getD Trace.off;
let workspaceFolders? := j.getObjValAs? (Array WorkspaceFolder) "workspaceFolders";
pure ⟨processId?, clientInfo?, rootUri?, initializationOptions?, capabilities, trace, workspaceFolders?⟩⟩
inductive InitializedParams | mk
instance InitializedParams.hasFromJson : HasFromJson InitializedParams :=
⟨fun j => InitializedParams.mk⟩
structure ServerInfo :=
(name : String)
(version? : Option String := none)
instance ServerInfo.hasToJson : HasToJson ServerInfo :=
⟨fun o => mkObj $
⟨"name", o.name⟩ ::
opt "version" o.version?⟩
structure InitializeResult :=
(capabilities : ServerCapabilities)
(serverInfo? : Option ServerInfo := none)
instance InitializeResult.hasToJson : HasToJson InitializeResult :=
⟨fun o => mkObj $
⟨"capabilities", toJson o.capabilities⟩ ::
opt "serverInfo" o.serverInfo?⟩
end Lsp
end Lean
|
ecdf97208a988d3e4095e7b2f7742aa73e4a8cd8 | 4a894698f2ae3f991490c25af3c13ea4435dac48 | /src/instructor/lectures/lecture_3.lean | 48b43efe580f8e63f06f0c31d3c41882981b5f78 | [] | no_license | gnujey/cs2120f21 | 8a33f636346d59ade049dcc1726634f434ac1955 | 138d43446c443c1d15cd2f17fb607c4f0dff702f | refs/heads/main | 1,690,598,880,775 | 1,631,132,566,000 | 1,631,132,566,000 | 405,182,739 | 1 | 0 | null | 1,631,299,824,000 | 1,631,299,823,000 | null | UTF-8 | Lean | false | false | 3,572 | lean | import .lecture_2
/-
From the axioms of reflexivity and substitutabilty
we now prove two *theorems*. A theorem is a proven
proposition. You can even think of the theorem as a
proof of a given proposition. A proof establishes a
conjecture as a theorem. And once it's a theorem,
you can use it like any other inference rule. In
this way, you can build up vast, layered libraries
of theorems. That's what we call mathematics. It's
what mathematicians do.
In this file, we provide both formal and informal
proofs of two theorems:
[1] Equality is symmetric.
[2] Equality is transitive.
-/
/-
To master these concepts, you must appreciate
the distinction between propositions and proofs.
Propositions are "statements of fact" that might
or might not be true. Proofs are objects that
show such propositions to be true. Propositions
that are not true, have no proofs (otherwise of
course they'd be true, as we just said).
So, first, you have to be able to *state* the
conjecture in the formal language of predicate
logic. We'll use an implementation of a specific
version of predicate logic provided by the Lean
Prover.
Second, you must understand how to construct
proofs of such propositions, if proofs actually
exist, which they might or might not for given
propositions. Not all propositions, statements
asserting "facts", are true. The sky is magenta.
-/
/-
Theorems
-/
-- We define eq_symm to be the proposition at issue
-- Note: Prop is the type of all propositions in Lean
-- And each proposition is itself a type: of it proofs
def eq_symm : Prop :=
∀ (T : Type)
(x y : T),
x = y →
y = x
-- We define eq_trans formally in the same basic way
def eq_trans : Prop :=
∀ (T : Type)
(x y z : T),
x = y →
y = z →
x = z
/-
Proofs
-/
/-
In the logic of Lean, a proposition is a type,
and the values of that type are its proofs. Just
as we can give an example value of an ordinary
data type, such as ℕ, we can also give example
values of propositions acting as types, i.e.,
proofs.
-/
-- give a value of the nat type
example : ℕ := 5
-- give a proof of the proposition 1 = 1
example : 1 = 1 := eq.refl 1
/-
We will now present formal proofs of our two
theorems in this style.
-/
/-
Proof: equality is symmetric.
-/
example : eq_symm :=
begin
unfold eq_symm, -- replace name with definition
assume T x y e, -- assume arbitrary values
rw e, -- rw uses eq.subst to replace x with y
-- and then applies eq.refl automatically
-- QED.
end
/-
Proof: equality is transitive.
-/
example : eq_trans :=
begin
unfold eq_trans,
assume T,
assume x y z,
assume e1,
assume e2,
rw e1,
exact e2,
end
/-
A fundamental idea is that any proof is as good as any
other for establishing the truth of a proposition. Here
is an equally good alternative proof (or to be correct,
proof-generating scripts in the "proof tactic" language"
of the Lean Prover.
-/
example : eq_symm :=
begin
unfold eq_symm, -- replace name with definition
assume T x y e, -- assume arbitrary values
apply eq.subst e, -- apply axiom 2, substitutability
exact eq.refl x, -- apply axiom 1, reflexivity
-- QED.
end
/-
There. That's a nice proof (script), as it closely
models a typical English language proof. To wit:
Theorem: Equality is symmetric.
Proof: Assume we're given arbitrary values of T,
x, y, and a proof of x = y. What remains to be
proved is y = x. Apply substitutability to write
x as y or y as x. The result is trivially true
by the reflexive property of equality.
-/ |
e2e3ef4b965f795d41526b47855e5603d2e9a369 | e030b0259b777fedcdf73dd966f3f1556d392178 | /library/init/meta/smt/ematch.lean | 3a4ac11384eb5e2208fc7a04ed71af62e493c5aa | [
"Apache-2.0"
] | permissive | fgdorais/lean | 17b46a095b70b21fa0790ce74876658dc5faca06 | c3b7c54d7cca7aaa25328f0a5660b6b75fe26055 | refs/heads/master | 1,611,523,590,686 | 1,484,412,902,000 | 1,484,412,902,000 | 38,489,734 | 0 | 0 | null | 1,435,923,380,000 | 1,435,923,379,000 | null | UTF-8 | Lean | false | false | 7,076 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.smt.congruence_closure
import init.meta.attribute init.meta.simp_tactic
open tactic
/- Heuristic instantiation lemma -/
meta constant hinst_lemma : Type
meta constant hinst_lemmas : Type
/- (mk_core m e as_simp), m is used to decide which definitions will be unfolded in patterns.
If as_simp is tt, then this tactic will try to use the left-hand-side of the conclusion
as a pattern. -/
meta constant hinst_lemma.mk_core : transparency → expr → bool → tactic hinst_lemma
meta constant hinst_lemma.mk_from_decl_core : transparency → name → bool → tactic hinst_lemma
meta constant hinst_lemma.pp : hinst_lemma → tactic format
meta constant hinst_lemma.id : hinst_lemma → name
meta instance : has_to_tactic_format hinst_lemma :=
⟨hinst_lemma.pp⟩
meta def hinst_lemma.mk (h : expr) : tactic hinst_lemma :=
hinst_lemma.mk_core reducible h ff
meta def hinst_lemma.mk_from_decl (h : name) : tactic hinst_lemma :=
hinst_lemma.mk_from_decl_core reducible h ff
meta constant hinst_lemmas.mk : hinst_lemmas
meta constant hinst_lemmas.add : hinst_lemmas → hinst_lemma → hinst_lemmas
meta constant hinst_lemmas.fold {α : Type} : hinst_lemmas → α → (hinst_lemma → α → α) → α
meta constant hinst_lemmas.merge : hinst_lemmas → hinst_lemmas → hinst_lemmas
meta def mk_hinst_singleton : hinst_lemma → hinst_lemmas :=
hinst_lemmas.add hinst_lemmas.mk
meta def hinst_lemmas.pp (s : hinst_lemmas) : tactic format :=
let tac := s^.fold (return format.nil)
(λ h tac, do
hpp ← h^.pp,
r ← tac,
if r^.is_nil then return hpp
else return (r ++ to_fmt "," ++ format.line ++ hpp))
in do
r ← tac,
return $ format.cbrace (format.group r)
meta instance : has_to_tactic_format hinst_lemmas :=
⟨hinst_lemmas.pp⟩
open tactic
meta def to_hinst_lemmas_core (m : transparency) : bool → list name → hinst_lemmas → tactic hinst_lemmas
| as_simp [] hs := return hs
| as_simp (n::ns) hs := do
/- First check if n is the name of a function with equational lemmas associated with it -/
eqns ← tactic.get_eqn_lemmas_for tt n,
match eqns with
| [] := do
/- n is not the name of a function definition or it does not have equational lemmas, then check if it is a lemma -/
h ← hinst_lemma.mk_from_decl_core m n as_simp,
new_hs ← return $ hs^.add h,
to_hinst_lemmas_core as_simp ns new_hs
| _ := do
/- And equational lemmas to resulting hinst_lemmas -/
new_hs ← to_hinst_lemmas_core tt eqns hs,
to_hinst_lemmas_core as_simp ns new_hs
end
meta def mk_hinst_lemma_attr_core (attr_name : name) (as_simp : bool) : command :=
do t ← to_expr `(caching_user_attribute hinst_lemmas),
a ← attr_name^.to_expr,
b ← if as_simp then to_expr `(tt) else to_expr `(ff),
v ← to_expr `(({ name := %%a,
descr := "hinst_lemma attribute",
mk_cache := λ ns, to_hinst_lemmas_core reducible %%b ns hinst_lemmas.mk,
dependencies := [`reducibility] } : caching_user_attribute hinst_lemmas)),
add_decl (declaration.defn attr_name [] t v reducibility_hints.abbrev ff),
attribute.register attr_name
meta def mk_hinst_lemma_attrs_core (as_simp : bool) : list name → command
| [] := skip
| (n::ns) :=
(mk_hinst_lemma_attr_core n as_simp >> mk_hinst_lemma_attrs_core ns)
<|>
(do type ← infer_type (expr.const n []),
expected ← to_expr `(caching_user_attribute hinst_lemmas),
(is_def_eq type expected
<|> fail ("failed to create hinst_lemma attribute '" ++ n^.to_string ++ "', declaration already exists and has different type.")),
mk_hinst_lemma_attrs_core ns)
meta def merge_hinst_lemma_attrs (m : transparency) (as_simp : bool) : list name → hinst_lemmas → tactic hinst_lemmas
| [] hs := return hs
| (attr::attrs) hs := do
ns ← attribute.get_instances attr,
new_hs ← to_hinst_lemmas_core m as_simp ns hs,
merge_hinst_lemma_attrs attrs new_hs
/--
Create a new "cached" attribute (attr_name : caching_user_attribute hinst_lemmas).
It also creates "cached" attributes for each attr_names and simp_attr_names if they have not been defined
yet. Moreover, the hinst_lemmas for attr_name will be the union of the lemmas tagged with
attr_name, attrs_name, and simp_attr_names.
For the ones in simp_attr_names, we use the left-hand-side of the conclusion as the pattern.
-/
meta def mk_hinst_lemma_attr_set (attr_name : name) (attr_names : list name) (simp_attr_names : list name) : command :=
do mk_hinst_lemma_attrs_core ff attr_names,
mk_hinst_lemma_attrs_core tt simp_attr_names,
t ← to_expr `(caching_user_attribute hinst_lemmas),
a ← attr_name^.to_expr,
l1 : expr ← list_name.to_expr attr_names,
l2 : expr ← list_name.to_expr simp_attr_names,
v ← to_expr `(({ name := %%a,
descr := "hinst_lemma attribute set",
mk_cache := λ ns,
let aux1 : list name := %%l1,
aux2 : list name := %%l2 in
do {
hs₁ ← to_hinst_lemmas_core reducible ff ns hinst_lemmas.mk,
hs₂ ← merge_hinst_lemma_attrs reducible ff aux1 hs₁,
merge_hinst_lemma_attrs reducible tt aux2 hs₂},
dependencies := [`reducibility] ++ %%l1 ++ %%l2 } : caching_user_attribute hinst_lemmas)),
add_decl (declaration.defn attr_name [] t v reducibility_hints.abbrev ff),
attribute.register attr_name
meta def get_hinst_lemmas_for_attr (attr_name : name) : tactic hinst_lemmas :=
do
cnst ← return (expr.const attr_name []),
attr ← eval_expr (caching_user_attribute hinst_lemmas) cnst,
caching_user_attribute.get_cache attr
structure ematch_config :=
(max_instances : nat)
def default_ematch_config : ematch_config :=
{max_instances := 10000}
/- Ematching -/
meta constant ematch_state : Type
meta constant ematch_state.mk : ematch_config → ematch_state
meta constant ematch_state.internalize : ematch_state → expr → tactic ematch_state
namespace tactic
meta constant ematch_core : transparency → cc_state → ematch_state → hinst_lemma → expr → tactic (list (expr × expr) × cc_state × ematch_state)
meta constant ematch_all_core : transparency → cc_state → ematch_state → hinst_lemma → bool → tactic (list (expr × expr) × cc_state × ematch_state)
meta def ematch : cc_state → ematch_state → hinst_lemma → expr → tactic (list (expr × expr) × cc_state × ematch_state) :=
ematch_core reducible
meta def ematch_all : cc_state → ematch_state → hinst_lemma → bool → tactic (list (expr × expr) × cc_state × ematch_state) :=
ematch_all_core reducible
end tactic
|
b6b2fbc8e0a3bf77d3f536c31c2d2d268f991c20 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Elab/RecAppSyntax.lean | ad742168b8081f048cfe3fc6334a01dad08cbb76 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 831 | 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.Expr
namespace Lean
private def recAppKey := `_recApp
/--
We store the syntax at recursive applications to be able to generate better error messages
when performing well-founded and structural recursion.
-/
def mkRecAppWithSyntax (e : Expr) (stx : Syntax) : Expr :=
mkMData (KVMap.empty.insert recAppKey (DataValue.ofSyntax stx)) e
/--
Retrieve (if available) the syntax object attached to a recursive application.
-/
def getRecAppSyntax? (e : Expr) : Option Syntax :=
match e with
| Expr.mdata d b _ =>
match d.find recAppKey with
| some (DataValue.ofSyntax stx) => some stx
| _ => none
| _ => none
end Lean
|
71ae4ec16dd4108b302ec1b05869e139af4b75d2 | 9c1ad797ec8a5eddb37d34806c543602d9a6bf70 | /discrete_category.lean | 8ef88c912a8de31204b1312c8fc5cc08c56653ac | [] | no_license | timjb/lean-category-theory | 816eefc3a0582c22c05f4ee1c57ed04e57c0982f | 12916cce261d08bb8740bc85e0175b75fb2a60f4 | refs/heads/master | 1,611,078,926,765 | 1,492,080,000,000 | 1,492,080,000,000 | 88,348,246 | 0 | 0 | null | 1,492,262,499,000 | 1,492,262,498,000 | null | UTF-8 | Lean | false | false | 819 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Stephen Morgan, Scott Morrison
import .category
namespace tqft.categories
universe variables u v
open plift -- we first plift propositional equality to Type 0,
open ulift -- then ulift up to Type v
definition DiscreteCategory ( α : Type u ) : Category.{u v} :=
{
Obj := α,
Hom := λ X Y, ulift (plift (X = Y)),
identity := λ X, up (up rfl),
compose := λ X Y Z f g, up (up (eq.trans (down (down f)) (down (down g)))),
left_identity := begin intros, induction f with f', induction f', trivial end,
right_identity := begin intros, induction f with f', induction f', trivial end,
associativity := begin intros, trivial end
}
end tqft.categories
|
fdb091bee2fd12bbf4b4527da63f8f0229e50770 | df7bb3acd9623e489e95e85d0bc55590ab0bc393 | /lean/love04_functional_programming_homework_sheet.lean | 935e5c0b6eba637e6fcfe185223a60875cf06e48 | [] | no_license | MaschavanderMarel/logical_verification_2020 | a41c210b9237c56cb35f6cd399e3ac2fe42e775d | 7d562ef174cc6578ca6013f74db336480470b708 | refs/heads/master | 1,692,144,223,196 | 1,634,661,675,000 | 1,634,661,675,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,478 | lean | import .love04_functional_programming_demo
/- # LoVe Homework 4: Functional Programming
Homework must be done individually. -/
set_option pp.beta true
set_option pp.generalized_field_notation false
namespace LoVe
/- ## Question 1 (2 points): Maps
1.1 (1 point). Complete the following definition. The `map_btree` function
should apply its argument `f` to all values of type `α` stored in the tree and
otherwise preserve the tree's structure. -/
def map_btree {α β : Type} (f : α → β) : btree α → btree β :=
sorry
/- 1.2 (1 point). Prove the following lemma about your `map_btree` function. -/
lemma map_btree_iden {α : Type} :
∀t : btree α, map_btree (λa, a) t = t :=
sorry
/- ## Question 2 (4 points): Tail-Recursive Factorials -/
/- Recall the definition of the factorial functions -/
#check fact
/- 2.1 (2 points). Experienced functional programmers tend to avoid definitions
such as the above, because they lead to a deep call stack. Tail recursion can be
used to avoid this. Results are accumulated in an argument and returned. This
can be optimized by compilers. For factorials, this gives the following
definition: -/
def accufact : ℕ → ℕ → ℕ
| a 0 := a
| a (n + 1) := accufact ((n + 1) * a) n
/- Prove that, given 1 as the accumulator `a`, `accufact` computes `fact`.
Hint: You will need to prove a generalized version of the statement as a
separate lemma or `have`, because the desired propery fixes `a` to 1, which
yields a too weak induction hypothesis. -/
lemma accufact_1_eq_fact (n : ℕ) :
accufact 1 n = fact n :=
sorry
/- 2.2 (2 points). Prove the same property as above again, this time as a
"paper" proof. Follow the guidelines given in question 1.4 of the exercise. -/
-- enter your paper proof here
/- ## Question 3 (3 points): Gauss's Summation Formula -/
-- `sum_upto f n = f 0 + f 1 + ⋯ + f n`
def sum_upto (f : ℕ → ℕ) : ℕ → ℕ
| 0 := f 0
| (m + 1) := sum_upto m + f (m + 1)
/- 3.1 (2 point). Prove the following lemma, discovered by Carl Friedrich Gauss
as a pupil.
Hint: The `mul_add` and `add_mul` lemmas might be useful to reason about
multiplication. -/
#check mul_add
#check add_mul
lemma sum_upto_eq :
∀m : ℕ, 2 * sum_upto (λi, i) m = m * (m + 1) :=
sorry
/- 3.2 (1 point). Prove the following property of `sum_upto`. -/
lemma sum_upto_mul (f g : ℕ → ℕ) :
∀n : ℕ, sum_upto (λi, f i + g i) n = sum_upto f n + sum_upto g n :=
sorry
end LoVe
|
fc784c838bd9f461602cfc3a580dc6ffd9470995 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/category_theory/pempty.lean | 2b576b0fbd505050d3f41795576401a56f11d275 | [
"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 | 1,296 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.discrete_category
import category_theory.equivalence
/-!
# The empty category
Defines a category structure on `pempty`, and the unique functor `pempty ⥤ C` for any category `C`.
-/
universes v u w -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
/-- The empty category -/
instance pempty_category : small_category.{w} pempty.{w+1} :=
{ hom := λ X Y, pempty,
id := by obviously,
comp := by obviously }
namespace functor
variables (C : Type u) [𝒞 : category.{v} C]
include 𝒞
/-- The unique functor from the empty category to any target category. -/
def empty : pempty.{v+1} ⥤ C := by tidy
/-- The natural isomorphism between any two functors out of the empty category. -/
@[simps]
def empty_ext (F G : pempty.{v+1} ⥤ C) : F ≅ G :=
{ hom := { app := λ j, by cases j },
inv := { app := λ j, by cases j } }
end functor
/-- The category `pempty` is equivalent to the category `discrete pempty`. -/
instance pempty_equiv_discrete_pempty : is_equivalence (functor.empty.{v} (discrete pempty.{v+1})) :=
by obviously
end category_theory
|
f677b4c20dc19ef0d207b01313441ca9c28132bf | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/choiceExpectedTypeBug.lean | f2fd7f915e103eae4454fcf3d82b32b31759a663 | [
"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 | 616 | lean | import Lean
structure A :=
(x : Nat := 10)
def f : A :=
{ }
theorem ex : f = { x := 10 } :=
rfl
#check f
syntax (name := emptyS) "⟨" "⟩" : term -- overload `⟨ ⟩` notation
open Lean
open Lean.Elab
open Lean.Elab.Term
@[term_elab emptyS] def elabEmptyS : TermElab :=
fun stx expectedType? => do
tryPostponeIfNoneOrMVar expectedType?
let stxNew ← `(Nat.zero)
withMacroExpansion stx stxNew $
elabTerm stxNew expectedType?
def foo (x : Unit) := x
def f1 : Unit :=
let x := ⟨ ⟩
foo x
def f2 : Unit :=
let x := ⟨ ⟩
x
def f3 : Nat :=
let x := ⟨ ⟩
x
theorem ex2 : f3 = 0 :=
rfl
|
ea2495bfdf08c88368dc11c157d3bfbf3e066d44 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /tests/lean/run/wfEqnsIssue.lean | 1f74010b0030b016bc081caa19c97c84109f9428 | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 3,762 | lean | def HList (αs : List (Type u)) : Type u := αs.foldr Prod.{u, u} PUnit
@[matchPattern] def HList.nil : HList [] := ⟨⟩
@[matchPattern] def HList.cons (a : α) (as : HList αs): HList (α :: αs) := (a, as)
def HList.set : {αs : _} → HList αs → (i : Fin αs.length) → αs.get i → HList αs
| _ :: _, cons a as, ⟨0, h⟩, b => cons b as
| _ :: _, cons a as, ⟨Nat.succ n, h⟩, b => cons a (set as ⟨n, Nat.le_of_succ_le_succ h⟩ b)
| [], nil, _, _ => nil
instance : EmptyCollection (HList ∅) where
emptyCollection := HList.nil
notation:30 Γ " ⊢ " α => HList Γ → α
-- simplify well-founded recursion proofs by ignoring context sizes
local instance : SizeOf (List α) := ⟨fun _ => 0⟩ in
-- m: base monad
-- ω: `return` type, `m ω` is the type of the entire `do` block
-- Γ: `do`-local immutable context
-- Δ: `do`-local mutable context
-- b: `break` allowed
-- c: `continue` allowed
-- α: local result type, `m α` is the type of the statement
inductive Stmt (m : Type u → Type _) (ω : Type u) : (Γ Δ : List (Type u)) → (b c : Bool) → (α : Type u) → Type _ where
| expr (e : Γ ⊢ Δ ⊢ m α) : Stmt m ω Γ Δ b c α
| bind (s₁ : Stmt m ω Γ Δ b c α) (s₂ : Stmt m ω (α :: Γ) Δ b c β) : Stmt m ω Γ Δ b c β
| letmut (e : Γ ⊢ Δ ⊢ α) (s : Stmt m ω Γ (α :: Δ) b c β) : Stmt m ω Γ Δ b c β
| ass (x : Fin Δ.length) (e : Γ ⊢ Δ ⊢ Δ.get x) : Stmt m ω Γ Δ b c PUnit
| ite (e : Γ ⊢ Δ ⊢ Bool) (s₁ s₂ : Stmt m ω Γ Δ b c α) : Stmt m ω Γ Δ b c α
| ret (e : Γ ⊢ Δ ⊢ ω) : Stmt m ω Γ Δ b c α
--| sfor [ForM m γ α] (e : Σ Γ → γ) (body : α → Stmt m ω Γ Δ true PUnit) : Stmt m ω Γ Δ b c PUnit
| sfor (e : Γ ⊢ Δ ⊢ List α) (body : Stmt m ω (α :: Γ) Δ true true PUnit) : Stmt m ω Γ Δ b c PUnit
| sbreak : Stmt m ω Γ Δ true c α
| scont : Stmt m ω Γ Δ b true α
-- normal and abnormal result values
inductive Res (ω α : Type _) : (b c : Bool) → Type _ where
| val (a : α) : Res ω α b c
| ret (o : ω) : Res ω α b c
| rbreak : Res ω α true c
| rcont : Res ω α b true
instance : Coe α (Res ω α b c) := ⟨Res.val⟩
instance : Coe (Id α) (Res ω α b c) := ⟨Res.val⟩
def Ctx.extendBot (x : α) : {Γ : _} → HList Γ → HList (Γ ++ [α])
| [], _ => HList.cons x HList.nil
| _ :: _, HList.cons a as => HList.cons a (extendBot x as)
def Ctx.extend (x : α) : HList Γ → HList (α :: Γ) :=
fun σ => HList.cons x σ
def Ctx.drop : HList (α :: Γ) → HList Γ
| HList.cons a as => as
-- custom wf tactic
theorem Nat.le_add_right_of_le (n m : Nat) : n ≤ m → n ≤ m + k :=
fun h => add_le_add h (Nat.zero_le _)
macro_rules
| `(tactic| decreasing_tactic) =>
`(tactic|
(simp_wf
repeat (first | apply PSigma.Lex.right | apply PSigma.Lex.left)
simp [Nat.add_comm (n := 1), Nat.succ_add, Nat.mul_succ]
try apply Nat.lt_succ_of_le
repeat apply Nat.le_step
first
| repeat first | apply Nat.le_add_left | apply Nat.le_add_right_of_le
| assumption
all_goals apply Nat.le_refl
))
@[simp]
def Stmt.mapCtx (f : HList Γ' → HList Γ) : Stmt m ω Γ Δ b c β → Stmt m ω Γ' Δ b c β
| expr e => expr (e ∘ f)
| bind s₁ s₂ => bind (s₁.mapCtx f) (s₂.mapCtx (fun | HList.cons a as => HList.cons a (f as)))
| letmut e s => letmut (e ∘ f) (s.mapCtx f)
| ass x e => ass x (e ∘ f)
| ite e s₁ s₂ => ite (e ∘ f) (s₁.mapCtx f) (s₂.mapCtx f)
| ret e => ret (e ∘ f)
| sfor e body => sfor (e ∘ f) (body.mapCtx (fun | HList.cons a as => HList.cons a (f as)))
| sbreak => sbreak
| scont => scont
termination_by mapCtx _ s => sizeOf s
|
73a2fdfae37a671163335c7a6be41d427cd23fce | 453dcd7c0d1ef170b0843a81d7d8caedc9741dce | /algebra/ordered_ring.lean | 7ee490a03ae70780a65f2f9583344f41fce67702 | [
"Apache-2.0"
] | permissive | amswerdlow/mathlib | 9af77a1f08486d8fa059448ae2d97795bd12ec0c | 27f96e30b9c9bf518341705c99d641c38638dfd0 | refs/heads/master | 1,585,200,953,598 | 1,534,275,532,000 | 1,534,275,532,000 | 144,564,700 | 0 | 0 | null | 1,534,156,197,000 | 1,534,156,197,000 | null | UTF-8 | Lean | false | false | 8,543 | 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 order.basic algebra.order algebra.ordered_group algebra.ring
universe u
variable {α : Type u}
-- TODO: this is necessary additionally to mul_nonneg otherwise the simplifier can not match
lemma zero_le_mul [ordered_semiring α] {a b : α} : 0 ≤ a → 0 ≤ b → 0 ≤ a * b :=
mul_nonneg
section linear_ordered_semiring
variable [linear_ordered_semiring α]
@[simp] lemma mul_le_mul_left {a b c : α} (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b :=
⟨λ h', le_of_mul_le_mul_left h' h, λ h', mul_le_mul_of_nonneg_left h' (le_of_lt h)⟩
@[simp] lemma mul_le_mul_right {a b c : α} (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b :=
⟨λ h', le_of_mul_le_mul_right h' h, λ h', mul_le_mul_of_nonneg_right h' (le_of_lt h)⟩
@[simp] lemma mul_lt_mul_left {a b c : α} (h : 0 < c) : c * a < c * b ↔ a < b :=
⟨λ h', lt_of_mul_lt_mul_left h' (le_of_lt h), λ h', mul_lt_mul_of_pos_left h' h⟩
@[simp] lemma mul_lt_mul_right {a b c : α} (h : 0 < c) : a * c < b * c ↔ a < b :=
⟨λ h', lt_of_mul_lt_mul_right h' (le_of_lt h), λ h', mul_lt_mul_of_pos_right h' h⟩
lemma mul_lt_mul'' {a b c d : α} (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) :
a * b < c * d :=
(lt_or_eq_of_le h4).elim
(λ b0, mul_lt_mul h1 (le_of_lt h2) b0 (le_trans h3 (le_of_lt h1)))
(λ b0, by rw [← b0, mul_zero]; exact
mul_pos (lt_of_le_of_lt h3 h1) (lt_of_le_of_lt h4 h2))
lemma le_mul_iff_one_le_left {a b : α} (hb : b > 0) : b ≤ a * b ↔ 1 ≤ a :=
suffices 1 * b ≤ a * b ↔ 1 ≤ a, by rwa one_mul at this,
mul_le_mul_right hb
lemma lt_mul_iff_one_lt_left {a b : α} (hb : b > 0) : b < a * b ↔ 1 < a :=
suffices 1 * b < a * b ↔ 1 < a, by rwa one_mul at this,
mul_lt_mul_right hb
lemma le_mul_iff_one_le_right {a b : α} (hb : b > 0) : b ≤ b * a ↔ 1 ≤ a :=
suffices b * 1 ≤ b * a ↔ 1 ≤ a, by rwa mul_one at this,
mul_le_mul_left hb
lemma lt_mul_iff_one_lt_right {a b : α} (hb : b > 0) : b < b * a ↔ 1 < a :=
suffices b * 1 < b * a ↔ 1 < a, by rwa mul_one at this,
mul_lt_mul_left hb
lemma lt_mul_of_gt_one_right' {a b : α} (hb : b > 0) : a > 1 → b < b * a :=
(lt_mul_iff_one_lt_right hb).2
lemma le_mul_of_ge_one_right' {a b : α} (hb : b ≥ 0) (h : a ≥ 1) : b ≤ b * a :=
suffices b * 1 ≤ b * a, by rwa mul_one at this,
mul_le_mul_of_nonneg_left h hb
lemma le_mul_of_ge_one_left' {a b : α} (hb : b ≥ 0) (h : a ≥ 1) : b ≤ a * b :=
suffices 1 * b ≤ a * b, by rwa one_mul at this,
mul_le_mul_of_nonneg_right h hb
theorem mul_nonneg_iff_right_nonneg_of_pos {a b : α} (h : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b :=
⟨assume : 0 ≤ b * a, nonneg_of_mul_nonneg_right this h, assume : 0 ≤ b, mul_nonneg this $ le_of_lt h⟩
lemma bit1_pos {a : α} (h : 0 ≤ a) : 0 < bit1 a :=
lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one
lemma bit1_pos' {a : α} (h : 0 < a) : 0 < bit1 a :=
bit1_pos (le_of_lt h)
lemma lt_add_one (a : α) : a < a + 1 :=
lt_add_of_le_of_pos (le_refl _) zero_lt_one
lemma one_lt_two : 1 < (2 : α) := lt_add_one _
end linear_ordered_semiring
instance linear_ordered_semiring.to_no_top_order {α : Type*} [linear_ordered_semiring α] :
no_top_order α :=
⟨assume a, ⟨a + 1, lt_add_of_pos_right _ zero_lt_one⟩⟩
instance linear_ordered_semiring.to_no_bot_order {α : Type*} [linear_ordered_ring α] :
no_bot_order α :=
⟨assume a, ⟨a - 1, sub_lt_iff_lt_add.mpr $ lt_add_of_pos_right _ zero_lt_one⟩⟩
instance to_domain [s : linear_ordered_ring α] : domain α :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_ring.eq_zero_or_eq_zero_of_mul_eq_zero α s,
..s }
section linear_ordered_ring
variable [linear_ordered_ring α]
@[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a :=
⟨le_imp_le_iff_lt_imp_lt.2 $ λ h', mul_lt_mul_of_neg_left h' h,
λ h', mul_le_mul_of_nonpos_left h' (le_of_lt h)⟩
@[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a :=
⟨le_imp_le_iff_lt_imp_lt.2 $ λ h', mul_lt_mul_of_neg_right h' h,
λ h', mul_le_mul_of_nonpos_right h' (le_of_lt h)⟩
@[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a :=
le_iff_le_iff_lt_iff_lt.1 (mul_le_mul_left_of_neg h)
@[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a :=
le_iff_le_iff_lt_iff_lt.1 (mul_le_mul_right_of_neg h)
lemma sub_one_lt (a : α) : a - 1 < a :=
sub_lt_iff_lt_add.2 (lt_add_one a)
end linear_ordered_ring
set_option old_structure_cmd true
/-- Extend `nonneg_comm_group` to support ordered rings
specified by their nonnegative elements -/
class nonneg_ring (α : Type*)
extends ring α, zero_ne_one_class α, nonneg_comm_group α :=
(mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b))
(mul_pos : ∀ {a b}, pos a → pos b → pos (a * b))
/-- Extend `nonneg_comm_group` to support linearly ordered rings
specified by their nonnegative elements -/
class linear_nonneg_ring (α : Type*) extends domain α, nonneg_comm_group α :=
(mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b))
(nonneg_total : ∀ a, nonneg a ∨ nonneg (-a))
namespace nonneg_ring
open nonneg_comm_group
variable [s : nonneg_ring α]
instance to_ordered_ring : ordered_ring α :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := @lt_iff_le_not_le _ _,
le_refl := @le_refl _ _,
le_trans := @le_trans _ _,
le_antisymm := @le_antisymm _ _,
add_lt_add_left := @add_lt_add_left _ _,
add_le_add_left := @add_le_add_left _ _,
mul_nonneg := λ a b, by simp [nonneg_def.symm]; exact mul_nonneg,
mul_pos := λ a b, by simp [pos_def.symm]; exact mul_pos,
..s }
def nonneg_ring.to_linear_nonneg_ring
(nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a))
: linear_nonneg_ring α :=
{ nonneg_total := nonneg_total,
eq_zero_or_eq_zero_of_mul_eq_zero :=
suffices ∀ {a} b : α, nonneg a → a * b = 0 → a = 0 ∨ b = 0,
from λ a b, (nonneg_total a).elim (this b)
(λ na, by simpa using this b na),
suffices ∀ {a b : α}, nonneg a → nonneg b → a * b = 0 → a = 0 ∨ b = 0,
from λ a b na, (nonneg_total b).elim (this na)
(λ nb, by simpa using this na nb),
λ a b na nb z, classical.by_cases
(λ nna : nonneg (-a), or.inl (nonneg_antisymm na nna))
(λ pa, classical.by_cases
(λ nnb : nonneg (-b), or.inr (nonneg_antisymm nb nnb))
(λ pb, absurd z $ ne_of_gt $ pos_def.1 $ mul_pos
((pos_iff _ _).2 ⟨na, pa⟩)
((pos_iff _ _).2 ⟨nb, pb⟩))),
..s }
end nonneg_ring
namespace linear_nonneg_ring
open nonneg_comm_group
variable [s : linear_nonneg_ring α]
instance to_nonneg_ring : nonneg_ring α :=
{ mul_pos := λ a b pa pb,
let ⟨a1, a2⟩ := (pos_iff α a).1 pa,
⟨b1, b2⟩ := (pos_iff α b).1 pb in
have ab : nonneg (a * b), from mul_nonneg a1 b1,
(pos_iff α _).2 ⟨ab, λ hn,
have a * b = 0, from nonneg_antisymm ab hn,
(eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).elim
(ne_of_gt (pos_def.1 pa))
(ne_of_gt (pos_def.1 pb))⟩,
..s }
instance to_linear_order : linear_order α :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := @lt_iff_le_not_le _ _,
le_refl := @le_refl _ _,
le_trans := @le_trans _ _,
le_antisymm := @le_antisymm _ _,
le_total := nonneg_total_iff.1 nonneg_total,
..s }
instance to_linear_ordered_ring : linear_ordered_ring α :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := @lt_iff_le_not_le _ _,
le_refl := @le_refl _ _,
le_trans := @le_trans _ _,
le_antisymm := @le_antisymm _ _,
le_total := @le_total _ _,
add_lt_add_left := @add_lt_add_left _ _,
add_le_add_left := @add_le_add_left _ _,
mul_nonneg := by simp [nonneg_def.symm]; exact @mul_nonneg _ _,
mul_pos := by simp [pos_def.symm]; exact @nonneg_ring.mul_pos _ _,
zero_lt_one := lt_of_not_ge $ λ (h : nonneg (0 - 1)), begin
rw [zero_sub] at h,
have := mul_nonneg h h, simp at this,
exact zero_ne_one _ (nonneg_antisymm this h).symm
end, ..s }
instance to_decidable_linear_ordered_comm_ring
[decidable_pred (@nonneg α _)]
[comm : @is_commutative α (*)]
: decidable_linear_ordered_comm_ring α :=
{ decidable_le := by apply_instance,
decidable_eq := by apply_instance,
decidable_lt := by apply_instance,
mul_comm := is_commutative.comm (*),
..@linear_nonneg_ring.to_linear_ordered_ring _ s }
end linear_nonneg_ring
|
ff7a96edb65b5a654aa0aa8ebcff1bf774194b48 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Init/Meta.lean | 8c42c2ce682d13ca117cbede62fc2a146f204b93 | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 46,877 | 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 and Sebastian Ullrich
Additional goodies for writing macros
-/
prelude
import Init.Data.Array.Basic
import Init.Data.Option.BasicAux
namespace Lean
@[extern c inline "lean_box(LEAN_VERSION_MAJOR)"]
private opaque version.getMajor (u : Unit) : Nat
def version.major : Nat := version.getMajor ()
@[extern c inline "lean_box(LEAN_VERSION_MINOR)"]
private opaque version.getMinor (u : Unit) : Nat
def version.minor : Nat := version.getMinor ()
@[extern c inline "lean_box(LEAN_VERSION_PATCH)"]
private opaque version.getPatch (u : Unit) : Nat
def version.patch : Nat := version.getPatch ()
@[extern "lean_get_githash"]
opaque getGithash (u : Unit) : String
def githash : String := getGithash ()
@[extern c inline "LEAN_VERSION_IS_RELEASE"]
opaque version.getIsRelease (u : Unit) : Bool
def version.isRelease : Bool := version.getIsRelease ()
/-- Additional version description like "nightly-2018-03-11" -/
@[extern c inline "lean_mk_string(LEAN_SPECIAL_VERSION_DESC)"]
opaque version.getSpecialDesc (u : Unit) : String
def version.specialDesc : String := version.getSpecialDesc ()
def versionStringCore :=
toString version.major ++ "." ++ toString version.minor ++ "." ++ toString version.patch
def versionString :=
if version.specialDesc ≠ "" then
versionStringCore ++ "-" ++ version.specialDesc
else if version.isRelease then
versionStringCore
else
versionStringCore ++ ", commit " ++ githash
def origin :=
"leanprover/lean4"
def toolchain :=
if version.specialDesc ≠ "" then
if version.isRelease then
origin ++ ":" ++ versionStringCore ++ "-" ++ version.specialDesc
else
origin ++ ":" ++ version.specialDesc
else if version.isRelease then
origin ++ ":" ++ versionStringCore
else
""
@[extern c inline "LEAN_IS_STAGE0"]
opaque Internal.isStage0 (u : Unit) : Bool
/-- Valid identifier names -/
def isGreek (c : Char) : Bool :=
0x391 ≤ c.val && c.val ≤ 0x3dd
def isLetterLike (c : Char) : Bool :=
(0x3b1 ≤ c.val && c.val ≤ 0x3c9 && c.val ≠ 0x3bb) || -- Lower greek, but lambda
(0x391 ≤ c.val && c.val ≤ 0x3A9 && c.val ≠ 0x3A0 && c.val ≠ 0x3A3) || -- Upper greek, but Pi and Sigma
(0x3ca ≤ c.val && c.val ≤ 0x3fb) || -- Coptic letters
(0x1f00 ≤ c.val && c.val ≤ 0x1ffe) || -- Polytonic Greek Extended Character Set
(0x2100 ≤ c.val && c.val ≤ 0x214f) || -- Letter like block
(0x1d49c ≤ c.val && c.val ≤ 0x1d59f) -- Latin letters, Script, Double-struck, Fractur
def isNumericSubscript (c : Char) : Bool :=
0x2080 ≤ c.val && c.val ≤ 0x2089
def isSubScriptAlnum (c : Char) : Bool :=
isNumericSubscript c ||
(0x2090 ≤ c.val && c.val ≤ 0x209c) ||
(0x1d62 ≤ c.val && c.val ≤ 0x1d6a)
def isIdFirst (c : Char) : Bool :=
c.isAlpha || c = '_' || isLetterLike c
def isIdRest (c : Char) : Bool :=
c.isAlphanum || c = '_' || c = '\'' || c == '!' || c == '?' || isLetterLike c || isSubScriptAlnum c
def idBeginEscape := '«'
def idEndEscape := '»'
def isIdBeginEscape (c : Char) : Bool := c = idBeginEscape
def isIdEndEscape (c : Char) : Bool := c = idEndEscape
namespace Name
def getRoot : Name → Name
| anonymous => anonymous
| n@(str anonymous _) => n
| n@(num anonymous _) => n
| str n _ => getRoot n
| num n _ => getRoot n
@[export lean_is_inaccessible_user_name]
def isInaccessibleUserName : Name → Bool
| Name.str _ s => s.contains '✝' || s == "_inaccessible"
| Name.num p _ => isInaccessibleUserName p
| _ => false
def escapePart (s : String) : Option String :=
if s.length > 0 && isIdFirst (s.get 0) && (s.toSubstring.drop 1).all isIdRest then s
else if s.any isIdEndEscape then none
else some <| idBeginEscape.toString ++ s ++ idEndEscape.toString
-- NOTE: does not roundtrip even with `escape = true` if name is anonymous or contains numeric part or `idEndEscape`
variable (sep : String) (escape : Bool)
def toStringWithSep : Name → String
| anonymous => "[anonymous]"
| str anonymous s => maybeEscape s
| num anonymous v => toString v
| str n s => toStringWithSep n ++ sep ++ maybeEscape s
| num n v => toStringWithSep n ++ sep ++ Nat.repr v
where
maybeEscape s := if escape then escapePart s |>.getD s else s
protected def toString (n : Name) (escape := true) : String :=
-- never escape "prettified" inaccessible names or macro scopes or pseudo-syntax introduced by the delaborator
toStringWithSep "." (escape && !n.isInaccessibleUserName && !n.hasMacroScopes && !maybePseudoSyntax) n
where
maybePseudoSyntax :=
if let .str _ s := n.getRoot then
-- could be pseudo-syntax for loose bvar or universe mvar, output as is
"#".isPrefixOf s || "?".isPrefixOf s
else
false
instance : ToString Name where
toString n := n.toString
private def hasNum : Name → Bool
| anonymous => false
| num .. => true
| str p .. => hasNum p
protected def reprPrec (n : Name) (prec : Nat) : Std.Format :=
match n with
| anonymous => Std.Format.text "Lean.Name.anonymous"
| num p i => Repr.addAppParen ("Lean.Name.mkNum " ++ Name.reprPrec p max_prec ++ " " ++ repr i) prec
| str p s =>
if p.hasNum then
Repr.addAppParen ("Lean.Name.mkStr " ++ Name.reprPrec p max_prec ++ " " ++ repr s) prec
else
Std.Format.text "`" ++ n.toString
instance : Repr Name where
reprPrec := Name.reprPrec
def capitalize : Name → Name
| .str p s => .str p s.capitalize
| n => n
def replacePrefix : Name → Name → Name → Name
| anonymous, anonymous, newP => newP
| anonymous, _, _ => anonymous
| n@(str p s), queryP, newP => if n == queryP then newP else Name.mkStr (p.replacePrefix queryP newP) s
| n@(num p s), queryP, newP => if n == queryP then newP else Name.mkNum (p.replacePrefix queryP newP) s
/--
`eraseSuffix? n s` return `n'` if `n` is of the form `n == n' ++ s`.
-/
def eraseSuffix? : Name → Name → Option Name
| n, anonymous => some n
| str p s, str p' s' => if s == s' then eraseSuffix? p p' else none
| num p s, num p' s' => if s == s' then eraseSuffix? p p' else none
| _, _ => none
/-- Remove macros scopes, apply `f`, and put them back -/
@[inline] def modifyBase (n : Name) (f : Name → Name) : Name :=
if n.hasMacroScopes then
let view := extractMacroScopes n
{ view with name := f view.name }.review
else
f n
@[export lean_name_append_after]
def appendAfter (n : Name) (suffix : String) : Name :=
n.modifyBase fun
| str p s => Name.mkStr p (s ++ suffix)
| n => Name.mkStr n suffix
@[export lean_name_append_index_after]
def appendIndexAfter (n : Name) (idx : Nat) : Name :=
n.modifyBase fun
| str p s => Name.mkStr p (s ++ "_" ++ toString idx)
| n => Name.mkStr n ("_" ++ toString idx)
@[export lean_name_append_before]
def appendBefore (n : Name) (pre : String) : Name :=
n.modifyBase fun
| anonymous => Name.mkStr anonymous pre
| str p s => Name.mkStr p (pre ++ s)
| num p n => Name.mkNum (Name.mkStr p pre) n
protected theorem beq_iff_eq {m n : Name} : m == n ↔ m = n := by
show m.beq n ↔ _
induction m generalizing n <;> cases n <;> simp_all [Name.beq, And.comm]
instance : LawfulBEq Name where
eq_of_beq := Name.beq_iff_eq.1
rfl := Name.beq_iff_eq.2 rfl
instance : DecidableEq Name :=
fun a b => if h : a == b then .isTrue (by simp_all) else .isFalse (by simp_all)
end Name
structure NameGenerator where
namePrefix : Name := `_uniq
idx : Nat := 1
deriving Inhabited
namespace NameGenerator
@[inline] def curr (g : NameGenerator) : Name :=
Name.mkNum g.namePrefix g.idx
@[inline] def next (g : NameGenerator) : NameGenerator :=
{ g with idx := g.idx + 1 }
@[inline] def mkChild (g : NameGenerator) : NameGenerator × NameGenerator :=
({ namePrefix := Name.mkNum g.namePrefix g.idx, idx := 1 },
{ g with idx := g.idx + 1 })
end NameGenerator
class MonadNameGenerator (m : Type → Type) where
getNGen : m NameGenerator
setNGen : NameGenerator → m Unit
export MonadNameGenerator (getNGen setNGen)
def mkFreshId {m : Type → Type} [Monad m] [MonadNameGenerator m] : m Name := do
let ngen ← getNGen
let r := ngen.curr
setNGen ngen.next
pure r
instance monadNameGeneratorLift (m n : Type → Type) [MonadLift m n] [MonadNameGenerator m] : MonadNameGenerator n := {
getNGen := liftM (getNGen : m _),
setNGen := fun ngen => liftM (setNGen ngen : m _)
}
namespace Syntax
deriving instance Repr for Syntax.Preresolved
deriving instance Repr for Syntax
deriving instance Repr for TSyntax
abbrev Term := TSyntax `term
abbrev Command := TSyntax `command
protected abbrev Level := TSyntax `level
protected abbrev Tactic := TSyntax `tactic
abbrev Prec := TSyntax `prec
abbrev Prio := TSyntax `prio
abbrev Ident := TSyntax identKind
abbrev StrLit := TSyntax strLitKind
abbrev CharLit := TSyntax charLitKind
abbrev NameLit := TSyntax nameLitKind
abbrev ScientificLit := TSyntax scientificLitKind
abbrev NumLit := TSyntax numLitKind
end Syntax
export Syntax (Term Command Prec Prio Ident StrLit CharLit NameLit ScientificLit NumLit)
namespace TSyntax
instance : Coe (TSyntax [k]) (TSyntax (k :: ks)) where
coe stx := ⟨stx⟩
instance : Coe (TSyntax ks) (TSyntax (k' :: ks)) where
coe stx := ⟨stx⟩
instance : Coe Ident Term where
coe s := ⟨s.raw⟩
instance : CoeDep Term ⟨Syntax.ident info ss n res⟩ Ident where
coe := ⟨Syntax.ident info ss n res⟩
instance : Coe StrLit Term where
coe s := ⟨s.raw⟩
instance : Coe NameLit Term where
coe s := ⟨s.raw⟩
instance : Coe ScientificLit Term where
coe s := ⟨s.raw⟩
instance : Coe NumLit Term where
coe s := ⟨s.raw⟩
instance : Coe CharLit Term where
coe s := ⟨s.raw⟩
instance : Coe Ident Syntax.Level where
coe s := ⟨s.raw⟩
instance : Coe NumLit Prio where
coe s := ⟨s.raw⟩
instance : Coe NumLit Prec where
coe s := ⟨s.raw⟩
namespace Compat
scoped instance : CoeTail Syntax (TSyntax k) where
coe s := ⟨s⟩
scoped instance : CoeTail (Array Syntax) (TSyntaxArray k) where
coe := .mk
end Compat
end TSyntax
namespace Syntax
deriving instance BEq for Syntax.Preresolved
/-- Compare syntax structures modulo source info. -/
partial def structEq : Syntax → Syntax → Bool
| Syntax.missing, Syntax.missing => true
| Syntax.node _ k args, Syntax.node _ k' args' => k == k' && args.isEqv args' structEq
| Syntax.atom _ val, Syntax.atom _ val' => val == val'
| Syntax.ident _ rawVal val preresolved, Syntax.ident _ rawVal' val' preresolved' => rawVal == rawVal' && val == val' && preresolved == preresolved'
| _, _ => false
instance : BEq Lean.Syntax := ⟨structEq⟩
instance : BEq (Lean.TSyntax k) := ⟨(·.raw == ·.raw)⟩
partial def getTailInfo? : Syntax → Option SourceInfo
| atom info _ => info
| ident info .. => info
| node SourceInfo.none _ args =>
args.findSomeRev? getTailInfo?
| node info _ _ => info
| _ => none
def getTailInfo (stx : Syntax) : SourceInfo :=
stx.getTailInfo?.getD SourceInfo.none
def getTrailingSize (stx : Syntax) : Nat :=
match stx.getTailInfo? with
| some (SourceInfo.original (trailing := trailing) ..) => trailing.bsize
| _ => 0
/--
Return substring of original input covering `stx`.
Result is meaningful only if all involved `SourceInfo.original`s refer to the same string (as is the case after parsing). -/
def getSubstring? (stx : Syntax) (withLeading := true) (withTrailing := true) : Option Substring :=
match stx.getHeadInfo, stx.getTailInfo with
| SourceInfo.original lead startPos _ _, SourceInfo.original _ _ trail stopPos =>
some {
str := lead.str
startPos := if withLeading then lead.startPos else startPos
stopPos := if withTrailing then trail.stopPos else stopPos
}
| _, _ => none
@[specialize] private partial def updateLast {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) :=
if i == 0 then
none
else
let i := i - 1
let v := a[i]!
match f v with
| some v => some <| a.set! i v
| none => updateLast a f i
partial def setTailInfoAux (info : SourceInfo) : Syntax → Option Syntax
| atom _ val => some <| atom info val
| ident _ rawVal val pre => some <| ident info rawVal val pre
| node info k args =>
match updateLast args (setTailInfoAux info) args.size with
| some args => some <| node info k args
| none => none
| _ => none
def setTailInfo (stx : Syntax) (info : SourceInfo) : Syntax :=
match setTailInfoAux info stx with
| some stx => stx
| none => stx
def unsetTrailing (stx : Syntax) : Syntax :=
match stx.getTailInfo with
| SourceInfo.original lead pos _ endPos => stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos)
| _ => stx
@[specialize] private partial def updateFirst {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) :=
if h : i < a.size then
let v := a[i]
match f v with
| some v => some <| a.set ⟨i, h⟩ v
| none => updateFirst a f (i+1)
else
none
partial def setHeadInfoAux (info : SourceInfo) : Syntax → Option Syntax
| atom _ val => some <| atom info val
| ident _ rawVal val pre => some <| ident info rawVal val pre
| node i k args =>
match updateFirst args (setHeadInfoAux info) 0 with
| some args => some <| node i k args
| _ => none
| _ => none
def setHeadInfo (stx : Syntax) (info : SourceInfo) : Syntax :=
match setHeadInfoAux info stx with
| some stx => stx
| none => stx
def setInfo (info : SourceInfo) : Syntax → Syntax
| atom _ val => atom info val
| ident _ rawVal val pre => ident info rawVal val pre
| node _ kind args => node info kind args
| missing => missing
/-- Return the first atom/identifier that has position information -/
partial def getHead? : Syntax → Option Syntax
| stx@(atom info ..) => info.getPos?.map fun _ => stx
| stx@(ident info ..) => info.getPos?.map fun _ => stx
| node SourceInfo.none _ args => args.findSome? getHead?
| stx@(node ..) => stx
| _ => none
def copyHeadTailInfoFrom (target source : Syntax) : Syntax :=
target.setHeadInfo source.getHeadInfo |>.setTailInfo source.getTailInfo
/-- Ensure head position is synthetic. The server regards syntax as "original" only if both head and tail info are `original`. -/
def mkSynthetic (stx : Syntax) : Syntax :=
stx.setHeadInfo (SourceInfo.fromRef stx)
end Syntax
/-- Use the head atom/identifier of the current `ref` as the `ref` -/
@[inline] def withHeadRefOnly {m : Type → Type} [Monad m] [MonadRef m] {α} (x : m α) : m α := do
match (← getRef).getHead? with
| none => x
| some ref => withRef ref x
@[inline] def mkNode (k : SyntaxNodeKind) (args : Array Syntax) : TSyntax k :=
⟨Syntax.node SourceInfo.none k args⟩
/-- Syntax objects for a Lean module. -/
structure Module where
header : Syntax
commands : Array Syntax
/--
Expand macros in the given syntax.
A node with kind `k` is visited only if `p k` is true.
Note that the default value for `p` returns false for `by ...` nodes.
This is a "hack". The tactic framework abuses the macro system to implement extensible tactics.
For example, one can define
```lean
syntax "my_trivial" : tactic -- extensible tactic
macro_rules | `(tactic| my_trivial) => `(tactic| decide)
macro_rules | `(tactic| my_trivial) => `(tactic| assumption)
```
When the tactic evaluator finds the tactic `my_trivial`, it tries to evaluate the `macro_rule` expansions
until one "works", i.e., the macro expansion is evaluated without producing an exception.
We say this solution is a bit hackish because the term elaborator may invoke `expandMacros` with `(p := fun _ => true)`,
and expand the tactic macros as just macros. In the example above, `my_trivial` would be replaced with `assumption`,
`decide` would not be tried if `assumption` fails at tactic evaluation time.
We are considering two possible solutions for this issue:
1- A proper extensible tactic feature that does not rely on the macro system.
2- Typed macros that know the syntax categories they're working in. Then, we would be able to select which
syntatic categories are expanded by `expandMacros`.
-/
partial def expandMacros (stx : Syntax) (p : SyntaxNodeKind → Bool := fun k => k != `Lean.Parser.Term.byTactic) : MacroM Syntax :=
withRef stx do
match stx with
| .node info k args => do
if p k then
match (← expandMacro? stx) with
| some stxNew => expandMacros stxNew
| none => do
let args ← Macro.withIncRecDepth stx <| args.mapM expandMacros
return .node info k args
else
return stx
| stx => return stx
/-! # Helper functions for processing Syntax programmatically -/
/--
Create an identifier copying the position from `src`.
To refer to a specific constant, use `mkCIdentFrom` instead. -/
def mkIdentFrom (src : Syntax) (val : Name) (canonical := false) : Ident :=
⟨Syntax.ident (SourceInfo.fromRef src canonical) (toString val).toSubstring val []⟩
def mkIdentFromRef [Monad m] [MonadRef m] (val : Name) (canonical := false) : m Ident := do
return mkIdentFrom (← getRef) val canonical
/--
Create an identifier referring to a constant `c` copying the position from `src`.
This variant of `mkIdentFrom` makes sure that the identifier cannot accidentally
be captured. -/
def mkCIdentFrom (src : Syntax) (c : Name) (canonical := false) : Ident :=
-- Remark: We use the reserved macro scope to make sure there are no accidental collision with our frontend
let id := addMacroScope `_internal c reservedMacroScope
⟨Syntax.ident (SourceInfo.fromRef src canonical) (toString id).toSubstring id [.decl c []]⟩
def mkCIdentFromRef [Monad m] [MonadRef m] (c : Name) (canonical := false) : m Syntax := do
return mkCIdentFrom (← getRef) c canonical
def mkCIdent (c : Name) : Ident :=
mkCIdentFrom Syntax.missing c
@[export lean_mk_syntax_ident]
def mkIdent (val : Name) : Ident :=
⟨Syntax.ident SourceInfo.none (toString val).toSubstring val []⟩
@[inline] def mkNullNode (args : Array Syntax := #[]) : Syntax :=
mkNode nullKind args
@[inline] def mkGroupNode (args : Array Syntax := #[]) : Syntax :=
mkNode groupKind args
def mkSepArray (as : Array Syntax) (sep : Syntax) : Array Syntax := Id.run do
let mut i := 0
let mut r := #[]
for a in as do
if i > 0 then
r := r.push sep |>.push a
else
r := r.push a
i := i + 1
return r
def mkOptionalNode (arg : Option Syntax) : Syntax :=
match arg with
| some arg => mkNullNode #[arg]
| none => mkNullNode #[]
def mkHole (ref : Syntax) (canonical := false) : Syntax :=
mkNode `Lean.Parser.Term.hole #[mkAtomFrom ref "_" canonical]
namespace Syntax
def mkSep (a : Array Syntax) (sep : Syntax) : Syntax :=
mkNullNode <| mkSepArray a sep
def SepArray.ofElems {sep} (elems : Array Syntax) : SepArray sep :=
⟨mkSepArray elems (if sep.isEmpty then mkNullNode else mkAtom sep)⟩
def SepArray.ofElemsUsingRef [Monad m] [MonadRef m] {sep} (elems : Array Syntax) : m (SepArray sep) := do
let ref ← getRef;
return ⟨mkSepArray elems (if sep.isEmpty then mkNullNode else mkAtomFrom ref sep)⟩
instance : Coe (Array Syntax) (SepArray sep) where
coe := SepArray.ofElems
instance : Coe (TSyntaxArray k) (TSepArray k sep) where
coe a := ⟨mkSepArray a.raw (mkAtom sep)⟩
/-- Create syntax representing a Lean term application, but avoid degenerate empty applications. -/
def mkApp (fn : Term) : (args : TSyntaxArray `term) → Term
| #[] => fn
| args => ⟨mkNode `Lean.Parser.Term.app #[fn, mkNullNode args.raw]⟩
def mkCApp (fn : Name) (args : TSyntaxArray `term) : Term :=
mkApp (mkCIdent fn) args
def mkLit (kind : SyntaxNodeKind) (val : String) (info := SourceInfo.none) : TSyntax kind :=
let atom : Syntax := Syntax.atom info val
mkNode kind #[atom]
def mkStrLit (val : String) (info := SourceInfo.none) : StrLit :=
mkLit strLitKind (String.quote val) info
def mkNumLit (val : String) (info := SourceInfo.none) : NumLit :=
mkLit numLitKind val info
def mkScientificLit (val : String) (info := SourceInfo.none) : TSyntax scientificLitKind :=
mkLit scientificLitKind val info
def mkNameLit (val : String) (info := SourceInfo.none) : NameLit :=
mkLit nameLitKind val info
/-! Recall that we don't have special Syntax constructors for storing numeric and string atoms.
The idea is to have an extensible approach where embedded DSLs may have new kind of atoms and/or
different ways of representing them. So, our atoms contain just the parsed string.
The main Lean parser uses the kind `numLitKind` for storing natural numbers that can be encoded
in binary, octal, decimal and hexadecimal format. `isNatLit` implements a "decoder"
for Syntax objects representing these numerals. -/
private partial def decodeBinLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat :=
if s.atEnd i then some val
else
let c := s.get i
if c == '0' then decodeBinLitAux s (s.next i) (2*val)
else if c == '1' then decodeBinLitAux s (s.next i) (2*val + 1)
else none
private partial def decodeOctalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat :=
if s.atEnd i then some val
else
let c := s.get i
if '0' ≤ c && c ≤ '7' then decodeOctalLitAux s (s.next i) (8*val + c.toNat - '0'.toNat)
else none
private def decodeHexDigit (s : String) (i : String.Pos) : Option (Nat × String.Pos) :=
let c := s.get i
let i := s.next i
if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat, i)
else if 'a' ≤ c && c ≤ 'f' then some (10 + c.toNat - 'a'.toNat, i)
else if 'A' ≤ c && c ≤ 'F' then some (10 + c.toNat - 'A'.toNat, i)
else none
private partial def decodeHexLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat :=
if s.atEnd i then some val
else match decodeHexDigit s i with
| some (d, i) => decodeHexLitAux s i (16*val + d)
| none => none
private partial def decodeDecimalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat :=
if s.atEnd i then some val
else
let c := s.get i
if '0' ≤ c && c ≤ '9' then decodeDecimalLitAux s (s.next i) (10*val + c.toNat - '0'.toNat)
else none
def decodeNatLitVal? (s : String) : Option Nat :=
let len := s.length
if len == 0 then none
else
let c := s.get 0
if c == '0' then
if len == 1 then some 0
else
let c := s.get ⟨1⟩
if c == 'x' || c == 'X' then decodeHexLitAux s ⟨2⟩ 0
else if c == 'b' || c == 'B' then decodeBinLitAux s ⟨2⟩ 0
else if c == 'o' || c == 'O' then decodeOctalLitAux s ⟨2⟩ 0
else if c.isDigit then decodeDecimalLitAux s 0 0
else none
else if c.isDigit then decodeDecimalLitAux s 0 0
else none
def isLit? (litKind : SyntaxNodeKind) (stx : Syntax) : Option String :=
match stx with
| Syntax.node _ k args =>
if k == litKind && args.size == 1 then
match args.get! 0 with
| (Syntax.atom _ val) => some val
| _ => none
else
none
| _ => none
private def isNatLitAux (litKind : SyntaxNodeKind) (stx : Syntax) : Option Nat :=
match isLit? litKind stx with
| some val => decodeNatLitVal? val
| _ => none
def isNatLit? (s : Syntax) : Option Nat :=
isNatLitAux numLitKind s
def isFieldIdx? (s : Syntax) : Option Nat :=
isNatLitAux fieldIdxKind s
/-- Decodes a 'scientific number' string which is consumed by the `OfScientific` class.
Takes as input a string such as `123`, `123.456e7` and returns a triple `(n, sign, e)` with value given by
`n * 10^-e` if `sign` else `n * 10^e`.
-/
partial def decodeScientificLitVal? (s : String) : Option (Nat × Bool × Nat) :=
let len := s.length
if len == 0 then none
else
let c := s.get 0
if c.isDigit then
decode 0 0
else none
where
decodeAfterExp (i : String.Pos) (val : Nat) (e : Nat) (sign : Bool) (exp : Nat) : Option (Nat × Bool × Nat) :=
if s.atEnd i then
if sign then
some (val, sign, exp + e)
else if exp >= e then
some (val, sign, exp - e)
else
some (val, true, e - exp)
else
let c := s.get i
if '0' ≤ c && c ≤ '9' then
decodeAfterExp (s.next i) val e sign (10*exp + c.toNat - '0'.toNat)
else
none
decodeExp (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) :=
if s.atEnd i then none else
let c := s.get i
if c == '-' then
decodeAfterExp (s.next i) val e true 0
else if c == '+' then
decodeAfterExp (s.next i) val e false 0
else
decodeAfterExp i val e false 0
decodeAfterDot (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) :=
if s.atEnd i then
some (val, true, e)
else
let c := s.get i
if '0' ≤ c && c ≤ '9' then
decodeAfterDot (s.next i) (10*val + c.toNat - '0'.toNat) (e+1)
else if c == 'e' || c == 'E' then
decodeExp (s.next i) val e
else
none
decode (i : String.Pos) (val : Nat) : Option (Nat × Bool × Nat) :=
if s.atEnd i then
none
else
let c := s.get i
if '0' ≤ c && c ≤ '9' then
decode (s.next i) (10*val + c.toNat - '0'.toNat)
else if c == '.' then
decodeAfterDot (s.next i) val 0
else if c == 'e' || c == 'E' then
decodeExp (s.next i) val 0
else
none
def isScientificLit? (stx : Syntax) : Option (Nat × Bool × Nat) :=
match isLit? scientificLitKind stx with
| some val => decodeScientificLitVal? val
| _ => none
def isIdOrAtom? : Syntax → Option String
| Syntax.atom _ val => some val
| Syntax.ident _ rawVal _ _ => some rawVal.toString
| _ => none
def toNat (stx : Syntax) : Nat :=
match stx.isNatLit? with
| some val => val
| none => 0
def decodeQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := do
let c := s.get i
let i := s.next i
if c == '\\' then pure ('\\', i)
else if c = '\"' then pure ('\"', i)
else if c = '\'' then pure ('\'', i)
else if c = 'r' then pure ('\r', i)
else if c = 'n' then pure ('\n', i)
else if c = 't' then pure ('\t', i)
else if c = 'x' then
let (d₁, i) ← decodeHexDigit s i
let (d₂, i) ← decodeHexDigit s i
pure (Char.ofNat (16*d₁ + d₂), i)
else if c = 'u' then do
let (d₁, i) ← decodeHexDigit s i
let (d₂, i) ← decodeHexDigit s i
let (d₃, i) ← decodeHexDigit s i
let (d₄, i) ← decodeHexDigit s i
pure (Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄), i)
else
none
partial def decodeStrLitAux (s : String) (i : String.Pos) (acc : String) : Option String := do
let c := s.get i
let i := s.next i
if c == '\"' then
pure acc
else if s.atEnd i then
none
else if c == '\\' then do
let (c, i) ← decodeQuotedChar s i
decodeStrLitAux s i (acc.push c)
else
decodeStrLitAux s i (acc.push c)
def decodeStrLit (s : String) : Option String :=
decodeStrLitAux s ⟨1⟩ ""
def isStrLit? (stx : Syntax) : Option String :=
match isLit? strLitKind stx with
| some val => decodeStrLit val
| _ => none
def decodeCharLit (s : String) : Option Char := do
let c := s.get ⟨1⟩
if c == '\\' then do
let (c, _) ← decodeQuotedChar s ⟨2⟩
pure c
else
pure c
def isCharLit? (stx : Syntax) : Option Char :=
match isLit? charLitKind stx with
| some val => decodeCharLit val
| _ => none
private partial def splitNameLitAux (ss : Substring) (acc : List Substring) : List Substring :=
let splitRest (ss : Substring) (acc : List Substring) : List Substring :=
if ss.front == '.' then
splitNameLitAux (ss.drop 1) acc
else if ss.isEmpty then
acc
else
[]
if ss.isEmpty then []
else
let curr := ss.front
if isIdBeginEscape curr then
let escapedPart := ss.takeWhile (!isIdEndEscape ·)
let escapedPart := { escapedPart with stopPos := ss.stopPos.min (escapedPart.str.next escapedPart.stopPos) }
if !isIdEndEscape (escapedPart.get <| escapedPart.prev ⟨escapedPart.bsize⟩) then []
else splitRest (ss.extract ⟨escapedPart.bsize⟩ ⟨ss.bsize⟩) (escapedPart :: acc)
else if isIdFirst curr then
let idPart := ss.takeWhile isIdRest
splitRest (ss.extract ⟨idPart.bsize⟩ ⟨ss.bsize⟩) (idPart :: acc)
else if curr.isDigit then
let idPart := ss.takeWhile Char.isDigit
splitRest (ss.extract ⟨idPart.bsize⟩ ⟨ss.bsize⟩) (idPart :: acc)
else
[]
/-- Split a name literal (without the backtick) into its dot-separated components. For example,
`foo.bla.«bo.o»` ↦ `["foo", "bla", "«bo.o»"]`. If the literal cannot be parsed, return `[]`. -/
def splitNameLit (ss : Substring) : List Substring :=
splitNameLitAux ss [] |>.reverse
def decodeNameLit (s : String) : Option Name :=
if s.get 0 == '`' then
match splitNameLitAux (s.toSubstring.drop 1) [] with
| [] => none
| comps => some <| comps.foldr (init := Name.anonymous)
fun comp n =>
let comp := comp.toString
if isIdBeginEscape comp.front then
Name.mkStr n (comp.drop 1 |>.dropRight 1)
else if comp.front.isDigit then
if let some k := decodeNatLitVal? comp then
Name.mkNum n k
else
unreachable!
else
Name.mkStr n comp
else
none
def isNameLit? (stx : Syntax) : Option Name :=
match isLit? nameLitKind stx with
| some val => decodeNameLit val
| _ => none
def hasArgs : Syntax → Bool
| Syntax.node _ _ args => args.size > 0
| _ => false
def isAtom : Syntax → Bool
| atom _ _ => true
| _ => false
def isToken (token : String) : Syntax → Bool
| atom _ val => val.trim == token.trim
| _ => false
def isNone (stx : Syntax) : Bool :=
match stx with
| Syntax.node _ k args => k == nullKind && args.size == 0
-- when elaborating partial syntax trees, it's reasonable to interpret missing parts as `none`
| Syntax.missing => true
| _ => false
def getOptionalIdent? (stx : Syntax) : Option Name :=
match stx.getOptional? with
| some stx => some stx.getId
| none => none
partial def findAux (p : Syntax → Bool) : Syntax → Option Syntax
| stx@(Syntax.node _ _ args) => if p stx then some stx else args.findSome? (findAux p)
| stx => if p stx then some stx else none
def find? (stx : Syntax) (p : Syntax → Bool) : Option Syntax :=
findAux p stx
end Syntax
namespace TSyntax
def getNat (s : NumLit) : Nat :=
s.raw.isNatLit?.getD 0
def getId (s : Ident) : Name :=
s.raw.getId
def getScientific (s : ScientificLit) : Nat × Bool × Nat :=
s.raw.isScientificLit?.getD (0, false, 0)
def getString (s : StrLit) : String :=
s.raw.isStrLit?.getD ""
def getChar (s : CharLit) : Char :=
s.raw.isCharLit?.getD default
def getName (s : NameLit) : Name :=
s.raw.isNameLit?.getD .anonymous
namespace Compat
scoped instance : CoeTail (Array Syntax) (Syntax.TSepArray k sep) where
coe a := (a : TSyntaxArray k)
end Compat
end TSyntax
/-- Reflect a runtime datum back to surface syntax (best-effort). -/
class Quote (α : Type) (k : SyntaxNodeKind := `term) where
quote : α → TSyntax k
export Quote (quote)
instance [Quote α k] [CoeHTCT (TSyntax k) (TSyntax [k'])] : Quote α k' := ⟨fun a => quote (k := k) a⟩
instance : Quote Term := ⟨id⟩
instance : Quote Bool := ⟨fun | true => mkCIdent ``Bool.true | false => mkCIdent ``Bool.false⟩
instance : Quote String strLitKind := ⟨Syntax.mkStrLit⟩
instance : Quote Nat numLitKind := ⟨fun n => Syntax.mkNumLit <| toString n⟩
instance : Quote Substring := ⟨fun s => Syntax.mkCApp ``String.toSubstring' #[quote s.toString]⟩
-- in contrast to `Name.toString`, we can, and want to be, precise here
private def getEscapedNameParts? (acc : List String) : Name → Option (List String)
| Name.anonymous => if acc.isEmpty then none else some acc
| Name.str n s => do
let s ← Name.escapePart s
getEscapedNameParts? (s::acc) n
| Name.num _ _ => none
def quoteNameMk : Name → Term
| .anonymous => mkCIdent ``Name.anonymous
| .str n s => Syntax.mkCApp ``Name.mkStr #[quoteNameMk n, quote s]
| .num n i => Syntax.mkCApp ``Name.mkNum #[quoteNameMk n, quote i]
instance : Quote Name `term where
quote n := match getEscapedNameParts? [] n with
| some ss => ⟨mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit ("`" ++ ".".intercalate ss)]⟩
| none => ⟨quoteNameMk n⟩
instance [Quote α `term] [Quote β `term] : Quote (α × β) `term where
quote
| ⟨a, b⟩ => Syntax.mkCApp ``Prod.mk #[quote a, quote b]
private def quoteList [Quote α `term] : List α → Term
| [] => mkCIdent ``List.nil
| (x::xs) => Syntax.mkCApp ``List.cons #[quote x, quoteList xs]
instance [Quote α `term] : Quote (List α) `term where
quote := quoteList
private def quoteArray [Quote α `term] (xs : Array α) : Term :=
if xs.size <= 8 then
go 0 #[]
else
Syntax.mkCApp ``List.toArray #[quote xs.toList]
where
go (i : Nat) (args : Array Term) : Term :=
if h : i < xs.size then
go (i+1) (args.push (quote xs[i]))
else
Syntax.mkCApp (Name.mkStr2 "Array" ("mkArray" ++ toString xs.size)) args
termination_by go i _ => xs.size - i
instance [Quote α `term] : Quote (Array α) `term where
quote := quoteArray
instance Option.hasQuote {α : Type} [Quote α `term] : Quote (Option α) `term where
quote
| none => mkIdent ``none
| (some x) => Syntax.mkCApp ``some #[quote x]
/-- Evaluator for `prec` DSL -/
def evalPrec (stx : Syntax) : MacroM Nat :=
Macro.withIncRecDepth stx do
let stx ← expandMacros stx
match stx with
| `(prec| $num:num) => return num.getNat
| _ => Macro.throwErrorAt stx "unexpected precedence"
macro_rules
| `(prec| $a + $b) => do `(prec| $(quote <| (← evalPrec a) + (← evalPrec b)):num)
macro_rules
| `(prec| $a - $b) => do `(prec| $(quote <| (← evalPrec a) - (← evalPrec b)):num)
macro "eval_prec " p:prec:max : term => return quote (k := `term) (← evalPrec p)
/-- Evaluator for `prio` DSL -/
def evalPrio (stx : Syntax) : MacroM Nat :=
Macro.withIncRecDepth stx do
let stx ← expandMacros stx
match stx with
| `(prio| $num:num) => return num.getNat
| _ => Macro.throwErrorAt stx "unexpected priority"
macro_rules
| `(prio| $a + $b) => do `(prio| $(quote <| (← evalPrio a) + (← evalPrio b)):num)
macro_rules
| `(prio| $a - $b) => do `(prio| $(quote <| (← evalPrio a) - (← evalPrio b)):num)
macro "eval_prio " p:prio:max : term => return quote (k := `term) (← evalPrio p)
def evalOptPrio : Option (TSyntax `prio) → MacroM Nat
| some prio => evalPrio prio
| none => return 1000 -- TODO: FIX back eval_prio default
end Lean
namespace Array
abbrev getSepElems := @getEvenElems
open Lean
private partial def filterSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do
if h : i < a.size then
let stx := a[i]
if (← p stx) then
if acc.isEmpty then
filterSepElemsMAux a p (i+2) (acc.push stx)
else if hz : i ≠ 0 then
have : i.pred < i := Nat.pred_lt hz
have : i.pred < a.size := Nat.lt_trans this h
let sepStx := a[i.pred]
filterSepElemsMAux a p (i+2) ((acc.push sepStx).push stx)
else
filterSepElemsMAux a p (i+2) (acc.push stx)
else
filterSepElemsMAux a p (i+2) acc
else
pure acc
def filterSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) : m (Array Syntax) :=
filterSepElemsMAux a p 0 #[]
def filterSepElems (a : Array Syntax) (p : Syntax → Bool) : Array Syntax :=
Id.run <| a.filterSepElemsM p
private partial def mapSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do
if h : i < a.size then
let stx := a[i]
if i % 2 == 0 then do
let stx ← f stx
mapSepElemsMAux a f (i+1) (acc.push stx)
else
mapSepElemsMAux a f (i+1) (acc.push stx)
else
pure acc
def mapSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) : m (Array Syntax) :=
mapSepElemsMAux a f 0 #[]
def mapSepElems (a : Array Syntax) (f : Syntax → Syntax) : Array Syntax :=
Id.run <| a.mapSepElemsM f
end Array
namespace Lean.Syntax
def SepArray.getElems (sa : SepArray sep) : Array Syntax :=
sa.elemsAndSeps.getSepElems
def TSepArray.getElems (sa : TSepArray k sep) : TSyntaxArray k :=
.mk sa.elemsAndSeps.getSepElems
def TSepArray.push (sa : TSepArray k sep) (e : TSyntax k) : TSepArray k sep :=
if sa.elemsAndSeps.isEmpty then
{ elemsAndSeps := #[e] }
else
{ elemsAndSeps := sa.elemsAndSeps.push (mkAtom sep) |>.push e }
instance : EmptyCollection (SepArray sep) where
emptyCollection := ⟨∅⟩
instance : EmptyCollection (TSepArray sep k) where
emptyCollection := ⟨∅⟩
/-
We use `CoeTail` here instead of `Coe` to avoid a "loop" when computing `CoeTC`.
The "loop" is interrupted using the maximum instance size threshold, but it is a performance bottleneck.
The loop occurs because the predicate `isNewAnswer` is too imprecise.
-/
instance : CoeTail (SepArray sep) (Array Syntax) where
coe := SepArray.getElems
instance : Coe (TSepArray k sep) (TSyntaxArray k) where
coe := TSepArray.getElems
instance [Coe (TSyntax k) (TSyntax k')] : Coe (TSyntaxArray k) (TSyntaxArray k') where
coe a := a.map Coe.coe
instance : Coe (TSyntaxArray k) (Array Syntax) where
coe a := a.raw
instance : Coe Ident (TSyntax `Lean.Parser.Command.declId) where
coe id := mkNode _ #[id, mkNullNode #[]]
instance : Coe (Lean.Term) (Lean.TSyntax `Lean.Parser.Term.funBinder) where
coe stx := ⟨stx⟩
end Lean.Syntax
set_option linter.unusedVariables.funArgs false in
/--
Gadget for automatic parameter support. This is similar to the `optParam` gadget, but it uses
the given tactic.
Like `optParam`, this gadget only affects elaboration.
For example, the tactic will *not* be invoked during type class resolution. -/
abbrev autoParam.{u} (α : Sort u) (tactic : Lean.Syntax) : Sort u := α
/-! # Helper functions for manipulating interpolated strings -/
namespace Lean.Syntax
private def decodeInterpStrQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := do
match decodeQuotedChar s i with
| some r => some r
| none =>
let c := s.get i
let i := s.next i
if c == '{' then pure ('{', i)
else none
private partial def decodeInterpStrLit (s : String) : Option String :=
let rec loop (i : String.Pos) (acc : String) : Option String :=
let c := s.get i
let i := s.next i
if c == '\"' || c == '{' then
pure acc
else if s.atEnd i then
none
else if c == '\\' then do
let (c, i) ← decodeInterpStrQuotedChar s i
loop i (acc.push c)
else
loop i (acc.push c)
loop ⟨1⟩ ""
partial def isInterpolatedStrLit? (stx : Syntax) : Option String :=
match isLit? interpolatedStrLitKind stx with
| none => none
| some val => decodeInterpStrLit val
def getSepArgs (stx : Syntax) : Array Syntax :=
stx.getArgs.getSepElems
end Syntax
namespace TSyntax
def expandInterpolatedStrChunks (chunks : Array Syntax) (mkAppend : Syntax → Syntax → MacroM Syntax) (mkElem : Syntax → MacroM Syntax) : MacroM Syntax := do
let mut i := 0
let mut result := Syntax.missing
for elem in chunks do
let elem ← match elem.isInterpolatedStrLit? with
| none => mkElem elem
| some str => mkElem (Syntax.mkStrLit str)
if i == 0 then
result := elem
else
result ← mkAppend result elem
i := i+1
return result
open TSyntax.Compat in
def expandInterpolatedStr (interpStr : TSyntax interpolatedStrKind) (type : Term) (toTypeFn : Term) : MacroM Term := do
let r ← expandInterpolatedStrChunks interpStr.raw.getArgs (fun a b => `($a ++ $b)) (fun a => `($toTypeFn $a))
`(($r : $type))
end TSyntax
namespace Meta
inductive TransparencyMode where
| all | default | reducible | instances
deriving Inhabited, BEq, Repr
inductive EtaStructMode where
/-- Enable eta for structure and classes. -/
| all
/-- Enable eta only for structures that are not classes. -/
| notClasses
/-- Disable eta for structures and classes. -/
| none
deriving Inhabited, BEq, Repr
namespace DSimp
structure Config where
zeta : Bool := true
beta : Bool := true
eta : Bool := true
etaStruct : EtaStructMode := .all
iota : Bool := true
proj : Bool := true
decide : Bool := true
autoUnfold : Bool := false
deriving Inhabited, BEq, Repr
end DSimp
namespace Simp
def defaultMaxSteps := 100000
structure Config where
maxSteps : Nat := defaultMaxSteps
maxDischargeDepth : Nat := 2
contextual : Bool := false
memoize : Bool := true
singlePass : Bool := false
zeta : Bool := true
beta : Bool := true
eta : Bool := true
etaStruct : EtaStructMode := .all
iota : Bool := true
proj : Bool := true
decide : Bool := true
arith : Bool := false
autoUnfold : Bool := false
/--
If `dsimp := true`, then switches to `dsimp` on dependent arguments where there is no congruence theorem that allows
`simp` to visit them. If `dsimp := false`, then argument is not visited.
-/
dsimp : Bool := true
deriving Inhabited, BEq, Repr
-- Configuration object for `simp_all`
structure ConfigCtx extends Config where
contextual := true
def neutralConfig : Simp.Config := {
zeta := false
beta := false
eta := false
iota := false
proj := false
decide := false
arith := false
autoUnfold := false
}
end Simp
namespace Rewrite
structure Config where
transparency : TransparencyMode := TransparencyMode.reducible
offsetCnstrs : Bool := true
end Rewrite
end Meta
namespace Parser.Tactic
/-- `erw [rules]` is a shorthand for `rw (config := { transparency := .default }) [rules]`.
This does rewriting up to unfolding of regular definitions (by comparison to regular `rw`
which only unfolds `@[reducible]` definitions). -/
macro "erw " s:rwRuleSeq loc:(location)? : tactic =>
`(rw (config := { transparency := .default }) $s $(loc)?)
syntax simpAllKind := atomic("(" &"all") " := " &"true" ")"
syntax dsimpKind := atomic("(" &"dsimp") " := " &"true" ")"
macro (name := declareSimpLikeTactic) doc?:(docComment)? "declare_simp_like_tactic" opt:((simpAllKind <|> dsimpKind)?) tacName:ident tacToken:str updateCfg:term : command => do
let (kind, tkn, stx) ←
if opt.raw.isNone then
pure (← `(``simp), ← `("simp"), ← `($[$doc?:docComment]? syntax (name := $tacName) $tacToken:str (config)? (discharger)? (&" only")? (" [" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic))
else if opt.raw[0].getKind == ``simpAllKind then
pure (← `(``simpAll), ← `("simp_all"), ← `($[$doc?:docComment]? syntax (name := $tacName) $tacToken:str (config)? (discharger)? (&" only")? (" [" (simpErase <|> simpLemma),* "]")? : tactic))
else
pure (← `(``dsimp), ← `("dsimp"), ← `($[$doc?:docComment]? syntax (name := $tacName) $tacToken:str (config)? (discharger)? (&" only")? (" [" (simpErase <|> simpLemma),* "]")? (location)? : tactic))
`($stx:command
@[macro $tacName] def expandSimp : Macro := fun s => do
let c ← match s[1][0] with
| `(config| (config := $$c)) => `(config| (config := $updateCfg $$c))
| _ => `(config| (config := $updateCfg {}))
let s := s.setKind $kind
let s := s.setArg 0 (mkAtomFrom s[0] $tkn (canonical := true))
let r := s.setArg 1 (mkNullNode #[c])
return r)
/-- `simp!` is shorthand for `simp` with `autoUnfold := true`.
This will rewrite with all equation lemmas, which can be used to
partially evaluate many definitions. -/
declare_simp_like_tactic simpAutoUnfold "simp! " fun (c : Lean.Meta.Simp.Config) => { c with autoUnfold := true }
/-- `simp_arith` is shorthand for `simp` with `arith := true`.
This enables the use of normalization by linear arithmetic. -/
declare_simp_like_tactic simpArith "simp_arith " fun (c : Lean.Meta.Simp.Config) => { c with arith := true }
/-- `simp_arith!` is shorthand for `simp_arith` with `autoUnfold := true`.
This will rewrite with all equation lemmas, which can be used to
partially evaluate many definitions. -/
declare_simp_like_tactic simpArithAutoUnfold "simp_arith! " fun (c : Lean.Meta.Simp.Config) => { c with arith := true, autoUnfold := true }
/-- `simp_all!` is shorthand for `simp_all` with `autoUnfold := true`.
This will rewrite with all equation lemmas, which can be used to
partially evaluate many definitions. -/
declare_simp_like_tactic (all := true) simpAllAutoUnfold "simp_all! " fun (c : Lean.Meta.Simp.ConfigCtx) => { c with autoUnfold := true }
/-- `simp_all_arith` combines the effects of `simp_all` and `simp_arith`. -/
declare_simp_like_tactic (all := true) simpAllArith "simp_all_arith " fun (c : Lean.Meta.Simp.ConfigCtx) => { c with arith := true }
/-- `simp_all_arith!` combines the effects of `simp_all`, `simp_arith` and `simp!`. -/
declare_simp_like_tactic (all := true) simpAllArithAutoUnfold "simp_all_arith! " fun (c : Lean.Meta.Simp.ConfigCtx) => { c with arith := true, autoUnfold := true }
/-- `dsimp!` is shorthand for `dsimp` with `autoUnfold := true`.
This will rewrite with all equation lemmas, which can be used to
partially evaluate many definitions. -/
declare_simp_like_tactic (dsimp := true) dsimpAutoUnfold "dsimp! " fun (c : Lean.Meta.DSimp.Config) => { c with autoUnfold := true }
end Parser.Tactic
end Lean
|
cb0263a2170a7e109c2f561e9839976305c87636 | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/topology/algebra/floor_ring.lean | e0c19eb26fe2d10903cbf47935a727b1fe59df84 | [
"Apache-2.0"
] | permissive | hamdysalah1/mathlib | b915f86b2503feeae268de369f1b16932321f097 | 95454452f6b3569bf967d35aab8d852b1ddf8017 | refs/heads/master | 1,677,154,116,545 | 1,611,797,994,000 | 1,611,797,994,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,355 | lean | /-
Copyright (c) 2020 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
Basic topological facts (limits and continuity) about `floor`,
`ceil` and `fract` in a `floor_ring`.
-/
import topology.algebra.ordered
import algebra.floor
open set function filter
open_locale topological_space
variables {α : Type*} [linear_ordered_ring α] [floor_ring α]
lemma tendsto_floor_at_top : tendsto (floor : α → ℤ) at_top at_top :=
begin
refine monotone.tendsto_at_top_at_top (λ a b hab, floor_mono hab) (λ b, _),
use (b : α) + ((1 : ℤ) : α),
rw [floor_add_int, floor_coe],
exact (lt_add_one _).le
end
lemma tendsto_floor_at_bot : tendsto (floor : α → ℤ) at_bot at_bot :=
begin
refine monotone.tendsto_at_bot_at_bot (λ a b hab, floor_mono hab) (λ b, ⟨b, _⟩),
rw floor_coe
end
lemma tendsto_ceil_at_top : tendsto (ceil : α → ℤ) at_top at_top :=
tendsto_neg_at_bot_at_top.comp (tendsto_floor_at_bot.comp tendsto_neg_at_top_at_bot)
lemma tendsto_ceil_at_bot : tendsto (ceil : α → ℤ) at_bot at_bot :=
tendsto_neg_at_top_at_bot.comp (tendsto_floor_at_top.comp tendsto_neg_at_bot_at_top)
variables [topological_space α]
lemma continuous_on_floor (n : ℤ) : continuous_on (λ x, floor x : α → α) (Ico n (n+1) : set α) :=
(continuous_on_congr $ floor_eq_on_Ico' n).mpr continuous_on_const
lemma continuous_on_ceil (n : ℤ) : continuous_on (λ x, ceil x : α → α) (Ioc (n-1) n : set α) :=
(continuous_on_congr $ ceil_eq_on_Ioc' n).mpr continuous_on_const
lemma tendsto_floor_right' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝 n) :=
begin
rw ← nhds_within_Ico_eq_nhds_within_Ici (lt_add_one (n : α)),
convert ← (continuous_on_floor _ _ (left_mem_Ico.mpr $ lt_add_one (_ : α))).tendsto,
rw floor_eq_iff,
exact ⟨le_refl _, lt_add_one _⟩
end
lemma tendsto_ceil_left' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝 n) :=
begin
rw ← nhds_within_Ioc_eq_nhds_within_Iic (sub_one_lt (n : α)),
convert ← (continuous_on_ceil _ _ (right_mem_Ioc.mpr $ sub_one_lt (_ : α))).tendsto,
rw ceil_eq_iff,
exact ⟨sub_one_lt _, le_refl _⟩
end
lemma tendsto_floor_right [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝[Ici n] n) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_floor_right' _)
begin
refine (eventually_nhds_with_of_forall $ λ x (hx : (n : α) ≤ x), _),
change _ ≤ _,
norm_cast,
convert ← floor_mono hx,
rw floor_eq_iff,
exact ⟨le_refl _, lt_add_one _⟩
end
lemma tendsto_ceil_left [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝[Iic n] n) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_ceil_left' _)
begin
refine (eventually_nhds_with_of_forall $ λ x (hx : x ≤ (n : α)), _),
change _ ≤ _,
norm_cast,
convert ← ceil_mono hx,
rw ceil_eq_iff,
exact ⟨sub_one_lt _, le_refl _⟩
end
lemma tendsto_floor_left [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝[Iic (n-1)] (n-1)) :=
begin
rw ← nhds_within_Ico_eq_nhds_within_Iio (sub_one_lt (n : α)),
convert (tendsto_nhds_within_congr $ (λ x hx, (floor_eq_on_Ico' (n-1) x hx).symm))
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds
(eventually_of_forall (λ _, mem_Iic.mpr $ le_refl _)));
norm_cast <|> apply_instance,
ring
end
lemma tendsto_ceil_right [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝[Ici (n+1)] (n+1)) :=
begin
rw ← nhds_within_Ioc_eq_nhds_within_Ioi (lt_add_one (n : α)),
convert (tendsto_nhds_within_congr $ (λ x hx, (ceil_eq_on_Ioc' (n+1) x hx).symm))
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds
(eventually_of_forall (λ _, mem_Ici.mpr $ le_refl _)));
norm_cast <|> apply_instance,
ring
end
lemma tendsto_floor_left' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝 (n-1)) :=
begin
rw ← nhds_within_univ,
exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_floor_left n),
end
lemma tendsto_ceil_right' [order_closed_topology α] (n : ℤ) :
tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝 (n+1)) :=
begin
rw ← nhds_within_univ,
exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_ceil_right n),
end
lemma continuous_on_fract [topological_add_group α] (n : ℤ) :
continuous_on (fract : α → α) (Ico n (n+1) : set α) :=
continuous_on_id.sub (continuous_on_floor n)
lemma tendsto_fract_left' [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝 1) :=
begin
convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_left' n);
[{norm_cast, ring}, apply_instance, apply_instance]
end
lemma tendsto_fract_left [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝[Iio 1] 1) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(tendsto_fract_left' _) (eventually_of_forall fract_lt_one)
lemma tendsto_fract_right' [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝 0) :=
begin
convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_right' n);
[exact (sub_self _).symm, apply_instance, apply_instance]
end
lemma tendsto_fract_right [order_closed_topology α] [topological_add_group α]
(n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝[Ici 0] 0) :=
tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(tendsto_fract_right' _) (eventually_of_forall fract_nonneg)
local notation `I` := (Icc 0 1 : set α)
lemma continuous_on.comp_fract' {β γ : Type*} [order_topology α]
[topological_add_group α] [topological_space β] [topological_space γ] {f : β → α → γ}
(h : continuous_on (uncurry f) $ (univ : set β).prod I) (hf : ∀ s, f s 0 = f s 1) :
continuous (λ st : β × α, f st.1 $ fract st.2) :=
begin
change continuous ((uncurry f) ∘ (prod.map id (fract))),
rw continuous_iff_continuous_at,
rintro ⟨s, t⟩,
by_cases ht : t = floor t,
{ rw ht,
rw ← continuous_within_at_univ,
have : (univ : set (β × α)) ⊆ (set.prod univ (Iio $ floor t)) ∪ (set.prod univ (Ici $ floor t)),
{ rintros p -,
rw ← prod_union,
exact ⟨true.intro, lt_or_le _ _⟩ },
refine continuous_within_at.mono _ this,
refine continuous_within_at.union _ _,
{ simp only [continuous_within_at, fract_coe, nhds_within_prod_eq,
nhds_within_univ, id.def, comp_app, prod.map_mk],
have : (uncurry f) (s, 0) = (uncurry f) (s, (1 : α)),
by simp [uncurry, hf],
rw this,
refine (h _ ⟨true.intro, by exact_mod_cast right_mem_Icc.mpr zero_le_one⟩).tendsto.comp _,
rw [nhds_within_prod_eq, nhds_within_univ],
rw nhds_within_Icc_eq_nhds_within_Iic (@zero_lt_one α _ _),
exact tendsto_id.prod_map
(tendsto_nhds_within_mono_right Iio_subset_Iic_self $ tendsto_fract_left _) },
{ simp only [continuous_within_at, fract_coe, nhds_within_prod_eq,
nhds_within_univ, id.def, comp_app, prod.map_mk],
refine (h _ ⟨true.intro, by exact_mod_cast left_mem_Icc.mpr zero_le_one⟩).tendsto.comp _,
rw [nhds_within_prod_eq, nhds_within_univ,
nhds_within_Icc_eq_nhds_within_Ici (@zero_lt_one α _ _)],
exact tendsto_id.prod_map (tendsto_fract_right _) } },
{ have : t ∈ Ioo (floor t : α) ((floor t : α) + 1),
from ⟨lt_of_le_of_ne (floor_le t) (ne.symm ht), lt_floor_add_one _⟩,
apply (h ((prod.map _ fract) _) ⟨trivial, ⟨fract_nonneg _, (fract_lt_one _).le⟩⟩).tendsto.comp,
simp only [nhds_prod_eq, nhds_within_prod_eq, nhds_within_univ, id.def, prod.map_mk],
exact continuous_at_id.tendsto.prod_map
(tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
(((continuous_on_fract _ _ (Ioo_subset_Ico_self this)).mono
Ioo_subset_Ico_self).continuous_at (Ioo_mem_nhds this.1 this.2))
(eventually_of_forall (λ x, ⟨fract_nonneg _, (fract_lt_one _).le⟩)) ) }
end
lemma continuous_on.comp_fract {β : Type*} [order_topology α]
[topological_add_group α] [topological_space β] {f : α → β}
(h : continuous_on f I) (hf : f 0 = f 1) : continuous (f ∘ fract) :=
begin
let f' : unit → α → β := λ x y, f y,
have : continuous_on (uncurry f') ((univ : set unit).prod I),
{ rintros ⟨s, t⟩ ⟨-, ht : t ∈ I⟩,
simp only [continuous_within_at, uncurry, nhds_within_prod_eq, nhds_within_univ, f'],
rw tendsto_prod_iff,
intros W hW,
specialize h t ht hW,
rw mem_map_sets_iff at h,
rcases h with ⟨V, hV, hVW⟩,
rw image_subset_iff at hVW,
use [univ, univ_mem_sets, V, hV],
intros x y hx hy,
exact hVW hy },
have key : continuous (λ s, ⟨unit.star, s⟩ : α → unit × α) := by continuity,
exact (this.comp_fract' (λ s, hf)).comp key
end
|
1120f746b2a6ed89c27d68550f873840ef3d1132 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/category_theory/abelian/homology.lean | 013bcb3f2c79ae45c85464cea7ee93f16428540f | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 9,683 | lean | /-
Copyright (c) 2022 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.abelian.exact
import category_theory.abelian.pseudoelements
/-!
The object `homology f g w`, where `w : f ≫ g = 0`, can be identified with either a
cokernel or a kernel. The isomorphism with a cokernel is `homology_iso_cokernel_lift`, which
was obtained elsewhere. In the case of an abelian category, this file shows the isomorphism
with a kernel as well.
We use these isomorphisms to obtain the analogous api for `homology`:
- `homology.ι` is the map from `homology f g w` into the cokernel of `f`.
- `homology.π'` is the map from `kernel g` to `homology f g w`.
- `homology.desc'` constructs a morphism from `homology f g w`, when it is viewed as a cokernel.
- `homology.lift` constructs a morphism to `homology f g w`, when it is viewed as a kernel.
- Various small lemmas are proved as well, mimicking the API for (co)kernels.
With these definitions and lemmas, the isomorphisms between homology and a (co)kernel need not
be used directly.
-/
open category_theory.limits
open category_theory
noncomputable theory
universes v u
variables {A : Type u} [category.{v} A] [abelian A]
variables {X Y Z : A} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0)
namespace category_theory.abelian
/-- The cokernel of `kernel.lift g f w`. This is isomorphic to `homology f g w`.
See `homology_iso_cokernel_lift`. -/
abbreviation homology_c : A :=
cokernel (kernel.lift g f w)
/-- The kernel of `cokernel.desc f g w`. This is isomorphic to `homology f g w`.
See `homology_iso_kernel_desc`. -/
abbreviation homology_k : A :=
kernel (cokernel.desc f g w)
/-- The canonical map from `homology_c` to `homology_k`.
This is an isomorphism, and it is used in obtaining the API for `homology f g w`
in the bottom of this file. -/
abbreviation homology_c_to_k : homology_c f g w ⟶ homology_k f g w :=
cokernel.desc _ (kernel.lift _ (kernel.ι _ ≫ cokernel.π _) (by simp)) begin
apply limits.equalizer.hom_ext,
simp,
end
local attribute [instance] pseudoelement.hom_to_fun pseudoelement.has_zero
instance : mono (homology_c_to_k f g w) :=
begin
apply pseudoelement.mono_of_zero_of_map_zero,
intros a ha,
obtain ⟨a,rfl⟩ := pseudoelement.pseudo_surjective_of_epi (cokernel.π (kernel.lift g f w)) a,
apply_fun (kernel.ι (cokernel.desc f g w)) at ha,
simp only [←pseudoelement.comp_apply, cokernel.π_desc,
kernel.lift_ι, pseudoelement.apply_zero] at ha,
simp only [pseudoelement.comp_apply] at ha,
obtain ⟨b,hb⟩ : ∃ b, f b = _ := (pseudoelement.pseudo_exact_of_exact (exact_cokernel f)).2 _ ha,
rsuffices ⟨c, rfl⟩ : ∃ c, kernel.lift g f w c = a,
{ simp [← pseudoelement.comp_apply] },
use b,
apply_fun kernel.ι g,
swap, { apply pseudoelement.pseudo_injective_of_mono },
simpa [← pseudoelement.comp_apply]
end
instance : epi (homology_c_to_k f g w) :=
begin
apply pseudoelement.epi_of_pseudo_surjective,
intros a,
let b := kernel.ι (cokernel.desc f g w) a,
obtain ⟨c,hc⟩ : ∃ c, cokernel.π f c = b,
apply pseudoelement.pseudo_surjective_of_epi (cokernel.π f),
have : g c = 0,
{ dsimp [b] at hc,
rw [(show g = cokernel.π f ≫ cokernel.desc f g w, by simp), pseudoelement.comp_apply, hc],
simp [← pseudoelement.comp_apply] },
obtain ⟨d,hd⟩ : ∃ d, kernel.ι g d = c,
{ apply (pseudoelement.pseudo_exact_of_exact exact_kernel_ι).2 _ this },
use cokernel.π (kernel.lift g f w) d,
apply_fun kernel.ι (cokernel.desc f g w),
swap, { apply pseudoelement.pseudo_injective_of_mono },
simp only [←pseudoelement.comp_apply, cokernel.π_desc, kernel.lift_ι],
simp only [pseudoelement.comp_apply, hd, hc],
end
instance (w : f ≫ g = 0) : is_iso (homology_c_to_k f g w) := is_iso_of_mono_of_epi _
end category_theory.abelian
/-- The homology associated to `f` and `g` is isomorphic to a kernel. -/
def homology_iso_kernel_desc : homology f g w ≅ kernel (cokernel.desc f g w) :=
homology_iso_cokernel_lift _ _ _ ≪≫ as_iso (category_theory.abelian.homology_c_to_k _ _ _)
namespace homology
-- `homology.π` is taken
/-- The canonical map from the kernel of `g` to the homology of `f` and `g`. -/
def π' : kernel g ⟶ homology f g w :=
cokernel.π _ ≫ (homology_iso_cokernel_lift _ _ _).inv
/-- The canonical map from the homology of `f` and `g` to the cokernel of `f`. -/
def ι : homology f g w ⟶ cokernel f :=
(homology_iso_kernel_desc _ _ _).hom ≫ kernel.ι _
/-- Obtain a morphism from the homology, given a morphism from the kernel. -/
def desc' {W : A} (e : kernel g ⟶ W) (he : kernel.lift g f w ≫ e = 0) :
homology f g w ⟶ W :=
(homology_iso_cokernel_lift _ _ _).hom ≫ cokernel.desc _ e he
/-- Obtain a moprhism to the homology, given a morphism to the kernel. -/
def lift {W : A} (e : W ⟶ cokernel f) (he : e ≫ cokernel.desc f g w = 0) :
W ⟶ homology f g w :=
kernel.lift _ e he ≫ (homology_iso_kernel_desc _ _ _).inv
@[simp, reassoc]
lemma π'_desc' {W : A} (e : kernel g ⟶ W) (he : kernel.lift g f w ≫ e = 0) :
π' f g w ≫ desc' f g w e he = e :=
by { dsimp [π', desc'], simp }
@[simp, reassoc]
lemma lift_ι {W : A} (e : W ⟶ cokernel f) (he : e ≫ cokernel.desc f g w = 0) :
lift f g w e he ≫ ι _ _ _ = e :=
by { dsimp [ι, lift], simp }
@[simp, reassoc]
lemma condition_π' : kernel.lift g f w ≫ π' f g w = 0 :=
by { dsimp [π'], simp }
@[simp, reassoc]
lemma condition_ι : ι f g w ≫ cokernel.desc f g w = 0 :=
by { dsimp [ι], simp }
@[ext]
lemma hom_from_ext {W : A} (a b : homology f g w ⟶ W)
(h : π' f g w ≫ a = π' f g w ≫ b) : a = b :=
begin
dsimp [π'] at h,
apply_fun (λ e, (homology_iso_cokernel_lift f g w).inv ≫ e),
swap,
{ intros i j hh,
apply_fun (λ e, (homology_iso_cokernel_lift f g w).hom ≫ e) at hh,
simpa using hh },
simp only [category.assoc] at h,
exact coequalizer.hom_ext h,
end
@[ext]
lemma hom_to_ext {W : A} (a b : W ⟶ homology f g w)
(h : a ≫ ι f g w = b ≫ ι f g w) : a = b :=
begin
dsimp [ι] at h,
apply_fun (λ e, e ≫ (homology_iso_kernel_desc f g w).hom),
swap,
{ intros i j hh,
apply_fun (λ e, e ≫ (homology_iso_kernel_desc f g w).inv) at hh,
simpa using hh },
simp only [← category.assoc] at h,
exact equalizer.hom_ext h,
end
@[simp, reassoc]
lemma π'_ι : π' f g w ≫ ι f g w = kernel.ι _ ≫ cokernel.π _ :=
by { dsimp [π', ι, homology_iso_kernel_desc], simp }
@[simp, reassoc]
lemma π'_eq_π : (kernel_subobject_iso _).hom ≫ π' f g w = π _ _ _ :=
begin
dsimp [π', homology_iso_cokernel_lift],
simp only [← category.assoc],
rw iso.comp_inv_eq,
dsimp [π, homology_iso_cokernel_image_to_kernel'],
simp,
end
section
variables {X' Y' Z' : A} (f' : X' ⟶ Y') (g' : Y' ⟶ Z') (w' : f' ≫ g' = 0)
@[simp, reassoc]
lemma π'_map (α β h) :
π' _ _ _ ≫ map w w' α β h = kernel.map _ _ α.right β.right (by simp [h,β.w.symm]) ≫ π' _ _ _ :=
begin
apply_fun (λ e, (kernel_subobject_iso _).hom ≫ e),
swap,
{ intros i j hh,
apply_fun (λ e, (kernel_subobject_iso _).inv ≫ e) at hh,
simpa using hh },
dsimp [map],
simp only [π'_eq_π_assoc],
dsimp [π],
simp only [cokernel.π_desc],
rw [← iso.inv_comp_eq, ← category.assoc],
have : (limits.kernel_subobject_iso g).inv ≫ limits.kernel_subobject_map β =
kernel.map _ _ β.left β.right β.w.symm ≫ (kernel_subobject_iso _).inv,
{ rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv],
ext,
dsimp,
simp },
rw this,
simp only [category.assoc],
dsimp [π', homology_iso_cokernel_lift],
simp only [cokernel_iso_of_eq_inv_comp_desc, cokernel.π_desc_assoc],
congr' 1,
{ congr, exact h.symm },
{ rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv],
dsimp [homology_iso_cokernel_image_to_kernel'],
simp }
end
lemma map_eq_desc'_lift_left (α β h) : map w w' α β h =
homology.desc' _ _ _ (homology.lift _ _ _ (kernel.ι _ ≫ β.left ≫ cokernel.π _) (by simp))
(by { ext, simp only [←h, category.assoc, zero_comp, lift_ι, kernel.lift_ι_assoc],
erw ← reassoc_of α.w, simp } ) :=
begin
apply homology.hom_from_ext,
simp only [π'_map, π'_desc'],
dsimp [π', lift],
rw iso.eq_comp_inv,
dsimp [homology_iso_kernel_desc],
ext,
simp [h],
end
lemma map_eq_lift_desc'_left (α β h) : map w w' α β h =
homology.lift _ _ _ (homology.desc' _ _ _ (kernel.ι _ ≫ β.left ≫ cokernel.π _)
(by { simp only [kernel.lift_ι_assoc, ← h], erw ← reassoc_of α.w, simp }))
(by { ext, simp }) :=
by { rw map_eq_desc'_lift_left, ext, simp }
lemma map_eq_desc'_lift_right (α β h) : map w w' α β h =
homology.desc' _ _ _ (homology.lift _ _ _ (kernel.ι _ ≫ α.right ≫ cokernel.π _) (by simp [h]))
(by { ext, simp only [category.assoc, zero_comp, lift_ι, kernel.lift_ι_assoc],
erw ← reassoc_of α.w, simp } ) :=
by { rw map_eq_desc'_lift_left, ext, simp [h] }
lemma map_eq_lift_desc'_right (α β h) : map w w' α β h =
homology.lift _ _ _ (homology.desc' _ _ _ (kernel.ι _ ≫ α.right ≫ cokernel.π _)
(by { simp only [kernel.lift_ι_assoc], erw ← reassoc_of α.w, simp }))
(by { ext, simp [h] }) :=
by { rw map_eq_desc'_lift_right, ext, simp }
@[simp, reassoc]
lemma map_ι (α β h) :
map w w' α β h ≫ ι f' g' w' = ι f g w ≫ cokernel.map f f' α.left β.left (by simp [h, β.w.symm]) :=
begin
rw [map_eq_lift_desc'_left, lift_ι],
ext,
simp only [← category.assoc],
rw [π'_ι, π'_desc', category.assoc, category.assoc, cokernel.π_desc],
end
end
end homology
|
efc1e4bc815c9906a1560efade718766aa6299f4 | 193da933cf42f2f9188bb47e3c973205bc2abc5c | /13.Inductive_Definitions/Data_Types/dm_point.lean | 9e27e5156becbe200c50f7d535159d445dc0cfa8 | [] | no_license | pandaman64/cs-dm | aa4e2621c7a19e2dae911bc237c33e02fcb0c7a3 | bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c | refs/heads/master | 1,647,620,340,607 | 1,570,055,187,000 | 1,570,055,187,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 220 | lean |
structure point(t: Type) := mk :: (x : t) (y : t)
#check point
structure point_nat := mk :: (x : ℕ) (y : ℕ)
#check point_nat
def point_nat' := point ℕ
#check point_nat'
#check point_nat.x
#check point_nat'.x
|
c6702f98c64abd29bf57ed6df5dc34196667e639 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/list/bag_inter_auto.lean | cb3885a199e4154652b3e0e7c098a3225563e41f | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,123 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.list.basic
import Mathlib.PostPort
universes u
namespace Mathlib
namespace list
/- bag_inter -/
@[simp] theorem nil_bag_inter {α : Type u} [DecidableEq α] (l : List α) :
list.bag_inter [] l = [] :=
list.cases_on l (Eq.refl (list.bag_inter [] []))
fun (l_hd : α) (l_tl : List α) => Eq.refl (list.bag_inter [] (l_hd :: l_tl))
@[simp] theorem bag_inter_nil {α : Type u} [DecidableEq α] (l : List α) :
list.bag_inter l [] = [] :=
list.cases_on l (Eq.refl (list.bag_inter [] []))
fun (l_hd : α) (l_tl : List α) => Eq.refl (list.bag_inter (l_hd :: l_tl) [])
@[simp] theorem cons_bag_inter_of_pos {α : Type u} [DecidableEq α] {a : α} (l₁ : List α)
{l₂ : List α} (h : a ∈ l₂) :
list.bag_inter (a :: l₁) l₂ = a :: list.bag_inter l₁ (list.erase l₂ a) :=
list.cases_on l₂ (fun (h : a ∈ []) => if_pos h)
(fun (l₂_hd : α) (l₂_tl : List α) (h : a ∈ l₂_hd :: l₂_tl) => if_pos h) h
@[simp] theorem cons_bag_inter_of_neg {α : Type u} [DecidableEq α] {a : α} (l₁ : List α)
{l₂ : List α} (h : ¬a ∈ l₂) : list.bag_inter (a :: l₁) l₂ = list.bag_inter l₁ l₂ :=
sorry
@[simp] theorem mem_bag_inter {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} {l₂ : List α} :
a ∈ list.bag_inter l₁ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ :=
sorry
@[simp] theorem count_bag_inter {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} {l₂ : List α} :
count a (list.bag_inter l₁ l₂) = min (count a l₁) (count a l₂) :=
sorry
theorem bag_inter_sublist_left {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) :
list.bag_inter l₁ l₂ <+ l₁ :=
sorry
theorem bag_inter_nil_iff_inter_nil {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) :
list.bag_inter l₁ l₂ = [] ↔ l₁ ∩ l₂ = [] :=
sorry
end Mathlib |
2fd015f9073e5bdf3f1653228719ca20911b2828 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Init/Data/Array/DecidableEq.lean | fb6c1f50bccc60a57b3d1d372c98201e2dcd1456 | [
"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,913 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.Array.Basic
import Init.Classical
namespace Array
theorem eq_of_isEqvAux [DecidableEq α] (a b : Array α) (hsz : a.size = b.size) (i : Nat) (hi : i ≤ a.size) (heqv : Array.isEqvAux a b hsz (fun x y => x = y) i) (j : Nat) (low : i ≤ j) (high : j < a.size) : a[j] = b[j]'(hsz ▸ high) := by
by_cases h : i < a.size
· unfold Array.isEqvAux at heqv
simp [h] at heqv
have hind := eq_of_isEqvAux a b hsz (i+1) (Nat.succ_le_of_lt h) heqv.2
by_cases heq : i = j
· subst heq; exact heqv.1
· exact hind j (Nat.succ_le_of_lt (Nat.lt_of_le_of_ne low heq)) high
· have heq : i = a.size := Nat.le_antisymm hi (Nat.ge_of_not_lt h)
subst heq
exact absurd (Nat.lt_of_lt_of_le high low) (Nat.lt_irrefl j)
termination_by _ => a.size - i
theorem eq_of_isEqv [DecidableEq α] (a b : Array α) : Array.isEqv a b (fun x y => x = y) → a = b := by
simp [Array.isEqv]
split
case inr => intro; contradiction
case inl hsz =>
intro h
have aux := eq_of_isEqvAux a b hsz 0 (Nat.zero_le ..) h
exact ext a b hsz fun i h _ => aux i (Nat.zero_le ..) _
theorem isEqvAux_self [DecidableEq α] (a : Array α) (i : Nat) : Array.isEqvAux a a rfl (fun x y => x = y) i = true := by
unfold Array.isEqvAux
split
case inl h => simp [h, isEqvAux_self a (i+1)]
case inr h => simp [h]
termination_by _ => a.size - i
theorem isEqv_self [DecidableEq α] (a : Array α) : Array.isEqv a a (fun x y => x = y) = true := by
simp [isEqv, isEqvAux_self]
instance [DecidableEq α] : DecidableEq (Array α) :=
fun a b =>
match h:isEqv a b (fun a b => a = b) with
| true => isTrue (eq_of_isEqv a b h)
| false => isFalse fun h' => by subst h'; rw [isEqv_self] at h; contradiction
end Array
|
fc1445974cb444ce31462308c74c5385e4eeb8b2 | 3984ab8555ab1e1084e22ef652544acdfc231f27 | /src/v0.1/dump/drbmap.lean | 1bdd10269dbf1b0da9fc4c09d49d190540d4cefd | [] | no_license | mrakgr/CFR-in-Lean | a35c7a478795cc794cc0caff3199cf28c8ee5448 | 720a3260297bcc158e08833d38964450dcaad2eb | refs/heads/master | 1,598,515,917,940 | 1,572,612,355,000 | 1,572,612,355,000 | 217,296,108 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,346 | lean | -- As it turns out, these red black trees not being able to understand that the key
-- being put in is the same as the one being taken out when doing indexing into them
-- is a lot bigger problem than I thought at first. With dependent types, that means
-- that it can no longer reduce based on the key and it is causing me all sorts of
-- issues downstream.
-- These trees are useless as they are now. I'd recommend avoiding them in favor of
-- something that uses direct equality.
-- Note, look into the previous commit for a completed version.
-- I'll leave this in a half baked state for the time being as I do not
-- know how to rewrite this to use trichotomnous comparison.
-- In particular, insertion requires a well foundedness proof related to the
-- standard comparison and I do not feel like spending time in order to thoroughly
-- get familiar with RB trees just so I can redesign them properly.
prelude
import init.data.rbtree
import init.data.rbtree.basic tactic.library_search
universes u v w
def drbmap_lt {α : Type u} {β : α → Type v} (lt : α → α → Prop) (a b : Σ α, β α) : Prop :=
lt a.1 b.1
set_option auto_param.check_exists false
def drbmap (α : Type u) [lt : has_lt α] [is_trichotomous α lt.lt] (β : α → Type v) : Type (max u v) :=
rbtree (Σ α, β α) (drbmap_lt lt.lt)
def mk_drbmap (α : Type u) [lt : has_lt α] [is_trichotomous α lt.lt] (β : α → Type v) : drbmap α β :=
mk_rbtree (Σ α, β α) (drbmap_lt lt.lt)
namespace drbmap
def empty {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(m : drbmap α β) : bool :=
m.empty
def to_list {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(m : drbmap α β) : list (Σ α, β α) :=
m.to_list
def min {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(m : drbmap α β) : option (Σ α, β α) :=
m.min
def max {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(m : drbmap α β) : option (Σ α, β α) :=
m.max
def fold {α δ : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(f : ∀ (a : α), β a → δ → δ) (m : drbmap α β) (d : δ) : δ :=
m.fold (λ e, f e.1 e.2) d
def rev_fold {α δ : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(f : ∀ (a : α), β a → δ → δ) (m : drbmap α β) (d : δ) : δ :=
m.rev_fold (λ e, f e.1 e.2) d
private def mem' {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(a : α) : ∀ (m : rbnode (Σ α, β α)), Prop
| rbnode.leaf := false
| (rbnode.red_node l ⟨ v, _ ⟩ r) := a < v ∨ a = v ∨ a > v
| (rbnode.black_node l ⟨ v, _ ⟩ r) := a < v ∨ a = v ∨ a > v
def mem {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(k : α) (m : drbmap α β) : Prop := mem' k m.val
instance {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt] : has_mem α (drbmap α β) := ⟨mem⟩
instance {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt] [has_repr (Σ α, β α)] : has_repr (drbmap α β) :=
⟨λ t, "drbmap_of " ++ repr t.to_list⟩
def insert {α : Type u} {β : α → Type v} [lt : has_lt α] [is_trichotomous α lt.lt]
(m : drbmap α β lt) (k : α) (v : β k) : drbmap α β lt :=
@rbtree.insert _ _ drbmap_lt_dec m ⟨ k, v ⟩
private def find (k : α) : rbnode (Σ α, β α) → option (Σ α, β α)
| rbnode.leaf := none
| (rbnode.red_node a k' b) :=
if lt k k'.1 then find a
else if lt k'.1 k then find b
else k'
| (rbnode.black_node a k' b) :=
if lt k k'.1 then find a
else if lt k'.1 k then find b
else k'
def find_entry (m : drbmap α β lt) (k : α) : option (Σ α, β α) := @find _ _ lt _ k m.val
def contains (m : drbmap α β lt) (k : α) : bool :=
(find_entry m k).is_some
def from_list (l : list (Σ α, β α)) (lt : α → α → Prop . rbtree.default_lt) [decidable_rel lt] : drbmap α β lt :=
l.foldl (λ m p, insert m p.1 p.2) (mk_drbmap α β lt)
end drbmap
def drbmap_of {α : Type u} {β : α → Type v} (l : list (Σ α, β α)) (lt : α → α → Prop . rbtree.default_lt) [decidable_rel lt] : drbmap α β lt :=
drbmap.from_list l lt |
68349c1c23f04dbf5b37ba6d5923a414ccc70bd1 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/nat/choose/vandermonde.lean | b9c0db02e314728d711a58db932e5e56c0707c6a | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 1,129 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.polynomial.coeff
import data.nat.choose.basic
/-!
# Vandermonde's identity
In this file we prove Vandermonde's identity (`nat.add_choose_eq`):
`(m + n).choose k = ∑ (ij : ℕ × ℕ) in antidiagonal k, m.choose ij.1 * n.choose ij.2`
We follow the algebraic proof from
https://en.wikipedia.org/wiki/Vandermonde%27s_identity#Algebraic_proof .
-/
open_locale big_operators
open polynomial finset.nat
/-- Vandermonde's identity -/
lemma nat.add_choose_eq (m n k : ℕ) :
(m + n).choose k = ∑ (ij : ℕ × ℕ) in antidiagonal k, m.choose ij.1 * n.choose ij.2 :=
begin
calc (m + n).choose k
= ((X + 1) ^ (m + n)).coeff k : _
... = ((X + 1) ^ m * (X + 1) ^ n).coeff k : by rw pow_add
... = ∑ (ij : ℕ × ℕ) in antidiagonal k, m.choose ij.1 * n.choose ij.2 : _,
{ rw [coeff_X_add_one_pow, nat.cast_id], },
{ rw [coeff_mul, finset.sum_congr rfl],
simp only [coeff_X_add_one_pow, nat.cast_id, eq_self_iff_true, imp_true_iff], }
end
|
a173bf4896d4ea79bc0dacf4739d97e6adaa0820 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/topology/compact_open.lean | 4b1eb2d35784369b74864010b8d6dfa3de82caf1 | [
"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 | 4,069 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton
Type of continuous maps and the compact-open topology on them.
-/
import topology.constructions tactic.tidy
open set
universes u v w
def continuous_map (α : Type u) (β : Type v) [topological_space α] [topological_space β] :
Type (max u v) :=
subtype (continuous : (α → β) → Prop)
local notation `C(` α `, ` β `)` := continuous_map α β
namespace continuous_map
section compact_open
variables {α : Type u} {β : Type v} {γ : Type w}
variables [topological_space α] [topological_space β] [topological_space γ]
instance : has_coe_to_fun C(α, β) :=
⟨λ_, α → β, λf, f.1⟩
def compact_open.gen (s : set α) (u : set β) : set C(α,β) := {f | f '' s ⊆ u}
-- The compact-open topology on the space of continuous maps α → β.
instance compact_open : topological_space C(α, β) :=
topological_space.generate_from
{m | ∃ (s : set α) (hs : compact s) (u : set β) (hu : is_open u), m = compact_open.gen s u}
private lemma is_open_gen {s : set α} (hs : compact s) {u : set β} (hu : is_open u) :
is_open (compact_open.gen s u) :=
topological_space.generate_open.basic _ (by dsimp [mem_set_of_eq]; tauto)
section functorial
variables {g : β → γ} (hg : continuous g)
def induced (f : C(α, β)) : C(α, γ) := ⟨g ∘ f, hg.comp f.property⟩
private lemma preimage_gen {s : set α} (hs : compact s) {u : set γ} (hu : is_open u) :
continuous_map.induced hg ⁻¹' (compact_open.gen s u) = compact_open.gen s (g ⁻¹' u) :=
begin
ext ⟨f, _⟩,
change g ∘ f '' s ⊆ u ↔ f '' s ⊆ g ⁻¹' u,
rw [image_comp, image_subset_iff]
end
/-- C(α, -) is a functor. -/
lemma continuous_induced : continuous (continuous_map.induced hg : C(α, β) → C(α, γ)) :=
continuous_generated_from $ assume m ⟨s, hs, u, hu, hm⟩,
by rw [hm, preimage_gen hg hs hu]; exact is_open_gen hs (hg _ hu)
end functorial
section ev
variables (α β)
def ev (p : C(α, β) × α) : β := p.1 p.2
variables {α β}
-- The evaluation map C(α, β) × α → β is continuous if α is locally compact.
lemma continuous_ev [locally_compact_space α] : continuous (ev α β) :=
continuous_iff_continuous_at.mpr $ assume ⟨f, x⟩ n hn,
let ⟨v, vn, vo, fxv⟩ := mem_nhds_sets_iff.mp hn in
have v ∈ nhds (f.val x), from mem_nhds_sets vo fxv,
let ⟨s, hs, sv, sc⟩ :=
locally_compact_space.local_compact_nhds x (f.val ⁻¹' v)
(f.property.tendsto x this) in
let ⟨u, us, uo, xu⟩ := mem_nhds_sets_iff.mp hs in
show (ev α β) ⁻¹' n ∈ nhds (f, x), from
let w := set.prod (compact_open.gen s v) u in
have w ⊆ ev α β ⁻¹' n, from assume ⟨f', x'⟩ ⟨hf', hx'⟩, calc
f'.val x' ∈ f'.val '' s : mem_image_of_mem f'.val (us hx')
... ⊆ v : hf'
... ⊆ n : vn,
have is_open w, from is_open_prod (is_open_gen sc vo) uo,
have (f, x) ∈ w, from ⟨image_subset_iff.mpr sv, xu⟩,
mem_nhds_sets_iff.mpr ⟨w, by assumption, by assumption, by assumption⟩
end ev
section coev
variables (α β)
def coev (b : β) : C(α, β × α) := ⟨λ a, (b, a), continuous.prod_mk continuous_const continuous_id⟩
variables {α β}
lemma image_coev {y : β} (s : set α) : (coev α β y).val '' s = set.prod {y} s := by tidy
-- The coevaluation map β → C(α, β × α) is continuous (always).
lemma continuous_coev : continuous (coev α β) :=
continuous_generated_from $ begin
rintros _ ⟨s, sc, u, uo, rfl⟩,
rw is_open_iff_forall_mem_open,
intros y hy,
change (coev α β y).val '' s ⊆ u at hy,
rw image_coev s at hy,
rcases generalized_tube_lemma compact_singleton sc uo hy
with ⟨v, w, vo, wo, yv, sw, vwu⟩,
refine ⟨v, _, vo, singleton_subset_iff.mp yv⟩,
intros y' hy',
change (coev α β y').val '' s ⊆ u,
rw image_coev s,
exact subset.trans (prod_mono (singleton_subset_iff.mpr hy') sw) vwu
end
end coev
end compact_open
end continuous_map
|
27168f3c6f473dc1d5f2ba5db9008386735ca2cf | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/pred_logic/unnamed_338.lean | cba2cf238e1643e6ec3f1428ff808f8068548a73 | [] | 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 | 411 | lean | variables (U : Type*) (S T : U → Prop) (u : U)
-- BEGIN
example (h₁ : ∀ x, S x) (h₂ : ∀ y, S y → T y) (u : U) : T u :=
begin
specialize h₁ u, -- We have `h₁ : S u` by for all elim. on `h₁` and `u`.
specialize h₂ u, -- We have `h₂ : S u → T u` by for all elim. on `h₂` and `u`
show T u, from h₂ h₁, -- We show `T u` by implication elimination on `h₂` and `h₁`.
end
-- END |
aa9fdf47733aa2704d73be905db767091a8c9b00 | ec62863c729b7eedee77b86d974f2c529fa79d25 | /4/b.lean | 186ad17b174acf497187a732cd6d55ee909a0950 | [] | no_license | rwbarton/advent-of-lean-4 | 2ac9b17ba708f66051e3d8cd694b0249bc433b65 | 417c7e2718253ba7148c0279fcb251b6fc291477 | refs/heads/main | 1,675,917,092,057 | 1,609,864,581,000 | 1,609,864,581,000 | 317,700,289 | 24 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,871 | lean | import Lean.Data.Json.Parser
open Lean Lean.Quickparse Lean.Json.Parser
def qguard (p : Bool) : Quickparse Unit :=
if p then return () else fail "bad"
def byr : Quickparse Unit := do
expect "byr:"
let ⟨n, d⟩ ← natNumDigits
qguard $ d == 4 && 1920 ≤ n && n ≤ 2002
def iyr : Quickparse Unit := do
expect "iyr:"
let ⟨n, d⟩ ← natNumDigits
qguard $ d == 4 && 2010 ≤ n && n ≤ 2020
def eyr : Quickparse Unit := do
expect "eyr:"
let ⟨n, d⟩ ← natNumDigits
qguard $ d == 4 && 2020 ≤ n && n ≤ 2030
def hgt : Quickparse Unit := do
expect "hgt:"
let ⟨n, d⟩ ← natNumDigits
let c ← peek!
if c == 'c' then do
qguard $ 150 ≤ n && n ≤ 193
expect "cm"
else if c == 'i' then do
qguard $ 59 ≤ n && n ≤ 76
expect "in"
else fail ""
def hcl : Quickparse Unit := do
expect "hcl:#"
let hex1 : Quickparse Unit := do
let c ← next
qguard $ '0' ≤ c && c ≤ '9' || 'a' ≤ c && c ≤ 'f'
for i in [:6] do hex1
def ecl : Quickparse Unit := do
expect "ecl:"
let mut acc := ""
for i in [:3] do
acc := acc.push (← next)
qguard $ List.elem acc ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
def pid : Quickparse Unit := do
expect "pid:"
for i in [:9] do
let c ← next
qguard c.isDigit
def testParse (p : Quickparse Unit) (f : String) : Bool :=
match (p.bind (λ _ => eoi)) f.mkIterator with
| Result.success _ _ => True
| Result.error _ _ => False
def isValid (para : String) : Bool :=
let fieldStrs : List String := para.split (λ c => c == ' ' || c == '\n');
List.all [byr, iyr, eyr, hgt, hcl, ecl, pid] $ λ p =>
List.any fieldStrs $ λ f =>
testParse p f
def solve (input : String) : Int :=
let paras := input.splitOn "\n\n"
(paras.filter isValid).length
def main : IO Unit := do
let input ← IO.FS.readFile "a.in"
IO.print s!"{solve input}\n"
|
e8a1c08d7bbf38d1b5a171aef4deecba32713959 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/pattern_hint1.lean | 441c46085d146067bcd0960bc01e7c608ca31cd2 | [
"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 | 295 | lean | constants f g : nat → Prop
definition foo₁ [forward] : ∀ x, f x ∧ g x := sorry
definition foo₂ [forward] : ∀ x, (: f x :) ∧ g x := sorry
definition foo₃ [forward] : ∀ x, (: f (id x) :) ∧ g x := sorry
print foo₁
print foo₂
print foo₃ -- id is unfolded
|
592cbd9701c5634c258354d19065f15a9264b513 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/probability/probability_mass_function/monad.lean | 2110791e1bd61223fe2fad15f176ea322281f15e | [
"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,281 | lean | /-
Copyright (c) 2020 Devon Tuma. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Devon Tuma
-/
import probability.probability_mass_function.basic
/-!
# Monad Operations for Probability Mass Functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file constructs two operations on `pmf` that give it a monad structure.
`pure a` is the distribution where a single value `a` has probability `1`.
`bind pa pb : pmf β` is the distribution given by sampling `a : α` from `pa : pmf α`,
and then sampling from `pb a : pmf β` to get a final result `b : β`.
`bind_on_support` generalizes `bind` to allow binding to a partial function,
so that the second argument only needs to be defined on the support of the first argument.
-/
noncomputable theory
variables {α β γ : Type*}
open_locale classical big_operators nnreal ennreal
open measure_theory
namespace pmf
section pure
/-- The pure `pmf` is the `pmf` where all the mass lies in one point.
The value of `pure a` is `1` at `a` and `0` elsewhere. -/
def pure (a : α) : pmf α := ⟨λ a', if a' = a then 1 else 0, has_sum_ite_eq _ _⟩
variables (a a' : α)
@[simp] lemma pure_apply : pure a a' = (if a' = a then 1 else 0) := rfl
@[simp] lemma support_pure : (pure a).support = {a} := set.ext (λ a', by simp [mem_support_iff])
lemma mem_support_pure_iff: a' ∈ (pure a).support ↔ a' = a := by simp
@[simp] lemma pure_apply_self : pure a a = 1 := if_pos rfl
lemma pure_apply_of_ne (h : a' ≠ a) : pure a a' = 0 := if_neg h
instance [inhabited α] : inhabited (pmf α) := ⟨pure default⟩
section measure
variable (s : set α)
@[simp] lemma to_outer_measure_pure_apply : (pure a).to_outer_measure s = if a ∈ s then 1 else 0 :=
begin
refine (to_outer_measure_apply (pure a) s).trans _,
split_ifs with ha ha,
{ refine ((tsum_congr (λ b, _)).trans (tsum_ite_eq a 1)),
exact ite_eq_left_iff.2 (λ hb, symm (ite_eq_right_iff.2 (λ h, (hb $ h.symm ▸ ha).elim))) },
{ refine ((tsum_congr (λ b, _)).trans (tsum_zero)),
exact ite_eq_right_iff.2 (λ hb, ite_eq_right_iff.2 (λ h, (ha $ h ▸ hb).elim)) }
end
variable [measurable_space α]
/-- The measure of a set under `pure a` is `1` for sets containing `a` and `0` otherwise -/
@[simp] lemma to_measure_pure_apply (hs : measurable_set s) :
(pure a).to_measure s = if a ∈ s then 1 else 0 :=
(to_measure_apply_eq_to_outer_measure_apply (pure a) s hs).trans (to_outer_measure_pure_apply a s)
lemma to_measure_pure : (pure a).to_measure = measure.dirac a :=
measure.ext (λ s hs, by simpa only [to_measure_pure_apply a s hs, measure.dirac_apply' a hs])
@[simp] lemma to_pmf_dirac [countable α] [h : measurable_singleton_class α] :
(measure.dirac a).to_pmf = pure a :=
by rw [to_pmf_eq_iff_to_measure_eq, to_measure_pure]
end measure
end pure
section bind
/-- The monadic bind operation for `pmf`. -/
def bind (p : pmf α) (f : α → pmf β) : pmf β :=
⟨λ b, ∑' a, p a * f a b, ennreal.summable.has_sum_iff.2 (ennreal.tsum_comm.trans $
by simp only [ennreal.tsum_mul_left, tsum_coe, mul_one])⟩
variables (p : pmf α) (f : α → pmf β) (g : β → pmf γ)
@[simp] lemma bind_apply (b : β) : p.bind f b = ∑'a, p a * f a b := rfl
@[simp] lemma support_bind : (p.bind f).support = ⋃ a ∈ p.support, (f a).support :=
set.ext (λ b, by simp [mem_support_iff, ennreal.tsum_eq_zero, not_or_distrib])
lemma mem_support_bind_iff (b : β) : b ∈ (p.bind f).support ↔ ∃ a ∈ p.support, b ∈ (f a).support :=
by simp only [support_bind, set.mem_Union, set.mem_set_of_eq]
@[simp] lemma pure_bind (a : α) (f : α → pmf β) : (pure a).bind f = f a :=
have ∀ b a', ite (a' = a) 1 0 * f a' b = ite (a' = a) (f a b) 0, from
assume b a', by split_ifs; simp; subst h; simp,
by ext b; simp [this]
@[simp] lemma bind_pure : p.bind pure = p :=
pmf.ext (λ x, (bind_apply _ _ _).trans (trans (tsum_eq_single x $
(λ y hy, by rw [pure_apply_of_ne _ _ hy.symm, mul_zero])) $ by rw [pure_apply_self, mul_one]))
@[simp] lemma bind_const (p : pmf α) (q : pmf β) : p.bind (λ _, q) = q :=
pmf.ext (λ x, by rw [bind_apply, ennreal.tsum_mul_right, tsum_coe, one_mul])
@[simp] lemma bind_bind : (p.bind f).bind g = p.bind (λ a, (f a).bind g) :=
pmf.ext (λ b, by simpa only [ennreal.coe_eq_coe.symm, bind_apply, ennreal.tsum_mul_left.symm,
ennreal.tsum_mul_right.symm, mul_assoc, mul_left_comm, mul_comm] using ennreal.tsum_comm)
lemma bind_comm (p : pmf α) (q : pmf β) (f : α → β → pmf γ) :
p.bind (λ a, q.bind (f a)) = q.bind (λ b, p.bind (λ a, f a b)) :=
pmf.ext (λ b, by simpa only [ennreal.coe_eq_coe.symm, bind_apply, ennreal.tsum_mul_left.symm,
ennreal.tsum_mul_right.symm, mul_assoc, mul_left_comm, mul_comm] using ennreal.tsum_comm)
section measure
variable (s : set β)
@[simp] lemma to_outer_measure_bind_apply :
(p.bind f).to_outer_measure s = ∑' a, p a * (f a).to_outer_measure s :=
calc (p.bind f).to_outer_measure s
= ∑' b, if b ∈ s then ∑' a, p a * f a b else 0 :
by simp [to_outer_measure_apply, set.indicator_apply]
... = ∑' b a, p a * (if b ∈ s then f a b else 0) :
tsum_congr (λ b, by split_ifs; simp)
... = ∑' a b, p a * (if b ∈ s then f a b else 0) :
tsum_comm' ennreal.summable (λ _, ennreal.summable) (λ _, ennreal.summable)
... = ∑' a, p a * ∑' b, (if b ∈ s then f a b else 0) :
tsum_congr (λ a, ennreal.tsum_mul_left)
... = ∑' a, p a * ∑' b, if b ∈ s then f a b else 0 :
tsum_congr (λ a, congr_arg (λ x, (p a) * x) $ tsum_congr (λ b, by split_ifs; refl))
... = ∑' a, p a * (f a).to_outer_measure s :
tsum_congr (λ a, by simp only [to_outer_measure_apply, set.indicator_apply])
/-- The measure of a set under `p.bind f` is the sum over `a : α`
of the probability of `a` under `p` times the measure of the set under `f a` -/
@[simp] lemma to_measure_bind_apply [measurable_space β] (hs : measurable_set s) :
(p.bind f).to_measure s = ∑' a, p a * (f a).to_measure s :=
(to_measure_apply_eq_to_outer_measure_apply (p.bind f) s hs).trans
((to_outer_measure_bind_apply p f s).trans (tsum_congr (λ a, congr_arg (λ x, p a * x)
(to_measure_apply_eq_to_outer_measure_apply (f a) s hs).symm)))
end measure
end bind
instance : monad pmf :=
{ pure := λ A a, pure a,
bind := λ A B pa pb, pa.bind pb }
section bind_on_support
/-- Generalized version of `bind` allowing `f` to only be defined on the support of `p`.
`p.bind f` is equivalent to `p.bind_on_support (λ a _, f a)`, see `bind_on_support_eq_bind` -/
def bind_on_support (p : pmf α) (f : Π a ∈ p.support, pmf β) : pmf β :=
⟨λ b, ∑' a, p a * if h : p a = 0 then 0 else f a h b,
ennreal.summable.has_sum_iff.2 begin
refine (ennreal.tsum_comm.trans (trans (tsum_congr $ λ a, _) p.tsum_coe)),
simp_rw [ennreal.tsum_mul_left],
split_ifs with h,
{ simp only [h, zero_mul] },
{ rw [(f a h).tsum_coe, mul_one] }
end⟩
variables {p : pmf α} (f : Π a ∈ p.support, pmf β)
@[simp] lemma bind_on_support_apply (b : β) :
p.bind_on_support f b = ∑' a, p a * if h : p a = 0 then 0 else f a h b := rfl
@[simp] lemma support_bind_on_support :
(p.bind_on_support f).support = ⋃ (a : α) (h : a ∈ p.support), (f a h).support :=
begin
refine set.ext (λ b, _),
simp only [ennreal.tsum_eq_zero, not_or_distrib, mem_support_iff,
bind_on_support_apply, ne.def, not_forall, mul_eq_zero, set.mem_Union],
exact ⟨λ hb, let ⟨a, ⟨ha, ha'⟩⟩ := hb in ⟨a, ha, by simpa [ha] using ha'⟩,
λ hb, let ⟨a, ha, ha'⟩ := hb in ⟨a, ⟨ha, by simpa [(mem_support_iff _ a).1 ha] using ha'⟩⟩⟩
end
lemma mem_support_bind_on_support_iff (b : β) :
b ∈ (p.bind_on_support f).support ↔ ∃ (a : α) (h : a ∈ p.support), b ∈ (f a h).support :=
by simp only [support_bind_on_support, set.mem_set_of_eq, set.mem_Union]
/-- `bind_on_support` reduces to `bind` if `f` doesn't depend on the additional hypothesis -/
@[simp] lemma bind_on_support_eq_bind (p : pmf α) (f : α → pmf β) :
p.bind_on_support (λ a _, f a) = p.bind f :=
begin
ext b x,
have : ∀ a, ite (p a = 0) 0 (p a * f a b) = p a * f a b,
from λ a, ite_eq_right_iff.2 (λ h, h.symm ▸ symm (zero_mul $ f a b)),
simp only [bind_on_support_apply (λ a _, f a), p.bind_apply f,
dite_eq_ite, mul_ite, mul_zero, this],
end
lemma bind_on_support_eq_zero_iff (b : β) :
p.bind_on_support f b = 0 ↔ ∀ a (ha : p a ≠ 0), f a ha b = 0 :=
begin
simp only [bind_on_support_apply, ennreal.tsum_eq_zero, mul_eq_zero, or_iff_not_imp_left],
exact ⟨λ h a ha, trans (dif_neg ha).symm (h a ha), λ h a ha, trans (dif_neg ha) (h a ha)⟩,
end
@[simp] lemma pure_bind_on_support (a : α) (f : Π (a' : α) (ha : a' ∈ (pure a).support), pmf β) :
(pure a).bind_on_support f = f a ((mem_support_pure_iff a a).mpr rfl) :=
begin
refine pmf.ext (λ b, _),
simp only [bind_on_support_apply, pure_apply],
refine trans (tsum_congr (λ a', _)) (tsum_ite_eq a _),
by_cases h : (a' = a); simp [h],
end
lemma bind_on_support_pure (p : pmf α) :
p.bind_on_support (λ a _, pure a) = p :=
by simp only [pmf.bind_pure, pmf.bind_on_support_eq_bind]
@[simp] lemma bind_on_support_bind_on_support (p : pmf α)
(f : ∀ a ∈ p.support, pmf β)
(g : ∀ (b ∈ (p.bind_on_support f).support), pmf γ) :
(p.bind_on_support f).bind_on_support g =
p.bind_on_support (λ a ha, (f a ha).bind_on_support
(λ b hb, g b ((mem_support_bind_on_support_iff f b).mpr ⟨a, ha, hb⟩))) :=
begin
refine pmf.ext (λ a, _),
simp only [ennreal.coe_eq_coe.symm, bind_on_support_apply, ← tsum_dite_right,
ennreal.tsum_mul_left.symm, ennreal.tsum_mul_right.symm],
simp only [ennreal.tsum_eq_zero, ennreal.coe_eq_coe, ennreal.coe_eq_zero, ennreal.coe_zero,
dite_eq_left_iff, mul_eq_zero],
refine ennreal.tsum_comm.trans (tsum_congr (λ a', tsum_congr (λ b, _))),
split_ifs,
any_goals { ring1 },
{ have := h_1 a', simp [h] at this, contradiction },
{ simp [h_2], },
end
lemma bind_on_support_comm (p : pmf α) (q : pmf β)
(f : ∀ (a ∈ p.support) (b ∈ q.support), pmf γ) :
p.bind_on_support (λ a ha, q.bind_on_support (f a ha)) =
q.bind_on_support (λ b hb, p.bind_on_support (λ a ha, f a ha b hb)) :=
begin
apply pmf.ext, rintro c,
simp only [ennreal.coe_eq_coe.symm, bind_on_support_apply, ← tsum_dite_right,
ennreal.tsum_mul_left.symm, ennreal.tsum_mul_right.symm],
refine trans (ennreal.tsum_comm) (tsum_congr (λ b, tsum_congr (λ a, _))),
split_ifs with h1 h2 h2; ring,
end
section measure
variable (s : set β)
@[simp] lemma to_outer_measure_bind_on_support_apply : (p.bind_on_support f).to_outer_measure s
= ∑' a, p a * if h : p a = 0 then 0 else (f a h).to_outer_measure s :=
begin
simp only [to_outer_measure_apply, set.indicator_apply, bind_on_support_apply],
calc ∑' b, ite (b ∈ s) (∑' a, p a * dite (p a = 0) (λ h, 0) (λ h, f a h b)) 0
= ∑' b a, ite (b ∈ s) (p a * dite (p a = 0) (λ h, 0) (λ h, f a h b)) 0 :
tsum_congr (λ b, by split_ifs with hbs; simp only [eq_self_iff_true, tsum_zero])
... = ∑' a b, ite (b ∈ s) (p a * dite (p a = 0) (λ h, 0) (λ h, f a h b)) 0 : ennreal.tsum_comm
... = ∑' a, p a * ∑' b, ite (b ∈ s) (dite (p a = 0) (λ h, 0) (λ h, f a h b)) 0 :
tsum_congr (λ a, by simp only [← ennreal.tsum_mul_left, mul_ite, mul_zero])
... = ∑' a, p a * dite (p a = 0) (λ h, 0) (λ h, ∑' b, ite (b ∈ s) (f a h b) 0) :
tsum_congr (λ a, by split_ifs with ha; simp only [if_t_t, tsum_zero, eq_self_iff_true])
end
/-- The measure of a set under `p.bind_on_support f` is the sum over `a : α`
of the probability of `a` under `p` times the measure of the set under `f a _`.
The additional if statement is needed since `f` is only a partial function -/
@[simp] lemma to_measure_bind_on_support_apply [measurable_space β] (hs : measurable_set s) :
(p.bind_on_support f).to_measure s
= ∑' a, p a * if h : p a = 0 then 0 else (f a h).to_measure s :=
by simp only [to_measure_apply_eq_to_outer_measure_apply _ _ hs,
to_outer_measure_bind_on_support_apply]
end measure
end bind_on_support
end pmf
|
0ec5178ccd366666bbb684e23b46bb2812c1a4ba | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/finset/powerset.lean | 3f5243bef484aa2b78cee0d829788801f6bfc80e | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 10,707 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.finset.lattice
/-!
# The powerset of a finset
-/
namespace finset
open multiset
variables {α : Type*}
/-! ### powerset -/
section powerset
/-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/
def powerset (s : finset α) : finset (finset α) :=
⟨s.1.powerset.pmap finset.mk
(λ t h, nodup_of_le (mem_powerset.1 h) s.2),
nodup_pmap (λ a ha b hb, congr_arg finset.val)
(nodup_powerset.2 s.2)⟩
@[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t :=
by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right];
rw ← val_le_iff
@[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s :=
mem_powerset.2 (empty_subset _)
@[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s :=
mem_powerset.2 (subset.refl _)
@[simp] lemma powerset_empty : finset.powerset (∅ : finset α) = {∅} := rfl
@[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t :=
⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _),
λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩
/-- **Number of Subsets of a Set** -/
@[simp] theorem card_powerset (s : finset α) :
card (powerset s) = 2 ^ card s :=
(card_pmap _ _ _).trans (card_powerset s.1)
lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α}
(ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t :=
by { apply mt _ h, apply mem_powerset.1 ht }
lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) :
powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) :=
begin
ext t,
simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff],
by_cases h : a ∈ t,
{ split,
{ exact λH, or.inr ⟨_, H, insert_erase h⟩ },
{ intros H,
cases H,
{ exact subset.trans (erase_subset a t) H },
{ rcases H with ⟨u, hu⟩,
rw ← hu.2,
exact subset.trans (erase_insert_subset a u) hu.1 } } },
{ have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t,
by simp [ne.symm (ne_insert_of_not_mem _ _ h)],
simp [finset.erase_eq_of_not_mem h, this] }
end
/-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for any subset. -/
instance decidable_exists_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop}
[Π t (h : t ⊆ s), decidable (p t h)] : decidable (∃ t (h : t ⊆ s), p t h) :=
decidable_of_iff (∃ t (hs : t ∈ s.powerset), p t (mem_powerset.1 hs))
⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_powerset.2 hs, hp⟩)⟩
/-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for every subset. -/
instance decidable_forall_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop}
[Π t (h : t ⊆ s), decidable (p t h)] : decidable (∀ t (h : t ⊆ s), p t h) :=
decidable_of_iff (∀ t (h : t ∈ s.powerset), p t (mem_powerset.1 h))
⟨(λ h t hs, h t (mem_powerset.2 hs)), (λ h _ _, h _ _)⟩
/-- A version of `finset.decidable_exists_of_decidable_subsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_exists_of_decidable_subsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∃ t (h : t ⊆ s), p t) :=
@finset.decidable_exists_of_decidable_subsets _ _ _ hu
/-- A version of `finset.decidable_forall_of_decidable_subsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_forall_of_decidable_subsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∀ t (h : t ⊆ s), p t) :=
@finset.decidable_forall_of_decidable_subsets _ _ _ hu
end powerset
section ssubsets
variables [decidable_eq α]
/-- For `s` a finset, `s.ssubsets` is the finset comprising strict subsets of `s`. -/
def ssubsets (s : finset α) : finset (finset α) :=
erase (powerset s) s
@[simp] lemma mem_ssubsets {s t : finset α} : t ∈ s.ssubsets ↔ t ⊂ s :=
by rw [ssubsets, mem_erase, mem_powerset, ssubset_iff_subset_ne, and.comm]
lemma empty_mem_ssubsets {s : finset α} (h : s.nonempty) : ∅ ∈ s.ssubsets :=
by { rw [mem_ssubsets, ssubset_iff_subset_ne], exact ⟨empty_subset s, h.ne_empty.symm⟩, }
/-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for any ssubset. -/
instance decidable_exists_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop}
[Π t (h : t ⊂ s), decidable (p t h)] : decidable (∃ t h, p t h) :=
decidable_of_iff (∃ t (hs : t ∈ s.ssubsets), p t (mem_ssubsets.1 hs))
⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_ssubsets.2 hs, hp⟩)⟩
/-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for every ssubset. -/
instance decidable_forall_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop}
[Π t (h : t ⊂ s), decidable (p t h)] : decidable (∀ t h, p t h) :=
decidable_of_iff (∀ t (h : t ∈ s.ssubsets), p t (mem_ssubsets.1 h))
⟨(λ h t hs, h t (mem_ssubsets.2 hs)), (λ h _ _, h _ _)⟩
/-- A version of `finset.decidable_exists_of_decidable_ssubsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_exists_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∃ t (h : t ⊂ s), p t) :=
@finset.decidable_exists_of_decidable_ssubsets _ _ _ _ hu
/-- A version of `finset.decidable_forall_of_decidable_ssubsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_forall_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∀ t (h : t ⊂ s), p t) :=
@finset.decidable_forall_of_decidable_ssubsets _ _ _ _ hu
end ssubsets
section powerset_len
/-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s`
of cardinality `n`. -/
def powerset_len (n : ℕ) (s : finset α) : finset (finset α) :=
⟨(s.1.powerset_len n).pmap finset.mk
(λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2),
nodup_pmap (λ a ha b hb, congr_arg finset.val)
(nodup_powerset_len s.2)⟩
/-- **Formula for the Number of Combinations** -/
theorem mem_powerset_len {n} {s t : finset α} :
s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n :=
by cases s; simp [powerset_len, val_le_iff.symm]; refl
@[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) :
powerset_len n s ⊆ powerset_len n t :=
λ u h', mem_powerset_len.2 $
and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h')
/-- **Formula for the Number of Combinations** -/
@[simp] theorem card_powerset_len (n : ℕ) (s : finset α) :
card (powerset_len n s) = nat.choose (card s) n :=
(card_pmap _ _ _).trans (card_powerset_len n s.1)
@[simp] lemma powerset_len_zero (s : finset α) : finset.powerset_len 0 s = {∅} :=
begin
ext, rw [mem_powerset_len, mem_singleton, card_eq_zero],
refine ⟨λ h, h.2, λ h, by { rw h, exact ⟨empty_subset s, rfl⟩ }⟩,
end
@[simp] theorem powerset_len_empty (n : ℕ) {s : finset α} (h : s.card < n) :
powerset_len n s = ∅ :=
finset.card_eq_zero.mp (by rw [card_powerset_len, nat.choose_eq_zero_of_lt h])
theorem powerset_len_eq_filter {n} {s : finset α} :
powerset_len n s = (powerset s).filter (λ x, x.card = n) :=
by { ext, simp [mem_powerset_len] }
lemma powerset_len_succ_insert [decidable_eq α] {x : α} {s : finset α} (h : x ∉ s) (n : ℕ) :
powerset_len n.succ (insert x s) = powerset_len n.succ s ∪ (powerset_len n s).image (insert x) :=
begin
rw [powerset_len_eq_filter, powerset_insert, filter_union, ←powerset_len_eq_filter],
congr,
rw [powerset_len_eq_filter, image_filter],
congr' 1,
ext t,
simp only [mem_powerset, mem_filter, function.comp_app, and.congr_right_iff],
intro ht,
have : x ∉ t := λ H, h (ht H),
simp [card_insert_of_not_mem this, nat.succ_inj']
end
lemma powerset_len_nonempty {n : ℕ} {s : finset α} (h : n < s.card) :
(powerset_len n s).nonempty :=
begin
classical,
induction s using finset.induction_on with x s hx IH generalizing n,
{ simpa using h },
{ cases n,
{ simp },
{ rw [card_insert_of_not_mem hx, nat.succ_lt_succ_iff] at h,
rw powerset_len_succ_insert hx,
refine nonempty.mono _ ((IH h).image (insert x)),
convert (subset_union_right _ _) } }
end
@[simp] lemma powerset_len_self (s : finset α) :
powerset_len s.card s = {s} :=
begin
ext,
rw [mem_powerset_len, mem_singleton],
split,
{ exact λ ⟨hs, hc⟩, eq_of_subset_of_card_le hs hc.ge },
{ rintro rfl,
simp }
end
lemma powerset_card_bUnion [decidable_eq (finset α)] (s : finset α) :
finset.powerset s = (range (s.card + 1)).bUnion (λ i, powerset_len i s) :=
begin
refine ext (λ a, ⟨λ ha, _, λ ha, _ ⟩),
{ rw mem_bUnion,
exact ⟨a.card, mem_range.mpr (nat.lt_succ_of_le (card_le_of_subset (mem_powerset.mp ha))),
mem_powerset_len.mpr ⟨mem_powerset.mp ha, rfl⟩⟩ },
{ rcases mem_bUnion.mp ha with ⟨i, hi, ha⟩,
exact mem_powerset.mpr (mem_powerset_len.mp ha).1, }
end
lemma powerset_len_sup [decidable_eq α] (u : finset α) (n : ℕ) (hn : n < u.card) :
(powerset_len n.succ u).sup id = u :=
begin
apply le_antisymm,
{ simp_rw [sup_le_iff, mem_powerset_len],
rintros x ⟨h, -⟩,
exact h },
{ rw [sup_eq_bUnion, le_iff_subset, subset_iff],
cases (nat.succ_le_of_lt hn).eq_or_lt with h' h',
{ simp [h'] },
{ intros x hx,
simp only [mem_bUnion, exists_prop, id.def],
obtain ⟨t, ht⟩ : ∃ t, t ∈ powerset_len n (u.erase x) := powerset_len_nonempty _,
{ refine ⟨insert x t, _, mem_insert_self _ _⟩,
rw [←insert_erase hx, powerset_len_succ_insert (not_mem_erase _ _)],
exact mem_union_right _ (mem_image_of_mem _ ht) },
{ rwa [card_erase_of_mem hx, nat.lt_pred_iff] } } }
end
@[simp]
lemma powerset_len_card_add (s : finset α) {i : ℕ} (hi : 0 < i) :
s.powerset_len (s.card + i) = ∅ :=
finset.powerset_len_empty _ (lt_add_of_pos_right (finset.card s) hi)
@[simp] theorem map_val_val_powerset_len (s : finset α) (i : ℕ) :
(s.powerset_len i).val.map finset.val = s.1.powerset_len i :=
by simp [finset.powerset_len, map_pmap, pmap_eq_map, map_id']
end powerset_len
end finset
|
f4174cd769bb9057dba13b0b318505bd3d3d59d6 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Lean/Elab/InfoTree/Main.lean | 286ea0d11f5069613c2608312e3b1ae2108e5b2f | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 15,324 | lean | /-
Copyright (c) 2020 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Meta.PPGoal
namespace Lean.Elab.ContextInfo
variable [Monad m] [MonadEnv m] [MonadMCtx m] [MonadOptions m] [MonadResolveName m] [MonadNameGenerator m]
def saveNoFileMap : m ContextInfo := return {
env := (← getEnv)
fileMap := default
mctx := (← getMCtx)
options := (← getOptions)
currNamespace := (← getCurrNamespace)
openDecls := (← getOpenDecls)
ngen := (← getNGen)
}
def save [MonadFileMap m] : m ContextInfo := do
let ctx ← saveNoFileMap
return { ctx with fileMap := (← getFileMap) }
end ContextInfo
def CompletionInfo.stx : CompletionInfo → Syntax
| dot i .. => i.stx
| id stx .. => stx
| dotId stx .. => stx
| fieldId stx .. => stx
| namespaceId stx => stx
| option stx => stx
| endSection stx .. => stx
| tactic stx .. => stx
def CustomInfo.format : CustomInfo → Format
| i => Std.ToFormat.format i.json
instance : ToFormat CustomInfo := ⟨CustomInfo.format⟩
partial def InfoTree.findInfo? (p : Info → Bool) (t : InfoTree) : Option Info :=
match t with
| context _ t => findInfo? p t
| node i ts =>
if p i then
some i
else
ts.findSome? (findInfo? p)
| _ => none
/-- Instantiate the holes on the given `tree` with the assignment table.
(analoguous to instantiating the metavariables in an expression) -/
partial def InfoTree.substitute (tree : InfoTree) (assignment : PersistentHashMap MVarId InfoTree) : InfoTree :=
match tree with
| node i c => node i <| c.map (substitute · assignment)
| context i t => context i (substitute t assignment)
| hole id => match assignment.find? id with
| none => hole id
| some tree => substitute tree assignment
def ContextInfo.runMetaM (info : ContextInfo) (lctx : LocalContext) (x : MetaM α) : IO α := do
let x := x.run { lctx := lctx } { mctx := info.mctx }
/-
We must execute `x` using the `ngen` stored in `info`. Otherwise, we may create `MVarId`s and `FVarId`s that
have been used in `lctx` and `info.mctx`.
-/
let ((a, _), _) ←
x.toIO { options := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls, fileName := "<InfoTree>", fileMap := default }
{ env := info.env, ngen := info.ngen }
return a
def ContextInfo.toPPContext (info : ContextInfo) (lctx : LocalContext) : PPContext :=
{ env := info.env, mctx := info.mctx, lctx := lctx,
opts := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls }
def ContextInfo.ppSyntax (info : ContextInfo) (lctx : LocalContext) (stx : Syntax) : IO Format := do
ppTerm (info.toPPContext lctx) ⟨stx⟩ -- HACK: might not be a term
private def formatStxRange (ctx : ContextInfo) (stx : Syntax) : Format :=
let pos := stx.getPos?.getD 0
let endPos := stx.getTailPos?.getD pos
f!"{fmtPos pos stx.getHeadInfo}-{fmtPos endPos stx.getTailInfo}"
where fmtPos pos info :=
let pos := format <| ctx.fileMap.toPosition pos
match info with
| SourceInfo.original .. => pos
| _ => f!"{pos}†"
private def formatElabInfo (ctx : ContextInfo) (info : ElabInfo) : Format :=
if info.elaborator.isAnonymous then
formatStxRange ctx info.stx
else
f!"{formatStxRange ctx info.stx} @ {info.elaborator}"
def TermInfo.runMetaM (info : TermInfo) (ctx : ContextInfo) (x : MetaM α) : IO α :=
ctx.runMetaM info.lctx x
def TermInfo.format (ctx : ContextInfo) (info : TermInfo) : IO Format := do
info.runMetaM ctx do
let ty : Format ← try
Meta.ppExpr (← Meta.inferType info.expr)
catch _ =>
pure "<failed-to-infer-type>"
return f!"{← Meta.ppExpr info.expr} {if info.isBinder then "(isBinder := true) " else ""}: {ty} @ {formatElabInfo ctx info.toElabInfo}"
def CompletionInfo.format (ctx : ContextInfo) (info : CompletionInfo) : IO Format :=
match info with
| .dot i (expectedType? := expectedType?) .. => return f!"[.] {← i.format ctx} : {expectedType?}"
| .id stx _ _ lctx expectedType? => ctx.runMetaM lctx do return f!"[.] {stx} : {expectedType?} @ {formatStxRange ctx info.stx}"
| _ => return f!"[.] {info.stx} @ {formatStxRange ctx info.stx}"
def CommandInfo.format (ctx : ContextInfo) (info : CommandInfo) : IO Format := do
return f!"command @ {formatElabInfo ctx info.toElabInfo}"
def FieldInfo.format (ctx : ContextInfo) (info : FieldInfo) : IO Format := do
ctx.runMetaM info.lctx do
return f!"{info.fieldName} : {← Meta.ppExpr (← Meta.inferType info.val)} := {← Meta.ppExpr info.val} @ {formatStxRange ctx info.stx}"
def ContextInfo.ppGoals (ctx : ContextInfo) (goals : List MVarId) : IO Format :=
if goals.isEmpty then
return "no goals"
else
ctx.runMetaM {} (return Std.Format.prefixJoin "\n" (← goals.mapM (Meta.ppGoal ·)))
def TacticInfo.format (ctx : ContextInfo) (info : TacticInfo) : IO Format := do
let ctxB := { ctx with mctx := info.mctxBefore }
let ctxA := { ctx with mctx := info.mctxAfter }
let goalsBefore ← ctxB.ppGoals info.goalsBefore
let goalsAfter ← ctxA.ppGoals info.goalsAfter
return f!"Tactic @ {formatElabInfo ctx info.toElabInfo}\n{info.stx}\nbefore {goalsBefore}\nafter {goalsAfter}"
def MacroExpansionInfo.format (ctx : ContextInfo) (info : MacroExpansionInfo) : IO Format := do
let stx ← ctx.ppSyntax info.lctx info.stx
let output ← ctx.ppSyntax info.lctx info.output
return f!"Macro expansion\n{stx}\n===>\n{output}"
def UserWidgetInfo.format (info : UserWidgetInfo) : Format :=
f!"UserWidget {info.widgetId}\n{Std.ToFormat.format info.props}"
def FVarAliasInfo.format (info : FVarAliasInfo) : Format :=
f!"FVarAlias {info.id.name} -> {info.baseId.name}"
def FieldRedeclInfo.format (ctx : ContextInfo) (info : FieldRedeclInfo) : Format :=
f!"FieldRedecl @ {formatStxRange ctx info.stx}"
def Info.format (ctx : ContextInfo) : Info → IO Format
| ofTacticInfo i => i.format ctx
| ofTermInfo i => i.format ctx
| ofCommandInfo i => i.format ctx
| ofMacroExpansionInfo i => i.format ctx
| ofFieldInfo i => i.format ctx
| ofCompletionInfo i => i.format ctx
| ofUserWidgetInfo i => pure <| i.format
| ofCustomInfo i => pure <| Std.ToFormat.format i
| ofFVarAliasInfo i => pure <| i.format
| ofFieldRedeclInfo i => pure <| i.format ctx
def Info.toElabInfo? : Info → Option ElabInfo
| ofTacticInfo i => some i.toElabInfo
| ofTermInfo i => some i.toElabInfo
| ofCommandInfo i => some i.toElabInfo
| ofMacroExpansionInfo _ => none
| ofFieldInfo _ => none
| ofCompletionInfo _ => none
| ofUserWidgetInfo _ => none
| ofCustomInfo _ => none
| ofFVarAliasInfo _ => none
| ofFieldRedeclInfo _ => none
/--
Helper function for propagating the tactic metavariable context to its children nodes.
We need this function because we preserve `TacticInfo` nodes during backtracking *and* their
children. Moreover, we backtrack the metavariable context to undo metavariable assignments.
`TacticInfo` nodes save the metavariable context before/after the tactic application, and
can be pretty printed without any extra information. This is not the case for `TermInfo` nodes.
Without this function, the formatting method would often fail when processing `TermInfo` nodes
that are children of `TacticInfo` nodes that have been preserved during backtracking.
Saving the metavariable context at `TermInfo` nodes is also not a good option because
at `TermInfo` creation time, the metavariable context often miss information, e.g.,
a TC problem has not been resolved, a postponed subterm has not been elaborated, etc.
See `Term.SavedState.restore`.
-/
def Info.updateContext? : Option ContextInfo → Info → Option ContextInfo
| some ctx, ofTacticInfo i => some { ctx with mctx := i.mctxAfter }
| ctx?, _ => ctx?
partial def InfoTree.format (tree : InfoTree) (ctx? : Option ContextInfo := none) : IO Format := do
match tree with
| hole id => return .nestD f!"• ?{toString id.name}"
| context i t => format t i
| node i cs => match ctx? with
| none => return "• <context-not-available>"
| some ctx =>
let fmt ← i.format ctx
if cs.size == 0 then
return .nestD f!"• {fmt}"
else
let ctx? := i.updateContext? ctx?
return .nestD f!"• {fmt}{Std.Format.prefixJoin .line (← cs.toList.mapM fun c => format c ctx?)}"
section
variable [Monad m] [MonadInfoTree m]
@[inline] private def modifyInfoTrees (f : PersistentArray InfoTree → PersistentArray InfoTree) : m Unit :=
modifyInfoState fun s => { s with trees := f s.trees }
/-- Returns the current array of InfoTrees and resets it to an empty array. -/
def getResetInfoTrees : m (PersistentArray InfoTree) := do
let trees := (← getInfoState).trees
modifyInfoTrees fun _ => {}
return trees
def pushInfoTree (t : InfoTree) : m Unit := do
if (← getInfoState).enabled then
modifyInfoTrees fun ts => ts.push t
def pushInfoLeaf (t : Info) : m Unit := do
if (← getInfoState).enabled then
pushInfoTree <| InfoTree.node (children := {}) t
def addCompletionInfo (info : CompletionInfo) : m Unit := do
pushInfoLeaf <| Info.ofCompletionInfo info
def addConstInfo [MonadEnv m] [MonadError m]
(stx : Syntax) (n : Name) (expectedType? : Option Expr := none) : m Unit := do
pushInfoLeaf <| .ofTermInfo {
elaborator := .anonymous
lctx := .empty
expr := (← mkConstWithLevelParams n)
stx
expectedType?
}
/-- This does the same job as `resolveGlobalConstNoOverload`; resolving an identifier
syntax to a unique fully resolved name or throwing if there are ambiguities.
But also adds this resolved name to the infotree. This means that when you hover
over a name in the sourcefile you will see the fully resolved name in the hover info.-/
def resolveGlobalConstNoOverloadWithInfo [MonadResolveName m] [MonadEnv m] [MonadError m]
(id : Syntax) (expectedType? : Option Expr := none) : m Name := do
let n ← resolveGlobalConstNoOverload id
if (← getInfoState).enabled then
-- we do not store a specific elaborator since identifiers are special-cased by the server anyway
addConstInfo id n expectedType?
return n
/-- Similar to `resolveGlobalConstNoOverloadWithInfo`, except if there are multiple name resolutions then it returns them as a list. -/
def resolveGlobalConstWithInfos [MonadResolveName m] [MonadEnv m] [MonadError m]
(id : Syntax) (expectedType? : Option Expr := none) : m (List Name) := do
let ns ← resolveGlobalConst id
if (← getInfoState).enabled then
for n in ns do
addConstInfo id n expectedType?
return ns
/-- Similar to `resolveGlobalName`, but it also adds the resolved name to the info tree. -/
def resolveGlobalNameWithInfos [MonadResolveName m] [MonadEnv m] [MonadError m]
(ref : Syntax) (id : Name) : m (List (Name × List String)) := do
let ns ← resolveGlobalName id
if (← getInfoState).enabled then
for (n, _) in ns do
addConstInfo ref n
return ns
/-- Use this to descend a node on the infotree that is being built.
It saves the current list of trees `t₀` and resets it and then runs `x >>= mkInfo`, producing either an `i : Info` or a hole id.
Running `x >>= mkInfo` will modify the trees state and produce a new list of trees `t₁`.
In the `i : Info` case, `t₁` become the children of a node `node i t₁` that is appended to `t₀`.
-/
def withInfoContext' [MonadFinally m] (x : m α) (mkInfo : α → m (Sum Info MVarId)) : m α := do
if (← getInfoState).enabled then
let treesSaved ← getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun a? => do
match a? with
| none => modifyInfoTrees fun _ => treesSaved
| some a =>
let info ← mkInfo a
modifyInfoTrees fun trees =>
match info with
| Sum.inl info => treesSaved.push <| InfoTree.node info trees
| Sum.inr mvarId => treesSaved.push <| InfoTree.hole mvarId
else
x
/-- Saves the current list of trees `t₀`, runs `x` to produce a new tree list `t₁` and
runs `mkInfoTree t₁` to get `n : InfoTree` and then restores the trees to be `t₀ ++ [n]`.-/
def withInfoTreeContext [MonadFinally m] (x : m α) (mkInfoTree : PersistentArray InfoTree → m InfoTree) : m α := do
if (← getInfoState).enabled then
let treesSaved ← getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun _ => do
let st ← getInfoState
let tree ← mkInfoTree st.trees
modifyInfoTrees fun _ => treesSaved.push tree
else
x
/-- Run `x` as a new child infotree node with header given by `mkInfo`. -/
@[inline] def withInfoContext [MonadFinally m] (x : m α) (mkInfo : m Info) : m α := do
withInfoTreeContext x (fun trees => do return InfoTree.node (← mkInfo) trees)
/-- Resets the trees state `t₀`, runs `x` to produce a new trees
state `t₁` and sets the state to be `t₀ ++ (InfoTree.context Γ <$> t₁)`
where `Γ` is the context derived from the monad state. -/
def withSaveInfoContext [MonadNameGenerator m] [MonadFinally m] [MonadEnv m] [MonadOptions m] [MonadMCtx m] [MonadResolveName m] [MonadFileMap m] (x : m α) : m α := do
if (← getInfoState).enabled then
let treesSaved ← getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun _ => do
let st ← getInfoState
let trees ← st.trees.mapM fun tree => do
let tree := tree.substitute st.assignment
pure <| InfoTree.context (← ContextInfo.save) tree
modifyInfoTrees fun _ => treesSaved ++ trees
else
x
def getInfoHoleIdAssignment? (mvarId : MVarId) : m (Option InfoTree) :=
return (← getInfoState).assignment[mvarId]
def assignInfoHoleId (mvarId : MVarId) (infoTree : InfoTree) : m Unit := do
assert! (← getInfoHoleIdAssignment? mvarId).isNone
modifyInfoState fun s => { s with assignment := s.assignment.insert mvarId infoTree }
end
def withMacroExpansionInfo [MonadFinally m] [Monad m] [MonadInfoTree m] [MonadLCtx m] (stx output : Syntax) (x : m α) : m α :=
let mkInfo : m Info := do
return Info.ofMacroExpansionInfo {
lctx := (← getLCtx)
stx, output
}
withInfoContext x mkInfo
@[inline] def withInfoHole [MonadFinally m] [Monad m] [MonadInfoTree m] (mvarId : MVarId) (x : m α) : m α := do
if (← getInfoState).enabled then
let treesSaved ← getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun _ => modifyInfoState fun s =>
if h : s.trees.size > 0 then
have : s.trees.size - 1 < s.trees.size := Nat.sub_lt h (by decide)
{ s with trees := treesSaved, assignment := s.assignment.insert mvarId s.trees[s.trees.size - 1] }
else
{ s with trees := treesSaved }
else
x
def enableInfoTree [MonadInfoTree m] (flag := true) : m Unit :=
modifyInfoState fun s => { s with enabled := flag }
def getInfoTrees [MonadInfoTree m] [Monad m] : m (PersistentArray InfoTree) :=
return (← getInfoState).trees
end Lean.Elab
|
141e01370c4cfe5c7a0e36e375ded8ebed3fa88a | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/meta/transfer.lean | 5140f1b70eb4f70b3fe0cae6002eb9970679a376 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 7,442 | 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)
-/
prelude
import init.meta.tactic init.meta.match_tactic init.relator init.meta.mk_dec_eq_instance
import init.data.list.instances
namespace transfer
open tactic expr list monad
/- Transfer rules are of the shape:
rel_t : {u} Πx, R t₁ t₂
where `u` is a list of universe parameters, `x` is a list of dependent variables, and `R` is a
relation. Then this rule will translate `t₁` (depending on `u` and `x`) into `t₂`. `u` and `x`
will be called parameters. When `R` is a relation on functions lifted from `S` and `R` the variables
bound by `S` are called arguments. `R` is generally constructed using `⇒` (i.e. `relator.lift_fun`).
As example:
rel_eq : (R ⇒ R ⇒ iff) eq t
transfer will match this rule when it sees:
(@eq α a b) and transfer it to (t a b)
Here `α` is a parameter and `a` and `b` are arguments.
TODO: add trace statements
TODO: currently the used relation must be fixed by the matched rule or through type class
inference. Maybe we want to replace this by type inference similar to Isabelle's transfer.
-/
private meta structure rel_data :=
(in_type : expr)
(out_type : expr)
(relation : expr)
meta instance has_to_tactic_format_rel_data : has_to_tactic_format rel_data :=
⟨λr, do
R ← pp r.relation,
α ← pp r.in_type,
β ← pp r.out_type,
return format!"({R}: rel ({α}) ({β}))" ⟩
private meta structure rule_data :=
(pr : expr)
(uparams : list name) -- levels not in pat
(params : list (expr × bool)) -- fst : local constant, snd = tt → param appears in pattern
(uargs : list name) -- levels not in pat
(args : list (expr × rel_data)) -- fst : local constant
(pat : pattern) -- `R c`
(out : expr) -- right-hand side `d` of rel equation `R c d`
meta instance has_to_tactic_format_rule_data : has_to_tactic_format rule_data :=
⟨λr, do
pr ← pp r.pr,
up ← pp r.uparams,
mp ← pp r.params,
ua ← pp r.uargs,
ma ← pp r.args,
pat ← pp r.pat.target,
out ← pp r.out,
return format!"{{ ⟨{pat}⟩ pr: {pr} → {out}, {up} {mp} {ua} {ma} }" ⟩
private meta def get_lift_fun : expr → tactic (list rel_data × expr)
| e :=
do {
guardb (is_constant_of (get_app_fn e) ``relator.lift_fun),
[α, β, γ, δ, R, S] ← return $ get_app_args e,
(ps, r) ← get_lift_fun S,
return (rel_data.mk α β R :: ps, r)} <|>
return ([], e)
private meta def mark_occurences (e : expr) : list expr → list (expr × bool)
| [] := []
| (h :: t) := let xs := mark_occurences t in
(h, occurs h e || any xs (λ⟨e, oc⟩, oc && occurs h e)) :: xs
private meta def analyse_rule (u' : list name) (pr : expr) : tactic rule_data := do
t ← infer_type pr,
(params, app (app r f) g) ← mk_local_pis t,
(arg_rels, R) ← get_lift_fun r,
args ← (enum arg_rels).mmap $ λ⟨n, a⟩,
prod.mk <$> mk_local_def (mk_simple_name ("a_" ++ repr n)) a.in_type <*> pure a,
a_vars ← return $ prod.fst <$> args,
p ← head_beta (app_of_list f a_vars),
p_data ← return $ mark_occurences (app R p) params,
p_vars ← return $ list.map prod.fst (p_data.filter (λx, ↑x.2)),
u ← return $ collect_univ_params (app R p) ∩ u',
pat ← mk_pattern (level.param <$> u) (p_vars ++ a_vars) (app R p) (level.param <$> u) (p_vars ++ a_vars),
return $ rule_data.mk pr (u'.remove_all u) p_data u args pat g
private meta def analyse_decls : list name → tactic (list rule_data) :=
mmap (λn, do
d ← get_decl n,
c ← return d.univ_params.length,
ls ← (repeat () c).mmap (λ_, mk_fresh_name),
analyse_rule ls (const n (ls.map level.param)))
private meta def split_params_args : list (expr × bool) → list expr → list (expr × option expr) × list expr
| ((lc, tt) :: ps) (e :: es) := let (ps', es') := split_params_args ps es in ((lc, some e) :: ps', es')
| ((lc, ff) :: ps) es := let (ps', es') := split_params_args ps es in ((lc, none) :: ps', es')
| _ es := ([], es)
private meta def param_substitutions (ctxt : list expr) :
list (expr × option expr) → tactic (list (name × expr) × list expr)
| (((local_const n _ bi t), s) :: ps) := do
(e, m) ← match s with
| (some e) := return (e, [])
| none :=
let ctxt' := list.filter (λv, occurs v t) ctxt in
let ty := pis ctxt' t in
if bi = binder_info.inst_implicit then do
guard (bi = binder_info.inst_implicit),
e ← instantiate_mvars ty >>= mk_instance,
return (e, [])
else do
mv ← mk_meta_var ty,
return (app_of_list mv ctxt', [mv])
end,
sb ← return $ instantiate_local n e,
ps ← return $ prod.map sb ((<$>) sb) <$> ps,
(ms, vs) ← param_substitutions ps,
return ((n, e) :: ms, m ++ vs)
| _ := return ([], [])
/- input expression a type `R a`, it finds a type `b`, s.t. there is a proof of the type `R a b`.
It return (`a`, pr : `R a b`) -/
meta def compute_transfer : list rule_data → list expr → expr → tactic (expr × expr × list expr)
| rds ctxt e := do
-- Select matching rule
(i, ps, args, ms, rd) ← first (rds.map (λrd, do
(l, m) ← match_pattern_core semireducible rd.pat e,
level_map ← rd.uparams.mmap $ λl, prod.mk l <$> mk_meta_univ,
inst_univ ← return $ λe, instantiate_univ_params e (level_map ++ zip rd.uargs l),
(ps, args) ← return $ split_params_args (rd.params.map (prod.map inst_univ id)) m,
(ps, ms) ← param_substitutions ctxt ps, /- this checks type class parameters -/
return (instantiate_locals ps ∘ inst_univ, ps, args, ms, rd))) <|>
(do trace e, fail "no matching rule"),
(bs, hs, mss) ← (zip rd.args args).mmap (λ⟨⟨_, d⟩, e⟩, do
-- Argument has function type
(args, r) ← get_lift_fun (i d.relation),
((a_vars, b_vars), (R_vars, bnds)) ← (enum args).mmap (λ⟨n, arg⟩, do
a ← mk_local_def sformat!"a{n}" arg.in_type,
b ← mk_local_def sformat!"b{n}" arg.out_type,
R ← mk_local_def sformat!"R{n}" (arg.relation a b),
return ((a, b), (R, [a, b, R]))) >>= (return ∘ prod.map unzip unzip ∘ unzip),
rds' ← R_vars.mmap (analyse_rule []),
-- Transfer argument
a ← return $ i e,
a' ← head_beta (app_of_list a a_vars),
(b, pr, ms) ← compute_transfer (rds ++ rds') (ctxt ++ a_vars) (app r a'),
b' ← head_eta (lambdas b_vars b),
return (b', [a, b', lambdas (list.join bnds) pr], ms)) >>= (return ∘ prod.map id unzip ∘ unzip),
-- Combine
b ← head_beta (app_of_list (i rd.out) bs),
pr ← return $ app_of_list (i rd.pr) (prod.snd <$> ps ++ list.join hs),
return (b, pr, ms ++ mss.join)
meta def transfer (ds : list name) : tactic unit := do
rds ← analyse_decls ds,
tgt ← target,
(guard (¬ tgt.has_meta_var) <|>
fail "Target contains (universe) meta variables. This is not supported by transfer."),
(new_tgt, pr, ms) ← compute_transfer rds [] ((const `iff [] : expr) tgt),
new_pr ← mk_meta_var new_tgt,
/- Setup final tactic state -/
exact ((const `iff.mpr [] : expr) tgt new_tgt pr new_pr),
ms ← ms.mmap (λm, (get_assignment m >> return []) <|> return [m]),
gs ← get_goals,
set_goals (ms.join ++ new_pr :: gs)
end transfer
|
88de6474196a7ce2299e9e7694a18ac657732933 | aa5a655c05e5359a70646b7154e7cac59f0b4132 | /src/Lean/Meta/SynthInstance.lean | c3d45498323d7a48457e1b196e2a8888468f6596 | [
"Apache-2.0"
] | permissive | lambdaxymox/lean4 | ae943c960a42247e06eff25c35338268d07454cb | 278d47c77270664ef29715faab467feac8a0f446 | refs/heads/master | 1,677,891,867,340 | 1,612,500,005,000 | 1,612,500,005,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,110 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Daniel Selsam, Leonardo de Moura
Type class instance synthesizer using tabled resolution.
-/
import Lean.Meta.Basic
import Lean.Meta.Instances
import Lean.Meta.LevelDefEq
import Lean.Meta.AbstractMVars
import Lean.Meta.WHNF
import Lean.Util.Profile
namespace Lean.Meta
register_builtin_option synthInstance.maxHeartbeats : Nat := {
defValue := 500
descr := "maximum amount of heartbeats per typeclass resolution problem. A heartbeat is number of (small) memory allocations (in thousands), 0 means no limit"
}
register_builtin_option synthInstance.maxSize : Nat := {
defValue := 128
descr := "maximum number of instances used to construct a solution in the type class instance synthesis procedure"
}
namespace SynthInstance
def getMaxHeartbeats (opts : Options) : Nat :=
synthInstance.maxHeartbeats.get opts * 1000
open Std (HashMap)
builtin_initialize inferTCGoalsRLAttr : TagAttribute ←
registerTagAttribute `inferTCGoalsRL "instruct type class resolution procedure to solve goals from right to left for this instance"
def hasInferTCGoalsRLAttribute (env : Environment) (constName : Name) : Bool :=
inferTCGoalsRLAttr.hasTag env constName
structure GeneratorNode where
mvar : Expr
key : Expr
mctx : MetavarContext
instances : Array Expr
currInstanceIdx : Nat
deriving Inhabited
structure ConsumerNode where
mvar : Expr
key : Expr
mctx : MetavarContext
subgoals : List Expr
size : Nat -- instance size so far
deriving Inhabited
inductive Waiter where
| consumerNode : ConsumerNode → Waiter
| root : Waiter
def Waiter.isRoot : Waiter → Bool
| Waiter.consumerNode _ => false
| Waiter.root => true
/-
In tabled resolution, we creating a mapping from goals (e.g., `Coe Nat ?x`) to
answers and waiters. Waiters are consumer nodes that are waiting for answers for a
particular node.
We implement this mapping using a `HashMap` where the keys are
normalized expressions. That is, we replace assignable metavariables
with auxiliary free variables of the form `_tc.<idx>`. We do
not declare these free variables in any local context, and we should
view them as "normalized names" for metavariables. For example, the
term `f ?m ?m ?n` is normalized as
`f _tc.0 _tc.0 _tc.1`.
This approach is structural, and we may visit the same goal more
than once if the different occurrences are just definitionally
equal, but not structurally equal.
Remark: a metavariable is assignable only if its depth is equal to
the metavar context depth.
-/
namespace MkTableKey
structure State where
nextIdx : Nat := 0
lmap : HashMap MVarId Level := {}
emap : HashMap MVarId Expr := {}
abbrev M := ReaderT MetavarContext (StateM State)
partial def normLevel (u : Level) : M Level := do
if !u.hasMVar then
pure u
else match u with
| Level.succ v _ => return u.updateSucc! (← normLevel v)
| Level.max v w _ => return u.updateMax! (← normLevel v) (← normLevel w)
| Level.imax v w _ => return u.updateIMax! (← normLevel v) (← normLevel w)
| Level.mvar mvarId _ =>
let mctx ← read
if !mctx.isLevelAssignable mvarId then
pure u
else
let s ← get
match s.lmap.find? mvarId with
| some u' => pure u'
| none =>
let u' := mkLevelParam $ Name.mkNum `_tc s.nextIdx
modify fun s => { s with nextIdx := s.nextIdx + 1, lmap := s.lmap.insert mvarId u' }
pure u'
| u => pure u
partial def normExpr (e : Expr) : M Expr := do
if !e.hasMVar then
pure e
else match e with
| Expr.const _ us _ => return e.updateConst! (← us.mapM normLevel)
| Expr.sort u _ => return e.updateSort! (← normLevel u)
| Expr.app f a _ => return e.updateApp! (← normExpr f) (← normExpr a)
| Expr.letE _ t v b _ => return e.updateLet! (← normExpr t) (← normExpr v) (← normExpr b)
| Expr.forallE _ d b _ => return e.updateForallE! (← normExpr d) (← normExpr b)
| Expr.lam _ d b _ => return e.updateLambdaE! (← normExpr d) (← normExpr b)
| Expr.mdata _ b _ => return e.updateMData! (← normExpr b)
| Expr.proj _ _ b _ => return e.updateProj! (← normExpr b)
| Expr.mvar mvarId _ =>
let mctx ← read
if !mctx.isExprAssignable mvarId then
pure e
else
let s ← get
match s.emap.find? mvarId with
| some e' => pure e'
| none => do
let e' := mkFVar $ Name.mkNum `_tc s.nextIdx
modify fun s => { s with nextIdx := s.nextIdx + 1, emap := s.emap.insert mvarId e' }
pure e'
| _ => pure e
end MkTableKey
/- Remark: `mkTableKey` assumes `e` does not contain assigned metavariables. -/
def mkTableKey (mctx : MetavarContext) (e : Expr) : Expr :=
MkTableKey.normExpr e mctx |>.run' {}
structure Answer where
result : AbstractMVarsResult
resultType : Expr
size : Nat
instance : Inhabited Answer where
default := { result := arbitrary, resultType := arbitrary, size := 0 }
structure TableEntry where
waiters : Array Waiter
answers : Array Answer := #[]
structure Context where
maxResultSize : Nat
maxHeartbeats : Nat
/-
Remark: the SynthInstance.State is not really an extension of `Meta.State`.
The field `postponed` is not needed, and the field `mctx` is misleading since
`synthInstance` methods operate over different `MetavarContext`s simultaneously.
That being said, we still use `extends` because it makes it simpler to move from
`M` to `MetaM`.
-/
structure State where
result : Option Expr := none
generatorStack : Array GeneratorNode := #[]
resumeStack : Array (ConsumerNode × Answer) := #[]
tableEntries : HashMap Expr TableEntry := {}
abbrev SynthM := ReaderT Context $ StateRefT State MetaM
def checkMaxHeartbeats : SynthM Unit := do
Core.checkMaxHeartbeatsCore "typeclass" `synthInstance.maxHeartbeats (← read).maxHeartbeats
@[inline] def mapMetaM (f : forall {α}, MetaM α → MetaM α) {α} : SynthM α → SynthM α :=
monadMap @f
instance {α} : Inhabited (SynthM α) where
default := fun _ _ => arbitrary
/-- Return globals and locals instances that may unify with `type` -/
def getInstances (type : Expr) : MetaM (Array Expr) := do
-- We must retrieve `localInstances` before we use `forallTelescopeReducing` because it will update the set of local instances
let localInstances ← getLocalInstances
forallTelescopeReducing type fun _ type => do
let className? ← isClass? type
match className? with
| none => throwError $ "type class instance expected" ++ indentExpr type
| some className =>
let globalInstances ← getGlobalInstancesIndex
let result ← globalInstances.getUnify type
-- Using insertion sort because it is stable and the array `result` should be mostly sorted.
-- Most instances have default priority.
let result := result.insertionSort fun e₁ e₂ => e₁.priority < e₂.priority
let result ← result.mapM fun e => match e.val with
| Expr.const constName us _ => return e.val.updateConst! (← us.mapM (fun _ => mkFreshLevelMVar))
| _ => panic! "global instance is not a constant"
trace[Meta.synthInstance.globalInstances]! "{type}, {result}"
let result := localInstances.foldl (init := result) fun (result : Array Expr) linst =>
if linst.className == className then result.push linst.fvar else result
pure result
def mkGeneratorNode? (key mvar : Expr) : MetaM (Option GeneratorNode) := do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
let instances ← getInstances mvarType
if instances.isEmpty then
pure none
else
let mctx ← getMCtx
pure $ some {
mvar := mvar,
key := key,
mctx := mctx,
instances := instances,
currInstanceIdx := instances.size
}
/-- Create a new generator node for `mvar` and add `waiter` as its waiter.
`key` must be `mkTableKey mctx mvarType`. -/
def newSubgoal (mctx : MetavarContext) (key : Expr) (mvar : Expr) (waiter : Waiter) : SynthM Unit :=
withMCtx mctx do
trace[Meta.synthInstance.newSubgoal]! key
match (← mkGeneratorNode? key mvar) with
| none => pure ()
| some node =>
let entry : TableEntry := { waiters := #[waiter] }
modify fun s =>
{ s with
generatorStack := s.generatorStack.push node,
tableEntries := s.tableEntries.insert key entry }
def findEntry? (key : Expr) : SynthM (Option TableEntry) := do
return (← get).tableEntries.find? key
def getEntry (key : Expr) : SynthM TableEntry := do
match (← findEntry? key) with
| none => panic! "invalid key at synthInstance"
| some entry => pure entry
/--
Create a `key` for the goal associated with the given metavariable.
That is, we create a key for the type of the metavariable.
We must instantiate assigned metavariables before we invoke `mkTableKey`. -/
def mkTableKeyFor (mctx : MetavarContext) (mvar : Expr) : SynthM Expr :=
withMCtx mctx do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
return mkTableKey mctx mvarType
/- See `getSubgoals` and `getSubgoalsAux`
We use the parameter `j` to reduce the number of `instantiate*` invocations.
It is the same approach we use at `forallTelescope` and `lambdaTelescope`.
Given `getSubgoalsAux args j subgoals instVal type`,
we have that `type.instantiateRevRange j args.size args` does not have loose bound variables. -/
structure SubgoalsResult where
subgoals : List Expr
instVal : Expr
instTypeBody : Expr
private partial def getSubgoalsAux (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr)
: Array Expr → Nat → List Expr → Expr → Expr → MetaM SubgoalsResult
| args, j, subgoals, instVal, Expr.forallE n d b c => do
let d := d.instantiateRevRange j args.size args
let mvarType ← mkForallFVars xs d
let mvar ← mkFreshExprMVarAt lctx localInsts mvarType
let arg := mkAppN mvar xs
let instVal := mkApp instVal arg
let subgoals := if c.binderInfo.isInstImplicit then mvar::subgoals else subgoals
let args := args.push (mkAppN mvar xs)
getSubgoalsAux lctx localInsts xs args j subgoals instVal b
| args, j, subgoals, instVal, type => do
let type := type.instantiateRevRange j args.size args
let type ← whnf type
if type.isForall then
getSubgoalsAux lctx localInsts xs args args.size subgoals instVal type
else
pure ⟨subgoals, instVal, type⟩
/--
`getSubgoals lctx localInsts xs inst` creates the subgoals for the instance `inst`.
The subgoals are in the context of the free variables `xs`, and
`(lctx, localInsts)` is the local context and instances before we added the free variables to it.
This extra complication is required because
1- We want all metavariables created by `synthInstance` to share the same local context.
2- We want to ensure that applications such as `mvar xs` are higher order patterns.
The method `getGoals` create a new metavariable for each parameter of `inst`.
For example, suppose the type of `inst` is `forall (x_1 : A_1) ... (x_n : A_n), B x_1 ... x_n`.
Then, we create the metavariables `?m_i : forall xs, A_i`, and return the subset of these
metavariables that are instance implicit arguments, and the expressions:
- `inst (?m_1 xs) ... (?m_n xs)` (aka `instVal`)
- `B (?m_1 xs) ... (?m_n xs)` -/
def getSubgoals (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) (inst : Expr) : MetaM SubgoalsResult := do
let instType ← inferType inst
let result ← getSubgoalsAux lctx localInsts xs #[] 0 [] inst instType
match inst.getAppFn with
| Expr.const constName _ _ =>
let env ← getEnv
if hasInferTCGoalsRLAttribute env constName then
pure result
else
pure { result with subgoals := result.subgoals.reverse }
| _ => pure result
def tryResolveCore (mvar : Expr) (inst : Expr) : MetaM (Option (MetavarContext × List Expr)) := do
let mvarType ← inferType mvar
let lctx ← getLCtx
let localInsts ← getLocalInstances
forallTelescopeReducing mvarType fun xs mvarTypeBody => do
let ⟨subgoals, instVal, instTypeBody⟩ ← getSubgoals lctx localInsts xs inst
trace[Meta.synthInstance.tryResolve]! "{mvarTypeBody} =?= {instTypeBody}"
if (← isDefEq mvarTypeBody instTypeBody) then
let instVal ← mkLambdaFVars xs instVal
if (← isDefEq mvar instVal) then
trace[Meta.synthInstance.tryResolve]! "success"
pure (some ((← getMCtx), subgoals))
else
trace[Meta.synthInstance.tryResolve]! "failure assigning"
pure none
else
trace[Meta.synthInstance.tryResolve]! "failure"
pure none
/--
Try to synthesize metavariable `mvar` using the instance `inst`.
Remark: `mctx` contains `mvar`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals.
A subgoal is created for each instance implicit parameter of `inst`. -/
def tryResolve (mctx : MetavarContext) (mvar : Expr) (inst : Expr) : SynthM (Option (MetavarContext × List Expr)) :=
traceCtx `Meta.synthInstance.tryResolve <| withMCtx mctx <| tryResolveCore mvar inst
/--
Assign a precomputed answer to `mvar`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals. -/
def tryAnswer (mctx : MetavarContext) (mvar : Expr) (answer : Answer) : SynthM (Option MetavarContext) :=
withMCtx mctx do
let (_, _, val) ← openAbstractMVarsResult answer.result
if (← isDefEq mvar val) then
pure (some (← getMCtx))
else
pure none
/-- Move waiters that are waiting for the given answer to the resume stack. -/
def wakeUp (answer : Answer) : Waiter → SynthM Unit
| Waiter.root => do
if answer.result.paramNames.isEmpty && answer.result.numMVars == 0 then
modify fun s => { s with result := answer.result.expr }
else
let (_, _, answerExpr) ← openAbstractMVarsResult answer.result
trace[Meta.synthInstance]! "skip answer containing metavariables {answerExpr}"
pure ()
| Waiter.consumerNode cNode =>
modify fun s => { s with resumeStack := s.resumeStack.push (cNode, answer) }
def isNewAnswer (oldAnswers : Array Answer) (answer : Answer) : Bool :=
oldAnswers.all fun oldAnswer => do
-- Remark: isDefEq here is too expensive. TODO: if `==` is too imprecise, add some light normalization to `resultType` at `addAnswer`
-- iseq ← isDefEq oldAnswer.resultType answer.resultType; pure (!iseq)
oldAnswer.resultType != answer.resultType
private def mkAnswer (cNode : ConsumerNode) : MetaM Answer :=
withMCtx cNode.mctx do
traceM `Meta.synthInstance.newAnswer do m!"size: {cNode.size}, {← inferType cNode.mvar}"
let val ← instantiateMVars cNode.mvar
trace[Meta.synthInstance.newAnswer]! "val: {val}"
let result ← abstractMVars val -- assignable metavariables become parameters
let resultType ← inferType result.expr
pure { result := result, resultType := resultType, size := cNode.size + 1 }
/--
Create a new answer after `cNode` resolved all subgoals.
That is, `cNode.subgoals == []`.
And then, store it in the tabled entries map, and wakeup waiters. -/
def addAnswer (cNode : ConsumerNode) : SynthM Unit := do
if cNode.size ≥ (← read).maxResultSize then
traceM `Meta.synthInstance.discarded do m!"size: {cNode.size} ≥ {(← read).maxResultSize}, {← inferType cNode.mvar}"
return ()
else
let answer ← mkAnswer cNode
-- Remark: `answer` does not contain assignable or assigned metavariables.
let key := cNode.key
let entry ← getEntry key
if isNewAnswer entry.answers answer then
let newEntry := { entry with answers := entry.answers.push answer }
modify fun s => { s with tableEntries := s.tableEntries.insert key newEntry }
entry.waiters.forM (wakeUp answer)
/-- Process the next subgoal in the given consumer node. -/
def consume (cNode : ConsumerNode) : SynthM Unit :=
match cNode.subgoals with
| [] => addAnswer cNode
| mvar::_ => do
let waiter := Waiter.consumerNode cNode
let key ← mkTableKeyFor cNode.mctx mvar
let entry? ← findEntry? key
match entry? with
| none => newSubgoal cNode.mctx key mvar waiter
| some entry => modify fun s =>
{ s with
resumeStack := entry.answers.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack,
tableEntries := s.tableEntries.insert key { entry with waiters := entry.waiters.push waiter } }
def getTop : SynthM GeneratorNode := do
pure (← get).generatorStack.back
@[inline] def modifyTop (f : GeneratorNode → GeneratorNode) : SynthM Unit :=
modify fun s => { s with generatorStack := s.generatorStack.modify (s.generatorStack.size - 1) f }
/-- Try the next instance in the node on the top of the generator stack. -/
def generate : SynthM Unit := do
let gNode ← getTop
if gNode.currInstanceIdx == 0 then
modify fun s => { s with generatorStack := s.generatorStack.pop }
else do
let key := gNode.key
let idx := gNode.currInstanceIdx - 1
let inst := gNode.instances.get! idx
let mctx := gNode.mctx
let mvar := gNode.mvar
trace[Meta.synthInstance.generate]! "instance {inst}"
modifyTop fun gNode => { gNode with currInstanceIdx := idx }
match (← tryResolve mctx mvar inst) with
| none => pure ()
| some (mctx, subgoals) => consume { key := key, mvar := mvar, subgoals := subgoals, mctx := mctx, size := 0 }
def getNextToResume : SynthM (ConsumerNode × Answer) := do
let s ← get
let r := s.resumeStack.back
modify fun s => { s with resumeStack := s.resumeStack.pop }
pure r
/--
Given `(cNode, answer)` on the top of the resume stack, continue execution by using `answer` to solve the
next subgoal. -/
def resume : SynthM Unit := do
let (cNode, answer) ← getNextToResume
match cNode.subgoals with
| [] => panic! "resume found no remaining subgoals"
| mvar::rest =>
match (← tryAnswer cNode.mctx mvar answer) with
| none => pure ()
| some mctx =>
withMCtx mctx <| traceM `Meta.synthInstance.resume do
let goal ← inferType cNode.mvar
let subgoal ← inferType mvar
pure m!"size: {cNode.size + answer.size}, {goal} <== {subgoal}"
consume { key := cNode.key, mvar := cNode.mvar, subgoals := rest, mctx := mctx, size := cNode.size + answer.size }
def step : SynthM Bool := do
checkMaxHeartbeats
let s ← get
if !s.resumeStack.isEmpty then
resume
pure true
else if !s.generatorStack.isEmpty then
generate
pure true
else
pure false
def getResult : SynthM (Option Expr) := do
pure (← get).result
partial def synth : SynthM (Option Expr) := do
if (← step) then
match (← getResult) with
| none => synth
| some result => pure result
else
trace[Meta.synthInstance]! "failed"
pure none
def main (type : Expr) (maxResultSize : Nat) : MetaM (Option Expr) :=
withCurrHeartbeats <| traceCtx `Meta.synthInstance do
trace[Meta.synthInstance]! "main goal {type}"
let mvar ← mkFreshExprMVar type
let mctx ← getMCtx
let key := mkTableKey mctx type
let action : SynthM (Option Expr) := do
newSubgoal mctx key mvar Waiter.root
synth
action.run { maxResultSize := maxResultSize, maxHeartbeats := getMaxHeartbeats (← getOptions) } |>.run' {}
end SynthInstance
/-
Type class parameters can be annotated with `outParam` annotations.
Given `C a_1 ... a_n`, we replace `a_i` with a fresh metavariable `?m_i` IF
`a_i` is an `outParam`.
The result is type correct because we reject type class declarations IF
it contains a regular parameter X that depends on an `out` parameter Y.
Then, we execute type class resolution as usual.
If it succeeds, and metavariables ?m_i have been assigned, we try to unify
the original type `C a_1 ... a_n` witht the normalized one.
-/
private def preprocess (type : Expr) : MetaM Expr :=
forallTelescopeReducing type fun xs type => do
let type ← whnf type
mkForallFVars xs type
private def preprocessLevels (us : List Level) : MetaM (List Level) := do
let mut r := []
for u in us do
let u ← instantiateLevelMVars u
if u.hasMVar then
r := (← mkFreshLevelMVar)::r
else
r := u::r
pure r.reverse
private partial def preprocessArgs (type : Expr) (i : Nat) (args : Array Expr) : MetaM (Array Expr) := do
if h : i < args.size then
let type ← whnf type
match type with
| Expr.forallE _ d b _ => do
let arg := args.get ⟨i, h⟩
let arg ← if isOutParam d then mkFreshExprMVar d else pure arg
let args := args.set ⟨i, h⟩ arg
preprocessArgs (b.instantiate1 arg) (i+1) args
| _ =>
throwError "type class resolution failed, insufficient number of arguments" -- TODO improve error message
else
pure args
private def preprocessOutParam (type : Expr) : MetaM Expr :=
forallTelescope type fun xs typeBody => do
match typeBody.getAppFn with
| c@(Expr.const constName us _) =>
let env ← getEnv
if !hasOutParams env constName then
pure type
else do
let args := typeBody.getAppArgs
let us ← preprocessLevels us
let c := mkConst constName us
let cType ← inferType c
let args ← preprocessArgs cType 0 args
mkForallFVars xs (mkAppN c args)
| _ => pure type
/-
Remark: when `maxResultSize? == none`, the configuration option `synthInstance.maxResultSize` is used.
Remark: we use a different option for controlling the maximum result size for coercions.
-/
def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (Option Expr) := do profileitM Exception "typeclass inference" (← getOptions) do
let opts ← getOptions
let maxResultSize := maxResultSize?.getD (synthInstance.maxSize.get opts)
let inputConfig ← getConfig
withConfig (fun config => { config with isDefEqStuckEx := true, transparency := TransparencyMode.reducible,
foApprox := true, ctxApprox := true, constApprox := false }) do
let type ← instantiateMVars type
let type ← preprocess type
let s ← get
match s.cache.synthInstance.find? type with
| some result => pure result
| none =>
let result? ← withNewMCtxDepth do
let normType ← preprocessOutParam type
trace[Meta.synthInstance]! "{type} ==> {normType}"
match (← SynthInstance.main normType maxResultSize) with
| none => pure none
| some result =>
trace[Meta.synthInstance]! "FOUND result {result}"
let result ← instantiateMVars result
if (← hasAssignableMVar result) then
trace[Meta.synthInstance]! "Failed has assignable mvar {result.setOption `pp.all true}"
pure none
else
pure (some result)
let result? ← match result? with
| none => pure none
| some result => do
trace[Meta.synthInstance]! "result {result}"
let resultType ← inferType result
if (← withConfig (fun _ => inputConfig) <| isDefEq type resultType) then
let result ← instantiateMVars result
pure (some result)
else
trace[Meta.synthInstance]! "result type{indentExpr resultType}\nis not definitionally equal to{indentExpr type}"
pure none
if type.hasMVar then
pure result?
else do
modify fun s => { s with cache := { s.cache with synthInstance := s.cache.synthInstance.insert type result? } }
pure result?
/--
Return `LOption.some r` if succeeded, `LOption.none` if it failed, and `LOption.undef` if
instance cannot be synthesized right now because `type` contains metavariables. -/
def trySynthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (LOption Expr) := do
catchInternalId isDefEqStuckExceptionId
(toLOptionM <| synthInstance? type maxResultSize?)
(fun _ => pure LOption.undef)
def synthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM Expr :=
catchInternalId isDefEqStuckExceptionId
(do
let result? ← synthInstance? type maxResultSize?
match result? with
| some result => pure result
| none => throwError! "failed to synthesize{indentExpr type}")
(fun _ => throwError! "failed to synthesize{indentExpr type}")
private def synthPendingImp (mvarId : MVarId) (maxResultSize? : Option Nat) : MetaM Bool := do
let mvarDecl ← getMVarDecl mvarId
match mvarDecl.kind with
| MetavarKind.synthetic =>
match (← isClass? mvarDecl.type) with
| none => pure false
| some _ => do
let val? ← catchInternalId isDefEqStuckExceptionId (synthInstance? mvarDecl.type maxResultSize?) (fun _ => pure none)
match val? with
| none => pure false
| some val =>
if (← isExprMVarAssigned mvarId) then
pure false
else
assignExprMVar mvarId val
pure true
| _ => pure false
builtin_initialize
synthPendingRef.set (synthPendingImp · none)
builtin_initialize
registerTraceClass `Meta.synthInstance
registerTraceClass `Meta.synthInstance.globalInstances
registerTraceClass `Meta.synthInstance.newSubgoal
registerTraceClass `Meta.synthInstance.tryResolve
registerTraceClass `Meta.synthInstance.resume
registerTraceClass `Meta.synthInstance.generate
end Lean.Meta
|
197de8f73dd60eb1910fdf3de05f1fe01b4f3120 | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /hott/homotopy/sphere.hlean | 9fad2bb997da558773a3c9eac2911399922c9bd7 | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,773 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Declaration of the n-spheres
-/
import .susp types.trunc
open eq nat susp bool is_trunc unit pointed
/-
We can define spheres with the following possible indices:
- trunc_index (defining S^-2 = S^-1 = empty)
- nat (forgetting that S^-1 = empty)
- nat, but counting wrong (S^0 = empty, S^1 = bool, ...)
- some new type "integers >= -1"
We choose the last option here.
-/
/- Sphere levels -/
inductive sphere_index : Type₀ :=
| minus_one : sphere_index
| succ : sphere_index → sphere_index
namespace trunc_index
definition sub_one [reducible] (n : sphere_index) : trunc_index :=
sphere_index.rec_on n -2 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
end trunc_index
namespace sphere_index
/-
notation for sphere_index is -1, 0, 1, ...
from 0 and up this comes from a coercion from num to sphere_index (via nat)
-/
postfix `.+1`:(max+1) := sphere_index.succ
postfix `.+2`:(max+1) := λ(n : sphere_index), (n .+1 .+1)
notation `-1` := minus_one
export [coercions] nat
notation `ℕ₋₁` := sphere_index
definition add (n m : sphere_index) : sphere_index :=
sphere_index.rec_on m n (λ k l, l .+1)
definition leq (n m : sphere_index) : Type₀ :=
sphere_index.rec_on n (λm, unit) (λ n p m, sphere_index.rec_on m (λ p, empty) (λ m q p, p m) p) m
infix `+1+`:65 := sphere_index.add
notation x <= y := sphere_index.leq x y
notation x ≤ y := sphere_index.leq x y
definition succ_le_succ {n m : sphere_index} (H : n ≤ m) : n.+1 ≤ m.+1 := H
definition le_of_succ_le_succ {n m : sphere_index} (H : n.+1 ≤ m.+1) : n ≤ m := H
definition minus_two_le (n : sphere_index) : -1 ≤ n := star
definition empty_of_succ_le_minus_two {n : sphere_index} (H : n .+1 ≤ -1) : empty := H
definition of_nat [coercion] [reducible] (n : nat) : sphere_index :=
(nat.rec_on n -1 (λ n k, k.+1)).+1
definition trunc_index_of_sphere_index [coercion] [reducible] (n : sphere_index) : trunc_index :=
(sphere_index.rec_on n -2 (λ n k, k.+1)).+1
definition sub_one [reducible] (n : ℕ) : sphere_index :=
nat.rec_on n -1 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
open trunc_index
definition sub_two_eq_sub_one_sub_one (n : ℕ) : n.-2 = n.-1.-1 :=
nat.rec_on n idp (λn p, ap trunc_index.succ p)
end sphere_index
open sphere_index equiv
definition sphere : sphere_index → Type₀
| -1 := empty
| n.+1 := susp (sphere n)
namespace sphere
definition base {n : ℕ} : sphere n := north
definition pointed_sphere [instance] [constructor] (n : ℕ) : pointed (sphere n) :=
pointed.mk base
definition Sphere [constructor] (n : ℕ) : Pointed := pointed.mk' (sphere n)
namespace ops
abbreviation S := sphere
notation `S.`:max := Sphere
end ops
open sphere.ops
definition equator (n : ℕ) : map₊ (S. n) (Ω (S. (succ n))) :=
pmap.mk (λa, merid a ⬝ (merid base)⁻¹) !con.right_inv
definition surf {n : ℕ} : Ω[n] S. n :=
nat.rec_on n (proof base qed)
(begin intro m s, refine cast _ (apn m (equator m) s),
exact ap Pointed.carrier !loop_space_succ_eq_in⁻¹ end)
definition bool_of_sphere : S 0 → bool :=
susp.rec ff tt (λx, empty.elim x)
definition sphere_of_bool : bool → S 0
| ff := north
| tt := south
definition sphere_equiv_bool : S 0 ≃ bool :=
equiv.MK bool_of_sphere
sphere_of_bool
(λb, match b with | tt := idp | ff := idp end)
(λx, susp.rec_on x idp idp (empty.rec _))
definition sphere_eq_bool : S 0 = bool :=
ua sphere_equiv_bool
definition sphere_eq_bool_pointed : S. 0 = Bool :=
Pointed_eq sphere_equiv_bool idp
-- TODO: the commented-out part makes the forward function below "apn _ surf"
definition pmap_sphere (A : Pointed) (n : ℕ) : map₊ (S. n) A ≃ Ω[n] A :=
begin
-- fapply equiv_change_fun,
-- {
revert A, induction n with n IH: intro A,
{ rewrite [sphere_eq_bool_pointed], apply pmap_bool_equiv},
{ refine susp_adjoint_loop (S. n) A ⬝e !IH ⬝e _, rewrite [loop_space_succ_eq_in]}
-- },
-- { intro f, exact apn n f surf},
-- { revert A, induction n with n IH: intro A f,
-- { exact sorry},
-- { exact sorry}}
end
protected definition elim {n : ℕ} {P : Pointed} (p : Ω[n] P) : map₊ (S. n) P :=
to_inv !pmap_sphere p
-- definition elim_surf {n : ℕ} {P : Pointed} (p : Ω[n] P) : apn n (sphere.elim p) surf = p :=
-- begin
-- induction n with n IH,
-- { esimp [apn,surf,sphere.elim,pmap_sphere], apply sorry},
-- { apply sorry}
-- end
end sphere
open sphere sphere.ops
structure weakly_constant [class] {A B : Type} (f : A → B) := --move
(is_weakly_constant : Πa a', f a = f a')
abbreviation wconst := @weakly_constant.is_weakly_constant
namespace is_trunc
open trunc_index
variables {n : ℕ} {A : Type}
definition is_trunc_of_pmap_sphere_constant
(H : Π(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply iff.elim_right !is_trunc_iff_is_contr_loop,
intro a,
apply is_trunc_equiv_closed, apply pmap_sphere,
fapply is_contr.mk,
{ exact pmap.mk (λx, a) idp},
{ intro f, fapply pmap_eq,
{ intro x, esimp, refine !respect_pt⁻¹ ⬝ (!H ⬝ !H⁻¹)},
{ rewrite [▸*,con.right_inv,▸*,con.left_inv]}}
end
definition is_trunc_iff_map_sphere_constant
(H : Π(f : S n → A) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply is_trunc_of_pmap_sphere_constant,
intros, cases f with f p, esimp at *, apply H
end
definition pmap_sphere_constant_of_is_trunc' [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n) : f x = f base :=
begin
let H' := iff.elim_left (is_trunc_iff_is_contr_loop n A) H a,
let H'' := @is_trunc_equiv_closed_rev _ _ _ !pmap_sphere H',
assert p : (f = pmap.mk (λx, f base) (respect_pt f)),
apply is_hprop.elim,
exact ap10 (ap pmap.map p) x
end
definition pmap_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x y : S n) : f x = f y :=
let H := pmap_sphere_constant_of_is_trunc' a f in !H ⬝ !H⁻¹
definition map_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x y : S n) : f x = f y :=
pmap_sphere_constant_of_is_trunc (f base) (pmap.mk f idp) x y
definition map_sphere_constant_of_is_trunc_self [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x : S n) : map_sphere_constant_of_is_trunc f x x = idp :=
!con.right_inv
end is_trunc
|
be882e069df94193e27c2a3d0ae79f102a26c363 | d3aa99b88d7159fbbb8ab10d699374ab7be89e03 | /src/data/equiv/basic.lean | 43ec85b8e32294d2e35500aaa1f254e409a7fc2b | [
"Apache-2.0"
] | permissive | mzinkevi/mathlib | 62e0920edaf743f7fc53aaf42a08e372954af298 | c718a22925872db4cb5f64c36ed6e6a07bdf647c | refs/heads/master | 1,599,359,590,404 | 1,573,098,221,000 | 1,573,098,221,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 42,619 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
In the standard library we cannot assume the univalence axiom.
We say two types are equivalent if they are isomorphic.
Two equivalent types have the same cardinality.
-/
import tactic.split_ifs logic.function logic.unique data.set.function data.bool data.quot
open function
universes u v w
variables {α : Sort u} {β : Sort v} {γ : Sort w}
/-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/
structure equiv (α : Sort*) (β : Sort*) :=
(to_fun : α → β)
(inv_fun : β → α)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
infix ` ≃ `:25 := equiv
def function.involutive.to_equiv {f : α → α} (h : involutive f) : α ≃ α :=
⟨f, f, h.left_inverse, h.right_inverse⟩
namespace equiv
/-- `perm α` is the type of bijections from `α` to itself. -/
@[reducible] def perm (α : Sort*) := equiv α α
instance : has_coe_to_fun (α ≃ β) :=
⟨_, to_fun⟩
@[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f :=
rfl
theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : equiv α β}, (e₁ : α → β) = e₂ → e₁ = e₂
| ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h :=
have f₁ = f₂, from h,
have g₁ = g₂, from funext $ assume x,
have f₁ (g₁ x) = f₂ (g₂ x), from (r₁ x).trans (r₂ x).symm,
have f₁ (g₁ x) = f₁ (g₂ x), by { subst f₂, exact this },
show g₁ x = g₂ x, from injective_of_left_inverse l₁ this,
by simp *
@[ext] lemma ext (f g : equiv α β) (H : ∀ x, f x = g x) : f = g :=
eq_of_to_fun_eq (funext H)
@[ext] lemma perm.ext (σ τ : equiv.perm α) (H : ∀ x, σ x = τ x) : σ = τ :=
equiv.ext _ _ H
@[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩
@[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩
@[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ e₂.inv_fun,
e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩
protected theorem injective : ∀ f : α ≃ β, injective f
| ⟨f, g, h₁, h₂⟩ := injective_of_left_inverse h₁
protected theorem surjective : ∀ f : α ≃ β, surjective f
| ⟨f, g, h₁, h₂⟩ := surjective_of_has_right_inverse ⟨_, h₂⟩
protected theorem bijective (f : α ≃ β) : bijective f :=
⟨f.injective, f.surjective⟩
protected theorem subsingleton (e : α ≃ β) : ∀ [subsingleton β], subsingleton α
| ⟨H⟩ := ⟨λ a b, e.injective (H _ _)⟩
protected def decidable_eq (e : α ≃ β) [H : decidable_eq β] : decidable_eq α
| a b := decidable_of_iff _ e.injective.eq_iff
lemma nonempty_iff_nonempty : α ≃ β → (nonempty α ↔ nonempty β)
| ⟨f, g, _, _⟩ := nonempty.congr f g
protected def cast {α β : Sort*} (h : α = β) : α ≃ β :=
⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩
@[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g :=
rfl
@[simp] theorem refl_apply (x : α) : equiv.refl α x = x := rfl
@[simp] theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl
@[simp] theorem apply_symm_apply : ∀ (e : α ≃ β) (x : β), e (e.symm x) = x
| ⟨f₁, g₁, l₁, r₁⟩ x := by { simp [equiv.symm], rw r₁ }
@[simp] theorem symm_apply_apply : ∀ (e : α ≃ β) (x : α), e.symm (e x) = x
| ⟨f₁, g₁, l₁, r₁⟩ x := by { simp [equiv.symm], rw l₁ }
@[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) :
(f.trans g).symm a = f.symm (g.symm a) := rfl
@[simp] theorem apply_eq_iff_eq : ∀ (f : α ≃ β) (x y : α), f x = f y ↔ x = y
| ⟨f₁, g₁, l₁, r₁⟩ x y := (injective_of_left_inverse l₁).eq_iff
@[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl
lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x :=
(eq_comm.trans e.symm_apply_eq).trans eq_comm
@[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl }
@[simp] theorem symm_symm_apply (e : α ≃ β) (a : α) : e.symm.symm a = e a := by { cases e, refl }
@[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl }
@[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl
@[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl }
@[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext _ _ (by simp)
@[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext _ _ (by simp)
lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) :
(ab.trans bc).trans cd = ab.trans (bc.trans cd) :=
equiv.ext _ _ $ assume a, rfl
theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv
theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv
def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) :=
⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm,
assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end,
assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end, ⟩
def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β :=
equiv_congr e e
protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s :=
set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv
protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s :=
by rw [set.image_subset_iff, e.image_eq_preimage]
lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s :=
by { rw [← set.image_comp], simp }
protected lemma image_compl {α β} (f : equiv α β) (s : set α) :
f '' -s = -(f '' s) :=
set.image_compl_eq f.bijective
/- The group of permutations (self-equivalences) of a type `α` -/
namespace perm
instance perm_group {α : Type u} : group (perm α) :=
begin
refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..};
intros; apply equiv.ext; try { apply trans_apply },
apply symm_apply_apply
end
@[simp] theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) :=
equiv.trans_apply _ _ _
@[simp] theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl
@[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) :
f⁻¹ (f x) = x := equiv.symm_apply_apply _ _
@[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) :
f (f⁻¹ x) = x := equiv.apply_symm_apply _ _
lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl
lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl
lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl
end perm
def equiv_empty (h : α → false) : α ≃ empty :=
⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩
def false_equiv_empty : false ≃ empty :=
equiv_empty _root_.id
def equiv_pempty (h : α → false) : α ≃ pempty :=
⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩
def false_equiv_pempty : false ≃ pempty :=
equiv_pempty _root_.id
def empty_equiv_pempty : empty ≃ pempty :=
equiv_pempty $ empty.rec _
def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} :=
equiv_pempty pempty.elim
def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty :=
equiv_empty $ assume a, h ⟨a⟩
def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty :=
equiv_pempty $ assume a, h ⟨a⟩
def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit :=
⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩
def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial
protected def ulift {α : Type u} : ulift α ≃ α :=
⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩
protected def plift : plift α ≃ α :=
⟨plift.down, plift.up, plift.up_down, plift.down_up⟩
@[congr] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) :
(α₁ → β₁) ≃ (α₂ → β₂) :=
{ to_fun := λ f, e₂.to_fun ∘ f ∘ e₁.inv_fun,
inv_fun := λ f, e₂.inv_fun ∘ f ∘ e₁.to_fun,
left_inv := λ f, funext $ λ x, by { dsimp, rw [e₂.left_inv, e₁.left_inv] },
right_inv := λ f, funext $ λ x, by { dsimp, rw [e₂.right_inv, e₁.right_inv] } }
@[simp] lemma arrow_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂)
(f : α₁ → β₁) (x : α₂) :
arrow_congr e₁ e₂ f x = (e₂ $ f $ e₁.symm x) :=
rfl
lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*}
(ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) :
arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) :=
by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] }
@[simp] lemma arrow_congr_refl {α β : Sort*} :
arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl
@[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*}
(e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) :
arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') :=
rfl
@[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) :
(arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm :=
rfl
def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e
@[simp] lemma conj_apply (e : α ≃ β) (f : α → α) (x : β) :
e.conj f x = (e $ f $ e.symm x) :=
rfl
@[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl
@[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl
@[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) :
(e₁.trans e₂).conj = e₁.conj.trans e₂.conj :=
rfl
@[simp] lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) :
e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) :=
by apply arrow_congr_comp
def punit_equiv_punit : punit.{v} ≃ punit.{w} :=
⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩
section
@[simp] def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} :=
⟨λ f, punit.star, λ u f, punit.star,
λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩
@[simp] def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α :=
⟨λ f, f punit.star, λ a u, a, λ f, by { funext x, cases x, refl }, λ u, rfl⟩
@[simp] def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} :=
⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩
@[simp] def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} :=
⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩
@[simp] def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} :=
calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _)
... ≃ punit : empty_arrow_equiv_punit _
end
@[congr] def prod_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ :β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ :=
⟨λp, (e₁ p.1, e₂ p.2), λp, (e₁.symm p.1, e₂.symm p.2),
λ ⟨a, b⟩, show (e₁.symm (e₁ a), e₂.symm (e₂ b)) = (a, b), by rw [symm_apply_apply, symm_apply_apply],
λ ⟨a, b⟩, show (e₁ (e₁.symm a), e₂ (e₂.symm b)) = (a, b), by rw [apply_symm_apply, apply_symm_apply]⟩
@[simp] theorem prod_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) (b : β₁) :
prod_congr e₁ e₂ (a, b) = (e₁ a, e₂ b) :=
rfl
@[simp] def prod_comm (α β : Sort*) : α × β ≃ β × α :=
⟨λ p, (p.2, p.1), λ p, (p.2, p.1), λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩
@[simp] def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) :=
⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, λp, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩
@[simp] theorem prod_assoc_apply {α β γ : Sort*} (p : (α × β) × γ) :
prod_assoc α β γ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl
section
@[simp] def prod_punit (α : Sort*) : α × punit.{u+1} ≃ α :=
⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩
@[simp] theorem prod_punit_apply {α : Sort*} (a : α × punit.{u+1}) : prod_punit α a = a.1 := rfl
@[simp] def punit_prod (α : Sort*) : punit.{u+1} × α ≃ α :=
calc punit × α ≃ α × punit : prod_comm _ _
... ≃ α : prod_punit _
@[simp] theorem punit_prod_apply {α : Sort*} (a : punit.{u+1} × α) : punit_prod α a = a.2 := rfl
@[simp] def prod_empty (α : Sort*) : α × empty ≃ empty :=
equiv_empty (λ ⟨_, e⟩, e.rec _)
@[simp] def empty_prod (α : Sort*) : empty × α ≃ empty :=
equiv_empty (λ ⟨e, _⟩, e.rec _)
@[simp] def prod_pempty (α : Sort*) : α × pempty ≃ pempty :=
equiv_pempty (λ ⟨_, e⟩, e.rec _)
@[simp] def pempty_prod (α : Sort*) : pempty × α ≃ pempty :=
equiv_pempty (λ ⟨e, _⟩, e.rec _)
end
section
open sum
def psum_equiv_sum (α β : Sort*) : psum α β ≃ α ⊕ β :=
⟨λ s, psum.cases_on s inl inr,
λ s, sum.cases_on s psum.inl psum.inr,
λ s, by cases s; refl,
λ s, by cases s; refl⟩
def sum_congr {α₁ β₁ α₂ β₂ : Sort*} : α₁ ≃ α₂ → β₁ ≃ β₂ → α₁ ⊕ β₁ ≃ α₂ ⊕ β₂
| ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ :=
⟨λ s, match s with inl a₁ := inl (f₁ a₁) | inr b₁ := inr (f₂ b₁) end,
λ s, match s with inl a₂ := inl (g₁ a₂) | inr b₂ := inr (g₂ b₂) end,
λ s, match s with inl a := congr_arg inl (l₁ a) | inr a := congr_arg inr (l₂ a) end,
λ s, match s with inl a := congr_arg inl (r₁ a) | inr a := congr_arg inr (r₂ a) end⟩
@[simp] theorem sum_congr_apply_inl {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) :
sum_congr e₁ e₂ (inl a) = inl (e₁ a) :=
by { cases e₁, cases e₂, refl }
@[simp] theorem sum_congr_apply_inr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (b : β₁) :
sum_congr e₁ e₂ (inr b) = inr (e₂ b) :=
by { cases e₁, cases e₂, refl }
@[simp] lemma sum_congr_symm {α β γ δ : Type u} (e : α ≃ β) (f : γ ≃ δ) (x) :
(equiv.sum_congr e f).symm x = (equiv.sum_congr (e.symm) (f.symm)) x :=
by { cases e, cases f, cases x; refl }
def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} :=
⟨λ b, cond b (inr punit.star) (inl punit.star),
λ s, sum.rec_on s (λ_, ff) (λ_, tt),
λ b, by cases b; refl,
λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩
noncomputable def Prop_equiv_bool : Prop ≃ bool :=
⟨λ p, @to_bool p (classical.prop_decidable _),
λ b, b, λ p, by simp, λ b, by simp⟩
@[simp] def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α :=
⟨λ s, match s with inl a := inr a | inr b := inl b end,
λ s, match s with inl b := inr b | inr a := inl a end,
λ s, by cases s; refl,
λ s, by cases s; refl⟩
@[simp] def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) :=
⟨λ s, match s with inl (inl a) := inl a | inl (inr b) := inr (inl b) | inr c := inr (inr c) end,
λ s, match s with inl a := inl (inl a) | inr (inl b) := inl (inr b) | inr (inr c) := inr c end,
λ s, by rcases s with ⟨_ | _⟩ | _; refl,
λ s, by rcases s with _ | _ | _; refl⟩
@[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl
@[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl
@[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl
@[simp] def sum_empty (α : Sort*) : α ⊕ empty ≃ α :=
⟨λ s, match s with inl a := a | inr e := empty.rec _ e end,
inl,
λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl },
λ a, rfl⟩
@[simp] def empty_sum (α : Sort*) : empty ⊕ α ≃ α :=
(sum_comm _ _).trans $ sum_empty _
@[simp] def sum_pempty (α : Sort*) : α ⊕ pempty ≃ α :=
⟨λ s, match s with inl a := a | inr e := pempty.rec _ e end,
inl,
λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl },
λ a, rfl⟩
@[simp] def pempty_sum (α : Sort*) : pempty ⊕ α ≃ α :=
(sum_comm _ _).trans $ sum_pempty _
@[simp] def option_equiv_sum_punit (α : Sort*) : option α ≃ α ⊕ punit.{u+1} :=
⟨λ o, match o with none := inr punit.star | some a := inl a end,
λ s, match s with inr _ := none | inl a := some a end,
λ o, by cases o; refl,
λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩
def sum_equiv_sigma_bool (α β : Sort*) : α ⊕ β ≃ (Σ b: bool, cond b α β) :=
⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end,
λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end,
λ s, by cases s; refl,
λ s, by rcases s with ⟨_|_, _⟩; refl⟩
def sigma_preimage_equiv {α β : Type*} (f : α → β) :
(Σ y : β, {x // f x = y}) ≃ α :=
⟨λ x, x.2.1, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩
end
section
def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) :=
⟨λ H a, F a (H a), λ H a, (F a).symm (H a),
λ H, funext $ by simp, λ H, funext $ by simp⟩
def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : sigma β, γ x.1 x.2) ≃ (Π a b, γ a b) :=
{ to_fun := λ f x y, f ⟨x,y⟩,
inv_fun := λ f x, f x.1 x.2,
left_inv := λ f, funext $ λ ⟨x,y⟩, rfl,
right_inv := λ f, funext $ λ x, funext $ λ y, rfl }
end
section
def psigma_equiv_sigma {α} (β : α → Sort*) : psigma β ≃ sigma β :=
⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩
def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : sigma β₁ ≃ sigma β₂ :=
⟨λ ⟨a, b⟩, ⟨a, F a b⟩, λ ⟨a, b⟩, ⟨a, (F a).symm b⟩,
λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b,
λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩
def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} : ∀ f : α₁ ≃ α₂, (Σ a:α₁, β (f a)) ≃ (Σ a:α₂, β a)
| ⟨f, g, l, r⟩ :=
⟨λ ⟨a, b⟩, ⟨f a, b⟩, λ ⟨a, b⟩, ⟨g a, @@eq.rec β b (r a).symm⟩,
λ ⟨a, b⟩, match g (f a), l a : ∀ a' (h : a' = a),
@sigma.mk _ (β ∘ f) _ (@@eq.rec β b (congr_arg f h.symm)) = ⟨a, b⟩ with
| _, rfl := rfl end,
λ ⟨a, b⟩, match f (g a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with
| _, rfl := rfl end⟩
def sigma_equiv_prod (α β : Sort*) : (Σ_:α, β) ≃ α × β :=
⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩
def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : ∀ a, β₁ a ≃ β) : sigma β₁ ≃ α × β :=
(sigma_congr_right F).trans (sigma_equiv_prod α β)
end
section
def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) :=
⟨λ f, (λ c, (f c).1, λ c, (f c).2),
λ p c, (p.1 c, p.2 c),
λ f, funext $ λ c, prod.mk.eta,
λ p, by { cases p, refl }⟩
def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) :=
⟨λ f, λ p, f p.1 p.2,
λ f, λ a b, f (a, b),
λ f, rfl,
λ f, by { funext p, cases p, refl }⟩
open sum
def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) :=
⟨λ f, (f ∘ inl, f ∘ inr),
λ p s, sum.rec_on s p.1 p.2,
λ f, by { funext s, cases s; refl },
λ p, by { cases p, refl }⟩
def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) :=
⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end,
λ s, match s with inl (a, c) := (inl a, c) | inr (b, c) := (inr b, c) end,
λ p, by rcases p with ⟨_ | _, _⟩; refl,
λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩
@[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) :
sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl
@[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) :
sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl
def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) :=
calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _
... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _
... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _)
@[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) :
prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl
@[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) :
prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl
def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) :
((Σ i, α i) × β) ≃ (Σ i, (α i × β)) :=
⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩,
λ p, (⟨p.1, p.2.1⟩, p.2.2),
λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl },
λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩
def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α :=
calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _)
... ≃ α × (unit ⊕ unit) : prod_comm _ _
... ≃ (α × unit) ⊕ (α × unit) : prod_sum_distrib _ _ _
... ≃ α ⊕ α : sum_congr (prod_punit _) (prod_punit _)
end
section
open sum nat
def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} :=
⟨λ n, match n with zero := inr punit.star | succ a := inl a end,
λ s, match s with inl n := succ n | inr punit.star := zero end,
λ n, begin cases n, repeat { refl } end,
λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩
@[simp] def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ :=
nat_equiv_nat_sum_punit.symm
def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ :=
by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl}
end
def list_equiv_of_equiv {α β : Type*} : α ≃ β → list α ≃ list β
| ⟨f, g, l, r⟩ :=
by refine ⟨list.map f, list.map g, λ x, _, λ x, _⟩;
simp [id_of_left_inverse l, id_of_right_inverse r]
def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} :=
⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩
def decidable_eq_of_equiv [decidable_eq β] (e : α ≃ β) : decidable_eq α
| a₁ a₂ := decidable_of_iff (e a₁ = e a₂) e.injective.eq_iff
def inhabited_of_equiv [inhabited β] (e : α ≃ β) : inhabited α :=
⟨e.symm (default _)⟩
def unique_of_equiv (e : α ≃ β) (h : unique β) : unique α :=
unique.of_surjective e.symm.surjective
def unique_congr (e : α ≃ β) : unique α ≃ unique β :=
{ to_fun := e.symm.unique_of_equiv,
inv_fun := e.unique_of_equiv,
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
section
open subtype
def subtype_congr {p : α → Prop} {q : β → Prop}
(e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} :=
⟨λ x, ⟨e x.1, (h _).1 x.2⟩,
λ y, ⟨e.symm y.1, (h _).2 (by { simp, exact y.2 })⟩,
λ ⟨x, h⟩, subtype.eq' $ by simp,
λ ⟨y, h⟩, subtype.eq' $ by simp⟩
def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : subtype p ≃ subtype q :=
subtype_congr (equiv.refl _) e
@[simp] lemma subtype_congr_right_mk {p q : α → Prop} (e : ∀x, p x ↔ q x)
{x : α} (h : p x) : subtype_congr_right e ⟨x, h⟩ = ⟨x, (e x).1 h⟩ := rfl
def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) :
{a : α // p a} ≃ {b : β // p (e.symm b)} :=
subtype_congr e $ by simp
def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q :=
subtype_congr (equiv.refl α) (assume a, h ▸ iff.refl _)
def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t :=
subtype_congr_prop h
def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) :
subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } :=
⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩,
λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩,
assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩
def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) :
{x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) :=
(subtype_subtype_equiv_subtype_exists p _).trans $
subtype_congr_right $ λ x, exists_prop
/-- If the outer subtype has more restrictive predicate than the inner one,
then we can drop the latter. -/
def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) :
{x : subtype p // q x.1} ≃ subtype q :=
(subtype_subtype_equiv_subtype_inter p _).trans $
subtype_congr_right $
assume x,
⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩
/-- If a proposition holds for all elements, then the subtype is
equivalent to the original type. -/
def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) :
subtype p ≃ α :=
⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩
/-- A subtype of a sigma-type is a sigma-type over a subtype. -/
def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) :
{ y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 :=
⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩,
λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩,
λ ⟨⟨x, h⟩, y⟩, rfl,
λ ⟨⟨x, y⟩, h⟩, rfl⟩
/-- A sigma type over a subtype is equivalent to the sigma set over the original type,
if the fiber is empty outside of the subset -/
def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop)
(h : ∀ x, p x → q x) :
(Σ x : subtype q, p x) ≃ Σ x : α, p x :=
(subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2
def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop)
(h : ∀ x, p (f x)) :
(Σ y : subtype p, {x : α // f x = y}) ≃ α :=
calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x)
... ≃ α : sigma_preimage_equiv f
def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β)
{p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) :
(Σ y : subtype q, {x : α // f x = y}) ≃ subtype p :=
calc (Σ y : subtype q, {x : α // f x = y}) ≃
Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} :
begin
apply sigma_congr_right,
assume y,
symmetry,
refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _),
assume x,
exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩
end
... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q))
def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) :
(Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } :=
⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end,
assume f, funext $ assume i, rfl,
assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $
eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩
def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} :
{f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } :=
⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩,
by { rintro ⟨f, h⟩, refl },
by { rintro f, funext a, exact subtype.eq' rfl }⟩
end
section
local attribute [elab_with_expected_type] quot.lift
def quot_equiv_of_quot' {r : α → α → Prop} {s : β → β → Prop} (e : α ≃ β)
(h : ∀ a a', r a a' ↔ s (e a) (e a')) : quot r ≃ quot s :=
⟨quot.lift (λ a, quot.mk _ (e a)) (λ a a' H, quot.sound ((h a a').mp H)),
quot.lift (λ b, quot.mk _ (e.symm b)) (λ b b' H, quot.sound ((h _ _).mpr (by convert H; simp))),
quot.ind $ by simp,
quot.ind $ by simp⟩
def quot_equiv_of_quot {r : α → α → Prop} (e : α ≃ β) :
quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) :=
quot_equiv_of_quot' e (by simp)
end
namespace set
open set
protected def univ (α) : @univ α ≃ α :=
⟨subtype.val, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩
@[simp] lemma univ_apply {α : Type u} (x : @univ α) :
equiv.set.univ α x = x := rfl
@[simp] lemma univ_symm_apply {α : Type u} (x : α) :
(equiv.set.univ α).symm x = ⟨x, trivial⟩ := rfl
protected def empty (α) : (∅ : set α) ≃ empty :=
equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h
protected def pempty (α) : (∅ : set α) ≃ pempty :=
equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h
protected def union' {α} {s t : set α}
(p : α → Prop) [decidable_pred p]
(hs : ∀ x ∈ s, p x)
(ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t :=
{ to_fun := λ x, if hp : p x.1
then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩
else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩,
inv_fun := λ o, match o with
| (sum.inl x) := ⟨x.1, or.inl x.2⟩
| (sum.inr x) := ⟨x.1, or.inr x.2⟩
end,
left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr,
right_inv := λ o, begin
rcases o with ⟨x, h⟩ | ⟨x, h⟩;
dsimp [union'._match_1];
[simp [hs _ h], simp [ht _ h]]
end }
protected def union {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅) :
(s ∪ t : set α) ≃ s ⊕ t :=
set.union' s (λ _, id) (λ x xt xs, subset_empty_iff.2 H ⟨xs, xt⟩)
lemma union_apply_left {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ :=
dif_pos ha
lemma union_apply_right {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ :=
dif_neg (show ↑a ∉ s, by finish [set.ext_iff])
protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} :=
⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩,
λ ⟨x, h⟩, by { simp at h, subst x },
λ ⟨⟩, rfl⟩
protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t :=
{ to_fun := λ x, ⟨x.1, h ▸ x.2⟩,
inv_fun := λ x, ⟨x.1, h.symm ▸ x.2⟩,
left_inv := λ _, subtype.eq rfl,
right_inv := λ _, subtype.eq rfl }
@[simp] lemma of_eq_apply {α : Type u} {s t : set α} (h : s = t) (a : s) :
equiv.set.of_eq h a = ⟨a, h ▸ a.2⟩ := rfl
@[simp] lemma of_eq_symm_apply {α : Type u} {s t : set α} (h : s = t) (a : t) :
(equiv.set.of_eq h).symm a = ⟨a, h.symm ▸ a.2⟩ := rfl
protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) :
(insert a s : set α) ≃ s ⊕ punit.{u+1} :=
calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp)
... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.ext_iff])
... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _)
protected def sum_compl {α} (s : set α) [decidable_pred s] : s ⊕ (-s : set α) ≃ α :=
calc s ⊕ (-s : set α) ≃ ↥(s ∪ -s) : (equiv.set.union (by simp [set.ext_iff])).symm
... ≃ @univ α : equiv.set.of_eq (by simp)
... ≃ α : equiv.set.univ _
@[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) :
equiv.set.sum_compl s (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : -s) :
equiv.set.sum_compl s (sum.inr x) = x := rfl
lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α}
(hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ :=
have ↑(⟨x, or.inl hx⟩ : (s ∪ -s : set α)) ∈ s, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this }
lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α}
(hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ :=
have ↑(⟨x, or.inr hx⟩ : (s ∪ -s : set α)) ∈ -s, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this }
protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] :
(s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t :=
calc (s ∪ t : set α) ⊕ (s ∩ t : set α)
≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self]
... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) :
sum_congr (set.union (inter_diff_self _ _)) (equiv.refl _)
... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _
... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin
refine (set.union' (∉ s) _ _).symm,
exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1]
end
... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] }
protected def prod {α β} (s : set α) (t : set β) :
s.prod t ≃ s × t :=
⟨λp, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩,
λp, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩,
λ ⟨⟨x, y⟩, ⟨h₁, h₂⟩⟩, rfl,
λ ⟨⟨x, h₁⟩, ⟨y, h₂⟩⟩, rfl⟩
protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) :
s ≃ (f '' s) :=
⟨λ ⟨x, h⟩, ⟨f x, mem_image_of_mem f h⟩,
λ ⟨y, h⟩, ⟨classical.some h, (classical.some_spec h).1⟩,
λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h
(classical.some_spec (mem_image_of_mem f h)).2),
λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩
protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) :=
equiv.set.image_of_inj_on f s (λ x y hx hy hxy, H hxy)
@[simp] theorem image_apply {α β} (f : α → β) (s : set α) (H : injective f) (a h) :
set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl
protected noncomputable def range {α β} (f : α → β) (H : injective f) :
α ≃ range f :=
{ to_fun := λ x, ⟨f x, mem_range_self _⟩,
inv_fun := λ x, classical.some x.2,
left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)),
right_inv := λ x, subtype.eq $ classical.some_spec x.2 }
@[simp] theorem range_apply {α β} (f : α → β) (H : injective f) (a) :
set.range f H a = ⟨f a, set.mem_range_self _⟩ := rfl
protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β :=
⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩
protected def sep {α : Type u} (s : set α) (t : α → Prop) :
({ x ∈ s | t x } : set α) ≃ { x : s | t x.1 } :=
(equiv.subtype_subtype_equiv_subtype_inter s t).symm
end set
noncomputable def of_bijective {α β} {f : α → β} (hf : bijective f) : α ≃ β :=
⟨f, λ x, classical.some (hf.2 x), λ x, hf.1 (classical.some_spec (hf.2 (f x))),
λ x, classical.some_spec (hf.2 x)⟩
@[simp] theorem of_bijective_to_fun {α β} {f : α → β} (hf : bijective f) : (of_bijective hf : α → β) = f := rfl
def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α]
[s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧)
(h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) :
{x // p₂ x} ≃ quotient s₂ :=
{ to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧)
(λ a b hab, hfunext (by rw quotient.sound hab)
(λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2,
inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x}))
(λ a b hab, subtype.eq' (quotient.sound ((h _ _).1 hab))),
left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha,
right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) }
section swap
variable [decidable_eq α]
open decidable
def swap_core (a b r : α) : α :=
if r = a then b
else if r = b then a
else r
theorem swap_core_self (r a : α) : swap_core a a r = r :=
by { unfold swap_core, split_ifs; cc }
theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r :=
by { unfold swap_core, split_ifs; cc }
theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r :=
by { unfold swap_core, split_ifs; cc }
/-- `swap a b` is the permutation that swaps `a` and `b` and
leaves other values as is. -/
def swap (a b : α) : perm α :=
⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩
theorem swap_self (a : α) : swap a a = equiv.refl _ :=
eq_of_to_fun_eq $ funext $ λ r, swap_core_self r a
theorem swap_comm (a b : α) : swap a b = swap b a :=
eq_of_to_fun_eq $ funext $ λ r, swap_core_comm r _ _
theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x :=
rfl
@[simp] theorem swap_apply_left (a b : α) : swap a b a = b :=
if_pos rfl
@[simp] theorem swap_apply_right (a b : α) : swap a b b = a :=
by { by_cases b = a; simp [swap_apply_def, *] }
theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x :=
by simp [swap_apply_def] {contextual := tt}
@[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ :=
eq_of_to_fun_eq $ funext $ λ x, swap_core_swap_core _ _ _
theorem swap_comp_apply {a b x : α} (π : perm α) :
π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x :=
by { cases π, refl }
@[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) :
(swap x y)⁻¹ = swap x y := rfl
@[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α)
(e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) :=
equiv.ext _ _ (λ x, begin
have : ∀ a, e.symm x = a ↔ x = e a :=
λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * },
simp [swap_apply_def, this],
split_ifs; simp
end)
@[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 :=
equiv.swap_swap i j
@[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a :=
by rw [← perm.mul_apply, swap_mul_self, perm.one_apply]
/-- Augment an equivalence with a prescribed mapping `f a = b` -/
def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β :=
(swap a (f.symm b)).trans f
@[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b :=
by { dsimp [set_value], simp [swap_apply_left] }
end swap
protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β)
(h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) :=
begin
split; intros h₂ x,
{ rw [←f.right_inv x], apply h.mp, apply h₂ },
apply h.mpr, apply h₂
end
end equiv
instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton
instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton
instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq
instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq
def unique_unique_equiv : unique (unique α) ≃ unique α :=
{ to_fun := λ h, h.default,
inv_fun := λ h, { default := h, uniq := λ _, subsingleton.elim _ _ },
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β :=
{ to_fun := λ _, default β,
inv_fun := λ _, default α,
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
def equiv_punit_of_unique [unique α] : α ≃ punit.{v} :=
equiv_of_unique_of_unique
namespace quot
protected def congr {α β} {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β)
(eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) :
quot ra ≃ quot rb :=
{ to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1),
inv_fun := quot.map e.symm
(assume b₁ b₂ h,
(eq (e.symm b₁) (e.symm b₂)).2
((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)),
left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] },
right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } }
/-- Quotients are congruent on equivalences under equality of their relation.
An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/
protected def congr_right {α} {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) :
quot r ≃ quot r' :=
quot.congr (equiv.refl α) eq
end quot
namespace quotient
protected def congr {α β} {ra : setoid α} {rb : setoid β} (e : α ≃ β)
(eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) :
quotient ra ≃ quotient rb :=
quot.congr e eq
protected def congr_right {α} {r r' : setoid α}
(eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' :=
quot.congr_right eq
end quotient
|
2352aeeff8009ee4b82dc4cf488642e07a8f1762 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/uniform_space/complete_separated.lean | 47c05bccd5af3e2bb9d9febe6306dfc7a2a0d160 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 1,345 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.uniform_space.cauchy
import topology.uniform_space.separation
import topology.dense_embedding
/-!
# Theory of complete separated uniform spaces.
This file is for elementary lemmas that depend on both Cauchy filters and separation.
-/
open filter
open_locale topological_space filter
variables {α : Type*}
/-In a separated space, a complete set is closed -/
lemma is_complete.is_closed [uniform_space α] [separated_space α] {s : set α} (h : is_complete s) :
is_closed s :=
is_closed_iff_cluster_pt.2 $ λ a ha, begin
let f := 𝓝[s] a,
have : cauchy f := cauchy_nhds.mono' ha inf_le_left,
rcases h f this (inf_le_right) with ⟨y, ys, fy⟩,
rwa (tendsto_nhds_unique' ha inf_le_left fy : a = y)
end
namespace dense_inducing
open filter
variables [topological_space α] {β : Type*} [topological_space β]
variables {γ : Type*} [uniform_space γ] [complete_space γ] [separated_space γ]
lemma continuous_extend_of_cauchy {e : α → β} {f : α → γ}
(de : dense_inducing e) (h : ∀ b : β, cauchy (map f (comap e $ 𝓝 b))) :
continuous (de.extend f) :=
de.continuous_extend $ λ b, complete_space.complete (h b)
end dense_inducing
|
a99ed9c99ed2f730dd8a1e2779f19f9fcebf99b0 | 41ebf3cb010344adfa84907b3304db00e02db0a6 | /uexp/src/uexp/rules/pushJoinThroughUnionOnRight.lean | 84864bcce04afd63385b551b40a595ddb4c478b1 | [
"BSD-2-Clause"
] | permissive | ReinierKoops/Cosette | e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb | eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29 | refs/heads/master | 1,686,483,953,198 | 1,624,293,498,000 | 1,624,293,498,000 | 378,997,885 | 0 | 0 | BSD-2-Clause | 1,624,293,485,000 | 1,624,293,484,000 | null | UTF-8 | Lean | false | false | 1,841 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..meta.UDP
set_option profiler true
open Expr
open Proj
open Pred
open SQL
open tree
notation `int` := datatypes.int
definition rule:
forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp),
denoteSQL
((SELECT1 (right⋅left⋅emp_sal) FROM1 (product (table rel_emp) (((SELECT * FROM1 (table rel_emp) )) UNION ALL ((SELECT * FROM1 (table rel_emp) )))) ) : SQL Γ _) =
denoteSQL ((SELECT1 (right⋅right⋅emp_sal) FROM1 (((SELECT * FROM1 (product (table rel_emp) (table rel_emp)) )) UNION ALL ((SELECT * FROM1 (product (table rel_emp) (table rel_emp)) ))) ) : SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
print_size,
simp,
print_size,
UDP,
end |
8178e19e9bc8143e7b870e474178fa87b7775714 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/field_theory/intermediate_field.lean | 3d2bdc156c9cd354e4736347b35bc833b701be69 | [
"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 | 14,752 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import field_theory.subfield
import field_theory.tower
import ring_theory.algebraic
/-!
# Intermediate fields
Let `L / K` be a field extension, given as an instance `algebra K L`.
This file defines the type of fields in between `K` and `L`, `intermediate_field K L`.
An `intermediate_field K L` is a subfield of `L` which contains (the image of) `K`,
i.e. it is a `subfield L` and a `subalgebra K L`.
## Main definitions
* `intermediate_field K L` : the type of intermediate fields between `K` and `L`.
* `subalgebra.to_intermediate_field`: turns a subalgebra closed under `⁻¹`
into an intermediate field
* `subfield.to_intermediate_field`: turns a subfield containing the image of `K`
into an intermediate field
* `intermediate_field.map`: map an intermediate field along an `alg_hom`
## Implementation notes
Intermediate fields are defined with a structure extending `subfield` and `subalgebra`.
A `subalgebra` is closed under all operations except `⁻¹`,
## Tags
intermediate field, field extension
-/
open finite_dimensional
open_locale big_operators
variables (K L : Type*) [field K] [field L] [algebra K L]
/-- `S : intermediate_field K L` is a subset of `L` such that there is a field
tower `L / S / K`. -/
structure intermediate_field extends subalgebra K L :=
(neg_mem' : ∀ x ∈ carrier, -x ∈ carrier)
(inv_mem' : ∀ x ∈ carrier, x⁻¹ ∈ carrier)
/-- Reinterpret an `intermediate_field` as a `subalgebra`. -/
add_decl_doc intermediate_field.to_subalgebra
variables {K L} (S : intermediate_field K L)
namespace intermediate_field
/-- Reinterpret an `intermediate_field` as a `subfield`. -/
def to_subfield : subfield L := { ..S.to_subalgebra, ..S }
instance : set_like (intermediate_field K L) L :=
⟨λ S, S.to_subalgebra.carrier, by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨h⟩, congr, }⟩
@[simp]
lemma mem_carrier {s : intermediate_field K L} {x : L} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
/-- Two intermediate fields are equal if they have the same elements. -/
@[ext] theorem ext {S T : intermediate_field K L} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T :=
set_like.ext h
@[simp] lemma coe_to_subalgebra : (S.to_subalgebra : set L) = S := rfl
@[simp] lemma coe_to_subfield : (S.to_subfield : set L) = S := rfl
@[simp] lemma mem_mk (s : set L) (hK : ∀ x, algebra_map K L x ∈ s)
(ho hm hz ha hn hi) (x : L) :
x ∈ intermediate_field.mk (subalgebra.mk s ho hm hz ha hK) hn hi ↔ x ∈ s := iff.rfl
@[simp] lemma mem_to_subalgebra (s : intermediate_field K L) (x : L) :
x ∈ s.to_subalgebra ↔ x ∈ s := iff.rfl
@[simp] lemma mem_to_subfield (s : intermediate_field K L) (x : L) :
x ∈ s.to_subfield ↔ x ∈ s := iff.rfl
/-- An intermediate field contains the image of the smaller field. -/
theorem algebra_map_mem (x : K) : algebra_map K L x ∈ S :=
S.algebra_map_mem' x
/-- An intermediate field contains the ring's 1. -/
theorem one_mem : (1 : L) ∈ S := S.one_mem'
/-- An intermediate field contains the ring's 0. -/
theorem zero_mem : (0 : L) ∈ S := S.zero_mem'
/-- An intermediate field is closed under multiplication. -/
theorem mul_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x * y ∈ S := S.mul_mem'
/-- An intermediate field is closed under scalar multiplication. -/
theorem smul_mem {y : L} : y ∈ S → ∀ {x : K}, x • y ∈ S := S.to_subalgebra.smul_mem
/-- An intermediate field is closed under addition. -/
theorem add_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x + y ∈ S := S.add_mem'
/-- An intermediate field is closed under subtraction -/
theorem sub_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S :=
S.to_subfield.sub_mem hx hy
/-- An intermediate field is closed under negation. -/
theorem neg_mem : ∀ {x : L}, x ∈ S → -x ∈ S := S.neg_mem'
/-- An intermediate field is closed under inverses. -/
theorem inv_mem : ∀ {x : L}, x ∈ S → x⁻¹ ∈ S := S.inv_mem'
/-- An intermediate field is closed under division. -/
theorem div_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x / y ∈ S :=
S.to_subfield.div_mem hx hy
/-- Product of a list of elements in an intermediate_field is in the intermediate_field. -/
lemma list_prod_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.prod ∈ S :=
S.to_subfield.list_prod_mem
/-- Sum of a list of elements in an intermediate field is in the intermediate_field. -/
lemma list_sum_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.sum ∈ S :=
S.to_subfield.list_sum_mem
/-- Product of a multiset of elements in an intermediate field is in the intermediate_field. -/
lemma multiset_prod_mem (m : multiset L) :
(∀ a ∈ m, a ∈ S) → m.prod ∈ S :=
S.to_subfield.multiset_prod_mem m
/-- Sum of a multiset of elements in a `intermediate_field` is in the `intermediate_field`. -/
lemma multiset_sum_mem (m : multiset L) :
(∀ a ∈ m, a ∈ S) → m.sum ∈ S :=
S.to_subfield.multiset_sum_mem m
/-- Product of elements of an intermediate field indexed by a `finset` is in the intermediate_field.
-/
lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) :
∏ i in t, f i ∈ S :=
S.to_subfield.prod_mem h
/-- Sum of elements in a `intermediate_field` indexed by a `finset` is in the `intermediate_field`.
-/
lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) :
∑ i in t, f i ∈ S :=
S.to_subfield.sum_mem h
lemma pow_mem {x : L} (hx : x ∈ S) : ∀ (n : ℤ), x^n ∈ S
| (n : ℕ) := by { rw zpow_coe_nat, exact S.to_subfield.pow_mem hx _, }
| -[1+ n] := by { rw [zpow_neg_succ_of_nat],
exact S.to_subfield.inv_mem (S.to_subfield.pow_mem hx _) }
lemma zsmul_mem {x : L} (hx : x ∈ S) (n : ℤ) :
n • x ∈ S := S.to_subfield.zsmul_mem hx n
lemma coe_int_mem (n : ℤ) : (n : L) ∈ S :=
by simp only [← zsmul_one, zsmul_mem, one_mem]
end intermediate_field
/-- Turn a subalgebra closed under inverses into an intermediate field -/
def subalgebra.to_intermediate_field (S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) :
intermediate_field K L :=
{ neg_mem' := λ x, S.neg_mem,
inv_mem' := inv_mem,
.. S }
@[simp] lemma to_subalgebra_to_intermediate_field
(S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) :
(S.to_intermediate_field inv_mem).to_subalgebra = S :=
by { ext, refl }
@[simp] lemma to_intermediate_field_to_subalgebra
(S : intermediate_field K L) (inv_mem : ∀ x ∈ S.to_subalgebra, x⁻¹ ∈ S) :
S.to_subalgebra.to_intermediate_field inv_mem = S :=
by { ext, refl }
/-- Turn a subfield of `L` containing the image of `K` into an intermediate field -/
def subfield.to_intermediate_field (S : subfield L)
(algebra_map_mem : ∀ x, algebra_map K L x ∈ S) :
intermediate_field K L :=
{ algebra_map_mem' := algebra_map_mem,
.. S }
namespace intermediate_field
/-- An intermediate field inherits a field structure -/
instance to_field : field S :=
S.to_subfield.to_field
@[simp, norm_cast] lemma coe_add (x y : S) : (↑(x + y) : L) = ↑x + ↑y := rfl
@[simp, norm_cast] lemma coe_neg (x : S) : (↑(-x) : L) = -↑x := rfl
@[simp, norm_cast] lemma coe_mul (x y : S) : (↑(x * y) : L) = ↑x * ↑y := rfl
@[simp, norm_cast] lemma coe_inv (x : S) : (↑(x⁻¹) : L) = (↑x)⁻¹ := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : S) : L) = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : S) : L) = 1 := rfl
@[simp, norm_cast] lemma coe_pow (x : S) (n : ℕ) : (↑(x ^ n) : L) = ↑x ^ n :=
begin
induction n with n ih,
{ simp },
{ simp [pow_succ, ih] }
end
/-! `intermediate_field`s inherit structure from their `subalgebra` coercions. -/
instance module' {R} [semiring R] [has_scalar R K] [module R L] [is_scalar_tower R K L] :
module R S :=
S.to_subalgebra.module'
instance module : module K S := S.to_subalgebra.module
instance is_scalar_tower {R} [semiring R] [has_scalar R K] [module R L]
[is_scalar_tower R K L] :
is_scalar_tower R K S :=
S.to_subalgebra.is_scalar_tower
@[simp] lemma coe_smul {R} [semiring R] [has_scalar R K] [module R L] [is_scalar_tower R K L]
(r : R) (x : S) :
↑(r • x) = (r • x : L) := rfl
instance algebra' {K'} [comm_semiring K'] [has_scalar K' K] [algebra K' L]
[is_scalar_tower K' K L] :
algebra K' S :=
S.to_subalgebra.algebra'
instance algebra : algebra K S := S.to_subalgebra.algebra
instance to_algebra {R : Type*} [semiring R] [algebra L R] : algebra S R :=
S.to_subalgebra.to_algebra
instance is_scalar_tower_bot {R : Type*} [semiring R] [algebra L R] :
is_scalar_tower S L R :=
is_scalar_tower.subalgebra _ _ _ S.to_subalgebra
instance is_scalar_tower_mid {R : Type*} [semiring R] [algebra L R] [algebra K R]
[is_scalar_tower K L R] : is_scalar_tower K S R :=
is_scalar_tower.subalgebra' _ _ _ S.to_subalgebra
/-- Specialize `is_scalar_tower_mid` to the common case where the top field is `L` -/
instance is_scalar_tower_mid' : is_scalar_tower K S L :=
S.is_scalar_tower_mid
variables {L' : Type*} [field L'] [algebra K L']
/-- If `f : L →+* L'` fixes `K`, `S.map f` is the intermediate field between `L'` and `K`
such that `x ∈ S ↔ f x ∈ S.map f`. -/
def map (f : L →ₐ[K] L') : intermediate_field K L' :=
{ inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, S.inv_mem hx, f.map_inv x⟩ },
neg_mem' := λ x hx, (S.to_subalgebra.map f).neg_mem hx,
.. S.to_subalgebra.map f}
/-- The embedding from an intermediate field of `L / K` to `L`. -/
def val : S →ₐ[K] L :=
S.to_subalgebra.val
@[simp] theorem coe_val : ⇑S.val = coe := rfl
@[simp] lemma val_mk {x : L} (hx : x ∈ S) : S.val ⟨x, hx⟩ = x := rfl
variables {S}
lemma to_subalgebra_injective {S S' : intermediate_field K L}
(h : S.to_subalgebra = S'.to_subalgebra) : S = S' :=
by { ext, rw [← mem_to_subalgebra, ← mem_to_subalgebra, h] }
variables (S)
lemma set_range_subset : set.range (algebra_map K L) ⊆ S :=
S.to_subalgebra.range_subset
lemma field_range_le : (algebra_map K L).field_range ≤ S.to_subfield :=
λ x hx, S.to_subalgebra.range_subset (by rwa [set.mem_range, ← ring_hom.mem_field_range])
@[simp] lemma to_subalgebra_le_to_subalgebra {S S' : intermediate_field K L} :
S.to_subalgebra ≤ S'.to_subalgebra ↔ S ≤ S' := iff.rfl
@[simp] lemma to_subalgebra_lt_to_subalgebra {S S' : intermediate_field K L} :
S.to_subalgebra < S'.to_subalgebra ↔ S < S' := iff.rfl
variables {S}
section tower
/-- Lift an intermediate_field of an intermediate_field -/
def lift1 {F : intermediate_field K L} (E : intermediate_field K F) : intermediate_field K L :=
map E (val F)
/-- Lift an intermediate_field of an intermediate_field -/
def lift2 {F : intermediate_field K L} (E : intermediate_field F L) : intermediate_field K L :=
{ carrier := E.carrier,
zero_mem' := zero_mem E,
add_mem' := λ x y, add_mem E,
neg_mem' := λ x, neg_mem E,
one_mem' := one_mem E,
mul_mem' := λ x y, mul_mem E,
inv_mem' := λ x, inv_mem E,
algebra_map_mem' := λ x, algebra_map_mem E (algebra_map K F x) }
instance has_lift1 {F : intermediate_field K L} :
has_lift_t (intermediate_field K F) (intermediate_field K L) := ⟨lift1⟩
instance has_lift2 {F : intermediate_field K L} :
has_lift_t (intermediate_field F L) (intermediate_field K L) := ⟨lift2⟩
@[simp] lemma mem_lift2 {F : intermediate_field K L} {E : intermediate_field F L} {x : L} :
x ∈ (↑E : intermediate_field K L) ↔ x ∈ E := iff.rfl
/-- This was formerly an instance called `lift2_alg`, but an instance above already provides it. -/
example {F : intermediate_field K L} {E : intermediate_field F L} : algebra K E :=
by apply_instance
lemma lift2_algebra_map {F : intermediate_field K L} {E : intermediate_field F L} :
algebra_map K E = (algebra_map F E).comp (algebra_map K F) := rfl
instance lift2_tower {F : intermediate_field K L} {E : intermediate_field F L} :
is_scalar_tower K F E :=
E.is_scalar_tower
/-- `lift2` is isomorphic to the original `intermediate_field`. -/
def lift2_alg_equiv {F : intermediate_field K L} (E : intermediate_field F L) :
(↑E : intermediate_field K L) ≃ₐ[K] E :=
alg_equiv.refl
end tower
section finite_dimensional
variables (F E : intermediate_field K L)
instance finite_dimensional_left [finite_dimensional K L] : finite_dimensional K F :=
finite_dimensional.finite_dimensional_submodule F.to_subalgebra.to_submodule
instance finite_dimensional_right [finite_dimensional K L] : finite_dimensional F L :=
right K F L
@[simp] lemma dim_eq_dim_subalgebra :
module.rank K F.to_subalgebra = module.rank K F := rfl
@[simp] lemma finrank_eq_finrank_subalgebra :
finrank K F.to_subalgebra = finrank K F := rfl
variables {F} {E}
@[simp] lemma to_subalgebra_eq_iff : F.to_subalgebra = E.to_subalgebra ↔ F = E :=
by { rw [set_like.ext_iff, set_like.ext'_iff, set.ext_iff], refl }
lemma eq_of_le_of_finrank_le [finite_dimensional K L] (h_le : F ≤ E)
(h_finrank : finrank K E ≤ finrank K F) : F = E :=
to_subalgebra_injective $ subalgebra.to_submodule_injective $ eq_of_le_of_finrank_le h_le h_finrank
lemma eq_of_le_of_finrank_eq [finite_dimensional K L] (h_le : F ≤ E)
(h_finrank : finrank K F = finrank K E) : F = E :=
eq_of_le_of_finrank_le h_le h_finrank.ge
lemma eq_of_le_of_finrank_le' [finite_dimensional K L] (h_le : F ≤ E)
(h_finrank : finrank F L ≤ finrank E L) : F = E :=
begin
apply eq_of_le_of_finrank_le h_le,
have h1 := finrank_mul_finrank K F L,
have h2 := finrank_mul_finrank K E L,
have h3 : 0 < finrank E L := finrank_pos,
nlinarith,
end
lemma eq_of_le_of_finrank_eq' [finite_dimensional K L] (h_le : F ≤ E)
(h_finrank : finrank F L = finrank E L) : F = E :=
eq_of_le_of_finrank_le' h_le h_finrank.le
end finite_dimensional
end intermediate_field
/-- If `L/K` is algebraic, the `K`-subalgebras of `L` are all fields. -/
def subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L) :
subalgebra K L ≃o intermediate_field K L :=
{ to_fun := λ S, S.to_intermediate_field (λ x hx, S.inv_mem_of_algebraic (alg (⟨x, hx⟩ : S))),
inv_fun := λ S, S.to_subalgebra,
left_inv := λ S, to_subalgebra_to_intermediate_field _ _,
right_inv := λ S, to_intermediate_field_to_subalgebra _ _,
map_rel_iff' := λ S S', iff.rfl }
@[simp] lemma mem_subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L)
{S : subalgebra K L} {x : L} :
x ∈ subalgebra_equiv_intermediate_field alg S ↔ x ∈ S :=
iff.rfl
@[simp] lemma mem_subalgebra_equiv_intermediate_field_symm (alg : algebra.is_algebraic K L)
{S : intermediate_field K L} {x : L} :
x ∈ (subalgebra_equiv_intermediate_field alg).symm S ↔ x ∈ S :=
iff.rfl
|
10fe5cabb5e8a1cd2bda5e03ca6931dfc22b7ecc | 88fb7558b0636ec6b181f2a548ac11ad3919f8a5 | /tests/lean/run/listex2.lean | e8b7e38be87077ae1a6a8a053b583f4268f781b3 | [
"Apache-2.0"
] | permissive | moritayasuaki/lean | 9f666c323cb6fa1f31ac597d777914aed41e3b7a | ae96ebf6ee953088c235ff7ae0e8c95066ba8001 | refs/heads/master | 1,611,135,440,814 | 1,493,852,869,000 | 1,493,852,869,000 | 90,269,903 | 0 | 0 | null | 1,493,906,291,000 | 1,493,906,291,000 | null | UTF-8 | Lean | false | false | 2,415 | lean | import tools.super
universe variable u
constant in_tail {α : Type u} {a : α} (b : α) {l : list α} : a ∈ l → a ∈ b::l
constant in_head {α : Type u} (a : α) (l : list α) : a ∈ a::l
constant in_left {α : Type u} {a : α} {l : list α} (r : list α) : a ∈ l → a ∈ l ++ r
constant in_right {α : Type u} {a : α} (l : list α) {r : list α} : a ∈ r → a ∈ l ++ r
open expr tactic
declare_trace search_mem_list
meta def match_append (e : expr) : tactic (expr × expr) :=
do [_, _, l, r] ← match_app_of e ``has_append.append | failed, return (l, r)
meta def match_cons (e : expr) : tactic (expr × expr) :=
do [_, a, t] ← match_app_of e ``list.cons | failed, return (a, t)
meta def match_mem (e : expr) : tactic (expr × expr) :=
do [_, _, _, a, t] ← match_app_of e ``has_mem.mem | failed, return (a, t)
meta def search_mem_list : expr → expr → tactic expr
| a e := when_tracing `search_mem_list (do f₁ ← pp a, f₂ ← pp e, trace (to_fmt "search " ++ f₁ ++ to_fmt " in " ++ f₂)) >>
(do m ← to_expr `(%%a ∈ %%e), find_assumption m)
<|>
(do (l, r) ← match_append e, h ← search_mem_list a l, to_expr `(in_left %%r %%h))
<|>
(do (l, r) ← match_append e, h ← search_mem_list a r, to_expr `(in_right %%l %%h))
<|>
(do (b, t) ← match_cons e, is_def_eq a b, to_expr `(in_head %%b %%t))
<|>
(do (b, t) ← match_cons e, h ← search_mem_list a t, to_expr `(in_tail %%b %%h))
meta def mk_mem_list : tactic unit :=
do t ← target,
(a, l) ← match_mem t,
search_mem_list a l >>= exact
example (a b c : nat) : a ∈ [b, c] ++ [b, a, b] :=
by mk_mem_list
example (a b c : nat) : a ∈ [b, c] ++ [b, a+0, b] :=
by mk_mem_list
example (a b c : nat) : a ∈ [b, c] ++ [b, c, c] ++ [b, a+0, b] :=
by mk_mem_list
example (a b c : nat) (l : list nat) : a ∈ l → a ∈ [b, c] ++ b::l :=
by tactic.intros >> mk_mem_list
set_option trace.search_mem_list true
lemma ex1 (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] :=
by tactic.intros >> mk_mem_list
set_option trace.smt.ematch true
/- Using ematching -/
lemma ex2 (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] :=
begin [smt]
intros,
add_lemma [in_left, in_right, in_head, in_tail],
repeat {ematch} -- It will loop if there is a matching loop
end
|
1180dc0caa090ad092f490828a1d49af676444ca | 4d3f29a7b2eff44af8fd0d3176232e039acb9ee3 | /Mathlib/Mathlib/Tactic/Split.lean | ea38e80c3b401dc63ce114edbf5d54ca1761162a | [] | no_license | marijnheule/lamr | 5fc5d69d326ff92e321242cfd7f72e78d7f99d7e | 28cc4114c7361059bb54f407fa312bf38b48728b | refs/heads/main | 1,689,338,013,620 | 1,630,359,632,000 | 1,630,359,632,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,117 | lean | /-
Copyright (c) 2021 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import Lean.Meta.Tactic.Apply
import Lean.Elab.Tactic.Basic
import Lean.Elab.SyntheticMVars
def Lean.Meta.split (mvarId : MVarId) : MetaM (List MVarId) := do
withMVarContext mvarId do
checkNotAssigned mvarId `split
let target ← getMVarType' mvarId
matchConstInduct target.getAppFn
(fun _ => throwTacticEx `split mvarId "target is not an inductive datatype")
fun ival us => do
match ival.ctors with
| [ctor] => apply mvarId (mkConst ctor us)
| _ => throwError "split failed, goal must be an inductive type with only one constructor {indentExpr target}"
namespace Lean.Elab
namespace Tactic
open Meta
elab "split" : tactic => withMainContext do
let mvarIds' ← Meta.split (← getMainGoal)
Term.synthesizeSyntheticMVarsNoPostponing
replaceMainGoal mvarIds'
-- TODO: we can't implement `fsplit`
-- until the TODO in `Lean.Meta.apply` (in core) for `ApplyNewGoals` is completed.
end Tactic
end Lean.Elab
|
9590059466d3cb5b371f03f65d3e67c57d37a0af | 534c92d7322a8676cfd1583e26f5946134561b54 | /src/Exercises/01_Propositions/Q0101/S0001.lean | 01f501a1dd2b503108307f35e2ef686c30498dcf | [
"Apache-2.0"
] | permissive | kbuzzard/mathematics-in-lean | 53f387174f04d6077f434e27c407aee9425837f7 | 3fad7bb7e888dabef94921101af8671b78a4304a | refs/heads/master | 1,586,812,457,439 | 1,546,893,744,000 | 1,546,893,744,000 | 163,450,734 | 8 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 109 | lean | example (P Q R : Prop) (HP : P) (HQ : Q) : P :=
begin
exact HP,
-- assumption would also have worked
end
|
93e98e5f2e330eb870e44d8447fdf86c4796b053 | 90edd5cdcf93124fe15627f7304069fdce3442dd | /src/Lean/Meta/Tactic/Simp/SimpLemmas.lean | fe09ba35cb411151c04b873b1ef67877c4d284eb | [
"Apache-2.0"
] | permissive | JLimperg/lean4-aesop | 8a9d9cd3ee484a8e67fda2dd9822d76708098712 | 5c4b9a3e05c32f69a4357c3047c274f4b94f9c71 | refs/heads/master | 1,689,415,944,104 | 1,627,383,284,000 | 1,627,383,284,000 | 377,536,770 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,150 | 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.ScopedEnvExtension
import Lean.Util.Recognizers
import Lean.Meta.LevelDefEq
import Lean.Meta.DiscrTree
import Lean.Meta.AppBuilder
import Lean.Meta.Tactic.AuxLemma
namespace Lean.Meta
/--
The fields `levelParams` and `proof` are used to encode the proof of the simp lemma.
If the `proof` is a global declaration `c`, we store `Expr.const c []` at `proof` without the universe levels, and `levelParams` is set to `#[]`
When using the lemma, we create fresh universe metavariables.
Motivation: most simp lemmas are global declarations, and this approach is faster and saves memory.
The field `levelParams` is not empty only when we elaborate an expression provided by the user, and it contains universe metavariables.
Then, we use `abstractMVars` to abstract the universe metavariables and create new fresh universe parameters that are stored at the field `levelParams`.
-/
structure SimpLemma where
keys : Array DiscrTree.Key
levelParams : Array Name -- non empty for local universe polymorhic proofs.
proof : Expr
priority : Nat
post : Bool
perm : Bool -- true is lhs and rhs are identical modulo permutation of variables
name? : Option Name := none -- for debugging and tracing purposes
deriving Inhabited
def SimpLemma.getName (s : SimpLemma) : Name :=
match s.name? with
| some n => n
| none => "<unknown>"
instance : ToFormat SimpLemma where
format s :=
let perm := if s.perm then ":perm" else ""
let name := fmt s.getName
let prio := f!":{s.priority}"
name ++ prio ++ perm
instance : ToMessageData SimpLemma where
toMessageData s := fmt s
instance : BEq SimpLemma where
beq e₁ e₂ := e₁.proof == e₂.proof
structure SimpLemmas where
pre : DiscrTree SimpLemma := DiscrTree.empty
post : DiscrTree SimpLemma := DiscrTree.empty
lemmaNames : Std.PHashSet Name := {}
toUnfold : Std.PHashSet Name := {}
erased : Std.PHashSet Name := {}
deriving Inhabited
def addSimpLemmaEntry (d : SimpLemmas) (e : SimpLemma) : SimpLemmas :=
if e.post then
{ d with post := d.post.insertCore e.keys e, lemmaNames := updateLemmaNames d.lemmaNames }
else
{ d with pre := d.pre.insertCore e.keys e, lemmaNames := updateLemmaNames d.lemmaNames }
where
updateLemmaNames (s : Std.PHashSet Name) : Std.PHashSet Name :=
match e.name? with
| none => s
| some name => s.insert name
def SimpLemmas.addDeclToUnfold (d : SimpLemmas) (declName : Name) : SimpLemmas :=
{ d with toUnfold := d.toUnfold.insert declName }
def SimpLemmas.isDeclToUnfold (d : SimpLemmas) (declName : Name) : Bool :=
d.toUnfold.contains declName
def SimpLemmas.isLemma (d : SimpLemmas) (declName : Name) : Bool :=
d.lemmaNames.contains declName
def SimpLemmas.eraseCore [Monad m] [MonadError m] (d : SimpLemmas) (declName : Name) : m SimpLemmas := do
return { d with erased := d.erased.insert declName, lemmaNames := d.lemmaNames.erase declName, toUnfold := d.toUnfold.erase declName }
def SimpLemmas.erase [Monad m] [MonadError m] (d : SimpLemmas) (declName : Name) : m SimpLemmas := do
unless d.isLemma declName || d.isDeclToUnfold declName do
throwError "'{declName}' does not have [simp] attribute"
d.eraseCore declName
inductive SimpEntry where
| lemma : SimpLemma → SimpEntry
| toUnfold : Name → SimpEntry
deriving Inhabited
builtin_initialize simpExtension : SimpleScopedEnvExtension SimpEntry SimpLemmas ←
registerSimpleScopedEnvExtension {
name := `simpExt
initial := {}
addEntry := fun d e =>
match e with
| SimpEntry.lemma e => addSimpLemmaEntry d e
| SimpEntry.toUnfold n => d.addDeclToUnfold n
}
private partial def isPerm : Expr → Expr → MetaM Bool
| Expr.app f₁ a₁ _, Expr.app f₂ a₂ _ => isPerm f₁ f₂ <&&> isPerm a₁ a₂
| Expr.mdata _ s _, t => isPerm s t
| s, Expr.mdata _ t _ => isPerm s t
| s@(Expr.mvar ..), t@(Expr.mvar ..) => isDefEq s t
| Expr.forallE n₁ d₁ b₁ _, Expr.forallE n₂ d₂ b₂ _ => isPerm d₁ d₂ <&&> withLocalDeclD n₁ d₁ fun x => isPerm (b₁.instantiate1 x) (b₂.instantiate1 x)
| Expr.lam n₁ d₁ b₁ _, Expr.lam n₂ d₂ b₂ _ => isPerm d₁ d₂ <&&> withLocalDeclD n₁ d₁ fun x => isPerm (b₁.instantiate1 x) (b₂.instantiate1 x)
| Expr.letE n₁ t₁ v₁ b₁ _, Expr.letE n₂ t₂ v₂ b₂ _ =>
isPerm t₁ t₂ <&&> isPerm v₁ v₂ <&&> withLetDecl n₁ t₁ v₁ fun x => isPerm (b₁.instantiate1 x) (b₂.instantiate1 x)
| Expr.proj _ i₁ b₁ _, Expr.proj _ i₂ b₂ _ => i₁ == i₂ <&&> isPerm b₁ b₂
| s, t => s == t
private partial def shouldPreprocess (type : Expr) : MetaM Bool :=
forallTelescopeReducing type fun xs result => return !result.isEq
private partial def preprocess (e type : Expr) : MetaM (List (Expr × Expr)) := do
let type ← whnf type
if type.isForall then
forallTelescopeReducing type fun xs type => do
let e := mkAppN e xs
let ps ← preprocess e type
ps.mapM fun (e, type) =>
return (← mkLambdaFVars xs e, ← mkForallFVars xs type)
else if type.isEq then
return [(e, type)]
else if let some (lhs, rhs) := type.iff? then
let type ← mkEq lhs rhs
let e ← mkPropExt e
return [(e, type)]
else if let some (_, lhs, rhs) := type.ne? then
let type ← mkEq (← mkEq lhs rhs) (mkConst ``False)
let e ← mkEqFalse e
return [(e, type)]
else if let some p := type.not? then
let type ← mkEq p (mkConst ``False)
let e ← mkEqFalse e
return [(e, type)]
else if let some (type₁, type₂) := type.and? then
let e₁ := mkProj ``And 0 e
let e₂ := mkProj ``And 1 e
return (← preprocess e₁ type₁) ++ (← preprocess e₂ type₂)
else
let type ← mkEq type (mkConst ``True)
let e ← mkEqTrue e
return [(e, type)]
private def checkTypeIsProp (type : Expr) : MetaM Unit :=
unless (← isProp type) do
throwError "invalid 'simp', proposition expected{indentExpr type}"
def mkSimpLemmaCore (e : Expr) (levelParams : Array Name) (proof : Expr) (post : Bool) (prio : Nat) (name? : Option Name) : MetaM SimpLemma := do
let type ← instantiateMVars (← inferType e)
withNewMCtxDepth do
let (xs, _, type) ← withReducible <| forallMetaTelescopeReducing type
let type ← whnfR type
let (keys, perm) ←
match type.eq? with
| some (_, lhs, rhs) => pure (← DiscrTree.mkPath lhs, ← isPerm lhs rhs)
| none => throwError "unexpected kind of 'simp' theorem{indentExpr type}"
return { keys := keys, perm := perm, post := post, levelParams := levelParams, proof := proof, name? := name?, priority := prio }
def mkSimpLemmasFromConst (declName : Name) (post : Bool) (prio : Nat) : MetaM (Array SimpLemma) := do
let cinfo ← getConstInfo declName
let val := mkConst declName (cinfo.levelParams.map mkLevelParam)
withReducible do
let type ← inferType val
checkTypeIsProp type
if (← shouldPreprocess type) then
let mut r := #[]
for (val, type) in (← preprocess val type) do
let auxName ← mkAuxLemma cinfo.levelParams type val
r := r.push <| (← mkSimpLemmaCore (mkConst auxName (cinfo.levelParams.map mkLevelParam)) #[] (mkConst auxName) post prio declName)
return r
else
#[← mkSimpLemmaCore (mkConst declName (cinfo.levelParams.map mkLevelParam)) #[] (mkConst declName) post prio declName]
def addSimpLemma (declName : Name) (post : Bool) (attrKind : AttributeKind) (prio : Nat) : MetaM Unit := do
let simpLemmas ← mkSimpLemmasFromConst declName post prio
for simpLemma in simpLemmas do
simpExtension.add (SimpEntry.lemma simpLemma) attrKind
builtin_initialize
registerBuiltinAttribute {
name := `simp
descr := "simplification theorem"
add := fun declName stx attrKind =>
let go : MetaM Unit := do
let info ← getConstInfo declName
if (← isProp info.type) then
let post :=
if stx[1].isNone then true else stx[1][0].getKind == ``Lean.Parser.Tactic.simpPost
let prio ← getAttrParamOptPrio stx[2]
addSimpLemma declName post attrKind prio
else if info.hasValue then
simpExtension.add (SimpEntry.toUnfold declName) attrKind
else
throwError "invalid 'simp', it is not a proposition nor a definition (to unfold)"
discard <| go.run {} {}
erase := fun declName => do
let s ← simpExtension.getState (← getEnv)
let s ← s.erase declName
modifyEnv fun env => simpExtension.modifyState env fun _ => s
}
def getSimpLemmas : MetaM SimpLemmas :=
return simpExtension.getState (← getEnv)
/- Auxiliary method for adding a global declaration to a `SimpLemmas` datastructure. -/
def SimpLemmas.addConst (s : SimpLemmas) (declName : Name) (post : Bool := true) (prio : Nat := eval_prio default) : MetaM SimpLemmas := do
let simpLemmas ← mkSimpLemmasFromConst declName post prio
return simpLemmas.foldl addSimpLemmaEntry s
def SimpLemma.getValue (simpLemma : SimpLemma) : MetaM Expr := do
if simpLemma.proof.isConst && simpLemma.levelParams.isEmpty then
let info ← getConstInfo simpLemma.proof.constName!
if info.levelParams.isEmpty then
return simpLemma.proof
else
return simpLemma.proof.updateConst! (← info.levelParams.mapM (fun _ => mkFreshLevelMVar))
else
let us ← simpLemma.levelParams.mapM fun _ => mkFreshLevelMVar
simpLemma.proof.instantiateLevelParamsArray simpLemma.levelParams us
private def preprocessProof (val : Expr) : MetaM (Array Expr) := do
let type ← inferType val
checkTypeIsProp type
let ps ← preprocess val type
return ps.toArray.map fun (val, _) => val
/- Auxiliary method for creating simp lemmas from a proof term `val`. -/
def mkSimpLemmas (levelParams : Array Name) (proof : Expr) (post : Bool := true) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM (Array SimpLemma) :=
withReducible do
(← preprocessProof proof).mapM fun val => mkSimpLemmaCore val levelParams val post prio name?
/- Auxiliary method for adding a local simp lemma to a `SimpLemmas` datastructure. -/
def SimpLemmas.add (s : SimpLemmas) (levelParams : Array Name) (proof : Expr) (post : Bool := true) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM SimpLemmas := do
if proof.isConst then
s.addConst proof.constName! post prio
else
let simpLemmas ← mkSimpLemmas levelParams proof post prio (← getName? proof)
return simpLemmas.foldl addSimpLemmaEntry s
where
getName? (e : Expr) : MetaM (Option Name) := do
match name? with
| some _ => return name?
| none =>
let f := e.getAppFn
if f.isConst then
return f.constName!
else if f.isFVar then
let localDecl ← getFVarLocalDecl f
return localDecl.userName
else
return none
end Lean.Meta
|
b980d1e70ab8d55421f3945a1a74fa85b89d4459 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/analytic/uniqueness.lean | 8668e4da3b970960894c1489314de9f07bfd8387 | [
"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 | 5,996 | lean | /-
Copyright (c) 2022 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.analytic.linear
import analysis.analytic.composition
import analysis.normed_space.completion
/-!
# Uniqueness principle for analytic functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We show that two analytic functions which coincide around a point coincide on whole connected sets,
in `analytic_on.eq_on_of_preconnected_of_eventually_eq`.
-/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F]
open set
open_locale topology ennreal
namespace analytic_on
/-- If an analytic function vanishes around a point, then it is uniformly zero along
a connected set. Superseded by `eq_on_zero_of_preconnected_of_locally_zero` which does not assume
completeness of the target space. -/
theorem eq_on_zero_of_preconnected_of_eventually_eq_zero_aux [complete_space F]
{f : E → F} {U : set E} (hf : analytic_on 𝕜 f U) (hU : is_preconnected U) {z₀ : E}
(h₀ : z₀ ∈ U) (hfz₀ : f =ᶠ[𝓝 z₀] 0) : eq_on f 0 U :=
begin
/- Let `u` be the set of points around which `f` vanishes. It is clearly open. We have to show
that its limit points in `U` still belong to it, from which the inclusion `U ⊆ u` will follow
by connectedness. -/
let u := {x | f =ᶠ[𝓝 x] 0},
suffices main : closure u ∩ U ⊆ u,
{ have Uu : U ⊆ u, from
hU.subset_of_closure_inter_subset is_open_set_of_eventually_nhds ⟨z₀, h₀, hfz₀⟩ main,
assume z hz,
simpa using mem_of_mem_nhds (Uu hz) },
/- Take a limit point `x`, then a ball `B (x, r)` on which it has a power series expansion, and
then `y ∈ B (x, r/2) ∩ u`. Then `f` has a power series expansion on `B (y, r/2)` as it is
contained in `B (x, r)`. All the coefficients in this series expansion vanish, as `f` is zero on a
neighborhood of `y`. Therefore, `f` is zero on `B (y, r/2)`. As this ball contains `x`, it follows
that `f` vanishes on a neighborhood of `x`, proving the claim. -/
rintros x ⟨xu, xU⟩,
rcases hf x xU with ⟨p, r, hp⟩,
obtain ⟨y, yu, hxy⟩ : ∃ y ∈ u, edist x y < r / 2,
from emetric.mem_closure_iff.1 xu (r / 2) (ennreal.half_pos hp.r_pos.ne'),
let q := p.change_origin (y - x),
have has_series : has_fpower_series_on_ball f q y (r / 2),
{ have A : (‖y - x‖₊ : ℝ≥0∞) < r / 2, by rwa [edist_comm, edist_eq_coe_nnnorm_sub] at hxy,
have := hp.change_origin (A.trans_le ennreal.half_le_self),
simp only [add_sub_cancel'_right] at this,
apply this.mono (ennreal.half_pos hp.r_pos.ne'),
apply ennreal.le_sub_of_add_le_left ennreal.coe_ne_top,
apply (add_le_add (A.le) (le_refl (r / 2))).trans (le_of_eq _),
exact ennreal.add_halves _ },
have M : emetric.ball y (r / 2) ∈ 𝓝 x, from emetric.is_open_ball.mem_nhds hxy,
filter_upwards [M] with z hz,
have A : has_sum (λ (n : ℕ), q n (λ (i : fin n), z - y)) (f z) := has_series.has_sum_sub hz,
have B : has_sum (λ (n : ℕ), q n (λ (i : fin n), z - y)) (0),
{ have : has_fpower_series_at 0 q y, from has_series.has_fpower_series_at.congr yu,
convert has_sum_zero,
ext n,
exact this.apply_eq_zero n _ },
exact has_sum.unique A B
end
/-- The *identity principle* for analytic functions: If an analytic function vanishes in a whole
neighborhood of a point `z₀`, then it is uniformly zero along a connected set. For a one-dimensional
version assuming only that the function vanishes at some points arbitrarily close to `z₀`, see
`eq_on_zero_of_preconnected_of_frequently_eq_zero`. -/
theorem eq_on_zero_of_preconnected_of_eventually_eq_zero
{f : E → F} {U : set E} (hf : analytic_on 𝕜 f U) (hU : is_preconnected U) {z₀ : E}
(h₀ : z₀ ∈ U) (hfz₀ : f =ᶠ[𝓝 z₀] 0) :
eq_on f 0 U :=
begin
let F' := uniform_space.completion F,
set e : F →L[𝕜] F' := uniform_space.completion.to_complL,
have : analytic_on 𝕜 (e ∘ f) U := λ x hx, (e.analytic_at _).comp (hf x hx),
have A : eq_on (e ∘ f) 0 U,
{ apply eq_on_zero_of_preconnected_of_eventually_eq_zero_aux this hU h₀,
filter_upwards [hfz₀] with x hx,
simp only [hx, function.comp_app, pi.zero_apply, map_zero] },
assume z hz,
have : e (f z) = e 0, by simpa only using A hz,
exact uniform_space.completion.coe_injective F this,
end
/-- The *identity principle* for analytic functions: If two analytic functions coincide in a whole
neighborhood of a point `z₀`, then they coincide globally along a connected set.
For a one-dimensional version assuming only that the functions coincide at some points
arbitrarily close to `z₀`, see `eq_on_of_preconnected_of_frequently_eq`. -/
theorem eq_on_of_preconnected_of_eventually_eq
{f g : E → F} {U : set E} (hf : analytic_on 𝕜 f U) (hg : analytic_on 𝕜 g U)
(hU : is_preconnected U) {z₀ : E} (h₀ : z₀ ∈ U) (hfg : f =ᶠ[𝓝 z₀] g) :
eq_on f g U :=
begin
have hfg' : (f - g) =ᶠ[𝓝 z₀] 0 := hfg.mono (λ z h, by simp [h]),
simpa [sub_eq_zero] using
λ z hz, (hf.sub hg).eq_on_zero_of_preconnected_of_eventually_eq_zero hU h₀ hfg' hz,
end
/-- The *identity principle* for analytic functions: If two analytic functions on a normed space
coincide in a neighborhood of a point `z₀`, then they coincide everywhere.
For a one-dimensional version assuming only that the functions coincide at some points
arbitrarily close to `z₀`, see `eq_of_frequently_eq`. -/
theorem eq_of_eventually_eq {f g : E → F} [preconnected_space E]
(hf : analytic_on 𝕜 f univ) (hg : analytic_on 𝕜 g univ) {z₀ : E} (hfg : f =ᶠ[𝓝 z₀] g) :
f = g :=
funext (λ x, eq_on_of_preconnected_of_eventually_eq hf hg is_preconnected_univ
(mem_univ z₀) hfg (mem_univ x))
end analytic_on
|
61ed840c35a65bb59e9bd85453ace0ea6aa1519d | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/algebra/continuous_monoid_hom.lean | 2f544699a9b787e91ddcf5396b00e1e665fe52b6 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 7,283 | lean | /-
Copyright (c) 2022 Thomas Browning. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning
-/
import topology.continuous_function.algebra
/-!
# Continuous Monoid Homs
This file defines the space of continuous homomorphisms between two topological groups.
## Main definitions
* `continuous_monoid_hom A B`: The continuous homomorphisms `A →* B`.
-/
variables (A B C D E : Type*)
[monoid A] [monoid B] [monoid C] [monoid D] [comm_group E]
[topological_space A] [topological_space B] [topological_space C] [topological_space D]
[topological_space E] [topological_group E]
set_option old_structure_cmd true
/-- Continuous homomorphisms between two topological groups. -/
structure continuous_monoid_hom extends A →* B, continuous_map A B
/-- Continuous homomorphisms between two topological groups. -/
structure continuous_add_monoid_hom (A B : Type*) [add_monoid A] [add_monoid B]
[topological_space A] [topological_space B] extends A →+ B, continuous_map A B
attribute [to_additive] continuous_monoid_hom
attribute [to_additive] continuous_monoid_hom.to_monoid_hom
initialize_simps_projections continuous_monoid_hom (to_fun → apply)
/-- Reinterpret a `continuous_monoid_hom` as a `monoid_hom`. -/
add_decl_doc continuous_monoid_hom.to_monoid_hom
/-- Reinterpret a `continuous_monoid_hom` as a `continuous_map`. -/
add_decl_doc continuous_monoid_hom.to_continuous_map
/-- Reinterpret a `continuous_add_monoid_hom` as an `add_monoid_hom`. -/
add_decl_doc continuous_add_monoid_hom.to_add_monoid_hom
/-- Reinterpret a `continuous_add_monoid_hom` as a `continuous_map`. -/
add_decl_doc continuous_add_monoid_hom.to_continuous_map
namespace continuous_monoid_hom
variables {A B C D E}
@[to_additive] instance : has_coe_to_fun (continuous_monoid_hom A B) (λ _, A → B) :=
⟨continuous_monoid_hom.to_fun⟩
@[to_additive] lemma ext {f g : continuous_monoid_hom A B} (h : ∀ x, f x = g x) : f = g :=
by cases f; cases g; congr; exact funext h
/-- Construct a `continuous_monoid_hom` from a `continuous` `monoid_hom`. -/
@[to_additive "Construct a `continuous_add_monoid_hom` from a `continuous` `add_monoid_hom`.",
simps]
def mk' (f : A →* B) (hf : continuous f) : continuous_monoid_hom A B := { .. f }
/-- Composition of two continuous homomorphisms. -/
@[to_additive "Composition of two continuous homomorphisms.", simps]
def comp (g : continuous_monoid_hom B C) (f : continuous_monoid_hom A B) :
continuous_monoid_hom A C :=
mk' (g.to_monoid_hom.comp f.to_monoid_hom) (g.continuous_to_fun.comp f.continuous_to_fun)
/-- Product of two continuous homomorphisms on the same space. -/
@[to_additive "Product of two continuous homomorphisms on the same space.", simps]
def prod (f : continuous_monoid_hom A B) (g : continuous_monoid_hom A C) :
continuous_monoid_hom A (B × C) :=
mk' (f.to_monoid_hom.prod g.to_monoid_hom) (f.continuous_to_fun.prod_mk g.continuous_to_fun)
/-- Product of two continuous homomorphisms on different spaces. -/
@[to_additive "Product of two continuous homomorphisms on different spaces.", simps]
def prod_map (f : continuous_monoid_hom A C) (g : continuous_monoid_hom B D) :
continuous_monoid_hom (A × B) (C × D) :=
mk' (f.to_monoid_hom.prod_map g.to_monoid_hom) (f.continuous_to_fun.prod_map g.continuous_to_fun)
variables (A B C D E)
/-- The trivial continuous homomorphism. -/
@[to_additive "The trivial continuous homomorphism.", simps]
def one : continuous_monoid_hom A B := mk' 1 continuous_const
@[to_additive] instance : inhabited (continuous_monoid_hom A B) := ⟨one A B⟩
/-- The identity continuous homomorphism. -/
@[to_additive "The identity continuous homomorphism.", simps]
def id : continuous_monoid_hom A A := mk' (monoid_hom.id A) continuous_id
/-- The continuous homomorphism given by projection onto the first factor. -/
@[to_additive "The continuous homomorphism given by projection onto the first factor.", simps]
def fst : continuous_monoid_hom (A × B) A := mk' (monoid_hom.fst A B) continuous_fst
/-- The continuous homomorphism given by projection onto the second factor. -/
@[to_additive "The continuous homomorphism given by projection onto the second factor.", simps]
def snd : continuous_monoid_hom (A × B) B := mk' (monoid_hom.snd A B) continuous_snd
/-- The continuous homomorphism given by inclusion of the first factor. -/
@[to_additive "The continuous homomorphism given by inclusion of the first factor.", simps]
def inl : continuous_monoid_hom A (A × B) := prod (id A) (one A B)
/-- The continuous homomorphism given by inclusion of the second factor. -/
@[to_additive "The continuous homomorphism given by inclusion of the second factor.", simps]
def inr : continuous_monoid_hom B (A × B) := prod (one B A) (id B)
/-- The continuous homomorphism given by the diagonal embedding. -/
@[to_additive "The continuous homomorphism given by the diagonal embedding.", simps]
def diag : continuous_monoid_hom A (A × A) := prod (id A) (id A)
/-- The continuous homomorphism given by swapping components. -/
@[to_additive "The continuous homomorphism given by swapping components.", simps]
def swap : continuous_monoid_hom (A × B) (B × A) := prod (snd A B) (fst A B)
/-- The continuous homomorphism given by multiplication. -/
@[to_additive "The continuous homomorphism given by addition.", simps]
def mul : continuous_monoid_hom (E × E) E :=
mk' mul_monoid_hom continuous_mul
/-- The continuous homomorphism given by inversion. -/
@[to_additive "The continuous homomorphism given by negation.", simps]
def inv : continuous_monoid_hom E E :=
mk' comm_group.inv_monoid_hom continuous_inv
variables {A B C D E}
/-- Coproduct of two continuous homomorphisms to the same space. -/
@[to_additive "Coproduct of two continuous homomorphisms to the same space.", simps]
def coprod (f : continuous_monoid_hom A E) (g : continuous_monoid_hom B E) :
continuous_monoid_hom (A × B) E :=
(mul E).comp (f.prod_map g)
@[to_additive] instance : comm_group (continuous_monoid_hom A E) :=
{ mul := λ f g, (mul E).comp (f.prod g),
mul_comm := λ f g, ext (λ x, mul_comm (f x) (g x)),
mul_assoc := λ f g h, ext (λ x, mul_assoc (f x) (g x) (h x)),
one := one A E,
one_mul := λ f, ext (λ x, one_mul (f x)),
mul_one := λ f, ext (λ x, mul_one (f x)),
inv := λ f, (inv E).comp f,
mul_left_inv := λ f, ext (λ x, mul_left_inv (f x)) }
instance : topological_space (continuous_monoid_hom A B) :=
topological_space.induced to_continuous_map continuous_map.compact_open
variables (A B C D E)
lemma is_inducing : inducing (to_continuous_map : continuous_monoid_hom A B → C(A, B)) := ⟨rfl⟩
lemma is_embedding : embedding (to_continuous_map : continuous_monoid_hom A B → C(A, B)) :=
⟨is_inducing A B, λ _ _, ext ∘ continuous_map.ext_iff.mp⟩
variables {A B C D E}
instance [locally_compact_space A] [t2_space B] : t2_space (continuous_monoid_hom A B) :=
(is_embedding A B).t2_space
instance : topological_group (continuous_monoid_hom A E) :=
let hi := is_inducing A E, hc := hi.continuous in
{ continuous_mul := hi.continuous_iff.mpr (continuous_mul.comp (continuous.prod_map hc hc)),
continuous_inv := hi.continuous_iff.mpr (continuous_inv.comp hc) }
end continuous_monoid_hom
|
a1b04051befed6d6ef30067c52be46e8b6075367 | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /stage0/src/Lean/Elab/Structure.lean | 71e07d2873133ea6c1de0a27cdc788ddeec25100 | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,275 | 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.Parser.Command
import Lean.Meta.Closure
import Lean.Elab.Command
import Lean.Elab.DeclModifiers
import Lean.Elab.DeclUtil
import Lean.Elab.Inductive
import Lean.Elab.DeclarationRange
namespace Lean.Elab.Command
open Meta
/- Recall that the `structure command syntax is
```
parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> optional (" := " >> optional structCtor >> structFields)
```
-/
structure StructCtorView where
ref : Syntax
modifiers : Modifiers
inferMod : Bool -- true if `{}` is used in the constructor declaration
name : Name
declName : Name
structure StructFieldView where
ref : Syntax
modifiers : Modifiers
binderInfo : BinderInfo
inferMod : Bool
declName : Name
name : Name
binders : Syntax
type? : Option Syntax
value? : Option Syntax
structure StructView where
ref : Syntax
modifiers : Modifiers
scopeLevelNames : List Name -- All `universe` declarations in the current scope
allUserLevelNames : List Name -- `scopeLevelNames` ++ explicit universe parameters provided in the `structure` command
isClass : Bool
declName : Name
scopeVars : Array Expr -- All `variable` declaration in the current scope
params : Array Expr -- Explicit parameters provided in the `structure` command
parents : Array Syntax
type : Syntax
ctor : StructCtorView
fields : Array StructFieldView
inductive StructFieldKind where
| newField | fromParent | subobject
deriving Inhabited
structure StructFieldInfo where
name : Name
declName : Name -- Remark: this field value doesn't matter for fromParent fields.
fvar : Expr
kind : StructFieldKind
inferMod : Bool := false
value? : Option Expr := none
deriving Inhabited
def StructFieldInfo.isFromParent (info : StructFieldInfo) : Bool :=
match info.kind with
| StructFieldKind.fromParent => true
| _ => false
def StructFieldInfo.isSubobject (info : StructFieldInfo) : Bool :=
match info.kind with
| StructFieldKind.subobject => true
| _ => false
/- Auxiliary declaration for `mkProjections` -/
structure ProjectionInfo where
declName : Name
inferMod : Bool
structure ElabStructResult where
decl : Declaration
projInfos : List ProjectionInfo
projInstances : List Name -- projections (to parent classes) that must be marked as instances.
mctx : MetavarContext
lctx : LocalContext
localInsts : LocalInstances
defaultAuxDecls : Array (Name × Expr × Expr)
private def defaultCtorName := `mk
/-
The structure constructor syntax is
```
parser! try (declModifiers >> ident >> optional inferMod >> " :: ")
```
-/
private def expandCtor (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM StructCtorView := do
let useDefault := do
let declName := structDeclName ++ defaultCtorName
addAuxDeclarationRanges declName structStx[2] structStx[2]
pure { ref := structStx, modifiers := {}, inferMod := false, name := defaultCtorName, declName := declName }
if structStx[5].isNone then
useDefault
else
let optCtor := structStx[5][1]
if optCtor.isNone then
useDefault
else
let ctor := optCtor[0]
withRef ctor do
let ctorModifiers ← elabModifiers ctor[0]
checkValidCtorModifier ctorModifiers
if ctorModifiers.isPrivate && structModifiers.isPrivate then
throwError "invalid 'private' constructor in a 'private' structure"
if ctorModifiers.isProtected && structModifiers.isPrivate then
throwError "invalid 'protected' constructor in a 'private' structure"
let inferMod := !ctor[2].isNone
let name := ctor[1].getId
let declName := structDeclName ++ name
let declName ← applyVisibility ctorModifiers.visibility declName
addDocString' declName ctorModifiers.docString?
addAuxDeclarationRanges declName ctor[1] ctor[1]
pure { ref := ctor, name := name, modifiers := ctorModifiers, inferMod := inferMod, declName := declName }
def checkValidFieldModifier (modifiers : Modifiers) : TermElabM Unit := do
if modifiers.isNoncomputable then
throwError "invalid use of 'noncomputable' in field declaration"
if modifiers.isPartial then
throwError "invalid use of 'partial' in field declaration"
if modifiers.isUnsafe then
throwError "invalid use of 'unsafe' in field declaration"
if modifiers.attrs.size != 0 then
throwError "invalid use of attributes in field declaration"
if modifiers.isPrivate then
throwError "private fields are not supported yet"
/-
```
def structExplicitBinder := parser! atomic (declModifiers true >> "(") >> many1 ident >> optional inferMod >> optDeclSig >> optional Term.binderDefault >> ")"
def structImplicitBinder := parser! atomic (declModifiers true >> "{") >> many1 ident >> optional inferMod >> declSig >> "}"
def structInstBinder := parser! atomic (declModifiers true >> "[") >> many1 ident >> optional inferMod >> declSig >> "]"
def structSimpleBinder := parser! atomic (declModifiers true >> ident) >> optional inferMod >> optDeclSig >> optional Term.binderDefault
def structFields := parser! many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
```
-/
private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM (Array StructFieldView) :=
let fieldBinders := if structStx[5].isNone then #[] else structStx[5][2][0].getArgs
fieldBinders.foldlM (init := #[]) fun (views : Array StructFieldView) fieldBinder => withRef fieldBinder do
let mut fieldBinder := fieldBinder
if fieldBinder.getKind == ``Parser.Command.structSimpleBinder then
fieldBinder := Syntax.node ``Parser.Command.structExplicitBinder
#[ fieldBinder[0], mkAtomFrom fieldBinder "(", mkNullNode #[ fieldBinder[1] ], fieldBinder[2], fieldBinder[3], fieldBinder[4], mkAtomFrom fieldBinder ")" ]
let k := fieldBinder.getKind
let binfo ←
if k == ``Parser.Command.structExplicitBinder then pure BinderInfo.default
else if k == ``Parser.Command.structImplicitBinder then pure BinderInfo.implicit
else if k == ``Parser.Command.structInstBinder then pure BinderInfo.instImplicit
else throwError "unexpected kind of structure field"
let fieldModifiers ← elabModifiers fieldBinder[0]
checkValidFieldModifier fieldModifiers
if fieldModifiers.isPrivate && structModifiers.isPrivate then
throwError "invalid 'private' field in a 'private' structure"
if fieldModifiers.isProtected && structModifiers.isPrivate then
throwError "invalid 'protected' field in a 'private' structure"
let inferMod := !fieldBinder[3].isNone
let (binders, type?) :=
if binfo == BinderInfo.default then
expandOptDeclSig fieldBinder[4]
else
let (binders, type) := expandDeclSig fieldBinder[4]
(binders, some type)
let value? :=
if binfo != BinderInfo.default then none
else
let optBinderDefault := fieldBinder[5]
if optBinderDefault.isNone then none
else
-- binderDefault := parser! " := " >> termParser
some optBinderDefault[0][1]
let idents := fieldBinder[2].getArgs
idents.foldlM (init := views) fun (views : Array StructFieldView) ident => withRef ident do
let name := ident.getId
if isInternalSubobjectFieldName name then
throwError! "invalid field name '{name}', identifiers starting with '_' are reserved to the system"
let declName := structDeclName ++ name
let declName ← applyVisibility fieldModifiers.visibility declName
addDocString' declName fieldModifiers.docString?
return views.push {
ref := ident,
modifiers := fieldModifiers,
binderInfo := binfo,
inferMod := inferMod,
declName := declName,
name := name,
binders := binders,
type? := type?,
value? := value?
}
private def validStructType (type : Expr) : Bool :=
match type with
| Expr.sort .. => true
| _ => false
private def checkParentIsStructure (parent : Expr) : TermElabM Name :=
match parent.getAppFn with
| Expr.const c _ _ => do
unless isStructure (← getEnv) c do
throwError! "'{c}' is not a structure"
pure c
| _ => throwError "expected structure"
private def findFieldInfo? (infos : Array StructFieldInfo) (fieldName : Name) : Option StructFieldInfo :=
infos.find? fun info => info.name == fieldName
private def containsFieldName (infos : Array StructFieldInfo) (fieldName : Name) : Bool :=
(findFieldInfo? infos fieldName).isSome
private partial def processSubfields (structDeclName : Name) (parentFVar : Expr) (parentStructName : Name) (subfieldNames : Array Name)
(infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (infos : Array StructFieldInfo) := do
if h : i < subfieldNames.size then
let subfieldName := subfieldNames.get ⟨i, h⟩
if containsFieldName infos subfieldName then
throwError! "field '{subfieldName}' from '{parentStructName}' has already been declared"
let val ← mkProjection parentFVar subfieldName
let type ← inferType val
withLetDecl subfieldName type val fun subfieldFVar =>
/- The following `declName` is only used for creating the `_default` auxiliary declaration name when
its default value is overwritten in the structure. -/
let declName := structDeclName ++ subfieldName
let infos := infos.push { name := subfieldName, declName := declName, fvar := subfieldFVar, kind := StructFieldKind.fromParent }
loop (i+1) infos
else
k infos
loop 0 infos
private partial def withParents (view : StructView) (i : Nat) (infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α := do
if h : i < view.parents.size then
let parentStx := view.parents.get ⟨i, h⟩
withRef parentStx do
let parent ← Term.elabType parentStx
let parentName ← checkParentIsStructure parent
let toParentName := Name.mkSimple $ "to" ++ parentName.eraseMacroScopes.getString! -- erase macro scopes?
if containsFieldName infos toParentName then
throwErrorAt! parentStx "field '{toParentName}' has already been declared"
let env ← getEnv
let binfo := if view.isClass && isClass env parentName then BinderInfo.instImplicit else BinderInfo.default
withLocalDecl toParentName binfo parent fun parentFVar =>
let infos := infos.push { name := toParentName, declName := view.declName ++ toParentName, fvar := parentFVar, kind := StructFieldKind.subobject }
let subfieldNames := getStructureFieldsFlattened env parentName
processSubfields view.declName parentFVar parentName subfieldNames infos fun infos => withParents view (i+1) infos k
else
k infos
private def elabFieldTypeValue (view : StructFieldView) (params : Array Expr) : TermElabM (Option Expr × Option Expr) := do
match view.type? with
| none =>
match view.value? with
| none => pure (none, none)
| some valStx =>
let value ← Term.elabTerm valStx none
let value ← mkLambdaFVars params value
pure (none, value)
| some typeStx =>
let type ← Term.elabType typeStx
match view.value? with
| none =>
let type ← mkForallFVars params type
pure (type, none)
| some valStx =>
let value ← Term.elabTermEnsuringType valStx type
let type ← mkForallFVars params type
let value ← mkLambdaFVars params value
pure (type, value)
private partial def withFields
(views : Array StructFieldView) (i : Nat) (infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α := do
if h : i < views.size then
let view := views.get ⟨i, h⟩
withRef view.ref $
match findFieldInfo? infos view.name with
| none => do
let (type?, value?) ← Term.elabBinders view.binders.getArgs fun params => elabFieldTypeValue view params
match type?, value? with
| none, none => throwError "invalid field, type expected"
| some type, _ =>
withLocalDecl view.name view.binderInfo type fun fieldFVar =>
let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, value? := value?,
kind := StructFieldKind.newField, inferMod := view.inferMod }
withFields views (i+1) infos k
| none, some value =>
let type ← inferType value
withLocalDecl view.name view.binderInfo type fun fieldFVar =>
let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, value? := value,
kind := StructFieldKind.newField, inferMod := view.inferMod }
withFields views (i+1) infos k
| some info =>
match info.kind with
| StructFieldKind.newField => throwError! "field '{view.name}' has already been declared"
| StructFieldKind.fromParent =>
match view.value? with
| none => throwError! "field '{view.name}' has been declared in parent structure"
| some valStx => do
if let some type := view.type? then
throwErrorAt! type "omit field '{view.name}' type to set default value"
else
let mut valStx := valStx
if view.binders.getArgs.size > 0 then
valStx ← `(fun $(view.binders.getArgs)* => $valStx:term)
let fvarType ← inferType info.fvar
let value ← Term.elabTermEnsuringType valStx fvarType
let infos := infos.push { info with value? := value }
withFields views (i+1) infos k
| StructFieldKind.subobject => unreachable!
else
k infos
private def getResultUniverse (type : Expr) : TermElabM Level := do
let type ← whnf type
match type with
| Expr.sort u _ => pure u
| _ => throwError "unexpected structure resulting type"
private def collectUsed (params : Array Expr) (fieldInfos : Array StructFieldInfo) : StateRefT CollectFVars.State TermElabM Unit := do
params.forM fun p => do
let type ← inferType p
Term.collectUsedFVars type
fieldInfos.forM fun info => do
let fvarType ← inferType info.fvar
Term.collectUsedFVars fvarType
match info.value? with
| none => pure ()
| some value => Term.collectUsedFVars value
private def removeUnused (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: TermElabM (LocalContext × LocalInstances × Array Expr) := do
let (_, used) ← (collectUsed params fieldInfos).run {}
Term.removeUnused scopeVars used
private def withUsed {α} (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) (k : Array Expr → TermElabM α)
: TermElabM α := do
let (lctx, localInsts, vars) ← removeUnused scopeVars params fieldInfos
withLCtx lctx localInsts $ k vars
private def levelMVarToParamFVar (fvar : Expr) : StateRefT Nat TermElabM Unit := do
let type ← inferType fvar
discard <| Term.levelMVarToParam' type
private def levelMVarToParamFVars (fvars : Array Expr) : StateRefT Nat TermElabM Unit :=
fvars.forM levelMVarToParamFVar
private def levelMVarToParamAux (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: StateRefT Nat TermElabM (Array StructFieldInfo) := do
levelMVarToParamFVars scopeVars
levelMVarToParamFVars params
fieldInfos.mapM fun info => do
levelMVarToParamFVar info.fvar
match info.value? with
| none => pure info
| some value =>
let value ← Term.levelMVarToParam' value
pure { info with value? := value }
private def levelMVarToParam (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM (Array StructFieldInfo) :=
(levelMVarToParamAux scopeVars params fieldInfos).run' 1
private partial def collectUniversesFromFields (r : Level) (rOffset : Nat) (fieldInfos : Array StructFieldInfo) : TermElabM (Array Level) := do
fieldInfos.foldlM (init := #[]) fun (us : Array Level) (info : StructFieldInfo) => do
let type ← inferType info.fvar
let u ← getLevel type
let u ← instantiateLevelMVars u
accLevelAtCtor u r rOffset us
private def updateResultingUniverse (fieldInfos : Array StructFieldInfo) (type : Expr) : TermElabM Expr := do
let r ← getResultUniverse type
let rOffset : Nat := r.getOffset
let r : Level := r.getLevelOffset
match r with
| Level.mvar mvarId _ =>
let us ← collectUniversesFromFields r rOffset fieldInfos
let rNew := mkResultUniverse us rOffset
assignLevelMVar mvarId rNew
instantiateMVars type
| _ => throwError "failed to compute resulting universe level of structure, provide universe explicitly"
private def collectLevelParamsInFVar (s : CollectLevelParams.State) (fvar : Expr) : TermElabM CollectLevelParams.State := do
let type ← inferType fvar
let type ← instantiateMVars type
pure $ collectLevelParams s type
private def collectLevelParamsInFVars (fvars : Array Expr) (s : CollectLevelParams.State) : TermElabM CollectLevelParams.State :=
fvars.foldlM collectLevelParamsInFVar s
private def collectLevelParamsInStructure (structType : Expr) (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: TermElabM (Array Name) := do
let s := collectLevelParams {} structType
let s ← collectLevelParamsInFVars scopeVars s
let s ← collectLevelParamsInFVars params s
let s ← fieldInfos.foldlM (fun (s : CollectLevelParams.State) info => collectLevelParamsInFVar s info.fvar) s
pure s.params
private def addCtorFields (fieldInfos : Array StructFieldInfo) : Nat → Expr → TermElabM Expr
| 0, type => pure type
| i+1, type => do
let info := fieldInfos[i]
let decl ← Term.getFVarLocalDecl! info.fvar
let type ← instantiateMVars type
let type := type.abstract #[info.fvar]
match info.kind with
| StructFieldKind.fromParent =>
let val := decl.value
addCtorFields fieldInfos i (type.instantiate1 val)
| StructFieldKind.subobject =>
let n := mkInternalSubobjectFieldName $ decl.userName
addCtorFields fieldInfos i (mkForall n decl.binderInfo decl.type type)
| StructFieldKind.newField =>
addCtorFields fieldInfos i (mkForall decl.userName decl.binderInfo decl.type type)
private def mkCtor (view : StructView) (levelParams : List Name) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM Constructor :=
withRef view.ref do
let type := mkAppN (mkConst view.declName (levelParams.map mkLevelParam)) params
let type ← addCtorFields fieldInfos fieldInfos.size type
let type ← mkForallFVars params type
let type ← instantiateMVars type
let type := type.inferImplicit params.size !view.ctor.inferMod
pure { name := view.ctor.declName, type := type }
@[extern "lean_mk_projections"]
private constant mkProjections (env : Environment) (structName : Name) (projs : List ProjectionInfo) (isClass : Bool) : Except KernelException Environment
private def addProjections (structName : Name) (projs : List ProjectionInfo) (isClass : Bool) : TermElabM Unit := do
let env ← getEnv
match mkProjections env structName projs isClass with
| Except.ok env => setEnv env
| Except.error ex => throwKernelException ex
private def mkAuxConstructions (declName : Name) : TermElabM Unit := do
let env ← getEnv
let hasUnit := env.contains `PUnit
let hasEq := env.contains `Eq
let hasHEq := env.contains `HEq
mkRecOn declName
if hasUnit then mkCasesOn declName
if hasUnit && hasEq && hasHEq then mkNoConfusion declName
private def addDefaults (lctx : LocalContext) (defaultAuxDecls : Array (Name × Expr × Expr)) : TermElabM Unit := do
let localInsts ← getLocalInstances
withLCtx lctx localInsts do
defaultAuxDecls.forM fun (declName, type, value) => do
/- The identity function is used as "marker". -/
let value ← mkId value
discard <| mkAuxDefinition declName type value (zeta := true)
setReducibleAttribute declName
private def elabStructureView (view : StructView) : TermElabM Unit := do
view.fields.forM fun field => do
if field.declName == view.ctor.declName then
throwErrorAt! field.ref "invalid field name '{field.name}', it is equal to structure constructor name"
addAuxDeclarationRanges field.declName field.ref field.ref
let numExplicitParams := view.params.size
let type ← Term.elabType view.type
unless validStructType type do throwErrorAt view.type "expected Type"
withRef view.ref do
withParents view 0 #[] fun fieldInfos =>
withFields view.fields 0 fieldInfos fun fieldInfos => do
Term.synthesizeSyntheticMVarsNoPostponing
let u ← getResultUniverse type
let inferLevel ← shouldInferResultUniverse u
withUsed view.scopeVars view.params fieldInfos $ fun scopeVars => do
let numParams := scopeVars.size + numExplicitParams
let fieldInfos ← levelMVarToParam scopeVars view.params fieldInfos
let type ← withRef view.ref do
if inferLevel then
updateResultingUniverse fieldInfos type
else
checkResultingUniverse (← getResultUniverse type)
pure type
trace[Elab.structure]! "type: {type}"
let usedLevelNames ← collectLevelParamsInStructure type scopeVars view.params fieldInfos
match sortDeclLevelParams view.scopeLevelNames view.allUserLevelNames usedLevelNames with
| Except.error msg => withRef view.ref <| throwError msg
| Except.ok levelParams =>
let params := scopeVars ++ view.params
let ctor ← mkCtor view levelParams params fieldInfos
let type ← mkForallFVars params type
let type ← instantiateMVars type
let indType := { name := view.declName, type := type, ctors := [ctor] : InductiveType }
let decl := Declaration.inductDecl levelParams params.size [indType] view.modifiers.isUnsafe
Term.ensureNoUnassignedMVars decl
addDecl decl
let projInfos := (fieldInfos.filter fun (info : StructFieldInfo) => !info.isFromParent).toList.map fun (info : StructFieldInfo) =>
{ declName := info.declName, inferMod := info.inferMod : ProjectionInfo }
addProjections view.declName projInfos view.isClass
mkAuxConstructions view.declName
let instParents ← fieldInfos.filterM fun info => do
let decl ← Term.getFVarLocalDecl! info.fvar
pure (info.isSubobject && decl.binderInfo.isInstImplicit)
let projInstances := instParents.toList.map fun info => info.declName
Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking
projInstances.forM fun declName => addInstance declName AttributeKind.global (evalPrio! default)
let lctx ← getLCtx
let fieldsWithDefault := fieldInfos.filter fun info => info.value?.isSome
let defaultAuxDecls ← fieldsWithDefault.mapM fun info => do
let type ← inferType info.fvar
pure (info.declName ++ `_default, type, info.value?.get!)
/- The `lctx` and `defaultAuxDecls` are used to create the auxiliary `_default` declarations
The parameters `params` for these definitions must be marked as implicit, and all others as explicit. -/
let lctx :=
params.foldl (init := lctx) fun (lctx : LocalContext) (p : Expr) =>
lctx.updateBinderInfo p.fvarId! BinderInfo.implicit
let lctx :=
fieldInfos.foldl (init := lctx) fun (lctx : LocalContext) (info : StructFieldInfo) =>
if info.isFromParent then lctx -- `fromParent` fields are elaborated as let-decls, and are zeta-expanded when creating `_default`.
else lctx.updateBinderInfo info.fvar.fvarId! BinderInfo.default
addDefaults lctx defaultAuxDecls
/-
parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields >> optDeriving
where
def «extends» := parser! " extends " >> sepBy1 termParser ", "
def typeSpec := parser! " : " >> termParser
def optType : Parser := optional typeSpec
def structFields := parser! many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
def structCtor := parser! try (declModifiers >> ident >> optional inferMod >> " :: ")
-/
def elabStructure (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do
checkValidInductiveModifier modifiers
let isClass := stx[0].getKind == ``Parser.Command.classTk
let modifiers := if isClass then modifiers.addAttribute { name := `class } else modifiers
let declId := stx[1]
let params := stx[2].getArgs
let exts := stx[3]
let parents := if exts.isNone then #[] else exts[0][1].getSepArgs
let optType := stx[4]
let derivingClassViews ← getOptDerivingClasses stx[6]
let type ← if optType.isNone then `(Sort _) else pure optType[0][1]
let declName ←
runTermElabM none fun scopeVars => do
let scopeLevelNames ← Term.getLevelNames
let ⟨name, declName, allUserLevelNames⟩ ← Elab.expandDeclId (← getCurrNamespace) scopeLevelNames declId modifiers
addDeclarationRanges declName stx
Term.withDeclName declName do
let ctor ← expandCtor stx modifiers declName
let fields ← expandFields stx modifiers declName
Term.withLevelNames allUserLevelNames <| Term.withAutoBoundImplicitLocal <|
Term.elabBinders params (catchAutoBoundImplicit := true) fun params => do
let params ← Term.addAutoBoundImplicits params
let allUserLevelNames ← Term.getLevelNames
Term.withAutoBoundImplicitLocal (flag := false) do
elabStructureView {
ref := stx
modifiers := modifiers
scopeLevelNames := scopeLevelNames
allUserLevelNames := allUserLevelNames
declName := declName
isClass := isClass
scopeVars := scopeVars
params := params
parents := parents
type := type
ctor := ctor
fields := fields
}
return declName
derivingClassViews.forM fun view => view.applyHandlers #[declName]
builtin_initialize registerTraceClass `Elab.structure
end Lean.Elab.Command
|
cd6b8496c635d2e5d526de398efaccdbd7ca4dc5 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/pempty.lean | 82c31284a288c301138d4fccaeaf3e90d537c047 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 1,531 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.discrete_category
/-!
# The empty category
Defines a category structure on `pempty`, and the unique functor `pempty ⥤ C` for any category `C`.
-/
universes w v u -- morphism levels before object levels. See note [category_theory universes].
namespace category_theory
namespace functor
variables (C : Type u) [category.{v} C]
/-- Equivalence between two empty categories. -/
def empty_equivalence : discrete.{w} pempty ≌ discrete.{v} pempty :=
equivalence.mk
{ obj := pempty.elim, map := λ x, x.elim }
{ obj := pempty.elim, map := λ x, x.elim }
(by tidy) (by tidy)
/-- The canonical functor out of the empty category. -/
def empty : discrete.{w} pempty ⥤ C := discrete.functor pempty.elim
variable {C}
/-- Any two functors out of the empty category are isomorphic. -/
def empty_ext (F G : discrete.{w} pempty ⥤ C) : F ≅ G :=
discrete.nat_iso (λ x, pempty.elim x)
/--
Any functor out of the empty category is isomorphic to the canonical functor from the empty
category.
-/
def unique_from_empty (F : discrete.{w} pempty ⥤ C) : F ≅ empty C :=
empty_ext _ _
/--
Any two functors out of the empty category are *equal*. You probably want to use
`empty_ext` instead of this.
-/
lemma empty_ext' (F G : discrete.{w} pempty ⥤ C) : F = G :=
functor.ext (λ x, x.elim) (λ x _ _, x.elim)
end functor
end category_theory
|
02006341be32bc80bf2ea689595fb9e94420bbf4 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/def9.lean | 9677d80b98309702dfefb8a17c86d8b5fcfe7542 | [
"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 | 579 | lean | def f : Nat → Nat → Nat
| 100, 2 => 0
| _, 4 => 1
| _, _ => 2
theorem ex1 : f 100 2 = 0 := rfl
theorem ex2 : f 9 4 = 1 := rfl
theorem ex3 : f 8 4 = 1 := rfl
theorem ex4 : f 6 3 = 2 := rfl
inductive BV : Nat → Type
| nil : BV 0
| cons : {n : Nat} → Bool → BV n → BV (n+1)
open BV
def g : {n : Nat} → BV n → Nat → Nat
| _, cons b v, 1000000 => g v 0
| _, cons b v, x => g v (x + 1)
| _, _, _ => 1
def g' : BV n → Nat → Nat
| cons b v, 1000000 => g v 0
| cons b v, x => g v (x + 1)
| _, _ => 1
|
d0cbeb027bc9864b0e67b99f69c9c2228820bbf7 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/general_notation.lean | d82767b16051d8404996d596dcb402b362df7de1 | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,901 | lean | -- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura, Jeremy Avigad
-- general_notation
-- ================
-- General operations
-- ------------------
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
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 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 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
|
2db1915c6235f62f90dd72ea75676afb62c8e04d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/algebra/order/extend_from.lean | d18d2ddd353552802b5bc286d977c8724b45e61a | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,239 | 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, Yury Kudryashov
-/
import topology.order.basic
import topology.extend_from
/-!
# Lemmas about `extend_from` in an order topology.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
open filter set topological_space
open_locale topology classical
universes u v
variables {α : Type u} {β : Type v}
lemma continuous_on_Icc_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [regular_space β] {f : α → β} {a b : α}
{la lb : β} (hab : a ≠ b) (hf : continuous_on f (Ioo a b))
(ha : tendsto f (𝓝[>] a) (𝓝 la)) (hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
continuous_on (extend_from (Ioo a b) f) (Icc a b) :=
begin
apply continuous_on_extend_from,
{ rw closure_Ioo hab },
{ intros x x_in,
rcases eq_endpoints_or_mem_Ioo_of_mem_Icc x_in with rfl | rfl | h,
{ exact ⟨la, ha.mono_left $ nhds_within_mono _ Ioo_subset_Ioi_self⟩ },
{ exact ⟨lb, hb.mono_left $ nhds_within_mono _ Ioo_subset_Iio_self⟩ },
{ use [f x, hf x h] } }
end
lemma eq_lim_at_left_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α}
{la : β} (hab : a < b) (ha : tendsto f (𝓝[>] a) (𝓝 la)) :
extend_from (Ioo a b) f a = la :=
begin
apply extend_from_eq,
{ rw closure_Ioo hab.ne,
simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] },
{ simpa [hab] }
end
lemma eq_lim_at_right_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α}
{lb : β} (hab : a < b) (hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
extend_from (Ioo a b) f b = lb :=
begin
apply extend_from_eq,
{ rw closure_Ioo hab.ne,
simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] },
{ simpa [hab] }
end
lemma continuous_on_Ico_extend_from_Ioo [topological_space α]
[linear_order α] [densely_ordered α] [order_topology α] [topological_space β]
[regular_space β] {f : α → β} {a b : α} {la : β} (hab : a < b) (hf : continuous_on f (Ioo a b))
(ha : tendsto f (𝓝[>] a) (𝓝 la)) :
continuous_on (extend_from (Ioo a b) f) (Ico a b) :=
begin
apply continuous_on_extend_from,
{ rw [closure_Ioo hab.ne], exact Ico_subset_Icc_self, },
{ intros x x_in,
rcases eq_left_or_mem_Ioo_of_mem_Ico x_in with rfl | h,
{ use la,
simpa [hab] },
{ use [f x, hf x h] } }
end
lemma continuous_on_Ioc_extend_from_Ioo [topological_space α]
[linear_order α] [densely_ordered α] [order_topology α] [topological_space β]
[regular_space β] {f : α → β} {a b : α} {lb : β} (hab : a < b) (hf : continuous_on f (Ioo a b))
(hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
continuous_on (extend_from (Ioo a b) f) (Ioc a b) :=
begin
have := @continuous_on_Ico_extend_from_Ioo αᵒᵈ _ _ _ _ _ _ _ f _ _ _ hab,
erw [dual_Ico, dual_Ioi, dual_Ioo] at this,
exact this hf hb
end
|
47489b8114ce78d005ee160669db95b6456e61b9 | ae9f8bf05de0928a4374adc7d6b36af3411d3400 | /src/formal_ml/real.lean | 8e2675c7fb065bf95aff76a76b8d4eb210bef804 | [
"Apache-2.0"
] | permissive | NeoTim/formal-ml | bc42cf6beba9cd2ed56c1cd054ab4eb5402ed445 | c9cbad2837104160a9832a29245471468748bb8d | refs/heads/master | 1,671,549,160,900 | 1,601,362,989,000 | 1,601,362,989,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,184 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import order.filter.basic
import data.real.basic
import topology.basic
import topology.instances.real
import data.complex.exponential
import topology.algebra.infinite_sum
import data.nat.basic
import analysis.specific_limits
import analysis.calculus.deriv
import analysis.asymptotics
import formal_ml.nat
import formal_ml.core
lemma abs_eq_norm {a:ℝ}:abs a = ∥a∥ := rfl
---Results about the reals--------------------------------------------------------------------------
--Unused (except in classical_limit.lean)
lemma inv_pow_cancel2 (x:ℝ) (n:ℕ):(x≠ 0) → (x⁻¹)^n * (x)^n = 1 :=
begin
intro A1,
rw ← mul_pow,
rw inv_mul_cancel,
simp,
apply A1,
end
lemma abs_pow {x:ℝ} {n:ℕ}:(abs (x^n)) = (abs x)^n :=
begin
induction n,
{
simp,
},
{
rw pow_succ,
rw pow_succ,
rw abs_mul,
rw n_ih,
}
end
lemma real_nat_abs_coe {n:ℕ}:abs (n:ℝ)= (n:ℝ) :=
begin
have A1:abs (n:ℝ) =((abs n):ℝ) := rfl,
rw A1,
simp,
end
lemma abs_pos_of_nonzero {x:ℝ}:x ≠ 0 → 0 < abs x :=
begin
intro A1,
have A2:x < 0 ∨ x = 0 ∨ 0 < x := lt_trichotomy x 0,
cases A2,
{
apply abs_pos_of_neg A2,
},
cases A2,
{
exfalso,
apply A1,
apply A2,
},
{
apply abs_pos_of_pos A2,
}
end
lemma pow_two_pos (n:ℕ):0 < (2:ℝ)^n :=
begin
apply pow_pos,
apply zero_lt_two,
end
lemma pow_nonneg_of_nonneg {x:ℝ} {k:ℕ}:0 ≤ x → 0 ≤ x^k :=
begin
intro A1,
induction k,
{
simp,
apply le_of_lt,
apply zero_lt_one,
},
{
rw pow_succ,
apply mul_nonneg,
apply A1,
apply k_ih,
}
end
lemma real_pow_mono {x y:real} {k:ℕ}:
0 ≤ x →
x ≤ y → x^k ≤ y^k :=
begin
intros A0 A1,
induction k,
{
simp,
},
{
rw pow_succ,
rw pow_succ,
apply mul_le_mul,
{
exact A1,
},
{
apply k_ih,
},
{
simp,
apply pow_nonneg_of_nonneg,
apply A0,
},
{
simp,
apply le_trans,
apply A0,
apply A1,
},
}
end
lemma div_inv (x:ℝ):1/x = x⁻¹ :=
begin
simp,
end
lemma inv_exp (n:ℕ):((2:ℝ)^n)⁻¹ = ((2:ℝ)⁻¹)^n :=
begin
induction n,
{
simp,
},
{
have A1:nat.succ n_n = 1 + n_n,
{
apply nat_succ_one_add,
},
rw A1,
rw pow_add,
rw pow_add,
simp,
rw mul_inv',
}
end
lemma abs_mul_eq_mul_abs (a b:ℝ):(0 ≤ a) → abs (a * b) = a * abs (b) :=
begin
intro A1,
rw abs_mul,
rw abs_of_nonneg,
apply A1,
end
lemma pos_of_pos_of_mul_pos {a b:ℝ}:(0 ≤ a) → (0 < a * b) → 0 < b :=
begin
intros A1 A2,
have A3:0 < b ∨ (b ≤ 0),
{
apply lt_or_ge,
},
cases A3,
{
apply A3,
},
{
exfalso,
have A3A:(a * b)≤ 0,
{
apply mul_nonpos_of_nonneg_of_nonpos,
apply A1,
apply A3,
},
apply not_lt_of_le A3A,
apply A2,
}
end
lemma inv_pos_of_pos {a:ℝ}:(0 < a) → 0 < (a⁻¹) :=
begin
intro A1,
apply pos_of_pos_of_mul_pos,
apply le_of_lt,
apply A1,
rw mul_inv_cancel,
apply zero_lt_one,
intro A2,
rw A2 at A1,
apply lt_irrefl _ A1,
end
lemma inv_lt_one_of_one_lt {a:ℝ}:(1 < a) → (a⁻¹)< 1 :=
begin
intro A1,
have A2:0 < a,
{
apply lt_trans,
apply zero_lt_one,
apply A1,
},
have A3:0 < a⁻¹,
{
apply inv_pos_of_pos A2,
},
have A4:a⁻¹ * 1 < a⁻¹ * a,
{
apply mul_lt_mul_of_pos_left,
apply A1,
apply A3,
},
rw inv_mul_cancel at A4,
rw mul_one at A4,
apply A4,
{
intro A5,
rw A5 at A2,
apply lt_irrefl _ A2,
}
end
--Lift to division_ring, or delete
lemma division_ring.div_def {α:Type*} [division_ring α] {a b:α}:a/b = a* b⁻¹ := rfl
lemma div_def {a b:ℝ}:a/b = a * b⁻¹ := rfl
--Raise to a linear ordered field (or a linear ordered division ring?).
--Unused.
lemma div_lt_div_of_lt_of_lt {a b c:ℝ}:(0<c) → (a < b) → (a/c < b/c) :=
begin
intros A1 A2,
rw division_ring.div_def,
rw division_ring.div_def,
apply mul_lt_mul_of_pos_right,
apply A2,
apply inv_pos_of_pos,
apply A1,
end
--TODO: replace with half_lt_self
lemma epsilon_half_lt_epsilon {α:Type*} [linear_ordered_field α] {ε:α}: 0 < ε → (ε / 2) < ε :=
begin
apply half_lt_self,
end
--TODO: replace with inv_nonneg.mpr.
lemma inv_nonneg_of_nonneg {a:ℝ}:(0 ≤ a) → (0 ≤ a⁻¹) :=
begin
apply inv_nonneg.mpr,
end
lemma move_le {a b c:ℝ}:(0 < c) → (a ≤ b * c) → (a * c⁻¹) ≤ b :=
begin
intros A1 A2,
have A3:0 < c⁻¹ := inv_pos_of_pos A1,
have A4:(a * c⁻¹) ≤ (b * c) * (c⁻¹),
{
apply mul_le_mul_of_nonneg_right,
apply A2,
apply le_of_lt,
apply A3,
},
have A5:b * c * c⁻¹ = b,
{
rw mul_assoc,
rw mul_inv_cancel,
rw mul_one,
symmetry,
apply ne_of_lt,
apply A1,
},
rw A5 at A4,
apply A4,
end
lemma move_le2 {a b c:ℝ}:(0 < c) → (a * c⁻¹) ≤ b → (a ≤ b * c) :=
begin
intros A1 A2,
have A4:(a * c⁻¹) * c ≤ (b * c),
{
apply mul_le_mul_of_nonneg_right,
apply A2,
apply le_of_lt,
apply A1,
},
have A5:a * c⁻¹ * c = a,
{
rw mul_assoc,
rw inv_mul_cancel,
rw mul_one,
symmetry,
apply ne_of_lt,
apply A1,
},
rw A5 at A4,
apply A4,
end
--Probably a repeat.
--nlinarith failed.
lemma inv_decreasing {x y:ℝ}:(0 < x) → (x < y)→ (y⁻¹ < x⁻¹) :=
begin
intros A1 A2,
have A3:0< y,
{
apply lt_trans,
apply A1,
apply A2,
},
have A4:0 < x * y,
{
apply mul_pos;assumption,
},
have A5:x⁻¹ * x < x⁻¹ * y,
{
apply mul_lt_mul_of_pos_left,
apply A2,
apply inv_pos_of_pos,
apply A1,
},
rw inv_mul_cancel at A5,
have A6:1 * y⁻¹ < x⁻¹ * y * y⁻¹,
{
apply mul_lt_mul_of_pos_right,
apply A5,
apply inv_pos_of_pos,
apply A3,
},
rw one_mul at A6,
rw mul_assoc at A6,
rw mul_inv_cancel at A6,
rw mul_one at A6,
apply A6,
{
intro A7,
rw A7 at A3,
apply lt_irrefl 0 A3,
},
{
intro A7,
rw A7 at A1,
apply lt_irrefl 0 A1,
}
end
lemma abs_nonzero_pos {x:ℝ}:(x ≠ 0) → (0 < abs (x)) :=
begin
intro A1,
have A2:(x < 0 ∨ x = 0 ∨ 0 < x) := lt_trichotomy x 0,
cases A2,
{
apply abs_pos_of_neg,
apply A2,
},
cases A2,
{
exfalso,
apply A1,
apply A2,
},
{
apply abs_pos_of_pos,
apply A2,
},
end
lemma diff_ne_zero_of_ne {x x':ℝ}:(x ≠ x') → (x - x' ≠ 0) :=
begin
intro A1,
intro A2,
apply A1,
linarith [A2],
end
--nlinarith failed
lemma abs_diff_pos {x x':ℝ}:(x ≠ x') → (0 < abs (x - x')) :=
begin
intro A1,
apply abs_nonzero_pos,
apply diff_ne_zero_of_ne A1,
end
--nlinarith failed
lemma neg_inv_of_neg {x:ℝ}:x < 0 → (x⁻¹ < 0) :=
begin
intro A1,
have A2:x⁻¹ < 0 ∨ (0 ≤ x⁻¹) := lt_or_le x⁻¹ 0,
cases A2,
{
apply A2,
},
{
exfalso,
have A3:(x * x⁻¹ ≤ 0),
{
apply mul_nonpos_of_nonpos_of_nonneg,
apply le_of_lt,
apply A1,
apply A2,
},
rw mul_inv_cancel at A3,
apply not_lt_of_le A3,
apply zero_lt_one,
intro A4,
rw A4 at A1,
apply lt_irrefl (0:ℝ),
apply A1,
}
end
lemma sub_inv {a b:ℝ}:a - (a - b) = b :=
begin
linarith,
end
--Classical, but filter_util.lean still has dependencies.
lemma x_in_Ioo {x r:ℝ}:(0 < r) → (x∈ set.Ioo (x- r) (x + r)) :=
begin
intro A1,
simp,
split,
{
apply lt_of_sub_pos,
rw sub_inv,
apply A1,
},
{
apply A1,
}
end
--Classical.
lemma abs_lt2 {x x' r:ℝ}:
(abs (x' - x) < r) ↔ ((x - r < x') ∧ (x' < x + r)) :=
begin
rw abs_lt,
split;intros A1;cases A1 with A2 A3;split,
{
apply add_lt_of_lt_sub_left A2,
},
{
apply lt_add_of_sub_left_lt A3,
},
{
apply lt_sub_left_of_add_lt A2,
},
{
apply sub_left_lt_of_lt_add A3,
}
end
--Classical.
lemma abs_lt_iff_in_Ioo {x x' r:ℝ}:
(abs (x' - x) < r) ↔ x' ∈ set.Ioo (x - r) (x + r) :=
begin
apply iff.trans,
apply abs_lt2,
simp,
end
--TODO: replace with neg_neg_of_pos
lemma neg_lt_of_pos {x:ℝ}:(0 < x) → (-x < 0) :=
begin
apply neg_neg_of_pos,
end
--Classical.
lemma abs_lt_iff_in_Ioo2 {x x' r:ℝ}:
(abs (x - x') < r) ↔ x' ∈ set.Ioo (x - r) (x + r) :=
begin
have A1:abs (x - x')=abs (x' - x),
{
have A1A:x' - x = -(x - x'),
{
simp,
},
rw A1A,
rw abs_neg,
},
rw A1,
apply abs_lt_iff_in_Ioo,
end
--Unlikely novel.
lemma real_lt_coe {a b:ℕ}:a < b → (a:ℝ) < (b:ℝ) :=
begin
simp,
end
lemma add_of_sub {a b c:ℝ}:a - b = c ↔ a = b + c :=
begin
split;intros A1;linarith [A1],
end
--linarith and nlinarith fails.
lemma sub_half_eq_half {a:ℝ}:(a - a * 2⁻¹)=a * 2⁻¹ :=
begin
rw add_of_sub,
rw ← division_ring.div_def,
simp,
end
--linarith and nlinarith fails.
lemma half_pos2:0 < (2:ℝ)⁻¹ :=
begin
apply inv_pos_of_pos,
apply zero_lt_two,
end
/- The halfway point is in the middle. -/
lemma half_bound_lower {a b:ℝ}:a < b → a < (a + b)/2 :=
begin
intro A1,
linarith [A1],
end
lemma half_bound_upper {a b:ℝ}:a < b → (a + b)/2 < b :=
begin
intro A1,
linarith [A1],
end
lemma lt_of_sub_lt_sub {a b c:ℝ}:a - c < b - c → a < b :=
begin
intro A1,
linarith [A1],
end
lemma abs_antisymm {a b:ℝ}:abs (a - b) = abs (b - a) :=
begin
have A1:-(a-b)=(b - a),
{
simp,
},
rw ← A1,
rw abs_neg,
end
lemma add_sub_triangle {a b c:ℝ}:(a - b) = (a - c) + (c - b) :=
begin
linarith,
end
lemma abs_triangle {a b c:ℝ}:abs (a - b) ≤ abs (a - c) + abs (c - b) :=
begin
have A1:(a - b) = (a - c) + (c - b) := add_sub_triangle,
rw A1,
apply abs_add_le_abs_add_abs,
end
lemma pow_complex {x:ℝ} {n:ℕ}:((x:ℂ)^n).re=(x^n) :=
begin
induction n,
{
simp,
},
{
rw pow_succ,
rw pow_succ,
simp,
rw n_ih,
}
end
lemma div_complex {x y:ℝ}:((x:ℂ)/(y:ℂ)).re=x/y :=
begin
rw complex.div_re,
simp,
have A1:y=0 ∨ y≠ 0 := eq_or_ne,
cases A1,
{
rw A1,
simp,
},
{
rw mul_div_mul_right,
apply A1,
}
end
lemma complex_norm_sq_nat {y:ℕ}:complex.norm_sq (y:ℂ) = ((y:ℝ) * (y:ℝ)) :=
begin
unfold complex.norm_sq,
simp,
end
lemma num_denom_eq (a b c d:ℝ): (a = b) → (c = d) → (a/c)=(b/d) :=
begin
intros A1 A2,
rw A1,
rw A2,
end
lemma complex_pow_div_complex_re {x:ℝ} {n:ℕ} {y:ℕ}:(((x:ℂ)^n)/(y:ℂ)).re=x^n/(y:ℝ) :=
begin
rw complex.div_re,
simp,
have A1:(y:ℝ)=0 ∨ (y:ℝ) ≠ 0 := eq_or_ne,
cases A1,
{
rw A1,
simp,
},
{
rw complex_norm_sq_nat,
rw mul_div_mul_right,
apply num_denom_eq,
{
rw pow_complex,
},
{
refl,
},
apply A1,
}
end
lemma half_lt_one:(2:ℝ)⁻¹ < 1 :=
begin
have A1:1/(2:ℝ) < 1 := epsilon_half_lt_epsilon zero_lt_one,
rw division_ring.div_def at A1,
rw one_mul at A1,
apply A1,
end
lemma half_pos_lit:0 < (2:ℝ)⁻¹ :=
begin
apply inv_pos_of_pos,
apply zero_lt_two,
end
/-
lemma div_re_eq_re_div_re (x y:ℂ):(x / y).re = (x.re)/(y.re) :=
begin
simp,
end
-/
--It is a common pattern that we calculate the distance to the middle,
--and then show that if you add or subtract it, you get to that middle.
lemma half_equal {x y ε:ℝ}:ε = (x - y)/2 → x - ε = y + ε :=
begin
intro A1,
have A2:(x - ε) + (ε - y) = (y + ε) + (ε - y),
{
rw ← add_sub_triangle,
rw ← add_sub_assoc,
rw add_comm (y+ε),
rw add_comm y ε,
rw ← add_assoc,
rw add_sub_assoc,
rw sub_self,
rw add_zero,
rw A1,
simp,
},
apply add_right_cancel A2,
end
|
be9ca3e22f89a058109fe5567255675b6d53dc9a | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/nat/choose/sum.lean | a1d4703169a2408f499403a065bf93bc24b3fb35 | [
"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,994 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Patrick Stevens
-/
import data.nat.choose.basic
import tactic.linarith
import tactic.omega
import algebra.big_operators.ring
import algebra.big_operators.intervals
import algebra.big_operators.order
/-!
# Sums of binomial coefficients
This file includes variants of the binomial theorem and other results on sums of binomial
coefficients. Theorems whose proofs depend on such sums may also go in this file for import
reasons.
-/
open nat
open finset
open_locale big_operators
variables {α : Type*}
/-- A version of the binomial theorem for noncommutative semirings. -/
theorem commute.add_pow [semiring α] {x y : α} (h : commute x y) (n : ℕ) :
(x + y) ^ n = ∑ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m :=
begin
let t : ℕ → ℕ → α := λ n m, x ^ m * (y ^ (n - m)) * (choose n m),
change (x + y) ^ n = ∑ m in range (n + 1), t n m,
have h_first : ∀ n, t n 0 = y ^ n :=
λ n, by { dsimp [t], rw[choose_zero_right, nat.cast_one, mul_one, one_mul] },
have h_last : ∀ n, t n n.succ = 0 :=
λ n, by { dsimp [t], rw [choose_succ_self, nat.cast_zero, mul_zero] },
have h_middle : ∀ (n i : ℕ), (i ∈ finset.range n.succ) →
((t n.succ) ∘ nat.succ) i = x * (t n i) + y * (t n i.succ) :=
begin
intros n i h_mem,
have h_le : i ≤ n := nat.le_of_lt_succ (finset.mem_range.mp h_mem),
dsimp [t],
rw [choose_succ_succ, nat.cast_add, mul_add],
congr' 1,
{ rw[pow_succ x, succ_sub_succ, mul_assoc, mul_assoc, mul_assoc] },
{ rw[← mul_assoc y, ← mul_assoc y, (h.symm.pow_right i.succ).eq],
by_cases h_eq : i = n,
{ rw [h_eq, choose_succ_self, nat.cast_zero, mul_zero, mul_zero] },
{ rw[succ_sub (lt_of_le_of_ne h_le h_eq)],
rw[pow_succ y, mul_assoc, mul_assoc, mul_assoc, mul_assoc] } }
end,
induction n with n ih,
{ rw [pow_zero, sum_range_succ, range_zero, sum_empty, add_zero],
dsimp [t], rw [choose_self, nat.cast_one, mul_one, mul_one] },
{ rw[sum_range_succ', h_first],
rw[finset.sum_congr rfl (h_middle n), finset.sum_add_distrib, add_assoc],
rw[pow_succ (x + y), ih, add_mul, finset.mul_sum, finset.mul_sum],
congr' 1,
rw[finset.sum_range_succ', finset.sum_range_succ, h_first, h_last,
mul_zero, zero_add, pow_succ] }
end
/-- The binomial theorem -/
theorem add_pow [comm_semiring α] (x y : α) (n : ℕ) :
(x + y) ^ n = ∑ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m :=
(commute.all x y).add_pow n
namespace nat
/-- The sum of entries in a row of Pascal's triangle -/
theorem sum_range_choose (n : ℕ) :
∑ m in range (n + 1), choose n m = 2 ^ n :=
by simpa using (add_pow 1 1 n).symm
lemma sum_range_choose_halfway (m : nat) :
∑ i in range (m + 1), choose (2 * m + 1) i = 4 ^ m :=
have ∑ i in range (m + 1), choose (2 * m + 1) (2 * m + 1 - i) =
∑ i in range (m + 1), choose (2 * m + 1) i,
from sum_congr rfl $ λ i hi, choose_symm $ by linarith [mem_range.1 hi],
(nat.mul_right_inj zero_lt_two).1 $
calc 2 * (∑ i in range (m + 1), choose (2 * m + 1) i) =
(∑ i in range (m + 1), choose (2 * m + 1) i) +
∑ i in range (m + 1), choose (2 * m + 1) (2 * m + 1 - i) :
by rw [two_mul, this]
... = (∑ i in range (m + 1), choose (2 * m + 1) i) +
∑ i in Ico (m + 1) (2 * m + 2), choose (2 * m + 1) i :
by { rw [range_eq_Ico, sum_Ico_reflect], { congr, omega }, omega }
... = ∑ i in range (2 * m + 2), choose (2 * m + 1) i : sum_range_add_sum_Ico _ (by omega)
... = 2^(2 * m + 1) : sum_range_choose (2 * m + 1)
... = 2 * 4^m : by { rw [pow_succ, pow_mul], refl }
lemma choose_middle_le_pow (n : ℕ) : choose (2 * n + 1) n ≤ 4 ^ n :=
begin
have t : choose (2 * n + 1) n ≤ ∑ i in range (n + 1), choose (2 * n + 1) i :=
single_le_sum (λ x _, by linarith) (self_mem_range_succ n),
simpa [sum_range_choose_halfway n] using t
end
end nat
|
32ff26a1bca1ab119cfcd74d997b9f7579d6d4f9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/metric_space/basic.lean | ef87f8314635fa23c0231898e3f7a7b775f42bc5 | [] | 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 | 71,241 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Metric spaces.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel
Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and
topological spaces. For example:
open and closed sets, compactness, completeness, continuity and uniform continuity
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.metric_space.emetric_space
import Mathlib.topology.algebra.ordered
import Mathlib.PostPort
universes u u_1 l v u_2
namespace Mathlib
/-- Construct a uniform structure from a distance function and metric space axioms -/
def uniform_space_of_dist {α : Type u} (dist : α → α → ℝ) (dist_self : ∀ (x : α), dist x x = 0) (dist_comm : ∀ (x y : α), dist x y = dist y x) (dist_triangle : ∀ (x y z : α), dist x z ≤ dist x y + dist y z) : uniform_space α :=
uniform_space.of_core
(uniform_space.core.mk
(infi
fun (ε : ℝ) =>
infi fun (H : ε > 0) => filter.principal (set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε))
sorry sorry sorry)
/-- The distance function (given an ambient metric space on `α`), which returns
a nonnegative real number `dist x y` given `x y : α`. -/
class has_dist (α : Type u_1)
where
dist : α → α → ℝ
-- the uniform structure and the emetric space structure are embedded in the metric space structure
-- to avoid instance diamond issues. See Note [forgetful inheritance].
/-- Metric space
Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be
filled in by default. In the same way, each metric space induces an emetric space structure.
It is included in the structure, but filled in by default.
-/
class metric_space (α : Type u)
extends uniform_space #2, metric_space.to_uniform_space._default #2 #1 #0 α _to_has_dist = id (uniform_space_of_dist dist #0 α _to_has_dist), uniform_space α, has_dist α
where
dist_self : ∀ (x : α), dist x x = 0
eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y
dist_comm : ∀ (x y : α), dist x y = dist y x
dist_triangle : ∀ (x y z : α), dist x z ≤ dist x y + dist y z
edist : α → α → ennreal
edist_dist : autoParam (∀ (x y : α), edist x y = ennreal.of_real (dist x y))
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.control_laws_tac")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "control_laws_tac") [])
to_uniform_space : uniform_space α
uniformity_dist : autoParam
(uniformity α =
infi
fun (ε : ℝ) =>
infi fun (H : ε > 0) => filter.principal (set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε))
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.control_laws_tac")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "control_laws_tac") [])
protected instance metric_space.to_uniform_space' {α : Type u} [metric_space α] : uniform_space α :=
metric_space.to_uniform_space
protected instance metric_space.to_has_edist {α : Type u} [metric_space α] : has_edist α :=
has_edist.mk metric_space.edist
@[simp] theorem dist_self {α : Type u} [metric_space α] (x : α) : dist x x = 0 :=
metric_space.dist_self x
theorem eq_of_dist_eq_zero {α : Type u} [metric_space α] {x : α} {y : α} : dist x y = 0 → x = y :=
metric_space.eq_of_dist_eq_zero
theorem dist_comm {α : Type u} [metric_space α] (x : α) (y : α) : dist x y = dist y x :=
metric_space.dist_comm x y
theorem edist_dist {α : Type u} [metric_space α] (x : α) (y : α) : edist x y = ennreal.of_real (dist x y) :=
metric_space.edist_dist x y
@[simp] theorem dist_eq_zero {α : Type u} [metric_space α] {x : α} {y : α} : dist x y = 0 ↔ x = y :=
{ mp := eq_of_dist_eq_zero, mpr := fun (this : x = y) => this ▸ dist_self x }
@[simp] theorem zero_eq_dist {α : Type u} [metric_space α] {x : α} {y : α} : 0 = dist x y ↔ x = y :=
eq.mpr (id (Eq._oldrec (Eq.refl (0 = dist x y ↔ x = y)) (propext eq_comm)))
(eq.mpr (id (Eq._oldrec (Eq.refl (dist x y = 0 ↔ x = y)) (propext dist_eq_zero))) (iff.refl (x = y)))
theorem dist_triangle {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : dist x z ≤ dist x y + dist y z :=
metric_space.dist_triangle x y z
theorem dist_triangle_left {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : dist x y ≤ dist z x + dist z y :=
eq.mpr (id (Eq._oldrec (Eq.refl (dist x y ≤ dist z x + dist z y)) (dist_comm z x))) (dist_triangle x z y)
theorem dist_triangle_right {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : dist x y ≤ dist x z + dist y z :=
eq.mpr (id (Eq._oldrec (Eq.refl (dist x y ≤ dist x z + dist y z)) (dist_comm y z))) (dist_triangle x z y)
theorem dist_triangle4 {α : Type u} [metric_space α] (x : α) (y : α) (z : α) (w : α) : dist x w ≤ dist x y + dist y z + dist z w :=
le_trans (dist_triangle x z w) (add_le_add_right (dist_triangle x y z) (dist z w))
theorem dist_triangle4_left {α : Type u} [metric_space α] (x₁ : α) (y₁ : α) (x₂ : α) (y₂ : α) : dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) := sorry
theorem dist_triangle4_right {α : Type u} [metric_space α] (x₁ : α) (y₁ : α) (x₂ : α) (y₂ : α) : dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ := sorry
/-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/
theorem dist_le_Ico_sum_dist {α : Type u} [metric_space α] (f : ℕ → α) {m : ℕ} {n : ℕ} (h : m ≤ n) : dist (f m) (f n) ≤ finset.sum (finset.Ico m n) fun (i : ℕ) => dist (f i) (f (i + 1)) := sorry
/-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/
theorem dist_le_range_sum_dist {α : Type u} [metric_space α] (f : ℕ → α) (n : ℕ) : dist (f 0) (f n) ≤ finset.sum (finset.range n) fun (i : ℕ) => dist (f i) (f (i + 1)) :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_dist f (nat.zero_le n)
/-- A version of `dist_le_Ico_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
theorem dist_le_Ico_sum_of_dist_le {α : Type u} [metric_space α] {f : ℕ → α} {m : ℕ} {n : ℕ} (hmn : m ≤ n) {d : ℕ → ℝ} (hd : ∀ {k : ℕ}, m ≤ k → k < n → dist (f k) (f (k + 1)) ≤ d k) : dist (f m) (f n) ≤ finset.sum (finset.Ico m n) fun (i : ℕ) => d i := sorry
/-- A version of `dist_le_range_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
theorem dist_le_range_sum_of_dist_le {α : Type u} [metric_space α] {f : ℕ → α} (n : ℕ) {d : ℕ → ℝ} (hd : ∀ {k : ℕ}, k < n → dist (f k) (f (k + 1)) ≤ d k) : dist (f 0) (f n) ≤ finset.sum (finset.range n) fun (i : ℕ) => d i :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_of_dist_le (zero_le n) fun (_x : ℕ) (_x_1 : 0 ≤ _x) => hd
theorem swap_dist {α : Type u} [metric_space α] : function.swap dist = dist :=
funext fun (x : α) => funext fun (y : α) => dist_comm y x
theorem abs_dist_sub_le {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : abs (dist x z - dist y z) ≤ dist x y :=
iff.mpr abs_sub_le_iff
{ left := iff.mpr sub_le_iff_le_add (dist_triangle x y z),
right := iff.mpr sub_le_iff_le_add (dist_triangle_left y z x) }
theorem dist_nonneg {α : Type u} [metric_space α] {x : α} {y : α} : 0 ≤ dist x y := sorry
@[simp] theorem dist_le_zero {α : Type u} [metric_space α] {x : α} {y : α} : dist x y ≤ 0 ↔ x = y := sorry
@[simp] theorem dist_pos {α : Type u} [metric_space α] {x : α} {y : α} : 0 < dist x y ↔ x ≠ y := sorry
@[simp] theorem abs_dist {α : Type u} [metric_space α] {a : α} {b : α} : abs (dist a b) = dist a b :=
abs_of_nonneg dist_nonneg
theorem eq_of_forall_dist_le {α : Type u} [metric_space α] {x : α} {y : α} (h : ∀ (ε : ℝ), ε > 0 → dist x y ≤ ε) : x = y :=
eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h)
/-- Distance as a nonnegative real number. -/
def nndist {α : Type u} [metric_space α] (a : α) (b : α) : nnreal :=
{ val := dist a b, property := dist_nonneg }
/--Express `nndist` in terms of `edist`-/
theorem nndist_edist {α : Type u} [metric_space α] (x : α) (y : α) : nndist x y = ennreal.to_nnreal (edist x y) := sorry
/--Express `edist` in terms of `nndist`-/
theorem edist_nndist {α : Type u} [metric_space α] (x : α) (y : α) : edist x y = ↑(nndist x y) := sorry
@[simp] theorem ennreal_coe_nndist {α : Type u} [metric_space α] (x : α) (y : α) : ↑(nndist x y) = edist x y :=
Eq.symm (edist_nndist x y)
@[simp] theorem edist_lt_coe {α : Type u} [metric_space α] {x : α} {y : α} {c : nnreal} : edist x y < ↑c ↔ nndist x y < c :=
eq.mpr (id (Eq._oldrec (Eq.refl (edist x y < ↑c ↔ nndist x y < c)) (edist_nndist x y)))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(nndist x y) < ↑c ↔ nndist x y < c)) (propext ennreal.coe_lt_coe)))
(iff.refl (nndist x y < c)))
@[simp] theorem edist_le_coe {α : Type u} [metric_space α] {x : α} {y : α} {c : nnreal} : edist x y ≤ ↑c ↔ nndist x y ≤ c :=
eq.mpr (id (Eq._oldrec (Eq.refl (edist x y ≤ ↑c ↔ nndist x y ≤ c)) (edist_nndist x y)))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(nndist x y) ≤ ↑c ↔ nndist x y ≤ c)) (propext ennreal.coe_le_coe)))
(iff.refl (nndist x y ≤ c)))
/--In a metric space, the extended distance is always finite-/
theorem edist_ne_top {α : Type u} [metric_space α] (x : α) (y : α) : edist x y ≠ ⊤ :=
eq.mpr (id (Eq._oldrec (Eq.refl (edist x y ≠ ⊤)) (edist_dist x y))) ennreal.coe_ne_top
/--In a metric space, the extended distance is always finite-/
theorem edist_lt_top {α : Type u_1} [metric_space α] (x : α) (y : α) : edist x y < ⊤ :=
iff.mpr ennreal.lt_top_iff_ne_top (edist_ne_top x y)
/--`nndist x x` vanishes-/
@[simp] theorem nndist_self {α : Type u} [metric_space α] (a : α) : nndist a a = 0 :=
iff.mp (nnreal.coe_eq_zero (nndist a a)) (dist_self a)
/--Express `dist` in terms of `nndist`-/
theorem dist_nndist {α : Type u} [metric_space α] (x : α) (y : α) : dist x y = ↑(nndist x y) :=
rfl
@[simp] theorem coe_nndist {α : Type u} [metric_space α] (x : α) (y : α) : ↑(nndist x y) = dist x y :=
Eq.symm (dist_nndist x y)
@[simp] theorem dist_lt_coe {α : Type u} [metric_space α] {x : α} {y : α} {c : nnreal} : dist x y < ↑c ↔ nndist x y < c :=
iff.rfl
@[simp] theorem dist_le_coe {α : Type u} [metric_space α] {x : α} {y : α} {c : nnreal} : dist x y ≤ ↑c ↔ nndist x y ≤ c :=
iff.rfl
/--Express `nndist` in terms of `dist`-/
theorem nndist_dist {α : Type u} [metric_space α] (x : α) (y : α) : nndist x y = nnreal.of_real (dist x y) :=
eq.mpr (id (Eq._oldrec (Eq.refl (nndist x y = nnreal.of_real (dist x y))) (dist_nndist x y)))
(eq.mpr (id (Eq._oldrec (Eq.refl (nndist x y = nnreal.of_real ↑(nndist x y))) nnreal.of_real_coe))
(Eq.refl (nndist x y)))
/--Deduce the equality of points with the vanishing of the nonnegative distance-/
theorem eq_of_nndist_eq_zero {α : Type u} [metric_space α] {x : α} {y : α} : nndist x y = 0 → x = y := sorry
theorem nndist_comm {α : Type u} [metric_space α] (x : α) (y : α) : nndist x y = nndist y x := sorry
/--Characterize the equality of points with the vanishing of the nonnegative distance-/
@[simp] theorem nndist_eq_zero {α : Type u} [metric_space α] {x : α} {y : α} : nndist x y = 0 ↔ x = y := sorry
@[simp] theorem zero_eq_nndist {α : Type u} [metric_space α] {x : α} {y : α} : 0 = nndist x y ↔ x = y := sorry
/--Triangle inequality for the nonnegative distance-/
theorem nndist_triangle {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : nndist x z ≤ nndist x y + nndist y z :=
dist_triangle x y z
theorem nndist_triangle_left {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : nndist x y ≤ nndist z x + nndist z y :=
dist_triangle_left x y z
theorem nndist_triangle_right {α : Type u} [metric_space α] (x : α) (y : α) (z : α) : nndist x y ≤ nndist x z + nndist y z :=
dist_triangle_right x y z
/--Express `dist` in terms of `edist`-/
theorem dist_edist {α : Type u} [metric_space α] (x : α) (y : α) : dist x y = ennreal.to_real (edist x y) := sorry
namespace metric
/- instantiate metric space as a topology -/
/-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/
def ball {α : Type u} [metric_space α] (x : α) (ε : ℝ) : set α :=
set_of fun (y : α) => dist y x < ε
@[simp] theorem mem_ball {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : y ∈ ball x ε ↔ dist y x < ε :=
iff.rfl
theorem mem_ball' {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : y ∈ ball x ε ↔ dist x y < ε :=
eq.mpr (id (Eq._oldrec (Eq.refl (y ∈ ball x ε ↔ dist x y < ε)) (dist_comm x y))) (iff.refl (y ∈ ball x ε))
@[simp] theorem nonempty_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} (h : 0 < ε) : set.nonempty (ball x ε) := sorry
theorem ball_eq_ball {α : Type u} [metric_space α] (ε : ℝ) (x : α) : uniform_space.ball x (set_of fun (p : α × α) => dist (prod.snd p) (prod.fst p) < ε) = ball x ε :=
rfl
theorem ball_eq_ball' {α : Type u} [metric_space α] (ε : ℝ) (x : α) : uniform_space.ball x (set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε) = ball x ε := sorry
/-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/
def closed_ball {α : Type u} [metric_space α] (x : α) (ε : ℝ) : set α :=
set_of fun (y : α) => dist y x ≤ ε
@[simp] theorem mem_closed_ball {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : y ∈ closed_ball x ε ↔ dist y x ≤ ε :=
iff.rfl
/-- `sphere x ε` is the set of all points `y` with `dist y x = ε` -/
def sphere {α : Type u} [metric_space α] (x : α) (ε : ℝ) : set α :=
set_of fun (y : α) => dist y x = ε
@[simp] theorem mem_sphere {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : y ∈ sphere x ε ↔ dist y x = ε :=
iff.rfl
theorem mem_closed_ball' {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : y ∈ closed_ball x ε ↔ dist x y ≤ ε :=
eq.mpr (id (Eq._oldrec (Eq.refl (y ∈ closed_ball x ε ↔ dist x y ≤ ε)) (dist_comm x y))) (iff.refl (y ∈ closed_ball x ε))
theorem nonempty_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} (h : 0 ≤ ε) : set.nonempty (closed_ball x ε) := sorry
theorem ball_subset_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : ball x ε ⊆ closed_ball x ε :=
fun (y : α) (hy : dist y x < ε) => le_of_lt hy
theorem sphere_subset_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : sphere x ε ⊆ closed_ball x ε :=
fun (y : α) => le_of_eq
theorem sphere_disjoint_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : disjoint (sphere x ε) (ball x ε) := sorry
@[simp] theorem ball_union_sphere {α : Type u} [metric_space α] {x : α} {ε : ℝ} : ball x ε ∪ sphere x ε = closed_ball x ε :=
set.ext fun (y : α) => iff.symm le_iff_lt_or_eq
@[simp] theorem sphere_union_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : sphere x ε ∪ ball x ε = closed_ball x ε :=
eq.mpr (id (Eq._oldrec (Eq.refl (sphere x ε ∪ ball x ε = closed_ball x ε)) (set.union_comm (sphere x ε) (ball x ε))))
(eq.mpr (id (Eq._oldrec (Eq.refl (ball x ε ∪ sphere x ε = closed_ball x ε)) ball_union_sphere))
(Eq.refl (closed_ball x ε)))
@[simp] theorem closed_ball_diff_sphere {α : Type u} [metric_space α] {x : α} {ε : ℝ} : closed_ball x ε \ sphere x ε = ball x ε := sorry
@[simp] theorem closed_ball_diff_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : closed_ball x ε \ ball x ε = sphere x ε := sorry
theorem pos_of_mem_ball {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} (hy : y ∈ ball x ε) : 0 < ε :=
lt_of_le_of_lt dist_nonneg hy
theorem mem_ball_self {α : Type u} [metric_space α] {x : α} {ε : ℝ} (h : 0 < ε) : x ∈ ball x ε :=
(fun (this : dist x x < ε) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (dist x x < ε)) (dist_self x))) h)
theorem mem_closed_ball_self {α : Type u} [metric_space α] {x : α} {ε : ℝ} (h : 0 ≤ ε) : x ∈ closed_ball x ε :=
(fun (this : dist x x ≤ ε) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (dist x x ≤ ε)) (dist_self x))) h)
theorem mem_ball_comm {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} : x ∈ ball y ε ↔ y ∈ ball x ε := sorry
theorem ball_subset_ball {α : Type u} [metric_space α] {x : α} {ε₁ : ℝ} {ε₂ : ℝ} (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
fun (y : α) (yx : dist y x < ε₁) => lt_of_lt_of_le yx h
theorem closed_ball_subset_closed_ball {α : Type u} [metric_space α] {x : α} {ε₁ : ℝ} {ε₂ : ℝ} (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ :=
fun (y : α) (yx : dist y x ≤ ε₁) => le_trans yx h
theorem ball_disjoint {α : Type u} [metric_space α] {x : α} {y : α} {ε₁ : ℝ} {ε₂ : ℝ} (h : ε₁ + ε₂ ≤ dist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ := sorry
theorem ball_disjoint_same {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} (h : ε ≤ dist x y / bit0 1) : ball x ε ∩ ball y ε = ∅ :=
ball_disjoint
(eq.mpr (id (Eq._oldrec (Eq.refl (ε + ε ≤ dist x y)) (Eq.symm (two_mul ε))))
(eq.mpr (id (Eq._oldrec (Eq.refl (bit0 1 * ε ≤ dist x y)) (Eq.symm (propext (le_div_iff' zero_lt_two))))) h))
theorem ball_subset {α : Type u} [metric_space α] {x : α} {y : α} {ε₁ : ℝ} {ε₂ : ℝ} (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ :=
fun (z : α) (zx : z ∈ ball x ε₁) =>
eq.mpr (id (Eq._oldrec (Eq.refl (z ∈ ball y ε₂)) (Eq.symm (add_sub_cancel'_right ε₁ ε₂))))
(lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h))
theorem ball_half_subset {α : Type u} [metric_space α] {x : α} {ε : ℝ} (y : α) (h : y ∈ ball x (ε / bit0 1)) : ball y (ε / bit0 1) ⊆ ball x ε :=
ball_subset (eq.mpr (id (Eq._oldrec (Eq.refl (dist y x ≤ ε - ε / bit0 1)) (sub_self_div_two ε))) (le_of_lt h))
theorem exists_ball_subset_ball {α : Type u} [metric_space α] {x : α} {y : α} {ε : ℝ} (h : y ∈ ball x ε) : ∃ (ε' : ℝ), ∃ (H : ε' > 0), ball y ε' ⊆ ball x ε := sorry
@[simp] theorem ball_eq_empty_iff_nonpos {α : Type u} [metric_space α] {x : α} {ε : ℝ} : ball x ε = ∅ ↔ ε ≤ 0 :=
iff.trans set.eq_empty_iff_forall_not_mem
{ mp := fun (h : ∀ (x_1 : α), ¬x_1 ∈ ball x ε) => le_of_not_gt fun (ε0 : ε > 0) => h x (mem_ball_self ε0),
mpr := fun (ε0 : ε ≤ 0) (y : α) (h : y ∈ ball x ε) => not_lt_of_le ε0 (pos_of_mem_ball h) }
@[simp] theorem closed_ball_eq_empty_iff_neg {α : Type u} [metric_space α] {x : α} {ε : ℝ} : closed_ball x ε = ∅ ↔ ε < 0 := sorry
@[simp] theorem ball_zero {α : Type u} [metric_space α] {x : α} : ball x 0 = ∅ :=
eq.mpr (id (Eq._oldrec (Eq.refl (ball x 0 = ∅)) (propext ball_eq_empty_iff_nonpos))) (le_refl 0)
@[simp] theorem closed_ball_zero {α : Type u} [metric_space α] {x : α} : closed_ball x 0 = singleton x :=
set.ext fun (y : α) => dist_le_zero
theorem uniformity_basis_dist {α : Type u} [metric_space α] : filter.has_basis (uniformity α) (fun (ε : ℝ) => 0 < ε)
fun (ε : ℝ) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε := sorry
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_dist`, `uniformity_basis_dist_inv_nat_succ`,
and `uniformity_basis_dist_inv_nat_pos`. -/
protected theorem mk_uniformity_basis {α : Type u} [metric_space α] {β : Type u_1} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ (i : β), p i → 0 < f i) (hf : ∀ {ε : ℝ}, 0 < ε → ∃ (i : β), ∃ (hi : p i), f i ≤ ε) : filter.has_basis (uniformity α) p fun (i : β) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < f i := sorry
theorem uniformity_basis_dist_inv_nat_succ {α : Type u} [metric_space α] : filter.has_basis (uniformity α) (fun (_x : ℕ) => True)
fun (n : ℕ) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < 1 / (↑n + 1) :=
metric.mk_uniformity_basis (fun (n : ℕ) (_x : True) => div_pos zero_lt_one (nat.cast_add_one_pos n))
fun (ε : ℝ) (ε0 : 0 < ε) =>
Exists.imp (fun (n : ℕ) (hn : 1 / (↑n + 1) < ε) => Exists.intro trivial (le_of_lt hn)) (exists_nat_one_div_lt ε0)
theorem uniformity_basis_dist_inv_nat_pos {α : Type u} [metric_space α] : filter.has_basis (uniformity α) (fun (n : ℕ) => 0 < n)
fun (n : ℕ) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < 1 / ↑n := sorry
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then closed neighborhoods of the diagonal of sizes `{f i | p i}`
form a basis of `𝓤 α`.
Currently we have only one specific basis `uniformity_basis_dist_le` based on this constructor.
More can be easily added if needed in the future. -/
protected theorem mk_uniformity_basis_le {α : Type u} [metric_space α] {β : Type u_1} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ (x : β), p x → 0 < f x) (hf : ∀ (ε : ℝ), 0 < ε → ∃ (x : β), ∃ (hx : p x), f x ≤ ε) : filter.has_basis (uniformity α) p fun (x : β) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) ≤ f x := sorry
/-- Contant size closed neighborhoods of the diagonal form a basis
of the uniformity filter. -/
theorem uniformity_basis_dist_le {α : Type u} [metric_space α] : filter.has_basis (uniformity α) (fun (ε : ℝ) => 0 < ε)
fun (ε : ℝ) => set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) ≤ ε :=
metric.mk_uniformity_basis_le (fun (_x : ℝ) => id)
fun (ε : ℝ) (ε₀ : 0 < ε) => Exists.intro ε (Exists.intro ε₀ (le_refl ε))
theorem mem_uniformity_dist {α : Type u} [metric_space α] {s : set (α × α)} : s ∈ uniformity α ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ {a b : α}, dist a b < ε → (a, b) ∈ s :=
filter.has_basis.mem_uniformity_iff uniformity_basis_dist
/-- A constant size neighborhood of the diagonal is an entourage. -/
theorem dist_mem_uniformity {α : Type u} [metric_space α] {ε : ℝ} (ε0 : 0 < ε) : (set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε) ∈ uniformity α :=
iff.mpr mem_uniformity_dist (Exists.intro ε (Exists.intro ε0 fun (a b : α) => id))
theorem uniform_continuous_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} : uniform_continuous f ↔ ∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {a b : α}, dist a b < δ → dist (f a) (f b) < ε :=
filter.has_basis.uniform_continuous_iff uniformity_basis_dist uniformity_basis_dist
theorem uniform_continuous_on_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {s : set α} : uniform_continuous_on f s ↔
∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ (x y : α), x ∈ s → y ∈ s → dist x y < δ → dist (f x) (f y) < ε := sorry
theorem uniform_embedding_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} : uniform_embedding f ↔
function.injective f ∧
uniform_continuous f ∧
∀ (δ : ℝ) (H : δ > 0), ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ := sorry
/-- A map between metric spaces is a uniform embedding if and only if the distance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/
theorem uniform_embedding_iff' {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} : uniform_embedding f ↔
(∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {a b : α}, dist a b < δ → dist (f a) (f b) < ε) ∧
∀ (δ : ℝ) (H : δ > 0), ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ := sorry
theorem totally_bounded_iff {α : Type u} [metric_space α] {s : set α} : totally_bounded s ↔
∀ (ε : ℝ) (H : ε > 0),
∃ (t : set α), set.finite t ∧ s ⊆ set.Union fun (y : α) => set.Union fun (H : y ∈ t) => ball y ε := sorry
/-- A metric space space is totally bounded if one can reconstruct up to any ε>0 any element of the
space from finitely many data. -/
theorem totally_bounded_of_finite_discretization {α : Type u} [metric_space α] {s : set α} (H : ∀ (ε : ℝ), ε > 0 → ∃ (β : Type u), Exists (∃ (F : ↥s → β), ∀ (x y : ↥s), F x = F y → dist ↑x ↑y < ε)) : totally_bounded s := sorry
theorem finite_approx_of_totally_bounded {α : Type u} [metric_space α] {s : set α} (hs : totally_bounded s) (ε : ℝ) (H : ε > 0) : ∃ (t : set α), ∃ (H : t ⊆ s), set.finite t ∧ s ⊆ set.Union fun (y : α) => set.Union fun (H : y ∈ t) => ball y ε :=
eq.mp (Eq._oldrec (Eq.refl (totally_bounded s)) (propext totally_bounded_iff_subset)) hs
(set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε) (dist_mem_uniformity ε_pos)
/-- Expressing locally uniform convergence on a set using `dist`. -/
theorem tendsto_locally_uniformly_on_iff {α : Type u} {β : Type v} [metric_space α] {ι : Type u_1} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_locally_uniformly_on F f p s ↔
∀ (ε : ℝ) (H : ε > 0) (x : β) (H : x ∈ s),
∃ (t : set β),
∃ (H : t ∈ nhds_within x s), filter.eventually (fun (n : ι) => ∀ (y : β), y ∈ t → dist (f y) (F n y) < ε) p := sorry
/-- Expressing uniform convergence on a set using `dist`. -/
theorem tendsto_uniformly_on_iff {α : Type u} {β : Type v} [metric_space α] {ι : Type u_1} {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_uniformly_on F f p s ↔
∀ (ε : ℝ), ε > 0 → filter.eventually (fun (n : ι) => ∀ (x : β), x ∈ s → dist (f x) (F n x) < ε) p := sorry
/-- Expressing locally uniform convergence using `dist`. -/
theorem tendsto_locally_uniformly_iff {α : Type u} {β : Type v} [metric_space α] {ι : Type u_1} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_locally_uniformly F f p ↔
∀ (ε : ℝ) (H : ε > 0) (x : β),
∃ (t : set β), ∃ (H : t ∈ nhds x), filter.eventually (fun (n : ι) => ∀ (y : β), y ∈ t → dist (f y) (F n y) < ε) p := sorry
/-- Expressing uniform convergence using `dist`. -/
theorem tendsto_uniformly_iff {α : Type u} {β : Type v} [metric_space α] {ι : Type u_1} {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_uniformly F f p ↔ ∀ (ε : ℝ), ε > 0 → filter.eventually (fun (n : ι) => ∀ (x : β), dist (f x) (F n x) < ε) p := sorry
protected theorem cauchy_iff {α : Type u} [metric_space α] {f : filter α} : cauchy f ↔
filter.ne_bot f ∧ ∀ (ε : ℝ) (H : ε > 0), ∃ (t : set α), ∃ (H : t ∈ f), ∀ (x y : α), x ∈ t → y ∈ t → dist x y < ε :=
filter.has_basis.cauchy_iff uniformity_basis_dist
theorem nhds_basis_ball {α : Type u} [metric_space α] {x : α} : filter.has_basis (nhds x) (fun (ε : ℝ) => 0 < ε) (ball x) :=
nhds_basis_uniformity uniformity_basis_dist
theorem mem_nhds_iff {α : Type u} [metric_space α] {x : α} {s : set α} : s ∈ nhds x ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ball x ε ⊆ s :=
filter.has_basis.mem_iff nhds_basis_ball
theorem eventually_nhds_iff {α : Type u} [metric_space α] {x : α} {p : α → Prop} : filter.eventually (fun (y : α) => p y) (nhds x) ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ {y : α}, dist y x < ε → p y :=
mem_nhds_iff
theorem eventually_nhds_iff_ball {α : Type u} [metric_space α] {x : α} {p : α → Prop} : filter.eventually (fun (y : α) => p y) (nhds x) ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ (y : α), y ∈ ball x ε → p y :=
mem_nhds_iff
theorem nhds_basis_closed_ball {α : Type u} [metric_space α] {x : α} : filter.has_basis (nhds x) (fun (ε : ℝ) => 0 < ε) (closed_ball x) :=
nhds_basis_uniformity uniformity_basis_dist_le
theorem nhds_basis_ball_inv_nat_succ {α : Type u} [metric_space α] {x : α} : filter.has_basis (nhds x) (fun (_x : ℕ) => True) fun (n : ℕ) => ball x (1 / (↑n + 1)) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_succ
theorem nhds_basis_ball_inv_nat_pos {α : Type u} [metric_space α] {x : α} : filter.has_basis (nhds x) (fun (n : ℕ) => 0 < n) fun (n : ℕ) => ball x (1 / ↑n) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_pos
theorem is_open_iff {α : Type u} [metric_space α] {s : set α} : is_open s ↔ ∀ (x : α) (H : x ∈ s), ∃ (ε : ℝ), ∃ (H : ε > 0), ball x ε ⊆ s := sorry
theorem is_open_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : is_open (ball x ε) :=
iff.mpr is_open_iff fun (y : α) => exists_ball_subset_ball
theorem ball_mem_nhds {α : Type u} [metric_space α] (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ nhds x :=
mem_nhds_sets is_open_ball (mem_ball_self ε0)
theorem closed_ball_mem_nhds {α : Type u} [metric_space α] (x : α) {ε : ℝ} (ε0 : 0 < ε) : closed_ball x ε ∈ nhds x :=
filter.mem_sets_of_superset (ball_mem_nhds x ε0) ball_subset_closed_ball
theorem nhds_within_basis_ball {α : Type u} [metric_space α] {x : α} {s : set α} : filter.has_basis (nhds_within x s) (fun (ε : ℝ) => 0 < ε) fun (ε : ℝ) => ball x ε ∩ s :=
nhds_within_has_basis nhds_basis_ball s
theorem mem_nhds_within_iff {α : Type u} [metric_space α] {x : α} {s : set α} {t : set α} : s ∈ nhds_within x t ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ball x ε ∩ t ⊆ s :=
filter.has_basis.mem_iff nhds_within_basis_ball
theorem tendsto_nhds_within_nhds_within {α : Type u} {β : Type v} [metric_space α] {s : set α} [metric_space β] {t : set β} {f : α → β} {a : α} {b : β} : filter.tendsto f (nhds_within a s) (nhds_within b t) ↔
∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {x : α}, x ∈ s → dist x a < δ → f x ∈ t ∧ dist (f x) b < ε := sorry
theorem tendsto_nhds_within_nhds {α : Type u} {β : Type v} [metric_space α] {s : set α} [metric_space β] {f : α → β} {a : α} {b : β} : filter.tendsto f (nhds_within a s) (nhds b) ↔
∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {x : α}, x ∈ s → dist x a < δ → dist (f x) b < ε := sorry
theorem tendsto_nhds_nhds {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {a : α} {b : β} : filter.tendsto f (nhds a) (nhds b) ↔
∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {x : α}, dist x a < δ → dist (f x) b < ε :=
filter.has_basis.tendsto_iff nhds_basis_ball nhds_basis_ball
theorem continuous_at_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {a : α} : continuous_at f a ↔ ∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {x : α}, dist x a < δ → dist (f x) (f a) < ε := sorry
theorem continuous_within_at_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {a : α} {s : set α} : continuous_within_at f s a ↔
∀ (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ {x : α}, x ∈ s → dist x a < δ → dist (f x) (f a) < ε := sorry
theorem continuous_on_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {s : set α} : continuous_on f s ↔
∀ (b : α) (H : b ∈ s) (ε : ℝ) (H : ε > 0),
∃ (δ : ℝ), ∃ (H : δ > 0), ∀ (a : α), a ∈ s → dist a b < δ → dist (f a) (f b) < ε := sorry
theorem continuous_iff {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} : continuous f ↔ ∀ (b : α) (ε : ℝ) (H : ε > 0), ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ (a : α), dist a b < δ → dist (f a) (f b) < ε :=
iff.trans continuous_iff_continuous_at (forall_congr fun (b : α) => tendsto_nhds_nhds)
theorem tendsto_nhds {α : Type u} {β : Type v} [metric_space α] {f : filter β} {u : β → α} {a : α} : filter.tendsto u f (nhds a) ↔ ∀ (ε : ℝ), ε > 0 → filter.eventually (fun (x : β) => dist (u x) a < ε) f :=
filter.has_basis.tendsto_right_iff nhds_basis_ball
theorem continuous_at_iff' {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ ∀ (ε : ℝ), ε > 0 → filter.eventually (fun (x : β) => dist (f x) (f b) < ε) (nhds b) := sorry
theorem continuous_within_at_iff' {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔
∀ (ε : ℝ), ε > 0 → filter.eventually (fun (x : β) => dist (f x) (f b) < ε) (nhds_within b s) := sorry
theorem continuous_on_iff' {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔
∀ (b : β), b ∈ s → ∀ (ε : ℝ), ε > 0 → filter.eventually (fun (x : β) => dist (f x) (f b) < ε) (nhds_within b s) := sorry
theorem continuous_iff' {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} : continuous f ↔ ∀ (a : β) (ε : ℝ), ε > 0 → filter.eventually (fun (x : β) => dist (f x) (f a) < ε) (nhds a) :=
iff.trans continuous_iff_continuous_at (forall_congr fun (b : β) => tendsto_nhds)
theorem tendsto_at_top {α : Type u} {β : Type v} [metric_space α] [Nonempty β] [semilattice_sup β] {u : β → α} {a : α} : filter.tendsto u filter.at_top (nhds a) ↔ ∀ (ε : ℝ), ε > 0 → ∃ (N : β), ∀ (n : β), n ≥ N → dist (u n) a < ε := sorry
theorem is_open_singleton_iff {X : Type u_1} [metric_space X] {x : X} : is_open (singleton x) ↔ ∃ (ε : ℝ), ∃ (H : ε > 0), ∀ (y : X), dist y x < ε → y = x := sorry
/-- Given a point `x` in a discrete subset `s` of a metric space, there is an open ball
centered at `x` and intersecting `s` only at `x`. -/
theorem exists_ball_inter_eq_singleton_of_mem_discrete {α : Type u} [metric_space α] {s : set α} [discrete_topology ↥s] {x : α} (hx : x ∈ s) : ∃ (ε : ℝ), ∃ (H : ε > 0), ball x ε ∩ s = singleton x :=
filter.has_basis.exists_inter_eq_singleton_of_mem_discrete nhds_basis_ball hx
/-- Given a point `x` in a discrete subset `s` of a metric space, there is a closed ball
of positive radius centered at `x` and intersecting `s` only at `x`. -/
theorem exists_closed_ball_inter_eq_singleton_of_discrete {α : Type u} [metric_space α] {s : set α} [discrete_topology ↥s] {x : α} (hx : x ∈ s) : ∃ (ε : ℝ), ∃ (H : ε > 0), closed_ball x ε ∩ s = singleton x :=
filter.has_basis.exists_inter_eq_singleton_of_mem_discrete nhds_basis_closed_ball hx
end metric
protected instance metric_space.to_separated {α : Type u} [metric_space α] : separated_space α :=
iff.mpr separated_def
fun (x y : α) (h : ∀ (r : set (α × α)), r ∈ uniformity α → (x, y) ∈ r) =>
eq_of_forall_dist_le
fun (ε : ℝ) (ε0 : ε > 0) =>
le_of_lt (h (set_of fun (p : α × α) => dist (prod.fst p) (prod.snd p) < ε) (metric.dist_mem_uniformity ε0))
/-Instantiate a metric space as an emetric space. Before we can state the instance,
we need to show that the uniform structure coming from the edistance and the
distance coincide. -/
/-- Expressing the uniformity in terms of `edist` -/
protected theorem metric.uniformity_basis_edist {α : Type u} [metric_space α] : filter.has_basis (uniformity α) (fun (ε : ennreal) => 0 < ε)
fun (ε : ennreal) => set_of fun (p : α × α) => edist (prod.fst p) (prod.snd p) < ε := sorry
theorem metric.uniformity_edist {α : Type u} [metric_space α] : uniformity α =
infi
fun (ε : ennreal) =>
infi fun (H : ε > 0) => filter.principal (set_of fun (p : α × α) => edist (prod.fst p) (prod.snd p) < ε) :=
filter.has_basis.eq_binfi metric.uniformity_basis_edist
/-- A metric space induces an emetric space -/
protected instance metric_space.to_emetric_space {α : Type u} [metric_space α] : emetric_space α :=
emetric_space.mk sorry sorry sorry sorry metric_space.to_uniform_space
/-- Balls defined using the distance or the edistance coincide -/
theorem metric.emetric_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : emetric.ball x (ennreal.of_real ε) = metric.ball x ε := sorry
/-- Balls defined using the distance or the edistance coincide -/
theorem metric.emetric_ball_nnreal {α : Type u} [metric_space α] {x : α} {ε : nnreal} : emetric.ball x ↑ε = metric.ball x ↑ε := sorry
/-- Closed balls defined using the distance or the edistance coincide -/
theorem metric.emetric_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} (h : 0 ≤ ε) : emetric.closed_ball x (ennreal.of_real ε) = metric.closed_ball x ε := sorry
/-- Closed balls defined using the distance or the edistance coincide -/
theorem metric.emetric_closed_ball_nnreal {α : Type u} [metric_space α] {x : α} {ε : nnreal} : emetric.closed_ball x ↑ε = metric.closed_ball x ↑ε := sorry
/-- Build a new metric space from an old one where the bundled uniform structure is provably
(but typically non-definitionaly) equal to some given uniform structure.
See Note [forgetful inheritance].
-/
def metric_space.replace_uniformity {α : Type u_1} [U : uniform_space α] (m : metric_space α) (H : uniformity α = uniformity α) : metric_space α :=
metric_space.mk dist_self eq_of_dist_eq_zero dist_comm dist_triangle edist U
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. In this definition, the distance
is given separately, to be able to prescribe some expression which is not defeq to the push-forward
of the edistance to reals. -/
def emetric_space.to_metric_space_of_dist {α : Type u} [e : emetric_space α] (dist : α → α → ℝ) (edist_ne_top : ∀ (x y : α), edist x y ≠ ⊤) (h : ∀ (x y : α), dist x y = ennreal.to_real (edist x y)) : metric_space α :=
let m : metric_space α :=
metric_space.mk sorry sorry sorry sorry (fun (x y : α) => edist x y) (uniform_space_of_dist dist sorry sorry sorry);
metric_space.replace_uniformity m sorry
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. -/
def emetric_space.to_metric_space {α : Type u} [e : emetric_space α] (h : ∀ (x y : α), edist x y ≠ ⊤) : metric_space α :=
emetric_space.to_metric_space_of_dist (fun (x y : α) => ennreal.to_real (edist x y)) h sorry
/-- A very useful criterion to show that a space is complete is to show that all sequences
which satisfy a bound of the form `dist (u n) (u m) < B N` for all `n m ≥ N` are
converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to
`0`, which makes it possible to use arguments of converging series, while this is impossible
to do in general for arbitrary Cauchy sequences. -/
theorem metric.complete_of_convergent_controlled_sequences {α : Type u} [metric_space α] (B : ℕ → ℝ) (hB : ∀ (n : ℕ), 0 < B n) (H : ∀ (u : ℕ → α),
(∀ (N n m : ℕ), N ≤ n → N ≤ m → dist (u n) (u m) < B N) → ∃ (x : α), filter.tendsto u filter.at_top (nhds x)) : complete_space α := sorry
theorem metric.complete_of_cauchy_seq_tendsto {α : Type u} [metric_space α] : (∀ (u : ℕ → α), cauchy_seq u → ∃ (a : α), filter.tendsto u filter.at_top (nhds a)) → complete_space α :=
emetric.complete_of_cauchy_seq_tendsto
/-- Instantiate the reals as a metric space. -/
protected instance real.metric_space : metric_space ℝ :=
metric_space.mk sorry sorry sorry sorry (fun (x y : ℝ) => ennreal.of_real ((fun (x y : ℝ) => abs (x - y)) x y))
(uniform_space_of_dist (fun (x y : ℝ) => abs (x - y)) sorry sorry sorry)
theorem real.dist_eq (x : ℝ) (y : ℝ) : dist x y = abs (x - y) :=
rfl
theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = abs x := sorry
protected instance real.order_topology : order_topology ℝ := sorry
theorem closed_ball_Icc {x : ℝ} {r : ℝ} : metric.closed_ball x r = set.Icc (x - r) (x + r) := sorry
/-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the
general case. -/
theorem squeeze_zero' {α : Type u_1} {f : α → ℝ} {g : α → ℝ} {t₀ : filter α} (hf : filter.eventually (fun (t : α) => 0 ≤ f t) t₀) (hft : filter.eventually (fun (t : α) => f t ≤ g t) t₀) (g0 : filter.tendsto g t₀ (nhds 0)) : filter.tendsto f t₀ (nhds 0) :=
tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds g0 hf hft
/-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le`
and `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the general case. -/
theorem squeeze_zero {α : Type u_1} {f : α → ℝ} {g : α → ℝ} {t₀ : filter α} (hf : ∀ (t : α), 0 ≤ f t) (hft : ∀ (t : α), f t ≤ g t) (g0 : filter.tendsto g t₀ (nhds 0)) : filter.tendsto f t₀ (nhds 0) :=
squeeze_zero' (filter.eventually_of_forall hf) (filter.eventually_of_forall hft) g0
theorem metric.uniformity_eq_comap_nhds_zero {α : Type u} [metric_space α] : uniformity α = filter.comap (fun (p : α × α) => dist (prod.fst p) (prod.snd p)) (nhds 0) := sorry
theorem cauchy_seq_iff_tendsto_dist_at_top_0 {α : Type u} {β : Type v} [metric_space α] [Nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ filter.tendsto (fun (n : β × β) => dist (u (prod.fst n)) (u (prod.snd n))) filter.at_top (nhds 0) := sorry
theorem tendsto_uniformity_iff_dist_tendsto_zero {α : Type u} [metric_space α] {ι : Type u_1} {f : ι → α × α} {p : filter ι} : filter.tendsto f p (uniformity α) ↔ filter.tendsto (fun (x : ι) => dist (prod.fst (f x)) (prod.snd (f x))) p (nhds 0) := sorry
theorem filter.tendsto.congr_dist {α : Type u} [metric_space α] {ι : Type u_1} {f₁ : ι → α} {f₂ : ι → α} {p : filter ι} {a : α} (h₁ : filter.tendsto f₁ p (nhds a)) (h : filter.tendsto (fun (x : ι) => dist (f₁ x) (f₂ x)) p (nhds 0)) : filter.tendsto f₂ p (nhds a) :=
filter.tendsto.congr_uniformity h₁ (iff.mpr tendsto_uniformity_iff_dist_tendsto_zero h)
theorem tendsto_of_tendsto_of_dist {α : Type u} [metric_space α] {ι : Type u_1} {f₁ : ι → α} {f₂ : ι → α} {p : filter ι} {a : α} (h₁ : filter.tendsto f₁ p (nhds a)) (h : filter.tendsto (fun (x : ι) => dist (f₁ x) (f₂ x)) p (nhds 0)) : filter.tendsto f₂ p (nhds a) :=
filter.tendsto.congr_dist
theorem tendsto_iff_of_dist {α : Type u} [metric_space α] {ι : Type u_1} {f₁ : ι → α} {f₂ : ι → α} {p : filter ι} {a : α} (h : filter.tendsto (fun (x : ι) => dist (f₁ x) (f₂ x)) p (nhds 0)) : filter.tendsto f₁ p (nhds a) ↔ filter.tendsto f₂ p (nhds a) :=
uniform.tendsto_congr (iff.mpr tendsto_uniformity_iff_dist_tendsto_zero h)
/-- In a metric space, Cauchy sequences are characterized by the fact that, eventually,
the distance between its elements is arbitrarily small -/
theorem metric.cauchy_seq_iff {α : Type u} {β : Type v} [metric_space α] [Nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ (ε : ℝ), ε > 0 → ∃ (N : β), ∀ (m n : β), m ≥ N → n ≥ N → dist (u m) (u n) < ε :=
filter.has_basis.cauchy_seq_iff metric.uniformity_basis_dist
/-- A variation around the metric characterization of Cauchy sequences -/
theorem metric.cauchy_seq_iff' {α : Type u} {β : Type v} [metric_space α] [Nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ (ε : ℝ), ε > 0 → ∃ (N : β), ∀ (n : β), n ≥ N → dist (u n) (u N) < ε :=
filter.has_basis.cauchy_seq_iff' metric.uniformity_basis_dist
/-- If the distance between `s n` and `s m`, `n, m ≥ N` is bounded above by `b N`
and `b` converges to zero, then `s` is a Cauchy sequence. -/
theorem cauchy_seq_of_le_tendsto_0 {α : Type u} {β : Type v} [metric_space α] [Nonempty β] [semilattice_sup β] {s : β → α} (b : β → ℝ) (h : ∀ (n m N : β), N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) (h₀ : filter.tendsto b filter.at_top (nhds 0)) : cauchy_seq s := sorry
/-- A Cauchy sequence on the natural numbers is bounded. -/
theorem cauchy_seq_bdd {α : Type u} [metric_space α] {u : ℕ → α} (hu : cauchy_seq u) : ∃ (R : ℝ), ∃ (H : R > 0), ∀ (m n : ℕ), dist (u m) (u n) < R := sorry
/-- Yet another metric characterization of Cauchy sequences on integers. This one is often the
most efficient. -/
theorem cauchy_seq_iff_le_tendsto_0 {α : Type u} [metric_space α] {s : ℕ → α} : cauchy_seq s ↔
∃ (b : ℕ → ℝ),
(∀ (n : ℕ), 0 ≤ b n) ∧
(∀ (n m N : ℕ), N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) ∧ filter.tendsto b filter.at_top (nhds 0) := sorry
/-- Metric space structure pulled back by an injective function. Injectivity is necessary to
ensure that `dist x y = 0` only if `x = y`. -/
def metric_space.induced {α : Type u_1} {β : Type u_2} (f : α → β) (hf : function.injective f) (m : metric_space β) : metric_space α :=
metric_space.mk sorry sorry sorry sorry (fun (x y : α) => edist (f x) (f y))
(uniform_space.comap f metric_space.to_uniform_space)
protected instance subtype.metric_space {α : Type u_1} {p : α → Prop} [t : metric_space α] : metric_space (Subtype p) :=
metric_space.induced coe sorry t
theorem subtype.dist_eq {α : Type u} [metric_space α] {p : α → Prop} (x : Subtype p) (y : Subtype p) : dist x y = dist ↑x ↑y :=
rfl
protected instance nnreal.metric_space : metric_space nnreal :=
eq.mpr sorry subtype.metric_space
theorem nnreal.dist_eq (a : nnreal) (b : nnreal) : dist a b = abs (↑a - ↑b) :=
rfl
theorem nnreal.nndist_eq (a : nnreal) (b : nnreal) : nndist a b = max (a - b) (b - a) := sorry
protected instance prod.metric_space_max {α : Type u} {β : Type v} [metric_space α] [metric_space β] : metric_space (α × β) :=
metric_space.mk sorry sorry sorry sorry
(fun (x y : α × β) => max (edist (prod.fst x) (prod.fst y)) (edist (prod.snd x) (prod.snd y))) prod.uniform_space
theorem prod.dist_eq {α : Type u} {β : Type v} [metric_space α] [metric_space β] {x : α × β} {y : α × β} : dist x y = max (dist (prod.fst x) (prod.fst y)) (dist (prod.snd x) (prod.snd y)) :=
rfl
theorem ball_prod_same {α : Type u} {β : Type v} [metric_space α] [metric_space β] (x : α) (y : β) (r : ℝ) : set.prod (metric.ball x r) (metric.ball y r) = metric.ball (x, y) r := sorry
theorem closed_ball_prod_same {α : Type u} {β : Type v} [metric_space α] [metric_space β] (x : α) (y : β) (r : ℝ) : set.prod (metric.closed_ball x r) (metric.closed_ball y r) = metric.closed_ball (x, y) r := sorry
theorem uniform_continuous_dist {α : Type u} [metric_space α] : uniform_continuous fun (p : α × α) => dist (prod.fst p) (prod.snd p) := sorry
theorem uniform_continuous.dist {α : Type u} {β : Type v} [metric_space α] [uniform_space β] {f : β → α} {g : β → α} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous fun (b : β) => dist (f b) (g b) :=
uniform_continuous.comp uniform_continuous_dist (uniform_continuous.prod_mk hf hg)
theorem continuous_dist {α : Type u} [metric_space α] : continuous fun (p : α × α) => dist (prod.fst p) (prod.snd p) :=
uniform_continuous.continuous uniform_continuous_dist
theorem continuous.dist {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous fun (b : β) => dist (f b) (g b) :=
continuous.comp continuous_dist (continuous.prod_mk hf hg)
theorem filter.tendsto.dist {α : Type u} {β : Type v} [metric_space α] {f : β → α} {g : β → α} {x : filter β} {a : α} {b : α} (hf : filter.tendsto f x (nhds a)) (hg : filter.tendsto g x (nhds b)) : filter.tendsto (fun (x : β) => dist (f x) (g x)) x (nhds (dist a b)) :=
filter.tendsto.comp (continuous.tendsto continuous_dist (a, b)) (filter.tendsto.prod_mk_nhds hf hg)
theorem nhds_comap_dist {α : Type u} [metric_space α] (a : α) : filter.comap (fun (a' : α) => dist a' a) (nhds 0) = nhds a := sorry
theorem tendsto_iff_dist_tendsto_zero {α : Type u} {β : Type v} [metric_space α] {f : β → α} {x : filter β} {a : α} : filter.tendsto f x (nhds a) ↔ filter.tendsto (fun (b : β) => dist (f b) a) x (nhds 0) := sorry
theorem uniform_continuous_nndist {α : Type u} [metric_space α] : uniform_continuous fun (p : α × α) => nndist (prod.fst p) (prod.snd p) :=
uniform_continuous_subtype_mk uniform_continuous_dist fun (p : α × α) => dist_nonneg
theorem uniform_continuous.nndist {α : Type u} {β : Type v} [metric_space α] [uniform_space β] {f : β → α} {g : β → α} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous fun (b : β) => nndist (f b) (g b) :=
uniform_continuous.comp uniform_continuous_nndist (uniform_continuous.prod_mk hf hg)
theorem continuous_nndist {α : Type u} [metric_space α] : continuous fun (p : α × α) => nndist (prod.fst p) (prod.snd p) :=
uniform_continuous.continuous uniform_continuous_nndist
theorem continuous.nndist {α : Type u} {β : Type v} [metric_space α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous fun (b : β) => nndist (f b) (g b) :=
continuous.comp continuous_nndist (continuous.prod_mk hf hg)
theorem filter.tendsto.nndist {α : Type u} {β : Type v} [metric_space α] {f : β → α} {g : β → α} {x : filter β} {a : α} {b : α} (hf : filter.tendsto f x (nhds a)) (hg : filter.tendsto g x (nhds b)) : filter.tendsto (fun (x : β) => nndist (f x) (g x)) x (nhds (nndist a b)) :=
filter.tendsto.comp (continuous.tendsto continuous_nndist (a, b)) (filter.tendsto.prod_mk_nhds hf hg)
namespace metric
theorem is_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : is_closed (closed_ball x ε) :=
is_closed_le (continuous.dist continuous_id continuous_const) continuous_const
theorem is_closed_sphere {α : Type u} [metric_space α] {x : α} {ε : ℝ} : is_closed (sphere x ε) :=
is_closed_eq (continuous.dist continuous_id continuous_const) continuous_const
@[simp] theorem closure_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : closure (closed_ball x ε) = closed_ball x ε :=
is_closed.closure_eq is_closed_ball
theorem closure_ball_subset_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : closure (ball x ε) ⊆ closed_ball x ε :=
closure_minimal ball_subset_closed_ball is_closed_ball
theorem frontier_ball_subset_sphere {α : Type u} [metric_space α] {x : α} {ε : ℝ} : frontier (ball x ε) ⊆ sphere x ε :=
frontier_lt_subset_eq (continuous.dist continuous_id continuous_const) continuous_const
theorem frontier_closed_ball_subset_sphere {α : Type u} [metric_space α] {x : α} {ε : ℝ} : frontier (closed_ball x ε) ⊆ sphere x ε :=
frontier_le_subset_eq (continuous.dist continuous_id continuous_const) continuous_const
theorem ball_subset_interior_closed_ball {α : Type u} [metric_space α] {x : α} {ε : ℝ} : ball x ε ⊆ interior (closed_ball x ε) :=
interior_maximal ball_subset_closed_ball is_open_ball
/-- ε-characterization of the closure in metric spaces-/
theorem mem_closure_iff {α : Type u} [metric_space α] {s : set α} {a : α} : a ∈ closure s ↔ ∀ (ε : ℝ) (H : ε > 0), ∃ (b : α), ∃ (H : b ∈ s), dist a b < ε := sorry
theorem mem_closure_range_iff {β : Type v} {α : Type u} [metric_space α] {e : β → α} {a : α} : a ∈ closure (set.range e) ↔ ∀ (ε : ℝ), ε > 0 → ∃ (k : β), dist a (e k) < ε := sorry
theorem mem_closure_range_iff_nat {β : Type v} {α : Type u} [metric_space α] {e : β → α} {a : α} : a ∈ closure (set.range e) ↔ ∀ (n : ℕ), ∃ (k : β), dist a (e k) < 1 / (↑n + 1) := sorry
theorem mem_of_closed' {α : Type u} [metric_space α] {s : set α} (hs : is_closed s) {a : α} : a ∈ s ↔ ∀ (ε : ℝ) (H : ε > 0), ∃ (b : α), ∃ (H : b ∈ s), dist a b < ε := sorry
end metric
/-- A finite product of metric spaces is a metric space, with the sup distance. -/
protected instance metric_space_pi {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] : metric_space ((b : β) → π b) :=
emetric_space.to_metric_space_of_dist
(fun (f g : (b : β) → π b) => ↑(finset.sup finset.univ fun (b : β) => nndist (f b) (g b))) sorry sorry
theorem nndist_pi_def {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (f : (b : β) → π b) (g : (b : β) → π b) : nndist f g = finset.sup finset.univ fun (b : β) => nndist (f b) (g b) :=
subtype.eta (finset.sup finset.univ fun (b : β) => nndist (f b) (g b)) dist_nonneg
theorem dist_pi_def {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (f : (b : β) → π b) (g : (b : β) → π b) : dist f g = ↑(finset.sup finset.univ fun (b : β) => nndist (f b) (g b)) :=
rfl
@[simp] theorem dist_pi_const {α : Type u} {β : Type v} [metric_space α] [fintype β] [Nonempty β] (a : α) (b : α) : (dist (fun (x : β) => a) fun (_x : β) => b) = dist a b := sorry
@[simp] theorem nndist_pi_const {α : Type u} {β : Type v} [metric_space α] [fintype β] [Nonempty β] (a : α) (b : α) : (nndist (fun (x : β) => a) fun (_x : β) => b) = nndist a b :=
nnreal.eq (dist_pi_const a b)
theorem dist_pi_lt_iff {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] {f : (b : β) → π b} {g : (b : β) → π b} {r : ℝ} (hr : 0 < r) : dist f g < r ↔ ∀ (b : β), dist (f b) (g b) < r := sorry
theorem dist_pi_le_iff {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] {f : (b : β) → π b} {g : (b : β) → π b} {r : ℝ} (hr : 0 ≤ r) : dist f g ≤ r ↔ ∀ (b : β), dist (f b) (g b) ≤ r := sorry
theorem nndist_le_pi_nndist {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (f : (b : β) → π b) (g : (b : β) → π b) (b : β) : nndist (f b) (g b) ≤ nndist f g :=
eq.mpr (id (Eq._oldrec (Eq.refl (nndist (f b) (g b) ≤ nndist f g)) (nndist_pi_def f g)))
(finset.le_sup (finset.mem_univ b))
theorem dist_le_pi_dist {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (f : (b : β) → π b) (g : (b : β) → π b) (b : β) : dist (f b) (g b) ≤ dist f g := sorry
/-- An open ball in a product space is a product of open balls. The assumption `0 < r`
is necessary for the case of the empty product. -/
theorem ball_pi {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (x : (b : β) → π b) {r : ℝ} (hr : 0 < r) : metric.ball x r = set_of fun (y : (b : β) → π b) => ∀ (b : β), y b ∈ metric.ball (x b) r := sorry
/-- A closed ball in a product space is a product of closed balls. The assumption `0 ≤ r`
is necessary for the case of the empty product. -/
theorem closed_ball_pi {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] (x : (b : β) → π b) {r : ℝ} (hr : 0 ≤ r) : metric.closed_ball x r = set_of fun (y : (b : β) → π b) => ∀ (b : β), y b ∈ metric.closed_ball (x b) r := sorry
/-- Any compact set in a metric space can be covered by finitely many balls of a given positive
radius -/
theorem finite_cover_balls_of_compact {α : Type u} [metric_space α] {s : set α} (hs : is_compact s) {e : ℝ} (he : 0 < e) : ∃ (t : set α), ∃ (H : t ⊆ s), set.finite t ∧ s ⊆ set.Union fun (x : α) => set.Union fun (H : x ∈ t) => metric.ball x e := sorry
theorem is_compact.finite_cover_balls {α : Type u} [metric_space α] {s : set α} (hs : is_compact s) {e : ℝ} (he : 0 < e) : ∃ (t : set α), ∃ (H : t ⊆ s), set.finite t ∧ s ⊆ set.Union fun (x : α) => set.Union fun (H : x ∈ t) => metric.ball x e :=
finite_cover_balls_of_compact
/-- A metric space is proper if all closed balls are compact. -/
class proper_space (α : Type u) [metric_space α]
where
compact_ball : ∀ (x : α) (r : ℝ), is_compact (metric.closed_ball x r)
theorem tendsto_dist_right_cocompact_at_top {α : Type u} [metric_space α] [proper_space α] (x : α) : filter.tendsto (fun (y : α) => dist y x) (filter.cocompact α) filter.at_top := sorry
theorem tendsto_dist_left_cocompact_at_top {α : Type u} [metric_space α] [proper_space α] (x : α) : filter.tendsto (dist x) (filter.cocompact α) filter.at_top := sorry
/-- If all closed balls of large enough radius are compact, then the space is proper. Especially
useful when the lower bound for the radius is 0. -/
theorem proper_space_of_compact_closed_ball_of_le {α : Type u} [metric_space α] (R : ℝ) (h : ∀ (x : α) (r : ℝ), R ≤ r → is_compact (metric.closed_ball x r)) : proper_space α := sorry
/- A compact metric space is proper -/
protected instance proper_of_compact {α : Type u} [metric_space α] [compact_space α] : proper_space α :=
proper_space.mk fun (x : α) (r : ℝ) => is_closed.compact metric.is_closed_ball
/-- A proper space is locally compact -/
protected instance locally_compact_of_proper {α : Type u} [metric_space α] [proper_space α] : locally_compact_space α :=
locally_compact_of_compact_nhds
fun (x : α) =>
Exists.intro (metric.closed_ball x 1)
{ left :=
iff.mpr metric.mem_nhds_iff
(Exists.intro 1
(eq.mpr
(id
(Eq.trans (propext exists_prop)
((fun (a a_1 : Prop) (e_1 : a = a_1) (b b_1 : Prop) (e_2 : b = b_1) =>
congr (congr_arg And e_1) e_2)
(1 > 0) (0 < 1) (propext gt_iff_lt) (metric.ball x 1 ⊆ metric.closed_ball x 1)
(metric.ball x 1 ⊆ metric.closed_ball x 1) (Eq.refl (metric.ball x 1 ⊆ metric.closed_ball x 1)))))
{ left := zero_lt_one, right := metric.ball_subset_closed_ball })),
right := proper_space.compact_ball x 1 }
/-- A proper space is complete -/
protected instance complete_of_proper {α : Type u} [metric_space α] [proper_space α] : complete_space α := sorry
/-- A proper metric space is separable, and therefore second countable. Indeed, any ball is
compact, and therefore admits a countable dense subset. Taking a countable union over the balls
centered at a fixed point and with integer radius, one obtains a countable set which is
dense in the whole space. -/
protected instance second_countable_of_proper {α : Type u} [metric_space α] [proper_space α] : topological_space.second_countable_topology α :=
emetric.second_countable_of_separable α
/-- A finite product of proper spaces is proper. -/
protected instance pi_proper_space {β : Type v} {π : β → Type u_1} [fintype β] [(b : β) → metric_space (π b)] [h : ∀ (b : β), proper_space (π b)] : proper_space ((b : β) → π b) :=
proper_space_of_compact_closed_ball_of_le 0
fun (x : (b : β) → π b) (r : ℝ) (hr : 0 ≤ r) =>
eq.mpr (id (Eq._oldrec (Eq.refl (is_compact (metric.closed_ball x r))) (closed_ball_pi x hr)))
(compact_pi_infinite fun (b : β) => proper_space.compact_ball (x b) r)
namespace metric
/-- A metric space is second countable if, for every `ε > 0`, there is a countable set which is
`ε`-dense. -/
theorem second_countable_of_almost_dense_set {α : Type u} [metric_space α] (H : ∀ (ε : ℝ) (H : ε > 0), ∃ (s : set α), set.countable s ∧ ∀ (x : α), ∃ (y : α), ∃ (H : y ∈ s), dist x y ≤ ε) : topological_space.second_countable_topology α := sorry
/-- A metric space space is second countable if one can reconstruct up to any `ε>0` any element of
the space from countably many data. -/
theorem second_countable_of_countable_discretization {α : Type u} [metric_space α] (H : ∀ (ε : ℝ), ε > 0 → ∃ (β : Type u_1), Exists (∃ (F : α → β), ∀ (x y : α), F x = F y → dist x y ≤ ε)) : topological_space.second_countable_topology α := sorry
end metric
theorem lebesgue_number_lemma_of_metric {α : Type u} [metric_space α] {s : set α} {ι : Sort u_1} {c : ι → set α} (hs : is_compact s) (hc₁ : ∀ (i : ι), is_open (c i)) (hc₂ : s ⊆ set.Union fun (i : ι) => c i) : ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ (x : α), x ∈ s → ∃ (i : ι), metric.ball x δ ⊆ c i := sorry
theorem lebesgue_number_lemma_of_metric_sUnion {α : Type u} [metric_space α] {s : set α} {c : set (set α)} (hs : is_compact s) (hc₁ : ∀ (t : set α), t ∈ c → is_open t) (hc₂ : s ⊆ ⋃₀c) : ∃ (δ : ℝ), ∃ (H : δ > 0), ∀ (x : α) (H : x ∈ s), ∃ (t : set α), ∃ (H : t ∈ c), metric.ball x δ ⊆ t := sorry
namespace metric
/-- Boundedness of a subset of a metric space. We formulate the definition to work
even in the empty space. -/
def bounded {α : Type u} [metric_space α] (s : set α) :=
∃ (C : ℝ), ∀ (x y : α), x ∈ s → y ∈ s → dist x y ≤ C
@[simp] theorem bounded_empty {α : Type u} [metric_space α] : bounded ∅ := sorry
theorem bounded_iff_mem_bounded {α : Type u} [metric_space α] {s : set α} : bounded s ↔ ∀ (x : α), x ∈ s → bounded s := sorry
/-- Subsets of a bounded set are also bounded -/
theorem bounded.subset {α : Type u} [metric_space α] {s : set α} {t : set α} (incl : s ⊆ t) : bounded t → bounded s :=
Exists.imp
fun (C : ℝ) (hC : ∀ (x y : α), x ∈ t → y ∈ t → dist x y ≤ C) (x y : α) (hx : x ∈ s) (hy : y ∈ s) =>
hC x y (incl hx) (incl hy)
/-- Closed balls are bounded -/
theorem bounded_closed_ball {α : Type u} [metric_space α] {x : α} {r : ℝ} : bounded (closed_ball x r) := sorry
/-- Open balls are bounded -/
theorem bounded_ball {α : Type u} [metric_space α] {x : α} {r : ℝ} : bounded (ball x r) :=
bounded.subset ball_subset_closed_ball bounded_closed_ball
/-- Given a point, a bounded subset is included in some ball around this point -/
theorem bounded_iff_subset_ball {α : Type u} [metric_space α] {s : set α} (c : α) : bounded s ↔ ∃ (r : ℝ), s ⊆ closed_ball c r := sorry
theorem bounded_closure_of_bounded {α : Type u} [metric_space α] {s : set α} (h : bounded s) : bounded (closure s) := sorry
theorem Mathlib.bounded.closure {α : Type u} [metric_space α] {s : set α} (h : bounded s) : bounded (closure s) :=
bounded_closure_of_bounded
/-- The union of two bounded sets is bounded iff each of the sets is bounded -/
@[simp] theorem bounded_union {α : Type u} [metric_space α] {s : set α} {t : set α} : bounded (s ∪ t) ↔ bounded s ∧ bounded t := sorry
/-- A finite union of bounded sets is bounded -/
theorem bounded_bUnion {α : Type u} {β : Type v} [metric_space α] {I : set β} {s : β → set α} (H : set.finite I) : bounded (set.Union fun (i : β) => set.Union fun (H : i ∈ I) => s i) ↔ ∀ (i : β), i ∈ I → bounded (s i) := sorry
/-- A compact set is bounded -/
-- We cover the compact set by finitely many balls of radius 1,
theorem bounded_of_compact {α : Type u} [metric_space α] {s : set α} (h : is_compact s) : bounded s := sorry
-- and then argue that a finite union of bounded sets is bounded
theorem Mathlib.is_compact.bounded {α : Type u} [metric_space α] {s : set α} (h : is_compact s) : bounded s :=
bounded_of_compact
/-- A finite set is bounded -/
theorem bounded_of_finite {α : Type u} [metric_space α] {s : set α} (h : set.finite s) : bounded s :=
is_compact.bounded (set.finite.is_compact h)
/-- A singleton is bounded -/
theorem bounded_singleton {α : Type u} [metric_space α] {x : α} : bounded (singleton x) :=
bounded_of_finite (set.finite_singleton x)
/-- Characterization of the boundedness of the range of a function -/
theorem bounded_range_iff {α : Type u} {β : Type v} [metric_space α] {f : β → α} : bounded (set.range f) ↔ ∃ (C : ℝ), ∀ (x y : β), dist (f x) (f y) ≤ C := sorry
/-- In a compact space, all sets are bounded -/
theorem bounded_of_compact_space {α : Type u} [metric_space α] {s : set α} [compact_space α] : bounded s :=
bounded.subset (set.subset_univ s) (is_compact.bounded compact_univ)
/-- The Heine–Borel theorem:
In a proper space, a set is compact if and only if it is closed and bounded -/
theorem compact_iff_closed_bounded {α : Type u} [metric_space α] {s : set α} [proper_space α] : is_compact s ↔ is_closed s ∧ bounded s := sorry
/-- The image of a proper space under an expanding onto map is proper. -/
theorem proper_image_of_proper {α : Type u} {β : Type v} [metric_space α] [proper_space α] [metric_space β] (f : α → β) (f_cont : continuous f) (hf : set.range f = set.univ) (C : ℝ) (hC : ∀ (x y : α), dist x y ≤ C * dist (f x) (f y)) : proper_space β := sorry
/-- The diameter of a set in a metric space. To get controllable behavior even when the diameter
should be infinite, we express it in terms of the emetric.diameter -/
def diam {α : Type u} [metric_space α] (s : set α) : ℝ :=
ennreal.to_real (emetric.diam s)
/-- The diameter of a set is always nonnegative -/
theorem diam_nonneg {α : Type u} [metric_space α] {s : set α} : 0 ≤ diam s :=
ennreal.to_real_nonneg
theorem diam_subsingleton {α : Type u} [metric_space α] {s : set α} (hs : set.subsingleton s) : diam s = 0 := sorry
/-- The empty set has zero diameter -/
@[simp] theorem diam_empty {α : Type u} [metric_space α] : diam ∅ = 0 :=
diam_subsingleton set.subsingleton_empty
/-- A singleton has zero diameter -/
@[simp] theorem diam_singleton {α : Type u} [metric_space α] {x : α} : diam (singleton x) = 0 :=
diam_subsingleton set.subsingleton_singleton
-- Does not work as a simp-lemma, since {x, y} reduces to (insert y {x})
theorem diam_pair {α : Type u} [metric_space α] {x : α} {y : α} : diam (insert x (singleton y)) = dist x y := sorry
-- Does not work as a simp-lemma, since {x, y, z} reduces to (insert z (insert y {x}))
theorem diam_triple {α : Type u} [metric_space α] {x : α} {y : α} {z : α} : diam (insert x (insert y (singleton z))) = max (max (dist x y) (dist x z)) (dist y z) := sorry
/-- If the distance between any two points in a set is bounded by some constant `C`,
then `ennreal.of_real C` bounds the emetric diameter of this set. -/
theorem ediam_le_of_forall_dist_le {α : Type u} [metric_space α] {s : set α} {C : ℝ} (h : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → dist x y ≤ C) : emetric.diam s ≤ ennreal.of_real C :=
emetric.diam_le_of_forall_edist_le
fun (x : α) (hx : x ∈ s) (y : α) (hy : y ∈ s) => Eq.symm (edist_dist x y) ▸ ennreal.of_real_le_of_real (h x hx y hy)
/-- If the distance between any two points in a set is bounded by some non-negative constant,
this constant bounds the diameter. -/
theorem diam_le_of_forall_dist_le {α : Type u} [metric_space α] {s : set α} {C : ℝ} (h₀ : 0 ≤ C) (h : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → dist x y ≤ C) : diam s ≤ C :=
ennreal.to_real_le_of_le_of_real h₀ (ediam_le_of_forall_dist_le h)
/-- If the distance between any two points in a nonempty set is bounded by some constant,
this constant bounds the diameter. -/
theorem diam_le_of_forall_dist_le_of_nonempty {α : Type u} [metric_space α] {s : set α} (hs : set.nonempty s) {C : ℝ} (h : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → dist x y ≤ C) : diam s ≤ C := sorry
/-- The distance between two points in a set is controlled by the diameter of the set. -/
theorem dist_le_diam_of_mem' {α : Type u} [metric_space α] {s : set α} {x : α} {y : α} (h : emetric.diam s ≠ ⊤) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s := sorry
/-- Characterize the boundedness of a set in terms of the finiteness of its emetric.diameter. -/
theorem bounded_iff_ediam_ne_top {α : Type u} [metric_space α] {s : set α} : bounded s ↔ emetric.diam s ≠ ⊤ := sorry
theorem bounded.ediam_ne_top {α : Type u} [metric_space α] {s : set α} (h : bounded s) : emetric.diam s ≠ ⊤ :=
iff.mp bounded_iff_ediam_ne_top h
/-- The distance between two points in a set is controlled by the diameter of the set. -/
theorem dist_le_diam_of_mem {α : Type u} [metric_space α] {s : set α} {x : α} {y : α} (h : bounded s) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s :=
dist_le_diam_of_mem' (bounded.ediam_ne_top h) hx hy
/-- An unbounded set has zero diameter. If you would prefer to get the value ∞, use `emetric.diam`.
This lemma makes it possible to avoid side conditions in some situations -/
theorem diam_eq_zero_of_unbounded {α : Type u} [metric_space α] {s : set α} (h : ¬bounded s) : diam s = 0 := sorry
/-- If `s ⊆ t`, then the diameter of `s` is bounded by that of `t`, provided `t` is bounded. -/
theorem diam_mono {α : Type u} [metric_space α] {s : set α} {t : set α} (h : s ⊆ t) (ht : bounded t) : diam s ≤ diam t := sorry
/-- The diameter of a union is controlled by the sum of the diameters, and the distance between
any two points in each of the sets. This lemma is true without any side condition, since it is
obviously true if `s ∪ t` is unbounded. -/
theorem diam_union {α : Type u} [metric_space α] {s : set α} {x : α} {y : α} {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + dist x y + diam t := sorry
/-- If two sets intersect, the diameter of the union is bounded by the sum of the diameters. -/
theorem diam_union' {α : Type u} [metric_space α] {s : set α} {t : set α} (h : set.nonempty (s ∩ t)) : diam (s ∪ t) ≤ diam s + diam t := sorry
/-- The diameter of a closed ball of radius `r` is at most `2 r`. -/
theorem diam_closed_ball {α : Type u} [metric_space α] {x : α} {r : ℝ} (h : 0 ≤ r) : diam (closed_ball x r) ≤ bit0 1 * r := sorry
/-- The diameter of a ball of radius `r` is at most `2 r`. -/
theorem diam_ball {α : Type u} [metric_space α] {x : α} {r : ℝ} (h : 0 ≤ r) : diam (ball x r) ≤ bit0 1 * r :=
le_trans (diam_mono ball_subset_closed_ball bounded_closed_ball) (diam_closed_ball h)
|
7062e9c8b129416732427b40aadda8022da1f3af | eb9357a70318e50e095b58730bebfe0cffee457f | /lean/love11_logical_foundations_of_mathematics_exercise_sheet.lean | be1ed35c95d1563912bac18fc86de7b653155475 | [] | no_license | Vierkantor/logical_verification_2021 | 7485dd916953131d501760f023d5b30fbb74d36a | 9500b9c194e22a9ab4067321cfed7a1f445afcfc | refs/heads/main | 1,692,560,845,086 | 1,624,721,275,000 | 1,624,721,275,000 | 416,354,079 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,996 | lean | import .love11_logical_foundations_of_mathematics_demo
/-! # LoVe Exercise 11: Logical Foundations of Mathematics -/
set_option pp.beta true
set_option pp.generalized_field_notation false
namespace LoVe
/-! ## Question 1: Vectors as Subtypes
Recall the definition of vectors from the demo: -/
#check vector
/-! The following function adds two lists of integers elementwise. If one
function is longer than the other, the tail of the longer function is
truncated. -/
def list.add : list ℤ → list ℤ → list ℤ
| [] [] := []
| (x :: xs) (y :: ys) := (x + y) :: list.add xs ys
| [] (y :: ys) := []
| (x :: xs) [] := []
/-! 1.1. Show that if the lists have the same length, the resulting list also
has that length. -/
lemma list.length_add :
∀(xs : list ℤ) (ys : list ℤ) (h : list.length xs = list.length ys),
list.length (list.add xs ys) = list.length xs
| [] [] :=
sorry
| (x :: xs) (y :: ys) :=
sorry
| [] (y :: ys) :=
sorry
| (x :: xs) [] :=
sorry
/-! 1.2. Define componentwise addition on vectors using `list.add` and
`list.length_add`. -/
def vector.add {n : ℕ} : vector ℤ n → vector ℤ n → vector ℤ n :=
sorry
/-! 1.3. Show that `list.add` and `vector.add` are commutative. -/
lemma list.add.comm :
∀(xs : list ℤ) (ys : list ℤ), list.add xs ys = list.add ys xs :=
sorry
lemma vector.add.comm {n : ℕ} (x y : vector ℤ n) :
vector.add x y = vector.add y x :=
sorry
/-! ## Question 2: Integers as Quotients
Recall the construction of integers from the lecture, not to be confused with
Lean's predefined type `int` (= `ℤ`): -/
#check int.rel
#check int.rel_iff
#check int
/-! 2.1. Define negation on these integers. -/
def int.neg : int → int :=
sorry
/-! 2.2. Prove the following lemmas about negation. -/
lemma int.neg_eq (p n : ℕ) :
int.neg ⟦(p, n)⟧ = ⟦(n, p)⟧ :=
sorry
lemma int.neg_neg (a : int) :
int.neg (int.neg a) = a :=
sorry
end LoVe
|
3fb3870634cb9ba8c7c53dfcbf75dae20fac3ba3 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/fourier.lean | 159b337ce02c0111f112b9029921dc6e8d1e6fa4 | [
"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 | 10,699 | lean | /-
Copyright (c) 2021 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import analysis.complex.circle
import analysis.inner_product_space.l2_space
import measure_theory.function.continuous_map_dense
import measure_theory.function.l2_space
import measure_theory.measure.haar
import measure_theory.group.integration
import topology.metric_space.emetric_paracompact
import topology.continuous_function.stone_weierstrass
/-!
# Fourier analysis on the circle
This file contains basic results on Fourier series.
## Main definitions
* `haar_circle`, Haar measure on the circle, normalized to have total measure `1`
* instances `measure_space`, `is_probability_measure` for the circle with respect to this measure
* for `n : ℤ`, `fourier n` is the monomial `λ z, z ^ n`, bundled as a continuous map from `circle`
to `ℂ`
* for `n : ℤ` and `p : ℝ≥0∞`, `fourier_Lp p n` is an abbreviation for the monomial `fourier n`
considered as an element of the Lᵖ-space `Lp ℂ p haar_circle`, via the embedding
`continuous_map.to_Lp`
* `fourier_series` is the canonical isometric isomorphism from `Lp ℂ 2 haar_circle` to `ℓ²(ℤ, ℂ)`
induced by taking Fourier series
## Main statements
The theorem `span_fourier_closure_eq_top` states that the span of the monomials `fourier n` is
dense in `C(circle, ℂ)`, i.e. that its `submodule.topological_closure` is `⊤`. This follows from
the Stone-Weierstrass theorem after checking that it is a subalgebra, closed under conjugation, and
separates points.
The theorem `span_fourier_Lp_closure_eq_top` states that for `1 ≤ p < ∞` the span of the monomials
`fourier_Lp` is dense in `Lp ℂ p haar_circle`, i.e. that its `submodule.topological_closure` is
`⊤`. This follows from the previous theorem using general theory on approximation of Lᵖ functions
by continuous functions.
The theorem `orthonormal_fourier` states that the monomials `fourier_Lp 2 n` form an orthonormal
set (in the L² space of the circle).
The last two results together provide that the functions `fourier_Lp 2 n` form a Hilbert basis for
L²; this is named as `fourier_series`.
Parseval's identity, `tsum_sq_fourier_series_repr`, is a direct consequence of the construction of
this Hilbert basis.
-/
noncomputable theory
open_locale ennreal complex_conjugate classical
open topological_space continuous_map measure_theory measure_theory.measure algebra submodule set
/-! ### Choice of measure on the circle -/
section haar_circle
/-! We make the circle into a measure space, using the Haar measure normalized to have total
measure 1. -/
instance : measurable_space circle := borel circle
instance : borel_space circle := ⟨rfl⟩
/-- Haar measure on the circle, normalized to have total measure 1. -/
@[derive is_haar_measure]
def haar_circle : measure circle := haar_measure ⊤
instance : is_probability_measure haar_circle := ⟨haar_measure_self⟩
instance : measure_space circle :=
{ volume := haar_circle,
.. circle.measurable_space }
end haar_circle
/-! ### Monomials on the circle -/
section monomials
/-- The family of monomials `λ z, z ^ n`, parametrized by `n : ℤ` and considered as bundled
continuous maps from `circle` to `ℂ`. -/
@[simps] def fourier (n : ℤ) : C(circle, ℂ) :=
{ to_fun := λ z, z ^ n,
continuous_to_fun := continuous_subtype_coe.zpow₀ n $ λ z, or.inl (ne_zero_of_mem_circle z) }
@[simp] lemma fourier_zero {z : circle} : fourier 0 z = 1 := rfl
@[simp] lemma fourier_neg {n : ℤ} {z : circle} : fourier (-n) z = conj (fourier n z) :=
by simp [← coe_inv_circle_eq_conj z]
@[simp] lemma fourier_add {m n : ℤ} {z : circle} :
fourier (m + n) z = (fourier m z) * (fourier n z) :=
by simp [zpow_add₀ (ne_zero_of_mem_circle z)]
/-- The subalgebra of `C(circle, ℂ)` generated by `z ^ n` for `n ∈ ℤ`; equivalently, polynomials in
`z` and `conj z`. -/
def fourier_subalgebra : subalgebra ℂ C(circle, ℂ) := algebra.adjoin ℂ (range fourier)
/-- The subalgebra of `C(circle, ℂ)` generated by `z ^ n` for `n ∈ ℤ` is in fact the linear span of
these functions. -/
lemma fourier_subalgebra_coe : fourier_subalgebra.to_submodule = span ℂ (range fourier) :=
begin
apply adjoin_eq_span_of_subset,
refine subset.trans _ submodule.subset_span,
intros x hx,
apply submonoid.closure_induction hx (λ _, id) ⟨0, rfl⟩,
rintros _ _ ⟨m, rfl⟩ ⟨n, rfl⟩,
refine ⟨m + n, _⟩,
ext1 z,
exact fourier_add,
end
/-- The subalgebra of `C(circle, ℂ)` generated by `z ^ n` for `n ∈ ℤ` separates points. -/
lemma fourier_subalgebra_separates_points : fourier_subalgebra.separates_points :=
begin
intros x y hxy,
refine ⟨_, ⟨fourier 1, _, rfl⟩, _⟩,
{ exact subset_adjoin ⟨1, rfl⟩ },
{ simp [hxy] }
end
/-- The subalgebra of `C(circle, ℂ)` generated by `z ^ n` for `n ∈ ℤ` is invariant under complex
conjugation. -/
lemma fourier_subalgebra_conj_invariant :
conj_invariant_subalgebra (fourier_subalgebra.restrict_scalars ℝ) :=
begin
rintros _ ⟨f, hf, rfl⟩,
change _ ∈ fourier_subalgebra,
change _ ∈ fourier_subalgebra at hf,
apply adjoin_induction hf,
{ rintros _ ⟨n, rfl⟩,
suffices : fourier (-n) ∈ fourier_subalgebra,
{ convert this,
ext1,
simp },
exact subset_adjoin ⟨-n, rfl⟩ },
{ intros c,
exact fourier_subalgebra.algebra_map_mem (conj c) },
{ intros f g hf hg,
convert fourier_subalgebra.add_mem hf hg,
exact alg_hom.map_add _ f g, },
{ intros f g hf hg,
convert fourier_subalgebra.mul_mem hf hg,
exact alg_hom.map_mul _ f g, }
end
/-- The subalgebra of `C(circle, ℂ)` generated by `z ^ n` for `n ∈ ℤ` is dense. -/
lemma fourier_subalgebra_closure_eq_top : fourier_subalgebra.topological_closure = ⊤ :=
continuous_map.subalgebra_complex_topological_closure_eq_top_of_separates_points
fourier_subalgebra
fourier_subalgebra_separates_points
fourier_subalgebra_conj_invariant
/-- The linear span of the monomials `z ^ n` is dense in `C(circle, ℂ)`. -/
lemma span_fourier_closure_eq_top : (span ℂ (range fourier)).topological_closure = ⊤ :=
begin
rw ← fourier_subalgebra_coe,
exact congr_arg subalgebra.to_submodule fourier_subalgebra_closure_eq_top,
end
/-- The family of monomials `λ z, z ^ n`, parametrized by `n : ℤ` and considered as elements of
the `Lp` space of functions on `circle` taking values in `ℂ`. -/
abbreviation fourier_Lp (p : ℝ≥0∞) [fact (1 ≤ p)] (n : ℤ) : Lp ℂ p haar_circle :=
to_Lp p haar_circle ℂ (fourier n)
lemma coe_fn_fourier_Lp (p : ℝ≥0∞) [fact (1 ≤ p)] (n : ℤ) :
⇑(fourier_Lp p n) =ᵐ[haar_circle] fourier n :=
coe_fn_to_Lp haar_circle (fourier n)
/-- For each `1 ≤ p < ∞`, the linear span of the monomials `z ^ n` is dense in
`Lp ℂ p haar_circle`. -/
lemma span_fourier_Lp_closure_eq_top {p : ℝ≥0∞} [fact (1 ≤ p)] (hp : p ≠ ∞) :
(span ℂ (range (fourier_Lp p))).topological_closure = ⊤ :=
begin
convert (continuous_map.to_Lp_dense_range ℂ hp haar_circle ℂ).topological_closure_map_submodule
span_fourier_closure_eq_top,
rw [map_span, range_comp],
simp
end
/-- For `n ≠ 0`, a rotation by `n⁻¹ * real.pi` negates the monomial `z ^ n`. -/
lemma fourier_add_half_inv_index {n : ℤ} (hn : n ≠ 0) (z : circle) :
fourier n ((exp_map_circle (n⁻¹ * real.pi) * z)) = - fourier n z :=
begin
have : ↑n * ((↑n)⁻¹ * ↑real.pi * complex.I) = ↑real.pi * complex.I,
{ have : (n:ℂ) ≠ 0 := by exact_mod_cast hn,
field_simp,
ring },
simp [mul_zpow₀, ← complex.exp_int_mul, complex.exp_pi_mul_I, this]
end
/-- The monomials `z ^ n` are an orthonormal set with respect to Haar measure on the circle. -/
lemma orthonormal_fourier : orthonormal ℂ (fourier_Lp 2) :=
begin
rw orthonormal_iff_ite,
intros i j,
rw continuous_map.inner_to_Lp haar_circle (fourier i) (fourier j),
split_ifs,
{ simp [h, is_probability_measure.measure_univ, ← fourier_neg, ← fourier_add, -fourier_to_fun] },
simp only [← fourier_add, ← fourier_neg],
have hij : -i + j ≠ 0,
{ rw add_comm,
exact sub_ne_zero.mpr (ne.symm h) },
exact integral_eq_zero_of_mul_left_eq_neg (fourier_add_half_inv_index hij)
end
end monomials
section fourier
/-- We define `fourier_series` to be a `ℤ`-indexed Hilbert basis for `Lp ℂ 2 haar_circle`, which by
definition is an isometric isomorphism from `Lp ℂ 2 haar_circle` to `ℓ²(ℤ, ℂ)`. -/
def fourier_series : hilbert_basis ℤ ℂ (Lp ℂ 2 haar_circle) :=
hilbert_basis.mk orthonormal_fourier (span_fourier_Lp_closure_eq_top (by norm_num))
/-- The elements of the Hilbert basis `fourier_series` for `Lp ℂ 2 haar_circle` are the functions
`fourier_Lp 2`, the monomials `λ z, z ^ n` on the circle considered as elements of `L2`. -/
@[simp] lemma coe_fourier_series : ⇑fourier_series = fourier_Lp 2 := hilbert_basis.coe_mk _ _
/-- Under the isometric isomorphism `fourier_series` from `Lp ℂ 2 haar_circle` to `ℓ²(ℤ, ℂ)`, the
`i`-th coefficient is the integral over the circle of `λ t, t ^ (-i) * f t`. -/
lemma fourier_series_repr (f : Lp ℂ 2 haar_circle) (i : ℤ) :
fourier_series.repr f i = ∫ t : circle, t ^ (-i) * f t ∂ haar_circle :=
begin
transitivity ∫ t : circle, conj ((fourier_Lp 2 i : circle → ℂ) t) * f t ∂ haar_circle,
{ simp [fourier_series.repr_apply_apply f i, measure_theory.L2.inner_def] },
apply integral_congr_ae,
filter_upwards [coe_fn_fourier_Lp 2 i] with _ ht,
rw [ht, ← fourier_neg],
simp [-fourier_neg]
end
/-- The Fourier series of an `L2` function `f` sums to `f`, in the `L2` topology on the circle. -/
lemma has_sum_fourier_series (f : Lp ℂ 2 haar_circle) :
has_sum (λ i, fourier_series.repr f i • fourier_Lp 2 i) f :=
by simpa using hilbert_basis.has_sum_repr fourier_series f
/-- **Parseval's identity**: the sum of the squared norms of the Fourier coefficients equals the
`L2` norm of the function. -/
lemma tsum_sq_fourier_series_repr (f : Lp ℂ 2 haar_circle) :
∑' i : ℤ, ∥fourier_series.repr f i∥ ^ 2 = ∫ t : circle, ∥f t∥ ^ 2 ∂ haar_circle :=
begin
have H₁ : ∥fourier_series.repr f∥ ^ 2 = ∑' i, ∥fourier_series.repr f i∥ ^ 2,
{ exact_mod_cast lp.norm_rpow_eq_tsum _ (fourier_series.repr f),
norm_num },
have H₂ : ∥fourier_series.repr f∥ ^ 2 = ∥f∥ ^2 := by simp,
have H₃ := congr_arg is_R_or_C.re (@L2.inner_def circle ℂ ℂ _ _ _ _ f f),
rw ← integral_re at H₃,
{ simp only [← norm_sq_eq_inner] at H₃,
rw [← H₁, H₂],
exact H₃ },
{ exact L2.integrable_inner f f },
end
end fourier
|
65550043385831024ff9410dac7744931aebcf4e | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/instances/real_vector_space.lean | 6d9165706d00c9f9dfaefaffd704c67f7cbeb8eb | [] | 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,890 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.algebra.module
import Mathlib.topology.instances.real
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# Continuous additive maps are `ℝ`-linear
In this file we prove that a continuous map `f : E →+ F` between two topological vector spaces
over `ℝ` is `ℝ`-linear
-/
namespace add_monoid_hom
/-- A continuous additive map between two vector spaces over `ℝ` is `ℝ`-linear. -/
theorem map_real_smul {E : Type u_1} [add_comm_group E] [vector_space ℝ E] [topological_space E] [topological_vector_space ℝ E] {F : Type u_2} [add_comm_group F] [vector_space ℝ F] [topological_space F] [topological_vector_space ℝ F] [t2_space F] (f : E →+ F) (hf : continuous ⇑f) (c : ℝ) (x : E) : coe_fn f (c • x) = c • coe_fn f x := sorry
/-- Reinterpret a continuous additive homomorphism between two real vector spaces
as a continuous real-linear map. -/
def to_real_linear_map {E : Type u_1} [add_comm_group E] [vector_space ℝ E] [topological_space E] [topological_vector_space ℝ E] {F : Type u_2} [add_comm_group F] [vector_space ℝ F] [topological_space F] [topological_vector_space ℝ F] [t2_space F] (f : E →+ F) (hf : continuous ⇑f) : continuous_linear_map ℝ E F :=
continuous_linear_map.mk (linear_map.mk ⇑f sorry (map_real_smul f hf))
@[simp] theorem coe_to_real_linear_map {E : Type u_1} [add_comm_group E] [vector_space ℝ E] [topological_space E] [topological_vector_space ℝ E] {F : Type u_2} [add_comm_group F] [vector_space ℝ F] [topological_space F] [topological_vector_space ℝ F] [t2_space F] (f : E →+ F) (hf : continuous ⇑f) : ⇑(to_real_linear_map f hf) = ⇑f :=
rfl
|
28497c7a14f2b2c3d000b1bc60be268c59cb04bb | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/omega/lin_comb_auto.lean | 2776810bfbec658092db96591df033128c25f8d1 | [] | 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,513 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.omega.clause
import Mathlib.PostPort
namespace Mathlib
/-
Linear combination of constraints.
-/
namespace omega
/-- Linear combination of constraints. The second
argument is the list of constraints, and the first
argument is the list of conefficients by which the
constraints are multiplied -/
@[simp] def lin_comb : List ℕ → List term → term := sorry
theorem lin_comb_holds {v : ℕ → ℤ} {ts : List term} (ns : List ℕ) :
(∀ (t : term), t ∈ ts → 0 ≤ term.val v t) → 0 ≤ term.val v (lin_comb ns ts) :=
sorry
/-- `unsat_lin_comb ns ts` asserts that the linear combination
`lin_comb ns ts` is unsatisfiable -/
def unsat_lin_comb (ns : List ℕ) (ts : List term) :=
prod.fst (lin_comb ns ts) < 0 ∧ ∀ (x : ℤ), x ∈ prod.snd (lin_comb ns ts) → x = 0
theorem unsat_lin_comb_of (ns : List ℕ) (ts : List term) :
prod.fst (lin_comb ns ts) < 0 →
(∀ (x : ℤ), x ∈ prod.snd (lin_comb ns ts) → x = 0) → unsat_lin_comb ns ts :=
fun (h1 : prod.fst (lin_comb ns ts) < 0)
(h2 : ∀ (x : ℤ), x ∈ prod.snd (lin_comb ns ts) → x = 0) => { left := h1, right := h2 }
theorem unsat_of_unsat_lin_comb (ns : List ℕ) (ts : List term) :
unsat_lin_comb ns ts → clause.unsat ([], ts) :=
sorry
end Mathlib |
3e84815952e2db2337a51351a0ccd6232cef5e12 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/group_theory/subgroup.lean | a6dc60701425a33a931e7035ba0191aaf6f2dcca | [
"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 | 28,251 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro,
Michael Howes
-/
import group_theory.submonoid
open set function
variables {α : Type*} {β : Type*} {a a₁ a₂ b c: α}
section group
variables [group α] [add_group β]
@[to_additive]
lemma injective_mul {a : α} : injective ((*) a) :=
assume a₁ a₂ h,
have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h],
by rwa [inv_mul_self, one_mul, one_mul] at this
/-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/
class is_add_subgroup (s : set β) extends is_add_submonoid s : Prop :=
(neg_mem {a} : a ∈ s → -a ∈ s)
/-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/
@[to_additive is_add_subgroup]
class is_subgroup (s : set α) extends is_submonoid s : Prop :=
(inv_mem {a} : a ∈ s → a⁻¹ ∈ s)
instance additive.is_add_subgroup
(s : set α) [is_subgroup s] : @is_add_subgroup (additive α) _ s :=
⟨@is_subgroup.inv_mem _ _ _ _⟩
theorem additive.is_add_subgroup_iff
{s : set α} : @is_add_subgroup (additive α) _ s ↔ is_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk α _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by resetI; apply_instance⟩
instance multiplicative.is_subgroup
(s : set β) [is_add_subgroup s] : @is_subgroup (multiplicative β) _ s :=
⟨@is_add_subgroup.neg_mem _ _ _ _⟩
theorem multiplicative.is_subgroup_iff
{s : set β} : @is_subgroup (multiplicative β) _ s ↔ is_add_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk β _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by resetI; apply_instance⟩
@[to_additive add_group]
instance subtype.group {s : set α} [is_subgroup s] : group s :=
by subtype_instance
@[to_additive add_comm_group]
instance subtype.comm_group {α : Type*} [comm_group α] {s : set α} [is_subgroup s] : comm_group s :=
by subtype_instance
@[simp, to_additive]
lemma is_subgroup.coe_inv {s : set α} [is_subgroup s] (a : s) : ((a⁻¹ : s) : α) = a⁻¹ := rfl
@[simp] lemma is_subgroup.coe_gpow {s : set α} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : α) = a ^ n :=
by induction n; simp [is_submonoid.coe_pow a]
@[simp] lemma is_add_subgroup.gsmul_coe {β : Type*} [add_group β] {s : set β} [is_add_subgroup s] (a : s) (n : ℤ) :
((gsmul n a : s) : β) = gsmul n a :=
by induction n; simp [is_add_submonoid.smul_coe a]
attribute [to_additive gsmul_coe] is_subgroup.coe_gpow
@[to_additive of_add_neg]
theorem is_subgroup.of_div (s : set α)
(one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) :
is_subgroup s :=
have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from
assume a ha,
have 1 * a⁻¹ ∈ s, from div_mem one_mem ha,
by simpa,
{ inv_mem := inv_mem,
mul_mem := assume a b ha hb,
have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb),
by simpa,
one_mem := one_mem }
theorem is_add_subgroup.of_sub (s : set β)
(zero_mem : (0:β) ∈ s) (sub_mem : ∀{a b:β}, a ∈ s → b ∈ s → a - b ∈ s) :
is_add_subgroup s :=
is_add_subgroup.of_add_neg s zero_mem (λ x y hx hy, sub_mem hx hy)
@[to_additive]
instance is_subgroup.inter (s₁ s₂ : set α) [is_subgroup s₁] [is_subgroup s₂] :
is_subgroup (s₁ ∩ s₂) :=
{ inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩ }
@[to_additive]
instance is_subgroup.Inter {ι : Sort*} (s : ι → set α) [h : ∀ y : ι, is_subgroup (s y)] :
is_subgroup (set.Inter s) :=
{ inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (set.mem_Inter.1 h y) }
@[to_additive is_add_subgroup_Union_of_directed]
lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι]
(s : ι → set α) [∀ i, is_subgroup (s i)]
(directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) :
is_subgroup (⋃i, s i) :=
{ inv_mem := λ a ha,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩,
to_is_submonoid := is_submonoid_Union_of_directed s directed }
def gpowers (x : α) : set α := set.range ((^) x : ℤ → α)
def gmultiples (x : β) : set β := set.range (λ i, gsmul i x)
attribute [to_additive gmultiples] gpowers
instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) :=
{ one_mem := ⟨(0:ℤ), by simp⟩,
mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩,
inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ }
instance gmultiples.is_add_subgroup (x : β) : is_add_subgroup (gmultiples x) :=
multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _
attribute [to_additive is_add_subgroup] gpowers.is_subgroup
lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s
| (n : ℕ) := is_submonoid.pow_mem h
| -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h)
lemma is_add_subgroup.gsmul_mem {a : β} {s : set β} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s :=
@is_subgroup.gpow_mem (multiplicative β) _ _ _ _
lemma gpowers_subset {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := is_subgroup.gpow_mem h end
lemma gmultiples_subset {a : β} {s : set β} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s :=
@gpowers_subset (multiplicative β) _ _ _ _ h
attribute [to_additive gmultiples_subset] gpowers_subset
lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩
lemma mem_gmultiples {a : β} : a ∈ gmultiples a := ⟨1, by simp⟩
attribute [to_additive mem_gmultiples] mem_gpowers
end group
namespace is_subgroup
open is_submonoid
variables [group α] (s : set α) [is_subgroup s]
@[to_additive]
lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s :=
⟨λ h, by simpa using inv_mem h, inv_mem⟩
@[to_additive]
lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s :=
⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s :=
⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩
end is_subgroup
theorem is_add_subgroup.sub_mem {α} [add_group α] (s : set α) [is_add_subgroup s] (a b : α)
(ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s :=
is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb)
class normal_add_subgroup [add_group α] (s : set α) extends is_add_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : α, g + n - g ∈ s)
@[to_additive normal_add_subgroup]
class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s)
@[to_additive normal_add_subgroup_of_add_comm_group]
lemma normal_subgroup_of_comm_group [comm_group α] (s : set α) [hs : is_subgroup s] :
normal_subgroup s :=
{ normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul],
..hs }
instance additive.normal_add_subgroup [group α]
(s : set α) [normal_subgroup s] : @normal_add_subgroup (additive α) _ s :=
⟨@normal_subgroup.normal _ _ _ _⟩
theorem additive.normal_add_subgroup_iff [group α]
{s : set α} : @normal_add_subgroup (additive α) _ s ↔ normal_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@normal_subgroup.mk α _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂,
λ h, by resetI; apply_instance⟩
instance multiplicative.normal_subgroup [add_group α]
(s : set α) [normal_add_subgroup s] : @normal_subgroup (multiplicative α) _ s :=
⟨@normal_add_subgroup.normal _ _ _ _⟩
theorem multiplicative.normal_subgroup_iff [add_group α]
{s : set α} : @normal_subgroup (multiplicative α) _ s ↔ normal_add_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@normal_add_subgroup.mk α _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂,
λ h, by resetI; apply_instance⟩
namespace is_subgroup
variable [group α]
-- Normal subgroup properties
@[to_additive]
lemma mem_norm_comm {s : set α} [normal_subgroup s] {a b : α} (hab : a * b ∈ s) : b * a ∈ s :=
have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹,
by simp at h; exact h
@[to_additive]
lemma mem_norm_comm_iff {s : set α} [normal_subgroup s] {a b : α} : a * b ∈ s ↔ b * a ∈ s :=
⟨mem_norm_comm, mem_norm_comm⟩
/-- The trivial subgroup -/
@[to_additive]
def trivial (α : Type*) [group α] : set α := {1}
@[simp, to_additive]
lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 :=
mem_singleton_iff
@[to_additive]
instance trivial_normal : normal_subgroup (trivial α) :=
by refine {..}; simp [trivial] {contextual := tt}
@[to_additive]
lemma eq_trivial_iff {H : set α} [is_subgroup H] :
H = trivial α ↔ (∀ x ∈ H, x = (1 : α)) :=
by simp only [set.ext_iff, is_subgroup.mem_trivial];
exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem H⟩⟩
@[to_additive]
instance univ_subgroup : normal_subgroup (@univ α) :=
by refine {..}; simp
@[to_additive add_center]
def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g}
@[to_additive mem_add_center]
lemma mem_center {a : α} : a ∈ center α ↔ ∀g, g * a = a * g := iff.rfl
@[to_additive add_center_normal]
instance center_normal : normal_subgroup (center α) :=
{ one_mem := by simp [center],
mul_mem := assume a b ha hb g,
by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc],
inv_mem := assume a ha g,
calc
g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g]
... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp,
normal := assume n ha g h,
calc
h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc]
... = g * g⁻¹ * n * h : by rw ha h; simp
... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] }
@[to_additive add_normalizer]
def normalizer (s : set α) : set α :=
{g : α | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s}
@[to_additive normalizer_is_add_subgroup]
instance normalizer_is_subgroup (s : set α) [is_subgroup s] : is_subgroup (normalizer s) :=
{ one_mem := by simp [normalizer],
mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s)
(hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n,
by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb],
inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n,
by rw [ha (a⁻¹ * n * a⁻¹⁻¹)];
simp [mul_assoc] }
@[to_additive subset_add_normalizer]
lemma subset_normalizer (s : set α) [is_subgroup s] : s ⊆ normalizer s :=
λ g hg n, by rw [is_subgroup.mul_mem_cancel_left _ ((is_subgroup.inv_mem_iff _).2 hg),
is_subgroup.mul_mem_cancel_right _ hg]
/-- Every subgroup is a normal subgroup of its normalizer -/
@[to_additive add_normal_in_add_normalizer]
instance normal_in_normalizer (s : set α) [is_subgroup s] :
normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) :=
{ one_mem := show (1 : α) ∈ s, from is_submonoid.one_mem _,
mul_mem := λ a b ha hb, show (a * b : α) ∈ s, from is_submonoid.mul_mem ha hb,
inv_mem := λ a ha, show (a⁻¹ : α) ∈ s, from is_subgroup.inv_mem ha,
normal := λ a ha ⟨m, hm⟩, (hm a).1 ha }
end is_subgroup
-- Homomorphism subgroups
namespace is_group_hom
open is_submonoid is_subgroup
open is_mul_hom (map_mul)
variables [group α] [group β]
@[to_additive]
def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β)
@[to_additive]
lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 :=
mem_trivial
@[to_additive]
lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b :=
begin
rw [map_mul f, map_inv f] at h,
rw [←inv_inv (f b), eq_inv_of_mul_eq_one h]
end
@[to_additive]
lemma one_ker_inv' (f : α → β) [is_group_hom f] {a b : α} (h : f (a⁻¹ * b) = 1) : f a = f b :=
begin
rw [map_mul f, map_inv f] at h,
apply eq_of_inv_eq_inv,
rw eq_inv_of_mul_eq_one h
end
@[to_additive]
lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 :=
have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv],
by rwa [←map_inv f, ←map_mul f] at this
@[to_additive]
lemma inv_ker_one' (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a⁻¹ * b) = 1 :=
have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv],
by rwa [←map_inv f, ←map_mul f] at this
@[to_additive]
lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 :=
⟨inv_ker_one f, one_ker_inv f⟩
@[to_additive]
lemma one_iff_ker_inv' (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a⁻¹ * b) = 1 :=
⟨inv_ker_one' f, one_ker_inv' f⟩
@[to_additive]
lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv _ _ _
@[to_additive]
lemma inv_iff_ker' (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a⁻¹ * b ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv' _ _ _
@[to_additive image_add_subgroup]
instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] :
is_subgroup (f '' s) :=
{ mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩,
⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩,
one_mem := ⟨1, one_mem s, map_one f⟩,
inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ }
@[to_additive range_add_subgroup]
instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) :=
@set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ
local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal
@[to_additive]
instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] :
is_subgroup (f ⁻¹' s) :=
by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem β _ s] {contextual:=tt}
@[to_additive]
instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] :
normal_subgroup (f ⁻¹' s) :=
⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩
@[to_additive]
instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) :=
is_group_hom.preimage_normal f (trivial β)
@[to_additive]
lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) :
function.injective f :=
begin
intros a₁ a₂ hfa,
simp [ext_iff, ker, is_subgroup.trivial] at h,
have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa,
rw [eq_inv_of_mul_eq_one ha, inv_inv a₂]
end
@[to_additive]
lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) :
ker f = trivial α :=
set.ext $ assume x, iff.intro
(assume hx,
suffices f x = f 1, by simpa using h this,
by simp [map_one f]; rwa [mem_ker] at hx)
(by simp [mem_ker, is_group_hom.map_one f] {contextual := tt})
@[to_additive]
lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] :
function.injective f ↔ ker f = trivial α :=
⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩
@[to_additive]
lemma trivial_ker_iff_eq_one (f : α → β) [is_group_hom f] :
ker f = trivial α ↔ ∀ x, f x = 1 → x = 1 :=
by rw set.ext_iff; simp [ker]; exact
⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩
end is_group_hom
@[to_additive is_add_group_hom]
instance subtype_val.is_group_hom [group α] {s : set α} [is_subgroup s] :
is_group_hom (subtype.val : s → α) := { ..subtype_val.is_monoid_hom }
@[to_additive is_add_group_hom]
instance coe.is_group_hom [group α] {s : set α} [is_subgroup s] :
is_group_hom (coe : s → α) := { ..subtype_val.is_monoid_hom }
@[to_additive is_add_group_hom]
instance subtype_mk.is_group_hom [group α] [group β] {s : set α}
[is_subgroup s] (f : β → α) [is_group_hom f] (h : ∀ x, f x ∈ s) :
is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h }
@[to_additive is_add_group_hom]
instance set_inclusion.is_group_hom [group α] {s t : set α}
[is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) :=
subtype_mk.is_group_hom _ _
namespace add_group
variables [add_group α]
inductive in_closure (s : set α) : α → Prop
| basic {a : α} : a ∈ s → in_closure a
| zero : in_closure 0
| neg {a : α} : in_closure a → in_closure (-a)
| add {a b : α} : in_closure a → in_closure b → in_closure (a + b)
end add_group
namespace group
open is_submonoid is_subgroup
variables [group α] {s : set α}
@[to_additive]
inductive in_closure (s : set α) : α → Prop
| basic {a : α} : a ∈ s → in_closure a
| one : in_closure 1
| inv {a : α} : in_closure a → in_closure a⁻¹
| mul {a b : α} : in_closure a → in_closure b → in_closure (a * b)
/-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/
@[to_additive]
def closure (s : set α) : set α := {a | in_closure s a }
@[to_additive]
lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic
@[to_additive is_add_subgroup]
instance closure.is_subgroup (s : set α) : is_subgroup (closure s) :=
{ one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv }
@[to_additive]
theorem subset_closure {s : set α} : s ⊆ closure s := λ a, mem_closure
@[to_additive]
theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t :=
assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff]
@[to_additive]
lemma closure_subset_iff (s t : set α) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t :=
⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩
@[to_additive]
theorem closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_subset $ set.subset.trans h subset_closure
@[simp, to_additive closure_add_subgroup]
lemma closure_subgroup (s : set α) [is_subgroup s] : closure s = s :=
set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure
@[to_additive]
theorem exists_list_of_mem_closure {s : set α} {a : α} (h : a ∈ closure s) :
(∃l:list α, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) :=
in_closure.rec_on h
(λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩)
⟨[], list.forall_mem_nil _, rfl⟩
(λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv,
λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in
hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm,
HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih,
by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton,
list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩)
(λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩,
by rw [list.prod_append, HL2, HL4]⟩)
@[to_additive]
lemma image_closure [group β] (f : α → β) [is_group_hom f] (s : set α) :
f '' closure s = closure (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [subset_closure, set.mem_image_of_mem] },
{ rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem },
{ rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption },
{ rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] }
end
(closure_subset $ set.image_subset _ subset_closure)
@[to_additive]
theorem mclosure_subset {s : set α} : monoid.closure s ⊆ closure s :=
monoid.closure_subset $ subset_closure
@[to_additive]
theorem mclosure_inv_subset {s : set α} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s :=
monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)
@[to_additive]
theorem closure_eq_mclosure {s : set α} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) :=
set.subset.antisymm
(@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s))
{ inv_mem := λ x hx, monoid.in_closure.rec_on hx
(λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $ show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx)
(λ hx, monoid.subset_closure $ or.inl hx))
((@one_inv α _).symm ▸ is_submonoid.one_mem _)
(λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) }
(set.subset.trans (set.subset_union_left _ _) monoid.subset_closure))
(monoid.closure_subset $ set.union_subset subset_closure $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx))
@[to_additive]
theorem mem_closure_union_iff {α : Type*} [comm_group α] {s t : set α} {x : α} :
x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x :=
begin
simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split,
{ rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm zs], refl },
{ rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm yt], refl }
end
@[to_additive gmultiples_eq_closure]
theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} :=
subset.antisymm
(gpowers_subset $ mem_closure $ by simp)
(closure_subset $ by simp [mem_gpowers])
end group
namespace is_subgroup
variable [group α]
@[to_additive]
lemma trivial_eq_closure : trivial α = group.closure ∅ :=
subset.antisymm
(by simp [set.subset_def, is_submonoid.one_mem])
(group.closure_subset $ by simp)
end is_subgroup
/-The normal closure of a set s is the subgroup closure of all the conjugates of
elements of s. It is the smallest normal subgroup containing s. -/
namespace group
variables {s : set α} [group α]
/-- Given an element a, conjugates a is the set of conjugates. -/
def conjugates (a : α) : set α := {b | is_conj a b}
lemma mem_conjugates_self {a : α} : a ∈ conjugates a := is_conj_refl _
/-- 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 α) : set α := ⋃ a ∈ s, conjugates a
lemma mem_conjugates_of_set_iff {x : α} : x ∈ conjugates_of_set s ↔ ∃ a : α, a ∈ s ∧ is_conj a x :=
set.mem_bUnion_iff
theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s :=
λ (x : α) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩
theorem conjugates_of_set_mono {s t : set α} (h : s ⊆ t) :
conjugates_of_set s ⊆ conjugates_of_set t :=
set.bUnion_subset_bUnion_left h
lemma conjugates_subset {t : set α} [normal_subgroup t] {a : α} (h : a ∈ t) : conjugates a ⊆ t :=
λ x ⟨c,w⟩,
begin
have H := normal_subgroup.normal a h c,
rwa ←w,
end
theorem conjugates_of_set_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) :
conjugates_of_set s ⊆ t :=
set.bUnion_subset (λ x H, conjugates_subset (h H))
/-- The set of conjugates of s is closed under conjugation. -/
lemma conj_mem_conjugates_of_set {x c : α} :
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₂ ⟨c,rfl⟩⟩,
end
/-- 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 α) : set α := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s :=
subset_closure
theorem subset_normal_closure : s ⊆ normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
/-- The normal closure of a set is a subgroup. -/
instance normal_closure.is_subgroup (s : set α) : is_subgroup (normal_closure s) :=
closure.is_subgroup (conjugates_of_set s)
/-- The normal closure of s is a normal subgroup. -/
instance normal_closure.is_normal : normal_subgroup (normal_closure s) :=
⟨ λ n h g,
begin
induction h with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))},
{simpa using (normal_closure.is_subgroup s).one_mem},
{rw ←conj_inv,
exact (is_subgroup.inv_mem ihx)},
{rw ←conj_mul,
exact (is_submonoid.mul_mem ihx ihy)},
end ⟩
/-- The normal closure of s is the smallest normal subgroup containing s. -/
theorem normal_closure_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) :
normal_closure s ⊆ t :=
λ a w,
begin
induction w with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset h $ hx)},
{exact is_submonoid.one_mem t},
{exact is_subgroup.inv_mem ihx},
{exact is_submonoid.mul_mem ihx ihy}
end
lemma normal_closure_subset_iff {s t : set α} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t :=
⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set α} : s ⊆ t → normal_closure s ⊆ normal_closure t :=
λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure))
end group
section simple_group
class simple_group (α : Type*) [group α] : Prop :=
(simple : ∀ (N : set α) [normal_subgroup N], N = is_subgroup.trivial α ∨ N = set.univ)
class simple_add_group (α : Type*) [add_group α] : Prop :=
(simple : ∀ (N : set α) [normal_add_subgroup N], N = is_add_subgroup.trivial α ∨ N = set.univ)
attribute [to_additive simple_add_group] simple_group
theorem additive.simple_add_group_iff [group α] :
simple_add_group (additive α) ↔ simple_group α :=
⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩,
λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩
instance additive.simple_add_group [group α] [simple_group α] :
simple_add_group (additive α) := additive.simple_add_group_iff.2 (by apply_instance)
theorem multiplicative.simple_group_iff [add_group α] :
simple_group (multiplicative α) ↔ simple_add_group α :=
⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩,
λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩
instance multiplicative.simple_group [add_group α] [simple_add_group α] :
simple_group (multiplicative α) := multiplicative.simple_group_iff.2 (by apply_instance)
lemma simple_group_of_surjective [group α] [group β] [simple_group α] (f : α → β)
[is_group_hom f] (hf : function.surjective f) : simple_group β :=
⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance,
begin
resetI,
cases simple_group.simple (f ⁻¹' H) with h h,
{ refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)),
cases hf x with y hy,
rw ← hy at hx,
rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.map_one f] },
{ refine or.inr (set.eq_univ_of_forall (λ x, _)),
cases hf x with y hy,
rw set.eq_univ_iff_forall at h,
rw ← hy,
exact h y }
end⟩
lemma simple_add_group_of_surjective [add_group α] [add_group β] [simple_add_group α] (f : α → β)
[is_add_group_hom f] (hf : function.surjective f) : simple_add_group β :=
multiplicative.simple_group_iff.1 (@simple_group_of_surjective (multiplicative α) (multiplicative β) _ _ _ f _ hf)
attribute [to_additive simple_add_group_of_surjective] simple_group_of_surjective
end simple_group
|
991ed1158eb7e7870904df54db733fa7bf0cbb90 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/sigma.lean | 4cf25c0f9a2fa66b8fb1e1577d1b2e50571b3617 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,022 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
Sigma types, aka dependent sum.
-/
import logic.cast
open inhabited eq.ops sigma.ops
namespace sigma
universe variables u v
variables {A A' : Type.{u}} {B : A → Type.{v}} {B' : A' → Type.{v}}
definition unpack {C : (Σa, B a) → Type} {u : Σa, B a} (H : C ⟨u.1 , u.2⟩) : C u :=
destruct u (λx y H, H) H
theorem dpair_heq {a : A} {a' : A'} {b : B a} {b' : B' a'}
(HB : B == B') (Ha : a == a') (Hb : b == b') : ⟨a, b⟩ == ⟨a', b'⟩ :=
hcongr_arg4 @mk (heq.type_eq Ha) HB Ha Hb
protected theorem heq {p : Σa : A, B a} {p' : Σa' : A', B' a'} (HB : B == B') :
∀(H₁ : p.1 == p'.1) (H₂ : p.2 == p'.2), p == p' :=
destruct p (take a₁ b₁, destruct p' (take a₂ b₂ H₁ H₂, dpair_heq HB H₁ H₂))
protected definition is_inhabited [instance] [H₁ : inhabited A] [H₂ : inhabited (B (default A))] :
inhabited (sigma B) :=
inhabited.destruct H₁ (λa, inhabited.destruct H₂ (λb, inhabited.mk ⟨default A, b⟩))
theorem eq_rec_dpair_commute {C : Πa, B a → Type} {a a' : A} (H : a = a') (b : B a) (c : C a b) :
eq.rec_on H ⟨b, c⟩ = ⟨eq.rec_on H b, eq.rec_on (dcongr_arg2 C H rfl) c⟩ :=
eq.drec_on H (dpair_eq rfl (!eq.rec_on_id⁻¹))
variables {C : Πa, B a → Type} {D : Πa b, C a b → Type}
definition dtrip (a : A) (b : B a) (c : C a b) := ⟨a, b, c⟩
definition dquad (a : A) (b : B a) (c : C a b) (d : D a b c) := ⟨a, b, c, d⟩
definition pr1' [reducible] (x : Σ a, B a) := x.1
definition pr2' [reducible] (x : Σ a b, C a b) := x.2.1
definition pr3 [reducible] (x : Σ a b, C a b) := x.2.2
definition pr3' [reducible] (x : Σ a b c, D a b c) := x.2.2.1
definition pr4 [reducible] (x : Σ a b c, D a b c) := x.2.2.2
theorem dtrip_eq {a₁ a₂ : A} {b₁ : B a₁} {b₂ : B a₂} {c₁ : C a₁ b₁} {c₂ : C a₂ b₂}
(H₁ : a₁ = a₂) (H₂ : eq.rec_on H₁ b₁ = b₂) (H₃ : cast (dcongr_arg2 C H₁ H₂) c₁ = c₂) :
⟨a₁, b₁, c₁⟩ = ⟨a₂, b₂, c₂⟩ :=
dcongr_arg3 dtrip H₁ H₂ H₃
theorem ndtrip_eq {A B : Type} {C : A → B → Type} {a₁ a₂ : A} {b₁ b₂ : B}
{c₁ : C a₁ b₁} {c₂ : C a₂ b₂} (H₁ : a₁ = a₂) (H₂ : b₁ = b₂)
(H₃ : cast (congr_arg2 C H₁ H₂) c₁ = c₂) : ⟨a₁, b₁, c₁⟩ = ⟨a₂, b₂, c₂⟩ :=
hdcongr_arg3 dtrip H₁ (heq.of_eq H₂) H₃
theorem ndtrip_equal {A B : Type} {C : A → B → Type} {p₁ p₂ : Σa b, C a b} :
∀(H₁ : pr1 p₁ = pr1 p₂) (H₂ : pr2' p₁ = pr2' p₂)
(H₃ : eq.rec_on (congr_arg2 C H₁ H₂) (pr3 p₁) = pr3 p₂), p₁ = p₂ :=
destruct p₁ (take a₁ q₁, destruct q₁ (take b₁ c₁, destruct p₂ (take a₂ q₂, destruct q₂
(take b₂ c₂ H₁ H₂ H₃, ndtrip_eq H₁ H₂ H₃))))
end sigma
|
5e9d04ab62c3fc842a61e4b6bcc6a61159a2aaa5 | 020c82b947b28c4d255384a0466715fbb44e5c1e | /src/fifteentactics.lean | 31938a5ada947c6816757ced2116ef73748cce0c | [] | no_license | SnobbyDragon/leanfifteen | 79ceb751749fa0185c4f9e60ffa2f128e64fbc3c | 4583ab44e1de89a25e693e5e611472a9ba1147b6 | refs/heads/main | 1,675,887,746,364 | 1,609,534,902,000 | 1,609,534,902,000 | 325,708,959 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,063 | lean | import fifteen
open fifteen fifteen.tile fifteen.position
open tactic tactic.interactive («have»)
namespace fifteentactics
-- just unfolds game
meta def start_game : tactic unit :=
do dunfold_target [``game] <|> tactic.fail "not a game !"
meta def get_start_position : tactic position :=
do { start_game,
`(can_slide_to %%p₁ %%p₂) ← tactic.target,
p ← (eval_expr position) p₁,
return p
} <|> tactic.fail "failed to get current position"
meta def get_position : tactic position :=
do { `(can_slide_to %%p₁ %%p₂) ← tactic.target,
p ← (eval_expr position) p₁,
return p
} <|> get_start_position
-- Thanks to Mario Carneiro :)
meta def finish_game : tactic unit :=
do { applyc ``can_slide_to.of_eq,
tactic.exact_dec_trivial
} <|> tactic.fail "we are not done !"
-- given a list of tiles, finds the hole
meta def find_hole (p : position) : list tile → tactic tile
| [] := tactic.fail "failed to find the hole !"
| (t :: lt) := if hole t p then return t else find_hole lt
-- gets the adjacent hole
meta def get_adj_hole (t : tile) (p : position) : tactic tile :=
do { let adj := get_adjacent t,
h ← find_hole p adj,
return h
} <|> tactic.fail "no adjacent hole !"
-- gets location of the hole
meta def get_hole (p : position) : tactic tile :=
do { let tiles := tiles_list,
h ← find_hole p tiles,
return h
} <|> tactic.fail "apparently there's no hole"
-- gets tiles adjacent to the hole
meta def get_hole_adj (p : position) : tactic (list tile) :=
do { h ← get_hole p,
return $ get_adjacent h
} <|> tactic.fail "apparently there's no hole"
meta def slide_tile (t : tile) : tactic unit :=
do { p ← get_position,
`[apply slide_one_step],
h ← get_adj_hole t p,
-- tactic.trace $ "Want to slide " ++ to_string t ++ " to " ++ to_string h,
-- tactic.target >>= tactic.trace,
tactic.interactive.use [pexpr.of_expr (reflect t), pexpr.of_expr (reflect h)],
`[split, split; dec_trivial]
} <|> tactic.fail "failed to slide tile :("
end fifteentactics |
00ce9efb2cfe03f20451de216796ece39a57282c | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/measure_theory/measurable_space.lean | b7c4d0ab4c33a3be6837c66746f59bdec47da3ee | [
"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 | 62,044 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import algebra.indicator_function
import data.prod.tprod
import group_theory.coset
import logic.equiv.fin
import measure_theory.measurable_space_def
import measure_theory.tactic
import order.filter.lift
/-!
# Measurable spaces and measurable functions
This file provides properties of measurable spaces and the functions and isomorphisms
between them. The definition of a measurable space is in `measure_theory.measurable_space_def`.
A measurable space is a set equipped with a σ-algebra, a collection of
subsets closed under complementation and countable union. A function
between measurable spaces is measurable if the preimage of each
measurable subset is measurable.
σ-algebras on a fixed set `α` form a complete lattice. Here we order
σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is
also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any
collection of subsets of `α` generates a smallest σ-algebra which
contains all of them. A function `f : α → β` induces a Galois connection
between the lattices of σ-algebras on `α` and `β`.
A measurable equivalence between measurable spaces is an equivalence
which respects the σ-algebras, that is, for which both directions of
the equivalence are measurable functions.
We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable
set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`.
## Notation
* We write `α ≃ᵐ β` for measurable equivalences between the measurable spaces `α` and `β`.
This should not be confused with `≃ₘ` which is used for diffeomorphisms between manifolds.
## Implementation notes
Measurability of a function `f : α → β` between measurable spaces is
defined in terms of the Galois connection induced by f.
## References
* <https://en.wikipedia.org/wiki/Measurable_space>
* <https://en.wikipedia.org/wiki/Sigma-algebra>
* <https://en.wikipedia.org/wiki/Dynkin_system>
## Tags
measurable space, σ-algebra, measurable function, measurable equivalence, dynkin system,
π-λ theorem, π-system
-/
open set encodable function equiv
open_locale filter measure_theory
variables {α β γ δ δ' : Type*} {ι : Sort*} {s t u : set α}
namespace measurable_space
section functors
variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α}
/-- The forward image of a measurable space under a function. `map f m` contains the sets
`s : set β` whose preimage under `f` is measurable. -/
protected def map (f : α → β) (m : measurable_space α) : measurable_space β :=
{ measurable_set' := λ s, measurable_set[m] $ f ⁻¹' s,
measurable_set_empty := m.measurable_set_empty,
measurable_set_compl := assume s hs, m.measurable_set_compl _ hs,
measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }}
@[simp] lemma map_id : m.map id = m :=
measurable_space.ext $ assume s, iff.rfl
@[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) :=
measurable_space.ext $ assume s, iff.rfl
/-- The reverse image of a measurable space under a function. `comap f m` contains the sets
`s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/
protected def comap (f : α → β) (m : measurable_space β) : measurable_space α :=
{ measurable_set' := λ s, ∃s', measurable_set[m] s' ∧ f ⁻¹' s' = s,
measurable_set_empty := ⟨∅, m.measurable_set_empty, rfl⟩,
measurable_set_compl := assume s ⟨s', h₁, h₂⟩, ⟨s'ᶜ, m.measurable_set_compl _ h₁, h₂ ▸ rfl⟩,
measurable_set_Union := assume s hs,
let ⟨s', hs'⟩ := classical.axiom_of_choice hs in
⟨⋃ i, s' i, m.measurable_set_Union _ (λ i, (hs' i).left), by simp [hs'] ⟩ }
lemma comap_eq_generate_from (m : measurable_space β) (f : α → β) :
m.comap f = generate_from {t | ∃ s, measurable_set s ∧ f ⁻¹' s = t} :=
by convert generate_from_measurable_set.symm
@[simp] lemma comap_id : m.comap id = m :=
measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩
@[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) :=
measurable_space.ext $ assume s,
⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩
lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f :=
⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩
lemma gc_comap_map (f : α → β) :
galois_connection (measurable_space.comap f) (measurable_space.map f) :=
assume f g, comap_le_iff_le_map
lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h
lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h
lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h
lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h
@[simp] lemma comap_bot : (⊥ : measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot
@[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup
@[simp] lemma comap_supr {m : ι → measurable_space α} : (⨆i, m i).comap g = (⨆i, (m i).comap g) :=
(gc_comap_map g).l_supr
@[simp] lemma map_top : (⊤ : measurable_space α).map f = ⊤ := (gc_comap_map f).u_top
@[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf
@[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) :=
(gc_comap_map f).u_infi
lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _
lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _
end functors
lemma comap_generate_from {f : α → β} {s : set (set β)} :
(generate_from s).comap f = generate_from (preimage f '' s) :=
le_antisymm
(comap_le_iff_le_map.2 $ generate_from_le $ assume t hts,
generate_measurable.basic _ $ mem_image_of_mem _ $ hts)
(generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩)
end measurable_space
section measurable_functions
open measurable_space
lemma measurable_iff_le_map {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} :
measurable f ↔ m₂ ≤ m₁.map f :=
iff.rfl
alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map
lemma measurable_iff_comap_le {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} :
measurable f ↔ m₂.comap f ≤ m₁ :=
comap_le_iff_le_map.symm
alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le
lemma measurable.mono {ma ma' : measurable_space α} {mb mb' : measurable_space β} {f : α → β}
(hf : @measurable α β ma mb f) (ha : ma ≤ ma') (hb : mb' ≤ mb) :
@measurable α β ma' mb' f :=
λ t ht, ha _ $ hf $ hb _ ht
@[measurability]
lemma measurable_from_top [measurable_space β] {f : α → β} : measurable[⊤] f :=
λ s hs, trivial
lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β}
(h : ∀ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f :=
measurable.of_le_map $ generate_from_le h
variables {f g : α → β}
section typeclass_measurable_space
variables [measurable_space α] [measurable_space β] [measurable_space γ]
@[nontriviality, measurability]
lemma subsingleton.measurable [subsingleton α] : measurable f :=
λ s hs, @subsingleton.measurable_set α _ _ _
@[nontriviality, measurability]
lemma measurable_of_subsingleton_codomain [subsingleton β] (f : α → β) :
measurable f :=
λ s hs, subsingleton.set_cases measurable_set.empty measurable_set.univ s
@[to_additive]
lemma measurable_one [has_one α] : measurable (1 : β → α) := @measurable_const _ _ _ _ 1
lemma measurable_of_empty [is_empty α] (f : α → β) : measurable f :=
subsingleton.measurable
lemma measurable_of_empty_codomain [is_empty β] (f : α → β) : measurable f :=
by { haveI := function.is_empty f, exact measurable_of_empty f }
/-- A version of `measurable_const` that assumes `f x = f y` for all `x, y`. This version works
for functions between empty types. -/
lemma measurable_const' {f : β → α} (hf : ∀ x y, f x = f y) : measurable f :=
begin
casesI is_empty_or_nonempty β,
{ exact measurable_of_empty f },
{ convert measurable_const, exact funext (λ x, hf x h.some) }
end
lemma measurable_of_finite [finite α] [measurable_singleton_class α] (f : α → β) : measurable f :=
λ s hs, (f ⁻¹' s).to_finite.measurable_set
end typeclass_measurable_space
variables {m : measurable_space α}
include m
@[measurability] lemma measurable.iterate {f : α → α} (hf : measurable f) : ∀ n, measurable (f^[n])
| 0 := measurable_id
| (n+1) := (measurable.iterate n).comp hf
variables {mβ : measurable_space β}
include mβ
@[measurability]
lemma measurable_set_preimage {t : set β} (hf : measurable f) (ht : measurable_set t) :
measurable_set (f ⁻¹' t) :=
hf ht
@[measurability]
lemma measurable.piecewise {_ : decidable_pred (∈ s)} (hs : measurable_set s)
(hf : measurable f) (hg : measurable g) :
measurable (piecewise s f g) :=
begin
intros t ht,
rw piecewise_preimage,
exact hs.ite (hf ht) (hg ht)
end
/-- this is slightly different from `measurable.piecewise`. It can be used to show
`measurable (ite (x=0) 0 1)` by
`exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`,
but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/
lemma measurable.ite {p : α → Prop} {_ : decidable_pred p}
(hp : measurable_set {a : α | p a}) (hf : measurable f) (hg : measurable g) :
measurable (λ x, ite (p x) (f x) (g x)) :=
measurable.piecewise hp hf hg
@[measurability]
lemma measurable.indicator [has_zero β] (hf : measurable f) (hs : measurable_set s) :
measurable (s.indicator f) :=
hf.piecewise hs measurable_const
@[measurability, to_additive] lemma measurable_set_mul_support [has_one β]
[measurable_singleton_class β] (hf : measurable f) :
measurable_set (mul_support f) :=
hf (measurable_set_singleton 1).compl
/-- If a function coincides with a measurable function outside of a countable set, it is
measurable. -/
lemma measurable.measurable_of_countable_ne [measurable_singleton_class α]
(hf : measurable f) (h : set.countable {x | f x ≠ g x}) : measurable g :=
begin
assume t ht,
have : g ⁻¹' t = (g ⁻¹' t ∩ {x | f x = g x}ᶜ) ∪ (g ⁻¹' t ∩ {x | f x = g x}),
by simp [← inter_union_distrib_left],
rw this,
apply measurable_set.union (h.mono (inter_subset_right _ _)).measurable_set,
have : g ⁻¹' t ∩ {x : α | f x = g x} = f ⁻¹' t ∩ {x : α | f x = g x},
by { ext x, simp {contextual := tt} },
rw this,
exact (hf ht).inter h.measurable_set.of_compl,
end
end measurable_functions
section constructions
instance : measurable_space empty := ⊤
instance : measurable_space punit := ⊤ -- this also works for `unit`
instance : measurable_space bool := ⊤
instance : measurable_space ℕ := ⊤
instance : measurable_space ℤ := ⊤
instance : measurable_space ℚ := ⊤
instance : measurable_singleton_class empty := ⟨λ _, trivial⟩
instance : measurable_singleton_class punit := ⟨λ _, trivial⟩
instance : measurable_singleton_class bool := ⟨λ _, trivial⟩
instance : measurable_singleton_class ℕ := ⟨λ _, trivial⟩
instance : measurable_singleton_class ℤ := ⟨λ _, trivial⟩
instance : measurable_singleton_class ℚ := ⟨λ _, trivial⟩
lemma measurable_to_countable [measurable_space α] [countable α] [measurable_space β] {f : β → α}
(h : ∀ y, measurable_set (f ⁻¹' {f y})) :
measurable f :=
begin
assume s hs,
rw [← bUnion_preimage_singleton],
refine measurable_set.Union (λ y, measurable_set.Union $ λ hy, _),
by_cases hyf : y ∈ range f,
{ rcases hyf with ⟨y, rfl⟩,
apply h },
{ simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] }
end
@[measurability] lemma measurable_unit [measurable_space α] (f : unit → α) : measurable f :=
measurable_from_top
section nat
variables [measurable_space α]
@[measurability] lemma measurable_from_nat {f : ℕ → α} : measurable f :=
measurable_from_top
lemma measurable_to_nat {f : α → ℕ} : (∀ y, measurable_set (f ⁻¹' {f y})) → measurable f :=
measurable_to_countable
lemma measurable_find_greatest' {p : α → ℕ → Prop} [∀ x, decidable_pred (p x)]
{N : ℕ} (hN : ∀ k ≤ N, measurable_set {x | nat.find_greatest (p x) N = k}) :
measurable (λ x, nat.find_greatest (p x) N) :=
measurable_to_nat $ λ x, hN _ N.find_greatest_le
lemma measurable_find_greatest {p : α → ℕ → Prop} [∀ x, decidable_pred (p x)]
{N} (hN : ∀ k ≤ N, measurable_set {x | p x k}) :
measurable (λ x, nat.find_greatest (p x) N) :=
begin
refine measurable_find_greatest' (λ k hk, _),
simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of],
repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter,
measurable_set.compl, hN]; try { intros } }
end
lemma measurable_find {p : α → ℕ → Prop} [∀ x, decidable_pred (p x)]
(hp : ∀ x, ∃ N, p x N) (hm : ∀ k, measurable_set {x | p x k}) :
measurable (λ x, nat.find (hp x)) :=
begin
refine measurable_to_nat (λ x, _),
rw [preimage_find_eq_disjointed],
exact measurable_set.disjointed hm _
end
end nat
section quotient
variables [measurable_space α] [measurable_space β]
instance {α} {r : α → α → Prop} [m : measurable_space α] : measurable_space (quot r) :=
m.map (quot.mk r)
instance {α} {s : setoid α} [m : measurable_space α] : measurable_space (quotient s) :=
m.map quotient.mk'
@[to_additive]
instance _root_.quotient_group.measurable_space {G} [group G] [measurable_space G]
(S : subgroup G) : measurable_space (G ⧸ S) :=
quotient.measurable_space
lemma measurable_set_quotient {s : setoid α} {t : set (quotient s)} :
measurable_set t ↔ measurable_set (quotient.mk' ⁻¹' t) :=
iff.rfl
lemma measurable_from_quotient {s : setoid α} {f : quotient s → β} :
measurable f ↔ measurable (f ∘ quotient.mk') :=
iff.rfl
@[measurability] lemma measurable_quotient_mk [s : setoid α] :
measurable (quotient.mk : α → quotient s) :=
λ s, id
@[measurability] lemma measurable_quotient_mk' {s : setoid α} :
measurable (quotient.mk' : α → quotient s) :=
λ s, id
@[measurability] lemma measurable_quot_mk {r : α → α → Prop} :
measurable (quot.mk r) :=
λ s, id
@[to_additive] lemma quotient_group.measurable_coe {G} [group G] [measurable_space G]
{S : subgroup G} : measurable (coe : G → G ⧸ S) :=
measurable_quotient_mk'
attribute [measurability] quotient_group.measurable_coe quotient_add_group.measurable_coe
@[to_additive] lemma quotient_group.measurable_from_quotient {G} [group G] [measurable_space G]
{S : subgroup G} {f : G ⧸ S → α} :
measurable f ↔ measurable (f ∘ (coe : G → G ⧸ S)) :=
measurable_from_quotient
end quotient
section subtype
instance {α} {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) :=
m.comap (coe : _ → α)
section
variables [measurable_space α]
@[measurability] lemma measurable_subtype_coe {p : α → Prop} : measurable (coe : subtype p → α) :=
measurable_space.le_map_comap
instance {p : α → Prop} [measurable_singleton_class α] : measurable_singleton_class (subtype p) :=
{ measurable_set_singleton := λ x,
begin
have : measurable_set {(x : α)} := measurable_set_singleton _,
convert @measurable_subtype_coe α _ p _ this,
ext y,
simp [subtype.ext_iff],
end }
end
variables {m : measurable_space α} {mβ : measurable_space β}
include m
lemma measurable_set.subtype_image {s : set α} {t : set s}
(hs : measurable_set s) : measurable_set t → measurable_set ((coe : s → α) '' t)
| ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ :=
begin
rw [← eq, subtype.image_preimage_coe],
exact hu.inter hs
end
include mβ
@[measurability] lemma measurable.subtype_coe {p : β → Prop} {f : α → subtype p}
(hf : measurable f) :
measurable (λ a : α, (f a : β)) :=
measurable_subtype_coe.comp hf
@[measurability]
lemma measurable.subtype_mk {p : β → Prop} {f : α → β} (hf : measurable f) {h : ∀ x, p (f x)} :
measurable (λ x, (⟨f x, h x⟩ : subtype p)) :=
λ t ⟨s, hs⟩, hs.2 ▸ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1]
lemma measurable_of_measurable_union_cover
{f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t)
(hc : measurable (λ a : s, f a)) (hd : measurable (λ a : t, f a)) :
measurable f :=
begin
intros u hu,
convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)),
change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t),
rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe,
subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ],
end
lemma measurable_of_restrict_of_restrict_compl {f : α → β} {s : set α}
(hs : measurable_set s) (h₁ : measurable (s.restrict f)) (h₂ : measurable (sᶜ.restrict f)) :
measurable f :=
measurable_of_measurable_union_cover s sᶜ hs hs.compl (union_compl_self s).ge h₁ h₂
lemma measurable.dite [∀ x, decidable (x ∈ s)] {f : s → β} (hf : measurable f)
{g : sᶜ → β} (hg : measurable g) (hs : measurable_set s) :
measurable (λ x, if hx : x ∈ s then f ⟨x, hx⟩ else g ⟨x, hx⟩) :=
measurable_of_restrict_of_restrict_compl hs (by simpa) (by simpa)
lemma measurable_of_measurable_on_compl_finite [measurable_singleton_class α]
{f : α → β} (s : set α) (hs : s.finite) (hf : measurable (sᶜ.restrict f)) :
measurable f :=
begin
letI : fintype s := finite.fintype hs,
exact measurable_of_restrict_of_restrict_compl hs.measurable_set
(measurable_of_finite _) hf
end
lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class α]
{f : α → β} (a : α) (hf : measurable ({x | x ≠ a}.restrict f)) :
measurable f :=
measurable_of_measurable_on_compl_finite {a} (finite_singleton a) hf
end subtype
section prod
/-- A `measurable_space` structure on the product of two measurable spaces. -/
def measurable_space.prod {α β} (m₁ : measurable_space α) (m₂ : measurable_space β) :
measurable_space (α × β) :=
m₁.comap prod.fst ⊔ m₂.comap prod.snd
instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) :=
m₁.prod m₂
@[measurability] lemma measurable_fst {ma : measurable_space α} {mb : measurable_space β} :
measurable (prod.fst : α × β → α) :=
measurable.of_comap_le le_sup_left
@[measurability] lemma measurable_snd {ma : measurable_space α} {mb : measurable_space β} :
measurable (prod.snd : α × β → β) :=
measurable.of_comap_le le_sup_right
variables {m : measurable_space α} {mβ : measurable_space β} {mγ : measurable_space γ}
include m mβ mγ
lemma measurable.fst {f : α → β × γ} (hf : measurable f) :
measurable (λ a : α, (f a).1) :=
measurable_fst.comp hf
lemma measurable.snd {f : α → β × γ} (hf : measurable f) :
measurable (λ a : α, (f a).2) :=
measurable_snd.comp hf
@[measurability] lemma measurable.prod {f : α → β × γ}
(hf₁ : measurable (λ a, (f a).1)) (hf₂ : measurable (λ a, (f a).2)) : measurable f :=
measurable.of_le_map $ sup_le
(by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ })
(by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₂ })
lemma measurable.prod_mk {β γ} {mβ : measurable_space β}
{mγ : measurable_space γ} {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) :
measurable (λ a : α, (f a, g a)) :=
measurable.prod hf hg
lemma measurable.prod_map [measurable_space δ] {f : α → β} {g : γ → δ} (hf : measurable f)
(hg : measurable g) : measurable (prod.map f g) :=
(hf.comp measurable_fst).prod_mk (hg.comp measurable_snd)
omit mγ
lemma measurable_prod_mk_left {x : α} : measurable (@prod.mk _ β x) :=
measurable_const.prod_mk measurable_id
lemma measurable_prod_mk_right {y : β} : measurable (λ x : α, (x, y)) :=
measurable_id.prod_mk measurable_const
include mγ
lemma measurable.of_uncurry_left {f : α → β → γ} (hf : measurable (uncurry f)) {x : α} :
measurable (f x) :=
hf.comp measurable_prod_mk_left
lemma measurable.of_uncurry_right {f : α → β → γ} (hf : measurable (uncurry f)) {y : β} :
measurable (λ x, f x y) :=
hf.comp measurable_prod_mk_right
lemma measurable_prod {f : α → β × γ} : measurable f ↔
measurable (λ a, (f a).1) ∧ measurable (λ a, (f a).2) :=
⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, λ h, measurable.prod h.1 h.2⟩
omit mγ
@[measurability] lemma measurable_swap :
measurable (prod.swap : α × β → β × α) :=
measurable.prod measurable_snd measurable_fst
lemma measurable_swap_iff {mγ : measurable_space γ} {f : α × β → γ} :
measurable (f ∘ prod.swap) ↔ measurable f :=
⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, λ hf, hf.comp measurable_swap⟩
@[measurability]
lemma measurable_set.prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) :
measurable_set (s ×ˢ t) :=
measurable_set.inter (measurable_fst hs) (measurable_snd ht)
lemma measurable_set_prod_of_nonempty {s : set α} {t : set β} (h : (s ×ˢ t).nonempty) :
measurable_set (s ×ˢ t) ↔ measurable_set s ∧ measurable_set t :=
begin
rcases h with ⟨⟨x, y⟩, hx, hy⟩,
refine ⟨λ hst, _, λ h, h.1.prod h.2⟩,
have : measurable_set ((λ x, (x, y)) ⁻¹' s ×ˢ t) := measurable_prod_mk_right hst,
have : measurable_set (prod.mk x ⁻¹' s ×ˢ t) := measurable_prod_mk_left hst,
simp * at *
end
lemma measurable_set_prod {s : set α} {t : set β} :
measurable_set (s ×ˢ t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = ∅ ∨ t = ∅ :=
begin
cases (s ×ˢ t).eq_empty_or_nonempty with h h,
{ simp [h, prod_eq_empty_iff.mp h] },
{ simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] }
end
lemma measurable_set_swap_iff {s : set (α × β)} :
measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s :=
⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, λ hs, measurable_swap hs⟩
lemma measurable_from_prod_countable [countable β] [measurable_singleton_class β]
{mγ : measurable_space γ} {f : α × β → γ} (hf : ∀ y, measurable (λ x, f (x, y))) :
measurable f :=
begin
intros s hs,
have : f ⁻¹' s = ⋃ y, ((λ x, f (x, y)) ⁻¹' s) ×ˢ ({y} : set β),
{ ext1 ⟨x, y⟩,
simp [and_assoc, and.left_comm] },
rw this,
exact measurable_set.Union (λ y, (hf y hs).prod (measurable_set_singleton y))
end
/-- A piecewise function on countably many pieces is measurable if all the data is measurable. -/
@[measurability]
lemma measurable.find {m : measurable_space α}
{f : ℕ → α → β} {p : ℕ → α → Prop} [∀ n, decidable_pred (p n)]
(hf : ∀ n, measurable (f n)) (hp : ∀ n, measurable_set {x | p n x}) (h : ∀ x, ∃ n, p n x) :
measurable (λ x, f (nat.find (h x)) x) :=
begin
have : measurable (λ (p : α × ℕ), f p.2 p.1) := measurable_from_prod_countable (λ n, hf n),
exact this.comp (measurable.prod_mk measurable_id (measurable_find h hp)),
end
/-- Given countably many disjoint measurable sets `t n` and countably many measurable
functions `g n`, one can construct a measurable function that coincides with `g n` on `t n`. -/
lemma exists_measurable_piecewise_nat {m : measurable_space α} (t : ℕ → set β)
(t_meas : ∀ n, measurable_set (t n)) (t_disj : pairwise (disjoint on t))
(g : ℕ → β → α) (hg : ∀ n, measurable (g n)) :
∃ f : β → α, measurable f ∧ (∀ n x, x ∈ t n → f x = g n x) :=
begin
classical,
let p : ℕ → β → Prop := λ n x, x ∈ t n ∪ (⋃ k, t k)ᶜ,
have M : ∀ n, measurable_set {x | p n x} :=
λ n, (t_meas n).union (measurable_set.compl (measurable_set.Union t_meas)),
have P : ∀ x, ∃ n, p n x,
{ assume x,
by_cases H : ∀ (i : ℕ), x ∉ t i,
{ exact ⟨0, or.inr (by simpa only [mem_Inter, compl_Union] using H)⟩ },
{ simp only [not_forall, not_not_mem] at H,
rcases H with ⟨n, hn⟩,
exact ⟨n, or.inl hn⟩ } },
refine ⟨λ x, g (nat.find (P x)) x, measurable.find hg M P, _⟩,
assume n x hx,
have : x ∈ t (nat.find (P x)),
{ have B : x ∈ t (nat.find (P x)) ∪ (⋃ k, t k)ᶜ := nat.find_spec (P x),
have B' : (∀ (i : ℕ), x ∉ t i) ↔ false,
{ simp only [iff_false, not_forall, not_not_mem], exact ⟨n, hx⟩ },
simpa only [B', mem_union_eq, mem_Inter, or_false, compl_Union, mem_compl_eq] using B },
congr,
by_contra h,
exact t_disj n (nat.find (P x)) (ne.symm h) ⟨hx, this⟩
end
end prod
section pi
variables {π : δ → Type*} [measurable_space α]
instance measurable_space.pi [m : Π a, measurable_space (π a)] : measurable_space (Π a, π a) :=
⨆ a, (m a).comap (λ b, b a)
variables [Π a, measurable_space (π a)] [measurable_space γ]
lemma measurable_pi_iff {g : α → Π a, π a} :
measurable g ↔ ∀ a, measurable (λ x, g x a) :=
by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr,
measurable_space.comap_comp, function.comp, supr_le_iff]
@[measurability]
lemma measurable_pi_apply (a : δ) : measurable (λ f : Π a, π a, f a) :=
measurable.of_comap_le $ le_supr _ a
@[measurability]
lemma measurable.eval {a : δ} {g : α → Π a, π a}
(hg : measurable g) : measurable (λ x, g x a) :=
(measurable_pi_apply a).comp hg
@[measurability]
lemma measurable_pi_lambda (f : α → Π a, π a) (hf : ∀ a, measurable (λ c, f c a)) :
measurable f :=
measurable_pi_iff.mpr hf
/-- The function `update f a : π a → Π a, π a` is always measurable.
This doesn't require `f` to be measurable.
This should not be confused with the statement that `update f a x` is measurable. -/
@[measurability]
lemma measurable_update (f : Π (a : δ), π a) {a : δ} [decidable_eq δ] : measurable (update f a) :=
begin
apply measurable_pi_lambda,
intro x, by_cases hx : x = a,
{ cases hx, convert measurable_id, ext, simp },
simp_rw [update_noteq hx], apply measurable_const,
end
/- Even though we cannot use projection notation, we still keep a dot to be consistent with similar
lemmas, like `measurable_set.prod`. -/
@[measurability]
lemma measurable_set.pi {s : set δ} {t : Π i : δ, set (π i)} (hs : s.countable)
(ht : ∀ i ∈ s, measurable_set (t i)) :
measurable_set (s.pi t) :=
by { rw [pi_def], exact measurable_set.bInter hs (λ i hi, measurable_pi_apply _ (ht i hi)) }
lemma measurable_set.univ_pi [countable δ] {t : Π i : δ, set (π i)}
(ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) :=
measurable_set.pi (to_countable _) (λ i _, ht i)
lemma measurable_set_pi_of_nonempty
{s : set δ} {t : Π i, set (π i)} (hs : s.countable)
(h : (pi s t).nonempty) : measurable_set (pi s t) ↔ ∀ i ∈ s, measurable_set (t i) :=
begin
classical,
rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩,
convert measurable_update f hst, rw [update_preimage_pi hi], exact λ j hj _, hf j hj
end
lemma measurable_set_pi {s : set δ} {t : Π i, set (π i)} (hs : s.countable) :
measurable_set (pi s t) ↔ (∀ i ∈ s, measurable_set (t i)) ∨ pi s t = ∅ :=
begin
cases (pi s t).eq_empty_or_nonempty with h h,
{ simp [h] },
{ simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] }
end
variable (π)
@[measurability]
lemma measurable_pi_equiv_pi_subtype_prod_symm (p : δ → Prop) [decidable_pred p] :
measurable (equiv.pi_equiv_pi_subtype_prod p π).symm :=
begin
apply measurable_pi_iff.2 (λ j, _),
by_cases hj : p j,
{ simp only [hj, dif_pos, equiv.pi_equiv_pi_subtype_prod_symm_apply],
have : measurable (λ (f : (Π (i : {x // p x}), π ↑i)), f ⟨j, hj⟩) :=
measurable_pi_apply ⟨j, hj⟩,
exact measurable.comp this measurable_fst },
{ simp only [hj, equiv.pi_equiv_pi_subtype_prod_symm_apply, dif_neg, not_false_iff],
have : measurable (λ (f : (Π (i : {x // ¬ p x}), π ↑i)), f ⟨j, hj⟩) :=
measurable_pi_apply ⟨j, hj⟩,
exact measurable.comp this measurable_snd }
end
@[measurability]
lemma measurable_pi_equiv_pi_subtype_prod (p : δ → Prop) [decidable_pred p] :
measurable (equiv.pi_equiv_pi_subtype_prod p π) :=
begin
refine measurable_prod.2 _,
split;
{ apply measurable_pi_iff.2 (λ j, _),
simp only [pi_equiv_pi_subtype_prod_apply, measurable_pi_apply] }
end
end pi
instance tprod.measurable_space (π : δ → Type*) [∀ x, measurable_space (π x)] :
∀ (l : list δ), measurable_space (list.tprod π l)
| [] := punit.measurable_space
| (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is)
section tprod
open list
variables {π : δ → Type*} [∀ x, measurable_space (π x)]
lemma measurable_tprod_mk (l : list δ) : measurable (@tprod.mk δ π l) :=
begin
induction l with i l ih,
{ exact measurable_const },
{ exact (measurable_pi_apply i).prod_mk ih }
end
lemma measurable_tprod_elim [decidable_eq δ] : ∀ {l : list δ} {i : δ} (hi : i ∈ l),
measurable (λ (v : tprod π l), v.elim hi)
| (i :: is) j hj := begin
by_cases hji : j = i,
{ subst hji, simp [measurable_fst] },
{ rw [funext $ tprod.elim_of_ne _ hji],
exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd }
end
lemma measurable_tprod_elim' [decidable_eq δ] {l : list δ} (h : ∀ i, i ∈ l) :
measurable (tprod.elim' h : tprod π l → Π i, π i) :=
measurable_pi_lambda _ (λ i, measurable_tprod_elim (h i))
lemma measurable_set.tprod (l : list δ) {s : ∀ i, set (π i)} (hs : ∀ i, measurable_set (s i)) :
measurable_set (set.tprod l s) :=
by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih }
end tprod
instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) :=
m₁.map sum.inl ⊓ m₂.map sum.inr
section sum
@[measurability] lemma measurable_inl [measurable_space α] [measurable_space β] :
measurable (@sum.inl α β) :=
measurable.of_le_map inf_le_left
@[measurability] lemma measurable_inr [measurable_space α] [measurable_space β] :
measurable (@sum.inr α β) :=
measurable.of_le_map inf_le_right
variables {m : measurable_space α} {mβ : measurable_space β}
include m mβ
lemma measurable_sum {mγ : measurable_space γ} {f : α ⊕ β → γ}
(hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f :=
measurable.of_comap_le $ le_inf
(measurable_space.comap_le_iff_le_map.2 $ hl)
(measurable_space.comap_le_iff_le_map.2 $ hr)
@[measurability]
lemma measurable.sum_elim {mγ : measurable_space γ} {f : α → γ} {g : β → γ}
(hf : measurable f) (hg : measurable g) :
measurable (sum.elim f g) :=
measurable_sum hf hg
lemma measurable_set.inl_image {s : set α} (hs : measurable_set s) :
measurable_set (sum.inl '' s : set (α ⊕ β)) :=
⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (λ a b, sum.inl.inj) },
have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ :=
eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction,
show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩
lemma measurable_set_inr_image {s : set β} (hs : measurable_set s) :
measurable_set (sum.inr '' s : set (α ⊕ β)) :=
⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ :=
eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction,
show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty },
show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact λ a b, sum.inr.inj }⟩
omit m
lemma measurable_set_range_inl [measurable_space α] :
measurable_set (range sum.inl : set (α ⊕ β)) :=
by { rw [← image_univ], exact measurable_set.univ.inl_image }
lemma measurable_set_range_inr [measurable_space α] :
measurable_set (range sum.inr : set (α ⊕ β)) :=
by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ }
end sum
instance {α} {β : α → Type*} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) :=
⨅a, (m a).map (sigma.mk a)
end constructions
/-- A map `f : α → β` is called a *measurable embedding* if it is injective, measurable, and sends
measurable sets to measurable sets. The latter assumption can be replaced with “`f` has measurable
inverse `g : range f → α`”, see `measurable_embedding.measurable_range_splitting`,
`measurable_embedding.of_measurable_inverse_range`, and
`measurable_embedding.of_measurable_inverse`.
One more interpretation: `f` is a measurable embedding if it defines a measurable equivalence to its
range and the range is a measurable set. One implication is formalized as
`measurable_embedding.equiv_range`; the other one follows from
`measurable_equiv.measurable_embedding`, `measurable_embedding.subtype_coe`, and
`measurable_embedding.comp`. -/
@[protect_proj]
structure measurable_embedding {α β : Type*} [measurable_space α] [measurable_space β] (f : α → β) :
Prop :=
(injective : injective f)
(measurable : measurable f)
(measurable_set_image' : ∀ ⦃s⦄, measurable_set s → measurable_set (f '' s))
namespace measurable_embedding
variables {mα : measurable_space α} [measurable_space β] [measurable_space γ]
{f : α → β} {g : β → γ}
include mα
lemma measurable_set_image (hf : measurable_embedding f) {s : set α} :
measurable_set (f '' s) ↔ measurable_set s :=
⟨λ h, by simpa only [hf.injective.preimage_image] using hf.measurable h,
λ h, hf.measurable_set_image' h⟩
lemma id : measurable_embedding (id : α → α) :=
⟨injective_id, measurable_id, λ s hs, by rwa image_id⟩
lemma comp (hg : measurable_embedding g) (hf : measurable_embedding f) :
measurable_embedding (g ∘ f) :=
⟨hg.injective.comp hf.injective, hg.measurable.comp hf.measurable,
λ s hs, by rwa [← image_image, hg.measurable_set_image, hf.measurable_set_image]⟩
lemma subtype_coe {s : set α} (hs : measurable_set s) : measurable_embedding (coe : s → α) :=
{ injective := subtype.coe_injective,
measurable := measurable_subtype_coe,
measurable_set_image' := λ _, measurable_set.subtype_image hs }
lemma measurable_set_range (hf : measurable_embedding f) : measurable_set (range f) :=
by { rw ← image_univ, exact hf.measurable_set_image' measurable_set.univ }
lemma measurable_set_preimage (hf : measurable_embedding f) {s : set β} :
measurable_set (f ⁻¹' s) ↔ measurable_set (s ∩ range f) :=
by rw [← image_preimage_eq_inter_range, hf.measurable_set_image]
lemma measurable_range_splitting (hf : measurable_embedding f) :
measurable (range_splitting f) :=
λ s hs, by rwa [preimage_range_splitting hf.injective,
← (subtype_coe hf.measurable_set_range).measurable_set_image, ← image_comp,
coe_comp_range_factorization, hf.measurable_set_image]
lemma measurable_extend (hf : measurable_embedding f) {g : α → γ} {g' : β → γ}
(hg : measurable g) (hg' : measurable g') :
measurable (extend f g g') :=
begin
refine measurable_of_restrict_of_restrict_compl hf.measurable_set_range _ _,
{ rw restrict_extend_range,
simpa only [range_splitting] using hg.comp hf.measurable_range_splitting },
{ rw restrict_extend_compl_range, exact hg'.comp measurable_subtype_coe }
end
lemma exists_measurable_extend (hf : measurable_embedding f) {g : α → γ} (hg : measurable g)
(hne : β → nonempty γ) :
∃ g' : β → γ, measurable g' ∧ g' ∘ f = g :=
⟨extend f g (λ x, classical.choice (hne x)),
hf.measurable_extend hg (measurable_const' $ λ _ _, rfl),
funext $ λ x, extend_apply hf.injective _ _ _⟩
lemma measurable_comp_iff (hg : measurable_embedding g) : measurable (g ∘ f) ↔ measurable f :=
begin
refine ⟨λ H, _, hg.measurable.comp⟩,
suffices : measurable ((range_splitting g ∘ range_factorization g) ∘ f),
by rwa [(right_inverse_range_splitting hg.injective).comp_eq_id] at this,
exact hg.measurable_range_splitting.comp H.subtype_mk
end
end measurable_embedding
lemma measurable_set.exists_measurable_proj {m : measurable_space α} {s : set α}
(hs : measurable_set s) (hne : s.nonempty) : ∃ f : α → s, measurable f ∧ ∀ x : s, f x = x :=
let ⟨f, hfm, hf⟩ := (measurable_embedding.subtype_coe hs).exists_measurable_extend
measurable_id (λ _, hne.to_subtype)
in ⟨f, hfm, congr_fun hf⟩
/-- Equivalences between measurable spaces. Main application is the simplification of measurability
statements along measurable equivalences. -/
structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β :=
(measurable_to_fun : measurable to_equiv)
(measurable_inv_fun : measurable to_equiv.symm)
infix ` ≃ᵐ `:25 := measurable_equiv
namespace measurable_equiv
variables (α β) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ]
instance : has_coe_to_fun (α ≃ᵐ β) (λ _, α → β) := ⟨λ e, e.to_fun⟩
variables {α β}
@[simp] lemma coe_to_equiv (e : α ≃ᵐ β) : (e.to_equiv : α → β) = e := rfl
@[measurability]
protected lemma measurable (e : α ≃ᵐ β) : measurable (e : α → β) :=
e.measurable_to_fun
@[simp] lemma coe_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) :
((⟨e, h1, h2⟩ : α ≃ᵐ β) : α → β) = e := rfl
/-- Any measurable space is equivalent to itself. -/
def refl (α : Type*) [measurable_space α] : α ≃ᵐ α :=
{ to_equiv := equiv.refl α,
measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id }
instance : inhabited (α ≃ᵐ α) := ⟨refl α⟩
/-- The composition of equivalences between measurable spaces. -/
def trans (ab : α ≃ᵐ β) (bc : β ≃ᵐ γ) :
α ≃ᵐ γ :=
{ to_equiv := ab.to_equiv.trans bc.to_equiv,
measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun,
measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun }
/-- The inverse of an equivalence between measurable spaces. -/
def symm (ab : α ≃ᵐ β) : β ≃ᵐ α :=
{ to_equiv := ab.to_equiv.symm,
measurable_to_fun := ab.measurable_inv_fun,
measurable_inv_fun := ab.measurable_to_fun }
@[simp] lemma coe_to_equiv_symm (e : α ≃ᵐ β) : (e.to_equiv.symm : β → α) = e.symm := rfl
/-- See Note [custom simps projection]. We need to specify this projection explicitly in this case,
because it is a composition of multiple projections. -/
def simps.apply (h : α ≃ᵐ β) : α → β := h
/-- See Note [custom simps projection] -/
def simps.symm_apply (h : α ≃ᵐ β) : β → α := h.symm
initialize_simps_projections measurable_equiv
(to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply)
lemma to_equiv_injective : injective (to_equiv : (α ≃ᵐ β) → (α ≃ β)) :=
by { rintro ⟨e₁, _, _⟩ ⟨e₂, _, _⟩ (rfl : e₁ = e₂), refl }
@[ext] lemma ext {e₁ e₂ : α ≃ᵐ β} (h : (e₁ : α → β) = e₂) : e₁ = e₂ :=
to_equiv_injective $ equiv.coe_fn_injective h
@[simp] lemma symm_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) :
(⟨e, h1, h2⟩ : α ≃ᵐ β).symm = ⟨e.symm, h2, h1⟩ := rfl
attribute [simps apply to_equiv] trans refl
@[simp] lemma symm_refl (α : Type*) [measurable_space α] : (refl α).symm = refl α := rfl
@[simp] theorem symm_comp_self (e : α ≃ᵐ β) : e.symm ∘ e = id := funext e.left_inv
@[simp] theorem self_comp_symm (e : α ≃ᵐ β) : e ∘ e.symm = id := funext e.right_inv
@[simp] theorem apply_symm_apply (e : α ≃ᵐ β) (y : β) : e (e.symm y) = y := e.right_inv y
@[simp] theorem symm_apply_apply (e : α ≃ᵐ β) (x : α) : e.symm (e x) = x := e.left_inv x
@[simp] theorem symm_trans_self (e : α ≃ᵐ β) : e.symm.trans e = refl β :=
ext e.self_comp_symm
@[simp] theorem self_trans_symm (e : α ≃ᵐ β) : e.trans e.symm = refl α :=
ext e.symm_comp_self
protected theorem surjective (e : α ≃ᵐ β) : surjective e := e.to_equiv.surjective
protected theorem bijective (e : α ≃ᵐ β) : bijective e := e.to_equiv.bijective
protected theorem injective (e : α ≃ᵐ β) : injective e := e.to_equiv.injective
@[simp] theorem symm_preimage_preimage (e : α ≃ᵐ β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s :=
e.to_equiv.symm_preimage_preimage s
theorem image_eq_preimage (e : α ≃ᵐ β) (s : set α) : e '' s = e.symm ⁻¹' s :=
e.to_equiv.image_eq_preimage s
@[simp] theorem measurable_set_preimage (e : α ≃ᵐ β) {s : set β} :
measurable_set (e ⁻¹' s) ↔ measurable_set s :=
⟨λ h, by simpa only [symm_preimage_preimage] using e.symm.measurable h, λ h, e.measurable h⟩
@[simp] theorem measurable_set_image (e : α ≃ᵐ β) {s : set α} :
measurable_set (e '' s) ↔ measurable_set s :=
by rw [image_eq_preimage, measurable_set_preimage]
/-- A measurable equivalence is a measurable embedding. -/
protected lemma measurable_embedding (e : α ≃ᵐ β) : measurable_embedding e :=
{ injective := e.injective,
measurable := e.measurable,
measurable_set_image' := λ s, e.measurable_set_image.2 }
/-- Equal measurable spaces are equivalent. -/
protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β]
(h : α = β) (hi : i₁ == i₂) : α ≃ᵐ β :=
{ to_equiv := equiv.cast h,
measurable_to_fun := by { substI h, substI hi, exact measurable_id },
measurable_inv_fun := by { substI h, substI hi, exact measurable_id }}
protected lemma measurable_comp_iff {f : β → γ} (e : α ≃ᵐ β) :
measurable (f ∘ e) ↔ measurable f :=
iff.intro
(assume hfe,
have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable,
by rwa [coe_to_equiv, symm_trans_self] at this)
(λ h, h.comp e.measurable)
/-- Any two types with unique elements are measurably equivalent. -/
def of_unique_of_unique (α β : Type*) [measurable_space α] [measurable_space β]
[unique α] [unique β] : α ≃ᵐ β :=
{ to_equiv := equiv_of_unique α β,
measurable_to_fun := subsingleton.measurable,
measurable_inv_fun := subsingleton.measurable }
/-- Products of equivalent measurable spaces are equivalent. -/
def prod_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α × γ ≃ᵐ β × δ :=
{ to_equiv := prod_congr ab.to_equiv cd.to_equiv,
measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk
(cd.measurable_to_fun.comp measurable_id.snd),
measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk
(cd.measurable_inv_fun.comp measurable_id.snd) }
/-- Products of measurable spaces are symmetric. -/
def prod_comm : α × β ≃ᵐ β × α :=
{ to_equiv := prod_comm α β,
measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst,
measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst }
/-- Products of measurable spaces are associative. -/
def prod_assoc : (α × β) × γ ≃ᵐ α × (β × γ) :=
{ to_equiv := prod_assoc α β γ,
measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd,
measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd }
/-- Sums of measurable spaces are symmetric. -/
def sum_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α ⊕ γ ≃ᵐ β ⊕ δ :=
{ to_equiv := sum_congr ab.to_equiv cd.to_equiv,
measurable_to_fun :=
begin
cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd',
refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm)
end,
measurable_inv_fun :=
begin
cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd',
refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm)
end }
/-- `s ×ˢ t ≃ (s × t)` as measurable spaces. -/
def set.prod (s : set α) (t : set β) : ↥(s ×ˢ t) ≃ᵐ s × t :=
{ to_equiv := equiv.set.prod s t,
measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk
measurable_id.subtype_coe.snd.subtype_mk,
measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk
measurable_id.snd.subtype_coe }
/-- `univ α ≃ α` as measurable spaces. -/
def set.univ (α : Type*) [measurable_space α] : (univ : set α) ≃ᵐ α :=
{ to_equiv := equiv.set.univ α,
measurable_to_fun := measurable_id.subtype_coe,
measurable_inv_fun := measurable_id.subtype_mk }
/-- `{a} ≃ unit` as measurable spaces. -/
def set.singleton (a : α) : ({a} : set α) ≃ᵐ unit :=
{ to_equiv := equiv.set.singleton a,
measurable_to_fun := measurable_const,
measurable_inv_fun := measurable_const }
/-- A set is equivalent to its image under a function `f` as measurable spaces,
if `f` is an injective measurable function that sends measurable sets to measurable sets. -/
noncomputable def set.image (f : α → β) (s : set α) (hf : injective f)
(hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : s ≃ᵐ (f '' s) :=
{ to_equiv := equiv.set.image f s hf,
measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk,
measurable_inv_fun :=
begin
rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf],
exact measurable_subtype_coe (hfi u hu)
end }
/-- The domain of `f` is equivalent to its range as measurable spaces,
if `f` is an injective measurable function that sends measurable sets to measurable sets. -/
noncomputable def set.range (f : α → β) (hf : injective f) (hfm : measurable f)
(hfi : ∀ s, measurable_set s → measurable_set (f '' s)) :
α ≃ᵐ (range f) :=
(measurable_equiv.set.univ _).symm.trans $
(measurable_equiv.set.image f univ hf hfm hfi).trans $
measurable_equiv.cast (by rw image_univ) (by rw image_univ)
/-- `α` is equivalent to its image in `α ⊕ β` as measurable spaces. -/
def set.range_inl : (range sum.inl : set (α ⊕ β)) ≃ᵐ α :=
{ to_fun := λ ab, match ab with
| ⟨sum.inl a, _⟩ := a
| ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim
end,
inv_fun := λ a, ⟨sum.inl a, a, rfl⟩,
left_inv := by { rintro ⟨ab, a, rfl⟩, refl },
right_inv := assume a, rfl,
measurable_to_fun := assume s (hs : measurable_set s),
begin
refine ⟨_, hs.inl_image, set.ext _⟩,
rintros ⟨ab, a, rfl⟩,
simp [set.range_inl._match_1]
end,
measurable_inv_fun := measurable.subtype_mk measurable_inl }
/-- `β` is equivalent to its image in `α ⊕ β` as measurable spaces. -/
def set.range_inr : (range sum.inr : set (α ⊕ β)) ≃ᵐ β :=
{ to_fun := λ ab, match ab with
| ⟨sum.inr b, _⟩ := b
| ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim
end,
inv_fun := λ b, ⟨sum.inr b, b, rfl⟩,
left_inv := by { rintro ⟨ab, b, rfl⟩, refl },
right_inv := assume b, rfl,
measurable_to_fun := assume s (hs : measurable_set s),
begin
refine ⟨_, measurable_set_inr_image hs, set.ext _⟩,
rintros ⟨ab, b, rfl⟩,
simp [set.range_inr._match_1]
end,
measurable_inv_fun := measurable.subtype_mk measurable_inr }
/-- Products distribute over sums (on the right) as measurable spaces. -/
def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] :
(α ⊕ β) × γ ≃ᵐ (α × γ) ⊕ (β × γ) :=
{ to_equiv := sum_prod_distrib α β γ,
measurable_to_fun :=
begin
refine measurable_of_measurable_union_cover
(range sum.inl ×ˢ (univ : set γ))
(range sum.inr ×ˢ (univ : set γ))
(measurable_set_range_inl.prod measurable_set.univ)
(measurable_set_range_inr.prod measurable_set.univ)
(by { rintro ⟨a|b, c⟩; simp [set.prod_eq] })
_
_,
{ refine (set.prod (range sum.inl) univ).symm.measurable_comp_iff.1 _,
refine (prod_congr set.range_inl (set.univ _)).symm.measurable_comp_iff.1 _,
dsimp [(∘)],
convert measurable_inl,
ext ⟨a, c⟩, refl },
{ refine (set.prod (range sum.inr) univ).symm.measurable_comp_iff.1 _,
refine (prod_congr set.range_inr (set.univ _)).symm.measurable_comp_iff.1 _,
dsimp [(∘)],
convert measurable_inr,
ext ⟨b, c⟩, refl }
end,
measurable_inv_fun :=
measurable_sum
((measurable_inl.comp measurable_fst).prod_mk measurable_snd)
((measurable_inr.comp measurable_fst).prod_mk measurable_snd) }
/-- Products distribute over sums (on the left) as measurable spaces. -/
def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] :
α × (β ⊕ γ) ≃ᵐ (α × β) ⊕ (α × γ) :=
prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm
/-- Products distribute over sums as measurable spaces. -/
def sum_prod_sum (α β γ δ)
[measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] :
(α ⊕ β) × (γ ⊕ δ) ≃ᵐ ((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ)) :=
(sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _)
variables {π π' : δ' → Type*} [∀ x, measurable_space (π x)] [∀ x, measurable_space (π' x)]
/-- A family of measurable equivalences `Π a, β₁ a ≃ᵐ β₂ a` generates a measurable equivalence
between `Π a, β₁ a` and `Π a, β₂ a`. -/
def Pi_congr_right (e : Π a, π a ≃ᵐ π' a) : (Π a, π a) ≃ᵐ (Π a, π' a) :=
{ to_equiv := Pi_congr_right (λ a, (e a).to_equiv),
measurable_to_fun :=
measurable_pi_lambda _ (λ i, (e i).measurable_to_fun.comp (measurable_pi_apply i)),
measurable_inv_fun :=
measurable_pi_lambda _ (λ i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) }
/-- Pi-types are measurably equivalent to iterated products. -/
@[simps {fully_applied := ff}]
def pi_measurable_equiv_tprod [decidable_eq δ']
{l : list δ'} (hnd : l.nodup) (h : ∀ i, i ∈ l) :
(Π i, π i) ≃ᵐ list.tprod π l :=
{ to_equiv := list.tprod.pi_equiv_tprod hnd h,
measurable_to_fun := measurable_tprod_mk l,
measurable_inv_fun := measurable_tprod_elim' h }
/-- If `α` has a unique term, then the type of function `α → β` is measurably equivalent to `β`. -/
@[simps {fully_applied := ff}] def fun_unique (α β : Type*) [unique α] [measurable_space β] :
(α → β) ≃ᵐ β :=
{ to_equiv := equiv.fun_unique α β,
measurable_to_fun := measurable_pi_apply _,
measurable_inv_fun := measurable_pi_iff.2 $ λ b, measurable_id }
/-- The space `Π i : fin 2, α i` is measurably equivalent to `α 0 × α 1`. -/
@[simps {fully_applied := ff}] def pi_fin_two (α : fin 2 → Type*) [∀ i, measurable_space (α i)] :
(Π i, α i) ≃ᵐ α 0 × α 1 :=
{ to_equiv := pi_fin_two_equiv α,
measurable_to_fun := measurable.prod (measurable_pi_apply _) (measurable_pi_apply _),
measurable_inv_fun := measurable_pi_iff.2 $
fin.forall_fin_two.2 ⟨measurable_fst, measurable_snd⟩ }
/-- The space `fin 2 → α` is measurably equivalent to `α × α`. -/
@[simps {fully_applied := ff}] def fin_two_arrow : (fin 2 → α) ≃ᵐ α × α := pi_fin_two (λ _, α)
/-- Measurable equivalence between `Π j : fin (n + 1), α j` and
`α i × Π j : fin n, α (fin.succ_above i j)`. -/
@[simps {fully_applied := ff}]
def pi_fin_succ_above_equiv {n : ℕ} (α : fin (n + 1) → Type*) [Π i, measurable_space (α i)]
(i : fin (n + 1)) :
(Π j, α j) ≃ᵐ α i × (Π j, α (i.succ_above j)) :=
{ to_equiv := pi_fin_succ_above_equiv α i,
measurable_to_fun := (measurable_pi_apply i).prod_mk $ measurable_pi_iff.2 $
λ j, measurable_pi_apply _,
measurable_inv_fun := by simp [measurable_pi_iff, i.forall_iff_succ_above, measurable_fst,
(measurable_pi_apply _).comp measurable_snd] }
variable (π)
/-- Measurable equivalence between (dependent) functions on a type and pairs of functions on
`{i // p i}` and `{i // ¬p i}`. See also `equiv.pi_equiv_pi_subtype_prod`. -/
@[simps {fully_applied := ff}]
def pi_equiv_pi_subtype_prod (p : δ' → Prop) [decidable_pred p] :
(Π i, π i) ≃ᵐ ((Π i : subtype p, π i) × (Π i : {i // ¬p i}, π i)) :=
{ to_equiv := pi_equiv_pi_subtype_prod p π,
measurable_to_fun := measurable_pi_equiv_pi_subtype_prod π p,
measurable_inv_fun := measurable_pi_equiv_pi_subtype_prod_symm π p }
end measurable_equiv
namespace measurable_embedding
variables [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β}
/-- A measurable embedding defines a measurable equivalence between its domain
and its range. -/
noncomputable def equiv_range (f : α → β) (hf : measurable_embedding f) :
α ≃ᵐ range f :=
{ to_equiv := equiv.of_injective f hf.injective,
measurable_to_fun := hf.measurable.subtype_mk,
measurable_inv_fun :=
by { rw coe_of_injective_symm, exact hf.measurable_range_splitting } }
lemma of_measurable_inverse_on_range {g : range f → α} (hf₁ : measurable f)
(hf₂ : measurable_set (range f)) (hg : measurable g)
(H : left_inverse g (range_factorization f)) : measurable_embedding f :=
begin
set e : α ≃ᵐ range f :=
⟨⟨range_factorization f, g, H, H.right_inverse_of_surjective surjective_onto_range⟩,
hf₁.subtype_mk, hg⟩,
exact (measurable_embedding.subtype_coe hf₂).comp e.measurable_embedding
end
lemma of_measurable_inverse {g : β → α} (hf₁ : measurable f)
(hf₂ : measurable_set (range f)) (hg : measurable g)
(H : left_inverse g f) : measurable_embedding f :=
of_measurable_inverse_on_range hf₁ hf₂ (hg.comp measurable_subtype_coe) H
end measurable_embedding
namespace filter
variables [measurable_space α]
/-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/
class is_measurably_generated (f : filter α) : Prop :=
(exists_measurable_subset : ∀ ⦃s⦄, s ∈ f → ∃ t ∈ f, measurable_set t ∧ t ⊆ s)
instance is_measurably_generated_bot : is_measurably_generated (⊥ : filter α) :=
⟨λ _ _, ⟨∅, mem_bot, measurable_set.empty, empty_subset _⟩⟩
instance is_measurably_generated_top : is_measurably_generated (⊤ : filter α) :=
⟨λ s hs, ⟨univ, univ_mem, measurable_set.univ, λ x _, hs x⟩⟩
lemma eventually.exists_measurable_mem {f : filter α} [is_measurably_generated f]
{p : α → Prop} (h : ∀ᶠ x in f, p x) :
∃ s ∈ f, measurable_set s ∧ ∀ x ∈ s, p x :=
is_measurably_generated.exists_measurable_subset h
lemma eventually.exists_measurable_mem_of_small_sets {f : filter α} [is_measurably_generated f]
{p : set α → Prop} (h : ∀ᶠ s in f.small_sets, p s) :
∃ s ∈ f, measurable_set s ∧ p s :=
let ⟨s, hsf, hs⟩ := eventually_small_sets.1 h,
⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf
in ⟨t, htf, htm, hs t hts⟩
instance inf_is_measurably_generated (f g : filter α) [is_measurably_generated f]
[is_measurably_generated g] :
is_measurably_generated (f ⊓ g) :=
begin
refine ⟨_⟩,
rintros t ⟨sf, hsf, sg, hsg, rfl⟩,
rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩,
rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩,
refine ⟨s'f ∩ s'g, inter_mem_inf hs'f hs'g, hmf.inter hmg, _⟩,
exact inter_subset_inter hs'sf hs'sg
end
lemma principal_is_measurably_generated_iff {s : set α} :
is_measurably_generated (𝓟 s) ↔ measurable_set s :=
begin
refine ⟨_, λ hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩,
rintros ⟨hs⟩,
rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩,
have : t = s := subset.antisymm hts ht,
rwa ← this
end
alias principal_is_measurably_generated_iff ↔
_ _root_.measurable_set.principal_is_measurably_generated
instance infi_is_measurably_generated {f : ι → filter α} [∀ i, is_measurably_generated (f i)] :
is_measurably_generated (⨅ i, f i) :=
begin
refine ⟨λ s hs, _⟩,
rw [← equiv.plift.surjective.infi_comp, mem_infi] at hs,
rcases hs with ⟨t, ht, ⟨V, hVf, rfl⟩⟩,
choose U hUf hU using λ i, is_measurably_generated.exists_measurable_subset (hVf i),
refine ⟨⋂ i : t, U i, _, _, _⟩,
{ rw [← equiv.plift.surjective.infi_comp, mem_infi],
refine ⟨t, ht, U, hUf, rfl⟩ },
{ haveI := ht.countable.to_encodable,
exact measurable_set.Inter (λ i, (hU i).1) },
{ exact Inter_mono (λ i, (hU i).2) }
end
end filter
/-- We say that a collection of sets is countably spanning if a countable subset spans the
whole type. This is a useful condition in various parts of measure theory. For example, it is
a needed condition to show that the product of two collections generate the product sigma algebra,
see `generate_from_prod_eq`. -/
def is_countably_spanning (C : set (set α)) : Prop :=
∃ (s : ℕ → set α), (∀ n, s n ∈ C) ∧ (⋃ n, s n) = univ
lemma is_countably_spanning_measurable_set [measurable_space α] :
is_countably_spanning {s : set α | measurable_set s} :=
⟨λ _, univ, λ _, measurable_set.univ, Union_const _⟩
namespace measurable_set
/-!
### Typeclasses on `subtype measurable_set`
-/
variables [measurable_space α]
instance : has_mem α (subtype (measurable_set : set α → Prop)) :=
⟨λ a s, a ∈ (s : set α)⟩
@[simp] lemma mem_coe (a : α) (s : subtype (measurable_set : set α → Prop)) :
a ∈ (s : set α) ↔ a ∈ s := iff.rfl
instance : has_emptyc (subtype (measurable_set : set α → Prop)) :=
⟨⟨∅, measurable_set.empty⟩⟩
@[simp] lemma coe_empty : ↑(∅ : subtype (measurable_set : set α → Prop)) = (∅ : set α) := rfl
instance [measurable_singleton_class α] : has_insert α (subtype (measurable_set : set α → Prop)) :=
⟨λ a s, ⟨has_insert.insert a s, s.prop.insert a⟩⟩
@[simp] lemma coe_insert [measurable_singleton_class α] (a : α)
(s : subtype (measurable_set : set α → Prop)) :
↑(has_insert.insert a s) = (has_insert.insert a s : set α) := rfl
instance : has_compl (subtype (measurable_set : set α → Prop)) :=
⟨λ x, ⟨xᶜ, x.prop.compl⟩⟩
@[simp] lemma coe_compl (s : subtype (measurable_set : set α → Prop)) : ↑(sᶜ) = (sᶜ : set α) := rfl
instance : has_union (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x ∪ y, x.prop.union y.prop⟩⟩
@[simp] lemma coe_union (s t : subtype (measurable_set : set α → Prop)) :
↑(s ∪ t) = (s ∪ t : set α) := rfl
instance : has_inter (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x ∩ y, x.prop.inter y.prop⟩⟩
@[simp] lemma coe_inter (s t : subtype (measurable_set : set α → Prop)) :
↑(s ∩ t) = (s ∩ t : set α) := rfl
instance : has_sdiff (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x \ y, x.prop.diff y.prop⟩⟩
@[simp] lemma coe_sdiff (s t : subtype (measurable_set : set α → Prop)) :
↑(s \ t) = (s \ t : set α) := rfl
instance : has_bot (subtype (measurable_set : set α → Prop)) :=
⟨⟨⊥, measurable_set.empty⟩⟩
@[simp] lemma coe_bot : ↑(⊥ : subtype (measurable_set : set α → Prop)) = (⊥ : set α) := rfl
instance : has_top (subtype (measurable_set : set α → Prop)) :=
⟨⟨⊤, measurable_set.univ⟩⟩
@[simp] lemma coe_top : ↑(⊤ : subtype (measurable_set : set α → Prop)) = (⊤ : set α) := rfl
instance : partial_order (subtype (measurable_set : set α → Prop)) :=
partial_order.lift _ subtype.coe_injective
instance : distrib_lattice (subtype (measurable_set : set α → Prop)) :=
{ sup := (∪),
le_sup_left := λ a b, show (a : set α) ≤ a ⊔ b, from le_sup_left,
le_sup_right := λ a b, show (b : set α) ≤ a ⊔ b, from le_sup_right,
sup_le := λ a b c ha hb, show (a ⊔ b : set α) ≤ c, from sup_le ha hb,
inf := (∩),
inf_le_left := λ a b, show (a ⊓ b : set α) ≤ a, from inf_le_left,
inf_le_right := λ a b, show (a ⊓ b : set α) ≤ b, from inf_le_right,
le_inf := λ a b c ha hb, show (a : set α) ≤ b ⊓ c, from le_inf ha hb,
le_sup_inf := λ x y z, show ((x ⊔ y) ⊓ (x ⊔ z) : set α) ≤ x ⊔ y ⊓ z, from le_sup_inf,
.. measurable_set.subtype.partial_order }
instance : bounded_order (subtype (measurable_set : set α → Prop)) :=
{ top := ⊤,
le_top := λ a, show (a : set α) ≤ ⊤, from le_top,
bot := ⊥,
bot_le := λ a, show (⊥ : set α) ≤ a, from bot_le }
instance : boolean_algebra (subtype (measurable_set : set α → Prop)) :=
{ sdiff := (\),
compl := has_compl.compl,
inf_compl_le_bot := λ a, boolean_algebra.inf_compl_le_bot (a : set α),
top_le_sup_compl := λ a, boolean_algebra.top_le_sup_compl (a : set α),
sdiff_eq := λ a b, subtype.eq $ sdiff_eq,
.. measurable_set.subtype.bounded_order,
.. measurable_set.subtype.distrib_lattice }
end measurable_set
|
3d0d24a299eff8e6874b8494cf37af63691dbe98 | 4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d | /src/Lean/Elab/Tactic/Conv/Pattern.lean | a2f04da7ec6f8603b847eb9b26772f091d4f90e9 | [
"Apache-2.0"
] | permissive | subfish-zhou/leanprover-zh_CN.github.io | 30b9fba9bd790720bd95764e61ae796697d2f603 | 8b2985d4a3d458ceda9361ac454c28168d920d3f | refs/heads/master | 1,689,709,967,820 | 1,632,503,056,000 | 1,632,503,056,000 | 409,962,097 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,818 | 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.Tactic.Simp
import Lean.Elab.Tactic.Conv.Basic
namespace Lean.Elab.Tactic.Conv
open Meta
private def getContext : MetaM Simp.Context := do
return {
simpLemmas := {}
congrLemmas := (← getCongrLemmas)
config.zeta := false
config.beta := false
config.eta := false
config.iota := false
config.proj := false
config.decide := false
}
private def pre (pattern : Expr) (found? : IO.Ref (Option Expr)) (e : Expr) : SimpM Simp.Step := do
if (← withReducible <| isDefEqGuarded pattern e) then
let (rhs, newGoal) ← mkConvGoalFor e
found?.set newGoal
return Simp.Step.done { expr := rhs, proof? := newGoal }
else
return Simp.Step.visit { expr := e }
private def findPattern? (pattern : Expr) (e : Expr) : MetaM (Option (MVarId × Simp.Result)) := do
let found? ← IO.mkRef none
let result ← Simp.main e (← getContext) (methods := { pre := pre pattern found? })
if let some newGoal ← found?.get then
return some (newGoal.mvarId!, result)
else
return none
@[builtinTactic Lean.Parser.Tactic.Conv.pattern] def evalPattern : Tactic := fun stx => withMainContext do
match stx with
| `(conv| pattern $p) =>
let pattern ← Term.withoutPending <| Term.elabTerm p none
let lhs ← getLhs
match (← findPattern? pattern lhs) with
| none => throwError "'pattern' conv tactic failed, pattern was not found{indentExpr pattern}"
| some (mvarId', result) =>
updateLhs result.expr (← result.getProof)
applyRefl (← getMainGoal)
replaceMainGoal [mvarId']
| _ => throwUnsupportedSyntax
end Lean.Elab.Tactic.Conv
|
65a03e35794f6c171bcd8ee9027b7a89981255b2 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/ring_theory/finiteness.lean | 3fc7cc91f7a21c68104bcc30439959fd5d660f16 | [
"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 | 35,462 | 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 group_theory.finiteness
import ring_theory.algebra_tower
import ring_theory.ideal.quotient
import ring_theory.noetherian
/-!
# Finiteness conditions in commutative algebra
In this file we define several notions of finiteness that are common in commutative algebra.
## Main declarations
- `module.finite`, `algebra.finite`, `ring_hom.finite`, `alg_hom.finite`
all of these express that some object is finitely generated *as module* over some base ring.
- `algebra.finite_type`, `ring_hom.finite_type`, `alg_hom.finite_type`
all of these express that some object is finitely generated *as algebra* over some base ring.
- `algebra.finite_presentation`, `ring_hom.finite_presentation`, `alg_hom.finite_presentation`
all of these express that some object is finitely presented *as algebra* over some base ring.
-/
open function (surjective)
open_locale big_operators
section module_and_algebra
variables (R A B M N : Type*)
/-- A module over a semiring is `finite` if it is finitely generated as a module. -/
class module.finite [semiring R] [add_comm_monoid M] [module R M] :
Prop := (out : (⊤ : submodule R M).fg)
/-- An algebra over a commutative semiring is of `finite_type` if it is finitely generated
over the base ring as algebra. -/
class algebra.finite_type [comm_semiring R] [semiring A] [algebra R A] : Prop :=
(out : (⊤ : subalgebra R A).fg)
/-- An algebra over a commutative semiring is `finite_presentation` if it is the quotient of a
polynomial ring in `n` variables by a finitely generated ideal. -/
def algebra.finite_presentation [comm_semiring R] [semiring A] [algebra R A] : Prop :=
∃ (n : ℕ) (f : mv_polynomial (fin n) R →ₐ[R] A),
surjective f ∧ f.to_ring_hom.ker.fg
namespace module
variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid N] [module R N]
lemma finite_def {R M} [semiring R] [add_comm_monoid M] [module R M] :
finite R M ↔ (⊤ : submodule R M).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance is_noetherian.finite [is_noetherian R M] : finite R M :=
⟨is_noetherian.noetherian ⊤⟩
namespace finite
open _root_.submodule set
lemma iff_add_monoid_fg {M : Type*} [add_comm_monoid M] : module.finite ℕ M ↔ add_monoid.fg M :=
⟨λ h, add_monoid.fg_def.2 $ (fg_iff_add_submonoid_fg ⊤).1 (finite_def.1 h),
λ h, finite_def.2 $ (fg_iff_add_submonoid_fg ⊤).2 (add_monoid.fg_def.1 h)⟩
lemma iff_add_group_fg {G : Type*} [add_comm_group G] : module.finite ℤ G ↔ add_group.fg G :=
⟨λ h, add_group.fg_def.2 $ (fg_iff_add_subgroup_fg ⊤).1 (finite_def.1 h),
λ h, finite_def.2 $ (fg_iff_add_subgroup_fg ⊤).2 (add_group.fg_def.1 h)⟩
variables {R M N}
lemma exists_fin [finite R M] : ∃ (n : ℕ) (s : fin n → M), span R (range s) = ⊤ :=
submodule.fg_iff_exists_fin_generating_family.mp out
lemma of_surjective [hM : finite R M] (f : M →ₗ[R] N) (hf : surjective f) :
finite R N :=
⟨begin
rw [← linear_map.range_eq_top.2 hf, ← submodule.map_top],
exact submodule.fg_map hM.1
end⟩
lemma of_injective [is_noetherian R N] (f : M →ₗ[R] N)
(hf : function.injective f) : finite R M :=
⟨fg_of_injective f hf⟩
variables (R)
instance self : finite R R :=
⟨⟨{1}, by simpa only [finset.coe_singleton] using ideal.span_singleton_one⟩⟩
variable (M)
lemma of_restrict_scalars_finite (R A M : Type*) [comm_semiring R] [semiring A] [add_comm_monoid M]
[module R M] [module A M] [algebra R A] [is_scalar_tower R A M] [hM : finite R M] :
finite A M :=
begin
rw [finite_def, fg_def] at hM ⊢,
obtain ⟨S, hSfin, hSgen⟩ := hM,
refine ⟨S, hSfin, eq_top_iff.2 _⟩,
have := submodule.span_le_restrict_scalars R A M S,
rw hSgen at this,
exact this
end
variables {R M}
instance prod [hM : finite R M] [hN : finite R N] : finite R (M × N) :=
⟨begin
rw ← submodule.prod_top,
exact submodule.fg_prod hM.1 hN.1
end⟩
lemma equiv [hM : finite R M] (e : M ≃ₗ[R] N) : finite R N :=
of_surjective (e : M →ₗ[R] N) e.surjective
section algebra
lemma trans {R : Type*} (A B : Type*) [comm_semiring R] [comm_semiring A] [algebra R A]
[semiring B] [algebra R B] [algebra A B] [is_scalar_tower R A B] :
∀ [finite R A] [finite A B], finite R B
| ⟨⟨s, hs⟩⟩ ⟨⟨t, ht⟩⟩ := ⟨submodule.fg_def.2
⟨set.image2 (•) (↑s : set A) (↑t : set B),
set.finite.image2 _ s.finite_to_set t.finite_to_set,
by rw [set.image2_smul, submodule.span_smul hs (↑t : set B),
ht, submodule.restrict_scalars_top]⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance finite_type {R : Type*} (A : Type*) [comm_semiring R] [comm_semiring A]
[algebra R A] [hRA : finite R A] : algebra.finite_type R A :=
⟨subalgebra.fg_of_submodule_fg hRA.1⟩
end algebra
end finite
end module
namespace algebra
variables [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B]
variables [add_comm_group M] [module R M]
variables [add_comm_group N] [module R N]
namespace finite_type
lemma self : finite_type R R := ⟨⟨{1}, subsingleton.elim _ _⟩⟩
section
open_locale classical
protected lemma mv_polynomial (ι : Type*) [fintype ι] : finite_type R (mv_polynomial ι R) :=
⟨⟨finset.univ.image mv_polynomial.X, begin
rw eq_top_iff, refine λ p, mv_polynomial.induction_on' p
(λ u x, finsupp.induction u (subalgebra.algebra_map_mem _ x)
(λ i n f hif hn ih, _))
(λ p q ihp ihq, subalgebra.add_mem _ ihp ihq),
rw [add_comm, mv_polynomial.monomial_add_single],
exact subalgebra.mul_mem _ ih
(subalgebra.pow_mem _ (subset_adjoin $ finset.mem_image_of_mem _ $ finset.mem_univ _) _)
end⟩⟩
end
lemma of_restrict_scalars_finite_type [algebra A B] [is_scalar_tower R A B] [hB : finite_type R B] :
finite_type A B :=
begin
obtain ⟨S, hS⟩ := hB.out,
refine ⟨⟨S, eq_top_iff.2 (λ b, _)⟩⟩,
have le : adjoin R (S : set B) ≤ subalgebra.restrict_scalars R (adjoin A S),
{ apply (algebra.adjoin_le _ : _ ≤ (subalgebra.restrict_scalars R (adjoin A ↑S))),
simp only [subalgebra.coe_restrict_scalars],
exact algebra.subset_adjoin, },
exact le (eq_top_iff.1 hS b),
end
variables {R A B}
lemma of_surjective (hRA : finite_type R A) (f : A →ₐ[R] B) (hf : surjective f) :
finite_type R B :=
⟨begin
convert subalgebra.fg_map _ f hRA.1,
simpa only [map_top f, @eq_comm _ ⊤, eq_top_iff, alg_hom.mem_range] using hf
end⟩
lemma equiv (hRA : finite_type R A) (e : A ≃ₐ[R] B) : finite_type R B :=
hRA.of_surjective e e.surjective
lemma trans [algebra A B] [is_scalar_tower R A B] (hRA : finite_type R A) (hAB : finite_type A B) :
finite_type R B :=
⟨fg_trans' hRA.1 hAB.1⟩
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a finset. -/
lemma iff_quotient_mv_polynomial : (finite_type R A) ↔ ∃ (s : finset A)
(f : (mv_polynomial {x // x ∈ s} R) →ₐ[R] A), (surjective f) :=
begin
split,
{ rintro ⟨s, hs⟩,
use [s, mv_polynomial.aeval coe],
intro x,
have hrw : (↑s : set A) = (λ (x : A), x ∈ s.val) := rfl,
rw [← set.mem_range, ← alg_hom.coe_range, ← adjoin_eq_range, ← hrw, hs],
exact set.mem_univ x },
{ rintro ⟨s, ⟨f, hsur⟩⟩,
exact finite_type.of_surjective (finite_type.mv_polynomial R {x // x ∈ s}) f hsur }
end
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a fintype. -/
lemma iff_quotient_mv_polynomial' : (finite_type R A) ↔ ∃ (ι : Type u_2) (_ : fintype ι)
(f : (mv_polynomial ι R) →ₐ[R] A), (surjective f) :=
begin
split,
{ rw iff_quotient_mv_polynomial,
rintro ⟨s, ⟨f, hsur⟩⟩,
use [{x // x ∈ s}, by apply_instance, f, hsur] },
{ rintro ⟨ι, ⟨hfintype, ⟨f, hsur⟩⟩⟩,
letI : fintype ι := hfintype,
exact finite_type.of_surjective (finite_type.mv_polynomial R ι) f hsur }
end
/-- An algebra is finitely generated if and only if it is a quotient of a polynomial ring in `n`
variables. -/
lemma iff_quotient_mv_polynomial'' : (finite_type R A) ↔ ∃ (n : ℕ)
(f : (mv_polynomial (fin n) R) →ₐ[R] A), (surjective f) :=
begin
split,
{ rw iff_quotient_mv_polynomial',
rintro ⟨ι, hfintype, ⟨f, hsur⟩⟩,
letI := hfintype,
obtain ⟨equiv⟩ := @fintype.trunc_equiv_fin ι (classical.dec_eq ι) hfintype,
replace equiv := mv_polynomial.rename_equiv R equiv,
exact ⟨fintype.card ι, alg_hom.comp f equiv.symm, function.surjective.comp hsur
(alg_equiv.symm equiv).surjective⟩ },
{ rintro ⟨n, ⟨f, hsur⟩⟩,
exact finite_type.of_surjective (finite_type.mv_polynomial R (fin n)) f hsur }
end
/-- A finitely presented algebra is of finite type. -/
lemma of_finite_presentation : finite_presentation R A → finite_type R A :=
begin
rintro ⟨n, f, hf⟩,
apply (finite_type.iff_quotient_mv_polynomial'').2,
exact ⟨n, f, hf.1⟩
end
instance prod [hA : finite_type R A] [hB : finite_type R B] : finite_type R (A × B) :=
⟨begin
rw ← subalgebra.prod_top,
exact subalgebra.fg_prod hA.1 hB.1
end⟩
end finite_type
namespace finite_presentation
variables {R A B}
/-- An algebra over a Noetherian ring is finitely generated if and only if it is finitely
presented. -/
lemma of_finite_type [is_noetherian_ring R] : finite_type R A ↔ finite_presentation R A :=
begin
refine ⟨λ h, _, algebra.finite_type.of_finite_presentation⟩,
obtain ⟨n, f, hf⟩ := algebra.finite_type.iff_quotient_mv_polynomial''.1 h,
refine ⟨n, f, hf, _⟩,
have hnoet : is_noetherian_ring (mv_polynomial (fin n) R) := by apply_instance,
replace hnoet := (is_noetherian_ring_iff.1 hnoet).noetherian,
exact hnoet f.to_ring_hom.ker,
end
/-- If `e : A ≃ₐ[R] B` and `A` is finitely presented, then so is `B`. -/
lemma equiv (hfp : finite_presentation R A) (e : A ≃ₐ[R] B) : finite_presentation R B :=
begin
obtain ⟨n, f, hf⟩ := hfp,
use [n, alg_hom.comp ↑e f],
split,
{ exact function.surjective.comp e.surjective hf.1 },
suffices hker : (alg_hom.comp ↑e f).to_ring_hom.ker = f.to_ring_hom.ker,
{ rw hker, exact hf.2 },
{ have hco : (alg_hom.comp ↑e f).to_ring_hom = ring_hom.comp ↑e.to_ring_equiv f.to_ring_hom,
{ have h : (alg_hom.comp ↑e f).to_ring_hom = e.to_alg_hom.to_ring_hom.comp f.to_ring_hom := rfl,
have h1 : ↑(e.to_ring_equiv) = (e.to_alg_hom).to_ring_hom := rfl,
rw [h, h1] },
rw [ring_hom.ker_eq_comap_bot, hco, ← ideal.comap_comap, ← ring_hom.ker_eq_comap_bot,
ring_hom.ker_coe_equiv (alg_equiv.to_ring_equiv e), ring_hom.ker_eq_comap_bot] }
end
variable (R)
/-- The ring of polynomials in finitely many variables is finitely presented. -/
lemma mv_polynomial (ι : Type u_2) [fintype ι] : finite_presentation R (mv_polynomial ι R) :=
begin
obtain ⟨equiv⟩ := @fintype.trunc_equiv_fin ι (classical.dec_eq ι) _,
replace equiv := mv_polynomial.rename_equiv R equiv,
refine ⟨_, alg_equiv.to_alg_hom equiv.symm, _⟩,
split,
{ exact (alg_equiv.symm equiv).surjective },
suffices hinj : function.injective equiv.symm.to_alg_hom.to_ring_hom,
{ rw [(ring_hom.injective_iff_ker_eq_bot _).1 hinj],
exact submodule.fg_bot },
exact (alg_equiv.symm equiv).injective
end
/-- `R` is finitely presented as `R`-algebra. -/
lemma self : finite_presentation R R :=
equiv (mv_polynomial R pempty) (mv_polynomial.is_empty_alg_equiv R pempty)
variable {R}
/-- The quotient of a finitely presented algebra by a finitely generated ideal is finitely
presented. -/
lemma quotient {I : ideal A} (h : submodule.fg I) (hfp : finite_presentation R A) :
finite_presentation R I.quotient :=
begin
obtain ⟨n, f, hf⟩ := hfp,
refine ⟨n, (ideal.quotient.mkₐ R I).comp f, _, _⟩,
{ exact (ideal.quotient.mkₐ_surjective R I).comp hf.1 },
{ refine submodule.fg_ker_ring_hom_comp _ _ hf.2 _ hf.1,
simp [h] }
end
/-- If `f : A →ₐ[R] B` is surjective with finitely generated kernel and `A` is finitely presented,
then so is `B`. -/
lemma of_surjective {f : A →ₐ[R] B} (hf : function.surjective f) (hker : f.to_ring_hom.ker.fg)
(hfp : finite_presentation R A) : finite_presentation R B :=
equiv (quotient hker hfp) (ideal.quotient_ker_alg_equiv_of_surjective hf)
lemma iff : finite_presentation R A ↔
∃ n (I : ideal (_root_.mv_polynomial (fin n) R)) (e : I.quotient ≃ₐ[R] A), I.fg :=
begin
split,
{ rintros ⟨n, f, hf⟩,
exact ⟨n, f.to_ring_hom.ker, ideal.quotient_ker_alg_equiv_of_surjective hf.1, hf.2⟩ },
{ rintros ⟨n, I, e, hfg⟩,
exact equiv (quotient hfg (mv_polynomial R _)) e }
end
/-- An algebra is finitely presented if and only if it is a quotient of a polynomial ring whose
variables are indexed by a fintype by a finitely generated ideal. -/
lemma iff_quotient_mv_polynomial' : finite_presentation R A ↔ ∃ (ι : Type u_2) (_ : fintype ι)
(f : (_root_.mv_polynomial ι R) →ₐ[R] A), (surjective f) ∧ f.to_ring_hom.ker.fg :=
begin
split,
{ rintro ⟨n, f, hfs, hfk⟩,
set ulift_var := mv_polynomial.rename_equiv R equiv.ulift,
refine ⟨ulift (fin n), infer_instance, f.comp ulift_var.to_alg_hom,
hfs.comp ulift_var.surjective,
submodule.fg_ker_ring_hom_comp _ _ _ hfk ulift_var.surjective⟩,
convert submodule.fg_bot,
exact ring_hom.ker_coe_equiv ulift_var.to_ring_equiv, },
{ rintro ⟨ι, hfintype, f, hf⟩,
haveI : fintype ι := hfintype,
obtain ⟨equiv⟩ := @fintype.trunc_equiv_fin ι (classical.dec_eq ι) _,
replace equiv := mv_polynomial.rename_equiv R equiv,
refine ⟨fintype.card ι, f.comp equiv.symm,
hf.1.comp (alg_equiv.symm equiv).surjective,
submodule.fg_ker_ring_hom_comp _ f _ hf.2 equiv.symm.surjective⟩,
convert submodule.fg_bot,
exact ring_hom.ker_coe_equiv (equiv.symm.to_ring_equiv), }
end
/-- If `A` is a finitely presented `R`-algebra, then `mv_polynomial (fin n) A` is finitely presented
as `R`-algebra. -/
lemma mv_polynomial_of_finite_presentation (hfp : finite_presentation R A) (ι : Type*)
[fintype ι] : finite_presentation R (_root_.mv_polynomial ι A) :=
begin
classical,
let n := fintype.card ι,
obtain ⟨e⟩ := fintype.trunc_equiv_fin ι,
replace e := (mv_polynomial.rename_equiv A e).restrict_scalars R,
refine equiv _ e.symm,
obtain ⟨m, I, e, hfg⟩ := iff.1 hfp,
refine equiv _ (mv_polynomial.map_alg_equiv (fin n) e),
-- typeclass inference seems to struggle to find this path
letI : is_scalar_tower R
(_root_.mv_polynomial (fin m) R) (_root_.mv_polynomial (fin m) R) :=
is_scalar_tower.right,
letI : is_scalar_tower R
(_root_.mv_polynomial (fin m) R)
(_root_.mv_polynomial (fin n) (_root_.mv_polynomial (fin m) R)) :=
mv_polynomial.is_scalar_tower,
refine equiv _ ((@mv_polynomial.quotient_equiv_quotient_mv_polynomial
_ (fin n) _ I).restrict_scalars R).symm,
refine quotient (submodule.map_fg_of_fg I hfg _) _,
let := mv_polynomial.sum_alg_equiv R (fin n) (fin m),
refine equiv _ this,
exact equiv (mv_polynomial R (fin (n + m))) (mv_polynomial.rename_equiv R fin_sum_fin_equiv).symm
end
/-- If `A` is an `R`-algebra and `S` is an `A`-algebra, both finitely presented, then `S` is
finitely presented as `R`-algebra. -/
lemma trans [algebra A B] [is_scalar_tower R A B] (hfpA : finite_presentation R A)
(hfpB : finite_presentation A B) : finite_presentation R B :=
begin
obtain ⟨n, I, e, hfg⟩ := iff.1 hfpB,
exact equiv (quotient hfg (mv_polynomial_of_finite_presentation hfpA _)) (e.restrict_scalars R)
end
end finite_presentation
end algebra
end module_and_algebra
namespace ring_hom
variables {A B C : Type*} [comm_ring A] [comm_ring B] [comm_ring C]
/-- A ring morphism `A →+* B` is `finite` if `B` is finitely generated as `A`-module. -/
def finite (f : A →+* B) : Prop :=
by letI : algebra A B := f.to_algebra; exact module.finite A B
/-- A ring morphism `A →+* B` is of `finite_type` if `B` is finitely generated as `A`-algebra. -/
def finite_type (f : A →+* B) : Prop := @algebra.finite_type A B _ _ f.to_algebra
/-- A ring morphism `A →+* B` is of `finite_presentation` if `B` is finitely presented as
`A`-algebra. -/
def finite_presentation (f : A →+* B) : Prop := @algebra.finite_presentation A B _ _ f.to_algebra
namespace finite
variables (A)
lemma id : finite (ring_hom.id A) := module.finite.self A
variables {A}
lemma of_surjective (f : A →+* B) (hf : surjective f) : f.finite :=
begin
letI := f.to_algebra,
exact module.finite.of_surjective (algebra.of_id A B).to_linear_map hf
end
lemma comp {g : B →+* C} {f : A →+* B} (hg : g.finite) (hf : f.finite) : (g.comp f).finite :=
@module.finite.trans A B C _ _ f.to_algebra _ (g.comp f).to_algebra g.to_algebra
begin
fconstructor,
intros a b c,
simp only [algebra.smul_def, ring_hom.map_mul, mul_assoc],
refl
end
hf hg
lemma finite_type {f : A →+* B} (hf : f.finite) : finite_type f :=
@module.finite.finite_type _ _ _ _ f.to_algebra hf
lemma of_comp_finite {f : A →+* B} {g : B →+* C} (h : (g.comp f).finite) : g.finite :=
begin
letI := f.to_algebra,
letI := g.to_algebra,
letI := (g.comp f).to_algebra,
letI : is_scalar_tower A B C := restrict_scalars.is_scalar_tower A B C,
letI : module.finite A C := h,
exact module.finite.of_restrict_scalars_finite A B C
end
end finite
namespace finite_type
variables (A)
lemma id : finite_type (ring_hom.id A) := algebra.finite_type.self A
variables {A}
lemma comp_surjective {f : A →+* B} {g : B →+* C} (hf : f.finite_type) (hg : surjective g) :
(g.comp f).finite_type :=
@algebra.finite_type.of_surjective A B C _ _ f.to_algebra _ (g.comp f).to_algebra hf
{ to_fun := g, commutes' := λ a, rfl, .. g } hg
lemma of_surjective (f : A →+* B) (hf : surjective f) : f.finite_type :=
by { rw ← f.comp_id, exact (id A).comp_surjective hf }
lemma comp {g : B →+* C} {f : A →+* B} (hg : g.finite_type) (hf : f.finite_type) :
(g.comp f).finite_type :=
@algebra.finite_type.trans A B C _ _ f.to_algebra _ (g.comp f).to_algebra g.to_algebra
begin
fconstructor,
intros a b c,
simp only [algebra.smul_def, ring_hom.map_mul, mul_assoc],
refl
end
hf hg
lemma of_finite_presentation {f : A →+* B} (hf : f.finite_presentation) : f.finite_type :=
@algebra.finite_type.of_finite_presentation A B _ _ f.to_algebra hf
lemma of_comp_finite_type {f : A →+* B} {g : B →+* C} (h : (g.comp f).finite_type) :
g.finite_type :=
begin
letI := f.to_algebra,
letI := g.to_algebra,
letI := (g.comp f).to_algebra,
letI : is_scalar_tower A B C := restrict_scalars.is_scalar_tower A B C,
letI : algebra.finite_type A C := h,
exact algebra.finite_type.of_restrict_scalars_finite_type A B C
end
end finite_type
namespace finite_presentation
variables (A)
lemma id : finite_presentation (ring_hom.id A) := algebra.finite_presentation.self A
variables {A}
lemma comp_surjective {f : A →+* B} {g : B →+* C} (hf : f.finite_presentation) (hg : surjective g)
(hker : g.ker.fg) : (g.comp f).finite_presentation :=
@algebra.finite_presentation.of_surjective A B C _ _ f.to_algebra _ (g.comp f).to_algebra
{ to_fun := g, commutes' := λ a, rfl, .. g } hg hker hf
lemma of_surjective (f : A →+* B) (hf : surjective f) (hker : f.ker.fg) : f.finite_presentation :=
by { rw ← f.comp_id, exact (id A).comp_surjective hf hker}
lemma of_finite_type [is_noetherian_ring A] {f : A →+* B} : f.finite_type ↔ f.finite_presentation :=
@algebra.finite_presentation.of_finite_type A B _ _ f.to_algebra _
lemma comp {g : B →+* C} {f : A →+* B} (hg : g.finite_presentation) (hf : f.finite_presentation) :
(g.comp f).finite_presentation :=
@algebra.finite_presentation.trans A B C _ _ f.to_algebra _ (g.comp f).to_algebra g.to_algebra
{ smul_assoc := λ a b c, begin
simp only [algebra.smul_def, ring_hom.map_mul, mul_assoc],
refl
end }
hf hg
end finite_presentation
end ring_hom
namespace alg_hom
variables {R A B C : Type*} [comm_ring R]
variables [comm_ring A] [comm_ring B] [comm_ring C]
variables [algebra R A] [algebra R B] [algebra R C]
/-- An algebra morphism `A →ₐ[R] B` is finite if it is finite as ring morphism.
In other words, if `B` is finitely generated as `A`-module. -/
def finite (f : A →ₐ[R] B) : Prop := f.to_ring_hom.finite
/-- An algebra morphism `A →ₐ[R] B` is of `finite_type` if it is of finite type as ring morphism.
In other words, if `B` is finitely generated as `A`-algebra. -/
def finite_type (f : A →ₐ[R] B) : Prop := f.to_ring_hom.finite_type
/-- An algebra morphism `A →ₐ[R] B` is of `finite_presentation` if it is of finite presentation as
ring morphism. In other words, if `B` is finitely presented as `A`-algebra. -/
def finite_presentation (f : A →ₐ[R] B) : Prop := f.to_ring_hom.finite_presentation
namespace finite
variables (R A)
lemma id : finite (alg_hom.id R A) := ring_hom.finite.id A
variables {R A}
lemma comp {g : B →ₐ[R] C} {f : A →ₐ[R] B} (hg : g.finite) (hf : f.finite) : (g.comp f).finite :=
ring_hom.finite.comp hg hf
lemma of_surjective (f : A →ₐ[R] B) (hf : surjective f) : f.finite :=
ring_hom.finite.of_surjective f hf
lemma finite_type {f : A →ₐ[R] B} (hf : f.finite) : finite_type f :=
ring_hom.finite.finite_type hf
lemma of_comp_finite {f : A →ₐ[R] B} {g : B →ₐ[R] C} (h : (g.comp f).finite) : g.finite :=
ring_hom.finite.of_comp_finite h
end finite
namespace finite_type
variables (R A)
lemma id : finite_type (alg_hom.id R A) := ring_hom.finite_type.id A
variables {R A}
lemma comp {g : B →ₐ[R] C} {f : A →ₐ[R] B} (hg : g.finite_type) (hf : f.finite_type) :
(g.comp f).finite_type :=
ring_hom.finite_type.comp hg hf
lemma comp_surjective {f : A →ₐ[R] B} {g : B →ₐ[R] C} (hf : f.finite_type) (hg : surjective g) :
(g.comp f).finite_type :=
ring_hom.finite_type.comp_surjective hf hg
lemma of_surjective (f : A →ₐ[R] B) (hf : surjective f) : f.finite_type :=
ring_hom.finite_type.of_surjective f hf
lemma of_finite_presentation {f : A →ₐ[R] B} (hf : f.finite_presentation) : f.finite_type :=
ring_hom.finite_type.of_finite_presentation hf
lemma of_comp_finite_type {f : A →ₐ[R] B} {g : B →ₐ[R] C} (h : (g.comp f).finite_type) :
g.finite_type :=
ring_hom.finite_type.of_comp_finite_type h
end finite_type
namespace finite_presentation
variables (R A)
lemma id : finite_presentation (alg_hom.id R A) := ring_hom.finite_presentation.id A
variables {R A}
lemma comp {g : B →ₐ[R] C} {f : A →ₐ[R] B} (hg : g.finite_presentation)
(hf : f.finite_presentation) : (g.comp f).finite_presentation :=
ring_hom.finite_presentation.comp hg hf
lemma comp_surjective {f : A →ₐ[R] B} {g : B →ₐ[R] C} (hf : f.finite_presentation)
(hg : surjective g) (hker : g.to_ring_hom.ker.fg) : (g.comp f).finite_presentation :=
ring_hom.finite_presentation.comp_surjective hf hg hker
lemma of_surjective (f : A →ₐ[R] B) (hf : surjective f) (hker : f.to_ring_hom.ker.fg) :
f.finite_presentation :=
ring_hom.finite_presentation.of_surjective f hf hker
lemma of_finite_type [is_noetherian_ring A] {f : A →ₐ[R] B} :
f.finite_type ↔ f.finite_presentation :=
ring_hom.finite_presentation.of_finite_type
end finite_presentation
end alg_hom
section monoid_algebra
variables {R : Type*} {M : Type*}
namespace add_monoid_algebra
open algebra add_submonoid submodule
section span
section semiring
variables [comm_semiring R] [add_monoid M]
/-- An element of `add_monoid_algebra R M` is in the subalgebra generated by its support. -/
lemma mem_adjoin_support (f : add_monoid_algebra R M) : f ∈ adjoin R (of' R M '' f.support) :=
begin
suffices : span R (of' R M '' f.support) ≤ (adjoin R (of' R M '' f.support)).to_submodule,
{ exact this (mem_span_support f) },
rw submodule.span_le,
exact subset_adjoin
end
/-- If a set `S` generates, as algebra, `add_monoid_algebra R M`, then the set of supports of
elements of `S` generates `add_monoid_algebra R M`. -/
lemma support_gen_of_gen {S : set (add_monoid_algebra R M)} (hS : algebra.adjoin R S = ⊤) :
algebra.adjoin R (⋃ f ∈ S, (of' R M '' (f.support : set M))) = ⊤ :=
begin
refine le_antisymm le_top _,
rw [← hS, adjoin_le_iff],
intros f hf,
have hincl : of' R M '' f.support ⊆
⋃ (g : add_monoid_algebra R M) (H : g ∈ S), of' R M '' g.support,
{ intros s hs,
exact set.mem_bUnion_iff.2 ⟨f, ⟨hf, hs⟩⟩ },
exact adjoin_mono hincl (mem_adjoin_support f)
end
/-- If a set `S` generates, as algebra, `add_monoid_algebra R M`, then the image of the union of
the supports of elements of `S` generates `add_monoid_algebra R M`. -/
lemma support_gen_of_gen' {S : set (add_monoid_algebra R M)} (hS : algebra.adjoin R S = ⊤) :
algebra.adjoin R (of' R M '' (⋃ f ∈ S, (f.support : set M))) = ⊤ :=
begin
suffices : of' R M '' (⋃ f ∈ S, (f.support : set M)) = ⋃ f ∈ S, (of' R M '' (f.support : set M)),
{ rw this,
exact support_gen_of_gen hS },
simp only [set.image_Union]
end
end semiring
section ring
variables [comm_ring R] [add_comm_monoid M]
/-- If `add_monoid_algebra R M` is of finite type, there there is a `G : finset M` such that its
image generates, as algera, `add_monoid_algebra R M`. -/
lemma exists_finset_adjoin_eq_top [h : finite_type R (add_monoid_algebra R M)] :
∃ G : finset M, algebra.adjoin R (of' R M '' G) = ⊤ :=
begin
unfreezingI { obtain ⟨S, hS⟩ := h },
letI : decidable_eq M := classical.dec_eq M,
use finset.bUnion S (λ f, f.support),
have : (finset.bUnion S (λ f, f.support) : set M) = ⋃ f ∈ S, (f.support : set M),
{ simp only [finset.set_bUnion_coe, finset.coe_bUnion] },
rw [this],
exact support_gen_of_gen' hS
end
/-- The image of an element `m : M` in `add_monoid_algebra R M` belongs the submodule generated by
`S : set M` if and only if `m ∈ S`. -/
lemma of'_mem_span [nontrivial R] {m : M} {S : set M} :
of' R M m ∈ span R (of' R M '' S) ↔ m ∈ S :=
begin
refine ⟨λ h, _, λ h, submodule.subset_span $ set.mem_image_of_mem (of R M) h⟩,
rw [of', ← finsupp.supported_eq_span_single, finsupp.mem_supported,
finsupp.support_single_ne_zero (@one_ne_zero R _ (by apply_instance))] at h,
simpa using h
end
/--If the image of an element `m : M` in `add_monoid_algebra R M` belongs the submodule generated by
the closure of some `S : set M` then `m ∈ closure S`. -/
lemma mem_closure_of_mem_span_closure [nontrivial R] {m : M} {S : set M}
(h : of' R M m ∈ span R (submonoid.closure (of' R M '' S) : set (add_monoid_algebra R M))) :
m ∈ closure S :=
begin
suffices : multiplicative.of_add m ∈ submonoid.closure (multiplicative.to_add ⁻¹' S),
{ simpa [← to_submonoid_closure] },
rw [set.image_congr' (show ∀ x, of' R M x = of R M x, from λ x, of'_eq_of x),
← monoid_hom.map_mclosure] at h,
simpa using of'_mem_span.1 h
end
end ring
end span
variables [add_comm_monoid M]
/-- If a set `S` generates an additive monoid `M`, then the image of `M` generates, as algebra,
`add_monoid_algebra R M`. -/
lemma mv_polynomial_aeval_of_surjective_of_closure [comm_semiring R] {S : set M}
(hS : closure S = ⊤) : function.surjective (mv_polynomial.aeval
(λ (s : S), of' R M ↑s) : mv_polynomial S R → add_monoid_algebra R M) :=
begin
refine λ f, induction_on f (λ m, _) _ _,
{ have : m ∈ closure S := hS.symm ▸ mem_top _,
refine closure_induction this (λ m hm, _) _ _,
{ exact ⟨mv_polynomial.X ⟨m, hm⟩, mv_polynomial.aeval_X _ _⟩ },
{ exact ⟨1, alg_hom.map_one _⟩ },
{ rintro m₁ m₂ ⟨P₁, hP₁⟩ ⟨P₂, hP₂⟩,
exact ⟨P₁ * P₂, by rw [alg_hom.map_mul, hP₁, hP₂, of_apply, of_apply, of_apply,
single_mul_single, one_mul]; refl⟩ } },
{ rintro f g ⟨P, rfl⟩ ⟨Q, rfl⟩,
exact ⟨P + Q, alg_hom.map_add _ _ _⟩ },
{ rintro r f ⟨P, rfl⟩,
exact ⟨r • P, alg_hom.map_smul _ _ _⟩ }
end
variables (R M)
/-- If an additive monoid `M` is finitely generated then `add_monoid_algebra R M` is of finite
type. -/
instance finite_type_of_fg [comm_ring R] [h : add_monoid.fg M] :
finite_type R (add_monoid_algebra R M) :=
begin
obtain ⟨S, hS⟩ := h.out,
exact (finite_type.mv_polynomial R (S : set M)).of_surjective (mv_polynomial.aeval
(λ (s : (S : set M)), of' R M ↑s)) (mv_polynomial_aeval_of_surjective_of_closure hS)
end
variables {R M}
/-- An additive monoid `M` is finitely generated if and only if `add_monoid_algebra R M` is of
finite type. -/
lemma finite_type_iff_fg [comm_ring R] [nontrivial R] :
finite_type R (add_monoid_algebra R M) ↔ add_monoid.fg M :=
begin
refine ⟨λ h, _, λ h, @add_monoid_algebra.finite_type_of_fg _ _ _ _ h⟩,
obtain ⟨S, hS⟩ := @exists_finset_adjoin_eq_top R M _ _ h,
refine add_monoid.fg_def.2 ⟨S, (eq_top_iff' _).2 (λ m, _)⟩,
have hm : of' R M m ∈ (adjoin R (of' R M '' ↑S)).to_submodule,
{ simp only [hS, top_to_submodule, submodule.mem_top], },
rw [adjoin_eq_span] at hm,
exact mem_closure_of_mem_span_closure hm
end
/-- If `add_monoid_algebra R M` is of finite type then `M` is finitely generated. -/
lemma fg_of_finite_type [comm_ring R] [nontrivial R] [h : finite_type R (add_monoid_algebra R M)] :
add_monoid.fg M :=
finite_type_iff_fg.1 h
/-- An additive group `G` is finitely generated if and only if `add_monoid_algebra R G` is of
finite type. -/
lemma finite_type_iff_group_fg {G : Type*} [add_comm_group G] [comm_ring R] [nontrivial R] :
finite_type R (add_monoid_algebra R G) ↔ add_group.fg G :=
by simpa [add_group.fg_iff_add_monoid.fg] using finite_type_iff_fg
end add_monoid_algebra
namespace monoid_algebra
open algebra submonoid submodule
section span
section semiring
variables [comm_semiring R] [monoid M]
/-- An element of `monoid_algebra R M` is in the subalgebra generated by its support. -/
lemma mem_adjoint_support (f : monoid_algebra R M) : f ∈ adjoin R (of R M '' f.support) :=
begin
suffices : span R (of R M '' f.support) ≤ (adjoin R (of R M '' f.support)).to_submodule,
{ exact this (mem_span_support f) },
rw submodule.span_le,
exact subset_adjoin
end
/-- If a set `S` generates, as algebra, `monoid_algebra R M`, then the set of supports of elements
of `S` generates `monoid_algebra R M`. -/
lemma support_gen_of_gen {S : set (monoid_algebra R M)} (hS : algebra.adjoin R S = ⊤) :
algebra.adjoin R (⋃ f ∈ S, (of R M '' (f.support : set M))) = ⊤ :=
begin
refine le_antisymm le_top _,
rw [← hS, adjoin_le_iff],
intros f hf,
have hincl : (of R M) '' f.support ⊆
⋃ (g : monoid_algebra R M) (H : g ∈ S), of R M '' g.support,
{ intros s hs,
exact set.mem_bUnion_iff.2 ⟨f, ⟨hf, hs⟩⟩ },
exact adjoin_mono hincl (mem_adjoint_support f)
end
/-- If a set `S` generates, as algebra, `monoid_algebra R M`, then the image of the union of the
supports of elements of `S` generates `monoid_algebra R M`. -/
lemma support_gen_of_gen' {S : set (monoid_algebra R M)} (hS : algebra.adjoin R S = ⊤) :
algebra.adjoin R (of R M '' (⋃ f ∈ S, (f.support : set M))) = ⊤ :=
begin
suffices : of R M '' (⋃ f ∈ S, (f.support : set M)) = ⋃ f ∈ S, (of R M '' (f.support : set M)),
{ rw this,
exact support_gen_of_gen hS },
simp only [set.image_Union]
end
end semiring
section ring
variables [comm_ring R] [comm_monoid M]
/-- If `monoid_algebra R M` is of finite type, there there is a `G : finset M` such that its image
generates, as algera, `monoid_algebra R M`. -/
lemma exists_finset_adjoin_eq_top [h :finite_type R (monoid_algebra R M)] :
∃ G : finset M, algebra.adjoin R (of R M '' G) = ⊤ :=
begin
unfreezingI { obtain ⟨S, hS⟩ := h },
letI : decidable_eq M := classical.dec_eq M,
use finset.bUnion S (λ f, f.support),
have : (finset.bUnion S (λ f, f.support) : set M) = ⋃ f ∈ S, (f.support : set M),
{ simp only [finset.set_bUnion_coe, finset.coe_bUnion] },
rw [this],
exact support_gen_of_gen' hS
end
/-- The image of an element `m : M` in `monoid_algebra R M` belongs the submodule generated by
`S : set M` if and only if `m ∈ S`. -/
lemma of_mem_span_of_iff [nontrivial R] {m : M} {S : set M} :
of R M m ∈ span R (of R M '' S) ↔ m ∈ S :=
begin
refine ⟨λ h, _, λ h, submodule.subset_span $ set.mem_image_of_mem (of R M) h⟩,
rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported,
finsupp.support_single_ne_zero (@one_ne_zero R _ (by apply_instance))] at h,
simpa using h
end
/--If the image of an element `m : M` in `monoid_algebra R M` belongs the submodule generated by the
closure of some `S : set M` then `m ∈ closure S`. -/
lemma mem_closure_of_mem_span_closure [nontrivial R] {m : M} {S : set M}
(h : of R M m ∈ span R (submonoid.closure (of R M '' S) : set (monoid_algebra R M))) :
m ∈ closure S :=
begin
rw ← monoid_hom.map_mclosure at h,
simpa using of_mem_span_of_iff.1 h
end
end ring
end span
variables [comm_monoid M]
/-- If a set `S` generates a monoid `M`, then the image of `M` generates, as algebra,
`monoid_algebra R M`. -/
lemma mv_polynomial_aeval_of_surjective_of_closure [comm_semiring R] {S : set M}
(hS : closure S = ⊤) : function.surjective (mv_polynomial.aeval
(λ (s : S), of R M ↑s) : mv_polynomial S R → monoid_algebra R M) :=
begin
refine λ f, induction_on f (λ m, _) _ _,
{ have : m ∈ closure S := hS.symm ▸ mem_top _,
refine closure_induction this (λ m hm, _) _ _,
{ exact ⟨mv_polynomial.X ⟨m, hm⟩, mv_polynomial.aeval_X _ _⟩ },
{ exact ⟨1, alg_hom.map_one _⟩ },
{ rintro m₁ m₂ ⟨P₁, hP₁⟩ ⟨P₂, hP₂⟩,
exact ⟨P₁ * P₂, by rw [alg_hom.map_mul, hP₁, hP₂, of_apply, of_apply, of_apply,
single_mul_single, one_mul]⟩ } },
{ rintro f g ⟨P, rfl⟩ ⟨Q, rfl⟩,
exact ⟨P + Q, alg_hom.map_add _ _ _⟩ },
{ rintro r f ⟨P, rfl⟩,
exact ⟨r • P, alg_hom.map_smul _ _ _⟩ }
end
/-- If a monoid `M` is finitely generated then `monoid_algebra R M` is of finite type. -/
instance finite_type_of_fg [comm_ring R] [monoid.fg M] : finite_type R (monoid_algebra R M) :=
(add_monoid_algebra.finite_type_of_fg R (additive M)).equiv (to_additive_alg_equiv R M).symm
/-- A monoid `M` is finitely generated if and only if `monoid_algebra R M` is of finite type. -/
lemma finite_type_iff_fg [comm_ring R] [nontrivial R] :
finite_type R (monoid_algebra R M) ↔ monoid.fg M :=
⟨λ h, monoid.fg_iff_add_fg.2 $ add_monoid_algebra.finite_type_iff_fg.1 $ h.equiv $
to_additive_alg_equiv R M, λ h, @monoid_algebra.finite_type_of_fg _ _ _ _ h⟩
/-- If `monoid_algebra R M` is of finite type then `M` is finitely generated. -/
lemma fg_of_finite_type [comm_ring R] [nontrivial R] [h : finite_type R (monoid_algebra R M)] :
monoid.fg M :=
finite_type_iff_fg.1 h
/-- A group `G` is finitely generated if and only if `add_monoid_algebra R G` is of finite type. -/
lemma finite_type_iff_group_fg {G : Type*} [comm_group G] [comm_ring R] [nontrivial R] :
finite_type R (monoid_algebra R G) ↔ group.fg G :=
by simpa [group.fg_iff_monoid.fg] using finite_type_iff_fg
end monoid_algebra
end monoid_algebra
|
eae8d0ec0f68898f7951febd42bdd01147751485 | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/multiplicity.lean | fe9116d68cdc97044241471f31cd90944a21af25 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 22,637 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Chris Hughes
-/
import algebra.associated
import algebra.big_operators.basic
import ring_theory.valuation.basic
/-!
# Multiplicity of a divisor
For a commutative monoid, this file introduces the notion of multiplicity of a divisor and proves
several basic results on it.
## Main definitions
* `multiplicity a b`: for two elements `a` and `b` of a commutative monoid returns the largest
number `n` such that `a ^ n ∣ b` or infinity, written `⊤`, if `a ^ n ∣ b` for all natural numbers
`n`.
* `multiplicity.finite a b`: a predicate denoting that the multiplicity of `a` in `b` is finite.
-/
variables {α : Type*}
open nat part
open_locale big_operators
/-- `multiplicity a b` returns the largest natural number `n` such that
`a ^ n ∣ b`, as an `part_enat` or natural with infinity. If `∀ n, a ^ n ∣ b`,
then it returns `⊤`-/
def multiplicity [monoid α] [decidable_rel ((∣) : α → α → Prop)] (a b : α) : part_enat :=
part_enat.find $ λ n, ¬a ^ (n + 1) ∣ b
namespace multiplicity
section monoid
variables [monoid α]
/-- `multiplicity.finite a b` indicates that the multiplicity of `a` in `b` is finite. -/
@[reducible] def finite (a b : α) : Prop := ∃ n : ℕ, ¬a ^ (n + 1) ∣ b
lemma finite_iff_dom [decidable_rel ((∣) : α → α → Prop)] {a b : α} :
finite a b ↔ (multiplicity a b).dom := iff.rfl
lemma finite_def {a b : α} : finite a b ↔ ∃ n : ℕ, ¬a ^ (n + 1) ∣ b := iff.rfl
lemma not_dvd_one_of_finite_one_right {a : α} : finite a 1 → ¬a ∣ 1 :=
λ ⟨n, hn⟩ ⟨d, hd⟩, hn ⟨d ^ (n + 1), (pow_mul_pow_eq_one (n + 1) hd.symm).symm⟩
@[norm_cast]
theorem int.coe_nat_multiplicity (a b : ℕ) :
multiplicity (a : ℤ) (b : ℤ) = multiplicity a b :=
begin
apply part.ext',
{ repeat { rw [← finite_iff_dom, finite_def] },
norm_cast },
{ intros h1 h2,
apply _root_.le_antisymm; { apply nat.find_mono, norm_cast, simp } }
end
lemma not_finite_iff_forall {a b : α} : (¬ finite a b) ↔ ∀ n : ℕ, a ^ n ∣ b :=
⟨λ h n, nat.cases_on n (by { rw pow_zero, exact one_dvd _ }) (by simpa [finite, not_not] using h),
by simp [finite, multiplicity, not_not]; tauto⟩
lemma not_unit_of_finite {a b : α} (h : finite a b) : ¬is_unit a :=
let ⟨n, hn⟩ := h in hn ∘ is_unit.dvd ∘ is_unit.pow (n + 1)
lemma finite_of_finite_mul_right {a b c : α} : finite a (b * c) → finite a b :=
λ ⟨n, hn⟩, ⟨n, λ h, hn (h.trans (dvd_mul_right _ _))⟩
variable [decidable_rel ((∣) : α → α → Prop)]
lemma pow_dvd_of_le_multiplicity {a b : α} {k : ℕ} :
(k : part_enat) ≤ multiplicity a b → a ^ k ∣ b :=
by { rw ← part_enat.some_eq_coe, exact
nat.cases_on k (λ _, by { rw pow_zero, exact one_dvd _ })
(λ k ⟨h₁, h₂⟩, by_contradiction (λ hk, (nat.find_min _ (lt_of_succ_le (h₂ ⟨k, hk⟩)) hk))) }
lemma pow_multiplicity_dvd {a b : α} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b :=
pow_dvd_of_le_multiplicity (by rw part_enat.coe_get)
lemma is_greatest {a b : α} {m : ℕ} (hm : multiplicity a b < m) : ¬a ^ m ∣ b :=
λ h, by rw [part_enat.lt_coe_iff] at hm; exact nat.find_spec hm.fst ((pow_dvd_pow _ hm.snd).trans h)
lemma is_greatest' {a b : α} {m : ℕ} (h : finite a b) (hm : get (multiplicity a b) h < m) :
¬a ^ m ∣ b :=
is_greatest (by rwa [← part_enat.coe_lt_coe, part_enat.coe_get] at hm)
lemma pos_of_dvd {a b : α} (hfin : finite a b) (hdiv : a ∣ b) : 0 < (multiplicity a b).get hfin :=
begin
refine zero_lt_iff.2 (λ h, _),
simpa [hdiv] using (is_greatest' hfin (lt_one_iff.mpr h)),
end
lemma unique {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) :
(k : part_enat) = multiplicity a b :=
le_antisymm (le_of_not_gt (λ hk', is_greatest hk' hk)) $
have finite a b, from ⟨k, hsucc⟩,
by { rw [part_enat.le_coe_iff], exact ⟨this, nat.find_min' _ hsucc⟩ }
lemma unique' {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬ a ^ (k + 1) ∣ b) :
k = get (multiplicity a b) ⟨k, hsucc⟩ :=
by rw [← part_enat.coe_inj, part_enat.coe_get, unique hk hsucc]
lemma le_multiplicity_of_pow_dvd {a b : α}
{k : ℕ} (hk : a ^ k ∣ b) : (k : part_enat) ≤ multiplicity a b :=
le_of_not_gt $ λ hk', is_greatest hk' hk
lemma pow_dvd_iff_le_multiplicity {a b : α}
{k : ℕ} : a ^ k ∣ b ↔ (k : part_enat) ≤ multiplicity a b :=
⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩
lemma multiplicity_lt_iff_neg_dvd {a b : α} {k : ℕ} :
multiplicity a b < (k : part_enat) ↔ ¬ a ^ k ∣ b :=
by { rw [pow_dvd_iff_le_multiplicity, not_le] }
lemma eq_coe_iff {a b : α} {n : ℕ} :
multiplicity a b = (n : part_enat) ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b :=
begin
rw [← part_enat.some_eq_coe],
exact ⟨λ h, let ⟨h₁, h₂⟩ := eq_some_iff.1 h in
h₂ ▸ ⟨pow_multiplicity_dvd _, is_greatest
(by { rw [part_enat.lt_coe_iff], exact ⟨h₁, lt_succ_self _⟩ })⟩,
λ h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩
end
lemma eq_top_iff {a b : α} :
multiplicity a b = ⊤ ↔ ∀ n : ℕ, a ^ n ∣ b :=
(part_enat.find_eq_top_iff _).trans $
by { simp only [not_not],
exact ⟨λ h n, nat.cases_on n (by { rw pow_zero, exact one_dvd _}) (λ n, h _), λ h n, h _⟩ }
@[simp] lemma is_unit_left {a : α} (b : α) (ha : is_unit a) : multiplicity a b = ⊤ :=
eq_top_iff.2 (λ _, is_unit.dvd (ha.pow _))
@[simp] lemma one_left (b : α) : multiplicity 1 b = ⊤ := is_unit_left b is_unit_one
@[simp] lemma get_one_right {a : α} (ha : finite a 1) : get (multiplicity a 1) ha = 0 :=
begin
rw [part_enat.get_eq_iff_eq_coe, eq_coe_iff, pow_zero],
simp [not_dvd_one_of_finite_one_right ha],
end
@[simp] lemma unit_left (a : α) (u : αˣ) : multiplicity (u : α) a = ⊤ :=
is_unit_left a u.is_unit
lemma multiplicity_eq_zero {a b : α} : multiplicity a b = 0 ↔ ¬a ∣ b :=
by { rw [← nat.cast_zero, eq_coe_iff], simp }
lemma eq_top_iff_not_finite {a b : α} : multiplicity a b = ⊤ ↔ ¬ finite a b :=
part.eq_none_iff'
lemma ne_top_iff_finite {a b : α} : multiplicity a b ≠ ⊤ ↔ finite a b :=
by rw [ne.def, eq_top_iff_not_finite, not_not]
lemma lt_top_iff_finite {a b : α} : multiplicity a b < ⊤ ↔ finite a b :=
by rw [lt_top_iff_ne_top, ne_top_iff_finite]
lemma exists_eq_pow_mul_and_not_dvd {a b : α} (hfin : finite a b) :
∃ (c : α), b = a ^ ((multiplicity a b).get hfin) * c ∧ ¬ a ∣ c :=
begin
obtain ⟨c, hc⟩ := multiplicity.pow_multiplicity_dvd hfin,
refine ⟨c, hc, _⟩,
rintro ⟨k, hk⟩,
rw [hk, ← mul_assoc, ← pow_succ'] at hc,
have h₁ : a ^ ((multiplicity a b).get hfin + 1) ∣ b := ⟨k, hc⟩,
exact (multiplicity.eq_coe_iff.1 (by simp)).2 h₁,
end
open_locale classical
lemma multiplicity_le_multiplicity_iff {a b c d : α} : multiplicity a b ≤ multiplicity c d ↔
(∀ n : ℕ, a ^ n ∣ b → c ^ n ∣ d) :=
⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)),
λ h, if hab : finite a b
then by rw [← part_enat.coe_get (finite_iff_dom.1 hab)];
exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _))
else
have ∀ n : ℕ, c ^ n ∣ d, from λ n, h n (not_finite_iff_forall.1 hab _),
by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2
(not_finite_iff_forall.2 this)]⟩
lemma multiplicity_eq_multiplicity_iff {a b c d : α} : multiplicity a b = multiplicity c d ↔
(∀ n : ℕ, a ^ n ∣ b ↔ c ^ n ∣ d) :=
⟨λ h n, ⟨multiplicity_le_multiplicity_iff.mp h.le n, multiplicity_le_multiplicity_iff.mp h.ge n⟩,
λ h, le_antisymm (multiplicity_le_multiplicity_iff.mpr (λ n, (h n).mp))
(multiplicity_le_multiplicity_iff.mpr (λ n, (h n).mpr))⟩
lemma multiplicity_le_multiplicity_of_dvd_right {a b c : α} (h : b ∣ c) :
multiplicity a b ≤ multiplicity a c :=
multiplicity_le_multiplicity_iff.2 $ λ n hb, hb.trans h
lemma eq_of_associated_right {a b c : α} (h : associated b c) :
multiplicity a b = multiplicity a c :=
le_antisymm (multiplicity_le_multiplicity_of_dvd_right h.dvd)
(multiplicity_le_multiplicity_of_dvd_right h.symm.dvd)
lemma dvd_of_multiplicity_pos {a b : α} (h : (0 : part_enat) < multiplicity a b) : a ∣ b :=
begin
rw ← pow_one a,
apply pow_dvd_of_le_multiplicity,
simpa only [nat.cast_one, part_enat.pos_iff_one_le] using h
end
lemma dvd_iff_multiplicity_pos {a b : α} : (0 : part_enat) < multiplicity a b ↔ a ∣ b :=
⟨dvd_of_multiplicity_pos,
λ hdvd, lt_of_le_of_ne (zero_le _) (λ heq, is_greatest
(show multiplicity a b < ↑1,
by simpa only [heq, nat.cast_zero] using part_enat.coe_lt_coe.mpr zero_lt_one)
(by rwa pow_one a))⟩
lemma finite_nat_iff {a b : ℕ} : finite a b ↔ (a ≠ 1 ∧ 0 < b) :=
begin
rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def,
not_not, not_lt, le_zero_iff],
exact ⟨λ h, or_iff_not_imp_right.2 (λ hb,
have ha : a ≠ 0, from λ ha, by simpa [ha] using h 1,
by_contradiction (λ ha1 : a ≠ 1,
have ha_gt_one : 1 < a, from
lt_of_not_ge (λ ha', by { clear h, revert ha ha1, dec_trivial! }),
not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b))
(lt_pow_self ha_gt_one b))),
λ h, by cases h; simp *⟩
end
alias dvd_iff_multiplicity_pos ↔ _ _root_.has_dvd.dvd.multiplicity_pos
end monoid
section comm_monoid
variables [comm_monoid α]
lemma finite_of_finite_mul_left {a b c : α} : finite a (b * c) → finite a c :=
by rw mul_comm; exact finite_of_finite_mul_right
variable [decidable_rel ((∣) : α → α → Prop)]
lemma is_unit_right {a b : α} (ha : ¬is_unit a) (hb : is_unit b) :
multiplicity a b = 0 :=
eq_coe_iff.2 ⟨show a ^ 0 ∣ b, by simp only [pow_zero, one_dvd],
by { rw pow_one, exact λ h, mt (is_unit_of_dvd_unit h) ha hb }⟩
lemma one_right {a : α} (ha : ¬is_unit a) : multiplicity a 1 = 0 := is_unit_right ha is_unit_one
lemma unit_right {a : α} (ha : ¬is_unit a) (u : αˣ) : multiplicity a u = 0 :=
is_unit_right ha u.is_unit
open_locale classical
lemma multiplicity_le_multiplicity_of_dvd_left {a b c : α} (hdvd : a ∣ b) :
multiplicity b c ≤ multiplicity a c :=
multiplicity_le_multiplicity_iff.2 $ λ n h, (pow_dvd_pow_of_dvd hdvd n).trans h
lemma eq_of_associated_left {a b c : α} (h : associated a b) :
multiplicity b c = multiplicity a c :=
le_antisymm (multiplicity_le_multiplicity_of_dvd_left h.dvd)
(multiplicity_le_multiplicity_of_dvd_left h.symm.dvd)
alias dvd_iff_multiplicity_pos ↔ _ _root_.has_dvd.dvd.multiplicity_pos
end comm_monoid
section monoid_with_zero
variable [monoid_with_zero α]
lemma ne_zero_of_finite {a b : α} (h : finite a b) : b ≠ 0 :=
let ⟨n, hn⟩ := h in λ hb, by simpa [hb] using hn
variable [decidable_rel ((∣) : α → α → Prop)]
@[simp] protected lemma zero (a : α) : multiplicity a 0 = ⊤ :=
part.eq_none_iff.2 (λ n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _))
@[simp] lemma multiplicity_zero_eq_zero_of_ne_zero (a : α) (ha : a ≠ 0) : multiplicity 0 a = 0 :=
multiplicity.multiplicity_eq_zero.2 $ mt zero_dvd_iff.1 ha
end monoid_with_zero
section comm_monoid_with_zero
variable [comm_monoid_with_zero α]
variable [decidable_rel ((∣) : α → α → Prop)]
lemma multiplicity_mk_eq_multiplicity [decidable_rel ((∣) : associates α → associates α → Prop)]
{a b : α} : multiplicity (associates.mk a) (associates.mk b) = multiplicity a b :=
begin
by_cases h : finite a b,
{ rw ← part_enat.coe_get (finite_iff_dom.mp h),
refine (multiplicity.unique
(show (associates.mk a)^(((multiplicity a b).get h)) ∣ associates.mk b, from _) _).symm ;
rw [← associates.mk_pow, associates.mk_dvd_mk],
{ exact pow_multiplicity_dvd h },
{ exact is_greatest ((part_enat.lt_coe_iff _ _).mpr (exists.intro
(finite_iff_dom.mp h) (nat.lt_succ_self _))) } },
{ suffices : ¬ (finite (associates.mk a) (associates.mk b)),
{ rw [finite_iff_dom, part_enat.not_dom_iff_eq_top] at h this,
rw [h, this] },
refine not_finite_iff_forall.mpr (λ n, by { rw [← associates.mk_pow, associates.mk_dvd_mk],
exact not_finite_iff_forall.mp h n }) }
end
end comm_monoid_with_zero
section semiring
variables [semiring α] [decidable_rel ((∣) : α → α → Prop)]
lemma min_le_multiplicity_add {p a b : α} :
min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) :=
(le_total (multiplicity p a) (multiplicity p b)).elim
(λ h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn))
(λ h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn)
end semiring
section ring
variables [ring α] [decidable_rel ((∣) : α → α → Prop)]
@[simp] protected lemma neg (a b : α) : multiplicity a (-b) = multiplicity a b :=
part.ext' (by simp only [multiplicity, part_enat.find, dvd_neg])
(λ h₁ h₂, part_enat.coe_inj.1 (by rw [part_enat.coe_get]; exact
eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _))
(mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _))))))
theorem int.nat_abs (a : ℕ) (b : ℤ) : multiplicity a b.nat_abs = multiplicity (a : ℤ) b :=
begin
cases int.nat_abs_eq b with h h; conv_rhs { rw h },
{ rw [int.coe_nat_multiplicity], },
{ rw [multiplicity.neg, int.coe_nat_multiplicity], },
end
lemma multiplicity_add_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a + b) = multiplicity p b :=
begin
apply le_antisymm,
{ apply part_enat.le_of_lt_add_one,
cases part_enat.ne_top_iff.mp (part_enat.ne_top_of_lt h) with k hk,
rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd], intro h_dvd,
rw [← dvd_add_iff_right] at h_dvd,
apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self,
rw [pow_dvd_iff_le_multiplicity, nat.cast_add, ← hk, nat.cast_one],
exact part_enat.add_one_le_of_lt h },
{ convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] }
end
lemma multiplicity_sub_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a - b) = multiplicity p b :=
by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] }
lemma multiplicity_add_eq_min {p a b : α} (h : multiplicity p a ≠ multiplicity p b) :
multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) :=
begin
rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab,
{ rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab },
{ contradiction },
{ rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab},
end
end ring
section cancel_comm_monoid_with_zero
variables [cancel_comm_monoid_with_zero α]
lemma finite_mul_aux {p : α} (hp : prime p) : ∀ {n m : ℕ} {a b : α},
¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b
| n m := λ a b ha hb ⟨s, hs⟩,
have p ∣ a * b, from ⟨p ^ (n + m) * s,
by simp [hs, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩,
(hp.2.2 a b this).elim
(λ ⟨x, hx⟩, have hn0 : 0 < n,
from nat.pos_of_ne_zero (λ hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha),
have wf : (n - 1) < n, from tsub_lt_self hn0 dec_trivial,
have hpx : ¬ p ^ (n - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, ha (hx.symm ▸ ⟨y, mul_right_cancel₀ hp.1
$ by rw [tsub_add_cancel_of_le (succ_le_of_lt hn0)] at hy;
simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
have 1 ≤ n + m, from le_trans hn0 (nat.le_add_right n m),
finite_mul_aux hpx hb ⟨s, mul_right_cancel₀ hp.1 begin
rw [tsub_add_eq_add_tsub (succ_le_of_lt hn0), tsub_add_cancel_of_le this],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at *
end⟩)
(λ ⟨x, hx⟩, have hm0 : 0 < m,
from nat.pos_of_ne_zero (λ hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb),
have wf : (m - 1) < m, from tsub_lt_self hm0 dec_trivial,
have hpx : ¬ p ^ (m - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, hb (hx.symm ▸ ⟨y, mul_right_cancel₀ hp.1
$ by rw [tsub_add_cancel_of_le (succ_le_of_lt hm0)] at hy;
simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
finite_mul_aux ha hpx ⟨s, mul_right_cancel₀ hp.1 begin
rw [add_assoc, tsub_add_cancel_of_le (succ_le_of_lt hm0)],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at *
end⟩)
lemma finite_mul {p a b : α} (hp : prime p) : finite p a → finite p b → finite p (a * b) :=
λ ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩
lemma finite_mul_iff {p a b : α} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b :=
⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩,
λ h, finite_mul hp h.1 h.2⟩
lemma finite_pow {p a : α} (hp : prime p) : Π {k : ℕ} (ha : finite p a), finite p (a ^ k)
| 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩
| (k+1) ha := by rw [pow_succ]; exact finite_mul hp ha (finite_pow ha)
variable [decidable_rel ((∣) : α → α → Prop)]
@[simp] lemma multiplicity_self {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) :
multiplicity a a = 1 :=
by { rw ← nat.cast_one, exact
eq_coe_iff.2 ⟨by simp, λ ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2
⟨b, mul_left_cancel₀ ha0 $ by { clear _fun_match,
simpa [pow_succ, mul_assoc] using hb }⟩)⟩ }
@[simp] lemma get_multiplicity_self {a : α} (ha : finite a a) :
get (multiplicity a a) ha = 1 :=
part_enat.get_eq_iff_eq_coe.2 (eq_coe_iff.2
⟨by simp, λ ⟨b, hb⟩,
by rw [← mul_one a, pow_add, pow_one, mul_assoc, mul_assoc,
mul_right_inj' (ne_zero_of_finite ha)] at hb;
exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha)
⟨b, by clear _fun_match; simp * at *⟩⟩)
protected lemma mul' {p a b : α} (hp : prime p)
(h : (multiplicity p (a * b)).dom) :
get (multiplicity p (a * b)) h =
get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2 :=
have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a,
from pow_multiplicity_dvd _,
have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b,
from pow_multiplicity_dvd _,
have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) =
p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 *
p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2,
by simp [pow_add],
have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b,
by rw [hpoweq]; apply mul_dvd_mul; assumption,
have hsucc : ¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b,
from λ h, by exact
not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _))
(_root_.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp hdiva hdivb h),
by rw [← part_enat.coe_inj, part_enat.coe_get, eq_coe_iff];
exact ⟨hdiv, hsucc⟩
open_locale classical
protected lemma mul {p a b : α} (hp : prime p) :
multiplicity p (a * b) = multiplicity p a + multiplicity p b :=
if h : finite p a ∧ finite p b then
by rw [← part_enat.coe_get (finite_iff_dom.1 h.1), ← part_enat.coe_get (finite_iff_dom.1 h.2),
← part_enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)),
← nat.cast_add, part_enat.coe_inj, multiplicity.mul' hp]; refl
else begin
rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)],
cases not_and_distrib.1 h with h h;
simp [eq_top_iff_not_finite.2 h]
end
lemma finset.prod {β : Type*} {p : α} (hp : prime p) (s : finset β) (f : β → α) :
multiplicity p (∏ x in s, f x) = ∑ x in s, multiplicity p (f x) :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp only [finset.sum_empty, finset.prod_empty],
convert one_right hp.not_unit },
{ simp [has, ← ih],
convert multiplicity.mul hp }
end
protected lemma pow' {p a : α} (hp : prime p) (ha : finite p a) : ∀ {k : ℕ},
get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha
| 0 := by simp [one_right hp.not_unit]
| (k+1) := have multiplicity p (a ^ (k + 1)) = multiplicity p (a * a ^ k), by rw pow_succ,
by rw [get_eq_get_of_eq _ _ this, multiplicity.mul' hp, pow', add_mul, one_mul, add_comm]
lemma pow {p a : α} (hp : prime p) : ∀ {k : ℕ},
multiplicity p (a ^ k) = k • (multiplicity p a)
| 0 := by simp [one_right hp.not_unit]
| (succ k) := by simp [pow_succ, succ_nsmul, pow, multiplicity.mul hp]
lemma multiplicity_pow_self {p : α} (h0 : p ≠ 0) (hu : ¬ is_unit p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
by { rw [eq_coe_iff], use dvd_rfl, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self }
lemma multiplicity_pow_self_of_prime {p : α} (hp : prime p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
multiplicity_pow_self hp.ne_zero hp.not_unit n
end cancel_comm_monoid_with_zero
section valuation
variables {R : Type*} [comm_ring R] [is_domain R] {p : R}
[decidable_rel (has_dvd.dvd : R → R → Prop)]
/-- `multiplicity` of a prime inan integral domain as an additive valuation to `part_enat`. -/
noncomputable def add_valuation (hp : prime p) : add_valuation R part_enat :=
add_valuation.of (multiplicity p) (multiplicity.zero _) (one_right hp.not_unit)
(λ _ _, min_le_multiplicity_add) (λ a b, multiplicity.mul hp)
@[simp]
lemma add_valuation_apply {hp : prime p} {r : R} : add_valuation hp r = multiplicity p r := rfl
end valuation
end multiplicity
section nat
open multiplicity
lemma multiplicity_eq_zero_of_coprime {p a b : ℕ} (hp : p ≠ 1)
(hle : multiplicity p a ≤ multiplicity p b)
(hab : nat.coprime a b) : multiplicity p a = 0 :=
begin
rw [multiplicity_le_multiplicity_iff] at hle,
rw [← nonpos_iff_eq_zero, ← not_lt, part_enat.pos_iff_one_le, ← nat.cast_one,
← pow_dvd_iff_le_multiplicity],
assume h,
have := nat.dvd_gcd h (hle _ h),
rw [coprime.gcd_eq_one hab, nat.dvd_one, pow_one] at this,
exact hp this
end
end nat
|
d5bd00d09e750206596227ba89b7b98788066344 | 8e6cad62ec62c6c348e5faaa3c3f2079012bdd69 | /src/geometry/manifold/mfderiv.lean | 72f358f936450ec1a4a1be8022f11f1a9fdf5e7d | [
"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 | 65,869 | 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 geometry.manifold.basic_smooth_bundle
/-!
# The derivative of functions between smooth manifolds
Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with
corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the
derivative of the function at a point, within a set or along the whole space, mimicking the API
for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and
"f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`).
## Main definitions
* `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have
at most one derivative. This technical condition is important when we define
`mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative,
and many properties will fail (for instance the chain rule). This is analogous to
`unique_diff_on 𝕜 s` in a vector space.
Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API.
* `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent
space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`.
* `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map
from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable
within `s`, this is `0`.
* `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`.
* `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s`
at `x`.
* `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`.
* `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative
within `s` at `x`.
* `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`.
* `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere.
* `tangent_map I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the
tangent bundle of `M'`.
We also establish results on the differential of the identity, constant functions, charts, extended
charts. For functions between vector spaces, we show that the usual notions and the manifold notions
coincide.
## Implementation notes
The tangent bundle is constructed using the machinery of topological fiber bundles, for which one
can define bundled morphisms and construct canonically maps from the total space of one bundle to
the total space of another one. One could use this mechanism to construct directly the derivative
of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map
is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the
details of the definition of the total space of a fiber bundle constructed from core, to cook up a
suitable definition of the derivative. It is the following: at each point, we have a preferred chart
(used to identify the fiber above the point with the model vector space in fiber bundles). Then one
should read the function using these preferred charts at `x` and `f x`, and take the derivative
of `f` in these charts.
Due to the fact that we are working in a model with corners, with an additional embedding `I` of the
model space `H` in the model vector space `E`, the charts taking values in `E` are not the original
charts of the manifold, but those ones composed with `I`, called extended charts. We define
`written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then
the manifold derivative of `f`, at `x`, is just the usual derivative of `written_in_ext_chart I I' x
f`, at the point `(ext_chart_at I x) x`.
There is a subtelty with respect to continuity: if the function is not continuous, then the image
of a small open set around `x` will not be contained in the source of the preferred chart around
`f x`, which means that when reading `f` in the chart one is losing some information. To avoid this,
we include continuity in the definition of differentiablity (which is reasonable since with any
definition, differentiability implies continuity).
*Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose
that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its
restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property
`mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined
(what values would one choose for vectors that are transverse to `N`?), which can create issues down
the road. The problem here is that knowing the value of `f` along `N` does not determine the
differential of `f` in all directions. This is in contrast to the case where `N` would be an open
subset, or a submanifold with boundary of maximal dimension, where this issue does not appear.
The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists,
and is an assumption in most statements requiring a form of uniqueness.
On a vector space, the manifold derivative and the usual derivative are equal. This means in
particular that they live on the same space, i.e., the tangent space is defeq to the original vector
space. To get this property is a motivation for our definition of the tangent space as a single
copy of the vector space, instead of more usual definitions such as the space of derivations, or
the space of equivalence classes of smooth curves in the manifold.
## Tags
Derivative, manifold
-/
noncomputable theory
open_locale classical topological_space manifold
open set
universe u
section derivatives_definitions
/-!
### Derivative of maps between manifolds
The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear
map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined
the tangent space using one specific chart, the formula for the derivative is written in terms of
this specific chart.
We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold".
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [charted_space H' M']
/-- Predicate ensuring that, at a point and within a set, a function can have at most one
derivative. This is expressed using the preferred chart at the considered point. -/
def unique_mdiff_within_at (s : set M) (x : M) :=
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/
def unique_mdiff_on (s : set M) :=
∀x∈s, unique_mdiff_within_at I s x
/-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative
of `f` will just be the derivative of this conjugated function. -/
@[simp, mfld_simps] def written_in_ext_chart_at (x : M) (f : M → M') : E → E' :=
(ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm
/-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds
has a derivative at the point `x` within the set `s`.
This is a generalization of `differentiable_within_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) :=
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds
has a derivative at the point `x`.
This is a generalization of `differentiable_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_at (f : M → M') (x : M) :=
continuous_at f x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I)
((ext_chart_at I x) x)
/-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds
has a derivative within `s` at all points of `s`.
This is a generalization of `differentiable_on` to manifolds. -/
def mdifferentiable_on (f : M → M') (s : set M) :=
∀x ∈ s, mdifferentiable_within_at I I' f s x
/-- `mdifferentiable I I' f` indicates that the function `f` between manifolds
has a derivative everywhere.
This is a generalization of `differentiable` to manifolds. -/
def mdifferentiable (f : M → M') :=
∀x, mdifferentiable_at I I' f x
/-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/
def local_homeomorph.mdifferentiable (f : local_homeomorph M M') :=
(mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
/-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds
has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`).
The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_within_at (f : M → M') (s : set M) (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_within_at f s x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f'
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds
has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
We require continuity in the definition, as otherwise points close to `x` `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_at (f : M → M') (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_at f x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I)
((ext_chart_at I x) x)
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the
derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the
tangent space at `f x`. -/
def mfderiv_within (f : M → M') (s : set M) (x : M) :
tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if h : mdifferentiable_within_at I I' f s x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) : _)
else 0
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of
`f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at
`f x`. -/
def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if h : mdifferentiable_at I I' f x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I)
((ext_chart_at I x) x) : _)
else 0
/-- The derivative within a set, as a map between the tangent bundles -/
def tangent_map_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
/-- The derivative, as a map between the tangent bundles -/
def tangent_map (f : M → M') : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
end derivatives_definitions
section derivatives_properties
/-! ### Unique differentiability sets in manifolds -/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M] --
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{E'' : Type*} [normed_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [charted_space H'' M'']
{f f₀ f₁ : M → M'}
{x : M}
{s t : set M}
{g : M' → M''}
{u : set M'}
lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x :=
begin
unfold unique_mdiff_within_at,
simp only [preimage_univ, univ_inter],
exact I.unique_diff _ (mem_range_self _)
end
variable {I}
lemma unique_mdiff_within_at_iff {s : set M} {x : M} :
unique_mdiff_within_at I s x ↔
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target)
((ext_chart_at I x) x) :=
begin
apply unique_diff_within_at_congr,
rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_target_eq]
end
lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) :
unique_mdiff_within_at I t x :=
unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _)
lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝[s] x) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq],
exact unique_diff_within_at.inter' hs (ext_chart_preimage_mem_nhds_within I x ht)
end
lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq],
exact unique_diff_within_at.inter hs (ext_chart_preimage_mem_nhds I x ht)
end
lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x :=
begin
have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (mem_nhds_sets hs xs),
rwa univ_inter at this
end
lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) :
unique_mdiff_on I (s ∩ t) :=
λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (mem_nhds_sets ht hx.2)
lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s :=
λx hx, is_open.unique_mdiff_within_at hx hs
lemma unique_mdiff_on_univ : unique_mdiff_on I (univ : set M) :=
is_open_univ.unique_mdiff_on
/- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are
necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we
want to include them or omit them when necessary. -/
variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M']
[I''s : smooth_manifold_with_corners I'' M'']
{f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)}
{g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))}
/-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/
theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') :
f' = f₁' :=
U.eq h.2 h₁.2
theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') :
f' = f₁' :=
unique_mdiff_within_at.eq (U _ hx) h h₁
/-!
### General lemmas on derivatives of functions between manifolds
We mimick the API for functions between vector spaces
-/
lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} :
mdifferentiable_within_at I I' f s x ↔
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) :=
begin
refine and_congr iff.rfl (exists_congr $ λ f', _),
rw [inter_comm],
simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_target_eq]
end
include Is I's
lemma mfderiv_within_zero_of_not_mdifferentiable_within_at
(h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 :=
by simp only [mfderiv_within, h, dif_neg, not_false_iff]
lemma mfderiv_zero_of_not_mdifferentiable_at
(h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 :=
by simp only [mfderiv, h, dif_neg, not_false_iff]
theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) :
has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_within_at.mono h.1 hst,
has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
theorem has_mfderiv_at.has_mfderiv_within_at
(h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩
lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') :
mdifferentiable_within_at I I' f s x :=
⟨h.1, ⟨f', h.2⟩⟩
lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') :
mdifferentiable_at I I' f x :=
⟨h.1, ⟨f', h.2⟩⟩
@[simp, mfld_simps] lemma has_mfderiv_within_at_univ :
has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' :=
by simp only [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] with mfld_simps
theorem has_mfderiv_at_unique
(h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' :=
begin
rw ← has_mfderiv_within_at_univ at h₀ h₁,
exact (unique_mdiff_within_at_univ I).eq h₀ h₁
end
lemma has_mfderiv_within_at_inter' (h : t ∈ 𝓝[s] x) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq,
has_fderiv_within_at_inter', continuous_within_at_inter' h],
exact ext_chart_preimage_mem_nhds_within I x h,
end
lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq,
has_fderiv_within_at_inter, continuous_within_at_inter h],
exact ext_chart_preimage_mem_nhds I x h,
end
lemma has_mfderiv_within_at.union
(hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') :
has_mfderiv_within_at I I' f (s ∪ t) x f' :=
begin
split,
{ exact continuous_within_at.union hs.1 ht.1 },
{ convert has_fderiv_within_at.union hs.2 ht.2,
simp only [union_inter_distrib_right, preimage_union] }
end
lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f')
(ht : s ∈ 𝓝[t] x) : has_mfderiv_within_at I I' f t x f' :=
(has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _))
lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f')
(hs : s ∈ 𝓝 x) :
has_mfderiv_at I I' f x f' :=
by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h
lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) :
has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) :=
begin
refine ⟨h.1, _⟩,
simp only [mfderiv_within, h, dif_pos] with mfld_simps,
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) :
(mfderiv_within I I' f s x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) :=
by simp only [mfderiv_within, h, dif_pos]
lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) :
has_mfderiv_at I I' f x (mfderiv I I' f x) :=
begin
refine ⟨h.1, _⟩,
simp only [mfderiv, h, dif_pos] with mfld_simps,
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) :
(mfderiv I I' f x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) :=
by simp only [mfderiv, h, dif_pos]
lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') :
mfderiv I I' f x = f' :=
by { ext, rw has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at }
lemma has_mfderiv_within_at.mfderiv_within
(h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = f' :=
by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at }
lemma mdifferentiable.mfderiv_within
(h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = mfderiv I I' f x :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact h.has_mfderiv_at.has_mfderiv_within_at
end
lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x)
(h : mdifferentiable_within_at I I' f t x) :
mfderiv_within I I' f s x = mfderiv_within I I' f t x :=
((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs
omit Is I's
lemma mdifferentiable_within_at.mono (hst : s ⊆ t)
(h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x :=
⟨ continuous_within_at.mono h.1 hst,
differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
lemma mdifferentiable_within_at_univ :
mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x :=
by simp only [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ]
with mfld_simps
lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq,
differentiable_within_at_inter, continuous_within_at_inter ht],
exact ext_chart_preimage_mem_nhds I x ht
end
lemma mdifferentiable_within_at_inter' (ht : t ∈ 𝓝[s] x) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq,
differentiable_within_at_inter', continuous_within_at_inter' ht],
exact ext_chart_preimage_mem_nhds_within I x ht
end
lemma mdifferentiable_at.mdifferentiable_within_at
(h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x :=
mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h)
lemma mdifferentiable_within_at.mdifferentiable_at
(h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h,
end
lemma mdifferentiable_on.mono
(h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s :=
λx hx, (h x (st hx)).mono st
lemma mdifferentiable_on_univ :
mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f :=
by { simp only [mdifferentiable_on, mdifferentiable_within_at_univ] with mfld_simps, refl }
lemma mdifferentiable.mdifferentiable_on
(h : mdifferentiable I I' f) : mdifferentiable_on I I' f s :=
(mdifferentiable_on_univ.2 h).mono (subset_univ _)
lemma mdifferentiable_on_of_locally_mdifferentiable_on
(h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) :
mdifferentiable_on I I' f s :=
begin
assume x xs,
rcases h x xs with ⟨t, t_open, xt, ht⟩,
exact (mdifferentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩)
end
include Is I's
@[simp, mfld_simps] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f :=
begin
ext x : 1,
simp only [mfderiv_within, mfderiv] with mfld_simps,
rw mdifferentiable_within_at_univ
end
lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x :=
by rw [mfderiv_within, mfderiv_within, ext_chart_preimage_inter_eq,
mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_preimage_mem_nhds I x ht) hs]
omit Is I's
/-! ### Deriving continuity from differentiability on manifolds -/
theorem has_mfderiv_within_at.continuous_within_at
(h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x :=
h.1
theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') :
continuous_at f x :=
h.1
lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) :
continuous_within_at f s x :=
h.1
lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x :=
h.1
lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s :=
λx hx, (h x hx).continuous_within_at
lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f :=
continuous_iff_continuous_at.2 $ λx, (h x).continuous_at
include Is I's
lemma tangent_map_within_subset {p : tangent_bundle I M}
(st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) :
tangent_map_within I I' f s p = tangent_map_within I I' f t p :=
begin
simp only [tangent_map_within] with mfld_simps,
rw mfderiv_within_subset st hs h,
end
lemma tangent_map_within_univ :
tangent_map_within I I' f univ = tangent_map I I' f :=
by { ext p : 1, simp only [tangent_map_within, tangent_map] with mfld_simps }
lemma tangent_map_within_eq_tangent_map {p : tangent_bundle I M}
(hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) :
tangent_map_within I I' f s p = tangent_map I I' f p :=
begin
rw ← mdifferentiable_within_at_univ at h,
rw ← tangent_map_within_univ,
exact tangent_map_within_subset (subset_univ _) hs h,
end
@[simp, mfld_simps] lemma tangent_map_within_tangent_bundle_proj {p : tangent_bundle I M} :
tangent_bundle.proj I' M' (tangent_map_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl
@[simp, mfld_simps] lemma tangent_map_within_proj {p : tangent_bundle I M} :
(tangent_map_within I I' f s p).1 = f p.1 := rfl
@[simp, mfld_simps] lemma tangent_map_tangent_bundle_proj {p : tangent_bundle I M} :
tangent_bundle.proj I' M' (tangent_map I I' f p) = f (tangent_bundle.proj I M p) := rfl
@[simp, mfld_simps] lemma tangent_map_proj {p : tangent_bundle I M} :
(tangent_map I I' f p).1 = f p.1 := rfl
omit Is I's
/-! ### Congruence lemmas for derivatives on manifolds -/
lemma has_mfderiv_within_at.congr_of_eventually_eq (h : has_mfderiv_within_at I I' f s x f')
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' :=
begin
refine ⟨continuous_within_at.congr_of_eventually_eq h.1 h₁ hx, _⟩,
apply has_fderiv_within_at.congr_of_eventually_eq h.2,
{ have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
ext_chart_preimage_mem_nhds_within I x h₁,
apply filter.mem_sets_of_superset this (λy, _),
simp only [hx] with mfld_simps {contextual := tt} },
{ simp only [hx] with mfld_simps },
end
lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f')
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
has_mfderiv_within_at I I' f₁ t x f' :=
(h.mono h₁).congr_of_eventually_eq (filter.mem_inf_sets_of_right ht) hx
lemma has_mfderiv_at.congr_of_eventually_eq (h : has_mfderiv_at I I' f x f')
(h₁ : f₁ =ᶠ[𝓝 x] f) : has_mfderiv_at I I' f₁ x f' :=
begin
rw ← has_mfderiv_within_at_univ at ⊢ h,
apply h.congr_of_eventually_eq _ (mem_of_nhds h₁ : _),
rwa nhds_within_univ
end
include Is I's
lemma mdifferentiable_within_at.congr_of_eventually_eq
(h : mdifferentiable_within_at I I' f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f)
(hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(h.has_mfderiv_within_at.congr_of_eventually_eq h₁ hx).mdifferentiable_within_at
variables (I I')
lemma filter.eventually_eq.mdifferentiable_within_at_iff
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x :=
begin
split,
{ assume h,
apply h.congr_of_eventually_eq h₁ hx },
{ assume h,
apply h.congr_of_eventually_eq _ hx.symm,
apply h₁.mono,
intro y,
apply eq.symm }
end
variables {I I'}
lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
mdifferentiable_within_at I I' f₁ t x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at
lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx
(subset.refl _)).mdifferentiable_within_at
lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x)
(h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t :=
λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁
lemma mdifferentiable_at.congr_of_eventually_eq (h : mdifferentiable_at I I' f x)
(hL : f₁ =ᶠ[𝓝 x] f) : mdifferentiable_at I I' f₁ x :=
((h.has_mfderiv_at).congr_of_eventually_eq hL).mdifferentiable_at
lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x)
(hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) :
mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt
lemma filter.eventually_eq.mfderiv_within_eq (hs : unique_mdiff_within_at I s x)
(hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) :=
begin
by_cases h : mdifferentiable_within_at I I' f s x,
{ exact ((h.has_mfderiv_within_at).congr_of_eventually_eq hL hx).mfderiv_within hs },
{ unfold mfderiv_within,
rw [dif_neg h, dif_neg],
rwa ← hL.mdifferentiable_within_at_iff I I' hx }
end
lemma mfderiv_within_congr (hs : unique_mdiff_within_at I s x)
(hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) :
mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) :=
filter.eventually_eq.mfderiv_within_eq hs (filter.eventually_eq_of_mem (self_mem_nhds_within) hL) hx
lemma tangent_map_within_congr (h : ∀ x ∈ s, f x = f₁ x)
(p : tangent_bundle I M) (hp : p.1 ∈ s) (hs : unique_mdiff_within_at I s p.1) :
tangent_map_within I I' f s p = tangent_map_within I I' f₁ s p :=
begin
simp only [tangent_map_within, h p.fst hp, true_and, eq_self_iff_true, heq_iff_eq,
sigma.mk.inj_iff],
congr' 1,
exact mfderiv_within_congr hs h (h _ hp)
end
lemma filter.eventually_eq.mfderiv_eq (hL : f₁ =ᶠ[𝓝 x] f) :
mfderiv I I' f₁ x = (mfderiv I I' f x : _) :=
begin
have A : f₁ x = f x := (mem_of_nhds hL : _),
rw [← mfderiv_within_univ, ← mfderiv_within_univ],
rw ← nhds_within_univ at hL,
exact hL.mfderiv_within_eq (unique_mdiff_within_at_univ I) A
end
/-! ### Composition lemmas -/
omit Is I's
lemma written_in_ext_chart_comp (h : continuous_within_at f s x) :
{y | written_in_ext_chart_at I I'' x (g ∘ f) y
= ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y}
∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
begin
apply @filter.mem_sets_of_superset _ _
((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _
(ext_chart_preimage_mem_nhds_within I x
(h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
mfld_set_tac,
end
variable (x)
include Is I's I''s
theorem has_mfderiv_within_at.comp
(hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f')
(hst : s ⊆ f ⁻¹' u) :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩,
have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘
(written_in_ext_chart_at I I' x f))
(continuous_linear_map.comp g' f' : E →L[𝕜] E'')
((ext_chart_at I x).symm ⁻¹' s ∩ range (I))
((ext_chart_at I x) x),
{ have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source)
∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
(ext_chart_preimage_mem_nhds_within I x
(hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
unfold has_mfderiv_within_at at *,
rw [← has_fderiv_within_at_inter' this, ← ext_chart_preimage_inter_eq] at hf ⊢,
have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x)
= (ext_chart_at I' (f x)) (f x),
by simp only with mfld_simps,
rw ← this at hg,
apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _,
assume y hy,
simp only with mfld_simps at hy,
have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1,
simp only [hy, this] with mfld_simps },
apply A.congr_of_eventually_eq (written_in_ext_chart_comp hf.1),
simp only with mfld_simps
end
/-- The chain rule. -/
theorem has_mfderiv_at.comp
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') :
has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
theorem has_mfderiv_at.comp_has_mfderiv_within_at
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
lemma mdifferentiable_within_at.comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x :=
begin
rcases hf.2 with ⟨f', hf'⟩,
have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩,
rcases hg.2 with ⟨g', hg'⟩,
have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩,
exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at
end
lemma mdifferentiable_at.comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mdifferentiable_at I I'' (g ∘ f) x :=
(hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at
lemma mfderiv_within_comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I'' (g ∘ f) s x =
(mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h
end
lemma mfderiv_comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) :=
begin
apply has_mfderiv_at.mfderiv,
exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at
end
lemma mdifferentiable_on.comp
(hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) :
mdifferentiable_on I I'' (g ∘ f) s :=
λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st
lemma mdifferentiable.comp
(hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) :=
λx, mdifferentiable_at.comp x (hg (f x)) (hf x)
lemma tangent_map_within_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1)
(h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) :
tangent_map_within I I'' (g ∘ f) s p =
tangent_map_within I' I'' g u (tangent_map_within I I' f s p) :=
begin
simp only [tangent_map_within] with mfld_simps,
rw mfderiv_within_comp p.1 hg hf h hps,
refl
end
lemma tangent_map_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) :
tangent_map I I'' (g ∘ f) p = tangent_map I' I'' g (tangent_map I I' f p) :=
begin
simp only [tangent_map] with mfld_simps,
rw mfderiv_comp p.1 hg hf,
refl
end
lemma tangent_map_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) :
tangent_map I I'' (g ∘ f) = (tangent_map I' I'' g) ∘ (tangent_map I I' f) :=
by { ext p : 1, exact tangent_map_comp_at _ (hg _) (hf _) }
end derivatives_properties
section specific_functions
/-! ### Differentiability of specific functions -/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
{s : set M} {x : M}
section id
/-! #### Identity -/
lemma has_mfderiv_at_id (x : M) :
has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
refine ⟨continuous_id.continuous_at, _⟩,
have : ∀ᶠ y in 𝓝[range I] ((ext_chart_at I x) x),
((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y,
{ apply filter.mem_sets_of_superset (ext_chart_at_target_mem_nhds_within I x),
mfld_set_tac },
apply has_fderiv_within_at.congr_of_eventually_eq (has_fderiv_within_at_id _ _) this,
simp only with mfld_simps
end
theorem has_mfderiv_within_at_id (s : set M) (x : M) :
has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
(has_mfderiv_at_id I x).has_mfderiv_within_at
lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x :=
(has_mfderiv_at_id I x).mdifferentiable_at
lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x :=
(mdifferentiable_at_id I).mdifferentiable_within_at
lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) :=
λx, mdifferentiable_at_id I
lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s :=
(mdifferentiable_id I).mdifferentiable_on
@[simp, mfld_simps] lemma mfderiv_id :
mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_id I x)
lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs,
exact mfderiv_id I
end
@[simp, mfld_simps] lemma tangent_map_id : tangent_map I I (id : M → M) = id :=
by { ext1 ⟨x, v⟩, simp [tangent_map] }
lemma tangent_map_within_id {p : tangent_bundle I M}
(hs : unique_mdiff_within_at I s (tangent_bundle.proj I M p)) :
tangent_map_within I I (id : M → M) s p = p :=
begin
simp only [tangent_map_within, id.def],
rw mfderiv_within_id,
{ rcases p, refl },
{ exact hs }
end
end id
section const
/-! #### Constants -/
variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M']
{c : M'}
lemma has_mfderiv_at_const (c : M') (x : M) :
has_mfderiv_at I I' (λy : M, c) x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
begin
refine ⟨continuous_const.continuous_at, _⟩,
have : (ext_chart_at I' c) ∘ (λ (y : M), c) ∘ (ext_chart_at I x).symm =
(λy, (ext_chart_at I' c) c) := rfl,
rw [written_in_ext_chart_at, this],
apply has_fderiv_within_at_const
end
theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) :
has_mfderiv_within_at I I' (λy : M, c) s x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
(has_mfderiv_at_const I I' c x).has_mfderiv_within_at
lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x :=
(has_mfderiv_at_const I I' c x).mdifferentiable_at
lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x :=
(mdifferentiable_at_const I I').mdifferentiable_within_at
lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) :=
λx, mdifferentiable_at_const I I'
lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s :=
(mdifferentiable_const I I').mdifferentiable_on
@[simp, mfld_simps] lemma mfderiv_const : mfderiv I I' (λy : M, c) x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x)
lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' (λy : M, c) s x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
begin
rw mdifferentiable.mfderiv_within (mdifferentiable_at_const I I') hxs,
{ exact mfderiv_const I I' },
{ apply_instance }
end
end const
section model_with_corners
/-! #### Model with corners -/
lemma model_with_corners.mdifferentiable :
mdifferentiable I (model_with_corners_self 𝕜 E) I :=
begin
simp only [mdifferentiable, mdifferentiable_at] with mfld_simps,
assume x,
refine ⟨I.continuous.continuous_at, _⟩,
have : differentiable_within_at 𝕜 id (range I) (I x) :=
differentiable_at_id.differentiable_within_at,
apply this.congr,
{ simp only with mfld_simps {contextual := tt} },
{ simp only with mfld_simps }
end
lemma model_with_corners.mdifferentiable_on_symm :
mdifferentiable_on (model_with_corners_self 𝕜 E) I I.symm (range I) :=
begin
simp only [mdifferentiable_on, mdifferentiable_within_at] with mfld_simps,
assume x hx,
refine ⟨I.continuous_symm.continuous_at.continuous_within_at, _⟩,
have : differentiable_within_at 𝕜 id (range I) x := differentiable_at_id.differentiable_within_at,
apply this.congr,
{ simp only with mfld_simps {contextual := tt} },
{ simp only [hx] with mfld_simps }
end
end model_with_corners
section charts
variable {e : local_homeomorph M H}
lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I e x :=
begin
refine ⟨(e.continuous_on x hx).continuous_at (mem_nhds_sets e.open_source hx), _⟩,
have mem : I ((chart_at H x : M → H) x) ∈
I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I,
by simp only [hx] with mfld_simps,
have : (chart_at H x).symm.trans e ∈ times_cont_diff_groupoid ∞ I :=
has_groupoid.compatible _ (chart_mem_atlas H x) h,
have A : times_cont_diff_on 𝕜 ∞
(I ∘ ((chart_at H x).symm.trans e) ∘ I.symm)
(I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) :=
this.1,
have B := A.differentiable_on le_top (I ((chart_at H x : M → H) x)) mem,
simp only with mfld_simps at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa only with mfld_simps },
{ apply mem_nhds_sets ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1 }
end
lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) :
mdifferentiable_on I I e e.source :=
λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at
lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) :
mdifferentiable_at I I e.symm x :=
begin
refine ⟨(e.continuous_on_symm x hx).continuous_at (mem_nhds_sets e.open_target hx), _⟩,
have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I),
by simp only [hx] with mfld_simps,
have : e.symm.trans (chart_at H (e.symm x)) ∈ times_cont_diff_groupoid ∞ I :=
has_groupoid.compatible _ h (chart_mem_atlas H _),
have A : times_cont_diff_on 𝕜 ∞
(I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm)
(I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) :=
this.1,
have B := A.differentiable_on le_top (I x) mem,
simp only with mfld_simps at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa only with mfld_simps },
{ apply (mem_nhds_sets ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1) }
end
lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) :
mdifferentiable_on I I e.symm e.target :=
λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at
lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I :=
⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩
lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I :=
mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)
/-- The derivative of the chart at a base point is the chart of the tangent bundle, composed with
the identification between the tangent bundle of the model space and the product space. -/
lemma tangent_map_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) :
tangent_map I I (chart_at H p.1) q =
(equiv.sigma_equiv_prod _ _).symm
((chart_at (model_prod H E) p : tangent_bundle I M → model_prod H E) q) :=
begin
dsimp [tangent_map],
rw mdifferentiable_at.mfderiv,
{ refl },
{ exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h }
end
/-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the
tangent bundle, composed with the identification between the tangent bundle of the model space and
the product space. -/
lemma tangent_map_chart_symm {p : tangent_bundle I M} {q : tangent_bundle I H}
(h : q.1 ∈ (chart_at H p.1).target) :
tangent_map I I (chart_at H p.1).symm q =
((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I M)
((equiv.sigma_equiv_prod H E) q) :=
begin
dsimp only [tangent_map],
rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h),
-- a trivial instance is needed after the rewrite, handle it right now.
rotate, { apply_instance },
simp only [chart_at, basic_smooth_bundle_core.chart, subtype.coe_mk, tangent_bundle_core, h,
basic_smooth_bundle_core.to_topological_fiber_bundle_core, equiv.sigma_equiv_prod_apply]
with mfld_simps,
end
end charts
end specific_functions
section mfderiv_fderiv
/-!
### Relations between vector space derivative and manifold derivative
The manifold derivative `mfderiv`, when considered on the model vector space with its trivial
manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove
this and related statements.
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{f : E → E'} {s : set E} {x : E}
lemma unique_mdiff_within_at_iff_unique_diff_within_at :
unique_mdiff_within_at (model_with_corners_self 𝕜 E) s x ↔ unique_diff_within_at 𝕜 s x :=
by simp only [unique_mdiff_within_at] with mfld_simps
lemma unique_mdiff_on_iff_unique_diff_on :
unique_mdiff_on (model_with_corners_self 𝕜 E) s ↔ unique_diff_on 𝕜 s :=
by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at]
@[simp, mfld_simps] lemma written_in_ext_chart_model_space :
written_in_ext_chart_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') x f = f :=
by { ext y, simp only with mfld_simps }
/-- For maps between vector spaces, `mdifferentiable_within_at` and `fdifferentiable_within_at`
coincide -/
theorem mdifferentiable_within_at_iff_differentiable_within_at :
mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x
↔ differentiable_within_at 𝕜 f s x :=
begin
simp only [mdifferentiable_within_at] with mfld_simps,
exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩
end
/-- For maps between vector spaces, `mdifferentiable_at` and `differentiable_at` coincide -/
theorem mdifferentiable_at_iff_differentiable_at :
mdifferentiable_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x
↔ differentiable_at 𝕜 f x :=
begin
simp only [mdifferentiable_at, differentiable_within_at_univ] with mfld_simps,
exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩
end
/-- For maps between vector spaces, `mdifferentiable_on` and `differentiable_on` coincide -/
theorem mdifferentiable_on_iff_differentiable_on :
mdifferentiable_on (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s
↔ differentiable_on 𝕜 f s :=
by simp only [mdifferentiable_on, differentiable_on,
mdifferentiable_within_at_iff_differentiable_within_at]
/-- For maps between vector spaces, `mdifferentiable` and `differentiable` coincide -/
theorem mdifferentiable_iff_differentiable :
mdifferentiable (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f
↔ differentiable 𝕜 f :=
by simp only [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at]
/-- For maps between vector spaces, `mfderiv_within` and `fderiv_within` coincide -/
theorem mfderiv_within_eq_fderiv_within :
mfderiv_within (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x
= fderiv_within 𝕜 f s x :=
begin
by_cases h :
mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x,
{ simp only [mfderiv_within, h, dif_pos] with mfld_simps },
{ simp only [mfderiv_within, h, dif_neg, not_false_iff],
rw [mdifferentiable_within_at_iff_differentiable_within_at,
differentiable_within_at] at h,
change ¬(∃(f' : tangent_space (model_with_corners_self 𝕜 E) x →L[𝕜]
tangent_space (model_with_corners_self 𝕜 E') (f x)),
has_fderiv_within_at f f' s x) at h,
simp only [fderiv_within, h, dif_neg, not_false_iff] }
end
/-- For maps between vector spaces, `mfderiv` and `fderiv` coincide -/
theorem mfderiv_eq_fderiv :
mfderiv (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x = fderiv 𝕜 f x :=
begin
rw [← mfderiv_within_univ, ← fderiv_within_univ],
exact mfderiv_within_eq_fderiv_within
end
end mfderiv_fderiv
/-! ### Differentiable local homeomorphisms -/
namespace local_homeomorph.mdifferentiable
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [charted_space H M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{E'' : Type*} [normed_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [charted_space H'' M'']
{e : local_homeomorph M M'} (he : e.mdifferentiable I I')
{e' : local_homeomorph M' M''}
include he
lemma symm : e.symm.mdifferentiable I' I :=
⟨he.2, he.1⟩
protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I' e x :=
(he.1 x hx).mdifferentiable_at (mem_nhds_sets e.open_source hx)
lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) :
mdifferentiable_at I' I e.symm x :=
(he.2 x hx).mdifferentiable_at (mem_nhds_sets e.open_target hx)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
[smooth_manifold_with_corners I'' M'']
lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) :
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) =
continuous_linear_map.id 𝕜 (tangent_space I x) :=
begin
have : (mfderiv I I (e.symm ∘ e) x) =
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) :=
mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx),
rw ← this,
have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I,
rw ← this,
apply filter.eventually_eq.mfderiv_eq,
have : e.source ∈ 𝓝 x := mem_nhds_sets e.open_source hx,
exact filter.mem_sets_of_superset this (by mfld_set_tac)
end
lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) :
(mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) =
continuous_linear_map.id 𝕜 (tangent_space I' x) :=
he.symm.symm_comp_deriv hx
/-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence
between the tangent spaces at `x` and `e x`. -/
protected def mfderiv {x : M} (hx : x ∈ e.source) :
tangent_space I x ≃L[𝕜] tangent_space I' (e x) :=
{ inv_fun := (mfderiv I' I e.symm (e x)),
continuous_to_fun := (mfderiv I I' e x).cont,
continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont,
left_inv := λy, begin
have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl,
conv_rhs { rw [← this, ← he.symm_comp_deriv hx] },
refl
end,
right_inv := λy, begin
have : (continuous_linear_map.id 𝕜 _ :
tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl,
conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] },
rw e.left_inv hx,
refl
end,
.. mfderiv I I' e x }
lemma mfderiv_bijective {x : M} (hx : x ∈ e.source) :
function.bijective (mfderiv I I' e x) :=
(he.mfderiv hx).bijective
lemma mfderiv_surjective {x : M} (hx : x ∈ e.source) :
function.surjective (mfderiv I I' e x) :=
(he.mfderiv hx).surjective
lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) :
range (mfderiv I I' e x) = univ :=
(he.mfderiv_surjective hx).range_eq
lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' :=
begin
split,
{ assume x hx,
simp only with mfld_simps at hx,
exact ((he'.mdifferentiable_at hx.2).comp _
(he.mdifferentiable_at hx.1)).mdifferentiable_within_at },
{ assume x hx,
simp only with mfld_simps at hx,
exact ((he.symm.mdifferentiable_at hx.2).comp _
(he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at }
end
end local_homeomorph.mdifferentiable
/-! ### Unique derivative sets in manifolds -/
section unique_mdiff
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{s : set M}
/-- If a set has the unique differential property, then its image under a local
diffeomorphism also has the unique differential property. -/
lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M']
(hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') :
unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) :=
begin
/- Start from a point `x` in the image, and let `z` be its preimage. Then the unique
derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique
derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that
the composition of these two charts with `e` is a local diffeomorphism in vector spaces,
and therefore preserves the unique differential property thanks to lemma
`has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto
derivative preserves the unique derivative property.-/
assume x hx,
let z := e.symm x,
have z_source : z ∈ e.source, by simp only [hx.1] with mfld_simps,
have zx : e z = x, by simp only [z, hx.1] with mfld_simps,
let F := ext_chart_at I z,
-- the unique derivative property at `z` is expressed through its preferred chart,
-- that we call `F`.
have B : unique_diff_within_at 𝕜
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z),
{ have : unique_mdiff_within_at I s z := hs _ hx.2,
have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z,
{ apply mem_nhds_sets,
apply e.continuous_on.preimage_open_of_open e.open_source (ext_chart_at_open_source I' x),
simp only [z_source, zx] with mfld_simps },
have := this.inter S,
rw [unique_mdiff_within_at_iff] at this,
exact this },
-- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and
-- of `e`
let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x),
-- `G` is differentiable
have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I',
{ have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z),
have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x),
exact A.symm.trans (he.trans B) },
have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source,
by simp only [z_source, zx] with mfld_simps,
have A : differentiable_within_at 𝕜 G (range I) (F z),
{ refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _;
simp only [G, F] with mfld_simps },
-- let `G'` be its derivative
let G' := fderiv_within 𝕜 G (range I) (F z),
have D₁ : has_fderiv_within_at G G' (range I) (F z) :=
A.has_fderiv_within_at,
have D₂ : has_fderiv_within_at G G'
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z) :=
D₁.mono (by mfld_set_tac),
-- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition
-- of the two charts and of `e`.
have C : dense_range (G' : E → E'),
{ have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x))
((chart_at H z : M → H) z),
by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl },
rw this,
exact (Diff.mfderiv_surjective Mmem).dense_range },
-- key step: thanks to what we have proved about it, `G` preserves the unique derivative property
have key : unique_diff_within_at 𝕜
(G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target))
(G (F z)) := D₂.unique_diff_within_at B C,
have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp only [hx.1] with mfld_simps },
rw this at key,
apply key.mono,
show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆
(ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩
range (I'),
rw image_subset_iff,
mfld_set_tac
end
/-- If a set in a manifold has the unique derivative property, then its pullback by any extended
chart, in the vector space, also has the unique derivative property. -/
lemma unique_mdiff_on.unique_diff_on (hs : unique_mdiff_on I s) (x : M) :
unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) :=
begin
-- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e`
-- the local chart at `x`.
assume z hz,
simp only with mfld_simps at hz,
have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _,
have T := (hs.unique_mdiff_on_preimage this) (I.symm z),
simp only [hz.left.left, hz.left.right, hz.right, unique_mdiff_within_at] with mfld_simps at ⊢ T,
convert T using 1,
rw @preimage_comp _ _ _ _ (chart_at H x).symm,
mfld_set_tac
end
/-- When considering functions between manifolds, this statement shows up often. It entails
the unique differential of the pullback in extended charts of the set where the function can
be read in the charts. -/
lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M')
{f : M → M'} (hf : continuous_on f s) :
unique_diff_on 𝕜 ((ext_chart_at I x).target
∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) :=
begin
have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source),
{ assume z hz,
apply (hs z hz.1).inter',
apply (hf z hz.1).preimage_mem_nhds_within,
exact mem_nhds_sets (ext_chart_at_open_source I' y) hz.2 },
exact this.unique_diff_on _
end
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
(Z : basic_smooth_bundle_core I M F)
/-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with
unique differential in the basis also has unique differential. -/
lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) :
unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F))
(Z.to_topological_fiber_bundle_core.proj ⁻¹' s) :=
begin
/- Using a chart (and the fact that unique differentiability is invariant under charts), we
reduce the situation to the model space, where we can use the fact that products respect
unique differentiability. -/
assume p hp,
replace hp : p.fst ∈ s, by simpa only with mfld_simps using hp,
let e₀ := chart_at H p.1,
let e := chart_at (model_prod H F) p,
-- It suffices to prove unique differentiability in a chart
suffices h : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F))
(e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)),
{ have A : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F)) (e.symm.target ∩
e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s))),
{ apply h.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm,
apply_instance },
have : p ∈ e.symm.target ∩
e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)),
by simp only [e, hp] with mfld_simps,
apply (A _ this).mono,
assume q hq,
simp only [e, local_homeomorph.left_inv _ hq.1] with mfld_simps at hq,
simp only [hq] with mfld_simps },
-- rewrite the relevant set in the chart as a direct product
have : (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target ∩
(λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.symm ⁻¹' (sigma.fst ⁻¹' s)) ∩
((range I).prod univ)
= set.prod (I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) univ,
by mfld_set_tac,
assume q hq,
replace hq : q.1 ∈ (chart_at H p.1).target ∧ ((chart_at H p.1).symm : H → M) q.1 ∈ s,
by simpa only with mfld_simps using hq,
simp only [unique_mdiff_within_at, model_with_corners.prod, preimage_inter, this] with mfld_simps,
-- apply unique differentiability of products to conclude
apply unique_diff_on.prod _ unique_diff_on_univ,
{ simp only [hq] with mfld_simps },
{ assume x hx,
have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s),
{ apply hs.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)),
apply_instance },
simp only [unique_mdiff_on, unique_mdiff_within_at, preimage_inter] with mfld_simps at A,
have B := A (I.symm x) hx.1.1 hx.1.2,
rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B }
end
lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s):
unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) :=
hs.smooth_bundle_preimage _
end unique_mdiff
|
42527ec585e8d968d4bd241cae1debeaae4ecf45 | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/tactic/linarith/verification.lean | ee466ee3c48c5a3639f53b6c5a3da4e967ebe8f5 | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 7,172 | lean | /-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import tactic.linarith.elimination
import tactic.linarith.parsing
/-!
# Deriving a proof of false
`linarith` uses an untrusted oracle to produce a certificate of unsatisfiability.
It needs to do some proof reconstruction work to turn this into a proof term.
This file implements the reconstruction.
## Main declarations
The public facing declaration in this file is `prove_false_by_linarith`.
-/
namespace linarith
open ineq tactic native
/-! ### Auxiliary functions for assembling proofs -/
/--
`mul_expr n e` creates a `pexpr` representing `n*e`.
When elaborated, the coefficient will be a native numeral of the same type as `e`.
-/
meta def mul_expr (n : ℕ) (e : expr) : pexpr :=
if n = 1 then ``(%%e) else
``(%%(nat.to_pexpr n) * %%e)
private meta def add_exprs_aux : pexpr → list pexpr → pexpr
| p [] := p
| p [a] := ``(%%p + %%a)
| p (h::t) := add_exprs_aux ``(%%p + %%h) t
/--
`add_exprs l` creates a `pexpr` representing the sum of the elements of `l`, associated left.
If `l` is empty, it will be the `pexpr` 0. Otherwise, it does not include 0 in the sum.
-/
meta def add_exprs : list pexpr → pexpr
| [] := ``(0)
| (h::t) := add_exprs_aux h t
/--
If our goal is to add together two inequalities `t1 R1 0` and `t2 R2 0`,
`ineq_const_nm R1 R2` produces the strength of the inequality in the sum `R`,
along with the name of a lemma to apply in order to conclude `t1 + t2 R 0`.
-/
meta def ineq_const_nm : ineq → ineq → (name × ineq)
| eq eq := (``eq_of_eq_of_eq, eq)
| eq le := (``le_of_eq_of_le, le)
| eq lt := (``lt_of_eq_of_lt, lt)
| le eq := (``le_of_le_of_eq, le)
| le le := (`add_nonpos, le)
| le lt := (`add_neg_of_nonpos_of_neg, lt)
| lt eq := (``lt_of_lt_of_eq, lt)
| lt le := (`add_neg_of_neg_of_nonpos, lt)
| lt lt := (`add_neg, lt)
/--
`mk_lt_zero_pf_aux c pf npf coeff` assumes that `pf` is a proof of `t1 R1 0` and `npf` is a proof
of `t2 R2 0`. It uses `mk_single_comp_zero_pf` to prove `t1 + coeff*t2 R 0`, and returns `R`
along with this proof.
-/
meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) :=
do (iq, h') ← mk_single_comp_zero_pf coeff npf,
let (nm, niq) := ineq_const_nm c iq,
prod.mk niq <$> mk_app nm [pf, h']
/--
`mk_lt_zero_pf coeffs pfs` takes a list of proofs of the form `tᵢ Rᵢ 0`,
paired with coefficients `cᵢ`.
It produces a proof that `∑cᵢ * tᵢ R 0`, where `R` is as strong as possible.
-/
meta def mk_lt_zero_pf : list (expr × ℕ) → tactic expr
| [] := fail "no linear hypotheses found"
| [(h, c)] := prod.snd <$> mk_single_comp_zero_pf c h
| ((h, c)::t) :=
do (iq, h') ← mk_single_comp_zero_pf c h,
prod.snd <$> t.mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.1 ce.2) (iq, h')
/-- If `prf` is a proof of `t R s`, `term_of_ineq_prf prf` returns `t`. -/
meta def term_of_ineq_prf (prf : expr) : tactic expr :=
prod.fst <$> (infer_type prf >>= get_rel_sides)
/-- If `prf` is a proof of `t R s`, `ineq_prf_tp prf` returns the type of `t`. -/
meta def ineq_prf_tp (prf : expr) : tactic expr :=
term_of_ineq_prf prf >>= infer_type
/--
`mk_neg_one_lt_zero_pf tp` returns a proof of `-1 < 0`,
where the numerals are natively of type `tp`.
-/
meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr :=
to_expr ``((neg_neg_of_pos zero_lt_one : -1 < (0 : %%tp)))
/--
If `e` is a proof that `t = 0`, `mk_neg_eq_zero_pf e` returns a proof that `-t = 0`.
-/
meta def mk_neg_eq_zero_pf (e : expr) : tactic expr :=
to_expr ``(neg_eq_zero.mpr %%e)
/--
`prove_eq_zero_using tac e` tries to use `tac` to construct a proof of `e = 0`.
-/
meta def prove_eq_zero_using (tac : tactic unit) (e : expr) : tactic expr :=
do tgt ← to_expr ``(%%e = 0),
prod.snd <$> solve_aux tgt (tac >> done)
/--
`add_neg_eq_pfs l` inspects the list of proofs `l` for proofs of the form `t = 0`. For each such
proof, it adds a proof of `-t = 0` to the list.
-/
meta def add_neg_eq_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h,
match iq with
| ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl
| _ := list.cons h <$> add_neg_eq_pfs t
end
/-! #### The main method -/
/--
`prove_false_by_linarith` is the main workhorse of `linarith`.
Given a list `l` of proofs of `tᵢ Rᵢ 0`,
it tries to derive a contradiction from `l` and use this to produce a proof of `false`.
An oracle is used to search for a certificate of unsatisfiability.
In the current implementation, this is the Fourier Motzkin elimination routine in
`elimination.lean`, but other oracles could easily be swapped in.
The returned certificate is a map `m` from hypothesis indices to natural number coefficients.
If our set of hypotheses has the form `{tᵢ Rᵢ 0}`,
then the elimination process should have guaranteed that
1.\ `∑ (m i)*tᵢ = 0`,
with at least one `i` such that `m i > 0` and `Rᵢ` is `<`.
We have also that
2.\ `∑ (m i)*tᵢ < 0`,
since for each `i`, `(m i)*tᵢ ≤ 0` and at least one is strictly negative.
So we conclude a contradiction `0 < 0`.
It remains to produce proofs of (1) and (2). (1) is verified by calling the `discharger` tactic
of the `linarith_config` object, which is typically `ring`. We prove (2) by folding over the
set of hypotheses.
-/
meta def prove_false_by_linarith (cfg : linarith_config) : list expr → tactic expr
| [] := fail "no args to linarith"
| l@(h::t) := do
-- for the elimination to work properly, we must add a proof of `-1 < 0` to the list,
-- along with negated equality proofs.
l' ← add_neg_eq_pfs l,
hz ← ineq_prf_tp h >>= mk_neg_one_lt_zero_pf,
let inputs := hz::l',
-- perform the elimination and fail if no contradiction is found.
(comps, max_var) ← linear_forms_and_max_var cfg.transparency inputs,
certificate ← fourier_motzkin.produce_certificate comps max_var
| fail "linarith failed to find a contradiction",
linarith_trace "linarith has found a contradiction",
let enum_inputs := inputs.enum,
-- construct a list pairing nonzero coeffs with the proof of their corresponding comparison
let zip := enum_inputs.filter_map $ λ ⟨n, e⟩, prod.mk e <$> certificate.find n,
mls ← zip.mmap (λ ⟨e, n⟩, do e ← term_of_ineq_prf e, return (mul_expr n e)),
-- `sm` is the sum of input terms, scaled to cancel out all variables.
sm ← to_expr $ add_exprs mls,
pformat! "The expression\n {sm}\nshould be both 0 and negative" >>= linarith_trace,
-- we prove that `sm = 0`, typically with `ring`.
sm_eq_zero ← prove_eq_zero_using cfg.discharger sm,
linarith_trace "We have proved that it is zero",
-- we also prove that `sm < 0`.
sm_lt_zero ← mk_lt_zero_pf zip,
linarith_trace "We have proved that it is negative",
-- this is a contradiction.
pftp ← infer_type sm_lt_zero,
(_, nep, _) ← rewrite_core sm_eq_zero pftp,
pf' ← mk_eq_mp nep sm_lt_zero,
mk_app `lt_irrefl [pf']
end linarith
|
cd92b56546038e4e7899d5e84d7e3925cfbe1c71 | bdc27058d2df65f1c05b3dd4ff23a30af9332a2b | /src/propositions.lean | da61046f3215336a2030441056b8bb1123839d51 | [] | no_license | bakerjd99/leanteach2020 | 19e3dda4167f7bb8785fe0e2fe762a9c53a9214d | 2160909674f3aec475eabb51df73af7fa1d91c65 | refs/heads/master | 1,668,724,511,289 | 1,594,407,424,000 | 1,594,407,424,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,411 | lean | import .hilbert
import tactic
noncomputable theory
open_locale classical
local infix ` ≃ `:55 := C -- \ equiv == equivalence/congruence
local infix `⬝`:56 := Segment.mk -- \ cdot == center-dot
--------------------
-- |#Instructions# |
--------------------
/-
0. Read these instructions carefully.
1. I have tried my best to translate statements of the propositions into Lean.
2. In place of proofs, I have placed sorry's.
3. Do NOT add your proofs to this file. Create a new file called
euclid_props.lean (and hilbert_props.lean) on CoCalc. Copy the statements there
and add your proofs to it.
4. The Euclid group can change the import in the first line to read
"import euclid" instead.
5. You might have to make minor changes to the statement to make it copatible with
your syntax. Don't make too many changes though.
6. If you do make a change to the statement, then don't forget to add a note
in your hilbert_props.lean file.
7. I am also mentioning missing definitions in this file. You need to define these
in your hilbert.lean / euclid.lean source file so that the propositions given
here make sense.
8. If you spot some obvious mistakes in the statement of the propositions given
below then let me know. You can also ask me for clarifications.
9. This is still an incomplete list and I will keep adding more propositions in
this file.
-- VK
-/
--------------------
-- |#Propositions# |
--------------------
-- Proposition 1. To construct an equilateral triangle on a given
-- finite straight line.
theorem prop1 (s : Segment) :
∃ t : Triangle, t.p₁ = s.p₁ ∧ t.p₂ = s.p₂ ∧ equilateral t := sorry
-- Proposition 2. To place a straight line equal to a given straight
-- line with one end at a given point.
theorem prop2 (s : Segment) :
∃ s' : Segment, s ≃ s' ∧ s'.p₁ = s.p₂ := sorry
-- Proposition 3. To cut off from the greater of two given unequal
-- straight lines a straight line equal to the less.
-- Assume s₂ < s₁
theorem prop3 (s₁ s₂ : Segment) (h : s₁.p₁ ≠ s₁.p₂):
∃ x : Point, lies_on_segment x s₁ h ∧ s₁.p₁⬝x ≃ s₂ := sorry
-- Proposition 4. If two triangles have two sides equal to two sides
-- respectively, and have the angles contained by the equal straight
-- lines equal, then they also have the base equal to the base, the
-- triangle equals the triangle, and the remaining angles equal the
-- remaining angles respectively, namely those opposite the equal
-- sides.
theorem prop4 (t₁ t₂ : Triangle) :
t₁.s₁₂ ≃ t₂.s₁₂
→ t₁.s₁₃ ≃ t₂.s₁₃
→ t₁.α₁ ≃ t₂.α₁
→ congruent_triangle t₁ t₂ := sorry
-- Proposition 5. In isosceles triangles the angles at the base equal
-- one another, and, if the equal straight lines are produced further,
-- then the angles under the base equal one another.
theorem prop5 (t : Triangle) :
t.s₁₂ ≃ t.s₁₃ → t.α₂ ≃ t.α₃ := sorry
-- Proposition 6. If in a triangle two angles equal one another, then
-- the sides opposite the equal angles also equal one another.
theorem prop6 (t : Triangle) :
t.α₁ ≃ t.α₂ → t.s₂₃ ≃ t.s₁₃ := sorry
-- Proposition 7. Given two straight lines constructed from the ends
-- of a straight line and meeting in a point, there cannot be
-- constructed from the ends of the same straight line, and on the
-- same side of it, two other straight lines meeting in another point
-- and equal to the former two respectively, namely each equal to that
-- from the same end.
theorem prop7 (t₁ t₂ : Triangle) :
t₁.s₁₂ = t₂.s₁₂ -- note that this says equal, not congruent.
→ t₁.s₁₃ ≃ t₂.s₁₃
→ t₁.s₂₃ ≃ t₂.s₂₃
→ t₁.p₃ = t₂.p₃ := sorry
-- Proposition 8. If two triangles have the two sides equal to two
-- sides respectively, and also have the base equal to the base, then
-- they also have the angles equal which are contained by the equal
-- straight lines.
theorem prop8 (t₁ t₂ : Triangle) :
t₁.s₁₂ ≃ t₂.s₁₂
→ t₁.s₁₃ ≃ t₂.s₁₃
→ t₁.s₂₃ ≃ t₁.s₂₃
-- Proposition 9.
-- To bisect a given rectilinear angle.
-- Proposition 10.
-- To bisect a given finite straight line.
-- Proposition 11.
-- To draw a straight line at right angles to a given straight line from a given point on it.
-- Proposition 12.
-- To draw a straight line perpendicular to a given infinite straight line from a given point not on it.
-- Proposition 13.
-- If a straight line stands on a straight line, then it makes either two right angles or angles whose sum equals two right angles.
-- Proposition 14.
-- If with any straight line, and at a point on it, two straight lines not lying on the same side make the sum of the adjacent angles equal to two right angles, then the two straight lines are in a straight line with one another.
-- Proposition 15.
-- If two straight lines cut one another, then they make the vertical angles equal to one another.
-- Corollary. If two straight lines cut one another, then they will make the angles at the point of section equal to four right angles.
--
/-
Proposition 16.
In any triangle, if one of the sides is produced, then the exterior angle is greater than either of the interior and opposite angles.
Proposition 17.
In any triangle the sum of any two angles is less than two right angles.
Proposition 18.
In any triangle the angle opposite the greater side is greater.
Proposition 19.
In any triangle the side opposite the greater angle is greater.
Proposition 20.
In any triangle the sum of any two sides is greater than the remaining one.
Proposition 21.
If from the ends of one of the sides of a triangle two straight lines are constructed meeting within the triangle, then the sum of the straight lines so constructed is less than the sum of the remaining two sides of the triangle, but the constructed straight lines contain a greater angle than the angle contained by the remaining two sides.
Proposition 22.
To construct a triangle out of three straight lines which equal three given straight lines: thus it is necessary that the sum of any two of the straight lines should be greater than the remaining one.
Proposition 23.
To construct a rectilinear angle equal to a given rectilinear angle on a given straight line and at a point on it.
Proposition 24.
If two triangles have two sides equal to two sides respectively, but have one of the angles contained by the equal straight lines greater than the other, then they also have the base greater than the base.
Proposition 25.
If two triangles have two sides equal to two sides respectively, but have the base greater than the base, then they also have the one of the angles contained by the equal straight lines greater than the other.
Proposition 26.
If two triangles have two angles equal to two angles respectively, and one side equal to one side, namely, either the side adjoining the equal angles, or that opposite one of the equal angles, then the remaining sides equal the remaining sides and the remaining angle equals the remaining angle.
Proposition 27.
If a straight line falling on two straight lines makes the alternate angles equal to one another, then the straight lines are parallel to one another.
Proposition 28.
If a straight line falling on two straight lines makes the exterior angle equal to the interior and opposite angle on the same side, or the sum of the interior angles on the same side equal to two right angles, then the straight lines are parallel to one another.
Proposition 29.
A straight line falling on parallel straight lines makes the alternate angles equal to one another, the exterior angle equal to the interior and opposite angle, and the sum of the interior angles on the same side equal to two right angles.
Proposition 30.
Straight lines parallel to the same straight line are also parallel to one another.
Proposition 31.
To draw a straight line through a given point parallel to a given straight line.
Proposition 32.
In any triangle, if one of the sides is produced, then the exterior angle equals the sum of the two interior and opposite angles, and the sum of the three interior angles of the triangle equals two right angles.
Proposition 33.
Straight lines which join the ends of equal and parallel straight lines in the same directions are themselves equal and parallel.
Proposition 34.
In parallelogrammic areas the opposite sides and angles equal one another, and the diameter bisects the areas.
Proposition 35.
Parallelograms which are on the same base and in the same parallels equal one another.
Proposition 36.
Parallelograms which are on equal bases and in the same parallels equal one another.
Proposition 37.
Triangles which are on the same base and in the same parallels equal one another.
Proposition 38.
Triangles which are on equal bases and in the same parallels equal one another.
Proposition 39.
Equal triangles which are on the same base and on the same side are also in the same parallels.
Proposition 40.
Equal triangles which are on equal bases and on the same side are also in the same parallels.
Proposition 41.
If a parallelogram has the same base with a triangle and is in the same parallels, then the parallelogram is double the triangle.
Proposition 42.
To construct a parallelogram equal to a given triangle in a given rectilinear angle.
Proposition 43.
In any parallelogram the complements of the parallelograms about the diameter equal one another.
Proposition 44.
To a given straight line in a given rectilinear angle, to apply a parallelogram equal to a given triangle.
Proposition 45.
To construct a parallelogram equal to a given rectilinear figure in a given rectilinear angle.
Proposition 46.
To describe a square on a given straight line.
Proposition 47.
In right-angled triangles the square on the side opposite the right angle equals the sum of the squares on the sides containing the right angle.
Proposition 48.
If in a triangle the square on one of the sides equals the sum of the squares on the remaining two sides of the triangle, then the angle contained by the remaining two sides of the triangle is right.
-/
|
ef81b9b6f6f15b1138221cd5fc630f59689a9268 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/lean/run/inj2.lean | 281c651cbccb293dc8c084ae30bf9999196ea752 | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,124 | lean | universes u v
inductive Vec2 (α : Type u) (β : Type v) : Nat → Type (max u v)
| nil : Vec2 0
| cons : α → β → forall {n}, Vec2 n → Vec2 (n+1)
inductive Fin2 : Nat → Type
| zero (n : Nat) : Fin2 (n+1)
| succ {n : Nat} (s : Fin2 n) : Fin2 (n+1)
new_frontend
theorem test1 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : a₁ = a₂ :=
by {
injection h;
assumption
}
theorem test2 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : v = w :=
by {
injection h with h1 h2 h3 h4;
assumption
}
theorem test3 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : v = w :=
by {
injection h with _ _ _ h4;
exact h4
}
theorem test4 {α} (v : Fin2 0) : α :=
by cases v
def test5 {α β} {n} (v : Vec2 α β (n+1)) : α :=
by cases v with
| cons h1 h2 n tail => exact h1
def test6 {α β} {n} (v : Vec2 α β (n+2)) : α :=
by cases v with
| cons h1 h2 n tail => exact h1
|
914cc2c515eb440abf5c4b7dca4880df0f3186e8 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /library/theories/number_theory/primes.lean | e0008571dd0331f7bd9bec945faec78885ec9b33 | [
"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 | 10,266 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad
Prime numbers.
-/
import data.nat logic.identities
open bool subtype
namespace nat
open decidable
attribute [reducible]
definition prime (p : nat) := p ≥ 2 ∧ ∀ m, m ∣ p → m = 1 ∨ m = p
definition prime_ext (p : nat) := p ≥ 2 ∧ ∀ m, m ≤ p → m ∣ p → m = 1 ∨ m = p
local attribute prime_ext [reducible]
lemma prime_ext_iff_prime (p : nat) : prime_ext p ↔ prime p :=
iff.intro
begin
intro h, cases h with h₁ h₂, constructor, assumption,
intro m d, exact h₂ m (le_of_dvd (lt_of_succ_le (le_of_succ_le h₁)) d) d
end
begin
intro h, cases h with h₁ h₂, constructor, assumption,
intro m l d, exact h₂ m d
end
attribute [instance]
definition decidable_prime (p : nat) : decidable (prime p) :=
decidable_of_decidable_of_iff _ (prime_ext_iff_prime p)
lemma ge_two_of_prime {p : nat} : prime p → p ≥ 2 :=
suppose prime p, obtain h₁ h₂, from this,
h₁
theorem gt_one_of_prime {p : ℕ} (primep : prime p) : p > 1 :=
lt_of_succ_le (ge_two_of_prime primep)
theorem pos_of_prime {p : ℕ} (primep : prime p) : p > 0 :=
lt.trans zero_lt_one (gt_one_of_prime primep)
lemma not_prime_zero : ¬ prime 0 :=
λ h, absurd (ge_two_of_prime h) dec_trivial
lemma not_prime_one : ¬ prime 1 :=
λ h, absurd (ge_two_of_prime h) dec_trivial
lemma prime_two : prime 2 :=
dec_trivial
lemma prime_three : prime 3 :=
dec_trivial
lemma pred_prime_pos {p : nat} : prime p → pred p > 0 :=
suppose prime p,
have p ≥ 2, from ge_two_of_prime this,
show pred p > 0, from lt_of_succ_le (pred_le_pred this)
lemma succ_pred_prime {p : nat} : prime p → succ (pred p) = p :=
assume h, succ_pred_of_pos (pos_of_prime h)
lemma eq_one_or_eq_self_of_prime_of_dvd {p m : nat} : prime p → m ∣ p → m = 1 ∨ m = p :=
assume h d, obtain h₁ h₂, from h, h₂ m d
lemma gt_one_of_pos_of_prime_dvd {i p : nat} : prime p → 0 < i → i % p = 0 → 1 < i :=
assume ipp pos h,
have p ≥ 2, from ge_two_of_prime ipp,
have p ∣ i, from dvd_of_mod_eq_zero h,
have p ≤ i, from le_of_dvd pos this,
lt_of_succ_le (le.trans `2 ≤ p` this)
definition sub_dvd_of_not_prime {n : nat} : n ≥ 2 → ¬ prime n → {m | m ∣ n ∧ m ≠ 1 ∧ m ≠ n} :=
assume h₁ h₂,
have ¬ prime_ext n, from iff.mpr (not_iff_not_of_iff !prime_ext_iff_prime) h₂,
have ¬ n ≥ 2 ∨ ¬ (∀ m, m ≤ n → m ∣ n → m = 1 ∨ m = n), from iff.mp !not_and_iff_not_or_not this,
have ¬ (∀ m, m ≤ n → m ∣ n → m = 1 ∨ m = n), from or_resolve_right this (not_not_intro h₁),
have ¬ (∀ m, m < succ n → m ∣ n → m = 1 ∨ m = n), from
assume h, absurd (λ m hl hd, h m (lt_succ_of_le hl) hd) this,
have {m | m < succ n ∧ ¬(m ∣ n → m = 1 ∨ m = n)}, from bsub_not_of_not_ball this,
obtain m hlt (h₃ : ¬(m ∣ n → m = 1 ∨ m = n)), from this,
obtain `m ∣ n` (h₅ : ¬ (m = 1 ∨ m = n)), from iff.mp !not_implies_iff_and_not h₃,
have ¬ m = 1 ∧ ¬ m = n, from iff.mp !not_or_iff_not_and_not h₅,
subtype.tag m (and.intro `m ∣ n` this)
theorem exists_dvd_of_not_prime {n : nat} : n ≥ 2 → ¬ prime n → ∃ m, m ∣ n ∧ m ≠ 1 ∧ m ≠ n :=
assume h₁ h₂, exists_of_subtype (sub_dvd_of_not_prime h₁ h₂)
definition sub_dvd_of_not_prime2 {n : nat} : n ≥ 2 → ¬ prime n → {m | m ∣ n ∧ m ≥ 2 ∧ m < n} :=
assume h₁ h₂,
have n ≠ 0, from assume h, begin subst n, exact absurd h₁ dec_trivial end,
obtain m m_dvd_n m_ne_1 m_ne_n, from sub_dvd_of_not_prime h₁ h₂,
have m_ne_0 : m ≠ 0, from assume h, begin subst m, exact absurd (eq_zero_of_zero_dvd m_dvd_n) `n ≠ 0` end,
begin
existsi m, split, assumption,
split,
{cases m with m, exact absurd rfl m_ne_0,
cases m with m, exact absurd rfl m_ne_1, exact succ_le_succ (succ_le_succ (zero_le _))},
{have m_le_n : m ≤ n, from le_of_dvd (pos_of_ne_zero `n ≠ 0`) m_dvd_n,
exact lt_of_le_of_ne m_le_n m_ne_n}
end
theorem exists_dvd_of_not_prime2 {n : nat} : n ≥ 2 → ¬ prime n → ∃ m, m ∣ n ∧ m ≥ 2 ∧ m < n :=
assume h₁ h₂, exists_of_subtype (sub_dvd_of_not_prime2 h₁ h₂)
definition sub_prime_and_dvd {n : nat} : n ≥ 2 → {p | prime p ∧ p ∣ n} :=
nat.strong_rec_on n
(take n,
assume ih : ∀ m, m < n → m ≥ 2 → {p | prime p ∧ p ∣ m},
suppose n ≥ 2,
by_cases
(suppose prime n, subtype.tag n (and.intro this (dvd.refl n)))
(suppose ¬ prime n,
obtain m m_dvd_n m_ge_2 m_lt_n, from sub_dvd_of_not_prime2 `n ≥ 2` this,
obtain p (hp : prime p) (p_dvd_m : p ∣ m), from ih m m_lt_n m_ge_2,
have p ∣ n, from dvd.trans p_dvd_m m_dvd_n,
subtype.tag p (and.intro hp this)))
lemma exists_prime_and_dvd {n : nat} : n ≥ 2 → ∃ p, prime p ∧ p ∣ n :=
assume h, exists_of_subtype (sub_prime_and_dvd h)
open eq.ops
definition infinite_primes (n : nat) : {p | p ≥ n ∧ prime p} :=
let m := fact (n + 1) in
have m ≥ 1, from le_of_lt_succ (succ_lt_succ (fact_pos _)),
have m + 1 ≥ 2, from succ_le_succ this,
obtain p `prime p` `p ∣ m + 1`, from sub_prime_and_dvd this,
have p ≥ 2, from ge_two_of_prime `prime p`,
have p > 0, from lt_of_succ_lt (lt_of_succ_le `p ≥ 2`),
have p ≥ n, from by_contradiction
(suppose ¬ p ≥ n,
have p < n, from lt_of_not_ge this,
have p ≤ n + 1, from le_of_lt (lt.step this),
have p ∣ m, from dvd_fact `p > 0` this,
have p ∣ 1, from dvd_of_dvd_add_right (!add.comm ▸ `p ∣ m + 1`) this,
have p ≤ 1, from le_of_dvd zero_lt_one this,
show false, from absurd (le.trans `2 ≤ p` `p ≤ 1`) dec_trivial),
subtype.tag p (and.intro this `prime p`)
lemma exists_infinite_primes (n : nat) : ∃ p, p ≥ n ∧ prime p :=
exists_of_subtype (infinite_primes n)
lemma odd_of_prime {p : nat} : prime p → p > 2 → odd p :=
λ pp p_gt_2, by_contradiction (λ hn,
have even p, from even_of_not_odd hn,
obtain k `p = 2*k`, from exists_of_even this,
have 2 ∣ p, by rewrite [`p = 2*k`]; apply dvd_mul_right,
or.elim (eq_one_or_eq_self_of_prime_of_dvd pp this)
(suppose 2 = 1, absurd this dec_trivial)
(suppose 2 = p, by subst this; exact absurd p_gt_2 !lt.irrefl))
theorem dvd_of_prime_of_not_coprime {p n : ℕ} (primep : prime p) (nc : ¬ coprime p n) : p ∣ n :=
have H : gcd p n = 1 ∨ gcd p n = p, from eq_one_or_eq_self_of_prime_of_dvd primep !gcd_dvd_left,
or_resolve_right H nc ▸ !gcd_dvd_right
theorem coprime_of_prime_of_not_dvd {p n : ℕ} (primep : prime p) (npdvdn : ¬ p ∣ n) :
coprime p n :=
by_contradiction (suppose ¬ coprime p n, npdvdn (dvd_of_prime_of_not_coprime primep this))
theorem not_dvd_of_prime_of_coprime {p n : ℕ} (primep : prime p) (cop : coprime p n) : ¬ p ∣ n :=
suppose p ∣ n,
have p ∣ gcd p n, from dvd_gcd !dvd.refl this,
have p ≤ gcd p n, from le_of_dvd (!gcd_pos_of_pos_left (pos_of_prime primep)) this,
have 2 ≤ 1, from le.trans (ge_two_of_prime primep) (cop ▸ this),
show false, from !not_succ_le_self this
theorem not_coprime_of_prime_dvd {p n : ℕ} (primep : prime p) (pdvdn : p ∣ n) : ¬ coprime p n :=
assume cop, not_dvd_of_prime_of_coprime primep cop pdvdn
theorem dvd_of_prime_of_dvd_mul_left {p m n : ℕ} (primep : prime p)
(Hmn : p ∣ m * n) (Hm : ¬ p ∣ m) :
p ∣ n :=
have coprime p m, from coprime_of_prime_of_not_dvd primep Hm,
show p ∣ n, from dvd_of_coprime_of_dvd_mul_left this Hmn
theorem dvd_of_prime_of_dvd_mul_right {p m n : ℕ} (primep : prime p)
(Hmn : p ∣ m * n) (Hn : ¬ p ∣ n) :
p ∣ m :=
dvd_of_prime_of_dvd_mul_left primep (!mul.comm ▸ Hmn) Hn
theorem not_dvd_mul_of_prime {p m n : ℕ} (primep : prime p) (Hm : ¬ p ∣ m) (Hn : ¬ p ∣ n) :
¬ p ∣ m * n :=
assume Hmn, Hm (dvd_of_prime_of_dvd_mul_right primep Hmn Hn)
lemma dvd_or_dvd_of_prime_of_dvd_mul {p m n : nat} : prime p → p ∣ m * n → p ∣ m ∨ p ∣ n :=
λ h₁ h₂, by_cases
(suppose p ∣ m, or.inl this)
(suppose ¬ p ∣ m, or.inr (dvd_of_prime_of_dvd_mul_left h₁ h₂ this))
lemma dvd_of_prime_of_dvd_pow {p m : nat} : ∀ {n}, prime p → p ∣ m^n → p ∣ m
| 0 hp hd :=
have p = 1, from eq_one_of_dvd_one hd,
have (1:nat) ≥ 2, begin rewrite -this at {1}, apply ge_two_of_prime hp end,
absurd this dec_trivial
| (succ n) hp hd :=
have p ∣ (m^n)*m, by rewrite [pow_succ' at hd]; exact hd,
or.elim (dvd_or_dvd_of_prime_of_dvd_mul hp this)
(suppose p ∣ m^n, dvd_of_prime_of_dvd_pow hp this)
(suppose p ∣ m, this)
lemma coprime_pow_of_prime_of_not_dvd {p m a : nat} : prime p → ¬ p ∣ a → coprime a (p^m) :=
λ h₁ h₂, coprime_pow_right m (coprime_swap (coprime_of_prime_of_not_dvd h₁ h₂))
lemma coprime_primes {p q : nat} : prime p → prime q → p ≠ q → coprime p q :=
λ hp hq hn,
have gcd p q ∣ p, from !gcd_dvd_left,
or.elim (eq_one_or_eq_self_of_prime_of_dvd hp this)
(suppose gcd p q = 1, this)
(assume h : gcd p q = p,
have gcd p q ∣ q, from !gcd_dvd_right,
have p ∣ q, by rewrite -h; exact this,
or.elim (eq_one_or_eq_self_of_prime_of_dvd hq this)
(suppose p = 1, by subst p; exact absurd hp not_prime_one)
(suppose p = q, by contradiction))
lemma coprime_pow_primes {p q : nat} (n m : nat) : prime p → prime q → p ≠ q → coprime (p^n) (q^m) :=
λ hp hq hn, coprime_pow_right m (coprime_pow_left n (coprime_primes hp hq hn))
lemma coprime_or_dvd_of_prime {p} (Pp : prime p) (i : nat) : coprime p i ∨ p ∣ i :=
by_cases
(suppose p ∣ i, or.inr this)
(suppose ¬ p ∣ i, or.inl (coprime_of_prime_of_not_dvd Pp this))
lemma eq_one_or_dvd_of_dvd_prime_pow {p : nat} : ∀ {m i : nat}, prime p → i ∣ (p^m) → i = 1 ∨ p ∣ i
| 0 := take i, assume Pp, begin rewrite [pow_zero], intro Pdvd, apply or.inl (eq_one_of_dvd_one Pdvd) end
| (succ m) := take i, assume Pp, or.elim (coprime_or_dvd_of_prime Pp i)
(λ Pcp, begin
rewrite [pow_succ'], intro Pdvd,
apply eq_one_or_dvd_of_dvd_prime_pow Pp,
apply dvd_of_coprime_of_dvd_mul_right,
apply coprime_swap Pcp, exact Pdvd
end)
(λ Pdvd, assume P, or.inr Pdvd)
end nat
|
38350a235bd01f97cf98b80286cf77aa2ed90b8f | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/abs.lean | 731ff7d54580c777ddb265d43acaef0e5ea26476 | [
"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 | 681 | lean | /-
Copyright (c) 2021 Christopher Hoskin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Christopher Hoskin
-/
/-!
# Absolute value
This file defines a notational class `has_abs` which adds the unary operator `abs` and the notation
`|.|`. The concept of an absolute value occurs in lattice ordered groups and in GL and GM spaces.
## Notations
The notation `|.|` is introduced for the absolute value.
## Tags
absolute
-/
/--
Absolute value is a unary operator with properties similar to the absolute value of a real number.
-/
class has_abs (α : Type*) := (abs : α → α)
export has_abs (abs)
notation `|`a`|` := abs a
|
b47e02731d80f14ccf77dcdea5ef5ae5290040bc | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/basic_monitor.lean | 83660ec71d55499e58022cfa4f18c984e3cbc595 | [
"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 | 268 | lean | @[vm_monitor]
meta def basic_monitor : vm_monitor nat :=
{ init := 0, step := λ s, return (trace ("step " ++ to_string s) (s+1)) }
set_option debugger true
example (a b : Prop) : a → b → a ∧ b :=
begin
intros,
constructor,
assumption,
assumption
end
|
8468226735eceecb97221880868a7f494e981324 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/algebra3.lean | 7a184e21ca7dbfa905405ce44b1f991e63e8298e | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,735 | lean | import logic
structure has_mul [class] (A : Type) :=
(mul : A → A → A)
structure has_one [class] (A : Type) :=
(one : A)
structure has_inv [class] (A : Type) :=
(inv : A → A)
infixl `*` := has_mul.mul
postfix `⁻¹` := has_inv.inv
notation 1 := has_one.one
structure semigroup [class] (A : Type) extends has_mul A :=
(assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c))
structure comm_semigroup [class] (A : Type) extends semigroup A renaming mul→add:=
(comm : ∀a b, add a b = add b a)
infixl `+` := comm_semigroup.add
structure monoid [class] (A : Type) extends semigroup A, has_one A :=
(right_id : ∀a, mul a one = a) (left_id : ∀a, mul one a = a)
-- We can suppress := and :: when we are not declaring any new field.
structure comm_monoid [class] (A : Type) extends monoid A renaming mul→add, comm_semigroup A
print fields comm_monoid
structure group [class] (A : Type) extends monoid A, has_inv A :=
(is_inv : ∀ a, mul a (inv a) = one)
structure abelian_group [class] (A : Type) extends group A renaming mul→add, comm_monoid A
structure ring [class] (A : Type)
extends abelian_group A renaming
assoc→add.assoc
comm→add.comm
one→zero
right_id→add.right_id
left_id→add.left_id
inv→uminus
is_inv→uminus_is_inv,
monoid A renaming
assoc→mul.assoc
right_id→mul.right_id
left_id→mul.left_id
:=
(dist_left : ∀ a b c, mul a (add b c) = add (mul a b) (mul a c))
(dist_right : ∀ a b c, mul (add a b) c = add (mul a c) (mul b c))
print fields ring
variable A : Type₁
variables a b c d : A
variable R : ring A
check a + b * c
set_option pp.implicit true
set_option pp.notation false
set_option pp.coercions true
check a + b * c
|
af6f83e7f0b2a13a252107c2eed3cf9fe993b72a | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/eq5.lean | 62186f0cf6753cb90c716e7053d820703ded753a | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 270 | lean | open nat
definition fib : nat → nat,
fib 0 := 1,
fib 1 := 1,
fib (x+2) := fib x + fib (x+1)
theorem fib0 : fib 0 = 1 :=
rfl
theorem fib1 : fib 1 = 1 :=
rfl
theorem fib_succ_succ (a : nat) : fib (a+2) = fib a + fib (a+1) :=
rfl
example : fib 8 = 34 :=
rfl
|
dc228bbc77a357bade72de9c50b53b7804675944 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Elab/Deriving.lean | 026fd7b1459f06e3f695db623249828f19a9da0f | [
"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 | 561 | 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.Elab.Deriving.Basic
import Lean.Elab.Deriving.Util
import Lean.Elab.Deriving.Inhabited
import Lean.Elab.Deriving.Nonempty
import Lean.Elab.Deriving.TypeName
import Lean.Elab.Deriving.BEq
import Lean.Elab.Deriving.DecEq
import Lean.Elab.Deriving.Repr
import Lean.Elab.Deriving.FromToJson
import Lean.Elab.Deriving.SizeOf
import Lean.Elab.Deriving.Hashable
import Lean.Elab.Deriving.Ord
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.