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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
f680ee3e287e8a9c71a41db1184c424f180ec4bb | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/sleep.lean | e52744e9cd1cb70063b79c9a512cee6be732f8a7 | [
"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 | 66 | lean | import system.io
run_cmd (tactic.unsafe_run_io $ io.proc.sleep 2) |
6ede9bd5e3bdf7a0581e645a4cf1098034d264c7 | 4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d | /src/Lean/Meta/Tactic/Simp/SimpLemmas.lean | 3baf56b8ce5b1c06ccbb204e142686c8f34f6436 | [
"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 | 12,419 | 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 := eval_prio default
post : Bool := true
perm : Bool := false -- 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 := format s.getName
let prio := f!":{s.priority}"
name ++ prio ++ perm
instance : ToMessageData SimpLemma where
toMessageData s := format 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
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) (inv : Bool) : 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 inv
ps.mapM fun (e, type) =>
return (β mkLambdaFVars xs e, β mkForallFVars xs type)
else if let some (_, lhs, rhs) := type.eq? then
if inv then
let type β mkEq rhs lhs
let e β mkEqSymm e
return [(e, type)]
else
return [(e, type)]
else if let some (lhs, rhs) := type.iff? then
if inv then
let type β mkEq rhs lhs
let e β mkEqSymm (β mkPropExt e)
return [(e, type)]
else
let type β mkEq lhs rhs
let e β mkPropExt e
return [(e, type)]
else if let some (_, lhs, rhs) := type.ne? then
if inv then
throwError "invalid 'β' modifier in rewrite rule to 'False'"
let type β mkEq (β mkEq lhs rhs) (mkConst ``False)
let e β mkEqFalse e
return [(e, type)]
else if let some p := type.not? then
if inv then
throwError "invalid 'β' modifier in rewrite rule to 'False'"
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β inv) ++ (β preprocess eβ typeβ inv)
else
if inv then
throwError "invalid 'β' modifier in rewrite rule to 'True'"
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}"
private 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 }
private def mkSimpLemmasFromConst (declName : Name) (post : Bool) (inv : 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 inv || (β shouldPreprocess type) then
let mut r := #[]
for (val, type) in (β preprocess val type inv) 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]
inductive SimpEntry where
| lemma : SimpLemma β SimpEntry
| toUnfold : Name β SimpEntry
deriving Inhabited
abbrev SimpExtension := SimpleScopedEnvExtension SimpEntry SimpLemmas
def SimpExtension.getLemmas (ext : SimpExtension) : CoreM SimpLemmas :=
return ext.getState (β getEnv)
def addSimpLemma (ext : SimpExtension) (declName : Name) (post : Bool) (inv : Bool) (attrKind : AttributeKind) (prio : Nat) : MetaM Unit := do
let simpLemmas β mkSimpLemmasFromConst declName post inv prio
for simpLemma in simpLemmas do
ext.add (SimpEntry.lemma simpLemma) attrKind
def mkSimpAttr (attrName : Name) (attrDescr : String) (ext : SimpExtension) : IO Unit :=
registerBuiltinAttribute {
name := attrName
descr := attrDescr
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 ext declName post (inv := false) attrKind prio
else if info.hasValue then
ext.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 β ext.getState (β getEnv)
let s β s.erase declName
modifyEnv fun env => ext.modifyState env fun _ => s
}
def mkSimpExt (extName : Name) : IO SimpExtension :=
registerSimpleScopedEnvExtension {
name := extName
initial := {}
addEntry := fun d e =>
match e with
| SimpEntry.lemma e => addSimpLemmaEntry d e
| SimpEntry.toUnfold n => d.addDeclToUnfold n
}
def registerSimpAttr (attrName : Name) (attrDescr : String) (extName : Name := attrName.appendAfter "Ext") : IO SimpExtension := do
let ext β mkSimpExt extName
mkSimpAttr attrName attrDescr ext
return ext
builtin_initialize simpExtension : SimpExtension β registerSimpAttr `simp "simplification theorem"
def getSimpLemmas : CoreM SimpLemmas :=
simpExtension.getLemmas
/- Auxiliary method for adding a global declaration to a `SimpLemmas` datastructure. -/
def SimpLemmas.addConst (s : SimpLemmas) (declName : Name) (post : Bool := true) (inv : Bool := false) (prio : Nat := eval_prio default) : MetaM SimpLemmas := do
let simpLemmas β mkSimpLemmasFromConst declName post inv 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) (inv : Bool) : MetaM (Array Expr) := do
let type β inferType val
checkTypeIsProp type
let ps β preprocess val type inv
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) (inv : Bool := false) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM (Array SimpLemma) :=
withReducible do
(β preprocessProof proof inv).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) (inv : Bool := false) (post : Bool := true) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM SimpLemmas := do
if proof.isConst then
s.addConst proof.constName! post inv prio
else
let simpLemmas β mkSimpLemmas levelParams proof post inv 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
|
0132fc4b0cf52dbf1db310d7ee06907002f207d3 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /test/apply_rules.lean | 529d3dd7889aaaf1298ad3e9a7509dcc3eae8986 | [
"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,126 | lean |
import data.nat.basic
open nat
example {a b c d e : nat} (h1 : a β€ b) (h2 : c β€ d) (h3 : 0 β€ e) :
a + c * e + a + c + 0 β€ b + d * e + b + d + e :=
add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3
example {a b c d e : nat} (h1 : a β€ b) (h2 : c β€ d) (h3 : 0 β€ e) :
a + c * e + a + c + 0 β€ b + d * e + b + d + e :=
by apply_rules [add_le_add, mul_le_mul_of_nonneg_right]
@[user_attribute]
meta def mono_rules : user_attribute :=
{ name := `mono_rules,
descr := "lemmas usable to prove monotonicity" }
attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right
example {a b c d e : nat} (h1 : a β€ b) (h2 : c β€ d) (h3 : 0 β€ e) :
a + c * e + a + c + 0 β€ b + d * e + b + d + e :=
by apply_rules [mono_rules]
example {a b c d e : nat} (h1 : a β€ b) (h2 : c β€ d) (h3 : 0 β€ e) :
a + c * e + a + c + 0 β€ b + d * e + b + d + e :=
by apply_rules mono_rules
-- test that metavariables created for implicit arguments don't get stuck
example (P : β β Type) (f : Ξ {n : β}, P n β P (n + 1)) (g : P 0) : P 2 :=
begin
apply_rules [f, g],
end
|
89e6453eaee92e0ddfbe601ceec6753233abe146 | 680b0d1592ce164979dab866b232f6fa743f2cc8 | /library/algebra/module.lean | edb5eedf88b77516d5ba6d9d36bf6f01e26842e7 | [
"Apache-2.0"
] | permissive | syohex/lean | 657428ab520f8277fc18cf04bea2ad200dbae782 | 081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc | refs/heads/master | 1,611,274,838,635 | 1,452,668,188,000 | 1,452,668,188,000 | 49,562,028 | 0 | 0 | null | 1,452,675,604,000 | 1,452,675,602,000 | null | UTF-8 | Lean | false | false | 4,455 | lean | /-
Copyright (c) 2015 Nathaniel Thomas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nathaniel Thomas, Jeremy Avigad
Modules and vector spaces over a ring.
-/
import algebra.field
structure has_scalar [class] (F V : Type) :=
(smul : F β V β V)
infixl ` β’ `:73 := has_scalar.smul
/- modules over a ring -/
structure left_module [class] (R M : Type) [ringR : ring R]
extends has_scalar R M, add_comm_group M :=
(smul_left_distrib : β (r : R) (x y : M), smul r (add x y) = (add (smul r x) (smul r y)))
(smul_right_distrib : β (r s : R) (x : M), smul (ring.add r s) x = (add (smul r x) (smul s x)))
(smul_mul : β r s x, smul (mul r s) x = smul r (smul s x))
(one_smul : β x, smul one x = x)
section left_module
variables {R M : Type}
variable [ringR : ring R]
variable [moduleRM : left_module R M]
include ringR moduleRM
-- Note: the anonymous include does not work in the propositions below.
proposition smul_left_distrib (a : R) (u v : M) : a β’ (u + v) = a β’ u + a β’ v :=
!left_module.smul_left_distrib
proposition smul_right_distrib (a b : R) (u : M) : (a + b)β’u = aβ’u + bβ’u :=
!left_module.smul_right_distrib
proposition smul_mul (a : R) (b : R) (u : M) : (a * b) β’ u = a β’ (b β’ u) :=
!left_module.smul_mul
proposition one_smul (u : M) : (1 : R) β’ u = u := !left_module.one_smul
proposition zero_smul (u : M) : (0 : R) β’ u = 0 :=
have (0 : R) β’ u + 0 β’ u = 0 β’ u + 0, by rewrite [-smul_right_distrib, *add_zero],
!add.left_cancel this
proposition smul_zero (a : R) : a β’ (0 : M) = 0 :=
have a β’ 0 + a β’ 0 = a β’ 0 + 0, by rewrite [-smul_left_distrib, *add_zero],
!add.left_cancel this
proposition neg_smul (a : R) (u : M) : (-a) β’ u = - (a β’ u) :=
eq_neg_of_add_eq_zero (by rewrite [-smul_right_distrib, add.left_inv, zero_smul])
proposition neg_one_smul (u : M) : -(1 : R) β’ u = -u :=
by rewrite [neg_smul, one_smul]
end left_module
/- linear maps -/
structure is_linear_map [class] (R : Type) {Mβ Mβ : Type}
[smulβ : has_scalar R Mβ] [smulβ : has_scalar R Mβ]
[addβ : has_add Mβ] [addβ : has_add Mβ]
(T : Mβ β Mβ) :=
(additive : β u v : Mβ, T (u + v) = T u + T v)
(homogeneous : β a : R, β u : Mβ, T (a β’ u) = a β’ T u)
proposition linear_map_additive (R : Type) {Mβ Mβ : Type}
[smulβ : has_scalar R Mβ] [smulβ : has_scalar R Mβ]
[addβ : has_add Mβ] [addβ : has_add Mβ]
(T : Mβ β Mβ) [linT : is_linear_map R T] (u v : Mβ) :
T (u + v) = T u + T v :=
is_linear_map.additive smulβ smulβ _ _ T u v
proposition linear_map_homogeneous {R Mβ Mβ : Type}
[smulβ : has_scalar R Mβ] [smulβ : has_scalar R Mβ]
[addβ : has_add Mβ] [addβ : has_add Mβ]
(T : Mβ β Mβ) [linT : is_linear_map R T] (a : R) (u : Mβ) :
T (a β’ u) = a β’ T u :=
is_linear_map.homogeneous smulβ smulβ _ _ T a u
proposition is_linear_map_id [instance] (R : Type) {M : Type}
[smulRM : has_scalar R M] [has_addM : has_add M] :
is_linear_map R (id : M β M) :=
is_linear_map.mk (take u v, rfl) (take a u, rfl)
section is_linear_map
variables {R Mβ Mβ : Type}
variable [ringR : ring R]
variable [moduleRMβ : left_module R Mβ]
variable [moduleRMβ : left_module R Mβ]
include ringR moduleRMβ moduleRMβ
variable T : Mβ β Mβ
variable [is_linear_mapT : is_linear_map R T]
include is_linear_mapT
proposition linear_map_zero : T 0 = 0 :=
calc
T 0 = T ((0 : R) β’ 0) : zero_smul
... = (0 : R) β’ T 0 : linear_map_homogeneous T
... = 0 : zero_smul
proposition linear_map_neg (u : Mβ) : T (-u) = -(T u) :=
by rewrite [-neg_one_smul, linear_map_homogeneous T, neg_one_smul]
proposition linear_map_smul_add_smul (a b : R) (u v : Mβ) :
T (a β’ u + b β’ v) = a β’ T u + b β’ T v :=
by rewrite [linear_map_additive R T, *linear_map_homogeneous T]
end is_linear_map
/- vector spaces -/
structure vector_space [class] (F V : Type) [fieldF : field F]
extends left_module F V
/- an example -/
section
variables (F V : Type)
variables [field F] [vector_spaceFV : vector_space F V]
variable T : V β V
variable [is_linear_map F T]
include vector_spaceFV
example (a b : F) (u v : V) : T (a β’ u + b β’ v) = a β’ T u + b β’ T v :=
!linear_map_smul_add_smul
end
|
6eaa5fe65e4f7f93b2671e0511d0cd75376d6ba6 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/bad_quoted_symbol.lean | c510326432418ebb82b8fe675b37b8651463ad43 | [
"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 | 121 | lean | notation a ` \/ ` b := a β¨ b
notation a `1\/` b := a β¨ b
notation a ` \ / ` b := a β¨ b
notation a ` ` b := a β¨ b
|
3bb9654c602ccaca994683d90fff0525145d5787 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/tactic/suggest.lean | 962fd1d7eac98ffec305671c7296dbe125ac5dce | [
"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 | 20,829 | lean | /-
Copyright (c) 2019 Lucas Allen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lucas Allen and Scott Morrison
-/
import data.mllist
import tactic.solve_by_elim
/-!
# `suggest` and `library_search`
`suggest` and `library_search` are a pair of tactics for applying lemmas from the library to the
current goal.
* `suggest` prints a list of `exact ...` or `refine ...` statements, which may produce new goals
* `library_search` prints a single `exact ...` which closes the goal, or fails
-/
namespace tactic
open native
namespace suggest
open solve_by_elim
/-- Map a name (typically a head symbol) to a "canonical" definitional synonym.
Given a name `n`, we want a name `n'` such that a sufficiently applied
expression with head symbol `n` is always definitionally equal to an expression
with head symbol `n'`.
Thus, we can search through all lemmas with a result type of `n'`
to solve a goal with head symbol `n`.
For example, `>` is mapped to `<` because `a > b` is definitionally equal to `b < a`,
and `not` is mapped to `false` because `Β¬ a` is definitionally equal to `p β false`
The default is that the original argument is returned, so `<` is just mapped to `<`.
`normalize_synonym` is called for every lemma in the library, so it needs to be fast.
-/
-- TODO this is a hack; if you suspect more cases here would help, please report them
meta def normalize_synonym : name β name
| `gt := `has_lt.lt
| `ge := `has_le.le
| `monotone := `has_le.le
| `not := `false
| n := n
/--
Compute the head symbol of an expression, then normalise synonyms.
This is only used when analysing the goal, so it is okay to do more expensive analysis here.
-/
-- We may want to tweak this further?
meta def allowed_head_symbols : expr β list name
-- We first have a various "customisations":
-- Because in `β` `a.succ β€ b` is definitionally `a < b`,
-- we add some special cases to allow looking for `<` lemmas even when the goal has a `β€`.
-- Note we only do this in the `β` case, for performance.
| `(@has_le.le β _ (nat.succ _) _) := [`has_le.le, `has_lt.lt]
| `(@ge β _ _ (nat.succ _)) := [`has_le.le, `has_lt.lt]
| `(@has_le.le β _ 1 _) := [`has_le.le, `has_lt.lt]
| `(@ge β _ _ 1) := [`has_le.le, `has_lt.lt]
-- And then the generic cases:
| (expr.pi _ _ _ t) := allowed_head_symbols t
| (expr.app f _) := allowed_head_symbols f
| (expr.const n _) := [normalize_synonym n]
| _ := [`_]
.
/--
A declaration can match the head symbol of the current goal in four possible ways:
* `ex` : an exact match
* `mp` : the declaration returns an `iff`, and the right hand side matches the goal
* `mpr` : the declaration returns an `iff`, and the left hand side matches the goal
* `both`: the declaration returns an `iff`, and the both sides match the goal
-/
@[derive decidable_eq, derive inhabited]
inductive head_symbol_match
| ex | mp | mpr | both
open head_symbol_match
/-- a textual representation of a `head_symbol_match`, for trace debugging. -/
def head_symbol_match.to_string : head_symbol_match β string
| ex := "exact"
| mp := "iff.mp"
| mpr := "iff.mpr"
| both := "iff.mp and iff.mpr"
/-- Determine if, and in which way, a given expression matches the specified head symbol. -/
meta def match_head_symbol (hs : name_set) : expr β option head_symbol_match
| (expr.pi _ _ _ t) := match_head_symbol t
| `(%%a β %%b) := if hs.contains `iff then some ex else
match (match_head_symbol a, match_head_symbol b) with
| (some ex, some ex) :=
some both
| (some ex, _) := some mpr
| (_, some ex) := some mp
| _ := none
end
| (expr.app f _) := match_head_symbol f
| (expr.const n _) := if hs.contains (normalize_synonym n) then some ex else none
| _ := if hs.contains `_ then some ex else none
/-- A package of `declaration` metadata, including the way in which its type matches the head symbol
which we are searching for. -/
meta structure decl_data :=
(d : declaration)
(n : name)
(m : head_symbol_match)
(l : β) -- cached length of name
/--
Generate a `decl_data` from the given declaration if
it matches the head symbol `hs` for the current goal.
-/
-- We used to check here for private declarations, or declarations with certain suffixes.
-- It turns out `apply` is so fast, it's better to just try them all.
meta def process_declaration (hs : name_set) (d : declaration) : option decl_data :=
let n := d.to_name in
if !d.is_trusted || n.is_internal then
none
else
(Ξ» m, β¨d, n, m, n.lengthβ©) <$> match_head_symbol hs d.type
/-- Retrieve all library definitions with a given head symbol. -/
meta def library_defs (hs : name_set) : tactic (list decl_data) :=
do trace_if_enabled `suggest format!"Looking for lemmas with head symbols {hs}.",
env β get_env,
let defs := env.decl_filter_map (process_declaration hs),
-- Sort by length; people like short proofs
let defs := defs.qsort(Ξ» dβ dβ, dβ.l β€ dβ.l),
trace_if_enabled `suggest format!"Found {defs.length} relevant lemmas:",
trace_if_enabled `suggest $ defs.map (Ξ» β¨d, n, m, lβ©, (n, m.to_string)),
return defs
/--
We unpack any element of a list of `decl_data` corresponding to an `β` statement that could apply
in both directions into two separate elements.
This ensures that both directions can be independently returned by `suggest`,
and avoids a problem where the application of one direction prevents
the application of the other direction. (See `exp_le_exp` in the tests.)
-/
meta def unpack_iff_both : list decl_data β list decl_data
| [] := []
| (β¨d, n, both, lβ© :: L) := β¨d, n, mp, lβ© :: β¨d, n, mpr, lβ© :: unpack_iff_both L
| (β¨d, n, m, lβ© :: L) := β¨d, n, m, lβ© :: unpack_iff_both L
/--
Apply the lemma `e`, then attempt to close all goals using
`solve_by_elim opt`, failing if `close_goals = tt`
and there are any goals remaining.
Returns the number of subgoals which were closed using `solve_by_elim`.
-/
-- Implementation note: as this is used by both `library_search` and `suggest`,
-- we first run `solve_by_elim` separately on the independent goals,
-- whether or not `close_goals` is set,
-- and then run `solve_by_elim { all_goals := tt }`,
-- requiring that it succeeds if `close_goals = tt`.
meta def apply_and_solve (close_goals : bool) (opt : opt := { }) (e : expr) : tactic β :=
do
trace_if_enabled `suggest format!"Trying to apply lemma: {e}",
apply e opt.to_apply_cfg,
trace_if_enabled `suggest format!"Applied lemma: {e}",
ng β num_goals,
-- Phase 1
-- Run `solve_by_elim` on each "safe" goal separately, not worrying about failures.
-- (We only attempt the "safe" goals in this way in Phase 1. In Phase 2 we will do
-- backtracking search across all goals, allowing us to guess solutions that involve data, or
-- unify metavariables, but only as long as we can finish all goals.)
try (any_goals (independent_goal >> solve_by_elim opt)),
-- Phase 2
(done >> return ng) <|> (do
-- If there were any goals that we did not attempt solving in the first phase
-- (because they weren't propositional, or contained a metavariable)
-- as a second phase we attempt to solve all remaining goals at once
-- (with backtracking across goals).
(any_goals (success_if_fail independent_goal) >>
solve_by_elim { backtrack_all_goals := tt, ..opt }) <|>
-- and fail unless `close_goals = ff`
guard Β¬ close_goals,
ng' β num_goals,
return (ng - ng'))
/--
Apply the declaration `d` (or the forward and backward implications separately, if it is an `iff`),
and then attempt to solve the subgoal using `apply_and_solve`.
Returns the number of subgoals successfully closed.
-/
meta def apply_declaration (close_goals : bool) (opt : opt := { }) (d : decl_data) :
tactic β :=
let tac := apply_and_solve close_goals opt in
do (e, t) β decl_mk_const d.d,
match d.m with
| ex := tac e
| mp := do l β iff_mp_core e t, tac l
| mpr := do l β iff_mpr_core e t, tac l
| both := undefined -- we use `unpack_iff_both` to ensure this isn't reachable
end
/-- An `application` records the result of a successful application of a library lemma. -/
meta structure application :=
(state : tactic_state)
(script : string)
(decl : option declaration)
(num_goals : β)
(hyps_used : β)
end suggest
open solve_by_elim
open suggest
declare_trace suggest -- Trace a list of all relevant lemmas
-- Call `apply_declaration`, then prepare the tactic script and
-- count the number of local hypotheses used.
private meta def apply_declaration_script
(g : expr) (hyps : list expr)
(opt : opt := { })
(d : decl_data) :
tactic application :=
-- (This tactic block is only executed when we evaluate the mllist,
-- so we need to do the `focus1` here.)
retrieve $ focus1 $ do
apply_declaration ff opt d,
ng β num_goals,
-- This `instantiate_mvars` is necessary so that we count used hypotheses correctly.
g β instantiate_mvars g,
s β read,
m β tactic_statement g,
return
{ application .
state := s,
decl := d.d,
script := m,
num_goals := ng,
hyps_used := hyps.countp (Ξ» h, h.occurs g) }
-- implementation note: we produce a `tactic (mllist tactic application)` first,
-- because it's easier to work in the tactic monad, but in a moment we squash this
-- down to an `mllist tactic application`.
private meta def suggest_core' (opt : opt := { }) :
tactic (mllist tactic application) :=
do g :: _ β get_goals,
hyps β local_context,
-- Make sure that `solve_by_elim` doesn't just solve the goal immediately:
(retrieve (do
focus1 $ solve_by_elim opt,
s β read,
m β tactic_statement g,
-- This `instantiate_mvars` is necessary so that we count used hypotheses correctly.
g β instantiate_mvars g,
return $ mllist.of_list [β¨s, m, none, 0, hyps.countp (Ξ» h, h.occurs g)β©])) <|>
-- Otherwise, let's actually try applying library lemmas.
(do
-- Collect all definitions with the correct head symbol
t β infer_type g,
defs β unpack_iff_both <$> library_defs (name_set.of_list $ allowed_head_symbols t),
let defs : mllist tactic _ := mllist.of_list defs,
-- Try applying each lemma against the goal,
-- recording the tactic script as a string,
-- the number of remaining goals,
-- and number of local hypotheses used.
let results := defs.mfilter_map (apply_declaration_script g hyps opt),
-- Now call `symmetry` and try again.
-- (Because we are using `mllist`, this is essentially free if we've already found a lemma.)
symm_state β retrieve $ try_core $ symmetry >> read,
let results_symm := match symm_state with
| (some s) :=
defs.mfilter_map (Ξ» d, retrieve $ set_state s >> apply_declaration_script g hyps opt d)
| none := mllist.nil
end,
return (results.append results_symm))
/--
The core `suggest` tactic.
It attempts to apply a declaration from the library,
then solve new goals using `solve_by_elim`.
It returns a list of `application`s consisting of fields:
* `state`, a tactic state resulting from the successful application of a declaration from
the library,
* `script`, a string of the form `Try this: refine ...` or `Try this: exact ...` which will
reproduce that tactic state,
* `decl`, an `option declaration` indicating the declaration that was applied
(or none, if `solve_by_elim` succeeded),
* `num_goals`, the number of remaining goals, and
* `hyps_used`, the number of local hypotheses used in the solution.
-/
meta def suggest_core (opt : opt := { }) : mllist tactic application :=
(mllist.monad_lift (suggest_core' opt)).join
/--
See `suggest_core`.
Returns a list of at most `limit` `application`s,
sorted by number of goals, and then (reverse) number of hypotheses used.
-/
meta def suggest (limit : option β := none) (opt : opt := { }) :
tactic (list application) :=
do let results := suggest_core opt,
-- Get the first n elements of the successful lemmas
L β if h : limit.is_some then results.take (option.get h) else results.force,
-- Sort by number of remaining goals, then by number of hypotheses used.
return $ L.qsort (Ξ» dβ dβ, dβ.num_goals < dβ.num_goals β¨
(dβ.num_goals = dβ.num_goals β§ dβ.hyps_used β₯ dβ.hyps_used))
/--
Returns a list of at most `limit` strings, of the form `Try this: exact ...` or
`Try this: refine ...`, which make progress on the current goal using a declaration
from the library.
-/
meta def suggest_scripts (limit : option β := none) (opt : opt := { }) :
tactic (list string) :=
do L β suggest limit opt,
return $ L.map application.script
/--
Returns a string of the form `Try this: exact ...`, which closes the current goal.
-/
meta def library_search (opt : opt := { }) : tactic string :=
(suggest_core opt).mfirst (Ξ» a, do guard (a.num_goals = 0), write a.state, return a.script)
namespace interactive
open tactic
open interactive
open lean.parser
open interactive.types
open solve_by_elim
local postfix `?`:9001 := optional
declare_trace silence_suggest -- Turn off `Try this: exact/refine ...` trace messages for `suggest`
/--
`suggest` tries to apply suitable theorems/defs from the library, and generates
a list of `exact ...` or `refine ...` scripts that could be used at this step.
It leaves the tactic state unchanged. It is intended as a complement of the search
function in your editor, the `#find` tactic, and `library_search`.
`suggest` takes an optional natural number `num` as input and returns the first `num`
(or less, if all possibilities are exhausted) possibilities ordered by length of lemma names.
The default for `num` is `50`.
For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that
`suggest` might miss some results if `num` is not large enough. However, because
`suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values.
You can add additional lemmas to be used along with local hypotheses
after the application of a library lemma,
using the same syntax as for `solve_by_elim`, e.g.
```
example {a b c d: nat} (hβ : a < c) (hβ : b < d) : max (c + d) (a + b) = (c + d) :=
begin
suggest [add_lt_add], -- Says: `Try this: exact max_eq_left_of_lt (add_lt_add hβ hβ)`
end
```
You can also use `suggest with attr` to include all lemmas with the attribute `attr`.
-/
meta def suggest (n : parse (with_desc "n" small_nat)?)
(hs : parse simp_arg_list) (attr_names : parse with_ident_list) (opt : opt := { }) :
tactic unit :=
do (lemma_thunks, ctx_thunk) β mk_assumption_set ff hs attr_names,
L β tactic.suggest_scripts (n.get_or_else 50)
{ lemma_thunks := some lemma_thunks, ctx_thunk := ctx_thunk, ..opt },
if is_trace_enabled_for `silence_suggest then
skip
else
if L.length = 0 then
fail "There are no applicable declarations"
else
L.mmap trace >> skip
/--
`suggest` lists possible usages of the `refine` tactic and leaves the tactic state unchanged.
It is intended as a complement of the search function in your editor, the `#find` tactic, and
`library_search`.
`suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if
all possibilities are exhausted) possibilities ordered by length of lemma names.
The default for `num` is `50`.
For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest`
might miss some results if `num` is not large enough. However, because `suggest` uses monadic
lazy lists, smaller values of `num` run faster than larger values.
An example of `suggest` in action,
```lean
example (n : nat) : n < n + 1 :=
begin suggest, sorry end
```
prints the list,
```lean
Try this: exact nat.lt.base n
Try this: exact nat.lt_succ_self n
Try this: refine not_le.mp _
Try this: refine gt_iff_lt.mp _
Try this: refine nat.lt.step _
Try this: refine lt_of_not_ge _
...
```
-/
add_tactic_doc
{ name := "suggest",
category := doc_category.tactic,
decl_names := [`tactic.interactive.suggest],
tags := ["search", "Try this"] }
-- Turn off `Try this: exact ...` trace message for `library_search`
declare_trace silence_library_search
/--
`library_search` attempts to apply every definition in the library whose head symbol
matches the goal, and then discharge any new goals using `solve_by_elim`.
If it succeeds, it prints a trace message `exact ...` which can replace the invocation
of `library_search`.
By default `library_search` only unfolds `reducible` definitions
when attempting to match lemmas against the goal.
Previously, it would unfold most definitions, sometimes giving surprising answers, or slow answers.
The old behaviour is still available via `library_search!`.
You can add additional lemmas to be used along with local hypotheses
after the application of a library lemma,
using the same syntax as for `solve_by_elim`, e.g.
```
example {a b c d: nat} (hβ : a < c) (hβ : b < d) : max (c + d) (a + b) = (c + d) :=
begin
library_search [add_lt_add], -- Says: `Try this: exact max_eq_left_of_lt (add_lt_add hβ hβ)`
end
```
You can also use `library_search with attr` to include all lemmas with the attribute `attr`.
-/
meta def library_search (semireducible : parse $ optional (tk "!"))
(hs : parse simp_arg_list) (attr_names : parse with_ident_list)
(opt : opt := { }) : tactic unit :=
do (lemma_thunks, ctx_thunk) β mk_assumption_set ff hs attr_names,
(tactic.library_search
{ backtrack_all_goals := tt,
lemma_thunks := some lemma_thunks,
ctx_thunk := ctx_thunk,
md := if semireducible.is_some then
tactic.transparency.semireducible else tactic.transparency.reducible,
..opt } >>=
if is_trace_enabled_for `silence_library_search then
(Ξ» _, skip)
else
trace) <|>
fail
"`library_search` failed.
If you aren't sure what to do next, you can also
try `library_search!`, `suggest`, or `hint`.
Possible reasons why `library_search` failed:
* `library_search` will only apply a single lemma from the library,
and then try to fill in its hypotheses from local hypotheses.
* If you haven't already, try stating the theorem you want in its own lemma.
* Sometimes the library has one version of a lemma
but not a very similar version obtained by permuting arguments.
Try replacing `a + b` with `b + a`, or `a - b < c` with `a < b + c`,
to see if maybe the lemma exists but isn't stated quite the way you would like.
* Make sure that you have all the side conditions for your theorem to be true.
For example you won't find `a - b + b = a` for natural numbers in the library because it's false!
Search for `b β€ a β a - b + b = a` instead.
* If a definition you made is in the goal,
you won't find any theorems about it in the library.
Try unfolding the definition using `unfold my_definition`.
* If all else fails, ask on https://leanprover.zulipchat.com/,
and maybe we can improve the library and/or `library_search` for next time."
/--
`library_search` is a tactic to identify existing lemmas in the library. It tries to close the
current goal by applying a lemma from the library, then discharging any new goals using
`solve_by_elim`.
Typical usage is:
```lean
example (n m k : β) : n * (m - k) = n * m - n * k :=
by library_search -- Try this: exact nat.mul_sub_left_distrib n m k
```
`library_search` prints a trace message showing the proof it found, shown above as a comment.
Typically you will then copy and paste this proof, replacing the call to `library_search`.
-/
add_tactic_doc
{ name := "library_search",
category := doc_category.tactic,
decl_names := [`tactic.interactive.library_search],
tags := ["search", "Try this"] }
end interactive
/-- Invoking the hole command `library_search` ("Use `library_search` to complete the goal") calls
the tactic `library_search` to produce a proof term with the type of the hole.
Running it on
```lean
example : 0 < 1 :=
{!!}
```
produces
```lean
example : 0 < 1 :=
nat.one_pos
```
-/
@[hole_command] meta def library_search_hole_cmd : hole_command :=
{ name := "library_search",
descr := "Use `library_search` to complete the goal.",
action := Ξ» _, do
script β library_search,
-- Is there a better API for dropping the 'Try this: exact ' prefix on this string?
return [((script.get_rest "Try this: exact ").get_or_else script, "by library_search")] }
add_tactic_doc
{ name := "library_search",
category := doc_category.hole_cmd,
decl_names := [`tactic.library_search_hole_cmd],
tags := ["search", "Try this"] }
end tactic
|
5f5e50a3061c3b081ecdc55af6488ac47e0f4f2e | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/data/num/basic.lean | 566b2603f74c838f5761cf504e109920efbbd594 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,605 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
/-!
# Binary representation of integers using inductive types
Note: Unlike in Coq, where this representation is preferred because of
the reliance on kernel reduction, in Lean this representation is discouraged
in favor of the "Peano" natural numbers `nat`, and the purpose of this
collection of theorems is to show the equivalence of the different approaches.
-/
/-- The type of positive binary numbers.
13 = 1101(base 2) = bit1 (bit0 (bit1 one)) -/
@[derive has_reflect, derive decidable_eq]
inductive pos_num : Type
| one : pos_num
| bit1 : pos_num β pos_num
| bit0 : pos_num β pos_num
instance : has_one pos_num := β¨pos_num.oneβ©
instance : inhabited pos_num := β¨1β©
/-- The type of nonnegative binary numbers, using `pos_num`.
13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -/
@[derive has_reflect, derive decidable_eq]
inductive num : Type
| zero : num
| pos : pos_num β num
instance : has_zero num := β¨num.zeroβ©
instance : has_one num := β¨num.pos 1β©
instance : inhabited num := β¨0β©
/-- Representation of integers using trichotomy around zero.
13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one)))
-13 = -1101(base 2) = neg (bit1 (bit0 (bit1 one))) -/
@[derive has_reflect, derive decidable_eq]
inductive znum : Type
| zero : znum
| pos : pos_num β znum
| neg : pos_num β znum
instance : has_zero znum := β¨znum.zeroβ©
instance : has_one znum := β¨znum.pos 1β©
instance : inhabited znum := β¨0β©
namespace pos_num
/--
`bit b n` appends the bit `b` to the end of `n`, where `bit tt x = x1` and `bit ff x = x0`.
-/
def bit (b : bool) : pos_num β pos_num := cond b bit1 bit0
/--
The successor of a `pos_num`.
-/
def succ : pos_num β pos_num
| 1 := bit0 one
| (bit1 n) := bit0 (succ n)
| (bit0 n) := bit1 n
/--
Returns a boolean for whether the `pos_num` is `one`.
-/
def is_one : pos_num β bool
| 1 := tt
| _ := ff
/--
Addition of two `pos_num`s.
-/
protected def add : pos_num β pos_num β pos_num
| 1 b := succ b
| a 1 := succ a
| (bit0 a) (bit0 b) := bit0 (add a b)
| (bit1 a) (bit1 b) := bit0 (succ (add a b))
| (bit0 a) (bit1 b) := bit1 (add a b)
| (bit1 a) (bit0 b) := bit1 (add a b)
instance : has_add pos_num := β¨pos_num.addβ©
/--
The predecessor of a `pos_num` as a `num`.
-/
def pred' : pos_num β num
| 1 := 0
| (bit0 n) := num.pos (num.cases_on (pred' n) 1 bit1)
| (bit1 n) := num.pos (bit0 n)
/--
The predecessor of a `pos_num` as a `pos_num`. This means that `pred 1 = 1`.
-/
def pred (a : pos_num) : pos_num :=
num.cases_on (pred' a) 1 id
/--
The number of bits of a `pos_num`, as a `pos_num`.
-/
def size : pos_num β pos_num
| 1 := 1
| (bit0 n) := succ (size n)
| (bit1 n) := succ (size n)
/--
The number of bits of a `pos_num`, as a `nat`.
-/
def nat_size : pos_num β nat
| 1 := 1
| (bit0 n) := nat.succ (nat_size n)
| (bit1 n) := nat.succ (nat_size n)
/--
Multiplication of two `pos_num`s.
-/
protected def mul (a : pos_num) : pos_num β pos_num
| 1 := a
| (bit0 b) := bit0 (mul b)
| (bit1 b) := bit0 (mul b) + a
instance : has_mul pos_num := β¨pos_num.mulβ©
/--
`of_nat_succ n` is the `pos_num` corresponding to `n + 1`.
-/
def of_nat_succ : β β pos_num
| 0 := 1
| (nat.succ n) := succ (of_nat_succ n)
/--
`of_nat n` is the `pos_num` corresponding to `n`, except for `of_nat 0 = 1`.
-/
def of_nat (n : β) : pos_num := of_nat_succ (nat.pred n)
open ordering
/--
Ordering of `pos_num`s.
-/
def cmp : pos_num β pos_num β ordering
| 1 1 := eq
| _ 1 := gt
| 1 _ := lt
| (bit0 a) (bit0 b) := cmp a b
| (bit0 a) (bit1 b) := ordering.cases_on (cmp a b) lt lt gt
| (bit1 a) (bit0 b) := ordering.cases_on (cmp a b) lt gt gt
| (bit1 a) (bit1 b) := cmp a b
instance : has_lt pos_num := β¨Ξ»a b, cmp a b = ordering.ltβ©
instance : has_le pos_num := β¨Ξ»a b, Β¬ b < aβ©
instance decidable_lt : @decidable_rel pos_num (<)
| a b := by dsimp [(<)]; apply_instance
instance decidable_le : @decidable_rel pos_num (β€)
| a b := by dsimp [(β€)]; apply_instance
end pos_num
section
variables {Ξ± : Type*} [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±]
/--
`cast_pos_num` casts a `pos_num` into any type which has `0`, `1` and `+`.
-/
def cast_pos_num : pos_num β Ξ±
| 1 := 1
| (pos_num.bit0 a) := bit0 (cast_pos_num a)
| (pos_num.bit1 a) := bit1 (cast_pos_num a)
/--
`cast_num` casts a `num` into any type which has `0`, `1` and `+`.
-/
def cast_num : num β Ξ±
| 0 := 0
| (num.pos p) := cast_pos_num p
-- see Note [coercion into rings]
@[priority 900] instance pos_num_coe : has_coe_t pos_num Ξ± := β¨cast_pos_numβ©
-- see Note [coercion into rings]
@[priority 900] instance num_nat_coe : has_coe_t num Ξ± := β¨cast_numβ©
instance : has_repr pos_num := β¨Ξ» n, repr (n : β)β©
instance : has_repr num := β¨Ξ» n, repr (n : β)β©
end
namespace num
open pos_num
/--
The successor of a `num` as a `pos_num`.
-/
def succ' : num β pos_num
| 0 := 1
| (pos p) := succ p
/--
The successor of a `num` as a `num`.
-/
def succ (n : num) : num := pos (succ' n)
/--
Addition of two `num`s.
-/
protected def add : num β num β num
| 0 a := a
| b 0 := b
| (pos a) (pos b) := pos (a + b)
instance : has_add num := β¨num.addβ©
/--
`bit0 n` appends a `0` to the end of `n`, where `bit0 n = n0`.
-/
protected def bit0 : num β num
| 0 := 0
| (pos n) := pos (pos_num.bit0 n)
/--
`bit1 n` appends a `1` to the end of `n`, where `bit1 n = n1`.
-/
protected def bit1 : num β num
| 0 := 1
| (pos n) := pos (pos_num.bit1 n)
/--
`bit b n` appends the bit `b` to the end of `n`, where `bit tt x = x1` and `bit ff x = x0`.
-/
def bit (b : bool) : num β num := cond b num.bit1 num.bit0
/--
The number of bits required to represent a `num`, as a `num`. `size 0` is defined to be `0`.
-/
def size : num β num
| 0 := 0
| (pos n) := pos (pos_num.size n)
/--
The number of bits required to represent a `num`, as a `nat`. `size 0` is defined to be `0`.
-/
def nat_size : num β nat
| 0 := 0
| (pos n) := pos_num.nat_size n
/--
Multiplication of two `num`s.
-/
protected def mul : num β num β num
| 0 _ := 0
| _ 0 := 0
| (pos a) (pos b) := pos (a * b)
instance : has_mul num := β¨num.mulβ©
open ordering
/--
Ordering of `num`s.
-/
def cmp : num β num β ordering
| 0 0 := eq
| _ 0 := gt
| 0 _ := lt
| (pos a) (pos b) := pos_num.cmp a b
instance : has_lt num := β¨Ξ»a b, cmp a b = ordering.ltβ©
instance : has_le num := β¨Ξ»a b, Β¬ b < aβ©
instance decidable_lt : @decidable_rel num (<)
| a b := by dsimp [(<)]; apply_instance
instance decidable_le : @decidable_rel num (β€)
| a b := by dsimp [(β€)]; apply_instance
/--
Converts a `num` to a `znum`.
-/
def to_znum : num β znum
| 0 := 0
| (pos a) := znum.pos a
/--
Converts `x : num` to `-x : znum`.
-/
def to_znum_neg : num β znum
| 0 := 0
| (pos a) := znum.neg a
/--
Converts a `nat` to a `num`.
-/
def of_nat' : β β num :=
nat.binary_rec 0 (Ξ» b n, cond b num.bit1 num.bit0)
end num
namespace znum
open pos_num
/--
The negation of a `znum`.
-/
def zneg : znum β znum
| 0 := 0
| (pos a) := neg a
| (neg a) := pos a
instance : has_neg znum := β¨znegβ©
/--
The absolute value of a `znum` as a `num`.
-/
def abs : znum β num
| 0 := 0
| (pos a) := num.pos a
| (neg a) := num.pos a
/--
The successor of a `znum`.
-/
def succ : znum β znum
| 0 := 1
| (pos a) := pos (pos_num.succ a)
| (neg a) := (pos_num.pred' a).to_znum_neg
/--
The predecessor of a `znum`.
-/
def pred : znum β znum
| 0 := neg 1
| (pos a) := (pos_num.pred' a).to_znum
| (neg a) := neg (pos_num.succ a)
/--
`bit0 n` appends a `0` to the end of `n`, where `bit0 n = n0`.
-/
protected def bit0 : znum β znum
| 0 := 0
| (pos n) := pos (pos_num.bit0 n)
| (neg n) := neg (pos_num.bit0 n)
/--
`bit1 x` appends a `1` to the end of `x`, mapping `x` to `2 * x + 1`.
-/
protected def bit1 : znum β znum
| 0 := 1
| (pos n) := pos (pos_num.bit1 n)
| (neg n) := neg (num.cases_on (pred' n) 1 pos_num.bit1)
/--
`bitm1 x` appends a `1` to the end of `x`, mapping `x` to `2 * x - 1`.
-/
protected def bitm1 : znum β znum
| 0 := neg 1
| (pos n) := pos (num.cases_on (pred' n) 1 pos_num.bit1)
| (neg n) := neg (pos_num.bit1 n)
/--
Converts an `int` to a `znum`.
-/
def of_int' : β€ β znum
| (n : β) := num.to_znum (num.of_nat' n)
| -[1+ n] := num.to_znum_neg (num.of_nat' (n+1))
end znum
namespace pos_num
open znum
/--
Subtraction of two `pos_num`s, producing a `znum`.
-/
def sub' : pos_num β pos_num β znum
| a 1 := (pred' a).to_znum
| 1 b := (pred' b).to_znum_neg
| (bit0 a) (bit0 b) := (sub' a b).bit0
| (bit0 a) (bit1 b) := (sub' a b).bitm1
| (bit1 a) (bit0 b) := (sub' a b).bit1
| (bit1 a) (bit1 b) := (sub' a b).bit0
/--
Converts a `znum` to `option pos_num`, where it is `some` if the `znum` was positive and `none`
otherwise.
-/
def of_znum' : znum β option pos_num
| (znum.pos p) := some p
| _ := none
/--
Converts a `znum` to a `pos_num`, mapping all out of range values to `1`.
-/
def of_znum : znum β pos_num
| (znum.pos p) := p
| _ := 1
/--
Subtraction of `pos_num`s, where if `a < b`, then `a - b = 1`.
-/
protected def sub (a b : pos_num) : pos_num :=
match sub' a b with
| (znum.pos p) := p
| _ := 1
end
instance : has_sub pos_num := β¨pos_num.subβ©
end pos_num
namespace num
/--
The predecessor of a `num` as an `option num`, where `ppred 0 = none`
-/
def ppred : num β option num
| 0 := none
| (pos p) := some p.pred'
/--
The predecessor of a `num` as a `num`, where `pred 0 = 0`.
-/
def pred : num β num
| 0 := 0
| (pos p) := p.pred'
/--
Divides a `num` by `2`
-/
def div2 : num β num
| 0 := 0
| 1 := 0
| (pos (pos_num.bit0 p)) := pos p
| (pos (pos_num.bit1 p)) := pos p
/--
Converts a `znum` to an `option num`, where `of_znum' p = none` if `p < 0`.
-/
def of_znum' : znum β option num
| 0 := some 0
| (znum.pos p) := some (pos p)
| (znum.neg p) := none
/--
Converts a `znum` to an `option num`, where `of_znum p = 0` if `p < 0`.
-/
def of_znum : znum β num
| (znum.pos p) := pos p
| _ := 0
/--
Subtraction of two `num`s, producing a `znum`.
-/
def sub' : num β num β znum
| 0 0 := 0
| (pos a) 0 := znum.pos a
| 0 (pos b) := znum.neg b
| (pos a) (pos b) := a.sub' b
/--
Subtraction of two `num`s, producing an `option num`.
-/
def psub (a b : num) : option num :=
of_znum' (sub' a b)
/--
Subtraction of two `num`s, where if `a < b`, `a - b = 0`.
-/
protected def sub (a b : num) : num :=
of_znum (sub' a b)
instance : has_sub num := β¨num.subβ©
end num
namespace znum
open pos_num
/--
Addition of `znum`s.
-/
protected def add : znum β znum β znum
| 0 a := a
| b 0 := b
| (pos a) (pos b) := pos (a + b)
| (pos a) (neg b) := sub' a b
| (neg a) (pos b) := sub' b a
| (neg a) (neg b) := neg (a + b)
instance : has_add znum := β¨znum.addβ©
/--
Multiplication of `znum`s.
-/
protected def mul : znum β znum β znum
| 0 a := 0
| b 0 := 0
| (pos a) (pos b) := pos (a * b)
| (pos a) (neg b) := neg (a * b)
| (neg a) (pos b) := neg (a * b)
| (neg a) (neg b) := pos (a * b)
instance : has_mul znum := β¨znum.mulβ©
open ordering
/--
Ordering on `znum`s.
-/
def cmp : znum β znum β ordering
| 0 0 := eq
| (pos a) (pos b) := pos_num.cmp a b
| (neg a) (neg b) := pos_num.cmp b a
| (pos _) _ := gt
| (neg _) _ := lt
| _ (pos _) := lt
| _ (neg _) := gt
instance : has_lt znum := β¨Ξ»a b, cmp a b = ordering.ltβ©
instance : has_le znum := β¨Ξ»a b, Β¬ b < aβ©
instance decidable_lt : @decidable_rel znum (<)
| a b := by dsimp [(<)]; apply_instance
instance decidable_le : @decidable_rel znum (β€)
| a b := by dsimp [(β€)]; apply_instance
end znum
namespace pos_num
def divmod_aux (d : pos_num) (q r : num) : num Γ num :=
match num.of_znum' (num.sub' r (num.pos d)) with
| some r' := (num.bit1 q, r')
| none := (num.bit0 q, r)
end
/--
`divmod x y = (y / x, y % x)`.
-/
def divmod (d : pos_num) : pos_num β num Γ num
| (bit0 n) := let (q, rβ) := divmod n in
divmod_aux d q (num.bit0 rβ)
| (bit1 n) := let (q, rβ) := divmod n in
divmod_aux d q (num.bit1 rβ)
| 1 := divmod_aux d 0 1
/--
Division of `pos_num`,
-/
def div' (n d : pos_num) : num := (divmod d n).1
/--
Modulus of `pos_num`s.
-/
def mod' (n d : pos_num) : num := (divmod d n).2
def sqrt_aux1 (b : pos_num) (r n : num) : num Γ num :=
match num.of_znum' (n.sub' (r + num.pos b)) with
| some n' := (r.div2 + num.pos b, n')
| none := (r.div2, n)
end
def sqrt_aux : pos_num β num β num β num
| b@(bit0 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n'
| b@(bit1 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n'
| 1 r n := (sqrt_aux1 1 r n).1
/-
def sqrt_aux : β β β β β β β
| b r n := if b0 : b = 0 then r else
let b' := shiftr b 2 in
have b' < b, from sqrt_aux_dec b0,
match (n - (r + b : β) : β€) with
| (n' : β) := sqrt_aux b' (div2 r + b) n'
| _ := sqrt_aux b' (div2 r) n
end
/-- `sqrt n` is the square root of a natural number `n`. If `n` is not a
perfect square, it returns the largest `k:β` such that `k*k β€ n`. -/
def sqrt (n : β) : β :=
match size n with
| 0 := 0
| succ s := sqrt_aux (shiftl 1 (bit0 (div2 s))) 0 n
end
-/
end pos_num
namespace num
/--
Division of `num`s, where `x / 0 = 0`.
-/
def div : num β num β num
| 0 _ := 0
| _ 0 := 0
| (pos n) (pos d) := pos_num.div' n d
/--
Modulus of `num`s.
-/
def mod : num β num β num
| 0 _ := 0
| n 0 := n
| (pos n) (pos d) := pos_num.mod' n d
instance : has_div num := β¨num.divβ©
instance : has_mod num := β¨num.modβ©
def gcd_aux : nat β num β num β num
| 0 a b := b
| (nat.succ n) 0 b := b
| (nat.succ n) a b := gcd_aux n (b % a) a
/--
Greatest Common Divisor (GCD) of two `num`s.
-/
def gcd (a b : num) : num :=
if a β€ b then
gcd_aux (a.nat_size + b.nat_size) a b
else
gcd_aux (b.nat_size + a.nat_size) b a
end num
namespace znum
/--
Division of `znum`, where `x / 0 = 0`.
-/
def div : znum β znum β znum
| 0 _ := 0
| _ 0 := 0
| (pos n) (pos d) := num.to_znum (pos_num.div' n d)
| (pos n) (neg d) := num.to_znum_neg (pos_num.div' n d)
| (neg n) (pos d) := neg (pos_num.pred' n / num.pos d).succ'
| (neg n) (neg d) := pos (pos_num.pred' n / num.pos d).succ'
/--
Modulus of `znum`s.
-/
def mod : znum β znum β znum
| 0 d := 0
| (pos n) d := num.to_znum (num.pos n % d.abs)
| (neg n) d := d.abs.sub' (pos_num.pred' n % d.abs).succ
instance : has_div znum := β¨znum.divβ©
instance : has_mod znum := β¨znum.modβ©
/--
Greatest Common Divisor (GCD) of two `znum`s.
-/
def gcd (a b : znum) : num := a.abs.gcd b.abs
end znum
section
variables {Ξ± : Type*} [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] [has_neg Ξ±]
/--
`cast_znum` casts a `znum` into any type which has `0`, `1`, `+` and `neg`
-/
def cast_znum : znum β Ξ±
| 0 := 0
| (znum.pos p) := p
| (znum.neg p) := -p
-- see Note [coercion into rings]
@[priority 900] instance znum_coe : has_coe_t znum Ξ± := β¨cast_znumβ©
instance : has_repr znum := β¨Ξ» n, repr (n : β€)β©
end
|
d32f22165412fccce458bb320858a038fc0c2300 | 57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5 | /linear_algebra/basic.lean | 2d814940f3271c8b5fa296bcd66c62355adb4ae0 | [
"Apache-2.0"
] | permissive | louisanu/mathlib | 11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe | 2bd5e2159d20a8f20d04fc4d382e65eea775ed39 | refs/heads/master | 1,617,706,993,439 | 1,523,163,654,000 | 1,523,163,654,000 | 124,519,997 | 0 | 0 | Apache-2.0 | 1,520,588,283,000 | 1,520,588,283,000 | null | UTF-8 | Lean | false | false | 37,233 | 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
Linear algebra -- classical
This file is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
We define the following concepts:
* `lc Ξ± Ξ²`: linear combinations over `Ξ²` (`Ξ±` is the scalar ring)
* `span s`: the submodule generated by `s`
* `linear_independent s`: states that `s` are linear independent
* `linear_independent.repr s b`: choose the linear combination representing `b` on the linear
independent vectors `s`. `b` should be in `span b` (uses classical choice)
* `is_basis s`: if `s` is a basis, i.e. linear independent and spans the entire space
* `is_basis.repr s b`: like `linear_independent.repr` but as a `linear_map`
* `is_basis.constr s g`: constructs a `linear_map` by extending `g` from the basis `s`
-/
import algebra algebra.big_operators order.zorn data.finset data.finsupp
noncomputable theory
open classical set function lattice
local attribute [instance] prop_decidable
reserve infix `ββ` : 50
universes u v w x y
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type y} {ΞΉ : Type x}
@[simp] lemma set.diff_self {s : set Ξ±} : s \ s = β
:=
set.ext $ by simp
lemma zero_ne_one_or_forall_eq_0 (Ξ± : Type u) [ring Ξ±] : (0 : Ξ±) β 1 β¨ (βa:Ξ±, a = 0) :=
not_or_of_imp $ Ξ» h a, by simpa using congr_arg ((*) a) h.symm
namespace finset
lemma smul_sum [ring Ξ³] [module Ξ³ Ξ²] {s : finset Ξ±} {a : Ξ³} {f : Ξ± β Ξ²} :
a β’ (s.sum f) = s.sum (Ξ»c, a β’ f c) :=
(finset.sum_hom ((β’) a) (@smul_zero Ξ³ Ξ² _ _ a) (assume _ _, smul_add)).symm
end finset
namespace finsupp
lemma smul_sum [has_zero Ξ²] [ring Ξ³] [module Ξ³ Ξ΄] {v : Ξ± ββ Ξ²} {c : Ξ³} {h : Ξ± β Ξ² β Ξ΄} :
c β’ (v.sum h) = v.sum (Ξ»a b, c β’ h a b) :=
finset.smul_sum
end finsupp
/-- The type of linear coefficients, which are simply the finitely supported functions
from the module `Ξ²` to the scalar ring `Ξ±`. -/
@[reducible] def lc (Ξ± : Type u) (Ξ² : Type v) [ring Ξ±] [module Ξ± Ξ²] : Type (max u v) := Ξ² ββ Ξ±
namespace lc
variables [ring Ξ±] [module Ξ± Ξ²]
instance : has_scalar Ξ± (lc Ξ± Ξ²) := finsupp.to_has_scalar
instance : module Ξ± (lc Ξ± Ξ²) := finsupp.to_module
lemma is_linear_map_sum [module Ξ± Ξ³] [module Ξ± Ξ΄] {f : Ξ² β Ξ± β Ξ³} {g : Ξ΄ β lc Ξ± Ξ²}
(hf : βb, is_linear_map (f b)) (hg : is_linear_map g) : is_linear_map (Ξ»d, (g d).sum f) :=
β¨assume dβ dβ, by simp [hg.add, finsupp.sum_add_index, (hf _).zero, (hf _).add],
assume a d,
by simp [hg.smul, finsupp.sum_smul_index, (hf _).zero, finsupp.smul_sum, ((hf _).smul _ _).symm]β©
end lc
namespace is_linear_map
@[simp] lemma finsupp_sum [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [has_zero Ξ΄]
{f : Ξ² β Ξ³} {t : ΞΉ ββ Ξ΄} {g : ΞΉ β Ξ΄ β Ξ²} (hf : is_linear_map f) :
f (t.sum g) = t.sum (Ξ»i d, f (g i d)) :=
hf.sum
end is_linear_map
structure linear_equiv {Ξ± : Type u} [ring Ξ±] (Ξ² : Type v) (Ξ³ : Type w) [module Ξ± Ξ²] [module Ξ± Ξ³]
extends equiv Ξ² Ξ³ :=
(linear_fun : is_linear_map to_fun)
infix ` ββ ` := linear_equiv
namespace linear_equiv
variables [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [module Ξ± Ξ΄]
include Ξ±
lemma linear_inv (e : Ξ² ββ Ξ³) : is_linear_map e.inv_fun :=
e.linear_fun.inverse e.left_inv e.right_inv
section
variable (Ξ²)
def refl : Ξ² ββ Ξ² := { linear_fun := is_linear_map.id, .. equiv.refl Ξ² }
end
def symm (e : Ξ² ββ Ξ³) : Ξ³ ββ Ξ² := { linear_fun := e.linear_inv, .. e.to_equiv.symm }
def trans (eβ : Ξ² ββ Ξ³) (eβ : Ξ³ ββ Ξ΄) : Ξ² ββ Ξ΄ :=
{ linear_fun := is_linear_map.comp eβ.linear_fun eβ.linear_fun,
.. eβ.to_equiv.trans eβ.to_equiv }
end linear_equiv
section module
variables [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [module Ξ± Ξ΄]
variables {a a' : Ξ±} {s t : set Ξ²} {b b' bβ bβ : Ξ²}
include Ξ±
/-- Linear span of a set of vectors -/
def span (s : set Ξ²) : set Ξ² := { x | β(v : lc Ξ± Ξ²), (βxβs, v x = 0) β§ x = v.sum (Ξ»b a, a β’ b) }
instance is_submodule_span : is_submodule (span s) :=
{ zero_ := β¨0,
by simp [finsupp.sum_zero_index]β©,
add_ := assume x y β¨vx, hx, eqxβ© β¨vy, hy, eqyβ©, β¨vx + vy,
by simp [hx, hy, eqx, eqy, finsupp.sum_add_index, add_smul] {contextual := tt}β©,
smul := assume a b β¨v, hv, veqβ©, β¨a β’ v,
by simp [hv, veq, finsupp.sum_smul_index, finsupp.smul_sum, smul_smul] {contextual := tt}β© }
lemma subset_span : s β span s :=
assume b (hb : b β s),
have βb'βs, b β b', by intros b' hb' ne; cc,
β¨finsupp.single b 1, by simp [finsupp.sum_single_index, this] {contextual := tt}β©
lemma span_eq_of_is_submodule (hs : is_submodule s) : span s = s :=
have span s β s,
from assume b β¨v, hv, eqβ©,
have βc, v c β’ c β s, from assume c, is_submodule.smul_ne_0 $ not_imp_comm.mp $ hv c,
eq.symm βΈ is_submodule.sum (by simp [this] {contextual := tt}),
subset.antisymm this subset_span
lemma span_mono (h : t β s) : span t β span s :=
assume b β¨v, hv, eqβ©, β¨v, assume b, hv b β mt (@h b), eqβ©
lemma span_minimal (hs : is_submodule s) (h : t β s) : span t β s :=
calc span t β span s : span_mono h
... = s : span_eq_of_is_submodule hs
lemma span_eq (hs : is_submodule s) (hts : t β s) (hst : s β span t) : span t = s :=
subset.antisymm (span_minimal hs hts) hst
@[simp] lemma span_empty : span (β
: set Ξ²) = {0} :=
span_eq is_submodule.single_zero (empty_subset _) (by simp [subset_def, is_submodule.zero])
lemma is_submodule_range_smul : is_submodule $ range (Ξ»a, a β’ b) :=
is_submodule.range $ is_linear_map.map_smul_left is_linear_map.id
lemma span_singleton : span {b} = range (Ξ»a, a β’ b) :=
span_eq is_submodule_range_smul
(assume b' hb', β¨1, by simp * at *β©)
(assume b' β¨a, eqβ©, eq βΈ is_submodule.smul _ $ subset_span $ mem_singleton _)
lemma span_union : span (s βͺ t) = {z | βxβspan s, βyβspan t, z = x + y } :=
span_eq is_submodule.add_submodule
(union_subset
(assume x hx, β¨x, subset_span hx, 0, is_submodule.zero, by simpβ©)
(assume y hy, β¨0, is_submodule.zero, y, subset_span hy, by simpβ©))
(assume b β¨x, hx, y, hy, eqβ©, eq.symm βΈ is_submodule.add
(span_mono (subset_union_left _ _) hx)
(span_mono (subset_union_right _ _) hy))
lemma span_insert_eq_span (h : b β span s) : span (insert b s) = span s :=
span_eq is_submodule_span (set.insert_subset.mpr β¨h, subset_spanβ©) (span_mono $ subset_insert _ _)
lemma span_insert : span (insert b s) = {z | βa, βxβspan s, z = a β’ b + x } :=
set.ext $ assume b',
begin
apply iff.intro; simp [insert_eq, span_union, span_singleton, set.set_eq_def, range, -add_comm],
exact (assume y a eq_y x hx eq, β¨a, x, hx, by simp [eq_y, eq]β©),
exact (assume a bβ hbβ eq, β¨a β’ b, β¨a, rflβ©, bβ, hbβ, eqβ©)
end
lemma mem_span_insert : bβ β span (insert b s) β βa, bβ + a β’ b β span s :=
begin
simp [span_insert],
constructor,
exact assume β¨a, b, hb, eqβ©, β¨-a, by simp [eq, hb]β©,
exact assume β¨a, hbβ©, β¨-a, _, hb, by simpβ©
end
@[simp] lemma span_span : span (span s) = span s :=
span_eq_of_is_submodule is_submodule_span
@[simp] lemma span_image_of_linear_map {f : Ξ² β Ξ³} (hf : is_linear_map f) :
span (f '' s) = f '' span s :=
subset.antisymm
(span_minimal (is_submodule.image hf) (image_subset _ subset_span))
(image_subset_iff.mpr $
span_minimal (is_submodule.preimage hf) (image_subset_iff.mp subset_span))
lemma linear_eq_on {f g : Ξ² β Ξ³} (hf : is_linear_map f) (hg : is_linear_map g)
(h : βxβs, f x = g x) : β{x}, x β span s β f x = g x
| _ β¨l, hl, rflβ© :=
begin
simp [hf.finsupp_sum, hg.finsupp_sum],
apply finset.sum_congr rfl,
assume b hb,
have : b β s, { by_contradiction, simp * at * },
simp [this, h, hf.smul, hg.smul]
end
/-- Linearly independent set of vectors -/
def linear_independent (s : set Ξ²) : Prop :=
βl : lc Ξ± Ξ², (βxβs, l x = 0) β l.sum (Ξ»v c, c β’ v) = 0 β l = 0
lemma linear_independent_empty : linear_independent (β
: set Ξ²) :=
assume l hl eq, finsupp.ext $ by simp * at *
lemma linear_independent.mono (hs : linear_independent s) (h : t β s) : linear_independent t :=
assume l hl eq, hs l (assume b, hl b β mt (@h b)) eq
lemma zero_not_mem_of_linear_independent (ne : 0 β (1:Ξ±)) (hs : linear_independent s) : (0:Ξ²) β s :=
assume (h : 0 β s),
let l : lc Ξ± Ξ² := finsupp.single 0 1 in
have l = 0,
from hs l
(by intro x; by_cases 0 = x; simp [l, finsupp.single_apply, *] at *)
(by simp [finsupp.sum_single_index]),
have l 0 = 1, from finsupp.single_eq_same,
by rw [βΉl = 0βΊ] at this; simp * at *
lemma linear_independent_union {s t : set Ξ²}
(hs : linear_independent s) (ht : linear_independent t) (hst : span s β© span t = {0}) :
linear_independent (s βͺ t) :=
(zero_ne_one_or_forall_eq_0 Ξ±).elim
(assume ne l hl eq0,
let ls := l.filter $ Ξ»b, b β s, lt := l.filter $ Ξ»b, b β t in
have hls : βls.support β s, by simp [ls, subset_def],
have hlt : βlt.support β t, by simp [ls, subset_def],
have lt.sum (Ξ»b a, a β’ b) β span t,
from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hlt hb,
have l = ls + lt,
from
have βb, b β s β b β t,
from assume b hbs hbt,
have b β span s β© span t, from β¨subset_span hbs, subset_span hbtβ©,
have b = 0, by rw [hst] at this; simp * at *,
zero_not_mem_of_linear_independent ne hs $ this βΈ hbs,
have lt = l.filter (Ξ»b, b β s),
from finsupp.ext $ assume b, by by_cases b β t; by_cases b β s; simp * at *,
by rw [this]; exact finsupp.filter_pos_add_filter_neg.symm,
have ls.sum (Ξ»b a, a β’ b) + lt.sum (Ξ»b a, a β’ b) = l.sum (Ξ»b a, a β’ b),
by rw [this, finsupp.sum_add_index]; simp [add_smul],
have ls_eq_neg_lt : ls.sum (Ξ»b a, a β’ b) = - lt.sum (Ξ»b a, a β’ b),
from eq_of_sub_eq_zero $ by simp [this, eq0],
have ls_sum_eq : ls.sum (Ξ»b a, a β’ b) = 0,
from
have - lt.sum (Ξ»b a, a β’ b) β span t,
from is_submodule.neg $ is_submodule.sum $
assume b hb, is_submodule.smul _ $ subset_span $ hlt hb,
have ls.sum (Ξ»b a, a β’ b) β span s β© span t,
from β¨is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hls hb,
ls_eq_neg_lt.symm βΈ thisβ©,
by rw [hst] at this; simp * at *,
have ls = 0, from hs _ (finsupp.support_subset_iff.mp hls) ls_sum_eq,
have lt_sum_eq : lt.sum (Ξ»b a, a β’ b) = 0,
from eq_of_neg_eq_neg $ by rw [βls_eq_neg_lt, ls_sum_eq]; simp,
have lt = 0, from ht _ (finsupp.support_subset_iff.mp hlt) lt_sum_eq,
by simp [βΉl = ls + ltβΊ, βΉls = 0βΊ, βΉlt = 0βΊ])
(assume eq_0 l _ _, finsupp.ext $ assume b, eq_0 _)
lemma linear_independent_Union_of_directed {s : set (set Ξ²)} (hs : βaβs, βbβs, βcβs, a βͺ b β c)
(h : βaβs, linear_independent a) : linear_independent (ββs) :=
assume l hl eq,
have βf:finset Ξ², {x | x β f} β ββ s β f = β
β¨ (βtβs, {x | x β f} β t),
from assume f, finset.induction_on f (by simp) $
assume a f haf ih haf_s,
let β¨t, ht, hatβ© := haf_s $ finset.mem_insert_self _ _ in
have f = β
β¨ β (t : set Ξ²) (H : t β s), {x : Ξ² | x β f} β t,
from ih $ assume x hx, haf_s $ finset.mem_insert_of_mem hx,
or.inr $ this.elim
(assume : f = β
, β¨t, ht, by simp [this, hat, subset_def]β©)
(assume β¨t', ht', hftβ©,
let β¨t'', ht''s, ht''β© := hs t ht t' ht' in
have a β t'',
from ht'' $ or.inl hat,
have βx, x β f β x β t'',
from subset.trans (subset.trans hft $ subset_union_right _ _) ht'',
β¨t'', ht''s, by simp [subset_def, or_imp_distrib, *] {contextual := tt}β©),
have l.support = β
β¨ (βtβs, {x | x β l.support} β t),
from this _ $ by intros x hx; by_contradiction; simp * at *,
this.elim
(assume : l.support = β
, by simp [finset.ext] at this; exact finsupp.ext this)
(assume β¨t, ht, htsβ©,
have βx, l x β 0 β x β t, by simpa using hts,
h t ht l (assume x, not_imp_comm.mp $ this x) eq)
lemma linear_independent_bUnion_of_directed {ΞΉ : Type w} {i : set ΞΉ} {s : ΞΉ β set Ξ²}
(hs : βaβi, βbβi, βcβi, s a βͺ s b β s c)
(h : βaβi, linear_independent (s a)) : linear_independent (βaβi, s a) :=
have linear_independent (ββ (s '' i)),
from linear_independent_Union_of_directed
(assume a β¨j, hj, a_eqβ© b β¨l, hl, b_eqβ©, let β¨k, hk, hβ© := hs j hj l hl in
β¨s k, mem_image_of_mem _ hk, a_eq βΈ b_eq βΈ hβ©)
(assume a β¨j, hj, a_eqβ©, a_eq βΈ h j hj),
by rwa [sUnion_image] at this
lemma linear_independent.unique (hs : linear_independent s) {lβ lβ : lc Ξ± Ξ²}
(hβ : βxβs, lβ x = 0) (hβ : βxβs, lβ x = 0) (eq : lβ.sum (Ξ»v c, c β’ v) = lβ.sum (Ξ»v c, c β’ v)) :
lβ = lβ :=
eq_of_sub_eq_zero $ show lβ - lβ = 0,
from hs (lβ - lβ)
(by simp [hβ, hβ] {contextual:=tt})
(by simp [finsupp.sum_sub_index, eq, sub_smul, -sub_eq_add_neg, sub_self])
section repr
variables (hs : linear_independent s)
def linear_independent.repr (hs : linear_independent s) (b : Ξ²) : lc Ξ± Ξ² :=
if h : b β span s then classical.some h else 0
lemma repr_not_span (h : b β span s) : hs.repr b = 0 := dif_neg h
lemma repr_spec (h : b β span s) : (βb'βs, hs.repr b b' = 0) β§ b = (hs.repr b).sum (Ξ»b a, a β’ b) :=
have hs.repr b = classical.some h, from dif_pos h,
by rw [this]; exact classical.some_spec h
lemma repr_eq_zero (hb' : b' β s) : hs.repr b b' = 0 :=
by_cases
(assume : b β span s, (repr_spec hs this).left _ hb')
(assume : b β span s, by rw [repr_not_span hs this]; refl)
lemma repr_sum_eq (hb : b β span s) : (hs.repr b).sum (Ξ»b a, a β’ b) = b :=
(repr_spec hs hb).right.symm
lemma repr_eq {l : lc Ξ± Ξ²} (hb : b β span s) (h : βxβs, l x = 0) (eq : l.sum (Ξ»v c, c β’ v) = b) :
hs.repr b = l :=
hs.unique (assume b, repr_eq_zero hs) h (by rw [repr_sum_eq hs hb, eq])
lemma repr_eq_single (hb : b β s) : hs.repr b = finsupp.single b 1 :=
repr_eq hs (subset_span hb)
(assume b' hb', finsupp.single_eq_of_ne $ show b β b', from assume eq, by simp * at *)
(by simp [finsupp.sum_single_index, add_smul])
@[simp] lemma repr_zero : hs.repr 0 = 0 :=
repr_eq hs is_submodule.zero (by simp) (by simp [finsupp.sum_zero_index])
lemma repr_support : β(hs.repr b).support β s :=
assume x hx, classical.by_contradiction $ assume hxs,
by simp at hx; exact hx (repr_eq_zero hs hxs)
@[simp] lemma repr_add (hb : b β span s) (hb' : b' β span s) :
hs.repr (b + b') = hs.repr b + hs.repr b' :=
repr_eq hs (is_submodule.add hb hb')
(by simp [repr_eq_zero] {contextual := tt})
(by simp [finsupp.sum_add_index, add_smul, repr_sum_eq hs, hb, hb'])
@[simp] lemma repr_smul (hb : b β span s) : hs.repr (a β’ b) = a β’ hs.repr b :=
repr_eq hs (is_submodule.smul _ hb)
(by simp [repr_eq_zero] {contextual := tt})
(calc (a β’ hs.repr b).sum (Ξ»b a, a β’ b) = (hs.repr b).sum (Ξ»b a', a β’ (a' β’ b)) :
by simp [finsupp.sum_smul_index, add_smul, smul_smul]
... = a β’ (hs.repr b).sum (Ξ»b a', a' β’ b) : finsupp.smul_sum.symm
... = a β’ b : by rw [repr_sum_eq hs hb])
@[simp] lemma repr_neg : hs.repr (- b) = - hs.repr b :=
by_cases
(assume hb : b β span s,
have hs.repr ((-1) β’ b) = (-1) β’ hs.repr b, from repr_smul hs hb,
by simpa)
(assume hb : b β span s,
have -b β span s, from assume hb, have - - b β span s, from is_submodule.neg hb, by simpa,
by simp [repr_not_span, this, hb])
@[simp] lemma repr_sub (hb : b β span s) (hb' : b' β span s) :
hs.repr (b - b') = hs.repr b - hs.repr b' :=
by simp [repr_add hs hb, repr_neg hs, is_submodule.neg hb']
@[simp] lemma repr_sum {ΞΉ : Type w} {f : finset ΞΉ} {b : ΞΉ β Ξ²} :
(βiβf, b i β span s) β hs.repr (f.sum b) = f.sum (Ξ»i, hs.repr (b i)) :=
by apply f.induction_on;
simp [or_imp_distrib, forall_and_distrib, repr_add hs, is_submodule.sum] {contextual := tt}
@[simp] lemma repr_finsupp_sum {ΞΉ : Type w} {Ξ΄ : Type x} [has_zero Ξ΄] {f : ΞΉ ββ Ξ΄} {b : ΞΉ β Ξ΄ β Ξ²} :
(βiβf.support, b i (f i) β span s) β hs.repr (f.sum b) = f.sum (Ξ»i d, hs.repr (b i d)) :=
repr_sum hs
lemma repr_eq_repr_of_subset {ht : linear_independent t} (h : t β s) (hb : b β span t) :
ht.repr b = hs.repr b :=
eq.symm $ repr_eq hs (span_mono h hb)
(assume x hx, repr_eq_zero _ $ assume hxt, hx $ h hxt)
(repr_sum_eq ht hb)
end repr
section
variables {f : Ξ² β Ξ³} {l : lc Ξ± Ξ²}
(hs : linear_independent (f '' s)) (hf : is_linear_map f)
(hf_inj : βaβs, βbβs, f a = f b β a = b) (hl : βxβs, l x = 0)
include hs hf hf_inj
private lemma l_eq_0 (h : f (l.sum (Ξ»b a, a β’ b)) = 0) : l = 0 :=
have l_imp_s : β{x}, l x β 0 β x β s,
from assume x hx, classical.by_contradiction $ assume hnx, hx $ hl _ $ hnx,
have βc, c β f '' s β c β (l.map_domain f).support,
from assume c, mt $ assume hb,
have c β l.support.image f, from finsupp.map_domain_support hb,
have βb, l b β 0 β§ f b = c, by simpa,
let β¨b, hb, c_eqβ© := this in
β¨b, l_imp_s hb, c_eqβ©,
have l.map_domain f = 0,
from hs _ (by simpa) $
calc (l.map_domain f).sum (Ξ»b a, a β’ b) = f (l.sum (Ξ»b a, a β’ b)):
by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul]
... = 0 : h,
calc l = l.map_domain id :
by rw [finsupp.map_domain_id]
... = l.map_domain (@inv_fun_on _ β¨0β© _ f s β f) :
finsupp.map_domain_congr $
assume b hb, (@inv_fun_on_eq' _ β¨0β© _ _ _ _ hf_inj $ l_imp_s $ by simpa using hb).symm
... = 0 :
by rw [finsupp.map_domain_comp, this, finsupp.map_domain_zero]
lemma linear_independent.of_image : linear_independent s :=
assume l hl eq, l_eq_0 hs hf hf_inj hl $ by simp [eq, hf.zero]
lemma linear_independent.eq_0_of_span : βaβspan s, f a = 0 β a = 0
| _ β¨l, hl, rflβ© eq_0 := by simp [l_eq_0 hs hf hf_inj hl eq_0, finsupp.sum_zero_index]
end
/-- A set of vectors is a basis if it is linearly independent and all vectors are in the span -/
def is_basis (s : set Ξ²) := linear_independent s β§ (βx, x β span s)
section is_basis
lemma is_basis.map_repr (hs : is_basis s) : is_linear_map hs.1.repr :=
β¨assume bβ bβ, repr_add hs.1 (hs.2 _) (hs.2 _), assume a b, repr_smul hs.1 (hs.2 _)β©
def is_basis.constr (hs : is_basis s) (f : Ξ² β Ξ³) (b : Ξ²) : Ξ³ := (hs.1.repr b).sum (Ξ»b a, a β’ f b)
lemma is_basis.map_constr (hs : is_basis s) {f : Ξ² β Ξ³} : is_linear_map (hs.constr f) :=
lc.is_linear_map_sum (assume b, is_linear_map.map_smul_left is_linear_map.id) hs.map_repr
lemma is_basis.eq_linear_map {f g : Ξ² β Ξ³} (hf : is_linear_map f) (hg : is_linear_map g)
(hs : is_basis s) (h : βbβs, f b = g b) : f = g :=
funext $ assume b, linear_eq_on hf hg h (hs.2 b)
lemma constr_congr {f g : Ξ² β Ξ³} {b : Ξ²} (hs : is_basis s) (h : βbβs, f b = g b) :
hs.constr f = hs.constr g :=
funext $ assume b', finset.sum_congr rfl $ assume b hb,
have b β s, from repr_support hs.1 hb,
by simp [h b this]
lemma constr_basis {f : Ξ² β Ξ³} {b : Ξ²} (hs : is_basis s) (hb : b β s) :
(hs.constr f : Ξ² β Ξ³) b = f b :=
show (hs.1.repr b).sum (Ξ»b a, a β’ f b) = f b,
by simp [hs.1, hs.2, hb, repr_eq_single, finsupp.sum_single_index]
lemma constr_eq {g : Ξ² β Ξ³} {f : Ξ² β Ξ³} (hs : is_basis s)
(hf : is_linear_map f) (h : βxβs, g x = f x) : hs.constr g = f :=
hs.eq_linear_map hs.map_constr hf $ assume b hb, h b hb βΈ constr_basis hs hb
lemma constr_zero (hs : is_basis s) : hs.constr (Ξ»b, (0 : Ξ³)) = (Ξ»b, 0) :=
constr_eq hs is_linear_map.map_zero $ by simp
lemma constr_add {g f : Ξ² β Ξ³} (hs : is_basis s) :
hs.constr (Ξ»b, f b + g b) = (Ξ»b, hs.constr f b + hs.constr g b) :=
constr_eq hs (is_linear_map.map_add hs.map_constr hs.map_constr) $
by simp [constr_basis hs] {contextual := tt}
lemma constr_sub {g f : Ξ² β Ξ³} (hs : is_basis s) :
hs.constr (Ξ»b, f b - g b) = (Ξ»b, hs.constr f b - hs.constr g b) :=
constr_eq hs (is_linear_map.map_sub hs.map_constr hs.map_constr) $
by simp [constr_basis hs] {contextual := tt}
lemma constr_neg {f : Ξ² β Ξ³} (hs : is_basis s) : hs.constr (Ξ»b, - f b) = (Ξ»b, - hs.constr f b) :=
constr_eq hs hs.map_constr.map_neg $ by simp [constr_basis hs] {contextual := tt}
-- this only works on functions if `Ξ±` is a commutative ring
lemma constr_smul {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [comm_ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³]
{f : Ξ² β Ξ³} {a : Ξ±} {s : set Ξ²} (hs : is_basis s) {b : Ξ²} :
hs.constr (Ξ»b, a β’ f b) = (Ξ»b, a β’ (hs.constr f) b) :=
constr_eq hs hs.map_constr.map_smul_right $ by simp [constr_basis hs] {contextual := tt}
lemma constr_mem_span (hs : is_basis s) {f : Ξ² β Ξ³} : (hs.constr f : Ξ² β Ξ³) b β span (f '' s) :=
is_submodule.sum $ assume b' hb',
have b' β s, from repr_support hs.1 hb',
is_submodule.smul _ $ subset_span $ mem_image_of_mem _ this
lemma constr_im_eq_span (hs : is_basis s) {f : Ξ² β Ξ³} : range (hs.constr f) = span (f '' s) :=
eq.symm $ span_eq (is_submodule.range hs.map_constr)
(assume b' β¨b, hb, eqβ©, β¨b, eq βΈ constr_basis hs hbβ©)
(assume b' β¨b, hbβ©, hb βΈ constr_mem_span hs)
def module_equiv_lc (hs : is_basis s) : Ξ² β (s ββ Ξ±) :=
{ to_fun := assume b, (hs.1.repr b).subtype_domain _,
inv_fun := assume v, v.sum $ Ξ»b a, a β’ b.1,
left_inv := assume b,
calc ((hs.1.repr b).subtype_domain s).sum (Ξ»b a, a β’ b.1) = (hs.1.repr b).sum (Ξ»b a, a β’ b) :
@finsupp.sum_subtype_domain_index Ξ² _ _ _ _ (Ξ»x, x β s) _ _ _ _ (Ξ»b a, a β’ b) (repr_support hs.1)
... = _ : repr_sum_eq _ $ hs.2 _,
right_inv := assume v, finsupp.ext $ assume β¨b, hbβ©,
have v.sum (Ξ»b' a, hs.1.repr (a β’ b'.val) b) = v β¨b, hbβ©,
from calc v.sum (Ξ»b' a, hs.1.repr (a β’ b'.val) b) =
v.sum (Ξ»b' a, a * (finsupp.single b'.val 1 : lc Ξ± Ξ²) b) :
finset.sum_congr rfl $ assume β¨b', hb'β© h',
by dsimp; rw [repr_smul hs.1 (hs.2 _), repr_eq_single _ hb']; refl
... = ({β¨b, hbβ©} : finset s).sum (Ξ»b', v b' * (finsupp.single b'.val 1 : lc Ξ± Ξ²) b) :
finset.sum_bij_ne_zero (Ξ»x hx x0, x)
(assume β¨x, hxβ©, by by_cases x = b; simp [*]) (by simp) (by simp)
(assume β¨x, hxβ©, by simp; intro e; subst x;
exact assume h, β¨b, hb, assume h', by simp * at *, h, rflβ©)
(by simp)
... = v β¨b, hbβ© : by simp,
begin
dsimp,
rw [repr_finsupp_sum, finsupp.sum_apply],
{ exact this },
{ simp [hs.2] }
end }
def equiv_of_is_basis {s : set Ξ²} {t : set Ξ³} {f : Ξ² β Ξ³} {g : Ξ³ β Ξ²}
(hs : is_basis s) (ht : is_basis t) (hf : βbβs, f b β t) (hg : βcβt, g c β s)
(hgf : βbβs, g (f b) = b) (hfg : βcβt, f (g c) = c) :
Ξ² ββ Ξ³ :=
{ to_fun := hs.constr f,
inv_fun := ht.constr g,
left_inv := assume b,
congr_fun (hs.eq_linear_map (ht.map_constr.comp hs.map_constr) is_linear_map.id $
by simp [constr_basis, hs, ht, hf, hgf, (β)] {contextual := tt}) b,
right_inv := assume c,
congr_fun (ht.eq_linear_map (hs.map_constr.comp ht.map_constr) is_linear_map.id $
by simp [constr_basis, hs, ht, hg, hfg, (β)] {contextual := tt}) c,
linear_fun := hs.map_constr }
end is_basis
lemma linear_independent.inj_span_iff_inj {s : set Ξ²} {f : Ξ² β Ξ³}
(hf : is_linear_map f) (hfs : linear_independent (f '' s)) :
(βaβspan s, βbβspan s, f a = f b β a = b) β (βaβs, βbβs, f a = f b β a = b) :=
iff.intro
(assume h a ha b hb eq, h a (subset_span ha) b (subset_span hb) eq)
(assume h a ha b hb eq, eq_of_sub_eq_zero $ hfs.eq_0_of_span hf h _ (is_submodule.sub ha hb)
(by simp [eq, hf.add, hf.neg]))
-- TODO: clean up proof / alternative proof
lemma linear_independent.image {s : set Ξ²} {f : Ξ² β Ξ³}
(hf : is_linear_map f) (hs : linear_independent s)
(hf_inj : βaβspan s, βbβspan s, f a = f b β a = b) :
linear_independent (f '' s) :=
let g := @inv_fun_on _ β¨0β© _ f (span s) in
have hg : βxβspan s, g (f x) = x,
from assume x, @inv_fun_on_eq' _ β¨0β© _ _ _ _ hf_inj,
assume l hl eq,
have l_g : βbβ(l.map_domain g).support, b β s, from
assume b hb,
have b β l.support.image g, from finsupp.map_domain_support hb,
have βc, l c β 0 β§ g c = b, by simpa,
let β¨c, hc, b_eqβ© := this in
have c β f '' s, by by_contradiction h; simp * at *,
let β¨b', hb', c_eqβ© := this in
have b' = b, from b_eq βΈ c_eq βΈ (hg _ $ subset_span hb').symm,
this βΈ hb',
have l_f_g : l.map_domain (f β g) = l.map_domain id, from
finsupp.map_domain_congr $ assume c hc,
have c β f '' s, by by_contradiction h; simp * at *,
let β¨b, hb, c_eqβ© := this in
by simp [c_eq.symm, (β), hg, subset_span hb],
have l.map_domain g = 0, from
have l_g_s : (l.map_domain g).sum (Ξ»b a, a β’ b) β span s,
from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ l_g b hb,
have f_sum : f ((l.map_domain g).sum (Ξ»b a, a β’ b)) = 0,
from calc f ((l.map_domain g).sum (Ξ»b a, a β’ b)) =
((l.map_domain g).map_domain f).sum (Ξ»b a, a β’ b) :
by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul]
... = 0 :
by rw [βfinsupp.map_domain_comp, l_f_g, finsupp.map_domain_id, eq],
have βbβs, (l.map_domain g) b = 0,
from assume b hb, classical.by_contradiction $ assume hnb, hb $ l_g b $ by simp *,
hs _ this $ hf_inj _ l_g_s _ is_submodule.zero (by simpa [hf.zero] using f_sum),
calc l = (l.map_domain g).map_domain f :
by rw [βfinsupp.map_domain_comp, l_f_g, finsupp.map_domain_id]
... = 0 :
by rw [this, finsupp.map_domain_zero]
lemma linear_map.linear_independent_image_iff {s : set Ξ²} {f : Ξ² β Ξ³}
(hf : is_linear_map f) (hf_inj : βaβspan s, βbβspan s, f a = f b β a = b) :
linear_independent (f '' s) β linear_independent s :=
iff.intro
(assume h, h.of_image hf $ assume x hx y hy, hf_inj x (subset_span hx) y (subset_span hy))
(assume h, h.image hf hf_inj)
end module
section vector_space
variables [field Ξ±] [vector_space Ξ± Ξ²] [vector_space Ξ± Ξ³] {s t : set Ξ²} {b bβ bβ : Ξ²}
include Ξ±
local attribute [instance] is_submodule_span
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type classs) -/
lemma mem_span_insert_exchange : bβ β span (insert bβ s) β bβ β span s β bβ β span (insert bβ s) :=
begin
simp [span_insert],
exact assume a bβ hbβ bβ_eq hbβ,
have a β 0, from assume a0, by simp * at *,
β¨1/a, (- 1/a) β’ bβ, is_submodule.smul _ hbβ,
by simp [bβ_eq, smul_add, smul_smul, mul_inv_cancel, this, neg_div]β©
end
lemma linear_independent_iff_not_mem_span : linear_independent s β (βbβs, b β span (s \ {b})) :=
iff.intro
(assume (hs : linear_independent s) b hb β¨l, hl, b_eqβ©,
let l' := l - finsupp.single b 1 in
have βb', b' β s β l' b' = 0,
from assume b' hb',
have ne: b β b', from assume h, hb' $ h βΈ hb,
have b' β s \ {b}, from assume β¨hβ, hββ©, hb' hβ,
by simp [ne, hl b' this],
have l' = 0, from hs l' this $
by simp [l', finsupp.sum_add_index, finsupp.sum_neg_index, add_smul, b_eq.symm, finsupp.sum_single_index],
have - l' b = 1, from have b β s \ {b}, by simp, by simp [hl _ this],
by rw [βΉl' = 0βΊ] at this; simp at this; assumption)
(assume hs l hl eq, finsupp.ext $ assume b, classical.by_contradiction $ assume h : l b β 0,
let a := -1 / l b in
hs b
(show b β s, from classical.by_contradiction $ assume hnb, h $ hl b hnb)
β¨a β’ l + finsupp.single b 1,
assume b', by_cases
(assume : b' = b, by simp [this, h, neg_div, a])
(assume : b' β b, by simp [finsupp.sub_apply, hl, this, this.symm] {contextual:=tt}),
have l.sum (Ξ»b a', (a * a') β’ b) = a β’ l.sum (Ξ»b a, a β’ b),
by simp [finsupp.smul_sum, smul_smul],
by simp [-sub_eq_add_neg, add_smul, finsupp.sum_add_index, finsupp.sum_single_index,
finsupp.sum_smul_index, this, eq]β©)
lemma linear_independent_singleton {b : Ξ²} (hb : b β 0) : linear_independent ({b} : set Ξ²) :=
linear_independent_iff_not_mem_span.mpr $ by simp [hb] {contextual := tt}
lemma linear_independent.insert (hs : linear_independent s) (hb : b β span s) :
linear_independent (insert b s) :=
assume l hl eq, by_cases
(assume : l b = 0,
hs l (assume x hx,
by_cases (assume h : x = b, h.symm βΈ this) (assume h', hl _ $ by simp [not_or_distrib, hx, h']))
eq)
(assume lb_ne_zero : l b β 0,
have (1 / l b) β’ (- (- l b β’ b)) β span s,
from is_submodule.smul _ $ is_submodule.neg β¨l - finsupp.single b (l b),
assume x hx, by_cases
(assume : b = x, by simp [this.symm])
(assume ne : b β x,
have x β insert b s, by simp [not_or_distrib, hx, ne.symm],
by simp [hl x this, ne] {contextual := tt}),
by simp [finsupp.sum_sub_index, finsupp.sum_single_index, -sub_eq_add_neg, sub_smul, eq]; simpβ©,
have (1 / l b) β’ (- (- l b β’ b)) = b,
by simp [smul_smul, mul_comm, mul_inv_cancel lb_ne_zero],
by simp * at *)
lemma exists_linear_independent (hs : linear_independent s) (hst : s β t) :
βbβt, s β b β§ t β span b β§ linear_independent b :=
let C := { b : set Ξ² // s β b β§ b β t β§ linear_independent b }, s' : C := β¨s, le_refl s, hst, hsβ© in
have βc, zorn.chain (Ξ»a b:C, a.val β b.val) c β c β β
β β(m : C), βa:C, a β c β a.val β m.val, from
assume c hc ne,
let β¨a, haβ© := exists_mem_of_ne_empty ne in
β¨β¨(βa β c, (a : C).val),
subset.trans a.property.1 $ subset_bUnion_of_mem ha,
bUnion_subset $ assume c hc, c.property.right.left,
linear_independent_bUnion_of_directed
(assume a ha b hb, by_cases
(assume h : a = b, β¨a, ha, h βΈ le_of_eq (@sup_idem (set _) _ a.val)β©)
(assume h : a β b, (hc a ha b hb h).elim
(assume h, β¨b, hb, union_subset h (subset.refl _)β©)
(assume h, β¨a, ha, union_subset (subset.refl _) hβ©)))
(assume a ha, a.property.2.2)β©,
assume a ha, subset_bUnion_of_mem haβ©,
have βm:C, βa:C, m.val β a.val β a.val β m.val, from
zorn.zorn
(assume c hc, by_cases (assume : c = β
, β¨s', assume a, this.symm βΈ false.elimβ©) (this c hc))
(assume a b c, subset.trans),
let β¨β¨m, hsm, hmt, hmlβ©, hmβ© := this in
have t β span m,
from classical.by_contradiction $ assume : Β¬ t β span m,
let β¨b, hbβ© := classical.not_forall.mp this, β¨hbt, hbmβ© := not_imp.mp hb in
have insert b m β m,
from hm
β¨_, subset.trans hsm $ subset_insert _ _, by simp [set.insert_subset, hmt, hbt], hml.insert hbmβ©
(subset_insert _ _),
have b β span m, from subset_span $ this $ mem_insert _ _,
hbm this,
β¨m, hmt, hsm, this, hmlβ©
lemma exists_subset_is_basis (hs : linear_independent s) : βb, s β b β§ is_basis b :=
let β¨b, hbβ, hbβ, hbβ, hbββ© := exists_linear_independent hs (@subset_univ _ _) in
β¨b, hbβ, hbβ, assume x, hbβ trivialβ©
variable (Ξ²)
lemma exists_is_basis : βb : set Ξ², is_basis b :=
let β¨b, _, hbβ© := exists_subset_is_basis linear_independent_empty in β¨b, hbβ©
variable {Ξ²}
lemma eq_of_linear_independent_of_span
(hs : linear_independent s) (h : t β s) (hst : s β span t) : s = t :=
suffices s β t, from subset.antisymm this h,
assume b hb,
have (hs.mono h).repr b = finsupp.single b 1,
from calc (hs.mono h).repr b = hs.repr b : repr_eq_repr_of_subset hs h $ hst hb
... = finsupp.single b 1 : repr_eq_single hs hb,
have b β (β((hs.mono h).repr b).support : set Ξ²), by simp [this],
repr_support _ this
lemma exists_of_linear_independent_of_finite_span {t : finset Ξ²}
(hs : linear_independent s) (hst : s β span βt) :
βt':finset Ξ², βt' β s βͺ βt β§ s β βt' β§ t'.card = t.card :=
have βt, β(s' : finset Ξ²), βs' β s β s β© βt = β
β s β span β(s' βͺ t) β
βt':finset Ξ², βt' β s βͺ βt β§ s β βt' β§ t'.card = (s' βͺ t).card :=
assume t, finset.induction_on t
(assume s' hs' _ hss',
have s = βs', from eq_of_linear_independent_of_span hs hs' $ by simpa using hss',
β¨s', by simp [this]β©)
(assume bβ t hbβt ih s' hs' hst hss',
have hbβs : bβ β s,
from assume h,
have bβ β s β© β(insert bβ t), from β¨h, finset.mem_insert_self _ _β©,
by rwa [hst] at this,
have hbβs' : bβ β s', from assume h, hbβs $ hs' h,
have hst : s β© βt = β
,
from eq_empty_of_subset_empty $ subset.trans
(by simp [inter_subset_inter, subset.refl]) (le_of_eq hst),
by_cases
(assume : s β span β(s' βͺ t),
let β¨u, hust, hsu, eqβ© := ih _ hs' hst this in
have hbβu : bβ β u, from assume h, (hust h).elim hbβs hbβt,
β¨insert bβ u, by simp [set.insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hbβt, hbβs', hbβu]β©)
(assume : Β¬ s β span β(s' βͺ t),
let β¨bβ, hbβs, hbβtβ© := set.not_subset.mp this in
have hbβt' : bβ β s' βͺ t, from assume h, hbβt $ subset_span h,
have s β span β(insert bβ s' βͺ t), from
assume bβ hbβ,
have β(s' βͺ insert bβ t) β insert bβ (insert bβ β(s' βͺ t) : set Ξ²),
by simp [insert_eq, union_subset_union, subset.refl, subset_union_right],
have hbβ : bβ β span (insert bβ (insert bβ β(s' βͺ t) : set Ξ²)),
from span_mono this (hss' hbβ),
have s β span (insert bβ β(s' βͺ t)),
by simpa [insert_eq] using hss',
have hbβ : bβ β span (insert bβ β(s' βͺ t)),
from mem_span_insert_exchange (this hbβs) hbβt,
by rw [span_insert_eq_span hbβ] at hbβ; simpa using hbβ,
let β¨u, hust, hsu, eqβ© := ih _ (by simp [set.insert_subset, hbβs, hs']) hst this in
β¨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by rw [finset.union_comm] at hbβt'; simp [eq, hbβt', hbβt, hbβs']β©)),
have eq : t.filter (Ξ»x, x β s) βͺ t.filter (Ξ»x, x β s) = t,
from finset.ext.mpr $ assume x, by by_cases x β s; simp *,
let β¨u, hβ, hβ, hβ© := this (t.filter (Ξ»x, x β s)) (t.filter (Ξ»x, x β s))
(by simp [set.subset_def]) (by simp [set.set_eq_def] {contextual := tt}) (by rwa [eq]) in
β¨u, subset.trans hβ (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
hβ, by rwa [eq] at hβ©
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : finite t) (hs : linear_independent s) (hst : s β span t) :
βh : finite s, h.to_finset.card β€ ht.to_finset.card :=
have s β span β(ht.to_finset), by simp; assumption,
let β¨u, hust, hsu, eqβ© := exists_of_linear_independent_of_finite_span hs this in
have finite s, from finite_subset u.finite_to_set hsu,
β¨this, by rw [βeq]; exact (finset.card_le_of_subset $ finset.coe_subseteq_coe.mp $ by simp [hsu])β©
lemma exists_left_inverse_linear_map_of_injective {f : Ξ² β Ξ³}
(hf : is_linear_map f) (hf_inj : injective f) : βg:Ξ³ β Ξ², is_linear_map g β§ g β f = id :=
let β¨bΞ², hbΞ²β© := exists_is_basis Ξ² in
have linear_independent (f '' bΞ²),
from hbΞ².1.image hf $ assume bβ _ bβ _ eq, hf_inj eq,
let β¨bΞ³, hbΞ³β, hbΞ³ββ© := exists_subset_is_basis this in
have βbβbΞ², (hbΞ³β.constr (@inv_fun _ β¨0β© _ f) : Ξ³ β Ξ²) (f b) = b,
begin
assume b hb,
rw [constr_basis],
{ exact @inv_fun_on_eq' Ξ² β¨0β© Ξ³ f univ b (assume bβ _ bβ _ eq, hf_inj eq) trivial },
{ exact hbΞ³β (mem_image_of_mem _ hb) }
end,
β¨hbΞ³β.constr $ @inv_fun _ β¨0β© _ f, hbΞ³β.map_constr,
hbΞ².eq_linear_map (hbΞ³β.map_constr.comp hf) is_linear_map.id thisβ©
lemma exists_right_inverse_linear_map_of_surjective {f : Ξ² β Ξ³}
(hf : is_linear_map f) (hf_surj : surjective f) :
βg:Ξ³ β Ξ², is_linear_map g β§ f β g = id :=
let g := @inv_fun _ β¨0β© _ f in
have ri_gf : right_inverse g f, from @right_inverse_inv_fun _ β¨0β© _ _ hf_surj,
have injective g, from injective_of_left_inverse ri_gf,
let β¨bΞ³, hbΞ³β© := exists_is_basis Ξ³ in
have βcβbΞ³, f ((hbΞ³.constr g : Ξ³ β Ξ²) c) = c,
from assume c hc, by rw [constr_basis hbΞ³ hc, ri_gf],
β¨hbΞ³.constr g, hbΞ³.map_constr, hbΞ³.eq_linear_map (hf.comp hbΞ³.map_constr) is_linear_map.id thisβ©
end vector_space
|
6309cfcb09830cedd3130dd68be0e36f1bd339aa | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/order/order_dual.lean | 66b43e9a72b55637571f5db0866f87eceb6155a4 | [
"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 | 2,286 | lean | /-
Copyright (c) 2020 Johan Commelin, Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Damiano Testa
-/
import order.basic
import data.equiv.basic
/-!
# Initial lemmas to work with the `order_dual`
## Definitions
`to_dual` and `of_dual` the order reversing identity maps, bundled as equivalences.
## Basic Lemmas to convert between an order and its dual
This file is similar to algebra/group/type_tags.lean
-/
open function
universes u v w
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {r : Ξ± β Ξ± β Prop}
namespace order_dual
/-- `to_dual` is the identity function to the `order_dual` of a linear order. -/
def to_dual : Ξ± β order_dual Ξ± := β¨id, id, Ξ» h, rfl, Ξ» h, rflβ©
/-- `of_dual` is the identity function from the `order_dual` of a linear order. -/
def of_dual : order_dual Ξ± β Ξ± := to_dual.symm
@[simp] lemma to_dual_symm_eq : (@to_dual Ξ±).symm = of_dual := rfl
@[simp] lemma of_dual_symm_eq : (@of_dual Ξ±).symm = to_dual := rfl
@[simp] lemma to_dual_of_dual (a : order_dual Ξ±) : to_dual (of_dual a) = a := rfl
@[simp] lemma of_dual_to_dual (a : Ξ±) : of_dual (to_dual a) = a := rfl
@[simp] lemma to_dual_inj {a b : Ξ±} :
to_dual a = to_dual b β a = b := iff.rfl
@[simp] lemma to_dual_le_to_dual [has_le Ξ±] {a b : Ξ±} :
to_dual a β€ to_dual b β b β€ a := iff.rfl
@[simp] lemma to_dual_lt_to_dual [has_lt Ξ±] {a b : Ξ±} :
to_dual a < to_dual b β b < a := iff.rfl
@[simp] lemma of_dual_inj {a b : order_dual Ξ±} :
of_dual a = of_dual b β a = b := iff.rfl
@[simp] lemma of_dual_le_of_dual [has_le Ξ±] {a b : order_dual Ξ±} :
of_dual a β€ of_dual b β b β€ a := iff.rfl
@[simp] lemma of_dual_lt_of_dual [has_lt Ξ±] {a b : order_dual Ξ±} :
of_dual a < of_dual b β b < a := iff.rfl
lemma le_to_dual [has_le Ξ±] {a : order_dual Ξ±} {b : Ξ±} :
a β€ to_dual b β b β€ of_dual a := iff.rfl
lemma lt_to_dual [has_lt Ξ±] {a : order_dual Ξ±} {b : Ξ±} :
a < to_dual b β b < of_dual a := iff.rfl
lemma to_dual_le [has_le Ξ±] {a : Ξ±} {b : order_dual Ξ±} :
to_dual a β€ b β of_dual b β€ a := iff.rfl
lemma to_dual_lt [has_lt Ξ±] {a : Ξ±} {b : order_dual Ξ±} :
to_dual a < b β of_dual b < a := iff.rfl
end order_dual
|
0d59e401053641dcb6be61b56c06711bf6cbce4c | 1963cb8789bf8cf695607cc2d4106d7fdd8fb9f2 | /homework3/solutions3.lean | d6bcf877f0cbe8f2c8c6e02a148dc14e1ecbd9ff | [] | no_license | mtegene/homework-mtegene-master | ed43dd0b8cbcea936b6eec82c614842eca02a406 | 5f9b3623a46d2d3b98c54d8cbc82962d3fef49f5 | refs/heads/master | 1,610,605,612,607 | 1,488,481,866,000 | 1,488,481,866,000 | 81,875,687 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,119 | lean | namespace nat
/-
Part 1. Summation.
-/
-- define summation up to (but not including) n
def sum_upto (n : β) (f : β β β) : β :=
nat.rec_on n 0 (Ξ» n recval, recval + f n)
-- the computation rules
theorem sum_upto_zero (f : β β β) : sum_upto 0 f = 0 := rfl
theorem sum_upto_succ (f : β β β) (n : β) : sum_upto (succ n) f = sum_upto n f + f n := rfl
-- fill in the next two proofs
theorem sum_upto_mul (n m : β) (f : β β β) : sum_upto n (Ξ» x, m * f x) = m * (sum_upto n f) :=
nat.induction_on n rfl
(take n,
assume ih: sum_upto n (Ξ» x, m * f x) = m * (sum_upto n f),
show sum_upto (succ n) (Ξ» x, m * f x) = m * (sum_upto (succ n) f), from
calc
sum_upto (succ n) (Ξ» x, m * f x) = sum_upto n (Ξ» x, m * f x) + (Ξ» x, m * f x) n : by rw sum_upto_succ
... = m * (sum_upto n f) + (Ξ» x, m * f x) n : by rw ih
... = m * (sum_upto n f) + m * ((Ξ» x, f x ) n) : rfl
... = m * (sum_upto n f + (Ξ» x, f x) n) : by rw mul_add
... = m * (sum_upto n f + f n) : rfl
... = m * (sum_upto (succ n) f) : by rw sum_upto_succ)
theorem sum_upto_id (n : β) : 2 * sum_upto (succ n) id = n * (n + 1) :=
nat.induction_on n rfl
(take n,
assume ih: 2 * sum_upto (succ n) id = n * (n + 1),
show 2 * sum_upto (succ (succ n)) id = (succ n) * ((succ n) + 1), from
calc
2 * sum_upto (succ (succ n)) id = 2 * (sum_upto (succ n) id + id (succ n)) : by rw sum_upto_succ
... = 2 * (sum_upto (succ n) id) + 2 * id(succ n) : by rw mul_add
... = n * (n + 1) + 2 * id (succ n) : by rw ih
... = n * (n + 1) + 2 * (succ n) : rfl
... = n * (succ n) + 2 * (succ n) : rfl
... = (succ n) * n + 2 * (succ n) : by rw mul_comm
... = (succ n) * n + (succ n) * 2 : by simp --by rw mul_comm
... = (succ n) * (n + 2) : by rw mul_add
... = (succ n) * (n + 1 + 1) : rfl
... = (succ n) * ((succ n) + 1) : rfl)
/-
Part 2. Exponentiation on nat.
-/
-- the definition
def pow : β β β β β
| m 0 := 1
| m (succ n) := m * pow m n
-- declare the notation
infix ^ := pow
-- the computation rules
theorem pow_zero (m : β) : m ^ 0 = 1 := rfl
theorem pow_succ (m n : β) : m ^ (succ n) = m * m ^ n := rfl
-- fill in the next four proofs
theorem zero_pow_succ (n : β) : 0^(succ n) = 0 :=
nat.induction_on n rfl
( take n,
assume ih,
show 0^(succ (succ n)) = 0, from
calc
0^(succ (succ n)) = 0 * 0^(succ n) : by rw pow_succ
... = 0 * 0 : by rw ih
... = 0 : by rw mul_zero)
theorem pow_add (m n k : β) : m ^ (n + k) = m ^ n * m ^ k :=
nat.induction_on n
(calc
m ^ (0 + k) = m ^ k : by rw zero_add
... = 1 * m ^ k : by rw one_mul
... = m ^ 0 * m ^ k : rfl)
(take n,
assume ih,
show m ^ ((succ n) + k) = m ^ (succ n) * m ^ k, from
calc
m ^ ((succ n) + k) = m ^ ((n + 1) + k) : rfl
... = m ^ (k + (n + 1)) : by rw add_comm
... = m ^ ((k + n) + 1) : by rw add_assoc
... = m ^ ((n + k) + 1) : by simp
... = m ^ (succ (n + k)) : rfl
... = m * m ^ (n + k) : by rw pow_succ
... = m * (m ^ n * m ^ k) : by rw ih
... = (m * m ^ n) * m ^ k : by rw mul_assoc
... = m ^ (succ n) * m ^ k : by rw pow_succ)
theorem pow_mul (m n k : β) : m ^ (n * k) = (m ^ n) ^ k :=
nat.induction_on k
(calc
m ^ (n * 0) = m ^ (0) : by rw mul_zero
... = 1 : by rw pow_zero
... = (m ^ n) ^ 0 : by rw pow_zero)
(take k,
assume ih,
show m ^ (n * (succ k)) = (m ^ n) ^ (succ k), from
calc
m ^ (n * (succ k)) = m ^ (n * (k + 1)) : rfl
... = m ^ (n * k + n * 1) : by rw mul_add
... = m ^ (n * k) * m ^ (n * 1) : sorry
... = (m ^ n) ^ k * m ^ (n * 1) : by rw ih
... = (m ^ n) ^ k * (m ^ n) : by rw mul_one
... = (m ^ n) * (m ^ n) ^ k : by rw mul_comm
... = (m ^ n) ^ (succ k) : by rw pow_succ)
check zero_lt_one
check @mul_pos
theorem pow_pos {m : β} (h : m > 0) {n : β} : m^n > 0 :=
nat.induction_on n
( have H1: m^0 = 1, from rfl,
show m^0 > 0, from zero_lt_one)
( take n,
assume ih,
have H1: m^ (succ n) = m * m^n, from rfl,
show m^(succ n) > 0, from mul_pos h ih)
/- The last one is pow_le, below. It is not easy, so just give it your best
shot. The next few examples might be helpful. -/
-- By the way, this is a useful trick. I will explain it in class.
example : 3 < 7 := dec_trivial
check @mul_le_mul
check @mul_le_mul_left
check @nat.zero_le
example (m : β) (h : m > 0) : m β₯ 1 := succ_le_of_lt h
-- actually, on β, x < y is defined to mean succ x < y
example (m : β) (h : m > 0) : m β₯ 1 := h
example (m n : β) (h : m β€ n) : β k, m + k = n := le.dest h
lemma le_mul_self (m n : β) (h : m = 0 β¨ n > 0) : m β€ m * n :=
or.elim h
(suppose m = 0,
begin simp [this] end)
(suppose n > 0,
have m * 1 β€ m * n,
from mul_le_mul_left _ this,
begin
rw mul_one at this, exact this
end)
check @mul_le_mul_left
-- Be careful! The following theorem is false without the hypotheses m > 0 (why?)
theorem pow_le (m : β) {n k : β} (h : n β€ k) (mpos : m > 0) : m^n β€ m^k :=
have H1: m*n β€ m*k, from (mul_le_mul_left m h),
sorry
--Are you supposed to use induction? Because I couldn't figure out what I
--was supposed to induct on.
--Without induction, I do not know how to expand m ^ n since ^ is defined for
--m ^ succ n
check le_of_eq
/-
Part 3. Division on the Natural Numbers.
These have nothing to do with inductive types (except for the fact that
the existential quantifier is really an inductive type).
-/
protected def dvd (m n : β) : Prop := β k, n = m * k
instance : has_dvd nat := β¨nat.dvdβ©
-- type "dvd" with \|
theorem dvd_rfl (m : β) : m β£ m :=
exists.intro 1 (by rw [(mul_one m)])
theorem dvd_trans : β {m n k : β}, m β£ n β n β£ k β m β£ k :=
take m n k,
assume h1 h2,
have H1: β a, n = m * a, from h1,
have H2: β b, k = n * b, from h2,
exists.elim H1
(take a,
assume H3: n = m * a,
exists.elim H2
(take b,
assume H4: k = n * b,
show β c, k = m * c, from
(have H5: k = m * a * b, from (by rw [-H3, H4]),
have H6: k = m * (a * b), from (by rw [H5, mul_assoc]),
exists.intro (a * b) H6)))
theorem dvd_mul_left (m n : β) : m β£ m * n :=
have H1: m β£ m, from (dvd_rfl m),
have H2: β a, m = m * a, from H1,
exists.elim H2
(take a,
assume H3: m = m * a,
have H4: m * n = m * a * n, from (by rw [-H3]),
have H5: m * n = m * (a * n), from (by rw [H4, mul_assoc]),
show m β£ m * n , from exists.intro (a * n) H5)
theorem dvd_mul_right (m n : β) : m β£ n * m :=
have H1: m β£ m * n, from (dvd_mul_left m n),
have H2: β a, m * n = m * a, from H1,
exists.elim H2
(take a,
assume H3: m * n = m * a,
have H4: n * m = m * a, from (by rw [-H3, mul_comm]),
show m β£ n * m, from exists.intro a H4)
theorem dvd_add : β {m n k : β}, m β£ n β m β£ k β m β£ (n + k) :=
take m n k,
take hβ hβ,
have H1: β a, n = m * a, from hβ,
have H2: β b, k = m * b, from hβ,
exists.elim H1
(take a,
assume H3: n = m * a,
exists.elim H2
(take b,
assume H4: k = m * b,
have H5: n + k = m * (a + b), from (by rw [H3, H4, mul_add]),
show m β£ (n + k), from exists.intro (a + b) H5))
end nat
/-
Part 4. The size of a binary tree.
This one is really optional, for those of you who said you want something
more challenging.
-/
open nat
inductive btree : Type
| leaf : btree
| node : btree β btree β btree
namespace btree
def size : btree β β
| leaf := 1
| (node bβ bβ) := size bβ + size bβ + 1
theorem size_pos (b : btree) : size b > 0 :=
btree.induction_on b
dec_trivial
(take bβ bβ,
assume ihβ ihβ,
succ_pos _)
def depth : btree β β
| leaf := 1
| (node bβ bβ) := max (depth bβ) (depth bβ) + 1
-- This is a scandal. I promise, eventually the simplifier will do this.
lemma add_self_eq_two_mul (m : β) : m + m = 2 * m :=
calc
m + m = (1 + 1) * m : by simp [add_mul, one_mul]
... = 2 * m : rfl
-- these might be useful.
check @nat.sub_add_cancel
check @nat.add_sub_assoc
-- prove this by induction on binary trees
theorem size_le' : β b : btree, size b + 1 β€ 2 ^ depth b :=
take b,
btree.induction_on b
dec_trivial
(take bβ bβ,
assume ihβ ihβ,
have H1: (size bβ + 1) + (size bβ + 1) β€ (2 ^ depth bβ) + (2 ^ depth bβ), from (add_le_add ihβ ihβ),
have H2: 2 ^ (max (depth bβ) (depth bβ) + 1) = 2 ^ depth (node bβ bβ), from rfl,
have H3: 2 ^ (max (depth bβ) (depth bβ) + 1) β€ 2 ^ depth (node bβ bβ), from (le_of_eq H2),
calc
size (node bβ bβ) + 1 = (size bβ + size bβ + 1) + 1 : rfl
... = (size bβ + 1) + (size bβ + 1) : by simp
... β€ (2 ^ depth bβ) + (2 ^ depth bβ) : H1
... β€ 2 ^ (max (depth bβ) (depth bβ) + 1) : sorry
... = 2 ^ depth (node bβ bβ) : rfl
... β€ 2 ^ depth (node bβ bβ) : H3)
check @add_le_add
theorem size_le : β b : btree, size b β€ 2 ^ depth b - 1:=
take b,
have size b = size b + 1 - 1, from rfl,
calc
size b = size b + 1 - 1 : this
... β€ 2 ^ depth b - 1 : nat.sub_le_sub_right (size_le' b) _
end btree
|
8af170213916e699968ebee5910db549c3db5d8b | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /tests/lean/hott/670.hlean | 39d0769109784360b0bf6608f6be56590559b06b | [
"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 | 487 | hlean | open equiv
constants (A B : Typeβ) (f : A β B) (g : B β A) (p : Ξ b, f (g b) = b) (q : Ξ a, g (f a) = a)
definition e [constructor] : A β B :=
equiv.MK f g p q
example (b : B) : g (f (g b)) = g b :=
by rewrite [to_right_inv e b]
example (b : B) : g (f (g b)) = g b :=
by xrewrite [to_right_inv e b]
example (b : B) : g (f (g b)) = g b :=
by krewrite [to_right_inv e b]
example (b : B) : g (f (g b)) = g b :=
begin
let H := to_right_inv e b,
esimp at H,
rewrite H
end
|
33ed1cbb6a39fc2b93751e639a382e29421ef357 | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/category_theory/natural_transformation.lean | c8bd4c7dd357955b92f71f3cf254161e702abf7e | [
"Apache-2.0"
] | permissive | johoelzl/mathlib | 253f46daa30b644d011e8e119025b01ad69735c4 | 592e3c7a2dfbd5826919b4605559d35d4d75938f | refs/heads/master | 1,625,657,216,488 | 1,551,374,946,000 | 1,551,374,946,000 | 98,915,829 | 0 | 0 | Apache-2.0 | 1,522,917,267,000 | 1,501,524,499,000 | Lean | UTF-8 | Lean | false | false | 4,235 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baumann, Stephen Morgan, Scott Morrison
Defines natural transformations between functors.
Introduces notations
`F βΉ G` for the type of natural transformations between functors `F` and `G`,
`Ο.app X` for the components of natural transformations,
`Ο β Ο` for vertical compositions, and
`Ο β« Ο` for horizontal compositions.
-/
import category_theory.functor
namespace category_theory
universes vβ vβ vβ vβ uβ uβ uβ uβ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type uβ} [π : category.{vβ} C] {D : Type uβ} [π : category.{vβ} D]
include π π
/--
`nat_trans F G` represents a natural transformation between functors `F` and `G`.
The field `app` provides the components of the natural transformation.
Naturality is expressed by `Ξ±.naturality_lemma`.
-/
structure nat_trans (F G : C β₯€ D) : Type (max uβ vβ) :=
(app : Ξ X : C, (F.obj X) βΆ (G.obj X))
(naturality' : β {X Y : C} (f : X βΆ Y), (F.map f) β« (app Y) = (app X) β« (G.map f) . obviously)
infixr ` βΉ `:50 := nat_trans -- type as \==> or βΉ
restate_axiom nat_trans.naturality'
namespace nat_trans
/-- `nat_trans.id F` is the identity natural transformation on a functor `F`. -/
protected def id (F : C β₯€ D) : F βΉ F :=
{ app := Ξ» X, π (F.obj X) }
@[simp] lemma id_app (F : C β₯€ D) (X : C) : (nat_trans.id F).app X = π (F.obj X) := rfl
open category
open category_theory.functor
section
variables {F G H I : C β₯€ D}
-- We'll want to be able to prove that two natural transformations are equal if they are componentwise equal.
@[extensionality] lemma ext (Ξ± Ξ² : F βΉ G) (w : β X : C, Ξ±.app X = Ξ².app X) : Ξ± = Ξ² :=
begin
induction Ξ± with Ξ±_components Ξ±_naturality,
induction Ξ² with Ξ²_components Ξ²_naturality,
have hc : Ξ±_components = Ξ²_components := funext w,
subst hc
end
lemma congr_app {Ξ± Ξ² : F βΉ G} (h : Ξ± = Ξ²) (X : C) : Ξ±.app X = Ξ².app X := by rw h
/-- `vcomp Ξ± Ξ²` is the vertical compositions of natural transformations. -/
def vcomp (Ξ± : F βΉ G) (Ξ² : G βΉ H) : F βΉ H :=
{ app := Ξ» X, (Ξ±.app X) β« (Ξ².app X),
naturality' := begin /- `obviously'` says: -/ intros, simp, rw [βassoc, naturality, assoc, βnaturality], end }
infixr ` β `:80 := vcomp
@[simp] lemma vcomp_app (Ξ± : F βΉ G) (Ξ² : G βΉ H) (X : C) : (Ξ± β Ξ²).app X = (Ξ±.app X) β« (Ξ².app X) := rfl
@[simp] lemma vcomp_assoc (Ξ± : F βΉ G) (Ξ² : G βΉ H) (Ξ³ : H βΉ I) : (Ξ± β Ξ²) β Ξ³ = Ξ± β (Ξ² β Ξ³) := by tidy
end
variables {E : Type uβ} [β° : category.{vβ} E]
include β°
/-- `hcomp Ξ± Ξ²` is the horizontal composition of natural transformations. -/
def hcomp {F G : C β₯€ D} {H I : D β₯€ E} (Ξ± : F βΉ G) (Ξ² : H βΉ I) : (F β H) βΉ (G β I) :=
{ app := Ξ» X : C, (Ξ².app (F.obj X)) β« (I.map (Ξ±.app X)),
naturality' := begin
/- `obviously'` says: -/
intros,
dsimp,
simp,
-- Actually, obviously doesn't use exactly this sequence of rewrites, but achieves the same result
rw [β assoc, naturality, assoc],
conv { to_rhs, rw [β map_comp, β Ξ±.naturality, map_comp] }
end }
infix ` β« `:80 := hcomp
@[simp] lemma hcomp_app {F G : C β₯€ D} {H I : D β₯€ E} (Ξ± : F βΉ G) (Ξ² : H βΉ I) (X : C) :
(Ξ± β« Ξ²).app X = (Ξ².app (F.obj X)) β« (I.map (Ξ±.app X)) := rfl
-- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we need to use associativity of functor composition
lemma exchange {F G H : C β₯€ D} {I J K : D β₯€ E} (Ξ± : F βΉ G) (Ξ² : G βΉ H) (Ξ³ : I βΉ J) (Ξ΄ : J βΉ K) :
((Ξ± β Ξ²) β« (Ξ³ β Ξ΄)) = ((Ξ± β« Ξ³) β (Ξ² β« Ξ΄)) :=
begin
-- `obviously'` says:
ext,
intros,
dsimp,
simp,
-- again, this isn't actually what obviously says, but it achieves the same effect.
conv { to_lhs, congr, skip, rw [βassoc, βnaturality, assoc] }
end
end nat_trans
end category_theory
|
5748a86305a0560c108dc1d7d6dc4ea7eb908e4c | de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41 | /expressions/scalar_expr.lean | 3a349d6744b973fff86d3597889bd7bde7ffa2ab | [] | no_license | kevinsullivan/lang | d3e526ba363dc1ddf5ff1c2f36607d7f891806a7 | e9d869bff94fb13ad9262222a6f3c4aafba82d5e | refs/heads/master | 1,687,840,064,795 | 1,628,047,969,000 | 1,628,047,969,000 | 282,210,749 | 0 | 1 | null | 1,608,153,830,000 | 1,595,592,637,000 | Lean | UTF-8 | Lean | false | false | 3,169 | lean | import .expr_base
import ...phys.scalar
structure scalar_var extends var
inductive scalar_expr : Type
| lit (s : scalar) : scalar_expr
| var (v : scalar_var) : scalar_expr
| add_scalar_scalar (s1 : scalar_expr) (s2 : scalar_expr) : scalar_expr
| mul_scalar_scalar (s1 : scalar_expr) (s2 : scalar_expr) : scalar_expr
| inv_scalar (s1 : scalar_expr) : scalar_expr
abbreviation scalar_env := scalar_var β scalar
abbreviation scalar_eval :=
scalar_env β scalar_expr β scalar
@[simp]
def default_scalar_env
: scalar_env :=
Ξ»v, 1
@[simp]
def default_scalar_eval
: scalar_eval :=
Ξ»env_, Ξ»expr_, 1
@[simp]
noncomputable def static_scalar_eval
: scalar_eval
| env_ (scalar_expr.lit s) := s
| env_ (scalar_expr.var v) := (env_ v)
| env_ (scalar_expr.add_scalar_scalar s1 s2) := (static_scalar_eval env_ s1) + (static_scalar_eval env_ s2)
| env_ (scalar_expr.mul_scalar_scalar s1 s2) := (static_scalar_eval env_ s1) * (static_scalar_eval env_ s2)
| env_ (scalar_expr.inv_scalar s1) := 1/(static_scalar_eval env_ s1)
@[simp]
noncomputable def scalar_expr.value
(expr_ : scalar_expr) : scalar :=
static_scalar_eval default_scalar_env expr_
notation `|`slit`|` := scalar_expr.lit slit
notation s1`+`s2 := scalar_expr.add_scalar_scalar s1 s2
notation s1`*`s2 := scalar_expr.mul_scalar_scalar s1 s2
notation s1`/`s2 := scalar_expr.mul_scalar_scalar s1 (scalar_expr.inv_scalar s2)
notation s1β»ΒΉ := scalar_expr.inv_scalar s1
structure real_scalar_var extends var
inductive real_scalar_expr : Type
| lit (s : real_scalar) : real_scalar_expr
| var (v : real_scalar_var) : real_scalar_expr
| add_real_scalar_real_scalar (s1 : real_scalar_expr) (s2 : real_scalar_expr) : real_scalar_expr
| mul_real_scalar_real_scalar (s1 : real_scalar_expr) (s2 : real_scalar_expr) : real_scalar_expr
| inv_real_scalar (s1 : real_scalar_expr) : real_scalar_expr
abbreviation real_scalar_env := real_scalar_var β real_scalar
abbreviation real_scalar_eval :=
real_scalar_env β real_scalar_expr β real_scalar
def default_real_scalar_env
: real_scalar_env :=
Ξ»v, 1
def default_real_scalar_eval
: real_scalar_eval :=
Ξ»env_, Ξ»expr_, 1
noncomputable def static_real_scalar_eval
: real_scalar_eval
| env_ (real_scalar_expr.lit s) := s
| env_ (real_scalar_expr.var v) := (env_ v)
| env_ (real_scalar_expr.add_real_scalar_real_scalar s1 s2) := (static_real_scalar_eval env_ s1) + (static_real_scalar_eval env_ s2)
| env_ (real_scalar_expr.mul_real_scalar_real_scalar s1 s2) := (static_real_scalar_eval env_ s1) * (static_real_scalar_eval env_ s2)
| env_ (real_scalar_expr.inv_real_scalar s1) := 1/(static_real_scalar_eval env_ s1)
noncomputable def real_scalar_expr.value
(expr_ : real_scalar_expr) : real_scalar :=
static_real_scalar_eval default_real_scalar_env expr_
notation `|`slit`|` := real_scalar_expr.lit slit
notation s1`+`s2 := real_scalar_expr.add_real_scalar_real_scalar s1 s2
notation s1`*`s2 := real_scalar_expr.mul_real_scalar_real_scalar s1 s2
notation s1`/`s2 := real_scalar_expr.mul_real_scalar_real_scalar s1 (real_scalar_expr.inv_real_scalar s2)
notation s1β»ΒΉ := real_scalar_expr.inv_real_scalar s1 |
7658307083d8b6d5ffa29f999720372fbc5d127d | b29f946a2f0afd23ef86b9219116968babbb9f4f | /src/lean_format_tests/patrick.lean | 9eb9e2973875115e9ec60c0e241ff6783e5ec331 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M1P1-lean | 58be7394fded719d95e45e6b10e1ecf2ed3c7c4c | 3723468cc50f8bebd00a9811caf25224a578de17 | refs/heads/master | 1,587,063,867,779 | 1,572,727,164,000 | 1,572,727,164,000 | 165,845,802 | 14 | 4 | Apache-2.0 | 1,549,730,698,000 | 1,547,554,675,000 | Lean | UTF-8 | Lean | false | false | 3,827 | lean | -- begin header
import tactic.linarith
import data.real.basic
import algebra.pi_instances
import data.set.function
noncomputable theory
local attribute [instance, priority 0] classical.prop_decidable
-- We introduce the usual mathematical notation for absolute value
local notation `|` x `|` := abs x
-- end header
/- Section
Limits of sequences
-/
/- Sub-section
Basic definitions
-/
/-
In this file, we introduce limits of sequences of real numbers.
We model a sequence $aβ, aβ, aβ, \dots$ of real numbers as a function
from $β := \{0,1,2,\dots\}$ to $β$, sending $n$ to $a_n$. So in the below
definition of the limit of a sequence, $a : β β β$ is the sequence.
-/
/- Definition
A sequence $a$ converges to a real number $l$ if, for all positive
$Ξ΅$, there is some $N$ such that, for every $n β₯ N$, $|a_n - l| < Ξ΅$.
-/
definition is_limit (a : β β β) (l : β) : Prop :=
β Ξ΅ > 0, β N, β n β₯ N, | a n - l | < Ξ΅
/- Definition
A sequence converges if and only if it has a limit.
-/
definition has_limit (a : β β β) : Prop := β l : β, is_limit a l
/-
The difference between the above definition and the preceding one is that we
don't specify the limit, we just claim that it exists.
-/
/- Sub-section
Basic lemmas
-/
/- Lemma
The constant sequence with value $a$ converges to $a$.
-/
lemma tendsto_const (a : β) : is_limit (Ξ» n, a) a :=
begin
-- Let $Ξ΅$ be any positive real number.
intros Ξ΅ Ξ΅pos,
-- We choose $N = 0$ in the definition of a limit,
use 0,
-- and observe that, for every $n β₯ N$, |a - a| < Ξ΅
intros n _,
simpa [sub_self] using Ξ΅pos
end
/-
We will need an easy reformulation of the limit definition
-/
/- Lemma
A sequence $a_n$ converges to a number $l$ if and only if the sequence
$a_n - l$ converges to zero.
-/
lemma tendsto_iff_sub_tendsto_zero {a : β β β} {l : β} :
is_limit (Ξ» n, a n - l) 0 β is_limit a l :=
begin
-- We need to prove both implications, but both proofs are the same.
split ;
{ -- We assume the premise, and consider any positive $Ξ΅$.
intros h Ξ΅ Ξ΅pos,
-- By the premise specialized to our $Ξ΅$, we get some $N$,
rcases h Ξ΅ Ξ΅pos with β¨N, Hβ©,
-- and use that $N$ to prove the other condition
use N,
-- which is immediate.
intros n hn,
simpa using H n hn }
end
/-
In the definition of a limit, the final Ξ΅ can be replaced
by a constant multiple of Ξ΅. We could assume this constant is positive
but we don't want to deal with this when applying the lemma.
-/
/- Lemma
Let $a$ be a sequence. In order to prove that $a$ converges to some limit
$l$, it is sufficient to find some number $K$ such that,
for all $Ξ΅ > 0$, there is some $N$ such that, for all $n β₯ N$,
$|a_n - l| < KΞ΅$.
-/
lemma tendsto_of_mul_eps (a : β β β) (l : β) (K : β)
(h : β Ξ΅ > 0, β N, β n β₯ N, | a n - l | < K*Ξ΅) : is_limit a l :=
begin
-- Let $Ξ΅$ be any positive number.
intros Ξ΅ Ξ΅pos,
-- $K$ is either non positive or positive
cases le_or_gt K 0 with Knonpos Kpos,
{ -- If $K$ is non positive then our assumed bound quickly
-- gives a contradiction.
exfalso,
-- Indeed we can apply our assumption to $Ξ΅ = 1$ to get $N$ such that
-- for all $n β₯ N$, $|a n - l| < K * 1$
rcases h 1 (by linarith) with β¨N, Hβ©,
-- in particular this holds when $n = N$
specialize H N (by linarith),
-- but $|a N - l| β₯ 0$ so we get a contradiction.
have : |a N - l| β₯ 0, from abs_nonneg _,
linarith },
{ -- Now assume $K$ is positive. Our assumption gives $N$ such that,
-- for all $n β₯ N$, $|a n - l| < K * (Ξ΅ / K)$
rcases h (Ξ΅/K) (div_pos Ξ΅pos Kpos) with β¨N, Hβ©,
-- we can simplify that $K (Ξ΅ / K)$ and we are done.
rw mul_div_cancel' _ (ne_of_gt Kpos) at H,
tauto }
end
|
8d96bcdfeb2a313b66a8a825c8a8dea0d764d6a9 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/probability/strong_law.lean | e09fa4393ca78d48814facb7b7a784ec0bee6a08 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 36,978 | 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 probability.ident_distrib
import measure_theory.integral.interval_integral
import analysis.specific_limits.floor_pow
import analysis.p_series
import analysis.asymptotics.specific_asymptotics
/-!
# The strong law of large numbers
We prove the strong law of large numbers, in `probability_theory.strong_law_ae`:
If `X n` is a sequence of independent identically distributed integrable real-valued random
variables, then `β i in range n, X i / n` converges almost surely to `πΌ[X 0]`.
We give here the strong version, due to Etemadi, that only requires pairwise independence.
This file also contains the Lα΅ version of the strong law of large numbers provided by
`probability_theory.strong_law_Lp` which shows `β i in range n, X i / n` converges in Lα΅ to
`πΌ[X 0]` provided `X n` is independent identically distributed and is Lα΅.
## Implementation
We follow the proof by Etemadi
[Etemadi, *An elementary proof of the strong law of large numbers*][etemadi_strong_law],
which goes as follows.
It suffices to prove the result for nonnegative `X`, as one can prove the general result by
splitting a general `X` into its positive part and negative part.
Consider `Xβ` a sequence of nonnegative integrable identically distributed pairwise independent
random variables. Let `Yβ` be the truncation of `Xβ` up to `n`. We claim that
* Almost surely, `Xβ = Yβ` for all but finitely many indices. Indeed, `β β (Xβ β Yβ)` is bounded by
`1 + πΌ[X]` (see `sum_prob_mem_Ioc_le` and `tsum_prob_mem_Ioi_lt_top`).
* Let `c > 1`. Along the sequence `n = c ^ k`, then `(β_{i=0}^{n-1} Yα΅’ - πΌ[Yα΅’])/n` converges almost
surely to `0`. This follows from a variance control, as
```
β_k β (|β_{i=0}^{c^k - 1} Yα΅’ - πΌ[Yα΅’]| > c^k Ξ΅)
β€ β_k (c^k Ξ΅)^{-2} β_{i=0}^{c^k - 1} Var[Yα΅’] (by Markov inequality)
β€ β_i (C/i^2) Var[Yα΅’] (as β_{c^k > i} 1/(c^k)^2 β€ C/i^2)
β€ β_i (C/i^2) πΌ[Yα΅’^2]
β€ 2C πΌ[X^2] (see `sum_variance_truncation_le`)
```
* As `πΌ[Yα΅’]` converges to `πΌ[X]`, it follows from the two previous items and Cesaro that, along
the sequence `n = c^k`, one has `(β_{i=0}^{n-1} Xα΅’) / n β πΌ[X]` almost surely.
* To generalize it to all indices, we use the fact that `β_{i=0}^{n-1} Xα΅’` is nondecreasing and
that, if `c` is close enough to `1`, the gap between `c^k` and `c^(k+1)` is small.
-/
noncomputable theory
open measure_theory filter finset asymptotics
open set (indicator)
open_locale topology big_operators measure_theory probability_theory ennreal nnreal
namespace probability_theory
/-! ### Prerequisites on truncations -/
section truncation
variables {Ξ± : Type*}
/-- Truncating a real-valued function to the interval `(-A, A]`. -/
def truncation (f : Ξ± β β) (A : β) :=
(indicator (set.Ioc (-A) A) id) β f
variables {m : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Ξ± β β}
lemma _root_.measure_theory.ae_strongly_measurable.truncation
(hf : ae_strongly_measurable f ΞΌ) {A : β} :
ae_strongly_measurable (truncation f A) ΞΌ :=
begin
apply ae_strongly_measurable.comp_ae_measurable _ hf.ae_measurable,
exact (strongly_measurable_id.indicator measurable_set_Ioc).ae_strongly_measurable,
end
lemma abs_truncation_le_bound (f : Ξ± β β) (A : β) (x : Ξ±) :
|truncation f A x| β€ |A| :=
begin
simp only [truncation, set.indicator, set.mem_Icc, id.def, function.comp_app],
split_ifs,
{ exact abs_le_abs h.2 (neg_le.2 h.1.le) },
{ simp [abs_nonneg] }
end
@[simp] lemma truncation_zero (f : Ξ± β β) :
truncation f 0 = 0 :=
by simp [truncation]
lemma abs_truncation_le_abs_self (f : Ξ± β β) (A : β) (x : Ξ±) :
|truncation f A x| β€ |f x| :=
begin
simp only [truncation, indicator, set.mem_Icc, id.def, function.comp_app],
split_ifs,
{ exact le_rfl },
{ simp [abs_nonneg] },
end
lemma truncation_eq_self {f : Ξ± β β} {A : β} {x : Ξ±} (h : |f x| < A) :
truncation f A x = f x :=
begin
simp only [truncation, indicator, set.mem_Icc, id.def, function.comp_app, ite_eq_left_iff],
assume H,
apply H.elim,
simp [(abs_lt.1 h).1, (abs_lt.1 h).2.le],
end
lemma truncation_eq_of_nonneg {f : Ξ± β β} {A : β} (h : β x, 0 β€ f x) :
truncation f A = (indicator (set.Ioc 0 A) id) β f :=
begin
ext x,
rcases (h x).lt_or_eq with hx|hx,
{ simp only [truncation, indicator, hx, set.mem_Ioc, id.def, function.comp_app, true_and],
by_cases h'x : f x β€ A,
{ have : - A < f x, by linarith [h x],
simp only [this, true_and] },
{ simp only [h'x, and_false] } },
{ simp only [truncation, indicator, hx, id.def, function.comp_app, if_t_t]},
end
lemma truncation_nonneg {f : Ξ± β β} (A : β) {x : Ξ±} (h : 0 β€ f x) : 0 β€ truncation f A x :=
set.indicator_apply_nonneg $ Ξ» _, h
lemma _root_.measure_theory.ae_strongly_measurable.mem_βp_truncation [is_finite_measure ΞΌ]
(hf : ae_strongly_measurable f ΞΌ) {A : β} {p : ββ₯0β} :
mem_βp (truncation f A) p ΞΌ :=
mem_βp.of_bound hf.truncation (|A|) (eventually_of_forall (Ξ» x, abs_truncation_le_bound _ _ _))
lemma _root_.measure_theory.ae_strongly_measurable.integrable_truncation [is_finite_measure ΞΌ]
(hf : ae_strongly_measurable f ΞΌ) {A : β} :
integrable (truncation f A) ΞΌ :=
by { rw β mem_βp_one_iff_integrable, exact hf.mem_βp_truncation }
lemma moment_truncation_eq_interval_integral (hf : ae_strongly_measurable f ΞΌ) {A : β}
(hA : 0 β€ A) {n : β} (hn : n β 0) :
β« x, (truncation f A x) ^ n βΞΌ = β« y in (-A)..A, y ^ n β(measure.map f ΞΌ) :=
begin
have M : measurable_set (set.Ioc (-A) A) := measurable_set_Ioc,
change β« x, (Ξ» z, (indicator (set.Ioc (-A) A) id z) ^ n) (f x) βΞΌ = _,
rw [β integral_map hf.ae_measurable, interval_integral.integral_of_le, β integral_indicator M],
{ simp only [indicator, zero_pow' _ hn, id.def, ite_pow] },
{ linarith },
{ exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable }
end
lemma moment_truncation_eq_interval_integral_of_nonneg (hf : ae_strongly_measurable f ΞΌ) {A : β}
{n : β} (hn : n β 0) (h'f : 0 β€ f) :
β« x, (truncation f A x) ^ n βΞΌ = β« y in 0..A, y ^ n β(measure.map f ΞΌ) :=
begin
have M : measurable_set (set.Ioc 0 A) := measurable_set_Ioc,
have M' : measurable_set (set.Ioc A 0) := measurable_set_Ioc,
rw truncation_eq_of_nonneg h'f,
change β« x, (Ξ» z, (indicator (set.Ioc 0 A) id z) ^ n) (f x) βΞΌ = _,
rcases le_or_lt 0 A with hA | hA,
{ rw [β integral_map hf.ae_measurable, interval_integral.integral_of_le hA,
β integral_indicator M],
{ simp only [indicator, zero_pow' _ hn, id.def, ite_pow] },
{ exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable } },
{ rw [β integral_map hf.ae_measurable, interval_integral.integral_of_ge hA.le,
β integral_indicator M'],
{ simp only [set.Ioc_eq_empty_of_le hA.le, zero_pow' _ hn, set.indicator_empty, integral_zero,
zero_eq_neg],
apply integral_eq_zero_of_ae,
have : βα΅ x β(measure.map f ΞΌ), (0 : β) β€ x :=
(ae_map_iff hf.ae_measurable measurable_set_Ici).2 (eventually_of_forall h'f),
filter_upwards [this] with x hx,
simp only [indicator, set.mem_Ioc, pi.zero_apply, ite_eq_right_iff, and_imp],
assume h'x h''x,
have : x = 0, by linarith,
simp [this, zero_pow' _ hn] },
{ exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable } }
end
lemma integral_truncation_eq_interval_integral (hf : ae_strongly_measurable f ΞΌ) {A : β}
(hA : 0 β€ A) :
β« x, truncation f A x βΞΌ = β« y in (-A)..A, y β(measure.map f ΞΌ) :=
by simpa using moment_truncation_eq_interval_integral hf hA one_ne_zero
lemma integral_truncation_eq_interval_integral_of_nonneg (hf : ae_strongly_measurable f ΞΌ) {A : β}
(h'f : 0 β€ f) :
β« x, truncation f A x βΞΌ = β« y in 0..A, y β(measure.map f ΞΌ) :=
by simpa using moment_truncation_eq_interval_integral_of_nonneg hf one_ne_zero h'f
lemma integral_truncation_le_integral_of_nonneg
(hf : integrable f ΞΌ) (h'f : 0 β€ f) {A : β} :
β« x, truncation f A x βΞΌ β€ β« x, f x βΞΌ :=
begin
apply integral_mono_of_nonneg (eventually_of_forall (Ξ» x, _)) hf (eventually_of_forall (Ξ» x, _)),
{ exact truncation_nonneg _ (h'f x) },
{ calc truncation f A x β€ |truncation f A x| : le_abs_self _
... β€ |f x| : abs_truncation_le_abs_self _ _ _
... = f x : abs_of_nonneg (h'f x) }
end
/-- If a function is integrable, then the integral of its truncated versions converges to the
integral of the whole function. -/
lemma tendsto_integral_truncation {f : Ξ± β β} (hf : integrable f ΞΌ) :
tendsto (Ξ» A, β« x, truncation f A x βΞΌ) at_top (π (β« x, f x βΞΌ)) :=
begin
refine tendsto_integral_filter_of_dominated_convergence (Ξ» x, abs (f x)) _ _ _ _,
{ exact eventually_of_forall (Ξ» A, hf.ae_strongly_measurable.truncation) },
{ apply eventually_of_forall (Ξ» A, _),
apply eventually_of_forall (Ξ» x, _),
rw real.norm_eq_abs,
exact abs_truncation_le_abs_self _ _ _ },
{ apply hf.abs },
{ apply eventually_of_forall (Ξ» x, _),
apply tendsto_const_nhds.congr' _,
filter_upwards [Ioi_mem_at_top (abs (f x))] with A hA,
exact (truncation_eq_self hA).symm },
end
lemma ident_distrib.truncation {Ξ² : Type*} [measurable_space Ξ²] {Ξ½ : measure Ξ²}
{f : Ξ± β β} {g : Ξ² β β} (h : ident_distrib f g ΞΌ Ξ½) {A : β} :
ident_distrib (truncation f A) (truncation g A) ΞΌ Ξ½ :=
h.comp (measurable_id.indicator measurable_set_Ioc)
end truncation
section strong_law_ae
variables {Ξ© : Type*} [measure_space Ξ©] [is_probability_measure (β : measure Ξ©)]
section moment_estimates
lemma sum_prob_mem_Ioc_le
{X : Ξ© β β} (hint : integrable X) (hnonneg : 0 β€ X) {K : β} {N : β} (hKN : K β€ N) :
β j in range K, β {Ο | X Ο β set.Ioc (j : β) N} β€ ennreal.of_real (πΌ[X] + 1) :=
begin
let Ο : measure β := measure.map X β,
haveI : is_probability_measure Ο := is_probability_measure_map hint.ae_measurable,
have A : β j in range K, β« x in j..N, (1 : β) βΟ β€ πΌ[X] + 1, from calc
β j in range K, β« x in j..N, (1 : β) βΟ
= β j in range K, β i in Ico j N, β« x in i..(i+1 : β), (1 : β) βΟ :
begin
apply sum_congr rfl (Ξ» j hj, _),
rw interval_integral.sum_integral_adjacent_intervals_Ico ((mem_range.1 hj).le.trans hKN),
assume k hk,
exact continuous_const.interval_integrable _ _,
end
... = β i in range N, β j in range (min (i+1) K), β« x in i..(i+1 : β), (1 : β) βΟ :
begin
simp_rw [sum_sigma'],
refine sum_bij' (Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ (Ξ» a ha, rfl)
(Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ _ _,
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range, mem_Ico] at hij,
simp only [hij, nat.lt_succ_iff.2 hij.2.1, mem_sigma, mem_range, lt_min_iff, and_self] },
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range, lt_min_iff] at hij,
simp only [hij, nat.lt_succ_iff.1 hij.2.1, mem_sigma, mem_range, mem_Ico, and_self] },
{ rintros β¨i, jβ© hij, refl },
{ rintros β¨i, jβ© hij, refl },
end
... β€ β i in range N, (i + 1) * β« x in i..(i+1 : β), (1 : β) βΟ :
begin
apply sum_le_sum (Ξ» i hi, _),
simp only [nat.cast_add, nat.cast_one, sum_const, card_range, nsmul_eq_mul, nat.cast_min],
refine mul_le_mul_of_nonneg_right (min_le_left _ _) _,
apply interval_integral.integral_nonneg,
{ simp only [le_add_iff_nonneg_right, zero_le_one] },
{ simp only [zero_le_one, implies_true_iff], }
end
... β€ β i in range N, β« x in i..(i+1 : β), (x + 1) βΟ :
begin
apply sum_le_sum (Ξ» i hi, _),
have I : (i : β) β€ (i + 1 : β),
by simp only [nat.cast_add, nat.cast_one, le_add_iff_nonneg_right, zero_le_one],
simp_rw [interval_integral.integral_of_le I, β integral_mul_left],
apply set_integral_mono_on,
{ exact continuous_const.integrable_on_Ioc },
{ exact (continuous_id.add continuous_const).integrable_on_Ioc },
{ exact measurable_set_Ioc },
{ assume x hx,
simp only [nat.cast_add, nat.cast_one, set.mem_Ioc] at hx,
simp [hx.1.le] }
end
... = β« x in 0..N, x + 1 βΟ :
begin
rw interval_integral.sum_integral_adjacent_intervals (Ξ» k hk, _),
{ norm_cast },
{ exact (continuous_id.add continuous_const).interval_integrable _ _ }
end
... = β« x in 0..N, x βΟ + β« x in 0..N, 1 βΟ :
begin
rw interval_integral.integral_add,
{ exact continuous_id.interval_integrable _ _ },
{ exact continuous_const.interval_integrable _ _ },
end
... = πΌ[truncation X N] + β« x in 0..N, 1 βΟ :
by rw integral_truncation_eq_interval_integral_of_nonneg hint.1 hnonneg
... β€ πΌ[X] + β« x in 0..N, 1 βΟ :
add_le_add_right (integral_truncation_le_integral_of_nonneg hint hnonneg) _
... β€ πΌ[X] + 1 :
begin
refine add_le_add le_rfl _,
rw interval_integral.integral_of_le (nat.cast_nonneg _),
simp only [integral_const, measure.restrict_apply', measurable_set_Ioc, set.univ_inter,
algebra.id.smul_eq_mul, mul_one],
rw β ennreal.one_to_real,
exact ennreal.to_real_mono ennreal.one_ne_top prob_le_one,
end,
have B : β a b, β {Ο | X Ο β set.Ioc a b} = ennreal.of_real (β« x in set.Ioc a b, (1 : β) βΟ),
{ assume a b,
rw [of_real_set_integral_one Ο _,
measure.map_apply_of_ae_measurable hint.ae_measurable measurable_set_Ioc],
refl },
calc β j in range K, β {Ο | X Ο β set.Ioc (j : β) N}
= β j in range K, ennreal.of_real (β« x in set.Ioc (j : β) N, (1 : β) βΟ) :
by simp_rw B
... = ennreal.of_real (β j in range K, β« x in set.Ioc (j : β) N, (1 : β) βΟ) :
begin
rw ennreal.of_real_sum_of_nonneg,
simp only [integral_const, algebra.id.smul_eq_mul, mul_one, ennreal.to_real_nonneg,
implies_true_iff],
end
... = ennreal.of_real (β j in range K, β« x in (j : β)..N, (1 : β) βΟ) :
begin
congr' 1,
refine sum_congr rfl (Ξ» j hj, _),
rw interval_integral.integral_of_le (nat.cast_le.2 ((mem_range.1 hj).le.trans hKN)),
end
... β€ ennreal.of_real (πΌ[X] + 1) : ennreal.of_real_le_of_real A
end
lemma tsum_prob_mem_Ioi_lt_top
{X : Ξ© β β} (hint : integrable X) (hnonneg : 0 β€ X) :
β' (j : β), β {Ο | X Ο β set.Ioi (j : β)} < β :=
begin
suffices : β (K : β), β j in range K, β {Ο | X Ο β set.Ioi (j : β)} β€ ennreal.of_real (πΌ[X] + 1),
from (le_of_tendsto_of_tendsto (ennreal.tendsto_nat_tsum _) tendsto_const_nhds
(eventually_of_forall this)).trans_lt ennreal.of_real_lt_top,
assume K,
have A : tendsto (Ξ» (N : β), β j in range K, β {Ο | X Ο β set.Ioc (j : β) N})
at_top (π (β j in range K, β {Ο | X Ο β set.Ioi (j : β)})),
{ refine tendsto_finset_sum _ (Ξ» i hi, _),
have : {Ο | X Ο β set.Ioi (i : β)} = β (N : β), {Ο | X Ο β set.Ioc (i : β) N},
{ apply set.subset.antisymm _ _,
{ assume Ο hΟ,
obtain β¨N, hNβ© : β (N : β), X Ο β€ N := exists_nat_ge (X Ο),
exact set.mem_Union.2 β¨N, hΟ, hNβ© },
{ simp only [set.mem_Ioc, set.mem_Ioi, set.Union_subset_iff, set.set_of_subset_set_of,
implies_true_iff] {contextual := tt} } },
rw this,
apply tendsto_measure_Union,
assume m n hmn x hx,
exact β¨hx.1, hx.2.trans (nat.cast_le.2 hmn)β© },
apply le_of_tendsto_of_tendsto A tendsto_const_nhds,
filter_upwards [Ici_mem_at_top K] with N hN,
exact sum_prob_mem_Ioc_le hint hnonneg hN
end
lemma sum_variance_truncation_le
{X : Ξ© β β} (hint : integrable X) (hnonneg : 0 β€ X) (K : β) :
β j in range K, ((j : β) ^ 2) β»ΒΉ * πΌ[(truncation X j) ^ 2] β€ 2 * πΌ[X] :=
begin
set Y := Ξ» (n : β), truncation X n,
let Ο : measure β := measure.map X β,
have Y2 : β n, πΌ[Y n ^ 2] = β« x in 0..n, x ^ 2 βΟ,
{ assume n,
change πΌ[Ξ» x, (Y n x)^2] = _,
rw [moment_truncation_eq_interval_integral_of_nonneg hint.1 two_ne_zero hnonneg] },
calc β j in range K, ((j : β) ^ 2) β»ΒΉ * πΌ[Y j ^ 2]
= β j in range K, ((j : β) ^ 2) β»ΒΉ * β« x in 0..j, x ^ 2 βΟ :
by simp_rw [Y2]
... = β j in range K, ((j : β) ^ 2) β»ΒΉ * β k in range j, β« x in k..(k+1 : β), x ^ 2 βΟ :
begin
congr' 1 with j,
congr' 1,
rw interval_integral.sum_integral_adjacent_intervals,
{ norm_cast },
assume k hk,
exact (continuous_id.pow _).interval_integrable _ _,
end
... = β k in range K, (β j in Ioo k K, ((j : β) ^ 2) β»ΒΉ) * β« x in k..(k+1 : β), x ^ 2 βΟ :
begin
simp_rw [mul_sum, sum_mul, sum_sigma'],
refine sum_bij' (Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ (Ξ» a ha, rfl)
(Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ _ _,
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range, mem_filter] at hij,
simp [hij, mem_sigma, mem_range, and_self, hij.2.trans hij.1], },
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range, mem_Ioo] at hij,
simp only [hij, mem_sigma, mem_range, and_self], },
{ rintros β¨i, jβ© hij, refl },
{ rintros β¨i, jβ© hij, refl },
end
... β€ β k in range K, (2/ (k+1)) * β« x in k..(k+1 : β), x ^ 2 βΟ :
begin
apply sum_le_sum (Ξ» k hk, _),
refine mul_le_mul_of_nonneg_right (sum_Ioo_inv_sq_le _ _) _,
refine interval_integral.integral_nonneg_of_forall _ (Ξ» u, sq_nonneg _),
simp only [nat.cast_add, nat.cast_one, le_add_iff_nonneg_right, zero_le_one],
end
... β€ β k in range K, β« x in k..(k+1 : β), 2 * x βΟ :
begin
apply sum_le_sum (Ξ» k hk, _),
have Ik : (k : β) β€ (k + 1 : β), by simp,
rw [β interval_integral.integral_const_mul, interval_integral.integral_of_le Ik,
interval_integral.integral_of_le Ik],
refine set_integral_mono_on _ _ measurable_set_Ioc (Ξ» x hx, _),
{ apply continuous.integrable_on_Ioc,
exact continuous_const.mul (continuous_pow 2) },
{ apply continuous.integrable_on_Ioc,
exact continuous_const.mul continuous_id' },
{ calc 2 / (βk + 1) * x ^ 2 = (x / (k+1)) * (2 * x) : by ring_exp
... β€ 1 * (2 * x) :
mul_le_mul_of_nonneg_right begin
apply_mod_cast (div_le_one _).2 hx.2,
simp only [nat.cast_add, nat.cast_one],
linarith only [show (0 : β) β€ k, from nat.cast_nonneg k],
end (mul_nonneg zero_le_two ((nat.cast_nonneg k).trans hx.1.le))
... = 2 * x : by rw one_mul }
end
... = 2 * β« x in (0 : β)..K, x βΟ :
begin
rw interval_integral.sum_integral_adjacent_intervals (Ξ» k hk, _),
swap, { exact (continuous_const.mul continuous_id').interval_integrable _ _ },
rw interval_integral.integral_const_mul,
norm_cast
end
... β€ 2 * πΌ[X] :
mul_le_mul_of_nonneg_left begin
rw β integral_truncation_eq_interval_integral_of_nonneg hint.1 hnonneg,
exact integral_truncation_le_integral_of_nonneg hint hnonneg,
end zero_le_two
end
end moment_estimates
section strong_law_nonneg
/- This paragraph proves the strong law of large numbers (almost sure version, assuming only
pairwise independence) for nonnegative random variables, following Etemadi's proof. -/
variables (X : β β Ξ© β β) (hint : integrable (X 0))
(hindep : pairwise (Ξ» i j, indep_fun (X i) (X j)))
(hident : β i, ident_distrib (X i) (X 0))
(hnonneg : β i Ο, 0 β€ X i Ο)
include X hint hindep hident hnonneg
/- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers (with respect to
the truncated expectation) along the sequence `c^n`, for any `c > 1`, up to a given `Ξ΅ > 0`.
This follows from a variance control. -/
lemma strong_law_aux1 {c : β} (c_one : 1 < c) {Ξ΅ : β} (Ξ΅pos : 0 < Ξ΅) :
βα΅ Ο, βαΆ (n : β) in at_top,
|β i in range βc^nββ, truncation (X i) i Ο - πΌ[β i in range βc^nββ, truncation (X i) i]|
< Ξ΅ * βc^nββ :=
begin
/- Let `S n = β i in range n, Y i` where `Y i = truncation (X i) i`. We should show that
`|S k - πΌ[S k]| / k β€ Ξ΅` along the sequence of powers of `c`. For this, we apply Borel-Cantelli:
it suffices to show that the converse probabilites are summable. From Chebyshev inequality, this
will follow from a variance control `β' Var[S (c^i)] / (c^i)^2 < β`. This is checked in `I2` using
pairwise independence to expand the variance of the sum as the sum of the variances, and then
a straightforward but tedious computation (essentially boiling down to the fact that the sum of
`1/(c ^ i)^2` beyong a threshold `j` is comparable to `1/j^2`).
Note that we have written `c^i` in the above proof sketch, but rigorously one should put integer
parts everywhere, making things more painful. We write `u i = βc^iββ` for brevity. -/
have c_pos : 0 < c := zero_lt_one.trans c_one,
let Ο : measure β := measure.map (X 0) β,
have hX : β i, ae_strongly_measurable (X i) β :=
Ξ» i, (hident i).symm.ae_strongly_measurable_snd hint.1,
have A : β i, strongly_measurable (indicator (set.Ioc (-i : β) i) id) :=
Ξ» i, strongly_measurable_id.indicator measurable_set_Ioc,
set Y := Ξ» (n : β), truncation (X n) n with hY,
set S := Ξ» n, β i in range n, Y i with hS,
let u : β β β := Ξ» n, βc ^ nββ,
have u_mono : monotone u := Ξ» i j hij, nat.floor_mono (pow_le_pow c_one.le hij),
have I1 : β K, β j in range K, ((j : β) ^ 2) β»ΒΉ * Var[Y j] β€ 2 * πΌ[X 0],
{ assume K,
calc β j in range K, ((j : β) ^ 2) β»ΒΉ * Var[Y j] β€
β j in range K, ((j : β) ^ 2) β»ΒΉ * πΌ[(truncation (X 0) j)^2] :
begin
apply sum_le_sum (Ξ» j hj, _),
refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 (sq_nonneg _)),
rw (hident j).truncation.variance_eq,
exact variance_le_expectation_sq (hX 0).truncation,
end
... β€ 2 * πΌ[X 0] : sum_variance_truncation_le hint (hnonneg 0) K },
let C := (c ^ 5 * (c - 1) β»ΒΉ ^ 3) * (2 * πΌ[X 0]),
have I2 : β N, β i in range N, ((u i : β) ^ 2) β»ΒΉ * Var[S (u i)] β€ C,
{ assume N,
calc
β i in range N, ((u i : β) ^ 2) β»ΒΉ * Var[S (u i)]
= β i in range N, ((u i : β) ^ 2) β»ΒΉ * (β j in range (u i), Var[Y j]) :
begin
congr' 1 with i,
congr' 1,
rw [hS, indep_fun.variance_sum],
{ assume j hj,
exact (hident j).ae_strongly_measurable_fst.mem_βp_truncation },
{ assume k hk l hl hkl,
exact (hindep hkl).comp (A k).measurable (A l).measurable }
end
... = β j in range (u (N - 1)),
(β i in (range N).filter (Ξ» i, j < u i), ((u i : β) ^ 2) β»ΒΉ) * Var[Y j] :
begin
simp_rw [mul_sum, sum_mul, sum_sigma'],
refine sum_bij' (Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ (Ξ» a ha, rfl)
(Ξ» (p : (Ξ£ (i : β), β)) hp, (β¨p.2, p.1β© : (Ξ£ (i : β), β))) _ _ _,
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range] at hij,
simp only [hij.1, hij.2, mem_sigma, mem_range, mem_filter, and_true],
exact hij.2.trans_le (u_mono (nat.le_pred_of_lt hij.1)) },
{ rintros β¨i, jβ© hij,
simp only [mem_sigma, mem_range, mem_filter] at hij,
simp only [hij.2.1, hij.2.2, mem_sigma, mem_range, and_self] },
{ rintros β¨i, jβ© hij, refl },
{ rintros β¨i, jβ© hij, refl },
end
... β€ β j in range (u (N - 1)), (c ^ 5 * (c - 1) β»ΒΉ ^ 3 / j ^ 2) * Var[Y j] :
begin
apply sum_le_sum (Ξ» j hj, _),
rcases @eq_zero_or_pos _ _ j with rfl|hj,
{ simp only [Y, nat.cast_zero, zero_pow', ne.def, bit0_eq_zero, nat.one_ne_zero,
not_false_iff, div_zero, zero_mul],
simp only [nat.cast_zero, truncation_zero, variance_zero, mul_zero] },
apply mul_le_mul_of_nonneg_right _ (variance_nonneg _ _),
convert sum_div_nat_floor_pow_sq_le_div_sq N (nat.cast_pos.2 hj) c_one,
{ simp only [nat.cast_lt] },
{ simp only [one_div] }
end
... = (c ^ 5 * (c - 1) β»ΒΉ ^ 3) * β j in range (u (N - 1)), ((j : β) ^ 2) β»ΒΉ * Var[Y j] :
by { simp_rw [mul_sum, div_eq_mul_inv], ring_nf }
... β€ (c ^ 5 * (c - 1) β»ΒΉ ^ 3) * (2 * πΌ[X 0]) :
begin
apply mul_le_mul_of_nonneg_left (I1 _),
apply mul_nonneg (pow_nonneg c_pos.le _),
exact pow_nonneg (inv_nonneg.2 (sub_nonneg.2 c_one.le)) _
end },
have I3 : β N, β i in range N,
β {Ο | (u i * Ξ΅ : β) β€ |S (u i) Ο - πΌ[S (u i)]|} β€ ennreal.of_real (Ξ΅ β»ΒΉ ^ 2 * C),
{ assume N,
calc β i in range N, β {Ο | (u i * Ξ΅ : β) β€ |S (u i) Ο - πΌ[S (u i)]|}
β€ β i in range N, ennreal.of_real (Var[S (u i)] / (u i * Ξ΅) ^ 2) :
begin
refine sum_le_sum (Ξ» i hi, _),
apply meas_ge_le_variance_div_sq,
{ exact mem_βp_finset_sum' _
(Ξ» j hj, (hident j).ae_strongly_measurable_fst.mem_βp_truncation) },
{ apply mul_pos (nat.cast_pos.2 _) Ξ΅pos,
refine zero_lt_one.trans_le _,
apply nat.le_floor,
rw nat.cast_one,
apply one_le_pow_of_one_le c_one.le }
end
... = ennreal.of_real (β i in range N, Var[S (u i)] / (u i * Ξ΅) ^ 2) :
begin
rw ennreal.of_real_sum_of_nonneg (Ξ» i hi, _),
exact div_nonneg (variance_nonneg _ _) (sq_nonneg _),
end
... β€ ennreal.of_real (Ξ΅ β»ΒΉ ^ 2 * C) :
begin
apply ennreal.of_real_le_of_real,
simp_rw [div_eq_inv_mul, β inv_pow, mul_inv, mul_comm _ (Ξ΅β»ΒΉ), mul_pow, mul_assoc,
β mul_sum],
refine mul_le_mul_of_nonneg_left _ (sq_nonneg _),
simp_rw [inv_pow],
exact I2 N
end },
have I4 : β' i, β {Ο | (u i * Ξ΅ : β) β€ |S (u i) Ο - πΌ[S (u i)]|} < β :=
(le_of_tendsto_of_tendsto' (ennreal.tendsto_nat_tsum _) tendsto_const_nhds I3).trans_lt
ennreal.of_real_lt_top,
filter_upwards [ae_eventually_not_mem I4.ne] with Ο hΟ,
simp_rw [not_le, mul_comm, S, sum_apply] at hΟ,
exact hΟ,
end
/- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers
(with respect to the truncated expectation) along the sequence
`c^n`, for any `c > 1`. This follows from `strong_law_aux1` by varying `Ξ΅`. -/
lemma strong_law_aux2 {c : β} (c_one : 1 < c) :
βα΅ Ο, (Ξ» (n : β), β i in range βc^nββ, truncation (X i) i Ο
- πΌ[β i in range βc^nββ, truncation (X i) i]) =o[at_top] (Ξ» (n : β), (βc^nββ : β)) :=
begin
obtain β¨v, -, v_pos, v_limβ© :
β (v : β β β), strict_anti v β§ (β (n : β), 0 < v n) β§ tendsto v at_top (π 0) :=
exists_seq_strict_anti_tendsto (0 : β),
have := Ξ» i, strong_law_aux1 X hint hindep hident hnonneg c_one (v_pos i),
filter_upwards [ae_all_iff.2 this] with Ο hΟ,
apply asymptotics.is_o_iff.2 (Ξ» Ξ΅ Ξ΅pos, _),
obtain β¨i, hiβ© : β i, v i < Ξ΅ := ((tendsto_order.1 v_lim).2 Ξ΅ Ξ΅pos).exists,
filter_upwards [hΟ i] with n hn,
simp only [real.norm_eq_abs, lattice_ordered_comm_group.abs_abs, nat.abs_cast],
exact hn.le.trans (mul_le_mul_of_nonneg_right hi.le (nat.cast_nonneg _)),
end
omit hindep hnonneg
/-- The expectation of the truncated version of `Xα΅’` behaves asymptotically like the whole
expectation. This follows from convergence and Cesaro averaging. -/
lemma strong_law_aux3 :
(Ξ» n, πΌ[β i in range n, truncation (X i) i] - n * πΌ[X 0]) =o[at_top] (coe : β β β) :=
begin
have A : tendsto (Ξ» i, πΌ[truncation (X i) i]) at_top (π (πΌ[X 0])),
{ convert (tendsto_integral_truncation hint).comp tendsto_coe_nat_at_top_at_top,
ext i,
exact (hident i).truncation.integral_eq },
convert asymptotics.is_o_sum_range_of_tendsto_zero (tendsto_sub_nhds_zero_iff.2 A),
ext1 n,
simp only [sum_sub_distrib, sum_const, card_range, nsmul_eq_mul, sum_apply, sub_left_inj],
rw integral_finset_sum _ (Ξ» i hi, _),
exact ((hident i).symm.integrable_snd hint).1.integrable_truncation,
end
include hindep hnonneg
/- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers
(with respect to the original expectation) along the sequence
`c^n`, for any `c > 1`. This follows from the version from the truncated expectation, and the
fact that the truncated and the original expectations have the same asymptotic behavior. -/
lemma strong_law_aux4 {c : β} (c_one : 1 < c) :
βα΅ Ο, (Ξ» (n : β), β i in range βc^nββ, truncation (X i) i Ο - βc^nββ * πΌ[X 0]) =o[at_top]
(Ξ» (n : β), (βc^nββ : β)) :=
begin
filter_upwards [strong_law_aux2 X hint hindep hident hnonneg c_one] with Ο hΟ,
have A : tendsto (Ξ» (n : β), βc ^ nββ) at_top at_top :=
tendsto_nat_floor_at_top.comp (tendsto_pow_at_top_at_top_of_one_lt c_one),
convert hΟ.add ((strong_law_aux3 X hint hident).comp_tendsto A),
ext1 n,
simp,
end
omit hindep
/-- The truncated and non-truncated versions of `Xα΅’` have the same asymptotic behavior, as they
almost surely coincide at all but finitely many steps. This follows from a probability computation
and Borel-Cantelli. -/
lemma strong_law_aux5 :
βα΅ Ο, (Ξ» (n : β), β i in range n, truncation (X i) i Ο - β i in range n, X i Ο) =o[at_top]
(Ξ» (n : β), (n : β)) :=
begin
have A : β' (j : β), β {Ο | X j Ο β set.Ioi (j : β)} < β,
{ convert tsum_prob_mem_Ioi_lt_top hint (hnonneg 0),
ext1 j,
exact (hident j).measure_mem_eq measurable_set_Ioi },
have B : βα΅ Ο, tendsto (Ξ» (n : β), truncation (X n) n Ο - X n Ο) at_top (π 0),
{ filter_upwards [ae_eventually_not_mem A.ne] with Ο hΟ,
apply tendsto_const_nhds.congr' _,
filter_upwards [hΟ, Ioi_mem_at_top 0] with n hn npos,
simp only [truncation, indicator, set.mem_Ioc, id.def, function.comp_app],
split_ifs,
{ exact (sub_self _).symm },
{ have : - (n : β) < X n Ο,
{ apply lt_of_lt_of_le _ (hnonneg n Ο),
simpa only [right.neg_neg_iff, nat.cast_pos] using npos },
simp only [this, true_and, not_le] at h,
exact (hn h).elim } },
filter_upwards [B] with Ο hΟ,
convert is_o_sum_range_of_tendsto_zero hΟ,
ext n,
rw sum_sub_distrib,
end
include hindep
/- `Xα΅’` satisfies the strong law of large numbers along the sequence
`c^n`, for any `c > 1`. This follows from the version for the truncated `Xα΅’`, and the fact that
`Xα΅’` and its truncated version have the same asymptotic behavior. -/
lemma strong_law_aux6 {c : β} (c_one : 1 < c) :
βα΅ Ο, tendsto (Ξ» (n : β), (β i in range βc^nββ, X i Ο) / βc^nββ) at_top (π (πΌ[X 0])) :=
begin
have H : β (n : β), (0 : β) < βc ^ nββ,
{ assume n,
refine zero_lt_one.trans_le _,
simp only [nat.one_le_cast, nat.one_le_floor_iff, one_le_pow_of_one_le c_one.le n] },
filter_upwards [strong_law_aux4 X hint hindep hident hnonneg c_one,
strong_law_aux5 X hint hident hnonneg] with Ο hΟ h'Ο,
rw [β tendsto_sub_nhds_zero_iff, β asymptotics.is_o_one_iff β],
have L : (Ξ» n : β, β i in range βc^nββ, X i Ο - βc^nββ * πΌ[X 0]) =o[at_top] (Ξ» n, (βc^nββ : β)),
{ have A : tendsto (Ξ» (n : β), βc ^ nββ) at_top at_top :=
tendsto_nat_floor_at_top.comp (tendsto_pow_at_top_at_top_of_one_lt c_one),
convert hΟ.sub (h'Ο.comp_tendsto A),
ext1 n,
simp only [sub_sub_sub_cancel_left] },
convert L.mul_is_O (is_O_refl (Ξ» (n : β), (βc ^ nββ : β) β»ΒΉ) at_top);
{ ext1 n,
field_simp [(H n).ne'] },
end
/-- `Xα΅’` satisfies the strong law of large numbers along all integers. This follows from the
corresponding fact along the sequences `c^n`, and the fact that any integer can be sandwiched
between `c^n` and `c^(n+1)` with comparably small error if `c` is close enough to `1`
(which is formalized in `tendsto_div_of_monotone_of_tendsto_div_floor_pow`). -/
lemma strong_law_aux7 :
βα΅ Ο, tendsto (Ξ» (n : β), (β i in range n, X i Ο) / n) at_top (π (πΌ[X 0])) :=
begin
obtain β¨c, -, cone, climβ© :
β (c : β β β), strict_anti c β§ (β (n : β), 1 < c n) β§ tendsto c at_top (π 1) :=
exists_seq_strict_anti_tendsto (1 : β),
have : β k, βα΅ Ο, tendsto (Ξ» (n : β), (β i in range βc k ^ nββ, X i Ο) / βc k ^ nββ)
at_top (π (πΌ[X 0])) := Ξ» k, strong_law_aux6 X hint hindep hident hnonneg (cone k),
filter_upwards [ae_all_iff.2 this] with Ο hΟ,
apply tendsto_div_of_monotone_of_tendsto_div_floor_pow _ _ _ c cone clim _,
{ assume m n hmn,
exact sum_le_sum_of_subset_of_nonneg (range_mono hmn) (Ξ» i hi h'i, hnonneg i Ο) },
{ exact hΟ }
end
end strong_law_nonneg
/-- *Strong law of large numbers*, almost sure version: if `X n` is a sequence of independent
identically distributed integrable real-valued random variables, then `β i in range n, X i / n`
converges almost surely to `πΌ[X 0]`. We give here the strong version, due to Etemadi, that only
requires pairwise independence. -/
theorem strong_law_ae
(X : β β Ξ© β β) (hint : integrable (X 0))
(hindep : pairwise (Ξ» i j, indep_fun (X i) (X j)))
(hident : β i, ident_distrib (X i) (X 0)) :
βα΅ Ο, tendsto (Ξ» (n : β), (β i in range n, X i Ο) / n) at_top (π (πΌ[X 0])) :=
begin
let pos : β β β := (Ξ» x, max x 0),
let neg : β β β := (Ξ» x, max (-x) 0),
have posm : measurable pos := measurable_id'.max measurable_const,
have negm : measurable neg := measurable_id'.neg.max measurable_const,
have A : βα΅ Ο, tendsto (Ξ» (n : β), (β i in range n, (pos β (X i)) Ο) / n)
at_top (π (πΌ[pos β (X 0)])) :=
strong_law_aux7 _ hint.pos_part (Ξ» i j hij, (hindep hij).comp posm posm)
(Ξ» i, (hident i).comp posm) (Ξ» i Ο, le_max_right _ _),
have B : βα΅ Ο, tendsto (Ξ» (n : β), (β i in range n, (neg β (X i)) Ο) / n)
at_top (π (πΌ[neg β (X 0)])) :=
strong_law_aux7 _ hint.neg_part (Ξ» i j hij, (hindep hij).comp negm negm)
(Ξ» i, (hident i).comp negm) (Ξ» i Ο, le_max_right _ _),
filter_upwards [A, B] with Ο hΟpos hΟneg,
convert hΟpos.sub hΟneg,
{ simp only [β sub_div, β sum_sub_distrib, max_zero_sub_max_neg_zero_eq_self] },
{ simp only [βintegral_sub hint.pos_part hint.neg_part, max_zero_sub_max_neg_zero_eq_self] }
end
end strong_law_ae
section strong_law_Lp
variables {Ξ© : Type*} [measure_space Ξ©] [is_probability_measure (β : measure Ξ©)]
/-- *Strong law of large numbers*, Lα΅ version: if `X n` is a sequence of independent
identically distributed real-valued random variables in Lα΅, then `β i in range n, X i / n`
converges in Lα΅ to `πΌ[X 0]`. -/
theorem strong_law_Lp
{p : ββ₯0β} (hp : 1 β€ p) (hp' : p β β)
(X : β β Ξ© β β) (hβp : mem_βp (X 0) p)
(hindep : pairwise (Ξ» i j, indep_fun (X i) (X j)))
(hident : β i, ident_distrib (X i) (X 0)) :
tendsto (Ξ» n, snorm (Ξ» Ο, (β i in range n, X i Ο) / n - πΌ[X 0]) p β) at_top (π 0) :=
begin
have hmeas : β i, ae_strongly_measurable (X i) β :=
Ξ» i, (hident i).ae_strongly_measurable_iff.2 hβp.1,
have hint : integrable (X 0) β := hβp.integrable hp,
have havg : β n, ae_strongly_measurable (Ξ» Ο, (β i in range n, X i Ο) / n) β,
{ intro n,
simp_rw div_eq_mul_inv,
exact ae_strongly_measurable.mul_const (ae_strongly_measurable_sum _ (Ξ» i _, hmeas i)) _ },
refine tendsto_Lp_of_tendsto_in_measure _ hp hp' havg (mem_βp_const _) _
(tendsto_in_measure_of_tendsto_ae havg (strong_law_ae _ hint hindep hident)),
rw (_ : (Ξ» n Ο, (β i in range n, X i Ο) / βn) = Ξ» n, (β i in range n, X i) / βn),
{ exact (uniform_integrable_average hp $
mem_βp.uniform_integrable_of_ident_distrib hp hp' hβp hident).2.1 },
{ ext n Ο,
simp only [pi.coe_nat, pi.div_apply, sum_apply] }
end
end strong_law_Lp
end probability_theory
|
e11ef43f1f5d4dffc2e083124965a549f430631f | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/concrete_category/unbundled_hom_auto.lean | f1fb60f30b3f09d0a023927411b0d200cdb15aa3 | [] | 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,565 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.concrete_category.bundled_hom
import Mathlib.PostPort
universes u l
namespace Mathlib
/-!
# Category instances for structures that use unbundled homs
This file provides basic infrastructure to define concrete
categories using unbundled homs (see `class unbundled_hom`), and
define forgetful functors between them (see
`unbundled_hom.mk_has_forgetβ`).
-/
namespace category_theory
/-- A class for unbundled homs used to define a category. `hom` must
take two types `Ξ±`, `Ξ²` and instances of the corresponding structures,
and return a predicate on `Ξ± β Ξ²`. -/
class unbundled_hom {c : Type u β Type u} (hom : {Ξ± Ξ² : Type u} β c Ξ± β c Ξ² β (Ξ± β Ξ²) β Prop) where
hom_id : β {Ξ± : Type u} (ia : c Ξ±), hom ia ia id
hom_comp :
β {Ξ± Ξ² Ξ³ : Type u} {IΞ± : c Ξ±} {IΞ² : c Ξ²} {IΞ³ : c Ξ³} {g : Ξ² β Ξ³} {f : Ξ± β Ξ²},
hom IΞ² IΞ³ g β hom IΞ± IΞ² f β hom IΞ± IΞ³ (g β f)
namespace unbundled_hom
protected instance bundled_hom (c : Type u β Type u)
(hom : {Ξ± Ξ² : Type u} β c Ξ± β c Ξ² β (Ξ± β Ξ²) β Prop) [π : unbundled_hom hom] :
bundled_hom fun (Ξ± Ξ² : Type u) (IΞ± : c Ξ±) (IΞ² : c Ξ²) => Subtype (hom IΞ± IΞ²) :=
bundled_hom.mk (fun (_x _x_1 : Type u) (_x_2 : c _x) (_x_3 : c _x_1) => subtype.val)
(fun (Ξ± : Type u) (IΞ± : c Ξ±) => { val := id, property := sorry })
fun (_x _x_1 _x_2 : Type u) (_x_3 : c _x) (_x_4 : c _x_1) (_x_5 : c _x_2)
(g : Subtype (hom _x_4 _x_5)) (f : Subtype (hom _x_3 _x_4)) =>
{ val := subtype.val g β subtype.val f, property := sorry }
/-- A custom constructor for forgetful functor between concrete categories defined using `unbundled_hom`. -/
def mk_has_forgetβ {c : Type u β Type u} {hom : {Ξ± Ξ² : Type u} β c Ξ± β c Ξ² β (Ξ± β Ξ²) β Prop}
[π : unbundled_hom hom] {c' : Type u β Type u}
{hom' : {Ξ± Ξ² : Type u} β c' Ξ± β c' Ξ² β (Ξ± β Ξ²) β Prop} [π' : unbundled_hom hom']
(obj : {Ξ± : Type u} β c Ξ± β c' Ξ±)
(map :
β {Ξ± Ξ² : Type u} {IΞ± : c Ξ±} {IΞ² : c Ξ²} {f : Ξ± β Ξ²}, hom IΞ± IΞ² f β hom' (obj IΞ±) (obj IΞ²) f) :
has_forgetβ (bundled c) (bundled c') :=
bundled_hom.mk_has_forgetβ obj
(fun (X Y : bundled c) (f : X βΆ Y) => { val := subtype.val f, property := sorry }) sorry
end Mathlib |
5ff8f57208a638203b4d9944976e117f144a2ca8 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/theories/analysis/bounded_linear_operator.lean | e24e7debf5830e4c65de59b24b55678e18636d4a | [
"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 | 23,267 | lean | /-
Copyright (c) 2016 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Bounded linear operators
-/
import .normed_space .real_limit algebra.module algebra.homomorphism
open real nat classical topology set
--open normed_vector_space (this confuses lots of stuff??)
noncomputable theory
namespace analysis
-- define bounded linear operators and basic instances
section bdd_lin_op
structure is_bdd_linear_map [class] {V W : Type} [normed_vector_space V] [normed_vector_space W] (f : V β W)
extends is_module_hom β f :=
(op_norm : β) (op_norm_pos : op_norm > 0) (op_norm_bound : β v : V, β₯f vβ₯ β€ op_norm * β₯vβ₯)
theorem is_bdd_linear_map_id [instance] (V : Type) [normed_vector_space V] : is_bdd_linear_map (Ξ» x : V, x) :=
begin
fapply is_bdd_linear_map.mk,
repeat (intros; reflexivity),
exact 1,
exact zero_lt_one,
intro, rewrite one_mul, apply le.refl
end
theorem is_bdd_linear_map_zero [instance] (V W : Type) [normed_vector_space V] [normed_vector_space W] :
is_bdd_linear_map (Ξ» x : V, (0 : W)) :=
begin
fapply is_bdd_linear_map.mk,
all_goals intros,
rewrite zero_add,
rewrite smul_zero,
exact 1,
exact zero_lt_one,
rewrite [norm_zero, one_mul],
apply norm_nonneg
end
theorem is_bdd_linear_map_add [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W]
(f g : V β W) [Hbf : is_bdd_linear_map f] [Hbg : is_bdd_linear_map g] :
is_bdd_linear_map (Ξ» x, f x + g x) :=
begin
fapply is_bdd_linear_map.mk,
all_goals intros,
{rewrite [hom_add f, hom_add g],-- (this takes 4 seconds: rewrite [2 hom_add])
simp},
{rewrite [hom_smul f, hom_smul g, smul_left_distrib]},
{exact is_bdd_linear_map.op_norm _ _ f + is_bdd_linear_map.op_norm _ _ g},
{apply add_pos,
repeat apply is_bdd_linear_map.op_norm_pos},
{apply le.trans,
apply norm_triangle,
rewrite right_distrib,
apply add_le_add,
repeat apply is_bdd_linear_map.op_norm_bound}
end
theorem is_bdd_linear_map_smul [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W]
(f : V β W) (c : β) [Hbf : is_bdd_linear_map f] : is_bdd_linear_map (Ξ» x, c β’ f x) :=
begin
apply @decidable.cases_on (c = 0),
exact _,
{intro Hcz,
rewrite Hcz,
have Hfe : (Ξ» x : V, (0 : β) β’ f x) = (Ξ» x : V, 0), from funext (Ξ» x, !zero_smul),
rewrite Hfe,
apply is_bdd_linear_map_zero},
intro Hcnz,
fapply is_bdd_linear_map.mk,
all_goals intros,
{rewrite [hom_add f, smul_left_distrib]},
{rewrite [hom_smul f, -*mul_smul, {c*r}mul.comm]},
{exact (abs c) * is_bdd_linear_map.op_norm _ _ f},
{have Hpos : abs c > 0, from abs_pos_of_ne_zero Hcnz,
apply mul_pos,
assumption,
apply is_bdd_linear_map.op_norm_pos},
{rewrite [norm_smul, mul.assoc],
apply mul_le_mul_of_nonneg_left,
apply is_bdd_linear_map.op_norm_bound,
apply abs_nonneg}
end
theorem is_bdd_linear_map_neg [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W]
(f : V β W) [Hbf : is_bdd_linear_map f] : is_bdd_linear_map (Ξ» x, -f x) :=
begin
have H : (Ξ» x : V, -f x) = (Ξ» x : V, (-1 : β) β’ f x), from funext (Ξ» x, eq.symm !neg_one_smul),
rewrite H,
apply is_bdd_linear_map_smul
end
-- this can't be an instance because things loop
theorem is_bdd_linear_map_comp {U V W : Type} [normed_vector_space U] [normed_vector_space V]
[normed_vector_space W] (f : V β W) (g : U β V) [is_bdd_linear_map f] [is_bdd_linear_map g] :
is_bdd_linear_map (Ξ» u, f (g u)) :=
begin
fapply is_bdd_linear_map.mk,
all_goals intros,
{rewrite [hom_add g, hom_add f]},
{rewrite [hom_smul g, hom_smul f]},
{exact is_bdd_linear_map.op_norm _ _ f * is_bdd_linear_map.op_norm _ _ g},
{apply mul_pos, repeat apply is_bdd_linear_map.op_norm_pos},
{apply le.trans,
apply is_bdd_linear_map.op_norm_bound _ _ f,
apply le.trans,
apply mul_le_mul_of_nonneg_left,
apply is_bdd_linear_map.op_norm_bound _ _ g,
apply le_of_lt !is_bdd_linear_map.op_norm_pos,
rewrite *mul.assoc,
apply le.refl}
end
variables {V W : Type}
variables [HV : normed_vector_space V] [HW : normed_vector_space W]
include HV HW
variable f : V β W
variable [Hf : is_bdd_linear_map f]
include Hf
definition op_norm := is_bdd_linear_map.op_norm _ _ f
theorem op_norm_pos : op_norm f > 0 := is_bdd_linear_map.op_norm_pos _ _ f
theorem op_norm_bound (v : V) : β₯f vβ₯ β€ (op_norm f) * β₯vβ₯ := is_bdd_linear_map.op_norm_bound _ _ f v
theorem bdd_linear_map_continuous : continuous f :=
begin
apply continuous_of_forall_continuous_at,
intro x,
apply normed_vector_space.continuous_at_intro,
intro Ξ΅ HΞ΅,
existsi Ξ΅ / (op_norm f),
split,
apply div_pos_of_pos_of_pos HΞ΅ !op_norm_pos,
intro x' Hx',
rewrite [-hom_sub f],
apply lt_of_le_of_lt,
apply op_norm_bound f,
rewrite [-@mul_div_cancel' _ _ Ξ΅ (op_norm f) (ne_of_gt !op_norm_pos)],
apply mul_lt_mul_of_pos_left,
exact Hx',
apply op_norm_pos
end
end bdd_lin_op
-- define Frechet derivative and basic properties
section frechet_deriv
variables {V W : Type}
variables [HV : normed_vector_space V] [HW : normed_vector_space W]
include HV HW
definition has_frechet_deriv_at (f A : V β W) [is_bdd_linear_map A] (x : V) :=
(Ξ» h : V, β₯f (x + h) - f x - A h β₯ / β₯ h β₯) βΆ 0 [at 0]
lemma diff_quot_cts {f A : V β W} [HA : is_bdd_linear_map A] {y : V} (Hf : has_frechet_deriv_at f A y) :
continuous_at (Ξ» h, β₯f (y + h) - f y - A hβ₯ / β₯hβ₯) 0 :=
begin
apply normed_vector_space.continuous_at_intro,
intro Ξ΅ HΞ΅,
cases normed_vector_space.approaches_at_dest Hf HΞ΅ with Ξ΄ HΞ΄,
existsi Ξ΄,
split,
exact and.left HΞ΄,
intro x' Hx',
cases em (x' = 0) with Heq Hneq,
{rewrite [Heq, norm_zero, div_zero, sub_zero, norm_zero], apply HΞ΅},
{rewrite [norm_zero, div_zero],
apply and.right HΞ΄,
repeat assumption}
end
theorem is_bdd_linear_map_of_eq {A B : V β W} [HA : is_bdd_linear_map A] (HAB : A = B) :
is_bdd_linear_map B :=
begin
fapply is_bdd_linear_map.mk,
all_goals try rewrite -HAB,
{apply hom_add},
{apply hom_smul},
{exact op_norm A},
{exact op_norm_pos A},
{rewrite -HAB, apply op_norm_bound}
end
definition is_frechet_deriv_at_of_eq {f A : V β W} [is_bdd_linear_map A] {x : V}
(Hfd : has_frechet_deriv_at f A x) {B : V β W} (HAB : A = B) :
@has_frechet_deriv_at _ _ _ _ f B (is_bdd_linear_map_of_eq HAB) x :=
begin
unfold has_frechet_deriv_at,
rewrite -HAB,
apply Hfd
end
theorem has_frechet_deriv_at_intro {f A : V β W} [is_bdd_linear_map A] {x : V}
(H : β β¦Ξ΅ : ββ¦, Ξ΅ > 0 β
(β Ξ΄ : β, Ξ΄ > 0 β§ β β¦x' : Vβ¦, x' β 0 β§ β₯x'β₯ < Ξ΄ β β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ < Ξ΅)) :
has_frechet_deriv_at f A x :=
begin
apply normed_vector_space.approaches_at_intro,
intros Ξ΅ HΞ΅,
cases H HΞ΅ with Ξ΄ HΞ΄,
cases HΞ΄ with HΞ΄ HΞ΄',
existsi Ξ΄,
split,
assumption,
intros x' Hx'1 Hx'2,
show abs (β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ - 0) < Ξ΅, begin
have H : β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ β₯ 0,
from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg,
rewrite [sub_zero, abs_of_nonneg H],
apply HΞ΄',
split,
assumption,
rewrite [-sub_zero x'],
apply Hx'1
end
end
theorem has_frechet_deriv_at_elim {f A : V β W} [is_bdd_linear_map A] {x : V} (H : has_frechet_deriv_at f A x) :
β β¦Ξ΅ : ββ¦, Ξ΅ > 0 β
(β Ξ΄ : β, Ξ΄ > 0 β§ β β¦x' : Vβ¦, x' β 0 β§ β₯x'β₯ < Ξ΄ β β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ < Ξ΅) :=
begin
intros Ξ΅ HΞ΅,
cases normed_vector_space.approaches_at_dest H HΞ΅ with Ξ΄ HΞ΄,
cases HΞ΄ with HΞ΄ HΞ΄',
existsi Ξ΄,
split,
assumption,
intros x' Hx',
rewrite [-sub_zero x' at Hx' {2}],
have HΞ΄'' : abs (β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ - 0) < Ξ΅, from HΞ΄' (and.right Hx') (and.left Hx'),
have Hpos : β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ β₯ 0, from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg,
rewrite [sub_zero at HΞ΄'', abs_of_nonneg Hpos at HΞ΄''],
assumption
end
structure frechet_diffable_at [class] (f : V β W) (x : V) :=
(A : V β W) [HA : is_bdd_linear_map A] (is_fr_der : has_frechet_deriv_at f A x)
variables f g : V β W
variable x : V
definition frechet_deriv_at [Hf : frechet_diffable_at f x] : V β W :=
frechet_diffable_at.A _ _ f x
definition frechet_deriv_at_is_bdd_linear_map [instance] (f : V β W) (x : V) [Hf : frechet_diffable_at f x] :
is_bdd_linear_map (frechet_deriv_at f x) :=
frechet_diffable_at.HA _ _ f x
theorem frechet_deriv_spec [Hf : frechet_diffable_at f x] :
(Ξ» h : V, β₯f (x + h) - f x - (frechet_deriv_at f x h) β₯ / β₯ h β₯) βΆ 0 [at 0] :=
frechet_diffable_at.is_fr_der _ _ f x
theorem has_frechet_deriv_at_const (w : W) : has_frechet_deriv_at (Ξ» v : V, w) (Ξ» v : V, 0) x :=
begin
apply normed_vector_space.approaches_at_intro,
intros Ξ΅ HΞ΅,
existsi 1,
split,
exact zero_lt_one,
intros x' Hx' _,
rewrite [2 sub_self, norm_zero],
krewrite [zero_div, sub_zero, norm_zero],
assumption
end
theorem has_frechet_deriv_at_id : has_frechet_deriv_at (Ξ» v : V, v) (Ξ» v : V, v) x :=
begin
apply normed_vector_space.approaches_at_intro,
intros Ξ΅ HΞ΅,
existsi 1,
split,
exact zero_lt_one,
intros x' Hx' _,
have x + x' - x - x' = 0, by simp,
rewrite [this, norm_zero, zero_div, sub_self, norm_zero],
exact HΞ΅
end
theorem has_frechet_deriv_at_smul (c : β) {A : V β W} [is_bdd_linear_map A]
(Hf : has_frechet_deriv_at f A x) : has_frechet_deriv_at (Ξ» y, c β’ f y) (Ξ» y, c β’ A y) x :=
begin
eapply @decidable.cases_on (abs c = 0),
exact _,
{intro Hc,
have Hz : c = 0, from eq_zero_of_abs_eq_zero Hc,
have Hfz : (Ξ» y : V, (0 : β) β’ f y) = (Ξ» y : V, 0), from funext (Ξ» y, !zero_smul),
--have Hfz' : (Ξ» x : V, (0 : β) β’ A x) = (Ξ» x : V, 0), from funext (Ξ» y, !zero_smul),
-- rewriting Hfz' produces type-incorrect term
rewrite [Hz, Hfz],
apply metric_space.approaches_at_intro,
intro Ξ΅ HΞ΅,
existsi 1,
split,
exact zero_lt_one,
intro x' Hx' _,
rewrite [zero_smul, *sub_zero, norm_zero],
krewrite [zero_div, dist_self],
exact HΞ΅},
intro Hcnz,
apply normed_vector_space.approaches_at_intro,
intros Ξ΅ HΞ΅,
have HΞ΅c : Ξ΅ / abs c > 0, from div_pos_of_pos_of_pos HΞ΅ (lt_of_le_of_ne !abs_nonneg (ne.symm Hcnz)),
cases normed_vector_space.approaches_at_dest Hf HΞ΅c with Ξ΄ HΞ΄,
cases HΞ΄ with HΞ΄p HΞ΄,
existsi Ξ΄,
split,
assumption,
intro x' Hx' _,
show abs ((β₯c β’ f (x + x') - c β’ f x - c β’ A x'β₯ / β₯x'β₯ - 0)) < Ξ΅, begin
rewrite [sub_zero, -2 smul_sub_left_distrib, norm_smul],
krewrite mul_div_assoc,
rewrite [abs_mul, abs_abs, -{Ξ΅}mul_div_cancel' Hcnz],
apply mul_lt_mul_of_pos_left,
have HΞ΄' : abs (β₯f (x + x') - f x - A x'β₯ / β₯x'β₯ - 0) < Ξ΅ / abs c, from HΞ΄ Hx' a,
rewrite sub_zero at HΞ΄',
apply HΞ΄',
apply lt_of_le_of_ne,
apply abs_nonneg,
apply ne.symm,
apply Hcnz
end
end
theorem has_frechet_deriv_at_neg {A : V β W} [is_bdd_linear_map A]
(Hf : has_frechet_deriv_at f A x) : has_frechet_deriv_at (Ξ» y, - f y) (Ξ» y, - A y) x :=
begin
apply has_frechet_deriv_at_intro,
intros Ξ΅ HΞ΅,
cases has_frechet_deriv_at_elim Hf HΞ΅ with Ξ΄ HΞ΄,
existsi Ξ΄,
split,
exact and.left HΞ΄,
intro x' Hx',
rewrite [-norm_neg, neg_sub, sub_neg_eq_add, sub_add_eq_sub_sub, sub_neg_eq_add,
add_sub_assoc, add.comm, -sub_eq_add_neg],
apply and.right HΞ΄,
assumption
end
theorem has_frechet_deriv_at_add (A B : V β W) [is_bdd_linear_map A] [is_bdd_linear_map B]
(Hf : has_frechet_deriv_at f A x) (Hg : has_frechet_deriv_at g B x) :
has_frechet_deriv_at (Ξ» y, f y + g y) (Ξ» y, A y + B y) x :=
begin
have Hle : β h, β₯f (x + h) + g (x + h) - (f x + g x) - (A h + B h)β₯ / β₯hβ₯ β€
β₯f (x + h) - f x - A hβ₯ / β₯hβ₯ + β₯g (x + h) - g x - B hβ₯ / β₯hβ₯, begin
intro h,
cases em (β₯hβ₯ > 0) with Hh Hh,
krewrite div_add_div_same,
apply div_le_div_of_le_of_pos,
have Hfeq : f (x + h) + g (x + h) - (f x + g x) - (A h + B h) =
(f (x + h) - f x - A h) + (g (x + h) - g x - B h), by simp,
rewrite Hfeq,
apply norm_triangle,
exact Hh,
have Hhe : β₯hβ₯ = 0, from eq_of_le_of_ge (le_of_not_gt Hh) !norm_nonneg,
krewrite [Hhe, *div_zero, zero_add],
eapply le.refl
end,
have Hlimge : (Ξ» h, β₯f (x + h) - f x - A hβ₯ / β₯hβ₯ + β₯g (x + h) - g x - B hβ₯ / β₯hβ₯) βΆ 0 [at 0], begin
rewrite [-zero_add 0],
apply add_approaches,
apply Hf,
apply Hg
end,
have Hlimle : (Ξ» (h : V), (0 : β)) βΆ 0 [at 0], by apply approaches_constant,
apply approaches_squeeze Hlimle Hlimge,
apply filter.eventually_of_forall,
intro y,
apply div_nonneg_of_nonneg_of_nonneg,
repeat apply norm_nonneg,
apply filter.eventually_of_forall,
apply Hle
end
open topology
theorem continuous_at_of_diffable_at [Hf : frechet_diffable_at f x] : continuous_at f x :=
begin
apply normed_vector_space.continuous_at_intro,
intros Ξ΅ HΞ΅,
note Hfds := normed_vector_space.approaches_at_dest (frechet_deriv_spec f x) HΞ΅,
cases Hfds with Ξ΄ HΞ΄,
cases HΞ΄ with HΞ΄ HΞ΄',
existsi min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + op_norm (frechet_deriv_at f x))),
split,
{apply lt_min,
exact HΞ΄,
repeat apply div_pos_of_pos_of_pos,
exact HΞ΅,
apply two_pos,
apply add_pos HΞ΅ !op_norm_pos},
{intro x' Hx',
cases em (x' - x = 0) with Heq Hneq,
rewrite [eq_of_sub_eq_zero Heq, sub_self, norm_zero], assumption,
have Hx'x : x' - x β 0 β§ β₯(x' - x) - 0β₯ < Ξ΄, from and.intro Hneq begin
rewrite sub_zero,
apply lt_of_lt_of_le,
apply Hx',
apply min_le_left
end,
have Hx'xp : β₯x' - xβ₯ > 0, from norm_pos_of_ne_zero Hneq,
have HΞ΄'' : abs (β₯f (x + (x' - x)) - f x - frechet_deriv_at f x (x' - x)β₯ / β₯x' - xβ₯ - 0) < Ξ΅,
from HΞ΄' (and.right Hx'x) (and.left Hx'x),
have Hnn : β₯f (x + (x' - x)) - f x - frechet_deriv_at f x (x' - x)β₯ / β₯x' - xβ₯ β₯ 0,
from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg,
rewrite [sub_zero at HΞ΄'', abs_of_nonneg Hnn at HΞ΄'', add.comm at HΞ΄'', sub_add_cancel at HΞ΄''],
note H1 := lt_mul_of_div_lt_of_pos Hx'xp HΞ΄'',
have H2 : f x' - f x = f x' - f x - frechet_deriv_at f x (x' - x) + frechet_deriv_at f x (x' - x),
by rewrite sub_add_cancel, --by simp, (simp takes .5 seconds to do this!)
rewrite H2,
apply lt_of_le_of_lt,
apply norm_triangle,
apply lt.trans,
apply add_lt_add_of_lt_of_le,
apply H1,
apply op_norm_bound (!frechet_deriv_at),
rewrite [-add_halves Ξ΅ at {2}],
apply add_lt_add,
let on := op_norm (frechet_deriv_at f x),
exact calc
Ξ΅ * β₯x' - xβ₯ < Ξ΅ * min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_lt_mul_of_pos_left Hx' HΞ΅
... β€ Ξ΅ * ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_le_mul_of_nonneg_left !min_le_right (le_of_lt HΞ΅)
... < Ξ΅ / 2 : mul_div_self_add_lt (div_pos_of_pos_of_pos HΞ΅ two_pos) HΞ΅ !op_norm_pos,
exact calc
on * β₯x' - xβ₯ < on * min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_lt_mul_of_pos_left Hx' !op_norm_pos
... β€ on * ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_le_mul_of_nonneg_left !min_le_right (le_of_lt !op_norm_pos)
... < Ξ΅ / 2 : mul_div_add_self_lt (div_pos_of_pos_of_pos HΞ΅ two_pos) HΞ΅ !op_norm_pos}
end
theorem continuous_at_of_has_frechet_deriv_at {A : V β W} [is_bdd_linear_map A]
(H : has_frechet_deriv_at f A x) : continuous_at f x :=
begin
apply @continuous_at_of_diffable_at,
existsi A,
exact H
end
end frechet_deriv
section comp
lemma div_mul_div_cancel {A : Type} [field A] (a b : A) {c : A} (Hc : c β 0) : (a / c) * (c / b) = a / b :=
by rewrite [-mul_div_assoc, div_mul_cancel _ Hc]
lemma div_add_eq_add_mul_div {A : Type} [field A] (a b c : A) (Hb : b β 0) : (a / b) + c = (a + c * b) / b :=
by rewrite [-div_add_div_same, mul_div_cancel _ Hb]
-- I'm not sure why smul_approaches doesn't unify where I use this?
private lemma real_limit_helper {U : Type} [normed_vector_space U] {f : U β β} {a : β} {x : U}
(Hf : f βΆ a [at x]) (c : β) : (Ξ» y, c * f y) βΆ c * a [at x] :=
begin
apply smul_approaches,
exact Hf
end
variables {U V W : Type}
variables [HU : normed_vector_space U] [HV : normed_vector_space V] [HW : normed_vector_space W]
variables {f : V β W} {g : U β V}
variables {A : V β W} {B : U β V}
variables [HA : is_bdd_linear_map A] [HB : is_bdd_linear_map B]
variable {x : U}
include HU HV HW HA HB
-- this takes 2 seconds without clearing the contexts before simp
theorem has_frechet_deriv_at_comp (Hg : has_frechet_deriv_at g B x) (Hf : has_frechet_deriv_at f A (g x)) :
@has_frechet_deriv_at _ _ _ _ (Ξ» y, f (g y)) (Ξ» y, A (B y)) !is_bdd_linear_map_comp x :=
begin
unfold has_frechet_deriv_at,
note Hf' := has_frechet_deriv_at_elim Hf,
note Hg' := has_frechet_deriv_at_elim Hg,
have H : β h, f (g (x + h)) - f (g x) - A (B h) =
(A (g (x + h) - g x - B h)) + (-f (g x) + f (g (x + h)) + A (g x - g (x + h))),
begin intro; rewrite [3 hom_sub A], clear [Hf, Hg, Hf', Hg'], simp end, -- .5 seconds for simp
have H' : (Ξ» h, β₯f (g (x + h)) - f (g x) - A (B h)β₯ / β₯hβ₯) =
(Ξ» h, β₯(A (g (x + h) - g x - B h)) + (-f (g x) + f (g (x + h)) + A (g x - g (x + h)))β₯ / β₯hβ₯),
from funext (Ξ» h, by rewrite H),
rewrite H',
clear [H, H'],
apply approaches_squeeze, -- show the limit holds by bounding it by something that vanishes
{apply approaches_constant},
rotate 1,
{apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg},
{apply filter.eventually_of_forall, intro,
apply div_le_div_of_le_of_nonneg,
apply norm_triangle,
apply norm_nonneg},
have H : (Ξ» (y : U), (β₯A (g (x + y) - g x - B y)β₯ + β₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))β₯) / β₯yβ₯) =
(Ξ» (y : U), (β₯A (g (x + y) - g x - B y)β₯ / β₯yβ₯ + β₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))β₯ / β₯yβ₯)),
from funext (Ξ» y, by rewrite [div_add_div_same]),
rewrite [H, -zero_add 0], -- the function is a sum of two things that both vanish
clear H,
apply add_approaches,
{apply approaches_squeeze, -- show the lhs vanishes by squeezing it again
{apply approaches_constant},
rotate 1,
{apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg},
{apply filter.eventually_of_forall, intro y,
show β₯A (g (x + y) - g x - B y)β₯ / β₯yβ₯ β€ op_norm A * (β₯(g (x + y) - g x - B y)β₯ / β₯yβ₯), begin
rewrite -mul_div_assoc,
apply div_le_div_of_le_of_nonneg,
apply op_norm_bound A,
apply norm_nonneg
end},
{rewrite [-mul_zero (op_norm A)],
apply real_limit_helper,
apply Hg}}, -- we have shown the lhs vanishes. now the rhs
{have H : β y, (β₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))β₯ / β₯yβ₯) =
((β₯(f (g (x + y)) - f (g x)) - A (g (x + y) - g x) β₯ / β₯g (x + y) - g xβ₯) * (β₯g (x + y) - g xβ₯ / β₯yβ₯)),
begin
intro,
cases em (g (x + y) - g x = 0) with Heq Hneq,
{note Heq' := eq_of_sub_eq_zero Heq,
rewrite [Heq', neg_add_eq_sub, *sub_self, hom_zero A, add_zero, *norm_zero, div_zero, zero_div]},
{rewrite [div_mul_div_cancel _ _ (norm_ne_zero_of_ne_zero Hneq), *sub_eq_add_neg,
-hom_neg A],
clear [Hf, Hg, Hf', Hg', Hneq],
simp} --(.5 seconds)
end,
apply approaches_squeeze, -- again, by squeezing
{apply approaches_constant},
rotate 1,
{apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg},
{apply filter.eventually_of_forall, intro y, rewrite H,
apply mul_le_mul_of_nonneg_left,
{show β₯g (x + y) - g xβ₯ / β₯yβ₯ β€ β₯g (x + y) - g x - B yβ₯ / β₯yβ₯ + op_norm B, begin
cases em (y = 0) with Heq Hneq,
{rewrite [Heq, norm_zero, *div_zero, zero_add], apply le_of_lt, apply op_norm_pos},
rewrite [div_add_eq_add_mul_div _ _ _ (norm_ne_zero_of_ne_zero Hneq)],
apply div_le_div_of_le_of_nonneg,
apply le.trans,
rotate 1,
apply add_le_add_left,
apply op_norm_bound,
apply norm_nonneg,
rewrite [-neg_add_cancel_right (g (x + y) - g x) (B y) at {1}, -sub_eq_add_neg],
apply norm_triangle
end},
{apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg}},
-- now to show the bounding function vanishes. it is a product of a vanishing function and a convergent one
apply mul_approaches_zero_of_approaches_zero_of_approaches,
{have H' : (Ξ» (y : U), β₯f (g (x + y)) - f (g x) - A (g (x + y) - g x)β₯ / β₯g (x + y) - g xβ₯) =
(Ξ» (y : U), β₯f (g x + (g (x + y) - g x)) - f (g x) - A (g (x + y) - g x)β₯ / β₯g (x + y) - g xβ₯),
from funext (Ξ» y, by rewrite [add.comm (g x), sub_add_cancel]), -- first, show lhs vanishes
rewrite H',
have Hgcts : continuous_at (Ξ» y, g (x + y) - g x) 0, begin
apply normed_vector_space.continuous_at_intro,
intro Ξ΅ HΞ΅,
cases normed_vector_space.continuous_at_dest (continuous_at_of_has_frechet_deriv_at g x Hg) _ HΞ΅ with Ξ΄ HΞ΄,
existsi Ξ΄,
split,
exact and.left HΞ΄,
intro x' Hx',
rewrite [add_zero, sub_self],
rewrite sub_zero,
apply and.right HΞ΄,
have (x + x') - x = x' - 0, begin clear [Hg, Hf, Hf', Hg', H, H', HΞ΄, Hx'], simp end, -- (.6 seconds w/o clear, .1 with)
rewrite this,
apply Hx'
end,
have Hfcts : continuous_at (Ξ» (x' : V), β₯f (g x + x') - f (g x) - A x'β₯ / β₯x'β₯) (g (x + 0) - g x), begin
rewrite [add_zero, sub_self],
apply diff_quot_cts,
exact Hf
end,
have Heqz : β₯f (g x + (g (x + 0) - g x)) - f (g x) - A (g (x + 0) - g x)β₯ / β₯g (x + 0) - g xβ₯ = 0,
by rewrite [*add_zero, sub_self, norm_zero, div_zero],
apply @tendsto_comp _ _ _ (Ξ» y, g (x + y) - g x),
apply tendsto_inf_left,
apply tendsto_at_of_continuous_at Hgcts,
note Hfcts' := tendsto_at_of_continuous_at Hfcts,
rewrite Heqz at Hfcts',
exact Hfcts'}, -- finally, show rhs converges to op_norm B
{apply add_approaches,
apply Hg,
apply approaches_constant}}
end
end comp
end analysis
|
7b43a1c27b0592289c555b6e2b25ca4a7a0c4e87 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/Data/Json/Basic.lean | 7bae4d83ec2915abb1f9de2f66183b69fbcc5b23 | [
"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 | 6,482 | lean | /-
Copyright (c) 2019 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Marc Huisinga
-/
import Std.Data.RBTree
namespace Lean
-- mantissa * 10^-exponent
structure JsonNumber where
mantissa : Int
exponent : Nat
deriving DecidableEq
namespace JsonNumber
protected def fromNat (n : Nat) : JsonNumber := β¨n, 0β©
protected def fromInt (n : Int) : JsonNumber := β¨n, 0β©
instance : Coe Nat JsonNumber := β¨JsonNumber.fromNatβ©
instance : Coe Int JsonNumber := β¨JsonNumber.fromIntβ©
private partial def countDigits (n : Nat) : Nat :=
let rec loop (n digits : Nat) : Nat :=
if n β€ 9 then
digits
else
loop (n/10) (digits+1)
loop n 1
-- convert mantissa * 10^-exponent to 0.mantissa * 10^exponent
protected def normalize : JsonNumber β Int Γ Nat Γ Int
| β¨m, eβ© => do
if m = 0 then (0, 0, 0)
else
let sign : Int := if m > 0 then 1 else -1
let mut mAbs := m.natAbs
let nDigits := countDigits mAbs
-- eliminate trailing zeros
for _ in [0:nDigits] do
if mAbs % 10 = 0 then
mAbs := mAbs / 10
else
break
(sign, mAbs, -(e : Int) + nDigits)
-- todo (Dany): We should have an Ordering version of this.
def lt (a b : JsonNumber) : Bool :=
let (as, am, ae) := a.normalize
let (bs, bm, be) := b.normalize
match (as, bs) with
| (-1, 1) => true
| (1, -1) => false
| _ =>
let ((am, ae), (bm, be)) :=
if as = -1 && bs = -1 then
((bm, be), (am, ae))
else
((am, ae), (bm, be))
let amDigits := countDigits am
let bmDigits := countDigits bm
-- align the mantissas
let (am, bm) :=
if amDigits < bmDigits then
(am * 10^(bmDigits - amDigits), bm)
else
(am, bm * 10^(amDigits - bmDigits))
if ae < be then true
else if ae > be then false
else am < bm
def ltProp : LT JsonNumber :=
β¨fun a b => lt a b = trueβ©
instance : LT JsonNumber :=
ltProp
instance (a b : JsonNumber) : Decidable (a < b) :=
inferInstanceAs (Decidable (lt a b = true))
instance : Ord JsonNumber where
compare x y :=
if x < y then Ordering.lt
else if x > y then Ordering.gt
else Ordering.eq
protected def toString : JsonNumber β String
| β¨m, 0β© => m.repr
| β¨m, eβ© =>
let sign := if m β₯ 0 then "" else "-"
let m := m.natAbs
-- if there are too many zeroes after the decimal, we
-- use exponents to compress the representation.
-- this is mostly done for memory usage reasons:
-- the size of the representation would otherwise
-- grow exponentially in the value of exponent.
let exp : Int := 9 + countDigits m - (e : Int)
let exp := if exp < 0 then exp else 0
let e' := (10 : Int) ^ (e - exp.natAbs)
let left := (m / e').repr
let right := e' + m % e'
|>.repr.toSubstring.drop 1
|>.dropRightWhile (fun c => c = '0')
|>.toString
let exp := if exp = 0 then "" else "e" ++ exp.repr
s!"{sign}{left}.{right}{exp}"
-- shift a JsonNumber by a specified amount of places to the left
protected def shiftl : JsonNumber β Nat β JsonNumber
-- if s β€ e, then 10 ^ (s - e) = 1, and hence the mantissa remains unchanged.
-- otherwise, the expression pads the mantissa with zeroes
-- to accomodate for the remaining places to shift.
| β¨m, eβ©, s => β¨m * (10 ^ (s - e) : Nat), e - sβ©
-- shift a JsonNumber by a specified amount of places to the right
protected def shiftr : JsonNumber β Nat β JsonNumber
| β¨m, eβ©, s => β¨m, e + sβ©
instance : ToString JsonNumber := β¨JsonNumber.toStringβ©
instance : Repr JsonNumber where
reprPrec | β¨m, eβ©, _ => Std.Format.bracket "β¨" (repr m ++ "," ++ repr e) "β©"
end JsonNumber
def strLt (a b : String) := Decidable.decide (a < b)
open Std (RBNode RBNode.leaf)
inductive Json where
| null
| bool (b : Bool)
| num (n : JsonNumber)
| str (s : String)
| arr (elems : Array Json)
-- uses RBNode instead of RBMap because RBMap is a def
-- and thus currently cannot be used to define a type that
-- is recursive in one of its parameters
| obj (kvPairs : RBNode String (fun _ => Json))
deriving Inhabited
namespace Json
-- HACK(Marc): temporary ugliness until we can use RBMap for JSON objects
def mkObj (o : List (String Γ Json)) : Json :=
obj $ do
let mut kvPairs := RBNode.leaf
for β¨k, vβ© in o do
kvPairs := kvPairs.insert compare k v
kvPairs
instance : Coe Nat Json := β¨fun n => Json.num nβ©
instance : Coe Int Json := β¨fun n => Json.num nβ©
instance : Coe String Json := β¨Json.strβ©
instance : Coe Bool Json := β¨Json.boolβ©
def isNull : Json -> Bool
| null => true
| _ => false
def getObj? : Json β Except String (RBNode String (fun _ => Json))
| obj kvs => kvs
| _ => throw "object expected"
def getArr? : Json β Except String (Array Json)
| arr a => a
| _ => throw "array expected"
def getStr? : Json β Except String String
| str s => s
| _ => throw "String expected"
def getNat? : Json β Except String Nat
| (n : Nat) => n
| _ => throw "Natural number expected"
def getInt? : Json β Except String Int
| (i : Int) => i
| _ => throw "Integer expected"
def getBool? : Json β Except String Bool
| (b : Bool) => b
| _ => throw "Bool expected"
def getNum? : Json β Except String JsonNumber
| num n => n
| _ => throw "number expected"
def getObjVal? : Json β String β Except String Json
| obj kvs, k =>
match kvs.find compare k with
| some v => v
| none => throw s!"property not found: {k}"
| _ , _ => throw "object expected"
def getArrVal? : Json β Nat β Except String Json
| arr a, i =>
match a.get? i with
| some v => v
| none => throw s!"index out of bounds: {i}"
| _ , _ => throw "array expected"
def getObjValD (j : Json) (k : String) : Json :=
(j.getObjVal? k).toOption.getD null
def setObjVal! : Json β String β Json β Json
| obj kvs, k, v => obj <| kvs.insert compare k v
| j , _, _ => panic! "Json.setObjVal!: not an object: {j}"
inductive Structured where
| arr (elems : Array Json)
| obj (kvPairs : RBNode String (fun _ => Json))
instance : Coe (Array Json) Structured := β¨Structured.arrβ©
instance : Coe (RBNode String (fun _ => Json)) Structured := β¨Structured.objβ©
end Json
end Lean
|
3cbe8b5f8f6cdbe57fd7e3bfcbece4fb96273ba2 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/number_theory/dioph.lean | fe4a89d6d0409fe38ade10f42aa15a42c5080f01 | [
"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 | 28,071 | 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 data.fin.fin2
import data.pfun
import data.vector3
import number_theory.pell
/-!
# Diophantine functions and Matiyasevic's theorem
Hilbert's tenth problem asked whether there exists an algorithm which for a given integer polynomial
determines whether this polynomial has integer solutions. It was answered in the negative in 1970,
the final step being completed by Matiyasevic who showed that the power function is Diophantine.
Here a function is called Diophantine if its graph is Diophantine as a set. A subset `S β β ^ Ξ±` in
turn is called Diophantine if there exists an integer polynomial on `Ξ± β Ξ²` such that `v β S` iff
there exists `t : β^Ξ²` with `p (v, t) = 0`.
## Main definitions
* `is_poly`: a predicate stating that a function is a multivariate integer polynomial.
* `poly`: the type of multivariate integer polynomial functions.
* `dioph`: a predicate stating that a set `S β β^Ξ±` is Diophantine, i.e. that
* `dioph_fn`: a predicate on a function stating that it is Diophantine in the sense that its graph
is Diophantine as a set.
## Main statements
* `pell_dioph` states that solutions to Pell's equation form a Diophantine set.
* `pow_dioph` states that the power function is Diophantine, a version of Matiyasevic's theorem.
## References
* [M. Carneiro, _A Lean formalization of Matiyasevic's theorem_][carneiro2018matiyasevic]
* [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916]
## Tags
Matiyasevic's theorem, Hilbert's tenth problem
## TODO
* Finish the solution of Hilbert's tenth problem.
* Connect `poly` to `mv_polynomial`
-/
universe u
open nat function
namespace int
lemma eq_nat_abs_iff_mul (x n) : nat_abs x = n β (x - n) * (x + n) = 0 :=
begin
refine iff.trans _ mul_eq_zero.symm,
refine iff.trans _ (or_congr sub_eq_zero add_eq_zero_iff_eq_neg).symm,
exact β¨Ξ»e, by rw β e; apply nat_abs_eq,
Ξ»o, by cases o; subst x; simp [nat_abs_of_nat]β©
end
end int
open fin2
/-- `list_all p l` is equivalent to `β a β l, p a`, but unfolds directly to a conjunction,
i.e. `list_all p [0, 1, 2] = p 0 β§ p 1 β§ p 2`. -/
@[simp] def list_all {Ξ±} (p : Ξ± β Prop) : list Ξ± β Prop
| [] := true
| (x :: []) := p x
| (x :: l) := p x β§ list_all l
@[simp] theorem list_all_cons {Ξ±} (p : Ξ± β Prop) (x : Ξ±) :
β (l : list Ξ±), list_all p (x :: l) β p x β§ list_all p l
| [] := (and_true _).symm
| (x :: l) := iff.rfl
theorem list_all_iff_forall {Ξ±} (p : Ξ± β Prop) : β (l : list Ξ±), list_all p l β β x β l, p x
| [] := (iff_true_intro $ list.ball_nil _).symm
| (x :: l) := by rw [list.ball_cons, β list_all_iff_forall l]; simp
theorem list_all.imp {Ξ±} {p q : Ξ± β Prop} (h : β x, p x β q x) :
β {l : list Ξ±}, list_all p l β list_all q l
| [] := id
| (x :: l) := by simpa using and.imp (h x) list_all.imp
@[simp] theorem list_all_map {Ξ± Ξ²} {p : Ξ² β Prop} (f : Ξ± β Ξ²) {l : list Ξ±} :
list_all p (l.map f) β list_all (p β f) l :=
by induction l; simp *
theorem list_all_congr {Ξ±} {p q : Ξ± β Prop} (h : β x, p x β q x) {l : list Ξ±} :
list_all p l β list_all q l :=
β¨list_all.imp (Ξ»x, (h x).1), list_all.imp (Ξ»x, (h x).2)β©
instance decidable_list_all {Ξ±} (p : Ξ± β Prop) [decidable_pred p] (l : list Ξ±) :
decidable (list_all p l) :=
decidable_of_decidable_of_iff (by apply_instance) (list_all_iff_forall _ _).symm
/- poly -/
/-- A predicate asserting that a function is a multivariate integer polynomial.
(We are being a bit lazy here by allowing many representations for multiplication,
rather than only allowing monomials and addition, but the definition is equivalent
and this is easier to use.) -/
inductive is_poly {Ξ±} : ((Ξ± β β) β β€) β Prop
| proj : β i, is_poly (Ξ»x : Ξ± β β, x i)
| const : Ξ (n : β€), is_poly (Ξ»x : Ξ± β β, n)
| sub : Ξ {f g : (Ξ± β β) β β€}, is_poly f β is_poly g β is_poly (Ξ»x, f x - g x)
| mul : Ξ {f g : (Ξ± β β) β β€}, is_poly f β is_poly g β is_poly (Ξ»x, f x * g x)
/-- The type of multivariate integer polynomials -/
def poly (Ξ± : Type u) := {f : (Ξ± β β) β β€ // is_poly f}
namespace poly
section
parameter {Ξ± : Type u}
instance : has_coe_to_fun (poly Ξ±) (Ξ» _, (Ξ± β β) β β€) := β¨Ξ» f, f.1β©
/-- The underlying function of a `poly` is a polynomial -/
lemma isp (f : poly Ξ±) : is_poly f := f.2
/-- Extensionality for `poly Ξ±` -/
lemma ext {f g : poly Ξ±} (e : βx, f x = g x) : f = g :=
subtype.eq (funext e)
/-- Construct a `poly` given an extensionally equivalent `poly`. -/
def subst (f : poly Ξ±) (g : (Ξ± β β) β β€) (e : βx, f x = g x) : poly Ξ± :=
β¨g, by rw β (funext e : coe_fn f = g); exact f.ispβ©
@[simp] theorem subst_eval (f g e x) : subst f g e x = g x := rfl
/-- The `i`th projection function, `x_i`. -/
def proj (i) : poly Ξ± := β¨_, is_poly.proj iβ©
@[simp] theorem proj_eval (i x) : proj i x = x i := rfl
/-- The constant function with value `n : β€`. -/
def const (n) : poly Ξ± := β¨_, is_poly.const nβ©
@[simp] theorem const_eval (n x) : const n x = n := rfl
/-- The zero polynomial -/
def zero : poly Ξ± := const 0
instance : has_zero (poly Ξ±) := β¨poly.zeroβ©
@[simp] theorem zero_eval (x) : (0 : poly Ξ±) x = 0 := rfl
/-- The zero polynomial -/
def one : poly Ξ± := const 1
instance : has_one (poly Ξ±) := β¨poly.oneβ©
@[simp] theorem one_eval (x) : (1 : poly Ξ±) x = 1 := rfl
/-- Subtraction of polynomials -/
def sub : poly Ξ± β poly Ξ± β poly Ξ± | β¨f, pfβ© β¨g, pgβ© :=
β¨_, is_poly.sub pf pgβ©
instance : has_sub (poly Ξ±) := β¨poly.subβ©
@[simp] theorem sub_eval : Ξ (f g x), (f - g : poly Ξ±) x = f x - g x
| β¨f, pfβ© β¨g, pgβ© x := rfl
/-- Negation of a polynomial -/
def neg (f : poly Ξ±) : poly Ξ± := 0 - f
instance : has_neg (poly Ξ±) := β¨poly.negβ©
@[simp] theorem neg_eval (f x) : (-f : poly Ξ±) x = -f x :=
show (0-f) x = _, by simp
/-- Addition of polynomials -/
def add : poly Ξ± β poly Ξ± β poly Ξ± | β¨f, pfβ© β¨g, pgβ© :=
subst (β¨f, pfβ© - -β¨g, pgβ©) _
(Ξ»x, show f x - (0 - g x) = f x + g x, by simp)
instance : has_add (poly Ξ±) := β¨poly.addβ©
@[simp] theorem add_eval : Ξ (f g x), (f + g : poly Ξ±) x = f x + g x
| β¨f, pfβ© β¨g, pgβ© x := rfl
/-- Multiplication of polynomials -/
def mul : poly Ξ± β poly Ξ± β poly Ξ± | β¨f, pfβ© β¨g, pgβ© :=
β¨_, is_poly.mul pf pgβ©
instance : has_mul (poly Ξ±) := β¨poly.mulβ©
@[simp] theorem mul_eval : Ξ (f g x), (f * g : poly Ξ±) x = f x * g x
| β¨f, pfβ© β¨g, pgβ© x := rfl
instance : comm_ring (poly Ξ±) := by refine_struct
{ add := ((+) : poly Ξ± β poly Ξ± β poly Ξ±),
zero := 0,
neg := (has_neg.neg : poly Ξ± β poly Ξ±),
mul := ((*)),
one := 1,
sub := (has_sub.sub),
npow := @npow_rec _ β¨(1 : poly Ξ±)β© β¨(*)β©,
nsmul := @nsmul_rec _ β¨(0 : poly Ξ±)β© β¨(+)β©,
zsmul := @zsmul_rec _ β¨(0 : poly Ξ±)β© β¨(+)β© β¨negβ© };
intros; try { refl }; refine ext (Ξ» _, _);
simp [sub_eq_add_neg, mul_add, mul_left_comm, mul_comm, add_comm, add_assoc]
lemma induction {C : poly Ξ± β Prop}
(H1 : βi, C (proj i)) (H2 : βn, C (const n))
(H3 : βf g, C f β C g β C (f - g))
(H4 : βf g, C f β C g β C (f * g)) (f : poly Ξ±) : C f :=
begin
cases f with f pf,
induction pf with i n f g pf pg ihf ihg f g pf pg ihf ihg,
apply H1, apply H2, apply H3 _ _ ihf ihg, apply H4 _ _ ihf ihg
end
/-- The sum of squares of a list of polynomials. This is relevant for
Diophantine equations, because it means that a list of equations
can be encoded as a single equation: `x = 0 β§ y = 0 β§ z = 0` is
equivalent to `x^2 + y^2 + z^2 = 0`. -/
def sumsq : list (poly Ξ±) β poly Ξ±
| [] := 0
| (p::ps) := p*p + sumsq ps
theorem sumsq_nonneg (x) : β l, 0 β€ sumsq l x
| [] := le_refl 0
| (p::ps) := by rw sumsq; simp [-add_comm];
exact add_nonneg (mul_self_nonneg _) (sumsq_nonneg ps)
theorem sumsq_eq_zero (x) : β l, sumsq l x = 0 β list_all (Ξ»a : poly Ξ±, a x = 0) l
| [] := eq_self_iff_true _
| (p::ps) := by rw [list_all_cons, β sumsq_eq_zero ps]; rw sumsq; simp [-add_comm]; exact
β¨Ξ»(h : p x * p x + sumsq ps x = 0),
have p x = 0, from eq_zero_of_mul_self_eq_zero $ le_antisymm
(by rw β h; have t := add_le_add_left (sumsq_nonneg x ps) (p x * p x); rwa [add_zero] at t)
(mul_self_nonneg _),
β¨this, by simp [this] at h; exact hβ©,
Ξ»β¨h1, h2β©, by rw [h1, h2]; reflβ©
end
/-- Map the index set of variables, replacing `x_i` with `x_(f i)`. -/
def remap {Ξ± Ξ²} (f : Ξ± β Ξ²) (g : poly Ξ±) : poly Ξ² :=
β¨Ξ»v, g $ v β f, g.induction
(Ξ»i, by simp; apply is_poly.proj)
(Ξ»n, by simp; apply is_poly.const)
(Ξ»f g pf pg, by simp; apply is_poly.sub pf pg)
(Ξ»f g pf pg, by simp; apply is_poly.mul pf pg)β©
@[simp] theorem remap_eval {Ξ± Ξ²} (f : Ξ± β Ξ²) (g : poly Ξ±) (v) : remap f g v = g (v β f) := rfl
end poly
namespace sum
/-- combine two functions into a function on the disjoint union -/
def join {Ξ± Ξ² Ξ³} (f : Ξ± β Ξ³) (g : Ξ² β Ξ³) : Ξ± β Ξ² β Ξ³ :=
by {refine sum.rec _ _, exacts [f, g]}
end sum
local infixr ` β `:65 := sum.join
open sum
namespace option
/-- Functions from `option` can be combined similarly to `vector.cons` -/
def cons {Ξ± Ξ²} (a : Ξ²) (v : Ξ± β Ξ²) : option Ξ± β Ξ² :=
by {refine option.rec _ _, exacts [a, v]}
notation a :: b := cons a b
@[simp] theorem cons_head_tail {Ξ± Ξ²} (v : option Ξ± β Ξ²) : v none :: v β some = v :=
funext $ Ξ»o, by cases o; refl
end option
/- dioph -/
/-- A set `S β β^Ξ±` is Diophantine if there exists a polynomial on
`Ξ± β Ξ²` such that `v β S` iff there exists `t : β^Ξ²` with `p (v, t) = 0`. -/
def dioph {Ξ± : Type u} (S : set (Ξ± β β)) : Prop :=
β {Ξ² : Type u} (p : poly (Ξ± β Ξ²)), β (v : Ξ± β β), S v β βt, p (v β t) = 0
namespace dioph
section
variables {Ξ± Ξ² Ξ³ : Type u}
theorem ext {S S' : set (Ξ± β β)} (d : dioph S) (H : βv, S v β S' v) : dioph S' :=
eq.rec d $ show S = S', from set.ext H
theorem of_no_dummies (S : set (Ξ± β β)) (p : poly Ξ±) (h : β (v : Ξ± β β), S v β p v = 0) :
dioph S :=
β¨ulift empty, p.remap inl, Ξ»v, (h v).trans
β¨Ξ»h, β¨Ξ»t, empty.rec _ t.down, by simp; rw [
show (v β Ξ»t:ulift empty, empty.rec _ t.down) β inl = v, from rfl, h]β©,
Ξ»β¨t, htβ©, by simp at ht; rwa [show (v β t) β inl = v, from rfl] at htβ©β©
lemma inject_dummies_lem (f : Ξ² β Ξ³) (g : Ξ³ β option Ξ²) (inv : β x, g (f x) = some x)
(p : poly (Ξ± β Ξ²)) (v : Ξ± β β) : (βt, p (v β t) = 0) β
(βt, p.remap (inl β (inr β f)) (v β t) = 0) :=
begin
simp, refine β¨Ξ»t, _, Ξ»t, _β©; cases t with t ht,
{ have : (v β (0 :: t) β g) β (inl β inr β f) = v β t :=
funext (Ξ»s, by cases s with a b; dsimp [join, (β)]; try {rw inv}; refl),
exact β¨(0 :: t) β g, by rwa thisβ© },
{ have : v β t β f = (v β t) β (inl β inr β f) :=
funext (Ξ»s, by cases s with a b; refl),
exact β¨t β f, by rwa thisβ© }
end
theorem inject_dummies {S : set (Ξ± β β)} (f : Ξ² β Ξ³) (g : Ξ³ β option Ξ²)
(inv : β x, g (f x) = some x) (p : poly (Ξ± β Ξ²)) (h : β (v : Ξ± β β), S v β βt, p (v β t) = 0) :
β q : poly (Ξ± β Ξ³), β (v : Ξ± β β), S v β βt, q (v β t) = 0 :=
β¨p.remap (inl β (inr β f)), Ξ»v, (h v).trans $ inject_dummies_lem f g inv _ _β©
theorem reindex_dioph {S : set (Ξ± β β)} : Ξ (d : dioph S) (f : Ξ± β Ξ²), dioph (Ξ»v, S (v β f))
| β¨Ξ³, p, peβ© f := β¨Ξ³, p.remap ((inl β f) β inr), Ξ»v, (pe _).trans $ exists_congr $ Ξ»t,
suffices v β f β t = (v β t) β (inl β f β inr), by simp [this],
funext $ Ξ»s, by cases s with a b; reflβ©
theorem dioph_list_all (l) (d : list_all dioph l) :
dioph (Ξ»v, list_all (Ξ»S : set (Ξ± β β), S v) l) :=
suffices β Ξ² (pl : list (poly (Ξ± β Ξ²))), β v,
list_all (Ξ»S : set _, S v) l β βt, list_all (Ξ»p : poly (Ξ± β Ξ²), p (v β t) = 0) pl,
from let β¨Ξ², pl, hβ© := this
in β¨Ξ², poly.sumsq pl, Ξ»v, (h v).trans $ exists_congr $ Ξ»t, (poly.sumsq_eq_zero _ _).symmβ©,
begin
induction l with S l IH,
exact β¨ulift empty, [], Ξ»v, by simp; exact β¨Ξ»β¨tβ©, empty.rec _ t, trivialβ©β©,
simp at d,
exact let β¨β¨Ξ², p, peβ©, dlβ© := d, β¨Ξ³, pl, pleβ© := IH dl in
β¨Ξ² β Ξ³, p.remap (inl β inr β inl) :: pl.map (Ξ»q, q.remap (inl β (inr β inr))), Ξ»v,
by simp; exact iff.trans (and_congr (pe v) (ple v))
β¨Ξ»β¨β¨m, hmβ©, β¨n, hnβ©β©,
β¨m β n, by rw [
show (v β m β n) β (inl β inr β inl) = v β m,
from funext $ Ξ»s, by cases s with a b; refl]; exact hm,
by { refine list_all.imp (Ξ»q hq, _) hn, dsimp [(β)],
rw [show (Ξ» (x : Ξ± β Ξ³), (v β m β n) ((inl β Ξ» (x : Ξ³), inr (inr x)) x)) = v β n,
from funext $ Ξ»s, by cases s with a b; refl]; exact hq }β©,
Ξ»β¨t, hl, hrβ©,
β¨β¨t β inl, by rwa [
show (v β t) β (inl β inr β inl) = v β t β inl,
from funext $ Ξ»s, by cases s with a b; refl] at hlβ©,
β¨t β inr, by {
refine list_all.imp (Ξ»q hq, _) hr, dsimp [(β)] at hq,
rwa [show (Ξ» (x : Ξ± β Ξ³), (v β t) ((inl β Ξ» (x : Ξ³), inr (inr x)) x)) = v β t β inr,
from funext $ Ξ»s, by cases s with a b; refl] at hq }β©β©β©β©
end
theorem and_dioph {S S' : set (Ξ± β β)} (d : dioph S) (d' : dioph S') : dioph (Ξ»v, S v β§ S' v) :=
dioph_list_all [S, S'] β¨d, d'β©
theorem or_dioph {S S' : set (Ξ± β β)} : β (d : dioph S) (d' : dioph S'), dioph (Ξ»v, S v β¨ S' v)
| β¨Ξ², p, peβ© β¨Ξ³, q, qeβ© := β¨Ξ² β Ξ³, p.remap (inl β inr β inl) * q.remap (inl β inr β inr), Ξ»v,
begin
refine iff.trans (or_congr ((pe v).trans _) ((qe v).trans _))
(exists_or_distrib.symm.trans (exists_congr $ Ξ»t,
(@mul_eq_zero _ _ _ (p ((v β t) β (inl β inr β inl)))
(q ((v β t) β (inl β inr β inr)))).symm)),
exact inject_dummies_lem _ (some β (Ξ»_, none)) (Ξ»x, rfl) _ _,
exact inject_dummies_lem _ ((Ξ»_, none) β some) (Ξ»x, rfl) _ _,
endβ©
/-- A partial function is Diophantine if its graph is Diophantine. -/
def dioph_pfun (f : (Ξ± β β) β. β) := dioph (Ξ»v : option Ξ± β β, f.graph (v β some, v none))
/-- A function is Diophantine if its graph is Diophantine. -/
def dioph_fn (f : (Ξ± β β) β β) := dioph (Ξ»v : option Ξ± β β, f (v β some) = v none)
theorem reindex_dioph_fn {f : (Ξ± β β) β β} (d : dioph_fn f) (g : Ξ± β Ξ²) :
dioph_fn (Ξ»v, f (v β g)) :=
reindex_dioph d (functor.map g)
theorem ex_dioph {S : set (Ξ± β Ξ² β β)} : dioph S β dioph (Ξ»v, βx, S (v β x))
| β¨Ξ³, p, peβ© := β¨Ξ² β Ξ³, p.remap ((inl β inr β inl) β inr β inr), Ξ»v,
β¨Ξ»β¨x, hxβ©, let β¨t, htβ© := (pe _).1 hx in β¨x β t, by simp; rw [
show (v β x β t) β ((inl β inr β inl) β inr β inr) = (v β x) β t,
from funext $ Ξ»s, by cases s with a b; try {cases a}; refl]; exact htβ©,
Ξ»β¨t, htβ©, β¨t β inl, (pe _).2 β¨t β inr, by simp at ht; rwa [
show (v β t) β ((inl β inr β inl) β inr β inr) = (v β t β inl) β t β inr,
from funext $ Ξ»s, by cases s with a b; try {cases a}; refl] at htβ©β©β©β©
theorem ex1_dioph {S : set (option Ξ± β β)} : dioph S β dioph (Ξ»v, βx, S (x :: v))
| β¨Ξ², p, peβ© := β¨option Ξ², p.remap (inr none :: inl β inr β some), Ξ»v,
β¨Ξ»β¨x, hxβ©, let β¨t, htβ© := (pe _).1 hx in β¨x :: t, by simp; rw [
show (v β x :: t) β (inr none :: inl β inr β some) = x :: v β t,
from funext $ Ξ»s, by cases s with a b; try {cases a}; refl]; exact htβ©,
Ξ»β¨t, htβ©, β¨t none, (pe _).2 β¨t β some, by simp at ht; rwa [
show (v β t) β (inr none :: inl β inr β some) = t none :: v β t β some,
from funext $ Ξ»s, by cases s with a b; try {cases a}; refl] at htβ©β©β©β©
theorem dom_dioph {f : (Ξ± β β) β. β} (d : dioph_pfun f) : dioph f.dom :=
cast (congr_arg dioph $ set.ext $ Ξ»v, (pfun.dom_iff_graph _ _).symm) (ex1_dioph d)
theorem dioph_fn_iff_pfun (f : (Ξ± β β) β β) : dioph_fn f = @dioph_pfun Ξ± f :=
by refine congr_arg dioph (set.ext $ Ξ»v, _); exact pfun.lift_graph.symm
theorem abs_poly_dioph (p : poly Ξ±) : dioph_fn (Ξ»v, (p v).nat_abs) :=
by refine of_no_dummies _ ((p.remap some - poly.proj none) * (p.remap some + poly.proj none))
(Ξ»v, _); apply int.eq_nat_abs_iff_mul
theorem proj_dioph (i : Ξ±) : dioph_fn (Ξ»v, v i) :=
abs_poly_dioph (poly.proj i)
theorem dioph_pfun_comp1 {S : set (option Ξ± β β)} (d : dioph S) {f} (df : dioph_pfun f) :
dioph (Ξ»v : Ξ± β β, β h : f.dom v, S (f.fn v h :: v)) :=
ext (ex1_dioph (and_dioph d df)) $ Ξ»v,
β¨Ξ»β¨x, hS, (h: Exists _)β©, by
rw [show (x :: v) β some = v, from funext $ Ξ»s, rfl] at h;
cases h with hf h; refine β¨hf, _β©; rw [pfun.fn, h]; exact hS,
Ξ»β¨x, hSβ©, β¨f.fn v x, hS, show Exists _,
by rw [show (f.fn v x :: v) β some = v, from funext $ Ξ»s, rfl]; exact β¨x, rflβ©β©β©
theorem dioph_fn_comp1 {S : set (option Ξ± β β)} (d : dioph S) {f : (Ξ± β β) β β} (df : dioph_fn f) :
dioph (Ξ»v : Ξ± β β, S (f v :: v)) :=
ext (dioph_pfun_comp1 d (cast (dioph_fn_iff_pfun f) df)) $ Ξ»v,
β¨Ξ»β¨_, hβ©, h, Ξ»h, β¨trivial, hβ©β©
end
section
variables {Ξ± Ξ² Ξ³ : Type}
open vector3
open_locale vector3
theorem dioph_fn_vec_comp1 {n} {S : set (vector3 β (succ n))} (d : dioph S) {f : (vector3 β n) β β}
(df : dioph_fn f) :
dioph (Ξ»v : vector3 β n, S (cons (f v) v)) :=
ext (dioph_fn_comp1 (reindex_dioph d (none :: some)) df) $ Ξ»v, by rw [
show option.cons (f v) v β (cons none some) = f v :: v,
from funext $ Ξ»s, by cases s with a b; refl]
theorem vec_ex1_dioph (n) {S : set (vector3 β (succ n))} (d : dioph S) :
dioph (Ξ»v : vector3 β n, βx, S (x :: v)) :=
ext (ex1_dioph $ reindex_dioph d (none :: some)) $ Ξ»v, exists_congr $ Ξ»x, by rw [
show (option.cons x v) β (cons none some) = x :: v,
from funext $ Ξ»s, by cases s with a b; refl]
theorem dioph_fn_vec {n} (f : vector3 β n β β) :
dioph_fn f β dioph (Ξ»v : vector3 β (succ n), f (v β fs) = v fz) :=
β¨Ξ»h, reindex_dioph h (fz :: fs), Ξ»h, reindex_dioph h (none :: some)β©
theorem dioph_pfun_vec {n} (f : vector3 β n β. β) :
dioph_pfun f β dioph (Ξ»v : vector3 β (succ n), f.graph (v β fs, v fz)) :=
β¨Ξ»h, reindex_dioph h (fz :: fs), Ξ»h, reindex_dioph h (none :: some)β©
theorem dioph_fn_compn {Ξ± : Type} : β {n} {S : set (Ξ± β fin2 n β β)} (d : dioph S)
{f : vector3 ((Ξ± β β) β β) n} (df : vector_allp dioph_fn f),
dioph (Ξ»v : Ξ± β β, S (v β Ξ»i, f i v))
| 0 S d f := Ξ»df, ext (reindex_dioph d (id β fin2.elim0)) $ Ξ»v,
by refine eq.to_iff (congr_arg S $ funext $ Ξ»s, _); {cases s with a b, refl, cases b}
| (succ n) S d f := f.cons_elim $ Ξ»f fl, by simp; exact Ξ» df dfl,
have dioph (Ξ»v, S (v β inl β f (v β inl) :: v β inr)),
from ext (dioph_fn_comp1 (reindex_dioph d (some β inl β none :: some β inr))
(reindex_dioph_fn df inl)) $
Ξ»v, by {refine eq.to_iff (congr_arg S $ funext $ Ξ»s, _); cases s with a b, refl, cases b; refl},
have dioph (Ξ»v, S (v β f v :: Ξ» (i : fin2 n), fl i v)),
from @dioph_fn_compn n (Ξ»v, S (v β inl β f (v β inl) :: v β inr)) this _ dfl,
ext this $ Ξ»v, by rw [
show cons (f v) (Ξ» (i : fin2 n), fl i v) = Ξ» (i : fin2 (succ n)), (f :: fl) i v,
from funext $ Ξ»s, by cases s with a b; refl]
theorem dioph_comp {n} {S : set (vector3 β n)} (d : dioph S)
(f : vector3 ((Ξ± β β) β β) n) (df : vector_allp dioph_fn f) : dioph (Ξ»v, S (Ξ»i, f i v)) :=
dioph_fn_compn (reindex_dioph d inr) df
theorem dioph_fn_comp {n} {f : vector3 β n β β} (df : dioph_fn f)
(g : vector3 ((Ξ± β β) β β) n) (dg : vector_allp dioph_fn g) : dioph_fn (Ξ»v, f (Ξ»i, g i v)) :=
dioph_comp ((dioph_fn_vec _).1 df) ((Ξ»v, v none) :: Ξ»i v, g i (v β some)) $
by simp; exact β¨proj_dioph none, (vector_allp_iff_forall _ _).2 $ Ξ»i,
reindex_dioph_fn ((vector_allp_iff_forall _ _).1 dg _) _β©
localized "notation x ` Dβ§ `:35 y := dioph.and_dioph x y" in dioph
localized "notation x ` D⨠`:35 y := dioph.or_dioph x y" in dioph
localized "notation `Dβ`:30 := dioph.vec_ex1_dioph" in dioph
localized "prefix `&`:max := of_nat'" in dioph
theorem proj_dioph_of_nat {n : β} (m : β) [is_lt m n] : dioph_fn (Ξ»v : vector3 β n, v &m) :=
proj_dioph &m
localized "prefix `D&`:100 := dioph.proj_dioph_of_nat" in dioph
theorem const_dioph (n : β) : dioph_fn (const (Ξ± β β) n) :=
abs_poly_dioph (poly.const n)
localized "prefix `D.`:100 := dioph.const_dioph" in dioph
variables {f g : (Ξ± β β) β β} (df : dioph_fn f) (dg : dioph_fn g)
include df dg
theorem dioph_comp2 {S : β β β β Prop} (d : dioph (Ξ»v:vector3 β 2, S (v &0) (v &1))) :
dioph (Ξ»v, S (f v) (g v)) :=
dioph_comp d [f, g] (by exact β¨df, dgβ©)
theorem dioph_fn_comp2 {h : β β β β β} (d : dioph_fn (Ξ»v:vector3 β 2, h (v &0) (v &1))) :
dioph_fn (Ξ»v, h (f v) (g v)) :=
dioph_fn_comp d [f, g] (by exact β¨df, dgβ©)
theorem eq_dioph : dioph (Ξ»v, f v = g v) :=
dioph_comp2 df dg $ of_no_dummies _ (poly.proj &0 - poly.proj &1)
(Ξ»v, (int.coe_nat_eq_coe_nat_iff _ _).symm.trans
β¨@sub_eq_zero_of_eq β€ _ (v &0) (v &1), eq_of_sub_eq_zeroβ©)
localized "infix ` D= `:50 := dioph.eq_dioph" in dioph
theorem add_dioph : dioph_fn (Ξ»v, f v + g v) :=
dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 + poly.proj &1)
localized "infix ` D+ `:80 := dioph.add_dioph" in dioph
theorem mul_dioph : dioph_fn (Ξ»v, f v * g v) :=
dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 * poly.proj &1)
localized "infix ` D* `:90 := dioph.mul_dioph" in dioph
theorem le_dioph : dioph (Ξ»v, f v β€ g v) :=
dioph_comp2 df dg $ ext (Dβ2 $ D&1 D+ D&0 D= D&2) (Ξ»v, β¨Ξ»β¨x, hxβ©, le.intro hx, le.destβ©)
localized "infix ` Dβ€ `:50 := dioph.le_dioph" in dioph
theorem lt_dioph : dioph (Ξ»v, f v < g v) := df D+ (D. 1) Dβ€ dg
localized "infix ` D< `:50 := dioph.lt_dioph" in dioph
theorem ne_dioph : dioph (Ξ»v, f v β g v) :=
ext (df D< dg D⨠dg D< df) $ λv, ne_iff_lt_or_gt.symm
localized "infix ` Dβ `:50 := dioph.ne_dioph" in dioph
theorem sub_dioph : dioph_fn (Ξ»v, f v - g v) :=
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $
ext (D&1 D= D&0 D+ D&2 D⨠D&1 D†D&2 D⧠D&0 D= D.0) $ (vector_all_iff_forall _).1 $ λx y z,
show (y = x + z β¨ y β€ z β§ x = 0) β y - z = x, from
β¨Ξ»o, begin
rcases o with ae | β¨yz, x0β©,
{ rw [ae, add_tsub_cancel_right] },
{ rw [x0, tsub_eq_zero_iff_le.mpr yz] }
end, Ξ»h, begin
subst x,
cases le_total y z with yz zy,
{ exact or.inr β¨yz, tsub_eq_zero_iff_le.mpr yzβ© },
{ exact or.inl (tsub_add_cancel_of_le zy).symm },
endβ©
localized "infix ` D- `:80 := dioph.sub_dioph" in dioph
theorem dvd_dioph : dioph (Ξ»v, f v β£ g v) :=
dioph_comp (Dβ2 $ D&2 D= D&1 D* D&0) [f, g] (by exact β¨df, dgβ©)
localized "infix ` Dβ£ `:50 := dioph.dvd_dioph" in dioph
theorem mod_dioph : dioph_fn (Ξ»v, f v % g v) :=
have dioph (Ξ»v : vector3 β 3, (v &2 = 0 β¨ v &0 < v &2) β§ β (x : β), v &0 + v &2 * x = v &1),
from (D&2 D= D.0 Dβ¨ D&0 D< D&2) Dβ§ (Dβ3 $ D&1 D+ D&3 D* D&0 D= D&2),
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ Ξ»z x y,
show ((y = 0 β¨ z < y) β§ β c, z + y * c = x) β x % y = z, from
β¨Ξ»β¨h, c, hcβ©, begin rw β hc; simp; cases h with x0 hl, rw [x0, mod_zero], exact mod_eq_of_lt hl end,
Ξ»e, by rw β e; exact β¨or_iff_not_imp_left.2 $ Ξ»h, mod_lt _ (nat.pos_of_ne_zero h), x / y,
mod_add_div _ _β©β©
localized "infix ` D% `:80 := dioph.mod_dioph" in dioph
theorem modeq_dioph {h : (Ξ± β β) β β} (dh : dioph_fn h) : dioph (Ξ»v, f v β‘ g v [MOD h v]) :=
df D% dh D= dg D% dh
localized "notation `Dβ‘` := dioph.modeq_dioph" in dioph
theorem div_dioph : dioph_fn (Ξ»v, f v / g v) :=
have dioph (Ξ»v : vector3 β 3, v &2 = 0 β§ v &0 = 0 β¨ v &0 * v &2 β€ v &1 β§ v &1 < (v &0 + 1) * v &2),
from (D&2 D= D.0 D⧠D&0 D= D.0) D⨠D&0 D* D&2 D†D&1 D⧠D&1 D< (D&0 D+ D.1) D* D&2,
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ Ξ»z x y,
show y = 0 β§ z = 0 β¨ z * y β€ x β§ x < (z + 1) * y β x / y = z,
by refine iff.trans _ eq_comm; exact y.eq_zero_or_pos.elim
(Ξ»y0, by rw [y0, nat.div_zero]; exact
β¨Ξ»o, (o.resolve_right $ Ξ»β¨_, h2β©, nat.not_lt_zero _ h2).right, Ξ»z0, or.inl β¨rfl, z0β©β©)
(Ξ»ypos, iff.trans β¨Ξ»o, o.resolve_left $ Ξ»β¨h1, _β©, ne_of_gt ypos h1, or.inrβ©
(le_antisymm_iff.trans $ and_congr (nat.le_div_iff_mul_le _ _ ypos) $
iff.trans β¨lt_succ_of_le, le_of_lt_succβ© (div_lt_iff_lt_mul _ _ ypos)).symm)
localized "infix ` D/ `:80 := dioph.div_dioph" in dioph
omit df dg
open pell
theorem pell_dioph : dioph (Ξ»v:vector3 β 4, β h : 1 < v &0,
xn h (v &1) = v &2 β§ yn h (v &1) = v &3) :=
have dioph {v : vector3 β 4 |
1 < v &0 β§ v &1 β€ v &3 β§
(v &2 = 1 β§ v &3 = 0 β¨
β (u w s t b : β),
v &2 * v &2 - (v &0 * v &0 - 1) * v &3 * v &3 = 1 β§
u * u - (v &0 * v &0 - 1) * w * w = 1 β§
s * s - (b * b - 1) * t * t = 1 β§
1 < b β§ (b β‘ 1 [MOD 4 * v &3]) β§ (b β‘ v &0 [MOD u]) β§
0 < w β§ v &3 * v &3 β£ w β§
(s β‘ v &2 [MOD u]) β§
(t β‘ v &1 [MOD 4 * v &3]))}, from
D.1 D< D&0 Dβ§ D&1 Dβ€ D&3 Dβ§
((D&2 D= D.1 Dβ§ D&3 D= D.0) Dβ¨
(Dβ4 $ Dβ5 $ Dβ6 $ Dβ7 $ Dβ8 $
D&7 D* D&7 D- (D&5 D* D&5 D- D.1) D* D&8 D* D&8 D= D.1 Dβ§
D&4 D* D&4 D- (D&5 D* D&5 D- D.1) D* D&3 D* D&3 D= D.1 Dβ§
D&2 D* D&2 D- (D&0 D* D&0 D- D.1) D* D&1 D* D&1 D= D.1 Dβ§
D.1 D< D&0 Dβ§ (Dβ‘ (D&0) (D.1) (D.4 D* D&8)) Dβ§ (Dβ‘ (D&0) (D&5) D&4) Dβ§
D.0 D< D&3 Dβ§ D&8 D* D&8 Dβ£ D&3 Dβ§
(Dβ‘ (D&2) (D&7) D&4) Dβ§
(Dβ‘ (D&1) (D&6) (D.4 D* D&8)))),
dioph.ext this $ Ξ»v, matiyasevic.symm
theorem xn_dioph : dioph_pfun (Ξ»v:vector3 β 2, β¨1 < v &0, Ξ»h, xn h (v &1)β©) :=
have dioph (Ξ»v:vector3 β 3, β y, β h : 1 < v &1, xn h (v &2) = v &0 β§ yn h (v &2) = y), from
let D_pell := @reindex_dioph _ (fin2 4) _ pell_dioph [&2, &3, &1, &0] in Dβ3 D_pell,
(dioph_pfun_vec _).2 $ dioph.ext this $ Ξ»v, β¨Ξ»β¨y, h, xe, yeβ©, β¨h, xeβ©, Ξ»β¨h, xeβ©, β¨_, h, xe, rflβ©β©
include df dg
/-- A version of **Matiyasevic's theorem** -/
theorem pow_dioph : dioph_fn (Ξ»v, f v ^ g v) :=
have dioph {v : vector3 β 3 |
v &2 = 0 β§ v &0 = 1 β¨ 0 < v &2 β§
(v &1 = 0 β§ v &0 = 0 β¨ 0 < v &1 β§
β (w a t z x y : β),
(β (a1 : 1 < a), xn a1 (v &2) = x β§ yn a1 (v &2) = y) β§
(x β‘ y * (a - v &1) + v &0 [MOD t]) β§
2 * a * v &1 = t + (v &1 * v &1 + 1) β§
v &0 < t β§ v &1 β€ w β§ v &2 β€ w β§
a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)}, from
let D_pell := @reindex_dioph _ (fin2 9) _ pell_dioph [&4, &8, &1, &0] in
(D&2 D= D.0 Dβ§ D&0 D= D.1) Dβ¨ (D.0 D< D&2 Dβ§
((D&1 D= D.0 Dβ§ D&0 D= D.0) Dβ¨ (D.0 D< D&1 Dβ§
(Dβ3 $ Dβ4 $ Dβ5 $ Dβ6 $ Dβ7 $ Dβ8 $ D_pell Dβ§
(Dβ‘ (D&1) (D&0 D* (D&4 D- D&7) D+ D&6) (D&3)) Dβ§
D.2 D* D&4 D* D&7 D= D&3 D+ (D&7 D* D&7 D+ D.1) Dβ§
D&6 D< D&3 Dβ§ D&7 Dβ€ D&5 Dβ§ D&8 Dβ€ D&5 Dβ§
D&4 D* D&4 D- ((D&5 D+ D.1) D* (D&5 D+ D.1) D- D.1) D* (D&5 D* D&2) D* (D&5 D* D&2) D= D.1)))),
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ dioph.ext this $ Ξ»v, iff.symm $
eq_pow_of_pell.trans $ or_congr iff.rfl $ and_congr iff.rfl $ or_congr iff.rfl $ and_congr iff.rfl $
β¨Ξ»β¨w, a, t, z, a1, hβ©, β¨w, a, t, z, _, _, β¨a1, rfl, rflβ©, hβ©,
Ξ»β¨w, a, t, z, ._, ._, β¨a1, rfl, rflβ©, hβ©, β¨w, a, t, z, a1, hβ©β©
end
end dioph
|
9412fee2934a2a3538ada65226455ba492473d79 | 99b5e6372af1f404777312358869f95be7de84a3 | /src/hott/hit/pushout.lean | 48b7aaf5212c1e058a1a312d6889cdf737b52016 | [
"Apache-2.0"
] | permissive | forked-from-1kasper/hott3 | 8fa064ab5e8c9d6752a783d74ab226ddc5b5232a | 2db24de7a361a7793b0eae4ca5c3fd4d4a0fc691 | refs/heads/master | 1,584,867,131,028 | 1,530,766,841,000 | 1,530,766,841,000 | 139,797,034 | 0 | 0 | Apache-2.0 | 1,530,766,961,000 | 1,530,766,961,000 | null | UTF-8 | Lean | false | false | 12,015 | lean | /-
Copyright (c) 2015-16 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Ulrik Buchholtz, Jakob von Raumer
Declaration and properties of the pushout
-/
import .quotient ..types.arrow_2
universes u v w
hott_theory
namespace hott
open hott.quotient hott.sum sum hott.equiv is_trunc pointed hott.sigma
namespace pushout
section
parameters {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β BL) (g : TL β TR)
private abbreviation A := BL β TR
inductive pushout_rel : A β A β Type (max u v w)
| Rmk : Ξ (x : TL), pushout_rel (inl (f x)) (inr (g x))
open pushout_rel
private abbreviation R := pushout_rel
@[hott] def pushout : Type _ := quotient R
parameters {f g}
@[hott] def inl (x : BL) : pushout :=
class_of R (inl x)
@[hott] def inr (x : TR) : pushout :=
class_of R (inr x)
@[hott] def glue (x : TL) : inl (f x) = inr (g x) :=
eq_of_rel pushout_rel (Rmk x)
@[hott, elab_as_eliminator, induction] protected def rec {P : pushout β Type _} (Pinl : Ξ (x : BL), P (inl x))
(Pinr : Ξ (x : TR), P (inr x)) (Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x))
(y : pushout) : P y :=
begin
hinduction y,
{ cases a,
apply Pinl,
apply Pinr},
{ cases H, apply Pglue}
end
@[hott, reducible] protected def rec_on {P : pushout β Type _} (y : pushout)
(Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x))
(Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y :=
rec Pinl Pinr Pglue y
@[hott] theorem rec_glue {P : pushout β Type _} (Pinl : Ξ (x : BL), P (inl x))
(Pinr : Ξ (x : TR), P (inr x)) (Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x))
(x : TL) : apd (rec Pinl Pinr Pglue) (glue x) = Pglue x :=
rec_eq_of_rel _ _ _
@[hott, induction, priority 1100] protected def elim {P : Type _} (Pinl : BL β P)
(Pinr : TR β P) (Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P :=
begin hinduction y, exact Pinl x, exact Pinr x, exact pathover_of_eq _ (Pglue x) end
@[hott] theorem elim_glue {P : Type _} (Pinl : BL β P) (Pinr : TR β P)
(Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) (x : TL)
: ap (elim Pinl Pinr Pglue) (glue x) = Pglue x :=
begin
apply eq_of_fn_eq_fn_inv ((pathover_constant (glue x)) _ _),
refine (apd_eq_pathover_of_eq_ap _ _)β»ΒΉ β¬ rec_eq_of_rel _ _ _
end
@[hott] protected def elim_type (Pinl : BL β Type _) (Pinr : TR β Type _)
(Pglue : Ξ (x : TL), Pinl (f x) β Pinr (g x)) : pushout β Type _ :=
quotient.elim_type (Ξ»z, sum.rec Pinl Pinr z)
begin intros v v' r, induction r, apply Pglue end
@[hott] theorem elim_type_glue (Pinl : BL β Type _) (Pinr : TR β Type _)
(Pglue : Ξ (x : TL), Pinl (f x) β Pinr (g x)) (x : TL)
: transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x :=
elim_type_eq_of_rel_fn _ _ _
@[hott] theorem elim_type_glue_inv (Pinl : BL β Type _) (Pinr : TR β Type _)
(Pglue : Ξ (x : TL), Pinl (f x) β Pinr (g x)) (x : TL)
: transport (elim_type Pinl Pinr Pglue) (glue x)β»ΒΉ = to_inv (Pglue x) :=
elim_type_eq_of_rel_inv _ _ _
@[hott] protected def rec_prop {P : pushout β Type _} [H : Ξ x, is_prop (P x)]
(Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x)) (y : pushout) :=
rec Pinl Pinr (Ξ»x, is_prop.elimo _ _ _) y
@[hott] protected def elim_prop {P : Type _} [H : is_prop P] (Pinl : BL β P) (Pinr : TR β P)
(y : pushout) : P :=
elim Pinl Pinr (Ξ»a, is_prop.elim _ _) y
end
variables {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β BL) (g : TL β TR)
@[hott] protected theorem elim_inl {P : Type _} (Pinl : BL β P) (Pinr : TR β P)
(Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) {b b' : BL} (p : b = b')
: ap (pushout.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p :=
(ap_compose _ _ _)β»ΒΉ
@[hott] protected theorem elim_inr {P : Type _} (Pinl : BL β P) (Pinr : TR β P)
(Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) {b b' : TR} (p : b = b')
: ap (pushout.elim Pinl Pinr Pglue) (ap inr p) = ap Pinr p :=
(ap_compose _ _ _)β»ΒΉ
/- The non-dependent universal property -/
@[hott] def pushout_arrow_equiv (C : Type _)
: (pushout f g β C) β (Ξ£(i : BL β C) (j : TR β C), Ξ c, i (f c) = j (g c)) :=
begin
fapply equiv.MK,
{ intro f, exact β¨Ξ»x, f (inl x), Ξ»x, f (inr x), Ξ»x, ap f (glue x)β©},
{ intros v x, induction v with i w, induction w with j p, hinduction x,
exact (i a), exact (j a), exact (p x)},
{ intro v, induction v with i w, induction w with j p, dsimp,
apply ap, apply ap, apply eq_of_homotopy, intro x, apply elim_glue},
{ intro f, apply eq_of_homotopy, intro x, hinduction x, refl, refl, dsimp,
apply eq_pathover, apply hdeg_square, apply elim_glue},
end
/- glue squares -/
@[hott] protected def glue_square {x x' : TL} (p : x = x')
: square (glue x) (glue x') (ap inl (ap f p)) (ap inr (ap g p)) :=
by cases p; apply vrefl
end pushout
--open function --
namespace pushout
/- The flattening lemma -/
section
parameters {TL : Type _} {BL : Type _} {TR : Type _} (f : TL β BL) (g : TL β TR)
(Pinl : BL β Type u) (Pinr : TR β Type u)
(Pglue : Ξ (x : TL), Pinl (f x) β Pinr (g x))
include Pglue
private abbreviation A := BL β TR
private abbreviation R : A β A β Type _ := pushout_rel f g
private abbreviation P := pushout.elim_type Pinl Pinr Pglue
private abbreviation F : sigma (Pinl β f) β sigma Pinl :=
Ξ»z, β¨ f z.1 , z.2 β©
private abbreviation G : sigma (Pinl β f) β sigma Pinr :=
Ξ»z, β¨ g z.1 , Pglue z.1 z.2 β©
@[hott] protected def flattening : sigma P β pushout F G :=
begin
refine quotient.flattening.flattening_lemma _ _ _ β¬e _,
fapply equiv.MK,
{ intro q, hinduction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ exact inl β¨x, pβ© },
{ exact inr β¨x, pβ© } },
{ induction fr with a a' r p, induction r with x,
exact glue (β¨x, pβ© : sigma _) } },
{ intro q, hinduction q with xp xp xp,
{ exact class_of _ β¨sum.inl xp.1, xp.2β© },
{ exact class_of _ β¨sum.inr xp.1, xp.2β© },
{ apply eq_of_rel, dsimp,
exact flattening.flattening_rel.mk _ (pushout_rel.Rmk _ _ _) _ } },
{ intro q, hinduction q with xp xp xp; induction xp with x p,
{ apply ap inl, reflexivity },
{ apply ap inr, reflexivity },
{ apply eq_pathover, dsimp,
rwr [ap_id, βap_compose' (quotient.elim _ _)],
rwr elim_glue, rwr elim_eq_of_rel, dsimp, apply hrefl } },
{ intro q, hinduction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ reflexivity },
{ reflexivity } },
{ induction fr with a a' r p, induction r with x,
apply eq_pathover,
rwr [ap_id, βap_compose' (pushout.elim _ _ _)],
rwr elim_eq_of_rel, rwr elim_glue, apply hrefl } }
end
end
-- Commutativity of pushouts
section
variables {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β BL) (g : TL β TR)
@[hott] protected def transpose : pushout f g β pushout g f :=
begin
intro x, hinduction x, apply inr a, apply inl a, exact (glue _)β»ΒΉ
end
--TODO prove without krwr?
@[hott] protected def transpose_involutive (x : pushout f g) :
pushout.transpose g f (pushout.transpose f g x) = x :=
begin
hinduction x, apply idp, apply idp,
apply eq_pathover, refine _ β¬hp (ap_id _)β»ΒΉα΅,
refine ap_compose (pushout.transpose _ _) _ _ β¬ph _,
apply hdeg_square,
dsimp [pushout.transpose],
rwr [elim_glue, ap_inv, elim_glue, hott.eq.inv_inv],
end
@[hott] protected def symm : pushout f g β pushout g f :=
begin
fapply equiv.MK, apply pushout.transpose, apply pushout.transpose,
intro x; apply pushout.transpose_involutive,
intro x; apply pushout.transpose_involutive
end
end
-- Functoriality of pushouts
section
section lemmas
variables {X : Type _} {xβ xβ xβ xβ : X}
(p : xβ = xβ) (q : xβ = xβ) (r : xβ = xβ)
@[hott] private def is_equiv_functor_lemmaβ
: (r β¬ ((p β¬ q β¬ r)β»ΒΉ β¬ p)) = qβ»ΒΉ :=
by cases p; cases r; cases q; reflexivity
@[hott] private def is_equiv_functor_lemmaβ
: (p β¬ q β¬ r)β»ΒΉ β¬ (p β¬ q) = rβ»ΒΉ :=
by cases p; cases r; cases q; reflexivity
end lemmas
variables {TL : Type _} {BL : Type _} {TR : Type _} {f : TL β BL} {g : TL β TR}
{TL' : Type _} {BL' : Type _} {TR' : Type _} {f' : TL' β BL'} {g' : TL' β TR'}
(tl : TL β TL') (bl : BL β BL') (tr : TR β TR')
(fh : bl β f ~ f' β tl) (gh : tr β g ~ g' β tl)
include fh gh
def pushout_rel_functor {{x x' : BL β TR}}
(r : pushout_rel f g x x') : pushout_rel f' g' (x.functor bl tr) (x'.functor bl tr) :=
begin
induction r,
apply transport11 (pushout_rel f' g') (ap sum.inl (fh r)β»ΒΉα΅) (ap sum.inr (gh r)β»ΒΉα΅),
constructor
end
@[hott] protected def functor : pushout f g β pushout f' g' :=
begin
intro x, hinduction x with a b z,
{ exact inl (bl a) },
{ exact inr (tr b) },
{ exact (ap inl (fh z)) β¬ glue (tl z) β¬ (ap inr (gh z)β»ΒΉ) }
end
@[hott] protected def pushout_functor_homotopy_quotient_functor :
pushout.functor tl bl tr fh gh ~
quotient.functor (sum.functor bl tr) (pushout_rel_functor tl bl tr fh gh) :=
begin
intro x, hinduction x with a b z,
{ refl },
{ refl },
{ dsimp, apply eq_pathover, apply hdeg_square,
refine elim_glue _ _ _ _ β¬ _ β¬ (functor_eq_of_rel _ _ _)β»ΒΉα΅,
dsimp [glue, pushout_rel_functor], rwr [ap_inv],
symmetry, apply eq_top_of_square,
refine ap_compose (class_of _) _ _ β¬ph _ β¬hp (ap_compose (class_of _) _ _)β»ΒΉα΅,
apply natural_square2,
rwr [βtransport11_con, ap_inv, con.left_inv, ap_inv, con.left_inv] }
end
@[hott] protected def ap_functor_inl {x x' : BL} (p : x = x')
: ap (pushout.functor tl bl tr fh gh) (ap inl p) = ap inl (ap bl p) :=
by cases p; reflexivity
@[hott] protected def ap_functor_inr {x x' : TR} (p : x = x')
: ap (pushout.functor tl bl tr fh gh) (ap inr p) = ap inr (ap tr p) :=
by cases p; reflexivity
variables [ietl : is_equiv tl] [iebl : is_equiv bl] [ietr : is_equiv tr]
include ietl iebl ietr
open equiv is_equiv arrow
@[hott, instance] protected def is_equiv_functor
: is_equiv (pushout.functor tl bl tr fh gh) :=
sorry
end
/- version giving the equivalence -/
section
variables {TL : Type _} {BL : Type _} {TR : Type _} {f : TL β BL} {g : TL β TR}
{TL' : Type _} {BL' : Type _} {TR' : Type _} {f' : TL' β BL'} {g' : TL' β TR'}
(tl : TL β TL') (bl : BL β BL') (tr : TR β TR')
(fh : bl β f ~ f' β tl) (gh : tr β g ~ g' β tl)
include fh gh
@[hott] protected def equiv : pushout f g β pushout f' g' :=
equiv.mk (pushout.functor tl bl tr fh gh) (by apply_instance)
end
@[hott, instance] def pointed_pushout {TL BL TR : Type _} [HTL : pointed TL]
[HBL : pointed BL] [HTR : pointed TR] (f : TL β BL) (g : TL β TR) : pointed (pushout f g) :=
pointed.mk (inl (point _))
end pushout open pushout
@[hott] def ppushout {TL BL TR : Type*} (f : TL β* BL) (g : TL β* TR) : Type* :=
pointed.mk' (pushout f g)
namespace pushout
section
parameters {TL : Type*} {BL : Type*} {TR : Type*} (f : TL β* BL) (g : TL β* TR)
parameters {f g}
@[hott] def pinl : BL β* ppushout f g :=
pmap.mk inl idp
@[hott] def pinr : TR β* ppushout f g :=
pmap.mk inr ((ap inr (respect_pt g))β»ΒΉ β¬ (glue _)β»ΒΉ β¬ (ap inl (respect_pt f)))
@[hott] def pglue (x : TL) : pinl (f x) = pinr (g x) := -- TODO do we need this?
glue _
end
section
variables {TL : Type*} {BL : Type*} {TR : Type*} (f : TL β* BL) (g : TL β* TR)
@[hott] protected def psymm : ppushout f g β* ppushout g f :=
begin
fapply pequiv_of_equiv,
{ apply pushout.symm },
{ exact ap inr (respect_pt f)β»ΒΉ β¬ (glue _)β»ΒΉ β¬ ap inl (respect_pt g) }
end
end
end pushout
end hott |
90c2b994e6d0aa9f061e0f08128cb43204448c77 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/measure_theory/giry_monad.lean | 5bc5511f75bebee81632cda381cbd71a26cd3a8f | [
"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 | 6,940 | lean | /-
Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes HΓΆlzl
Giry monad: `measure` is a monad in the category of `measurable_space` and `measurable` functions.
-/
import measure_theory.integration
noncomputable theory
local attribute [instance, priority 0] classical.prop_decidable
open classical set lattice filter
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} {Ξ΅ : Type*}
namespace measure_theory
namespace measure
variables [measurable_space Ξ±] [measurable_space Ξ²]
/-- Measurability structure on `measure`: Measures are measurable w.r.t. all projections -/
instance : measurable_space (measure Ξ±) :=
⨠(s : set α) (hs : is_measurable s), (borel ennreal).comap (λμ, μ s)
lemma measurable_coe {s : set α} (hs : is_measurable s) : measurable (λμ : measure α, μ s) :=
measurable_space.comap_le_iff_le_map.1 $ le_supr_of_le s $ le_supr_of_le hs $ le_refl _
lemma measurable_of_measurable_coe (f : Ξ² β measure Ξ±)
(h : β(s : set Ξ±) (hs : is_measurable s), measurable (Ξ»b, f b s)) :
measurable f :=
supr_le $ assume s, supr_le $ assume hs, measurable_space.comap_le_iff_le_map.2 $
by rw [measurable_space.map_comp]; exact h s hs
lemma measurable_map (f : Ξ± β Ξ²) (hf : measurable f) :
measurable (λμ : measure α, μ.map f) :=
measurable_of_measurable_coe _ $ assume s hs,
suffices measurable (Ξ» (ΞΌ : measure Ξ±), ΞΌ (f β»ΒΉ' s)),
by simpa [map_apply, hs, hf],
measurable_coe (hf.preimage hs)
lemma measurable_dirac :
measurable (measure.dirac : Ξ± β measure Ξ±) :=
measurable_of_measurable_coe _ $ assume s hs,
begin
simp [hs, lattice.supr_eq_if],
exact measurable_const.if hs measurable_const
end
lemma measurable_integral (f : Ξ± β ennreal) (hf : measurable f) :
measurable (λμ : measure α, μ.integral f) :=
suffices measurable (λμ : measure α,
(β¨n:β, @simple_func.integral Ξ± { ΞΌ := ΞΌ } (simple_func.eapprox f n)) : _ β ennreal),
begin
convert this,
funext ΞΌ,
exact @lintegral_eq_supr_eapprox_integral Ξ± {ΞΌ := ΞΌ} f hf
end,
measurable.supr $ assume n,
begin
dunfold simple_func.integral,
refine measurable_finset_sum (simple_func.eapprox f n).range _,
assume i,
refine ennreal.measurable_mul measurable_const _,
exact measurable_coe ((simple_func.eapprox f n).preimage_measurable _)
end
/-- Monadic join on `measure` in the category of measurable spaces and measurable
functions. -/
def join (m : measure (measure Ξ±)) : measure Ξ± :=
measure.of_measurable
(λs hs, m.integral (λμ, μ s))
(by simp [integral])
begin
assume f hf h,
simp [measure_Union h hf],
apply lintegral_tsum,
assume i, exact measurable_coe (hf i)
end
@[simp] lemma join_apply {m : measure (measure Ξ±)} :
β{s : set Ξ±}, is_measurable s β join m s = m.integral (λμ, ΞΌ s) :=
measure.of_measurable_apply
lemma measurable_join : measurable (join : measure (measure Ξ±) β measure Ξ±) :=
measurable_of_measurable_coe _ $ assume s hs,
by simp [hs]; exact measurable_integral _ (measurable_coe hs)
lemma integral_join {m : measure (measure Ξ±)} {f : Ξ± β ennreal} (hf : measurable f) :
integral (join m) f = integral m (λμ, integral μ f) :=
begin
transitivity,
apply lintegral_eq_supr_eapprox_integral,
{ exact hf },
have : βn x,
@volume Ξ± { ΞΌ := join m} (β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {x}) =
m.integral (λμ, @volume Ξ± { ΞΌ := ΞΌ } ((β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {x}))) :=
assume n x, join_apply (simple_func.measurable_sn _ _),
conv {
to_lhs,
congr,
funext,
rw [simple_func.integral] },
simp [this],
transitivity,
have : β(s : β β finset ennreal) (f : β β ennreal β measure Ξ± β ennreal)
(hf : βn r, measurable (f n r)) (hm : monotone (Ξ»n ΞΌ, (s n).sum (Ξ» r, r * f n r ΞΌ))),
(β¨n:β, (s n).sum (Ξ»r, r * integral m (f n r))) =
integral m (λμ, β¨n:β, (s n).sum (Ξ»r, r * f n r ΞΌ)),
{ assume s f hf hm,
symmetry,
transitivity,
apply lintegral_supr,
{ exact assume n,
measurable_finset_sum _ (assume r, ennreal.measurable_mul measurable_const (hf _ _)) },
{ exact hm },
congr, funext n,
transitivity,
apply lintegral_finset_sum,
{ exact assume r, ennreal.measurable_mul measurable_const (hf _ _) },
congr, funext r,
apply lintegral_const_mul,
exact hf _ _ },
specialize this (Ξ»n, simple_func.range (simple_func.eapprox f n)),
specialize this
(Ξ»n r ΞΌ, @volume Ξ± { ΞΌ := ΞΌ } (β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {r})),
refine this _ _; clear this,
{ assume n r,
apply measurable_coe,
exact simple_func.measurable_sn _ _ },
{ change monotone (Ξ»n ΞΌ, @simple_func.integral Ξ± {ΞΌ := ΞΌ} (simple_func.eapprox f n)),
assume n m h ΞΌ,
apply simple_func.integral_le_integral,
apply simple_func.monotone_eapprox,
assumption },
congr, funext ΞΌ,
symmetry,
apply lintegral_eq_supr_eapprox_integral,
exact hf
end
/-- Monadic bind on `measure`, only works in the category of measurable spaces and measurable
functions. When the function `f` is not measurable the result is not well defined. -/
def bind (m : measure Ξ±) (f : Ξ± β measure Ξ²) : measure Ξ² := join (map f m)
@[simp] lemma bind_apply {m : measure Ξ±} {f : Ξ± β measure Ξ²} {s : set Ξ²}
(hs : is_measurable s) (hf : measurable f) : bind m f s = m.integral (Ξ»a, f a s) :=
by rw [bind, join_apply hs, integral_map (measurable_coe hs) hf]
lemma measurable_bind' {g : Ξ± β measure Ξ²} (hg : measurable g) : measurable (Ξ»m, bind m g) :=
measurable_join.comp (measurable_map _ hg)
lemma integral_bind {m : measure Ξ±} {g : Ξ± β measure Ξ²} {f : Ξ² β ennreal}
(hg : measurable g) (hf : measurable f) :
integral (bind m g) f = integral m (Ξ»a, integral (g a) f) :=
begin
transitivity,
exact integral_join hf,
exact integral_map (measurable_integral _ hf) hg
end
lemma bind_bind {Ξ³} [measurable_space Ξ³] {m : measure Ξ±} {f : Ξ± β measure Ξ²} {g : Ξ² β measure Ξ³}
(hf : measurable f) (hg : measurable g) :
bind (bind m f) g = bind m (Ξ»a, bind (f a) g) :=
measure.ext $ assume s hs,
begin
rw [bind_apply hs hg, bind_apply hs ((measurable_bind' hg).comp hf), integral_bind hf],
{ congr, funext a,
exact (bind_apply hs hg).symm },
exact (measurable_coe hs).comp hg
end
lemma bind_dirac {f : Ξ± β measure Ξ²} (hf : measurable f) (a : Ξ±) : bind (dirac a) f = f a :=
measure.ext $ assume s hs, by rw [bind_apply hs hf, integral_dirac a ((measurable_coe hs).comp hf)]
lemma dirac_bind {m : measure Ξ±} : bind m dirac = m :=
measure.ext $ assume s hs,
begin
rw [bind_apply hs measurable_dirac],
simp [dirac_apply _ hs],
transitivity,
apply lintegral_supr_const,
assumption,
exact one_mul _
end
end measure
end measure_theory
|
4e4e654c64a303896d4650dddc7bb18022061578 | 2fbe653e4bc441efde5e5d250566e65538709888 | /src/tactic/finish.lean | ed777ac421c64151fef1ee08d2333c44f6fb29ea | [
"Apache-2.0"
] | permissive | aceg00/mathlib | 5e15e79a8af87ff7eb8c17e2629c442ef24e746b | 8786ea6d6d46d6969ac9a869eb818bf100802882 | refs/heads/master | 1,649,202,698,930 | 1,580,924,783,000 | 1,580,924,783,000 | 149,197,272 | 0 | 0 | Apache-2.0 | 1,537,224,208,000 | 1,537,224,207,000 | null | UTF-8 | Lean | false | false | 22,207 | lean | /-
Copyright (c) 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Jesse Michael Han
-/
import logic.basic tactic.core
/-!
# The `finish` family of tactics
These tactics do straightforward things: they call the simplifier, split conjunctive assumptions,
eliminate existential quantifiers on the left, and look for contradictions. They rely on ematching
and congruence closure to try to finish off a goal at the end.
The procedures *do* split on disjunctions and recreate the smt state for each terminal call, so
they are only meant to be used on small, straightforward problems.
## Main definitions
We provide the following tactics:
`finish` -- solves the goal or fails
`clarify` -- makes as much progress as possible while not leaving more than one goal
`safe` -- splits freely, finishes off whatever subgoals it can, and leaves the rest
All accept an optional list of simplifier rules, typically definitions that should be expanded.
(The equations and identities should not refer to the local context.)
## Implementation notes
The variants `ifinish`, `iclarify`, and `isafe` try to restrict to intuitionistic logic. But the
`done` tactic leaks classical logic:
```
example {P : Prop} : ¬¬P β P :=
by using_smt (do smt_tactic.intros, smt_tactic.close)
```
They also do not work well with the current heuristic instantiation method used by `ematch`.
So they are left here mainly for reference.
-/
declare_trace auto.done
declare_trace auto.finish
namespace tactic
/- call (assert n t) with a fresh name n. -/
meta def assert_fresh (t : expr) : tactic expr :=
do n β get_unused_name `h none,
assert n t
/- call (assertv n t v) with a fresh name n. -/
meta def assertv_fresh (t : expr) (v : expr) : tactic expr :=
do h β get_unused_name `h none,
assertv h t v
namespace interactive
meta def revert_all := tactic.revert_all
end interactive
end tactic
open tactic expr
namespace auto
/- Utilities -/
meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible
-- stolen from interactive.lean
meta def add_simps : simp_lemmas β list name β tactic simp_lemmas
| s [] := return s
| s (n::ns) := do s' β s.add_simp n, add_simps s' ns
/-
Configuration information for the auto tactics.
-/
@[derive decidable_eq, derive inhabited]
structure auto_config : Type :=
(use_simp := tt) -- call the simplifier
(classical := tt) -- use classical logic
(max_ematch_rounds := 20) -- for the "done" tactic
/-
Preprocess goal.
We want to move everything to the left of the sequent arrow. For intuitionistic logic,
we replace the goal p with β f, (p β f) β f and introduce.
-/
theorem by_contradiction_trick (p : Prop) (h : β f : Prop, (p β f) β f) : p :=
h p id
meta def preprocess_goal (cfg : auto_config) : tactic unit :=
do repeat (intro1 >> skip),
tgt β target >>= whnf_reducible,
if (Β¬ (is_false tgt)) then
if cfg.classical then
(mk_mapp ``classical.by_contradiction [some tgt]) >>= apply >> intro1 >> skip
else
(mk_mapp ``decidable.by_contradiction [some tgt, none] >>= apply >> intro1 >> skip) <|>
applyc ``by_contradiction_trick >> intro1 >> intro1 >> skip
else
skip
/-
Normalize hypotheses. Bring conjunctions to the outside (for splitting),
bring universal quantifiers to the outside (for ematching). The classical normalizer
eliminates a β b in favor of Β¬ a β¨ b.
For efficiency, we push negations inwards from the top down. (For example, consider
simplifying Β¬ Β¬ (p β¨ q).)
-/
section
universe u
variable {Ξ± : Type u}
variables (p q : Prop)
variable (s : Ξ± β Prop)
local attribute [instance, priority 10] classical.prop_decidable
theorem not_not_eq : (Β¬ Β¬ p) = p := propext not_not
theorem not_and_eq : (Β¬ (p β§ q)) = (Β¬ p β¨ Β¬ q) := propext not_and_distrib
theorem not_or_eq : (Β¬ (p β¨ q)) = (Β¬ p β§ Β¬ q) := propext not_or_distrib
theorem not_forall_eq : (Β¬ β x, s x) = (β x, Β¬ s x) := propext not_forall
theorem not_exists_eq : (Β¬ β x, s x) = (β x, Β¬ s x) := propext not_exists
theorem not_implies_eq : (Β¬ (p β q)) = (p β§ Β¬ q) := propext not_imp
theorem classical.implies_iff_not_or : (p β q) β (Β¬ p β¨ q) := imp_iff_not_or
end
def common_normalize_lemma_names : list name :=
[``bex_def, ``forall_and_distrib, ``exists_imp_distrib, ``or.assoc, ``or.comm, ``or.left_comm,
``and.assoc, ``and.comm, ``and.left_comm]
def classical_normalize_lemma_names : list name :=
common_normalize_lemma_names ++ [``classical.implies_iff_not_or]
-- optionally returns an equivalent expression and proof of equivalence
private meta def transform_negation_step (cfg : auto_config) (e : expr) :
tactic (option (expr Γ expr)) :=
do e β whnf_reducible e,
match e with
| `(Β¬ %%ne) :=
(do ne β whnf_reducible ne,
match ne with
| `(Β¬ %%a) := if Β¬ cfg.classical then return none
else do pr β mk_app ``not_not_eq [a],
return (some (a, pr))
| `(%%a β§ %%b) := do pr β mk_app ``not_and_eq [a, b],
return (some (`(Β¬ %%a β¨ Β¬ %%b), pr))
| `(%%a β¨ %%b) := do pr β mk_app ``not_or_eq [a, b],
return (some (`(Β¬ %%a β§ Β¬ %%b), pr))
| `(Exists %%p) := do pr β mk_app ``not_exists_eq [p],
`(%%_ = %%e') β infer_type pr,
return (some (e', pr))
| (pi n bi d p) := if Β¬ cfg.classical then return none
else if p.has_var then do
pr β mk_app ``not_forall_eq [lam n bi d (expr.abstract_local p n)],
`(%%_ = %%e') β infer_type pr,
return (some (e', pr))
else do
pr β mk_app ``not_implies_eq [d, p],
`(%%_ = %%e') β infer_type pr,
return (some (e', pr))
| _ := return none
end)
| _ := return none
end
-- given an expr 'e', returns a new expression and a proof of equality
private meta def transform_negation (cfg : auto_config) : expr β tactic (option (expr Γ expr)) :=
Ξ» e, do
opr β transform_negation_step cfg e,
match opr with
| (some (e', pr)) := do
opr' β transform_negation e',
match opr' with
| none := return (some (e', pr))
| (some (e'', pr')) := do pr'' β mk_eq_trans pr pr',
return (some (e'', pr''))
end
| none := return none
end
meta def normalize_negations (cfg : auto_config) (h : expr) : tactic unit :=
do t β infer_type h,
(_, e, pr) β simplify_top_down ()
(Ξ» _, Ξ» e, do
oepr β transform_negation cfg e,
match oepr with
| (some (e', pr)) := return ((), e', pr)
| none := do pr β mk_eq_refl e, return ((), e, pr)
end)
t,
replace_hyp h e pr,
skip
meta def normalize_hyp (cfg : auto_config) (simps : simp_lemmas) (h : expr) : tactic unit :=
(do h β simp_hyp simps [] h, try (normalize_negations cfg h)) <|>
try (normalize_negations cfg h)
meta def normalize_hyps (cfg : auto_config) : tactic unit :=
do simps β if cfg.classical then
add_simps simp_lemmas.mk classical_normalize_lemma_names
else
add_simps simp_lemmas.mk common_normalize_lemma_names,
local_context >>= monad.mapm' (normalize_hyp cfg simps)
/-
Eliminate existential quantifiers.
-/
-- eliminate an existential quantifier if there is one
meta def eelim : tactic unit :=
do ctx β local_context,
first $ ctx.map $ Ξ» h,
do t β infer_type h >>= whnf_reducible,
guard (is_app_of t ``Exists),
tgt β target,
to_expr ``(@exists.elim _ _ %%tgt %%h) >>= apply,
intros,
clear h
-- eliminate all existential quantifiers, fails if there aren't any
meta def eelims : tactic unit := eelim >> repeat eelim
/-
Substitute if there is a hypothesis x = t or t = x.
-/
-- carries out a subst if there is one, fails otherwise
meta def do_subst : tactic unit :=
do ctx β local_context,
first $ ctx.map $ Ξ» h,
do t β infer_type h >>= whnf_reducible,
match t with
| `(%%a = %%b) := subst h
| _ := failed
end
meta def do_substs : tactic unit := do_subst >> repeat do_subst
/-
Split all conjunctions.
-/
-- Assumes pr is a proof of t. Adds the consequences of t to the context
-- and returns tt if anything nontrivial has been added.
meta def add_conjuncts : expr β expr β tactic bool :=
Ξ» pr t,
let assert_consequences := Ξ» e t, mcond (add_conjuncts e t) skip (assertv_fresh t e >> skip) in
do t' β whnf_reducible t,
match t' with
| `(%%a β§ %%b) :=
do eβ β mk_app ``and.left [pr],
assert_consequences eβ a,
eβ β mk_app ``and.right [pr],
assert_consequences eβ b,
return tt
| `(true) :=
do return tt
| _ := return ff
end
-- return tt if any progress is made
meta def split_hyp (h : expr) : tactic bool :=
do t β infer_type h,
mcond (add_conjuncts h t) (clear h >> return tt) (return ff)
-- return tt if any progress is made
meta def split_hyps_aux : list expr β tactic bool
| [] := return ff
| (h :: hs) := do bβ β split_hyp h,
bβ β split_hyps_aux hs,
return (bβ || bβ)
-- fail if no progress is made
meta def split_hyps : tactic unit := local_context >>= split_hyps_aux >>= guardb
/-
Eagerly apply all the preprocessing rules.
-/
meta def preprocess_hyps (cfg : auto_config) : tactic unit :=
do repeat (intro1 >> skip),
preprocess_goal cfg,
normalize_hyps cfg,
repeat (do_substs <|> split_hyps <|> eelim /-<|> self_simplify_hyps-/)
/-
The terminal tactic, used to try to finish off goals:
- Call the contradiction tactic.
- Open an SMT state, and use ematching and congruence closure, with all the universal
statements in the context.
TODO(Jeremy): allow users to specify attribute for ematching lemmas?
-/
meta def mk_hinst_lemmas : list expr β smt_tactic hinst_lemmas
| [] := -- return hinst_lemmas.mk
do get_hinst_lemmas_for_attr `ematch
| (h :: hs) := do his β mk_hinst_lemmas hs,
t β infer_type h,
match t with
| (pi _ _ _ _) :=
do t' β infer_type t,
if t' = `(Prop) then
(do new_lemma β hinst_lemma.mk h,
return (hinst_lemmas.add his new_lemma)) <|> return his
else return his
| _ := return his
end
private meta def report_invalid_em_lemma {Ξ± : Type} (n : name) : smt_tactic Ξ± :=
fail format!"invalid ematch lemma '{n}'"
private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name)
(hs : hinst_lemmas) (ref : pexpr) : smt_tactic hinst_lemmas :=
do p β resolve_name n,
match p 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β β smt_tactic.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) (hs : hinst_lemmas)
: pexpr β smt_tactic hinst_lemmas
| p@(expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs p
| p@(expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs p
| p := do new_e β to_expr p, h β hinst_lemma.mk_core md new_e lhs_lemma,
return $ hs.add h
private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool)
(ps : list pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas :=
list.mfoldl (add_hinst_lemma_from_pexpr md lhs_lemma) hs ps
/--
`done` first attempts to close the goal using `contradiction`. If this fails, it creates an
SMT state and will repeatedly use `ematch` (using `ematch` lemmas in the environment,
universally quantified assumptions, and the supplied lemmas `ps`) and congruence closure.
-/
meta def done (ps : list pexpr) (cfg : auto_config := {}) : tactic unit :=
do when_tracing `auto.done (trace "entering done" >> trace_state),
contradiction <|>
(solve1 $
(do revert_all,
using_smt
(do smt_tactic.intros,
ctx β local_context,
hs β mk_hinst_lemmas ctx,
hs' β add_hinst_lemmas_from_pexprs reducible ff ps hs,
smt_tactic.iterate_at_most cfg.max_ematch_rounds
(smt_tactic.ematch_using hs' >> smt_tactic.try smt_tactic.close))))
/-
Tactics that perform case splits.
-/
@[derive decidable_eq, derive inhabited]
inductive case_option
| force -- fail unless all goals are solved
| at_most_one -- leave at most one goal
| accept -- leave as many goals as necessary
private meta def case_cont (s : case_option) (cont : case_option β tactic unit) : tactic unit :=
do match s with
| case_option.force := cont case_option.force >> cont case_option.force
| case_option.at_most_one :=
-- if the first one succeeds, commit to it, and try the second
(mcond (cont case_option.force >> return tt) (cont case_option.at_most_one) skip) <|>
-- otherwise, try the second
(swap >> cont case_option.force >> cont case_option.at_most_one)
| case_option.accept := focus [cont case_option.accept, cont case_option.accept]
end
-- three possible outcomes:
-- finds something to case, the continuations succeed ==> returns tt
-- finds something to case, the continutations fail ==> fails
-- doesn't find anything to case ==> returns ff
meta def case_hyp (h : expr) (s : case_option) (cont : case_option β tactic unit) : tactic bool :=
do t β infer_type h,
match t with
| `(%%a β¨ %%b) := cases h >> case_cont s cont >> return tt
| _ := return ff
end
meta def case_some_hyp_aux (s : case_option) (cont : case_option β tactic unit) :
list expr β tactic bool
| [] := return ff
| (h::hs) := mcond (case_hyp h s cont) (return tt) (case_some_hyp_aux hs)
meta def case_some_hyp (s : case_option) (cont : case_option β tactic unit) : tactic bool :=
local_context >>= case_some_hyp_aux s cont
/-
The main tactics.
-/
/--
`safe_core s ps cfg opt` negates the goal, normalizes hypotheses
(by splitting conjunctions, eliminating existentials, pushing negations inwards,
and calling `simp` with the supplied lemmas `s`), and then tries `contradiction`.
If this fails, it will create an SMT state and repeatedly use `ematch`
(using `ematch` lemmas in the environment, universally quantified assumptions,
and the supplied lemmas `ps`) and congruence closure.
`safe_core` is complete for propositional logic. Depending on the form of `opt`
it will:
- (if `opt` is `case_option.force`) fail if it does not close the goal,
- (if `opt` is `case_option.at_most_one`) fail if it produces more than one goal, and
- (if `opt` is `case_option.accept`) ignore the number of goals it produces.
-/
meta def safe_core (s : simp_lemmas Γ list name) (ps : list pexpr) (cfg : auto_config) : case_option β tactic unit :=
Ξ» co, focus1 $
do when_tracing `auto.finish (trace "entering safe_core" >> trace_state),
if cfg.use_simp then do
when_tracing `auto.finish (trace "simplifying hypotheses"),
simp_all s.1 s.2 { fail_if_unchanged := ff },
when_tracing `auto.finish (trace "result:" >> trace_state)
else skip,
tactic.done <|>
do when_tracing `auto.finish (trace "preprocessing hypotheses"),
preprocess_hyps cfg,
when_tracing `auto.finish (trace "result:" >> trace_state),
done ps cfg <|>
(mcond (case_some_hyp co safe_core)
skip
(match co with
| case_option.force := done ps cfg
| case_option.at_most_one := try (done ps cfg)
| case_option.accept := try (done ps cfg)
end))
/--
`clarify` is `safe_core`, but with the `(opt : case_option)`
parameter fixed at `case_option.at_most_one`.
-/
meta def clarify (s : simp_lemmas Γ list name) (ps : list pexpr)
(cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.at_most_one
/--
`safe` is `safe_core`, but with the `(opt : case_option)`
parameter fixed at `case_option.accept`.
-/
meta def safe (s : simp_lemmas Γ list name) (ps : list pexpr)
(cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.accept
/--
`finish` is `safe_core`, but with the `(opt : case_option)`
parameter fixed at `case_option.force`.
-/
meta def finish (s : simp_lemmas Γ list name) (ps : list pexpr)
(cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.force
/--
`iclarify` is like `clarify`, but in some places restricts to intuitionistic logic.
Classical logic still leaks, so this tactic is deprecated.
-/
meta def iclarify (s : simp_lemmas Γ list name) (ps : list pexpr)
(cfg : auto_config := {}) : tactic unit := clarify s ps {classical := ff, ..cfg}
/--
`isafe` is like `safe`, but in some places restricts to intuitionistic logic.
Classical logic still leaks, so this tactic is deprecated.
-/
meta def isafe (s : simp_lemmas Γ list name) (ps : list pexpr)
(cfg : auto_config := {}) : tactic unit := safe s ps {classical := ff, ..cfg}
/--
`ifinish` is like `finish`, but in some places restricts to intuitionistic logic.
Classical logic still leaks, so this tactic is deprecated.
-/
meta def ifinish (s : simp_lemmas Γ list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit :=
finish s ps {classical := ff, ..cfg}
end auto
/- interactive versions -/
open auto
namespace tactic
namespace interactive
open lean lean.parser interactive interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many
/--
`clarify [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses
(by splitting conjunctions, eliminating existentials, pushing negations inwards,
and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`.
If this fails, it will create an SMT state and repeatedly use `ematch`
(using `ematch` lemmas in the environment, universally quantified assumptions,
and the supplied lemmas `e1,...,en`) and congruence closure.
`clarify` is complete for propositional logic.
Either of the supplied simp lemmas or the supplied ematch lemmas are optional.
`clarify` will fail if it produces more than one goal.
-/
meta def clarify (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.clarify s (ps.get_or_else []) cfg
/--
`safe [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses
(by splitting conjunctions, eliminating existentials, pushing negations inwards,
and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`.
If this fails, it will create an SMT state and repeatedly use `ematch`
(using `ematch` lemmas in the environment, universally quantified assumptions,
and the supplied lemmas `e1,...,en`) and congruence closure.
`safe` is complete for propositional logic.
Either of the supplied simp lemmas or the supplied ematch lemmas are optional.
`safe` ignores the number of goals it produces, and should never fail.
-/
meta def safe (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.safe s (ps.get_or_else []) cfg
/--
`finish [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses
(by splitting conjunctions, eliminating existentials, pushing negations inwards,
and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`.
If this fails, it will create an SMT state and repeatedly use `ematch`
(using `ematch` lemmas in the environment, universally quantified assumptions,
and the supplied lemmas `e1,...,en`) and congruence closure.
`finish` is complete for propositional logic.
Either of the supplied simp lemmas or the supplied ematch lemmas are optional.
`finish` will fail if it does not close the goal.
-/
meta def finish (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.finish s (ps.get_or_else []) cfg
/--
`iclarify` is like `clarify`, but only uses intuitionistic logic.
-/
meta def iclarify (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.iclarify s (ps.get_or_else []) cfg
/--
`isafe` is like `safe`, but only uses intuitionistic logic.
-/
meta def isafe (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.isafe s (ps.get_or_else []) cfg
/--
`ifinish` is like `finish`, but only uses intuitionistic logic.
-/
meta def ifinish (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?)
(cfg : auto_config := {}) : tactic unit :=
do s β mk_simp_set ff [] hs,
auto.ifinish s (ps.get_or_else []) cfg
end interactive
end tactic
|
ecf315bd7ad3b9d32b5c3aa5d7fd25e170648890 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/run/closure1.lean | 87dcba6f7d8ecb5fb481589a8d0927cb6fccc59f | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 1,468 | lean | import Lean
open Lean
open Lean.Meta
universes u
inductive Vec (Ξ± : Type u) : Nat β Type u
| nil : Vec Ξ± 0
| cons {n} : Ξ± β Vec Ξ± n β Vec Ξ± (n+1)
set_option trace.Meta.debug true
def printDef (declName : Name) : MetaM Unit := do
let cinfo β getConstInfo declName;
trace[Meta.debug] cinfo.value!
def tst1 : MetaM Unit := do
let u := mkLevelParam `u
let v := mkLevelMVar `v
let m1 β mkFreshExprMVar (mkSort levelOne)
withLocalDeclD `Ξ± (mkSort u) $ fun Ξ± => do
withLocalDeclD `Ξ² (mkSort v) $ fun Ξ² => do
let m2 β mkFreshExprMVar (β mkArrow Ξ± m1)
withLocalDeclD `a Ξ± $ fun a => do
withLocalDeclD `f (β mkArrow Ξ± Ξ±) $ fun f => do
withLetDecl `b Ξ± (mkApp f a) $ fun b => do
let t := mkApp m2 (mkApp f b)
let e β mkAuxDefinitionFor `foo1 t
trace[Meta.debug] e
printDef `foo1
#eval tst1
def tst2 : MetaM Unit := do
let u := mkLevelParam `u
withLocalDeclD `Ξ± (mkSort (mkLevelSucc u)) $ fun Ξ± => do
withLocalDeclD `v1 (mkApp2 (mkConst `Vec [u]) Ξ± (mkNatLit 10)) $ fun v1 =>
withLetDecl `n (mkConst `Nat) (mkNatLit 10) $ fun n =>
withLocalDeclD `v2 (mkApp2 (mkConst `Vec [u]) Ξ± n) $ fun v2 => do
let m β mkFreshExprMVar (β mkArrow (mkApp2 (mkConst `Vec [u]) Ξ± (mkNatLit 10)) (mkSort levelZero))
withLocalDeclD `p (mkSort levelZero) $ fun p => do
let t β mkEq v1 v2
let t := mkApp2 (mkConst `And) t (mkApp2 (mkConst `Or) (mkApp m v2) p)
let e β mkAuxDefinitionFor `foo2 t
trace[Meta.debug] e
printDef `foo2
#eval tst2
|
189688c7ab8a08e558a646c5881bfbae0165af62 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1274.lean | f80dd81b1f07253690807cdfe619877a9f5ad8bd | [
"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 | 110 | lean | macro "π" t:(ppSpace ident) : term => `($t)
macro "π" t:(lookahead(term) ident ppSpace) : term => `($t)
|
d64e02ce9d3949c4344b8b40d6fbdc9454a48149 | 649957717d58c43b5d8d200da34bf374293fe739 | /src/category_theory/pempty.lean | 78a0e525f57ea0b54bef0e5c0c5b39f2acc03022 | [
"Apache-2.0"
] | permissive | Vtec234/mathlib | b50c7b21edea438df7497e5ed6a45f61527f0370 | fb1848bbbfce46152f58e219dc0712f3289d2b20 | refs/heads/master | 1,592,463,095,113 | 1,562,737,749,000 | 1,562,737,749,000 | 196,202,858 | 0 | 0 | Apache-2.0 | 1,562,762,338,000 | 1,562,762,337,000 | null | UTF-8 | Lean | false | false | 587 | 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.functor
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
instance pempty_category : small_category pempty :=
{ hom := Ξ» X Y, pempty,
id := by obviously,
comp := by obviously }
namespace functor
variables (C : Type u) [π : category.{v} C]
include π
def empty : pempty β₯€ C := by tidy
end functor
end category_theory
|
afba6996bb5908bbde3d034f01533505712d5b43 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/normed_space/hahn_banach/separation.lean | 42850fe41480399a7235ff422dddf3f65a3e28b8 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 10,712 | lean | /-
Copyright (c) 2022 Bhavik Mehta All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, YaΓ«l Dillies
-/
import analysis.convex.cone.basic
import analysis.convex.gauge
import topology.algebra.module.finite_dimension
import topology.algebra.module.locally_convex
/-!
# Separation Hahn-Banach theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove the geometric Hahn-Banach theorem. For any two disjoint convex sets, there
exists a continuous linear functional separating them, geometrically meaning that we can intercalate
a plane between them.
We provide many variations to stricten the result under more assumptions on the convex sets:
* `geometric_hahn_banach_open`: One set is open. Weak separation.
* `geometric_hahn_banach_open_point`, `geometric_hahn_banach_point_open`: One set is open, the
other is a singleton. Weak separation.
* `geometric_hahn_banach_open_open`: Both sets are open. Semistrict separation.
* `geometric_hahn_banach_compact_closed`, `geometric_hahn_banach_closed_compact`: One set is closed,
the other one is compact. Strict separation.
* `geometric_hahn_banach_point_closed`, `geometric_hahn_banach_closed_point`: One set is closed, the
other one is a singleton. Strict separation.
* `geometric_hahn_banach_point_point`: Both sets are singletons. Strict separation.
## TODO
* Eidelheit's theorem
* `convex β s β interior (closure s) β s`
-/
open set
open_locale pointwise
variables {π E : Type*}
/-- Given a set `s` which is a convex neighbourhood of `0` and a point `xβ` outside of it, there is
a continuous linear functional `f` separating `xβ` and `s`, in the sense that it sends `xβ` to 1 and
all of `s` to values strictly below `1`. -/
lemma separate_convex_open_set [topological_space E] [add_comm_group E] [topological_add_group E]
[module β E] [has_continuous_smul β E] {s : set E}
(hsβ : (0 : E) β s) (hsβ : convex β s) (hsβ : is_open s) {xβ : E} (hxβ : xβ β s) :
β f : E βL[β] β, f xβ = 1 β§ β x β s, f x < 1 :=
begin
let f : E ββ.[β] β :=
linear_pmap.mk_span_singleton xβ 1 (ne_of_mem_of_not_mem hsβ hxβ).symm,
obtain β¨Ο, hΟβ, hΟββ© := exists_extension_of_le_sublinear f (gauge s)
(Ξ» c hc, gauge_smul_of_nonneg hc.le)
(gauge_add_le hsβ $ absorbent_nhds_zero $ hsβ.mem_nhds hsβ) _,
have hΟβ : Ο xβ = 1,
{ rw [βsubmodule.coe_mk xβ (submodule.mem_span_singleton_self _), hΟβ,
linear_pmap.mk_span_singleton'_apply_self] },
have hΟβ : β x β s, Ο x < 1,
{ exact Ξ» x hx, (hΟβ x).trans_lt (gauge_lt_one_of_mem_of_open hsβ hsβ hsβ hx) },
{ refine β¨β¨Ο, _β©, hΟβ, hΟββ©,
refine Ο.continuous_of_nonzero_on_open _ (hsβ.vadd (-xβ)) (nonempty.vadd_set β¨0, hsββ©)
(vadd_set_subset_iff.mpr $ Ξ» x hx, _),
change Ο (-xβ + x) β 0,
rw [map_add, map_neg],
specialize hΟβ x hx,
linarith },
rintro β¨x, hxβ©,
obtain β¨y, rflβ© := submodule.mem_span_singleton.1 hx,
rw linear_pmap.mk_span_singleton'_apply,
simp only [mul_one, algebra.id.smul_eq_mul, submodule.coe_mk],
obtain h | h := le_or_lt y 0,
{ exact h.trans (gauge_nonneg _) },
{ rw [gauge_smul_of_nonneg h.le, smul_eq_mul, le_mul_iff_one_le_right h],
exact one_le_gauge_of_not_mem (hsβ.star_convex hsβ)
(absorbent_nhds_zero $ hsβ.mem_nhds hsβ).absorbs hxβ,
apply_instance }
end
variables [topological_space E] [add_comm_group E] [topological_add_group E] [module β E]
[has_continuous_smul β E] {s t : set E} {x y : E}
/-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is open,
there is a continuous linear functional which separates them. -/
theorem geometric_hahn_banach_open (hsβ : convex β s) (hsβ : is_open s) (ht : convex β t)
(disj : disjoint s t) :
β (f : E βL[β] β) (u : β), (β a β s, f a < u) β§ β b β t, u β€ f b :=
begin
obtain rfl | β¨aβ, haββ© := s.eq_empty_or_nonempty,
{ exact β¨0, 0, by simp, Ξ» b hb, le_rflβ© },
obtain rfl | β¨bβ, hbββ© := t.eq_empty_or_nonempty,
{ exact β¨0, 1, Ξ» a ha, zero_lt_one, by simpβ© },
let xβ := bβ - aβ,
let C := xβ +α΅₯ (s - t),
have : (0:E) β C := β¨aβ - bβ, sub_mem_sub haβ hbβ,
by rw [vadd_eq_add, sub_add_sub_cancel', sub_self]β©,
have : convex β C := (hsβ.sub ht).vadd _,
have : xβ β C,
{ intro hxβ,
rw βadd_zero xβ at hxβ,
exact disj.zero_not_mem_sub_set (vadd_mem_vadd_set_iff.1 hxβ) },
obtain β¨f, hfβ, hfββ© := separate_convex_open_set βΉ0 β CβΊ βΉ_βΊ (hsβ.sub_right.vadd _) βΉxβ β CβΊ,
have : f bβ = f aβ + 1 := by simp [βhfβ],
have forall_le : β (a β s) (b β t), f a β€ f b,
{ intros a ha b hb,
have := hfβ (xβ + (a - b)) (vadd_mem_vadd_set $ sub_mem_sub ha hb),
simp only [f.map_add, f.map_sub, hfβ] at this,
linarith },
refine β¨f, Inf (f '' t), image_subset_iff.1 (_ : f '' s β Iio (Inf (f '' t))), Ξ» b hb, _β©,
{ rw βinterior_Iic,
refine interior_maximal (image_subset_iff.2 $ Ξ» a ha, _) (f.is_open_map_of_ne_zero _ _ hsβ),
{ exact le_cInf (nonempty.image _ β¨_, hbββ©) (ball_image_of_ball $ forall_le _ ha) },
{ rintro rfl,
simpa using hfβ } },
{ exact cInf_le β¨f aβ, ball_image_of_ball $ forall_le _ haββ© (mem_image_of_mem _ hb) }
end
theorem geometric_hahn_banach_open_point (hsβ : convex β s) (hsβ : is_open s) (disj : x β s) :
β f : E βL[β] β, β a β s, f a < f x :=
let β¨f, s, hs, hxβ© := geometric_hahn_banach_open hsβ hsβ (convex_singleton x)
(disjoint_singleton_right.2 disj)
in β¨f, Ξ» a ha, lt_of_lt_of_le (hs a ha) (hx x (mem_singleton _))β©
theorem geometric_hahn_banach_point_open (htβ : convex β t) (htβ : is_open t) (disj : x β t) :
β f : E βL[β] β, β b β t, f x < f b :=
let β¨f, hfβ© := geometric_hahn_banach_open_point htβ htβ disj in β¨-f, by simpaβ©
theorem geometric_hahn_banach_open_open (hsβ : convex β s) (hsβ : is_open s) (htβ : convex β t)
(htβ : is_open t) (disj : disjoint s t) :
β (f : E βL[β] β) (u : β), (β a β s, f a < u) β§ β b β t, u < f b :=
begin
obtain (rfl | β¨aβ, haββ©) := s.eq_empty_or_nonempty,
{ exact β¨0, -1, by simp, Ξ» b hb, by norm_numβ© },
obtain (rfl | β¨bβ, hbββ©) := t.eq_empty_or_nonempty,
{ exact β¨0, 1, Ξ» a ha, by norm_num, by simpβ© },
obtain β¨f, s, hfβ, hfββ© := geometric_hahn_banach_open hsβ hsβ htβ disj,
have hf : is_open_map f,
{ refine f.is_open_map_of_ne_zero _,
rintro rfl,
exact (hfβ _ haβ).not_le (hfβ _ hbβ) },
refine β¨f, s, hfβ, image_subset_iff.1 (_ : f '' t β Ioi s)β©,
rw βinterior_Ici,
refine interior_maximal (image_subset_iff.2 hfβ) (f.is_open_map_of_ne_zero _ _ htβ),
rintro rfl,
exact (hfβ _ haβ).not_le (hfβ _ hbβ),
end
variables [locally_convex_space β E]
/-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is
compact and `t` is closed, there is a continuous linear functional which strongly separates them. -/
theorem geometric_hahn_banach_compact_closed (hsβ : convex β s) (hsβ : is_compact s)
(htβ : convex β t) (htβ : is_closed t) (disj : disjoint s t) :
β (f : E βL[β] β) (u v : β), (β a β s, f a < u) β§ u < v β§ β b β t, v < f b :=
begin
obtain rfl | hs := s.eq_empty_or_nonempty,
{ exact β¨0, -2, -1, by simp, by norm_num, Ξ» b hb, by norm_numβ© },
unfreezingI { obtain rfl | ht := t.eq_empty_or_nonempty },
{ exact β¨0, 1, 2, Ξ» a ha, by norm_num, by norm_num, by simpβ© },
obtain β¨U, V, hU, hV, hUβ, hVβ, sU, tV, disj'β© := disj.exists_open_convexes hsβ hsβ htβ htβ,
obtain β¨f, u, hfβ, hfββ© := geometric_hahn_banach_open_open hUβ hU hVβ hV disj',
obtain β¨x, hxβ, hxββ© := hsβ.exists_forall_ge hs f.continuous.continuous_on,
have : f x < u := hfβ x (sU hxβ),
exact β¨f, (f x + u)/2, u, Ξ» a ha, by linarith [hxβ a ha], by linarith, Ξ» b hb, hfβ b (tV hb)β©,
end
/-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is
closed, and `t` is compact, there is a continuous linear functional which strongly separates them.
-/
theorem geometric_hahn_banach_closed_compact (hsβ : convex β s) (hsβ : is_closed s)
(htβ : convex β t) (htβ : is_compact t) (disj : disjoint s t) :
β (f : E βL[β] β) (u v : β), (β a β s, f a < u) β§ u < v β§ β b β t, v < f b :=
let β¨f, s, t, hs, st, htβ© := geometric_hahn_banach_compact_closed htβ htβ hsβ hsβ disj.symm in
β¨-f, -t, -s, by simpa using ht, by simpa using st, by simpa using hsβ©
theorem geometric_hahn_banach_point_closed (htβ : convex β t) (htβ : is_closed t) (disj : x β t) :
β (f : E βL[β] β) (u : β), f x < u β§ β b β t, u < f b :=
let β¨f, u, v, ha, hst, hbβ© := geometric_hahn_banach_compact_closed (convex_singleton x)
is_compact_singleton htβ htβ (disjoint_singleton_left.2 disj)
in β¨f, v, hst.trans' $ ha x $ mem_singleton _, hbβ©
theorem geometric_hahn_banach_closed_point (hsβ : convex β s) (hsβ : is_closed s) (disj : x β s) :
β (f : E βL[β] β) (u : β), (β a β s, f a < u) β§ u < f x :=
let β¨f, s, t, ha, hst, hbβ© := geometric_hahn_banach_closed_compact hsβ hsβ (convex_singleton x)
is_compact_singleton (disjoint_singleton_right.2 disj)
in β¨f, s, ha, hst.trans $ hb x $ mem_singleton _β©
/-- See also `normed_space.eq_iff_forall_dual_eq`. -/
theorem geometric_hahn_banach_point_point [t1_space E] (hxy : x β y) :
β (f : E βL[β] β), f x < f y :=
begin
obtain β¨f, s, t, hs, st, htβ© :=
geometric_hahn_banach_compact_closed (convex_singleton x) is_compact_singleton
(convex_singleton y) is_closed_singleton (disjoint_singleton.2 hxy),
exact β¨f, by linarith [hs x rfl, ht y rfl]β©,
end
/-- A closed convex set is the intersection of the halfspaces containing it. -/
lemma Inter_halfspaces_eq (hsβ : convex β s) (hsβ : is_closed s) :
(β (l : E βL[β] β), {x | β y β s, l x β€ l y}) = s :=
begin
rw set.Inter_set_of,
refine set.subset.antisymm (Ξ» x hx, _) (Ξ» x hx l, β¨x, hx, le_rflβ©),
by_contra,
obtain β¨l, s, hlA, hlβ© := geometric_hahn_banach_closed_point hsβ hsβ h,
obtain β¨y, hy, hxyβ© := hx l,
exact ((hxy.trans_lt (hlA y hy)).trans hl).not_le le_rfl,
end
|
a8ce9fab8f20ff6e5865b3d881bc9933416b5ef2 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/compiler/termparsertest1.lean | 80ee6241a7561441ac607288925efb0a57126681 | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,587 | lean | import Lean
open Lean
open Lean.Parser
def testParser (input : String) : IO Unit :=
do
env β mkEmptyEnvironment;
stx β IO.ofExcept $ runParserCategory env `term input "<input>";
IO.println stx
def test (is : List String) : IO Unit :=
is.forM $ fun input => do
IO.println input;
testParser input
def testParserFailure (input : String) : IO Unit :=
do
env β mkEmptyEnvironment;
match runParserCategory env `term input "<input>" with
| Except.ok stx => throw (IO.userError ("unexpected success\n" ++ toString stx))
| Except.error msg => IO.println ("failed as expected, error: " ++ msg)
def testFailures (is : List String) : IO Unit :=
is.forM $ fun input => do
IO.println input;
testParserFailure input
def main (xs : List String) : IO Unit :=
do
test [
"Prod.mk",
"x.{u, v+1}",
"x.{u}",
"x",
"x.{max u v}",
"x.{max u v, 0}",
"f 0 1",
"f.{u+1} \"foo\" x",
"(f x, 0, 1)",
"()",
"(f x)",
"(f x : Type)",
"h (f x) (g y)",
"if x then f x else g x",
"if h : x then f x h else g x h",
"have p x y from f x; g this",
"suffices h : p x y from f x; g this",
"show p x y from f x",
"fun x y => f y x",
"fun (x y : Nat) => f y x",
"fun (x, y) => f y x",
"fun z (x, y) => f y x",
"fun β¨x, yβ© β¨z, wβ© => f y x w z",
"fun (Prod.mk x y) => f y x",
"{ x := 10, y := 20 }",
"{ x := 10, y := 20, }",
"{ x // p x 10 }",
"{ x : Nat // p x 10 }",
"{ .. }",
"{ fst := 10, .. : Nat Γ Nat }",
"a[i]",
"f [10, 20]",
"g a[x+2]",
"g f.a.1.2.bla x.1.a",
"x+y*z < 10/3",
"id (Ξ± := Nat) 10",
"(x : a)",
"a -> b",
"{x : a} -> b",
"{a : Type} -> [HasToString a] -> (x : a) -> b",
"f ({x : a} -> b)",
"f (x : a) -> b",
"f ((x : a) -> b)",
"(f : (n : Nat) β Vector Nat n) -> Nat",
"β x y (z : Nat), x > y -> x > y - z",
"
match x with
| some x => true
| none => false",
"
match x with
| some y => match y with
| some (a, b) => a + b
| none => 1
| none => 0
",
"Type u",
"Sort v",
"Type 1",
"f Type 1",
"let x := 0; x + 1",
"let x : Nat := 0; x + 1",
"let f (x : Nat) := x + 1; f 0",
"let f {Ξ± : Type} (a : Ξ±) : Ξ± := a; f 10",
"let f (x) := x + 1; f 10 + f 20",
"let (x, y) := f 10; x + y",
"let { fst := x, .. } := f 10; x + x",
"let x.y := f 10; x",
"let x.1 := f 10; x",
"let x[i].y := f 10; x",
"let x[i] := f 20; x",
"-x + y",
"!x",
"Β¬ a β§ b",
"
do
x β f a;
x : Nat β f a;
g x;
let y := g x;
(a, b) <- h x y;
let (a, b) := (b, a);
pure (a + b)",
"do { x β f a; pure $ a + a }",
"let f : Nat β Nat β Nat
| 0, a => a + 10
| n+1, b => n * b;
f 20",
"max a b"
];
testFailures [
"f {x : a} -> b",
"(x := 20)",
"let x 10; x",
"let x := y"
]
|
0b2d52989751b3881fc4b90404c5e629f70f1211 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/algebra/order_functions.lean | 49b2a8150ff74b6a9451dbefb08a0a00d96888d7 | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 11,271 | 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.ordered_group order.lattice
open lattice
universes u v
variables {Ξ± : Type u} {Ξ² : Type v}
attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right
/-- A function `f` is strictly monotone if `a < b` implies `f a < f b`. -/
def strict_mono [has_lt Ξ±] [has_lt Ξ²] (f : Ξ± β Ξ²) : Prop :=
β a b, a < b β f a < f b
namespace strict_mono
open ordering function
section
variables [linear_order Ξ±] [preorder Ξ²] {f : Ξ± β Ξ²}
lemma lt_iff_lt (H : strict_mono f) {a b} :
f a < f b β a < b :=
β¨Ξ» h, ((lt_trichotomy b a)
.resolve_left $ Ξ» h', lt_asymm h $ H _ _ h')
.resolve_left $ Ξ» e, ne_of_gt h $ congr_arg _ e, H _ _β©
lemma injective (H : strict_mono f) : injective f
| a b e := ((lt_trichotomy a b)
.resolve_left $ Ξ» h, ne_of_lt (H _ _ h) e)
.resolve_right $ Ξ» h, ne_of_gt (H _ _ h) e
theorem compares (H : strict_mono f) {a b} :
β {o}, compares o (f a) (f b) β compares o a b
| lt := H.lt_iff_lt
| eq := β¨Ξ» h, H.injective h, congr_arg _β©
| gt := H.lt_iff_lt
lemma le_iff_le (H : strict_mono f) {a b} :
f a β€ f b β a β€ b :=
β¨Ξ» h, le_of_not_gt $ Ξ» h', not_le_of_lt (H b a h') h,
Ξ» h, (lt_or_eq_of_le h).elim (Ξ» h', le_of_lt (H _ _ h')) (Ξ» h', h' βΈ le_refl _)β©
end
protected lemma nat {Ξ²} [preorder Ξ²] {f : β β Ξ²} (h : βn, f n < f (n+1)) : strict_mono f :=
by { intros n m hnm, induction hnm with m' hnm' ih, apply h, exact lt.trans ih (h _) }
-- `preorder Ξ±` isn't strong enough: if the preorder on Ξ± is an equivalence relation,
-- then `strict_mono f` is vacuously true.
lemma monotone [partial_order Ξ±] [preorder Ξ²] {f : Ξ± β Ξ²} (H : strict_mono f) : monotone f :=
Ξ» a b h, (lt_or_eq_of_le h).rec (le_of_lt β (H _ _)) (by rintro rfl; refl)
end strict_mono
section
open function
variables [partial_order Ξ±] [partial_order Ξ²] {f : Ξ± β Ξ²}
lemma strict_mono_of_monotone_of_injective (hβ : monotone f) (hβ : injective f) :
strict_mono f :=
Ξ» a b h,
begin
rw lt_iff_le_and_ne at β’ h,
exact β¨hβ h.1, Ξ» e, h.2 (hβ e)β©
end
end
section
variables [decidable_linear_order Ξ±] [decidable_linear_order Ξ²] {f : Ξ± β Ξ²} {a b c d : Ξ±}
-- translate from lattices to linear orders (sup β max, inf β min)
@[simp] lemma le_min_iff : c β€ min a b β c β€ a β§ c β€ b := le_inf_iff
@[simp] lemma max_le_iff : max a b β€ c β a β€ c β§ b β€ c := sup_le_iff
lemma max_le_max : a β€ c β b β€ d β max a b β€ max c d := sup_le_sup
lemma min_le_min : a β€ c β b β€ d β min a b β€ min c d := inf_le_inf
lemma le_max_left_of_le : a β€ b β a β€ max b c := le_sup_left_of_le
lemma le_max_right_of_le : a β€ c β a β€ max b c := le_sup_right_of_le
lemma min_le_left_of_le : a β€ c β min a b β€ c := inf_le_left_of_le
lemma min_le_right_of_le : b β€ c β min a b β€ c := inf_le_right_of_le
lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left
lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right
lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left
lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right
instance max_idem : is_idempotent Ξ± max := by apply_instance
instance min_idem : is_idempotent Ξ± min := by apply_instance
@[simp] lemma min_le_iff : min a b β€ c β a β€ c β¨ b β€ c :=
have a β€ b β (a β€ c β¨ b β€ c β a β€ c),
from assume h, or_iff_left_of_imp $ le_trans h,
have b β€ a β (a β€ c β¨ b β€ c β b β€ c),
from assume h, or_iff_right_of_imp $ le_trans h,
by cases le_total a b; simp *
@[simp] lemma le_max_iff : a β€ max b c β a β€ b β¨ a β€ c :=
have b β€ c β (a β€ b β¨ a β€ c β a β€ c),
from assume h, or_iff_right_of_imp $ assume h', le_trans h' h,
have c β€ b β (a β€ b β¨ a β€ c β a β€ b),
from assume h, or_iff_left_of_imp $ assume h', le_trans h' h,
by cases le_total b c; simp *
@[simp] lemma max_lt_iff : max a b < c β (a < c β§ b < c) :=
by rw [lt_iff_not_ge]; simp [(β₯), le_max_iff, not_or_distrib]
@[simp] lemma lt_min_iff : a < min b c β (a < b β§ a < c) :=
by rw [lt_iff_not_ge]; simp [(β₯), min_le_iff, not_or_distrib]
@[simp] lemma lt_max_iff : a < max b c β a < b β¨ a < c :=
by rw [lt_iff_not_ge]; simp [(β₯), max_le_iff, not_and_distrib]
@[simp] lemma min_lt_iff : min a b < c β a < c β¨ b < c :=
by rw [lt_iff_not_ge]; simp [(β₯), le_min_iff, not_and_distrib]
lemma max_lt_max (hβ : a < c) (hβ : b < d) : max a b < max c d :=
by apply max_lt; simp [lt_max_iff, hβ, hβ]
lemma min_lt_min (hβ : a < c) (hβ : b < d) : min a b < min c d :=
by apply lt_min; simp [min_lt_iff, hβ, hβ]
theorem min_right_comm (a b c : Ξ±) : min (min a b) c = min (min a c) b :=
right_comm min min_comm min_assoc a b c
theorem max.left_comm (a b c : Ξ±) : max a (max b c) = max b (max a c) :=
left_comm max max_comm max_assoc a b c
theorem max.right_comm (a b c : Ξ±) : max (max a b) c = max (max a c) b :=
right_comm max max_comm max_assoc a b c
lemma max_distrib_of_monotone (hf : monotone f) : f (max a b) = max (f a) (f b) :=
by cases le_total a b; simp [h, hf h]
lemma min_distrib_of_monotone (hf : monotone f) : f (min a b) = min (f a) (f b) :=
by cases le_total a b; simp [h, hf h]
theorem min_choice (a b : Ξ±) : min a b = a β¨ min a b = b :=
by by_cases h : a β€ b; simp [min, h]
theorem max_choice (a b : Ξ±) : max a b = a β¨ max a b = b :=
by by_cases h : a β€ b; simp [max, h]
lemma le_of_max_le_left {a b c : Ξ±} (h : max a b β€ c) : a β€ c :=
le_trans (le_max_left _ _) h
lemma le_of_max_le_right {a b c : Ξ±} (h : max a b β€ c) : b β€ c :=
le_trans (le_max_right _ _) h
end
lemma min_add {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) :
min a b + c = min (a + c) (b + c) :=
if hle : a β€ b then
have a - c β€ b - c, from sub_le_sub hle (le_refl _),
by simp * at *
else
have b - c β€ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _),
by simp * at *
lemma min_sub {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) :
min a b - c = min (a - c) (b - c) :=
by simp [min_add, sub_eq_add_neg]
/- Some lemmas about types that have an ordering and a binary operation, with no
rules relating them. -/
lemma fn_min_add_fn_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ²] (f : Ξ± β Ξ²) (n m : Ξ±) :
f (min n m) + f (max n m) = f n + f m :=
by { cases le_total n m with h h; simp [h] }
lemma min_add_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ±] (n m : Ξ±) :
min n m + max n m = n + m :=
fn_min_add_fn_max id n m
lemma fn_min_mul_fn_max [decidable_linear_order Ξ±] [comm_semigroup Ξ²] (f : Ξ± β Ξ²) (n m : Ξ±) :
f (min n m) * f (max n m) = f n * f m :=
by { cases le_total n m with h h; simp [h, mul_comm] }
lemma min_mul_max [decidable_linear_order Ξ±] [comm_semigroup Ξ±] (n m : Ξ±) :
min n m * max n m = n * m :=
fn_min_mul_fn_max id n m
section decidable_linear_ordered_comm_group
variables [decidable_linear_ordered_comm_group Ξ±] {a b c : Ξ±}
attribute [simp] abs_zero abs_neg
def abs_add := @abs_add_le_abs_add_abs
theorem abs_le : abs a β€ b β - b β€ a β§ a β€ b :=
β¨assume h, β¨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) hβ©,
assume β¨hβ, hββ©, abs_le_of_le_of_neg_le hβ $ neg_le_of_neg_le hββ©
lemma abs_lt : abs a < b β - b < a β§ a < b :=
β¨assume h, β¨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) hβ©,
assume β¨hβ, hββ©, abs_lt_of_lt_of_neg_lt hβ $ neg_lt_of_neg_lt hββ©
lemma abs_sub_le_iff : abs (a - b) β€ c β a - b β€ c β§ b - a β€ c :=
by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm]
lemma abs_sub_lt_iff : abs (a - b) < c β a - b < c β§ b - a < c :=
by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm]
def sub_abs_le_abs_sub := @abs_sub_abs_le_abs_sub
lemma abs_abs_sub_le_abs_sub (a b : Ξ±) : abs (abs a - abs b) β€ abs (a - b) :=
abs_sub_le_iff.2 β¨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_subβ©
lemma abs_eq (hb : b β₯ 0) : abs a = b β a = b β¨ a = -b :=
iff.intro
begin
cases le_total a 0 with a_nonpos a_nonneg,
{ rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr },
{ rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl }
end
(by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb)
@[simp] lemma abs_eq_zero : abs a = 0 β a = 0 :=
β¨eq_zero_of_abs_eq_zero, Ξ» e, e.symm βΈ abs_zeroβ©
lemma abs_pos_iff {a : Ξ±} : 0 < abs a β a β 0 :=
β¨Ξ» h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zeroβ©
@[simp] lemma abs_nonpos_iff {a : Ξ±} : abs a β€ 0 β a = 0 :=
by rw [β not_lt, abs_pos_iff, not_not]
lemma abs_le_max_abs_abs (hab : a β€ b) (hbc : b β€ c) : abs b β€ max (abs a) (abs c) :=
abs_le_of_le_of_neg_le
(by simp [le_max_iff, le_trans hbc (le_abs_self c)])
(by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)])
theorem abs_le_abs {Ξ± : Type*} [decidable_linear_ordered_comm_group Ξ±] {a b : Ξ±}
(hβ : a β€ b) (hβ : -a β€ b) :
abs a β€ abs b :=
calc abs a
β€ b : by { apply abs_le_of_le_of_neg_le; assumption }
... β€ abs b : le_abs_self _
lemma min_le_add_of_nonneg_right {a b : Ξ±} (hb : b β₯ 0) : min a b β€ a + b :=
calc
min a b β€ a : by apply min_le_left
... β€ a + b : le_add_of_nonneg_right hb
lemma min_le_add_of_nonneg_left {a b : Ξ±} (ha : a β₯ 0) : min a b β€ a + b :=
calc
min a b β€ b : by apply min_le_right
... β€ a + b : le_add_of_nonneg_left ha
lemma max_le_add_of_nonneg {a b : Ξ±} (ha : a β₯ 0) (hb : b β₯ 0) : max a b β€ a + b :=
max_le_iff.2 (by split; simpa)
end decidable_linear_ordered_comm_group
section decidable_linear_ordered_semiring
variables [decidable_linear_ordered_semiring Ξ±] {a b c d : Ξ±}
lemma monotone_mul_of_nonneg (ha : 0 β€ a) : monotone (Ξ» x, a*x) :=
assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha
lemma mul_max_of_nonneg (b c : Ξ±) (ha : 0 β€ a) : a * max b c = max (a * b) (a * c) :=
max_distrib_of_monotone (monotone_mul_of_nonneg ha)
lemma mul_min_of_nonneg (b c : Ξ±) (ha : 0 β€ a) : a * min b c = min (a * b) (a * c) :=
min_distrib_of_monotone (monotone_mul_of_nonneg ha)
end decidable_linear_ordered_semiring
section decidable_linear_ordered_comm_ring
variables [decidable_linear_ordered_comm_ring Ξ±] {a b c d : Ξ±}
@[simp] lemma abs_one : abs (1 : Ξ±) = 1 := abs_of_pos zero_lt_one
lemma max_mul_mul_le_max_mul_max (b c : Ξ±) (ha : 0 β€ a) (hd: 0 β€ d) :
max (a * b) (d * c) β€ max a c * max d b :=
have ba : b * a β€ max d b * max c a,
from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)),
have cd : c * d β€ max a c * max b d,
from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)),
max_le
(by simpa [mul_comm, max_comm] using ba)
(by simpa [mul_comm, max_comm] using cd)
end decidable_linear_ordered_comm_ring
|
5724224b3fabec6eedcba38e14069faf1430ee04 | 9c2e8d73b5c5932ceb1333265f17febc6a2f0a39 | /src/S4/data.lean | 0fe0da27c42eceae951c9640fef5484e3a791bb3 | [
"MIT"
] | permissive | minchaowu/ModalTab | 2150392108dfdcaffc620ff280a8b55fe13c187f | 9bb0bf17faf0554d907ef7bdd639648742889178 | refs/heads/master | 1,626,266,863,244 | 1,592,056,874,000 | 1,592,056,874,000 | 153,314,364 | 12 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 6,586 | lean | import defs data.list.perm
open nnf
namespace list
universes u v w
variables {Ξ± : Type u}
theorem length_sub_lt_of_nodup_subperm [decidable_eq Ξ±] {lβ lβ : list Ξ±} {a : Ξ±}
(hβ : lβ <+~ lβ) (hβ : a β lβ) (hβ : a β lβ) (hβ : nodup lβ):
length lβ - length (a :: lβ) < length lβ - length lβ
:=
begin
rw nat.sub_lt_sub_left_iff,
{ simp [zero_lt_one] },
{ apply subperm.length_le,
apply cons_subperm_of_mem; assumption }
end
end list
structure psig : Type :=
(d : nnf)
(b : list nnf)
instance : decidable_eq psig := by tactic.mk_dec_eq_instance
def sig : Type := option psig
instance : decidable_eq sig := by tactic.mk_dec_eq_instance
def dsig : Ξ (s : sig) (h : s β none), nnf
| none h := by contradiction
| (some β¨d, bβ©) h := d
def bsig : Ξ (s : sig) (h : s β none), list nnf
| none h := by contradiction
| (some β¨d, bβ©) h := b
instance (Ξ) : decidable_eq (no_literals Ξ) := by tactic.mk_dec_eq_instance
instance (Ξ) : decidable_eq (saturated Ξ) := by tactic.mk_dec_eq_instance
instance (Ξ) : decidable_eq (box_only Ξ) := by tactic.mk_dec_eq_instance
structure sseqt : Type :=
(goal : list nnf)
(s : sig) -- sig := option psig
(a : list psig) -- psig is the signature, which is of the form (d, b)
(h b m: list nnf)
(ndh : list.nodup h) -- nodup says there is no duplicate elements
(ndb : list.nodup b)
(sph : h <+~ closure goal) -- <+~ denotes sublist permutation
(spb : b <+~ closure goal)
(sbm : m β closure goal)
(ha : β Ο β h, (β¨Ο, bβ© : psig) β a)
(hb : box_only b)
-- dsig takes a signature (d, b) and a proof h, and returns d
(psβ : Ξ (h : s β none), dsig s h β m)
-- bsig takes a signature (d, b) and a proof h, and returns b
(psβ : Ξ (h : s β none), bsig s h β m)
instance : decidable_eq sseqt := by tactic.mk_dec_eq_instance
class val_constructible (Ξ : sseqt) :=
(satu : saturated Ξ.m)
(no_box_main : β {Ο}, box Ο β Ξ.m)
(no_contra_main : β {n}, var n β Ξ.m β neg n β Ξ.m)
class modal_applicable (Ξ : sseqt) extends val_constructible Ξ :=
(Ο : nnf)
(ex : dia Ο β Ξ.m)
class model_constructible (Ξ : sseqt) extends val_constructible Ξ :=
(no_dia : β {Ο}, nnf.dia Ο β Ξ.m)
def and_child {Ο Ο} (Ξ : sseqt) (h : nnf.and Ο Ο β Ξ.m) : sseqt :=
{ goal := Ξ.goal,
s := none,
m := Ο :: Ο :: Ξ.m.erase (and Ο Ο),
sbm := begin
intros x hx, cases hx,
{rw hx, apply (mem_closure_and _ (Ξ.sbm h)).1},
{cases hx,
{rw hx, apply (mem_closure_and _ (Ξ.sbm h)).2},
{apply Ξ.sbm, apply list.erase_subset, exact hx}}
end,
psβ := by intro; contradiction,
psβ := by intro; contradiction,
.. Ξ}
inductive and_instance_seqt (Ξ : sseqt) : sseqt β Type
| cons : Ξ {Ο Ο} (h : nnf.and Ο Ο β Ξ.m),
and_instance_seqt $ and_child Ξ h
def or_child_left {Ο Ο} (Ξ : sseqt) (h : nnf.or Ο Ο β Ξ.m) : sseqt :=
{ goal := Ξ.goal,
s := none,
m := Ο :: Ξ.m.erase (or Ο Ο),
sbm := begin
intros x hx, cases hx,
{rw hx, apply (mem_closure_or _ (Ξ.sbm h)).1},
{apply Ξ.sbm, apply list.erase_subset, exact hx}
end,
psβ := by intro; contradiction,
psβ := by intro; contradiction,
.. Ξ}
def or_child_right {Ο Ο} (Ξ : sseqt) (h : nnf.or Ο Ο β Ξ.m) : sseqt :=
{ goal := Ξ.goal,
s := none,
m := Ο :: Ξ.m.erase (or Ο Ο),
sbm := begin
intros x hx, cases hx,
{rw hx, apply (mem_closure_or _ (Ξ.sbm h)).2},
{apply Ξ.sbm, apply list.erase_subset, exact hx}
end,
psβ := by intro; contradiction,
psβ := by intro; contradiction,
.. Ξ}
inductive or_instance_seqt (Ξ : sseqt) : sseqt β sseqt β Type
| cons : Ξ {Ο Ο} (h : nnf.or Ο Ο β Ξ.m),
or_instance_seqt (or_child_left Ξ h) (or_child_right Ξ h)
def box_child_new {Ο} (Ξ : sseqt) (hβ : nnf.box Ο β Ξ.m) (hβ : nnf.box Ο β Ξ.b) : sseqt :=
{ goal := Ξ.goal,
s := none,
h := [],
b := box Ο :: Ξ.b,
m := Ο :: Ξ.m.erase (box Ο),
ndh := by simp,
ndb := begin rw list.nodup_cons, split, exact hβ, exact Ξ.ndb end,
sph := begin apply list.nil_subperm end,
spb := begin
apply list.cons_subperm_of_mem Ξ.ndb hβ,
apply Ξ.sbm hβ, apply Ξ.spb
end,
sbm := begin
intros x hx, cases hx,
{rw hx, apply mem_closure_box _ (Ξ.sbm hβ)},
{apply Ξ.sbm, apply list.erase_subset, exact hx}
end,
ha := Ξ» Ο h, absurd h $ list.not_mem_nil _,
hb := cons_box_only Ξ.hb,
psβ := by intro; contradiction,
psβ := by intro; contradiction,
.. Ξ}
inductive box_new_instance_seqt (Ξ : sseqt) : sseqt β Type
| cons : Ξ {Ο} (hβ : nnf.box Ο β Ξ.m) (hβ : nnf.box Ο β Ξ.b),
box_new_instance_seqt $ box_child_new Ξ hβ hβ
def box_child {Ο} (Ξ : sseqt) (hβ : nnf.box Ο β Ξ.m) : sseqt :=
{ goal := Ξ.goal,
s := none,
a := Ξ.a,
h := Ξ.h,
b := Ξ.b,
m := Ο :: Ξ.m.erase (box Ο),
ndh := Ξ.ndh,
ndb := Ξ.ndb,
sph := Ξ.sph,
spb := Ξ.spb,
sbm := begin
intros x hx, cases hx,
{rw hx, apply mem_closure_box _ (Ξ.sbm hβ)},
{apply Ξ.sbm, apply list.erase_subset, exact hx}
end,
ha := Ξ.ha,
hb := Ξ.hb,
psβ := by intro; contradiction,
psβ := by intro; contradiction}
inductive box_dup_instance_seqt (Ξ : sseqt) : sseqt β Type
| cons : Ξ {Ο} (hβ : nnf.box Ο β Ξ.m) (hβ : nnf.box Ο β Ξ.b),
box_dup_instance_seqt $ box_child Ξ hβ
theorem hintikka_vc {Ξ} (h : val_constructible Ξ) : hintikka Ξ.m :=
{hno_contra := h.no_contra_main,
hand_left := begin intros Ο Ο hβ, exfalso, apply h.satu.no_and, exact hβ end,
hand_right := begin intros Ο Ο hβ, exfalso, apply h.satu.no_and, exact hβ end,
hor := begin intros Ο Ο hβ, exfalso, apply h.satu.no_or, exact hβ end,
hbox := begin intros Ο hβ, exfalso, apply h.no_box_main, exact hβ end}
theorem hintikka_ma {Ξ} (h : modal_applicable Ξ) : hintikka Ξ.m :=
hintikka_vc h.to_val_constructible
theorem hintikka_mc {Ξ} (h : model_constructible Ξ) : hintikka Ξ.m :=
hintikka_vc h.to_val_constructible
structure info : Type :=
(id : sseqt)
(htk : list nnf)
(hhtk : hintikka htk)
(mhtk : id.m β htk)
instance : decidable_eq info := by tactic.mk_dec_eq_instance
inductive tmodel
| cons : info β list tmodel β list psig β tmodel
instance : decidable_eq tmodel := by tactic.mk_dec_eq_instance
|
8dbc36c627e2ef24c8a1e5c276c3a72bc3e46aae | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/nat/sqrt.lean | f8e3210cb501b7ad0753517c02ac4444ea10476d | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,788 | 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, Johannes HΓΆlzl, Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.int.basic
import Mathlib.PostPort
namespace Mathlib
/-!
# Square root of natural numbers
An efficient binary implementation of a (`sqrt n`) function that
returns `s` such that
```
s*s β€ n β€ s*s + s + s
```
-/
namespace nat
theorem sqrt_aux_dec {b : β} (h : b β 0) : shiftr b (bit0 1) < b := sorry
/-- Auxiliary function for `nat.sqrt`. See e.g.
<https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)> -/
def sqrt_aux : β β β β β β β :=
sorry
/-- `sqrt n` is the square root of a natural number `n`. If `n` is not a
perfect square, it returns the largest `k:β` such that `k*k β€ n`. -/
def sqrt (n : β) : β :=
sorry
theorem sqrt_aux_0 (r : β) (n : β) : sqrt_aux 0 r n = r := sorry
theorem sqrt_aux_1 {r : β} {n : β} {b : β} (h : b β 0) {n' : β} (hβ : r + b + n' = n) : sqrt_aux b r n = sqrt_aux (shiftr b (bit0 1)) (div2 r + b) n' := sorry
theorem sqrt_aux_2 {r : β} {n : β} {b : β} (h : b β 0) (hβ : n < r + b) : sqrt_aux b r n = sqrt_aux (shiftr b (bit0 1)) (div2 r) n := sorry
theorem sqrt_le (n : β) : sqrt n * sqrt n β€ n :=
and.left (sqrt_is_sqrt n)
theorem lt_succ_sqrt (n : β) : n < Nat.succ (sqrt n) * Nat.succ (sqrt n) :=
and.right (sqrt_is_sqrt n)
theorem sqrt_le_add (n : β) : n β€ sqrt n * sqrt n + sqrt n + sqrt n :=
eq.mpr (id (Eq._oldrec (Eq.refl (n β€ sqrt n * sqrt n + sqrt n + sqrt n)) (Eq.symm (succ_mul (sqrt n) (sqrt n)))))
(le_of_lt_succ (lt_succ_sqrt n))
theorem le_sqrt {m : β} {n : β} : m β€ sqrt n β m * m β€ n :=
{ mp := fun (h : m β€ sqrt n) => le_trans (mul_self_le_mul_self h) (sqrt_le n),
mpr := fun (h : m * m β€ n) => le_of_lt_succ (iff.mpr mul_self_lt_mul_self_iff (lt_of_le_of_lt h (lt_succ_sqrt n))) }
theorem sqrt_lt {m : β} {n : β} : sqrt m < n β m < n * n :=
lt_iff_lt_of_le_iff_le le_sqrt
theorem sqrt_le_self (n : β) : sqrt n β€ n :=
le_trans (le_mul_self (sqrt n)) (sqrt_le n)
theorem sqrt_le_sqrt {m : β} {n : β} (h : m β€ n) : sqrt m β€ sqrt n :=
iff.mpr le_sqrt (le_trans (sqrt_le m) h)
theorem sqrt_eq_zero {n : β} : sqrt n = 0 β n = 0 := sorry
theorem eq_sqrt {n : β} {q : β} : q = sqrt n β q * q β€ n β§ n < (q + 1) * (q + 1) := sorry
theorem le_three_of_sqrt_eq_one {n : β} (h : sqrt n = 1) : n β€ bit1 1 :=
le_of_lt_succ (iff.mp sqrt_lt (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt n < bit0 1)) h)) (of_as_true trivial)))
theorem sqrt_lt_self {n : β} (h : 1 < n) : sqrt n < n :=
iff.mpr sqrt_lt
(eq.mp (Eq._oldrec (Eq.refl (n * 1 < n * n)) (mul_one n)) (nat.mul_lt_mul_of_pos_left h (lt_of_succ_lt h)))
theorem sqrt_pos {n : β} : 0 < sqrt n β 0 < n :=
le_sqrt
theorem sqrt_add_eq (n : β) {a : β} (h : a β€ n + n) : sqrt (n * n + a) = n := sorry
theorem sqrt_eq (n : β) : sqrt (n * n) = n :=
sqrt_add_eq n (zero_le (n + n))
theorem sqrt_succ_le_succ_sqrt (n : β) : sqrt (Nat.succ n) β€ Nat.succ (sqrt n) := sorry
theorem exists_mul_self (x : β) : (β (n : β), n * n = x) β sqrt x * sqrt x = x := sorry
theorem sqrt_mul_sqrt_lt_succ (n : β) : sqrt n * sqrt n < n + 1 :=
iff.mpr lt_succ_iff (sqrt_le n)
theorem succ_le_succ_sqrt (n : β) : n + 1 β€ (sqrt n + 1) * (sqrt n + 1) :=
le_of_pred_lt (lt_succ_sqrt (Nat.pred (n + 1)))
/-- There are no perfect squares strictly between mΒ² and (m+1)Β² -/
theorem not_exists_sq {n : β} {m : β} (hl : m * m < n) (hr : n < (m + 1) * (m + 1)) : Β¬β (t : β), t * t = n := sorry
|
1f23a6c78f056c288cfd694472670fe5f729d795 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/nat/mul_ind.lean | bd9b629db6a39afe949fd64b49737943ac15cfb5 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 4,359 | lean | /-
Copyright (c) 2021 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import number_theory.padics.padic_norm
/-!
# Multiplicative induction principles for β
This file provides three (closely linked) induction principles for β, commonly used for proofs
about multiplicative functions, such as the totient function and the MΓΆbius function.
## Main definitions
* `nat.rec_on_prime_pow`: Given `P 0, P 1` and a way to extend `P a` to `P (p ^ k * a)`, you can
define `P` for all natural numbers.
* `nat.rec_on_prime_coprime`: Given `P 0`, `P (p ^ k)` for all `p` prime, and a way to extend `P a`
and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers.
* `nat.rec_on_pos_prime_coprime`: Given `P 0`, `P 1`, and `P (p ^ k)` for positive prime powers, and
a way to extend `P a` and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all
natural numbers.
* `nat.rec_on_mul`: Given `P 0`, `P 1`, `P p` for all primes, and a proof that
you can extend `P a` and `P b` to `P (a * b)`, you can define `P` for all natural numbers.
## Implementation notes
The proofs use `padic_val_nat`; however, we have `padic_val_nat p = nat.log p $ nat.gcd k (p ^ k)`
for any `p β£ k`, which requires far less imports - the API isn't there though; however, this is why
it's in `data` even though we import `number_theory`; it's not a particularly deep theorem.
## TODO:
Extend these results to any `normalization_monoid` with unique factorization.
-/
namespace nat
/-- Given `P 0, P 1` and a way to extend `P a` to `P (p ^ k * a)`,
you can define `P` for all natural numbers. -/
@[elab_as_eliminator]
def rec_on_prime_pow {P : β β Sort*} (h0 : P 0) (h1 : P 1)
(h : β a p n : β, p.prime β Β¬ p β£ a β P a β P (p ^ n * a)) : β (a : β), P a :=
Ξ» a, nat.strong_rec_on a $ Ξ» n,
match n with
| 0 := Ξ» _, h0
| 1 := Ξ» _, h1
| (k+2) := Ξ» hk, begin
let p := (k + 2).min_fac,
haveI : fact (prime p) := β¨min_fac_prime (succ_succ_ne_one k)β©,
let t := padic_val_nat p (k+2),
have hpt : p ^ t β£ k + 2 := pow_padic_val_nat_dvd,
have ht : 0 < t := one_le_padic_val_nat_of_dvd (nat.succ_ne_zero (k + 1)) (min_fac_dvd _),
convert h ((k + 2) / p ^ t) p t (fact.out _) _ _,
{ rw nat.mul_div_cancel' hpt },
{ rw [nat.dvd_div_iff hpt, βpow_succ'],
exact pow_succ_padic_val_nat_not_dvd nat.succ_pos' },
apply hk _ (nat.div_lt_of_lt_mul _),
rw [lt_mul_iff_one_lt_left nat.succ_pos', one_lt_pow_iff ht.ne],
exact (prime.one_lt' p).out
end
end
/-- Given `P 0`, `P 1`, and `P (p ^ k)` for positive prime powers, and a way to extend `P a` and
`P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. -/
@[elab_as_eliminator]
def rec_on_pos_prime_coprime {P : β β Sort*} (hp : β p n : β, prime p β 0 < n β P (p ^ n))
(h0 : P 0) (h1 : P 1) (h : β a b, 0 < a β 0 < b β coprime a b β P a β P b β P (a * b)) :
β a, P a :=
rec_on_prime_pow h0 h1 $ Ξ» a p n hp' hpa ha,
(h (p ^ n) a (pow_pos hp'.pos _) (nat.pos_of_ne_zero (Ξ» t, by simpa [t] using hpa))
(prime.coprime_pow_of_not_dvd hp' hpa).symm
(if h : n = 0 then eq.rec h1 h.symm else hp p n hp' $ nat.pos_of_ne_zero h) ha)
/-- Given `P 0`, `P (p ^ k)` for all prime powers, and a way to extend `P a` and `P b` to
`P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. -/
@[elab_as_eliminator]
def rec_on_prime_coprime {P : β β Sort*} (h0 : P 0) (hp : β p n : β, prime p β P (p ^ n))
(h : β a b, 0 < a β 0 < b β coprime a b β P a β P b β P (a * b)) : β a, P a :=
rec_on_pos_prime_coprime (Ξ» p n h _, hp p n h) h0 (hp 2 0 prime_two) h
/-- Given `P 0`, `P 1`, `P p` for all primes, and a proof that you can extend
`P a` and `P b` to `P (a * b)`, you can define `P` for all natural numbers. -/
@[elab_as_eliminator]
def rec_on_mul {P : β β Sort*} (h0 : P 0) (h1 : P 1)
(hp : β p, prime p β P p) (h : β a b, P a β P b β P (a * b)) : β a, P a :=
let hp : β p n : β, prime p β P (p ^ n) :=
Ξ» p n hp', match n with
| 0 := h1
| (n+1) := by exact h _ _ (hp p hp') (_match _)
end in
rec_on_prime_coprime h0 hp $ Ξ» a b _ _ _, h a b
end nat
|
805dd414cf52f6a37e470914357308c4aee9911e | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/ring_theory/multiplicity.lean | 726f85cd2129e0643f64446527e7a1409982194e | [
"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 | 19,086 | 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 roption
open_locale big_operators
/-- `multiplicity a b` returns the largest natural number `n` such that
`a ^ n β£ b`, as an `enat` or natural with infinity. If `β n, a ^ n β£ b`,
then it returns `β€`-/
def multiplicity [comm_monoid Ξ±] [decidable_rel ((β£) : Ξ± β Ξ± β Prop)] (a b : Ξ±) : enat :=
enat.find $ Ξ» n, Β¬a ^ (n + 1) β£ b
namespace multiplicity
section comm_monoid
variables [comm_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
@[norm_cast]
theorem int.coe_nat_multiplicity (a b : β) :
multiplicity (a : β€) (b : β€) = multiplicity a b :=
begin
apply roption.ext',
{ repeat {rw [β finite_iff_dom, finite_def]},
norm_cast },
{ intros h1 h2,
apply _root_.le_antisymm; { apply nat.find_le, norm_cast, simp }}
end
lemma not_finite_iff_forall {a b : Ξ±} : (Β¬ finite a b) β β n : β, a ^ n β£ b :=
β¨Ξ» h n, nat.cases_on n (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 mt (is_unit_iff_forall_dvd.1 β is_unit.pow (n + 1)) $
Ξ» h, hn (h b)
lemma finite_of_finite_mul_left {a b c : Ξ±} : finite a (b * c) β finite a c :=
Ξ» β¨n, hnβ©, β¨n, Ξ» h, hn (dvd.trans h (by simp [mul_pow]))β©
lemma finite_of_finite_mul_right {a b c : Ξ±} : finite a (b * c) β finite a b :=
by rw mul_comm; exact finite_of_finite_mul_left
variable [decidable_rel ((β£) : Ξ± β Ξ± β Prop)]
lemma pow_dvd_of_le_multiplicity {a b : Ξ±} {k : β} : (k : enat) β€ multiplicity a b β a ^ k β£ b :=
nat.cases_on k (Ξ» _, 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 enat.coe_get)
lemma is_greatest {a b : Ξ±} {m : β} (hm : multiplicity a b < m) : Β¬a ^ m β£ b :=
Ξ» h, by rw [enat.lt_coe_iff] at hm; exact nat.find_spec hm.fst (dvd.trans (pow_dvd_pow _ hm.snd) 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 [β enat.coe_lt_coe, enat.coe_get] at hm)
lemma unique {a b : Ξ±} {k : β} (hk : a ^ k β£ b) (hsucc : Β¬a ^ (k + 1) β£ b) :
(k : enat) = multiplicity a b :=
le_antisymm (le_of_not_gt (Ξ» hk', is_greatest hk' hk)) $
have finite a b, from β¨k, hsuccβ©,
by { rw [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 [β enat.coe_inj, enat.coe_get, unique hk hsucc]
lemma le_multiplicity_of_pow_dvd {a b : Ξ±}
{k : β} (hk : a ^ k β£ b) : (k : 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 : 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 : enat) β Β¬ a ^ k β£ b :=
by { rw [pow_dvd_iff_le_multiplicity, not_le] }
lemma eq_some_iff {a b : Ξ±} {n : β} :
multiplicity a b = (n : enat) β a ^ n β£ b β§ Β¬a ^ (n + 1) β£ b :=
β¨Ξ» h, let β¨hβ, hββ© := eq_some_iff.1 h in
hβ βΈ β¨pow_multiplicity_dvd _, is_greatest
(by { rw [enat.lt_coe_iff], exact β¨hβ, lt_succ_self _β© })β©,
Ξ» h, eq_some_iff.2 β¨β¨n, h.2β©, eq.symm $ unique' h.1 h.2β©β©
lemma eq_top_iff {a b : Ξ±} :
multiplicity a b = β€ β β n : β, a ^ n β£ b :=
(enat.find_eq_top_iff _).trans $
by { simp only [not_not], exact β¨Ξ» h n, nat.cases_on n (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_iff_forall_dvd.1 (ha.pow _) _)
lemma is_unit_right {a b : Ξ±} (ha : Β¬is_unit a) (hb : is_unit b) :
multiplicity a b = 0 :=
eq_some_iff.2 β¨by simp, by { rw pow_one, exact Ξ» h, mt (is_unit_of_dvd_unit h) ha hb, }β©
@[simp] lemma one_left (b : Ξ±) : multiplicity 1 b = β€ := is_unit_left b is_unit_one
lemma one_right {a : Ξ±} (ha : Β¬is_unit a) : multiplicity a 1 = 0 := is_unit_right ha is_unit_one
@[simp] lemma get_one_right {a : Ξ±} (ha : finite a 1) : get (multiplicity a 1) ha = 0 :=
get_eq_iff_eq_some.2 (eq_some_iff.2 β¨dvd_refl _,
by simpa [is_unit_iff_dvd_one.symm] using not_unit_of_finite haβ©)
@[simp] lemma unit_left (a : Ξ±) (u : units Ξ±) : multiplicity (u : Ξ±) a = β€ :=
is_unit_left a u.is_unit
lemma unit_right {a : Ξ±} (ha : Β¬is_unit a) (u : units Ξ±) : multiplicity a u = 0 :=
is_unit_right ha u.is_unit
lemma multiplicity_eq_zero_of_not_dvd {a b : Ξ±} (ha : Β¬a β£ b) : multiplicity a b = 0 :=
eq_some_iff.2 (by simpa)
lemma eq_top_iff_not_finite {a b : Ξ±} : multiplicity a b = β€ β Β¬ finite a b :=
roption.eq_none_iff'
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 [β 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_le_multiplicity_of_dvd_left {a b c : Ξ±} (hdvd : a β£ b) :
multiplicity b c β€ multiplicity a c :=
multiplicity_le_multiplicity_iff.2 $ Ξ» n h, dvd_trans (pow_dvd_pow_of_dvd hdvd n) 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 (dvd_of_associated h))
(multiplicity_le_multiplicity_of_dvd_left (dvd_of_associated h.symm))
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, dvd.trans hb 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 (dvd_of_associated h))
(multiplicity_le_multiplicity_of_dvd_right (dvd_of_associated h.symm))
lemma dvd_of_multiplicity_pos {a b : Ξ±} (h : (0 : enat) < multiplicity a b) : a β£ b :=
by rw [β pow_one a]; exact pow_dvd_of_le_multiplicity (enat.pos_iff_one_le.1 h)
lemma dvd_iff_multiplicity_pos {a b : Ξ±} : (0 : 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, from heq βΈ 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, nat.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
end comm_monoid
section comm_monoid_with_zero
variable [comm_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 = β€ :=
roption.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 :=
begin
apply multiplicity.multiplicity_eq_zero_of_not_dvd,
rwa zero_dvd_iff,
end
end comm_monoid_with_zero
section comm_semiring
variables [comm_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 comm_semiring
section comm_ring
variables [comm_ring Ξ±] [decidable_rel ((β£) : Ξ± β Ξ± β Prop)]
open_locale classical
@[simp] protected lemma neg (a b : Ξ±) : multiplicity a (-b) = multiplicity a b :=
roption.ext' (by simp only [multiplicity, enat.find, dvd_neg])
(Ξ» hβ hβ, enat.coe_inj.1 (by rw [enat.coe_get]; exact
eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _))
(mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _))))))
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 enat.le_of_lt_add_one,
cases enat.ne_top_iff.mp (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, enat.coe_add, β hk], exact 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 comm_ring
section comm_cancel_monoid_with_zero
variables [comm_cancel_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 nat.sub_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 [nat.sub_add_cancel hn0] at hy;
simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]β©),
have 1 β€ n + m, from le_trans hn0 (le_add_right n m),
finite_mul_aux hpx hb β¨s, mul_right_cancel' hp.1 begin
rw [β nat.sub_add_comm hn0, nat.sub_add_cancel 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 nat.sub_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 [nat.sub_add_cancel 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, nat.sub_add_cancel 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 :=
eq_some_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 :=
roption.get_eq_iff_eq_some.2 (eq_some_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, not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _))
(by exact succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp hdiva hdivb h),
by rw [β enat.coe_inj, enat.coe_get, eq_some_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 [β enat.coe_get (finite_iff_dom.1 h.1), β enat.coe_get (finite_iff_dom.1 h.2),
β enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)),
β enat.coe_add, 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 dsimp [pow_zero]; simp [one_right hp.not_unit]; refl
| (k+1) := by dsimp only [pow_succ];
erw [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_some_iff], use dvd_refl _, 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 comm_cancel_monoid_with_zero
section valuation
variables {R : Type*} [integral_domain R] {p : R} [decidable_rel (has_dvd.dvd : R β R β Prop)]
/-- `multiplicity` of a prime inan integral domain as an additive valuation to `enat`. -/
noncomputable def add_valuation (hp : prime p) : add_valuation R 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, enat.pos_iff_one_le, β enat.coe_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
|
c1c8ba2641f77228ae89651078924aa52c3bf72f | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/algebra/category/Module/basic.lean | 7d06f4ac61a6eba3f217de87ce0d9183527b4750 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,789 | lean | /-
Copyright (c) 2019 Robert A. Spencer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert A. Spencer, Markus Himmel
-/
import algebra.category.Group.basic
import category_theory.concrete_category
import category_theory.limits.shapes.kernels
import category_theory.linear
import linear_algebra.basic
/-!
# The category of `R`-modules
`Module.{v} R` is the category of bundled `R`-modules with carrier in the universe `v`. We show
that it is preadditive and show that being an isomorphism, monomorphism and epimorphism is
equivalent to being a linear equivalence, an injective linear map and a surjective linear map,
respectively.
## Implementation details
To construct an object in the category of `R`-modules from a type `M` with an instance of the
`module` typeclass, write `of R M`. There is a coercion in the other direction.
Similarly, there is a coercion from morphisms in `Module R` to linear maps.
Unfortunately, Lean is not smart enough to see that, given an object `M : Module R`, the expression
`of R M`, where we coerce `M` to the carrier type, is definitionally equal to `M` itself.
This means that to go the other direction, i.e., from linear maps/equivalences to (iso)morphisms
in the category of `R`-modules, we have to take care not to inadvertently end up with an
`of R M` where `M` is already an object. Hence, given `f : M ββ[R] N`,
* if `M N : Module R`, simply use `f`;
* if `M : Module R` and `N` is an unbundled `R`-module, use `βΏf` or `as_hom_left f`;
* if `M` is an unbundled `R`-module and `N : Module R`, use `βΎf` or `as_hom_right f`;
* if `M` and `N` are unbundled `R`-modules, use `βf` or `as_hom f`.
Similarly, given `f : M ββ[R] N`, use `to_Module_iso`, `to_Module_iso'_left`, `to_Module_iso'_right`
or `to_Module_iso'`, respectively.
The arrow notations are localized, so you may have to `open_locale Module` to use them. Note that
the notation for `as_hom_left` clashes with the notation used to promote functions between types to
morphisms in the category `Type`, so to avoid confusion, it is probably a good idea to avoid having
the locales `Module` and `category_theory.Type` open at the same time.
If you get an error when trying to apply a theorem and the `convert` tactic produces goals of the
form `M = of R M`, then you probably used an incorrect variant of `as_hom` or `to_Module_iso`.
-/
open category_theory
open category_theory.limits
open category_theory.limits.walking_parallel_pair
universes v u
variables (R : Type u) [ring R]
/-- The category of R-modules and their morphisms.
Note that in the case of `R = β€`, we can not
impose here that the `β€`-multiplication field from the module structure is defeq to the one coming
from the `is_add_comm_group` structure (contrary to what we do for all module structures in
mathlib), which creates some difficulties down the road. -/
structure Module :=
(carrier : Type v)
[is_add_comm_group : add_comm_group carrier]
[is_module : module R carrier]
attribute [instance] Module.is_add_comm_group Module.is_module
namespace Module
instance : has_coe_to_sort (Module.{v} R) :=
{ S := Type v, coe := Module.carrier }
instance Module_category : category (Module.{v} R) :=
{ hom := Ξ» M N, M ββ[R] N,
id := Ξ» M, 1,
comp := Ξ» A B C f g, g.comp f,
id_comp' := Ξ» X Y f, linear_map.id_comp _,
comp_id' := Ξ» X Y f, linear_map.comp_id _,
assoc' := Ξ» W X Y Z f g h, linear_map.comp_assoc _ _ _ }
instance Module_concrete_category : concrete_category.{v} (Module.{v} R) :=
{ forget := { obj := Ξ» R, R, map := Ξ» R S f, (f : R β S) },
forget_faithful := { } }
instance has_forget_to_AddCommGroup : has_forgetβ (Module R) AddCommGroup :=
{ forgetβ :=
{ obj := Ξ» M, AddCommGroup.of M,
map := Ξ» Mβ Mβ f, linear_map.to_add_monoid_hom f } }
/-- The object in the category of R-modules associated to an R-module -/
def of (X : Type v) [add_comm_group X] [module R X] : Module R := β¨Xβ©
/-- Typecheck a `linear_map` as a morphism in `Module R`. -/
def of_hom {R : Type u} [ring R] {X Y : Type u} [add_comm_group X] [module R X] [add_comm_group Y]
[module R Y] (f : X ββ[R] Y) : of R X βΆ of R Y := f
instance : has_zero (Module R) := β¨of R punitβ©
instance : inhabited (Module R) := β¨0β©
@[simp]
lemma coe_of (X : Type u) [add_comm_group X] [module R X] : (of R X : Type u) = X := rfl
variables {R}
/-- Forgetting to the underlying type and then building the bundled object returns the original
module. -/
@[simps]
def of_self_iso (M : Module R) : Module.of R M β
M :=
{ hom := π M, inv := π M }
instance : subsingleton (of R punit) :=
by { rw coe_of R punit, apply_instance }
instance : has_zero_object (Module.{v} R) :=
{ zero := 0,
unique_to := Ξ» X,
{ default := (0 : punit ββ[R] X),
uniq := Ξ» _, linear_map.ext $ Ξ» x,
have h : x = 0, from dec_trivial,
by simp only [h, linear_map.map_zero]},
unique_from := Ξ» X,
{ default := (0 : X ββ[R] punit),
uniq := Ξ» _, linear_map.ext $ Ξ» x, dec_trivial } }
variables {R} {M N U : Module.{v} R}
@[simp] lemma id_apply (m : M) : (π M : M β M) m = m := rfl
@[simp] lemma coe_comp (f : M βΆ N) (g : N βΆ U) :
((f β« g) : M β U) = g β f := rfl
lemma comp_def (f : M βΆ N) (g : N βΆ U) : f β« g = g.comp f := rfl
end Module
variables {R}
variables {Xβ Xβ : Type v}
/-- Reinterpreting a linear map in the category of `R`-modules. -/
def Module.as_hom [add_comm_group Xβ] [module R Xβ] [add_comm_group Xβ] [module R Xβ] :
(Xβ ββ[R] Xβ) β (Module.of R Xβ βΆ Module.of R Xβ) := id
localized "notation `β` f : 1024 := Module.as_hom f" in Module
/-- Reinterpreting a linear map in the category of `R`-modules. -/
def Module.as_hom_right [add_comm_group Xβ] [module R Xβ] {Xβ : Module.{v} R} :
(Xβ ββ[R] Xβ) β (Module.of R Xβ βΆ Xβ) := id
localized "notation `βΎ` f : 1024 := Module.as_hom_right f" in Module
/-- Reinterpreting a linear map in the category of `R`-modules. -/
def Module.as_hom_left {Xβ : Module.{v} R} [add_comm_group Xβ] [module R Xβ] :
(Xβ ββ[R] Xβ) β (Xβ βΆ Module.of R Xβ) := id
localized "notation `βΏ` f : 1024 := Module.as_hom_left f" in Module
/-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/
@[simps]
def linear_equiv.to_Module_iso
{gβ : add_comm_group Xβ} {gβ : add_comm_group Xβ} {mβ : module R Xβ} {mβ : module R Xβ}
(e : Xβ ββ[R] Xβ) :
Module.of R Xβ β
Module.of R Xβ :=
{ hom := (e : Xβ ββ[R] Xβ),
inv := (e.symm : Xβ ββ[R] Xβ),
hom_inv_id' := begin ext, exact e.left_inv x, end,
inv_hom_id' := begin ext, exact e.right_inv x, end, }
/--
Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s.
This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see
`Module.of R M` is defeq to `M` when `M : Module R`. -/
@[simps]
def linear_equiv.to_Module_iso' {M N : Module.{v} R} (i : M ββ[R] N) : M β
N :=
{ hom := i,
inv := i.symm,
hom_inv_id' := linear_map.ext $ Ξ» x, by simp,
inv_hom_id' := linear_map.ext $ Ξ» x, by simp }
/--
Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s.
This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see
`Module.of R M` is defeq to `M` when `M : Module R`. -/
@[simps]
def linear_equiv.to_Module_iso'_left {Xβ : Module.{v} R} {gβ : add_comm_group Xβ} {mβ : module R Xβ}
(e : Xβ ββ[R] Xβ) : Xβ β
Module.of R Xβ :=
{ hom := (e : Xβ ββ[R] Xβ),
inv := (e.symm : Xβ ββ[R] Xβ),
hom_inv_id' := linear_map.ext $ Ξ» x, by simp,
inv_hom_id' := linear_map.ext $ Ξ» x, by simp }
/--
Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s.
This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see
`Module.of R M` is defeq to `M` when `M : Module R`. -/
@[simps]
def linear_equiv.to_Module_iso'_right {gβ : add_comm_group Xβ} {mβ : module R Xβ}
{Xβ : Module.{v} R} (e : Xβ ββ[R] Xβ) : Module.of R Xβ β
Xβ :=
{ hom := (e : Xβ ββ[R] Xβ),
inv := (e.symm : Xβ ββ[R] Xβ),
hom_inv_id' := linear_map.ext $ Ξ» x, by simp,
inv_hom_id' := linear_map.ext $ Ξ» x, by simp }
namespace category_theory.iso
/-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/
@[simps]
def to_linear_equiv {X Y : Module R} (i : X β
Y) : X ββ[R] Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_smul' := by tidy, }.
end category_theory.iso
/-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms
in `Module` -/
@[simps]
def linear_equiv_iso_Module_iso {X Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X]
[module R Y] :
(X ββ[R] Y) β
(Module.of R X β
Module.of R Y) :=
{ hom := Ξ» e, e.to_Module_iso,
inv := Ξ» i, i.to_linear_equiv, }
namespace Module
instance : preadditive (Module.{v} R) :=
{ add_comp' := Ξ» P Q R f f' g,
show (f + f') β« g = f β« g + f' β« g, by { ext, simp },
comp_add' := Ξ» P Q R f g g',
show f β« (g + g') = f β« g + f β« g', by { ext, simp } }
section
variables {S : Type u} [comm_ring S]
instance : linear S (Module.{v} S) :=
{ hom_module := Ξ» X Y, linear_map.module,
smul_comp' := by { intros, ext, simp },
comp_smul' := by { intros, ext, simp }, }
end
end Module
instance (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) :=
β¨ Ξ» N, Module.of R N β©
|
50d2d66f3aef49edc8ac60f3eb970a08f7e7b170 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/algebra/group_power.lean | bafe98b2a2250204e19b03afcba6806bb17cd63f | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 25,653 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis
The power operation on monoids and groups. We separate this from group, because it depends on
nat, which in turn depends on other parts of algebra.
We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation
a^n is used for the first, but users can locally redefine it to gpow when needed.
Note: power adopts the convention that 0^0=1.
-/
import algebra.group
import data.int.basic data.list.basic
universes u v
variable {Ξ± : Type u}
@[simp] theorem inv_one [division_ring Ξ±] : (1β»ΒΉ : Ξ±) = 1 := by rw [inv_eq_one_div, one_div_one]
@[simp] theorem inv_inv' [discrete_field Ξ±] {a:Ξ±} : aβ»ΒΉβ»ΒΉ = a :=
by rw [inv_eq_one_div, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_one]
/-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/
def monoid.pow [monoid Ξ±] (a : Ξ±) : β β Ξ±
| 0 := 1
| (n+1) := a * monoid.pow n
def add_monoid.smul [add_monoid Ξ±] (n : β) (a : Ξ±) : Ξ± :=
@monoid.pow (multiplicative Ξ±) _ a n
precedence `β’`:70
local infix ` β’ ` := add_monoid.smul
@[priority 5] instance monoid.has_pow [monoid Ξ±] : has_pow Ξ± β := β¨monoid.powβ©
/- monoid -/
section monoid
variables [monoid Ξ±] {Ξ² : Type u} [add_monoid Ξ²]
@[simp] theorem pow_zero (a : Ξ±) : a^0 = 1 := rfl
@[simp] theorem add_monoid.zero_smul (a : Ξ²) : 0 β’ a = 0 := rfl
theorem pow_succ (a : Ξ±) (n : β) : a^(n+1) = a * a^n := rfl
theorem succ_smul (a : Ξ²) (n : β) : (n+1)β’a = a + nβ’a := rfl
@[simp] theorem pow_one (a : Ξ±) : a^1 = a := mul_one _
@[simp] theorem add_monoid.one_smul (a : Ξ²) : 1β’a = a := add_zero _
theorem pow_mul_comm' (a : Ξ±) (n : β) : a^n * a = a * a^n :=
by induction n with n ih; [rw [pow_zero, one_mul, mul_one],
rw [pow_succ, mul_assoc, ih]]
theorem smul_add_comm' : β (a : Ξ²) (n : β), nβ’a + a = a + nβ’a :=
@pow_mul_comm' (multiplicative Ξ²) _
theorem pow_succ' (a : Ξ±) (n : β) : a^(n+1) = a^n * a :=
by rw [pow_succ, pow_mul_comm']
theorem succ_smul' (a : Ξ²) (n : β) : (n+1)β’a = nβ’a + a :=
by rw [succ_smul, smul_add_comm']
theorem pow_two (a : Ξ±) : a^2 = a * a :=
show a*(a*1)=a*a, by rw mul_one
theorem two_smul (a : Ξ²) : 2β’a = a + a :=
show a+(a+0)=a+a, by rw add_zero
theorem pow_add (a : Ξ±) (m n : β) : a^(m + n) = a^m * a^n :=
by induction n with n ih; [rw [add_zero, pow_zero, mul_one],
rw [pow_succ, β pow_mul_comm', β mul_assoc, β ih, β pow_succ']]; refl
theorem add_monoid.add_smul : β (a : Ξ²) (m n : β), (m + n)β’a = mβ’a + nβ’a :=
@pow_add (multiplicative Ξ²) _
@[simp] theorem one_pow (n : β) : (1 : Ξ±)^n = (1:Ξ±) :=
by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]]
@[simp] theorem add_monoid.smul_zero (n : β) : nβ’(0 : Ξ²) = (0:Ξ²) :=
by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]]
theorem pow_mul (a : Ξ±) (m n : β) : a^(m * n) = (a^m)^n :=
by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl
theorem add_monoid.mul_smul' : β (a : Ξ²) (m n : β), m * n β’ a = nβ’(mβ’a) :=
@pow_mul (multiplicative Ξ²) _
theorem pow_mul' (a : Ξ±) (m n : β) : a^(m * n) = (a^n)^m :=
by rw [mul_comm, pow_mul]
theorem add_monoid.mul_smul (a : Ξ²) (m n : β) : m * n β’ a = mβ’(nβ’a) :=
by rw [mul_comm, add_monoid.mul_smul']
@[simp] theorem add_monoid.smul_one [has_one Ξ²] : β n : β, n β’ (1 : Ξ²) = n :=
nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _)
theorem pow_bit0 (a : Ξ±) (n : β) : a ^ bit0 n = a^n * a^n := pow_add _ _ _
theorem bit0_smul (a : Ξ²) (n : β) : bit0 n β’ a = nβ’a + nβ’a := add_monoid.add_smul _ _ _
theorem pow_bit1 (a : Ξ±) (n : β) : a ^ bit1 n = a^n * a^n * a :=
by rw [bit1, pow_succ', pow_bit0]
theorem bit1_smul : β (a : Ξ²) (n : β), bit1 n β’ a = nβ’a + nβ’a + a :=
@pow_bit1 (multiplicative Ξ²) _
theorem pow_mul_comm (a : Ξ±) (m n : β) : a^m * a^n = a^n * a^m :=
by rw [βpow_add, βpow_add, add_comm]
theorem smul_add_comm : β (a : Ξ²) (m n : β), mβ’a + nβ’a = nβ’a + mβ’a :=
@pow_mul_comm (multiplicative Ξ²) _
@[simp] theorem list.prod_repeat (a : Ξ±) (n : β) : (list.repeat a n).prod = a ^ n :=
by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl
@[simp] theorem list.sum_repeat : β (a : Ξ²) (n : β), (list.repeat a n).sum = n β’ a :=
@list.prod_repeat (multiplicative Ξ²) _
@[simp] lemma units.coe_pow (u : units Ξ±) (n : β) : ((u ^ n : units Ξ±) : Ξ±) = u ^ n :=
by induction n; simp [*, pow_succ]
end monoid
namespace is_monoid_hom
variables {Ξ² : Type v} [monoid Ξ±] [monoid Ξ²] (f : Ξ± β Ξ²) [is_monoid_hom f]
theorem map_pow (a : Ξ±) : β(n : β), f (a ^ n) = (f a) ^ n
| 0 := is_monoid_hom.map_one f
| (nat.succ n) := by rw [pow_succ, is_monoid_hom.map_mul f, map_pow n]; refl
end is_monoid_hom
namespace is_add_monoid_hom
variables {Ξ² : Type*} [add_monoid Ξ±] [add_monoid Ξ²] (f : Ξ± β Ξ²) [is_add_monoid_hom f]
theorem map_smul (a : Ξ±) : β(n : β), f (n β’ a) = n β’ (f a)
| 0 := is_add_monoid_hom.map_zero f
| (nat.succ n) := by rw [succ_smul, is_add_monoid_hom.map_add f, map_smul n]; refl
end is_add_monoid_hom
@[simp] theorem nat.pow_eq_pow (p q : β) :
@has_pow.pow _ _ monoid.has_pow p q = p ^ q :=
by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]]
@[simp] theorem nat.smul_eq_mul (m n : β) : m β’ n = m * n :=
by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul],
rw [succ_smul', ih, nat.succ_mul]]
/- commutative monoid -/
section comm_monoid
variables [comm_monoid Ξ±] {Ξ² : Type*} [add_comm_monoid Ξ²]
theorem mul_pow (a b : Ξ±) (n : β) : (a * b)^n = a^n * b^n :=
by induction n with n ih; [exact (mul_one _).symm,
simp only [pow_succ, ih, mul_assoc, mul_left_comm]]
theorem add_monoid.smul_add : β (a b : Ξ²) (n : β), nβ’(a + b) = nβ’a + nβ’b :=
@mul_pow (multiplicative Ξ²) _
instance pow.is_monoid_hom (n : β) : is_monoid_hom ((^ n) : Ξ± β Ξ±) :=
{ map_mul := Ξ» _ _, mul_pow _ _ _, map_one := one_pow _ }
instance add_monoid.smul.is_add_monoid_hom (n : β) : is_add_monoid_hom (add_monoid.smul n : Ξ² β Ξ²) :=
{ map_add := Ξ» _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ }
end comm_monoid
section group
variables [group Ξ±] {Ξ² : Type*} [add_group Ξ²]
section nat
@[simp] theorem inv_pow (a : Ξ±) (n : β) : (aβ»ΒΉ)^n = (a^n)β»ΒΉ :=
by induction n with n ih; [exact one_inv.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev]]
@[simp] theorem add_monoid.neg_smul : β (a : Ξ²) (n : β), nβ’(-a) = -(nβ’a) :=
@inv_pow (multiplicative Ξ²) _
theorem pow_sub (a : Ξ±) {m n : β} (h : m β₯ n) : 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_mul_inv_of_mul_eq h2
theorem add_monoid.smul_sub : β (a : Ξ²) {m n : β}, m β₯ n β (m - n)β’a = mβ’a - nβ’a :=
@pow_sub (multiplicative Ξ²) _
theorem pow_inv_comm (a : Ξ±) (m n : β) : (aβ»ΒΉ)^m * a^n = a^n * (aβ»ΒΉ)^m :=
by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _)
theorem add_monoid.smul_neg_comm : β (a : Ξ²) (m n : β), mβ’(-a) + nβ’a = nβ’a + mβ’(-a) :=
@pow_inv_comm (multiplicative Ξ²) _
end nat
open int
/--
The power operation in a group. This extends `monoid.pow` to negative integers
with the definition `a^(-n) = (a^n)β»ΒΉ`.
-/
def gpow (a : Ξ±) : β€ β Ξ±
| (of_nat n) := a^n
| -[1+n] := (a^(nat.succ n))β»ΒΉ
def gsmul (n : β€) (a : Ξ²) : Ξ² :=
@gpow (multiplicative Ξ²) _ a n
@[priority 10] instance group.has_pow : has_pow Ξ± β€ := β¨gpowβ©
local infix ` β’ `:70 := gsmul
local infix ` β’β `:70 := add_monoid.smul
@[simp] theorem gpow_coe_nat (a : Ξ±) (n : β) : a ^ (n:β€) = a ^ n := rfl
@[simp] theorem gsmul_coe_nat (a : Ξ²) (n : β) : (n:β€) β’ a = n β’β a := rfl
@[simp] theorem gpow_of_nat (a : Ξ±) (n : β) : a ^ of_nat n = a ^ n := rfl
@[simp] theorem gsmul_of_nat (a : Ξ²) (n : β) : of_nat n β’ a = n β’β a := rfl
@[simp] theorem gpow_neg_succ (a : Ξ±) (n : β) : a ^ -[1+n] = (a ^ n.succ)β»ΒΉ := rfl
@[simp] theorem gsmul_neg_succ (a : Ξ²) (n : β) : -[1+n] β’ a = - (n.succ β’β a) := rfl
local attribute [ematch] le_of_lt
open nat
@[simp] theorem gpow_zero (a : Ξ±) : a ^ (0:β€) = 1 := rfl
@[simp] theorem zero_gsmul (a : Ξ²) : (0:β€) β’ a = 0 := rfl
@[simp] theorem gpow_one (a : Ξ±) : a ^ (1:β€) = a := mul_one _
@[simp] theorem one_gsmul (a : Ξ²) : (1:β€) β’ a = a := add_zero _
@[simp] theorem one_gpow : β (n : β€), (1 : Ξ±) ^ n = 1
| (n : β) := one_pow _
| -[1+ n] := show _β»ΒΉ=(1:Ξ±), by rw [_root_.one_pow, one_inv]
@[simp] theorem gsmul_zero : β (n : β€), n β’ (0 : Ξ²) = 0 :=
@one_gpow (multiplicative Ξ²) _
@[simp] theorem gpow_neg (a : Ξ±) : β (n : β€), a ^ -n = (a ^ n)β»ΒΉ
| (n+1:β) := rfl
| 0 := one_inv.symm
| -[1+ n] := (inv_inv _).symm
@[simp] theorem neg_gsmul : β (a : Ξ²) (n : β€), -n β’ a = -(n β’ a) :=
@gpow_neg (multiplicative Ξ²) _
theorem gpow_neg_one (x : Ξ±) : x ^ (-1:β€) = xβ»ΒΉ := congr_arg has_inv.inv $ pow_one x
theorem neg_one_gsmul (x : Ξ²) : (-1:β€) β’ x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x
theorem inv_gpow (a : Ξ±) : βn:β€, aβ»ΒΉ ^ n = (a ^ n)β»ΒΉ
| (n : β) := inv_pow a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1)
private lemma gpow_add_aux (a : Ξ±) (m n : nat) :
a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] :=
or.elim (nat.lt_or_ge m (nat.succ n))
(assume h1 : m < succ n,
have h2 : m β€ n, from le_of_lt_succ h1,
suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n],
by rwa [of_nat_add_neg_succ_of_nat_of_lt h1],
show (a ^ nat.succ (n - m))β»ΒΉ = a ^ of_nat m * a ^ -[1+n],
by rw [β succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl)
(assume : m β₯ succ n,
suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]),
by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption,
suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)β»ΒΉ, from this,
by rw pow_sub; assumption)
theorem gpow_add (a : Ξ±) : β (i j : β€), a ^ (i + j) = a ^ i * a ^ j
| (of_nat m) (of_nat n) := pow_add _ _ _
| (of_nat m) -[1+n] := gpow_add_aux _ _ _
| -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux,
gpow_neg_succ, gpow_of_nat, β inv_pow, β pow_inv_comm]
| -[1+m] -[1+n] :=
suffices (a ^ (m + succ (succ n)))β»ΒΉ = (a ^ m.succ)β»ΒΉ * (a ^ n.succ)β»ΒΉ, from this,
by rw [β succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev]
theorem add_gsmul : β (a : Ξ²) (i j : β€), (i + j) β’ a = i β’ a + j β’ a :=
@gpow_add (multiplicative Ξ²) _
theorem gpow_add_one (a : Ξ±) (i : β€) : a ^ (i + 1) = a ^ i * a :=
by rw [gpow_add, gpow_one]
theorem add_one_gsmul : β (a : Ξ²) (i : β€), (i + 1) β’ a = i β’ a + a :=
@gpow_add_one (multiplicative Ξ²) _
theorem gpow_one_add (a : Ξ±) (i : β€) : a ^ (1 + i) = a * a ^ i :=
by rw [gpow_add, gpow_one]
theorem one_add_gsmul : β (a : Ξ²) (i : β€), (1 + i) β’ a = a + i β’ a :=
@gpow_one_add (multiplicative Ξ²) _
theorem gpow_mul_comm (a : Ξ±) (i j : β€) : a ^ i * a ^ j = a ^ j * a ^ i :=
by rw [β gpow_add, β gpow_add, add_comm]
theorem gsmul_add_comm : β (a : Ξ²) (i j), i β’ a + j β’ a = j β’ a + i β’ a :=
@gpow_mul_comm (multiplicative Ξ²) _
theorem gpow_mul (a : Ξ±) : β m n : β€, a ^ (m * n) = (a ^ m) ^ n
| (m : β) (n : β) := pow_mul _ _ _
| (m : β) -[1+ n] := (gpow_neg _ (m * succ n)).trans $
show (a ^ (m * succ n))β»ΒΉ = _, by rw pow_mul; refl
| -[1+ m] (n : β) := (gpow_neg _ (succ m * n)).trans $
show (a ^ (m.succ * n))β»ΒΉ = _, by rw [pow_mul, β inv_pow]; refl
| -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $
show _ = (_β»ΒΉ^_)β»ΒΉ, by rw [inv_pow, inv_inv]
theorem gsmul_mul' : β (a : Ξ²) (m n : β€), m * n β’ a = n β’ (m β’ a) :=
@gpow_mul (multiplicative Ξ²) _
theorem gpow_mul' (a : Ξ±) (m n : β€) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, gpow_mul]
theorem gsmul_mul (a : Ξ²) (m n : β€) : m * n β’ a = m β’ (n β’ a) :=
by rw [mul_comm, gsmul_mul']
theorem gpow_bit0 (a : Ξ±) (n : β€) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _
theorem bit0_gsmul (a : Ξ²) (n : β€) : bit0 n β’ a = n β’ a + n β’ a := gpow_add _ _ _
theorem gpow_bit1 (a : Ξ±) (n : β€) : a ^ bit1 n = a ^ n * a ^ n * a :=
by rw [bit1, gpow_add]; simp [gpow_bit0]
theorem bit1_gsmul : β (a : Ξ²) (n : β€), bit1 n β’ a = n β’ a + n β’ a + a :=
@gpow_bit1 (multiplicative Ξ²) _
theorem gsmul_neg (a : Ξ²) (n : β€) : gsmul n (- a) = - gsmul n a :=
begin
induction n using int.induction_on with z ih z ih,
{ simp },
{ rw [add_comm] {occs := occurrences.pos [1]}, simp [add_gsmul, ih, -add_comm] },
{ rw [sub_eq_add_neg, add_comm] {occs := occurrences.pos [1]},
simp [ih, add_gsmul, neg_gsmul, -add_comm] }
end
end group
namespace is_group_hom
variables {Ξ² : Type v} [group Ξ±] [group Ξ²] (f : Ξ± β Ξ²) [is_group_hom f]
theorem map_pow (a : Ξ±) (n : β) : f (a ^ n) = f a ^ n :=
is_monoid_hom.map_pow f a n
theorem map_gpow (a : Ξ±) (n : β€) : f (a ^ n) = f a ^ n :=
by cases n; [exact is_group_hom.map_pow f _ _,
exact (is_group_hom.map_inv f _).trans (congr_arg _ $ is_group_hom.map_pow f _ _)]
end is_group_hom
namespace is_add_group_hom
variables {Ξ² : Type v} [add_group Ξ±] [add_group Ξ²] (f : Ξ± β Ξ²) [is_add_group_hom f]
theorem map_smul (a : Ξ±) (n : β) : f (n β’ a) = n β’ f a :=
is_add_monoid_hom.map_smul f a n
theorem map_gsmul (a : Ξ±) (n : β€) : f (gsmul n a) = gsmul n (f a) :=
@is_group_hom.map_gpow (multiplicative Ξ±) (multiplicative Ξ²) _ _ f _ a n
end is_add_group_hom
local infix ` β’β€ `:70 := gsmul
section comm_monoid
variables [comm_group Ξ±] {Ξ² : Type*} [add_comm_group Ξ²]
theorem mul_gpow (a b : Ξ±) : β n:β€, (a * b)^n = a^n * b^n
| (n : β) := mul_pow a b n
| -[1+ n] := show _β»ΒΉ=_β»ΒΉ*_β»ΒΉ, by rw [mul_pow, mul_inv_rev, mul_comm]
theorem gsmul_add : β (a b : Ξ²) (n : β€), n β’β€ (a + b) = n β’β€ a + n β’β€ b :=
@mul_gpow (multiplicative Ξ²) _
theorem gsmul_sub : β (a b : Ξ²) (n : β€), gsmul n (a - b) = gsmul n a - gsmul n b :=
by simp [gsmul_add, gsmul_neg]
instance gpow.is_group_hom (n : β€) : is_group_hom ((^ n) : Ξ± β Ξ±) :=
{ map_mul := Ξ» _ _, mul_gpow _ _ n }
instance gsmul.is_add_group_hom (n : β€) : is_add_group_hom (gsmul n : Ξ² β Ξ²) :=
{ map_add := Ξ» _ _, gsmul_add _ _ n }
end comm_monoid
section group
@[instance]
theorem is_add_group_hom.gsmul
{Ξ± Ξ²} [add_group Ξ±] [add_comm_group Ξ²] (f : Ξ± β Ξ²) [is_add_group_hom f] (z : β€) :
is_add_group_hom (Ξ»a, gsmul z (f a)) :=
{ map_add := assume a b, by rw [is_add_hom.map_add f, gsmul_add] }
end group
@[simp] lemma with_bot.coe_smul [add_monoid Ξ±] (a : Ξ±) (n : β) :
((add_monoid.smul n a : Ξ±) : with_bot Ξ±) = add_monoid.smul n a :=
by induction n; simp [*, succ_smul]; refl
theorem add_monoid.smul_eq_mul' [semiring Ξ±] (a : Ξ±) (n : β) : n β’ a = a * n :=
by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero],
rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]]
theorem add_monoid.smul_eq_mul [semiring Ξ±] (n : β) (a : Ξ±) : n β’ a = n * a :=
by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm]
theorem add_monoid.mul_smul_left [semiring Ξ±] (a b : Ξ±) (n : β) : n β’ (a * b) = a * (n β’ b) :=
by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc]
theorem add_monoid.mul_smul_assoc [semiring Ξ±] (a b : Ξ±) (n : β) : n β’ (a * b) = n β’ a * b :=
by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc]
lemma zero_pow [semiring Ξ±] : β {n : β}, 0 < n β (0 : Ξ±) ^ n = 0
| (n+1) _ := zero_mul _
@[simp, move_cast] theorem nat.cast_pow [semiring Ξ±] (n m : β) : (β(n ^ m) : Ξ±) = βn ^ m :=
by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]]
@[simp, move_cast] theorem int.coe_nat_pow (n m : β) : ((n ^ m : β) : β€) = n ^ m :=
by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]]
theorem int.nat_abs_pow (n : β€) (k : β) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k :=
by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]]
theorem is_semiring_hom.map_pow {Ξ²} [semiring Ξ±] [semiring Ξ²]
(f : Ξ± β Ξ²) [is_semiring_hom f] (x : Ξ±) (n : β) : f (x ^ n) = f x ^ n :=
by induction n with n ih; [exact is_semiring_hom.map_one f,
rw [pow_succ, pow_succ, is_semiring_hom.map_mul f, ih]]
theorem neg_one_pow_eq_or {R} [ring R] : β n : β, (-1 : R)^n = 1 β¨ (-1 : R)^n = -1
| 0 := or.inl rfl
| (n+1) := (neg_one_pow_eq_or n).swap.imp
(Ξ» h, by rw [pow_succ, h, neg_one_mul, neg_neg])
(Ξ» h, by rw [pow_succ, h, mul_one])
lemma pow_dvd_pow [comm_semiring Ξ±] (a : Ξ±) {m n : β} (h : m β€ n) :
a ^ m β£ a ^ n := β¨a ^ (n - m), by rw [β pow_add, nat.add_sub_cancel' h]β©
theorem gsmul_eq_mul [ring Ξ±] (a : Ξ±) : β n, n β’β€ a = n * a
| (n : β) := add_monoid.smul_eq_mul _ _
| -[1+ n] := show -(_β’_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ]
theorem gsmul_eq_mul' [ring Ξ±] (a : Ξ±) (n : β€) : n β’β€ a = a * n :=
by rw [gsmul_eq_mul, int.mul_cast_comm]
theorem mul_gsmul_left [ring Ξ±] (a b : Ξ±) (n : β€) : n β’β€ (a * b) = a * (n β’β€ b) :=
by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc]
theorem mul_gsmul_assoc [ring Ξ±] (a b : Ξ±) (n : β€) : n β’β€ (a * b) = n β’β€ a * b :=
by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc]
@[simp, move_cast] theorem int.cast_pow [ring Ξ±] (n : β€) (m : β) : (β(n ^ m) : Ξ±) = βn ^ m :=
by induction m with m ih; [exact int.cast_one,
rw [pow_succ, pow_succ, int.cast_mul, ih]]
lemma neg_one_pow_eq_pow_mod_two [ring Ξ±] {n : β} : (-1 : Ξ±) ^ n = -1 ^ (n % 2) :=
by rw [β nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two]
theorem sq_sub_sq [comm_ring Ξ±] (a b : Ξ±) : a ^ 2 - b ^ 2 = (a + b) * (a - b) :=
by rw [pow_two, pow_two, mul_self_sub_mul_self]
theorem pow_eq_zero [domain Ξ±] {x : Ξ±} {n : β} (H : x^n = 0) : x = 0 :=
begin
induction n with n ih,
{ rw pow_zero at H,
rw [β mul_one x, H, mul_zero] },
exact or.cases_on (mul_eq_zero.1 H) id ih
end
theorem pow_ne_zero [domain Ξ±] {a : Ξ±} (n : β) (h : a β 0) : a ^ n β 0 :=
mt pow_eq_zero h
@[simp] theorem one_div_pow [division_ring Ξ±] {a : Ξ±} (ha : a β 0) (n : β) : (1 / a) ^ n = 1 / a ^ n :=
by induction n with n ih; [exact (div_one _).symm,
rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl
@[simp] theorem division_ring.inv_pow [division_ring Ξ±] {a : Ξ±} (ha : a β 0) (n : β) : aβ»ΒΉ ^ n = (a ^ n)β»ΒΉ :=
by simpa only [inv_eq_one_div] using one_div_pow ha n
@[simp] theorem div_pow [field Ξ±] (a : Ξ±) {b : Ξ±} (hb : b β 0) (n : β) : (a / b) ^ n = a ^ n / b ^ n :=
by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, β div_eq_mul_one_div]
theorem add_monoid.smul_nonneg [ordered_comm_monoid Ξ±] {a : Ξ±} (H : 0 β€ a) : β n : β, 0 β€ n β’ a
| 0 := le_refl _
| (n+1) := add_nonneg' H (add_monoid.smul_nonneg n)
lemma pow_abs [decidable_linear_ordered_comm_ring Ξ±] (a : Ξ±) (n : β) : (abs a)^n = abs (a^n) :=
by induction n with n ih; [exact (abs_one).symm,
rw [pow_succ, pow_succ, ih, abs_mul]]
lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring Ξ±] (n : β) : abs ((-1 : Ξ±)^n) = 1 :=
by rw [βpow_abs, abs_neg, abs_one, one_pow]
lemma inv_pow' [discrete_field Ξ±] (a : Ξ±) (n : β) : (a ^ n)β»ΒΉ = aβ»ΒΉ ^ n :=
by induction n; simp [*, pow_succ, mul_inv', mul_comm]
lemma pow_inv [division_ring Ξ±] (a : Ξ±) : β n : β, a β 0 β (a^n)β»ΒΉ = (aβ»ΒΉ)^n
| 0 ha := inv_one
| (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha]
namespace add_monoid
variable [ordered_comm_monoid Ξ±]
theorem smul_le_smul {a : Ξ±} {n m : β} (ha : 0 β€ a) (h : n β€ m) : n β’ a β€ m β’ a :=
let β¨k, hkβ© := nat.le.dest h in
calc n β’ a = n β’ a + 0 : (add_zero _).symm
... β€ n β’ a + k β’ a : add_le_add_left' (smul_nonneg ha _)
... = m β’ a : by rw [β hk, add_smul]
lemma smul_le_smul_of_le_right {a b : Ξ±} (hab : a β€ b) : β i : β, i β’ a β€ i β’ b
| 0 := by simp
| (k+1) := add_le_add' hab (smul_le_smul_of_le_right _)
end add_monoid
section linear_ordered_semiring
variable [linear_ordered_semiring Ξ±]
theorem pow_pos {a : Ξ±} (H : 0 < a) : β (n : β), 0 < a ^ n
| 0 := zero_lt_one
| (n+1) := mul_pos H (pow_pos _)
theorem pow_nonneg {a : Ξ±} (H : 0 β€ a) : β (n : β), 0 β€ a ^ n
| 0 := zero_le_one
| (n+1) := mul_nonneg H (pow_nonneg _)
theorem pow_lt_pow_of_lt_left {x y : Ξ±} {n : β} (Hxy : x < y) (Hxpos : 0 β€ x) (Hnpos : 0 < n) : x ^ n < y ^ n :=
begin
cases lt_or_eq_of_le Hxpos,
{ rw βnat.sub_add_cancel Hnpos,
induction (n - 1), { simpa only [pow_one] },
rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one],
apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) },
{ rw [βh, zero_pow Hnpos], apply pow_pos (by rwa βh at Hxy : 0 < y),}
end
theorem pow_right_inj {x y : Ξ±} {n : β} (Hxpos : 0 β€ x) (Hypos : 0 β€ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y :=
begin
rcases lt_trichotomy x y with hxy | rfl | hyx,
{ exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) },
{ refl },
{ exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) },
end
theorem one_le_pow_of_one_le {a : Ξ±} (H : 1 β€ a) : β (n : β), 1 β€ a ^ n
| 0 := le_refl _
| (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n)
zero_le_one (le_trans zero_le_one H)
theorem pow_ge_one_add_mul {a : Ξ±} (H : a β₯ 0) :
β (n : β), 1 + n β’ a β€ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| (n+1) := begin
rw [pow_succ', succ_smul'],
refine le_trans _ (mul_le_mul_of_nonneg_right
(pow_ge_one_add_mul n) (add_nonneg zero_le_one H)),
rw [mul_add, mul_one, β add_assoc, add_le_add_iff_left],
simpa only [one_mul] using mul_le_mul_of_nonneg_right
((le_add_iff_nonneg_right 1).2 (add_monoid.smul_nonneg H n)) H
end
theorem pow_le_pow {a : Ξ±} {n m : β} (ha : 1 β€ a) (h : n β€ m) : a ^ n β€ a ^ m :=
let β¨k, hkβ© := nat.le.dest h in
calc a ^ n = a ^ n * 1 : (mul_one _).symm
... β€ a ^ n * a ^ k : mul_le_mul_of_nonneg_left
(one_le_pow_of_one_le ha _)
(pow_nonneg (le_trans zero_le_one ha) _)
... = a ^ m : by rw [βhk, pow_add]
lemma pow_lt_pow {a : Ξ±} {n m : β} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m :=
begin
have h' : 1 β€ a := le_of_lt h,
have h'' : 0 < a := lt_trans zero_lt_one h,
cases m, cases h2, rw [pow_succ, βone_mul (a ^ n)],
exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'')
end
lemma pow_le_pow_of_le_left {a b : Ξ±} (ha : 0 β€ a) (hab : a β€ b) : β i : β, a^i β€ b^i
| 0 := by simp
| (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab)
lemma lt_of_pow_lt_pow {a b : Ξ±} (n : β) (hb : 0 β€ b) (h : a ^ n < b ^ n) : a < b :=
lt_of_not_ge $ Ξ» hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h
private lemma pow_lt_pow_of_lt_one_aux {a : Ξ±} (h : 0 < a) (ha : a < 1) (i : β) :
β k : β, a ^ (i + k + 1) < a ^ i
| 0 := begin simp only [add_zero], rw βone_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end
| (k+1) :=
begin
rw βone_mul (a^i),
apply mul_lt_mul ha _ _ zero_le_one,
{ apply le_of_lt, apply pow_lt_pow_of_lt_one_aux },
{ show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h }
end
private lemma pow_le_pow_of_le_one_aux {a : Ξ±} (h : 0 β€ a) (ha : a β€ 1) (i : β) :
β k : β, a ^ (i + k) β€ a ^ i
| 0 := by simp
| (k+1) := by rw [βadd_assoc, βone_mul (a^i)];
exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one
lemma pow_lt_pow_of_lt_one {a : Ξ±} (h : 0 < a) (ha : a < 1)
{i j : β} (hij : i < j) : a ^ j < a ^ i :=
let β¨k, hkβ© := nat.exists_eq_add_of_lt hij in
by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _
lemma pow_le_pow_of_le_one {a : Ξ±} (h : 0 β€ a) (ha : a β€ 1)
{i j : β} (hij : i β€ j) : a ^ j β€ a ^ i :=
let β¨k, hkβ© := nat.exists_eq_add_of_le hij in
by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _
lemma pow_le_one {x : Ξ±} : β (n : β) (h0 : 0 β€ x) (h1 : x β€ 1), x ^ n β€ 1
| 0 h0 h1 := le_refl (1 : Ξ±)
| (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1)
end linear_ordered_semiring
theorem pow_two_nonneg [linear_ordered_ring Ξ±] (a : Ξ±) : 0 β€ a ^ 2 :=
by rw pow_two; exact mul_self_nonneg _
theorem pow_ge_one_add_sub_mul [linear_ordered_ring Ξ±]
{a : Ξ±} (H : a β₯ 1) (n : β) : 1 + n β’ (a - 1) β€ a ^ n :=
by simpa only [add_sub_cancel'_right] using pow_ge_one_add_mul (sub_nonneg.2 H) n
namespace int
lemma units_pow_two (u : units β€) : u ^ 2 = 1 :=
(units_eq_one_or u).elim (Ξ» h, h.symm βΈ rfl) (Ξ» h, h.symm βΈ rfl)
lemma units_pow_eq_pow_mod_two (u : units β€) (n : β) : u ^ n = u ^ (n % 2) :=
by conv {to_lhs, rw β nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one]
end int
@[simp] lemma neg_square {Ξ±} [ring Ξ±] (z : Ξ±) : (-z)^2 = z^2 :=
by simp [pow, monoid.pow]
lemma div_sq_cancel {Ξ±} [field Ξ±] {a : Ξ±} (ha : a β 0) (b : Ξ±) : a^2 * b / a = a * b :=
by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
|
cf81fe3583474932e90f399f9320b675af0db765 | df561f413cfe0a88b1056655515399c546ff32a5 | /8-inequality-world/l8.lean | 188749dd92e2ba11e32922ac35919a5c1e20a0bb | [] | no_license | nicholaspun/natural-number-game-solutions | 31d5158415c6f582694680044c5c6469032c2a06 | 1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0 | refs/heads/main | 1,675,123,625,012 | 1,607,633,548,000 | 1,607,633,548,000 | 318,933,860 | 3 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 131 | lean | lemma succ_le_succ (a b : mynat) (h : a β€ b) : succ a β€ succ b :=
begin
cases h with c hc,
rw hc,
use c,
rw succ_add,
refl,
end |
3f3e3bf9e97627756c284d3fbb85f8228ed349dd | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/data/set/basic.lean | f1d50731cf156db2d30f8e98ef502da523dfed8f | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 108,081 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
import logic.unique
import order.boolean_algebra
/-!
# Basic properties of sets
Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements
have type `X` are thus defined as `set X := X β Prop`. Note that this function need not
be decidable. The definition is in the core library.
This file provides some basic definitions related to sets and functions not present in the core
library, as well as extra lemmas for functions in the core library (empty set, univ, union,
intersection, insert, singleton, set-theoretic difference, complement, and powerset).
Note that a set is a term, not a type. There is a coercion from `set Ξ±` to `Type*` sending
`s` to the corresponding subtype `β₯s`.
See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean.
## Main definitions
Notation used here:
- `f : Ξ± β Ξ²` is a function,
- `s : set Ξ±` and `sβ sβ : set Ξ±` are subsets of `Ξ±`
- `t : set Ξ²` is a subset of `Ξ²`.
Definitions in the file:
* `strict_subset sβ sβ : Prop` : the predicate `sβ β sβ` but `sβ β sβ`.
* `nonempty s : Prop` : the predicate `s β β
`. Note that this is the preferred way to express the
fact that `s` has an element (see the Implementation Notes).
* `preimage f t : set Ξ±` : the preimage fβ»ΒΉ(t) (written `f β»ΒΉ' t` in Lean) of a subset of Ξ².
* `subsingleton s : Prop` : the predicate saying that `s` has at most one element.
* `range f : set Ξ²` : the image of `univ` under `f`.
Also works for `{p : Prop} (f : p β Ξ±)` (unlike `image`)
* `s.prod t : set (Ξ± Γ Ξ²)` : the subset `s Γ t`.
* `inclusion sβ sβ : β₯sβ β β₯sβ` : the map `β₯sβ β β₯sβ` induced by an inclusion `sβ β sβ`.
## Notation
* `f β»ΒΉ' t` for `preimage f t`
* `f '' s` for `image f s`
* `sαΆ` for the complement of `s`
## Implementation notes
* `s.nonempty` is to be preferred to `s β β
` or `β x, x β s`. It has the advantage that
the `s.nonempty` dot notation can be used.
* For `s : set Ξ±`, do not use `subtype s`. Instead use `β₯s` or `(s : Type*)` or `s`.
## Tags
set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert,
singleton, complement, powerset
-/
/-! ### Set coercion to a type -/
open function
universe variables u v w x
run_cmd do e β tactic.get_env,
tactic.set_env $ e.mk_protected `set.compl
lemma has_subset.subset.trans {Ξ± : Type*} [has_subset Ξ±] [is_trans Ξ± (β)]
{a b c : Ξ±} (h : a β b) (h' : b β c) : a β c := trans h h'
lemma has_subset.subset.antisymm {Ξ± : Type*} [has_subset Ξ±] [is_antisymm Ξ± (β)]
{a b : Ξ±} (h : a β b) (h' : b β a) : a = b := antisymm h h'
lemma has_ssubset.ssubset.trans {Ξ± : Type*} [has_ssubset Ξ±] [is_trans Ξ± (β)]
{a b c : Ξ±} (h : a β b) (h' : b β c) : a β c := trans h h'
lemma has_ssubset.ssubset.asymm {Ξ± : Type*} [has_ssubset Ξ±] [is_asymm Ξ± (β)]
{a b : Ξ±} (h : a β b) : Β¬(b β a) := asymm h
namespace set
variable {Ξ± : Type*}
instance : has_le (set Ξ±) := β¨(β)β©
instance : has_lt (set Ξ±) := β¨Ξ» s t, s β€ t β§ Β¬t β€ sβ© -- `β` is not defined until further down
instance {Ξ± : Type*} : boolean_algebra (set Ξ±) :=
{ sup := (βͺ),
le := (β€),
lt := (<),
inf := (β©),
bot := β
,
compl := set.compl,
top := univ,
sdiff := (\),
.. (infer_instance : boolean_algebra (Ξ± β Prop)) }
@[simp] lemma top_eq_univ : (β€ : set Ξ±) = univ := rfl
@[simp] lemma bot_eq_empty : (β₯ : set Ξ±) = β
:= rfl
@[simp] lemma sup_eq_union : ((β) : set Ξ± β set Ξ± β set Ξ±) = (βͺ) := rfl
@[simp] lemma inf_eq_inter : ((β) : set Ξ± β set Ξ± β set Ξ±) = (β©) := rfl
@[simp] lemma le_eq_subset : ((β€) : set Ξ± β set Ξ± β Prop) = (β) := rfl
/-! `set.lt_eq_ssubset` is defined further down -/
@[simp] lemma compl_eq_compl : set.compl = (has_compl.compl : set Ξ± β set Ξ±) := rfl
/-- Coercion from a set to the corresponding subtype. -/
instance {Ξ± : Type*} : has_coe_to_sort (set Ξ±) := β¨_, Ξ» s, {x // x β s}β©
instance pi_set_coe.can_lift (ΞΉ : Type u) (Ξ± : Ξ i : ΞΉ, Type v) [ne : Ξ i, nonempty (Ξ± i)]
(s : set ΞΉ) :
can_lift (Ξ i : s, Ξ± i) (Ξ i, Ξ± i) :=
{ coe := Ξ» f i, f i,
.. pi_subtype.can_lift ΞΉ Ξ± s }
instance pi_set_coe.can_lift' (ΞΉ : Type u) (Ξ± : Type v) [ne : nonempty Ξ±] (s : set ΞΉ) :
can_lift (s β Ξ±) (ΞΉ β Ξ±) :=
pi_set_coe.can_lift ΞΉ (Ξ» _, Ξ±) s
instance set_coe.can_lift (s : set Ξ±) : can_lift Ξ± s :=
{ coe := coe,
cond := Ξ» a, a β s,
prf := Ξ» a ha, β¨β¨a, haβ©, rflβ© }
end set
section set_coe
variables {Ξ± : Type u}
theorem set.set_coe_eq_subtype (s : set Ξ±) :
coe_sort.{(u+1) (u+2)} s = {x // x β s} := rfl
@[simp] theorem set_coe.forall {s : set Ξ±} {p : s β Prop} :
(β x : s, p x) β (β x (h : x β s), p β¨x, hβ©) :=
subtype.forall
@[simp] theorem set_coe.exists {s : set Ξ±} {p : s β Prop} :
(β x : s, p x) β (β x (h : x β s), p β¨x, hβ©) :=
subtype.exists
theorem set_coe.exists' {s : set Ξ±} {p : Ξ x, x β s β Prop} :
(β x (h : x β s), p x h) β (β x : s, p x x.2) :=
(@set_coe.exists _ _ $ Ξ» x, p x.1 x.2).symm
theorem set_coe.forall' {s : set Ξ±} {p : Ξ x, x β s β Prop} :
(β x (h : x β s), p x h) β (β x : s, p x x.2) :=
(@set_coe.forall _ _ $ Ξ» x, p x.1 x.2).symm
@[simp] theorem set_coe_cast : β {s t : set Ξ±} (H' : s = t) (H : @eq (Type u) s t) (x : s),
cast H x = β¨x.1, H' βΈ x.2β©
| s _ rfl _ β¨x, hβ© := rfl
theorem set_coe.ext {s : set Ξ±} {a b : s} : (βa : Ξ±) = βb β a = b :=
subtype.eq
theorem set_coe.ext_iff {s : set Ξ±} {a b : s} : (βa : Ξ±) = βb β a = b :=
iff.intro set_coe.ext (assume h, h βΈ rfl)
end set_coe
/-- See also `subtype.prop` -/
lemma subtype.mem {Ξ± : Type*} {s : set Ξ±} (p : s) : (p : Ξ±) β s := p.prop
lemma eq.subset {Ξ±} {s t : set Ξ±} : s = t β s β t :=
by { rintro rfl x hx, exact hx }
namespace set
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {a : Ξ±} {s t : set Ξ±}
instance : inhabited (set Ξ±) := β¨β
β©
@[ext]
theorem ext {a b : set Ξ±} (h : β x, x β a β x β b) : a = b :=
funext (assume x, propext (h x))
theorem ext_iff {s t : set Ξ±} : s = t β β x, x β s β x β t :=
β¨Ξ» h x, by rw h, extβ©
@[trans] theorem mem_of_mem_of_subset {x : Ξ±} {s t : set Ξ±}
(hx : x β s) (h : s β t) : x β t := h hx
/-! ### Lemmas about `mem` and `set_of` -/
@[simp] theorem mem_set_of_eq {a : Ξ±} {p : Ξ± β Prop} : a β {a | p a} = p a := rfl
theorem nmem_set_of_eq {a : Ξ±} {P : Ξ± β Prop} : a β {a : Ξ± | P a} = Β¬ P a := rfl
@[simp] theorem set_of_mem_eq {s : set Ξ±} : {x | x β s} = s := rfl
theorem set_of_set {s : set Ξ±} : set_of s = s := rfl
lemma set_of_app_iff {p : Ξ± β Prop} {x : Ξ±} : { x | p x } x β p x := iff.rfl
theorem mem_def {a : Ξ±} {s : set Ξ±} : a β s β s a := iff.rfl
instance decidable_mem (s : set Ξ±) [H : decidable_pred s] : β a, decidable (a β s) := H
instance decidable_set_of (p : Ξ± β Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H
@[simp] theorem set_of_subset_set_of {p q : Ξ± β Prop} :
{a | p a} β {a | q a} β (βa, p a β q a) := iff.rfl
@[simp] lemma sep_set_of {p q : Ξ± β Prop} : {a β {a | p a } | q a} = {a | p a β§ q a} := rfl
lemma set_of_and {p q : Ξ± β Prop} : {a | p a β§ q a} = {a | p a} β© {a | q a} := rfl
lemma set_of_or {p q : Ξ± β Prop} : {a | p a β¨ q a} = {a | p a} βͺ {a | q a} := rfl
/-! ### Lemmas about subsets -/
-- TODO(Jeremy): write a tactic to unfold specific instances of generic notation?
theorem subset_def {s t : set Ξ±} : (s β t) = β x, x β s β x β t := rfl
@[refl] theorem subset.refl (a : set Ξ±) : a β a := assume x, id
theorem subset.rfl {s : set Ξ±} : s β s := subset.refl s
@[trans] theorem subset.trans {a b c : set Ξ±} (ab : a β b) (bc : b β c) : a β c :=
assume x h, bc (ab h)
@[trans] theorem mem_of_eq_of_mem {x y : Ξ±} {s : set Ξ±} (hx : x = y) (h : y β s) : x β s :=
hx.symm βΈ h
theorem subset.antisymm {a b : set Ξ±} (hβ : a β b) (hβ : b β a) : a = b :=
set.ext $ Ξ» x, β¨@hβ _, @hβ _β©
theorem subset.antisymm_iff {a b : set Ξ±} : a = b β a β b β§ b β a :=
β¨Ξ» e, β¨e.subset, e.symm.subsetβ©, Ξ» β¨hβ, hββ©, subset.antisymm hβ hββ©
-- an alternative name
theorem eq_of_subset_of_subset {a b : set Ξ±} : a β b β b β a β a = b := subset.antisymm
theorem mem_of_subset_of_mem {sβ sβ : set Ξ±} {a : Ξ±} (h : sβ β sβ) : a β sβ β a β sβ := @h _
theorem not_mem_subset (h : s β t) : a β t β a β s :=
mt $ mem_of_subset_of_mem h
theorem not_subset : (Β¬ s β t) β βa β s, a β t := by simp only [subset_def, not_forall]
/-! ### Definition of strict subsets `s β t` and basic properties. -/
instance : has_ssubset (set Ξ±) := β¨(<)β©
@[simp] lemma lt_eq_ssubset : ((<) : set Ξ± β set Ξ± β Prop) = (β) := rfl
theorem ssubset_def : (s β t) = (s β t β§ Β¬ (t β s)) := rfl
theorem eq_or_ssubset_of_subset (h : s β t) : s = t β¨ s β t :=
eq_or_lt_of_le h
lemma exists_of_ssubset {s t : set Ξ±} (h : s β t) : (βxβt, x β s) :=
not_subset.1 h.2
lemma ssubset_iff_subset_ne {s t : set Ξ±} : s β t β s β t β§ s β t :=
@lt_iff_le_and_ne (set Ξ±) _ s t
lemma ssubset_iff_of_subset {s t : set Ξ±} (h : s β t) : s β t β β x β t, x β s :=
β¨exists_of_ssubset, Ξ» β¨x, hxt, hxsβ©, β¨h, Ξ» h, hxs $ h hxtβ©β©
lemma ssubset_of_ssubset_of_subset {sβ sβ sβ : set Ξ±} (hsβsβ : sβ β sβ) (hsβsβ : sβ β sβ) :
sβ β sβ :=
β¨subset.trans hsβsβ.1 hsβsβ, Ξ» hsβsβ, hsβsβ.2 (subset.trans hsβsβ hsβsβ)β©
lemma ssubset_of_subset_of_ssubset {sβ sβ sβ : set Ξ±} (hsβsβ : sβ β sβ) (hsβsβ : sβ β sβ) :
sβ β sβ :=
β¨subset.trans hsβsβ hsβsβ.1, Ξ» hsβsβ, hsβsβ.2 (subset.trans hsβsβ hsβsβ)β©
theorem not_mem_empty (x : Ξ±) : Β¬ (x β (β
: set Ξ±)) := id
@[simp] theorem not_not_mem : Β¬ (a β s) β a β s := not_not
/-! ### Non-empty sets -/
/-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used
in theorem assumptions instead of `β x, x β s` or `s β β
` as it gives access to a nice API thanks
to the dot notation. -/
protected def nonempty (s : set Ξ±) : Prop := β x, x β s
lemma nonempty_def : s.nonempty β β x, x β s := iff.rfl
lemma nonempty_of_mem {x} (h : x β s) : s.nonempty := β¨x, hβ©
theorem nonempty.not_subset_empty : s.nonempty β Β¬(s β β
)
| β¨x, hxβ© hs := hs hx
theorem nonempty.ne_empty : β {s : set Ξ±}, s.nonempty β s β β
| _ β¨x, hxβ© rfl := hx
@[simp] theorem not_nonempty_empty : Β¬(β
: set Ξ±).nonempty :=
Ξ» h, h.ne_empty rfl
/-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis
on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/
protected noncomputable def nonempty.some (h : s.nonempty) : Ξ± := classical.some h
protected lemma nonempty.some_mem (h : s.nonempty) : h.some β s := classical.some_spec h
lemma nonempty.mono (ht : s β t) (hs : s.nonempty) : t.nonempty := hs.imp ht
lemma nonempty_of_not_subset (h : Β¬s β t) : (s \ t).nonempty :=
let β¨x, xs, xtβ© := not_subset.1 h in β¨x, xs, xtβ©
lemma nonempty_of_ssubset (ht : s β t) : (t \ s).nonempty :=
nonempty_of_not_subset ht.2
lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left
lemma nonempty_of_ssubset' (ht : s β t) : t.nonempty := (nonempty_of_ssubset ht).of_diff
lemma nonempty.inl (hs : s.nonempty) : (s βͺ t).nonempty := hs.imp $ Ξ» _, or.inl
lemma nonempty.inr (ht : t.nonempty) : (s βͺ t).nonempty := ht.imp $ Ξ» _, or.inr
@[simp] lemma union_nonempty : (s βͺ t).nonempty β s.nonempty β¨ t.nonempty := exists_or_distrib
lemma nonempty.left (h : (s β© t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left
lemma nonempty.right (h : (s β© t).nonempty) : t.nonempty := h.imp $ Ξ» _, and.right
lemma nonempty_inter_iff_exists_right : (s β© t).nonempty β β x : t, βx β s :=
β¨Ξ» β¨x, xs, xtβ©, β¨β¨x, xtβ©, xsβ©, Ξ» β¨β¨x, xtβ©, xsβ©, β¨x, xs, xtβ©β©
lemma nonempty_inter_iff_exists_left : (s β© t).nonempty β β x : s, βx β t :=
β¨Ξ» β¨x, xs, xtβ©, β¨β¨x, xsβ©, xtβ©, Ξ» β¨β¨x, xtβ©, xsβ©, β¨x, xt, xsβ©β©
lemma nonempty_iff_univ_nonempty : nonempty Ξ± β (univ : set Ξ±).nonempty :=
β¨Ξ» β¨xβ©, β¨x, trivialβ©, Ξ» β¨x, _β©, β¨xβ©β©
@[simp] lemma univ_nonempty : β [h : nonempty Ξ±], (univ : set Ξ±).nonempty
| β¨xβ© := β¨x, trivialβ©
lemma nonempty.to_subtype (h : s.nonempty) : nonempty s :=
nonempty_subtype.2 h
instance [nonempty Ξ±] : nonempty (set.univ : set Ξ±) := set.univ_nonempty.to_subtype
@[simp] lemma nonempty_insert (a : Ξ±) (s : set Ξ±) : (insert a s).nonempty := β¨a, or.inl rflβ©
lemma nonempty_of_nonempty_subtype [nonempty s] : s.nonempty :=
nonempty_subtype.mp βΉ_βΊ
/-! ### Lemmas about the empty set -/
theorem empty_def : (β
: set Ξ±) = {x | false} := rfl
@[simp] theorem mem_empty_eq (x : Ξ±) : x β (β
: set Ξ±) = false := rfl
@[simp] theorem set_of_false : {a : Ξ± | false} = β
:= rfl
@[simp] theorem empty_subset (s : set Ξ±) : β
β s.
theorem subset_empty_iff {s : set Ξ±} : s β β
β s = β
:=
(subset.antisymm_iff.trans $ and_iff_left (empty_subset _)).symm
theorem eq_empty_iff_forall_not_mem {s : set Ξ±} : s = β
β β x, x β s := subset_empty_iff.symm
theorem eq_empty_of_subset_empty {s : set Ξ±} : s β β
β s = β
:= subset_empty_iff.1
theorem eq_empty_of_not_nonempty (h : Β¬nonempty Ξ±) (s : set Ξ±) : s = β
:=
eq_empty_of_subset_empty $ Ξ» x hx, h β¨xβ©
lemma not_nonempty_iff_eq_empty {s : set Ξ±} : Β¬s.nonempty β s = β
:=
by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists]
lemma empty_not_nonempty : Β¬(β
: set Ξ±).nonempty := Ξ» h, h.ne_empty rfl
theorem ne_empty_iff_nonempty : s β β
β s.nonempty := not_iff_comm.1 not_nonempty_iff_eq_empty
lemma eq_empty_or_nonempty (s : set Ξ±) : s = β
β¨ s.nonempty :=
or_iff_not_imp_left.2 ne_empty_iff_nonempty.1
theorem subset_eq_empty {s t : set Ξ±} (h : t β s) (e : s = β
) : t = β
:=
subset_empty_iff.1 $ e βΈ h
theorem ball_empty_iff {p : Ξ± β Prop} : (β x β (β
: set Ξ±), p x) β true :=
iff_true_intro $ Ξ» x, false.elim
instance (Ξ± : Type u) : is_empty.{u+1} (β
: set Ξ±) :=
β¨Ξ» x, x.2β©
/-!
### Universal set.
In Lean `@univ Ξ±` (or `univ : set Ξ±`) is the set that contains all elements of type `Ξ±`.
Mathematically it is the same as `Ξ±` but it has a different type.
-/
@[simp] theorem set_of_true : {x : Ξ± | true} = univ := rfl
@[simp] theorem mem_univ (x : Ξ±) : x β @univ Ξ± := trivial
@[simp] lemma univ_eq_empty_iff : (univ : set Ξ±) = β
β Β¬ nonempty Ξ± :=
eq_empty_iff_forall_not_mem.trans β¨Ξ» H β¨xβ©, H x trivial, Ξ» H x _, H β¨xβ©β©
theorem empty_ne_univ [h : nonempty Ξ±] : (β
: set Ξ±) β univ :=
Ξ» e, univ_eq_empty_iff.1 e.symm h
@[simp] theorem subset_univ (s : set Ξ±) : s β univ := Ξ» x H, trivial
theorem univ_subset_iff {s : set Ξ±} : univ β s β s = univ :=
(subset.antisymm_iff.trans $ and_iff_right (subset_univ _)).symm
theorem eq_univ_of_univ_subset {s : set Ξ±} : univ β s β s = univ := univ_subset_iff.1
theorem eq_univ_iff_forall {s : set Ξ±} : s = univ β β x, x β s :=
univ_subset_iff.symm.trans $ forall_congr $ Ξ» x, imp_iff_right β¨β©
theorem eq_univ_of_forall {s : set Ξ±} : (β x, x β s) β s = univ := eq_univ_iff_forall.2
lemma eq_univ_of_subset {s t : set Ξ±} (h : s β t) (hs : s = univ) : t = univ :=
eq_univ_of_univ_subset $ hs βΈ h
lemma exists_mem_of_nonempty (Ξ±) : β [nonempty Ξ±], βx:Ξ±, x β (univ : set Ξ±)
| β¨xβ© := β¨x, trivialβ©
instance univ_decidable : decidable_pred (@set.univ Ξ±) :=
Ξ» x, is_true trivial
/-- `diagonal Ξ±` is the subset of `Ξ± Γ Ξ±` consisting of all pairs of the form `(a, a)`. -/
def diagonal (Ξ± : Type*) : set (Ξ± Γ Ξ±) := {p | p.1 = p.2}
@[simp]
lemma mem_diagonal {Ξ± : Type*} (x : Ξ±) : (x, x) β diagonal Ξ± :=
by simp [diagonal]
/-! ### Lemmas about union -/
theorem union_def {sβ sβ : set Ξ±} : sβ βͺ sβ = {a | a β sβ β¨ a β sβ} := rfl
theorem mem_union_left {x : Ξ±} {a : set Ξ±} (b : set Ξ±) : x β a β x β a βͺ b := or.inl
theorem mem_union_right {x : Ξ±} {b : set Ξ±} (a : set Ξ±) : x β b β x β a βͺ b := or.inr
theorem mem_or_mem_of_mem_union {x : Ξ±} {a b : set Ξ±} (H : x β a βͺ b) : x β a β¨ x β b := H
theorem mem_union.elim {x : Ξ±} {a b : set Ξ±} {P : Prop}
(Hβ : x β a βͺ b) (Hβ : x β a β P) (Hβ : x β b β P) : P :=
or.elim Hβ Hβ Hβ
theorem mem_union (x : Ξ±) (a b : set Ξ±) : x β a βͺ b β x β a β¨ x β b := iff.rfl
@[simp] theorem mem_union_eq (x : Ξ±) (a b : set Ξ±) : x β a βͺ b = (x β a β¨ x β b) := rfl
@[simp] theorem union_self (a : set Ξ±) : a βͺ a = a := ext $ Ξ» x, or_self _
@[simp] theorem union_empty (a : set Ξ±) : a βͺ β
= a := ext $ Ξ» x, or_false _
@[simp] theorem empty_union (a : set Ξ±) : β
βͺ a = a := ext $ Ξ» x, false_or _
theorem union_comm (a b : set Ξ±) : a βͺ b = b βͺ a := ext $ Ξ» x, or.comm
theorem union_assoc (a b c : set Ξ±) : (a βͺ b) βͺ c = a βͺ (b βͺ c) := ext $ Ξ» x, or.assoc
instance union_is_assoc : is_associative (set Ξ±) (βͺ) := β¨union_assocβ©
instance union_is_comm : is_commutative (set Ξ±) (βͺ) := β¨union_commβ©
theorem union_left_comm (sβ sβ sβ : set Ξ±) : sβ βͺ (sβ βͺ sβ) = sβ βͺ (sβ βͺ sβ) :=
ext $ Ξ» x, or.left_comm
theorem union_right_comm (sβ sβ sβ : set Ξ±) : (sβ βͺ sβ) βͺ sβ = (sβ βͺ sβ) βͺ sβ :=
ext $ Ξ» x, or.right_comm
theorem union_eq_self_of_subset_left {s t : set Ξ±} (h : s β t) : s βͺ t = t :=
ext $ Ξ» x, or_iff_right_of_imp $ @h _
theorem union_eq_self_of_subset_right {s t : set Ξ±} (h : t β s) : s βͺ t = s :=
ext $ Ξ» x, or_iff_left_of_imp $ @h _
@[simp] theorem subset_union_left (s t : set Ξ±) : s β s βͺ t := Ξ» x, or.inl
@[simp] theorem subset_union_right (s t : set Ξ±) : t β s βͺ t := Ξ» x, or.inr
theorem union_subset {s t r : set Ξ±} (sr : s β r) (tr : t β r) : s βͺ t β r :=
Ξ» x, or.rec (@sr _) (@tr _)
@[simp] theorem union_subset_iff {s t u : set Ξ±} : s βͺ t β u β s β u β§ t β u :=
(forall_congr (by exact Ξ» x, or_imp_distrib)).trans forall_and_distrib
theorem union_subset_union {sβ sβ tβ tβ : set Ξ±}
(hβ : sβ β sβ) (hβ : tβ β tβ) : sβ βͺ tβ β sβ βͺ tβ := Ξ» x, or.imp (@hβ _) (@hβ _)
theorem union_subset_union_left {sβ sβ : set Ξ±} (t) (h : sβ β sβ) : sβ βͺ t β sβ βͺ t :=
union_subset_union h subset.rfl
theorem union_subset_union_right (s) {tβ tβ : set Ξ±} (h : tβ β tβ) : s βͺ tβ β s βͺ tβ :=
union_subset_union subset.rfl h
lemma subset_union_of_subset_left {s t : set Ξ±} (h : s β t) (u : set Ξ±) : s β t βͺ u :=
subset.trans h (subset_union_left t u)
lemma subset_union_of_subset_right {s u : set Ξ±} (h : s β u) (t : set Ξ±) : s β t βͺ u :=
subset.trans h (subset_union_right t u)
@[simp] theorem union_empty_iff {s t : set Ξ±} : s βͺ t = β
β s = β
β§ t = β
:=
by simp only [β subset_empty_iff]; exact union_subset_iff
@[simp] lemma union_univ {s : set Ξ±} : s βͺ univ = univ := sup_top_eq
@[simp] lemma univ_union {s : set Ξ±} : univ βͺ s = univ := top_sup_eq
/-! ### Lemmas about intersection -/
theorem inter_def {sβ sβ : set Ξ±} : sβ β© sβ = {a | a β sβ β§ a β sβ} := rfl
theorem mem_inter_iff (x : Ξ±) (a b : set Ξ±) : x β a β© b β x β a β§ x β b := iff.rfl
@[simp] theorem mem_inter_eq (x : Ξ±) (a b : set Ξ±) : x β a β© b = (x β a β§ x β b) := rfl
theorem mem_inter {x : Ξ±} {a b : set Ξ±} (ha : x β a) (hb : x β b) : x β a β© b := β¨ha, hbβ©
theorem mem_of_mem_inter_left {x : Ξ±} {a b : set Ξ±} (h : x β a β© b) : x β a := h.left
theorem mem_of_mem_inter_right {x : Ξ±} {a b : set Ξ±} (h : x β a β© b) : x β b := h.right
@[simp] theorem inter_self (a : set Ξ±) : a β© a = a := ext $ Ξ» x, and_self _
@[simp] theorem inter_empty (a : set Ξ±) : a β© β
= β
:= ext $ Ξ» x, and_false _
@[simp] theorem empty_inter (a : set Ξ±) : β
β© a = β
:= ext $ Ξ» x, false_and _
theorem inter_comm (a b : set Ξ±) : a β© b = b β© a := ext $ Ξ» x, and.comm
theorem inter_assoc (a b c : set Ξ±) : (a β© b) β© c = a β© (b β© c) := ext $ Ξ» x, and.assoc
instance inter_is_assoc : is_associative (set Ξ±) (β©) := β¨inter_assocβ©
instance inter_is_comm : is_commutative (set Ξ±) (β©) := β¨inter_commβ©
theorem inter_left_comm (sβ sβ sβ : set Ξ±) : sβ β© (sβ β© sβ) = sβ β© (sβ β© sβ) :=
ext $ Ξ» x, and.left_comm
theorem inter_right_comm (sβ sβ sβ : set Ξ±) : (sβ β© sβ) β© sβ = (sβ β© sβ) β© sβ :=
ext $ Ξ» x, and.right_comm
@[simp] theorem inter_subset_left (s t : set Ξ±) : s β© t β s := Ξ» x, and.left
@[simp] theorem inter_subset_right (s t : set Ξ±) : s β© t β t := Ξ» x, and.right
theorem subset_inter {s t r : set Ξ±} (rs : r β s) (rt : r β t) : r β s β© t := Ξ» x h, β¨rs h, rt hβ©
@[simp] theorem subset_inter_iff {s t r : set Ξ±} : r β s β© t β r β s β§ r β t :=
(forall_congr (by exact Ξ» x, imp_and_distrib)).trans forall_and_distrib
theorem inter_eq_left_iff_subset {s t : set Ξ±} : s β© t = s β s β t :=
ext_iff.trans $ forall_congr $ Ξ» x, and_iff_left_iff_imp
theorem inter_eq_right_iff_subset {s t : set Ξ±} : s β© t = t β t β s :=
ext_iff.trans $ forall_congr $ Ξ» x, and_iff_right_iff_imp
theorem inter_eq_self_of_subset_left {s t : set Ξ±} : s β t β s β© t = s :=
inter_eq_left_iff_subset.mpr
theorem inter_eq_self_of_subset_right {s t : set Ξ±} : t β s β s β© t = t :=
inter_eq_right_iff_subset.mpr
@[simp] theorem inter_univ (a : set Ξ±) : a β© univ = a :=
inter_eq_self_of_subset_left $ subset_univ _
@[simp] theorem univ_inter (a : set Ξ±) : univ β© a = a :=
inter_eq_self_of_subset_right $ subset_univ _
theorem inter_subset_inter {sβ sβ tβ tβ : set Ξ±}
(hβ : sβ β tβ) (hβ : sβ β tβ) : sβ β© sβ β tβ β© tβ := Ξ» x, and.imp (@hβ _) (@hβ _)
theorem inter_subset_inter_left {s t : set Ξ±} (u : set Ξ±) (H : s β t) : s β© u β t β© u :=
inter_subset_inter H subset.rfl
theorem inter_subset_inter_right {s t : set Ξ±} (u : set Ξ±) (H : s β t) : u β© s β u β© t :=
inter_subset_inter subset.rfl H
theorem union_inter_cancel_left {s t : set Ξ±} : (s βͺ t) β© s = s :=
inter_eq_self_of_subset_right $ subset_union_left _ _
theorem union_inter_cancel_right {s t : set Ξ±} : (s βͺ t) β© t = t :=
inter_eq_self_of_subset_right $ subset_union_right _ _
/-! ### Distributivity laws -/
theorem inter_distrib_left (s t u : set Ξ±) : s β© (t βͺ u) = (s β© t) βͺ (s β© u) :=
inf_sup_left
theorem inter_union_distrib_left {s t u : set Ξ±} : s β© (t βͺ u) = (s β© t) βͺ (s β© u) :=
inf_sup_left
theorem inter_distrib_right (s t u : set Ξ±) : (s βͺ t) β© u = (s β© u) βͺ (t β© u) :=
inf_sup_right
theorem union_inter_distrib_right {s t u : set Ξ±} : (s βͺ t) β© u = (s β© u) βͺ (t β© u) :=
inf_sup_right
theorem union_distrib_left (s t u : set Ξ±) : s βͺ (t β© u) = (s βͺ t) β© (s βͺ u) :=
sup_inf_left
theorem union_inter_distrib_left {s t u : set Ξ±} : s βͺ (t β© u) = (s βͺ t) β© (s βͺ u) :=
sup_inf_left
theorem union_distrib_right (s t u : set Ξ±) : (s β© t) βͺ u = (s βͺ u) β© (t βͺ u) :=
sup_inf_right
theorem inter_union_distrib_right {s t u : set Ξ±} : (s β© t) βͺ u = (s βͺ u) β© (t βͺ u) :=
sup_inf_right
/-!
### Lemmas about `insert`
`insert Ξ± s` is the set `{Ξ±} βͺ s`.
-/
theorem insert_def (x : Ξ±) (s : set Ξ±) : insert x s = { y | y = x β¨ y β s } := rfl
@[simp] theorem subset_insert (x : Ξ±) (s : set Ξ±) : s β insert x s := Ξ» y, or.inr
theorem mem_insert (x : Ξ±) (s : set Ξ±) : x β insert x s := or.inl rfl
theorem mem_insert_of_mem {x : Ξ±} {s : set Ξ±} (y : Ξ±) : x β s β x β insert y s := or.inr
theorem eq_or_mem_of_mem_insert {x a : Ξ±} {s : set Ξ±} : x β insert a s β x = a β¨ x β s := id
theorem mem_of_mem_insert_of_ne {x a : Ξ±} {s : set Ξ±} : x β insert a s β x β a β x β s :=
or.resolve_left
@[simp] theorem mem_insert_iff {x a : Ξ±} {s : set Ξ±} : x β insert a s β x = a β¨ x β s := iff.rfl
@[simp] theorem insert_eq_of_mem {a : Ξ±} {s : set Ξ±} (h : a β s) : insert a s = s :=
ext $ Ξ» x, or_iff_right_of_imp $ Ξ» e, e.symm βΈ h
lemma ne_insert_of_not_mem {s : set Ξ±} (t : set Ξ±) {a : Ξ±} : a β s β s β insert a t :=
mt $ Ξ» e, e.symm βΈ mem_insert _ _
theorem insert_subset : insert a s β t β (a β t β§ s β t) :=
by simp only [subset_def, or_imp_distrib, forall_and_distrib, forall_eq, mem_insert_iff]
theorem insert_subset_insert (h : s β t) : insert a s β insert a t := Ξ» x, or.imp_right (@h _)
theorem ssubset_iff_insert {s t : set Ξ±} : s β t β β a β s, insert a s β t :=
begin
simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset],
simp only [exists_prop, and_comm]
end
theorem ssubset_insert {s : set Ξ±} {a : Ξ±} (h : a β s) : s β insert a s :=
ssubset_iff_insert.2 β¨a, h, subset.refl _β©
theorem insert_comm (a b : Ξ±) (s : set Ξ±) : insert a (insert b s) = insert b (insert a s) :=
ext $ Ξ» x, or.left_comm
theorem insert_union : insert a s βͺ t = insert a (s βͺ t) := ext $ Ξ» x, or.assoc
@[simp] theorem union_insert : s βͺ insert a t = insert a (s βͺ t) := ext $ Ξ» x, or.left_comm
theorem insert_nonempty (a : Ξ±) (s : set Ξ±) : (insert a s).nonempty := β¨a, mem_insert a sβ©
instance (a : Ξ±) (s : set Ξ±) : nonempty (insert a s : set Ξ±) := (insert_nonempty a s).to_subtype
lemma insert_inter (x : Ξ±) (s t : set Ξ±) : insert x (s β© t) = insert x s β© insert x t :=
ext $ Ξ» y, or_and_distrib_left
-- useful in proofs by induction
theorem forall_of_forall_insert {P : Ξ± β Prop} {a : Ξ±} {s : set Ξ±}
(H : β x, x β insert a s β P x) (x) (h : x β s) : P x := H _ (or.inr h)
theorem forall_insert_of_forall {P : Ξ± β Prop} {a : Ξ±} {s : set Ξ±}
(H : β x, x β s β P x) (ha : P a) (x) (h : x β insert a s) : P x :=
h.elim (Ξ» e, e.symm βΈ ha) (H _)
theorem bex_insert_iff {P : Ξ± β Prop} {a : Ξ±} {s : set Ξ±} :
(β x β insert a s, P x) β P a β¨ (β x β s, P x) :=
bex_or_left_distrib.trans $ or_congr_left bex_eq_left
theorem ball_insert_iff {P : Ξ± β Prop} {a : Ξ±} {s : set Ξ±} :
(β x β insert a s, P x) β P a β§ (βx β s, P x) :=
ball_or_left_distrib.trans $ and_congr_left' forall_eq
/-! ### Lemmas about singletons -/
theorem singleton_def (a : Ξ±) : ({a} : set Ξ±) = insert a β
:= (insert_emptyc_eq _).symm
@[simp] theorem mem_singleton_iff {a b : Ξ±} : a β ({b} : set Ξ±) β a = b := iff.rfl
@[simp]
lemma set_of_eq_eq_singleton {a : Ξ±} : {n | n = a} = {a} :=
ext $ Ξ» n, (set.mem_singleton_iff).symm
-- TODO: again, annotation needed
@[simp] theorem mem_singleton (a : Ξ±) : a β ({a} : set Ξ±) := @rfl _ _
theorem eq_of_mem_singleton {x y : Ξ±} (h : x β ({y} : set Ξ±)) : x = y := h
@[simp] theorem singleton_eq_singleton_iff {x y : Ξ±} : {x} = ({y} : set Ξ±) β x = y :=
ext_iff.trans eq_iff_eq_cancel_left
theorem mem_singleton_of_eq {x y : Ξ±} (H : x = y) : x β ({y} : set Ξ±) := H
theorem insert_eq (x : Ξ±) (s : set Ξ±) : insert x s = ({x} : set Ξ±) βͺ s := rfl
@[simp] theorem pair_eq_singleton (a : Ξ±) : ({a, a} : set Ξ±) = {a} := union_self _
theorem pair_comm (a b : Ξ±) : ({a, b} : set Ξ±) = {b, a} := union_comm _ _
@[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : set Ξ±).nonempty :=
β¨a, rflβ©
@[simp] theorem singleton_subset_iff {a : Ξ±} {s : set Ξ±} : {a} β s β a β s := forall_eq
theorem set_compr_eq_eq_singleton {a : Ξ±} : {b | b = a} = {a} := rfl
@[simp] theorem singleton_union : {a} βͺ s = insert a s := rfl
@[simp] theorem union_singleton : s βͺ {a} = insert a s := union_comm _ _
@[simp] theorem singleton_inter_nonempty : ({a} β© s).nonempty β a β s :=
by simp only [set.nonempty, mem_inter_eq, mem_singleton_iff, exists_eq_left]
@[simp] theorem inter_singleton_nonempty : (s β© {a}).nonempty β a β s :=
by rw [inter_comm, singleton_inter_nonempty]
@[simp] theorem singleton_inter_eq_empty : {a} β© s = β
β a β s :=
not_nonempty_iff_eq_empty.symm.trans $ not_congr singleton_inter_nonempty
@[simp] theorem inter_singleton_eq_empty : s β© {a} = β
β a β s :=
by rw [inter_comm, singleton_inter_eq_empty]
lemma nmem_singleton_empty {s : set Ξ±} : s β ({β
} : set (set Ξ±)) β s.nonempty :=
ne_empty_iff_nonempty
instance unique_singleton (a : Ξ±) : unique β₯({a} : set Ξ±) :=
β¨β¨β¨a, mem_singleton aβ©β©, Ξ» β¨x, hβ©, subtype.eq hβ©
lemma eq_singleton_iff_unique_mem {s : set Ξ±} {a : Ξ±} : s = {a} β a β s β§ β x β s, x = a :=
subset.antisymm_iff.trans $ and.comm.trans $ and_congr_left' singleton_subset_iff
lemma eq_singleton_iff_nonempty_unique_mem {s : set Ξ±} {a : Ξ±} :
s = {a} β s.nonempty β§ β x β s, x = a :=
eq_singleton_iff_unique_mem.trans $ and_congr_left $ Ξ» H, β¨Ξ» h', β¨_, h'β©, Ξ» β¨x, hβ©, H x h βΈ hβ©
-- while `simp` is capable of proving this, it is not capable of turning the LHS into the RHS.
@[simp] lemma default_coe_singleton (x : Ξ±) :
default ({x} : set Ξ±) = β¨x, rflβ© := rfl
/-! ### Lemmas about sets defined as `{x β s | p x}`. -/
theorem mem_sep {s : set Ξ±} {p : Ξ± β Prop} {x : Ξ±} (xs : x β s) (px : p x) : x β {x β s | p x} :=
β¨xs, pxβ©
@[simp] theorem sep_mem_eq {s t : set Ξ±} : {x β s | x β t} = s β© t := rfl
@[simp] theorem mem_sep_eq {s : set Ξ±} {p : Ξ± β Prop} {x : Ξ±} :
x β {x β s | p x} = (x β s β§ p x) := rfl
theorem mem_sep_iff {s : set Ξ±} {p : Ξ± β Prop} {x : Ξ±} : x β {x β s | p x} β x β s β§ p x :=
iff.rfl
theorem eq_sep_of_subset {s t : set Ξ±} (h : s β t) : s = {x β t | x β s} :=
(inter_eq_self_of_subset_right h).symm
theorem sep_subset (s : set Ξ±) (p : Ξ± β Prop) : {x β s | p x} β s := Ξ» x, and.left
theorem forall_not_of_sep_empty {s : set Ξ±} {p : Ξ± β Prop} (H : {x β s | p x} = β
)
(x) : x β s β Β¬ p x := not_and.1 (eq_empty_iff_forall_not_mem.1 H x : _)
@[simp] lemma sep_univ {Ξ±} {p : Ξ± β Prop} : {a β (univ : set Ξ±) | p a} = {a | p a} := univ_inter _
@[simp] lemma sep_true : {a β s | true} = s :=
by { ext, simp }
@[simp] lemma sep_false : {a β s | false} = β
:=
by { ext, simp }
@[simp] lemma subset_singleton_iff {Ξ± : Type*} {s : set Ξ±} {x : Ξ±} : s β {x} β β y β s, y = x :=
iff.rfl
lemma subset_singleton_iff_eq {s : set Ξ±} {x : Ξ±} : s β {x} β s = β
β¨ s = {x} :=
begin
obtain (rfl | hs) := s.eq_empty_or_nonempty,
use β¨Ξ» _, or.inl rfl, Ξ» _, empty_subset _β©,
simp [eq_singleton_iff_nonempty_unique_mem, hs, ne_empty_iff_nonempty.2 hs],
end
lemma ssubset_singleton_iff {s : set Ξ±} {x : Ξ±} : s β {x} β s = β
:=
begin
rw [ssubset_iff_subset_ne, subset_singleton_iff_eq, or_and_distrib_right, and_not_self, or_false,
and_iff_left_iff_imp],
rintro rfl,
refine ne_comm.1 (ne_empty_iff_nonempty.2 (singleton_nonempty _)),
end
lemma eq_empty_of_ssubset_singleton {s : set Ξ±} {x : Ξ±} (hs : s β {x}) : s = β
:=
ssubset_singleton_iff.1 hs
/-! ### Lemmas about complement -/
theorem mem_compl {s : set Ξ±} {x : Ξ±} (h : x β s) : x β sαΆ := h
lemma compl_set_of {Ξ±} (p : Ξ± β Prop) : {a | p a}αΆ = { a | Β¬ p a } := rfl
theorem not_mem_of_mem_compl {s : set Ξ±} {x : Ξ±} (h : x β sαΆ) : x β s := h
@[simp] theorem mem_compl_eq (s : set Ξ±) (x : Ξ±) : x β sαΆ = (x β s) := rfl
theorem mem_compl_iff (s : set Ξ±) (x : Ξ±) : x β sαΆ β x β s := iff.rfl
@[simp] theorem inter_compl_self (s : set Ξ±) : s β© sαΆ = β
:= inf_compl_eq_bot
@[simp] theorem compl_inter_self (s : set Ξ±) : sαΆ β© s = β
:= compl_inf_eq_bot
@[simp] theorem compl_empty : (β
: set Ξ±)αΆ = univ := compl_bot
@[simp] theorem compl_union (s t : set Ξ±) : (s βͺ t)αΆ = sαΆ β© tαΆ := compl_sup
theorem compl_inter (s t : set Ξ±) : (s β© t)αΆ = sαΆ βͺ tαΆ := compl_inf
@[simp] theorem compl_univ : (univ : set Ξ±)αΆ = β
:= compl_top
@[simp] lemma compl_empty_iff {s : set Ξ±} : sαΆ = β
β s = univ := compl_eq_bot
@[simp] lemma compl_univ_iff {s : set Ξ±} : sαΆ = univ β s = β
:= compl_eq_top
lemma nonempty_compl {s : set Ξ±} : sαΆ.nonempty β s β univ :=
ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff
lemma mem_compl_singleton_iff {a x : Ξ±} : x β ({a} : set Ξ±)αΆ β x β a :=
not_congr mem_singleton_iff
lemma compl_singleton_eq (a : Ξ±) : ({a} : set Ξ±)αΆ = {x | x β a} :=
ext $ Ξ» x, mem_compl_singleton_iff
@[simp]
lemma compl_ne_eq_singleton (a : Ξ±) : ({x | x β a} : set Ξ±)αΆ = {a} :=
by { ext, simp, }
theorem union_eq_compl_compl_inter_compl (s t : set Ξ±) : s βͺ t = (sαΆ β© tαΆ)αΆ :=
ext $ Ξ» x, or_iff_not_and_not
theorem inter_eq_compl_compl_union_compl (s t : set Ξ±) : s β© t = (sαΆ βͺ tαΆ)αΆ :=
ext $ Ξ» x, and_iff_not_or_not
@[simp] theorem union_compl_self (s : set Ξ±) : s βͺ sαΆ = univ := eq_univ_iff_forall.2 $ Ξ» x, em _
@[simp] theorem compl_union_self (s : set Ξ±) : sαΆ βͺ s = univ := by rw [union_comm, union_compl_self]
theorem compl_comp_compl : compl β compl = @id (set Ξ±) := funext compl_compl
theorem compl_subset_comm {s t : set Ξ±} : sαΆ β t β tαΆ β s := @compl_le_iff_compl_le _ s t _
@[simp] lemma compl_subset_compl {s t : set Ξ±} : sαΆ β tαΆ β t β s := @compl_le_compl_iff_le _ t s _
theorem compl_subset_iff_union {s t : set Ξ±} : sαΆ β t β s βͺ t = univ :=
iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ Ξ» a, or_iff_not_imp_left
theorem subset_compl_comm {s t : set Ξ±} : s β tαΆ β t β sαΆ :=
forall_congr $ Ξ» a, imp_not_comm
theorem subset_compl_iff_disjoint {s t : set Ξ±} : s β tαΆ β s β© t = β
:=
iff.trans (forall_congr $ Ξ» a, and_imp.symm) subset_empty_iff
lemma subset_compl_singleton_iff {a : Ξ±} {s : set Ξ±} : s β {a}αΆ β a β s :=
subset_compl_comm.trans singleton_subset_iff
theorem inter_subset (a b c : set Ξ±) : a β© b β c β a β bαΆ βͺ c :=
forall_congr $ Ξ» x, and_imp.trans $ imp_congr_right $ Ξ» _, imp_iff_not_or
lemma inter_compl_nonempty_iff {s t : set Ξ±} : (s β© tαΆ).nonempty β Β¬ s β t :=
(not_subset.trans $ exists_congr $ by exact Ξ» x, by simp [mem_compl]).symm
/-! ### Lemmas about set difference -/
theorem diff_eq (s t : set Ξ±) : s \ t = s β© tαΆ := rfl
@[simp] theorem mem_diff {s t : set Ξ±} (x : Ξ±) : x β s \ t β x β s β§ x β t := iff.rfl
theorem mem_diff_of_mem {s t : set Ξ±} {x : Ξ±} (h1 : x β s) (h2 : x β t) : x β s \ t :=
β¨h1, h2β©
theorem mem_of_mem_diff {s t : set Ξ±} {x : Ξ±} (h : x β s \ t) : x β s :=
h.left
theorem not_mem_of_mem_diff {s t : set Ξ±} {x : Ξ±} (h : x β s \ t) : x β t :=
h.right
theorem diff_eq_compl_inter {s t : set Ξ±} : s \ t = tαΆ β© s :=
by rw [diff_eq, inter_comm]
theorem nonempty_diff {s t : set Ξ±} : (s \ t).nonempty β Β¬ (s β t) := inter_compl_nonempty_iff
theorem diff_subset (s t : set Ξ±) : s \ t β s := show s \ t β€ s, from sdiff_le
theorem union_diff_cancel' {s t u : set Ξ±} (hβ : s β t) (hβ : t β u) : t βͺ (u \ s) = u :=
sup_sdiff_cancel' hβ hβ
theorem union_diff_cancel {s t : set Ξ±} (h : s β t) : s βͺ (t \ s) = t :=
sup_sdiff_of_le h
theorem union_diff_cancel_left {s t : set Ξ±} (h : s β© t β β
) : (s βͺ t) \ s = t :=
disjoint.sup_sdiff_cancel_left h
theorem union_diff_cancel_right {s t : set Ξ±} (h : s β© t β β
) : (s βͺ t) \ t = s :=
disjoint.sup_sdiff_cancel_right h
@[simp] theorem union_diff_left {s t : set Ξ±} : (s βͺ t) \ s = t \ s :=
sup_sdiff_left_self
@[simp] theorem union_diff_right {s t : set Ξ±} : (s βͺ t) \ t = s \ t :=
sup_sdiff_right_self
theorem union_diff_distrib {s t u : set Ξ±} : (s βͺ t) \ u = s \ u βͺ t \ u :=
sup_sdiff
theorem inter_diff_assoc (a b c : set Ξ±) : (a β© b) \ c = a β© (b \ c) :=
inf_sdiff_assoc
@[simp] theorem inter_diff_self (a b : set Ξ±) : a β© (b \ a) = β
:=
inf_sdiff_self_right
@[simp] theorem inter_union_diff (s t : set Ξ±) : (s β© t) βͺ (s \ t) = s :=
sup_inf_sdiff s t
@[simp] lemma diff_union_inter (s t : set Ξ±) : (s \ t) βͺ (s β© t) = s :=
by { rw union_comm, exact sup_inf_sdiff _ _ }
@[simp] theorem inter_union_compl (s t : set Ξ±) : (s β© t) βͺ (s β© tαΆ) = s := inter_union_diff _ _
theorem diff_subset_diff {sβ sβ tβ tβ : set Ξ±} : sβ β sβ β tβ β tβ β sβ \ tβ β sβ \ tβ :=
show sβ β€ sβ β tβ β€ tβ β sβ \ tβ β€ sβ \ tβ, from sdiff_le_sdiff
theorem diff_subset_diff_left {sβ sβ t : set Ξ±} (h : sβ β sβ) : sβ \ t β sβ \ t :=
sdiff_le_self_sdiff βΉsβ β€ sββΊ
theorem diff_subset_diff_right {s t u : set Ξ±} (h : t β u) : s \ u β s \ t :=
sdiff_le_sdiff_self βΉt β€ uβΊ
theorem compl_eq_univ_diff (s : set Ξ±) : sαΆ = univ \ s :=
top_sdiff.symm
@[simp] lemma empty_diff (s : set Ξ±) : (β
\ s : set Ξ±) = β
:=
bot_sdiff
theorem diff_eq_empty {s t : set Ξ±} : s \ t = β
β s β t :=
sdiff_eq_bot_iff
@[simp] theorem diff_empty {s : set Ξ±} : s \ β
= s :=
sdiff_bot
@[simp] lemma diff_univ (s : set Ξ±) : s \ univ = β
:= diff_eq_empty.2 (subset_univ s)
theorem diff_diff {u : set Ξ±} : s \ t \ u = s \ (t βͺ u) :=
sdiff_sdiff_left
-- the following statement contains parentheses to help the reader
lemma diff_diff_comm {s t u : set Ξ±} : (s \ t) \ u = (s \ u) \ t :=
sdiff_sdiff_comm
lemma diff_subset_iff {s t u : set Ξ±} : s \ t β u β s β t βͺ u :=
show s \ t β€ u β s β€ t βͺ u, from sdiff_le_iff
lemma subset_diff_union (s t : set Ξ±) : s β (s \ t) βͺ t :=
show s β€ (s \ t) βͺ t, from le_sdiff_sup
lemma diff_union_of_subset {s t : set Ξ±} (h : t β s) :
(s \ t) βͺ t = s :=
subset.antisymm (union_subset (diff_subset _ _) h) (subset_diff_union _ _)
@[simp] lemma diff_singleton_subset_iff {x : Ξ±} {s t : set Ξ±} : s \ {x} β t β s β insert x t :=
by { rw [βunion_singleton, union_comm], apply diff_subset_iff }
lemma subset_diff_singleton {x : Ξ±} {s t : set Ξ±} (h : s β t) (hx : x β s) : s β t \ {x} :=
subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx
lemma subset_insert_diff_singleton (x : Ξ±) (s : set Ξ±) : s β insert x (s \ {x}) :=
by rw [βdiff_singleton_subset_iff]
lemma diff_subset_comm {s t u : set Ξ±} : s \ t β u β s \ u β t :=
show s \ t β€ u β s \ u β€ t, from sdiff_le_comm
lemma diff_inter {s t u : set Ξ±} : s \ (t β© u) = (s \ t) βͺ (s \ u) :=
sdiff_inf
lemma diff_inter_diff {s t u : set Ξ±} : s \ t β© (s \ u) = s \ (t βͺ u) :=
sdiff_sup.symm
lemma diff_compl : s \ tαΆ = s β© t := sdiff_compl
lemma diff_diff_right {s t u : set Ξ±} : s \ (t \ u) = (s \ t) βͺ (s β© u) :=
sdiff_sdiff_right'
@[simp] theorem insert_diff_of_mem (s) (h : a β t) : insert a s \ t = s \ t :=
by { ext, split; simp [or_imp_distrib, h] {contextual := tt} }
theorem insert_diff_of_not_mem (s) (h : a β t) : insert a s \ t = insert a (s \ t) :=
begin
classical,
ext x,
by_cases h' : x β t,
{ have : x β a,
{ assume H,
rw H at h',
exact h h' },
simp [h, h', this] },
{ simp [h, h'] }
end
lemma insert_diff_self_of_not_mem {a : Ξ±} {s : set Ξ±} (h : a β s) :
insert a s \ {a} = s :=
by { ext, simp [and_iff_left_of_imp (Ξ» hx : x β s, show x β a, from Ξ» hxa, h $ hxa βΈ hx)] }
lemma insert_inter_of_mem {sβ sβ : set Ξ±} {a : Ξ±} (h : a β sβ) :
insert a sβ β© sβ = insert a (sβ β© sβ) :=
by simp [set.insert_inter, h]
lemma insert_inter_of_not_mem {sβ sβ : set Ξ±} {a : Ξ±} (h : a β sβ) :
insert a sβ β© sβ = sβ β© sβ :=
begin
ext x,
simp only [mem_inter_iff, mem_insert_iff, mem_inter_eq, and.congr_left_iff, or_iff_right_iff_imp],
cc,
end
@[simp] theorem union_diff_self {s t : set Ξ±} : s βͺ (t \ s) = s βͺ t :=
sup_sdiff_self_right
@[simp] theorem diff_union_self {s t : set Ξ±} : (s \ t) βͺ t = s βͺ t :=
sup_sdiff_self_left
theorem diff_inter_self {a b : set Ξ±} : (b \ a) β© a = β
:=
inf_sdiff_self_left
theorem diff_inter_self_eq_diff {s t : set Ξ±} : s \ (t β© s) = s \ t :=
sdiff_inf_self_right
theorem diff_self_inter {s t : set Ξ±} : s \ (s β© t) = s \ t :=
sdiff_inf_self_left
theorem diff_eq_self {s t : set Ξ±} : s \ t = s β t β© s β β
:=
show s \ t = s β t β s β€ β₯, from sdiff_eq_self_iff_disjoint
@[simp] theorem diff_singleton_eq_self {a : Ξ±} {s : set Ξ±} (h : a β s) : s \ {a} = s :=
diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h]
@[simp] theorem insert_diff_singleton {a : Ξ±} {s : set Ξ±} :
insert a (s \ {a}) = insert a s :=
by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union]
@[simp] lemma diff_self {s : set Ξ±} : s \ s = β
:= sdiff_self
lemma diff_diff_cancel_left {s t : set Ξ±} (h : s β t) : t \ (t \ s) = s :=
sdiff_sdiff_eq_self h
lemma mem_diff_singleton {x y : Ξ±} {s : set Ξ±} : x β s \ {y} β (x β s β§ x β y) :=
iff.rfl
lemma mem_diff_singleton_empty {s : set Ξ±} {t : set (set Ξ±)} :
s β t \ {β
} β (s β t β§ s.nonempty) :=
mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty
lemma union_eq_diff_union_diff_union_inter (s t : set Ξ±) :
s βͺ t = (s \ t) βͺ (t \ s) βͺ (s β© t) :=
sup_eq_sdiff_sup_sdiff_sup_inf
/-! ### Powerset -/
theorem mem_powerset {x s : set Ξ±} (h : x β s) : x β powerset s := h
theorem subset_of_mem_powerset {x s : set Ξ±} (h : x β powerset s) : x β s := h
@[simp] theorem mem_powerset_iff (x s : set Ξ±) : x β powerset s β x β s := iff.rfl
theorem powerset_inter (s t : set Ξ±) : π« (s β© t) = π« s β© π« t :=
ext $ Ξ» u, subset_inter_iff
@[simp] theorem powerset_mono : π« s β π« t β s β t :=
β¨Ξ» h, h (subset.refl s), Ξ» h u hu, subset.trans hu hβ©
theorem monotone_powerset : monotone (powerset : set Ξ± β set (set Ξ±)) :=
Ξ» s t, powerset_mono.2
@[simp] theorem powerset_nonempty : (π« s).nonempty :=
β¨β
, empty_subset sβ©
@[simp] theorem powerset_empty : π« (β
: set Ξ±) = {β
} :=
ext $ Ξ» s, subset_empty_iff
@[simp] theorem powerset_univ : π« (univ : set Ξ±) = univ :=
eq_univ_of_forall subset_univ
/-! ### If-then-else for sets -/
/-- `ite` for sets: `set.ite t s s' β© t = s β© t`, `set.ite t s s' β© tαΆ = s' β© tαΆ`.
Defined as `s β© t βͺ s' \ t`. -/
protected def ite (t s s' : set Ξ±) : set Ξ± := s β© t βͺ s' \ t
@[simp] lemma ite_inter_self (t s s' : set Ξ±) : t.ite s s' β© t = s β© t :=
by rw [set.ite, union_inter_distrib_right, diff_inter_self, inter_assoc, inter_self, union_empty]
@[simp] lemma ite_compl (t s s' : set Ξ±) : tαΆ.ite s s' = t.ite s' s :=
by rw [set.ite, set.ite, diff_compl, union_comm, diff_eq]
@[simp] lemma ite_inter_compl_self (t s s' : set Ξ±) : t.ite s s' β© tαΆ = s' β© tαΆ :=
by rw [β ite_compl, ite_inter_self]
@[simp] lemma ite_diff_self (t s s' : set Ξ±) : t.ite s s' \ t = s' \ t :=
ite_inter_compl_self t s s'
@[simp] lemma ite_same (t s : set Ξ±) : t.ite s s = s := inter_union_diff _ _
@[simp] lemma ite_left (s t : set Ξ±) : s.ite s t = s βͺ t := by simp [set.ite]
@[simp] lemma ite_right (s t : set Ξ±) : s.ite t s = t β© s := by simp [set.ite]
@[simp] lemma ite_empty (s s' : set Ξ±) : set.ite β
s s' = s' :=
by simp [set.ite]
@[simp] lemma ite_univ (s s' : set Ξ±) : set.ite univ s s' = s :=
by simp [set.ite]
@[simp] lemma ite_empty_left (t s : set Ξ±) : t.ite β
s = s \ t :=
by simp [set.ite]
@[simp] lemma ite_empty_right (t s : set Ξ±) : t.ite s β
= s β© t :=
by simp [set.ite]
lemma ite_mono (t : set Ξ±) {sβ sβ' sβ sβ' : set Ξ±} (h : sβ β sβ) (h' : sβ' β sβ') :
t.ite sβ sβ' β t.ite sβ sβ' :=
union_subset_union (inter_subset_inter_left _ h) (inter_subset_inter_left _ h')
lemma ite_subset_union (t s s' : set Ξ±) : t.ite s s' β s βͺ s' :=
union_subset_union (inter_subset_left _ _) (diff_subset _ _)
lemma inter_subset_ite (t s s' : set Ξ±) : s β© s' β t.ite s s' :=
ite_same t (s β© s') βΈ ite_mono _ (inter_subset_left _ _) (inter_subset_right _ _)
lemma ite_inter_inter (t sβ sβ sβ' sβ' : set Ξ±) :
t.ite (sβ β© sβ) (sβ' β© sβ') = t.ite sβ sβ' β© t.ite sβ sβ' :=
by { ext x, finish [set.ite, iff_def] }
lemma ite_inter (t sβ sβ s : set Ξ±) :
t.ite (sβ β© s) (sβ β© s) = t.ite sβ sβ β© s :=
by rw [ite_inter_inter, ite_same]
lemma ite_inter_of_inter_eq (t : set Ξ±) {sβ sβ s : set Ξ±} (h : sβ β© s = sβ β© s) :
t.ite sβ sβ β© s = sβ β© s :=
by rw [β ite_inter, β h, ite_same]
lemma subset_ite {t s s' u : set Ξ±} : u β t.ite s s' β u β© t β s β§ u \ t β s' :=
begin
simp only [subset_def, β forall_and_distrib],
refine forall_congr (Ξ» x, _),
by_cases hx : x β t; simp [*, set.ite]
end
/-! ### Inverse image -/
/-- The preimage of `s : set Ξ²` by `f : Ξ± β Ξ²`, written `f β»ΒΉ' s`,
is the set of `x : Ξ±` such that `f x β s`. -/
def preimage {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ²) : set Ξ± := {x | f x β s}
infix ` β»ΒΉ' `:80 := preimage
section preimage
variables {f : Ξ± β Ξ²} {g : Ξ² β Ξ³}
@[simp] theorem preimage_empty : f β»ΒΉ' β
= β
:= rfl
@[simp] theorem mem_preimage {s : set Ξ²} {a : Ξ±} : (a β f β»ΒΉ' s) β (f a β s) := iff.rfl
lemma preimage_congr {f g : Ξ± β Ξ²} {s : set Ξ²} (h : β (x : Ξ±), f x = g x) : f β»ΒΉ' s = g β»ΒΉ' s :=
by { congr' with x, apply_assumption }
theorem preimage_mono {s t : set Ξ²} (h : s β t) : f β»ΒΉ' s β f β»ΒΉ' t :=
assume x hx, h hx
@[simp] theorem preimage_univ : f β»ΒΉ' univ = univ := rfl
theorem subset_preimage_univ {s : set Ξ±} : s β f β»ΒΉ' univ := subset_univ _
@[simp] theorem preimage_inter {s t : set Ξ²} : f β»ΒΉ' (s β© t) = f β»ΒΉ' s β© f β»ΒΉ' t := rfl
@[simp] theorem preimage_union {s t : set Ξ²} : f β»ΒΉ' (s βͺ t) = f β»ΒΉ' s βͺ f β»ΒΉ' t := rfl
@[simp] theorem preimage_compl {s : set Ξ²} : f β»ΒΉ' sαΆ = (f β»ΒΉ' s)αΆ := rfl
@[simp] theorem preimage_diff (f : Ξ± β Ξ²) (s t : set Ξ²) :
f β»ΒΉ' (s \ t) = f β»ΒΉ' s \ f β»ΒΉ' t := rfl
@[simp] theorem preimage_ite (f : Ξ± β Ξ²) (s tβ tβ : set Ξ²) :
f β»ΒΉ' (s.ite tβ tβ) = (f β»ΒΉ' s).ite (f β»ΒΉ' tβ) (f β»ΒΉ' tβ) :=
rfl
@[simp] theorem preimage_set_of_eq {p : Ξ± β Prop} {f : Ξ² β Ξ±} : f β»ΒΉ' {a | p a} = {a | p (f a)} :=
rfl
@[simp] theorem preimage_id {s : set Ξ±} : id β»ΒΉ' s = s := rfl
@[simp] theorem preimage_id' {s : set Ξ±} : (Ξ» x, x) β»ΒΉ' s = s := rfl
theorem preimage_const_of_mem {b : Ξ²} {s : set Ξ²} (h : b β s) :
(Ξ» (x : Ξ±), b) β»ΒΉ' s = univ :=
eq_univ_of_forall $ Ξ» x, h
theorem preimage_const_of_not_mem {b : Ξ²} {s : set Ξ²} (h : b β s) :
(Ξ» (x : Ξ±), b) β»ΒΉ' s = β
:=
eq_empty_of_subset_empty $ Ξ» x hx, h hx
theorem preimage_const (b : Ξ²) (s : set Ξ²) [decidable (b β s)] :
(Ξ» (x : Ξ±), b) β»ΒΉ' s = if b β s then univ else β
:=
by { split_ifs with hb hb, exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] }
theorem preimage_comp {s : set Ξ³} : (g β f) β»ΒΉ' s = f β»ΒΉ' (g β»ΒΉ' s) := rfl
lemma preimage_preimage {g : Ξ² β Ξ³} {f : Ξ± β Ξ²} {s : set Ξ³} :
f β»ΒΉ' (g β»ΒΉ' s) = (Ξ» x, g (f x)) β»ΒΉ' s :=
preimage_comp.symm
theorem eq_preimage_subtype_val_iff {p : Ξ± β Prop} {s : set (subtype p)} {t : set Ξ±} :
s = subtype.val β»ΒΉ' t β (βx (h : p x), (β¨x, hβ© : subtype p) β s β x β t) :=
β¨assume s_eq x h, by { rw [s_eq], simp },
assume h, ext $ Ξ» β¨x, hxβ©, by simp [h]β©
lemma preimage_coe_coe_diagonal {Ξ± : Type*} (s : set Ξ±) :
(prod.map coe coe) β»ΒΉ' (diagonal Ξ±) = diagonal s :=
begin
ext β¨β¨x, x_inβ©, β¨y, y_inβ©β©,
simp [set.diagonal],
end
end preimage
/-! ### Image of a set under a function -/
section image
infix ` '' `:80 := image
theorem mem_image_iff_bex {f : Ξ± β Ξ²} {s : set Ξ±} {y : Ξ²} :
y β f '' s β β x (_ : x β s), f x = y := bex_def.symm
theorem mem_image_eq (f : Ξ± β Ξ²) (s : set Ξ±) (y: Ξ²) : y β f '' s = β x, x β s β§ f x = y := rfl
@[simp] theorem mem_image (f : Ξ± β Ξ²) (s : set Ξ±) (y : Ξ²) :
y β f '' s β β x, x β s β§ f x = y := iff.rfl
lemma image_eta (f : Ξ± β Ξ²) : f '' s = (Ξ» x, f x) '' s := rfl
theorem mem_image_of_mem (f : Ξ± β Ξ²) {x : Ξ±} {a : set Ξ±} (h : x β a) : f x β f '' a :=
β¨_, h, rflβ©
theorem mem_image_of_injective {f : Ξ± β Ξ²} {a : Ξ±} {s : set Ξ±} (hf : injective f) :
f a β f '' s β a β s :=
iff.intro
(assume β¨b, hb, eqβ©, (hf eq) βΈ hb)
(assume h, mem_image_of_mem _ h)
theorem ball_image_iff {f : Ξ± β Ξ²} {s : set Ξ±} {p : Ξ² β Prop} :
(β y β f '' s, p y) β (β x β s, p (f x)) :=
by simp
theorem ball_image_of_ball {f : Ξ± β Ξ²} {s : set Ξ±} {p : Ξ² β Prop}
(h : β x β s, p (f x)) : β y β f '' s, p y :=
ball_image_iff.2 h
theorem bex_image_iff {f : Ξ± β Ξ²} {s : set Ξ±} {p : Ξ² β Prop} :
(β y β f '' s, p y) β (β x β s, p (f x)) :=
by simp
theorem mem_image_elim {f : Ξ± β Ξ²} {s : set Ξ±} {C : Ξ² β Prop} (h : β (x : Ξ±), x β s β C (f x)) :
β{y : Ξ²}, y β f '' s β C y
| ._ β¨a, a_in, rflβ© := h a a_in
theorem mem_image_elim_on {f : Ξ± β Ξ²} {s : set Ξ±} {C : Ξ² β Prop} {y : Ξ²} (h_y : y β f '' s)
(h : β (x : Ξ±), x β s β C (f x)) : C y :=
mem_image_elim h h_y
@[congr] lemma image_congr {f g : Ξ± β Ξ²} {s : set Ξ±}
(h : βaβs, f a = g a) : f '' s = g '' s :=
by safe [ext_iff, iff_def]
/-- A common special case of `image_congr` -/
lemma image_congr' {f g : Ξ± β Ξ²} {s : set Ξ±} (h : β (x : Ξ±), f x = g x) : f '' s = g '' s :=
image_congr (Ξ»x _, h x)
theorem image_comp (f : Ξ² β Ξ³) (g : Ξ± β Ξ²) (a : set Ξ±) : (f β g) '' a = f '' (g '' a) :=
subset.antisymm
(ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha)
(ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha)
/-- A variant of `image_comp`, useful for rewriting -/
lemma image_image (g : Ξ² β Ξ³) (f : Ξ± β Ξ²) (s : set Ξ±) : g '' (f '' s) = (Ξ» x, g (f x)) '' s :=
(image_comp g f s).symm
/-- Image is monotone with respect to `β`. See `set.monotone_image` for the statement in
terms of `β€`. -/
theorem image_subset {a b : set Ξ±} (f : Ξ± β Ξ²) (h : a β b) : f '' a β f '' b :=
by finish [subset_def, mem_image_eq]
theorem image_union (f : Ξ± β Ξ²) (s t : set Ξ±) :
f '' (s βͺ t) = f '' s βͺ f '' t :=
ext $ Ξ» x, β¨by rintro β¨a, h|h, rflβ©; [left, right]; exact β¨_, h, rflβ©,
by rintro (β¨a, h, rflβ© | β¨a, h, rflβ©); refine β¨_, _, rflβ©; [left, right]; exact hβ©
@[simp] theorem image_empty (f : Ξ± β Ξ²) : f '' β
= β
:= by { ext, simp }
lemma image_inter_subset (f : Ξ± β Ξ²) (s t : set Ξ±) :
f '' (s β© t) β f '' s β© f '' t :=
subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _)
theorem image_inter_on {f : Ξ± β Ξ²} {s t : set Ξ±} (h : βxβt, βyβs, f x = f y β x = y) :
f '' s β© f '' t = f '' (s β© t) :=
subset.antisymm
(assume b β¨β¨aβ, haβ, hββ©, β¨aβ, haβ, hββ©β©,
have aβ = aβ, from h _ haβ _ haβ (by simp *),
β¨aβ, β¨haβ, this βΈ haββ©, hββ©)
(image_inter_subset _ _ _)
theorem image_inter {f : Ξ± β Ξ²} {s t : set Ξ±} (H : injective f) :
f '' s β© f '' t = f '' (s β© t) :=
image_inter_on (assume x _ y _ h, H h)
theorem image_univ_of_surjective {ΞΉ : Type*} {f : ΞΉ β Ξ²} (H : surjective f) : f '' univ = univ :=
eq_univ_of_forall $ by { simpa [image] }
@[simp] theorem image_singleton {f : Ξ± β Ξ²} {a : Ξ±} : f '' {a} = {f a} :=
by { ext, simp [image, eq_comm] }
@[simp] theorem nonempty.image_const {s : set Ξ±} (hs : s.nonempty) (a : Ξ²) : (Ξ» _, a) '' s = {a} :=
ext $ Ξ» x, β¨Ξ» β¨y, _, hβ©, h βΈ mem_singleton _,
Ξ» h, (eq_of_mem_singleton h).symm βΈ hs.imp (Ξ» y hy, β¨hy, rflβ©)β©
@[simp] lemma image_eq_empty {Ξ± Ξ²} {f : Ξ± β Ξ²} {s : set Ξ±} : f '' s = β
β s = β
:=
by { simp only [eq_empty_iff_forall_not_mem],
exact β¨Ξ» H a ha, H _ β¨_, ha, rflβ©, Ξ» H b β¨_, ha, _β©, H _ haβ© }
-- TODO(Jeremy): there is an issue with - t unfolding to compl t
theorem mem_compl_image (t : set Ξ±) (S : set (set Ξ±)) :
t β compl '' S β tαΆ β S :=
begin
suffices : β x, xαΆ = t β tαΆ = x, { simp [this] },
intro x, split; { intro e, subst e, simp }
end
/-- A variant of `image_id` -/
@[simp] lemma image_id' (s : set Ξ±) : (Ξ»x, x) '' s = s := by { ext, simp }
theorem image_id (s : set Ξ±) : id '' s = s := by simp
theorem compl_compl_image (S : set (set Ξ±)) :
compl '' (compl '' S) = S :=
by rw [β image_comp, compl_comp_compl, image_id]
theorem image_insert_eq {f : Ξ± β Ξ²} {a : Ξ±} {s : set Ξ±} :
f '' (insert a s) = insert (f a) (f '' s) :=
by { ext, simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] }
theorem image_pair (f : Ξ± β Ξ²) (a b : Ξ±) : f '' {a, b} = {f a, f b} :=
by simp only [image_insert_eq, image_singleton]
theorem image_subset_preimage_of_inverse {f : Ξ± β Ξ²} {g : Ξ² β Ξ±}
(I : left_inverse g f) (s : set Ξ±) : f '' s β g β»ΒΉ' s :=
Ξ» b β¨a, h, eβ©, e βΈ ((I a).symm βΈ h : g (f a) β s)
theorem preimage_subset_image_of_inverse {f : Ξ± β Ξ²} {g : Ξ² β Ξ±}
(I : left_inverse g f) (s : set Ξ²) : f β»ΒΉ' s β g '' s :=
Ξ» b h, β¨f b, h, I bβ©
theorem image_eq_preimage_of_inverse {f : Ξ± β Ξ²} {g : Ξ² β Ξ±}
(hβ : left_inverse g f) (hβ : right_inverse g f) :
image f = preimage g :=
funext $ Ξ» s, subset.antisymm
(image_subset_preimage_of_inverse hβ s)
(preimage_subset_image_of_inverse hβ s)
theorem mem_image_iff_of_inverse {f : Ξ± β Ξ²} {g : Ξ² β Ξ±} {b : Ξ²} {s : set Ξ±}
(hβ : left_inverse g f) (hβ : right_inverse g f) :
b β f '' s β g b β s :=
by rw image_eq_preimage_of_inverse hβ hβ; refl
theorem image_compl_subset {f : Ξ± β Ξ²} {s : set Ξ±} (H : injective f) : f '' sαΆ β (f '' s)αΆ :=
subset_compl_iff_disjoint.2 $ by simp [image_inter H]
theorem subset_image_compl {f : Ξ± β Ξ²} {s : set Ξ±} (H : surjective f) : (f '' s)αΆ β f '' sαΆ :=
compl_subset_iff_union.2 $
by { rw β image_union, simp [image_univ_of_surjective H] }
theorem image_compl_eq {f : Ξ± β Ξ²} {s : set Ξ±} (H : bijective f) : f '' sαΆ = (f '' s)αΆ :=
subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2)
theorem subset_image_diff (f : Ξ± β Ξ²) (s t : set Ξ±) :
f '' s \ f '' t β f '' (s \ t) :=
begin
rw [diff_subset_iff, β image_union, union_diff_self],
exact image_subset f (subset_union_right t s)
end
theorem image_diff {f : Ξ± β Ξ²} (hf : injective f) (s t : set Ξ±) :
f '' (s \ t) = f '' s \ f '' t :=
subset.antisymm
(subset.trans (image_inter_subset _ _ _) $ inter_subset_inter_right _ $ image_compl_subset hf)
(subset_image_diff f s t)
lemma nonempty.image (f : Ξ± β Ξ²) {s : set Ξ±} : s.nonempty β (f '' s).nonempty
| β¨x, hxβ© := β¨f x, mem_image_of_mem f hxβ©
lemma nonempty.of_image {f : Ξ± β Ξ²} {s : set Ξ±} : (f '' s).nonempty β s.nonempty
| β¨y, x, hx, _β© := β¨x, hxβ©
@[simp] lemma nonempty_image_iff {f : Ξ± β Ξ²} {s : set Ξ±} :
(f '' s).nonempty β s.nonempty :=
β¨nonempty.of_image, Ξ» h, h.image fβ©
lemma nonempty.preimage {s : set Ξ²} (hs : s.nonempty) {f : Ξ± β Ξ²} (hf : surjective f) :
(f β»ΒΉ' s).nonempty :=
let β¨y, hyβ© := hs, β¨x, hxβ© := hf y in β¨x, mem_preimage.2 $ hx.symm βΈ hyβ©
instance (f : Ξ± β Ξ²) (s : set Ξ±) [nonempty s] : nonempty (f '' s) :=
(set.nonempty.image f nonempty_of_nonempty_subtype).to_subtype
/-- image and preimage are a Galois connection -/
@[simp] theorem image_subset_iff {s : set Ξ±} {t : set Ξ²} {f : Ξ± β Ξ²} :
f '' s β t β s β f β»ΒΉ' t :=
ball_image_iff
theorem image_preimage_subset (f : Ξ± β Ξ²) (s : set Ξ²) :
f '' (f β»ΒΉ' s) β s :=
image_subset_iff.2 (subset.refl _)
theorem subset_preimage_image (f : Ξ± β Ξ²) (s : set Ξ±) :
s β f β»ΒΉ' (f '' s) :=
Ξ» x, mem_image_of_mem f
theorem preimage_image_eq {f : Ξ± β Ξ²} (s : set Ξ±) (h : injective f) : f β»ΒΉ' (f '' s) = s :=
subset.antisymm
(Ξ» x β¨y, hy, eβ©, h e βΈ hy)
(subset_preimage_image f s)
theorem image_preimage_eq {f : Ξ± β Ξ²} (s : set Ξ²) (h : surjective f) : f '' (f β»ΒΉ' s) = s :=
subset.antisymm
(image_preimage_subset f s)
(Ξ» x hx, let β¨y, eβ© := h x in β¨y, (e.symm βΈ hx : f y β s), eβ©)
lemma preimage_eq_preimage {f : Ξ² β Ξ±} (hf : surjective f) : f β»ΒΉ' s = f β»ΒΉ' t β s = t :=
iff.intro
(assume eq, by rw [β image_preimage_eq s hf, β image_preimage_eq t hf, eq])
(assume eq, eq βΈ rfl)
lemma image_inter_preimage (f : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) :
f '' (s β© f β»ΒΉ' t) = f '' s β© t :=
begin
apply subset.antisymm,
{ calc f '' (s β© f β»ΒΉ' t) β f '' s β© (f '' (fβ»ΒΉ' t)) : image_inter_subset _ _ _
... β f '' s β© t : inter_subset_inter_right _ (image_preimage_subset f t) },
{ rintros _ β¨β¨x, h', rflβ©, hβ©,
exact β¨x, β¨h', hβ©, rflβ© }
end
lemma image_preimage_inter (f : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) :
f '' (f β»ΒΉ' t β© s) = t β© f '' s :=
by simp only [inter_comm, image_inter_preimage]
@[simp] lemma image_inter_nonempty_iff {f : Ξ± β Ξ²} {s : set Ξ±} {t : set Ξ²} :
(f '' s β© t).nonempty β (s β© f β»ΒΉ' t).nonempty :=
by rw [βimage_inter_preimage, nonempty_image_iff]
lemma image_diff_preimage {f : Ξ± β Ξ²} {s : set Ξ±} {t : set Ξ²} : f '' (s \ f β»ΒΉ' t) = f '' s \ t :=
by simp_rw [diff_eq, β preimage_compl, image_inter_preimage]
theorem compl_image : image (compl : set Ξ± β set Ξ±) = preimage compl :=
image_eq_preimage_of_inverse compl_compl compl_compl
theorem compl_image_set_of {p : set Ξ± β Prop} :
compl '' {s | p s} = {s | p sαΆ} :=
congr_fun compl_image p
theorem inter_preimage_subset (s : set Ξ±) (t : set Ξ²) (f : Ξ± β Ξ²) :
s β© f β»ΒΉ' t β f β»ΒΉ' (f '' s β© t) :=
Ξ» x h, β¨mem_image_of_mem _ h.left, h.rightβ©
theorem union_preimage_subset (s : set Ξ±) (t : set Ξ²) (f : Ξ± β Ξ²) :
s βͺ f β»ΒΉ' t β f β»ΒΉ' (f '' s βͺ t) :=
Ξ» x h, or.elim h (Ξ» l, or.inl $ mem_image_of_mem _ l) (Ξ» r, or.inr r)
theorem subset_image_union (f : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) :
f '' (s βͺ f β»ΒΉ' t) β f '' s βͺ t :=
image_subset_iff.2 (union_preimage_subset _ _ _)
lemma preimage_subset_iff {A : set Ξ±} {B : set Ξ²} {f : Ξ± β Ξ²} :
fβ»ΒΉ' B β A β (β a : Ξ±, f a β B β a β A) := iff.rfl
lemma image_eq_image {f : Ξ± β Ξ²} (hf : injective f) : f '' s = f '' t β s = t :=
iff.symm $ iff.intro (assume eq, eq βΈ rfl) $ assume eq,
by rw [β preimage_image_eq s hf, β preimage_image_eq t hf, eq]
lemma image_subset_image_iff {f : Ξ± β Ξ²} (hf : injective f) : f '' s β f '' t β s β t :=
begin
refine (iff.symm $ iff.intro (image_subset f) $ assume h, _),
rw [β preimage_image_eq s hf, β preimage_image_eq t hf],
exact preimage_mono h
end
lemma prod_quotient_preimage_eq_image [s : setoid Ξ±] (g : quotient s β Ξ²) {h : Ξ± β Ξ²}
(Hh : h = g β quotient.mk) (r : set (Ξ² Γ Ξ²)) :
{x : quotient s Γ quotient s | (g x.1, g x.2) β r} =
(Ξ» a : Ξ± Γ Ξ±, (β¦a.1β§, β¦a.2β§)) '' ((Ξ» a : Ξ± Γ Ξ±, (h a.1, h a.2)) β»ΒΉ' r) :=
Hh.symm βΈ set.ext (Ξ» β¨aβ, aββ©, β¨quotient.induction_onβ aβ aβ
(Ξ» aβ aβ h, β¨(aβ, aβ), h, rflβ©),
Ξ» β¨β¨bβ, bββ©, hβ, hββ©, show (g aβ, g aβ) β r, from
have hβ : β¦bββ§ = aβ β§ β¦bββ§ = aβ := prod.ext_iff.1 hβ,
hβ.1 βΈ hβ.2 βΈ hββ©)
/-- Restriction of `f` to `s` factors through `s.image_factorization f : s β f '' s`. -/
def image_factorization (f : Ξ± β Ξ²) (s : set Ξ±) : s β f '' s :=
Ξ» p, β¨f p.1, mem_image_of_mem f p.2β©
lemma image_factorization_eq {f : Ξ± β Ξ²} {s : set Ξ±} :
subtype.val β image_factorization f s = f β subtype.val :=
funext $ Ξ» p, rfl
lemma surjective_onto_image {f : Ξ± β Ξ²} {s : set Ξ±} :
surjective (image_factorization f s) :=
Ξ» β¨_, β¨a, ha, rflβ©β©, β¨β¨a, haβ©, rflβ©
end image
/-! ### Subsingleton -/
/-- A set `s` is a `subsingleton`, if it has at most one element. -/
protected def subsingleton (s : set Ξ±) : Prop :=
β β¦xβ¦ (hx : x β s) β¦yβ¦ (hy : y β s), x = y
lemma subsingleton.mono (ht : t.subsingleton) (hst : s β t) : s.subsingleton :=
Ξ» x hx y hy, ht (hst hx) (hst hy)
lemma subsingleton.image (hs : s.subsingleton) (f : Ξ± β Ξ²) : (f '' s).subsingleton :=
Ξ» _ β¨x, hx, Hxβ© _ β¨y, hy, Hyβ©, Hx βΈ Hy βΈ congr_arg f (hs hx hy)
lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:Ξ±} (hx : x β s) :
s = {x} :=
ext $ Ξ» y, β¨Ξ» hy, (hs hx hy) βΈ mem_singleton _, Ξ» hy, (eq_of_mem_singleton hy).symm βΈ hxβ©
@[simp] lemma subsingleton_empty : (β
: set Ξ±).subsingleton := Ξ» x, false.elim
@[simp] lemma subsingleton_singleton {a} : ({a} : set Ξ±).subsingleton :=
Ξ» x hx y hy, (eq_of_mem_singleton hx).symm βΈ (eq_of_mem_singleton hy).symm βΈ rfl
lemma subsingleton_iff_singleton {x} (hx : x β s) : s.subsingleton β s = {x} :=
β¨Ξ» h, h.eq_singleton_of_mem hx, Ξ» h,h.symm βΈ subsingleton_singletonβ©
lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) :
s = β
β¨ β x, s = {x} :=
s.eq_empty_or_nonempty.elim or.inl (Ξ» β¨x, hxβ©, or.inr β¨x, hs.eq_singleton_of_mem hxβ©)
lemma subsingleton.induction_on {p : set Ξ± β Prop} (hs : s.subsingleton) (he : p β
)
(hβ : β x, p {x}) : p s :=
by { rcases hs.eq_empty_or_singleton with rfl|β¨x, rflβ©, exacts [he, hβ _] }
lemma subsingleton_univ [subsingleton Ξ±] : (univ : set Ξ±).subsingleton :=
Ξ» x hx y hy, subsingleton.elim x y
/-- `s`, coerced to a type, is a subsingleton type if and only if `s`
is a subsingleton set. -/
@[simp, norm_cast] lemma subsingleton_coe (s : set Ξ±) : subsingleton s β s.subsingleton :=
begin
split,
{ refine Ξ» h, (Ξ» a ha b hb, _),
exact set_coe.ext_iff.2 (@subsingleton.elim s h β¨a, haβ© β¨b, hbβ©) },
{ exact Ξ» h, subsingleton.intro (Ξ» a b, set_coe.ext (h a.property b.property)) }
end
/-- `s` is a subsingleton, if its image of an injective function is. -/
theorem subsingleton_of_image {Ξ± Ξ² : Type*} {f : Ξ± β Ξ²} (hf : function.injective f)
(s : set Ξ±) (hs : (f '' s).subsingleton) : s.subsingleton :=
Ξ» a ha b hb, hf $ hs (mem_image_of_mem _ ha) (mem_image_of_mem _ hb)
theorem univ_eq_true_false : univ = ({true, false} : set Prop) :=
eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp)
/-! ### Lemmas about range of a function. -/
section range
variables {f : ΞΉ β Ξ±}
open function
/-- Range of a function.
This function is more flexible than `f '' univ`, as the image requires that the domain is in Type
and not an arbitrary Sort. -/
def range (f : ΞΉ β Ξ±) : set Ξ± := {x | βy, f y = x}
@[simp] theorem mem_range {x : Ξ±} : x β range f β β y, f y = x := iff.rfl
@[simp] theorem mem_range_self (i : ΞΉ) : f i β range f := β¨i, rflβ©
theorem forall_range_iff {p : Ξ± β Prop} : (β a β range f, p a) β (β i, p (f i)) :=
by simp
theorem exists_range_iff {p : Ξ± β Prop} : (β a β range f, p a) β (β i, p (f i)) :=
by simp
lemma exists_range_iff' {p : Ξ± β Prop} :
(β a, a β range f β§ p a) β β i, p (f i) :=
by simpa only [exists_prop] using exists_range_iff
theorem range_iff_surjective : range f = univ β surjective f :=
eq_univ_iff_forall
alias range_iff_surjective β _ function.surjective.range_eq
@[simp] theorem range_id : range (@id Ξ±) = univ := range_iff_surjective.2 surjective_id
theorem is_compl_range_inl_range_inr : is_compl (range $ @sum.inl Ξ± Ξ²) (range sum.inr) :=
β¨by { rintro y β¨β¨xβ, rflβ©, β¨xβ, _β©β©, cc },
by { rintro (x|y) -; [left, right]; exact mem_range_self _ }β©
@[simp] theorem range_inl_union_range_inr : range (sum.inl : Ξ± β Ξ± β Ξ²) βͺ range sum.inr = univ :=
is_compl_range_inl_range_inr.sup_eq_top
@[simp] theorem range_inl_inter_range_inr : range (sum.inl : Ξ± β Ξ± β Ξ²) β© range sum.inr = β
:=
is_compl_range_inl_range_inr.inf_eq_bot
@[simp] theorem range_inr_union_range_inl : range (sum.inr : Ξ² β Ξ± β Ξ²) βͺ range sum.inl = univ :=
is_compl_range_inl_range_inr.symm.sup_eq_top
@[simp] theorem range_inr_inter_range_inl : range (sum.inr : Ξ² β Ξ± β Ξ²) β© range sum.inl = β
:=
is_compl_range_inl_range_inr.symm.inf_eq_bot
@[simp] theorem preimage_inl_range_inr : sum.inl β»ΒΉ' range (sum.inr : Ξ² β Ξ± β Ξ²) = β
:=
by { ext, simp }
@[simp] theorem preimage_inr_range_inl : sum.inr β»ΒΉ' range (sum.inl : Ξ± β Ξ± β Ξ²) = β
:=
by { ext, simp }
@[simp] theorem range_quot_mk (r : Ξ± β Ξ± β Prop) : range (quot.mk r) = univ :=
(surjective_quot_mk r).range_eq
@[simp] theorem image_univ {ΞΉ : Type*} {f : ΞΉ β Ξ²} : f '' univ = range f :=
by { ext, simp [image, range] }
theorem image_subset_range {ΞΉ : Type*} (f : ΞΉ β Ξ²) (s : set ΞΉ) : f '' s β range f :=
by rw β image_univ; exact image_subset _ (subset_univ _)
theorem range_comp (g : Ξ± β Ξ²) (f : ΞΉ β Ξ±) : range (g β f) = g '' range f :=
subset.antisymm
(forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _))
(ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self)
theorem range_subset_iff {s : set Ξ±} : range f β s β β y, f y β s :=
forall_range_iff
lemma range_comp_subset_range (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) : range (g β f) β range g :=
by rw range_comp; apply image_subset_range
lemma range_nonempty_iff_nonempty : (range f).nonempty β nonempty ΞΉ :=
β¨Ξ» β¨y, x, hxyβ©, β¨xβ©, Ξ» β¨xβ©, β¨f x, mem_range_self xβ©β©
lemma range_nonempty [h : nonempty ΞΉ] (f : ΞΉ β Ξ±) : (range f).nonempty :=
range_nonempty_iff_nonempty.2 h
@[simp] lemma range_eq_empty {f : ΞΉ β Ξ±} : range f = β
β Β¬ nonempty ΞΉ :=
not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty
instance [nonempty ΞΉ] (f : ΞΉ β Ξ±) : nonempty (range f) := (range_nonempty f).to_subtype
@[simp] lemma image_union_image_compl_eq_range (f : Ξ± β Ξ²) :
(f '' s) βͺ (f '' sαΆ) = range f :=
by rw [β image_union, β image_univ, β union_compl_self]
theorem image_preimage_eq_inter_range {f : Ξ± β Ξ²} {t : set Ξ²} :
f '' (f β»ΒΉ' t) = t β© range f :=
ext $ assume x, β¨assume β¨x, hx, heqβ©, heq βΈ β¨hx, mem_range_self _β©,
assume β¨hx, β¨y, h_eqβ©β©, h_eq βΈ mem_image_of_mem f $
show y β f β»ΒΉ' t, by simp [preimage, h_eq, hx]β©
lemma image_preimage_eq_of_subset {f : Ξ± β Ξ²} {s : set Ξ²} (hs : s β range f) :
f '' (f β»ΒΉ' s) = s :=
by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs]
lemma image_preimage_eq_iff {f : Ξ± β Ξ²} {s : set Ξ²} : f '' (f β»ΒΉ' s) = s β s β range f :=
β¨by { intro h, rw [β h], apply image_subset_range }, image_preimage_eq_of_subsetβ©
lemma preimage_subset_preimage_iff {s t : set Ξ±} {f : Ξ² β Ξ±} (hs : s β range f) :
f β»ΒΉ' s β f β»ΒΉ' t β s β t :=
begin
split,
{ intros h x hx, rcases hs hx with β¨y, rflβ©, exact h hx },
intros h x, apply h
end
lemma preimage_eq_preimage' {s t : set Ξ±} {f : Ξ² β Ξ±} (hs : s β range f) (ht : t β range f) :
f β»ΒΉ' s = f β»ΒΉ' t β s = t :=
begin
split,
{ intro h, apply subset.antisymm, rw [βpreimage_subset_preimage_iff hs, h],
rw [βpreimage_subset_preimage_iff ht, h] },
rintro rfl, refl
end
@[simp] theorem preimage_inter_range {f : Ξ± β Ξ²} {s : set Ξ²} : f β»ΒΉ' (s β© range f) = f β»ΒΉ' s :=
set.ext $ Ξ» x, and_iff_left β¨x, rflβ©
@[simp] theorem preimage_range_inter {f : Ξ± β Ξ²} {s : set Ξ²} : f β»ΒΉ' (range f β© s) = f β»ΒΉ' s :=
by rw [inter_comm, preimage_inter_range]
theorem preimage_image_preimage {f : Ξ± β Ξ²} {s : set Ξ²} :
f β»ΒΉ' (f '' (f β»ΒΉ' s)) = f β»ΒΉ' s :=
by rw [image_preimage_eq_inter_range, preimage_inter_range]
@[simp] theorem quot_mk_range_eq [setoid Ξ±] : range (Ξ»x : Ξ±, β¦xβ§) = univ :=
range_iff_surjective.2 quot.exists_rep
lemma range_const_subset {c : Ξ±} : range (Ξ»x:ΞΉ, c) β {c} :=
range_subset_iff.2 $ Ξ» x, rfl
@[simp] lemma range_const : β [nonempty ΞΉ] {c : Ξ±}, range (Ξ»x:ΞΉ, c) = {c}
| β¨xβ© c := subset.antisymm range_const_subset $
assume y hy, (mem_singleton_iff.1 hy).symm βΈ mem_range_self x
lemma diagonal_eq_range {Ξ± : Type*} : diagonal Ξ± = range (Ξ» x, (x, x)) :=
by { ext β¨x, yβ©, simp [diagonal, eq_comm] }
theorem preimage_singleton_nonempty {f : Ξ± β Ξ²} {y : Ξ²} :
(f β»ΒΉ' {y}).nonempty β y β range f :=
iff.rfl
theorem preimage_singleton_eq_empty {f : Ξ± β Ξ²} {y : Ξ²} :
f β»ΒΉ' {y} = β
β y β range f :=
not_nonempty_iff_eq_empty.symm.trans $ not_congr preimage_singleton_nonempty
lemma range_subset_singleton {f : ΞΉ β Ξ±} {x : Ξ±} : range f β {x} β f = const ΞΉ x :=
by simp [range_subset_iff, funext_iff, mem_singleton]
lemma image_compl_preimage {f : Ξ± β Ξ²} {s : set Ξ²} : f '' ((f β»ΒΉ' s)αΆ) = range f \ s :=
by rw [compl_eq_univ_diff, image_diff_preimage, image_univ]
@[simp] theorem range_sigma_mk {Ξ² : Ξ± β Type*} (a : Ξ±) :
range (sigma.mk a : Ξ² a β Ξ£ a, Ξ² a) = sigma.fst β»ΒΉ' {a} :=
begin
apply subset.antisymm,
{ rintros _ β¨b, rflβ©, simp },
{ rintros β¨x, yβ© (rfl|_),
exact mem_range_self y }
end
/-- Any map `f : ΞΉ β Ξ²` factors through a map `range_factorization f : ΞΉ β range f`. -/
def range_factorization (f : ΞΉ β Ξ²) : ΞΉ β range f :=
Ξ» i, β¨f i, mem_range_self iβ©
lemma range_factorization_eq {f : ΞΉ β Ξ²} :
subtype.val β range_factorization f = f :=
funext $ Ξ» i, rfl
lemma surjective_onto_range : surjective (range_factorization f) :=
Ξ» β¨_, β¨i, rflβ©β©, β¨i, rflβ©
lemma image_eq_range (f : Ξ± β Ξ²) (s : set Ξ±) : f '' s = range (Ξ»(x : s), f x) :=
by { ext, split, rintro β¨x, h1, h2β©, exact β¨β¨x, h1β©, h2β©, rintro β¨β¨x, h1β©, h2β©, exact β¨x, h1, h2β© }
@[simp] lemma sum.elim_range {Ξ± Ξ² Ξ³ : Type*} (f : Ξ± β Ξ³) (g : Ξ² β Ξ³) :
range (sum.elim f g) = range f βͺ range g :=
by simp [set.ext_iff, mem_range]
lemma range_ite_subset' {p : Prop} [decidable p] {f g : Ξ± β Ξ²} :
range (if p then f else g) β range f βͺ range g :=
begin
by_cases h : p, {rw if_pos h, exact subset_union_left _ _},
{rw if_neg h, exact subset_union_right _ _}
end
lemma range_ite_subset {p : Ξ± β Prop} [decidable_pred p] {f g : Ξ± β Ξ²} :
range (Ξ» x, if p x then f x else g x) β range f βͺ range g :=
begin
rw range_subset_iff, intro x, by_cases h : p x,
simp [if_pos h, mem_union, mem_range_self],
simp [if_neg h, mem_union, mem_range_self]
end
@[simp] lemma preimage_range (f : Ξ± β Ξ²) : f β»ΒΉ' (range f) = univ :=
eq_univ_of_forall mem_range_self
/-- The range of a function from a `unique` type contains just the
function applied to its single value. -/
lemma range_unique [h : unique ΞΉ] : range f = {f $ default ΞΉ} :=
begin
ext x,
rw mem_range,
split,
{ rintros β¨i, hiβ©,
rw h.uniq i at hi,
exact hi βΈ mem_singleton _ },
{ exact Ξ» h, β¨default ΞΉ, h.symmβ© }
end
lemma range_diff_image_subset (f : Ξ± β Ξ²) (s : set Ξ±) :
range f \ f '' s β f '' sαΆ :=
Ξ» y β¨β¨x, hββ©, hββ©, β¨x, Ξ» h, hβ β¨x, h, hββ©, hββ©
lemma range_diff_image {f : Ξ± β Ξ²} (H : injective f) (s : set Ξ±) :
range f \ f '' s = f '' sαΆ :=
subset.antisymm (range_diff_image_subset f s) $ Ξ» y β¨x, hx, hyβ©, hy βΈ
β¨mem_range_self _, Ξ» β¨x', hx', eqβ©, hx $ H eq βΈ hx'β©
end range
/-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y β s`. -/
def pairwise_on (s : set Ξ±) (r : Ξ± β Ξ± β Prop) := β x β s, β y β s, x β y β r x y
lemma pairwise_on_of_forall (s : set Ξ±) (p : Ξ± β Ξ± β Prop) (h : β (a b : Ξ±), p a b) :
pairwise_on s p :=
Ξ» a _ b _ _, h a b
lemma pairwise_on.imp_on {s : set Ξ±} {p q : Ξ± β Ξ± β Prop}
(h : pairwise_on s p) (hpq : pairwise_on s (Ξ» β¦a b : Ξ±β¦, p a b β q a b)) : pairwise_on s q :=
Ξ» a ha b hb hab, hpq a ha b hb hab (h a ha b hb hab)
lemma pairwise_on.imp {s : set Ξ±} {p q : Ξ± β Ξ± β Prop}
(h : pairwise_on s p) (hpq : β β¦a b : Ξ±β¦, p a b β q a b) : pairwise_on s q :=
h.imp_on (pairwise_on_of_forall s _ hpq)
theorem pairwise_on.mono {s t : set Ξ±} {r}
(h : t β s) (hp : pairwise_on s r) : pairwise_on t r :=
Ξ» x xt y yt, hp x (h xt) y (h yt)
theorem pairwise_on.mono' {s : set Ξ±} {r r' : Ξ± β Ξ± β Prop}
(H : r β€ r') (hp : pairwise_on s r) : pairwise_on s r' :=
hp.imp H
theorem pairwise_on_top (s : set Ξ±) :
pairwise_on s β€ :=
pairwise_on_of_forall s _ (Ξ» a b, trivial)
/-- If and only if `f` takes pairwise equal values on `s`, there is
some value it takes everywhere on `s`. -/
lemma pairwise_on_eq_iff_exists_eq [nonempty Ξ²] (s : set Ξ±) (f : Ξ± β Ξ²) :
(pairwise_on s (Ξ» x y, f x = f y)) β β z, β x β s, f x = z :=
begin
split,
{ intro h,
rcases eq_empty_or_nonempty s with rfl | β¨x, hxβ©,
{ exact β¨classical.arbitrary Ξ², Ξ» x hx, false.elim hxβ© },
{ use f x,
intros y hy,
by_cases hyx : y = x,
{ rw hyx },
{ exact h y hy x hx hyx } } },
{ rintros β¨z, hzβ© x hx y hy hne,
rw [hz x hx, hz y hy] }
end
protected lemma subsingleton.pairwise_on (h : s.subsingleton) (r : Ξ± β Ξ± β Prop) :
pairwise_on s r :=
Ξ» x hx y hy hne, (hne (h hx hy)).elim
@[simp] lemma pairwise_on_empty (r : Ξ± β Ξ± β Prop) :
(β
: set Ξ±).pairwise_on r :=
subsingleton_empty.pairwise_on r
@[simp] lemma pairwise_on_singleton (a : Ξ±) (r : Ξ± β Ξ± β Prop) :
pairwise_on {a} r :=
subsingleton_singleton.pairwise_on r
lemma pairwise_on_insert_of_symmetric {Ξ±} {s : set Ξ±} {a : Ξ±} {r : Ξ± β Ξ± β Prop}
(hr : symmetric r) :
(insert a s).pairwise_on r β s.pairwise_on r β§ β b β s, a β b β r a b :=
begin
refine β¨Ξ» h, β¨_, _β©, Ξ» h, _β©,
{ exact h.mono (s.subset_insert a) },
{ intros b hb hn,
exact h a (s.mem_insert _) b (set.mem_insert_of_mem _ hb) hn },
{ intros b hb c hc hn,
rw [mem_insert_iff] at hb hc,
rcases hb with (rfl | hb);
rcases hc with (rfl | hc),
{ exact absurd rfl hn },
{ exact h.right _ hc hn },
{ exact hr (h.right _ hb hn.symm) },
{ exact h.left _ hb _ hc hn } }
end
end set
open set
namespace function
variables {ΞΉ : Sort*} {Ξ± : Type*} {Ξ² : Type*} {f : Ξ± β Ξ²}
lemma surjective.preimage_injective (hf : surjective f) : injective (preimage f) :=
assume s t, (preimage_eq_preimage hf).1
lemma injective.preimage_image (hf : injective f) (s : set Ξ±) : f β»ΒΉ' (f '' s) = s :=
preimage_image_eq s hf
lemma injective.preimage_surjective (hf : injective f) : surjective (preimage f) :=
by { intro s, use f '' s, rw hf.preimage_image }
lemma injective.subsingleton_image_iff (hf : injective f) {s : set Ξ±} :
(f '' s).subsingleton β s.subsingleton :=
β¨subsingleton_of_image hf s, Ξ» h, h.image fβ©
lemma surjective.image_preimage (hf : surjective f) (s : set Ξ²) : f '' (f β»ΒΉ' s) = s :=
image_preimage_eq s hf
lemma surjective.image_surjective (hf : surjective f) : surjective (image f) :=
by { intro s, use f β»ΒΉ' s, rw hf.image_preimage }
lemma injective.image_injective (hf : injective f) : injective (image f) :=
by { intros s t h, rw [βpreimage_image_eq s hf, βpreimage_image_eq t hf, h] }
lemma surjective.preimage_subset_preimage_iff {s t : set Ξ²} (hf : surjective f) :
f β»ΒΉ' s β f β»ΒΉ' t β s β t :=
by { apply preimage_subset_preimage_iff, rw [hf.range_eq], apply subset_univ }
lemma surjective.range_comp {ΞΉ' : Sort*} {f : ΞΉ β ΞΉ'} (hf : surjective f) (g : ΞΉ' β Ξ±) :
range (g β f) = range g :=
ext $ Ξ» y, (@surjective.exists _ _ _ hf (Ξ» x, g x = y)).symm
lemma injective.nonempty_apply_iff {f : set Ξ± β set Ξ²} (hf : injective f)
(h2 : f β
= β
) {s : set Ξ±} : (f s).nonempty β s.nonempty :=
by rw [β ne_empty_iff_nonempty, β h2, β ne_empty_iff_nonempty, hf.ne_iff]
lemma injective.mem_range_iff_exists_unique (hf : injective f) {b : Ξ²} :
b β range f β β! a, f a = b :=
β¨Ξ» β¨a, hβ©, β¨a, h, Ξ» a' ha, hf (ha.trans h.symm)β©, exists_unique.existsβ©
lemma injective.exists_unique_of_mem_range (hf : injective f) {b : Ξ²} (hb : b β range f) :
β! a, f a = b :=
hf.mem_range_iff_exists_unique.mp hb
end function
open function
/-! ### Image and preimage on subtypes -/
namespace subtype
variable {Ξ± : Type*}
lemma coe_image {p : Ξ± β Prop} {s : set (subtype p)} :
coe '' s = {x | βh : p x, (β¨x, hβ© : subtype p) β s} :=
set.ext $ assume a,
β¨assume β¨β¨a', ha'β©, in_s, h_eqβ©, h_eq βΈ β¨ha', in_sβ©,
assume β¨ha, in_sβ©, β¨β¨a, haβ©, in_s, rflβ©β©
lemma range_coe {s : set Ξ±} :
range (coe : s β Ξ±) = s :=
by { rw β set.image_univ, simp [-set.image_univ, coe_image] }
/-- A variant of `range_coe`. Try to use `range_coe` if possible.
This version is useful when defining a new type that is defined as the subtype of something.
In that case, the coercion doesn't fire anymore. -/
lemma range_val {s : set Ξ±} :
range (subtype.val : s β Ξ±) = s :=
range_coe
/-- We make this the simp lemma instead of `range_coe`. The reason is that if we write
for `s : set Ξ±` the function `coe : s β Ξ±`, then the inferred implicit arguments of `coe` are
`coe Ξ± (Ξ» x, x β s)`. -/
@[simp] lemma range_coe_subtype {p : Ξ± β Prop} :
range (coe : subtype p β Ξ±) = {x | p x} :=
range_coe
@[simp] lemma coe_preimage_self (s : set Ξ±) : (coe : s β Ξ±) β»ΒΉ' s = univ :=
by rw [β preimage_range (coe : s β Ξ±), range_coe]
lemma range_val_subtype {p : Ξ± β Prop} :
range (subtype.val : subtype p β Ξ±) = {x | p x} :=
range_coe
theorem coe_image_subset (s : set Ξ±) (t : set s) : coe '' t β s :=
Ξ» x β¨y, yt, yvaleqβ©, by rw βyvaleq; exact y.property
theorem coe_image_univ (s : set Ξ±) : (coe : s β Ξ±) '' set.univ = s :=
image_univ.trans range_coe
@[simp] theorem image_preimage_coe (s t : set Ξ±) :
(coe : s β Ξ±) '' (coe β»ΒΉ' t) = t β© s :=
image_preimage_eq_inter_range.trans $ congr_arg _ range_coe
theorem image_preimage_val (s t : set Ξ±) :
(subtype.val : s β Ξ±) '' (subtype.val β»ΒΉ' t) = t β© s :=
image_preimage_coe s t
theorem preimage_coe_eq_preimage_coe_iff {s t u : set Ξ±} :
((coe : s β Ξ±) β»ΒΉ' t = coe β»ΒΉ' u) β t β© s = u β© s :=
begin
rw [βimage_preimage_coe, βimage_preimage_coe],
split, { intro h, rw h },
intro h, exact coe_injective.image_injective h
end
theorem preimage_val_eq_preimage_val_iff (s t u : set Ξ±) :
((subtype.val : s β Ξ±) β»ΒΉ' t = subtype.val β»ΒΉ' u) β (t β© s = u β© s) :=
preimage_coe_eq_preimage_coe_iff
lemma exists_set_subtype {t : set Ξ±} (p : set Ξ± β Prop) :
(β(s : set t), p (coe '' s)) β β(s : set Ξ±), s β t β§ p s :=
begin
split,
{ rintro β¨s, hsβ©, refine β¨coe '' s, _, hsβ©,
convert image_subset_range _ _, rw [range_coe] },
rintro β¨s, hsβ, hsββ©, refine β¨coe β»ΒΉ' s, _β©,
rw [image_preimage_eq_of_subset], exact hsβ, rw [range_coe], exact hsβ
end
lemma preimage_coe_nonempty {s t : set Ξ±} : ((coe : s β Ξ±) β»ΒΉ' t).nonempty β (s β© t).nonempty :=
by rw [inter_comm, β image_preimage_coe, nonempty_image_iff]
lemma preimage_coe_eq_empty {s t : set Ξ±} : (coe : s β Ξ±) β»ΒΉ' t = β
β s β© t = β
:=
by simp only [β not_nonempty_iff_eq_empty, preimage_coe_nonempty]
@[simp] lemma preimage_coe_compl (s : set Ξ±) : (coe : s β Ξ±) β»ΒΉ' sαΆ = β
:=
preimage_coe_eq_empty.2 (inter_compl_self s)
@[simp] lemma preimage_coe_compl' (s : set Ξ±) : (coe : sαΆ β Ξ±) β»ΒΉ' s = β
:=
preimage_coe_eq_empty.2 (compl_inter_self s)
end subtype
namespace set
/-! ### Lemmas about cartesian product of sets -/
section prod
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*}
variables {s sβ sβ : set Ξ±} {t tβ tβ : set Ξ²}
/-- The cartesian product `prod s t` is the set of `(a, b)`
such that `a β s` and `b β t`. -/
protected def prod (s : set Ξ±) (t : set Ξ²) : set (Ξ± Γ Ξ²) :=
{p | p.1 β s β§ p.2 β t}
lemma prod_eq (s : set Ξ±) (t : set Ξ²) : s.prod t = prod.fst β»ΒΉ' s β© prod.snd β»ΒΉ' t := rfl
theorem mem_prod_eq {p : Ξ± Γ Ξ²} : p β s.prod t = (p.1 β s β§ p.2 β t) := rfl
@[simp] theorem mem_prod {p : Ξ± Γ Ξ²} : p β s.prod t β p.1 β s β§ p.2 β t := iff.rfl
@[simp] theorem prod_mk_mem_set_prod_eq {a : Ξ±} {b : Ξ²} :
(a, b) β s.prod t = (a β s β§ b β t) := rfl
lemma mk_mem_prod {a : Ξ±} {b : Ξ²} (a_in : a β s) (b_in : b β t) : (a, b) β s.prod t :=
β¨a_in, b_inβ©
theorem prod_mono {sβ sβ : set Ξ±} {tβ tβ : set Ξ²} (hs : sβ β sβ) (ht : tβ β tβ) :
sβ.prod tβ β sβ.prod tβ :=
assume x β¨hβ, hββ©, β¨hs hβ, ht hββ©
lemma prod_subset_iff {P : set (Ξ± Γ Ξ²)} :
(s.prod t β P) β β (x β s) (y β t), (x, y) β P :=
β¨Ξ» h _ xin _ yin, h (mk_mem_prod xin yin), Ξ» h β¨_, _β© pin, h _ pin.1 _ pin.2β©
lemma forall_prod_set {p : Ξ± Γ Ξ² β Prop} :
(β x β s.prod t, p x) β β (x β s) (y β t), p (x, y) :=
prod_subset_iff
lemma exists_prod_set {p : Ξ± Γ Ξ² β Prop} :
(β x β s.prod t, p x) β β (x β s) (y β t), p (x, y) :=
by simp [and_assoc]
@[simp] theorem prod_empty : s.prod β
= (β
: set (Ξ± Γ Ξ²)) :=
by { ext, simp }
@[simp] theorem empty_prod : set.prod β
t = (β
: set (Ξ± Γ Ξ²)) :=
by { ext, simp }
@[simp] theorem univ_prod_univ : (@univ Ξ±).prod (@univ Ξ²) = univ :=
by { ext β¨x, yβ©, simp }
lemma univ_prod {t : set Ξ²} : set.prod (univ : set Ξ±) t = prod.snd β»ΒΉ' t :=
by simp [prod_eq]
lemma prod_univ {s : set Ξ±} : set.prod s (univ : set Ξ²) = prod.fst β»ΒΉ' s :=
by simp [prod_eq]
@[simp] theorem singleton_prod {a : Ξ±} : set.prod {a} t = prod.mk a '' t :=
by { ext β¨x, yβ©, simp [and.left_comm, eq_comm] }
@[simp] theorem prod_singleton {b : Ξ²} : s.prod {b} = (Ξ» a, (a, b)) '' s :=
by { ext β¨x, yβ©, simp [and.left_comm, eq_comm] }
theorem singleton_prod_singleton {a : Ξ±} {b : Ξ²} : set.prod {a} {b} = ({(a, b)} : set (Ξ± Γ Ξ²)) :=
by simp
@[simp] theorem union_prod : (sβ βͺ sβ).prod t = sβ.prod t βͺ sβ.prod t :=
by { ext β¨x, yβ©, simp [or_and_distrib_right] }
@[simp] theorem prod_union : s.prod (tβ βͺ tβ) = s.prod tβ βͺ s.prod tβ :=
by { ext β¨x, yβ©, simp [and_or_distrib_left] }
theorem prod_inter_prod : sβ.prod tβ β© sβ.prod tβ = (sβ β© sβ).prod (tβ β© tβ) :=
by { ext β¨x, yβ©, simp [and_assoc, and.left_comm] }
theorem insert_prod {a : Ξ±} : (insert a s).prod t = (prod.mk a '' t) βͺ s.prod t :=
by { ext β¨x, yβ©, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} }
theorem prod_insert {b : Ξ²} : s.prod (insert b t) = ((Ξ»a, (a, b)) '' s) βͺ s.prod t :=
by { ext β¨x, yβ©, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} }
theorem prod_preimage_eq {f : Ξ³ β Ξ±} {g : Ξ΄ β Ξ²} :
(f β»ΒΉ' s).prod (g β»ΒΉ' t) = (Ξ» p, (f p.1, g p.2)) β»ΒΉ' s.prod t := rfl
lemma prod_preimage_left {f : Ξ³ β Ξ±} : (f β»ΒΉ' s).prod t = (Ξ»p, (f p.1, p.2)) β»ΒΉ' (s.prod t) := rfl
lemma prod_preimage_right {g : Ξ΄ β Ξ²} : s.prod (g β»ΒΉ' t) = (Ξ»p, (p.1, g p.2)) β»ΒΉ' (s.prod t) := rfl
lemma preimage_prod_map_prod (f : Ξ± β Ξ²) (g : Ξ³ β Ξ΄) (s : set Ξ²) (t : set Ξ΄) :
prod.map f g β»ΒΉ' (s.prod t) = (f β»ΒΉ' s).prod (g β»ΒΉ' t) :=
rfl
lemma mk_preimage_prod (f : Ξ³ β Ξ±) (g : Ξ³ β Ξ²) :
(Ξ» x, (f x, g x)) β»ΒΉ' s.prod t = f β»ΒΉ' s β© g β»ΒΉ' t := rfl
@[simp] lemma mk_preimage_prod_left {y : Ξ²} (h : y β t) : (Ξ» x, (x, y)) β»ΒΉ' s.prod t = s :=
by { ext x, simp [h] }
@[simp] lemma mk_preimage_prod_right {x : Ξ±} (h : x β s) : prod.mk x β»ΒΉ' s.prod t = t :=
by { ext y, simp [h] }
@[simp] lemma mk_preimage_prod_left_eq_empty {y : Ξ²} (hy : y β t) :
(Ξ» x, (x, y)) β»ΒΉ' s.prod t = β
:=
by { ext z, simp [hy] }
@[simp] lemma mk_preimage_prod_right_eq_empty {x : Ξ±} (hx : x β s) :
prod.mk x β»ΒΉ' s.prod t = β
:=
by { ext z, simp [hx] }
lemma mk_preimage_prod_left_eq_if {y : Ξ²} [decidable_pred (β t)] :
(Ξ» x, (x, y)) β»ΒΉ' s.prod t = if y β t then s else β
:=
by { split_ifs; simp [h] }
lemma mk_preimage_prod_right_eq_if {x : Ξ±} [decidable_pred (β s)] :
prod.mk x β»ΒΉ' s.prod t = if x β s then t else β
:=
by { split_ifs; simp [h] }
lemma mk_preimage_prod_left_fn_eq_if {y : Ξ²} [decidable_pred (β t)] (f : Ξ³ β Ξ±) :
(Ξ» x, (f x, y)) β»ΒΉ' s.prod t = if y β t then f β»ΒΉ' s else β
:=
by rw [β mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage]
lemma mk_preimage_prod_right_fn_eq_if {x : Ξ±} [decidable_pred (β s)] (g : Ξ΄ β Ξ²) :
(Ξ» y, (x, g y)) β»ΒΉ' s.prod t = if x β s then g β»ΒΉ' t else β
:=
by rw [β mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage]
theorem image_swap_eq_preimage_swap : image (@prod.swap Ξ± Ξ²) = preimage prod.swap :=
image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse
theorem preimage_swap_prod {s : set Ξ±} {t : set Ξ²} : prod.swap β»ΒΉ' t.prod s = s.prod t :=
by { ext β¨x, yβ©, simp [and_comm] }
theorem image_swap_prod : prod.swap '' t.prod s = s.prod t :=
by rw [image_swap_eq_preimage_swap, preimage_swap_prod]
theorem prod_image_image_eq {mβ : Ξ± β Ξ³} {mβ : Ξ² β Ξ΄} :
(mβ '' s).prod (mβ '' t) = image (Ξ»p:Ξ±ΓΞ², (mβ p.1, mβ p.2)) (s.prod t) :=
ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm,
and.assoc, and.comm]
theorem prod_range_range_eq {Ξ± Ξ² Ξ³ Ξ΄} {mβ : Ξ± β Ξ³} {mβ : Ξ² β Ξ΄} :
(range mβ).prod (range mβ) = range (Ξ»p:Ξ±ΓΞ², (mβ p.1, mβ p.2)) :=
ext $ by simp [range]
@[simp] theorem range_prod_map {Ξ± Ξ² Ξ³ Ξ΄} {mβ : Ξ± β Ξ³} {mβ : Ξ² β Ξ΄} :
range (prod.map mβ mβ) = (range mβ).prod (range mβ) :=
prod_range_range_eq.symm
theorem prod_range_univ_eq {Ξ± Ξ² Ξ³} {mβ : Ξ± β Ξ³} :
(range mβ).prod (univ : set Ξ²) = range (Ξ»p:Ξ±ΓΞ², (mβ p.1, p.2)) :=
ext $ by simp [range]
theorem prod_univ_range_eq {Ξ± Ξ² Ξ΄} {mβ : Ξ² β Ξ΄} :
(univ : set Ξ±).prod (range mβ) = range (Ξ»p:Ξ±ΓΞ², (p.1, mβ p.2)) :=
ext $ by simp [range]
theorem nonempty.prod : s.nonempty β t.nonempty β (s.prod t).nonempty
| β¨x, hxβ© β¨y, hyβ© := β¨(x, y), β¨hx, hyβ©β©
theorem nonempty.fst : (s.prod t).nonempty β s.nonempty
| β¨p, hpβ© := β¨p.1, hp.1β©
theorem nonempty.snd : (s.prod t).nonempty β t.nonempty
| β¨p, hpβ© := β¨p.2, hp.2β©
theorem prod_nonempty_iff : (s.prod t).nonempty β s.nonempty β§ t.nonempty :=
β¨Ξ» h, β¨h.fst, h.sndβ©, Ξ» h, nonempty.prod h.1 h.2β©
theorem prod_eq_empty_iff :
s.prod t = β
β (s = β
β¨ t = β
) :=
by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib]
lemma prod_sub_preimage_iff {W : set Ξ³} {f : Ξ± Γ Ξ² β Ξ³} :
s.prod t β f β»ΒΉ' W β β a b, a β s β b β t β f (a, b) β W :=
by simp [subset_def]
lemma fst_image_prod_subset (s : set Ξ±) (t : set Ξ²) :
prod.fst '' (s.prod t) β s :=
Ξ» _ h, let β¨_, β¨hβ, _β©, hββ© := (set.mem_image _ _ _).1 h in hβ βΈ hβ
lemma prod_subset_preimage_fst (s : set Ξ±) (t : set Ξ²) :
s.prod t β prod.fst β»ΒΉ' s :=
image_subset_iff.1 (fst_image_prod_subset s t)
lemma fst_image_prod (s : set Ξ²) {t : set Ξ±} (ht : t.nonempty) :
prod.fst '' (s.prod t) = s :=
set.subset.antisymm (fst_image_prod_subset _ _)
$ Ξ» y y_in, let β¨x, x_inβ© := ht in
β¨(y, x), β¨y_in, x_inβ©, rflβ©
lemma snd_image_prod_subset (s : set Ξ±) (t : set Ξ²) :
prod.snd '' (s.prod t) β t :=
Ξ» _ h, let β¨_, β¨_, hββ©, hββ© := (set.mem_image _ _ _).1 h in hβ βΈ hβ
lemma prod_subset_preimage_snd (s : set Ξ±) (t : set Ξ²) :
s.prod t β prod.snd β»ΒΉ' t :=
image_subset_iff.1 (snd_image_prod_subset s t)
lemma snd_image_prod {s : set Ξ±} (hs : s.nonempty) (t : set Ξ²) :
prod.snd '' (s.prod t) = t :=
set.subset.antisymm (snd_image_prod_subset _ _)
$ Ξ» y y_in, let β¨x, x_inβ© := hs in
β¨(x, y), β¨x_in, y_inβ©, rflβ©
lemma prod_diff_prod : s.prod t \ sβ.prod tβ = s.prod (t \ tβ) βͺ (s \ sβ).prod t :=
by { ext x, by_cases hβ : x.1 β sβ; by_cases hβ : x.2 β tβ; simp * }
/-- A product set is included in a product set if and only factors are included, or a factor of the
first set is empty. -/
lemma prod_subset_prod_iff :
(s.prod t β sβ.prod tβ) β (s β sβ β§ t β tβ) β¨ (s = β
) β¨ (t = β
) :=
begin
classical,
cases (s.prod t).eq_empty_or_nonempty with h h,
{ simp [h, prod_eq_empty_iff.1 h] },
{ have st : s.nonempty β§ t.nonempty, by rwa [prod_nonempty_iff] at h,
split,
{ assume H : s.prod t β sβ.prod tβ,
have h' : sβ.nonempty β§ tβ.nonempty := prod_nonempty_iff.1 (h.mono H),
refine or.inl β¨_, _β©,
show s β sβ,
{ have := image_subset (prod.fst : Ξ± Γ Ξ² β Ξ±) H,
rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this },
show t β tβ,
{ have := image_subset (prod.snd : Ξ± Γ Ξ² β Ξ²) H,
rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } },
{ assume H,
simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H,
exact prod_mono H.1 H.2 } }
end
end prod
/-! ### Lemmas about set-indexed products of sets -/
section pi
variables {ΞΉ : Type*} {Ξ± : ΞΉ β Type*} {s sβ : set ΞΉ} {t tβ tβ : Ξ i, set (Ξ± i)}
/-- Given an index set `ΞΉ` and a family of sets `t : Ξ i, set (Ξ± i)`, `pi s t`
is the set of dependent functions `f : Ξ a, Ο a` such that `f a` belongs to `t a`
whenever `a β s`. -/
def pi (s : set ΞΉ) (t : Ξ i, set (Ξ± i)) : set (Ξ i, Ξ± i) := { f | βi β s, f i β t i }
@[simp] lemma mem_pi {f : Ξ i, Ξ± i} : f β s.pi t β β i β s, f i β t i :=
by refl
@[simp] lemma mem_univ_pi {f : Ξ i, Ξ± i} : f β pi univ t β β i, f i β t i :=
by simp
@[simp] lemma empty_pi (s : Ξ i, set (Ξ± i)) : pi β
s = univ := by { ext, simp [pi] }
@[simp] lemma pi_univ (s : set ΞΉ) : pi s (Ξ» i, (univ : set (Ξ± i))) = univ :=
eq_univ_of_forall $ Ξ» f i hi, mem_univ _
lemma pi_mono (h : β i β s, tβ i β tβ i) : pi s tβ β pi s tβ :=
Ξ» x hx i hi, (h i hi $ hx i hi)
lemma pi_inter_distrib : s.pi (Ξ» i, t i β© tβ i) = s.pi t β© s.pi tβ :=
ext $ Ξ» x, by simp only [forall_and_distrib, mem_pi, mem_inter_eq]
lemma pi_congr (h : s = sβ) (h' : β i β s, t i = tβ i) : pi s t = pi sβ tβ :=
h βΈ (ext $ Ξ» x, forall_congr $ Ξ» i, forall_congr $ Ξ» hi, h' i hi βΈ iff.rfl)
lemma pi_eq_empty {i : ΞΉ} (hs : i β s) (ht : t i = β
) : s.pi t = β
:=
by { ext f, simp only [mem_empty_eq, not_forall, iff_false, mem_pi, not_imp],
exact β¨i, hs, by simp [ht]β© }
lemma univ_pi_eq_empty {i : ΞΉ} (ht : t i = β
) : pi univ t = β
:=
pi_eq_empty (mem_univ i) ht
lemma pi_nonempty_iff : (s.pi t).nonempty β β i, β x, i β s β x β t i :=
by simp [classical.skolem, set.nonempty]
lemma univ_pi_nonempty_iff : (pi univ t).nonempty β β i, (t i).nonempty :=
by simp [classical.skolem, set.nonempty]
lemma pi_eq_empty_iff : s.pi t = β
β β i, (Ξ± i β false) β¨ (i β s β§ t i = β
) :=
begin
rw [β not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, apply exists_congr, intro i,
split,
{ intro h, by_cases hΞ± : nonempty (Ξ± i),
{ cases hΞ± with x, refine or.inr β¨(h x).1, by simp [eq_empty_iff_forall_not_mem, h]β© },
{ exact or.inl (Ξ» x, hΞ± β¨xβ©) }},
{ rintro (h|h) x, exfalso, exact h x, simp [h] }
end
lemma univ_pi_eq_empty_iff : pi univ t = β
β β i, t i = β
:=
by simp [β not_nonempty_iff_eq_empty, univ_pi_nonempty_iff]
@[simp] lemma range_dcomp {Ξ² : ΞΉ β Type*} (f : Ξ i, Ξ± i β Ξ² i) :
range (Ξ» (g : Ξ i, Ξ± i), (Ξ» i, f i (g i))) = pi univ (Ξ» i, range (f i)) :=
begin
apply subset.antisymm,
{ rintro _ β¨x, rflβ© i -,
exact β¨x i, rflβ© },
{ intros x hx,
choose y hy using hx,
exact β¨Ξ» i, y i trivial, funext $ Ξ» i, hy i trivialβ© }
end
@[simp] lemma insert_pi (i : ΞΉ) (s : set ΞΉ) (t : Ξ i, set (Ξ± i)) :
pi (insert i s) t = (eval i β»ΒΉ' t i) β© pi s t :=
by { ext, simp [pi, or_imp_distrib, forall_and_distrib] }
@[simp] lemma singleton_pi (i : ΞΉ) (t : Ξ i, set (Ξ± i)) :
pi {i} t = (eval i β»ΒΉ' t i) :=
by { ext, simp [pi] }
lemma singleton_pi' (i : ΞΉ) (t : Ξ i, set (Ξ± i)) : pi {i} t = {x | x i β t i} :=
singleton_pi i t
lemma pi_if {p : ΞΉ β Prop} [h : decidable_pred p] (s : set ΞΉ) (tβ tβ : Ξ i, set (Ξ± i)) :
pi s (Ξ» i, if p i then tβ i else tβ i) = pi {i β s | p i} tβ β© pi {i β s | Β¬ p i} tβ :=
begin
ext f,
split,
{ assume h, split; { rintros i β¨his, hpiβ©, simpa [*] using h i } },
{ rintros β¨htβ, htββ© i his,
by_cases p i; simp * at * }
end
lemma union_pi : (s βͺ sβ).pi t = s.pi t β© sβ.pi t :=
by simp [pi, or_imp_distrib, forall_and_distrib, set_of_and]
@[simp] lemma pi_inter_compl (s : set ΞΉ) : pi s t β© pi sαΆ t = pi univ t :=
by rw [β union_pi, union_compl_self]
lemma pi_update_of_not_mem [decidable_eq ΞΉ] {Ξ² : Ξ i, Type*} {i : ΞΉ} (hi : i β s) (f : Ξ j, Ξ± j)
(a : Ξ± i) (t : Ξ j, Ξ± j β set (Ξ² j)) :
s.pi (Ξ» j, t j (update f i a j)) = s.pi (Ξ» j, t j (f j)) :=
pi_congr rfl $ Ξ» j hj, by { rw update_noteq, exact Ξ» h, hi (h βΈ hj) }
lemma pi_update_of_mem [decidable_eq ΞΉ] {Ξ² : Ξ i, Type*} {i : ΞΉ} (hi : i β s) (f : Ξ j, Ξ± j)
(a : Ξ± i) (t : Ξ j, Ξ± j β set (Ξ² j)) :
s.pi (Ξ» j, t j (update f i a j)) = {x | x i β t i a} β© (s \ {i}).pi (Ξ» j, t j (f j)) :=
calc s.pi (Ξ» j, t j (update f i a j)) = ({i} βͺ s \ {i}).pi (Ξ» j, t j (update f i a j)) :
by rw [union_diff_self, union_eq_self_of_subset_left (singleton_subset_iff.2 hi)]
... = {x | x i β t i a} β© (s \ {i}).pi (Ξ» j, t j (f j)) :
by { rw [union_pi, singleton_pi', update_same, pi_update_of_not_mem], simp }
lemma univ_pi_update [decidable_eq ΞΉ] {Ξ² : Ξ i, Type*} (i : ΞΉ) (f : Ξ j, Ξ± j)
(a : Ξ± i) (t : Ξ j, Ξ± j β set (Ξ² j)) :
pi univ (Ξ» j, t j (update f i a j)) = {x | x i β t i a} β© pi {i}αΆ (Ξ» j, t j (f j)) :=
by rw [compl_eq_univ_diff, β pi_update_of_mem (mem_univ _)]
lemma univ_pi_update_univ [decidable_eq ΞΉ] (i : ΞΉ) (s : set (Ξ± i)) :
pi univ (update (Ξ» j : ΞΉ, (univ : set (Ξ± j))) i s) = eval i β»ΒΉ' s :=
by rw [univ_pi_update i (Ξ» j, (univ : set (Ξ± j))) s (Ξ» j t, t), pi_univ, inter_univ, preimage]
open_locale classical
lemma eval_image_pi {i : ΞΉ} (hs : i β s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i :=
begin
ext x, rcases ht with β¨f, hfβ©, split,
{ rintro β¨g, hg, rflβ©, exact hg i hs },
{ intro hg, refine β¨update f i x, _, by simpβ©,
intros j hj, by_cases hji : j = i,
{ subst hji, simp [hg] },
{ rw [mem_pi] at hf, simp [hji, hf, hj] }},
end
@[simp] lemma eval_image_univ_pi {i : ΞΉ} (ht : (pi univ t).nonempty) :
(Ξ» f : Ξ i, Ξ± i, f i) '' pi univ t = t i :=
eval_image_pi (mem_univ i) ht
lemma eval_preimage {ΞΉ} {Ξ± : ΞΉ β Type*} {i : ΞΉ} {s : set (Ξ± i)} :
eval i β»ΒΉ' s = pi univ (update (Ξ» i, univ) i s) :=
by { ext x, simp [@forall_update_iff _ (Ξ» i, set (Ξ± i)) _ _ _ _ (Ξ» i' y, x i' β y)] }
lemma eval_preimage' {ΞΉ} {Ξ± : ΞΉ β Type*} {i : ΞΉ} {s : set (Ξ± i)} :
eval i β»ΒΉ' s = pi {i} (update (Ξ» i, univ) i s) :=
by { ext, simp }
lemma update_preimage_pi {i : ΞΉ} {f : Ξ i, Ξ± i} (hi : i β s)
(hf : β j β s, j β i β f j β t j) : (update f i) β»ΒΉ' s.pi t = t i :=
begin
ext x, split,
{ intro h, convert h i hi, simp },
{ intros hx j hj, by_cases h : j = i,
{ cases h, simpa },
{ rw [update_noteq h], exact hf j hj h }}
end
lemma update_preimage_univ_pi {i : ΞΉ} {f : Ξ i, Ξ± i} (hf : β j β i, f j β t j) :
(update f i) β»ΒΉ' pi univ t = t i :=
update_preimage_pi (mem_univ i) (Ξ» j _, hf j)
lemma subset_pi_eval_image (s : set ΞΉ) (u : set (Ξ i, Ξ± i)) : u β pi s (Ξ» i, eval i '' u) :=
Ξ» f hf i hi, β¨f, hf, rflβ©
lemma univ_pi_ite (s : set ΞΉ) (t : Ξ i, set (Ξ± i)) :
pi univ (Ξ» i, if i β s then t i else univ) = s.pi t :=
by { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i, split_ifs; simp [h] }
end pi
/-! ### Lemmas about `inclusion`, the injection of subtypes induced by `β` -/
section inclusion
variable {Ξ± : Type*}
/-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s β t` -/
def inclusion {s t : set Ξ±} (h : s β t) : s β t :=
Ξ» x : s, (β¨x, h x.2β© : t)
@[simp] lemma inclusion_self {s : set Ξ±} (x : s) :
inclusion (set.subset.refl _) x = x :=
by { cases x, refl }
@[simp] lemma inclusion_right {s t : set Ξ±} (h : s β t) (x : t) (m : (x : Ξ±) β s) :
inclusion h β¨x, mβ© = x :=
by { cases x, refl }
@[simp] lemma inclusion_inclusion {s t u : set Ξ±} (hst : s β t) (htu : t β u)
(x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x :=
by { cases x, refl }
@[simp] lemma coe_inclusion {s t : set Ξ±} (h : s β t) (x : s) :
(inclusion h x : Ξ±) = (x : Ξ±) := rfl
lemma inclusion_injective {s t : set Ξ±} (h : s β t) :
function.injective (inclusion h)
| β¨_, _β© β¨_, _β© := subtype.ext_iff_val.2 β subtype.ext_iff_val.1
@[simp] lemma range_inclusion {s t : set Ξ±} (h : s β t) :
range (inclusion h) = {x : t | (x:Ξ±) β s} :=
by { ext β¨x, hxβ©, simp [inclusion] }
lemma eq_of_inclusion_surjective {s t : set Ξ±} {h : s β t}
(h_surj : function.surjective (inclusion h)) : s = t :=
begin
rw [β range_iff_surjective, range_inclusion, eq_univ_iff_forall] at h_surj,
exact set.subset.antisymm h (Ξ» x hx, h_surj β¨x, hxβ©)
end
end inclusion
/-! ### Injectivity and surjectivity lemmas for image and preimage -/
section image_preimage
variables {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β Ξ²}
@[simp]
lemma preimage_injective : injective (preimage f) β surjective f :=
begin
refine β¨Ξ» h y, _, surjective.preimage_injectiveβ©,
obtain β¨x, hxβ© : (f β»ΒΉ' {y}).nonempty,
{ rw [h.nonempty_apply_iff preimage_empty], apply singleton_nonempty },
exact β¨x, hxβ©
end
@[simp]
lemma preimage_surjective : surjective (preimage f) β injective f :=
begin
refine β¨Ξ» h x x' hx, _, injective.preimage_surjectiveβ©,
cases h {x} with s hs, have := mem_singleton x,
rwa [β hs, mem_preimage, hx, β mem_preimage, hs, mem_singleton_iff, eq_comm] at this
end
@[simp] lemma image_surjective : surjective (image f) β surjective f :=
begin
refine β¨Ξ» h y, _, surjective.image_surjectiveβ©,
cases h {y} with s hs,
have := mem_singleton y, rw [β hs] at this, rcases this with β¨x, h1x, h2xβ©,
exact β¨x, h2xβ©
end
@[simp] lemma image_injective : injective (image f) β injective f :=
begin
refine β¨Ξ» h x x' hx, _, injective.image_injectiveβ©,
rw [β singleton_eq_singleton_iff], apply h,
rw [image_singleton, image_singleton, hx]
end
lemma preimage_eq_iff_eq_image {f : Ξ± β Ξ²} (hf : bijective f) {s t} :
f β»ΒΉ' s = t β s = f '' t :=
by rw [β image_eq_image hf.1, hf.2.image_preimage]
lemma eq_preimage_iff_image_eq {f : Ξ± β Ξ²} (hf : bijective f) {s t} :
s = f β»ΒΉ' t β f '' s = t :=
by rw [β image_eq_image hf.1, hf.2.image_preimage]
end image_preimage
/-! ### Lemmas about images of binary and ternary functions -/
section n_ary_image
variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΅ : Type*} {f f' : Ξ± β Ξ² β Ξ³} {g g' : Ξ± β Ξ² β Ξ³ β Ξ΄}
variables {s s' : set Ξ±} {t t' : set Ξ²} {u u' : set Ξ³} {a a' : Ξ±} {b b' : Ξ²} {c c' : Ξ³} {d d' : Ξ΄}
/-- The image of a binary function `f : Ξ± β Ξ² β Ξ³` as a function `set Ξ± β set Ξ² β set Ξ³`.
Mathematically this should be thought of as the image of the corresponding function `Ξ± Γ Ξ² β Ξ³`.
-/
def image2 (f : Ξ± β Ξ² β Ξ³) (s : set Ξ±) (t : set Ξ²) : set Ξ³ :=
{c | β a b, a β s β§ b β t β§ f a b = c }
lemma mem_image2_eq : c β image2 f s t = β a b, a β s β§ b β t β§ f a b = c := rfl
@[simp] lemma mem_image2 : c β image2 f s t β β a b, a β s β§ b β t β§ f a b = c := iff.rfl
lemma mem_image2_of_mem (h1 : a β s) (h2 : b β t) : f a b β image2 f s t :=
β¨a, b, h1, h2, rflβ©
lemma mem_image2_iff (hf : injective2 f) : f a b β image2 f s t β a β s β§ b β t :=
β¨ by { rintro β¨a', b', ha', hb', hβ©, rcases hf h with β¨rfl, rflβ©, exact β¨ha', hb'β© },
Ξ» β¨ha, hbβ©, mem_image2_of_mem ha hbβ©
/-- image2 is monotone with respect to `β`. -/
lemma image2_subset (hs : s β s') (ht : t β t') : image2 f s t β image2 f s' t' :=
by { rintro _ β¨a, b, ha, hb, rflβ©, exact mem_image2_of_mem (hs ha) (ht hb) }
lemma forall_image2_iff {p : Ξ³ β Prop} :
(β z β image2 f s t, p z) β β (x β s) (y β t), p (f x y) :=
β¨Ξ» h x hx y hy, h _ β¨x, y, hx, hy, rflβ©, Ξ» h z β¨x, y, hx, hy, hzβ©, hz βΈ h x hx y hyβ©
@[simp] lemma image2_subset_iff {u : set Ξ³} :
image2 f s t β u β β (x β s) (y β t), f x y β u :=
forall_image2_iff
lemma image2_union_left : image2 f (s βͺ s') t = image2 f s t βͺ image2 f s' t :=
begin
ext c, split,
{ rintros β¨a, b, h1a|h2a, hb, rflβ©;[left, right]; exact β¨_, _, βΉ_βΊ, βΉ_βΊ, rflβ© },
{ rintro (β¨_, _, _, _, rflβ©|β¨_, _, _, _, rflβ©); refine β¨_, _, _, βΉ_βΊ, rflβ©; simp [mem_union, *] }
end
lemma image2_union_right : image2 f s (t βͺ t') = image2 f s t βͺ image2 f s t' :=
begin
ext c, split,
{ rintros β¨a, b, ha, h1b|h2b, rflβ©;[left, right]; exact β¨_, _, βΉ_βΊ, βΉ_βΊ, rflβ© },
{ rintro (β¨_, _, _, _, rflβ©|β¨_, _, _, _, rflβ©); refine β¨_, _, βΉ_βΊ, _, rflβ©; simp [mem_union, *] }
end
@[simp] lemma image2_empty_left : image2 f β
t = β
:= ext $ by simp
@[simp] lemma image2_empty_right : image2 f s β
= β
:= ext $ by simp
lemma image2_inter_subset_left : image2 f (s β© s') t β image2 f s t β© image2 f s' t :=
by { rintro _ β¨a, b, β¨h1a, h2aβ©, hb, rflβ©, split; exact β¨_, _, βΉ_βΊ, βΉ_βΊ, rflβ© }
lemma image2_inter_subset_right : image2 f s (t β© t') β image2 f s t β© image2 f s t' :=
by { rintro _ β¨a, b, ha, β¨h1b, h2bβ©, rflβ©, split; exact β¨_, _, βΉ_βΊ, βΉ_βΊ, rflβ© }
@[simp] lemma image2_singleton_left : image2 f {a} t = f a '' t :=
ext $ Ξ» x, by simp
@[simp] lemma image2_singleton_right : image2 f s {b} = (Ξ» a, f a b) '' s :=
ext $ Ξ» x, by simp
lemma image2_singleton : image2 f {a} {b} = {f a b} := by simp
@[congr] lemma image2_congr (h : β (a β s) (b β t), f a b = f' a b) :
image2 f s t = image2 f' s t :=
by { ext, split; rintro β¨a, b, ha, hb, rflβ©; refine β¨a, b, ha, hb, by rw h a ha b hbβ© }
/-- A common special case of `image2_congr` -/
lemma image2_congr' (h : β a b, f a b = f' a b) : image2 f s t = image2 f' s t :=
image2_congr (Ξ» a _ b _, h a b)
/-- The image of a ternary function `f : Ξ± β Ξ² β Ξ³ β Ξ΄` as a function
`set Ξ± β set Ξ² β set Ξ³ β set Ξ΄`. Mathematically this should be thought of as the image of the
corresponding function `Ξ± Γ Ξ² Γ Ξ³ β Ξ΄`.
-/
def image3 (g : Ξ± β Ξ² β Ξ³ β Ξ΄) (s : set Ξ±) (t : set Ξ²) (u : set Ξ³) : set Ξ΄ :=
{d | β a b c, a β s β§ b β t β§ c β u β§ g a b c = d }
@[simp] lemma mem_image3 : d β image3 g s t u β β a b c, a β s β§ b β t β§ c β u β§ g a b c = d :=
iff.rfl
@[congr] lemma image3_congr (h : β (a β s) (b β t) (c β u), g a b c = g' a b c) :
image3 g s t u = image3 g' s t u :=
by { ext x,
split; rintro β¨a, b, c, ha, hb, hc, rflβ©; exact β¨a, b, c, ha, hb, hc, by rw h a ha b hb c hcβ© }
/-- A common special case of `image3_congr` -/
lemma image3_congr' (h : β a b c, g a b c = g' a b c) : image3 g s t u = image3 g' s t u :=
image3_congr (Ξ» a _ b _ c _, h a b c)
lemma image2_image2_left (f : Ξ΄ β Ξ³ β Ξ΅) (g : Ξ± β Ξ² β Ξ΄) :
image2 f (image2 g s t) u = image3 (Ξ» a b c, f (g a b) c) s t u :=
begin
ext, split,
{ rintro β¨_, c, β¨a, b, ha, hb, rflβ©, hc, rflβ©, refine β¨a, b, c, ha, hb, hc, rflβ© },
{ rintro β¨a, b, c, ha, hb, hc, rflβ©, refine β¨_, c, β¨a, b, ha, hb, rflβ©, hc, rflβ© }
end
lemma image2_image2_right (f : Ξ± β Ξ΄ β Ξ΅) (g : Ξ² β Ξ³ β Ξ΄) :
image2 f s (image2 g t u) = image3 (Ξ» a b c, f a (g b c)) s t u :=
begin
ext, split,
{ rintro β¨a, _, ha, β¨b, c, hb, hc, rflβ©, rflβ©, refine β¨a, b, c, ha, hb, hc, rflβ© },
{ rintro β¨a, b, c, ha, hb, hc, rflβ©, refine β¨a, _, ha, β¨b, c, hb, hc, rflβ©, rflβ© }
end
lemma image2_assoc {Ξ΅'} {f : Ξ΄ β Ξ³ β Ξ΅} {g : Ξ± β Ξ² β Ξ΄} {f' : Ξ± β Ξ΅' β Ξ΅} {g' : Ξ² β Ξ³ β Ξ΅'}
(h_assoc : β a b c, f (g a b) c = f' a (g' b c)) :
image2 f (image2 g s t) u = image2 f' s (image2 g' t u) :=
by simp only [image2_image2_left, image2_image2_right, h_assoc]
lemma image_image2 (f : Ξ± β Ξ² β Ξ³) (g : Ξ³ β Ξ΄) :
g '' image2 f s t = image2 (Ξ» a b, g (f a b)) s t :=
begin
ext, split,
{ rintro β¨_, β¨a, b, ha, hb, rflβ©, rflβ©, refine β¨a, b, ha, hb, rflβ© },
{ rintro β¨a, b, ha, hb, rflβ©, refine β¨_, β¨a, b, ha, hb, rflβ©, rflβ© }
end
lemma image2_image_left (f : Ξ³ β Ξ² β Ξ΄) (g : Ξ± β Ξ³) :
image2 f (g '' s) t = image2 (Ξ» a b, f (g a) b) s t :=
begin
ext, split,
{ rintro β¨_, b, β¨a, ha, rflβ©, hb, rflβ©, refine β¨a, b, ha, hb, rflβ© },
{ rintro β¨a, b, ha, hb, rflβ©, refine β¨_, b, β¨a, ha, rflβ©, hb, rflβ© }
end
lemma image2_image_right (f : Ξ± β Ξ³ β Ξ΄) (g : Ξ² β Ξ³) :
image2 f s (g '' t) = image2 (Ξ» a b, f a (g b)) s t :=
begin
ext, split,
{ rintro β¨a, _, ha, β¨b, hb, rflβ©, rflβ©, refine β¨a, b, ha, hb, rflβ© },
{ rintro β¨a, b, ha, hb, rflβ©, refine β¨a, _, ha, β¨b, hb, rflβ©, rflβ© }
end
lemma image2_swap (f : Ξ± β Ξ² β Ξ³) (s : set Ξ±) (t : set Ξ²) :
image2 f s t = image2 (Ξ» a b, f b a) t s :=
by { ext, split; rintro β¨a, b, ha, hb, rflβ©; refine β¨b, a, hb, ha, rflβ© }
@[simp] lemma image2_left (h : t.nonempty) : image2 (Ξ» x y, x) s t = s :=
by simp [nonempty_def.mp h, ext_iff]
@[simp] lemma image2_right (h : s.nonempty) : image2 (Ξ» x y, y) s t = t :=
by simp [nonempty_def.mp h, ext_iff]
@[simp] lemma image_prod (f : Ξ± β Ξ² β Ξ³) : (Ξ» x : Ξ± Γ Ξ², f x.1 x.2) '' s.prod t = image2 f s t :=
set.ext $ Ξ» a,
β¨ by { rintros β¨_, _, rflβ©, exact β¨_, _, (mem_prod.mp βΉ_βΊ).1, (mem_prod.mp βΉ_βΊ).2, rflβ© },
by { rintros β¨_, _, _, _, rflβ©, exact β¨(_, _), mem_prod.mpr β¨βΉ_βΊ, βΉ_βΊβ©, rflβ© }β©
lemma nonempty.image2 (hs : s.nonempty) (ht : t.nonempty) : (image2 f s t).nonempty :=
by { cases hs with a ha, cases ht with b hb, exact β¨f a b, β¨a, b, ha, hb, rflβ©β© }
end n_ary_image
end set
namespace subsingleton
variables {Ξ± : Type*} [subsingleton Ξ±]
lemma eq_univ_of_nonempty {s : set Ξ±} : s.nonempty β s = univ :=
Ξ» β¨x, hxβ©, eq_univ_of_forall $ Ξ» y, subsingleton.elim x y βΈ hx
@[elab_as_eliminator]
lemma set_cases {p : set Ξ± β Prop} (h0 : p β
) (h1 : p univ) (s) : p s :=
s.eq_empty_or_nonempty.elim (Ξ» h, h.symm βΈ h0) $ Ξ» h, (eq_univ_of_nonempty h).symm βΈ h1
end subsingleton
|
02cc8c20b0f2df5fcdf9e27a5434be32dac7434c | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/topology/metric_space/closeds.lean | 873c9b828995d2bff4f6be64f97ac0d60ff6c685 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,321 | lean | /-
Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: SΓ©bastien GouΓ«zel
-/
import topology.metric_space.hausdorff_distance
import topology.compacts
import analysis.specific_limits
/-!
# Closed subsets
This file defines the metric and emetric space structure on the types of closed subsets and nonempty
compact subsets of a metric or emetric space.
The Hausdorff distance induces an emetric space structure on the type of closed subsets
of an emetric space, called `closeds`. Its completeness, resp. compactness, resp.
second-countability, follow from the corresponding properties of the original space.
In a metric space, the type of nonempty compact subsets (called `nonempty_compacts`) also
inherits a metric space structure from the Hausdorff distance, as the Hausdorff edistance is
always finite in this context.
-/
noncomputable theory
open_locale classical topological_space ennreal
universe u
open classical set function topological_space filter
namespace emetric
section
variables {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±}
/-- In emetric spaces, the Hausdorff edistance defines an emetric space structure
on the type of closed subsets -/
instance closeds.emetric_space : emetric_space (closeds Ξ±) :=
{ edist := Ξ»s t, Hausdorff_edist s.val t.val,
edist_self := Ξ»s, Hausdorff_edist_self,
edist_comm := Ξ»s t, Hausdorff_edist_comm,
edist_triangle := Ξ»s t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero :=
Ξ»s t h, subtype.eq ((Hausdorff_edist_zero_iff_eq_of_closed s.property t.property).1 h) }
/-- The edistance to a closed set depends continuously on the point and the set -/
lemma continuous_inf_edist_Hausdorff_edist :
continuous (Ξ»p : Ξ± Γ (closeds Ξ±), inf_edist p.1 (p.2).val) :=
begin
refine continuous_of_le_add_edist 2 (by simp) _,
rintros β¨x, sβ© β¨y, tβ©,
calc inf_edist x (s.val) β€ inf_edist x (t.val) + Hausdorff_edist (t.val) (s.val) :
inf_edist_le_inf_edist_add_Hausdorff_edist
... β€ (inf_edist y (t.val) + edist x y) + Hausdorff_edist (t.val) (s.val) :
add_le_add_right inf_edist_le_inf_edist_add_edist _
... = inf_edist y (t.val) + (edist x y + Hausdorff_edist (s.val) (t.val)) :
by simp [add_comm, add_left_comm, Hausdorff_edist_comm, -subtype.val_eq_coe]
... β€ inf_edist y (t.val) + (edist (x, s) (y, t) + edist (x, s) (y, t)) :
add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _
... = inf_edist y (t.val) + 2 * edist (x, s) (y, t) :
by rw [β mul_two, mul_comm]
end
/-- Subsets of a given closed subset form a closed set -/
lemma is_closed_subsets_of_is_closed (hs : is_closed s) :
is_closed {t : closeds Ξ± | t.val β s} :=
begin
refine is_closed_of_closure_subset (Ξ»t ht x hx, _),
-- t : closeds Ξ±, ht : t β closure {t : closeds Ξ± | t.val β s},
-- x : Ξ±, hx : x β t.val
-- goal : x β s
have : x β closure s,
{ refine mem_closure_iff.2 (λΡ Ρpos, _),
rcases mem_closure_iff.1 ht Ξ΅ Ξ΅pos with β¨u, hu, Dtuβ©,
-- u : closeds Ξ±, hu : u β {t : closeds Ξ± | t.val β s}, hu' : edist t u < Ξ΅
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dtu with β¨y, hy, Dxyβ©,
-- y : Ξ±, hy : y β u.val, Dxy : edist x y < Ξ΅
exact β¨y, hu hy, Dxyβ© },
rwa hs.closure_eq at this,
end
/-- By definition, the edistance on `closeds Ξ±` is given by the Hausdorff edistance -/
lemma closeds.edist_eq {s t : closeds Ξ±} : edist s t = Hausdorff_edist s.val t.val := rfl
/-- In a complete space, the type of closed subsets is complete for the
Hausdorff edistance. -/
instance closeds.complete_space [complete_space Ξ±] : complete_space (closeds Ξ±) :=
begin
/- We will show that, if a sequence of sets `s n` satisfies
`edist (s n) (s (n+1)) < 2^{-n}`, then it converges. This is enough to guarantee
completeness, by a standard completeness criterion.
We use the shorthand `B n = 2^{-n}` in ennreal. -/
let B : β β ββ₯0β := Ξ» n, (2β»ΒΉ)^n,
have B_pos : β n, (0:ββ₯0β) < B n,
by simp [B, ennreal.pow_pos],
have B_ne_top : β n, B n β β€,
by simp [B, ennreal.pow_ne_top],
/- Consider a sequence of closed sets `s n` with `edist (s n) (s (n+1)) < B n`.
We will show that it converges. The limit set is t0 = βn, closure (βmβ₯n, s m).
We will have to show that a point in `s n` is close to a point in `t0`, and a point
in `t0` is close to a point in `s n`. The completeness then follows from a
standard criterion. -/
refine complete_of_convergent_controlled_sequences B B_pos (Ξ»s hs, _),
let t0 := βn, closure (βmβ₯n, (s m).val),
let t : closeds Ξ± := β¨t0, is_closed_Inter (Ξ»_, is_closed_closure)β©,
use t,
-- The inequality is written this way to agree with `edist_le_of_edist_le_geometric_of_tendstoβ`
have I1 : βn:β, βx β (s n).val, βy β t0, edist x y β€ 2 * B n,
{ /- This is the main difficulty of the proof. Starting from `x β s n`, we want
to find a point in `t0` which is close to `x`. Define inductively a sequence of
points `z m` with `z n = x` and `z m β s m` and `edist (z m) (z (m+1)) β€ B m`. This is
possible since the Hausdorff distance between `s m` and `s (m+1)` is at most `B m`.
This sequence is a Cauchy sequence, therefore converging as the space is complete, to
a limit which satisfies the required properties. -/
assume n x hx,
obtain β¨z, hzβ, hzβ© : β z : Ξ l, (s (n+l)).val, (z 0:Ξ±) = x β§
β k, edist (z k:Ξ±) (z (k+1):Ξ±) β€ B n / 2^k,
{ -- We prove existence of the sequence by induction.
have : β (l : β) (z : (s (n+l)).val), β z' : (s (n+l+1)).val, edist (z:Ξ±) z' β€ B n / 2^l,
{ assume l z,
obtain β¨z', z'_mem, hz'β© : β z' β (s (n+l+1)).val, edist (z:Ξ±) z' < B n / 2^l,
{ apply exists_edist_lt_of_Hausdorff_edist_lt z.2,
simp only [B, ennreal.inv_pow, div_eq_mul_inv],
rw [β pow_add],
apply hs; simp },
exact β¨β¨z', z'_memβ©, le_of_lt hz'β© },
use [Ξ» k, nat.rec_on k β¨x, hxβ© (Ξ»l z, some (this l z)), rfl],
exact Ξ» k, some_spec (this k _) },
-- it follows from the previous bound that `z` is a Cauchy sequence
have : cauchy_seq (Ξ» k, ((z k):Ξ±)),
from cauchy_seq_of_edist_le_geometric_two (B n) (B_ne_top n) hz,
-- therefore, it converges
rcases cauchy_seq_tendsto_of_complete this with β¨y, y_limβ©,
use y,
-- the limit point `y` will be the desired point, in `t0` and close to our initial point `x`.
-- First, we check it belongs to `t0`.
have : y β t0 := mem_Inter.2 (Ξ»k, mem_closure_of_tendsto y_lim
begin
simp only [exists_prop, set.mem_Union, filter.eventually_at_top, set.mem_preimage,
set.preimage_Union],
exact β¨k, Ξ» m hm, β¨n+m, zero_add k βΈ add_le_add (zero_le n) hm, (z m).2β©β©
end),
use this,
-- Then, we check that `y` is close to `x = z n`. This follows from the fact that `y`
-- is the limit of `z k`, and the distance between `z n` and `z k` has already been estimated.
rw [β hzβ],
exact edist_le_of_edist_le_geometric_two_of_tendstoβ (B n) hz y_lim },
have I2 : βn:β, βx β t0, βy β (s n).val, edist x y β€ 2 * B n,
{ /- For the (much easier) reverse inequality, we start from a point `x β t0` and we want
to find a point `y β s n` which is close to `x`.
`x` belongs to `t0`, the intersection of the closures. In particular, it is well
approximated by a point `z` in `βmβ₯n, s m`, say in `s m`. Since `s m` and
`s n` are close, this point is itself well approximated by a point `y` in `s n`,
as required. -/
assume n x xt0,
have : x β closure (βmβ₯n, (s m).val), by apply mem_Inter.1 xt0 n,
rcases mem_closure_iff.1 this (B n) (B_pos n) with β¨z, hz, Dxzβ©,
-- z : Ξ±, Dxz : edist x z < B n,
simp only [exists_prop, set.mem_Union] at hz,
rcases hz with β¨m, β¨m_ge_n, hmβ©β©,
-- m : β, m_ge_n : m β₯ n, hm : z β (s m).val
have : Hausdorff_edist (s m).val (s n).val < B n := hs n m n m_ge_n (le_refl n),
rcases exists_edist_lt_of_Hausdorff_edist_lt hm this with β¨y, hy, Dzyβ©,
-- y : Ξ±, hy : y β (s n).val, Dzy : edist z y < B n
exact β¨y, hy, calc
edist x y β€ edist x z + edist z y : edist_triangle _ _ _
... β€ B n + B n : add_le_add (le_of_lt Dxz) (le_of_lt Dzy)
... = 2 * B n : (two_mul _).symm β© },
-- Deduce from the above inequalities that the distance between `s n` and `t0` is at most `2 B n`.
have main : βn:β, edist (s n) t β€ 2 * B n := Ξ»n, Hausdorff_edist_le_of_mem_edist (I1 n) (I2 n),
-- from this, the convergence of `s n` to `t0` follows.
refine tendsto_at_top.2 (λΡ Ρpos, _),
have : tendsto (Ξ»n, 2 * B n) at_top (π (2 * 0)),
from ennreal.tendsto.const_mul
(ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 $ by simp [ennreal.one_lt_two])
(or.inr $ by simp),
rw mul_zero at this,
obtain β¨N, hNβ© : β N, β b β₯ N, Ξ΅ > 2 * B b,
from ((tendsto_order.1 this).2 Ξ΅ Ξ΅pos).exists_forall_of_at_top,
exact β¨N, Ξ»n hn, lt_of_le_of_lt (main n) (hN n hn)β©
end
/-- In a compact space, the type of closed subsets is compact. -/
instance closeds.compact_space [compact_space Ξ±] : compact_space (closeds Ξ±) :=
β¨begin
/- by completeness, it suffices to show that it is totally bounded,
i.e., for all Ξ΅>0, there is a finite set which is Ξ΅-dense.
start from a set `s` which is Ξ΅-dense in Ξ±. Then the subsets of `s`
are finitely many, and Ξ΅-dense for the Hausdorff distance. -/
refine compact_of_totally_bounded_is_closed (emetric.totally_bounded_iff.2 (λΡ Ρpos, _))
is_closed_univ,
rcases exists_between Ξ΅pos with β¨Ξ΄, Ξ΄pos, Ξ΄ltβ©,
rcases emetric.totally_bounded_iff.1
(compact_iff_totally_bounded_complete.1 (@compact_univ Ξ± _ _)).1 Ξ΄ Ξ΄pos with β¨s, fs, hsβ©,
-- s : set Ξ±, fs : finite s, hs : univ β β (y : Ξ±) (H : y β s), eball y Ξ΄
-- we first show that any set is well approximated by a subset of `s`.
have main : β u : set Ξ±, βv β s, Hausdorff_edist u v β€ Ξ΄,
{ assume u,
let v := {x : Ξ± | x β s β§ βyβu, edist x y < Ξ΄},
existsi [v, ((Ξ»x hx, hx.1) : v β s)],
refine Hausdorff_edist_le_of_mem_edist _ _,
{ assume x hx,
have : x β βy β s, ball y Ξ΄ := hs (by simp),
rcases mem_bUnion_iff.1 this with β¨y, ys, dyβ©,
have : edist y x < Ξ΄ := by simp at dy; rwa [edist_comm] at dy,
exact β¨y, β¨ys, β¨x, hx, thisβ©β©, le_of_lt dyβ© },
{ rintros x β¨hx1, β¨y, yu, hyβ©β©,
exact β¨y, yu, le_of_lt hyβ© }},
-- introduce the set F of all subsets of `s` (seen as members of `closeds Ξ±`).
let F := {f : closeds Ξ± | f.val β s},
use F,
split,
-- `F` is finite
{ apply @finite_of_finite_image _ _ F (Ξ»f, f.val),
{ exact subtype.val_injective.inj_on F },
{ refine fs.finite_subsets.subset (Ξ»b, _),
simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib],
assume x hx hx',
rwa hx' at hx }},
-- `F` is Ξ΅-dense
{ assume u _,
rcases main u.val with β¨t0, t0s, Dut0β©,
have : is_closed t0 := (fs.subset t0s).is_compact.is_closed,
let t : closeds Ξ± := β¨t0, thisβ©,
have : t β F := t0s,
have : edist u t < Ξ΅ := lt_of_le_of_lt Dut0 Ξ΄lt,
apply mem_bUnion_iff.2,
exact β¨t, βΉt β FβΊ, thisβ© }
endβ©
/-- In an emetric space, the type of non-empty compact subsets is an emetric space,
where the edistance is the Hausdorff edistance -/
instance nonempty_compacts.emetric_space : emetric_space (nonempty_compacts Ξ±) :=
{ edist := Ξ»s t, Hausdorff_edist s.val t.val,
edist_self := Ξ»s, Hausdorff_edist_self,
edist_comm := Ξ»s t, Hausdorff_edist_comm,
edist_triangle := Ξ»s t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero := Ξ»s t h, subtype.eq $ begin
have : closure (s.val) = closure (t.val) := Hausdorff_edist_zero_iff_closure_eq_closure.1 h,
rwa [s.property.2.is_closed.closure_eq,
t.property.2.is_closed.closure_eq] at this,
end }
/-- `nonempty_compacts.to_closeds` is a uniform embedding (as it is an isometry) -/
lemma nonempty_compacts.to_closeds.uniform_embedding :
uniform_embedding (@nonempty_compacts.to_closeds Ξ± _ _) :=
isometry.uniform_embedding $ Ξ»x y, rfl
/-- The range of `nonempty_compacts.to_closeds` is closed in a complete space -/
lemma nonempty_compacts.is_closed_in_closeds [complete_space Ξ±] :
is_closed (range $ @nonempty_compacts.to_closeds Ξ± _ _) :=
begin
have : range nonempty_compacts.to_closeds = {s : closeds Ξ± | s.val.nonempty β§ is_compact s.val},
from range_inclusion _,
rw this,
refine is_closed_of_closure_subset (Ξ»s hs, β¨_, _β©),
{ -- take a set set t which is nonempty and at a finite distance of s
rcases mem_closure_iff.1 hs β€ ennreal.coe_lt_top with β¨t, ht, Dstβ©,
rw edist_comm at Dst,
-- since `t` is nonempty, so is `s`
exact nonempty_of_Hausdorff_edist_ne_top ht.1 (ne_of_lt Dst) },
{ refine compact_iff_totally_bounded_complete.2 β¨_, s.property.is_completeβ©,
refine totally_bounded_iff.2 (λΡ (Ρpos : 0 < Ρ), _),
-- we have to show that s is covered by finitely many eballs of radius Ξ΅
-- pick a nonempty compact set t at distance at most Ξ΅/2 of s
rcases mem_closure_iff.1 hs (Ξ΅/2) (ennreal.half_pos Ξ΅pos.ne') with β¨t, ht, Dstβ©,
-- cover this space with finitely many balls of radius Ξ΅/2
rcases totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 ht.2).1 (Ξ΅/2)
(ennreal.half_pos Ξ΅pos.ne') with β¨u, fu, utβ©,
refine β¨u, β¨fu, Ξ»x hx, _β©β©,
-- u : set Ξ±, fu : finite u, ut : t.val β β (y : Ξ±) (H : y β u), eball y (Ξ΅ / 2)
-- then s is covered by the union of the balls centered at u of radius Ξ΅
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dst with β¨z, hz, Dxzβ©,
rcases mem_bUnion_iff.1 (ut hz) with β¨y, hy, Dzyβ©,
have : edist x y < Ξ΅ := calc
edist x y β€ edist x z + edist z y : edist_triangle _ _ _
... < Ξ΅/2 + Ξ΅/2 : ennreal.add_lt_add Dxz Dzy
... = Ξ΅ : ennreal.add_halves _,
exact mem_bUnion hy this },
end
/-- In a complete space, the type of nonempty compact subsets is complete. This follows
from the same statement for closed subsets -/
instance nonempty_compacts.complete_space [complete_space Ξ±] :
complete_space (nonempty_compacts Ξ±) :=
(complete_space_iff_is_complete_range
nonempty_compacts.to_closeds.uniform_embedding.to_uniform_inducing).2 $
nonempty_compacts.is_closed_in_closeds.is_complete
/-- In a compact space, the type of nonempty compact subsets is compact. This follows from
the same statement for closed subsets -/
instance nonempty_compacts.compact_space [compact_space Ξ±] : compact_space (nonempty_compacts Ξ±) :=
β¨begin
rw nonempty_compacts.to_closeds.uniform_embedding.embedding.is_compact_iff_is_compact_image,
rw [image_univ],
exact nonempty_compacts.is_closed_in_closeds.is_compact
endβ©
/-- In a second countable space, the type of nonempty compact subsets is second countable -/
instance nonempty_compacts.second_countable_topology [second_countable_topology Ξ±] :
second_countable_topology (nonempty_compacts Ξ±) :=
begin
haveI : separable_space (nonempty_compacts Ξ±) :=
begin
/- To obtain a countable dense subset of `nonempty_compacts Ξ±`, start from
a countable dense subset `s` of Ξ±, and then consider all its finite nonempty subsets.
This set is countable and made of nonempty compact sets. It turns out to be dense:
by total boundedness, any compact set `t` can be covered by finitely many small balls, and
approximations in `s` of the centers of these balls give the required finite approximation
of `t`. -/
rcases exists_countable_dense Ξ± with β¨s, cs, s_denseβ©,
let v0 := {t : set Ξ± | finite t β§ t β s},
let v : set (nonempty_compacts Ξ±) := {t : nonempty_compacts Ξ± | t.val β v0},
refine β¨β¨v, β¨_, _β©β©β©,
{ have : countable (subtype.val '' v),
{ refine (countable_set_of_finite_subset cs).mono (Ξ»x hx, _),
rcases (mem_image _ _ _).1 hx with β¨y, β¨hy, yxβ©β©,
rw β yx,
exact hy },
apply countable_of_injective_of_countable_image _ this,
apply subtype.val_injective.inj_on },
{ refine λt, mem_closure_iff.2 (λΡ Ρpos, _),
-- t is a compact nonempty set, that we have to approximate uniformly by a a set in `v`.
rcases exists_between Ξ΅pos with β¨Ξ΄, Ξ΄pos, Ξ΄ltβ©,
have Ξ΄pos' : 0 < Ξ΄ / 2, from ennreal.half_pos Ξ΄pos.ne',
-- construct a map F associating to a point in Ξ± an approximating point in s, up to Ξ΄/2.
have Exy : βx, βy, y β s β§ edist x y < Ξ΄/2,
{ assume x,
rcases mem_closure_iff.1 (s_dense x) (Ξ΄/2) Ξ΄pos' with β¨y, ys, hyβ©,
exact β¨y, β¨ys, hyβ©β© },
let F := Ξ»x, some (Exy x),
have Fspec : βx, F x β s β§ edist x (F x) < Ξ΄/2 := Ξ»x, some_spec (Exy x),
-- cover `t` with finitely many balls. Their centers form a set `a`
have : totally_bounded t.val := (compact_iff_totally_bounded_complete.1 t.property.2).1,
rcases totally_bounded_iff.1 this (Ξ΄/2) Ξ΄pos' with β¨a, af, taβ©,
-- a : set Ξ±, af : finite a, ta : t.val β β (y : Ξ±) (H : y β a), eball y (Ξ΄ / 2)
-- replace each center by a nearby approximation in `s`, giving a new set `b`
let b := F '' a,
have : finite b := af.image _,
have tb : βx β t.val, βy β b, edist x y < Ξ΄,
{ assume x hx,
rcases mem_bUnion_iff.1 (ta hx) with β¨z, za, Dxzβ©,
existsi [F z, mem_image_of_mem _ za],
calc edist x (F z) β€ edist x z + edist z (F z) : edist_triangle _ _ _
... < Ξ΄/2 + Ξ΄/2 : ennreal.add_lt_add Dxz (Fspec z).2
... = Ξ΄ : ennreal.add_halves _ },
-- keep only the points in `b` that are close to point in `t`, yielding a new set `c`
let c := {y β b | βxβt.val, edist x y < Ξ΄},
have : finite c := βΉfinite bβΊ.subset (Ξ»x hx, hx.1),
-- points in `t` are well approximated by points in `c`
have tc : βx β t.val, βy β c, edist x y β€ Ξ΄,
{ assume x hx,
rcases tb x hx with β¨y, yv, Dxyβ©,
have : y β c := by simp [c, -mem_image]; exact β¨yv, β¨x, hx, Dxyβ©β©,
exact β¨y, this, le_of_lt Dxyβ© },
-- points in `c` are well approximated by points in `t`
have ct : βy β c, βx β t.val, edist y x β€ Ξ΄,
{ rintros y β¨hy1, β¨x, xt, Dyxβ©β©,
have : edist y x β€ Ξ΄ := calc
edist y x = edist x y : edist_comm _ _
... β€ Ξ΄ : le_of_lt Dyx,
exact β¨x, xt, thisβ© },
-- it follows that their Hausdorff distance is small
have : Hausdorff_edist t.val c β€ Ξ΄ :=
Hausdorff_edist_le_of_mem_edist tc ct,
have Dtc : Hausdorff_edist t.val c < Ξ΅ := lt_of_le_of_lt this Ξ΄lt,
-- the set `c` is not empty, as it is well approximated by a nonempty set
have hc : c.nonempty,
from nonempty_of_Hausdorff_edist_ne_top t.property.1 (ne_top_of_lt Dtc),
-- let `d` be the version of `c` in the type `nonempty_compacts Ξ±`
let d : nonempty_compacts Ξ± := β¨c, β¨hc, βΉfinite cβΊ.is_compactβ©β©,
have : c β s,
{ assume x hx,
rcases (mem_image _ _ _).1 hx.1 with β¨y, β¨ya, yxβ©β©,
rw β yx,
exact (Fspec y).1 },
have : d β v := β¨βΉfinite cβΊ, thisβ©,
-- we have proved that `d` is a good approximation of `t` as requested
exact β¨d, βΉd β vβΊ, Dtcβ© },
end,
apply second_countable_of_separable,
end
end --section
end emetric --namespace
namespace metric
section
variables {Ξ± : Type u} [metric_space Ξ±]
/-- `nonempty_compacts Ξ±` inherits a metric space structure, as the Hausdorff
edistance between two such sets is finite. -/
instance nonempty_compacts.metric_space : metric_space (nonempty_compacts Ξ±) :=
emetric_space.to_metric_space $ Ξ»x y, Hausdorff_edist_ne_top_of_nonempty_of_bounded x.2.1 y.2.1
x.2.2.bounded y.2.2.bounded
/-- The distance on `nonempty_compacts Ξ±` is the Hausdorff distance, by construction -/
lemma nonempty_compacts.dist_eq {x y : nonempty_compacts Ξ±} :
dist x y = Hausdorff_dist x.val y.val := rfl
lemma lipschitz_inf_dist_set (x : Ξ±) :
lipschitz_with 1 (Ξ» s : nonempty_compacts Ξ±, inf_dist x s.val) :=
lipschitz_with.of_le_add $ assume s t,
by { rw dist_comm,
exact inf_dist_le_inf_dist_add_Hausdorff_dist (edist_ne_top t s) }
lemma lipschitz_inf_dist :
lipschitz_with 2 (Ξ» p : Ξ± Γ (nonempty_compacts Ξ±), inf_dist p.1 p.2.val) :=
@lipschitz_with.uncurry _ _ _ _ _ _ (Ξ» (x : Ξ±) (s : nonempty_compacts Ξ±), inf_dist x s.val) 1 1
(Ξ» s, lipschitz_inf_dist_pt s.val) lipschitz_inf_dist_set
lemma uniform_continuous_inf_dist_Hausdorff_dist :
uniform_continuous (Ξ»p : Ξ± Γ (nonempty_compacts Ξ±), inf_dist p.1 (p.2).val) :=
lipschitz_inf_dist.uniform_continuous
end --section
end metric --namespace
|
9d3d71286c8c8f4f94cb0bcb0330d969a61081ac | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/tactic/norm_num.lean | 29ca1f35a1488a019f384f2d8205a6d8f8498db2 | [
"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 | 62,145 | lean | /-
Copyright (c) 2017 Simon Hudon All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Mario Carneiro
-/
import data.rat.cast
import data.rat.meta_defs
import tactic.doc_commands
/-!
# `norm_num`
Evaluating arithmetic expressions including *, +, -, ^, β€
-/
universes u v w
namespace tactic
/-- Reflexivity conversion: given `e` returns `(e, β’ e = e)` -/
meta def refl_conv (e : expr) : tactic (expr Γ expr) :=
do p β mk_eq_refl e, return (e, p)
/-- Transitivity conversion: given two conversions (which take an
expression `e` and returns `(e', β’ e = e')`), produces another
conversion that combines them with transitivity, treating failures
as reflexivity conversions. -/
meta def trans_conv (tβ tβ : expr β tactic (expr Γ expr)) (e : expr) :
tactic (expr Γ expr) :=
(do (eβ, pβ) β tβ e,
(do (eβ, pβ) β tβ eβ,
p β mk_eq_trans pβ pβ, return (eβ, p)) <|>
return (eβ, pβ)) <|> tβ e
namespace instance_cache
/-- Faster version of `mk_app ``bit0 [e]`. -/
meta def mk_bit0 (c : instance_cache) (e : expr) : tactic (instance_cache Γ expr) :=
do (c, ai) β c.get ``has_add,
return (c, (expr.const ``bit0 [c.univ]).mk_app [c.Ξ±, ai, e])
/-- Faster version of `mk_app ``bit1 [e]`. -/
meta def mk_bit1 (c : instance_cache) (e : expr) : tactic (instance_cache Γ expr) :=
do (c, ai) β c.get ``has_add,
(c, oi) β c.get ``has_one,
return (c, (expr.const ``bit1 [c.univ]).mk_app [c.Ξ±, oi, ai, e])
end instance_cache
end tactic
open tactic
namespace norm_num
variable {Ξ± : Type u}
lemma subst_into_add {Ξ±} [has_add Ξ±] (l r tl tr t)
(prl : (l : Ξ±) = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t :=
by rw [prl, prr, prt]
lemma subst_into_mul {Ξ±} [has_mul Ξ±] (l r tl tr t)
(prl : (l : Ξ±) = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t :=
by rw [prl, prr, prt]
lemma subst_into_neg {Ξ±} [has_neg Ξ±] (a ta t : Ξ±) (pra : a = ta) (prt : -ta = t) : -a = t :=
by simp [pra, prt]
/-- The result type of `match_numeral`, either `0`, `1`, or a top level
decomposition of `bit0 e` or `bit1 e`. The `other` case means it is not a numeral. -/
meta inductive match_numeral_result
| zero | one | bit0 (e : expr) | bit1 (e : expr) | other
/-- Unfold the top level constructor of the numeral expression. -/
meta def match_numeral : expr β match_numeral_result
| `(bit0 %%e) := match_numeral_result.bit0 e
| `(bit1 %%e) := match_numeral_result.bit1 e
| `(@has_zero.zero _ _) := match_numeral_result.zero
| `(@has_one.one _ _) := match_numeral_result.one
| _ := match_numeral_result.other
theorem zero_succ {Ξ±} [semiring Ξ±] : (0 + 1 : Ξ±) = 1 := zero_add _
theorem one_succ {Ξ±} [semiring Ξ±] : (1 + 1 : Ξ±) = 2 := rfl
theorem bit0_succ {Ξ±} [semiring Ξ±] (a : Ξ±) : bit0 a + 1 = bit1 a := rfl
theorem bit1_succ {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit1 a + 1 = bit0 b :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
section
open match_numeral_result
/-- Given `a`, `b` natural numerals, proves `β’ a + 1 = b`, assuming that this is provable.
(It may prove garbage instead of failing if `a + 1 = b` is false.) -/
meta def prove_succ : instance_cache β expr β expr β tactic (instance_cache Γ expr)
| c e r := match match_numeral e with
| zero := c.mk_app ``zero_succ []
| one := c.mk_app ``one_succ []
| bit0 e := c.mk_app ``bit0_succ [e]
| bit1 e := do
let r := r.app_arg,
(c, p) β prove_succ c e r,
c.mk_app ``bit1_succ [e, r, p]
| _ := failed
end
end
theorem zero_adc {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 0 + a + 1 = b := by rwa zero_add
theorem adc_zero {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : a + 0 + 1 = b := by rwa add_zero
theorem one_add {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + a = b := by rwa add_comm
theorem add_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit0 b = bit0 c :=
h βΈ by simp [bit0, add_left_comm, add_assoc]
theorem add_bit0_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit1 b = bit1 c :=
h βΈ by simp [bit0, bit1, add_left_comm, add_assoc]
theorem add_bit1_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit1 a + bit0 b = bit1 c :=
h βΈ by simp [bit0, bit1, add_left_comm, add_comm]
theorem add_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit1 b = bit0 c :=
h βΈ by simp [bit0, bit1, add_left_comm, add_comm]
theorem adc_one_one {Ξ±} [semiring Ξ±] : (1 + 1 + 1 : Ξ±) = 3 := rfl
theorem adc_bit0_one {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit0 a + 1 + 1 = bit0 b :=
h βΈ by simp [bit0, add_left_comm, add_assoc]
theorem adc_one_bit0 {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + bit0 a + 1 = bit0 b :=
h βΈ by simp [bit0, add_left_comm, add_assoc]
theorem adc_bit1_one {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit1 a + 1 + 1 = bit1 b :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
theorem adc_one_bit1 {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + bit1 a + 1 = bit1 b :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
theorem adc_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit0 b + 1 = bit1 c :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
theorem adc_bit1_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit0 b + 1 = bit0 c :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
theorem adc_bit0_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit0 a + bit1 b + 1 = bit0 c :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
theorem adc_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit1 b + 1 = bit1 c :=
h βΈ by simp [bit1, bit0, add_left_comm, add_assoc]
section
open match_numeral_result
meta mutual def prove_add_nat, prove_adc_nat
with prove_add_nat : instance_cache β expr β expr β expr β tactic (instance_cache Γ expr)
| c a b r := do
match match_numeral a, match_numeral b with
| zero, _ := c.mk_app ``zero_add [b]
| _, zero := c.mk_app ``add_zero [a]
| _, one := prove_succ c a r
| one, _ := do (c, p) β prove_succ c b r, c.mk_app ``one_add [b, r, p]
| bit0 a, bit0 b := do let r := r.app_arg, (c, p) β prove_add_nat c a b r, c.mk_app ``add_bit0_bit0 [a, b, r, p]
| bit0 a, bit1 b := do let r := r.app_arg, (c, p) β prove_add_nat c a b r, c.mk_app ``add_bit0_bit1 [a, b, r, p]
| bit1 a, bit0 b := do let r := r.app_arg, (c, p) β prove_add_nat c a b r, c.mk_app ``add_bit1_bit0 [a, b, r, p]
| bit1 a, bit1 b := do let r := r.app_arg, (c, p) β prove_adc_nat c a b r, c.mk_app ``add_bit1_bit1 [a, b, r, p]
| _, _ := failed
end
with prove_adc_nat : instance_cache β expr β expr β expr β tactic (instance_cache Γ expr)
| c a b r := do
match match_numeral a, match_numeral b with
| zero, _ := do (c, p) β prove_succ c b r, c.mk_app ``zero_adc [b, r, p]
| _, zero := do (c, p) β prove_succ c b r, c.mk_app ``adc_zero [b, r, p]
| one, one := c.mk_app ``adc_one_one []
| bit0 a, one := do let r := r.app_arg, (c, p) β prove_succ c a r, c.mk_app ``adc_bit0_one [a, r, p]
| one, bit0 b := do let r := r.app_arg, (c, p) β prove_succ c b r, c.mk_app ``adc_one_bit0 [b, r, p]
| bit1 a, one := do let r := r.app_arg, (c, p) β prove_succ c a r, c.mk_app ``adc_bit1_one [a, r, p]
| one, bit1 b := do let r := r.app_arg, (c, p) β prove_succ c b r, c.mk_app ``adc_one_bit1 [b, r, p]
| bit0 a, bit0 b := do let r := r.app_arg, (c, p) β prove_add_nat c a b r, c.mk_app ``adc_bit0_bit0 [a, b, r, p]
| bit0 a, bit1 b := do let r := r.app_arg, (c, p) β prove_adc_nat c a b r, c.mk_app ``adc_bit0_bit1 [a, b, r, p]
| bit1 a, bit0 b := do let r := r.app_arg, (c, p) β prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit0 [a, b, r, p]
| bit1 a, bit1 b := do let r := r.app_arg, (c, p) β prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit1 [a, b, r, p]
| _, _ := failed
end
/-- Given `a`,`b`,`r` natural numerals, proves `β’ a + b = r`. -/
add_decl_doc prove_add_nat
/-- Given `a`,`b`,`r` natural numerals, proves `β’ a + b + 1 = r`. -/
add_decl_doc prove_adc_nat
/-- Given `a`,`b` natural numerals, returns `(r, β’ a + b = r)`. -/
meta def prove_add_nat' (c : instance_cache) (a b : expr) : tactic (instance_cache Γ expr Γ expr) := do
na β a.to_nat,
nb β b.to_nat,
(c, r) β c.of_nat (na + nb),
(c, p) β prove_add_nat c a b r,
return (c, r, p)
end
theorem bit0_mul {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) :
bit0 a * b = bit0 c := h βΈ by simp [bit0, add_mul]
theorem mul_bit0' {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) :
a * bit0 b = bit0 c := h βΈ by simp [bit0, mul_add]
theorem mul_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) :
bit0 a * bit0 b = bit0 (bit0 c) := bit0_mul _ _ _ (mul_bit0' _ _ _ h)
theorem mul_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c d e : Ξ±)
(hc : a * b = c) (hd : a + b = d) (he : bit0 c + d = e) :
bit1 a * bit1 b = bit1 e :=
by rw [β he, β hd, β hc]; simp [bit1, bit0, mul_add, add_mul, add_left_comm, add_assoc]
section
open match_numeral_result
/-- Given `a`,`b` natural numerals, returns `(r, β’ a * b = r)`. -/
meta def prove_mul_nat : instance_cache β expr β expr β tactic (instance_cache Γ expr Γ expr)
| ic a b :=
match match_numeral a, match_numeral b with
| zero, _ := do
(ic, z) β ic.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``zero_mul [b],
return (ic, z, p)
| _, zero := do
(ic, z) β ic.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``mul_zero [a],
return (ic, z, p)
| one, _ := do (ic, p) β ic.mk_app ``one_mul [b], return (ic, b, p)
| _, one := do (ic, p) β ic.mk_app ``mul_one [a], return (ic, a, p)
| bit0 a, bit0 b := do
(ic, c, p) β prove_mul_nat ic a b,
(ic, p) β ic.mk_app ``mul_bit0_bit0 [a, b, c, p],
(ic, c') β ic.mk_bit0 c,
(ic, c') β ic.mk_bit0 c',
return (ic, c', p)
| bit0 a, _ := do
(ic, c, p) β prove_mul_nat ic a b,
(ic, p) β ic.mk_app ``bit0_mul [a, b, c, p],
(ic, c') β ic.mk_bit0 c,
return (ic, c', p)
| _, bit0 b := do
(ic, c, p) β prove_mul_nat ic a b,
(ic, p) β ic.mk_app ``mul_bit0' [a, b, c, p],
(ic, c') β ic.mk_bit0 c,
return (ic, c', p)
| bit1 a, bit1 b := do
(ic, c, pc) β prove_mul_nat ic a b,
(ic, d, pd) β prove_add_nat' ic a b,
(ic, c') β ic.mk_bit0 c,
(ic, e, pe) β prove_add_nat' ic c' d,
(ic, p) β ic.mk_app ``mul_bit1_bit1 [a, b, c, d, e, pc, pd, pe],
(ic, e') β ic.mk_bit1 e,
return (ic, e', p)
| _, _ := failed
end
end
section
open match_numeral_result
/-- Given `a` a positive natural numeral, returns `β’ 0 < a`. -/
meta def prove_pos_nat (c : instance_cache) : expr β tactic (instance_cache Γ expr)
| e :=
match match_numeral e with
| one := c.mk_app ``zero_lt_one []
| bit0 e := do (c, p) β prove_pos_nat e, c.mk_app ``bit0_pos [e, p]
| bit1 e := do (c, p) β prove_pos_nat e, c.mk_app ``bit1_pos' [e, p]
| _ := failed
end
end
/-- Given `a` a rational numeral, returns `β’ 0 < a`. -/
meta def prove_pos (c : instance_cache) : expr β tactic (instance_cache Γ expr)
| `(%%eβ / %%eβ) := do
(c, pβ) β prove_pos_nat c eβ, (c, pβ) β prove_pos_nat c eβ,
c.mk_app ``div_pos_of_pos_of_pos [eβ, eβ, pβ, pβ]
| e := prove_pos_nat c e
/-- `match_neg (- e) = some e`, otherwise `none` -/
meta def match_neg : expr β option expr
| `(- %%e) := some e
| _ := none
/-- `match_sign (- e) = inl e`, `match_sign 0 = inr ff`, otherwise `inr tt` -/
meta def match_sign : expr β expr β bool
| `(- %%e) := sum.inl e
| `(has_zero.zero) := sum.inr ff
| _ := sum.inr tt
theorem ne_zero_of_pos {Ξ±} [ordered_add_comm_group Ξ±] (a : Ξ±) : 0 < a β a β 0 := ne_of_gt
theorem ne_zero_neg {Ξ±} [add_group Ξ±] (a : Ξ±) : a β 0 β -a β 0 := mt neg_eq_zero.1
/-- Given `a` a rational numeral, returns `β’ a β 0`. -/
meta def prove_ne_zero (c : instance_cache) : expr β tactic (instance_cache Γ expr)
| a :=
match match_neg a with
| some a := do (c, p) β prove_ne_zero a, c.mk_app ``ne_zero_neg [a, p]
| none := do (c, p) β prove_pos c a, c.mk_app ``ne_zero_of_pos [a, p]
end
theorem clear_denom_div {Ξ±} [division_ring Ξ±] (a b b' c d : Ξ±)
(hβ : b β 0) (hβ : b * b' = d) (hβ : a * b' = c) : (a / b) * d = c :=
by rwa [β hβ, β mul_assoc, div_mul_cancel _ hβ]
/-- Given `a` nonnegative rational and `d` a natural number, returns `(b, β’ a * d = b)`.
(`d` should be a multiple of the denominator of `a`, so that `b` is a natural number.) -/
meta def prove_clear_denom (c : instance_cache) (a d : expr) (na : β) (nd : β) : tactic (instance_cache Γ expr Γ expr) :=
if na.denom = 1 then
prove_mul_nat c a d
else do
[_, _, a, b] β return a.get_app_args,
(c, b') β c.of_nat (nd / na.denom),
(c, pβ) β prove_ne_zero c b,
(c, _, pβ) β prove_mul_nat c b b',
(c, r, pβ) β prove_mul_nat c a b',
(c, p) β c.mk_app ``clear_denom_div [a, b, b', r, d, pβ, pβ, pβ],
return (c, r, p)
theorem clear_denom_add {Ξ±} [division_ring Ξ±] (a a' b b' c c' d : Ξ±)
(hβ : d β 0) (ha : a * d = a') (hb : b * d = b') (hc : c * d = c')
(h : a' + b' = c') : a + b = c :=
mul_right_cancel' hβ $ by rwa [add_mul, ha, hb, hc]
/-- Given `a`,`b`,`c` nonnegative rational numerals, returns `β’ a + b = c`. -/
meta def prove_add_nonneg_rat (ic : instance_cache) (a b c : expr) (na nb nc : β) : tactic (instance_cache Γ expr) :=
if na.denom = 1 β§ nb.denom = 1 then
prove_add_nat ic a b c
else do
let nd := na.denom.lcm nb.denom,
(ic, d) β ic.of_nat nd,
(ic, pβ) β prove_ne_zero ic d,
(ic, a', pa) β prove_clear_denom ic a d na nd,
(ic, b', pb) β prove_clear_denom ic b d nb nd,
(ic, c', pc) β prove_clear_denom ic c d nc nd,
(ic, p) β prove_add_nat ic a' b' c',
ic.mk_app ``clear_denom_add [a, a', b, b', c, c', d, pβ, pa, pb, pc, p]
theorem add_pos_neg_pos {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : c + b = a) : a + -b = c :=
h βΈ by simp
theorem add_pos_neg_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : c + a = b) : a + -b = -c :=
h βΈ by simp
theorem add_neg_pos_pos {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : a + c = b) : -a + b = c :=
h βΈ by simp
theorem add_neg_pos_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : b + c = a) : -a + b = -c :=
h βΈ by simp
theorem add_neg_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : b + a = c) : -a + -b = -c :=
h βΈ by simp
/-- Given `a`,`b`,`c` rational numerals, returns `β’ a + b = c`. -/
meta def prove_add_rat (ic : instance_cache) (ea eb ec : expr) (a b c : β) : tactic (instance_cache Γ expr) :=
match match_neg ea, match_neg eb, match_neg ec with
| some ea, some eb, some ec := do
(ic, p) β prove_add_nonneg_rat ic eb ea ec (-b) (-a) (-c),
ic.mk_app ``add_neg_neg [ea, eb, ec, p]
| some ea, none, some ec := do
(ic, p) β prove_add_nonneg_rat ic eb ec ea b (-c) (-a),
ic.mk_app ``add_neg_pos_neg [ea, eb, ec, p]
| some ea, none, none := do
(ic, p) β prove_add_nonneg_rat ic ea ec eb (-a) c b,
ic.mk_app ``add_neg_pos_pos [ea, eb, ec, p]
| none, some eb, some ec := do
(ic, p) β prove_add_nonneg_rat ic ec ea eb (-c) a (-b),
ic.mk_app ``add_pos_neg_neg [ea, eb, ec, p]
| none, some eb, none := do
(ic, p) β prove_add_nonneg_rat ic ec eb ea c (-b) a,
ic.mk_app ``add_pos_neg_pos [ea, eb, ec, p]
| _, _, _ := prove_add_nonneg_rat ic ea eb ec a b c
end
/-- Given `a`,`b` rational numerals, returns `(c, β’ a + b = c)`. -/
meta def prove_add_rat' (ic : instance_cache) (a b : expr) : tactic (instance_cache Γ expr Γ expr) := do
na β a.to_rat,
nb β b.to_rat,
let nc := na + nb,
(ic, c) β ic.of_rat nc,
(ic, p) β prove_add_rat ic a b c na nb nc,
return (ic, c, p)
theorem clear_denom_simple_nat {Ξ±} [division_ring Ξ±] (a : Ξ±) :
(1:Ξ±) β 0 β§ a * 1 = a := β¨one_ne_zero, mul_one _β©
theorem clear_denom_simple_div {Ξ±} [division_ring Ξ±] (a b : Ξ±) (h : b β 0) :
b β 0 β§ a / b * b = a := β¨h, div_mul_cancel _ hβ©
/-- Given `a` a nonnegative rational numeral, returns `(b, c, β’ a * b = c)`
where `b` and `c` are natural numerals. (`b` will be the denominator of `a`.) -/
meta def prove_clear_denom_simple (c : instance_cache) (a : expr) (na : β) : tactic (instance_cache Γ expr Γ expr Γ expr) :=
if na.denom = 1 then do
(c, d) β c.mk_app ``has_one.one [],
(c, p) β c.mk_app ``clear_denom_simple_nat [a],
return (c, d, a, p)
else do
[Ξ±, _, a, b] β return a.get_app_args,
(c, pβ) β prove_ne_zero c b,
(c, p) β c.mk_app ``clear_denom_simple_div [a, b, pβ],
return (c, b, a, p)
theorem clear_denom_mul {Ξ±} [field Ξ±] (a a' b b' c c' dβ dβ d : Ξ±)
(ha : dβ β 0 β§ a * dβ = a') (hb : dβ β 0 β§ b * dβ = b')
(hc : c * d = c') (hd : dβ * dβ = d)
(h : a' * b' = c') : a * b = c :=
mul_right_cancel' ha.1 $ mul_right_cancel' hb.1 $
by rw [mul_assoc c, hd, hc, β h, β ha.2, β hb.2, β mul_assoc, mul_right_comm a]
/-- Given `a`,`b` nonnegative rational numerals, returns `(c, β’ a * b = c)`. -/
meta def prove_mul_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr Γ expr) :=
if na.denom = 1 β§ nb.denom = 1 then
prove_mul_nat ic a b
else do
let nc := na * nb, (ic, c) β ic.of_rat nc,
(ic, dβ, a', pa) β prove_clear_denom_simple ic a na,
(ic, dβ, b', pb) β prove_clear_denom_simple ic b nb,
(ic, d, pd) β prove_mul_nat ic dβ dβ, nd β d.to_nat,
(ic, c', pc) β prove_clear_denom ic c d nc nd,
(ic, _, p) β prove_mul_nat ic a' b',
(ic, p) β ic.mk_app ``clear_denom_mul [a, a', b, b', c, c', dβ, dβ, d, pa, pb, pc, pd, p],
return (ic, c, p)
theorem mul_neg_pos {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : -a * b = -c := h βΈ by simp
theorem mul_pos_neg {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : a * -b = -c := h βΈ by simp
theorem mul_neg_neg {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : -a * -b = c := h βΈ by simp
/-- Given `a`,`b` rational numerals, returns `(c, β’ a * b = c)`. -/
meta def prove_mul_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr Γ expr) :=
match match_sign a, match_sign b with
| sum.inl a, sum.inl b := do
(ic, c, p) β prove_mul_nonneg_rat ic a b (-na) (-nb),
(ic, p) β ic.mk_app ``mul_neg_neg [a, b, c, p],
return (ic, c, p)
| sum.inr ff, _ := do
(ic, z) β ic.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``zero_mul [b],
return (ic, z, p)
| _, sum.inr ff := do
(ic, z) β ic.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``mul_zero [a],
return (ic, z, p)
| sum.inl a, sum.inr tt := do
(ic, c, p) β prove_mul_nonneg_rat ic a b (-na) nb,
(ic, p) β ic.mk_app ``mul_neg_pos [a, b, c, p],
(ic, c') β ic.mk_app ``has_neg.neg [c],
return (ic, c', p)
| sum.inr tt, sum.inl b := do
(ic, c, p) β prove_mul_nonneg_rat ic a b na (-nb),
(ic, p) β ic.mk_app ``mul_pos_neg [a, b, c, p],
(ic, c') β ic.mk_app ``has_neg.neg [c],
return (ic, c', p)
| sum.inr tt, sum.inr tt := prove_mul_nonneg_rat ic a b na nb
end
theorem inv_neg {Ξ±} [division_ring Ξ±] (a b : Ξ±) (h : aβ»ΒΉ = b) : (-a)β»ΒΉ = -b :=
h βΈ by simp only [inv_eq_one_div, one_div_neg_eq_neg_one_div]
theorem inv_one {Ξ±} [division_ring Ξ±] : (1 : Ξ±)β»ΒΉ = 1 := inv_one
theorem inv_one_div {Ξ±} [division_ring Ξ±] (a : Ξ±) : (1 / a)β»ΒΉ = a :=
by rw [one_div_eq_inv, inv_inv']
theorem inv_div_one {Ξ±} [division_ring Ξ±] (a : Ξ±) : aβ»ΒΉ = 1 / a :=
inv_eq_one_div _
theorem inv_div {Ξ±} [division_ring Ξ±] (a b : Ξ±) : (a / b)β»ΒΉ = b / a :=
by simp only [inv_eq_one_div, one_div_div]
/-- Given `a` a rational numeral, returns `(b, β’ aβ»ΒΉ = b)`. -/
meta def prove_inv : instance_cache β expr β β β tactic (instance_cache Γ expr Γ expr)
| ic e n :=
match match_sign e with
| sum.inl e := do
(ic, e', p) β prove_inv ic e (-n),
(ic, r) β ic.mk_app ``has_neg.neg [e'],
(ic, p) β ic.mk_app ``inv_neg [e, e', p],
return (ic, r, p)
| sum.inr ff := do
(ic, p) β ic.mk_app ``inv_zero [],
return (ic, e, p)
| sum.inr tt :=
if n.num = 1 then
if n.denom = 1 then do
(ic, p) β ic.mk_app ``inv_one [],
return (ic, e, p)
else do
let e := e.app_arg,
(ic, p) β ic.mk_app ``inv_one_div [e],
return (ic, e, p)
else if n.denom = 1 then do
(ic, p) β ic.mk_app ``inv_div_one [e],
e β infer_type p,
return (ic, e.app_arg, p)
else do
[_, _, a, b] β return e.get_app_args,
(ic, e') β ic.mk_app ``has_div.div [b, a],
(ic, p) β ic.mk_app ``inv_div [a, b],
return (ic, e', p)
end
theorem div_eq {Ξ±} [division_ring Ξ±] (a b b' c : Ξ±)
(hb : bβ»ΒΉ = b') (h : a * b' = c) : a / b = c := by rwa β hb at h
/-- Given `a`,`b` rational numerals, returns `(c, β’ a / b = c)`. -/
meta def prove_div (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr Γ expr) :=
do (ic, b', pb) β prove_inv ic b nb,
(ic, c, p) β prove_mul_rat ic a b' na nbβ»ΒΉ,
(ic, p) β ic.mk_app ``div_eq [a, b, b', c, pb, p],
return (ic, c, p)
/-- Given `a` a rational numeral, returns `(b, β’ -a = b)`. -/
meta def prove_neg (ic : instance_cache) (a : expr) : tactic (instance_cache Γ expr Γ expr) :=
match match_sign a with
| sum.inl a := do
(ic, p) β ic.mk_app ``neg_neg [a],
return (ic, a, p)
| sum.inr ff := do
(ic, p) β ic.mk_app ``neg_zero [],
return (ic, a, p)
| sum.inr tt := do
(ic, a') β ic.mk_app ``has_neg.neg [a],
p β mk_eq_refl a',
return (ic, a', p)
end
theorem sub_pos {Ξ±} [add_group Ξ±] (a b b' c : Ξ±) (hb : -b = b') (h : a + b' = c) : a - b = c :=
by rwa β hb at h
theorem sub_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : a + b = c) : a - -b = c :=
by rwa sub_neg_eq_add
/-- Given `a`,`b` rational numerals, returns `(c, β’ a - b = c)`. -/
meta def prove_sub (ic : instance_cache) (a b : expr) : tactic (instance_cache Γ expr Γ expr) :=
match match_sign b with
| sum.inl b := do
(ic, c, p) β prove_add_rat' ic a b,
(ic, p) β ic.mk_app ``sub_neg [a, b, c, p],
return (ic, c, p)
| sum.inr ff := do
(ic, p) β ic.mk_app ``sub_zero [a],
return (ic, a, p)
| sum.inr tt := do
(ic, b', pb) β prove_neg ic b,
(ic, c, p) β prove_add_rat' ic a b',
(ic, p) β ic.mk_app ``sub_pos [a, b, b', c, pb, p],
return (ic, c, p)
end
theorem sub_nat_pos (a b c : β) (h : b + c = a) : a - b = c :=
h βΈ nat.add_sub_cancel_left _ _
theorem sub_nat_neg (a b c : β) (h : a + c = b) : a - b = 0 :=
nat.sub_eq_zero_of_le $ h βΈ nat.le_add_right _ _
/-- Given `a : nat`,`b : nat` natural numerals, returns `(c, β’ a - b = c)`. -/
meta def prove_sub_nat (ic : instance_cache) (a b : expr) : tactic (expr Γ expr) :=
do na β a.to_nat, nb β b.to_nat,
if nb β€ na then do
(ic, c) β ic.of_nat (na - nb),
(ic, p) β prove_add_nat ic b c a,
return (c, `(sub_nat_pos).mk_app [a, b, c, p])
else do
(ic, c) β ic.of_nat (nb - na),
(ic, p) β prove_add_nat ic a c b,
return (`(0 : β), `(sub_nat_neg).mk_app [a, b, c, p])
/-- This is needed because when `a` and `b` are numerals lean is more likely to unfold them
than unfold the instances in order to prove that `add_group_has_sub = int.has_sub`. -/
theorem int_sub_hack (a b c : β€) (h : @has_sub.sub β€ add_group_has_sub a b = c) : a - b = c := h
/-- Given `a : β€`, `b : β€` integral numerals, returns `(c, β’ a - b = c)`. -/
meta def prove_sub_int (ic : instance_cache) (a b : expr) : tactic (expr Γ expr) :=
do (_, c, p) β prove_sub ic a b,
return (c, `(int_sub_hack).mk_app [a, b, c, p])
/-- Evaluates the basic field operations `+`,`neg`,`-`,`*`,`inv`,`/` on numerals.
Also handles nat subtraction. Does not do recursive simplification; that is,
`1 + 1 + 1` will not simplify but `2 + 1` will. This is handled by the top level
`simp` call in `norm_num.derive`. -/
meta def eval_field : expr β tactic (expr Γ expr)
| `(%%eβ + %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
let nβ := nβ + nβ,
(c, eβ) β c.of_rat nβ,
(_, p) β prove_add_rat c eβ eβ eβ nβ nβ nβ,
return (eβ, p)
| `(%%eβ * %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
prod.snd <$> prove_mul_rat c eβ eβ nβ nβ
| `(- %%e) := do
c β infer_type e >>= mk_instance_cache,
prod.snd <$> prove_neg c e
| `(@has_sub.sub %%Ξ± %%inst %%a %%b) := do
c β mk_instance_cache Ξ±,
if Ξ± = `(nat) then prove_sub_nat c a b
else if inst = `(int.has_sub) then prove_sub_int c a b
else prod.snd <$> prove_sub c a b
| `(has_inv.inv %%e) := do
n β e.to_rat,
c β infer_type e >>= mk_instance_cache,
prod.snd <$> prove_inv c e n
| `(%%eβ / %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
prod.snd <$> prove_div c eβ eβ nβ nβ
| _ := failed
lemma pow_bit0 [monoid Ξ±] (a c' c : Ξ±) (b : β)
(h : a ^ b = c') (hβ : c' * c' = c) : a ^ bit0 b = c :=
hβ βΈ by simp [pow_bit0, h]
lemma pow_bit1 [monoid Ξ±] (a cβ cβ c : Ξ±) (b : β)
(h : a ^ b = cβ) (hβ : cβ * cβ = cβ) (hβ : cβ * a = c) : a ^ bit1 b = c :=
by rw [β hβ, β hβ]; simp [pow_bit1, h]
section
open match_numeral_result
/-- Given `a` a rational numeral and `b : nat`, returns `(c, β’ a ^ b = c)`. -/
meta def prove_pow (a : expr) (na : β) : instance_cache β expr β tactic (instance_cache Γ expr Γ expr)
| ic b :=
match match_numeral b with
| zero := do
(ic, p) β ic.mk_app ``pow_zero [a],
(ic, o) β ic.mk_app ``has_one.one [],
return (ic, o, p)
| one := do
(ic, p) β ic.mk_app ``pow_one [a],
return (ic, a, p)
| bit0 b := do
(ic, c', p) β prove_pow ic b,
nc' β expr.to_rat c',
(ic, c, pβ) β prove_mul_rat ic c' c' nc' nc',
(ic, p) β ic.mk_app ``pow_bit0 [a, c', c, b, p, pβ],
return (ic, c, p)
| bit1 b := do
(ic, cβ, p) β prove_pow ic b,
ncβ β expr.to_rat cβ,
(ic, cβ, pβ) β prove_mul_rat ic cβ cβ ncβ ncβ,
(ic, c, pβ) β prove_mul_rat ic cβ a (ncβ * ncβ) na,
(ic, p) β ic.mk_app ``pow_bit1 [a, cβ, cβ, c, b, p, pβ, pβ],
return (ic, c, p)
| _ := failed
end
end
lemma from_nat_pow (a b c : β) (h : @has_pow.pow _ _ monoid.has_pow a b = c) : a ^ b = c :=
(nat.pow_eq_pow _ _).symm.trans h
/-- Evaluates expressions of the form `a ^ b`, `monoid.pow a b` or `nat.pow a b`. -/
meta def eval_pow : expr β tactic (expr Γ expr)
| `(@has_pow.pow %%Ξ± _ %%m %%eβ %%eβ) := do
nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
match m with
| `(@monoid.has_pow %%_ %%_) := prod.snd <$> prove_pow eβ nβ c eβ
| `(nat.has_pow) := do
(_, c, p) β prove_pow eβ nβ c eβ,
return (c, `(from_nat_pow).mk_app [eβ, eβ, c, p])
| _ := failed
end
| `(monoid.pow %%eβ %%eβ) := do
nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
prod.snd <$> prove_pow eβ nβ c eβ
| `(nat.pow %%eβ %%eβ) := do
nβ β eβ.to_rat,
c β mk_instance_cache `(β),
(_, c, p) β prove_pow eβ nβ c eβ,
return (c, `(from_nat_pow).mk_app [eβ, eβ, c, p])
| _ := failed
theorem nonneg_pos {Ξ±} [ordered_cancel_add_comm_monoid Ξ±] (a : Ξ±) : 0 < a β 0 β€ a := le_of_lt
theorem lt_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 1 β€ a) : 1 < bit0 a :=
lt_of_lt_of_le one_lt_two (bit0_le_bit0.2 h)
theorem lt_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 0 < a) : 1 < bit1 a :=
one_lt_bit1.2 h
theorem lt_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a < b β bit0 a < bit0 b := bit0_lt_bit0.2
theorem lt_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a β€ b) : bit0 a < bit1 b :=
lt_of_le_of_lt (bit0_le_bit0.2 h) (lt_add_one _)
theorem lt_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 β€ b) : bit1 a < bit0 b :=
lt_of_lt_of_le (by simp [bit0, bit1, zero_lt_one, add_assoc]) (bit0_le_bit0.2 h)
theorem lt_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a < b β bit1 a < bit1 b := bit1_lt_bit1.2
theorem le_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 1 β€ a) : 1 β€ bit0 a :=
le_of_lt (lt_one_bit0 _ h)
-- deliberately strong hypothesis because bit1 0 is not a numeral
theorem le_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 0 < a) : 1 β€ bit1 a :=
le_of_lt (lt_one_bit1 _ h)
theorem le_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a β€ b β bit0 a β€ bit0 b := bit0_le_bit0.2
theorem le_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a β€ b) : bit0 a β€ bit1 b :=
le_of_lt (lt_bit0_bit1 _ _ h)
theorem le_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 β€ b) : bit1 a β€ bit0 b :=
le_of_lt (lt_bit1_bit0 _ _ h)
theorem le_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a β€ b β bit1 a β€ bit1 b := bit1_le_bit1.2
theorem sle_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) : 1 β€ a β 1 + 1 β€ bit0 a := bit0_le_bit0.2
theorem sle_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) : 1 β€ a β 1 + 1 β€ bit1 a := le_bit0_bit1 _ _
theorem sle_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a + 1 β€ b β bit0 a + 1 β€ bit0 b := le_bit1_bit0 _ _
theorem sle_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a β€ b) : bit0 a + 1 β€ bit1 b := bit1_le_bit1.2 h
theorem sle_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 β€ b) : bit1 a + 1 β€ bit0 b :=
(bit1_succ a _ rfl).symm βΈ bit0_le_bit0.2 h
theorem sle_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 β€ b) : bit1 a + 1 β€ bit1 b :=
(bit1_succ a _ rfl).symm βΈ le_bit0_bit1 _ _ h
/-- Given `a` a rational numeral, returns `β’ 0 β€ a`. -/
meta def prove_nonneg (ic : instance_cache) : expr β tactic (instance_cache Γ expr)
| e@`(has_zero.zero) := ic.mk_app ``le_refl [e]
| e :=
if ic.Ξ± = `(β) then
return (ic, `(nat.zero_le).mk_app [e])
else do
(ic, p) β prove_pos ic e,
ic.mk_app ``nonneg_pos [e, p]
section
open match_numeral_result
/-- Given `a` a rational numeral, returns `β’ 1 β€ a`. -/
meta def prove_one_le_nat (ic : instance_cache) : expr β tactic (instance_cache Γ expr)
| a :=
match match_numeral a with
| one := ic.mk_app ``le_refl [a]
| bit0 a := do (ic, p) β prove_one_le_nat a, ic.mk_app ``le_one_bit0 [a, p]
| bit1 a := do (ic, p) β prove_pos_nat ic a, ic.mk_app ``le_one_bit1 [a, p]
| _ := failed
end
meta mutual def prove_le_nat, prove_sle_nat (ic : instance_cache)
with prove_le_nat : expr β expr β tactic (instance_cache Γ expr)
| a b :=
if a = b then ic.mk_app ``le_refl [a] else
match match_numeral a, match_numeral b with
| zero, _ := prove_nonneg ic b
| one, bit0 b := do (ic, p) β prove_one_le_nat ic b, ic.mk_app ``le_one_bit0 [b, p]
| one, bit1 b := do (ic, p) β prove_pos_nat ic b, ic.mk_app ``le_one_bit1 [b, p]
| bit0 a, bit0 b := do (ic, p) β prove_le_nat a b, ic.mk_app ``le_bit0_bit0 [a, b, p]
| bit0 a, bit1 b := do (ic, p) β prove_le_nat a b, ic.mk_app ``le_bit0_bit1 [a, b, p]
| bit1 a, bit0 b := do (ic, p) β prove_sle_nat a b, ic.mk_app ``le_bit1_bit0 [a, b, p]
| bit1 a, bit1 b := do (ic, p) β prove_le_nat a b, ic.mk_app ``le_bit1_bit1 [a, b, p]
| _, _ := failed
end
with prove_sle_nat : expr β expr β tactic (instance_cache Γ expr)
| a b :=
match match_numeral a, match_numeral b with
| zero, _ := prove_nonneg ic b
| one, bit0 b := do (ic, p) β prove_one_le_nat ic b, ic.mk_app ``sle_one_bit0 [b, p]
| one, bit1 b := do (ic, p) β prove_one_le_nat ic b, ic.mk_app ``sle_one_bit1 [b, p]
| bit0 a, bit0 b := do (ic, p) β prove_sle_nat a b, ic.mk_app ``sle_bit0_bit0 [a, b, p]
| bit0 a, bit1 b := do (ic, p) β prove_le_nat a b, ic.mk_app ``sle_bit0_bit1 [a, b, p]
| bit1 a, bit0 b := do (ic, p) β prove_sle_nat a b, ic.mk_app ``sle_bit1_bit0 [a, b, p]
| bit1 a, bit1 b := do (ic, p) β prove_sle_nat a b, ic.mk_app ``sle_bit1_bit1 [a, b, p]
| _, _ := failed
end
/-- Given `a`,`b` natural numerals, proves `β’ a β€ b`. -/
add_decl_doc prove_le_nat
/-- Given `a`,`b` natural numerals, proves `β’ a + 1 β€ b`. -/
add_decl_doc prove_sle_nat
/-- Given `a`,`b` natural numerals, proves `β’ a < b`. -/
meta def prove_lt_nat (ic : instance_cache) : expr β expr β tactic (instance_cache Γ expr)
| a b :=
match match_numeral a, match_numeral b with
| zero, _ := prove_pos ic b
| one, bit0 b := do (ic, p) β prove_one_le_nat ic b, ic.mk_app ``lt_one_bit0 [b, p]
| one, bit1 b := do (ic, p) β prove_pos_nat ic b, ic.mk_app ``lt_one_bit1 [b, p]
| bit0 a, bit0 b := do (ic, p) β prove_lt_nat a b, ic.mk_app ``lt_bit0_bit0 [a, b, p]
| bit0 a, bit1 b := do (ic, p) β prove_le_nat ic a b, ic.mk_app ``lt_bit0_bit1 [a, b, p]
| bit1 a, bit0 b := do (ic, p) β prove_sle_nat ic a b, ic.mk_app ``lt_bit1_bit0 [a, b, p]
| bit1 a, bit1 b := do (ic, p) β prove_lt_nat a b, ic.mk_app ``lt_bit1_bit1 [a, b, p]
| _, _ := failed
end
end
theorem clear_denom_lt {Ξ±} [linear_ordered_semiring Ξ±] (a a' b b' d : Ξ±)
(hβ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' < b') : a < b :=
lt_of_mul_lt_mul_right (by rwa [ha, hb]) (le_of_lt hβ)
/-- Given `a`,`b` nonnegative rational numerals, proves `β’ a < b`. -/
meta def prove_lt_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr) :=
if na.denom = 1 β§ nb.denom = 1 then
prove_lt_nat ic a b
else do
let nd := na.denom.lcm nb.denom,
(ic, d) β ic.of_nat nd,
(ic, pβ) β prove_pos ic d,
(ic, a', pa) β prove_clear_denom ic a d na nd,
(ic, b', pb) β prove_clear_denom ic b d nb nd,
(ic, p) β prove_lt_nat ic a' b',
ic.mk_app ``clear_denom_lt [a, a', b, b', d, pβ, pa, pb, p]
lemma lt_neg_pos {Ξ±} [ordered_add_comm_group Ξ±] (a b : Ξ±) (ha : 0 < a) (hb : 0 < b) : -a < b :=
lt_trans (neg_neg_of_pos ha) hb
/-- Given `a`,`b` rational numerals, proves `β’ a < b`. -/
meta def prove_lt_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr) :=
match match_sign a, match_sign b with
| sum.inl a, sum.inl b := do
(ic, p) β prove_lt_nonneg_rat ic a b (-na) (-nb),
ic.mk_app ``neg_lt_neg [a, b, p]
| sum.inl a, sum.inr ff := do
(ic, p) β prove_pos ic a,
ic.mk_app ``neg_neg_of_pos [a, p]
| sum.inl a, sum.inr tt := do
(ic, pa) β prove_pos ic a,
(ic, pb) β prove_pos ic b,
ic.mk_app ``lt_neg_pos [a, b, pa, pb]
| sum.inr ff, _ := prove_pos ic b
| sum.inr tt, _ := prove_lt_nonneg_rat ic a b na nb
end
theorem clear_denom_le {Ξ±} [linear_ordered_semiring Ξ±] (a a' b b' d : Ξ±)
(hβ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' β€ b') : a β€ b :=
le_of_mul_le_mul_right (by rwa [ha, hb]) hβ
/-- Given `a`,`b` nonnegative rational numerals, proves `β’ a β€ b`. -/
meta def prove_le_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr) :=
if na.denom = 1 β§ nb.denom = 1 then
prove_le_nat ic a b
else do
let nd := na.denom.lcm nb.denom,
(ic, d) β ic.of_nat nd,
(ic, pβ) β prove_pos ic d,
(ic, a', pa) β prove_clear_denom ic a d na nd,
(ic, b', pb) β prove_clear_denom ic b d nb nd,
(ic, p) β prove_le_nat ic a' b',
ic.mk_app ``clear_denom_le [a, a', b, b', d, pβ, pa, pb, p]
lemma le_neg_pos {Ξ±} [ordered_add_comm_group Ξ±] (a b : Ξ±) (ha : 0 β€ a) (hb : 0 β€ b) : -a β€ b :=
le_trans (neg_nonpos_of_nonneg ha) hb
/-- Given `a`,`b` rational numerals, proves `β’ a β€ b`. -/
meta def prove_le_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr) :=
match match_sign a, match_sign b with
| sum.inl a, sum.inl b := do
(ic, p) β prove_le_nonneg_rat ic a b (-na) (-nb),
ic.mk_app ``neg_le_neg [a, b, p]
| sum.inl a, sum.inr ff := do
(ic, p) β prove_nonneg ic a,
ic.mk_app ``neg_nonpos_of_nonneg [a, p]
| sum.inl a, sum.inr tt := do
(ic, pa) β prove_nonneg ic a,
(ic, pb) β prove_nonneg ic b,
ic.mk_app ``le_neg_pos [a, b, pa, pb]
| sum.inr ff, _ := prove_nonneg ic b
| sum.inr tt, _ := prove_le_nonneg_rat ic a b na nb
end
/-- Given `a`,`b` rational numerals, proves `β’ a β b`. This version
tries to prove `β’ a < b` or `β’ b < a`, and so is not appropriate for types without an order relation. -/
meta def prove_ne_rat (ic : instance_cache) (a b : expr) (na nb : β) : tactic (instance_cache Γ expr) :=
if na < nb then do
(ic, p) β prove_lt_rat ic a b na nb,
ic.mk_app ``ne_of_lt [a, b, p]
else do
(ic, p) β prove_lt_rat ic b a nb na,
ic.mk_app ``ne_of_gt [a, b, p]
theorem nat_cast_zero {Ξ±} [semiring Ξ±] : β(0 : β) = (0 : Ξ±) := nat.cast_zero
theorem nat_cast_one {Ξ±} [semiring Ξ±] : β(1 : β) = (1 : Ξ±) := nat.cast_one
theorem nat_cast_bit0 {Ξ±} [semiring Ξ±] (a : β) (a' : Ξ±) (h : βa = a') : β(bit0 a) = bit0 a' :=
h βΈ nat.cast_bit0 _
theorem nat_cast_bit1 {Ξ±} [semiring Ξ±] (a : β) (a' : Ξ±) (h : βa = a') : β(bit1 a) = bit1 a' :=
h βΈ nat.cast_bit1 _
theorem int_cast_zero {Ξ±} [ring Ξ±] : β(0 : β€) = (0 : Ξ±) := int.cast_zero
theorem int_cast_one {Ξ±} [ring Ξ±] : β(1 : β€) = (1 : Ξ±) := int.cast_one
theorem int_cast_bit0 {Ξ±} [ring Ξ±] (a : β€) (a' : Ξ±) (h : βa = a') : β(bit0 a) = bit0 a' :=
h βΈ int.cast_bit0 _
theorem int_cast_bit1 {Ξ±} [ring Ξ±] (a : β€) (a' : Ξ±) (h : βa = a') : β(bit1 a) = bit1 a' :=
h βΈ int.cast_bit1 _
theorem rat_cast_bit0 {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a : β) (a' : Ξ±) (h : βa = a') : β(bit0 a) = bit0 a' :=
h βΈ rat.cast_bit0 _
theorem rat_cast_bit1 {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a : β) (a' : Ξ±) (h : βa = a') : β(bit1 a) = bit1 a' :=
h βΈ rat.cast_bit1 _
/-- Given `a' : Ξ±` a natural numeral, returns `(a : β, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_nat_uncast (ic nc : instance_cache) : β (a' : expr),
tactic (instance_cache Γ instance_cache Γ expr Γ expr)
| a' :=
match match_numeral a' with
| match_numeral_result.zero := do
(nc, e) β nc.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``nat_cast_zero [],
return (ic, nc, e, p)
| match_numeral_result.one := do
(nc, e) β nc.mk_app ``has_one.one [],
(ic, p) β ic.mk_app ``nat_cast_one [],
return (ic, nc, e, p)
| match_numeral_result.bit0 a' := do
(ic, nc, a, p) β prove_nat_uncast a',
(nc, a0) β nc.mk_bit0 a,
(ic, p) β ic.mk_app ``nat_cast_bit0 [a, a', p],
return (ic, nc, a0, p)
| match_numeral_result.bit1 a' := do
(ic, nc, a, p) β prove_nat_uncast a',
(nc, a1) β nc.mk_bit1 a,
(ic, p) β ic.mk_app ``nat_cast_bit1 [a, a', p],
return (ic, nc, a1, p)
| _ := failed
end
/-- Given `a' : Ξ±` a natural numeral, returns `(a : β€, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_int_uncast_nat (ic zc : instance_cache) : β (a' : expr),
tactic (instance_cache Γ instance_cache Γ expr Γ expr)
| a' :=
match match_numeral a' with
| match_numeral_result.zero := do
(zc, e) β zc.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``int_cast_zero [],
return (ic, zc, e, p)
| match_numeral_result.one := do
(zc, e) β zc.mk_app ``has_one.one [],
(ic, p) β ic.mk_app ``int_cast_one [],
return (ic, zc, e, p)
| match_numeral_result.bit0 a' := do
(ic, zc, a, p) β prove_int_uncast_nat a',
(zc, a0) β zc.mk_bit0 a,
(ic, p) β ic.mk_app ``int_cast_bit0 [a, a', p],
return (ic, zc, a0, p)
| match_numeral_result.bit1 a' := do
(ic, zc, a, p) β prove_int_uncast_nat a',
(zc, a1) β zc.mk_bit1 a,
(ic, p) β ic.mk_app ``int_cast_bit1 [a, a', p],
return (ic, zc, a1, p)
| _ := failed
end
/-- Given `a' : Ξ±` a natural numeral, returns `(a : β, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_rat_uncast_nat (ic qc : instance_cache) (cz_inst : expr) : β (a' : expr),
tactic (instance_cache Γ instance_cache Γ expr Γ expr)
| a' :=
match match_numeral a' with
| match_numeral_result.zero := do
(qc, e) β qc.mk_app ``has_zero.zero [],
(ic, p) β ic.mk_app ``rat.cast_zero [cz_inst],
return (ic, qc, e, p)
| match_numeral_result.one := do
(qc, e) β qc.mk_app ``has_one.one [],
(ic, p) β ic.mk_app ``rat.cast_one [],
return (ic, qc, e, p)
| match_numeral_result.bit0 a' := do
(ic, qc, a, p) β prove_rat_uncast_nat a',
(qc, a0) β qc.mk_bit0 a,
(ic, p) β ic.mk_app ``rat_cast_bit0 [cz_inst, a, a', p],
return (ic, qc, a0, p)
| match_numeral_result.bit1 a' := do
(ic, qc, a, p) β prove_rat_uncast_nat a',
(qc, a1) β qc.mk_bit1 a,
(ic, p) β ic.mk_app ``rat_cast_bit1 [cz_inst, a, a', p],
return (ic, qc, a1, p)
| _ := failed
end
theorem rat_cast_div {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a b : β) (a' b' : Ξ±)
(ha : βa = a') (hb : βb = b') : β(a / b) = a' / b' :=
ha βΈ hb βΈ rat.cast_div _ _
/-- Given `a' : Ξ±` a nonnegative rational numeral, returns `(a : β, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_rat_uncast_nonneg (ic qc : instance_cache) (cz_inst a' : expr) (na' : β) :
tactic (instance_cache Γ instance_cache Γ expr Γ expr) :=
if na'.denom = 1 then
prove_rat_uncast_nat ic qc cz_inst a'
else do
[_, _, a', b'] β return a'.get_app_args,
(ic, qc, a, pa) β prove_rat_uncast_nat ic qc cz_inst a',
(ic, qc, b, pb) β prove_rat_uncast_nat ic qc cz_inst b',
(qc, e) β qc.mk_app ``has_div.div [a, b],
(ic, p) β ic.mk_app ``rat_cast_div [cz_inst, a, b, a', b', pa, pb],
return (ic, qc, e, p)
theorem int_cast_neg {Ξ±} [ring Ξ±] (a : β€) (a' : Ξ±) (h : βa = a') : β-a = -a' :=
h βΈ int.cast_neg _
theorem rat_cast_neg {Ξ±} [division_ring Ξ±] (a : β) (a' : Ξ±) (h : βa = a') : β-a = -a' :=
h βΈ rat.cast_neg _
/-- Given `a' : Ξ±` an integer numeral, returns `(a : β€, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_int_uncast (ic zc : instance_cache) (a' : expr) :
tactic (instance_cache Γ instance_cache Γ expr Γ expr) :=
match match_neg a' with
| some a' := do
(ic, zc, a, p) β prove_int_uncast_nat ic zc a',
(zc, e) β zc.mk_app ``has_neg.neg [a],
(ic, p) β ic.mk_app ``int_cast_neg [a, a', p],
return (ic, zc, e, p)
| none := prove_int_uncast_nat ic zc a'
end
/-- Given `a' : Ξ±` a rational numeral, returns `(a : β, β’ βa = a')`.
(Note that the returned value is on the left of the equality.) -/
meta def prove_rat_uncast (ic qc : instance_cache) (cz_inst a' : expr) (na' : β) :
tactic (instance_cache Γ instance_cache Γ expr Γ expr) :=
match match_neg a' with
| some a' := do
(ic, qc, a, p) β prove_rat_uncast_nonneg ic qc cz_inst a' (-na'),
(qc, e) β qc.mk_app ``has_neg.neg [a],
(ic, p) β ic.mk_app ``rat_cast_neg [a, a', p],
return (ic, qc, e, p)
| none := prove_rat_uncast_nonneg ic qc cz_inst a' na'
end
theorem nat_cast_ne {Ξ±} [semiring Ξ±] [char_zero Ξ±] (a b : β) (a' b' : Ξ±)
(ha : βa = a') (hb : βb = b') (h : a β b) : a' β b' :=
ha βΈ hb βΈ mt nat.cast_inj.1 h
theorem int_cast_ne {Ξ±} [ring Ξ±] [char_zero Ξ±] (a b : β€) (a' b' : Ξ±)
(ha : βa = a') (hb : βb = b') (h : a β b) : a' β b' :=
ha βΈ hb βΈ mt int.cast_inj.1 h
theorem rat_cast_ne {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a b : β) (a' b' : Ξ±)
(ha : βa = a') (hb : βb = b') (h : a β b) : a' β b' :=
ha βΈ hb βΈ mt rat.cast_inj.1 h
/-- Given `a`,`b` rational numerals, proves `β’ a β b`. Currently it tries two methods:
* Prove `β’ a < b` or `β’ b < a`, if the base type has an order
* Embed `β(a':β) = a` and `β(b':β) = b`, and then prove `a' β b'`.
This requires that the base type be `char_zero`, and also that it be a `division_ring`
so that the coercion from `β` is well defined.
We may also add coercions to `β€` and `β` as well in order to support `char_zero`
rings and semirings. -/
meta def prove_ne : instance_cache β expr β expr β β β β β tactic (instance_cache Γ expr)
| ic a b na nb := prove_ne_rat ic a b na nb <|> do
cz_inst β mk_mapp ``char_zero [ic.Ξ±, none, none] >>= mk_instance,
if na.denom = 1 β§ nb.denom = 1 then
if na β₯ 0 β§ nb β₯ 0 then do
guard (ic.Ξ± β `(β)),
nc β mk_instance_cache `(β),
(ic, nc, a', pa) β prove_nat_uncast ic nc a,
(ic, nc, b', pb) β prove_nat_uncast ic nc b,
(nc, p) β prove_ne_rat nc a' b' na nb,
ic.mk_app ``nat_cast_ne [cz_inst, a', b', a, b, pa, pb, p]
else do
guard (ic.Ξ± β `(β€)),
zc β mk_instance_cache `(β€),
(ic, zc, a', pa) β prove_int_uncast ic zc a,
(ic, zc, b', pb) β prove_int_uncast ic zc b,
(zc, p) β prove_ne_rat zc a' b' na nb,
ic.mk_app ``int_cast_ne [cz_inst, a', b', a, b, pa, pb, p]
else do
guard (ic.Ξ± β `(β)),
qc β mk_instance_cache `(β),
(ic, qc, a', pa) β prove_rat_uncast ic qc cz_inst a na,
(ic, qc, b', pb) β prove_rat_uncast ic qc cz_inst b nb,
(qc, p) β prove_ne_rat qc a' b' na nb,
ic.mk_app ``rat_cast_ne [cz_inst, a', b', a, b, pa, pb, p]
/-- Given `β£- p`, returns `(true, β’ p = true)`. -/
meta def true_intro (p : expr) : tactic (expr Γ expr) :=
prod.mk `(true) <$> mk_app ``eq_true_intro [p]
/-- Given `β£- Β¬ p`, returns `(false, β’ p = false)`. -/
meta def false_intro (p : expr) : tactic (expr Γ expr) :=
prod.mk `(false) <$> mk_app ``eq_false_intro [p]
theorem not_refl_false_intro {Ξ±} (a : Ξ±) : (a β a) = false :=
eq_false_intro $ not_not_intro rfl
/-- Evaluates the inequality operations `=`,`<`,`>`,`β€`,`β₯`,`β ` on numerals. -/
meta def eval_ineq : expr β tactic (expr Γ expr)
| `(%%eβ < %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
if nβ < nβ then
do (_, p) β prove_lt_rat c eβ eβ nβ nβ, true_intro p
else if nβ = nβ then do
(_, p) β c.mk_app ``lt_irrefl [eβ],
false_intro p
else do
(c, p') β prove_lt_rat c eβ eβ nβ nβ,
(_, p) β c.mk_app ``not_lt_of_gt [eβ, eβ, p'],
false_intro p
| `(%%eβ β€ %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
if nβ β€ nβ then do
(_, p) β
if nβ = nβ then c.mk_app ``le_refl [eβ]
else prove_le_rat c eβ eβ nβ nβ,
true_intro p
else do
(c, p) β prove_lt_rat c eβ eβ nβ nβ,
(_, p) β c.mk_app ``not_le_of_gt [eβ, eβ, p],
false_intro p
| `(%%eβ = %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
if nβ = nβ then mk_eq_refl eβ >>= true_intro
else do (_, p) β prove_ne c eβ eβ nβ nβ, false_intro p
| `(%%eβ > %%eβ) := mk_app ``has_lt.lt [eβ, eβ] >>= eval_ineq
| `(%%eβ β₯ %%eβ) := mk_app ``has_le.le [eβ, eβ] >>= eval_ineq
| `(%%eβ β %%eβ) := do
nβ β eβ.to_rat, nβ β eβ.to_rat,
c β infer_type eβ >>= mk_instance_cache,
if nβ = nβ then
prod.mk `(false) <$> mk_app ``not_refl_false_intro [eβ]
else do (_, p) β prove_ne c eβ eβ nβ nβ, true_intro p
| _ := failed
theorem nat_succ_eq (a b c : β) (hβ : a = b) (hβ : b + 1 = c) : nat.succ a = c := by rwa hβ
/-- Evaluates the expression `nat.succ ... (nat.succ n)` where `n` is a natural numeral.
(We could also just handle `nat.succ n` here and rely on `simp` to work bottom up, but we figure
that towers of successors coming from e.g. `induction` are a common case.) -/
meta def prove_nat_succ (ic : instance_cache) : expr β tactic (instance_cache Γ β Γ expr Γ expr)
| `(nat.succ %%a) := do
(ic, n, b, pβ) β prove_nat_succ a,
let n' := n + 1,
(ic, c) β ic.of_nat n',
(ic, pβ) β prove_add_nat ic b `(1) c,
return (ic, n', c, `(nat_succ_eq).mk_app [a, b, c, pβ, pβ])
| e := do
n β e.to_nat,
p β mk_eq_refl e,
return (ic, n, e, p)
lemma nat_div (a b q r m : β) (hm : q * b = m) (h : r + m = a) (hβ : r < b) : a / b = q :=
by rw [β h, β hm, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) hβ),
nat.div_eq_of_lt hβ, zero_add]
lemma int_div (a b q r m : β€) (hm : q * b = m) (h : r + m = a) (hβ : 0 β€ r) (hβ : r < b) : a / b = q :=
by rw [β h, β hm, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt hβ hβ)),
int.div_eq_zero_of_lt hβ hβ, zero_add]
lemma nat_mod (a b q r m : β) (hm : q * b = m) (h : r + m = a) (hβ : r < b) : a % b = r :=
by rw [β h, β hm, nat.add_mul_mod_self_right, nat.mod_eq_of_lt hβ]
lemma int_mod (a b q r m : β€) (hm : q * b = m) (h : r + m = a) (hβ : 0 β€ r) (hβ : r < b) : a % b = r :=
by rw [β h, β hm, int.add_mul_mod_self, int.mod_eq_of_lt hβ hβ]
lemma int_div_neg (a b c' c : β€) (h : a / b = c') (hβ : -c' = c) : a / -b = c :=
hβ βΈ h βΈ int.div_neg _ _
lemma int_mod_neg (a b c : β€) (h : a % b = c) : a % -b = c :=
(int.mod_neg _ _).trans h
/-- Given `a`,`b` numerals in `nat` or `int`,
* `prove_div_mod ic a b ff` returns `(c, β’ a / b = c)`
* `prove_div_mod ic a b tt` returns `(c, β’ a % b = c)`
-/
meta def prove_div_mod (ic : instance_cache) : expr β expr β bool β tactic (instance_cache Γ expr Γ expr)
| a b mod :=
match match_neg b with
| some b := do
(ic, c', p) β prove_div_mod a b mod,
if mod then
return (ic, c', `(int_mod_neg).mk_app [a, b, c', p])
else do
(ic, c, pβ) β prove_neg ic c',
return (ic, c, `(int_div_neg).mk_app [a, b, c', c, p, pβ])
| none := do
nb β b.to_nat,
na β a.to_int,
let nq := na / nb,
let nr := na % nb,
let nm := nq * nr,
(ic, q) β ic.of_int nq,
(ic, r) β ic.of_int nr,
(ic, m, pm) β prove_mul_rat ic q b (rat.of_int nq) (rat.of_int nb),
(ic, p) β prove_add_rat ic r m a (rat.of_int nr) (rat.of_int nm) (rat.of_int na),
(ic, p') β prove_lt_nat ic r b,
if ic.Ξ± = `(nat) then
if mod then return (ic, r, `(nat_mod).mk_app [a, b, q, r, m, pm, p, p'])
else return (ic, q, `(nat_div).mk_app [a, b, q, r, m, pm, p, p'])
else if ic.Ξ± = `(int) then do
(ic, pβ) β prove_nonneg ic r,
if mod then return (ic, r, `(int_mod).mk_app [a, b, q, r, m, pm, p, pβ, p'])
else return (ic, q, `(int_div).mk_app [a, b, q, r, m, pm, p, pβ, p'])
else failed
end
theorem dvd_eq_nat (a b c : β) (p) (hβ : b % a = c) (hβ : (c = 0) = p) : (a β£ b) = p :=
(propext $ by rw [β hβ, nat.dvd_iff_mod_eq_zero]).trans hβ
theorem dvd_eq_int (a b c : β€) (p) (hβ : b % a = c) (hβ : (c = 0) = p) : (a β£ b) = p :=
(propext $ by rw [β hβ, int.dvd_iff_mod_eq_zero]).trans hβ
/-- Evaluates some extra numeric operations on `nat` and `int`, specifically
`nat.succ`, `/` and `%`, and `β£` (divisibility). -/
meta def eval_nat_int_ext : expr β tactic (expr Γ expr)
| e@`(nat.succ _) := do
ic β mk_instance_cache `(β),
(_, _, ep) β prove_nat_succ ic e,
return ep
| `(%%a / %%b) := do
c β infer_type a >>= mk_instance_cache,
prod.snd <$> prove_div_mod c a b ff
| `(%%a % %%b) := do
c β infer_type a >>= mk_instance_cache,
prod.snd <$> prove_div_mod c a b tt
| `(%%a β£ %%b) := do
Ξ± β infer_type a,
ic β mk_instance_cache Ξ±,
th β if Ξ± = `(nat) then return (`(dvd_eq_nat):expr) else
if Ξ± = `(int) then return `(dvd_eq_int) else failed,
(ic, c, pβ) β prove_div_mod ic b a tt,
(ic, z) β ic.mk_app ``has_zero.zero [],
(e', pβ) β mk_app ``eq [c, z] >>= eval_ineq,
return (e', th.mk_app [a, b, c, e', pβ, pβ])
| _ := failed
lemma not_prime_helper (a b n : β)
(h : a * b = n) (hβ : 1 < a) (hβ : 1 < b) : Β¬ nat.prime n :=
by rw β h; exact nat.not_prime_mul hβ hβ
lemma is_prime_helper (n : β)
(hβ : 1 < n) (hβ : nat.min_fac n = n) : nat.prime n :=
nat.prime_def_min_fac.2 β¨hβ, hββ©
lemma min_fac_bit0 (n : β) : nat.min_fac (bit0 n) = 2 :=
by simp [nat.min_fac_eq, show 2 β£ bit0 n, by simp [bit0_eq_two_mul n]]
/-- A predicate representing partial progress in a proof of `min_fac`. -/
def min_fac_helper (n k : β) : Prop :=
0 < k β§ bit1 k β€ nat.min_fac (bit1 n)
theorem min_fac_helper.n_pos {n k : β} (h : min_fac_helper n k) : 0 < n :=
nat.pos_iff_ne_zero.2 $ Ξ» e,
by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2
lemma min_fac_ne_bit0 {n k : β} : nat.min_fac (bit1 n) β bit0 k :=
by rw bit0_eq_two_mul; exact Ξ» e, absurd
((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2
(dvd_trans β¨_, eβ© (nat.min_fac_dvd _)))
dec_trivial
lemma min_fac_helper_0 (n : β) (h : 0 < n) : min_fac_helper n 1 :=
begin
refine β¨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symmβ©,
refine @lt_of_le_of_ne β _ _ _ (nat.min_fac_pos _) _,
intro e,
have := nat.min_fac_prime _,
{ rw β e at this, exact nat.not_prime_one this },
{ exact ne_of_gt (nat.bit1_lt h) }
end
lemma min_fac_helper_1 {n k k' : β} (e : k + 1 = k')
(np : nat.min_fac (bit1 n) β bit1 k)
(h : min_fac_helper n k) : min_fac_helper n k' :=
begin
rw β e,
refine β¨nat.succ_pos _,
(lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _)
min_fac_ne_bit0.symm : bit0 (k+1) < _)β©,
{ rw add_right_comm, exact h.2 },
{ rw add_right_comm, exact np.symm }
end
lemma min_fac_helper_2 (n k k' : β) (e : k + 1 = k')
(np : Β¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' :=
begin
refine min_fac_helper_1 e _ h,
intro eβ, rw β eβ at np,
exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos)
end
lemma min_fac_helper_3 (n k k' c : β) (e : k + 1 = k')
(nc : bit1 n % bit1 k = c) (c0 : 0 < c)
(h : min_fac_helper n k) : min_fac_helper n k' :=
begin
refine min_fac_helper_1 e _ h,
refine mt _ (ne_of_gt c0), intro eβ,
rw [β nc, β nat.dvd_iff_mod_eq_zero, β eβ],
apply nat.min_fac_dvd
end
lemma min_fac_helper_4 (n k : β) (hd : bit1 n % bit1 k = 0)
(h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k :=
by rw β nat.dvd_iff_mod_eq_zero at hd; exact
le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2
lemma min_fac_helper_5 (n k k' : β) (e : bit1 k * bit1 k = k')
(hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n :=
begin
refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2
β¨nat.bit1_lt h.n_pos, _β©)).2,
rw β e at hd,
intros m m2 hm md,
have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm),
rw nat.le_sqrt at this,
exact not_le_of_lt hd this
end
/-- Given `e` a natural numeral and `d : nat` a factor of it, return `β’ Β¬ prime e`. -/
meta def prove_non_prime (e : expr) (n dβ : β) : tactic expr :=
do let eβ := reflect dβ,
c β mk_instance_cache `(nat),
(c, pβ) β prove_lt_nat c `(1) eβ,
let dβ := n / dβ, let eβ := reflect dβ,
(c, e', p) β prove_mul_nat c eβ eβ,
guard (e' =β e),
(c, pβ) β prove_lt_nat c `(1) eβ,
return $ `(not_prime_helper).mk_app [eβ, eβ, e, p, pβ, pβ]
/-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`,
returns `(c, β’ min_fac a1 = c)`. -/
meta def prove_min_fac_aux (a a1 : expr) (n1 : β) :
instance_cache β expr β expr β tactic (instance_cache Γ expr Γ expr)
| ic b p := do
k β b.to_nat,
let k1 := bit1 k,
let b1 := `(bit1:βββ).mk_app [b],
if n1 < k1*k1 then do
(ic, e', pβ) β prove_mul_nat ic b1 b1,
(ic, pβ) β prove_lt_nat ic a1 e',
return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', pβ, pβ, p])
else let d := k1.min_fac in
if to_bool (d < k1) then do
let k' := k+1, let e' := reflect k',
(ic, pβ) β prove_succ ic b e',
pβ β prove_non_prime b1 k1 d,
prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', pβ, pβ, p]
else do
let nc := n1 % k1,
(ic, c, pc) β prove_div_mod ic a1 b1 tt,
if nc = 0 then
return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p])
else do
(ic, pβ) β prove_pos ic c,
let k' := k+1, let e' := reflect k',
(ic, pβ) β prove_succ ic b e',
prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, pβ, pc, pβ, p]
/-- Given `a` a natural numeral, returns `(b, β’ min_fac a = b)`. -/
meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache Γ expr Γ expr) :=
match match_numeral e with
| match_numeral_result.zero := return (ic, `(2:β), `(nat.min_fac_zero))
| match_numeral_result.one := return (ic, `(1:β), `(nat.min_fac_one))
| match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e])
| match_numeral_result.bit1 e := do
n β e.to_nat,
c β mk_instance_cache `(nat),
(c, p) β prove_pos c e,
let a1 := `(bit1:βββ).mk_app [e],
prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p])
| _ := failed
end
/-- Evaluates the `prime` and `min_fac` functions. -/
meta def eval_prime : expr β tactic (expr Γ expr)
| `(nat.prime %%e) := do
n β e.to_nat,
match n with
| 0 := false_intro `(nat.not_prime_zero)
| 1 := false_intro `(nat.not_prime_one)
| _ := let dβ := n.min_fac in
if dβ < n then prove_non_prime e n dβ >>= false_intro
else do
let eβ := reflect dβ,
c β mk_instance_cache `(nat),
(c, pβ) β prove_lt_nat c `(1) eβ,
(c, eβ, p) β prove_min_fac c e,
true_intro $ `(is_prime_helper).mk_app [e, pβ, p]
end
| `(nat.min_fac %%e) := do
ic β mk_instance_cache `(β),
prod.snd <$> prove_min_fac ic e
| _ := failed
/-- This version of `derive` does not fail when the input is already a numeral -/
meta def derive' (e : expr) : tactic (expr Γ expr) :=
eval_field e <|> eval_nat_int_ext e <|>
eval_pow e <|> eval_ineq e <|> eval_prime e
meta def derive : expr β tactic (expr Γ expr) | e :=
do e β instantiate_mvars e,
(_, e', pr) β
ext_simplify_core () {} simp_lemmas.mk (Ξ» _, failed) (Ξ» _ _ _ _ _, failed)
(Ξ» _ _ _ _ e,
do (new_e, pr) β derive' e,
guard (Β¬ new_e =β e),
return ((), new_e, some pr, tt))
`eq e,
return (e', pr)
end norm_num
namespace tactic.interactive
open norm_num interactive interactive.types
/-- Basic version of `norm_num` that does not call `simp`. -/
meta def norm_num1 (loc : parse location) : tactic unit :=
do ns β loc.get_locals,
tt β tactic.replace_at derive ns loc.include_goal
| fail "norm_num failed to simplify",
when loc.include_goal $ try tactic.triv,
when (Β¬ ns.empty) $ try tactic.contradiction
/-- Normalize numerical expressions. Supports the operations
`+` `-` `*` `/` `^` and `%` over numerical types such as
`β`, `β€`, `β`, `β`, `β` and some general algebraic types,
and can prove goals of the form `A = B`, `A β B`, `A < B` and `A β€ B`,
where `A` and `B` are numerical expressions.
It also has a relatively simple primality prover. -/
meta def norm_num (hs : parse simp_arg_list) (l : parse location) : tactic unit :=
repeat1 $ orelse' (norm_num1 l) $
simp_core {} (norm_num1 (loc.ns [none])) ff hs [] l
add_hint_tactic "norm_num"
/-- Normalizes a numerical expression and tries to close the goal with the result. -/
meta def apply_normed (x : parse texpr) : tactic unit :=
do xβ β to_expr x,
(xβ,_) β derive xβ,
tactic.exact xβ
/--
Normalises numerical expressions. It supports the operations `+` `-` `*` `/` `^` and `%` over
numerical types such as `β`, `β€`, `β`, `β`, `β`, and can prove goals of the form `A = B`, `A β B`,
`A < B` and `A β€ B`, where `A` and `B` are
numerical expressions. It also has a relatively simple primality prover.
```lean
import data.real.basic
example : (2 : β) + 2 = 4 := by norm_num
example : (12345.2 : β) β 12345.3 := by norm_num
example : (73 : β) < 789/2 := by norm_num
example : 123456789 + 987654321 = 1111111110 := by norm_num
example (R : Type*) [ring R] : (2 : R) + 2 = 4 := by norm_num
example (F : Type*) [linear_ordered_field F] : (2 : F) + 2 < 5 := by norm_num
example : nat.prime (2^13 - 1) := by norm_num
example : Β¬ nat.prime (2^11 - 1) := by norm_num
example (x : β) (h : x = 123 + 456) : x = 579 := by norm_num at h; assumption
```
The variant `norm_num1` does not call `simp`.
Both `norm_num` and `norm_num1` can be called inside the `conv` tactic.
The tactic `apply_normed` normalises a numerical expression and tries to close the goal with
the result. Compare:
```lean
def a : β := 2^100
#print a -- 2 ^ 100
def normed_a : β := by apply_normed 2^100
#print normed_a -- 1267650600228229401496703205376
```
-/
add_tactic_doc
{ name := "norm_num",
category := doc_category.tactic,
decl_names := [`tactic.interactive.norm_num1, `tactic.interactive.norm_num,
`tactic.interactive.apply_normed],
tags := ["arithmetic", "decision procedure"] }
end tactic.interactive
namespace conv.interactive
open conv interactive tactic.interactive
open norm_num (derive)
/-- Basic version of `norm_num` that does not call `simp`. -/
meta def norm_num1 : conv unit := replace_lhs derive
/-- Normalize numerical expressions. Supports the operations
`+` `-` `*` `/` `^` and `%` over numerical types such as
`β`, `β€`, `β`, `β`, `β` and some general algebraic types,
and can prove goals of the form `A = B`, `A β B`, `A < B` and `A β€ B`,
where `A` and `B` are numerical expressions.
It also has a relatively simple primality prover. -/
meta def norm_num (hs : parse simp_arg_list) : conv unit :=
repeat1 $ orelse' norm_num1 $
conv.interactive.simp ff hs [] { discharger := tactic.interactive.norm_num1 (loc.ns [none]) }
end conv.interactive
|
350de6425e0b6f88249969fea819b333d32b9aa9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/number_theory/primes_congruent_one_auto.lean | e0439a56fccc58b9dafe7c8237d11c030e9c5c4f | [] | 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,170 | lean | /-
Copyright (c) 2020 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Riccardo Brasca
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.polynomial.cyclotomic
import Mathlib.topology.algebra.polynomial
import Mathlib.field_theory.finite.basic
import Mathlib.PostPort
namespace Mathlib
/-!
# Primes congruent to one
We prove that, for any positive `k : β`, there are infinitely many primes `p` such that
`p β‘ 1 [MOD k]`.
-/
namespace nat
/-- For any positive `k : β` there are infinitely many primes `p` such that `p β‘ 1 [MOD k]`. -/
theorem exists_prime_ge_modeq_one (k : β) (n : β) (hpos : 0 < k) :
β (p : β), prime p β§ n β€ p β§ modeq k p 1 :=
sorry
theorem frequently_at_top_modeq_one (k : β) (hpos : 0 < k) :
filter.frequently (fun (p : β) => prime p β§ modeq k p 1) filter.at_top :=
sorry
theorem infinite_set_of_prime_modeq_one (k : β) (hpos : 0 < k) :
set.infinite (set_of fun (p : β) => prime p β§ modeq k p 1) :=
iff.mp frequently_at_top_iff_infinite (frequently_at_top_modeq_one k hpos)
end Mathlib |
9c48bb0db9c556a82e01a7e1177381675516f98d | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/measure/regular.lean | 3b2ffe59991688650d1d8977bfd1c5775f44a742 | [
"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 | 33,788 | lean | /-
Copyright (c) 2021 SΓ©bastien GouΓ«zel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris Van Doorn, Yury Kudryashov
-/
import measure_theory.constructions.borel_space
/-!
# Regular measures
A measure is `outer_regular` if the measure of any measurable set `A` is the infimum of `ΞΌ U` over
all open sets `U` containing `A`.
A measure is `regular` if it satisfies the following properties:
* it is finite on compact sets;
* it is outer regular;
* it is inner regular for open sets with respect to compacts sets: the measure of any open set `U`
is the supremum of `ΞΌ K` over all compact sets `K` contained in `U`.
A measure is `weakly_regular` if it satisfies the following properties:
* it is outer regular;
* it is inner regular for open sets with respect to closed sets: the measure of any open set `U`
is the supremum of `ΞΌ F` over all closed sets `F` contained in `U`.
In a Hausdorff topological space, regularity implies weak regularity. These three conditions are
registered as typeclasses for a measure `ΞΌ`, and this implication is recorded as an instance.
In order to avoid code duplication, we also define a measure `ΞΌ` to be `inner_regular` for sets
satisfying a predicate `q` with respect to sets satisfying a predicate `p` if for any set
`U β {U | q U}` and a number `r < ΞΌ U` there exists `F β U` such that `p F` and `r < ΞΌ F`.
We prove that inner regularity for open sets with respect to compact sets or closed sets implies
inner regularity for all measurable sets of finite measure (with respect to
compact sets or closed sets respectively), and register some corollaries for (weakly) regular
measures.
Note that a similar statement for measurable sets of infinite mass can fail. For a counterexample,
consider the group `β Γ β` where the first factor has the discrete topology and the second one the
usual topology. It is a locally compact Hausdorff topological group, with Haar measure equal to
Lebesgue measure on each vertical fiber. The set `β Γ {0}` has infinite measure (by outer
regularity), but any compact set it contains has zero measure (as it is finite).
Several authors require as a definition of regularity that all measurable sets are inner regular.
We have opted for the slightly weaker definition above as it holds for all Haar measures, it is
enough for essentially all applications, and it is equivalent to the other definition when the
measure is finite.
The interest of the notion of weak regularity is that it is enough for many applications, and it
is automatically satisfied by any finite measure on a metric space.
## Main definitions
* `measure_theory.measure.outer_regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a
topological space is outer regular.
* `measure_theory.measure.regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a topological
space is regular.
* `measure_theory.measure.weakly_regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a
topological space is weakly regular.
* `measure_theory.measure.inner_regular ΞΌ p q`: a non-typeclass predicate saying that a measure `ΞΌ`
is inner regular for sets satisfying `q` with respect to sets satisfying `p`.
## Main results
### Outer regular measures
* `set.measure_eq_infi_is_open` asserts that, when `ΞΌ` is outer regular, the measure of a
set is the infimum of the measure of open sets containing it.
* `set.exists_is_open_lt_of_lt'` asserts that, when `ΞΌ` is outer regular, for every set `s`
and `r > ΞΌ s` there exists an open superset `U β s` of measure less than `r`.
* push forward of an outer regular measure is outer regular, and scalar multiplication of a regular
measure by a finite number is outer regular.
* `measure_theory.measure.outer_regular.of_sigma_compact_space_of_is_locally_finite_measure`:
a locally finite measure on a `Ο`-compact metric (or even pseudo emetric) space is outer regular.
### Weakly regular measures
* `is_open.measure_eq_supr_is_closed` asserts that the measure of an open set is the supremum of
the measure of closed sets it contains.
* `is_open.exists_lt_is_closed`: for an open set `U` and `r < ΞΌ U`, there exists a closed `F β U`
of measure greater than `r`;
* `measurable_set.measure_eq_supr_is_closed_of_ne_top` asserts that the measure of a measurable set
of finite measure is the supremum of the measure of closed sets it contains.
* `measurable_set.exists_lt_is_closed_of_ne_top` and `measurable_set.exists_is_closed_lt_add`:
a measurable set of finite measure can be approximated by a closed subset (stated as
`r < ΞΌ F` and `ΞΌ s < ΞΌ F + Ξ΅`, respectively).
* `measure_theory.measure.weakly_regular.of_pseudo_emetric_space_of_is_finite_measure` is an
instance registering that a finite measure on a metric space is weakly regular (in fact, a pseudo
emetric space is enough);
* `measure_theory.measure.weakly_regular.of_pseudo_emetric_sigma_compact_space_of_locally_finite`
is an instance registering that a locally finite measure on a `Ο`-compact metric space (or even
a pseudo emetric space) is weakly regular.
### Regular measures
* `is_open.measure_eq_supr_is_compact` asserts that the measure of an open set is the supremum of
the measure of compact sets it contains.
* `is_open.exists_lt_is_compact`: for an open set `U` and `r < ΞΌ U`, there exists a compact `K β U`
of measure greater than `r`;
* `measurable_set.measure_eq_supr_is_compact_of_ne_top` asserts that the measure of a measurable set
of finite measure is the supremum of the measure of compact sets it contains.
* `measurable_set.exists_lt_is_compact_of_ne_top` and `measurable_set.exists_is_compact_lt_add`:
a measurable set of finite measure can be approximated by a compact subset (stated as
`r < ΞΌ K` and `ΞΌ s < ΞΌ K + Ξ΅`, respectively).
* `measure_theory.measure.regular.of_sigma_compact_space_of_is_locally_finite_measure` is an
instance registering that a locally finite measure on a `Ο`-compact metric space is regular (in
fact, an emetric space is enough).
## Implementation notes
The main nontrivial statement is `measure_theory.measure.inner_regular.weakly_regular_of_finite`,
expressing that in a finite measure space, if every open set can be approximated from inside by
closed sets, then the measure is in fact weakly regular. To prove that we show that any measurable
set can be approximated from inside by closed sets and from outside by open sets. This statement is
proved by measurable induction, starting from open sets and checking that it is stable by taking
complements (this is the point of this condition, being symmetrical between inside and outside) and
countable disjoint unions.
Once this statement is proved, one deduces results for `Ο`-finite measures from this statement, by
restricting them to finite measure sets (and proving that this restriction is weakly regular, using
again the same statement).
## References
[Halmos, Measure Theory, Β§52][halmos1950measure]. Note that Halmos uses an unusual definition of
Borel sets (for him, they are elements of the `Ο`-algebra generated by compact sets!), so his
proofs or statements do not apply directly.
[Billingsley, Convergence of Probability Measures][billingsley1999]
-/
open set filter
open_locale ennreal topological_space nnreal big_operators
namespace measure_theory
namespace measure
/-- We say that a measure `ΞΌ` is *inner regular* with respect to predicates `p q : set Ξ± β Prop`,
if for every `U` such that `q U` and `r < ΞΌ U`, there exists a subset `K β U` satisfying `p K`
of measure greater than `r`.
This definition is used to prove some facts about regular and weakly regular measures without
repeating the proofs. -/
def inner_regular {Ξ±} {m : measurable_space Ξ±} (ΞΌ : measure Ξ±) (p q : set Ξ± β Prop) :=
β β¦Uβ¦, q U β β r < ΞΌ U, β K β U, p K β§ r < ΞΌ K
namespace inner_regular
variables {Ξ± : Type*} {m : measurable_space Ξ±} {ΞΌ : measure Ξ±} {p q : set Ξ± β Prop}
{U : set Ξ±} {Ξ΅ : ββ₯0β}
lemma measure_eq_supr (H : inner_regular ΞΌ p q) (hU : q U) : ΞΌ U = β¨ (K β U) (hK : p K), ΞΌ K :=
begin
refine le_antisymm (le_of_forall_lt $ Ξ» r hr, _) (suprβ_le $ Ξ» K hK, supr_le $ Ξ» _, ΞΌ.mono hK),
simpa only [lt_supr_iff, exists_prop] using H hU r hr
end
lemma exists_subset_lt_add (H : inner_regular ΞΌ p q) (h0 : p β
) (hU : q U) (hΞΌU : ΞΌ U β β)
(hΞ΅ : Ξ΅ β 0) :
β K β U, p K β§ ΞΌ U < ΞΌ K + Ξ΅ :=
begin
cases eq_or_ne (ΞΌ U) 0 with hβ hβ,
{ refine β¨β
, empty_subset _, h0, _β©,
rwa [measure_empty, hβ, zero_add, pos_iff_ne_zero] },
{ rcases H hU _ (ennreal.sub_lt_self hΞΌU hβ hΞ΅) with β¨K, hKU, hKc, hrKβ©,
exact β¨K, hKU, hKc, ennreal.lt_add_of_sub_lt_right (or.inl hΞΌU) hrKβ© }
end
lemma map {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] {ΞΌ : measure Ξ±} {pa qa : set Ξ± β Prop}
(H : inner_regular ΞΌ pa qa) (f : Ξ± β Ξ²) (hf : ae_measurable f ΞΌ)
{pb qb : set Ξ² β Prop} (hAB : β U, qb U β qa (f β»ΒΉ' U)) (hAB' : β K, pa K β pb (f '' K))
(hBβ : β K, pb K β measurable_set K) (hBβ : β U, qb U β measurable_set U) :
inner_regular (map f ΞΌ) pb qb :=
begin
intros U hU r hr,
rw [map_apply_of_ae_measurable hf (hBβ _ hU)] at hr,
rcases H (hAB U hU) r hr with β¨K, hKU, hKc, hKβ©,
refine β¨f '' K, image_subset_iff.2 hKU, hAB' _ hKc, _β©,
rwa [map_apply_of_ae_measurable hf (hBβ _ $ hAB' _ hKc), f.preimage_image]
end
lemma smul (H : inner_regular ΞΌ p q) (c : ββ₯0β) : inner_regular (c β’ ΞΌ) p q :=
begin
intros U hU r hr,
rw [smul_apply, H.measure_eq_supr hU, smul_eq_mul] at hr,
simpa only [ennreal.mul_supr, lt_supr_iff, exists_prop] using hr
end
lemma trans {q' : set Ξ± β Prop} (H : inner_regular ΞΌ p q) (H' : inner_regular ΞΌ q q') :
inner_regular ΞΌ p q' :=
begin
intros U hU r hr,
rcases H' hU r hr with β¨F, hFU, hqF, hFβ©, rcases H hqF _ hF with β¨K, hKF, hpK, hrKβ©,
exact β¨K, hKF.trans hFU, hpK, hrKβ©
end
end inner_regular
variables {Ξ± Ξ² : Type*} [measurable_space Ξ±] [topological_space Ξ±] {ΞΌ : measure Ξ±}
/-- A measure `ΞΌ` is outer regular if `ΞΌ(A) = inf {ΞΌ(U) | A β U open}` for a measurable set `A`.
This definition implies the same equality for any (not necessarily measurable) set, see
`set.measure_eq_infi_is_open`. -/
@[protect_proj] class outer_regular (ΞΌ : measure Ξ±) : Prop :=
(outer_regular : β β¦A : set Ξ±β¦, measurable_set A β β r > ΞΌ A, β U β A, is_open U β§ ΞΌ U < r)
/-- A measure `ΞΌ` is regular if
- it is finite on all compact sets;
- it is outer regular: `ΞΌ(A) = inf {ΞΌ(U) | A β U open}` for `A` measurable;
- it is inner regular for open sets, using compact sets:
`ΞΌ(U) = sup {ΞΌ(K) | K β U compact}` for `U` open. -/
@[protect_proj] class regular (ΞΌ : measure Ξ±)
extends is_finite_measure_on_compacts ΞΌ, outer_regular ΞΌ : Prop :=
(inner_regular : inner_regular ΞΌ is_compact is_open)
/-- A measure `ΞΌ` is weakly regular if
- it is outer regular: `ΞΌ(A) = inf {ΞΌ(U) | A β U open}` for `A` measurable;
- it is inner regular for open sets, using closed sets:
`ΞΌ(U) = sup {ΞΌ(F) | F β U compact}` for `U` open. -/
@[protect_proj] class weakly_regular (ΞΌ : measure Ξ±) extends outer_regular ΞΌ : Prop :=
(inner_regular : inner_regular ΞΌ is_closed is_open)
/-- A regular measure is weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance regular.weakly_regular [t2_space Ξ±] [regular ΞΌ] : weakly_regular ΞΌ :=
{ inner_regular := Ξ» U hU r hr, let β¨K, hKU, hcK, hKβ© := regular.inner_regular hU r hr
in β¨K, hKU, hcK.is_closed, hKβ© }
namespace outer_regular
instance zero : outer_regular (0 : measure Ξ±) :=
β¨Ξ» A hA r hr, β¨univ, subset_univ A, is_open_univ, hrβ©β©
/-- Given `r` larger than the measure of a set `A`, there exists an open superset of `A` with
measure less than `r`. -/
lemma _root_.set.exists_is_open_lt_of_lt [outer_regular ΞΌ] (A : set Ξ±) (r : ββ₯0β) (hr : ΞΌ A < r) :
β U β A, is_open U β§ ΞΌ U < r :=
begin
rcases outer_regular.outer_regular (measurable_set_to_measurable ΞΌ A) r
(by rwa measure_to_measurable) with β¨U, hAU, hUo, hUβ©,
exact β¨U, (subset_to_measurable _ _).trans hAU, hUo, hUβ©
end
/-- For an outer regular measure, the measure of a set is the infimum of the measures of open sets
containing it. -/
lemma _root_.set.measure_eq_infi_is_open (A : set Ξ±) (ΞΌ : measure Ξ±) [outer_regular ΞΌ] :
ΞΌ A = (β¨
(U : set Ξ±) (h : A β U) (h2 : is_open U), ΞΌ U) :=
begin
refine le_antisymm (le_infiβ $ Ξ» s hs, le_infi $ Ξ» h2s, ΞΌ.mono hs) _,
refine le_of_forall_lt' (Ξ» r hr, _),
simpa only [infi_lt_iff, exists_prop] using A.exists_is_open_lt_of_lt r hr
end
lemma _root_.set.exists_is_open_lt_add [outer_regular ΞΌ] (A : set Ξ±) (hA : ΞΌ A β β)
{Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β U β A, is_open U β§ ΞΌ U < ΞΌ A + Ξ΅ :=
A.exists_is_open_lt_of_lt _ (ennreal.lt_add_right hA hΞ΅)
lemma _root_.set.exists_is_open_le_add (A : set Ξ±) (ΞΌ : measure Ξ±) [outer_regular ΞΌ]
{Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β U β A, is_open U β§ ΞΌ U β€ ΞΌ A + Ξ΅ :=
begin
rcases le_or_lt β (ΞΌ A) with H|H,
{ exact β¨univ, subset_univ _, is_open_univ,
by simp only [top_le_iff.mp H, ennreal.top_add, le_top]β© },
{ rcases A.exists_is_open_lt_add H.ne hΞ΅ with β¨U, AU, U_open, hUβ©,
exact β¨U, AU, U_open, hU.leβ© }
end
lemma _root_.measurable_set.exists_is_open_diff_lt [outer_regular ΞΌ] {A : set Ξ±}
(hA : measurable_set A) (hA' : ΞΌ A β β) {Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β U β A, is_open U β§ ΞΌ U < β β§ ΞΌ (U \ A) < Ξ΅ :=
begin
rcases A.exists_is_open_lt_add hA' hΞ΅ with β¨U, hAU, hUo, hUβ©,
use [U, hAU, hUo, hU.trans_le le_top],
exact measure_diff_lt_of_lt_add hA hAU hA' hU,
end
protected lemma map [opens_measurable_space Ξ±] [measurable_space Ξ²] [topological_space Ξ²]
[borel_space Ξ²] (f : Ξ± ββ Ξ²) (ΞΌ : measure Ξ±) [outer_regular ΞΌ] :
(measure.map f ΞΌ).outer_regular :=
begin
refine β¨Ξ» A hA r hr, _β©,
rw [map_apply f.measurable hA, β f.image_symm] at hr,
rcases set.exists_is_open_lt_of_lt _ r hr with β¨U, hAU, hUo, hUβ©,
have : is_open (f.symm β»ΒΉ' U), from hUo.preimage f.symm.continuous,
refine β¨f.symm β»ΒΉ' U, image_subset_iff.1 hAU, this, _β©,
rwa [map_apply f.measurable this.measurable_set, f.preimage_symm, f.preimage_image],
end
protected lemma smul (ΞΌ : measure Ξ±) [outer_regular ΞΌ] {x : ββ₯0β} (hx : x β β) :
(x β’ ΞΌ).outer_regular :=
begin
rcases eq_or_ne x 0 with rfl|h0,
{ rw zero_smul, exact outer_regular.zero },
{ refine β¨Ξ» A hA r hr, _β©,
rw [smul_apply, A.measure_eq_infi_is_open, smul_eq_mul] at hr,
simpa only [ennreal.mul_infi_of_ne h0 hx, gt_iff_lt, infi_lt_iff, exists_prop] using hr }
end
end outer_regular
/-- If a measure `ΞΌ` admits finite spanning open sets such that the restriction of `ΞΌ` to each set
is outer regular, then the original measure is outer regular as well. -/
protected lemma finite_spanning_sets_in.outer_regular [opens_measurable_space Ξ±] {ΞΌ : measure Ξ±}
(s : ΞΌ.finite_spanning_sets_in {U | is_open U β§ outer_regular (ΞΌ.restrict U)}) :
outer_regular ΞΌ :=
begin
refine β¨Ξ» A hA r hr, _β©,
have hm : β n, measurable_set (s.set n), from Ξ» n, (s.set_mem n).1.measurable_set,
haveI : β n, outer_regular (ΞΌ.restrict (s.set n)) := Ξ» n, (s.set_mem n).2,
-- Note that `A = β n, A β© disjointed s n`. We replace `A` with this sequence.
obtain β¨A, hAm, hAs, hAd, rflβ© : β A' : β β set Ξ±, (β n, measurable_set (A' n)) β§
(β n, A' n β s.set n) β§ pairwise (disjoint on A') β§ A = β n, A' n,
{ refine β¨Ξ» n, A β© disjointed s.set n, Ξ» n, hA.inter (measurable_set.disjointed hm _),
Ξ» n, (inter_subset_right _ _).trans (disjointed_subset _ _),
(disjoint_disjointed s.set).mono (Ξ» k l hkl, hkl.mono inf_le_right inf_le_right), _β©,
rw [β inter_Union, Union_disjointed, s.spanning, inter_univ] },
rcases ennreal.exists_pos_sum_of_countable' (tsub_pos_iff_lt.2 hr).ne' β with β¨Ξ΄, Ξ΄0, hδΡβ©,
rw [lt_tsub_iff_right, add_comm] at hδΡ,
have : β n, β U β A n, is_open U β§ ΞΌ U < ΞΌ (A n) + Ξ΄ n,
{ intro n,
have Hβ : β t, ΞΌ.restrict (s.set n) t = ΞΌ (t β© s.set n), from Ξ» t, restrict_apply' (hm n),
have Ht : ΞΌ.restrict (s.set n) (A n) β β€,
{ rw Hβ, exact ((measure_mono $ inter_subset_right _ _).trans_lt (s.finite n)).ne },
rcases (A n).exists_is_open_lt_add Ht (Ξ΄0 n).ne' with β¨U, hAU, hUo, hUβ©,
rw [Hβ, Hβ, inter_eq_self_of_subset_left (hAs _)] at hU,
exact β¨U β© s.set n, subset_inter hAU (hAs _), hUo.inter (s.set_mem n).1, hUβ© },
choose U hAU hUo hU,
refine β¨β n, U n, Union_mono hAU, is_open_Union hUo, _β©,
calc ΞΌ (β n, U n) β€ β' n, ΞΌ (U n) : measure_Union_le _
... β€ β' n, (ΞΌ (A n) + Ξ΄ n) : ennreal.tsum_le_tsum (Ξ» n, (hU n).le)
... = β' n, ΞΌ (A n) + β' n, Ξ΄ n : ennreal.tsum_add
... = ΞΌ (β n, A n) + β' n, Ξ΄ n : congr_arg2 (+) (measure_Union hAd hAm).symm rfl
... < r : hδΡ
end
namespace inner_regular
variables {p q : set Ξ± β Prop} {U s : set Ξ±} {Ξ΅ r : ββ₯0β}
/-- If a measure is inner regular (using closed or compact sets), then every measurable set of
finite measure can by approximated by a (closed or compact) subset. -/
lemma measurable_set_of_open [outer_regular ΞΌ]
(H : inner_regular ΞΌ p is_open) (h0 : p β
) (hd : β β¦s Uβ¦, p s β is_open U β p (s \ U)) :
inner_regular ΞΌ p (Ξ» s, measurable_set s β§ ΞΌ s β β) :=
begin
rintros s β¨hs, hΞΌsβ© r hr,
obtain β¨Ξ΅, hΞ΅, hΞ΅s, rflβ© : β Ξ΅ β 0, Ξ΅ + Ξ΅ β€ ΞΌ s β§ r = ΞΌ s - (Ξ΅ + Ξ΅),
{ use (ΞΌ s - r) / 2, simp [*, hr.le, ennreal.add_halves, ennreal.sub_sub_cancel, le_add_right] },
rcases hs.exists_is_open_diff_lt hΞΌs hΞ΅ with β¨U, hsU, hUo, hUt, hΞΌUβ©,
rcases (U \ s).exists_is_open_lt_of_lt _ hΞΌU with β¨U', hsU', hU'o, hΞΌU'β©,
replace hsU' := diff_subset_comm.1 hsU',
rcases H.exists_subset_lt_add h0 hUo hUt.ne hΞ΅ with β¨K, hKU, hKc, hKrβ©,
refine β¨K \ U', Ξ» x hx, hsU' β¨hKU hx.1, hx.2β©, hd hKc hU'o, ennreal.sub_lt_of_lt_add hΞ΅s _β©,
calc ΞΌ s β€ ΞΌ U : ΞΌ.mono hsU
... < ΞΌ K + Ξ΅ : hKr
... β€ ΞΌ (K \ U') + ΞΌ U' + Ξ΅ :
add_le_add_right (tsub_le_iff_right.1 le_measure_diff) _
... β€ ΞΌ (K \ U') + Ξ΅ + Ξ΅ : by { mono*, exacts [hΞΌU'.le, le_rfl] }
... = ΞΌ (K \ U') + (Ξ΅ + Ξ΅) : add_assoc _ _ _
end
open finset
/-- In a finite measure space, assume that any open set can be approximated from inside by closed
sets. Then the measure is weakly regular. -/
lemma weakly_regular_of_finite [borel_space Ξ±] (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ]
(H : inner_regular ΞΌ is_closed is_open) : weakly_regular ΞΌ :=
begin
have hfin : β {s}, ΞΌ s β β€ := measure_ne_top ΞΌ,
suffices : β s, measurable_set s β β Ξ΅ β 0,
β (F β s) (U β s), is_closed F β§ is_open U β§ ΞΌ s β€ ΞΌ F + Ξ΅ β§ ΞΌ U β€ ΞΌ s + Ξ΅,
{ refine { outer_regular := Ξ» s hs r hr, _, inner_regular := H },
rcases exists_between hr with β¨r', hsr', hr'rβ©,
rcases this s hs _ (tsub_pos_iff_lt.2 hsr').ne' with β¨-, -, U, hsU, -, hUo, -, Hβ©,
refine β¨U, hsU, hUo, _β©,
rw [add_tsub_cancel_of_le hsr'.le] at H, exact H.trans_lt hr'r },
refine measurable_set.induction_on_open _ _ _,
/- The proof is by measurable induction: we should check that the property is true for the empty
set, for open sets, and is stable by taking the complement and by taking countable disjoint
unions. The point of the property we are proving is that it is stable by taking complements
(exchanging the roles of closed and open sets and thanks to the finiteness of the measure). -/
-- check for open set
{ intros U hU Ξ΅ hΞ΅,
rcases H.exists_subset_lt_add is_closed_empty hU hfin hΞ΅ with β¨F, hsF, hFc, hFβ©,
exact β¨F, hsF, U, subset.rfl, hFc, hU, hF.le, le_self_addβ© },
-- check for complements
{ rintros s hs H Ξ΅ hΞ΅,
rcases H Ξ΅ hΞ΅ with β¨F, hFs, U, hsU, hFc, hUo, hF, hUβ©,
refine β¨UαΆ, compl_subset_compl.2 hsU, FαΆ, compl_subset_compl.2 hFs,
hUo.is_closed_compl, hFc.is_open_compl, _β©,
simp only [measure_compl_le_add_iff, *, hUo.measurable_set, hFc.measurable_set, true_and] },
-- check for disjoint unions
{ intros s hsd hsm H Ξ΅ Ξ΅0, have Ξ΅0' : Ξ΅ / 2 β 0, from (ennreal.half_pos Ξ΅0).ne',
rcases ennreal.exists_pos_sum_of_countable' Ξ΅0' β with β¨Ξ΄, Ξ΄0, hδΡβ©,
choose F hFs U hsU hFc hUo hF hU using Ξ» n, H n (Ξ΄ n) (Ξ΄0 n).ne',
-- the approximating closed set is constructed by considering finitely many sets `s i`, which
-- cover all the measure up to `Ξ΅/2`, approximating each of these by a closed set `F i`, and
-- taking the union of these (finitely many) `F i`.
have : tendsto (Ξ» t, β k in t, ΞΌ (s k) + Ξ΅ / 2) at_top (π $ ΞΌ (β n, s n) + Ξ΅ / 2),
{ rw measure_Union hsd hsm, exact tendsto.add ennreal.summable.has_sum tendsto_const_nhds },
rcases (this.eventually $ lt_mem_nhds $ ennreal.lt_add_right hfin Ξ΅0').exists with β¨t, htβ©,
-- the approximating open set is constructed by taking for each `s n` an approximating open set
-- `U n` with measure at most `ΞΌ (s n) + Ξ΄ n` for a summable `Ξ΄`, and taking the union of these.
refine β¨β k β t, F k, Union_mono $ Ξ» k, Union_subset $ Ξ» _, hFs _,
β n, U n, Union_mono hsU, is_closed_bUnion t.finite_to_set $ Ξ» k _, hFc k,
is_open_Union hUo, ht.le.trans _, _β©,
{ calc β k in t, ΞΌ (s k) + Ξ΅ / 2 β€ β k in t, ΞΌ (F k) + β k in t, Ξ΄ k + Ξ΅ / 2 :
by { rw β sum_add_distrib, exact add_le_add_right (sum_le_sum $ Ξ» k hk, hF k) _ }
... β€ β k in t, ΞΌ (F k) + Ξ΅ / 2 + Ξ΅ / 2 :
add_le_add_right (add_le_add_left ((ennreal.sum_le_tsum _).trans hδΡ.le) _) _
... = ΞΌ (β k β t, F k) + Ξ΅ : _,
rw [measure_bUnion_finset, add_assoc, ennreal.add_halves],
exacts [Ξ» k _ n _ hkn, (hsd hkn).mono (hFs k) (hFs n), Ξ» k hk, (hFc k).measurable_set] },
{ calc ΞΌ (β n, U n) β€ β' n, ΞΌ (U n) : measure_Union_le _
... β€ β' n, (ΞΌ (s n) + Ξ΄ n) : ennreal.tsum_le_tsum hU
... = ΞΌ (β n, s n) + β' n, Ξ΄ n : by rw [measure_Union hsd hsm, ennreal.tsum_add]
... β€ ΞΌ (β n, s n) + Ξ΅ : add_le_add_left (hδΡ.le.trans ennreal.half_le_self) _ } }
end
/-- In a metric space (or even a pseudo emetric space), an open set can be approximated from inside
by closed sets. -/
lemma of_pseudo_emetric_space {X : Type*} [pseudo_emetric_space X]
[measurable_space X] (ΞΌ : measure X) :
inner_regular ΞΌ is_closed is_open :=
begin
intros U hU r hr,
rcases hU.exists_Union_is_closed with β¨F, F_closed, -, rfl, F_monoβ©,
rw measure_Union_eq_supr F_mono.directed_le at hr,
rcases lt_supr_iff.1 hr with β¨n, hnβ©,
exact β¨F n, subset_Union _ _, F_closed n, hnβ©
end
/-- In a `Ο`-compact space, any closed set can be approximated by a compact subset. -/
lemma is_compact_is_closed {X : Type*} [topological_space X]
[sigma_compact_space X] [measurable_space X] (ΞΌ : measure X) :
inner_regular ΞΌ is_compact is_closed :=
begin
intros F hF r hr,
set B : β β set X := compact_covering X,
have hBc : β n, is_compact (F β© B n), from Ξ» n, (is_compact_compact_covering X n).inter_left hF,
have hBU : (β n, F β© B n) = F, by rw [β inter_Union, Union_compact_covering, set.inter_univ],
have : ΞΌ F = β¨ n, ΞΌ (F β© B n),
{ rw [β measure_Union_eq_supr, hBU],
exact monotone.directed_le
(Ξ» m n h, inter_subset_inter_right _ (compact_covering_subset _ h)) },
rw this at hr, rcases lt_supr_iff.1 hr with β¨n, hnβ©,
exact β¨_, inter_subset_left _ _, hBc n, hnβ©
end
end inner_regular
namespace regular
instance zero : regular (0 : measure Ξ±) :=
β¨Ξ» U hU r hr, β¨β
, empty_subset _, is_compact_empty, hrβ©β©
/-- If `ΞΌ` is a regular measure, then any open set can be approximated by a compact subset. -/
lemma _root_.is_open.exists_lt_is_compact [regular ΞΌ] β¦U : set Ξ±β¦ (hU : is_open U)
{r : ββ₯0β} (hr : r < ΞΌ U) :
β K β U, is_compact K β§ r < ΞΌ K :=
regular.inner_regular hU r hr
/-- The measure of an open set is the supremum of the measures of compact sets it contains. -/
lemma _root_.is_open.measure_eq_supr_is_compact β¦U : set Ξ±β¦ (hU : is_open U)
(ΞΌ : measure Ξ±) [regular ΞΌ] :
ΞΌ U = (β¨ (K : set Ξ±) (h : K β U) (h2 : is_compact K), ΞΌ K) :=
regular.inner_regular.measure_eq_supr hU
lemma exists_compact_not_null [regular ΞΌ] : (β K, is_compact K β§ ΞΌ K β 0) β ΞΌ β 0 :=
by simp_rw [ne.def, β measure_univ_eq_zero, is_open_univ.measure_eq_supr_is_compact,
ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and]
/-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add` and
`measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma inner_regular_measurable [regular ΞΌ] :
inner_regular ΞΌ is_compact (Ξ» s, measurable_set s β§ ΞΌ s β β) :=
regular.inner_regular.measurable_set_of_open is_compact_empty (Ξ» _ _, is_compact.diff)
/-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma _root_.measurable_set.exists_is_compact_lt_add
[regular ΞΌ] β¦A : set Ξ±β¦ (hA : measurable_set A) (h'A : ΞΌ A β β) {Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β K β A, is_compact K β§ ΞΌ A < ΞΌ K + Ξ΅ :=
regular.inner_regular_measurable.exists_subset_lt_add is_compact_empty β¨hA, h'Aβ© h'A hΞ΅
/-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add` and
`measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma _root_.measurable_set.exists_is_compact_diff_lt [opens_measurable_space Ξ±] [t2_space Ξ±]
[regular ΞΌ] β¦A : set Ξ±β¦ (hA : measurable_set A) (h'A : ΞΌ A β β) {Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β K β A, is_compact K β§ ΞΌ (A \ K) < Ξ΅ :=
begin
rcases hA.exists_is_compact_lt_add h'A hΞ΅ with β¨K, hKA, hKc, hKβ©,
exact β¨K, hKA, hKc, measure_diff_lt_of_lt_add hKc.measurable_set hKA
(ne_top_of_le_ne_top h'A $ measure_mono hKA) hKβ©
end
/-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add`. -/
lemma _root_.measurable_set.exists_lt_is_compact_of_ne_top [regular ΞΌ] β¦A : set Ξ±β¦
(hA : measurable_set A) (h'A : ΞΌ A β β) {r : ββ₯0β} (hr : r < ΞΌ A) :
β K β A, is_compact K β§ r < ΞΌ K :=
regular.inner_regular_measurable β¨hA, h'Aβ© _ hr
/-- Given a regular measure, any measurable set of finite mass can be approximated from
inside by compact sets. -/
lemma _root_.measurable_set.measure_eq_supr_is_compact_of_ne_top [regular ΞΌ]
β¦A : set Ξ±β¦ (hA : measurable_set A) (h'A : ΞΌ A β β) :
ΞΌ A = (β¨ (K β A) (h : is_compact K), ΞΌ K) :=
regular.inner_regular_measurable.measure_eq_supr β¨hA, h'Aβ©
protected lemma map [opens_measurable_space Ξ±] [measurable_space Ξ²] [topological_space Ξ²]
[t2_space Ξ²] [borel_space Ξ²] [regular ΞΌ] (f : Ξ± ββ Ξ²) :
(measure.map f ΞΌ).regular :=
begin
haveI := outer_regular.map f ΞΌ,
haveI := is_finite_measure_on_compacts.map ΞΌ f,
exact β¨regular.inner_regular.map f.to_equiv f.measurable.ae_measurable
(Ξ» U hU, hU.preimage f.continuous) (Ξ» K hK, hK.image f.continuous)
(Ξ» K hK, hK.measurable_set) (Ξ» U hU, hU.measurable_set)β©
end
protected lemma smul [regular ΞΌ] {x : ββ₯0β} (hx : x β β) :
(x β’ ΞΌ).regular :=
begin
haveI := outer_regular.smul ΞΌ hx,
haveI := is_finite_measure_on_compacts.smul ΞΌ hx,
exact β¨regular.inner_regular.smul xβ©
end
/-- A regular measure in a Ο-compact space is Ο-finite. -/
@[priority 100] -- see Note [lower instance priority]
instance sigma_finite [sigma_compact_space Ξ±] [regular ΞΌ] : sigma_finite ΞΌ :=
β¨β¨{ set := compact_covering Ξ±,
set_mem := Ξ» n, trivial,
finite := Ξ» n, (is_compact_compact_covering Ξ± n).measure_lt_top,
spanning := Union_compact_covering Ξ± }β©β©
end regular
namespace weakly_regular
/-- If `ΞΌ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/
lemma _root_.is_open.exists_lt_is_closed [weakly_regular ΞΌ] β¦U : set Ξ±β¦ (hU : is_open U)
{r : ββ₯0β} (hr : r < ΞΌ U) :
β F β U, is_closed F β§ r < ΞΌ F :=
weakly_regular.inner_regular hU r hr
/-- If `ΞΌ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/
lemma _root_.is_open.measure_eq_supr_is_closed β¦U : set Ξ±β¦ (hU : is_open U)
(ΞΌ : measure Ξ±) [weakly_regular ΞΌ] :
ΞΌ U = (β¨ (F β U) (h : is_closed F), ΞΌ F) :=
weakly_regular.inner_regular.measure_eq_supr hU
lemma inner_regular_measurable [weakly_regular ΞΌ] :
inner_regular ΞΌ is_closed (Ξ» s, measurable_set s β§ ΞΌ s β β) :=
weakly_regular.inner_regular.measurable_set_of_open is_closed_empty
(Ξ» _ _ hβ hβ, hβ.inter hβ.is_closed_compl)
/-- If `s` is a measurable set, a weakly regular measure `ΞΌ` is finite on `s`, and `Ξ΅` is a positive
number, then there exist a closed set `K β s` such that `ΞΌ s < ΞΌ K + Ξ΅`. -/
lemma _root_.measurable_set.exists_is_closed_lt_add [weakly_regular ΞΌ] {s : set Ξ±}
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) {Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β K β s, is_closed K β§ ΞΌ s < ΞΌ K + Ξ΅ :=
inner_regular_measurable.exists_subset_lt_add is_closed_empty β¨hs, hΞΌsβ© hΞΌs hΞ΅
lemma _root_.measurable_set.exists_is_closed_diff_lt [opens_measurable_space Ξ±]
[weakly_regular ΞΌ] β¦A : set Ξ±β¦ (hA : measurable_set A) (h'A : ΞΌ A β β) {Ξ΅ : ββ₯0β} (hΞ΅ : Ξ΅ β 0) :
β F β A, is_closed F β§ ΞΌ (A \ F) < Ξ΅ :=
begin
rcases hA.exists_is_closed_lt_add h'A hΞ΅ with β¨F, hFA, hFc, hFβ©,
exact β¨F, hFA, hFc, measure_diff_lt_of_lt_add hFc.measurable_set hFA
(ne_top_of_le_ne_top h'A $ measure_mono hFA) hFβ©
end
/-- Given a weakly regular measure, any measurable set of finite mass can be approximated from
inside by closed sets. -/
lemma _root_.measurable_set.exists_lt_is_closed_of_ne_top [weakly_regular ΞΌ]
β¦A : set Ξ±β¦ (hA : measurable_set A) (h'A : ΞΌ A β β) {r : ββ₯0β} (hr : r < ΞΌ A) :
β K β A, is_closed K β§ r < ΞΌ K :=
inner_regular_measurable β¨hA, h'Aβ© _ hr
/-- Given a weakly regular measure, any measurable set of finite mass can be approximated from
inside by closed sets. -/
lemma _root_.measurable_set.measure_eq_supr_is_closed_of_ne_top [weakly_regular ΞΌ] β¦A : set Ξ±β¦
(hA : measurable_set A) (h'A : ΞΌ A β β) :
ΞΌ A = (β¨ (K β A) (h : is_closed K), ΞΌ K) :=
inner_regular_measurable.measure_eq_supr β¨hA, h'Aβ©
/-- The restriction of a weakly regular measure to a measurable set of finite measure is
weakly regular. -/
lemma restrict_of_measurable_set [borel_space Ξ±] [weakly_regular ΞΌ] (A : set Ξ±)
(hA : measurable_set A) (h'A : ΞΌ A β β) : weakly_regular (ΞΌ.restrict A) :=
begin
haveI : fact (ΞΌ A < β) := β¨h'A.lt_topβ©,
refine inner_regular.weakly_regular_of_finite _ (Ξ» V V_open, _),
simp only [restrict_apply' hA], intros r hr,
have : ΞΌ (V β© A) β β, from ne_top_of_le_ne_top h'A (measure_mono $ inter_subset_right _ _),
rcases (V_open.measurable_set.inter hA).exists_lt_is_closed_of_ne_top this hr
with β¨F, hFVA, hFc, hFβ©,
refine β¨F, hFVA.trans (inter_subset_left _ _), hFc, _β©,
rwa inter_eq_self_of_subset_left (hFVA.trans $ inter_subset_right _ _)
end
/-- Any finite measure on a metric space (or even a pseudo emetric space) is weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance of_pseudo_emetric_space_of_is_finite_measure {X : Type*} [pseudo_emetric_space X]
[measurable_space X] [borel_space X] (ΞΌ : measure X) [is_finite_measure ΞΌ] :
weakly_regular ΞΌ :=
(inner_regular.of_pseudo_emetric_space ΞΌ).weakly_regular_of_finite ΞΌ
/-- Any locally finite measure on a `Ο`-compact metric space (or even a pseudo emetric space) is
weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance of_pseudo_emetric_sigma_compact_space_of_locally_finite {X : Type*}
[pseudo_emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X]
(ΞΌ : measure X) [is_locally_finite_measure ΞΌ] :
weakly_regular ΞΌ :=
begin
haveI : outer_regular ΞΌ,
{ refine (ΞΌ.finite_spanning_sets_in_open.mono' $ Ξ» U hU, _).outer_regular,
haveI : fact (ΞΌ U < β), from β¨hU.2β©,
exact β¨hU.1, infer_instanceβ© },
exact β¨inner_regular.of_pseudo_emetric_space ΞΌβ©
end
end weakly_regular
/-- Any locally finite measure on a `Ο`-compact (e)metric space is regular. -/
@[priority 100] -- see Note [lower instance priority]
instance regular.of_sigma_compact_space_of_is_locally_finite_measure {X : Type*}
[emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (ΞΌ : measure X)
[is_locally_finite_measure ΞΌ] : regular ΞΌ :=
{ lt_top_of_is_compact := Ξ» K hK, hK.measure_lt_top,
inner_regular := (inner_regular.is_compact_is_closed ΞΌ).trans
(inner_regular.of_pseudo_emetric_space ΞΌ) }
end measure
end measure_theory
|
2075c6d56e67fd282c5ddcaf87fdda85c027100c | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/tree.lean | 90b76db4ca0d89a047708a911211e706523b87a9 | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 1,994 | lean | import logic data.prod
open eq.ops prod
inductive tree (A : Type) :=
| leaf : A β tree A
| node : tree A β tree A β tree A
inductive one1.{l} : Type.{max 1 l} :=
star : one1
set_option pp.universes true
namespace tree
namespace manual
section
universe variables lβ lβ
variable {A : Type.{lβ}}
variable (C : tree A β Type.{lβ})
definition below (t : tree A) : Type :=
tree.rec_on t (Ξ» a, one1.{lβ}) (Ξ» tβ tβ rβ rβ, C tβ Γ C tβ Γ rβ Γ rβ)
end
section
universe variables lβ lβ
variable {A : Type.{lβ}}
variable {C : tree A β Type.{lβ}}
definition below_rec_on (t : tree A) (H : Ξ (n : tree A), below C n β C n) : C t
:= have general : C t Γ below C t, from
tree.rec_on t
(Ξ»a, (H (leaf a) one1.star, one1.star))
(Ξ» (l r : tree A) (Hl : C l Γ below C l) (Hr : C r Γ below C r),
have b : below C (node l r), from
(prβ Hl, prβ Hr, prβ Hl, prβ Hr),
have c : C (node l r), from
H (node l r) b,
(c, b)),
prβ general
end
end manual
section
universe variables lβ lβ
variable {A : Type.{lβ}}
variable {C : tree A β Type.{lβ+1}}
definition below_rec_on (t : tree A) (H : Ξ (n : tree A), @tree.below A C n β C n) : C t
:= have general : C t Γ @tree.below A C t, from
tree.rec_on t
(Ξ»a, (H (leaf a) poly_unit.star, poly_unit.star))
(Ξ» (l r : tree A) (Hl : C l Γ @tree.below A C l) (Hr : C r Γ @tree.below A C r),
have b : @tree.below A C (node l r), from
((prβ Hl, prβ Hl), (prβ Hr, prβ Hr)),
have c : C (node l r), from
H (node l r) b,
(c, b)),
prβ general
end
set_option pp.universes true
theorem leaf_ne_tree {A : Type} (a : A) (l r : tree A) : leaf a β node l r :=
assume h : leaf a = node l r,
tree.no_confusion h
end tree
|
5e8c2acf54bdf96ed431e9531b494ef3aba853b0 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/function/conditional_expectation.lean | de4ba9188a3d50f6d7d53417906eaceecf6ade4f | [
"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 | 85,748 | lean | /-
Copyright (c) 2021 RΓ©my Degenne. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: RΓ©my Degenne
-/
import analysis.inner_product_space.projection
import measure_theory.function.l2_space
import measure_theory.decomposition.radon_nikodym
/-! # Conditional expectation
We build the conditional expectation of a function `f` with value in a Banach space with respect to
a measure `ΞΌ` (defined on a measurable space structure `m0`) and a measurable space structure `m`
with `hm : m β€ m0` (a sub-sigma-algebra). This is an `m`-measurable function `ΞΌ[f|hm]` which is
integrable and verifies `β« x in s, ΞΌ[f|hm] x βΞΌ = β« x in s, f x βΞΌ` for any `m`-measurable sets `s`.
It is unique as an element of `LΒΉ`.
The construction is done in four steps:
* Define the conditional expectation of an `LΒ²` function, as an element of `LΒ²`. This is the
orthogonal projection on the subspace of almost everywhere `m`-measurable functions.
* Show that the conditional expectation of the indicator of a measurable set with finite measure
is integrable and define a map `set Ξ± β (E βL[β] (Ξ± ββ[ΞΌ] E))` which to a set associates a linear
map. That linear map sends `x β E` to the conditional expectation of the indicator of the set
with value `x`.
* Extend that map to `condexp_L1_clm : (Ξ± ββ[ΞΌ] E) βL[β] (Ξ± ββ[ΞΌ] E)`. This is done using the same
construction as the Bochner integral (see the file `measure_theory/integral/set_to_L1`).
* Define the conditional expectation of a function `f : Ξ± β E`, which is an integrable function
`Ξ± β E` equal to 0 if `f` is not integrable, and equal to an `m`-measurable representative of
`condexp_L1_clm` applied to `[f]`, the equivalence class of `f` in `LΒΉ`.
## Main results
The conditional expectation and its properties
* `condexp (hm : m β€ m0) (ΞΌ : measure Ξ±) (f : Ξ± β E)`: conditional expectation of `f` with respect
to `m`.
* `integrable_condexp` : `condexp` is integrable.
* `measurable_condexp` : `condexp` is `m`-measurable.
* `set_integral_condexp (hf : integrable f ΞΌ) (hs : measurable_set[m] s)` : the conditional
expectation verifies `β« x in s, condexp hm ΞΌ f x βΞΌ = β« x in s, f x βΞΌ` for any `m`-measurable
set `s`.
While `condexp` is function-valued, we also define `condexp_L1` with value in `L1` and a continuous
linear map `condexp_L1_clm` from `L1` to `L1`. `condexp` should be used in most cases.
Uniqueness of the conditional expectation
* `Lp.ae_eq_of_forall_set_integral_eq'`: two `Lp` functions verifying the equality of integrals
defining the conditional expectation are equal everywhere.
* `ae_eq_of_forall_set_integral_eq_of_sigma_finite'`: two functions verifying the equality of
integrals defining the conditional expectation are equal everywhere.
Requires `[sigma_finite (ΞΌ.trim hm)]`.
* `ae_eq_condexp_of_forall_set_integral_eq`: an a.e. `m`-measurable function which verifies the
equality of integrals is a.e. equal to `condexp`.
## Notations
For a measure `ΞΌ` defined on a measurable space structure `m0`, another measurable space structure
`m` with `hm : m β€ m0` (a sub-sigma-algebra) and a function `f`, we define the notation
* `ΞΌ[f|hm] = condexp hm ΞΌ f`.
## Implementation notes
Most of the results in this file are valid for a second countable, borel, real normed space `F`.
However, some lemmas also use `π : is_R_or_C`:
* `condexp_L2` is defined only for an `inner_product_space` for now, and we use `π` for its field.
* results about scalar multiplication are stated not only for `β` but also for `π` if we happen to
have `normed_space π F`.
## Tags
conditional expectation, conditional expected value
-/
noncomputable theory
open topological_space measure_theory.Lp filter continuous_linear_map
open_locale nnreal ennreal topological_space big_operators measure_theory
namespace measure_theory
/-- A function `f` verifies `ae_measurable' m f ΞΌ` if it is `ΞΌ`-a.e. equal to an `m`-measurable
function. This is similar to `ae_measurable`, but the `measurable_space` structures used for the
measurability statement and for the measure are different. -/
def ae_measurable' {Ξ± Ξ²} [measurable_space Ξ²] (m : measurable_space Ξ±) {m0 : measurable_space Ξ±}
(f : Ξ± β Ξ²) (ΞΌ : measure Ξ±) : Prop :=
β g : Ξ± β Ξ², measurable[m] g β§ f =α΅[ΞΌ] g
namespace ae_measurable'
variables {Ξ± Ξ² π : Type*} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
[measurable_space Ξ²] [measurable_space π] {f g : Ξ± β Ξ²}
lemma congr (hf : ae_measurable' m f ΞΌ) (hfg : f =α΅[ΞΌ] g) : ae_measurable' m g ΞΌ :=
by { obtain β¨f', hf'_meas, hff'β© := hf, exact β¨f', hf'_meas, hfg.symm.trans hff'β©, }
lemma add [has_add Ξ²] [has_measurable_addβ Ξ²] (hf : ae_measurable' m f ΞΌ)
(hg : ae_measurable' m g ΞΌ) :
ae_measurable' m (f+g) ΞΌ :=
begin
rcases hf with β¨f', h_f'_meas, hff'β©,
rcases hg with β¨g', h_g'_meas, hgg'β©,
exact β¨f' + g', h_f'_meas.add h_g'_meas, hff'.add hgg'β©,
end
lemma neg [has_neg Ξ²] [has_measurable_neg Ξ²] {f : Ξ± β Ξ²} (hfm : ae_measurable' m f ΞΌ) :
ae_measurable' m (-f) ΞΌ :=
begin
rcases hfm with β¨f', hf'_meas, hf_aeβ©,
refine β¨-f', hf'_meas.neg, hf_ae.mono (Ξ» x hx, _)β©,
simp_rw pi.neg_apply,
rw hx,
end
lemma sub [has_sub Ξ²] [has_measurable_subβ Ξ²] {f g : Ξ± β Ξ²}
(hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) :
ae_measurable' m (f - g) ΞΌ :=
begin
rcases hfm with β¨f', hf'_meas, hf_aeβ©,
rcases hgm with β¨g', hg'_meas, hg_aeβ©,
refine β¨f'-g', hf'_meas.sub hg'_meas, hf_ae.mp (hg_ae.mono (Ξ» x hx1 hx2, _))β©,
simp_rw pi.sub_apply,
rw [hx1, hx2],
end
lemma const_smul [has_scalar π Ξ²] [has_measurable_smul π Ξ²] (c : π) (hf : ae_measurable' m f ΞΌ) :
ae_measurable' m (c β’ f) ΞΌ :=
begin
rcases hf with β¨f', h_f'_meas, hff'β©,
refine β¨c β’ f', h_f'_meas.const_smul c, _β©,
exact eventually_eq.fun_comp hff' (Ξ» x, c β’ x),
end
lemma const_inner {π} [is_R_or_C π] [inner_product_space π Ξ²]
[second_countable_topology Ξ²] [opens_measurable_space Ξ²]
{f : Ξ± β Ξ²} (hfm : ae_measurable' m f ΞΌ) (c : Ξ²) :
ae_measurable' m (Ξ» x, (inner c (f x) : π)) ΞΌ :=
begin
rcases hfm with β¨f', hf'_meas, hf_aeβ©,
refine β¨Ξ» x, (inner c (f' x) : π), (@measurable_const _ _ _ m _).inner hf'_meas,
hf_ae.mono (Ξ» x hx, _)β©,
dsimp only,
rw hx,
end
/-- A m-measurable function almost everywhere equal to `f`. -/
def mk (f : Ξ± β Ξ²) (hfm : ae_measurable' m f ΞΌ) : Ξ± β Ξ² := hfm.some
lemma measurable_mk {f : Ξ± β Ξ²} (hfm : ae_measurable' m f ΞΌ) : measurable[m] (hfm.mk f) :=
hfm.some_spec.1
lemma ae_eq_mk {f : Ξ± β Ξ²} (hfm : ae_measurable' m f ΞΌ) : f =α΅[ΞΌ] hfm.mk f :=
hfm.some_spec.2
lemma measurable_comp {Ξ³} [measurable_space Ξ³] {f : Ξ± β Ξ²} {g : Ξ² β Ξ³}
(hg : measurable g) (hf : ae_measurable' m f ΞΌ) :
ae_measurable' m (g β f) ΞΌ :=
β¨Ξ» x, g (hf.mk _ x), @measurable.comp _ _ _ m _ _ _ _ hg hf.measurable_mk,
hf.ae_eq_mk.mono (Ξ» x hx, by rw [function.comp_apply, hx])β©
end ae_measurable'
lemma ae_measurable'_of_ae_measurable'_trim {Ξ± Ξ²} {m m0 m0' : measurable_space Ξ±}
[measurable_space Ξ²] (hm0 : m0 β€ m0') {ΞΌ : measure Ξ±} {f : Ξ± β Ξ²}
(hf : ae_measurable' m f (ΞΌ.trim hm0)) :
ae_measurable' m f ΞΌ :=
by { obtain β¨g, hg_meas, hfgβ© := hf, exact β¨g, hg_meas, ae_eq_of_ae_eq_trim hfgβ©, }
lemma measurable.ae_measurable' {Ξ± Ξ²} {m m0 : measurable_space Ξ±} [measurable_space Ξ²]
{ΞΌ : measure Ξ±} {f : Ξ± β Ξ²} (hf : measurable[m] f) :
ae_measurable' m f ΞΌ :=
β¨f, hf, ae_eq_refl _β©
lemma ae_eq_trim_iff_of_ae_measurable' {Ξ± Ξ²} [add_group Ξ²] [measurable_space Ξ²]
[measurable_singleton_class Ξ²] [has_measurable_subβ Ξ²]
{m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f g : Ξ± β Ξ²}
(hm : m β€ m0) (hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) :
hfm.mk f =α΅[ΞΌ.trim hm] hgm.mk g β f =α΅[ΞΌ] g :=
(ae_eq_trim_iff hm hfm.measurable_mk hgm.measurable_mk).trans
β¨Ξ» h, hfm.ae_eq_mk.trans (h.trans hgm.ae_eq_mk.symm),
Ξ» h, hfm.ae_eq_mk.symm.trans (h.trans hgm.ae_eq_mk)β©
variables {Ξ± Ξ² Ξ³ E E' F F' G G' H π : Type*} {p : ββ₯0β}
[is_R_or_C π] -- π for β or β
[measurable_space Ξ²] -- Ξ² for a generic measurable space
-- E for an inner product space
[inner_product_space π E] [measurable_space E] [borel_space E] [second_countable_topology E]
-- E' for an inner product space on which we compute integrals
[inner_product_space π E'] [measurable_space E'] [borel_space E'] [second_countable_topology E']
[complete_space E'] [normed_space β E']
-- F for a Lp submodule
[normed_group F] [normed_space π F] [measurable_space F] [borel_space F]
[second_countable_topology F]
-- F' for integrals on a Lp submodule
[normed_group F'] [normed_space π F'] [measurable_space F'] [borel_space F']
[second_countable_topology F'] [normed_space β F'] [complete_space F']
-- G for a Lp add_subgroup
[normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G]
-- G' for integrals on a Lp add_subgroup
[normed_group G'] [measurable_space G'] [borel_space G'] [second_countable_topology G']
[normed_space β G'] [complete_space G']
-- H for measurable space and normed group (hypotheses of mem_βp)
[measurable_space H] [normed_group H]
section Lp_meas
/-! ## The subset `Lp_meas` of `Lp` functions a.e. measurable with respect to a sub-sigma-algebra -/
variables (F)
/-- `Lp_meas_subgroup F m p ΞΌ` is the subspace of `Lp F p ΞΌ` containing functions `f` verifying
`ae_measurable' m f ΞΌ`, i.e. functions which are `ΞΌ`-a.e. equal to an `m`-measurable function. -/
def Lp_meas_subgroup (m : measurable_space Ξ±) [measurable_space Ξ±] (p : ββ₯0β) (ΞΌ : measure Ξ±) :
add_subgroup (Lp F p ΞΌ) :=
{ carrier := {f : (Lp F p ΞΌ) | ae_measurable' m f ΞΌ} ,
zero_mem' := β¨(0 : Ξ± β F), @measurable_zero _ Ξ± m _ _, Lp.coe_fn_zero _ _ _β©,
add_mem' := Ξ» f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm,
neg_mem' := Ξ» f hf, ae_measurable'.congr hf.neg (Lp.coe_fn_neg f).symm, }
variables (π)
/-- `Lp_meas F π m p ΞΌ` is the subspace of `Lp F p ΞΌ` containing functions `f` verifying
`ae_measurable' m f ΞΌ`, i.e. functions which are `ΞΌ`-a.e. equal to an `m`-measurable function. -/
def Lp_meas [opens_measurable_space π] (m : measurable_space Ξ±) [measurable_space Ξ±] (p : ββ₯0β)
(ΞΌ : measure Ξ±) :
submodule π (Lp F p ΞΌ) :=
{ carrier := {f : (Lp F p ΞΌ) | ae_measurable' m f ΞΌ} ,
zero_mem' := β¨(0 : Ξ± β F), @measurable_zero _ Ξ± m _ _, Lp.coe_fn_zero _ _ _β©,
add_mem' := Ξ» f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm,
smul_mem' := Ξ» c f hf, (hf.const_smul c).congr (Lp.coe_fn_smul c f).symm, }
variables {F π}
variables [opens_measurable_space π]
lemma mem_Lp_meas_subgroup_iff_ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
{f : Lp F p ΞΌ} :
f β Lp_meas_subgroup F m p ΞΌ β ae_measurable' m f ΞΌ :=
by rw [β add_subgroup.mem_carrier, Lp_meas_subgroup, set.mem_set_of_eq]
lemma mem_Lp_meas_iff_ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp F p ΞΌ} :
f β Lp_meas F π m p ΞΌ β ae_measurable' m f ΞΌ :=
by rw [β set_like.mem_coe, β submodule.mem_carrier, Lp_meas, set.mem_set_of_eq]
lemma Lp_meas.ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} (f : Lp_meas F π m p ΞΌ) :
ae_measurable' m f ΞΌ :=
mem_Lp_meas_iff_ae_measurable'.mp f.mem
lemma mem_Lp_meas_self {m0 : measurable_space Ξ±} (ΞΌ : measure Ξ±) (f : Lp F p ΞΌ) :
f β Lp_meas F π m0 p ΞΌ :=
mem_Lp_meas_iff_ae_measurable'.mpr (Lp.ae_measurable f)
lemma Lp_meas_subgroup_coe {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
{f : Lp_meas_subgroup F m p ΞΌ} :
βf = (f : Lp F p ΞΌ) :=
coe_fn_coe_base f
lemma Lp_meas_coe {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp_meas F π m p ΞΌ} :
βf = (f : Lp F p ΞΌ) :=
coe_fn_coe_base f
lemma mem_Lp_meas_indicator_const_Lp {m m0 : measurable_space Ξ±} (hm : m β€ m0)
{ΞΌ : measure Ξ±} {s : set Ξ±} (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) {c : F} :
indicator_const_Lp p (hm s hs) hΞΌs c β Lp_meas F π m p ΞΌ :=
β¨s.indicator (Ξ» x : Ξ±, c), (@measurable_const _ Ξ± _ m _).indicator hs, indicator_const_Lp_coe_fnβ©
section complete_subspace
/-! ## The subspace `Lp_meas` is complete.
We define an `isometric` between `Lp_meas_subgroup` and the `Lp` space corresponding to the
measure `ΞΌ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of
`Lp_meas_subgroup` (and `Lp_meas`). -/
variables {ΞΉ : Type*} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
/-- If `f` belongs to `Lp_meas_subgroup F m p ΞΌ`, then the measurable function it is almost
everywhere equal to (given by `ae_measurable.mk`) belongs to `βp` for the measure `ΞΌ.trim hm`. -/
lemma mem_βp_trim_of_mem_Lp_meas_subgroup (hm : m β€ m0) (f : Lp F p ΞΌ)
(hf_meas : f β Lp_meas_subgroup F m p ΞΌ) :
mem_βp (mem_Lp_meas_subgroup_iff_ae_measurable'.mp hf_meas).some p (ΞΌ.trim hm) :=
begin
have hf : ae_measurable' m f ΞΌ, from (mem_Lp_meas_subgroup_iff_ae_measurable'.mp hf_meas),
let g := hf.some,
obtain β¨hg, hfgβ© := hf.some_spec,
change mem_βp g p (ΞΌ.trim hm),
refine β¨hg.ae_measurable, _β©,
have h_snorm_fg : snorm g p (ΞΌ.trim hm) = snorm f p ΞΌ,
by { rw snorm_trim hm hg, exact snorm_congr_ae hfg.symm, },
rw h_snorm_fg,
exact Lp.snorm_lt_top f,
end
/-- If `f` belongs to `Lp` for the measure `ΞΌ.trim hm`, then it belongs to the subgroup
`Lp_meas_subgroup F m p ΞΌ`. -/
lemma mem_Lp_meas_subgroup_to_Lp_of_trim (hm : m β€ m0) (f : Lp F p (ΞΌ.trim hm)) :
(mem_βp_of_mem_βp_trim hm (Lp.mem_βp f)).to_Lp f β Lp_meas_subgroup F m p ΞΌ :=
begin
let hf_mem_βp := mem_βp_of_mem_βp_trim hm (Lp.mem_βp f),
rw mem_Lp_meas_subgroup_iff_ae_measurable',
refine ae_measurable'.congr _ (mem_βp.coe_fn_to_Lp hf_mem_βp).symm,
refine ae_measurable'_of_ae_measurable'_trim hm _,
exact (Lp.ae_measurable f),
end
variables (F p ΞΌ)
/-- Map from `Lp_meas_subgroup` to `Lp F p (ΞΌ.trim hm)`. -/
def Lp_meas_subgroup_to_Lp_trim (hm : m β€ m0) (f : Lp_meas_subgroup F m p ΞΌ) : Lp F p (ΞΌ.trim hm) :=
mem_βp.to_Lp (mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some
(mem_βp_trim_of_mem_Lp_meas_subgroup hm f f.mem)
variables (π)
/-- Map from `Lp_meas` to `Lp F p (ΞΌ.trim hm)`. -/
def Lp_meas_to_Lp_trim (hm : m β€ m0) (f : Lp_meas F π m p ΞΌ) : Lp F p (ΞΌ.trim hm) :=
mem_βp.to_Lp (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some
(mem_βp_trim_of_mem_Lp_meas_subgroup hm f f.mem)
variables {π}
/-- Map from `Lp F p (ΞΌ.trim hm)` to `Lp_meas_subgroup`, inverse of
`Lp_meas_subgroup_to_Lp_trim`. -/
def Lp_trim_to_Lp_meas_subgroup (hm : m β€ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_meas_subgroup F m p ΞΌ :=
β¨(mem_βp_of_mem_βp_trim hm (Lp.mem_βp f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm fβ©
variables (π)
/-- Map from `Lp F p (ΞΌ.trim hm)` to `Lp_meas`, inverse of `Lp_meas_to_Lp_trim`. -/
def Lp_trim_to_Lp_meas (hm : m β€ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_meas F π m p ΞΌ :=
β¨(mem_βp_of_mem_βp_trim hm (Lp.mem_βp f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm fβ©
variables {F π p ΞΌ}
lemma Lp_meas_subgroup_to_Lp_trim_ae_eq (hm : m β€ m0) (f : Lp_meas_subgroup F m p ΞΌ) :
Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f =α΅[ΞΌ] f :=
(ae_eq_of_ae_eq_trim (mem_βp.coe_fn_to_Lp (mem_βp_trim_of_mem_Lp_meas_subgroup hm βf f.mem))).trans
(mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some_spec.2.symm
lemma Lp_trim_to_Lp_meas_subgroup_ae_eq (hm : m β€ m0) (f : Lp F p (ΞΌ.trim hm)) :
Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm f =α΅[ΞΌ] f :=
mem_βp.coe_fn_to_Lp _
lemma Lp_meas_to_Lp_trim_ae_eq (hm : m β€ m0) (f : Lp_meas F π m p ΞΌ) :
Lp_meas_to_Lp_trim F π p ΞΌ hm f =α΅[ΞΌ] f :=
(ae_eq_of_ae_eq_trim (mem_βp.coe_fn_to_Lp (mem_βp_trim_of_mem_Lp_meas_subgroup hm βf f.mem))).trans
(mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some_spec.2.symm
lemma Lp_trim_to_Lp_meas_ae_eq (hm : m β€ m0) (f : Lp F p (ΞΌ.trim hm)) :
Lp_trim_to_Lp_meas F π p ΞΌ hm f =α΅[ΞΌ] f :=
mem_βp.coe_fn_to_Lp _
/-- `Lp_trim_to_Lp_meas_subgroup` is a right inverse of `Lp_meas_subgroup_to_Lp_trim`. -/
lemma Lp_meas_subgroup_to_Lp_trim_right_inv (hm : m β€ m0) :
function.right_inverse (Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm)
(Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) :=
begin
intro f,
ext1,
refine ae_eq_trim_of_measurable hm (Lp.measurable _) (Lp.measurable _) _,
exact (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _),
end
/-- `Lp_trim_to_Lp_meas_subgroup` is a left inverse of `Lp_meas_subgroup_to_Lp_trim`. -/
lemma Lp_meas_subgroup_to_Lp_trim_left_inv (hm : m β€ m0) :
function.left_inverse (Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm)
(Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) :=
begin
intro f,
ext1,
ext1,
rw β Lp_meas_subgroup_coe,
exact (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _).trans (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _),
end
lemma Lp_meas_subgroup_to_Lp_trim_add (hm : m β€ m0) (f g : Lp_meas_subgroup F m p ΞΌ) :
Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (f + g)
= Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f + Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm g :=
begin
ext1,
refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm,
refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _,
{ exact (Lp.measurable _).add (Lp.measurable _), },
refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _,
refine eventually_eq.trans _
(eventually_eq.add (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm
(Lp_meas_subgroup_to_Lp_trim_ae_eq hm g).symm),
refine (Lp.coe_fn_add _ _).trans _,
simp_rw Lp_meas_subgroup_coe,
exact eventually_of_forall (Ξ» x, by refl),
end
lemma Lp_meas_subgroup_to_Lp_trim_neg (hm : m β€ m0) (f : Lp_meas_subgroup F m p ΞΌ) :
Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (-f)
= -Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f :=
begin
ext1,
refine eventually_eq.trans _ (Lp.coe_fn_neg _).symm,
refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _,
{ exact @measurable.neg _ _ _ _ _ m _ (Lp.measurable _), },
refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _,
refine eventually_eq.trans _
(eventually_eq.neg (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm),
refine (Lp.coe_fn_neg _).trans _,
simp_rw Lp_meas_subgroup_coe,
exact eventually_of_forall (Ξ» x, by refl),
end
lemma Lp_meas_subgroup_to_Lp_trim_sub (hm : m β€ m0) (f g : Lp_meas_subgroup F m p ΞΌ) :
Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (f - g)
= Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f - Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm g :=
by rw [sub_eq_add_neg, sub_eq_add_neg, Lp_meas_subgroup_to_Lp_trim_add,
Lp_meas_subgroup_to_Lp_trim_neg]
lemma Lp_meas_to_Lp_trim_smul (hm : m β€ m0) (c : π) (f : Lp_meas F π m p ΞΌ) :
Lp_meas_to_Lp_trim F π p ΞΌ hm (c β’ f) = c β’ Lp_meas_to_Lp_trim F π p ΞΌ hm f :=
begin
ext1,
refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm,
refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _,
{ exact (Lp.measurable _).const_smul c, },
refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _,
refine (Lp.coe_fn_smul _ _).trans _,
refine (Lp_meas_to_Lp_trim_ae_eq hm f).mono (Ξ» x hx, _),
rw [pi.smul_apply, pi.smul_apply, hx],
refl,
end
/-- `Lp_meas_subgroup_to_Lp_trim` preserves the norm. -/
lemma Lp_meas_subgroup_to_Lp_trim_norm_map [hp : fact (1 β€ p)] (hm : m β€ m0)
(f : Lp_meas_subgroup F m p ΞΌ) :
β₯Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm fβ₯ = β₯fβ₯ :=
begin
rw [Lp.norm_def, snorm_trim hm (Lp.measurable _)],
swap, { apply_instance, },
rw [snorm_congr_ae (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _), Lp_meas_subgroup_coe, β Lp.norm_def],
congr,
end
lemma isometry_Lp_meas_subgroup_to_Lp_trim [hp : fact (1 β€ p)] (hm : m β€ m0) :
isometry (Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) :=
begin
rw isometry_emetric_iff_metric,
intros f g,
rw [dist_eq_norm, β Lp_meas_subgroup_to_Lp_trim_sub, Lp_meas_subgroup_to_Lp_trim_norm_map,
dist_eq_norm],
end
variables (F p ΞΌ)
/-- `Lp_meas_subgroup` and `Lp F p (ΞΌ.trim hm)` are isometric. -/
def Lp_meas_subgroup_to_Lp_trim_iso [hp : fact (1 β€ p)] (hm : m β€ m0) :
Lp_meas_subgroup F m p ΞΌ βα΅’ Lp F p (ΞΌ.trim hm) :=
{ to_fun := Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm,
inv_fun := Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm,
left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm,
right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm,
isometry_to_fun := isometry_Lp_meas_subgroup_to_Lp_trim hm, }
variables (π)
/-- `Lp_meas_subgroup` and `Lp_meas` are isometric. -/
def Lp_meas_subgroup_to_Lp_meas_iso [hp : fact (1 β€ p)] :
Lp_meas_subgroup F m p ΞΌ βα΅’ Lp_meas F π m p ΞΌ :=
isometric.refl (Lp_meas_subgroup F m p ΞΌ)
/-- `Lp_meas` and `Lp F p (ΞΌ.trim hm)` are isometric, with a linear equivalence. -/
def Lp_meas_to_Lp_trim_lie [hp : fact (1 β€ p)] (hm : m β€ m0) :
Lp_meas F π m p ΞΌ ββα΅’[π] Lp F p (ΞΌ.trim hm) :=
{ to_fun := Lp_meas_to_Lp_trim F π p ΞΌ hm,
inv_fun := Lp_trim_to_Lp_meas F π p ΞΌ hm,
left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm,
right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm,
map_add' := Lp_meas_subgroup_to_Lp_trim_add hm,
map_smul' := Lp_meas_to_Lp_trim_smul hm,
norm_map' := Lp_meas_subgroup_to_Lp_trim_norm_map hm, }
variables {F π p ΞΌ}
instance [hm : fact (m β€ m0)] [complete_space F] [hp : fact (1 β€ p)] :
complete_space (Lp_meas_subgroup F m p ΞΌ) :=
by { rw (Lp_meas_subgroup_to_Lp_trim_iso F p ΞΌ hm.elim).complete_space_iff, apply_instance, }
instance [hm : fact (m β€ m0)] [complete_space F] [hp : fact (1 β€ p)] :
complete_space (Lp_meas F π m p ΞΌ) :=
by { rw (Lp_meas_subgroup_to_Lp_meas_iso F π p ΞΌ).symm.complete_space_iff, apply_instance, }
lemma is_complete_ae_measurable' [hp : fact (1 β€ p)] [complete_space F] (hm : m β€ m0) :
is_complete {f : Lp F p ΞΌ | ae_measurable' m f ΞΌ} :=
begin
rw β complete_space_coe_iff_is_complete,
haveI : fact (m β€ m0) := β¨hmβ©,
change complete_space (Lp_meas_subgroup F m p ΞΌ),
apply_instance,
end
lemma is_closed_ae_measurable' [hp : fact (1 β€ p)] [complete_space F] (hm : m β€ m0) :
is_closed {f : Lp F p ΞΌ | ae_measurable' m f ΞΌ} :=
is_complete.is_closed (is_complete_ae_measurable' hm)
end complete_subspace
section strongly_measurable
variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
/-- We do not get `ae_fin_strongly_measurable f (ΞΌ.trim hm)`, since we don't have
`f =α΅[ΞΌ.trim hm] Lp_meas_to_Lp_trim F π p ΞΌ hm f` but only the weaker
`f =α΅[ΞΌ] Lp_meas_to_Lp_trim F π p ΞΌ hm f`. -/
lemma Lp_meas.ae_fin_strongly_measurable' (hm : m β€ m0) (f : Lp_meas F π m p ΞΌ) (hp_ne_zero : p β 0)
(hp_ne_top : p β β) :
β g, fin_strongly_measurable g (ΞΌ.trim hm) β§ f =α΅[ΞΌ] g :=
β¨Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f, Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top,
(Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symmβ©
end strongly_measurable
end Lp_meas
section uniqueness_of_conditional_expectation
/-! ## Uniqueness of the conditional expectation -/
variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
lemma Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero
(hm : m β€ m0) (f : Lp_meas E' π m p ΞΌ) (hp_ne_zero : p β 0) (hp_ne_top : p β β)
(hf_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on f s ΞΌ)
(hf_zero : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, f x βΞΌ = 0) :
f =α΅[ΞΌ] 0 :=
begin
obtain β¨g, hg_sm, hfgβ© := Lp_meas.ae_fin_strongly_measurable' hm f hp_ne_zero hp_ne_top,
refine hfg.trans _,
refine ae_eq_zero_of_forall_set_integral_eq_of_fin_strongly_measurable_trim hm _ _ hg_sm,
{ intros s hs hΞΌs,
have hfg_restrict : f =α΅[ΞΌ.restrict s] g, from ae_restrict_of_ae hfg,
rw [integrable_on, integrable_congr hfg_restrict.symm],
exact hf_int_finite s hs hΞΌs, },
{ intros s hs hΞΌs,
have hfg_restrict : f =α΅[ΞΌ.restrict s] g, from ae_restrict_of_ae hfg,
rw integral_congr_ae hfg_restrict.symm,
exact hf_zero s hs hΞΌs, },
end
include π
lemma Lp.ae_eq_zero_of_forall_set_integral_eq_zero'
(hm : m β€ m0) (f : Lp E' p ΞΌ) (hp_ne_zero : p β 0) (hp_ne_top : p β β)
(hf_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on f s ΞΌ)
(hf_zero : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, f x βΞΌ = 0)
(hf_meas : ae_measurable' m f ΞΌ) :
f =α΅[ΞΌ] 0 :=
begin
let f_meas : Lp_meas E' π m p ΞΌ := β¨f, hf_measβ©,
have hf_f_meas : f =α΅[ΞΌ] f_meas, by simp only [coe_fn_coe_base', subtype.coe_mk],
refine hf_f_meas.trans _,
refine Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero hm f_meas hp_ne_zero hp_ne_top _ _,
{ intros s hs hΞΌs,
have hfg_restrict : f =α΅[ΞΌ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas,
rw [integrable_on, integrable_congr hfg_restrict.symm],
exact hf_int_finite s hs hΞΌs, },
{ intros s hs hΞΌs,
have hfg_restrict : f =α΅[ΞΌ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas,
rw integral_congr_ae hfg_restrict.symm,
exact hf_zero s hs hΞΌs, },
end
/-- **Uniqueness of the conditional expectation** -/
lemma Lp.ae_eq_of_forall_set_integral_eq'
(hm : m β€ m0) (f g : Lp E' p ΞΌ) (hp_ne_zero : p β 0) (hp_ne_top : p β β)
(hf_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on f s ΞΌ)
(hg_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on g s ΞΌ)
(hfg : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, f x βΞΌ = β« x in s, g x βΞΌ)
(hf_meas : ae_measurable' m f ΞΌ) (hg_meas : ae_measurable' m g ΞΌ) :
f =α΅[ΞΌ] g :=
begin
suffices h_sub : β(f-g) =α΅[ΞΌ] 0,
by { rw β sub_ae_eq_zero, exact (Lp.coe_fn_sub f g).symm.trans h_sub, },
have hfg' : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, (f - g) x βΞΌ = 0,
{ intros s hs hΞΌs,
rw integral_congr_ae (ae_restrict_of_ae (Lp.coe_fn_sub f g)),
rw integral_sub' (hf_int_finite s hs hΞΌs) (hg_int_finite s hs hΞΌs),
exact sub_eq_zero.mpr (hfg s hs hΞΌs), },
have hfg_int : β s, measurable_set[m] s β ΞΌ s < β β integrable_on β(f-g) s ΞΌ,
{ intros s hs hΞΌs,
rw [integrable_on, integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub f g))],
exact (hf_int_finite s hs hΞΌs).sub (hg_int_finite s hs hΞΌs), },
have hfg_meas : ae_measurable' m β(f - g) ΞΌ,
from ae_measurable'.congr (hf_meas.sub hg_meas) (Lp.coe_fn_sub f g).symm,
exact Lp.ae_eq_zero_of_forall_set_integral_eq_zero' hm (f-g) hp_ne_zero hp_ne_top hfg_int hfg'
hfg_meas,
end
omit π
lemma ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
{f g : Ξ± β F'}
(hf_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on f s ΞΌ)
(hg_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on g s ΞΌ)
(hfg_eq : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, f x βΞΌ = β« x in s, g x βΞΌ)
(hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) :
f =α΅[ΞΌ] g :=
begin
rw β ae_eq_trim_iff_of_ae_measurable' hm hfm hgm,
have hf_mk_int_finite : β s, measurable_set[m] s β ΞΌ.trim hm s < β β
@integrable_on _ _ m _ _ (hfm.mk f) s (ΞΌ.trim hm),
{ intros s hs hΞΌs,
rw trim_measurable_set_eq hm hs at hΞΌs,
rw [integrable_on, restrict_trim hm _ hs],
refine integrable.trim hm _ hfm.measurable_mk,
exact integrable.congr (hf_int_finite s hs hΞΌs) (ae_restrict_of_ae hfm.ae_eq_mk), },
have hg_mk_int_finite : β s, measurable_set[m] s β ΞΌ.trim hm s < β β
@integrable_on _ _ m _ _ (hgm.mk g) s (ΞΌ.trim hm),
{ intros s hs hΞΌs,
rw trim_measurable_set_eq hm hs at hΞΌs,
rw [integrable_on, restrict_trim hm _ hs],
refine integrable.trim hm _ hgm.measurable_mk,
exact integrable.congr (hg_int_finite s hs hΞΌs) (ae_restrict_of_ae hgm.ae_eq_mk), },
have hfg_mk_eq : β s : set Ξ±, measurable_set[m] s β ΞΌ.trim hm s < β β
β« x in s, (hfm.mk f x) β(ΞΌ.trim hm) = β« x in s, (hgm.mk g x) β(ΞΌ.trim hm),
{ intros s hs hΞΌs,
rw trim_measurable_set_eq hm hs at hΞΌs,
rw [restrict_trim hm _ hs, β integral_trim hm hfm.measurable_mk,
β integral_trim hm hgm.measurable_mk, integral_congr_ae (ae_restrict_of_ae hfm.ae_eq_mk.symm),
integral_congr_ae (ae_restrict_of_ae hgm.ae_eq_mk.symm)],
exact hfg_eq s hs hΞΌs, },
exact ae_eq_of_forall_set_integral_eq_of_sigma_finite hf_mk_int_finite hg_mk_int_finite hfg_mk_eq,
end
end uniqueness_of_conditional_expectation
section integral_norm_le
variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {s : set Ξ±}
/-- Let `m` be a sub-Ο-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable
function, such that their integrals coincide on `m`-measurable sets with finite measure.
Then `β« x in s, β₯g xβ₯ βΞΌ β€ β« x in s, β₯f xβ₯ βΞΌ` on all `m`-measurable sets with finite measure. -/
lemma integral_norm_le_of_forall_fin_meas_integral_eq (hm : m β€ m0) {f g : Ξ± β β}
(hf : measurable f) (hfi : integrable_on f s ΞΌ) (hg : measurable[m] g) (hgi : integrable_on g s ΞΌ)
(hgf : β t, measurable_set[m] t β ΞΌ t < β β β« x in t, g x βΞΌ = β« x in t, f x βΞΌ)
(hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) :
β« x in s, β₯g xβ₯ βΞΌ β€ β« x in s, β₯f xβ₯ βΞΌ :=
begin
rw [integral_norm_eq_pos_sub_neg (hg.mono hm le_rfl) hgi, integral_norm_eq_pos_sub_neg hf hfi],
have h_meas_nonneg_g : measurable_set[m] {x | 0 β€ g x},
from @measurable_set_le _ Ξ± _ _ _ m _ _ _ _ g (@measurable_const _ Ξ± _ m _) hg,
have h_meas_nonneg_f : measurable_set {x | 0 β€ f x},
from measurable_set_le measurable_const hf,
have h_meas_nonpos_g : measurable_set[m] {x | g x β€ 0},
from @measurable_set_le _ Ξ± _ _ _ m _ _ _ g _ hg (@measurable_const _ Ξ± _ m _),
have h_meas_nonpos_f : measurable_set {x | f x β€ 0},
from measurable_set_le hf measurable_const,
refine sub_le_sub _ _,
{ rw [measure.restrict_restrict (hm _ h_meas_nonneg_g),
measure.restrict_restrict h_meas_nonneg_f,
hgf _ (@measurable_set.inter Ξ± m _ _ h_meas_nonneg_g hs)
((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hΞΌs)),
β measure.restrict_restrict (hm _ h_meas_nonneg_g),
β measure.restrict_restrict h_meas_nonneg_f],
exact set_integral_le_nonneg (hm _ h_meas_nonneg_g) hf hfi, },
{ rw [measure.restrict_restrict (hm _ h_meas_nonpos_g),
measure.restrict_restrict h_meas_nonpos_f,
hgf _ (@measurable_set.inter Ξ± m _ _ h_meas_nonpos_g hs)
((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hΞΌs)),
β measure.restrict_restrict (hm _ h_meas_nonpos_g),
β measure.restrict_restrict h_meas_nonpos_f],
exact set_integral_nonpos_le (hm _ h_meas_nonpos_g) hf hfi, },
end
/-- Let `m` be a sub-Ο-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable
function, such that their integrals coincide on `m`-measurable sets with finite measure.
Then `β«β» x in s, β₯g xβ₯β βΞΌ β€ β«β» x in s, β₯f xβ₯β βΞΌ` on all `m`-measurable sets with finite
measure. -/
lemma lintegral_nnnorm_le_of_forall_fin_meas_integral_eq (hm : m β€ m0) {f g : Ξ± β β}
(hf : measurable f) (hfi : integrable_on f s ΞΌ) (hg : measurable[m] g) (hgi : integrable_on g s ΞΌ)
(hgf : β t, measurable_set[m] t β ΞΌ t < β β β« x in t, g x βΞΌ = β« x in t, f x βΞΌ)
(hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) :
β«β» x in s, β₯g xβ₯β βΞΌ β€ β«β» x in s, β₯f xβ₯β βΞΌ :=
begin
rw [β of_real_integral_norm_eq_lintegral_nnnorm hfi,
β of_real_integral_norm_eq_lintegral_nnnorm hgi, ennreal.of_real_le_of_real_iff],
{ exact integral_norm_le_of_forall_fin_meas_integral_eq hm hf hfi hg hgi hgf hs hΞΌs, },
{ exact integral_nonneg (Ξ» x, norm_nonneg _), },
end
end integral_norm_le
/-! ## Conditional expectation in L2
We define a conditional expectation in `L2`: it is the orthogonal projection on the subspace
`Lp_meas`. -/
section condexp_L2
variables [complete_space E] {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
{s t : set Ξ±}
local notation `βͺ`x`, `y`β«` := @inner π E _ x y
local notation `βͺ`x`, `y`β«β` := @inner π (Ξ± ββ[ΞΌ] E) _ x y
variables (π)
/-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/
def condexp_L2 (hm : m β€ m0) : (Ξ± ββ[ΞΌ] E) βL[π] (Lp_meas E π m 2 ΞΌ) :=
@orthogonal_projection π (Ξ± ββ[ΞΌ] E) _ _ (Lp_meas E π m 2 ΞΌ)
(by { haveI : fact (m β€ m0) := β¨hmβ©, exact infer_instance, })
variables {π}
lemma ae_measurable'_condexp_L2 (hm : m β€ m0) (f : Ξ± ββ[ΞΌ] E) :
ae_measurable' m (condexp_L2 π hm f) ΞΌ :=
Lp_meas.ae_measurable' _
lemma integrable_on_condexp_L2_of_measure_ne_top (hm : m β€ m0) (hΞΌs : ΞΌ s β β) (f : Ξ± ββ[ΞΌ] E) :
integrable_on (condexp_L2 π hm f) s ΞΌ :=
integrable_on_Lp_of_measure_ne_top ((condexp_L2 π hm f) : Ξ± ββ[ΞΌ] E)
fact_one_le_two_ennreal.elim hΞΌs
lemma integrable_condexp_L2_of_is_finite_measure (hm : m β€ m0) [is_finite_measure ΞΌ]
{f : Ξ± ββ[ΞΌ] E} :
integrable (condexp_L2 π hm f) ΞΌ :=
integrable_on_univ.mp $ integrable_on_condexp_L2_of_measure_ne_top hm (measure_ne_top _ _) f
lemma norm_condexp_L2_le_one (hm : m β€ m0) : β₯@condexp_L2 Ξ± E π _ _ _ _ _ _ _ _ ΞΌ hmβ₯ β€ 1 :=
by { haveI : fact (m β€ m0) := β¨hmβ©, exact orthogonal_projection_norm_le _, }
lemma norm_condexp_L2_le (hm : m β€ m0) (f : Ξ± ββ[ΞΌ] E) : β₯condexp_L2 π hm fβ₯ β€ β₯fβ₯ :=
((@condexp_L2 _ E π _ _ _ _ _ _ _ _ ΞΌ hm).le_op_norm f).trans
(mul_le_of_le_one_left (norm_nonneg _) (norm_condexp_L2_le_one hm))
lemma snorm_condexp_L2_le (hm : m β€ m0) (f : Ξ± ββ[ΞΌ] E) :
snorm (condexp_L2 π hm f) 2 ΞΌ β€ snorm f 2 ΞΌ :=
begin
rw [Lp_meas_coe, β ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _),
β Lp.norm_def, β Lp.norm_def, submodule.norm_coe],
exact norm_condexp_L2_le hm f,
end
lemma norm_condexp_L2_coe_le (hm : m β€ m0) (f : Ξ± ββ[ΞΌ] E) :
β₯(condexp_L2 π hm f : Ξ± ββ[ΞΌ] E)β₯ β€ β₯fβ₯ :=
begin
rw [Lp.norm_def, Lp.norm_def, β Lp_meas_coe],
refine (ennreal.to_real_le_to_real _ (Lp.snorm_ne_top _)).mpr (snorm_condexp_L2_le hm f),
exact Lp.snorm_ne_top _,
end
lemma inner_condexp_L2_left_eq_right (hm : m β€ m0) {f g : Ξ± ββ[ΞΌ] E} :
βͺ(condexp_L2 π hm f : Ξ± ββ[ΞΌ] E), gβ«β = βͺf, (condexp_L2 π hm g : Ξ± ββ[ΞΌ] E)β«β :=
by { haveI : fact (m β€ m0) := β¨hmβ©, exact inner_orthogonal_projection_left_eq_right _ f g, }
lemma condexp_L2_indicator_of_measurable (hm : m β€ m0)
(hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) (c : E) :
(condexp_L2 π hm (indicator_const_Lp 2 (hm s hs) hΞΌs c) : Ξ± ββ[ΞΌ] E)
= indicator_const_Lp 2 (hm s hs) hΞΌs c :=
begin
rw condexp_L2,
haveI : fact (m β€ m0) := β¨hmβ©,
have h_mem : indicator_const_Lp 2 (hm s hs) hΞΌs c β Lp_meas E π m 2 ΞΌ,
from mem_Lp_meas_indicator_const_Lp hm hs hΞΌs,
let ind := (β¨indicator_const_Lp 2 (hm s hs) hΞΌs c, h_memβ© : Lp_meas E π m 2 ΞΌ),
have h_coe_ind : (ind : Ξ± ββ[ΞΌ] E) = indicator_const_Lp 2 (hm s hs) hΞΌs c, by refl,
have h_orth_mem := orthogonal_projection_mem_subspace_eq_self ind,
rw [β h_coe_ind, h_orth_mem],
end
lemma inner_condexp_L2_eq_inner_fun (hm : m β€ m0) (f g : Ξ± ββ[ΞΌ] E) (hg : ae_measurable' m g ΞΌ) :
βͺ(condexp_L2 π hm f : Ξ± ββ[ΞΌ] E), gβ«β = βͺf, gβ«β :=
begin
symmetry,
rw [β sub_eq_zero, β inner_sub_left, condexp_L2],
simp only [mem_Lp_meas_iff_ae_measurable'.mpr hg, orthogonal_projection_inner_eq_zero],
end
section real
variables {hm : m β€ m0}
lemma integral_condexp_L2_eq_of_fin_meas_real (f : Lp π 2 ΞΌ) (hs : measurable_set[m] s)
(hΞΌs : ΞΌ s β β) :
β« x in s, condexp_L2 π hm f x βΞΌ = β« x in s, f x βΞΌ :=
begin
rw β L2.inner_indicator_const_Lp_one (hm s hs) hΞΌs,
have h_eq_inner : β« x in s, condexp_L2 π hm f x βΞΌ
= inner (indicator_const_Lp 2 (hm s hs) hΞΌs (1 : π)) (condexp_L2 π hm f),
{ rw L2.inner_indicator_const_Lp_one (hm s hs) hΞΌs,
congr, },
rw [h_eq_inner, β inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable hm hs hΞΌs],
end
lemma lintegral_nnnorm_condexp_L2_le (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) (f : Lp β 2 ΞΌ) :
β«β» x in s, β₯condexp_L2 β hm f xβ₯β βΞΌ β€ β«β» x in s, β₯f xβ₯β βΞΌ :=
begin
let h_meas := Lp_meas.ae_measurable' (condexp_L2 β hm f),
let g := h_meas.some,
have hg_meas : measurable[m] g, from h_meas.some_spec.1,
have hg_eq : g =α΅[ΞΌ] condexp_L2 β hm f, from h_meas.some_spec.2.symm,
have hg_eq_restrict : g =α΅[ΞΌ.restrict s] condexp_L2 β hm f, from ae_restrict_of_ae hg_eq,
have hg_nnnorm_eq : (Ξ» x, (β₯g xβ₯β : ββ₯0β))
=α΅[ΞΌ.restrict s] (Ξ» x, (β₯condexp_L2 β hm f xβ₯β : ββ₯0β)),
{ refine hg_eq_restrict.mono (Ξ» x hx, _),
dsimp only,
rw hx, },
rw lintegral_congr_ae hg_nnnorm_eq.symm,
refine lintegral_nnnorm_le_of_forall_fin_meas_integral_eq hm (Lp.measurable f) _ _ _ _ hs hΞΌs,
{ exact integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hΞΌs, },
{ exact hg_meas, },
{ rw [integrable_on, integrable_congr hg_eq_restrict],
exact integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs f, },
{ intros t ht hΞΌt,
rw β integral_condexp_L2_eq_of_fin_meas_real f ht hΞΌt.ne,
exact set_integral_congr_ae (hm t ht) (hg_eq.mono (Ξ» x hx _, hx)), },
end
lemma condexp_L2_ae_eq_zero_of_ae_eq_zero (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β)
{f : Lp β 2 ΞΌ} (hf : f =α΅[ΞΌ.restrict s] 0) :
condexp_L2 β hm f =α΅[ΞΌ.restrict s] 0 :=
begin
suffices h_nnnorm_eq_zero : β«β» x in s, β₯condexp_L2 β hm f xβ₯β βΞΌ = 0,
{ rw lintegral_eq_zero_iff at h_nnnorm_eq_zero,
refine h_nnnorm_eq_zero.mono (Ξ» x hx, _),
dsimp only at hx,
rw pi.zero_apply at hx β’,
{ rwa [ennreal.coe_eq_zero, nnnorm_eq_zero] at hx, },
{ refine measurable.coe_nnreal_ennreal (measurable.nnnorm _),
rw Lp_meas_coe,
exact Lp.measurable _, }, },
refine le_antisymm _ (zero_le _),
refine (lintegral_nnnorm_condexp_L2_le hs hΞΌs f).trans (le_of_eq _),
rw lintegral_eq_zero_iff,
{ refine hf.mono (Ξ» x hx, _),
dsimp only,
rw hx,
simp, },
{ exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal, },
end
lemma lintegral_nnnorm_condexp_L2_indicator_le_real
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β β) :
β«β» a in t, β₯condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) aβ₯β βΞΌ β€ ΞΌ (s β© t) :=
begin
refine (lintegral_nnnorm_condexp_L2_le ht hΞΌt _).trans (le_of_eq _),
have h_eq : β«β» x in t, β₯(indicator_const_Lp 2 hs hΞΌs (1 : β)) xβ₯β βΞΌ
= β«β» x in t, s.indicator (Ξ» x, (1 : ββ₯0β)) x βΞΌ,
{ refine lintegral_congr_ae (ae_restrict_of_ae _),
refine (@indicator_const_Lp_coe_fn _ _ _ 2 _ _ _ _ hs hΞΌs (1 : β) _ _).mono (Ξ» x hx, _),
rw hx,
simp_rw set.indicator_apply,
split_ifs; simp, },
rw [h_eq, lintegral_indicator _ hs, lintegral_const, measure.restrict_restrict hs],
simp only [one_mul, set.univ_inter, measurable_set.univ, measure.restrict_apply],
end
end real
/-- `condexp_L2` commutes with taking inner products with constants. See the lemma
`condexp_L2_comp_continuous_linear_map` for a more general result about commuting with continuous
linear maps. -/
lemma condexp_L2_const_inner (hm : m β€ m0) (f : Lp E 2 ΞΌ) (c : E) :
condexp_L2 π hm (((Lp.mem_βp f).const_inner c).to_Lp (Ξ» a, βͺc, f aβ«))
=α΅[ΞΌ] Ξ» a, βͺc, condexp_L2 π hm f aβ« :=
begin
rw Lp_meas_coe,
have h_mem_Lp : mem_βp (Ξ» a, βͺc, condexp_L2 π hm f aβ«) 2 ΞΌ,
{ refine mem_βp.const_inner _ _, rw Lp_meas_coe, exact Lp.mem_βp _, },
have h_eq : h_mem_Lp.to_Lp _ =α΅[ΞΌ] Ξ» a, βͺc, condexp_L2 π hm f aβ«, from h_mem_Lp.coe_fn_to_Lp,
refine eventually_eq.trans _ h_eq,
refine Lp.ae_eq_of_forall_set_integral_eq' hm _ _ ennreal.zero_lt_two.ne.symm ennreal.coe_ne_top
(Ξ» s hs hΞΌs, integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _) _ _ _ _,
{ intros s hs hΞΌs,
rw [integrable_on, integrable_congr (ae_restrict_of_ae h_eq)],
exact (integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _).const_inner _, },
{ intros s hs hΞΌs,
rw [β Lp_meas_coe, integral_condexp_L2_eq_of_fin_meas_real _ hs hΞΌs.ne,
integral_congr_ae (ae_restrict_of_ae h_eq), Lp_meas_coe,
β L2.inner_indicator_const_Lp_eq_set_integral_inner π β(condexp_L2 π hm f) (hm s hs) c hΞΌs.ne,
β inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable,
L2.inner_indicator_const_Lp_eq_set_integral_inner π f (hm s hs) c hΞΌs.ne,
set_integral_congr_ae (hm s hs)
((mem_βp.coe_fn_to_Lp ((Lp.mem_βp f).const_inner c)).mono (Ξ» x hx hxs, hx))], },
{ rw β Lp_meas_coe, exact Lp_meas.ae_measurable' _, },
{ refine ae_measurable'.congr _ h_eq.symm, exact (Lp_meas.ae_measurable' _).const_inner _, },
end
/-- `condexp_L2` verifies the equality of integrals defining the conditional expectation. -/
lemma integral_condexp_L2_eq (hm : m β€ m0)
(f : Lp E' 2 ΞΌ) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) :
β« x in s, condexp_L2 π hm f x βΞΌ = β« x in s, f x βΞΌ :=
begin
rw [β sub_eq_zero, Lp_meas_coe, β integral_sub'
(integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs)
(integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs)],
refine integral_eq_zero_of_forall_integral_inner_eq_zero _ _ _,
{ rw integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub β(condexp_L2 π hm f) f).symm),
exact integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs, },
intro c,
simp_rw [pi.sub_apply, inner_sub_right],
rw integral_sub
((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs).const_inner c)
((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs).const_inner c),
have h_ae_eq_f := mem_βp.coe_fn_to_Lp ((Lp.mem_βp f).const_inner c),
rw [β Lp_meas_coe, sub_eq_zero,
β set_integral_congr_ae (hm s hs) ((condexp_L2_const_inner hm f c).mono (Ξ» x hx _, hx)),
β set_integral_congr_ae (hm s hs) (h_ae_eq_f.mono (Ξ» x hx _, hx))],
exact integral_condexp_L2_eq_of_fin_meas_real _ hs hΞΌs,
end
variables {E'' π' : Type*} [is_R_or_C π']
[measurable_space E''] [inner_product_space π' E''] [borel_space E'']
[second_countable_topology E''] [complete_space E''] [normed_space β E'']
variables (π π')
lemma condexp_L2_comp_continuous_linear_map (hm : m β€ m0) (T : E' βL[β] E'') (f : Ξ± ββ[ΞΌ] E') :
(condexp_L2 π' hm (T.comp_Lp f) : Ξ± ββ[ΞΌ] E'') =α΅[ΞΌ] T.comp_Lp (condexp_L2 π hm f : Ξ± ββ[ΞΌ] E') :=
begin
refine Lp.ae_eq_of_forall_set_integral_eq' hm _ _ ennreal.zero_lt_two.ne.symm ennreal.coe_ne_top
(Ξ» s hs hΞΌs, integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _)
(Ξ» s hs hΞΌs, integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs.ne)
_ _ _,
{ intros s hs hΞΌs,
rw [T.set_integral_comp_Lp _ (hm s hs),
T.integral_comp_comm
(integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs.ne),
β Lp_meas_coe, β Lp_meas_coe, integral_condexp_L2_eq hm f hs hΞΌs.ne,
integral_condexp_L2_eq hm (T.comp_Lp f) hs hΞΌs.ne, T.set_integral_comp_Lp _ (hm s hs),
T.integral_comp_comm
(integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hΞΌs.ne)], },
{ rw β Lp_meas_coe, exact Lp_meas.ae_measurable' _, },
{ have h_coe := T.coe_fn_comp_Lp (condexp_L2 π hm f : Ξ± ββ[ΞΌ] E'),
rw β eventually_eq at h_coe,
refine ae_measurable'.congr _ h_coe.symm,
exact (Lp_meas.ae_measurable' (condexp_L2 π hm f)).measurable_comp T.measurable, },
end
variables {π π'}
section condexp_L2_indicator
variables (π)
lemma condexp_L2_indicator_ae_eq_smul (hm : m β€ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β β)
(x : E') :
condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x)
=α΅[ΞΌ] Ξ» a, (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) a) β’ x :=
begin
rw indicator_const_Lp_eq_to_span_singleton_comp_Lp hs hΞΌs x,
have h_comp := condexp_L2_comp_continuous_linear_map β π hm (to_span_singleton β x)
(indicator_const_Lp 2 hs hΞΌs (1 : β)),
rw β Lp_meas_coe at h_comp,
refine h_comp.trans _,
exact (to_span_singleton β x).coe_fn_comp_Lp _,
end
lemma condexp_L2_indicator_eq_to_span_singleton_comp (hm : m β€ m0) (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : E') :
(condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x) : Ξ± ββ[ΞΌ] E')
= (to_span_singleton β x).comp_Lp (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β))) :=
begin
ext1,
rw β Lp_meas_coe,
refine (condexp_L2_indicator_ae_eq_smul π hm hs hΞΌs x).trans _,
have h_comp := (to_span_singleton β x).coe_fn_comp_Lp
(condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) : Ξ± ββ[ΞΌ] β),
rw β eventually_eq at h_comp,
refine eventually_eq.trans _ h_comp.symm,
refine eventually_of_forall (Ξ» y, _),
refl,
end
variables {π}
lemma set_lintegral_nnnorm_condexp_L2_indicator_le (hm : m β€ m0) (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : E') {t : set Ξ±} (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β β) :
β«β» a in t, β₯condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x) aβ₯β βΞΌ β€ ΞΌ (s β© t) * β₯xβ₯β :=
calc β«β» a in t, β₯condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x) aβ₯β βΞΌ
= β«β» a in t, β₯(condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) a) β’ xβ₯β βΞΌ :
set_lintegral_congr_fun (hm t ht)
((condexp_L2_indicator_ae_eq_smul π hm hs hΞΌs x).mono (Ξ» a ha hat, by rw ha))
... = β«β» a in t, β₯condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) aβ₯β βΞΌ * β₯xβ₯β :
begin
simp_rw [nnnorm_smul, ennreal.coe_mul],
rw [lintegral_mul_const, Lp_meas_coe],
exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal,
end
... β€ ΞΌ (s β© t) * β₯xβ₯β :
ennreal.mul_le_mul (lintegral_nnnorm_condexp_L2_indicator_le_real hs hΞΌs ht hΞΌt) le_rfl
lemma lintegral_nnnorm_condexp_L2_indicator_le (hm : m β€ m0) (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : E') [sigma_finite (ΞΌ.trim hm)] :
β«β» a, β₯condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x) aβ₯β βΞΌ β€ ΞΌ s * β₯xβ₯β :=
begin
refine lintegral_le_of_forall_fin_meas_le' hm (ΞΌ s * β₯xβ₯β) _ (Ξ» t ht hΞΌt, _),
{ rw Lp_meas_coe,
exact (Lp.ae_measurable _).nnnorm.coe_nnreal_ennreal, },
refine (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hΞΌs x ht hΞΌt).trans _,
refine ennreal.mul_le_mul _ le_rfl,
exact measure_mono (set.inter_subset_left _ _),
end
/-- If the measure `ΞΌ.trim hm` is sigma-finite, then the conditional expectation of a measurable set
with finite measure is integrable. -/
lemma integrable_condexp_L2_indicator (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : E') :
integrable (condexp_L2 π hm (indicator_const_Lp 2 hs hΞΌs x)) ΞΌ :=
begin
refine integrable_of_forall_fin_meas_le' hm (ΞΌ s * β₯xβ₯β)
(ennreal.mul_lt_top hΞΌs ennreal.coe_ne_top) _ _,
{ rw Lp_meas_coe, exact Lp.ae_measurable _, },
{ refine Ξ» t ht hΞΌt, (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hΞΌs x ht hΞΌt).trans _,
exact ennreal.mul_le_mul (measure_mono (set.inter_subset_left _ _)) le_rfl, },
end
end condexp_L2_indicator
section condexp_ind_smul
variables [normed_space β G] {hm : m β€ m0}
/-- Conditional expectation of the indicator of a measurable set with finite measure, in L2. -/
def condexp_ind_smul (hm : m β€ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) : Lp G 2 ΞΌ :=
(to_span_singleton β x).comp_LpL 2 ΞΌ (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)))
lemma ae_measurable'_condexp_ind_smul (hm : m β€ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β β)
(x : G) :
ae_measurable' m (condexp_ind_smul hm hs hΞΌs x) ΞΌ :=
begin
have h : ae_measurable' m (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β))) ΞΌ,
from ae_measurable'_condexp_L2 _ _,
rw condexp_ind_smul,
suffices : ae_measurable' m
((to_span_singleton β x) β (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)))) ΞΌ,
{ refine ae_measurable'.congr this _,
refine eventually_eq.trans _ (coe_fn_comp_LpL _ _).symm,
rw Lp_meas_coe, },
exact ae_measurable'.measurable_comp (to_span_singleton β x).measurable h,
end
lemma condexp_ind_smul_add (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x y : G) :
condexp_ind_smul hm hs hΞΌs (x + y)
= condexp_ind_smul hm hs hΞΌs x + condexp_ind_smul hm hs hΞΌs y :=
by { simp_rw [condexp_ind_smul], rw [to_span_singleton_add, add_comp_LpL, add_apply], }
lemma condexp_ind_smul_smul (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (c : β) (x : G) :
condexp_ind_smul hm hs hΞΌs (c β’ x) = c β’ condexp_ind_smul hm hs hΞΌs x :=
by { simp_rw [condexp_ind_smul], rw [to_span_singleton_smul, smul_comp_LpL, smul_apply], }
lemma condexp_ind_smul_smul' [normed_space β F] [smul_comm_class β π F] (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (c : π) (x : F) :
condexp_ind_smul hm hs hΞΌs (c β’ x) = c β’ condexp_ind_smul hm hs hΞΌs x :=
by rw [condexp_ind_smul, condexp_ind_smul, to_span_singleton_smul',
(to_span_singleton β x).smul_comp_LpL_apply c
β(condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)))]
lemma condexp_ind_smul_ae_eq_smul (hm : m β€ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
condexp_ind_smul hm hs hΞΌs x
=α΅[ΞΌ] Ξ» a, (condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) a) β’ x :=
(to_span_singleton β x).coe_fn_comp_LpL _
lemma set_lintegral_nnnorm_condexp_ind_smul_le (hm : m β€ m0) (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : G) {t : set Ξ±} (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β β) :
β«β» a in t, β₯condexp_ind_smul hm hs hΞΌs x aβ₯β βΞΌ β€ ΞΌ (s β© t) * β₯xβ₯β :=
calc β«β» a in t, β₯condexp_ind_smul hm hs hΞΌs x aβ₯β βΞΌ
= β«β» a in t, β₯condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) a β’ xβ₯β βΞΌ :
set_lintegral_congr_fun (hm t ht)
((condexp_ind_smul_ae_eq_smul hm hs hΞΌs x).mono (Ξ» a ha hat, by rw ha ))
... = β«β» a in t, β₯condexp_L2 β hm (indicator_const_Lp 2 hs hΞΌs (1 : β)) aβ₯β βΞΌ * β₯xβ₯β :
begin
simp_rw [nnnorm_smul, ennreal.coe_mul],
rw [lintegral_mul_const, Lp_meas_coe],
exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal,
end
... β€ ΞΌ (s β© t) * β₯xβ₯β :
ennreal.mul_le_mul (lintegral_nnnorm_condexp_L2_indicator_le_real hs hΞΌs ht hΞΌt) le_rfl
lemma lintegral_nnnorm_condexp_ind_smul_le (hm : m β€ m0) (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : G) [sigma_finite (ΞΌ.trim hm)] :
β«β» a, β₯condexp_ind_smul hm hs hΞΌs x aβ₯β βΞΌ β€ ΞΌ s * β₯xβ₯β :=
begin
refine lintegral_le_of_forall_fin_meas_le' hm (ΞΌ s * β₯xβ₯β) _ (Ξ» t ht hΞΌt, _),
{ exact (Lp.ae_measurable _).nnnorm.coe_nnreal_ennreal, },
refine (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x ht hΞΌt).trans _,
refine ennreal.mul_le_mul _ le_rfl,
exact measure_mono (set.inter_subset_left _ _),
end
/-- If the measure `ΞΌ.trim hm` is sigma-finite, then the conditional expectation of a measurable set
with finite measure is integrable. -/
lemma integrable_condexp_ind_smul (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
integrable (condexp_ind_smul hm hs hΞΌs x) ΞΌ :=
begin
refine integrable_of_forall_fin_meas_le' hm (ΞΌ s * β₯xβ₯β)
(ennreal.mul_lt_top hΞΌs ennreal.coe_ne_top) _ _,
{ exact Lp.ae_measurable _, },
{ refine Ξ» t ht hΞΌt, (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x ht hΞΌt).trans _,
exact ennreal.mul_le_mul (measure_mono (set.inter_subset_left _ _)) le_rfl, },
end
lemma condexp_ind_smul_empty {x : G} :
condexp_ind_smul hm measurable_set.empty
((@measure_empty _ _ ΞΌ).le.trans_lt ennreal.coe_lt_top).ne x = 0 :=
begin
rw [condexp_ind_smul, indicator_const_empty],
simp only [coe_fn_coe_base, submodule.coe_zero, continuous_linear_map.map_zero],
end
lemma set_integral_condexp_ind_smul (hs : measurable_set[m] s) (ht : measurable_set t)
(hΞΌs : ΞΌ s β β) (hΞΌt : ΞΌ t β β) (x : G') :
β« a in s, (condexp_ind_smul hm ht hΞΌt x) a βΞΌ = (ΞΌ (t β© s)).to_real β’ x :=
calc β« a in s, (condexp_ind_smul hm ht hΞΌt x) a βΞΌ
= (β« a in s, (condexp_L2 β hm (indicator_const_Lp 2 ht hΞΌt (1 : β)) a β’ x) βΞΌ) :
set_integral_congr_ae (hm s hs) ((condexp_ind_smul_ae_eq_smul hm ht hΞΌt x).mono (Ξ» x hx hxs, hx))
... = (β« a in s, condexp_L2 β hm (indicator_const_Lp 2 ht hΞΌt (1 : β)) a βΞΌ) β’ x :
integral_smul_const _ x
... = (β« a in s, indicator_const_Lp 2 ht hΞΌt (1 : β) a βΞΌ) β’ x :
by rw @integral_condexp_L2_eq Ξ± _ β _ _ _ _ _ _ _ _ _ _ _ hm
(indicator_const_Lp 2 ht hΞΌt (1 : β)) hs hΞΌs
... = (ΞΌ (t β© s)).to_real β’ x :
by rw [set_integral_indicator_const_Lp (hm s hs), smul_assoc, one_smul]
end condexp_ind_smul
end condexp_L2
section condexp_ind
/-! ## Conditional expectation of an indicator as a continuous linear map.
The goal of this section is to build
`condexp_ind (hm : m β€ m0) (ΞΌ : measure Ξ±) (s : set s) : G βL[β] Ξ± ββ[ΞΌ] G`, which
takes `x : G` to the conditional expectation of the indicator of the set `s` with value `x`,
seen as an element of `Ξ± ββ[ΞΌ] G`.
-/
variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {s t : set Ξ±} [normed_space β G]
section condexp_ind_L1_fin
/-- Conditional expectation of the indicator of a measurable set with finite measure,
as a function in L1. -/
def condexp_ind_L1_fin (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s)
(hΞΌs : ΞΌ s β β) (x : G) : Ξ± ββ[ΞΌ] G :=
(integrable_condexp_ind_smul hm hs hΞΌs x).to_L1 _
lemma condexp_ind_L1_fin_ae_eq_condexp_ind_smul (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
condexp_ind_L1_fin hm hs hΞΌs x =α΅[ΞΌ] condexp_ind_smul hm hs hΞΌs x :=
(integrable_condexp_ind_smul hm hs hΞΌs x).coe_fn_to_L1
variables {hm : m β€ m0} [sigma_finite (ΞΌ.trim hm)]
lemma condexp_ind_L1_fin_add (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x y : G) :
condexp_ind_L1_fin hm hs hΞΌs (x + y)
= condexp_ind_L1_fin hm hs hΞΌs x + condexp_ind_L1_fin hm hs hΞΌs y :=
begin
ext1,
refine (mem_βp.coe_fn_to_Lp _).trans _,
refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm,
refine eventually_eq.trans _
(eventually_eq.add (mem_βp.coe_fn_to_Lp _).symm (mem_βp.coe_fn_to_Lp _).symm),
rw condexp_ind_smul_add,
refine (Lp.coe_fn_add _ _).trans (eventually_of_forall (Ξ» a, _)),
refl,
end
lemma condexp_ind_L1_fin_smul (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (c : β) (x : G) :
condexp_ind_L1_fin hm hs hΞΌs (c β’ x) = c β’ condexp_ind_L1_fin hm hs hΞΌs x :=
begin
ext1,
refine (mem_βp.coe_fn_to_Lp _).trans _,
refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm,
rw condexp_ind_smul_smul hs hΞΌs c x,
refine (Lp.coe_fn_smul _ _).trans _,
refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» y hy, _),
rw [pi.smul_apply, pi.smul_apply, hy],
end
lemma condexp_ind_L1_fin_smul' [normed_space β F] [smul_comm_class β π F]
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (c : π) (x : F) :
condexp_ind_L1_fin hm hs hΞΌs (c β’ x) = c β’ condexp_ind_L1_fin hm hs hΞΌs x :=
begin
ext1,
refine (mem_βp.coe_fn_to_Lp _).trans _,
refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm,
rw condexp_ind_smul_smul' hs hΞΌs c x,
refine (Lp.coe_fn_smul _ _).trans _,
refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» y hy, _),
rw [pi.smul_apply, pi.smul_apply, hy],
end
lemma norm_condexp_ind_L1_fin_le (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
β₯condexp_ind_L1_fin hm hs hΞΌs xβ₯ β€ (ΞΌ s).to_real * β₯xβ₯ :=
begin
have : 0 β€ β« (a : Ξ±), β₯condexp_ind_L1_fin hm hs hΞΌs x aβ₯ βΞΌ,
from integral_nonneg (Ξ» a, norm_nonneg _),
rw [L1.norm_eq_integral_norm, β ennreal.to_real_of_real (norm_nonneg x), β ennreal.to_real_mul,
β ennreal.to_real_of_real this, ennreal.to_real_le_to_real ennreal.of_real_ne_top
(ennreal.mul_ne_top hΞΌs ennreal.of_real_ne_top),
of_real_integral_norm_eq_lintegral_nnnorm],
swap, { rw [β mem_βp_one_iff_integrable], exact Lp.mem_βp _, },
have h_eq : β«β» a, β₯condexp_ind_L1_fin hm hs hΞΌs x aβ₯β βΞΌ
= β«β» a, nnnorm (condexp_ind_smul hm hs hΞΌs x a) βΞΌ,
{ refine lintegral_congr_ae _,
refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» z hz, _),
dsimp only,
rw hz, },
rw [h_eq, of_real_norm_eq_coe_nnnorm],
exact lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x,
end
lemma condexp_ind_L1_fin_disjoint_union (hs : measurable_set s) (ht : measurable_set t)
(hΞΌs : ΞΌ s β β) (hΞΌt : ΞΌ t β β) (hst : s β© t = β
) (x : G) :
condexp_ind_L1_fin hm (hs.union ht) ((measure_union_le s t).trans_lt
(lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr β¨hΞΌs, hΞΌtβ©))).ne x
= condexp_ind_L1_fin hm hs hΞΌs x + condexp_ind_L1_fin hm ht hΞΌt x :=
begin
ext1,
have hΞΌst := ((measure_union_le s t).trans_lt
(lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr β¨hΞΌs, hΞΌtβ©))).ne,
refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm (hs.union ht) hΞΌst x).trans _,
refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm,
have hs_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x,
have ht_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm ht hΞΌt x,
refine eventually_eq.trans _ (eventually_eq.add hs_eq.symm ht_eq.symm),
rw condexp_ind_smul,
rw indicator_const_Lp_disjoint_union hs ht hΞΌs hΞΌt hst (1 : β),
rw (condexp_L2 β hm).map_add,
push_cast,
rw ((to_span_singleton β x).comp_LpL 2 ΞΌ).map_add,
refine (Lp.coe_fn_add _ _).trans _,
refine eventually_of_forall (Ξ» y, _),
refl,
end
end condexp_ind_L1_fin
open_locale classical
section condexp_ind_L1
/-- Conditional expectation of the indicator of a set, as a function in L1. Its value for sets
which are not both measurable and of finite measure is not used: we set it to 0. -/
def condexp_ind_L1 {m m0 : measurable_space Ξ±} (hm : m β€ m0) (ΞΌ : measure Ξ±) (s : set Ξ±)
[sigma_finite (ΞΌ.trim hm)] (x : G) :
Ξ± ββ[ΞΌ] G :=
if hs : measurable_set s β§ ΞΌ s β β then condexp_ind_L1_fin hm hs.1 hs.2 x else 0
variables {hm : m β€ m0} [sigma_finite (ΞΌ.trim hm)]
lemma condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs : measurable_set s) (hΞΌs : ΞΌ s β β)
(x : G) :
condexp_ind_L1 hm ΞΌ s x = condexp_ind_L1_fin hm hs hΞΌs x :=
by simp only [condexp_ind_L1, and.intro hs hΞΌs, dif_pos, ne.def, not_false_iff, and_self]
lemma condexp_ind_L1_of_measure_eq_top (hΞΌs : ΞΌ s = β) (x : G) :
condexp_ind_L1 hm ΞΌ s x = 0 :=
by simp only [condexp_ind_L1, hΞΌs, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff,
and_false]
lemma condexp_ind_L1_of_not_measurable_set (hs : Β¬ measurable_set s) (x : G) :
condexp_ind_L1 hm ΞΌ s x = 0 :=
by simp only [condexp_ind_L1, hs, dif_neg, not_false_iff, false_and]
lemma condexp_ind_L1_add (x y : G) :
condexp_ind_L1 hm ΞΌ s (x + y) = condexp_ind_L1 hm ΞΌ s x + condexp_ind_L1 hm ΞΌ s y :=
begin
by_cases hs : measurable_set s,
swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw zero_add, },
by_cases hΞΌs : ΞΌ s = β,
{ simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw zero_add, },
{ simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs,
exact condexp_ind_L1_fin_add hs hΞΌs x y, },
end
lemma condexp_ind_L1_smul (c : β) (x : G) :
condexp_ind_L1 hm ΞΌ s (c β’ x) = c β’ condexp_ind_L1 hm ΞΌ s x :=
begin
by_cases hs : measurable_set s,
swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, },
by_cases hΞΌs : ΞΌ s = β,
{ simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw smul_zero, },
{ simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs,
exact condexp_ind_L1_fin_smul hs hΞΌs c x, },
end
lemma condexp_ind_L1_smul' [normed_space β F] [smul_comm_class β π F] (c : π) (x : F) :
condexp_ind_L1 hm ΞΌ s (c β’ x) = c β’ condexp_ind_L1 hm ΞΌ s x :=
begin
by_cases hs : measurable_set s,
swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, },
by_cases hΞΌs : ΞΌ s = β,
{ simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw smul_zero, },
{ simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs,
exact condexp_ind_L1_fin_smul' hs hΞΌs c x, },
end
lemma norm_condexp_ind_L1_le (x : G) :
β₯condexp_ind_L1 hm ΞΌ s xβ₯ β€ (ΞΌ s).to_real * β₯xβ₯ :=
begin
by_cases hs : measurable_set s,
swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw Lp.norm_zero,
exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), },
by_cases hΞΌs : ΞΌ s = β,
{ rw [condexp_ind_L1_of_measure_eq_top hΞΌs x, Lp.norm_zero],
exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), },
{ rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs x,
exact norm_condexp_ind_L1_fin_le hs hΞΌs x, },
end
lemma continuous_condexp_ind_L1 : continuous (Ξ» x : G, condexp_ind_L1 hm ΞΌ s x) :=
continuous_of_linear_of_bound condexp_ind_L1_add condexp_ind_L1_smul norm_condexp_ind_L1_le
lemma condexp_ind_L1_disjoint_union (hs : measurable_set s) (ht : measurable_set t)
(hΞΌs : ΞΌ s β β) (hΞΌt : ΞΌ t β β) (hst : s β© t = β
) (x : G) :
condexp_ind_L1 hm ΞΌ (s βͺ t) x = condexp_ind_L1 hm ΞΌ s x + condexp_ind_L1 hm ΞΌ t x :=
begin
have hΞΌst : ΞΌ (s βͺ t) β β, from ((measure_union_le s t).trans_lt
(lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr β¨hΞΌs, hΞΌtβ©))).ne,
rw [condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs x,
condexp_ind_L1_of_measurable_set_of_measure_ne_top ht hΞΌt x,
condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs.union ht) hΞΌst x],
exact condexp_ind_L1_fin_disjoint_union hs ht hΞΌs hΞΌt hst x,
end
end condexp_ind_L1
/-- Conditional expectation of the indicator of a set, as a linear map from `G` to L1. -/
def condexp_ind {m m0 : measurable_space Ξ±} (hm : m β€ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)]
(s : set Ξ±) : G βL[β] Ξ± ββ[ΞΌ] G :=
{ to_fun := condexp_ind_L1 hm ΞΌ s,
map_add' := condexp_ind_L1_add,
map_smul' := condexp_ind_L1_smul,
cont := continuous_condexp_ind_L1, }
lemma condexp_ind_ae_eq_condexp_ind_smul (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
(hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
condexp_ind hm ΞΌ s x =α΅[ΞΌ] condexp_ind_smul hm hs hΞΌs x :=
begin
refine eventually_eq.trans _ (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x),
simp [condexp_ind, condexp_ind_L1, hs, hΞΌs],
end
variables {hm : m β€ m0} [sigma_finite (ΞΌ.trim hm)]
lemma ae_measurable'_condexp_ind (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : G) :
ae_measurable' m (condexp_ind hm ΞΌ s x) ΞΌ :=
ae_measurable'.congr (ae_measurable'_condexp_ind_smul hm hs hΞΌs x)
(condexp_ind_ae_eq_condexp_ind_smul hm hs hΞΌs x).symm
@[simp] lemma condexp_ind_empty : condexp_ind hm ΞΌ β
= (0 : G βL[β] Ξ± ββ[ΞΌ] G) :=
begin
ext1,
ext1,
refine (condexp_ind_ae_eq_condexp_ind_smul hm measurable_set.empty (by simp) x).trans _,
rw condexp_ind_smul_empty,
refine (Lp.coe_fn_zero G 2 ΞΌ).trans _,
refine eventually_eq.trans _ (Lp.coe_fn_zero G 1 ΞΌ).symm,
refl,
end
lemma condexp_ind_smul' [normed_space β F] [smul_comm_class β π F] (c : π) (x : F) :
condexp_ind hm ΞΌ s (c β’ x) = c β’ condexp_ind hm ΞΌ s x :=
condexp_ind_L1_smul' c x
lemma norm_condexp_ind_apply_le (x : G) : β₯condexp_ind hm ΞΌ s xβ₯ β€ (ΞΌ s).to_real * β₯xβ₯ :=
norm_condexp_ind_L1_le x
lemma norm_condexp_ind_le : β₯(condexp_ind hm ΞΌ s : G βL[β] Ξ± ββ[ΞΌ] G)β₯ β€ (ΞΌ s).to_real :=
continuous_linear_map.op_norm_le_bound _ ennreal.to_real_nonneg norm_condexp_ind_apply_le
lemma condexp_ind_disjoint_union_apply (hs : measurable_set s) (ht : measurable_set t)
(hΞΌs : ΞΌ s β β) (hΞΌt : ΞΌ t β β) (hst : s β© t = β
) (x : G) :
condexp_ind hm ΞΌ (s βͺ t) x = condexp_ind hm ΞΌ s x + condexp_ind hm ΞΌ t x :=
condexp_ind_L1_disjoint_union hs ht hΞΌs hΞΌt hst x
lemma condexp_ind_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hΞΌs : ΞΌ s β β)
(hΞΌt : ΞΌ t β β) (hst : s β© t = β
) :
(condexp_ind hm ΞΌ (s βͺ t) : G βL[β] Ξ± ββ[ΞΌ] G) = condexp_ind hm ΞΌ s + condexp_ind hm ΞΌ t :=
by { ext1, push_cast, exact condexp_ind_disjoint_union_apply hs ht hΞΌs hΞΌt hst x, }
variables (G)
lemma dominated_fin_meas_additive_condexp_ind (hm : m β€ m0) (ΞΌ : measure Ξ±)
[sigma_finite (ΞΌ.trim hm)] :
dominated_fin_meas_additive ΞΌ (condexp_ind hm ΞΌ : set Ξ± β G βL[β] Ξ± ββ[ΞΌ] G) 1 :=
β¨Ξ» s t, condexp_ind_disjoint_union, Ξ» s _ _, norm_condexp_ind_le.trans (one_mul _).symm.leβ©
variables {G}
lemma set_integral_condexp_ind (hs : measurable_set[m] s) (ht : measurable_set t) (hΞΌs : ΞΌ s β β)
(hΞΌt : ΞΌ t β β) (x : G') :
β« a in s, condexp_ind hm ΞΌ t x a βΞΌ = (ΞΌ (t β© s)).to_real β’ x :=
calc
β« a in s, condexp_ind hm ΞΌ t x a βΞΌ = β« a in s, condexp_ind_smul hm ht hΞΌt x a βΞΌ :
set_integral_congr_ae (hm s hs)
((condexp_ind_ae_eq_condexp_ind_smul hm ht hΞΌt x).mono (Ξ» x hx hxs, hx))
... = (ΞΌ (t β© s)).to_real β’ x : set_integral_condexp_ind_smul hs ht hΞΌs hΞΌt x
lemma condexp_ind_of_measurable (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β β) (c : G) :
condexp_ind hm ΞΌ s c = indicator_const_Lp 1 (hm s hs) hΞΌs c :=
begin
ext1,
refine eventually_eq.trans _ indicator_const_Lp_coe_fn.symm,
refine (condexp_ind_ae_eq_condexp_ind_smul hm (hm s hs) hΞΌs c).trans _,
refine (condexp_ind_smul_ae_eq_smul hm (hm s hs) hΞΌs c).trans _,
rw [Lp_meas_coe, condexp_L2_indicator_of_measurable hm hs hΞΌs (1 : β)],
refine (@indicator_const_Lp_coe_fn Ξ± _ _ 2 ΞΌ _ _ s (hm s hs) hΞΌs (1 : β) _ _).mono (Ξ» x hx, _),
dsimp only,
rw hx,
by_cases hx_mem : x β s; simp [hx_mem],
end
end condexp_ind
section condexp_L1
variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
{hm : m β€ m0} [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β F'} {s : set Ξ±}
/-- Conditional expectation of a function as a linear map from `Ξ± ββ[ΞΌ] F'` to itself. -/
def condexp_L1_clm (hm : m β€ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] :
(Ξ± ββ[ΞΌ] F') βL[β] Ξ± ββ[ΞΌ] F' :=
L1.set_to_L1 (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ)
lemma condexp_L1_clm_smul (c : π) (f : Ξ± ββ[ΞΌ] F') :
condexp_L1_clm hm ΞΌ (c β’ f) = c β’ condexp_L1_clm hm ΞΌ f :=
L1.set_to_L1_smul (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ)
(Ξ» c s x, condexp_ind_smul' c x) c f
lemma condexp_L1_clm_indicator_const_Lp (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : F') :
(condexp_L1_clm hm ΞΌ) (indicator_const_Lp 1 hs hΞΌs x) = condexp_ind hm ΞΌ s x :=
L1.set_to_L1_indicator_const_Lp (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hs hΞΌs x
lemma condexp_L1_clm_indicator_const (hs : measurable_set s) (hΞΌs : ΞΌ s β β) (x : F') :
(condexp_L1_clm hm ΞΌ) β(simple_func.indicator_const 1 hs hΞΌs x) = condexp_ind hm ΞΌ s x :=
by { rw Lp.simple_func.coe_indicator_const, exact condexp_L1_clm_indicator_const_Lp hs hΞΌs x, }
/-- Auxiliary lemma used in the proof of `set_integral_condexp_L1_clm`. -/
lemma set_integral_condexp_L1_clm_of_measure_ne_top (f : Ξ± ββ[ΞΌ] F') (hs : measurable_set[m] s)
(hΞΌs : ΞΌ s β β) :
β« x in s, condexp_L1_clm hm ΞΌ f x βΞΌ = β« x in s, f x βΞΌ :=
begin
refine Lp.induction ennreal.one_ne_top
(Ξ» f : Ξ± ββ[ΞΌ] F', β« x in s, condexp_L1_clm hm ΞΌ f x βΞΌ = β« x in s, f x βΞΌ)
_ _ (is_closed_eq _ _) f,
{ intros x t ht hΞΌt,
simp_rw condexp_L1_clm_indicator_const ht hΞΌt.ne x,
rw [Lp.simple_func.coe_indicator_const, set_integral_indicator_const_Lp (hm _ hs)],
exact set_integral_condexp_ind hs ht hΞΌs hΞΌt.ne x, },
{ intros f g hf_Lp hg_Lp hfg_disj hf hg,
simp_rw (condexp_L1_clm hm ΞΌ).map_add,
rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (condexp_L1_clm hm ΞΌ (hf_Lp.to_Lp f))
(condexp_L1_clm hm ΞΌ (hg_Lp.to_Lp g))).mono (Ξ» x hx hxs, hx)),
rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (hf_Lp.to_Lp f) (hg_Lp.to_Lp g)).mono
(Ξ» x hx hxs, hx)),
simp_rw pi.add_apply,
rw [integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on,
integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on,
hf, hg], },
{ exact (continuous_set_integral s).comp (condexp_L1_clm hm ΞΌ).continuous, },
{ exact continuous_set_integral s, },
end
/-- The integral of the conditional expectation `condexp_L1_clm` over an `m`-measurable set is equal
to the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for
`condexp`. -/
lemma set_integral_condexp_L1_clm (f : Ξ± ββ[ΞΌ] F') (hs : measurable_set[m] s) :
β« x in s, condexp_L1_clm hm ΞΌ f x βΞΌ = β« x in s, f x βΞΌ :=
begin
let S := spanning_sets (ΞΌ.trim hm),
have hS_meas : β i, measurable_set[m] (S i) := measurable_spanning_sets (ΞΌ.trim hm),
have hS_meas0 : β i, measurable_set (S i) := Ξ» i, hm _ (hS_meas i),
have hs_eq : s = β i, S i β© s,
{ simp_rw set.inter_comm,
rw [β set.inter_Union, (Union_spanning_sets (ΞΌ.trim hm)), set.inter_univ], },
have hS_finite : β i, ΞΌ (S i β© s) < β,
{ refine Ξ» i, (measure_mono (set.inter_subset_left _ _)).trans_lt _,
have hS_finite_trim := measure_spanning_sets_lt_top (ΞΌ.trim hm) i,
rwa trim_measurable_set_eq hm (hS_meas i) at hS_finite_trim, },
have h_mono : monotone (Ξ» i, (S i) β© s),
{ intros i j hij x,
simp_rw set.mem_inter_iff,
exact Ξ» h, β¨monotone_spanning_sets (ΞΌ.trim hm) hij h.1, h.2β©, },
have h_eq_forall : (Ξ» i, β« x in (S i) β© s, condexp_L1_clm hm ΞΌ f x βΞΌ)
= Ξ» i, β« x in (S i) β© s, f x βΞΌ,
from funext (Ξ» i, set_integral_condexp_L1_clm_of_measure_ne_top f
(@measurable_set.inter Ξ± m _ _ (hS_meas i) hs) (hS_finite i).ne),
have h_right : tendsto (Ξ» i, β« x in (S i) β© s, f x βΞΌ) at_top (π (β« x in s, f x βΞΌ)),
{ have h := tendsto_set_integral_of_monotone (Ξ» i, (hS_meas0 i).inter (hm s hs)) h_mono
(L1.integrable_coe_fn f).integrable_on,
rwa β hs_eq at h, },
have h_left : tendsto (Ξ» i, β« x in (S i) β© s, condexp_L1_clm hm ΞΌ f x βΞΌ) at_top
(π (β« x in s, condexp_L1_clm hm ΞΌ f x βΞΌ)),
{ have h := tendsto_set_integral_of_monotone (Ξ» i, (hS_meas0 i).inter (hm s hs))
h_mono (L1.integrable_coe_fn (condexp_L1_clm hm ΞΌ f)).integrable_on,
rwa β hs_eq at h, },
rw h_eq_forall at h_left,
exact tendsto_nhds_unique h_left h_right,
end
lemma ae_measurable'_condexp_L1_clm (f : Ξ± ββ[ΞΌ] F') : ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ :=
begin
refine Lp.induction ennreal.one_ne_top
(Ξ» f : Ξ± ββ[ΞΌ] F', ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ)
_ _ _ f,
{ intros c s hs hΞΌs,
rw condexp_L1_clm_indicator_const hs hΞΌs.ne c,
exact ae_measurable'_condexp_ind hs hΞΌs.ne c, },
{ intros f g hf hg h_disj hfm hgm,
rw (condexp_L1_clm hm ΞΌ).map_add,
refine ae_measurable'.congr _ (coe_fn_add _ _).symm,
exact ae_measurable'.add hfm hgm, },
{ have : {f : Lp F' 1 ΞΌ | ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ}
= (condexp_L1_clm hm ΞΌ) β»ΒΉ' {f | ae_measurable' m f ΞΌ},
by refl,
rw this,
refine is_closed.preimage (condexp_L1_clm hm ΞΌ).continuous _,
exact is_closed_ae_measurable' hm, },
end
lemma Lp_meas_to_Lp_trim_lie_symm_indicator [normed_space β F] {ΞΌ : measure Ξ±}
(hs : measurable_set[m] s) (hΞΌs : ΞΌ.trim hm s β β) (c : F) :
((Lp_meas_to_Lp_trim_lie F β 1 ΞΌ hm).symm
(indicator_const_Lp 1 hs hΞΌs c) : Ξ± ββ[ΞΌ] F)
= indicator_const_Lp 1 (hm s hs) ((le_trim hm).trans_lt hΞΌs.lt_top).ne c :=
begin
ext1,
rw β Lp_meas_coe,
change Lp_trim_to_Lp_meas F β 1 ΞΌ hm (indicator_const_Lp 1 hs hΞΌs c)
=α΅[ΞΌ] (indicator_const_Lp 1 _ _ c : Ξ± β F),
refine (Lp_trim_to_Lp_meas_ae_eq hm _).trans _,
exact (ae_eq_of_ae_eq_trim indicator_const_Lp_coe_fn).trans indicator_const_Lp_coe_fn.symm,
end
lemma condexp_L1_clm_Lp_meas (f : Lp_meas F' β m 1 ΞΌ) :
condexp_L1_clm hm ΞΌ (f : Ξ± ββ[ΞΌ] F') = βf :=
begin
let g := Lp_meas_to_Lp_trim_lie F' β 1 ΞΌ hm f,
have hfg : f = (Lp_meas_to_Lp_trim_lie F' β 1 ΞΌ hm).symm g,
by simp only [linear_isometry_equiv.symm_apply_apply],
rw hfg,
refine @Lp.induction Ξ± F' m _ _ _ _ 1 (ΞΌ.trim hm) _ ennreal.coe_ne_top
(Ξ» g : Ξ± ββ[ΞΌ.trim hm] F',
condexp_L1_clm hm ΞΌ ((Lp_meas_to_Lp_trim_lie F' β 1 ΞΌ hm).symm g : Ξ± ββ[ΞΌ] F')
= β((Lp_meas_to_Lp_trim_lie F' β 1 ΞΌ hm).symm g)) _ _ _ g,
{ intros c s hs hΞΌs,
rw [Lp.simple_func.coe_indicator_const, Lp_meas_to_Lp_trim_lie_symm_indicator hs hΞΌs.ne c,
condexp_L1_clm_indicator_const_Lp],
exact condexp_ind_of_measurable hs ((le_trim hm).trans_lt hΞΌs).ne c, },
{ intros f g hf hg hfg_disj hf_eq hg_eq,
rw linear_isometry_equiv.map_add,
push_cast,
rw [map_add, hf_eq, hg_eq], },
{ refine is_closed_eq _ _,
{ refine (condexp_L1_clm hm ΞΌ).continuous.comp (continuous_induced_dom.comp _),
exact linear_isometry_equiv.continuous _, },
{ refine continuous_induced_dom.comp _,
exact linear_isometry_equiv.continuous _, }, },
end
lemma condexp_L1_clm_of_ae_measurable' (f : Ξ± ββ[ΞΌ] F') (hfm : ae_measurable' m f ΞΌ) :
condexp_L1_clm hm ΞΌ f = f :=
condexp_L1_clm_Lp_meas (β¨f, hfmβ© : Lp_meas F' β m 1 ΞΌ)
/-- Conditional expectation of a function, in L1. Its value is 0 if the function is not
integrable. The function-valued `condexp` should be used instead in most cases. -/
def condexp_L1 (hm : m β€ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] (f : Ξ± β F') : Ξ± ββ[ΞΌ] F' :=
set_to_fun ΞΌ (condexp_ind hm ΞΌ) (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) f
lemma condexp_L1_undef (hf : Β¬ integrable f ΞΌ) : condexp_L1 hm ΞΌ f = 0 :=
set_to_fun_undef (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hf
lemma condexp_L1_eq (hf : integrable f ΞΌ) :
condexp_L1 hm ΞΌ f = condexp_L1_clm hm ΞΌ (hf.to_L1 f) :=
set_to_fun_eq (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hf
lemma condexp_L1_zero : condexp_L1 hm ΞΌ (0 : Ξ± β F') = 0 :=
set_to_fun_zero _
lemma ae_measurable'_condexp_L1 {f : Ξ± β F'} : ae_measurable' m (condexp_L1 hm ΞΌ f) ΞΌ :=
begin
by_cases hf : integrable f ΞΌ,
{ rw condexp_L1_eq hf,
exact ae_measurable'_condexp_L1_clm _, },
{ rw condexp_L1_undef hf,
refine ae_measurable'.congr _ (coe_fn_zero _ _ _).symm,
exact measurable.ae_measurable' (@measurable_zero _ _ m _ _), },
end
lemma integrable_condexp_L1 (f : Ξ± β F') : integrable (condexp_L1 hm ΞΌ f) ΞΌ :=
L1.integrable_coe_fn _
/-- The integral of the conditional expectation `condexp_L1` over an `m`-measurable set is equal to
the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for
`condexp`. -/
lemma set_integral_condexp_L1 (hf : integrable f ΞΌ) (hs : measurable_set[m] s) :
β« x in s, condexp_L1 hm ΞΌ f x βΞΌ = β« x in s, f x βΞΌ :=
begin
simp_rw condexp_L1_eq hf,
rw set_integral_condexp_L1_clm (hf.to_L1 f) hs,
exact set_integral_congr_ae (hm s hs) ((hf.coe_fn_to_L1).mono (Ξ» x hx hxs, hx)),
end
lemma condexp_L1_add (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
condexp_L1 hm ΞΌ (f + g) = condexp_L1 hm ΞΌ f + condexp_L1 hm ΞΌ g :=
set_to_fun_add _ hf hg
lemma condexp_L1_neg (f : Ξ± β F') : condexp_L1 hm ΞΌ (-f) = - condexp_L1 hm ΞΌ f :=
set_to_fun_neg _ f
lemma condexp_L1_smul (c : π) (f : Ξ± β F') : condexp_L1 hm ΞΌ (c β’ f) = c β’ condexp_L1 hm ΞΌ f :=
set_to_fun_smul _ (Ξ» c _ x, condexp_ind_smul' c x) c f
lemma condexp_L1_sub (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
condexp_L1 hm ΞΌ (f - g) = condexp_L1 hm ΞΌ f - condexp_L1 hm ΞΌ g :=
set_to_fun_sub _ hf hg
lemma condexp_L1_of_ae_measurable' (hfm : ae_measurable' m f ΞΌ) (hfi : integrable f ΞΌ) :
condexp_L1 hm ΞΌ f =α΅[ΞΌ] f :=
begin
rw condexp_L1_eq hfi,
refine eventually_eq.trans _ (integrable.coe_fn_to_L1 hfi),
rw condexp_L1_clm_of_ae_measurable',
exact ae_measurable'.congr hfm (integrable.coe_fn_to_L1 hfi).symm,
end
end condexp_L1
section condexp
/-! ### Conditional expectation of a function -/
open_locale classical
variables {π} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
{hm : m β€ m0} [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β F'} {s : set Ξ±}
variables (m)
/-- Conditional expectation of a function. Its value is 0 if the function is not integrable. -/
@[irreducible] def condexp (hm : m β€ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] (f : Ξ± β F') :
Ξ± β F' :=
if (measurable[m] f β§ integrable f ΞΌ) then f else ae_measurable'_condexp_L1.mk (condexp_L1 hm ΞΌ f)
variables {m}
-- We define notations `ΞΌ[f|hm]` and `ΞΌ[f|m,hm]` for the conditional expectation of `f` with
-- respect to `m`. Both can be used in code but only the second one will be used by the goal view.
-- The first notation avoids the repetition of `m`, which is already present in `hm`. The second
-- one ensures that `m` stays visible in the goal view: when `hm` is complicated, it gets rendered
-- as `_` and the measurable space would not be visible in `ΞΌ[f|_]`, but is clear in `ΞΌ[f|m,_]`.
localized "notation ΞΌ `[` f `|` hm `]` := measure_theory.condexp _ hm ΞΌ f" in measure_theory
localized "notation ΞΌ `[` f `|` m `,` hm `]` := measure_theory.condexp m hm ΞΌ f" in measure_theory
lemma condexp_of_measurable {f : Ξ± β F'} (hf : measurable[m] f) (hfi : integrable f ΞΌ) :
ΞΌ[f|m,hm] = f :=
by rw [condexp, if_pos (β¨hf, hfiβ© : measurable[m] f β§ integrable f ΞΌ)]
lemma condexp_const (c : F') [is_finite_measure ΞΌ] : ΞΌ[(Ξ» x : Ξ±, c)|m,hm] = Ξ» _, c :=
condexp_of_measurable (@measurable_const _ _ _ m _) (integrable_const c)
lemma condexp_ae_eq_condexp_L1 (f : Ξ± β F') : ΞΌ[f|m,hm] =α΅[ΞΌ] condexp_L1 hm ΞΌ f :=
begin
unfold condexp,
by_cases hfm : measurable[m] f,
{ by_cases hfi : integrable f ΞΌ,
{ rw if_pos (β¨hfm, hfiβ© : measurable[m] f β§ integrable f ΞΌ),
exact (condexp_L1_of_ae_measurable' (measurable.ae_measurable' hfm) hfi).symm, },
{ simp only [hfi, if_false, and_false],
exact (ae_measurable'.ae_eq_mk ae_measurable'_condexp_L1).symm, }, },
simp only [hfm, if_false, false_and],
exact (ae_measurable'.ae_eq_mk ae_measurable'_condexp_L1).symm,
end
lemma condexp_ae_eq_condexp_L1_clm (hf : integrable f ΞΌ) :
ΞΌ[f|m,hm] =α΅[ΞΌ] condexp_L1_clm hm ΞΌ (hf.to_L1 f) :=
begin
refine (condexp_ae_eq_condexp_L1 f).trans (eventually_of_forall (Ξ» x, _)),
rw condexp_L1_eq hf,
end
lemma condexp_undef (hf : Β¬ integrable f ΞΌ) : ΞΌ[f|m,hm] =α΅[ΞΌ] 0 :=
begin
refine (condexp_ae_eq_condexp_L1 f).trans (eventually_eq.trans _ (coe_fn_zero _ 1 _)),
rw condexp_L1_undef hf,
end
@[simp] lemma condexp_zero : ΞΌ[(0 : Ξ± β F')|m,hm] = 0 :=
condexp_of_measurable (@measurable_zero _ _ m _ _) (integrable_zero _ _ _)
lemma measurable_condexp : measurable[m] (ΞΌ[f|m,hm]) :=
begin
unfold condexp,
by_cases hfm : measurable[m] f,
{ by_cases hfi : integrable f ΞΌ,
{ rwa if_pos (β¨hfm, hfiβ© : measurable[m] f β§ integrable f ΞΌ), },
{ simp only [hfi, if_false, and_false],
exact ae_measurable'.measurable_mk _, }, },
simp only [hfm, if_false, false_and],
exact ae_measurable'.measurable_mk _,
end
lemma integrable_condexp : integrable (ΞΌ[f|m,hm]) ΞΌ :=
(integrable_condexp_L1 f).congr (condexp_ae_eq_condexp_L1 f).symm
variable (hm)
/-- The integral of the conditional expectation `ΞΌ[f|hm]` over an `m`-measurable set is equal to
the integral of `f` on that set. -/
lemma set_integral_condexp (hf : integrable f ΞΌ) (hs : measurable_set[m] s) :
β« x in s, ΞΌ[f|m,hm] x βΞΌ = β« x in s, f x βΞΌ :=
begin
rw set_integral_congr_ae (hm s hs) ((condexp_ae_eq_condexp_L1 f).mono (Ξ» x hx _, hx)),
exact set_integral_condexp_L1 hf hs,
end
variable {hm}
lemma integral_condexp (hf : integrable f ΞΌ) : β« x, ΞΌ[f|m,hm] x βΞΌ = β« x, f x βΞΌ :=
begin
suffices : β« x in set.univ, ΞΌ[f|m,hm] x βΞΌ = β« x in set.univ, f x βΞΌ,
by { simp_rw integral_univ at this, exact this, },
exact set_integral_condexp hm hf (@measurable_set.univ _ m),
end
/-- **Uniqueness of the conditional expectation**
If a function is a.e. `m`-measurable, verifies an integrability condition and has same integral
as `f` on all `m`-measurable sets, then it is a.e. equal to `ΞΌ[f|hm]`. -/
lemma ae_eq_condexp_of_forall_set_integral_eq (hm : m β€ m0) [sigma_finite (ΞΌ.trim hm)]
{f g : Ξ± β F'} (hf : integrable f ΞΌ)
(hg_int_finite : β s, measurable_set[m] s β ΞΌ s < β β integrable_on g s ΞΌ)
(hg_eq : β s : set Ξ±, measurable_set[m] s β ΞΌ s < β β β« x in s, g x βΞΌ = β« x in s, f x βΞΌ)
(hgm : ae_measurable' m g ΞΌ) :
g =α΅[ΞΌ] ΞΌ[f|m,hm] :=
begin
refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' hm hg_int_finite
(Ξ» s hs hΞΌs, integrable_condexp.integrable_on) (Ξ» s hs hΞΌs, _) hgm
(measurable.ae_measurable' measurable_condexp),
rw [hg_eq s hs hΞΌs, set_integral_condexp hm hf hs],
end
lemma condexp_add (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
ΞΌ[f + g | m,hm] =α΅[ΞΌ] ΞΌ[f|m,hm] + ΞΌ[g|m,hm] :=
begin
refine (condexp_ae_eq_condexp_L1 _).trans _,
rw condexp_L1_add hf hg,
exact (coe_fn_add _ _).trans
((condexp_ae_eq_condexp_L1 _).symm.add (condexp_ae_eq_condexp_L1 _).symm),
end
lemma condexp_smul (c : π) (f : Ξ± β F') : ΞΌ[c β’ f | m,hm] =α΅[ΞΌ] c β’ ΞΌ[f|m,hm] :=
begin
refine (condexp_ae_eq_condexp_L1 _).trans _,
rw condexp_L1_smul c f,
refine (@condexp_ae_eq_condexp_L1 _ _ _ _ _ _ _ _ m _ _ hm _ f).mp _,
refine (coe_fn_smul c (condexp_L1 hm ΞΌ f)).mono (Ξ» x hx1 hx2, _),
rw [hx1, pi.smul_apply, pi.smul_apply, hx2],
end
lemma condexp_neg (f : Ξ± β F') : ΞΌ[-f|m,hm] =α΅[ΞΌ] - ΞΌ[f|m,hm] :=
by letI : module β (Ξ± β F') := @pi.module Ξ± (Ξ» _, F') β _ _ (Ξ» _, infer_instance);
calc ΞΌ[-f|m,hm] = ΞΌ[(-1 : β) β’ f|m,hm] : by rw neg_one_smul β f
... =α΅[ΞΌ] (-1 : β) β’ ΞΌ[f|m,hm] : condexp_smul (-1) f
... = -ΞΌ[f|m,hm] : neg_one_smul β (ΞΌ[f|m,hm])
lemma condexp_sub (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
ΞΌ[f - g | m,hm] =α΅[ΞΌ] ΞΌ[f|m,hm] - ΞΌ[g|m,hm] :=
begin
simp_rw sub_eq_add_neg,
exact (condexp_add hf hg.neg).trans (eventually_eq.rfl.add (condexp_neg g)),
end
lemma condexp_condexp_of_le {mβ mβ m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±}
(hmββ : mβ β€ mβ) (hmβ : mβ β€ m0) [sigma_finite (ΞΌ.trim (hmββ.trans hmβ))]
[sigma_finite (ΞΌ.trim hmβ)] :
ΞΌ[ ΞΌ[f|mβ, hmβ] | mβ, hmββ.trans hmβ] =α΅[ΞΌ] ΞΌ[f | mβ, hmββ.trans hmβ] :=
begin
refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hmββ.trans hmβ)
(Ξ» s hs hΞΌs, integrable_condexp.integrable_on) (Ξ» s hs hΞΌs, integrable_condexp.integrable_on)
_ (measurable.ae_measurable' measurable_condexp) (measurable.ae_measurable' measurable_condexp),
intros s hs hΞΌs,
rw set_integral_condexp _ integrable_condexp hs,
by_cases hf : integrable f ΞΌ,
{ rw [set_integral_condexp _ hf hs, set_integral_condexp _ hf (hmββ s hs)], },
{ simp_rw integral_congr_ae (ae_restrict_of_ae (condexp_undef hf)), },
end
section real
lemma rn_deriv_ae_eq_condexp {f : Ξ± β β} (hf : integrable f ΞΌ) :
signed_measure.rn_deriv ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm) =α΅[ΞΌ] ΞΌ[f | m,hm] :=
begin
refine ae_eq_condexp_of_forall_set_integral_eq hm hf _ _ _,
{ exact Ξ» _ _ _, (integrable_of_integrable_trim hm (signed_measure.integrable_rn_deriv
((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm))).integrable_on },
{ intros s hs hlt,
conv_rhs { rw [β hf.with_densityα΅₯_trim_eq_integral hm hs,
β signed_measure.with_densityα΅₯_rn_deriv_eq ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm)
(hf.with_densityα΅₯_trim_absolutely_continuous hm)], },
rw [with_densityα΅₯_apply
(signed_measure.integrable_rn_deriv ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm)) hs,
β set_integral_trim hm _ hs],
exact signed_measure.measurable_rn_deriv _ _ },
{ exact measurable.ae_measurable' (signed_measure.measurable_rn_deriv _ _) },
end
end real
end condexp
end measure_theory
|
6ea1e68dc2ae6d78d947e67b27126cbd666ba690 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/1705.lean | 2397ddf674529211c2e93d0482f69ae5a4b999ad | [
"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 | 24 | lean | class S (R : T u) M : R
|
f5d360a187474ecaa21cee52640f48cc2198cd3a | 8b9f17008684d796c8022dab552e42f0cb6fb347 | /tests/lean/hott/crash1.hlean | 0412ce4aadeeae2069cac5e78cab7e6f46002663 | [
"Apache-2.0"
] | permissive | chubbymaggie/lean | 0d06ae25f9dd396306fb02190e89422ea94afd7b | d2c7b5c31928c98f545b16420d37842c43b4ae9a | refs/heads/master | 1,611,313,622,901 | 1,430,266,839,000 | 1,430,267,083,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 740 | hlean | import types.sigma types.prod
import algebra.binary algebra.group
open eq eq.ops
namespace path_algebra
variable {A : Type}
structure distrib [class] (A : Type) extends has_mul A, has_add A :=
(left_distrib : βa b c, mul a (add b c) = add (mul a b) (mul a c))
(right_distrib : βa b c, mul (add a b) c = add (mul a c) (mul b c))
structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A :=
(zero_mul : Ξ a, mul zero a = zero)
(mul_zero : Ξ a, mul a zero = zero)
structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A :=
(zero_ne_one : zero β one)
structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A,
distrib A, mul_zero_class A, zero_ne_one_class A
end path_algebra
|
faabf777e5ea10a9ee498050e2ae684e8356920b | 2c819623a83d8c53b7282622429e344389ce4030 | /proofs/util/nat.lean | 482f39675c5c466e3f9164edce7fe929544ed1cc | [] | no_license | uw-unsat/jitsynth | 6bc97dd1ede9da42c0b9313a4766095345493adc | 69529e18d4a8d4dace884bfde91aa26b549523fa | refs/heads/master | 1,653,802,737,129 | 1,588,361,732,000 | 1,588,361,732,000 | 260,513,113 | 14 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,307 | lean | import tactic
import tactic.find
import tactic.core
-- Lemmas involving facts about β
namespace jitsynth
lemma sub_lt_sub :
β a b c : β,
c < b β
c < a β
a - b < a - c
| 0 b c hβ hβ :=
-- base case
begin
have c_le_zero := nat.le_of_lt hβ,
have c_eq_zero := nat.eq_zero_of_le_zero c_le_zero,
rewrite c_eq_zero at hβ,
repeat {rewrite nat.zero_sub},
exact hβ
end
| (a+1) b c hβ hβ :=
-- inductive case
begin
have ih := sub_lt_sub a b c,
have hβ := nat.succ_le_of_lt hβ,
have hc : nat.succ c = c + 1,
simp,
rewrite hc at hβ,
have hβ : c β€ a,
rewrite β nat.add_le_add_iff_le_right,
apply hβ,
have hβ
:= nat.eq_or_lt_of_le hβ,
cases hβ
,
-- c = a
rewrite β hβ
,
rewrite nat.add_sub_cancel_left,
rewrite nat.sub_eq_zero_of_le,
apply nat.zero_lt_one,
----
apply nat.succ_le_of_lt; assumption,
-- c < a
have hβ := nat.lt_or_ge a b,
cases hβ,
-- a < b
have hβ : a + 1 β€ b,
apply nat.succ_le_of_lt; assumption,
have rewrite_l : a + 1 - b = 0,
apply nat.sub_eq_zero_of_le; assumption,
rewrite rewrite_l,
rewrite nat.add_comm,
rewrite nat.add_sub_assoc,
have temp : 1 + (a - c) = nat.succ (a - c),
rewrite nat.add_comm,
rewrite temp,
apply nat.zero_lt_succ,
assumption,
-- a β₯ b
rewrite nat.add_comm,
rewrite nat.add_sub_assoc,
rewrite nat.add_sub_assoc,
apply add_lt_add_left,
apply ih,
repeat {assumption}
end
lemma sub_le :
β n m : β, n - m β€ n
| 0 m := by simp
| n 0 := by simp
| (n+1) (m+1) :=
begin
rewrite nat.add_comm,
simp,
apply nat.le_succ_of_le,
apply sub_le
end
lemma sub_succ :
β n : β,
1 β€ n β
n - 1 + 1 = n
| 0 h := by cases h
| (n+1) h := by simp
lemma one_le_of_add_le :
β n m : β,
1 + m β€ n β
1 β€ n
| n 0 h := by simp at *; assumption
| n (m+1) h :=
begin
apply one_le_of_add_le n m,
apply nat.le_of_succ_le,
assumption
end
lemma succ_le_le_sub :
β n m : β,
n + 1 β€ m β
n β€ m - 1
| n 0 h := by cases h
| n (m+1) h :=
begin
simp,
rewrite β nat.add_le_add_iff_le_right,
assumption
end
lemma smaller_sub :
β n a b : β,
a < b β a < n β n - b < n - a
| 0 a b h1 h2 := by cases h2
| n a 0 h1 h2 := by cases h1
| n 0 b h1 h2 :=
begin
simp,
cases nat.lt_or_ge n b,
rewrite nat.sub_eq_zero_of_le,
assumption,
apply nat.le_of_lt h,
---
apply nat.sub_lt_of_pos_le,
assumption,
assumption,
end
| (n+1) (a+1) (b+1) h1 h2 :=
begin
repeat {rewrite nat.add_sub_add_right},
apply smaller_sub,
rewrite nat.add_comm at h1,
rewrite nat.add_comm b 1 at h1,
apply nat.lt_of_add_lt_add_left; assumption,
rewrite nat.add_comm at h2,
rewrite nat.add_comm n 1 at h2,
apply nat.lt_of_add_lt_add_left; assumption,
end
lemma sub_le_of_sub_le_succ_of_lt :
β n m a b : β,
a < b β
n - a β€ m + 1 β
n - b β€ m
| n m a b hβ hβ :=
begin
cases nat.lt_or_ge b n,
-- b < n
have h' := nat.lt_trans hβ h,
have h'' := smaller_sub n a b hβ h',
apply nat.le_of_lt_succ,
cases nat.eq_or_lt_of_le hβ with heq hlt,
rewrite heq at h'',
apply h'',
---
apply nat.lt_trans h'' hlt,
-- b β₯ n
rewrite nat.sub_eq_zero_of_le,
apply nat.zero_le,
assumption
end
lemma lt_sub_succ_n :
β n m k : β,
n - m = k + 1 β m < n
| n m k h :=
begin
cases nat.lt_or_ge m n,
assumption,
rewrite nat.sub_eq_zero_of_le at h,
cases h,
assumption
end
lemma one_add :
β n : β,
nat.succ n = 1 + n
| n := by rewrite β nat.add_one; simp
lemma only_zero_lt_one :
β n : β,
n < 1 β n = 0
| 0 h := by reflexivity
| (n+1) h :=
begin
simp at h,
cases h
end
lemma mul_le_inside :
β a b n m : β,
a β€ b β
n * b β€ m β
n * a β€ m
| a b n m h1 h2 :=
begin
have h3 := nat.mul_le_mul_left n h1,
apply nat.le_trans,
assumption,
assumption
end
lemma diff_ge_1 :
β n m : β,
n < m β 1 β€ m - n
| n m h :=
begin
-- have h' := nat.le_of_lt_succ h,
rewrite β nat.add_le_add_iff_le_right n,
rewrite nat.sub_add_cancel,
-- rewrite hm,
apply nat.le_of_lt_succ,
rewrite β nat.add_one,
rewrite nat.add_comm,
apply nat.add_lt_add_right,
assumption,
apply nat.le_of_lt,
assumption
end
end jitsynth |
3e04964e810fa0191f306af446f48520da03fcaf | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/ring_theory/roots_of_unity.lean | 666bec8ac669aeb6514457b3a50aaceb28b2d2b7 | [
"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 | 40,537 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.nat.parity
import data.polynomial.ring_division
import group_theory.specific_groups.cyclic
import ring_theory.integral_domain
import number_theory.divisors
import data.zmod.basic
import tactic.zify
import field_theory.separable
import field_theory.finite.basic
/-!
# Roots of unity and primitive roots of unity
We define roots of unity in the context of an arbitrary commutative monoid,
as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative
monoids, expressing that an element is a primitive root of unity.
## Main definitions
* `roots_of_unity n M`, for `n : β+` is the subgroup of the units of a commutative monoid `M`
consisting of elements `x` that satisfy `x ^ n = 1`.
* `is_primitive_root ΞΆ k`: an element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`,
and if `l` satisfies `ΞΆ ^ l = 1` then `k β£ l`.
* `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`.
## Main results
* `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group.
* `is_primitive_root.zmod_equiv_gpowers`: `zmod k` is equivalent to
the subgroup generated by a primitive `k`-th root of unity.
* `is_primitive_root.gpowers_eq`: in an integral domain, the subgroup generated by
a primitive `k`-th root of unity is equal to the `k`-th roots of unity.
* `is_primitive_root.card_primitive_roots`: if an integral domain
has a primitive `k`-th root of unity, then it has `Ο k` of them.
## Implementation details
It is desirable that `roots_of_unity` is a subgroup,
and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields.
We therefore implement it as a subgroup of the units of a commutative monoid.
We have chosen to define `roots_of_unity n` for `n : β+`, instead of `n : β`,
because almost all lemmas need the positivity assumption,
and in particular the type class instances for `fintype` and `is_cyclic`.
On the other hand, for primitive roots of unity, it is desirable to have a predicate
not just on units, but directly on elements of the ring/field.
For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity
in the complex numbers, without having to turn that number into a unit first.
This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and
`is_primitive_root.coe_units_iff` should provide the necessary glue.
-/
open_locale classical big_operators
noncomputable theory
open polynomial
open finset
variables {M N G Gβ R S : Type*}
variables [comm_monoid M] [comm_monoid N] [comm_group G] [comm_group_with_zero Gβ]
section roots_of_unity
variables {k l : β+}
/-- `roots_of_unity k M` is the subgroup of elements `m : units M` that satisfy `m ^ k = 1` -/
def roots_of_unity (k : β+) (M : Type*) [comm_monoid M] : subgroup (units M) :=
{ carrier := { ΞΆ | ΞΆ ^ (k : β) = 1 },
one_mem' := one_pow _,
mul_mem' := Ξ» ΞΆ ΞΎ hΞΆ hΞΎ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *,
inv_mem' := Ξ» ΞΆ hΞΆ, by simp only [*, set.mem_set_of_eq, inv_pow, one_inv] at * }
@[simp] lemma mem_roots_of_unity (k : β+) (ΞΆ : units M) :
ΞΆ β roots_of_unity k M β ΞΆ ^ (k : β) = 1 := iff.rfl
lemma roots_of_unity_le_of_dvd (h : k β£ l) : roots_of_unity k M β€ roots_of_unity l M :=
begin
obtain β¨d, rflβ© := h,
intros ΞΆ h,
simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *,
end
lemma map_roots_of_unity (f : units M β* units N) (k : β+) :
(roots_of_unity k M).map f β€ roots_of_unity k N :=
begin
rintros _ β¨ΞΆ, h, rflβ©,
simp only [βmonoid_hom.map_pow, *, mem_roots_of_unity, set_like.mem_coe, monoid_hom.map_one] at *
end
variables [comm_ring R]
@[norm_cast]
lemma roots_of_unity.coe_pow (ΞΆ : roots_of_unity k R) (m : β) : β(ΞΆ ^ m) = (ΞΆ ^ m : R) :=
begin
change β(β(ΞΆ ^ m) : units R) = β(ΞΆ : units R) ^ m,
rw [subgroup.coe_pow, units.coe_pow],
end
variables [comm_ring S]
/-- Restrict a ring homomorphism between integral domains to the nth roots of unity -/
def ring_hom.restrict_roots_of_unity (Ο : R β+* S) (n : β+) :
roots_of_unity n R β* roots_of_unity n S :=
let h : β ΞΎ : roots_of_unity n R, (Ο ΞΎ) ^ (n : β) = 1 := Ξ» ΞΎ, by
{ change (Ο (ΞΎ : units R)) ^ (n : β) = 1,
rw [βΟ.map_pow, βunits.coe_pow, show ((ΞΎ : units R) ^ (n : β) = 1), from ΞΎ.2,
units.coe_one, Ο.map_one] } in
{ to_fun := Ξ» ΞΎ, β¨@unit_of_invertible _ _ _ (invertible_of_pow_eq_one _ _ (h ΞΎ) n.2),
by { ext, rw units.coe_pow, exact h ΞΎ }β©,
map_one' := by { ext, exact Ο.map_one },
map_mul' := Ξ» ΞΎβ ΞΎβ, by { ext, rw [subgroup.coe_mul, units.coe_mul], exact Ο.map_mul _ _ } }
@[simp] lemma ring_hom.restrict_roots_of_unity_coe_apply (Ο : R β+* S) (ΞΆ : roots_of_unity k R) :
β(Ο.restrict_roots_of_unity k ΞΆ) = Ο βΞΆ :=
rfl
/-- Restrict a ring isomorphism between integral domains to the nth roots of unity -/
def ring_equiv.restrict_roots_of_unity (Ο : R β+* S) (n : β+) :
roots_of_unity n R β* roots_of_unity n S :=
{ to_fun := Ο.to_ring_hom.restrict_roots_of_unity n,
inv_fun := Ο.symm.to_ring_hom.restrict_roots_of_unity n,
left_inv := Ξ» ΞΎ, by { ext, exact Ο.symm_apply_apply ΞΎ },
right_inv := Ξ» ΞΎ, by { ext, exact Ο.apply_symm_apply ΞΎ },
map_mul' := (Ο.to_ring_hom.restrict_roots_of_unity n).map_mul }
@[simp] lemma ring_equiv.restrict_roots_of_unity_coe_apply (Ο : R β+* S) (ΞΆ : roots_of_unity k R) :
β(Ο.restrict_roots_of_unity k ΞΆ) = Ο βΞΆ :=
rfl
@[simp] lemma ring_equiv.restrict_roots_of_unity_symm (Ο : R β+* S) :
(Ο.restrict_roots_of_unity k).symm = Ο.symm.restrict_roots_of_unity k :=
rfl
variables [is_domain R]
lemma mem_roots_of_unity_iff_mem_nth_roots {ΞΆ : units R} :
ΞΆ β roots_of_unity k R β (ΞΆ : R) β nth_roots k (1 : R) :=
by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow]
variables (k R)
/-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`.
This is implemented as equivalence of subtypes,
because `roots_of_unity` is a subgroup of the group of units,
whereas `nth_roots` is a multiset. -/
def roots_of_unity_equiv_nth_roots :
roots_of_unity k R β {x // x β nth_roots k (1 : R)} :=
begin
refine
{ to_fun := Ξ» x, β¨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2β©,
inv_fun := Ξ» x, β¨β¨x, x ^ (k - 1 : β), _, _β©, _β©,
left_inv := _,
right_inv := _ },
swap 4, { rintro β¨x, hxβ©, ext, refl },
swap 4, { rintro β¨x, hxβ©, ext, refl },
all_goals
{ rcases x with β¨x, hxβ©, rw [mem_nth_roots k.pos] at hx,
simp only [subtype.coe_mk, β pow_succ, β pow_succ', hx,
tsub_add_cancel_of_le (show 1 β€ (k : β), from k.one_le)] },
{ show (_ : units R) ^ (k : β) = 1,
simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] }
end
variables {k R}
@[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) :
(roots_of_unity_equiv_nth_roots R k x : R) = x :=
rfl
@[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x β nth_roots k (1 : R)}) :
((roots_of_unity_equiv_nth_roots R k).symm x : R) = x :=
rfl
variables (k R)
instance roots_of_unity.fintype : fintype (roots_of_unity k R) :=
fintype.of_equiv {x // x β nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm
instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) :=
is_cyclic_of_subgroup_is_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype)
(units.ext.comp subtype.val_injective)
lemma card_roots_of_unity : fintype.card (roots_of_unity k R) β€ k :=
calc fintype.card (roots_of_unity k R)
= fintype.card {x // x β nth_roots k (1 : R)} :
fintype.card_congr (roots_of_unity_equiv_nth_roots R k)
... β€ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.erase_dup_le _)
... = (nth_roots k (1 : R)).card : multiset.card_attach
... β€ k : card_nth_roots k 1
variables {k R}
lemma ring_hom.map_root_of_unity_eq_pow_self (Ο : R β+* R) (ΞΆ : roots_of_unity k R) :
β m : β, Ο ΞΆ = ΞΆ ^ m :=
begin
obtain β¨m, hmβ© := (Ο.restrict_roots_of_unity k).map_cyclic,
rw [βΟ.restrict_roots_of_unity_coe_apply, hm, gpow_eq_mod_order_of, βint.to_nat_of_nonneg
(m.mod_nonneg (int.coe_nat_ne_zero.mpr (pos_iff_ne_zero.mp (order_of_pos ΞΆ)))),
gpow_coe_nat, roots_of_unity.coe_pow],
exact β¨(m % (order_of ΞΆ)).to_nat, rflβ©,
end
end roots_of_unity
/-- An element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`,
and if `l` satisfies `ΞΆ ^ l = 1` then `k β£ l`. -/
structure is_primitive_root (ΞΆ : M) (k : β) : Prop :=
(pow_eq_one : ΞΆ ^ (k : β) = 1)
(dvd_of_pow_eq_one : β l : β, ΞΆ ^ l = 1 β k β£ l)
section primitive_roots
variables {k : β}
/-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity
in the integral domain `R`. -/
def primitive_roots (k : β) (R : Type*) [comm_ring R] [is_domain R] : finset R :=
(nth_roots k (1 : R)).to_finset.filter (Ξ» ΞΆ, is_primitive_root ΞΆ k)
variables [comm_ring R] [is_domain R]
@[simp] lemma mem_primitive_roots {ΞΆ : R} (h0 : 0 < k) :
ΞΆ β primitive_roots k R β is_primitive_root ΞΆ k :=
begin
rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp],
exact is_primitive_root.pow_eq_one
end
end primitive_roots
namespace is_primitive_root
variables {k l : β}
lemma iff_def (ΞΆ : M) (k : β) :
is_primitive_root ΞΆ k β (ΞΆ ^ k = 1) β§ (β l : β, ΞΆ ^ l = 1 β k β£ l) :=
β¨Ξ» β¨h1, h2β©, β¨h1, h2β©, Ξ» β¨h1, h2β©, β¨h1, h2β©β©
lemma mk_of_lt (ΞΆ : M) (hk : 0 < k) (h1 : ΞΆ ^ k = 1) (h : β l : β, 0 < l β l < k β ΞΆ ^ l β 1) :
is_primitive_root ΞΆ k :=
begin
refine β¨h1, _β©,
intros l hl,
apply dvd_trans _ (k.gcd_dvd_right l),
suffices : k.gcd l = k, { rw this },
rw eq_iff_le_not_lt,
refine β¨nat.le_of_dvd hk (k.gcd_dvd_left l), _β©,
intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h',
exact pow_gcd_eq_one _ h1 hl
end
section comm_monoid
variables {ΞΆ : M} (h : is_primitive_root ΞΆ k)
lemma pow_eq_one_iff_dvd (l : β) : ΞΆ ^ l = 1 β k β£ l :=
β¨h.dvd_of_pow_eq_one l,
by { rintro β¨i, rflβ©, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }β©
lemma is_unit (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_unit ΞΆ :=
begin
apply is_unit_of_mul_eq_one ΞΆ (ΞΆ ^ (k - 1)),
rw [β pow_succ, tsub_add_cancel_of_le h0.nat_succ_le, h.pow_eq_one]
end
lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ΞΆ ^ l β 1 :=
mt (nat.le_of_dvd h0 β h.dvd_of_pow_eq_one _) $ not_le_of_lt hl
lemma pow_inj (h : is_primitive_root ΞΆ k) β¦i j : ββ¦ (hi : i < k) (hj : j < k) (H : ΞΆ ^ i = ΞΆ ^ j) :
i = j :=
begin
wlog hij : i β€ j,
apply le_antisymm hij,
rw β tsub_eq_zero_iff_le,
apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt tsub_le_self hj),
apply h.dvd_of_pow_eq_one,
rw [β ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj,
β pow_add, tsub_add_cancel_of_le hij, H, one_mul]
end
lemma one : is_primitive_root (1 : M) 1 :=
{ pow_eq_one := pow_one _,
dvd_of_pow_eq_one := Ξ» l hl, one_dvd _ }
@[simp] lemma one_right_iff : is_primitive_root ΞΆ 1 β ΞΆ = 1 :=
begin
split,
{ intro h, rw [β pow_one ΞΆ, h.pow_eq_one] },
{ rintro rfl, exact one }
end
@[simp] lemma coe_units_iff {ΞΆ : units M} :
is_primitive_root (ΞΆ : M) k β is_primitive_root ΞΆ k :=
by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one]
lemma pow_of_coprime (h : is_primitive_root ΞΆ k) (i : β) (hi : i.coprime k) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : k = 0,
{ subst k, simp only [*, pow_one, nat.coprime_zero_right] at * },
rcases h.is_unit (nat.pos_of_ne_zero h0) with β¨ΞΆ, rflβ©,
rw [β units.coe_pow],
rw coe_units_iff at h β’,
refine
{ pow_eq_one := by rw [β pow_mul', pow_mul, h.pow_eq_one, one_pow],
dvd_of_pow_eq_one := _ },
intros l hl,
apply h.dvd_of_pow_eq_one,
rw [β pow_one ΞΆ, β gpow_coe_nat ΞΆ, β hi.gcd_eq_one, nat.gcd_eq_gcd_ab, gpow_add,
mul_pow, β gpow_coe_nat, β gpow_mul, mul_right_comm],
simp only [gpow_mul, hl, h.pow_eq_one, one_gpow, one_pow, one_mul, gpow_coe_nat]
end
lemma pow_of_prime (h : is_primitive_root ΞΆ k) {p : β} (hprime : nat.prime p) (hdiv : Β¬ p β£ k) :
is_primitive_root (ΞΆ ^ p) k :=
h.pow_of_coprime p (hprime.coprime_iff_not_dvd.2 hdiv)
lemma pow_iff_coprime (h : is_primitive_root ΞΆ k) (h0 : 0 < k) (i : β) :
is_primitive_root (ΞΆ ^ i) k β i.coprime k :=
begin
refine β¨_, h.pow_of_coprime iβ©,
intro hi,
obtain β¨a, haβ© := i.gcd_dvd_left k,
obtain β¨b, hbβ© := i.gcd_dvd_right k,
suffices : b = k,
{ rwa [this, β one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } },
rw [ha] at hi,
rw [mul_comm] at hb,
apply nat.dvd_antisymm β¨i.gcd k, hbβ© (hi.dvd_of_pow_eq_one b _),
rw [β pow_mul', β mul_assoc, β hb, pow_mul, h.pow_eq_one, one_pow]
end
end comm_monoid
section comm_group
variables {ΞΆ : G}
lemma gpow_eq_one (h : is_primitive_root ΞΆ k) : ΞΆ ^ (k : β€) = 1 :=
by { rw gpow_coe_nat, exact h.pow_eq_one }
lemma gpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β€) :
ΞΆ ^ l = 1 β (k : β€) β£ l :=
begin
by_cases h0 : 0 β€ l,
{ lift l to β using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 β€ -l, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -l to β using this with l' hl',
rw [β dvd_neg, β hl'],
norm_cast,
rw [β h.pow_eq_one_iff_dvd, β inv_inj, β gpow_neg, β hl', gpow_coe_nat, one_inv] }
end
lemma inv (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΆβ»ΒΉ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, one_inv, eq_self_iff_true, inv_pow],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [β inv_inj, β inv_pow, hl, one_inv]
end }
@[simp] lemma inv_iff : is_primitive_root ΞΆβ»ΒΉ k β is_primitive_root ΞΆ k :=
by { refine β¨_, Ξ» h, inv hβ©, intro h, rw [β inv_inv ΞΆ], exact inv h }
lemma gpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β€) (hi : i.gcd k = 1) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : 0 β€ i,
{ lift i to β using h0,
rw gpow_coe_nat,
exact h.pow_of_coprime i hi },
have : 0 β€ -i, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -i to β using this with i' hi',
rw [β inv_iff, β gpow_neg, β hi', gpow_coe_nat],
apply h.pow_of_coprime,
rw [int.gcd, β int.nat_abs_neg, β hi'] at hi,
exact hi
end
@[simp] lemma coe_subgroup_iff (H : subgroup G) {ΞΆ : H} :
is_primitive_root (ΞΆ : G) k β is_primitive_root ΞΆ k :=
by simp only [iff_def, β subgroup.coe_pow, β H.coe_one, β subtype.ext_iff]
end comm_group
section comm_group_with_zero
variables {ΞΆ : Gβ}
lemma fpow_eq_one (h : is_primitive_root ΞΆ k) : ΞΆ ^ (k : β€) = 1 :=
by { rw gpow_coe_nat, exact h.pow_eq_one }
lemma fpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β€) :
ΞΆ ^ l = 1 β (k : β€) β£ l :=
begin
by_cases h0 : 0 β€ l,
{ lift l to β using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 β€ -l, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -l to β using this with l' hl',
rw [β dvd_neg, β hl'],
norm_cast,
rw [β h.pow_eq_one_iff_dvd, β inv_injβ, β fpow_neg, β hl', gpow_coe_nat, inv_one] }
end
lemma inv' (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΆβ»ΒΉ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_powβ],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [β inv_injβ, β inv_powβ, hl, inv_one]
end }
@[simp] lemma inv_iff' : is_primitive_root ΞΆβ»ΒΉ k β is_primitive_root ΞΆ k :=
by { refine β¨_, Ξ» h, inv' hβ©, intro h, rw [β inv_invβ ΞΆ], exact inv' h }
lemma fpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β€) (hi : i.gcd k = 1) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : 0 β€ i,
{ lift i to β using h0,
rw gpow_coe_nat,
exact h.pow_of_coprime i hi },
have : 0 β€ -i, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -i to β using this with i' hi',
rw [β inv_iff', β fpow_neg, β hi', gpow_coe_nat],
apply h.pow_of_coprime,
rw [int.gcd, β int.nat_abs_neg, β hi'] at hi,
exact hi
end
end comm_group_with_zero
section is_domain
variables {ΞΆ : R}
variables [comm_ring R] [is_domain R]
@[simp] lemma primitive_roots_zero : primitive_roots 0 R = β
:=
begin
rw [β finset.val_eq_zero, β multiset.subset_zero, β nth_roots_zero (1 : R), primitive_roots],
simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero,
finset.filter_true_of_mem, finset.empty_val, not_false_iff,
multiset.zero_subset, nth_roots_zero]
end
@[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} :=
begin
apply finset.eq_singleton_iff_unique_mem.2,
split,
{ simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] },
{ intros x hx,
rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx,
exact hx }
end
lemma neg_one (p : β) [char_p R p] (hp : p β 2) : is_primitive_root (-1 : R) 2 :=
mk_of_lt (-1 : R) dec_trivial (by simp only [one_pow, neg_sq]) $
begin
intros l hl0 hl2,
obtain rfl : l = 1,
{ unfreezingI { clear_dependent R p }, dec_trivial! },
simp only [pow_one, ne.def],
intro h,
suffices h2 : p β£ 2,
{ have := char_p.char_ne_one R p,
unfreezingI { clear_dependent R },
have aux := nat.le_of_dvd dec_trivial h2,
revert this hp h2, revert p, dec_trivial },
simp only [β char_p.cast_eq_zero_iff R p, nat.cast_bit0, nat.cast_one],
rw [bit0, β h, neg_add_self] { occs := occurrences.pos [1] }
end
lemma eq_neg_one_of_two_right (h : is_primitive_root ΞΆ 2) : ΞΆ = -1 :=
begin
apply (eq_or_eq_neg_of_sq_eq_sq ΞΆ 1 _).resolve_left,
{ rw [β pow_one ΞΆ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial },
{ simp only [h.pow_eq_one, one_pow] }
end
end is_domain
section is_domain
variables [comm_ring R]
variables {ΞΆ : units R} (h : is_primitive_root ΞΆ k)
protected
lemma mem_roots_of_unity {n : β+} (h : is_primitive_root ΞΆ n) : ΞΆ β roots_of_unity n R :=
h.pow_eq_one
/-- The (additive) monoid equivalence between `zmod k`
and the powers of a primitive root of unity `ΞΆ`. -/
def zmod_equiv_gpowers (h : is_primitive_root ΞΆ k) : zmod k β+ additive (subgroup.gpowers ΞΆ) :=
add_equiv.of_bijective
(add_monoid_hom.lift_of_right_inverse (int.cast_add_hom $ zmod k) _ zmod.int_cast_right_inverse
β¨{ to_fun := Ξ» i, additive.of_mul (β¨_, i, rflβ© : subgroup.gpowers ΞΆ),
map_zero' := by { simp only [gpow_zero], refl },
map_add' := by { intros i j, simp only [gpow_add], refl } },
(Ξ» i hi,
begin
simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k,
add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi β’,
obtain β¨i, rflβ© := hi,
simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat],
refl
end)β©)
begin
split,
{ rw add_monoid_hom.injective_iff,
intros i hi,
rw subtype.ext_iff at hi,
have := (h.gpow_eq_one_iff_dvd _).mp hi,
rw [β (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm],
exact zmod.int_cast_right_inverse i },
{ rintro β¨ΞΎ, i, rflβ©,
refine β¨int.cast_add_hom _ i, _β©,
rw [add_monoid_hom.lift_of_right_inverse_comp_apply],
refl }
end
@[simp] lemma zmod_equiv_gpowers_apply_coe_int (i : β€) :
h.zmod_equiv_gpowers i = additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ) :=
add_monoid_hom.lift_of_right_inverse_comp_apply _ _ zmod.int_cast_right_inverse _ _
@[simp] lemma zmod_equiv_gpowers_apply_coe_nat (i : β) :
h.zmod_equiv_gpowers i = additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ) :=
begin
have : (i : zmod k) = (i : β€), by norm_cast,
simp only [this, zmod_equiv_gpowers_apply_coe_int, gpow_coe_nat],
refl
end
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow (i : β€) :
h.zmod_equiv_gpowers.symm (additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ)) = i :=
by rw [β h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_int]
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow' (i : β€) :
h.zmod_equiv_gpowers.symm β¨ΞΆ ^ i, i, rflβ© = i :=
h.zmod_equiv_gpowers_symm_apply_gpow i
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow (i : β) :
h.zmod_equiv_gpowers.symm (additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ)) = i :=
by rw [β h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_nat]
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow' (i : β) :
h.zmod_equiv_gpowers.symm β¨ΞΆ ^ i, i, rflβ© = i :=
h.zmod_equiv_gpowers_symm_apply_pow i
/-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`,
then there is a `b`-th primitive root of unity in `R`. -/
lemma pow {ΞΆ : R} {n : β} {a b : β}
(hn : 0 < n) (h : is_primitive_root ΞΆ n) (hprod : n = a * b) :
is_primitive_root (ΞΆ ^ a) b :=
begin
subst n,
simp only [iff_def, β pow_mul, h.pow_eq_one, eq_self_iff_true, true_and],
intros l hl,
have ha0 : a β 0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn },
rwa β mul_dvd_mul_iff_left ha0,
exact h.dvd_of_pow_eq_one _ hl
end
variables [is_domain R]
lemma gpowers_eq {k : β+} {ΞΆ : units R} (h : is_primitive_root ΞΆ k) :
subgroup.gpowers ΞΆ = roots_of_unity k R :=
begin
apply set_like.coe_injective,
haveI : fact (0 < (k : β)) := β¨k.posβ©,
haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ (h.zmod_equiv_gpowers).to_equiv,
refine @set.eq_of_subset_of_card_le (units R) (subgroup.gpowers ΞΆ) (roots_of_unity k R)
F (roots_of_unity.fintype R k)
(subgroup.gpowers_subset $ show ΞΆ β roots_of_unity k R, from h.pow_eq_one) _,
calc fintype.card (roots_of_unity k R)
β€ k : card_roots_of_unity R k
... = fintype.card (zmod k) : (zmod.card k).symm
... = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr (h.zmod_equiv_gpowers).to_equiv
end
lemma eq_pow_of_mem_roots_of_unity {k : β+} {ΞΆ ΞΎ : units R}
(h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ β roots_of_unity k R) :
β (i : β) (hi : i < k), ΞΆ ^ i = ΞΎ :=
begin
obtain β¨n, rflβ© : β n : β€, ΞΆ ^ n = ΞΎ, by rwa [β h.gpowers_eq] at hΞΎ,
have hk0 : (0 : β€) < k := by exact_mod_cast k.pos,
let i := n % k,
have hi0 : 0 β€ i := int.mod_nonneg _ (ne_of_gt hk0),
lift i to β using hi0 with iβ hiβ,
refine β¨iβ, _, _β©,
{ zify, rw [hiβ], exact int.mod_lt_of_pos _ hk0 },
{ have aux := h.gpow_eq_one, rw [β coe_coe] at aux,
rw [β gpow_coe_nat, hiβ, β int.mod_add_div n k, gpow_add, gpow_mul,
aux, one_gpow, mul_one] }
end
lemma eq_pow_of_pow_eq_one {k : β} {ΞΆ ΞΎ : R}
(h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ^ k = 1) (h0 : 0 < k) :
β i < k, ΞΆ ^ i = ΞΎ :=
begin
obtain β¨ΞΆ, rflβ© := h.is_unit h0,
obtain β¨ΞΎ, rflβ© := is_unit_of_pow_eq_one ΞΎ k hΞΎ h0,
obtain β¨k, rflβ© : β k' : β+, k = k' := β¨β¨k, h0β©, rflβ©,
simp only [β units.coe_pow, β units.ext_iff],
rw coe_units_iff at h,
apply h.eq_pow_of_mem_roots_of_unity,
rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hΞΎ, units.coe_one]
end
lemma is_primitive_root_iff' {k : β+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) :
is_primitive_root ΞΎ k β β (i < (k : β)) (hi : i.coprime k), ΞΆ ^ i = ΞΎ :=
begin
split,
{ intro hΞΎ,
obtain β¨i, hik, rflβ© := h.eq_pow_of_mem_roots_of_unity hΞΎ.pow_eq_one,
rw h.pow_iff_coprime k.pos at hΞΎ,
exact β¨i, hik, hΞΎ, rflβ© },
{ rintro β¨i, -, hi, rflβ©, exact h.pow_of_coprime i hi }
end
lemma is_primitive_root_iff {k : β} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) :
is_primitive_root ΞΎ k β β (i < k) (hi : i.coprime k), ΞΆ ^ i = ΞΎ :=
begin
split,
{ intro hΞΎ,
obtain β¨i, hik, rflβ© := h.eq_pow_of_pow_eq_one hΞΎ.pow_eq_one h0,
rw h.pow_iff_coprime h0 at hΞΎ,
exact β¨i, hik, hΞΎ, rflβ© },
{ rintro β¨i, -, hi, rflβ©, exact h.pow_of_coprime i hi }
end
lemma card_roots_of_unity' {n : β+} (h : is_primitive_root ΞΆ n) :
fintype.card (roots_of_unity n R) = n :=
begin
haveI : fact (0 < βn) := β¨n.posβ©,
let e := h.zmod_equiv_gpowers,
haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ e.to_equiv,
calc fintype.card (roots_of_unity n R)
= fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr $ by rw h.gpowers_eq
... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm
... = n : zmod.card n
end
lemma card_roots_of_unity {ΞΆ : R} {n : β+} (h : is_primitive_root ΞΆ n) :
fintype.card (roots_of_unity n R) = n :=
begin
obtain β¨ΞΆ, hΞΆβ© := h.is_unit n.pos,
rw [β hΞΆ, is_primitive_root.coe_units_iff] at h,
exact h.card_roots_of_unity'
end
/-- The cardinality of the multiset `nth_roots βn (1 : R)` is `n`
if there is a primitive root of unity in `R`. -/
lemma card_nth_roots {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(nth_roots n (1 : R)).card = n :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.card_zero, nth_roots_zero] },
rw eq_iff_le_not_lt,
use card_nth_roots n 1,
{ rw [not_lt],
have hcard : fintype.card {x // x β nth_roots n (1 : R)}
β€ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.erase_dup_le _),
rw multiset.card_attach at hcard,
rw β pnat.to_pnat'_coe hpos at hcard h β’,
set m := nat.to_pnat' n,
rw [β fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard,
exact hcard }
end
/-- The multiset `nth_roots βn (1 : R)` has no repeated elements
if there is a primitive root of unity in `R`. -/
lemma nth_roots_nodup {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).nodup :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.nodup_zero, nth_roots_zero] },
apply (@multiset.erase_dup_eq_self R _ _).1,
rw eq_iff_le_not_lt,
split,
{ exact multiset.erase_dup_le (nth_roots n (1 : R)) },
{ by_contra ha,
replace ha := multiset.card_lt_of_lt ha,
rw card_nth_roots h at ha,
have hrw : (nth_roots n (1 : R)).erase_dup.card =
fintype.card {x // x β (nth_roots n (1 : R))},
{ set fs := (β¨(nth_roots n (1 : R)).erase_dup, multiset.nodup_erase_dup _β© : finset R),
rw [β finset.card_mk, β fintype.card_of_subtype fs _],
intro x,
simp only [multiset.mem_erase_dup, finset.mem_mk] },
rw β pnat.to_pnat'_coe hpos at h hrw ha,
set m := nat.to_pnat' n,
rw [hrw, β fintype.card_congr (roots_of_unity_equiv_nth_roots R m),
card_roots_of_unity h] at ha,
exact nat.lt_asymm ha ha }
end
@[simp] lemma card_nth_roots_finset {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(nth_roots_finset n R).card = n :=
by rw [nth_roots_finset, β multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots]
open_locale nat
/-- If an integral domain has a primitive `k`-th root of unity, then it has `Ο k` of them. -/
lemma card_primitive_roots {ΞΆ : R} {k : β} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) :
(primitive_roots k R).card = Ο k :=
begin
symmetry,
refine finset.card_congr (Ξ» i _, ΞΆ ^ i) _ _ _,
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i - hi,
rw mem_primitive_roots h0,
exact h.pow_of_coprime i hi.symm },
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i j hi - hj - H,
exact h.pow_inj hi hj H },
{ simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ],
intros ΞΎ hΞΎ,
rw [mem_primitive_roots h0, h.is_primitive_root_iff h0] at hΞΎ,
rcases hΞΎ with β¨i, hin, hi, Hβ©,
exact β¨i, β¨hin, hi.symmβ©, Hβ© }
end
/-- The sets `primitive_roots k R` are pairwise disjoint. -/
lemma disjoint {k l : β} (hk : 0 < k) (hl : 0 < l) (h : k β l) :
disjoint (primitive_roots k R) (primitive_roots l R) :=
begin
intro z,
simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots, hk, hl, iff_def],
rintro β¨β¨hzk, Hzkβ©, β¨hzl, Hzlβ©β©,
apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl]
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i β£ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots' {ΞΆ : R} {n : β+} (h : is_primitive_root ΞΆ n) :
nth_roots_finset n R = (nat.divisors βn).bUnion (Ξ» i, (primitive_roots i R)) :=
begin
symmetry,
apply finset.eq_of_subset_of_card_le,
{ intros x,
simp only [nth_roots_finset, β multiset.to_finset_eq (nth_roots_nodup h),
exists_prop, finset.mem_bUnion, finset.mem_filter, finset.mem_range, mem_nth_roots,
finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff],
rintro β¨a, β¨d, hdβ©, haβ©,
have hazero : 0 < a,
{ contrapose! hd with ha0,
simp only [nonpos_iff_eq_zero, zero_mul, *] at *,
exact n.ne_zero },
rw mem_primitive_roots hazero at ha,
rw [hd, pow_mul, ha.pow_eq_one, one_pow] },
{ apply le_of_eq,
rw [h.card_nth_roots_finset, finset.card_bUnion],
{ rw [β nat.sum_totient n, nat.filter_dvd_eq_divisors (pnat.ne_zero n), sum_congr rfl]
{ occs := occurrences.pos [1] },
simp only [finset.mem_filter, finset.mem_range, nat.mem_divisors],
rintro k β¨H, hkβ©,
have hdvd := H,
rcases H with β¨d, hdβ©,
rw mul_comm at hd,
rw (h.pow n.pos hd).card_primitive_roots (pnat.pos_of_div_pos hdvd) },
{ intros i hi j hj hdiff,
simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hi hj,
exact disjoint (pnat.pos_of_div_pos hi) (pnat.pos_of_div_pos hj) hdiff } }
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i β£ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots {ΞΆ : R} {n : β} (hpos : 0 < n)
(h : is_primitive_root ΞΆ n) :
nth_roots_finset n R = (nat.divisors n).bUnion (Ξ» i, (primitive_roots i R)) :=
@nth_roots_one_eq_bUnion_primitive_roots' _ _ _ _ β¨n, hposβ© h
end is_domain
section minpoly
open minpoly
variables {n : β} {K : Type*} [field K] {ΞΌ : K} (h : is_primitive_root ΞΌ n) (hpos : 0 < n)
include n ΞΌ h hpos
/--`ΞΌ` is integral over `β€`. -/
lemma is_integral : is_integral β€ ΞΌ :=
begin
use (X ^ n - 1),
split,
{ exact (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm) },
{ simp only [((is_primitive_root.iff_def ΞΌ n).mp h).left, evalβ_one, evalβ_X_pow, evalβ_sub,
sub_self] }
end
variables [char_zero K]
/--The minimal polynomial of a root of unity `ΞΌ` divides `X ^ n - 1`. -/
lemma minpoly_dvd_X_pow_sub_one : minpoly β€ ΞΌ β£ X ^ n - 1 :=
begin
apply minpoly.gcd_domain_dvd β (is_integral h hpos) (polynomial.monic.is_primitive
(monic_X_pow_sub_C 1 (ne_of_lt hpos).symm)),
simp only [((is_primitive_root.iff_def ΞΌ n).mp h).left, aeval_X_pow, ring_hom.eq_int_cast,
int.cast_one, aeval_one, alg_hom.map_sub, sub_self]
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `ΞΌ` is separable. -/
lemma separable_minpoly_mod {p : β} [fact p.prime] (hdiv : Β¬p β£ n) :
separable (map (int.cast_ring_hom (zmod p)) (minpoly β€ ΞΌ)) :=
begin
have hdvd : (map (int.cast_ring_hom (zmod p))
(minpoly β€ ΞΌ)) β£ X ^ n - 1,
{ simpa [map_pow, map_X, map_one, map_sub] using
ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p)))
(minpoly_dvd_X_pow_sub_one h hpos) },
refine separable.of_dvd (separable_X_pow_sub_C 1 _ one_ne_zero) hdvd,
by_contra hzero,
exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero)
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `ΞΌ` is squarefree. -/
lemma squarefree_minpoly_mod {p : β} [fact p.prime] (hdiv : Β¬ p β£ n) :
squarefree (map (int.cast_ring_hom (zmod p)) (minpoly β€ ΞΌ)) :=
(separable_minpoly_mod h hpos hdiv).squarefree
/- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of
`ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `expand β€ p Q`. -/
lemma minpoly_dvd_expand {p : β} (hprime : nat.prime p) (hdiv : Β¬ p β£ n) :
minpoly β€ ΞΌ β£
expand β€ p (minpoly β€ (ΞΌ ^ p)) :=
begin
apply minpoly.gcd_domain_dvd β (h.is_integral hpos),
{ apply monic.is_primitive,
rw [polynomial.monic, leading_coeff, nat_degree_expand, mul_comm, coeff_expand_mul'
(nat.prime.pos hprime), β leading_coeff, β polynomial.monic],
exact minpoly.monic (is_integral (pow_of_prime h hprime hdiv) hpos) },
{ rw [aeval_def, coe_expand, β comp, evalβ_eq_eval_map, map_comp, map_pow, map_X, eval_comp,
eval_pow, eval_X, β evalβ_eq_eval_map, β aeval_def],
exact minpoly.aeval _ _ }
end
/- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of
`ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q ^ p` modulo `p`. -/
lemma minpoly_dvd_pow_mod {p : β} [hprime : fact p.prime] (hdiv : Β¬ p β£ n) :
map (int.cast_ring_hom (zmod p)) (minpoly β€ ΞΌ) β£
map (int.cast_ring_hom (zmod p)) (minpoly β€ (ΞΌ ^ p)) ^ p :=
begin
set Q := minpoly β€ (ΞΌ ^ p),
have hfrob : map (int.cast_ring_hom (zmod p)) Q ^ p =
map (int.cast_ring_hom (zmod p)) (expand β€ p Q),
by rw [β zmod.expand_card, map_expand hprime.1.pos],
rw [hfrob],
apply ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))),
exact minpoly_dvd_expand h hpos hprime.1 hdiv
end
/- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of
`ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q` modulo `p`. -/
lemma minpoly_dvd_mod_p {p : β} [hprime : fact p.prime] (hdiv : Β¬ p β£ n) :
map (int.cast_ring_hom (zmod p)) (minpoly β€ ΞΌ) β£
map (int.cast_ring_hom (zmod p)) (minpoly β€ (ΞΌ ^ p)) :=
(unique_factorization_monoid.dvd_pow_iff_dvd_of_squarefree (squarefree_minpoly_mod h
hpos hdiv) hprime.1.ne_zero).1 (minpoly_dvd_pow_mod h hpos hdiv)
/-- If `p` is a prime that does not divide `n`,
then the minimal polynomials of a primitive `n`-th root of unity `ΞΌ`
and of `ΞΌ ^ p` are the same. -/
lemma minpoly_eq_pow {p : β} [hprime : fact p.prime] (hdiv : Β¬ p β£ n) :
minpoly β€ ΞΌ = minpoly β€ (ΞΌ ^ p) :=
begin
by_contra hdiff,
set P := minpoly β€ ΞΌ,
set Q := minpoly β€ (ΞΌ ^ p),
have Pmonic : P.monic := minpoly.monic (h.is_integral hpos),
have Qmonic : Q.monic := minpoly.monic ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have Pirr : irreducible P := minpoly.irreducible (h.is_integral hpos),
have Qirr : irreducible Q :=
minpoly.irreducible ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have PQprim : is_primitive (P * Q) := Pmonic.is_primitive.mul Qmonic.is_primitive,
have prod : P * Q β£ X ^ n - 1,
{ rw [(is_primitive.int.dvd_iff_map_cast_dvd_map_cast (P * Q) (X ^ n - 1) PQprim
(monic_X_pow_sub_C (1 : β€) (ne_of_gt hpos)).is_primitive), map_mul],
refine is_coprime.mul_dvd _ _ _,
{ have aux := is_primitive.int.irreducible_iff_irreducible_map_cast Pmonic.is_primitive,
refine (dvd_or_coprime _ _ (aux.1 Pirr)).resolve_left _,
rw map_dvd_map (int.cast_ring_hom β) int.cast_injective Pmonic,
intro hdiv,
refine hdiff (eq_of_monic_of_associated Pmonic Qmonic _),
exact associated_of_dvd_dvd hdiv (Pirr.dvd_symm Qirr hdiv) },
{ apply (map_dvd_map (int.cast_ring_hom β) int.cast_injective Pmonic).2,
exact minpoly_dvd_X_pow_sub_one h hpos },
{ apply (map_dvd_map (int.cast_ring_hom β) int.cast_injective Qmonic).2,
exact minpoly_dvd_X_pow_sub_one (pow_of_prime h hprime.1 hdiv) hpos } },
replace prod := ring_hom.map_dvd ((map_ring_hom (int.cast_ring_hom (zmod p)))) prod,
rw [coe_map_ring_hom, map_mul, map_sub, map_one, map_pow, map_X] at prod,
obtain β¨R, hRβ© := minpoly_dvd_mod_p h hpos hdiv,
rw [hR, β mul_assoc, β map_mul, β sq, map_pow] at prod,
have habs : map (int.cast_ring_hom (zmod p)) P ^ 2 β£ map (int.cast_ring_hom (zmod p)) P ^ 2 * R,
{ use R },
replace habs := lt_of_lt_of_le (enat.coe_lt_coe.2 one_lt_two)
(multiplicity.le_multiplicity_of_pow_dvd (dvd_trans habs prod)),
have hfree : squarefree (X ^ n - 1 : polynomial (zmod p)),
{ refine squarefree_X_pow_sub_C 1 _ one_ne_zero,
by_contra hzero,
exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero) },
cases (multiplicity.squarefree_iff_multiplicity_le_one (X ^ n - 1)).1 hfree
(map (int.cast_ring_hom (zmod p)) P) with hle hunit,
{ rw nat.cast_one at habs, exact hle.not_lt habs },
{ replace hunit := degree_eq_zero_of_is_unit hunit,
rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom (zmod p)) _ at hunit,
{ exact (minpoly.degree_pos (is_integral h hpos)).ne' hunit },
simp only [Pmonic, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def,
not_false_iff, one_ne_zero] }
end
/-- If `m : β` is coprime with `n`,
then the minimal polynomials of a primitive `n`-th root of unity `ΞΌ`
and of `ΞΌ ^ m` are the same. -/
lemma minpoly_eq_pow_coprime {m : β} (hcop : nat.coprime m n) :
minpoly β€ ΞΌ = minpoly β€ (ΞΌ ^ m) :=
begin
revert n hcop,
refine unique_factorization_monoid.induction_on_prime m _ _ _,
{ intros n hn h hpos,
congr,
simpa [(nat.coprime_zero_left n).mp hn] using h },
{ intros u hunit n hcop h hpos,
congr,
simp [nat.is_unit_iff.mp hunit] },
{ intros a p ha hprime hind n hcop h hpos,
rw hind (nat.coprime.coprime_mul_left hcop) h hpos, clear hind,
replace hprime := nat.prime_iff.2 hprime,
have hdiv := (nat.prime.coprime_iff_not_dvd hprime).1 (nat.coprime.coprime_mul_right hcop),
haveI := fact.mk hprime,
rw [minpoly_eq_pow
(h.pow_of_coprime a (nat.coprime.coprime_mul_left hcop)) hpos hdiv],
congr' 1,
ring_exp }
end
/-- If `m : β` is coprime with `n`,
then the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`
has `ΞΌ ^ m` as root. -/
lemma pow_is_root_minpoly {m : β} (hcop : nat.coprime m n) :
is_root (map (int.cast_ring_hom K) (minpoly β€ ΞΌ)) (ΞΌ ^ m) :=
by simpa [minpoly_eq_pow_coprime h hpos hcop, eval_map, aeval_def (ΞΌ ^ m) _]
using minpoly.aeval β€ (ΞΌ ^ m)
/-- `primitive_roots n K` is a subset of the roots of the minimal polynomial of a primitive
`n`-th root of unity `ΞΌ`. -/
lemma is_roots_of_minpoly : primitive_roots n K β (map (int.cast_ring_hom K)
(minpoly β€ ΞΌ)).roots.to_finset :=
begin
intros x hx,
obtain β¨m, hle, hcop, rflβ© := (is_primitive_root_iff h hpos).1 ((mem_primitive_roots hpos).1 hx),
simpa [multiset.mem_to_finset,
mem_roots (map_monic_ne_zero $ minpoly.monic $ is_integral h hpos)]
using pow_is_root_minpoly h hpos hcop
end
/-- The degree of the minimal polynomial of `ΞΌ` is at least `totient n`. -/
lemma totient_le_degree_minpoly : nat.totient n β€ (minpoly β€ ΞΌ).nat_degree :=
let P : polynomial β€ := minpoly β€ ΞΌ,-- minimal polynomial of `ΞΌ`
P_K : polynomial K := map (int.cast_ring_hom K) P -- minimal polynomial of `ΞΌ` sent to `K[X]`
in calc
n.totient = (primitive_roots n K).card : (h.card_primitive_roots hpos).symm
... β€ P_K.roots.to_finset.card : finset.card_le_of_subset (is_roots_of_minpoly h hpos)
... β€ P_K.roots.card : multiset.to_finset_card_le _
... β€ P_K.nat_degree : (card_roots' $ map_monic_ne_zero
(minpoly.monic $ is_integral h hpos))
... β€ P.nat_degree : nat_degree_map_le _ _
end minpoly
end is_primitive_root
|
e625dacdbb4948012ee3b3f71491267465827a61 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/field_theory/ratfunc.lean | 234b2cfa9914f9de6035fe874a2090a4cae9fecb | [
"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,954 | lean | /-
Copyright (c) 2021 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import ring_theory.euclidean_domain
import ring_theory.laurent_series
import ring_theory.localization.fraction_ring
import ring_theory.polynomial.content
/-!
# The field of rational functions
This file defines the field `ratfunc K` of rational functions over a field `K`,
and shows it is the field of fractions of `polynomial K`.
## Main definitions
Working with rational functions as polynomials:
- `ratfunc.field` provides a field structure
- `ratfunc.C` is the constant polynomial
- `ratfunc.X` is the indeterminate
- `ratfunc.eval` evaluates a rational function given a value for the indeterminate
You can use `is_fraction_ring` API to treat `ratfunc` as the field of fractions of polynomials:
* `algebra_map K[X] (ratfunc K)` maps polynomials to rational functions
* `is_fraction_ring.alg_equiv` maps other fields of fractions of `polynomial K` to `ratfunc K`,
in particular:
* `fraction_ring.alg_equiv K[X] (ratfunc K)` maps the generic field of
fraction construction to `ratfunc K`. Combine this with `alg_equiv.restrict_scalars` to change
the `fraction_ring K[X] ββ[polynomial K] ratfunc K` to
`fraction_ring K[X] ββ[K] ratfunc K`.
Working with rational functions as fractions:
- `ratfunc.num` and `ratfunc.denom` give the numerator and denominator.
These values are chosen to be coprime and such that `ratfunc.denom` is monic.
Embedding of rational functions into Laurent series, provided as a coercion, utilizing
the underlying `ratfunc.coe_alg_hom`.
Lifting homomorphisms of polynomials to other types, by mapping and dividing, as long
as the homomorphism retains the non-zero-divisor property:
- `ratfunc.lift_monoid_with_zero_hom` lifts a `polynomial K β*β Gβ` to
a `ratfunc K β*β Gβ`, where `[comm_ring K] [comm_group_with_zero Gβ]`
- `ratfunc.lift_ring_hom` lifts a `polynomial K β+* L` to a `ratfunc K β+* L`,
where `[comm_ring K] [field L]`
- `ratfunc.lift_alg_hom` lifts a `polynomial K ββ[S] L` to a `ratfunc K ββ[S] L`,
where `[comm_ring K] [field L] [comm_semiring S] [algebra S K[X]] [algebra S L]`
This is satisfied by injective homs.
We also have lifting homomorphisms of polynomials to other polynomials,
with the same condition on retaining the non-zero-divisor property across the map:
- `ratfunc.map` lifts `polynomial K β* R[X]` when `[comm_ring K] [comm_ring R]`
- `ratfunc.map_ring_hom` lifts `polynomial K β+* R[X]` when `[comm_ring K] [comm_ring R]`
- `ratfunc.map_alg_hom` lifts `polynomial K ββ[S] R[X]` when
`[comm_ring K] [is_domain K] [comm_ring R] [is_domain R]`
We also have a set of recursion and induction principles:
- `ratfunc.lift_on`: define a function by mapping a fraction of polynomials `p/q` to `f p q`,
if `f` is well-defined in the sense that `p/q = p'/q' β f p q = f p' q'`.
- `ratfunc.lift_on'`: define a function by mapping a fraction of polynomials `p/q` to `f p q`,
if `f` is well-defined in the sense that `f (a * p) (a * q) = f p' q'`.
- `ratfunc.induction_on`: if `P` holds on `p / q` for all polynomials `p q`, then `P` holds on all
rational functions
We define the degree of a rational function, with values in `β€`:
- `int_degree` is the degree of a rational function, defined as the difference between the
`nat_degree` of its numerator and the `nat_degree` of its denominator. In particular,
`int_degree 0 = 0`.
## Implementation notes
To provide good API encapsulation and speed up unification problems,
`ratfunc` is defined as a structure, and all operations are `@[irreducible] def`s
We need a couple of maps to set up the `field` and `is_fraction_ring` structure,
namely `ratfunc.of_fraction_ring`, `ratfunc.to_fraction_ring`, `ratfunc.mk` and
`ratfunc.to_fraction_ring_ring_equiv`.
All these maps get `simp`ed to bundled morphisms like `algebra_map K[X] (ratfunc K)`
and `is_localization.alg_equiv`.
There are separate lifts and maps of homomorphisms, to provide routes of lifting even when
the codomain is not a field or even an integral domain.
## References
* [Kleiman, *Misconceptions about $K_X$*][kleiman1979]
* https://freedommathdance.blogspot.com/2012/11/misconceptions-about-kx.html
* https://stacks.math.columbia.edu/tag/01X1
-/
noncomputable theory
open_locale classical
open_locale non_zero_divisors polynomial
universes u v
variables (K : Type u) [hring : comm_ring K] [hdomain : is_domain K]
include hring
/-- `ratfunc K` is `K(x)`, the field of rational functions over `K`.
The inclusion of polynomials into `ratfunc` is `algebra_map K[X] (ratfunc K)`,
the maps between `ratfunc K` and another field of fractions of `polynomial K`,
especially `fraction_ring K[X]`, are given by `is_localization.algebra_equiv`.
-/
structure ratfunc : Type u := of_fraction_ring ::
(to_fraction_ring : fraction_ring K[X])
namespace ratfunc
variables {K}
section rec
/-! ### Constructing `ratfunc`s and their induction principles -/
lemma of_fraction_ring_injective : function.injective (of_fraction_ring : _ β ratfunc K) :=
Ξ» x y, of_fraction_ring.inj
lemma to_fraction_ring_injective :
function.injective (to_fraction_ring : _ β fraction_ring K[X])
| β¨xβ© β¨yβ© rfl := rfl
/-- Non-dependent recursion principle for `ratfunc K`:
To construct a term of `P : Sort*` out of `x : ratfunc K`,
it suffices to provide a constructor `f : Ξ (p q : K[X]), P`
and a proof that `f p q = f p' q'` for all `p q p' q'` such that `p * q' = p' * q` where
both `q` and `q'` are not zero divisors, stated as `q β K[X]β°`, `q' β K[X]β°`.
If considering `K` as an integral domain, this is the same as saying that
we construct a value of `P` for such elements of `ratfunc K` by setting
`lift_on (p / q) f _ = f p q`.
When `[is_domain K]`, one can use `ratfunc.lift_on'`, which has the stronger requirement
of `β {p q a : K[X]} (hq : q β 0) (ha : a β 0), f (a * p) (a * q) = f p q)`.
-/
@[irreducible] protected def lift_on {P : Sort v} (x : ratfunc K)
(f : β (p q : K[X]), P)
(H : β {p q p' q'} (hq : q β K[X]β°) (hq' : q' β K[X]β°), p * q' = p' * q β
f p q = f p' q') :
P :=
localization.lift_on (to_fraction_ring x) (Ξ» p q, f p q) (Ξ» p p' q q' h, H q.2 q'.2
(let β¨β¨c, hcβ©, mul_eqβ© := (localization.r_iff_exists).mp h in
mul_cancel_right_coe_non_zero_divisor.mp mul_eq))
lemma lift_on_of_fraction_ring_mk {P : Sort v} (n : K[X]) (d : K[X]β°)
(f : β (p q : K[X]), P)
(H : β {p q p' q'} (hq : q β K[X]β°) (hq' : q' β K[X]β°), p * q' = p' * q β
f p q = f p' q') :
ratfunc.lift_on (of_fraction_ring (localization.mk n d)) f @H = f n d :=
begin
unfold ratfunc.lift_on,
exact localization.lift_on_mk _ _ _ _
end
include hdomain
/-- `ratfunc.mk (p q : K[X])` is `p / q` as a rational function.
If `q = 0`, then `mk` returns 0.
This is an auxiliary definition used to define an `algebra` structure on `ratfunc`;
the `simp` normal form of `mk p q` is `algebra_map _ _ p / algebra_map _ _ q`.
-/
@[irreducible] protected def mk (p q : K[X]) : ratfunc K :=
of_fraction_ring (algebra_map _ _ p / algebra_map _ _ q)
lemma mk_eq_div' (p q : K[X]) :
ratfunc.mk p q = of_fraction_ring (algebra_map _ _ p / algebra_map _ _ q) :=
by unfold ratfunc.mk
lemma mk_zero (p : K[X]) : ratfunc.mk p 0 = of_fraction_ring 0 :=
by rw [mk_eq_div', ring_hom.map_zero, div_zero]
lemma mk_coe_def (p : K[X]) (q : K[X]β°) :
ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p q) :=
by simp only [mk_eq_div', β localization.mk_eq_mk', fraction_ring.mk_eq_div]
lemma mk_def_of_mem (p : K[X]) {q} (hq : q β K[X]β°) :
ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p β¨q, hqβ©) :=
by simp only [β mk_coe_def, set_like.coe_mk]
lemma mk_def_of_ne (p : K[X]) {q : K[X]} (hq : q β 0) :
ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p
β¨q, mem_non_zero_divisors_iff_ne_zero.mpr hqβ©) :=
mk_def_of_mem p _
lemma mk_eq_localization_mk (p : K[X]) {q : K[X]} (hq : q β 0) :
ratfunc.mk p q = of_fraction_ring (localization.mk p
β¨q, mem_non_zero_divisors_iff_ne_zero.mpr hqβ©) :=
by rw [mk_def_of_ne, localization.mk_eq_mk']
lemma mk_one' (p : K[X]) : ratfunc.mk p 1 = of_fraction_ring (algebra_map _ _ p) :=
by rw [β is_localization.mk'_one (fraction_ring K[X]) p, β mk_coe_def, submonoid.coe_one]
lemma mk_eq_mk {p q p' q' : K[X]} (hq : q β 0) (hq' : q' β 0) :
ratfunc.mk p q = ratfunc.mk p' q' β p * q' = p' * q :=
by rw [mk_def_of_ne _ hq, mk_def_of_ne _ hq', of_fraction_ring_injective.eq_iff,
is_localization.mk'_eq_iff_eq, set_like.coe_mk, set_like.coe_mk,
(is_fraction_ring.injective K[X] (fraction_ring K[X])).eq_iff]
lemma lift_on_mk {P : Sort v} (p q : K[X])
(f : β (p q : K[X]), P) (f0 : β p, f p 0 = f 0 1)
(H' : β {p q p' q'} (hq : q β 0) (hq' : q' β 0), p * q' = p' * q β f p q = f p' q')
(H : β {p q p' q'} (hq : q β K[X]β°) (hq' : q' β K[X]β°), p * q' = p' * q β
f p q = f p' q' :=
Ξ» p q p' q' hq hq' h, H' (non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq') h) :
(ratfunc.mk p q).lift_on f @H = f p q :=
begin
by_cases hq : q = 0,
{ subst hq,
simp only [mk_zero, f0, β localization.mk_zero 1, localization.lift_on_mk,
lift_on_of_fraction_ring_mk, submonoid.coe_one], },
{ simp only [mk_eq_localization_mk _ hq, localization.lift_on_mk, lift_on_of_fraction_ring_mk,
set_like.coe_mk] }
end
lemma lift_on_condition_of_lift_on'_condition {P : Sort v} {f : β (p q : K[X]), P}
(H : β {p q a} (hq : q β 0) (ha : a β 0), f (a * p) (a * q) = f p q)
β¦p q p' q' : K[X]β¦ (hq : q β 0) (hq' : q' β 0) (h : p * q' = p' * q) :
f p q = f p' q' :=
begin
have H0 : f 0 q = f 0 q',
{ calc f 0 q = f (q' * 0) (q' * q) : (H hq hq').symm
... = f (q * 0) (q * q') : by rw [mul_zero, mul_zero, mul_comm]
... = f 0 q' : H hq' hq },
by_cases hp : p = 0,
{ simp only [hp, hq, zero_mul, or_false, zero_eq_mul] at β’ h, rw [h, H0] },
by_cases hp' : p' = 0,
{ simpa only [hp, hp', hq', zero_mul, or_self, mul_eq_zero] using h },
calc f p q = f (p' * p) (p' * q) : (H hq hp').symm
... = f (p * p') (p * q') : by rw [mul_comm p p', h]
... = f p' q' : H hq' hp
end
-- f
/-- Non-dependent recursion principle for `ratfunc K`: if `f p q : P` for all `p q`,
such that `f (a * p) (a * q) = f p q`, then we can find a value of `P`
for all elements of `ratfunc K` by setting `lift_on' (p / q) f _ = f p q`.
The value of `f p 0` for any `p` is never used and in principle this may be anything,
although many usages of `lift_on'` assume `f p 0 = f 0 1`.
-/
@[irreducible] protected def lift_on' {P : Sort v} (x : ratfunc K)
(f : β (p q : K[X]), P)
(H : β {p q a} (hq : q β 0) (ha : a β 0), f (a * p) (a * q) = f p q) :
P :=
x.lift_on f (Ξ» p q p' q' hq hq', lift_on_condition_of_lift_on'_condition @H
(non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq'))
lemma lift_on'_mk {P : Sort v} (p q : K[X])
(f : β (p q : K[X]), P) (f0 : β p, f p 0 = f 0 1)
(H : β {p q a} (hq : q β 0) (ha : a β 0), f (a * p) (a * q) = f p q) :
(ratfunc.mk p q).lift_on' f @H = f p q :=
begin
rw [ratfunc.lift_on', ratfunc.lift_on_mk _ _ _ f0],
exact lift_on_condition_of_lift_on'_condition @H
end
/-- Induction principle for `ratfunc K`: if `f p q : P (ratfunc.mk p q)` for all `p q`,
then `P` holds on all elements of `ratfunc K`.
See also `induction_on`, which is a recursion principle defined in terms of `algebra_map`.
-/
@[irreducible] protected lemma induction_on' {P : ratfunc K β Prop} :
Ξ (x : ratfunc K) (f : β (p q : K[X]) (hq : q β 0), P (ratfunc.mk p q)), P x
| β¨xβ© f := localization.induction_on x
(Ξ» β¨p, qβ©, by simpa only [mk_coe_def, localization.mk_eq_mk'] using f p q
(mem_non_zero_divisors_iff_ne_zero.mp q.2))
end rec
section field
/-! ### Defining the field structure -/
/-- The zero rational function. -/
@[irreducible] protected def zero : ratfunc K := β¨0β©
instance : has_zero (ratfunc K) := β¨ratfunc.zeroβ©
lemma of_fraction_ring_zero : (of_fraction_ring 0 : ratfunc K) = 0 :=
by unfold has_zero.zero ratfunc.zero
/-- Addition of rational functions. -/
@[irreducible] protected def add : ratfunc K β ratfunc K β ratfunc K
| β¨pβ© β¨qβ© := β¨p + qβ©
instance : has_add (ratfunc K) := β¨ratfunc.addβ©
lemma of_fraction_ring_add (p q : fraction_ring K[X]) :
of_fraction_ring (p + q) = of_fraction_ring p + of_fraction_ring q :=
by unfold has_add.add ratfunc.add
/-- Subtraction of rational functions. -/
@[irreducible] protected def sub : ratfunc K β ratfunc K β ratfunc K
| β¨pβ© β¨qβ© := β¨p - qβ©
instance : has_sub (ratfunc K) := β¨ratfunc.subβ©
lemma of_fraction_ring_sub (p q : fraction_ring K[X]) :
of_fraction_ring (p - q) = of_fraction_ring p - of_fraction_ring q :=
by unfold has_sub.sub ratfunc.sub
/-- Additive inverse of a rational function. -/
@[irreducible] protected def neg : ratfunc K β ratfunc K
| β¨pβ© := β¨-pβ©
instance : has_neg (ratfunc K) := β¨ratfunc.negβ©
lemma of_fraction_ring_neg (p : fraction_ring K[X]) :
of_fraction_ring (-p) = - of_fraction_ring p :=
by unfold has_neg.neg ratfunc.neg
/-- The multiplicative unit of rational functions. -/
@[irreducible] protected def one : ratfunc K := β¨1β©
instance : has_one (ratfunc K) := β¨ratfunc.oneβ©
lemma of_fraction_ring_one : (of_fraction_ring 1 : ratfunc K) = 1 :=
by unfold has_one.one ratfunc.one
/-- Multiplication of rational functions. -/
@[irreducible] protected def mul : ratfunc K β ratfunc K β ratfunc K
| β¨pβ© β¨qβ© := β¨p * qβ©
instance : has_mul (ratfunc K) := β¨ratfunc.mulβ©
lemma of_fraction_ring_mul (p q : fraction_ring K[X]) :
of_fraction_ring (p * q) = of_fraction_ring p * of_fraction_ring q :=
by unfold has_mul.mul ratfunc.mul
include hdomain
/-- Division of rational functions. -/
@[irreducible] protected def div : ratfunc K β ratfunc K β ratfunc K
| β¨pβ© β¨qβ© := β¨p / qβ©
instance : has_div (ratfunc K) := β¨ratfunc.divβ©
lemma of_fraction_ring_div (p q : fraction_ring K[X]) :
of_fraction_ring (p / q) = of_fraction_ring p / of_fraction_ring q :=
by unfold has_div.div ratfunc.div
/-- Multiplicative inverse of a rational function. -/
@[irreducible] protected def inv : ratfunc K β ratfunc K
| β¨pβ© := β¨pβ»ΒΉβ©
instance : has_inv (ratfunc K) := β¨ratfunc.invβ©
lemma of_fraction_ring_inv (p : fraction_ring K[X]) :
of_fraction_ring (pβ»ΒΉ) = (of_fraction_ring p)β»ΒΉ :=
by unfold has_inv.inv ratfunc.inv
-- Auxiliary lemma for the `field` instance
lemma mul_inv_cancel : β {p : ratfunc K} (hp : p β 0), p * pβ»ΒΉ = 1
| β¨pβ© h := have p β 0 := Ξ» hp, h $ by rw [hp, of_fraction_ring_zero],
by simpa only [β of_fraction_ring_inv, β of_fraction_ring_mul, β of_fraction_ring_one]
using _root_.mul_inv_cancel this
section has_smul
omit hdomain
variables {R : Type*}
/-- Scalar multiplication of rational functions. -/
@[irreducible] protected def smul [has_smul R (fraction_ring K[X])] :
R β ratfunc K β ratfunc K
| r β¨pβ© := β¨r β’ pβ©
@[nolint fails_quickly] -- cannot reproduce
instance [has_smul R (fraction_ring K[X])] : has_smul R (ratfunc K) :=
β¨ratfunc.smulβ©
lemma of_fraction_ring_smul [has_smul R (fraction_ring K[X])]
(c : R) (p : fraction_ring K[X]) :
of_fraction_ring (c β’ p) = c β’ of_fraction_ring p :=
by unfold has_smul.smul ratfunc.smul
lemma to_fraction_ring_smul [has_smul R (fraction_ring K[X])]
(c : R) (p : ratfunc K) :
to_fraction_ring (c β’ p) = c β’ to_fraction_ring p :=
by { cases p, rw βof_fraction_ring_smul }
lemma smul_eq_C_smul (x : ratfunc K) (r : K) :
r β’ x = polynomial.C r β’ x :=
begin
cases x,
induction x,
{ rw [βof_fraction_ring_smul, βof_fraction_ring_smul, localization.smul_mk, localization.smul_mk,
smul_eq_mul, polynomial.smul_eq_C_mul] },
{ simp only }
end
include hdomain
variables [monoid R] [distrib_mul_action R K[X]]
variables [htower : is_scalar_tower R K[X] K[X]]
include htower
lemma mk_smul (c : R) (p q : K[X]) :
ratfunc.mk (c β’ p) q = c β’ ratfunc.mk p q :=
begin
by_cases hq : q = 0,
{ rw [hq, mk_zero, mk_zero, βof_fraction_ring_smul, smul_zero] },
{ rw [mk_eq_localization_mk _ hq, mk_eq_localization_mk _ hq,
βlocalization.smul_mk, βof_fraction_ring_smul] }
end
instance : is_scalar_tower R K[X] (ratfunc K) :=
β¨Ξ» c p q, q.induction_on' (Ξ» q r _, by rw [β mk_smul, smul_assoc, mk_smul, mk_smul])β©
end has_smul
variables (K)
omit hdomain
instance [subsingleton K] : subsingleton (ratfunc K) :=
to_fraction_ring_injective.subsingleton
instance : inhabited (ratfunc K) :=
β¨0β©
instance [nontrivial K] : nontrivial (ratfunc K) :=
of_fraction_ring_injective.nontrivial
/-- `ratfunc K` is isomorphic to the field of fractions of `polynomial K`, as rings.
This is an auxiliary definition; `simp`-normal form is `is_localization.alg_equiv`.
-/
@[simps apply] def to_fraction_ring_ring_equiv : ratfunc K β+* fraction_ring K[X] :=
{ to_fun := to_fraction_ring,
inv_fun := of_fraction_ring,
left_inv := Ξ» β¨_β©, rfl,
right_inv := Ξ» _, rfl,
map_add' := Ξ» β¨_β© β¨_β©, by simp [βof_fraction_ring_add],
map_mul' := Ξ» β¨_β© β¨_β©, by simp [βof_fraction_ring_mul] }
omit hring
/-- Solve equations for `ratfunc K` by working in `fraction_ring K[X]`. -/
meta def frac_tac : tactic unit :=
`[repeat { rintro (β¨β© : ratfunc _) },
simp only [β of_fraction_ring_zero, β of_fraction_ring_add, β of_fraction_ring_sub,
β of_fraction_ring_neg, β of_fraction_ring_one, β of_fraction_ring_mul, β of_fraction_ring_div,
β of_fraction_ring_inv,
add_assoc, zero_add, add_zero, mul_assoc, mul_zero, mul_one, mul_add, inv_zero,
add_comm, add_left_comm, mul_comm, mul_left_comm, sub_eq_add_neg, div_eq_mul_inv,
add_mul, zero_mul, one_mul, neg_mul, mul_neg, add_right_neg]]
/-- Solve equations for `ratfunc K` by applying `ratfunc.induction_on`. -/
meta def smul_tac : tactic unit :=
`[repeat { rintro (β¨β© : ratfunc _) <|> intro },
simp_rw [βof_fraction_ring_smul],
simp only [add_comm, mul_comm, zero_smul, succ_nsmul, zsmul_eq_mul, mul_add, mul_one, mul_zero,
neg_add, mul_neg,
int.of_nat_eq_coe, int.coe_nat_succ, int.cast_zero, int.cast_add, int.cast_one,
int.cast_neg_succ_of_nat, int.cast_coe_nat, nat.cast_succ,
localization.mk_zero, localization.add_mk_self, localization.neg_mk,
of_fraction_ring_zero, β of_fraction_ring_add, β of_fraction_ring_neg]]
include hring
instance : comm_ring (ratfunc K) :=
{ add := (+),
add_assoc := by frac_tac,
add_comm := by frac_tac,
zero := 0,
zero_add := by frac_tac,
add_zero := by frac_tac,
neg := has_neg.neg,
add_left_neg := by frac_tac,
sub := has_sub.sub,
sub_eq_add_neg := by frac_tac,
mul := (*),
mul_assoc := by frac_tac,
mul_comm := by frac_tac,
left_distrib := by frac_tac,
right_distrib := by frac_tac,
one := 1,
one_mul := by frac_tac,
mul_one := by frac_tac,
nsmul := (β’),
nsmul_zero' := by smul_tac,
nsmul_succ' := Ξ» _, by smul_tac,
zsmul := (β’),
zsmul_zero' := by smul_tac,
zsmul_succ' := Ξ» _, by smul_tac,
zsmul_neg' := Ξ» _, by smul_tac,
npow := npow_rec }
variables {K}
section lift_hom
variables {Gβ L R S F : Type*} [comm_group_with_zero Gβ] [field L] [comm_ring R] [comm_ring S]
omit hring
/-- Lift a monoid homomorphism that maps polynomials `Ο : R[X] β* S[X]`
to a `ratfunc R β* ratfunc S`,
on the condition that `Ο` maps non zero divisors to non zero divisors,
by mapping both the numerator and denominator and quotienting them. -/
def map [monoid_hom_class F R[X] S[X]] (Ο : F)
(hΟ : R[X]β° β€ S[X]β°.comap Ο) :
ratfunc R β* ratfunc S :=
{ to_fun := Ξ» f, ratfunc.lift_on f (Ξ» n d, if h : Ο d β S[X]β°
then of_fraction_ring (localization.mk (Ο n) β¨Ο d, hβ©) else 0) $ Ξ» p q p' q' hq hq' h,
begin
rw [dif_pos, dif_pos, of_fraction_ring.inj_eq, localization.mk_eq_mk_iff],
rotate,
{ exact hΟ hq' },
{ exact hΟ hq },
refine localization.r_of_eq _,
simpa only [map_mul] using (congr_arg Ο h).symm,
end,
map_one' := begin
rw [βof_fraction_ring_one, βlocalization.mk_one, lift_on_of_fraction_ring_mk, dif_pos],
{ simpa using of_fraction_ring_one },
{ simpa using submonoid.one_mem _}
end,
map_mul' := Ξ» x y, begin
cases x, cases y, induction x with p q, induction y with p' q',
{ have hq : Ο q β S[X]β° := hΟ q.prop,
have hq' : Ο q' β S[X]β° := hΟ q'.prop,
have hqq' : Ο β(q * q') β S[X]β°,
{ simpa using submonoid.mul_mem _ hq hq' },
simp_rw [βof_fraction_ring_mul, localization.mk_mul, lift_on_of_fraction_ring_mk, dif_pos hq,
dif_pos hq', dif_pos hqq', βof_fraction_ring_mul, submonoid.coe_mul, map_mul,
localization.mk_mul, submonoid.mk_mul_mk] },
{ refl },
{ refl }
end }
lemma map_apply_of_fraction_ring_mk [monoid_hom_class F R[X] S[X]] (Ο : F)
(hΟ : R[X]β° β€ S[X]β°.comap Ο) (n : R[X]) (d : R[X]β°) :
map Ο hΟ (of_fraction_ring (localization.mk n d)) =
of_fraction_ring (localization.mk (Ο n) β¨Ο d, hΟ d.propβ©) :=
begin
convert lift_on_of_fraction_ring_mk _ _ _ _,
rw dif_pos
end
lemma map_injective [monoid_hom_class F R[X] S[X]] (Ο : F)
(hΟ : R[X]β° β€ S[X]β°.comap Ο) (hf : function.injective Ο) :
function.injective (map Ο hΟ) :=
begin
rintro β¨xβ© β¨yβ© h, induction x, induction y,
{ simpa only [map_apply_of_fraction_ring_mk, of_fraction_ring_injective.eq_iff,
localization.mk_eq_mk_iff, localization.r_iff_exists,
mul_cancel_right_coe_non_zero_divisor, exists_const, set_like.coe_mk, βmap_mul,
hf.eq_iff] using h },
{ refl },
{ refl }
end
/-- Lift a ring homomorphism that maps polynomials `Ο : R[X] β+* S[X]`
to a `ratfunc R β+* ratfunc S`,
on the condition that `Ο` maps non zero divisors to non zero divisors,
by mapping both the numerator and denominator and quotienting them. -/
def map_ring_hom [ring_hom_class F R[X] S[X]] (Ο : F)
(hΟ : R[X]β° β€ S[X]β°.comap Ο) : ratfunc R β+* ratfunc S :=
{ map_zero' := begin
simp_rw [monoid_hom.to_fun_eq_coe, βof_fraction_ring_zero,
βlocalization.mk_zero (1 : R[X]β°),
βlocalization.mk_zero (1 : S[X]β°), map_apply_of_fraction_ring_mk, map_zero,
localization.mk_eq_mk', is_localization.mk'_zero],
end,
map_add' := begin
rintro β¨xβ© β¨yβ©, induction x, induction y,
{ simp only [βof_fraction_ring_add, localization.add_mk, map_add, set_like.coe_mk, map_mul,
monoid_hom.to_fun_eq_coe, map_apply_of_fraction_ring_mk, submonoid.mk_mul_mk,
submonoid.coe_mul] },
{ refl },
{ refl }
end,
..map Ο hΟ }
lemma coe_map_ring_hom_eq_coe_map [ring_hom_class F R[X] S[X]] (Ο : F)
(hΟ : R[X]β° β€ S[X]β°.comap Ο) :
(map_ring_hom Ο hΟ : ratfunc R β ratfunc S) = map Ο hΟ := rfl
-- TODO: Generalize to `fun_like` classes,
/-- Lift an monoid with zero homomorphism `polynomial R β*β Gβ` to a `ratfunc R β*β Gβ`
on the condition that `Ο` maps non zero divisors to non zero divisors,
by mapping both the numerator and denominator and quotienting them. --/
def lift_monoid_with_zero_hom (Ο : R[X] β*β Gβ) (hΟ : R[X]β° β€ Gββ°.comap Ο) :
ratfunc R β*β Gβ :=
{ to_fun := Ξ» f, ratfunc.lift_on f (Ξ» p q, Ο p / (Ο q)) $ Ξ» p q p' q' hq hq' h, begin
casesI subsingleton_or_nontrivial R,
{ rw [subsingleton.elim p q, subsingleton.elim p' q, subsingleton.elim q' q] },
rw [div_eq_div_iff, βmap_mul, h, map_mul];
exact non_zero_divisors.ne_zero (hΟ βΉ_βΊ),
end,
map_one' := by { rw [βof_fraction_ring_one, βlocalization.mk_one, lift_on_of_fraction_ring_mk],
simp only [map_one, submonoid.coe_one, div_one] },
map_mul' := Ξ» x y, by { cases x, cases y, induction x with p q, induction y with p' q',
{ rw [βof_fraction_ring_mul, localization.mk_mul],
simp only [lift_on_of_fraction_ring_mk, div_mul_div_comm, map_mul, submonoid.coe_mul] },
{ refl },
{ refl } },
map_zero' := by { rw [βof_fraction_ring_zero, βlocalization.mk_zero (1 : R[X]β°),
lift_on_of_fraction_ring_mk],
simp only [map_zero, zero_div] } }
lemma lift_monoid_with_zero_hom_apply_of_fraction_ring_mk (Ο : R[X] β*β Gβ)
(hΟ : R[X]β° β€ Gββ°.comap Ο) (n : R[X]) (d : R[X]β°) :
lift_monoid_with_zero_hom Ο hΟ (of_fraction_ring (localization.mk n d)) = Ο n / Ο d :=
lift_on_of_fraction_ring_mk _ _ _ _
lemma lift_monoid_with_zero_hom_injective [nontrivial R] (Ο : R[X] β*β Gβ)
(hΟ : function.injective Ο)
(hΟ' : R[X]β° β€ Gββ°.comap Ο :=
non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ) :
function.injective (lift_monoid_with_zero_hom Ο hΟ') :=
begin
rintro β¨xβ© β¨yβ©, induction x, induction y,
{ simp_rw [lift_monoid_with_zero_hom_apply_of_fraction_ring_mk, localization.mk_eq_mk_iff],
intro h,
refine localization.r_of_eq _,
simpa only [βhΟ.eq_iff, map_mul] using mul_eq_mul_of_div_eq_div _ _ _ _ h.symm;
exact (map_ne_zero_of_mem_non_zero_divisors _ hΟ (set_like.coe_mem _)) },
{ exact Ξ» _, rfl },
{ exact Ξ» _, rfl }
end
/-- Lift an injective ring homomorphism `polynomial R β+* L` to a `ratfunc R β+* L`
by mapping both the numerator and denominator and quotienting them. --/
def lift_ring_hom (Ο : R[X] β+* L) (hΟ : R[X]β° β€ Lβ°.comap Ο) : ratfunc R β+* L :=
{ map_add' := Ξ» x y, by { simp only [monoid_with_zero_hom.to_fun_eq_coe],
casesI subsingleton_or_nontrivial R,
{ rw [subsingleton.elim (x + y) y, subsingleton.elim x 0, map_zero, zero_add] },
cases x, cases y, induction x with p q, induction y with p' q',
{ rw [βof_fraction_ring_add, localization.add_mk],
simp only [ring_hom.to_monoid_with_zero_hom_eq_coe,
lift_monoid_with_zero_hom_apply_of_fraction_ring_mk],
rw [div_add_div, div_eq_div_iff],
{ rw [mul_comm _ p, mul_comm _ p', mul_comm _ (Ο p'), add_comm],
simp only [map_add, map_mul, submonoid.coe_mul] },
all_goals {
try { simp only [βmap_mul, βsubmonoid.coe_mul] },
exact non_zero_divisors.ne_zero (hΟ (set_like.coe_mem _)) } },
{ refl },
{ refl } },
..lift_monoid_with_zero_hom Ο.to_monoid_with_zero_hom hΟ }
lemma lift_ring_hom_apply_of_fraction_ring_mk (Ο : R[X] β+* L)
(hΟ : R[X]β° β€ Lβ°.comap Ο) (n : R[X]) (d : R[X]β°) :
lift_ring_hom Ο hΟ (of_fraction_ring (localization.mk n d)) = Ο n / Ο d :=
lift_monoid_with_zero_hom_apply_of_fraction_ring_mk _ _ _ _
lemma lift_ring_hom_injective [nontrivial R] (Ο : R[X] β+* L) (hΟ : function.injective Ο)
(hΟ' : R[X]β° β€ Lβ°.comap Ο :=
non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ) :
function.injective (lift_ring_hom Ο hΟ') :=
lift_monoid_with_zero_hom_injective _ hΟ
end lift_hom
variables (K)
include hdomain
instance : field (ratfunc K) :=
{ inv := has_inv.inv,
inv_zero := by frac_tac,
div := (/),
div_eq_mul_inv := by frac_tac,
mul_inv_cancel := Ξ» _, mul_inv_cancel,
zpow := zpow_rec,
.. ratfunc.comm_ring K,
.. ratfunc.nontrivial K }
end field
section is_fraction_ring
/-! ### `ratfunc` as field of fractions of `polynomial` -/
include hdomain
instance (R : Type*) [comm_semiring R] [algebra R K[X]] :
algebra R (ratfunc K) :=
{ to_fun := Ξ» x, ratfunc.mk (algebra_map _ _ x) 1,
map_add' := Ξ» x y, by simp only [mk_one', ring_hom.map_add, of_fraction_ring_add],
map_mul' := Ξ» x y, by simp only [mk_one', ring_hom.map_mul, of_fraction_ring_mul],
map_one' := by simp only [mk_one', ring_hom.map_one, of_fraction_ring_one],
map_zero' := by simp only [mk_one', ring_hom.map_zero, of_fraction_ring_zero],
smul := (β’),
smul_def' := Ξ» c x, x.induction_on' $ Ξ» p q hq,
by simp_rw [mk_one', β mk_smul, mk_def_of_ne (c β’ p) hq, mk_def_of_ne p hq,
β of_fraction_ring_mul, is_localization.mul_mk'_eq_mk'_of_mul, algebra.smul_def],
commutes' := Ξ» c x, mul_comm _ _ }
variables {K}
lemma mk_one (x : K[X]) : ratfunc.mk x 1 = algebra_map _ _ x := rfl
lemma of_fraction_ring_algebra_map (x : K[X]) :
of_fraction_ring (algebra_map _ (fraction_ring K[X]) x) = algebra_map _ _ x :=
by rw [β mk_one, mk_one']
@[simp] lemma mk_eq_div (p q : K[X]) :
ratfunc.mk p q = (algebra_map _ _ p / algebra_map _ _ q) :=
by simp only [mk_eq_div', of_fraction_ring_div, of_fraction_ring_algebra_map]
@[simp] lemma div_smul {R} [monoid R] [distrib_mul_action R K[X]]
[is_scalar_tower R K[X] K[X]] (c : R) (p q : K[X]) :
algebra_map _ (ratfunc K) (c β’ p) / (algebra_map _ _ q) =
c β’ (algebra_map _ _ p / algebra_map _ _ q) :=
by rw [βmk_eq_div, mk_smul, mk_eq_div]
lemma algebra_map_apply {R : Type*} [comm_semiring R] [algebra R K[X]] (x : R) :
algebra_map R (ratfunc K) x = (algebra_map _ _ (algebra_map R K[X] x)) /
(algebra_map K[X] _ 1) :=
by { rw [βmk_eq_div], refl }
lemma map_apply_div_ne_zero {R F : Type*} [comm_ring R] [is_domain R]
[monoid_hom_class F K[X] R[X]] (Ο : F)
(hΟ : K[X]β° β€ R[X]β°.comap Ο) (p q : K[X]) (hq : q β 0) :
map Ο hΟ (algebra_map _ _ p / algebra_map _ _ q) =
algebra_map _ _ (Ο p) / algebra_map _ _ (Ο q) :=
begin
have hq' : Ο q β 0 := non_zero_divisors.ne_zero (hΟ (mem_non_zero_divisors_iff_ne_zero.mpr hq)),
simp only [βmk_eq_div, mk_eq_localization_mk _ hq, map_apply_of_fraction_ring_mk,
mk_eq_localization_mk _ hq', set_like.coe_mk],
end
@[simp] lemma map_apply_div {R F : Type*} [comm_ring R] [is_domain R]
[monoid_with_zero_hom_class F K[X] R[X]] (Ο : F)
(hΟ : K[X]β° β€ R[X]β°.comap Ο) (p q : K[X]) :
map Ο hΟ (algebra_map _ _ p / algebra_map _ _ q) =
algebra_map _ _ (Ο p) / algebra_map _ _ (Ο q) :=
begin
rcases eq_or_ne q 0 with rfl|hq,
{ have : (0 : ratfunc K) = algebra_map K[X] _ 0 / algebra_map K[X] _ 1,
{ simp },
rw [map_zero, map_zero, map_zero, div_zero, div_zero, this, map_apply_div_ne_zero,
map_one, map_one, div_one, map_zero, map_zero],
exact one_ne_zero },
exact map_apply_div_ne_zero _ _ _ _ hq
end
@[simp] lemma lift_monoid_with_zero_hom_apply_div {L : Type*} [comm_group_with_zero L]
(Ο : monoid_with_zero_hom K[X] L)
(hΟ : K[X]β° β€ Lβ°.comap Ο) (p q : K[X]) :
lift_monoid_with_zero_hom Ο hΟ (algebra_map _ _ p / algebra_map _ _ q) = Ο p / Ο q :=
begin
rcases eq_or_ne q 0 with rfl|hq,
{ simp only [div_zero, map_zero] },
simpa only [βmk_eq_div, mk_eq_localization_mk _ hq,
lift_monoid_with_zero_hom_apply_of_fraction_ring_mk],
end
@[simp] lemma lift_ring_hom_apply_div {L : Type*} [field L]
(Ο : K[X] β+* L) (hΟ : K[X]β° β€ Lβ°.comap Ο) (p q : K[X]) :
lift_ring_hom Ο hΟ (algebra_map _ _ p / algebra_map _ _ q) = Ο p / Ο q :=
lift_monoid_with_zero_hom_apply_div _ _ _ _
variables (K)
lemma of_fraction_ring_comp_algebra_map :
of_fraction_ring β algebra_map K[X] (fraction_ring K[X]) = algebra_map _ _ :=
funext of_fraction_ring_algebra_map
lemma algebra_map_injective : function.injective (algebra_map K[X] (ratfunc K)) :=
begin
rw β of_fraction_ring_comp_algebra_map,
exact of_fraction_ring_injective.comp (is_fraction_ring.injective _ _),
end
@[simp] lemma algebra_map_eq_zero_iff {x : K[X]} :
algebra_map K[X] (ratfunc K) x = 0 β x = 0 :=
β¨(injective_iff_map_eq_zero _).mp (algebra_map_injective K) _, Ξ» hx, by rw [hx, ring_hom.map_zero]β©
variables {K}
lemma algebra_map_ne_zero {x : K[X]} (hx : x β 0) :
algebra_map K[X] (ratfunc K) x β 0 :=
mt (algebra_map_eq_zero_iff K).mp hx
section lift_alg_hom
variables {L R S : Type*} [field L] [comm_ring R] [is_domain R] [comm_semiring S]
[algebra S K[X]] [algebra S L] [algebra S R[X]]
(Ο : K[X] ββ[S] L) (hΟ : K[X]β° β€ Lβ°.comap Ο)
/-- Lift an algebra homomorphism that maps polynomials `Ο : polynomial K ββ[S] R[X]`
to a `ratfunc K ββ[S] ratfunc R`,
on the condition that `Ο` maps non zero divisors to non zero divisors,
by mapping both the numerator and denominator and quotienting them. -/
def map_alg_hom (Ο : K[X] ββ[S] R[X])
(hΟ : K[X]β° β€ R[X]β°.comap Ο) : ratfunc K ββ[S] ratfunc R :=
{ commutes' := Ξ» r, by simp_rw [ring_hom.to_fun_eq_coe, coe_map_ring_hom_eq_coe_map,
algebra_map_apply r, map_apply_div, map_one, alg_hom.commutes],
..map_ring_hom Ο hΟ }
lemma coe_map_alg_hom_eq_coe_map (Ο : K[X] ββ[S] R[X])
(hΟ : K[X]β° β€ R[X]β°.comap Ο) :
(map_alg_hom Ο hΟ : ratfunc K β ratfunc R) = map Ο hΟ := rfl
/-- Lift an injective algebra homomorphism `polynomial K ββ[S] L` to a `ratfunc K ββ[S] L`
by mapping both the numerator and denominator and quotienting them. --/
def lift_alg_hom : ratfunc K ββ[S] L :=
{ commutes' := Ξ» r, by simp_rw [ring_hom.to_fun_eq_coe, alg_hom.to_ring_hom_eq_coe,
algebra_map_apply r, lift_ring_hom_apply_div, alg_hom.coe_to_ring_hom, map_one,
div_one, alg_hom.commutes],
..lift_ring_hom Ο.to_ring_hom hΟ }
lemma lift_alg_hom_apply_of_fraction_ring_mk (n : K[X]) (d : K[X]β°) :
lift_alg_hom Ο hΟ (of_fraction_ring (localization.mk n d)) = Ο n / Ο d :=
lift_monoid_with_zero_hom_apply_of_fraction_ring_mk _ _ _ _
lemma lift_alg_hom_injective (Ο : K[X] ββ[S] L) (hΟ : function.injective Ο)
(hΟ' : K[X]β° β€ Lβ°.comap Ο :=
non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ) :
function.injective (lift_alg_hom Ο hΟ') :=
lift_monoid_with_zero_hom_injective _ hΟ
@[simp] lemma lift_alg_hom_apply_div (p q : K[X]) :
lift_alg_hom Ο hΟ (algebra_map _ _ p / algebra_map _ _ q) = Ο p / Ο q :=
lift_monoid_with_zero_hom_apply_div _ _ _ _
end lift_alg_hom
variables (K)
omit hdomain
include hdomain
/-- `ratfunc K` is the field of fractions of the polynomials over `K`. -/
instance : is_fraction_ring K[X] (ratfunc K) :=
{ map_units := Ξ» y, by rw β of_fraction_ring_algebra_map;
exact (to_fraction_ring_ring_equiv K).symm.to_ring_hom.is_unit_map
(is_localization.map_units _ y),
eq_iff_exists := Ξ» x y, by rw [β of_fraction_ring_algebra_map, β of_fraction_ring_algebra_map];
exact (to_fraction_ring_ring_equiv K).symm.injective.eq_iff.trans
(is_localization.eq_iff_exists _ _),
surj := by { rintro β¨zβ©, convert is_localization.surj K[X]β° z, ext β¨x, yβ©,
simp only [β of_fraction_ring_algebra_map, function.comp_app, β of_fraction_ring_mul] } }
variables {K}
@[simp] lemma lift_on_div {P : Sort v} (p q : K[X])
(f : β (p q : K[X]), P) (f0 : β p, f p 0 = f 0 1)
(H' : β {p q p' q'} (hq : q β 0) (hq' : q' β 0), p * q' = p' * q β f p q = f p' q')
(H : β {p q p' q'} (hq : q β K[X]β°) (hq' : q' β K[X]β°), p * q' = p' * q β
f p q = f p' q' :=
Ξ» p q p' q' hq hq' h, H' (non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq') h) :
(algebra_map _ (ratfunc K) p / algebra_map _ _ q).lift_on f @H = f p q :=
by rw [β mk_eq_div, lift_on_mk _ _ f f0 @H']
@[simp] lemma lift_on'_div {P : Sort v} (p q : K[X])
(f : β (p q : K[X]), P) (f0 : β p, f p 0 = f 0 1) (H) :
(algebra_map _ (ratfunc K) p / algebra_map _ _ q).lift_on' f @H = f p q :=
begin
rw [ratfunc.lift_on', lift_on_div _ _ _ f0],
exact lift_on_condition_of_lift_on'_condition @H
end
/-- Induction principle for `ratfunc K`: if `f p q : P (p / q)` for all `p q : polynomial K`,
then `P` holds on all elements of `ratfunc K`.
See also `induction_on'`, which is a recursion principle defined in terms of `ratfunc.mk`.
-/
protected lemma induction_on {P : ratfunc K β Prop} (x : ratfunc K)
(f : β (p q : K[X]) (hq : q β 0),
P (algebra_map _ (ratfunc K) p / algebra_map _ _ q)) :
P x :=
x.induction_on' (Ξ» p q hq, by simpa using f p q hq)
lemma of_fraction_ring_mk' (x : K[X]) (y : K[X]β°) :
of_fraction_ring (is_localization.mk' _ x y) = is_localization.mk' (ratfunc K) x y :=
by rw [is_fraction_ring.mk'_eq_div, is_fraction_ring.mk'_eq_div, β mk_eq_div', β mk_eq_div]
@[simp] lemma of_fraction_ring_eq :
(of_fraction_ring : fraction_ring K[X] β ratfunc K) =
is_localization.alg_equiv K[X]β° _ _ :=
funext $ Ξ» x, localization.induction_on x $ Ξ» x,
by simp only [is_localization.alg_equiv_apply, is_localization.ring_equiv_of_ring_equiv_apply,
ring_equiv.to_fun_eq_coe, localization.mk_eq_mk'_apply, is_localization.map_mk',
of_fraction_ring_mk', ring_equiv.coe_to_ring_hom, ring_equiv.refl_apply, set_like.eta]
@[simp] lemma to_fraction_ring_eq :
(to_fraction_ring : ratfunc K β fraction_ring K[X]) =
is_localization.alg_equiv K[X]β° _ _ :=
funext $ Ξ» β¨xβ©, localization.induction_on x $ Ξ» x,
by simp only [localization.mk_eq_mk'_apply, of_fraction_ring_mk', is_localization.alg_equiv_apply,
ring_equiv.to_fun_eq_coe, is_localization.ring_equiv_of_ring_equiv_apply,
is_localization.map_mk', ring_equiv.coe_to_ring_hom, ring_equiv.refl_apply, set_like.eta]
@[simp] lemma to_fraction_ring_ring_equiv_symm_eq :
(to_fraction_ring_ring_equiv K).symm =
(is_localization.alg_equiv K[X]β° _ _).to_ring_equiv :=
by { ext x,
simp [to_fraction_ring_ring_equiv, of_fraction_ring_eq, alg_equiv.coe_ring_equiv'] }
end is_fraction_ring
section num_denom
/-! ### Numerator and denominator -/
open gcd_monoid polynomial
omit hring
variables [hfield : field K]
include hfield
/-- `ratfunc.num_denom` are numerator and denominator of a rational function over a field,
normalized such that the denominator is monic. -/
def num_denom (x : ratfunc K) : K[X] Γ K[X] :=
x.lift_on' (Ξ» p q, if q = 0 then β¨0, 1β© else let r := gcd p q in
β¨polynomial.C ((q / r).leading_coeffβ»ΒΉ) * (p / r),
polynomial.C ((q / r).leading_coeffβ»ΒΉ) * (q / r)β©)
begin
intros p q a hq ha,
rw [if_neg hq, if_neg (mul_ne_zero ha hq)],
have hpq : gcd p q β 0 := mt (and.right β (gcd_eq_zero_iff _ _).mp) hq,
have ha' : a.leading_coeff β 0 := polynomial.leading_coeff_ne_zero.mpr ha,
have hainv : (a.leading_coeff)β»ΒΉ β 0 := inv_ne_zero ha',
simp only [prod.ext_iff, gcd_mul_left, normalize_apply, polynomial.coe_norm_unit, mul_assoc,
comm_group_with_zero.coe_norm_unit _ ha'],
have hdeg : (gcd p q).degree β€ q.degree := degree_gcd_le_right _ hq,
have hdeg' : (polynomial.C (a.leading_coeffβ»ΒΉ) * gcd p q).degree β€ q.degree,
{ rw [polynomial.degree_mul, polynomial.degree_C hainv, zero_add],
exact hdeg },
have hdivp : (polynomial.C a.leading_coeffβ»ΒΉ) * gcd p q β£ p :=
(C_mul_dvd hainv).mpr (gcd_dvd_left p q),
have hdivq : (polynomial.C a.leading_coeffβ»ΒΉ) * gcd p q β£ q :=
(C_mul_dvd hainv).mpr (gcd_dvd_right p q),
rw [euclidean_domain.mul_div_mul_cancel ha hdivp, euclidean_domain.mul_div_mul_cancel ha hdivq,
leading_coeff_div hdeg, leading_coeff_div hdeg', polynomial.leading_coeff_mul,
polynomial.leading_coeff_C, div_C_mul, div_C_mul,
β mul_assoc, β polynomial.C_mul, β mul_assoc, β polynomial.C_mul],
split; congr; rw [inv_div, mul_comm, mul_div_assoc, β mul_assoc, inv_inv,
_root_.mul_inv_cancel ha', one_mul, inv_div],
end
@[simp] lemma num_denom_div (p : K[X]) {q : K[X]} (hq : q β 0) :
num_denom (algebra_map _ _ p / algebra_map _ _ q) =
(polynomial.C ((q / gcd p q).leading_coeffβ»ΒΉ) * (p / gcd p q),
polynomial.C ((q / gcd p q).leading_coeffβ»ΒΉ) * (q / gcd p q)) :=
begin
rw [num_denom, lift_on'_div, if_neg hq],
intros p,
rw [if_pos rfl, if_neg (@one_ne_zero K[X] _ _)],
simp,
end
/-- `ratfunc.num` is the numerator of a rational function,
normalized such that the denominator is monic. -/
def num (x : ratfunc K) : K[X] := x.num_denom.1
private lemma num_div' (p : K[X]) {q : K[X]} (hq : q β 0) :
num (algebra_map _ _ p / algebra_map _ _ q) =
polynomial.C ((q / gcd p q).leading_coeffβ»ΒΉ) * (p / gcd p q) :=
by rw [num, num_denom_div _ hq]
@[simp] lemma num_zero : num (0 : ratfunc K) = 0 :=
by { convert num_div' (0 : K[X]) one_ne_zero; simp }
@[simp] lemma num_div (p q : K[X]) :
num (algebra_map _ _ p / algebra_map _ _ q) =
polynomial.C ((q / gcd p q).leading_coeffβ»ΒΉ) * (p / gcd p q) :=
begin
by_cases hq : q = 0,
{ simp [hq] },
{ exact num_div' p hq, },
end
@[simp] lemma num_one : num (1 : ratfunc K) = 1 :=
by { convert num_div (1 : K[X]) 1; simp }
@[simp] lemma num_algebra_map (p : K[X]) :
num (algebra_map _ _ p) = p :=
by { convert num_div p 1; simp }
lemma num_div_dvd (p : K[X]) {q : K[X]} (hq : q β 0) :
num (algebra_map _ _ p / algebra_map _ _ q) β£ p :=
begin
rw [num_div _ q, C_mul_dvd],
{ exact euclidean_domain.div_dvd_of_dvd (gcd_dvd_left p q) },
{ simpa only [ne.def, inv_eq_zero, polynomial.leading_coeff_eq_zero]
using right_div_gcd_ne_zero hq },
end
/-- A version of `num_div_dvd` with the LHS in simp normal form -/
@[simp] lemma num_div_dvd' (p : K[X]) {q : K[X]} (hq : q β 0) :
C ((q / gcd p q).leading_coeff)β»ΒΉ * (p / gcd p q) β£ p :=
by simpa using num_div_dvd p hq
/-- `ratfunc.denom` is the denominator of a rational function,
normalized such that it is monic. -/
def denom (x : ratfunc K) : K[X] := x.num_denom.2
@[simp] lemma denom_div (p : K[X]) {q : K[X]} (hq : q β 0) :
denom (algebra_map _ _ p / algebra_map _ _ q) =
polynomial.C ((q / gcd p q).leading_coeffβ»ΒΉ) * (q / gcd p q) :=
by rw [denom, num_denom_div _ hq]
lemma monic_denom (x : ratfunc K) : (denom x).monic :=
x.induction_on (Ξ» p q hq, begin
rw [denom_div p hq, mul_comm],
exact polynomial.monic_mul_leading_coeff_inv (right_div_gcd_ne_zero hq)
end)
lemma denom_ne_zero (x : ratfunc K) : denom x β 0 :=
(monic_denom x).ne_zero
@[simp] lemma denom_zero : denom (0 : ratfunc K) = 1 :=
by { convert denom_div (0 : K[X]) one_ne_zero; simp }
@[simp] lemma denom_one : denom (1 : ratfunc K) = 1 :=
by { convert denom_div (1 : K[X]) one_ne_zero; simp }
@[simp] lemma denom_algebra_map (p : K[X]) :
denom (algebra_map _ (ratfunc K) p) = 1 :=
by { convert denom_div p one_ne_zero; simp }
@[simp] lemma denom_div_dvd (p q : K[X]) :
denom (algebra_map _ _ p / algebra_map _ _ q) β£ q :=
begin
by_cases hq : q = 0,
{ simp [hq], },
rw [denom_div _ hq, C_mul_dvd],
{ exact euclidean_domain.div_dvd_of_dvd (gcd_dvd_right p q) },
{ simpa only [ne.def, inv_eq_zero, polynomial.leading_coeff_eq_zero]
using right_div_gcd_ne_zero hq },
end
@[simp] lemma num_div_denom (x : ratfunc K) :
algebra_map _ _ (num x) / algebra_map _ _ (denom x) = x :=
x.induction_on (Ξ» p q hq, begin
have q_div_ne_zero := right_div_gcd_ne_zero hq,
rw [num_div p q, denom_div p hq, ring_hom.map_mul, ring_hom.map_mul,
mul_div_mul_left, div_eq_div_iff, β ring_hom.map_mul, β ring_hom.map_mul, mul_comm _ q,
β euclidean_domain.mul_div_assoc, β euclidean_domain.mul_div_assoc, mul_comm],
{ apply gcd_dvd_right },
{ apply gcd_dvd_left },
{ exact algebra_map_ne_zero q_div_ne_zero },
{ exact algebra_map_ne_zero hq },
{ refine algebra_map_ne_zero (mt polynomial.C_eq_zero.mp _),
exact inv_ne_zero (polynomial.leading_coeff_ne_zero.mpr q_div_ne_zero) },
end)
@[simp] lemma num_eq_zero_iff {x : ratfunc K} : num x = 0 β x = 0 :=
β¨Ξ» h, by rw [β num_div_denom x, h, ring_hom.map_zero, zero_div],
Ξ» h, h.symm βΈ num_zeroβ©
lemma num_ne_zero {x : ratfunc K} (hx : x β 0) : num x β 0 :=
mt num_eq_zero_iff.mp hx
lemma num_mul_eq_mul_denom_iff {x : ratfunc K} {p q : K[X]}
(hq : q β 0) :
x.num * q = p * x.denom β x = algebra_map _ _ p / algebra_map _ _ q :=
begin
rw [β (algebra_map_injective K).eq_iff, eq_div_iff (algebra_map_ne_zero hq)],
conv_rhs { rw β num_div_denom x },
rw [ring_hom.map_mul, ring_hom.map_mul, div_eq_mul_inv, mul_assoc, mul_comm (has_inv.inv _),
β mul_assoc, β div_eq_mul_inv, div_eq_iff],
exact algebra_map_ne_zero (denom_ne_zero x)
end
lemma num_denom_add (x y : ratfunc K) :
(x + y).num * (x.denom * y.denom) = (x.num * y.denom + x.denom * y.num) * (x + y).denom :=
(num_mul_eq_mul_denom_iff (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y))).mpr $
begin
conv_lhs { rw [β num_div_denom x, β num_div_denom y] },
rw [div_add_div, ring_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_mul],
{ exact algebra_map_ne_zero (denom_ne_zero x) },
{ exact algebra_map_ne_zero (denom_ne_zero y) }
end
lemma num_denom_neg (x : ratfunc K) :
(-x).num * x.denom = - x.num * (-x).denom :=
by rw [num_mul_eq_mul_denom_iff (denom_ne_zero x), _root_.map_neg, neg_div, num_div_denom]
lemma num_denom_mul (x y : ratfunc K) :
(x * y).num * (x.denom * y.denom) = x.num * y.num * (x * y).denom :=
(num_mul_eq_mul_denom_iff (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y))).mpr $
by conv_lhs { rw [β num_div_denom x, β num_div_denom y, div_mul_div_comm,
β ring_hom.map_mul, β ring_hom.map_mul] }
lemma num_dvd {x : ratfunc K} {p : K[X]} (hp : p β 0) :
num x β£ p β β (q : K[X]) (hq : q β 0), x = algebra_map _ _ p / algebra_map _ _ q :=
begin
split,
{ rintro β¨q, rflβ©,
obtain β¨hx, hqβ© := mul_ne_zero_iff.mp hp,
use denom x * q,
rw [ring_hom.map_mul, ring_hom.map_mul, β div_mul_div_comm, div_self, mul_one, num_div_denom],
{ exact β¨mul_ne_zero (denom_ne_zero x) hq, rflβ© },
{ exact algebra_map_ne_zero hq } },
{ rintro β¨q, hq, rflβ©,
exact num_div_dvd p hq },
end
lemma denom_dvd {x : ratfunc K} {q : K[X]} (hq : q β 0) :
denom x β£ q β β (p : K[X]), x = algebra_map _ _ p / algebra_map _ _ q :=
begin
split,
{ rintro β¨p, rflβ©,
obtain β¨hx, hpβ© := mul_ne_zero_iff.mp hq,
use num x * p,
rw [ring_hom.map_mul, ring_hom.map_mul, β div_mul_div_comm, div_self, mul_one, num_div_denom],
{ exact algebra_map_ne_zero hp } },
{ rintro β¨p, rflβ©,
exact denom_div_dvd p q },
end
lemma num_mul_dvd (x y : ratfunc K) : num (x * y) β£ num x * num y :=
begin
by_cases hx : x = 0,
{ simp [hx] },
by_cases hy : y = 0,
{ simp [hy] },
rw num_dvd (mul_ne_zero (num_ne_zero hx) (num_ne_zero hy)),
refine β¨x.denom * y.denom, mul_ne_zero (denom_ne_zero x) (denom_ne_zero y), _β©,
rw [ring_hom.map_mul, ring_hom.map_mul, β div_mul_div_comm, num_div_denom, num_div_denom]
end
lemma denom_mul_dvd (x y : ratfunc K) : denom (x * y) β£ denom x * denom y :=
begin
rw denom_dvd (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y)),
refine β¨x.num * y.num, _β©,
rw [ring_hom.map_mul, ring_hom.map_mul, β div_mul_div_comm, num_div_denom, num_div_denom]
end
lemma denom_add_dvd (x y : ratfunc K) : denom (x + y) β£ denom x * denom y :=
begin
rw denom_dvd (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y)),
refine β¨x.num * y.denom + x.denom * y.num, _β©,
rw [ring_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_mul, β div_add_div,
num_div_denom, num_div_denom],
{ exact algebra_map_ne_zero (denom_ne_zero x) },
{ exact algebra_map_ne_zero (denom_ne_zero y) },
end
lemma map_denom_ne_zero {L F : Type*} [has_zero L] [zero_hom_class F K[X] L]
(Ο : F) (hΟ : function.injective Ο) (f : ratfunc K) : Ο f.denom β 0 :=
Ξ» H, (denom_ne_zero f) ((map_eq_zero_iff Ο hΟ).mp H)
lemma map_apply {R F : Type*} [comm_ring R] [is_domain R]
[monoid_hom_class F K[X] R[X]]
(Ο : F) (hΟ : K[X]β° β€ R[X]β°.comap Ο) (f : ratfunc K) :
map Ο hΟ f = algebra_map _ _ (Ο f.num) / algebra_map _ _ (Ο f.denom) :=
begin
rw [βnum_div_denom f, map_apply_div_ne_zero, num_div_denom f],
exact denom_ne_zero _
end
lemma lift_monoid_with_zero_hom_apply {L : Type*} [comm_group_with_zero L]
(Ο : K[X] β*β L) (hΟ : K[X]β° β€ Lβ°.comap Ο) (f : ratfunc K) :
lift_monoid_with_zero_hom Ο hΟ f = Ο f.num / Ο f.denom :=
by rw [βnum_div_denom f, lift_monoid_with_zero_hom_apply_div, num_div_denom]
lemma lift_ring_hom_apply {L : Type*} [field L]
(Ο : K[X] β+* L) (hΟ : K[X]β° β€ Lβ°.comap Ο) (f : ratfunc K) :
lift_ring_hom Ο hΟ f = Ο f.num / Ο f.denom :=
lift_monoid_with_zero_hom_apply _ _ _
lemma lift_alg_hom_apply {L S : Type*} [field L] [comm_semiring S] [algebra S K[X]]
[algebra S L] (Ο : K[X] ββ[S] L) (hΟ : K[X]β° β€ Lβ°.comap Ο) (f : ratfunc K) :
lift_alg_hom Ο hΟ f = Ο f.num / Ο f.denom :=
lift_monoid_with_zero_hom_apply _ _ _
lemma num_mul_denom_add_denom_mul_num_ne_zero {x y : ratfunc K} (hxy : x + y β 0) :
x.num * y.denom + x.denom * y.num β 0 :=
begin
intro h_zero,
have h := num_denom_add x y,
rw [h_zero, zero_mul] at h,
exact (mul_ne_zero (num_ne_zero hxy) (mul_ne_zero x.denom_ne_zero y.denom_ne_zero)) h
end
end num_denom
section eval
/-! ### Polynomial structure: `C`, `X`, `eval` -/
include hdomain
/-- `ratfunc.C a` is the constant rational function `a`. -/
def C : K β+* ratfunc K :=
algebra_map _ _
@[simp] lemma algebra_map_eq_C : algebra_map K (ratfunc K) = C := rfl
@[simp] lemma algebra_map_C (a : K) :
algebra_map K[X] (ratfunc K) (polynomial.C a) = C a := rfl
@[simp] lemma algebra_map_comp_C :
(algebra_map K[X] (ratfunc K)).comp polynomial.C = C := rfl
lemma smul_eq_C_mul (r : K) (x : ratfunc K) :
r β’ x = C r * x :=
by rw [algebra.smul_def, algebra_map_eq_C]
/-- `ratfunc.X` is the polynomial variable (aka indeterminate). -/
def X : ratfunc K := algebra_map K[X] (ratfunc K) polynomial.X
@[simp] lemma algebra_map_X :
algebra_map K[X] (ratfunc K) polynomial.X = X := rfl
omit hring hdomain
variables [hfield : field K]
include hfield
@[simp] lemma num_C (c : K) : num (C c) = polynomial.C c :=
num_algebra_map _
@[simp] lemma denom_C (c : K) : denom (C c) = 1 :=
denom_algebra_map _
@[simp] lemma num_X : num (X : ratfunc K) = polynomial.X :=
num_algebra_map _
@[simp] lemma denom_X : denom (X : ratfunc K) = 1 :=
denom_algebra_map _
lemma X_ne_zero : (ratfunc.X : ratfunc K) β 0 :=
ratfunc.algebra_map_ne_zero polynomial.X_ne_zero
variables {L : Type*} [field L]
/-- Evaluate a rational function `p` given a ring hom `f` from the scalar field
to the target and a value `x` for the variable in the target.
Fractions are reduced by clearing common denominators before evaluating:
`eval id 1 ((X^2 - 1) / (X - 1)) = eval id 1 (X + 1) = 2`, not `0 / 0 = 0`.
-/
def eval (f : K β+* L) (a : L) (p : ratfunc K) : L :=
(num p).evalβ f a / (denom p).evalβ f a
variables {f : K β+* L} {a : L}
lemma eval_eq_zero_of_evalβ_denom_eq_zero
{x : ratfunc K} (h : polynomial.evalβ f a (denom x) = 0) :
eval f a x = 0 :=
by rw [eval, h, div_zero]
lemma evalβ_denom_ne_zero {x : ratfunc K} (h : eval f a x β 0) :
polynomial.evalβ f a (denom x) β 0 :=
mt eval_eq_zero_of_evalβ_denom_eq_zero h
variables (f a)
@[simp] lemma eval_C {c : K} : eval f a (C c) = f c := by simp [eval]
@[simp] lemma eval_X : eval f a X = a := by simp [eval]
@[simp] lemma eval_zero : eval f a 0 = 0 := by simp [eval]
@[simp] lemma eval_one : eval f a 1 = 1 := by simp [eval]
@[simp] lemma eval_algebra_map {S : Type*} [comm_semiring S] [algebra S K[X]] (p : S) :
eval f a (algebra_map _ _ p) = (algebra_map _ K[X] p).evalβ f a :=
by simp [eval, is_scalar_tower.algebra_map_apply S K[X] (ratfunc K)]
/-- `eval` is an additive homomorphism except when a denominator evaluates to `0`.
Counterexample: `eval _ 1 (X / (X-1)) + eval _ 1 (-1 / (X-1)) = 0`
`... β 1 = eval _ 1 ((X-1) / (X-1))`.
See also `ratfunc.evalβ_denom_ne_zero` to make the hypotheses simpler but less general.
-/
lemma eval_add {x y : ratfunc K}
(hx : polynomial.evalβ f a (denom x) β 0) (hy : polynomial.evalβ f a (denom y) β 0) :
eval f a (x + y) = eval f a x + eval f a y :=
begin
unfold eval,
by_cases hxy : polynomial.evalβ f a (denom (x + y)) = 0,
{ have := polynomial.evalβ_eq_zero_of_dvd_of_evalβ_eq_zero f a (denom_add_dvd x y) hxy,
rw polynomial.evalβ_mul at this,
cases mul_eq_zero.mp this; contradiction },
rw [div_add_div _ _ hx hy, eq_div_iff (mul_ne_zero hx hy), div_eq_mul_inv, mul_right_comm,
β div_eq_mul_inv, div_eq_iff hxy],
simp only [β polynomial.evalβ_mul, β polynomial.evalβ_add],
congr' 1,
apply num_denom_add
end
/-- `eval` is a multiplicative homomorphism except when a denominator evaluates to `0`.
Counterexample: `eval _ 0 X * eval _ 0 (1/X) = 0 β 1 = eval _ 0 1 = eval _ 0 (X * 1/X)`.
See also `ratfunc.evalβ_denom_ne_zero` to make the hypotheses simpler but less general.
-/
lemma eval_mul {x y : ratfunc K}
(hx : polynomial.evalβ f a (denom x) β 0) (hy : polynomial.evalβ f a (denom y) β 0) :
eval f a (x * y) = eval f a x * eval f a y :=
begin
unfold eval,
by_cases hxy : polynomial.evalβ f a (denom (x * y)) = 0,
{ have := polynomial.evalβ_eq_zero_of_dvd_of_evalβ_eq_zero f a (denom_mul_dvd x y) hxy,
rw polynomial.evalβ_mul at this,
cases mul_eq_zero.mp this; contradiction },
rw [div_mul_div_comm, eq_div_iff (mul_ne_zero hx hy), div_eq_mul_inv, mul_right_comm,
β div_eq_mul_inv, div_eq_iff hxy],
repeat { rw β polynomial.evalβ_mul },
congr' 1,
apply num_denom_mul,
end
end eval
section int_degree
open polynomial
omit hring
variables [field K]
/-- `int_degree x` is the degree of the rational function `x`, defined as the difference between
the `nat_degree` of its numerator and the `nat_degree` of its denominator. In particular,
`int_degree 0 = 0`. -/
def int_degree (x : ratfunc K) : β€ := nat_degree x.num - nat_degree x.denom
@[simp] lemma int_degree_zero : int_degree (0 : ratfunc K) = 0 :=
by rw [int_degree, num_zero, nat_degree_zero, denom_zero, nat_degree_one, sub_self]
@[simp] lemma int_degree_one : int_degree (1 : ratfunc K) = 0 :=
by rw [int_degree, num_one, denom_one, sub_self]
@[simp] lemma int_degree_C (k : K): int_degree (ratfunc.C k) = 0 :=
by rw [int_degree, num_C, nat_degree_C, denom_C, nat_degree_one, sub_self]
@[simp] lemma int_degree_X : int_degree (X : ratfunc K) = 1 :=
by rw [int_degree, ratfunc.num_X, polynomial.nat_degree_X, ratfunc.denom_X,
polynomial.nat_degree_one, int.coe_nat_one, int.coe_nat_zero, sub_zero]
@[simp] lemma int_degree_polynomial {p : polynomial K} :
int_degree (algebra_map (polynomial K) (ratfunc K) p) = nat_degree p :=
by rw [int_degree, ratfunc.num_algebra_map, ratfunc.denom_algebra_map, polynomial.nat_degree_one,
int.coe_nat_zero, sub_zero]
lemma int_degree_mul {x y : ratfunc K} (hx : x β 0) (hy : y β 0) :
int_degree (x * y) = int_degree x + int_degree y :=
begin
simp only [int_degree, add_sub, sub_add, sub_sub_eq_add_sub, sub_sub, sub_eq_sub_iff_add_eq_add],
norm_cast,
rw [β polynomial.nat_degree_mul x.denom_ne_zero y.denom_ne_zero,
β polynomial.nat_degree_mul (ratfunc.num_ne_zero (mul_ne_zero hx hy))
(mul_ne_zero x.denom_ne_zero y.denom_ne_zero),
β polynomial.nat_degree_mul (ratfunc.num_ne_zero hx) (ratfunc.num_ne_zero hy),
β polynomial.nat_degree_mul (mul_ne_zero (ratfunc.num_ne_zero hx) (ratfunc.num_ne_zero hy))
(x * y).denom_ne_zero, ratfunc.num_denom_mul]
end
@[simp] lemma int_degree_neg (x : ratfunc K) : int_degree (-x) = int_degree x :=
begin
by_cases hx : x = 0,
{ rw [hx, neg_zero] },
{ rw [int_degree, int_degree, β nat_degree_neg x.num],
exact nat_degree_sub_eq_of_prod_eq (num_ne_zero (neg_ne_zero.mpr hx)) (denom_ne_zero (- x))
(neg_ne_zero.mpr (num_ne_zero hx)) (denom_ne_zero x) (num_denom_neg x) }
end
lemma int_degree_add {x y : ratfunc K}
(hxy : x + y β 0) : (x + y).int_degree =
(x.num * y.denom + x.denom * y.num).nat_degree - (x.denom * y.denom).nat_degree :=
nat_degree_sub_eq_of_prod_eq (num_ne_zero hxy) ((x + y).denom_ne_zero)
(num_mul_denom_add_denom_mul_num_ne_zero hxy) (mul_ne_zero x.denom_ne_zero y.denom_ne_zero)
(num_denom_add x y)
lemma nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree {x : ratfunc K}
(hx : x β 0) {s : polynomial K} (hs : s β 0) :
((x.num * s).nat_degree : β€) - (s * x.denom).nat_degree = x.int_degree :=
begin
apply nat_degree_sub_eq_of_prod_eq (mul_ne_zero (num_ne_zero hx) hs)
(mul_ne_zero hs x.denom_ne_zero) (num_ne_zero hx) x.denom_ne_zero,
rw mul_assoc
end
lemma int_degree_add_le {x y : ratfunc K} (hy : y β 0) (hxy : x + y β 0) :
int_degree (x + y) β€ max (int_degree x) (int_degree y) :=
begin
by_cases hx : x = 0,
{ simp [hx] at *, },
rw [int_degree_add hxy,
β nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree hx y.denom_ne_zero,
mul_comm y.denom,
β nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree hy x.denom_ne_zero,
le_max_iff,sub_le_sub_iff_right, int.coe_nat_le, sub_le_sub_iff_right, int.coe_nat_le,
β le_max_iff, mul_comm y.num],
exact nat_degree_add_le _ _,
end
end int_degree
section laurent_series
open power_series laurent_series hahn_series
omit hring
variables {F : Type u} [field F] (p q : F[X]) (f g : ratfunc F)
/-- The coercion `ratfunc F β laurent_series F` as bundled alg hom. -/
def coe_alg_hom (F : Type u) [field F] : ratfunc F ββ[polynomial F] laurent_series F :=
lift_alg_hom (algebra.of_id _ _) $ non_zero_divisors_le_comap_non_zero_divisors_of_injective _ $
polynomial.algebra_map_hahn_series_injective _
instance coe_to_laurent_series : has_coe (ratfunc F) (laurent_series F) :=
β¨coe_alg_hom Fβ©
lemma coe_def : (f : laurent_series F) = coe_alg_hom F f := rfl
lemma coe_num_denom : (f : laurent_series F) = f.num / f.denom :=
lift_alg_hom_apply _ _ f
lemma coe_injective : function.injective (coe : ratfunc F β laurent_series F) :=
lift_alg_hom_injective _ (polynomial.algebra_map_hahn_series_injective _)
@[simp, norm_cast] lemma coe_apply : coe_alg_hom F f = f := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : ratfunc F) : laurent_series F) = 0 :=
(coe_alg_hom F).map_zero
@[simp, norm_cast] lemma coe_one : ((1 : ratfunc F) : laurent_series F) = 1 :=
(coe_alg_hom F).map_one
@[simp, norm_cast] lemma coe_add : ((f + g : ratfunc F) : laurent_series F) = f + g :=
(coe_alg_hom F).map_add _ _
@[simp, norm_cast] lemma coe_mul : ((f * g : ratfunc F) : laurent_series F) = f * g :=
(coe_alg_hom F).map_mul _ _
@[simp, norm_cast] lemma coe_div : ((f / g : ratfunc F) : laurent_series F) =
(f : laurent_series F) / (g : laurent_series F) :=
map_divβ (coe_alg_hom F) _ _
@[simp, norm_cast] lemma coe_C (r : F) : ((C r : ratfunc F) : laurent_series F) = hahn_series.C r :=
by rw [coe_num_denom, num_C, denom_C, coe_coe, polynomial.coe_C, coe_C, coe_coe, polynomial.coe_one,
power_series.coe_one, div_one]
-- TODO: generalize over other modules
@[simp, norm_cast] lemma coe_smul (r : F) : ((r β’ f : ratfunc F) : laurent_series F) = r β’ f :=
by rw [smul_eq_C_mul, βC_mul_eq_smul, coe_mul, coe_C]
@[simp, norm_cast] lemma coe_X : ((X : ratfunc F) : laurent_series F) = single 1 1 :=
by rw [coe_num_denom, num_X, denom_X, coe_coe, polynomial.coe_X, coe_X, coe_coe, polynomial.coe_one,
power_series.coe_one, div_one]
instance : algebra (ratfunc F) (laurent_series F) :=
ring_hom.to_algebra (coe_alg_hom F).to_ring_hom
lemma algebra_map_apply_div :
algebra_map (ratfunc F) (laurent_series F) (algebra_map _ _ p / algebra_map _ _ q) =
algebra_map F[X] (laurent_series F) p / algebra_map _ _ q :=
begin
convert coe_div _ _;
rw [βmk_one, coe_def, coe_alg_hom, mk_eq_div, lift_alg_hom_apply_div, map_one, div_one,
algebra.of_id_apply]
end
instance : is_scalar_tower F[X] (ratfunc F) (laurent_series F) :=
β¨Ξ» x y z, by { ext, simp }β©
end laurent_series
end ratfunc
|
199a2f24baabf934647cc8ea3e9d3cffff7661ea | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/data/string/thms.lean | 30e045b35af365f2f5b920fee4d6adb16c6996f7 | [
"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 | 475 | 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
import data.string.decl data.bool
open bool inhabited
namespace char
protected definition is_inhabited [instance] : inhabited char :=
inhabited.mk (mk ff ff ff ff ff ff ff ff)
end char
namespace string
protected definition is_inhabited [instance] : inhabited string :=
inhabited.mk empty
end string
|
9c37e8b6efc9ae5f069af0b9c4fbbc4b156294bb | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/logic/unnamed_2397.lean | ee7063bfd8ec21436a18ef9be2d171db1ab7104e | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 364 | lean | import data.real.basic
def converges_to (s : β β β) (a : β) :=
β Ξ΅ > 0, β N, β n β₯ N, abs (s n - a) < Ξ΅
variables {s : β β β} {a : β}
-- BEGIN
theorem exists_abs_le_of_converges_to (cs : converges_to s a) :
β N b, β n, N β€ n β abs (s n) < b :=
begin
cases cs 1 zero_lt_one with N h,
use [N, abs a + 1],
sorry
end
-- END |
1eaa781ad47e1d52978a2d24bc80a645e29ab061 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/finmap.lean | b7b411e2cb3eccf98e515528d601edd095b126d6 | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 18,201 | lean | /-
Copyright (c) 2018 Sean Leather. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sean Leather, Mario Carneiro
Finite maps over `multiset`.
-/
import data.list.alist data.finset data.pfun
universes u v w
open list
variables {Ξ± : Type u} {Ξ² : Ξ± β Type v}
namespace multiset
/-- Multiset of keys of an association multiset. -/
def keys (s : multiset (sigma Ξ²)) : multiset Ξ± :=
s.map sigma.fst
@[simp] theorem coe_keys {l : list (sigma Ξ²)} :
keys (l : multiset (sigma Ξ²)) = (l.keys : multiset Ξ±) :=
rfl
/-- `nodupkeys s` means that `s` has no duplicate keys. -/
def nodupkeys (s : multiset (sigma Ξ²)) : Prop :=
quot.lift_on s list.nodupkeys (Ξ» s t p, propext $ perm_nodupkeys p)
@[simp] theorem coe_nodupkeys {l : list (sigma Ξ²)} : @nodupkeys Ξ± Ξ² l β l.nodupkeys := iff.rfl
end multiset
/-- `finmap Ξ²` is the type of finite maps over a multiset. It is effectively
a quotient of `alist Ξ²` by permutation of the underlying list. -/
structure finmap (Ξ² : Ξ± β Type v) : Type (max u v) :=
(entries : multiset (sigma Ξ²))
(nodupkeys : entries.nodupkeys)
/-- The quotient map from `alist` to `finmap`. -/
def alist.to_finmap (s : alist Ξ²) : finmap Ξ² := β¨s.entries, s.nodupkeysβ©
local notation `β¦`:max a `β§`:0 := alist.to_finmap a
theorem alist.to_finmap_eq {sβ sβ : alist Ξ²} :
β¦sββ§ = β¦sββ§ β sβ.entries ~ sβ.entries :=
by cases sβ; cases sβ; simp [alist.to_finmap]
@[simp] theorem alist.to_finmap_entries (s : alist Ξ²) : β¦sβ§.entries = s.entries := rfl
def list.to_finmap [decidable_eq Ξ±] (s : list (sigma Ξ²)) : finmap Ξ² :=
alist.to_finmap (list.to_alist s)
namespace finmap
open alist
/-- Lift a permutation-respecting function on `alist` to `finmap`. -/
@[elab_as_eliminator] def lift_on
{Ξ³} (s : finmap Ξ²) (f : alist Ξ² β Ξ³)
(H : β a b : alist Ξ², a.entries ~ b.entries β f a = f b) : Ξ³ :=
begin
refine (quotient.lift_on s.1 (Ξ» l, (β¨_, Ξ» nd, f β¨l, ndβ©β© : roption Ξ³))
(Ξ» lβ lβ p, roption.ext' (perm_nodupkeys p) _) : roption Ξ³).get _,
{ exact Ξ» hβ hβ, H _ _ (by exact p) },
{ have := s.nodupkeys, rcases s.entries with β¨lβ©, exact id }
end
@[simp] theorem lift_on_to_finmap {Ξ³} (s : alist Ξ²) (f : alist Ξ² β Ξ³) (H) :
lift_on β¦sβ§ f H = f s := by cases s; refl
/-- Lift a permutation-respecting function on 2 `alist`s to 2 `finmap`s. -/
@[elab_as_eliminator] def lift_onβ
{Ξ³} (sβ sβ : finmap Ξ²) (f : alist Ξ² β alist Ξ² β Ξ³)
(H : β aβ bβ aβ bβ : alist Ξ², aβ.entries ~ aβ.entries β bβ.entries ~ bβ.entries β f aβ bβ = f aβ bβ) : Ξ³ :=
lift_on sβ
(Ξ» lβ, lift_on sβ (f lβ) (Ξ» bβ bβ p, H _ _ _ _ (perm.refl _) p))
(Ξ» aβ aβ p, have H' : f aβ = f aβ := funext (Ξ» _, H _ _ _ _ p (perm.refl _)), by simp only [H'])
@[simp] theorem lift_onβ_to_finmap {Ξ³} (sβ sβ : alist Ξ²) (f : alist Ξ² β alist Ξ² β Ξ³) (H) :
lift_onβ β¦sββ§ β¦sββ§ f H = f sβ sβ :=
by cases sβ; cases sβ; refl
@[elab_as_eliminator] theorem induction_on
{C : finmap Ξ² β Prop} (s : finmap Ξ²) (H : β (a : alist Ξ²), C β¦aβ§) : C s :=
by rcases s with β¨β¨aβ©, hβ©; exact H β¨a, hβ©
@[elab_as_eliminator] theorem induction_onβ {C : finmap Ξ² β finmap Ξ² β Prop}
(sβ sβ : finmap Ξ²) (H : β (aβ aβ : alist Ξ²), C β¦aββ§ β¦aββ§) : C sβ sβ :=
induction_on sβ $ Ξ» lβ, induction_on sβ $ Ξ» lβ, H lβ lβ
@[elab_as_eliminator] theorem induction_onβ {C : finmap Ξ² β finmap Ξ² β finmap Ξ² β Prop}
(sβ sβ sβ : finmap Ξ²) (H : β (aβ aβ aβ : alist Ξ²), C β¦aββ§ β¦aββ§ β¦aββ§) : C sβ sβ sβ :=
induction_onβ sβ sβ $ Ξ» lβ lβ, induction_on sβ $ Ξ» lβ, H lβ lβ lβ
@[ext] theorem ext : β {s t : finmap Ξ²}, s.entries = t.entries β s = t
| β¨lβ, hββ© β¨lβ, hββ© H := by congr'
@[simp] theorem ext_iff {s t : finmap Ξ²} : s.entries = t.entries β s = t :=
β¨ext, congr_arg _β©
/-- The predicate `a β s` means that `s` has a value associated to the key `a`. -/
instance : has_mem Ξ± (finmap Ξ²) := β¨Ξ» a s, a β s.entries.keysβ©
theorem mem_def {a : Ξ±} {s : finmap Ξ²} :
a β s β a β s.entries.keys := iff.rfl
@[simp] theorem mem_to_finmap {a : Ξ±} {s : alist Ξ²} :
a β β¦sβ§ β a β s := iff.rfl
/-- The set of keys of a finite map. -/
def keys (s : finmap Ξ²) : finset Ξ± :=
β¨s.entries.keys, induction_on s keys_nodupβ©
@[simp] theorem keys_val (s : alist Ξ²) : (keys β¦sβ§).val = s.keys := rfl
@[simp] theorem keys_ext {sβ sβ : alist Ξ²} :
keys β¦sββ§ = keys β¦sββ§ β sβ.keys ~ sβ.keys :=
by simp [keys, alist.keys]
theorem mem_keys {a : Ξ±} {s : finmap Ξ²} : a β s.keys β a β s :=
induction_on s $ Ξ» s, alist.mem_keys
/-- The empty map. -/
instance : has_emptyc (finmap Ξ²) := β¨β¨0, nodupkeys_nilβ©β©
instance : inhabited (finmap Ξ²) := β¨β
β©
@[simp] theorem empty_to_finmap : (β¦β
β§ : finmap Ξ²) = β
:= rfl
@[simp] theorem to_finmap_nil [decidable_eq Ξ±] : (list.to_finmap [] : finmap Ξ²) = β
:= rfl
theorem not_mem_empty {a : Ξ±} : a β (β
: finmap Ξ²) :=
multiset.not_mem_zero a
@[simp] theorem keys_empty : (β
: finmap Ξ²).keys = β
:= rfl
/-- The singleton map. -/
def singleton (a : Ξ±) (b : Ξ² a) : finmap Ξ² :=
β¦ alist.singleton a b β§
@[simp] theorem keys_singleton (a : Ξ±) (b : Ξ² a) :
(singleton a b).keys = finset.singleton a := rfl
@[simp] lemma mem_singleton (x y : Ξ±) (b : Ξ² y) : x β singleton y b β x = y :=
by simp only [singleton]; erw [mem_cons_eq,mem_nil_iff,or_false]
variables [decidable_eq Ξ±]
instance has_decidable_eq [β a, decidable_eq (Ξ² a)] : decidable_eq (finmap Ξ²)
| sβ sβ := decidable_of_iff _ ext_iff
/-- Look up the value associated to a key in a map. -/
def lookup (a : Ξ±) (s : finmap Ξ²) : option (Ξ² a) :=
lift_on s (lookup a) (Ξ» s t, perm_lookup)
@[simp] theorem lookup_to_finmap (a : Ξ±) (s : alist Ξ²) :
lookup a β¦sβ§ = s.lookup a := rfl
@[simp] theorem lookup_list_to_finmap (a : Ξ±) (s : list (sigma Ξ²)) : lookup a s.to_finmap = s.lookup a :=
by rw [list.to_finmap,lookup_to_finmap,lookup_to_alist]
@[simp] theorem lookup_empty (a) : lookup a (β
: finmap Ξ²) = none :=
rfl
theorem lookup_is_some {a : Ξ±} {s : finmap Ξ²} :
(s.lookup a).is_some β a β s :=
induction_on s $ Ξ» s, alist.lookup_is_some
theorem lookup_eq_none {a} {s : finmap Ξ²} : lookup a s = none β a β s :=
induction_on s $ Ξ» s, alist.lookup_eq_none
@[simp] lemma lookup_singleton_eq {a : Ξ±} {b : Ξ² a} : (singleton a b).lookup a = some b :=
by rw [singleton,lookup_to_finmap,alist.singleton,alist.lookup,lookup_cons_eq]
instance (a : Ξ±) (s : finmap Ξ²) : decidable (a β s) :=
decidable_of_iff _ lookup_is_some
/-- Replace a key with a given value in a finite map.
If the key is not present it does nothing. -/
def replace (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) : finmap Ξ² :=
lift_on s (Ξ» t, β¦replace a b tβ§) $
Ξ» sβ sβ p, to_finmap_eq.2 $ perm_replace p
@[simp] theorem replace_to_finmap (a : Ξ±) (b : Ξ² a) (s : alist Ξ²) :
replace a b β¦sβ§ = β¦s.replace a bβ§ := by simp [replace]
@[simp] theorem keys_replace (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) :
(replace a b s).keys = s.keys :=
induction_on s $ Ξ» s, by simp
@[simp] theorem mem_replace {a a' : Ξ±} {b : Ξ² a} {s : finmap Ξ²} :
a' β replace a b s β a' β s :=
induction_on s $ Ξ» s, by simp
/-- Fold a commutative function over the key-value pairs in the map -/
def foldl {Ξ΄ : Type w} (f : Ξ΄ β Ξ a, Ξ² a β Ξ΄)
(H : β d aβ bβ aβ bβ, f (f d aβ bβ) aβ bβ = f (f d aβ bβ) aβ bβ)
(d : Ξ΄) (m : finmap Ξ²) : Ξ΄ :=
m.entries.foldl (Ξ» d s, f d s.1 s.2) (Ξ» d s t, H _ _ _ _ _) d
def any (f : Ξ x, Ξ² x β bool) (s : finmap Ξ²) : bool :=
s.foldl (Ξ» x y z, x β¨ f y z) (by simp [or_assoc]; intros; congr' 2; rw or_comm) ff
def all (f : Ξ x, Ξ² x β bool) (s : finmap Ξ²) : bool :=
s.foldl (Ξ» x y z, x β§ f y z) (by simp [and_assoc]; intros; congr' 2; rw and_comm) ff
/-- Erase a key from the map. If the key is not present it does nothing. -/
def erase (a : Ξ±) (s : finmap Ξ²) : finmap Ξ² :=
lift_on s (Ξ» t, β¦erase a tβ§) $
Ξ» sβ sβ p, to_finmap_eq.2 $ perm_erase p
@[simp] theorem erase_to_finmap (a : Ξ±) (s : alist Ξ²) :
erase a β¦sβ§ = β¦s.erase aβ§ := by simp [erase]
@[simp] theorem keys_erase_to_finset (a : Ξ±) (s : alist Ξ²) :
keys β¦s.erase aβ§ = (keys β¦sβ§).erase a :=
by simp [finset.erase, keys, alist.erase, keys_kerase]
@[simp] theorem keys_erase (a : Ξ±) (s : finmap Ξ²) :
(erase a s).keys = s.keys.erase a :=
induction_on s $ Ξ» s, by simp
@[simp] theorem mem_erase {a a' : Ξ±} {s : finmap Ξ²} : a' β erase a s β a' β a β§ a' β s :=
induction_on s $ Ξ» s, by simp
theorem not_mem_erase_self {a : Ξ±} {s : finmap Ξ²} : Β¬ a β erase a s :=
by rw [mem_erase,not_and_distrib,not_not]; left; refl
@[simp] theorem lookup_erase (a) (s : finmap Ξ²) : lookup a (erase a s) = none :=
induction_on s $ lookup_erase a
@[simp] theorem lookup_erase_ne {a a'} {s : finmap Ξ²} (h : a β a') :
lookup a (erase a' s) = lookup a s :=
induction_on s $ Ξ» s, lookup_erase_ne h
theorem erase_erase {a a' : Ξ±} {s : finmap Ξ²} : erase a (erase a' s) = erase a' (erase a s) :=
induction_on s $ Ξ» s, ext (by simp [alist.erase_erase])
lemma mem_iff {a : Ξ±} {s : finmap Ξ²} : a β s β β b, s.lookup a = some b :=
induction_on s $ Ξ» s,
iff.trans list.mem_keys $ exists_congr $ Ξ» b,
(mem_lookup_iff s.nodupkeys).symm
lemma mem_of_lookup_eq_some {a : Ξ±} {b : Ξ² a} {s : finmap Ξ²} (h : s.lookup a = some b) : a β s :=
mem_iff.mpr β¨_,hβ©
/- sub -/
def sdiff (s s' : finmap Ξ²) : finmap Ξ² :=
s'.foldl (Ξ» s x _, s.erase x) (Ξ» aβ aβ _ aβ _, erase_erase) s
instance : has_sdiff (finmap Ξ²) :=
β¨ sdiff β©
/- insert -/
/-- Insert a key-value pair into a finite map, replacing any existing pair with
the same key. -/
def insert (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) : finmap Ξ² :=
lift_on s (Ξ» t, β¦insert a b tβ§) $
Ξ» sβ sβ p, to_finmap_eq.2 $ perm_insert p
@[simp] theorem insert_to_finmap (a : Ξ±) (b : Ξ² a) (s : alist Ξ²) :
insert a b β¦sβ§ = β¦s.insert a bβ§ := by simp [insert]
theorem insert_entries_of_neg {a : Ξ±} {b : Ξ² a} {s : finmap Ξ²} : a β s β
(insert a b s).entries = β¨a, bβ© :: s.entries :=
induction_on s $ Ξ» s h,
by simp [insert_entries_of_neg (mt mem_to_finmap.1 h)]
@[simp] theorem mem_insert {a a' : Ξ±} {b' : Ξ² a'} {s : finmap Ξ²} :
a β insert a' b' s β a = a' β¨ a β s :=
induction_on s mem_insert
@[simp] theorem lookup_insert {a} {b : Ξ² a} (s : finmap Ξ²) :
lookup a (insert a b s) = some b :=
induction_on s $ Ξ» s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert]
@[simp] theorem lookup_insert_of_ne {a a'} {b : Ξ² a} (s : finmap Ξ²) (h : a' β a) :
lookup a' (insert a b s) = lookup a' s :=
induction_on s $ Ξ» s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert_ne h]
@[simp] theorem insert_insert {a} {b b' : Ξ² a} (s : finmap Ξ²) : (s.insert a b).insert a b' = s.insert a b' :=
induction_on s $ Ξ» s,
by simp only [insert_to_finmap, insert_insert]
theorem insert_insert_of_ne {a a'} {b : Ξ² a} {b' : Ξ² a'} (s : finmap Ξ²) (h : a β a') :
(s.insert a b).insert a' b' = (s.insert a' b').insert a b :=
induction_on s $ Ξ» s,
by simp only [insert_to_finmap,alist.to_finmap_eq,insert_insert_of_ne _ h]
theorem to_finmap_cons (a : Ξ±) (b : Ξ² a) (xs : list (sigma Ξ²)) : list.to_finmap (β¨a,bβ© :: xs) = insert a b xs.to_finmap := rfl
theorem mem_list_to_finmap (a : Ξ±) (xs : list (sigma Ξ²)) : a β xs.to_finmap β (β b : Ξ² a, sigma.mk a b β xs) :=
by { induction xs with x xs; [skip, cases x];
simp only [to_finmap_cons, *, not_mem_empty, exists_or_distrib, list.not_mem_nil, finmap.to_finmap_nil, iff_self,
exists_false, mem_cons_iff, mem_insert, exists_and_distrib_left];
apply or_congr _ iff.rfl,
conv { to_lhs, rw β and_true (a = x_fst) },
apply and_congr_right, rintro β¨β©, simp only [exists_eq, iff_self, heq_iff_eq] }
@[simp] theorem insert_singleton_eq {a : Ξ±} {b b' : Ξ² a} : insert a b (singleton a b') = singleton a b :=
by simp only [singleton, finmap.insert_to_finmap, alist.insert_singleton_eq]
/- extract -/
/-- Erase a key from the map, and return the corresponding value, if found. -/
def extract (a : Ξ±) (s : finmap Ξ²) : option (Ξ² a) Γ finmap Ξ² :=
lift_on s (Ξ» t, prod.map id to_finmap (extract a t)) $
Ξ» sβ sβ p, by simp [perm_lookup p, to_finmap_eq, perm_erase p]
@[simp] theorem extract_eq_lookup_erase (a : Ξ±) (s : finmap Ξ²) :
extract a s = (lookup a s, erase a s) :=
induction_on s $ Ξ» s, by simp [extract]
/- union -/
/-- `sβ βͺ sβ` is the key-based union of two finite maps. It is left-biased: if
there exists an `a β sβ`, `lookup a (sβ βͺ sβ) = lookup a sβ`. -/
def union (sβ sβ : finmap Ξ²) : finmap Ξ² :=
lift_onβ sβ sβ (Ξ» sβ sβ, β¦sβ βͺ sββ§) $
Ξ» sβ sβ sβ sβ pββ pββ, to_finmap_eq.mpr $ perm_union pββ pββ
instance : has_union (finmap Ξ²) := β¨unionβ©
@[simp] theorem mem_union {a} {sβ sβ : finmap Ξ²} :
a β sβ βͺ sβ β a β sβ β¨ a β sβ :=
induction_onβ sβ sβ $ Ξ» _ _, mem_union
@[simp] theorem union_to_finmap (sβ sβ : alist Ξ²) : β¦sββ§ βͺ β¦sββ§ = β¦sβ βͺ sββ§ :=
by simp [(βͺ), union]
theorem keys_union {sβ sβ : finmap Ξ²} : (sβ βͺ sβ).keys = sβ.keys βͺ sβ.keys :=
induction_onβ sβ sβ $ Ξ» sβ sβ, finset.ext' $ by simp [keys]
@[simp] theorem lookup_union_left {a} {sβ sβ : finmap Ξ²} :
a β sβ β lookup a (sβ βͺ sβ) = lookup a sβ :=
induction_onβ sβ sβ $ Ξ» sβ sβ, lookup_union_left
@[simp] theorem lookup_union_right {a} {sβ sβ : finmap Ξ²} :
a β sβ β lookup a (sβ βͺ sβ) = lookup a sβ :=
induction_onβ sβ sβ $ Ξ» sβ sβ, lookup_union_right
theorem lookup_union_left_of_not_in {a} {sβ sβ : finmap Ξ²} :
a β sβ β lookup a (sβ βͺ sβ) = lookup a sβ :=
begin
intros h,
by_cases h' : a β sβ,
{ rw lookup_union_left h' },
{ rw [lookup_union_right h',lookup_eq_none.mpr h,lookup_eq_none.mpr h'] }
end
@[simp] theorem mem_lookup_union {a} {b : Ξ² a} {sβ sβ : finmap Ξ²} :
b β lookup a (sβ βͺ sβ) β b β lookup a sβ β¨ a β sβ β§ b β lookup a sβ :=
induction_onβ sβ sβ $ Ξ» sβ sβ, mem_lookup_union
theorem mem_lookup_union_middle {a} {b : Ξ² a} {sβ sβ sβ : finmap Ξ²} :
b β lookup a (sβ βͺ sβ) β a β sβ β b β lookup a (sβ βͺ sβ βͺ sβ) :=
induction_onβ sβ sβ sβ $ Ξ» sβ sβ sβ, mem_lookup_union_middle
theorem insert_union {a} {b : Ξ² a} {sβ sβ : finmap Ξ²} :
insert a b (sβ βͺ sβ) = insert a b sβ βͺ sβ :=
induction_onβ sβ sβ $ Ξ» aβ aβ, by simp [insert_union]
theorem union_assoc {sβ sβ sβ : finmap Ξ²} : (sβ βͺ sβ) βͺ sβ = sβ βͺ (sβ βͺ sβ) :=
induction_onβ sβ sβ sβ $ Ξ» sβ sβ sβ,
by simp only [alist.to_finmap_eq,union_to_finmap,alist.union_assoc]
@[simp] theorem empty_union {sβ : finmap Ξ²} : β
βͺ sβ = sβ :=
induction_on sβ $ Ξ» sβ,
by rw β empty_to_finmap; simp [- empty_to_finmap, alist.to_finmap_eq,union_to_finmap,alist.union_assoc]
@[simp] theorem union_empty {sβ : finmap Ξ²} : sβ βͺ β
= sβ :=
induction_on sβ $ Ξ» sβ,
by rw β empty_to_finmap; simp [- empty_to_finmap, alist.to_finmap_eq,union_to_finmap,alist.union_assoc]
theorem ext_lookup {sβ sβ : finmap Ξ²} : (β x, sβ.lookup x = sβ.lookup x) β sβ = sβ :=
induction_onβ sβ sβ $ Ξ» sβ sβ h,
by simp only [alist.lookup, lookup_to_finmap] at h;
rw [alist.to_finmap_eq]; apply lookup_ext sβ.nodupkeys sβ.nodupkeys;
intros x y; rw h
theorem erase_union_singleton (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) (h : s.lookup a = some b) :
s.erase a βͺ singleton a b = s :=
ext_lookup
(by { intro, by_cases h' : x = a,
{ subst a, rw [lookup_union_right not_mem_erase_self,lookup_singleton_eq,h], },
{ have : x β singleton a b, { rw mem_singleton, exact h' },
rw [lookup_union_left_of_not_in this,lookup_erase_ne h'] } } )
/- disjoint -/
def disjoint (sβ sβ : finmap Ξ²) :=
β x β sβ, Β¬ x β sβ
instance : decidable_rel (@disjoint Ξ± Ξ² _) :=
by intros x y; dsimp [disjoint]; apply_instance
lemma disjoint_empty (x : finmap Ξ²) : disjoint β
x .
@[symm]
lemma disjoint.symm (x y : finmap Ξ²) (h : disjoint x y) : disjoint y x :=
Ξ» p hy hx, h p hx hy
lemma disjoint.symm_iff (x y : finmap Ξ²) : disjoint x y β disjoint y x :=
β¨ disjoint.symm x y, disjoint.symm y x β©
lemma disjoint_union_left (x y z : finmap Ξ²) : disjoint (x βͺ y) z β disjoint x z β§ disjoint y z :=
by simp [disjoint,finmap.mem_union,or_imp_distrib,forall_and_distrib]
lemma disjoint_union_right (x y z : finmap Ξ²) : disjoint x (y βͺ z) β disjoint x y β§ disjoint x z :=
by rw [disjoint.symm_iff,disjoint_union_left,disjoint.symm_iff _ x,disjoint.symm_iff _ x]
theorem union_comm_of_disjoint {sβ sβ : finmap Ξ²} : disjoint sβ sβ β sβ βͺ sβ = sβ βͺ sβ :=
induction_onβ sβ sβ $ Ξ» sβ sβ,
by { intros h, simp only [alist.to_finmap_eq,union_to_finmap,alist.union_comm_of_disjoint h] }
theorem union_cancel {sβ sβ sβ : finmap Ξ²} (h : disjoint sβ sβ) (h' : disjoint sβ sβ) : sβ βͺ sβ = sβ βͺ sβ β sβ = sβ :=
β¨Ξ» h'', begin
apply ext_lookup, intro x,
have : (sβ βͺ sβ).lookup x = (sβ βͺ sβ).lookup x, from h'' βΈ rfl,
by_cases hsβ : x β sβ,
{ rw [lookup_union_left hsβ,lookup_union_left_of_not_in (h _ hsβ)] at this,
exact this },
{ by_cases hsβ : x β sβ,
{ rw [lookup_union_left_of_not_in (h' _ hsβ),lookup_union_left hsβ] at this; exact this },
{ rw [lookup_eq_none.mpr hsβ,lookup_eq_none.mpr hsβ] } }
end,
Ξ» h, h βΈ rflβ©
end finmap
|
bc7c61a38aafb6920eaf788a1f602b475514eab5 | 3446e92e64a5de7ed1f2109cfb024f83cd904c34 | /src/game/world2/level3.lean | ea12f38c326ad61194ec0162198ca55381b2d90d | [] | no_license | kckennylau/natural_number_game | 019f4a5f419c9681e65234ecd124c564f9a0a246 | ad8c0adaa725975be8a9f978c8494a39311029be | refs/heads/master | 1,598,784,137,722 | 1,571,905,156,000 | 1,571,905,156,000 | 218,354,686 | 0 | 0 | null | 1,572,373,319,000 | 1,572,373,318,000 | null | UTF-8 | Lean | false | false | 1,689 | lean | import mynat.definition -- hide
import mynat.add -- hide
import game.world2.level2 -- hide
namespace mynat -- hide
/-
# World 2 -- addition world
## Level 3 : `succ_inj`
## You are equipped with:
* `zero_ne_succ : β (a : mynat), zero β succ(a)`
* `succ_inj : β a b : mynat, succ(a) = succ(b) β a = b`
* `add_zero : β a : mynat, a + 0 = a`
* `add_succ : β a b : mynat, a + succ(b) = succ(a + b)`
* `zero_add` : β a : mynat, 0 + a = a`
* `add_assoc : β a b c : mynat, (a + b) + c = a + (b + c)`
Oh no! On the way to `add_comm`, a wild `succ_add` appears. `succ_add`
is the statement that `succ(a) + b = succ(a+b)` for `a` and `b` in your
natural number type. You will
need this theorem to prove `a + b = b + a` so you'd better prove it first.
NB: think about why computer scientists called this result `succ_add` .
There is a logic to all the names.
Note that if you want to be more precise about exactly where you want
to rewrite something like `add_succ`, you can do things like `rw add_succ (succ a)` or
`rw add_succ (succ a) d`, telling Lean explicitly what to use for
the input variables for the function `add_succ`. Indeed, `add_succ`
is a function -- it takes as input two variables `a` and `b` and outputs a proof
that `a + succ(b) = succ(a + b)`. The tactic `rw add_succ` just says to Lean "guess
what the variables are".
-/
/- Lemma
For all natural numbers $a, b$, we have
$$ \operatorname{succ}(a) + b = \operatorname{succ}(a + b). $$
-/
lemma succ_add (a b : mynat) : succ a + b = succ (a + b) :=
begin [less_leaky]
induction b with d hd,
{
refl
},
{ rw add_succ,
rw hd,
rw add_succ,
refl
}
end
end mynat -- hide
|
67c9120ffbcb07fe51e08c6896977d5dcf755ae2 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/data/mv_polynomial/rename.lean | 858e5b2cf69d032ec38378f47ecae4a9d5bf1c3f | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 11,035 | 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, Johan Commelin, Mario Carneiro
-/
import data.mv_polynomial.basic
/-!
# Renaming variables of polynomials
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file establishes the `rename` operation on multivariate polynomials,
which modifies the set of variables.
## Main declarations
* `mv_polynomial.rename`
* `mv_polynomial.rename_equiv`
## Notation
As in other polynomial files, we typically use the notation:
+ `Ο Ο Ξ± : Type*` (indexing the variables)
+ `R S : Type*` `[comm_semiring R]` `[comm_semiring S]` (the coefficients)
+ `s : Ο ββ β`, a function from `Ο` to `β` which is zero away from a finite set.
This will give rise to a monomial in `mv_polynomial Ο R` which mathematicians might call `X^s`
+ `r : R` elements of the coefficient ring
+ `i : Ο`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : mv_polynomial Ο Ξ±`
-/
noncomputable theory
open_locale classical big_operators
open set function finsupp add_monoid_algebra
open_locale big_operators
variables {Ο Ο Ξ± R S : Type*} [comm_semiring R] [comm_semiring S]
namespace mv_polynomial
section rename
/-- Rename all the variables in a multivariable polynomial. -/
def rename (f : Ο β Ο) : mv_polynomial Ο R ββ[R] mv_polynomial Ο R :=
aeval (X β f)
@[simp] lemma rename_C (f : Ο β Ο) (r : R) : rename f (C r) = C r :=
evalβ_C _ _ _
@[simp] lemma rename_X (f : Ο β Ο) (i : Ο) : rename f (X i : mv_polynomial Ο R) = X (f i) :=
evalβ_X _ _ _
lemma map_rename (f : R β+* S) (g : Ο β Ο) (p : mv_polynomial Ο R) :
map f (rename g p) = rename g (map f p) :=
mv_polynomial.induction_on p
(Ξ» a, by simp only [map_C, rename_C])
(Ξ» p q hp hq, by simp only [hp, hq, alg_hom.map_add, ring_hom.map_add])
(Ξ» p n hp, by simp only [hp, rename_X, map_X, ring_hom.map_mul, alg_hom.map_mul])
@[simp] lemma rename_rename (f : Ο β Ο) (g : Ο β Ξ±) (p : mv_polynomial Ο R) :
rename g (rename f p) = rename (g β f) p :=
show rename g (evalβ C (X β f) p) = _,
begin
simp only [rename, aeval_eq_evalβ_hom],
simp [evalβ_comp_left _ C (X β f) p, (β), evalβ_C, eval_X],
apply evalβ_hom_congr _ rfl rfl,
ext1, simp only [comp_app, ring_hom.coe_comp, evalβ_hom_C],
end
@[simp] lemma rename_id (p : mv_polynomial Ο R) : rename id p = p :=
evalβ_eta p
lemma rename_monomial (f : Ο β Ο) (d : Ο ββ β) (r : R) :
rename f (monomial d r) = monomial (d.map_domain f) r :=
begin
rw [rename, aeval_monomial, monomial_eq, finsupp.prod_map_domain_index],
{ refl },
{ exact assume n, pow_zero _ },
{ exact assume n iβ iβ, pow_add _ _ _ }
end
lemma rename_eq (f : Ο β Ο) (p : mv_polynomial Ο R) :
rename f p = finsupp.map_domain (finsupp.map_domain f) p :=
begin
simp only [rename, aeval_def, evalβ, finsupp.map_domain, algebra_map_eq, X_pow_eq_monomial,
β monomial_finsupp_sum_index],
refl
end
lemma rename_injective (f : Ο β Ο) (hf : function.injective f) :
function.injective (rename f : mv_polynomial Ο R β mv_polynomial Ο R) :=
have (rename f : mv_polynomial Ο R β mv_polynomial Ο R) =
finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f),
begin
rw this,
exact finsupp.map_domain_injective (finsupp.map_domain_injective hf)
end
section
variables {f : Ο β Ο} (hf : function.injective f)
open_locale classical
/-- Given a function between sets of variables `f : Ο β Ο` that is injective with proof `hf`,
`kill_compl hf` is the `alg_hom` from `R[Ο]` to `R[Ο]` that is left inverse to
`rename f : R[Ο] β R[Ο]` and sends the variables in the complement of the range of `f` to `0`. -/
def kill_compl : mv_polynomial Ο R ββ[R] mv_polynomial Ο R :=
aeval (Ξ» i, if h : i β set.range f then X $ (equiv.of_injective f hf).symm β¨i,hβ© else 0)
lemma kill_compl_comp_rename : (kill_compl hf).comp (rename f) = alg_hom.id R _ := alg_hom_ext $
Ξ» i, by { dsimp, rw [rename, kill_compl, aeval_X, aeval_X, dif_pos, equiv.of_injective_symm_apply] }
@[simp] lemma kill_compl_rename_app (p : mv_polynomial Ο R) : kill_compl hf (rename f p) = p :=
alg_hom.congr_fun (kill_compl_comp_rename hf) p
end
section
variables (R)
/-- `mv_polynomial.rename e` is an equivalence when `e` is. -/
@[simps apply]
def rename_equiv (f : Ο β Ο) : mv_polynomial Ο R ββ[R] mv_polynomial Ο R :=
{ to_fun := rename f,
inv_fun := rename f.symm,
left_inv := Ξ» p, by rw [rename_rename, f.symm_comp_self, rename_id],
right_inv := Ξ» p, by rw [rename_rename, f.self_comp_symm, rename_id],
..rename f}
@[simp] lemma rename_equiv_refl :
rename_equiv R (equiv.refl Ο) = alg_equiv.refl :=
alg_equiv.ext rename_id
@[simp] lemma rename_equiv_symm (f : Ο β Ο) :
(rename_equiv R f).symm = rename_equiv R f.symm := rfl
@[simp] lemma rename_equiv_trans (e : Ο β Ο) (f : Ο β Ξ±):
(rename_equiv R e).trans (rename_equiv R f) = rename_equiv R (e.trans f) :=
alg_equiv.ext (rename_rename e f)
end
section
variables (f : R β+* S) (k : Ο β Ο) (g : Ο β S) (p : mv_polynomial Ο R)
lemma evalβ_rename : (rename k p).evalβ f g = p.evalβ f (g β k) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma evalβ_hom_rename : evalβ_hom f g (rename k p) = evalβ_hom f (g β k) p :=
evalβ_rename _ _ _ _
lemma aeval_rename [algebra R S] : aeval g (rename k p) = aeval (g β k) p :=
evalβ_hom_rename _ _ _ _
lemma rename_evalβ (g : Ο β mv_polynomial Ο R) :
rename k (p.evalβ C (g β k)) = (rename k p).evalβ C (rename k β g) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_prodmk_evalβ (j : Ο) (g : Ο β mv_polynomial Ο R) :
rename (prod.mk j) (p.evalβ C g) = p.evalβ C (Ξ» x, rename (prod.mk j) (g x)) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma evalβ_rename_prodmk (g : Ο Γ Ο β S) (i : Ο) (p : mv_polynomial Ο R) :
(rename (prod.mk i) p).evalβ f g = evalβ f (Ξ» j, g (i, j)) p :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval_rename_prodmk (g : Ο Γ Ο β R) (i : Ο) (p : mv_polynomial Ο R) :
eval g (rename (prod.mk i) p) = eval (Ξ» j, g (i, j)) p :=
evalβ_rename_prodmk (ring_hom.id _) _ _ _
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_finset_rename (p : mv_polynomial Ο R) :
β (s : finset Ο) (q : mv_polynomial {x // x β s} R), p = rename coe q :=
begin
apply induction_on p,
{ intro r, exact β¨β
, C r, by rw rename_Cβ© },
{ rintro p q β¨s, p, rflβ© β¨t, q, rflβ©,
refine β¨s βͺ t, β¨_, _β©β©,
{ refine rename (subtype.map id _) p + rename (subtype.map id _) q;
simp only [id.def, true_or, or_true, finset.mem_union, forall_true_iff] {contextual := tt}, },
{ simp only [rename_rename, alg_hom.map_add], refl, }, },
{ rintro p n β¨s, p, rflβ©,
refine β¨insert n s, β¨_, _β©β©,
{ refine rename (subtype.map id _) p * X β¨n, s.mem_insert_self nβ©,
simp only [id.def, or_true, finset.mem_insert, forall_true_iff] {contextual := tt}, },
{ simp only [rename_rename, rename_X, subtype.coe_mk, alg_hom.map_mul], refl, }, },
end
/-- `exists_finset_rename` for two polyonomials at once: for any two polynomials `pβ`, `pβ` in a
polynomial semiring `R[Ο]` of possibly infinitely many variables, `exists_finset_renameβ` yields
a finite subset `s` of `Ο` such that both `pβ` and `pβ` are contained in the polynomial semiring
`R[s]` of finitely many variables. -/
lemma exists_finset_renameβ (pβ pβ : mv_polynomial Ο R) :
β (s : finset Ο) (qβ qβ : mv_polynomial s R), pβ = rename coe qβ β§ pβ = rename coe qβ :=
begin
obtain β¨sβ,qβ,rflβ© := exists_finset_rename pβ,
obtain β¨sβ,qβ,rflβ© := exists_finset_rename pβ,
classical, use sβ βͺ sβ,
use rename (set.inclusion $ sβ.subset_union_left sβ) qβ,
use rename (set.inclusion $ sβ.subset_union_right sβ) qβ,
split; simpa,
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_fin_rename (p : mv_polynomial Ο R) :
β (n : β) (f : fin n β Ο) (hf : injective f) (q : mv_polynomial (fin n) R), p = rename f q :=
begin
obtain β¨s, q, rflβ© := exists_finset_rename p,
let n := fintype.card {x // x β s},
let e := fintype.equiv_fin {x // x β s},
refine β¨n, coe β e.symm, subtype.val_injective.comp e.symm.injective, rename e q, _β©,
rw [β rename_rename, rename_rename e],
simp only [function.comp, equiv.symm_apply_apply, rename_rename]
end
end rename
lemma evalβ_cast_comp (f : Ο β Ο) (c : β€ β+* R) (g : Ο β R) (p : mv_polynomial Ο β€) :
evalβ c (g β f) p = evalβ c g (rename f p) :=
mv_polynomial.induction_on p
(Ξ» n, by simp only [evalβ_C, rename_C])
(Ξ» p q hp hq, by simp only [hp, hq, rename, evalβ_add, alg_hom.map_add])
(Ξ» p n hp, by simp only [hp, rename, aeval_def, evalβ_X, evalβ_mul])
section coeff
@[simp]
lemma coeff_rename_map_domain (f : Ο β Ο) (hf : injective f) (Ο : mv_polynomial Ο R) (d : Ο ββ β) :
(rename f Ο).coeff (d.map_domain f) = Ο.coeff d :=
begin
apply induction_on' Ο,
{ intros u r,
rw [rename_monomial, coeff_monomial, coeff_monomial],
simp only [(finsupp.map_domain_injective hf).eq_iff] },
{ intros, simp only [*, alg_hom.map_add, coeff_add], }
end
lemma coeff_rename_eq_zero (f : Ο β Ο) (Ο : mv_polynomial Ο R) (d : Ο ββ β)
(h : β u : Ο ββ β, u.map_domain f = d β Ο.coeff u = 0) :
(rename f Ο).coeff d = 0 :=
begin
rw [rename_eq, β not_mem_support_iff],
intro H,
replace H := map_domain_support H,
rw [finset.mem_image] at H,
obtain β¨u, hu, rflβ© := H,
specialize h u rfl,
simp at h hu,
contradiction
end
lemma coeff_rename_ne_zero (f : Ο β Ο) (Ο : mv_polynomial Ο R) (d : Ο ββ β)
(h : (rename f Ο).coeff d β 0) :
β u : Ο ββ β, u.map_domain f = d β§ Ο.coeff u β 0 :=
by { contrapose! h, apply coeff_rename_eq_zero _ _ _ h }
@[simp] lemma constant_coeff_rename {Ο : Type*} (f : Ο β Ο) (Ο : mv_polynomial Ο R) :
constant_coeff (rename f Ο) = constant_coeff Ο :=
begin
apply Ο.induction_on,
{ intro a, simp only [constant_coeff_C, rename_C]},
{ intros p q hp hq, simp only [hp, hq, ring_hom.map_add, alg_hom.map_add] },
{ intros p n hp, simp only [hp, rename_X, constant_coeff_X, ring_hom.map_mul, alg_hom.map_mul] }
end
end coeff
section support
lemma support_rename_of_injective {p : mv_polynomial Ο R} {f : Ο β Ο} (h : function.injective f) :
(rename f p).support = finset.image (map_domain f) p.support :=
begin
rw rename_eq,
exact finsupp.map_domain_support_of_injective (map_domain_injective h) _,
end
end support
end mv_polynomial
|
4b3ea827ab9c2ac4eebbd4c614c057eccf36c343 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/polynomial/monomial.lean | e0a1a3cb90c7550eef77fdaa74262d0248c9e6fb | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,613 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes HΓΆlzl, Scott Morrison, Jens Wagemaker
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.polynomial.basic
import Mathlib.PostPort
universes u u_1
namespace Mathlib
/-!
# Univariate monomials
Preparatory lemmas for degree_basic.
-/
namespace polynomial
/--
`C a` is the constant polynomial `a`.
`C` is provided as a ring homomorphism.
-/
def C {R : Type u} [semiring R] : R β+* polynomial R :=
add_monoid_algebra.single_zero_ring_hom
@[simp] theorem monomial_zero_left {R : Type u} [semiring R] (a : R) : coe_fn (monomial 0) a = coe_fn C a :=
rfl
theorem C_0 {R : Type u} [semiring R] : coe_fn C 0 = 0 :=
finsupp.single_zero
theorem C_1 {R : Type u} [semiring R] : coe_fn C 1 = 1 :=
rfl
theorem C_mul {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C (a * b) = coe_fn C a * coe_fn C b :=
ring_hom.map_mul C a b
theorem C_add {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C (a + b) = coe_fn C a + coe_fn C b :=
ring_hom.map_add C a b
@[simp] theorem C_bit0 {R : Type u} {a : R} [semiring R] : coe_fn C (bit0 a) = bit0 (coe_fn C a) :=
C_add
@[simp] theorem C_bit1 {R : Type u} {a : R} [semiring R] : coe_fn C (bit1 a) = bit1 (coe_fn C a) := sorry
theorem C_pow {R : Type u} {a : R} {n : β} [semiring R] : coe_fn C (a ^ n) = coe_fn C a ^ n :=
ring_hom.map_pow C a n
@[simp] theorem C_eq_nat_cast {R : Type u} [semiring R] (n : β) : coe_fn C βn = βn :=
ring_hom.map_nat_cast C n
@[simp] theorem sum_C_index {R : Type u} [semiring R] {a : R} {Ξ² : Type u_1} [add_comm_monoid Ξ²] {f : β β R β Ξ²} (h : f 0 0 = 0) : finsupp.sum (coe_fn C a) f = f 0 a :=
finsupp.sum_single_index h
theorem coeff_C {R : Type u} {a : R} {n : β} [semiring R] : coeff (coe_fn C a) n = ite (n = 0) a 0 := sorry
@[simp] theorem coeff_C_zero {R : Type u} {a : R} [semiring R] : coeff (coe_fn C a) 0 = a :=
coeff_monomial
theorem nontrivial.of_polynomial_ne {R : Type u} [semiring R] {p : polynomial R} {q : polynomial R} (h : p β q) : nontrivial R := sorry
theorem single_eq_C_mul_X {R : Type u} {a : R} [semiring R] {n : β} : coe_fn (monomial n) a = coe_fn C a * X ^ n := sorry
@[simp] theorem C_inj {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C a = coe_fn C b β a = b :=
{ mp := fun (h : coe_fn C a = coe_fn C b) => Eq.trans (Eq.symm coeff_C_zero) (Eq.symm h βΈ coeff_C_zero),
mpr := congr_arg βC }
@[simp] theorem C_eq_zero {R : Type u} {a : R} [semiring R] : coe_fn C a = 0 β a = 0 :=
iff.trans
(eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn C a = 0 β coe_fn C a = coe_fn C 0)) C_0)) (iff.refl (coe_fn C a = 0))) C_inj
protected instance infinite {R : Type u} [semiring R] [nontrivial R] : infinite (polynomial R) := sorry
theorem monomial_eq_smul_X {R : Type u} {a : R} [semiring R] {n : β} : coe_fn (monomial n) a = a β’ X ^ n := sorry
theorem ring_hom_ext {R : Type u} [semiring R] {S : Type u_1} [semiring S] {f : polynomial R β+* S} {g : polynomial R β+* S} (hβ : β (a : R), coe_fn f (coe_fn C a) = coe_fn g (coe_fn C a)) (hβ : coe_fn f X = coe_fn g X) : f = g :=
add_monoid_algebra.ring_hom_ext' (ring_hom.ext fun (x : R) => hβ x) (monoid_hom.ext_mnat hβ)
theorem ring_hom_ext' {R : Type u} [semiring R] {S : Type u_1} [semiring S] {f : polynomial R β+* S} {g : polynomial R β+* S} (hβ : ring_hom.comp f C = ring_hom.comp g C) (hβ : coe_fn f X = coe_fn g X) : f = g :=
ring_hom_ext (ring_hom.congr_fun hβ) hβ
|
7b3fa3722ecf8130bd84322242040d29e783c2b6 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/groupoid/free_groupoid.lean | 33fb79fc5f4912c195be2e27b5bf754deb0059a6 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,694 | lean | /-
Copyright (c) 2022 RΓ©mi Bottinelli. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: RΓ©mi Bottinelli
-/
import category_theory.category.basic
import category_theory.functor.basic
import category_theory.groupoid
import tactic.nth_rewrite
import category_theory.path_category
import category_theory.quotient
import combinatorics.quiver.symmetric
/-!
# Free groupoid on a quiver
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the free groupoid on a quiver, the lifting of a prefunctor to its unique
extension as a functor from the free groupoid, and proves uniqueness of this extension.
## Main results
Given the type `V` and a quiver instance on `V`:
- `free_groupoid V`: a type synonym for `V`.
- `free_groupoid_groupoid`: the `groupoid` instance on `free_groupoid V`.
- `lift`: the lifting of a prefunctor from `V` to `V'` where `V'` is a groupoid, to a functor.
`free_groupoid V β₯€ V'`.
- `lift_spec` and `lift_unique`: the proofs that, respectively, `lift` indeed is a lifting
and is the unique one.
## Implementation notes
The free groupoid is first defined by symmetrifying the quiver, taking the induced path category
and finally quotienting by the reducibility relation.
-/
open set classical function
local attribute [instance] prop_decidable
namespace category_theory
namespace groupoid
namespace free
universes u v u' v' u'' v''
variables {V : Type u} [quiver.{v+1} V]
/-- Shorthand for the "forward" arrow corresponding to `f` in `paths $ symmetrify V` -/
abbreviation quiver.hom.to_pos_path {X Y : V} (f : X βΆ Y) :
((category_theory.paths.category_paths $ quiver.symmetrify V).hom X Y) := f.to_pos.to_path
/-- Shorthand for the "forward" arrow corresponding to `f` in `paths $ symmetrify V` -/
abbreviation quiver.hom.to_neg_path {X Y : V} (f : X βΆ Y) :
((category_theory.paths.category_paths $ quiver.symmetrify V).hom Y X) := f.to_neg.to_path
/-- The "reduction" relation -/
inductive red_step : hom_rel (paths (quiver.symmetrify V))
| step (X Z : quiver.symmetrify V) (f : X βΆ Z) :
red_step (π X) (f.to_path β« (quiver.reverse f).to_path)
/-- The underlying vertices of the free groupoid -/
def _root_.category_theory.free_groupoid (V) [Q : quiver V] := quotient (@red_step V Q)
instance {V} [Q : quiver V] [h : nonempty V] : nonempty (free_groupoid V) := β¨β¨h.someβ©β©
lemma congr_reverse {X Y : paths $ quiver.symmetrify V} (p q : X βΆ Y) :
quotient.comp_closure red_step p q β
quotient.comp_closure red_step (p.reverse) (q.reverse) :=
begin
rintro β¨XW, pp, qq, WY, _, Z, fβ©,
have : quotient.comp_closure red_step (WY.reverse β« π _ β« XW.reverse)
(WY.reverse β« (f.to_path β« (quiver.reverse f).to_path) β« XW.reverse),
{ apply quotient.comp_closure.intro,
apply red_step.step, },
simpa only [category_struct.comp, category_struct.id, quiver.path.reverse, quiver.path.nil_comp,
quiver.path.reverse_comp, quiver.reverse_reverse, quiver.path.reverse_to_path,
quiver.path.comp_assoc] using this,
end
lemma congr_comp_reverse {X Y : paths $ quiver.symmetrify V} (p : X βΆ Y) :
quot.mk (@quotient.comp_closure _ _ red_step _ _) (p β« p.reverse) =
quot.mk (@quotient.comp_closure _ _ red_step _ _) (π X) :=
begin
apply quot.eqv_gen_sound,
induction p with _ _ q f ih,
{ apply eqv_gen.refl, },
{ simp only [quiver.path.reverse],
fapply eqv_gen.trans,
{ exact q β« q.reverse, },
{ apply eqv_gen.symm, apply eqv_gen.rel,
have : quotient.comp_closure
red_step (q β« (π _) β« q.reverse)
(q β« (f.to_path β« (quiver.reverse f).to_path) β« q.reverse), by
{ apply quotient.comp_closure.intro, apply red_step.step, },
have that : q.cons f = q.comp f.to_path, by refl, rw that,
simp only [category.assoc, category.id_comp] at this β’,
simp only [category_struct.comp, quiver.path.comp_assoc] at this β’,
exact this, },
{ exact ih }, },
end
lemma congr_reverse_comp {X Y : paths $ quiver.symmetrify V} (p : X βΆ Y) :
quot.mk (@quotient.comp_closure _ _ red_step _ _) (p.reverse β« p) =
quot.mk (@quotient.comp_closure _ _ red_step _ _) (π Y) :=
begin
nth_rewrite 1 βquiver.path.reverse_reverse p,
apply congr_comp_reverse,
end
instance : category (free_groupoid V) := quotient.category red_step
/-- The inverse of an arrow in the free groupoid -/
def quot_inv {X Y : free_groupoid V} (f : X βΆ Y) : Y βΆ X :=
quot.lift_on f
(Ξ» pp, quot.mk _ $ pp.reverse)
(Ξ» pp qq con, quot.sound $ congr_reverse pp qq con)
instance : groupoid (free_groupoid V) :=
{ inv := Ξ» X Y f, quot_inv f,
inv_comp' := Ξ» X Y p, quot.induction_on p $ Ξ» pp, congr_reverse_comp pp,
comp_inv' := Ξ» X Y p, quot.induction_on p $ Ξ» pp, congr_comp_reverse pp }
/-- The inclusion of the quiver on `V` to the underlying quiver on `free_groupoid V`-/
def of (V) [quiver V] : V β₯€q (free_groupoid V) :=
{ obj := Ξ» X, β¨Xβ©,
map := Ξ» X Y f, quot.mk _ f.to_pos_path }
lemma of_eq : of V =
(quiver.symmetrify.of βq paths.of).comp (quotient.functor $ @red_step V _).to_prefunctor :=
begin
apply prefunctor.ext, rotate,
{ rintro X, refl, },
{ rintro X Y f, refl, }
end
section universal_property
variables {V' : Type u'} [groupoid V'] (Ο : V β₯€q V')
/-- The lift of a prefunctor to a groupoid, to a functor from `free_groupoid V` -/
def lift (Ο : V β₯€q V') : free_groupoid V β₯€ V' :=
quotient.lift _
(paths.lift $ quiver.symmetrify.lift Ο)
(by
{ rintros _ _ _ _ β¨X,Y,fβ©,
simp only [quiver.symmetrify.lift_reverse, paths.lift_nil, quiver.path.comp_nil,
paths.lift_cons, paths.lift_to_path],
symmetry,
apply groupoid.comp_inv, })
lemma lift_spec (Ο : V β₯€q V') : of V βq (lift Ο).to_prefunctor = Ο :=
begin
rw [of_eq, prefunctor.comp_assoc, prefunctor.comp_assoc, functor.to_prefunctor_comp],
dsimp [lift],
rw [quotient.lift_spec, paths.lift_spec, quiver.symmetrify.lift_spec],
end
lemma lift_unique (Ο : V β₯€q V') (Ξ¦ : free_groupoid V β₯€ V')
(hΞ¦ : of V βq Ξ¦.to_prefunctor = Ο) : Ξ¦ = lift Ο :=
begin
apply quotient.lift_unique,
apply paths.lift_unique,
fapply @quiver.symmetrify.lift_unique _ _ _ _ _ _ _ _ _,
{ rw βfunctor.to_prefunctor_comp, exact hΞ¦, },
{ constructor, rintros X Y f,
simp only [βfunctor.to_prefunctor_comp,prefunctor.comp_map, paths.of_map, inv_eq_inv],
change Ξ¦.map (inv ((quotient.functor red_step).to_prefunctor.map f.to_path)) =
inv (Ξ¦.map ((quotient.functor red_step).to_prefunctor.map f.to_path)),
have := functor.map_inv Ξ¦ ((quotient.functor red_step).to_prefunctor.map f.to_path),
convert this; simp only [inv_eq_inv], },
end
end universal_property
section functoriality
variables {V' : Type u'} [quiver.{v'+1} V'] {V'' : Type u''} [quiver.{v''+1} V'']
/-- The functor of free groupoid induced by a prefunctor of quivers -/
def _root_.category_theory.free_groupoid_functor (Ο : V β₯€q V') :
free_groupoid V β₯€ free_groupoid V' := lift (Ο βq of V')
lemma free_groupoid_functor_id :
free_groupoid_functor (prefunctor.id V) = functor.id (free_groupoid V) :=
begin
dsimp only [free_groupoid_functor], symmetry,
apply lift_unique, refl,
end
lemma free_groupoid_functor_comp
(Ο : V β₯€q V') (Ο' : V' β₯€q V'') :
free_groupoid_functor (Ο βq Ο') = free_groupoid_functor Ο β free_groupoid_functor Ο' :=
begin
dsimp only [free_groupoid_functor], symmetry,
apply lift_unique, refl,
end
end functoriality
end free
end groupoid
end category_theory
|
d5e7bd8e85b552a619b84caf9a96fe51c90e21d7 | 6214e13b31733dc9aeb4833db6a6466005763162 | /src/others.lean | cf938e414b1edcf7f87ee3237ebc45c7c1fe2e99 | [] | no_license | joshua0pang/esverify-theory | 272a250445f3aeea49a7e72d1ab58c2da6618bbe | 8565b123c87b0113f83553d7732cd6696c9b5807 | refs/heads/master | 1,585,873,849,081 | 1,527,304,393,000 | 1,527,304,393,000 | 154,901,199 | 1 | 0 | null | 1,540,593,067,000 | 1,540,593,067,000 | null | UTF-8 | Lean | false | false | 5,087 | lean | -- other minor lemmas related to verification but not included in the other files
import .definitions2
lemma unchanged_of_apply_termctx_without_hole {t tt: term}:
t.to_termctx tt = t :=
begin
induction t with v y unop tβ ihβ binop tβ tβ ihβ ihβ tβ tβ
ihβ ihβ
,
show (termctx.apply (term.to_termctx (term.value v)) tt = term.value v), by begin
unfold term.to_termctx,
unfold termctx.apply
end,
show (termctx.apply (term.to_termctx (term.var y)) tt = term.var y), by begin
unfold term.to_termctx,
unfold termctx.apply
end,
show (termctx.apply (term.to_termctx (term.unop unop tβ)) tt = term.unop unop tβ), by begin
unfold term.to_termctx,
unfold termctx.apply,
congr,
from ihβ
end,
show (termctx.apply (term.to_termctx (term.binop binop tβ tβ)) tt = term.binop binop tβ tβ), by begin
unfold term.to_termctx,
unfold termctx.apply,
congr,
from ihβ,
from ihβ
end,
show (termctx.apply (term.to_termctx (term.app tβ tβ
)) tt = term.app tβ tβ
), by begin
unfold term.to_termctx,
unfold termctx.apply,
congr,
from ihβ,
from ihβ
end
end
lemma unchanged_of_apply_propctx_without_hole {P: prop} {t: term}:
P.to_propctx t = P :=
begin
change (propctx.apply (prop.to_propctx P) t = P),
induction P,
case prop.term tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole
},
case prop.not Pβ ih {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from ih
},
case prop.and Pβ Pβ ihβ ihβ {
unfold prop.to_propctx,
change (propctx.apply (propctx.and (prop.to_propctx Pβ) (prop.to_propctx Pβ)) t = prop.and Pβ Pβ),
unfold propctx.apply,
congr,
from ihβ,
from ihβ
},
case prop.or Pβ Pβ ihβ ihβ {
unfold prop.to_propctx,
change (propctx.apply (propctx.or (prop.to_propctx Pβ) (prop.to_propctx Pβ)) t = prop.or Pβ Pβ),
unfold propctx.apply,
congr,
from ihβ,
from ihβ
},
case prop.pre tβ tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole,
from unchanged_of_apply_termctx_without_hole
},
case prop.preβ op tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole
},
case prop.preβ op tβ tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole,
from unchanged_of_apply_termctx_without_hole
},
case prop.post tβ tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole,
from unchanged_of_apply_termctx_without_hole
},
case prop.call tβ tβ {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from unchanged_of_apply_termctx_without_hole
},
case prop.forallc y Pβ ih {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from ih
},
case prop.exis y Pβ ih {
unfold prop.to_propctx,
unfold propctx.apply,
congr,
from ih
}
end
lemma vc.term.inj.inv {tβ tβ: term}: (tβ = tβ) β (vc.term tβ = vc.term tβ) :=
begin
assume h1,
congr,
from h1
end
lemma vc.not.inj.inv {P Q: vc}: (P = Q) β (vc.not P = vc.not Q) :=
begin
assume h1,
congr,
from h1
end
lemma vc.and.inj.inv {Pβ Pβ Pβ Pβ: vc}: (Pβ = Pβ) β (Pβ = Pβ) β (vc.and Pβ Pβ = vc.and Pβ Pβ) :=
begin
assume h1,
assume h2,
congr,
from h1,
from h2
end
lemma vc.or.inj.inv {Pβ Pβ Pβ Pβ: vc}: (Pβ = Pβ) β (Pβ = Pβ) β (vc.or Pβ Pβ = vc.or Pβ Pβ) :=
begin
assume h1,
assume h2,
congr,
from h1,
from h2
end
lemma vc.pre.inj.inv {tβ tβ tβ tβ: term}: (tβ = tβ) β (tβ = tβ) β (vc.pre tβ tβ = vc.pre tβ tβ) :=
begin
assume h1,
assume h2,
congr,
from h1,
from h2
end
lemma vc.preβ.inj.inv {tβ tβ: term} {op: unop}: (tβ = tβ) β (vc.preβ op tβ = vc.preβ op tβ) :=
begin
assume h1,
congr,
from h1
end
lemma vc.preβ.inj.inv {tβ tβ tβ tβ: term} {op: binop}: (tβ = tβ) β (tβ = tβ) β (vc.preβ op tβ tβ = vc.preβ op tβ tβ) :=
begin
assume h1,
assume h2,
congr,
from h1,
from h2
end
lemma vc.post.inj.inv {tβ tβ tβ tβ: term}: (tβ = tβ) β (tβ = tβ) β (vc.post tβ tβ = vc.post tβ tβ) :=
begin
assume h1,
assume h2,
congr,
from h1,
from h2
end
lemma vc.univ.inj.inv {P Q: vc} {x: var}: (P = Q) β (vc.univ x P = vc.univ x Q) :=
begin
assume h1,
congr,
from h1
end
|
cf9b11a1bf12240d53c4660c3fc4a62c04d38fe5 | 618003631150032a5676f229d13a079ac875ff77 | /src/category_theory/products/basic.lean | a9f67a2b8244df3a035ba5bc440a3e53b7582008 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 5,457 | 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_theory.eq_to_hom
namespace category_theory
universes vβ vβ vβ vβ uβ uβ uβ uβ -- declare the `v`'s first; see `category_theory.category` for an explanation
section
variables (C : Type uβ) [category.{vβ} C] (D : Type uβ) [category.{vβ} D]
/--
`prod C D` gives the cartesian product of two categories.
-/
instance prod : category.{max vβ vβ} (C Γ D) :=
{ hom := Ξ» X Y, ((X.1) βΆ (Y.1)) Γ ((X.2) βΆ (Y.2)),
id := Ξ» X, β¨ π (X.1), π (X.2) β©,
comp := Ξ» _ _ _ f g, (f.1 β« g.1, f.2 β« g.2) }
-- rfl lemmas for category.prod
@[simp] lemma prod_id (X : C) (Y : D) : π (X, Y) = (π X, π Y) := rfl
@[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) βΆ (Q, T)) (g : (Q, T) βΆ (R, U)) :
f β« g = (f.1 β« g.1, f.2 β« g.2) := rfl
@[simp] lemma prod_id_fst (X : prod C D) : prod.fst (π X) = π X.fst := rfl
@[simp] lemma prod_id_snd (X : prod C D) : prod.snd (π X) = π X.snd := rfl
@[simp] lemma prod_comp_fst {X Y Z : prod C D} (f : X βΆ Y) (g : Y βΆ Z) :
(f β« g).1 = f.1 β« g.1 := rfl
@[simp] lemma prod_comp_snd {X Y Z : prod C D} (f : X βΆ Y) (g : Y βΆ Z) :
(f β« g).2 = f.2 β« g.2 := rfl
end
section
variables (C : Type uβ) [category.{vβ} C] (D : Type uβ) [category.{vβ} D]
/--
`prod.category.uniform C D` is an additional instance specialised so both factors have the same
universe levels. This helps typeclass resolution.
-/
instance uniform_prod : category (C Γ D) := category_theory.prod C D
end
-- Next we define the natural functors into and out of product categories. For now this doesn't
-- address the universal properties.
namespace prod
/-- `sectl C Z` is the functor `C β₯€ C Γ D` given by `X β¦ (X, Z)`. -/
-- Here and below we specify explicitly the projections to generate `@[simp]` lemmas for,
-- as the default behaviour of `@[simps]` will generate projections all the way down to components
-- of pairs.
@[simps obj map] def sectl
(C : Type uβ) [category.{vβ} C] {D : Type uβ} [category.{vβ} D] (Z : D) : C β₯€ C Γ D :=
{ obj := Ξ» X, (X, Z),
map := Ξ» X Y f, (f, π Z) }
/-- `sectr Z D` is the functor `D β₯€ C Γ D` given by `Y β¦ (Z, Y)` . -/
@[simps obj map] def sectr
{C : Type uβ} [category.{vβ} C] (Z : C) (D : Type uβ) [category.{vβ} D] : D β₯€ C Γ D :=
{ obj := Ξ» X, (Z, X),
map := Ξ» X Y f, (π Z, f) }
variables (C : Type uβ) [category.{vβ} C] (D : Type uβ) [category.{vβ} D]
/-- `fst` is the functor `(X, Y) β¦ X`. -/
@[simps obj map] def fst : C Γ D β₯€ C :=
{ obj := Ξ» X, X.1,
map := Ξ» X Y f, f.1 }
/-- `snd` is the functor `(X, Y) β¦ Y`. -/
@[simps obj map] def snd : C Γ D β₯€ D :=
{ obj := Ξ» X, X.2,
map := Ξ» X Y f, f.2 }
@[simps obj map] def swap : C Γ D β₯€ D Γ C :=
{ obj := Ξ» X, (X.2, X.1),
map := Ξ» _ _ f, (f.2, f.1) }
@[simps hom_app inv_app] def symmetry : swap C D β swap D C β
π (C Γ D) :=
{ hom := { app := Ξ» X, π X },
inv := { app := Ξ» X, π X } }
def braiding : C Γ D β D Γ C :=
equivalence.mk (swap C D) (swap D C)
(nat_iso.of_components (Ξ» X, eq_to_iso (by simp)) (by tidy))
(nat_iso.of_components (Ξ» X, eq_to_iso (by simp)) (by tidy))
instance swap_is_equivalence : is_equivalence (swap C D) :=
(by apply_instance : is_equivalence (braiding C D).functor)
end prod
section
variables (C : Type uβ) [category.{vβ} C] (D : Type uβ) [category.{vβ} D]
@[simps] def evaluation : C β₯€ (C β₯€ D) β₯€ D :=
{ obj := Ξ» X,
{ obj := Ξ» F, F.obj X,
map := Ξ» F G Ξ±, Ξ±.app X, },
map := Ξ» X Y f,
{ app := Ξ» F, F.map f,
naturality' := Ξ» F G Ξ±, eq.symm (Ξ±.naturality f) } }
@[simps obj map] def evaluation_uncurried : C Γ (C β₯€ D) β₯€ D :=
{ obj := Ξ» p, p.2.obj p.1,
map := Ξ» x y f, (x.2.map f.1) β« (f.2.app y.1),
map_comp' := Ξ» X Y Z f g,
begin
cases g, cases f, cases Z, cases Y, cases X,
simp only [prod_comp, nat_trans.comp_app, functor.map_comp, category.assoc],
rw [βnat_trans.comp_app, nat_trans.naturality, nat_trans.comp_app,
category.assoc, nat_trans.naturality],
end }
end
variables {A : Type uβ} [category.{vβ} A]
{B : Type uβ} [category.{vβ} B]
{C : Type uβ} [category.{vβ} C]
{D : Type uβ} [category.{vβ} D]
namespace functor
/-- The cartesian product of two functors. -/
@[simps obj map] def prod (F : A β₯€ B) (G : C β₯€ D) : A Γ C β₯€ B Γ D :=
{ obj := Ξ» X, (F.obj X.1, G.obj X.2),
map := Ξ» _ _ f, (F.map f.1, G.map f.2) }
/- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F Γ G`.
You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/
end functor
namespace nat_trans
/-- The cartesian product of two natural transformations. -/
@[simps app] def prod {F G : A β₯€ B} {H I : C β₯€ D} (Ξ± : F βΆ G) (Ξ² : H βΆ I) :
F.prod H βΆ G.prod I :=
{ app := Ξ» X, (Ξ±.app X.1, Ξ².app X.2),
naturality' := Ξ» X Y f,
begin
cases X, cases Y,
simp only [functor.prod_map, prod.mk.inj_iff, prod_comp],
split; rw naturality
end }
/- Again, it is inadvisable in Lean 3 to setup a notation `Ξ± Γ Ξ²`;
use instead `Ξ±.prod Ξ²` or `nat_trans.prod Ξ± Ξ²`. -/
end nat_trans
end category_theory
|
363dd4397347ed36e70481efe6a53befcc93a938 | 3c9dc4ea6cc92e02634ef557110bde9eae393338 | /src/Lean/Compiler/Util.lean | 3bf806cf3ae1f32c447aebf5dffaa3d6210f96c2 | [
"Apache-2.0"
] | permissive | shingtaklam1324/lean4 | 3d7efe0c8743a4e33d3c6f4adbe1300df2e71492 | 351285a2e8ad0cef37af05851cfabf31edfb5970 | refs/heads/master | 1,676,827,679,740 | 1,610,462,623,000 | 1,610,552,340,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,679 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Environment
namespace Lean.Compiler
def neutralExpr : Expr := mkConst `_neutral
def unreachableExpr : Expr := mkConst `_unreachable
def objectType : Expr := mkConst `_obj
def voidType : Expr := mkConst `_void
def mkLcProof (pred : Expr) := mkApp (mkConst `lcProof []) pred
namespace atMostOnce
structure AtMostOnceData where
found : Bool
result : Bool
def Visitor := AtMostOnceData β AtMostOnceData
@[inline] def seq (f g : Visitor) : Visitor := fun d =>
match f d with
| β¨found, falseβ© => β¨found, falseβ©
| other => g other
instance : AndThen Visitor := β¨seqβ©
@[inline] def skip : Visitor := id
@[inline] def visitFVar (x y : Name) : Visitor
| d@{result := false, ..} => d
| {found := false, result := true} => {found := x == y, result := true}
| {found := true, result := true} => {found := true, result := x != y}
def visit (x : Name) : Expr β Visitor
| Expr.fvar y _ => visitFVar y x
| Expr.app f a _ => visit x a >> visit x f
| Expr.lam _ d b _ => visit x d >> visit x b
| Expr.forallE _ d b _ => visit x d >> visit x b
| Expr.letE _ t v b _ => visit x t >> visit x v >> visit x b
| Expr.mdata _ e _ => visit x e
| Expr.proj _ _ e _ => visit x e
| _ => skip
end atMostOnce
open atMostOnce (visit) in
/-- Return true iff the free variable with id `x` occurs at most once in `e` -/
@[export lean_at_most_once]
def atMostOnce (e : Expr) (x : Name) : Bool :=
let {result := result, ..} := visit x e {found := false, result := true}
result
/- Helper functions for creating auxiliary names used in compiler passes. -/
@[export lean_mk_eager_lambda_lifting_name]
def mkEagerLambdaLiftingName (n : Name) (idx : Nat) : Name :=
Name.mkStr n ("_elambda_" ++ toString idx)
@[export lean_is_eager_lambda_lifting_name]
def isEagerLambdaLiftingName : Name β Bool
| Name.str p s _ => "_elambda".isPrefixOf s || isEagerLambdaLiftingName p
| Name.num p _ _ => isEagerLambdaLiftingName p
| _ => false
/-- Return the name of new definitions in the a given declaration.
Here we consider only declarations we generate code for.
We use this definition to implement `add_and_compile`. -/
@[export lean_get_decl_names_for_code_gen]
private def getDeclNamesForCodeGen : Declaration β List Name
| Declaration.defnDecl { name := n, .. } => [n]
| Declaration.mutualDefnDecl defs => defs.map fun d => d.name
| Declaration.opaqueDecl { name := n, .. } => [n]
| _ => []
def checkIsDefinition (env : Environment) (n : Name) : Except String Unit :=
match env.find? n with
| (some (ConstantInfo.defnInfo _)) => Except.ok ()
| (some (ConstantInfo.opaqueInfo _)) => Except.ok ()
| none => Except.error s!"unknow declaration '{n}'"
| _ => Except.error s!"declaration is not a definition '{n}'"
/--
We generate auxiliary unsafe definitions for regular recursive definitions.
The auxiliary unsafe definition has a clear runtime cost execution model.
This function returns the auxiliary unsafe definition name for the given name. -/
@[export lean_mk_unsafe_rec_name]
def mkUnsafeRecName (declName : Name) : Name :=
Name.mkStr declName "_unsafe_rec"
/-- Return `some _` if the given name was created using `mkUnsafeRecName` -/
@[export lean_is_unsafe_rec_name]
def isUnsafeRecName? : Name β Option Name
| Name.str n "_unsafe_rec" _ => some n
| _ => none
end Lean.Compiler
|
683148a6efa291577e0a71bc2397f08c82c0fef2 | c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447 | /src/categories/products/default.lean | 971296908647520a909095a369c9af7bb4c9b2a7 | [
"Apache-2.0"
] | permissive | rwbarton/lean-category-theory-pr | 77207b6674eeec1e258ec85dea58f3bff8d27065 | 591847d70c6a11c4d5561cd0eaf69b1fe85a70ab | refs/heads/master | 1,584,595,111,303 | 1,528,029,041,000 | 1,528,029,041,000 | 135,919,126 | 0 | 0 | null | 1,528,041,805,000 | 1,528,041,805,000 | null | UTF-8 | Lean | false | false | 6,751 | 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 ..functor_categories
open categories
open categories.functor
open categories.natural_transformation
open categories.functor_categories
namespace categories.products
universes uβ vβ uβ vβ uβ vβ uβ vβ
-- For now we only allow products of categories at the same universe level, to make type inference a bit easier. This could be generalised, but we'd have to provide specific instances at various universe levels.
section
variable (C : Type uβ)
variable [category.{uβ vβ} C]
variable (D : Type uβ)
variable [category.{uβ vβ} D]
instance ProductCategory : category.{(max uβ uβ) (max vβ vβ)} (C Γ D) :=
{ Hom := Ξ» X Y, ((X.1) βΆ (Y.1)) Γ ((X.2) βΆ (Y.2)),
identity := Ξ» X, β¨ π (X.1), π (X.2) β©,
compose := Ξ» _ _ _ f g, (f.1 β« g.1, f.2 β« g.2),
left_identity := begin
-- `obviously'` says:
intros,
automatic_induction,
dsimp,
dsimp at *,
simp
end,
right_identity := begin
-- `obviously'` says:
intros,
automatic_induction,
dsimp,
dsimp at *,
simp
end,
associativity := begin
-- `obviously'` says:
intros,
automatic_induction,
dsimp,
dsimp at *,
simp
end }
end
instance ProductCategory' (C : Type uβ) [category.{uβ vβ} C] (D : Type uβ) [category.{uβ vβ} D] : category.{uβ vβ} (C Γ D) := products.ProductCategory C D
section
variable {C : Type uβ}
variable [π : category.{uβ vβ} C]
variable {D : Type uβ}
variable [π : category.{uβ vβ} D]
include π π
@[simp] lemma ProductCategory.identity {X : C} {Y : D} : π (X, Y) = (π X, π Y) := by refl
@[simp] lemma ProductCategory.compose {P Q R : C} {S T U : D} (f : (P, S) βΆ (Q, T)) (g : (Q, T) βΆ (R, U)) : f β« g = (f.1 β« g.1, f.2 β« g.2) := by refl
end
definition RightInjectionAt (C : Type uβ) [category.{uβ vβ} C] {D : Type uβ} [category.{uβ vβ} D] (Z : D) : C β (C Γ D) :=
{ onObjects := Ξ» X, (X, Z),
onMorphisms := Ξ» X Y f, (f, π Z),
identities := begin
-- `obviously'` says:
intros,
refl
end,
functoriality := begin
-- `obviously'` says:
intros,
dsimp,
simp
end }
definition LeftInjectionAt {C : Type uβ} [category.{uβ vβ} C] (Z : C) (D : Type uβ) [category.{uβ vβ} D] : D β (C Γ D) :=
{ onObjects := Ξ» X, (Z, X),
onMorphisms := Ξ» X Y f, (π Z, f),
identities := begin
-- `obviously'` says:
intros,
refl
end,
functoriality := begin
-- `obviously'` says:
intros,
dsimp,
simp
end }
definition LeftProjection (C : Type uβ) [category.{uβ vβ} C] (Z : C) (D : Type uβ) [category.{uβ vβ} D] : (C Γ D) β C :=
{ onObjects := Ξ» X, X.1,
onMorphisms := Ξ» X Y f, f.1,
identities := begin
-- `obviously'` says:
intros,
refl
end,
functoriality := begin
-- `obviously'` says:
intros,
refl
end }
definition RightProjection (C : Type uβ) [category.{uβ vβ} C] (Z : C) (D : Type uβ) [category.{uβ vβ} D] : (C Γ D) β D :=
{ onObjects := Ξ» X, X.2,
onMorphisms := Ξ» X Y f, f.2,
identities := begin
-- `obviously'` says:
intros,
refl
end,
functoriality := begin
-- `obviously'` says:
intros,
refl
end }
section
variables {A : Type uβ}
[A_cat : category.{uβ vβ} A]
{B : Type uβ}
[B_cat : category.{uβ vβ} B]
{C : Type uβ}
[C_cat : category.{uβ vβ} C]
{D : Type uβ}
[D_cat : category.{uβ vβ} D]
include A_cat B_cat C_cat D_cat
definition ProductFunctor
(F : A β B) (G : C β D) : (A Γ C) β (B Γ D) :=
{ onObjects := Ξ» X, (F +> X.1, G +> X.2),
onMorphisms := Ξ» _ _ f, (F &> f.1, G &> f.2),
identities := begin
-- `obviously'` says (something equivalent to):
intros,
cases X,
dsimp,
erw Functor.identities_lemma,
erw Functor.identities_lemma,
refl,
end,
functoriality := begin
-- `obviously'` says (something equivalent to):
intros,
cases Z, cases Y, cases X,
dsimp,
cases f, cases g,
dsimp,
dsimp at *,
erw Functor.functoriality_lemma,
erw Functor.functoriality_lemma,
refl
end }
notation F `Γ` G := ProductFunctor F G
@[simp,ematch] lemma ProductFunctor.onObjects (F : A β B) (G : C β D) (a : A) (c : C) : (F Γ G) +> (a, c) = (F +> a, G +> c) := by refl
@[simp,ematch] lemma ProductFunctor.onMorphisms (F : A β B) (G : C β D) {a a' : A} {c c' : C} (f : (a, c) βΆ (a', c')) : (F Γ G).onMorphisms f = (F &> f.1, G &> f.2) := by refl
definition ProductNaturalTransformation
{F G : A β B} {H I : C β D} (Ξ± : F βΉ G) (Ξ² : H βΉ I) : (F Γ H) βΉ (G Γ I) :=
{ components := Ξ» X, (Ξ±.components X.1, Ξ².components X.2),
naturality := begin
-- `obviously'` says:
intros,
cases f, cases Y, cases X,
dsimp,
dsimp at *,
simp,
dsimp,
fsplit,
erw [NaturalTransformation.naturality_lemma],
erw [NaturalTransformation.naturality_lemma]
end }
notation Ξ± `Γ` Ξ² := ProductNaturalTransformation Ξ± Ξ²
end
end categories.products |
53f06edffee4b43ba7b573ba6e864adf48c26785 | 5a6ff5f8d173cbfe51967eb4c96837e3a791fe3d | /mm0-lean/mm0/set/post.lean | 38dc7a6bbc8e59b966991be1121ed9449e00bda6 | [
"CC0-1.0"
] | permissive | digama0/mm0 | 491ac09146708aa1bb775007bf3dbe339ffc0096 | 98496badaf6464e56ed7b4204e7d54b85667cb01 | refs/heads/master | 1,692,321,030,902 | 1,686,254,458,000 | 1,686,254,458,000 | 172,456,790 | 273 | 38 | CC0-1.0 | 1,689,939,563,000 | 1,551,080,059,000 | Rust | UTF-8 | Lean | false | false | 24,748 | lean | import .set6 data.int.basic data.nat.prime data.set.finite
namespace mm0
local notation x ` β‘ ` y := eq (βx : Β«classΒ») βy
local notation x ` β' `:50 y:50 := (x : Β«classΒ») β (y : Β«classΒ»)
theorem welc' {x : setvar} {A : Β«classΒ»} :
x β' A β @has_mem.mem Set (set Set) _ x A :=
Class.mem_hom_left _ _
class rel_class (A : Β«classΒ») (Ξ± : out_param Type) :=
(aset : A β V)
(to : Ξ± β Β«classΒ»)
(to_mem {} : β a, to a β A)
(to_inj {} : function.injective to)
(to_surj {} : β x, x β A β β a, x = to a)
open rel_class
theorem to_V {A Ξ±} [rel_class A Ξ±] (a) : to A a β V := elexi (to_mem _)
theorem to_inj_iff (A) {Ξ±} [rel_class A Ξ±] {a b} : to A a = to A b β a = b :=
β¨@to_inj _ _ _ _ _, congr_arg _β©
theorem to_mem_iff (A) {Ξ±} [rel_class A Ξ±] {x} : x β A β β a, x = to A a :=
β¨to_surj _, by rintro β¨a, rflβ©; exact to_mem _β©
@[elab_as_eliminator]
theorem to_ind (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β Prop)
(H : β a, P (to A a)) : β x, x β A β P x :=
Ξ» x hx, let β¨a, eβ© := to_surj x hx in e.symm βΈ H _
theorem to_ind_ral (A) {Ξ±} [rel_class A Ξ±] (P : setvar β wff)
(H : β a x, βx = to A a β P x) : wral P (Ξ» _, A) :=
Ξ» x hx, let β¨a, eβ© := to_surj x hx in H a _ e
theorem ral_iff (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β Prop) :
wral (Ξ» x, P (cv x)) (Ξ» _, A) β β a, P (to A a) :=
β¨Ξ» H a, let β¨x, e, hxβ© := @to_mem A _ _ a in e βΈ H _ (welc'.2 hx),
Ξ» H, to_ind_ral _ _ (Ξ» a x e, by rw [cv, e]; exact H _)β©
theorem rex_iff (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β Prop) :
wrex (Ξ» x, P x) (Ξ» _, A) β β a, P (to A a) :=
by classical; exact
dfrex2.trans ((notbii (ral_iff A (Ξ» x, Β¬ P x))).trans not_forall_not)
def rel (A) {Ξ±} [rel_class A Ξ±] (x) (a : Ξ±) : Prop :=
x β A β§ to A a = x
instance prod.rel_class (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] :
rel_class (cxp A B) (Ξ± Γ Ξ²) :=
{ aset := xpex (aset _) (aset _),
to := Ξ» p, cop (to A p.1) (to B p.2),
to_mem := Ξ» β¨a, bβ©, opelxp.2 β¨to_mem _, to_mem _β©,
to_inj := Ξ» β¨a, bβ© β¨c, dβ© e, prod.mk.inj_iff.2
(((opth (to_V _) (to_V _)).1 e).imp (Ξ» h, to_inj h) (Ξ» h, to_inj h)),
to_surj := Ξ» p h, begin
rcases elxp.1 h with β¨a, b, e, ha, hbβ©,
cases to_surj _ ha with x hx,
cases to_surj _ hb with y hy,
refine β¨β¨x, yβ©, e.trans _β©, rw [hx, hy]
end }
@[simp] theorem to_xp_eq (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(a b) : to (cxp A B) (a, b) = cop (to A a) (to B b) := rfl
def to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β Prop) : Β«classΒ» :=
crab (Ξ» x, β a, βx = to A a β§ P a) (Ξ» _, A)
theorem to_ab_ss (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β Prop) :
to_ab A P β A := ssrab2
theorem mem_to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β Prop) {x} :
x β to_ab A P β β a, x = to A a β§ P a :=
β¨Ξ» β¨y, e, hy, hβ©, e βΈ h, Ξ» β¨a, e, hβ©,
let β¨y, e', _β© := @to_mem A _ _ a in
β¨y, e'.trans e.symm, e'.symm βΈ to_mem a, a, e', hβ©β©
theorem to_mem_to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β Prop) {a} :
to A a β to_ab A P β P a :=
(mem_to_ab _ _).trans β¨Ξ» β¨a, e, hβ©, to_inj e.symm βΈ h, Ξ» h, β¨_, rfl, hβ©β©
def to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(P : Ξ± β Ξ² β Prop) : Β«classΒ» :=
to_ab (cxp A B) (Ξ» p, P p.1 p.2)
theorem to_opab_ss (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(P : Ξ± β Ξ² β Prop) : to_opab A B P β cxp A B := to_ab_ss _ _
theorem to_opab_rel (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(P : Ξ± β Ξ² β Prop) : wrel (to_opab A B P) :=
relss (to_opab_ss _ _ _) relxp
theorem mem_to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(P : Ξ± β Ξ² β Prop) {x} :
x β to_opab A B P β β a b, x = cop (to A a) (to B b) β§ P a b :=
(mem_to_ab _ _).trans $ by simp [eq_comm]
theorem to_br_to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(P : Ξ± β Ξ² β Prop) {a b} :
wbr (to A a) (to B b) (to_opab A B P) β P a b :=
@to_mem_to_ab (cxp A B) _ _ _ (a, b)
def to_mpt (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (f : Ξ± β Ξ²) : Β«classΒ» :=
to_opab A B (Ξ» a b, f a = b)
theorem to_mpt_fn (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(f : Ξ± β Ξ²) : wfn (to_mpt A B f) A :=
begin
refine β¨dffun4.2 β¨to_opab_rel _ _ _, _β©,
eqssi (sstri (dmss (to_opab_ss _ _ _)) dmxpss) _β©,
{ rintro x y z β¨hβ, hββ©,
rcases (mem_to_opab _ _ _).1 hβ with β¨a, _, eβ, rflβ©,
rcases (mem_to_opab _ _ _).1 hβ with β¨b, _, eβ, rflβ©,
cases (opth (vex _) (vex _)).1 eβ with lβ rβ,
cases (opth (vex _) (vex _)).1 eβ with lβ rβ,
cases to_inj (lβ.symm.trans lβ),
exact rβ.trans rβ.symm },
{ refine Ξ» x h, welc'.1 $ to_ind A _ (Ξ» a, _) x (welc'.2 h),
rcases to_mem (f a) with β¨b, e, hβ©,
refine breldm (to_V _) (vex b) _,
rw [cv, e], exact (to_br_to_opab _ _ _).2 rfl }
end
@[simp] theorem to_mpt_fv (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(f : Ξ± β Ξ²) (a) : cfv (to A a) (to_mpt A B f) = to B (f a) :=
(fnbrfvb β¨to_mpt_fn _ _ _, to_mem _β©).2 $ (to_br_to_opab _ _ _).2 rfl
theorem to_mpt_f (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²]
(f : Ξ± β Ξ²) : wf A B (to_mpt A B f) :=
ffnfv.2 β¨to_mpt_fn _ _ _, to_ind_ral A _ $ Ξ» a x e,
by rw [cv, e, to_mpt_fv A B f a]; apply to_memβ©
def carrow (A B) := co B A cmap
local infix ` cβ `:25 := carrow
theorem map.rel_class (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] :
rel_class (A cβ B) (Ξ± β Ξ²) :=
{ aset := ovex,
to := to_mpt A B,
to_mem := Ξ» f, (elmap (aset _) (aset _)).2 (to_mpt_f _ _ f),
to_inj := Ξ» f g e, funext $ Ξ» a, begin
have := to_mpt_fv A B g a,
rw [β e, to_mpt_fv A B f a] at this,
exact to_inj this
end,
to_surj := Ξ» p hp, begin
have fp := (elmap (aset _) (aset _)).1 hp,
have := Ξ» a, to_surj (cfv (to A a) p) (ffvelrni fp (to_mem a)),
choose f hf using this, use f,
refine (eqfnfv β¨ffn fp, to_mpt_fn _ _ _β©).2 (to_ind_ral _ _ _),
intros a x e, rw [cv, e, to_mpt_fv A B f a], apply hf
end }
instance : has_zero Β«classΒ» := β¨cc0β©
instance : has_one Β«classΒ» := β¨c1β©
instance : has_add Β«classΒ» := β¨Ξ» x y, co x y caddcβ©
instance : has_mul Β«classΒ» := β¨Ξ» x y, co x y cmulβ©
instance : has_neg Β«classΒ» := β¨cnegβ©
instance : has_sub Β«classΒ» := β¨Ξ» x y, co x y cminβ©
instance : has_div Β«classΒ» := β¨Ξ» x y, co x y cdivβ©
instance : has_dvd Β«classΒ» := β¨Ξ» x y, wbr x y cdividesβ©
instance : has_lt Β«classΒ» := β¨Ξ» x y, wbr x y cltβ©
instance : has_le Β«classΒ» := β¨Ξ» x y, wbr x y cleβ©
instance : has_equiv Β«classΒ» := β¨Ξ» x y, wbr x y cenβ©
@[simp] theorem c0_eq : cc0 = 0 := rfl
@[simp] theorem c1_eq : c1 = 1 := rfl
@[simp] theorem add_eq (x y) : co x y caddc = x + y := rfl
@[simp] theorem mul_eq (x y) : co x y cmul = x * y := rfl
@[simp] theorem neg_eq (x) : cneg x = -x := rfl
@[simp] theorem sub_eq (x y) : co x y cmin = x - y := rfl
@[simp] theorem div_eq (x y) : co x y cdiv = x / y := rfl
@[simp] theorem dvd_eq (x y) : wbr x y cdivides = (x β£ y) := rfl
@[simp] theorem lt_eq (x y) : wbr x y clt = (x < y) := rfl
@[simp] theorem le_eq (x y) : wbr x y cle = (x β€ y) := rfl
@[simp] theorem en_eq (x y) : wbr x y cen = (x β y) := rfl
local notation `cββ` := cn0
local notation `cβ€` := cz
local notation `cβ` := cr
local notation `cβ` := cc
instance : has_coe_to_sort Β«classΒ» := β¨_, Ξ» A, {x // x β A}β©
def semiring_cn (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(mulcl : β x y, x β A β y β A β x * y β A) : comm_semiring A :=
have h : β x : A, x.1 β cβ := Ξ» x, sselii ss x.2,
{ add := Ξ» x y, β¨x.1 + y.1, addcl _ _ x.2 y.2β©,
add_assoc := Ξ» x y z, subtype.eq (addassi (h x) (h y) (h z)),
mul := Ξ» x y, β¨x.1 * y.1, mulcl _ _ x.2 y.2β©,
mul_assoc := Ξ» x y z, subtype.eq (mulassi (h x) (h y) (h z)),
zero := β¨0, Β«0clΒ»β©,
zero_add := Ξ» x, subtype.eq (addid2 (h x)),
add_zero := Ξ» x, subtype.eq (addid1 (h x)),
add_comm := Ξ» x y, subtype.eq (addcomi (h x) (h y)),
mul := Ξ» x y, β¨x.1 * y.1, mulcl _ _ x.2 y.2β©,
mul_assoc := Ξ» x y z, subtype.eq (mulassi (h x) (h y) (h z)),
one := β¨1, Β«1clΒ»β©,
one_mul := Ξ» x, subtype.eq (mulid2 (h x)),
mul_one := Ξ» x, subtype.eq (mulid1 (h x)),
mul_comm := Ξ» x y, subtype.eq (mulcomi (h x) (h y)),
left_distrib := Ξ» x y z, subtype.eq (adddii (h x) (h y) (h z)),
right_distrib := Ξ» x y z, subtype.eq (adddiri (h x) (h y) (h z)),
zero_mul := Ξ» x, subtype.eq (mul02 (h x)),
mul_zero := Ξ» x, subtype.eq (mul01 (h x)) }
def linear_order_xr (A : Β«classΒ») (ss : A β cxr) :
linear_order A :=
have h : β x:A, x.1 β cxr := Ξ» x, sselii ss x.2,
{ le := Ξ» x y, x.1 β€ y.1,
lt := Ξ» x y, x.1 < y.1,
le_refl := Ξ» x, xrleid (h x),
le_trans := Ξ» x y z h1 h2, xrletr β¨h x, h y, h zβ© β¨h1, h2β©,
le_antisymm := Ξ» x y h1 h2, subtype.eq ((xrletri3 β¨h x, h yβ©).2 β¨h1, h2β©),
le_total := Ξ» x y, xrletri β¨h x, h yβ©,
lt_iff_le_not_le := Ξ» x y, β¨
Ξ» h1, β¨xrltle β¨h x, h yβ© h1, (xrltnle β¨h x, h yβ©).1 h1β©,
Ξ» β¨h1, h2β©, (xrltnle β¨h x, h yβ©).2 h2β© }
noncomputable def ordered_semiring_re (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(mulcl : β x y, x β A β y β A β x * y β A) :
decidable_linear_ordered_semiring A :=
have h : β x:A, x.1 β cβ := Ξ» x, sselii ss x.2,
{ add_left_cancel := Ξ» x y z e, subtype.eq
((readdcan β¨h y, h z, h xβ©).1 (subtype.ext.1 e)),
add_right_cancel := Ξ» x y z e, subtype.eq
((addcan2 β¨recn (h x), recn (h z), recn (h y)β©).1
(subtype.ext.1 e)),
add_le_add_left := Ξ» x y h1 z, (leadd2 β¨h x, h y, h zβ©).1 h1,
le_of_add_le_add_left := Ξ» x y z h1, (leadd2 β¨h y, h z, h xβ©).2 h1,
mul_le_mul_of_nonneg_left := Ξ» x y z h1 h2, lemul2a β¨β¨h x, h y, h z, h2β©, h1β©,
mul_le_mul_of_nonneg_right := Ξ» x y z h1 h2, lemul1a β¨β¨h x, h y, h z, h2β©, h1β©,
mul_lt_mul_of_pos_left := Ξ» x y z h1 h2, (ltmul2 β¨h x, h y, h z, h2β©).1 h1,
mul_lt_mul_of_pos_right := Ξ» x y z h1 h2, (ltmul1 β¨h x, h y, h z, h2β©).1 h1,
zero_lt_one := Β«0lt1Β»,
decidable_le := by classical; apply_instance,
..linear_order_xr _ (sstri ss ressxr),
..semiring_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl mulcl }
def ring_cn (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(negcl : β x, x β A β -x β A)
(mulcl : β x y, x β A β y β A β x * y β A) : comm_ring A :=
have h : β x : A, x.1 β cβ := Ξ» x, sselii ss x.2,
{ neg := Ξ» x, β¨-x.1, negcl _ x.2β©,
add_left_neg := Ξ» x, subtype.eq $
(addcomi (mm0.negcl (h x)) (h x)).trans (negid (h x)),
..semiring_cn _ ss Β«0clΒ» Β«1clΒ» addcl mulcl }
noncomputable def ordered_ring_re (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(negcl : β x, x β A β -x β A)
(mulcl : β x y, x β A β y β A β x * y β A) :
decidable_linear_ordered_comm_ring A :=
have h : β x : A, x.1 β cβ := Ξ» x, sselii ss x.2,
{ add_lt_add_left := Ξ» x y h1 z, (ltadd2 β¨h x, h y, h zβ©).1 h1,
zero_ne_one := mt subtype.ext.1 Β«0ne1Β»,
mul_nonneg := Ξ» x y h1 h2, mulge0 β¨β¨h x, h1β©, β¨h y, h2β©β©,
mul_pos := Ξ» x y h1 h2, mulgt0 β¨β¨h x, h1β©, β¨h y, h2β©β©,
..ring_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl negcl mulcl,
..ordered_semiring_re _ ss Β«0clΒ» Β«1clΒ» addcl mulcl }
noncomputable def field_cn (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(negcl : β x, x β A β -x β A)
(mulcl : β x y, x β A β y β A β x * y β A)
(reccl : β x:Β«classΒ», x β A β x β 0 β 1 / x β A) : discrete_field A :=
begin
classical,
letI := ring_cn _ ss Β«0clΒ» Β«1clΒ» addcl negcl mulcl,
have h : β {{x}}, x β A β x β cβ := Ξ» x, sselii ss,
let inv := Ξ» x : A,
if h : x.1 = 0 then (0:A) else β¨1 / x.1, reccl _ x.2 hβ©,
have n0 : β {x:A}, x β 0 β x.1 β 0 := Ξ» x h, (mt subtype.eq h:_),
have inveq : β {x:A}, x β 0 β (inv x).1 = 1 / x.1 :=
Ξ» x h, by simp [inv, n0 h],
exact
{ inv := inv,
zero_ne_one := mt subtype.ext.1 (necomi ax_1ne0),
mul_inv_cancel := Ξ» x x0, subtype.eq (show x.1 * _ = 1,
by rw [inveq x0]; exact recid β¨h x.2, n0 x0β©),
inv_mul_cancel := Ξ» x x0, subtype.eq (show _ * x.1 = 1,
by rw [inveq x0]; exact recid2 β¨h x.2, n0 x0β©),
inv_zero := dif_pos rfl,
has_decidable_eq := by apply_instance,
..βΉcomm_ring AβΊ }
end
noncomputable def ordered_field_re (A : Β«classΒ») (ss : A β cβ)
(Β«0clΒ» : (0 : Β«classΒ») β A) (Β«1clΒ» : (1 : Β«classΒ») β A)
(addcl : β x y, x β A β y β A β x + y β A)
(negcl : β x, x β A β -x β A)
(mulcl : β x y, x β A β y β A β x * y β A)
(reccl : β x:Β«classΒ», x β A β x β 0 β 1 / x β A) :
discrete_linear_ordered_field A :=
{ ..field_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl negcl mulcl reccl,
..ordered_ring_re _ ss Β«0clΒ» Β«1clΒ» addcl negcl mulcl }
noncomputable instance nn0.ordered_semiring : decidable_linear_ordered_semiring cββ :=
ordered_semiring_re _ nn0ssre Β«0nn0Β» Β«1nn0Β» (Ξ» x y, nn0addcli) (Ξ» x y, nn0mulcli)
@[simp] theorem cn0_zero : (0 : cββ).1 = 0 := rfl
@[simp] theorem cn0_one : (1 : cββ).1 = 1 := rfl
@[simp] theorem cn0_add (x y : cββ) : (x + y).1 = x.1 + y.1 := rfl
@[simp] theorem cn0_mul (x y : cββ) : (x * y).1 = x.1 * y.1 := rfl
@[simp] theorem cn0_le {x y : cββ} : x.1 β€ y.1 β x β€ y := iff.rfl
@[simp] theorem cn0_lt {x y : cββ} : x.1 < y.1 β x < y := iff.rfl
noncomputable instance z.ordered_ring : decidable_linear_ordered_comm_ring cβ€ :=
ordered_ring_re _ zssre Β«0zΒ» Β«1zΒ»
(Ξ» x y h1 h2, zaddcl β¨h1, h2β©) (Ξ» x, znegcl)
(Ξ» x y h1 h2, zmulcl β¨h1, h2β©)
@[simp] theorem cz_zero : (0 : cβ€).1 = 0 := rfl
@[simp] theorem cz_one : (1 : cβ€).1 = 1 := rfl
@[simp] theorem cz_add (x y : cβ€) : (x + y).1 = x.1 + y.1 := rfl
@[simp] theorem cz_neg (x : cβ€) : (-x).1 = -x.1 := rfl
@[simp] theorem cz_sub (x y : cβ€) : (x - y).1 = x.1 - y.1 := negsub β¨zcn x.2, zcn y.2β©
@[simp] theorem cz_mul (x y : cβ€) : (x * y).1 = x.1 * y.1 := rfl
@[simp] theorem cz_le {x y : cβ€} : x.1 β€ y.1 β x β€ y := iff.rfl
@[simp] theorem cz_lt {x y : cβ€} : x.1 < y.1 β x < y := iff.rfl
noncomputable instance re.ordered_field : discrete_linear_ordered_field cβ :=
ordered_field_re _ ssid Β«0reΒ» Β«1reΒ»
(Ξ» x y, readdcli) (Ξ» x, renegcl)
(Ξ» x y, remulcli) (Ξ» x, rereccli)
@[simp] theorem cr_zero : (0 : cβ).1 = 0 := rfl
@[simp] theorem cr_one : (1 : cβ).1 = 1 := rfl
@[simp] theorem cr_add (x y : cβ) : (x + y).1 = x.1 + y.1 := rfl
@[simp] theorem cr_neg (x : cβ) : (-x).1 = -x.1 := rfl
@[simp] theorem cr_sub (x y : cβ) : (x - y).1 = x.1 - y.1 := negsub β¨recn x.2, recn y.2β©
@[simp] theorem cr_mul (x y : cβ) : (x * y).1 = x.1 * y.1 := rfl
@[simp] theorem cr_le {x y : cβ} : x.1 β€ y.1 β x β€ y := iff.rfl
@[simp] theorem cr_lt {x y : cβ} : x.1 < y.1 β x < y := iff.rfl
noncomputable instance cn.field : discrete_field cβ :=
field_cn _ ssid Β«0cnΒ» Β«ax_1cnΒ»
(Ξ» x y, addcli) (Ξ» x, negcl)
(Ξ» x y, mulcli) (Ξ» x, reccli)
@[simp] theorem cc_zero : (0 : cβ).1 = 0 := rfl
@[simp] theorem cc_one : (1 : cβ).1 = 1 := rfl
@[simp] theorem cc_add (x y : cβ) : (x + y).1 = x.1 + y.1 := rfl
@[simp] theorem cc_neg (x : cβ) : (-x).1 = -x.1 := rfl
@[simp] theorem cc_sub (x y : cβ) : (x - y).1 = x.1 - y.1 := negsub β¨x.2, y.2β©
@[simp] theorem cc_mul (x y : cβ) : (x * y).1 = x.1 * y.1 := rfl
theorem wb_congr (P : Β«classΒ» β Prop) {x:setvar} {A : Β«classΒ»} :
βx = A β (P x β P A) := Ξ» h, by rw h
@[elab_as_eliminator]
theorem nn0ind' (P : Β«classΒ» β Prop) {n} (h : n β cββ)
(H0 : P 0) (H1 : β x β cββ, P x β P (x + 1)) : P n :=
@nn0ind _ _ _ _ _ (Ξ» _, n)
(Ξ» _ _, wb_congr P) (Ξ» _ _, wb_congr P)
(Ξ» _ _, wb_congr P) (Ξ» _ _, wb_congr P) (Ξ» _, H0) (Ξ» x, H1 _) β
h
instance nn0.rel_class : rel_class cββ β :=
{ aset := nn0ex,
to := Ξ» n, (n:cββ).1,
to_mem := Ξ» n, (n:cββ).2,
to_inj := Ξ» m n e, nat.cast_inj.1 (subtype.eq e),
to_surj := Ξ» x h, begin
refine nn0ind' _ h β¨0, rflβ© _,
rintro _ h β¨n, rflβ©, exact β¨n+1, rflβ©
end }
@[simp] theorem to_cn0_val (x : β) : to cββ x = (x : cββ).1 := rfl
theorem to_cn0_mem {x : Β«classΒ»} : x β cββ β β n:β, x = (n : cββ).1 :=
to_mem_iff cββ
@[simp] theorem to_cn0_zero : ((0 : β) : cββ).1 = 0 := rfl
@[simp] theorem to_cn0_one : ((1 : β) : cββ).1 = 1 := by simp
@[simp] theorem to_cn0_add (x y : β) : ((x + y:β):cββ).1 = (x:cββ).1 + (y:cββ).1 := by simp
@[simp] theorem to_cn0_mul (x y : β) : ((x * y:β):cββ).1 = (x:cββ).1 * (y:cββ).1 := by simp
@[simp] theorem to_cn0_le {x y : β} : (x:cββ).1 β€ (y:cββ).1 β x β€ y := by simp
@[simp] theorem to_cn0_lt {x y : β} : (x:cββ).1 < (y:cββ).1 β x < y := by simp
theorem to_cn0_two : ((2 : β) : cββ).1 = c2 := oveq1 to_cn0_one
@[simp] theorem to_cn0_eq {x y : β} : (x:cββ).1 = (y:cββ).1 β x = y :=
to_inj_iff cββ
@[simp] theorem nat_cast_cz : β n : β, (n : cβ€).1 = (n : cββ).1
| 0 := rfl
| (n+1) := oveq1 (nat_cast_cz n)
@[simp] theorem nat_cast_cz' (n : β) : ((n : β€) : cβ€).1 = (n : cββ).1 :=
nat_cast_cz _
instance z.rel_class : rel_class cβ€ β€ :=
{ aset := zex,
to := Ξ» n, (n:cβ€).1,
to_mem := Ξ» n, (n:cβ€).2,
to_inj := Ξ» m n e, int.cast_inj.1 (subtype.eq e),
to_surj := Ξ» x h, begin
rcases elznn0.1 h with β¨xre, h | hβ©,
{ cases to_surj _ h with n e, refine β¨n, _β©,
exact e.trans (nat_cast_cz' n).symm },
{ cases to_surj _ h with n e, use -n,
rw [int.cast_neg n], show x = -((n:β€):cβ€).1,
rw nat_cast_cz',
exact (negneg (recn xre)).symm.trans (negeq e) }
end }
@[simp] theorem to_cz_val (x : β€) : to cβ€ x = (x : cβ€).1 := rfl
theorem to_cz_mem {x : Β«classΒ»} : x β cβ€ β β n:β€, x = (n : cβ€).1 := to_mem_iff cβ€
@[simp] theorem to_cz_eq {x y : β€} : (x:cβ€).1 = (y:cβ€).1 β x = y := to_inj_iff cβ€
@[simp] theorem to_cz_zero : ((0 : β€) : cβ€).1 = 0 := rfl
@[simp] theorem to_cz_one : ((1 : β€) : cβ€).1 = 1 := by simp
@[simp] theorem to_cz_add (x y : β€) : ((x + y:β€):cβ€).1 = (x:cβ€).1 + (y:cβ€).1 := by simp
@[simp] theorem to_cz_neg (x : β€) : ((-x:β€):cβ€).1 = -(x:cβ€).1 := by simp
@[simp] theorem to_cz_sub (x y : β€) : ((x - y:β€):cβ€).1 = (x:cβ€).1 - (y:cβ€).1 := by rw [int.cast_sub, cz_sub]
@[simp] theorem to_cz_mul (x y : β€) : ((x * y:β€):cβ€).1 = (x:cβ€).1 * (y:cβ€).1 := by simp
@[simp] theorem to_cz_le {x y : β€} : (x:cβ€).1 β€ (y:cβ€).1 β x β€ y := by simp
@[simp] theorem to_cz_lt {x y : β€} : (x:cβ€).1 < (y:cβ€).1 β x < y := by simp
theorem to_cz_two : ((2 : β€) : cβ€).1 = c2 := oveq1 to_cz_one
@[simp] theorem to_cz_dvd {x y : β€} : (x:cβ€).1 β£ (y:cβ€).1 β x β£ y :=
(divides β¨(x:cβ€).2, (y:cβ€).2β©).trans $
(rex_iff cβ€ (Ξ» z, z * (x:cβ€).1 = (y:cβ€).1)).trans $
exists_congr $ Ξ» z,
by rw [β to_inj_iff cβ€, eq_comm, mul_comm]; simp
@[simp] theorem to_cn0_dvd {x y : β} : (x:cββ).1 β£ (y:cββ).1 β x β£ y :=
by rw [β int.coe_nat_dvd, β to_cz_dvd, nat_cast_cz', nat_cast_cz']
def cz_gcd (a b : cβ€) : cββ := β¨co a b cgcd, gcdcl β¨a.2, b.2β©β©
theorem cz_gcd_eq (a b : β€) : cz_gcd a b = int.gcd a b :=
begin
refine subtype.eq (dvdseq β¨β¨(_:cββ).2, (_:cββ).2β©,
β¨_, _β©β©),
{ cases @to_surj cββ _ _ _ (cz_gcd a b).2 with x hx,
replace hx := subtype.eq hx, rw hx,
cases gcddvds β¨(a:cβ€).2, (b:cβ€).2β© with h1 h2,
refine to_cn0_dvd.2 (nat.dvd_gcd _ _);
rw [β int.coe_nat_dvd, int.dvd_nat_abs, β to_cz_dvd,
nat_cast_cz', β hx],
exacts [h1, h2] },
{ rw β nat_cast_cz',
refine dvdsgcd β¨(_:cβ€).2, (_:cβ€).2, (_:cβ€).2β© β¨_, _β©;
refine (@to_cz_dvd (int.gcd a b) _).2
(int.dvd_nat_abs.1 (int.coe_nat_dvd.2 _));
[apply nat.gcd_dvd_left, apply nat.gcd_dvd_right] }
end
theorem mem_cn {n : β} : (n:cββ).1 β cn β n β 0 :=
(baib (elnnne0) (_:cββ).2).trans $ not_congr $ @to_inj_iff cββ _ _ n 0
theorem mem_cprime {p : β} : (p:cββ).1 β cprime β nat.prime p :=
begin
have ge2 : (p:cββ).1 β cfv c2 cuz β 2 β€ p,
{ refine (eluz β¨Β«2zΒ», nn0z (_:cββ).2β©).trans _,
rw β to_cn0_two, exact @nat.cast_le cββ _ 2 p },
refine β¨Ξ» h, _, Ξ» h, _β©,
{ refine β¨ge2.1 (prmuz2 h), Ξ» m hm, _β©,
cases (dvdsprime β¨h, mem_cn.2 _β©).1 (to_cn0_dvd.2 hm) with e e,
{ exact or.inr (to_cn0_eq.1 e) },
{ rw [c1_eq, β cn0_one, β nat.cast_one] at e,
exact or.inl (to_cn0_eq.1 e) },
{ rintro rfl, cases zero_dvd_iff.1 hm,
exact Β«0nnnΒ» (prmnn h) } },
{ refine isprm2.2 β¨ge2.2 h.1, Ξ» z hz, _β©,
cases to_cn0_mem.1 (nnnn0 hz) with m hm, simp [hm],
rw [β to_cn0_one, to_cn0_eq],
exact h.2 _ }
end
theorem is_finite (A) {Ξ±} [rel_class A Ξ±] (s : set Ξ±) :
set.finite s β to_ab A (Ξ» a, a β s) β cfn :=
begin
have ab0 : to_ab A (Ξ» (a : Ξ±), a β (β
: set Ξ±)) = β
,
{ apply eq0.2, rintro x β¨_, _, _, _, _, β¨β©β© },
have absuc : β (a : Ξ±) (s : set Ξ±),
to_ab A (Ξ» (a_1 : Ξ±), a_1 β insert a s) =
to_ab A (Ξ» (a : Ξ±), a β s) βͺ csn (to A a),
{ refine Ξ» a s, eqriv (Ξ» x, _),
refine pm5_21nii (sseli (to_ab_ss _ _))
(sseli (unssi (to_ab_ss _ _) (snssi (to_mem _)))) (Ξ» h, _),
rcases to_surj _ h with β¨b, eβ©, rw [e], simp [to_mem_to_ab],
exact (elun.trans (or.comm.trans $ or_congr
((elsnc (to_V _)).trans (to_inj_iff _))
(to_mem_to_ab _ _))).symm },
split,
{ refine Ξ» h, set.finite.induction_on h _ (Ξ» a s hn h IH, _),
{ rw ab0, exact Β«0finΒ» },
{ rw absuc, exact unfi β¨IH, snfiβ© } },
{ let P := Ξ» x, x β A β β s:set Ξ±,
set.finite s β§ to_ab A (Ξ» (a : Ξ±), a β s) = x,
have := findcard2 (Ξ» _ _ _, wb_congr P) (Ξ» _ _ _, wb_congr P)
(Ξ» _ _ _, wb_congr P) (Ξ» _ _ _, wb_congr P)
(Ξ» _ _, (_ : P β
)) (Ξ» x y _ IH, _) β
β
,
{ intro h,
rcases this h (to_ab_ss A (Ξ» (a : Ξ±), a β s)) with β¨t, tf, eβ©,
have : t = s := set.ext (Ξ» a,
(to_mem_to_ab A _).symm.trans ((eleq2 e).trans (to_mem_to_ab A _))),
exact this βΈ tf },
{ refine Ξ» _, β¨β
, set.finite_empty, ab0β© },
{ intro h, cases unss.2 h with h1 h2,
rcases IH h1 with β¨s, sf, eβ©,
cases to_surj _ ((snss (vex _)).2 h2) with a e',
rw [e.symm, e'],
exact β¨_, set.finite_insert a sf, absuc _ _β© } }
end
theorem dirith_auxβ {n a : Β«classΒ»} :
n β cn β§ a β cβ€ β§ co a n cgcd = 1 β
cab (Ξ» x, βx β cprime β§ n β£ x - a) β cn :=
dirith
theorem dirith_auxβ {n : β} {a : β€} (n0 : n β 0) (h : int.gcd a n = 1) :
to_ab cββ (Ξ» x, nat.prime x β§ βn β£ βx - a) β cn :=
begin
refine eqbrtri (eqriv $ Ξ» x, _) (dirith_auxβ β¨mem_cn.2 n0, (a:cβ€).2, _β©),
{ refine (mem_to_ab _ _).trans (iff.trans _ (abid _).symm),
dsimp, split,
{ rintro β¨n, e, hβ, hββ©, simp [e],
refine β¨mem_cprime.2 hβ, _β©,
rwa [β nat_cast_cz', β nat_cast_cz', β to_cz_sub, to_cz_dvd] },
{ rintro β¨hβ, hββ©,
cases to_cn0_mem.1 (nnnn0 (prmnn hβ)) with m hm,
simp [hm] at hβ hβ β’,
rw [β nat_cast_cz', β nat_cast_cz', β to_cz_sub, to_cz_dvd] at hβ,
exact β¨mem_cprime.1 hβ, hββ© } },
{ rw [β nat_cast_cz'],
refine congr_arg subtype.val (_ : cz_gcd a n = 1),
rw [β int.cast_coe_nat, cz_gcd_eq, h, nat.cast_one] }
end
theorem dirith' {n : β} {a : β€} (n0 : n β 0) (g1 : int.gcd a n = 1) :
Β¬ set.finite {x | nat.prime x β§ βn β£ βx - a} :=
Ξ» h, ominf $ (enfi nnenom).1 $
(enfi (dirith_auxβ n0 g1)).1 $ (is_finite cββ _).1 h
end mm0
|
5cd89f9ae6861246026b074fd20ed244624b010d | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/587.lean | 8442145a2c72acdd64698f009e1873edd5366fa4 | [
"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 | 300 | lean | open quot setoid
variables A B : Typeβ
variable s : setoid A
include s
variable f : A β B
variable c : β aβ aβ, aβ β aβ β f aβ = f aβ
example (a : A) (g h : B β B) : g = h β g (quot.lift_on β¦aβ§ f c) = h (f a) :=
begin
intro gh,
esimp,
state,
rewrite gh
end
|
5d72b9a80b2c5092e1ba67baf3adb169820b5550 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/export.lean | ffbe4603232aece4243f8065efb379058e390f44 | [
"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 | 132 | lean | constants a b : nat
namespace boo
export nat (rec add)
check a + b
check nat.add
end boo
open boo
check a + b
check nat.rec
|
3128b1ad8765f20fbc996d61c95bb97da80329b8 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/ring_theory/polynomial/scale_roots.lean | 101465abaa35bccf468ec15ea398516548919c36 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 5,176 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Devon Tuma
-/
import ring_theory.non_zero_divisors
import data.polynomial.algebra_map
/-!
# Scaling the roots of a polynomial
This file defines `scale_roots p s` for a polynomial `p` in one variable and a ring element `s` to
be the polynomial with root `r * s` for each root `r` of `p` and proves some basic results about it.
-/
variables {A K R S : Type*} [comm_ring A] [is_domain A] [field K] [comm_ring R] [comm_ring S]
variables {M : submonoid A}
namespace polynomial
open_locale big_operators polynomial
/-- `scale_roots p s` is a polynomial with root `r * s` for each root `r` of `p`. -/
noncomputable def scale_roots (p : R[X]) (s : R) : R[X] :=
β i in p.support, monomial i (p.coeff i * s ^ (p.nat_degree - i))
@[simp] lemma coeff_scale_roots (p : R[X]) (s : R) (i : β) :
(scale_roots p s).coeff i = coeff p i * s ^ (p.nat_degree - i) :=
by simp [scale_roots, coeff_monomial] {contextual := tt}
lemma coeff_scale_roots_nat_degree (p : R[X]) (s : R) :
(scale_roots p s).coeff p.nat_degree = p.leading_coeff :=
by rw [leading_coeff, coeff_scale_roots, tsub_self, pow_zero, mul_one]
@[simp] lemma zero_scale_roots (s : R) : scale_roots 0 s = 0 := by { ext, simp }
lemma scale_roots_ne_zero {p : R[X]} (hp : p β 0) (s : R) :
scale_roots p s β 0 :=
begin
intro h,
have : p.coeff p.nat_degree β 0 := mt leading_coeff_eq_zero.mp hp,
have : (scale_roots p s).coeff p.nat_degree = 0 :=
congr_fun (congr_arg (coeff : R[X] β β β R) h) p.nat_degree,
rw [coeff_scale_roots_nat_degree] at this,
contradiction
end
lemma support_scale_roots_le (p : R[X]) (s : R) :
(scale_roots p s).support β€ p.support :=
by { intro, simpa using left_ne_zero_of_mul }
lemma support_scale_roots_eq (p : R[X]) {s : R} (hs : s β non_zero_divisors R) :
(scale_roots p s).support = p.support :=
le_antisymm (support_scale_roots_le p s)
begin
intro i,
simp only [coeff_scale_roots, polynomial.mem_support_iff],
intros p_ne_zero ps_zero,
have := pow_mem hs (p.nat_degree - i) _ ps_zero,
contradiction
end
@[simp] lemma degree_scale_roots (p : R[X]) {s : R} :
degree (scale_roots p s) = degree p :=
begin
haveI := classical.prop_decidable,
by_cases hp : p = 0,
{ rw [hp, zero_scale_roots] },
have := scale_roots_ne_zero hp s,
refine le_antisymm (finset.sup_mono (support_scale_roots_le p s)) (degree_le_degree _),
rw coeff_scale_roots_nat_degree,
intro h,
have := leading_coeff_eq_zero.mp h,
contradiction,
end
@[simp] lemma nat_degree_scale_roots (p : R[X]) (s : R) :
nat_degree (scale_roots p s) = nat_degree p :=
by simp only [nat_degree, degree_scale_roots]
lemma monic_scale_roots_iff {p : R[X]} (s : R) :
monic (scale_roots p s) β monic p :=
by simp only [monic, leading_coeff, nat_degree_scale_roots, coeff_scale_roots_nat_degree]
lemma scale_roots_evalβ_eq_zero {p : S[X]} (f : S β+* R)
{r : R} {s : S} (hr : evalβ f r p = 0) :
evalβ f (f s * r) (scale_roots p s) = 0 :=
calc evalβ f (f s * r) (scale_roots p s) =
(scale_roots p s).support.sum (Ξ» i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) :
by simp [evalβ_eq_sum, sum_def]
... = p.support.sum (Ξ» i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) :
finset.sum_subset (support_scale_roots_le p s)
(Ξ» i hi hi', let this : coeff p i * s ^ (p.nat_degree - i) = 0 :=
by simpa using hi' in by simp [this])
... = p.support.sum (Ξ» (i : β), f (p.coeff i) * f s ^ (p.nat_degree - i + i) * r ^ i) :
finset.sum_congr rfl
(Ξ» i hi, by simp_rw [f.map_mul, f.map_pow, pow_add, mul_pow, mul_assoc])
... = p.support.sum (Ξ» (i : β), f s ^ p.nat_degree * (f (p.coeff i) * r ^ i)) :
finset.sum_congr rfl
(Ξ» i hi, by { rw [mul_assoc, mul_left_comm, tsub_add_cancel_of_le],
exact le_nat_degree_of_ne_zero (polynomial.mem_support_iff.mp hi) })
... = f s ^ p.nat_degree * p.support.sum (Ξ» (i : β), (f (p.coeff i) * r ^ i)) : finset.mul_sum.symm
... = f s ^ p.nat_degree * evalβ f r p : by { simp [evalβ_eq_sum, sum_def] }
... = 0 : by rw [hr, _root_.mul_zero]
lemma scale_roots_aeval_eq_zero [algebra S R] {p : S[X]}
{r : R} {s : S} (hr : aeval r p = 0) :
aeval (algebra_map S R s * r) (scale_roots p s) = 0 :=
scale_roots_evalβ_eq_zero (algebra_map S R) hr
lemma scale_roots_evalβ_eq_zero_of_evalβ_div_eq_zero
{p : A[X]} {f : A β+* K} (hf : function.injective f)
{r s : A} (hr : evalβ f (f r / f s) p = 0) (hs : s β non_zero_divisors A) :
evalβ f (f r) (scale_roots p s) = 0 :=
begin
convert scale_roots_evalβ_eq_zero f hr,
rw [βmul_div_assoc, mul_comm, mul_div_cancel],
exact map_ne_zero_of_mem_non_zero_divisors _ hf hs
end
lemma scale_roots_aeval_eq_zero_of_aeval_div_eq_zero [algebra A K]
(inj : function.injective (algebra_map A K)) {p : A[X]} {r s : A}
(hr : aeval (algebra_map A K r / algebra_map A K s) p = 0) (hs : s β non_zero_divisors A) :
aeval (algebra_map A K r) (scale_roots p s) = 0 :=
scale_roots_evalβ_eq_zero_of_evalβ_div_eq_zero inj hr hs
end polynomial
|
03ed60be71162ddf5229e2ee16b4cdb4d26622a2 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/tactic/default.lean | 3ebbb27ebc4028a98ebe3f609d20dffd7d84b5f8 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,221 | lean | /-
This file imports many useful tactics ("the kitchen sink").
You can use `import tactic` at the beginning of your file to get everything.
(Although you may want to strip things down when you're polishing.)
Because this file imports some complicated tactics, it has many transitive dependencies
(which of course may not use `import tactic`, and must import selectively).
As (non-exhaustive) examples, these includes things like:
* algebra.group_power
* algebra.order.ring
* data.rat
* data.nat.prime
* data.list.perm
* data.set.lattice
* data.equiv.encodable.basic
* order.complete_lattice
-/
import tactic.basic -- ensure basic tactics are available
import tactic.abel
import tactic.ring_exp
import tactic.noncomm_ring
import tactic.linarith
import tactic.omega
import tactic.tfae
import tactic.apply_fun
import tactic.interval_cases
import tactic.reassoc_axiom -- most likely useful only for category_theory
import tactic.slice
import tactic.subtype_instance
import tactic.derive_fintype
import tactic.group
import tactic.cancel_denoms
import tactic.zify
import tactic.transport
import tactic.unfold_cases
import tactic.field_simp
import tactic.linear_combination
import tactic.polyrith
import tactic.expand_exists
|
98dadff861cb7c74989e5ab606a3d02d1084007c | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/hex_numeral.lean | 53b7249c53e0bce97a163a3bf18096485a2176fc | [
"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 | 110 | lean | vm_eval (0xff : nat)
vm_eval (0xffff : nat)
vm_eval (0xaa : nat)
vm_eval (0x10 : nat)
vm_eval (0x10000 : nat)
|
7186759e48ddb97f46cd3106a1d3431c9a30bad1 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Lean/Meta/Tactic/Induction.lean | d1b6f7ccc094f03c66976254996c4afc32809e27 | [
"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 | 11,794 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.RecursorInfo
import Lean.Meta.SynthInstance
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.FVarSubst
namespace Lean.Meta
private partial def getTargetArity : Expr β Nat
| Expr.mdata _ b _ => getTargetArity b
| Expr.forallE _ _ b _ => getTargetArity b + 1
| e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0
private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) β Expr β MetaM Expr
| [], recursor => pure recursor
| some pos :: rest, recursor =>
if h : pos < majorTypeArgs.size then
addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get β¨pos, hβ©))
else
throwTacticEx `induction mvarId "ill-formed recursor"
| none :: rest, recursor => do
let recursorType β inferType recursor
let recursorType β whnfForall recursorType
match recursorType with
| Expr.forallE _ d _ _ => do
let param β try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter"
addRecParams mvarId majorTypeArgs rest (mkApp recursor param)
| _ =>
throwTacticEx `induction mvarId "ill-formed recursor"
structure InductionSubgoal where
mvarId : MVarId
fields : Array Expr := #[]
subst : FVarSubst := {}
deriving Inhabited
private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do
let type β whnfForall type
match type with
| Expr.forallE _ _ b _ => pure $ b.instantiate1 x
| _ => throwTacticEx `induction mvarId "ill-formed recursor"
structure AltVarNames where
explicit : Bool := false -- true if `@` modifier was used
varNames : List Name := []
deriving Inhabited
private partial def finalize
(mvarId : MVarId) (givenNames : Array AltVarNames) (recursorInfo : RecursorInfo)
(reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr)
: MetaM (Array InductionSubgoal) := do
let target β getMVarType mvarId
let initialArity := getTargetArity target
let recursorType β inferType recursor
let numMinors := recursorInfo.produceMotive.length
let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do
let recursorType β whnfForall recursorType
if recursorType.isForall && pos < recursorInfo.numArgs then
if pos == recursorInfo.firstIndexPos then
let (recursor, recursorType) β indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do
let recursor := mkApp recursor index
let recursorType β getTypeBody mvarId recursorType index
pure (recursor, recursorType)
let recursor := mkApp recursor major
let recursorType β getTypeBody mvarId recursorType major
loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals
else
-- consume motive
let tag β getMVarTag mvarId
if minorIdx β₯ numMinors then throwTacticEx `induction mvarId "ill-formed recursor"
match recursorType with
| Expr.forallE n d b c =>
let d := d.headBeta
-- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise
if c.binderInfo.isInstImplicit && givenNames.isEmpty then
match (β synthInstance? d) with
| some inst =>
let recursor := mkApp recursor inst
let recursorType β getTypeBody mvarId recursorType inst
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals
| none => do
-- Add newSubgoal if type class resolution failed
let mvar β mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType β getTypeBody mvarId recursorType mvar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! })
else
let arity := getTargetArity d
if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor"
let nparams := arity - initialArity -- number of fields due to minor premise
let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted
let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get β¨minorIdx, hβ© else {}
let mvar β mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType β getTypeBody mvarId recursorType mvar
-- Try to clear major premise from new goal
let mvarId' β tryClear mvar.mvarId! major.fvarId!
let (fields, mvarId') β introN mvarId' nparams minorGivenNames.varNames (useNamesForExplicitOnly := !minorGivenNames.explicit)
let (extra, mvarId') β introNP mvarId' nextra
let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) =>
if i < indices.size + 1 then subst
else
let revertedFVarId := reverted[i]
let newFVarId := extra[i - indices.size - 1]
subst.insert revertedFVarId (mkFVar newFVarId)
let fields := fields.map mkFVar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst })
| _ => unreachable!
else
unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor"
assignExprMVar mvarId recursor
pure subgoals
loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[]
private def throwUnexpectedMajorType {Ξ±} (mvarId : MVarId) (majorType : Expr) : MetaM Ξ± :=
throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}"
def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array AltVarNames := #[]) : MetaM (Array InductionSubgoal) :=
withMVarContext mvarId do
trace[Meta.Tactic.induction] "initial\n{MessageData.ofGoal mvarId}"
checkNotAssigned mvarId `induction
let majorLocalDecl β getLocalDecl majorFVarId
let recursorInfo β mkRecursorInfo recursorName
let some majorType β whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun _ majorTypeArgs => do
recursorInfo.paramsPos.forM fun paramPos? => do
match paramPos? with
| none => pure ()
| some paramPos => if paramPos β₯ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let mctx β getMCtx
let indices β recursorInfo.indicesPos.toArray.mapM fun idxPos => do
if idxPos β₯ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let idx := majorTypeArgs.get! idxPos
unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}"
majorTypeArgs.size.forM fun i => do
let arg := majorTypeArgs[i]
if i != idxPos && arg == idx then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}"
if i < idxPos && mctx.exprDependsOn arg idx.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}"
-- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`.
-- Note that if `arg` is not a variable, we will fail anyway when we visit it.
if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then
let idxDecl β getLocalDecl idx.fvarId!
if mctx.localDeclDependsOn idxDecl arg.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}"
pure idx
let target β getMVarType mvarId
if !recursorInfo.depElim && mctx.exprDependsOn target majorFVarId then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise"
-- Revert indices and major premise preserving variable order
let (reverted, mvarId) β revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true
-- Re-introduce indices and major
let (indices', mvarId) β introNP mvarId indices.size
let (majorFVarId', mvarId) β intro1P mvarId
-- Create FVarSubst with indices
let baseSubst := do
let mut subst : FVarSubst := {}
let mut i := 0
for index in indices do
subst := subst.insert index.fvarId! (mkFVar indices'[i])
i := i + 1
pure subst
trace[Meta.Tactic.induction] "after revert&intro\n{MessageData.ofGoal mvarId}"
-- Update indices and major
let indices := indices'.map mkFVar
let majorFVarId := majorFVarId'
let major := mkFVar majorFVarId
withMVarContext mvarId do
let target β getMVarType mvarId
let targetLevel β getLevel target
let targetLevel β normalizeLevel targetLevel
let majorLocalDecl β getLocalDecl majorFVarId
let some majorType β whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun majorTypeFn majorTypeArgs => do
match majorTypeFn with
| Expr.const majorTypeFnName majorTypeFnLevels _ => do
let majorTypeFnLevels := majorTypeFnLevels.toArray
let (recursorLevels, foundTargetLevel) β recursorInfo.univLevelPos.foldlM (init := (#[], false))
fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do
match univPos with
| RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true)
| RecursorUnivLevelPos.majorType idx =>
if idx β₯ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor"
pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel)
if !foundTargetLevel && !targetLevel.isZero then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop"
let recursor := mkConst recursorName recursorLevels.toList
let recursor β addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor
-- Compute motive
let motive := target
let motive β if recursorInfo.depElim then mkLambdaFVars #[major] motive else pure motive
let motive β mkLambdaFVars indices motive
let recursor := mkApp recursor motive
finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor
| _ =>
throwTacticEx `induction mvarId "major premise is not of the form (C ...)"
builtin_initialize registerTraceClass `Meta.Tactic.induction
end Lean.Meta
|
d55b447606dbbecba75035e8e10513c82d87f481 | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/ring_theory/roots_of_unity.lean | d213ba24b068cb95a68dd303426e5b669949144b | [
"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 | 28,234 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.nat.parity
import data.polynomial.ring_division
import group_theory.order_of_element
import ring_theory.integral_domain
import number_theory.divisors
import data.zmod.basic
import tactic.zify
/-!
# Roots of unity and primitive roots of unity
We define roots of unity in the context of an arbitrary commutative monoid,
as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative
monoids, expressing that an element is a primitive root of unity.
## Main definitions
* `roots_of_unity n M`, for `n : β+` is the subgroup of the units of a commutative monoid `M`
consisting of elements `x` that satisfy `x ^ n = 1`.
* `is_primitive_root ΞΆ k`: an element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`,
and if `l` satisfies `ΞΆ ^ l = 1` then `k β£ l`.
* `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`.
## Main results
* `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group.
* `is_primitive_root.zmod_equiv_gpowers`: `zmod k` is equivalent to
the subgroup generated by a primitive `k`-th root of unity.
* `is_primitive_root.gpowers_eq`: in an integral domain, the subgroup generated by
a primitive `k`-th root of unity is equal to the `k`-th roots of unity.
* `is_primitive_root.card_primitive_roots`: if an integral domain
has a primitive `k`-th root of unity, then it has `Ο k` of them.
## Implementation details
It is desirable that `roots_of_unity` is a subgroup,
and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields.
We therefore implement it as a subgroup of the units of a commutative monoid.
We have chosen to define `roots_of_unity n` for `n : β+`, instead of `n : β`,
because almost all lemmas need the positivity assumption,
and in particular the type class instances for `fintype` and `is_cyclic`.
On the other hand, for primitive roots of unity, it is desirable to have a predicate
not just on units, but directly on elements of the ring/field.
For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity
in the complex numbers, without having to turn that number into a unit first.
This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and
`is_primitive_root.coe_units_iff` should provide the necessary glue.
-/
open_locale classical big_operators
noncomputable theory
open polynomial
open finset
variables {M N G Gβ R S : Type*}
variables [comm_monoid M] [comm_monoid N] [comm_group G] [comm_group_with_zero Gβ]
variables [integral_domain R] [integral_domain S]
section roots_of_unity
variables {k l : β+}
/-- `roots_of_unity k M` is the subgroup of elements `m : units M` that satisfy `m ^ k = 1` -/
def roots_of_unity (k : β+) (M : Type*) [comm_monoid M] : subgroup (units M) :=
{ carrier := { ΞΆ | ΞΆ ^ (k : β) = 1 },
one_mem' := one_pow _,
mul_mem' := Ξ» ΞΆ ΞΎ hΞΆ hΞΎ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *,
inv_mem' := Ξ» ΞΆ hΞΆ, by simp only [*, set.mem_set_of_eq, inv_pow, one_inv] at * }
@[simp] lemma mem_roots_of_unity (k : β+) (ΞΆ : units M) :
ΞΆ β roots_of_unity k M β ΞΆ ^ (k : β) = 1 := iff.rfl
lemma roots_of_unity_le_of_dvd (h : k β£ l) : roots_of_unity k M β€ roots_of_unity l M :=
begin
obtain β¨d, rflβ© := h,
intros ΞΆ h,
simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *,
end
lemma map_roots_of_unity (f : units M β* units N) (k : β+) :
(roots_of_unity k M).map f β€ roots_of_unity k N :=
begin
rintros _ β¨ΞΆ, h, rflβ©,
simp only [βmonoid_hom.map_pow, *, mem_roots_of_unity, subgroup.mem_coe, monoid_hom.map_one] at *
end
lemma mem_roots_of_unity_iff_mem_nth_roots {ΞΆ : units R} :
ΞΆ β roots_of_unity k R β (ΞΆ : R) β nth_roots k (1 : R) :=
by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow]
variables (k R)
/-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`.
This is implemented as equivalence of subtypes,
because `roots_of_unity` is a subgroup of the group of units,
whereas `nth_roots` is a multiset. -/
def roots_of_unity_equiv_nth_roots :
roots_of_unity k R β {x // x β nth_roots k (1 : R)} :=
begin
refine
{ to_fun := Ξ» x, β¨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2β©,
inv_fun := Ξ» x, β¨β¨x, x ^ (k - 1 : β), _, _β©, _β©,
left_inv := _,
right_inv := _ },
swap 4, { rintro β¨x, hxβ©, ext, refl },
swap 4, { rintro β¨x, hxβ©, ext, refl },
all_goals
{ rcases x with β¨x, hxβ©, rw [mem_nth_roots k.pos] at hx,
simp only [subtype.coe_mk, β pow_succ, β pow_succ', hx,
nat.sub_add_cancel (show 1 β€ (k : β), from k.one_le)] },
{ show (_ : units R) ^ (k : β) = 1,
simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] }
end
variables {k R}
@[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) :
(roots_of_unity_equiv_nth_roots R k x : R) = x :=
rfl
@[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x β nth_roots k (1 : R)}) :
((roots_of_unity_equiv_nth_roots R k).symm x : R) = x :=
rfl
variables (k R)
instance roots_of_unity.fintype : fintype (roots_of_unity k R) :=
fintype.of_equiv {x // x β nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm
instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) :=
is_cyclic_of_subgroup_integral_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype)
(units.ext.comp subtype.val_injective)
lemma card_roots_of_unity : fintype.card (roots_of_unity k R) β€ k :=
calc fintype.card (roots_of_unity k R)
= fintype.card {x // x β nth_roots k (1 : R)} : fintype.card_congr (roots_of_unity_equiv_nth_roots R k)
... β€ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.erase_dup_le _)
... = (nth_roots k (1 : R)).card : multiset.card_attach
... β€ k : card_nth_roots k 1
end roots_of_unity
/-- An element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`,
and if `l` satisfies `ΞΆ ^ l = 1` then `k β£ l`. -/
structure is_primitive_root (ΞΆ : M) (k : β) : Prop :=
(pow_eq_one : ΞΆ ^ (k : β) = 1)
(dvd_of_pow_eq_one : β l : β, ΞΆ ^ l = 1 β k β£ l)
section primitive_roots
variables {k : β}
/-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity
in the integral domain `R`. -/
def primitive_roots (k : β) (R : Type*) [integral_domain R] : finset R :=
(nth_roots k (1 : R)).to_finset.filter (Ξ» ΞΆ, is_primitive_root ΞΆ k)
@[simp] lemma mem_primitive_roots {ΞΆ : R} (h0 : 0 < k) :
ΞΆ β primitive_roots k R β is_primitive_root ΞΆ k :=
begin
rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp],
exact is_primitive_root.pow_eq_one
end
end primitive_roots
namespace is_primitive_root
variables {k l : β}
lemma iff_def (ΞΆ : M) (k : β) :
is_primitive_root ΞΆ k β (ΞΆ ^ k = 1) β§ (β l : β, ΞΆ ^ l = 1 β k β£ l) :=
β¨Ξ» β¨h1, h2β©, β¨h1, h2β©, Ξ» β¨h1, h2β©, β¨h1, h2β©β©
lemma mk_of_lt (ΞΆ : M) (hk : 0 < k) (h1 : ΞΆ ^ k = 1) (h : β l : β, 0 < l β l < k β ΞΆ ^ l β 1) :
is_primitive_root ΞΆ k :=
begin
refine β¨h1, _β©,
intros l hl,
apply dvd_trans _ (k.gcd_dvd_right l),
suffices : k.gcd l = k, { rw this },
rw eq_iff_le_not_lt,
refine β¨nat.le_of_dvd hk (k.gcd_dvd_left l), _β©,
intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h',
exact pow_gcd_eq_one _ h1 hl
end
section comm_monoid
variables {ΞΆ : M} (h : is_primitive_root ΞΆ k)
lemma pow_eq_one_iff_dvd (l : β) : ΞΆ ^ l = 1 β k β£ l :=
β¨h.dvd_of_pow_eq_one l,
by { rintro β¨i, rflβ©, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }β©
lemma is_unit (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_unit ΞΆ :=
begin
apply is_unit_of_mul_eq_one ΞΆ (ΞΆ ^ (k - 1)),
rw [β pow_succ, nat.sub_add_cancel h0, h.pow_eq_one]
end
lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ΞΆ ^ l β 1 :=
mt (nat.le_of_dvd h0 β h.dvd_of_pow_eq_one _) $ not_le_of_lt hl
lemma pow_inj (h : is_primitive_root ΞΆ k) β¦i j : ββ¦ (hi : i < k) (hj : j < k) (H : ΞΆ ^ i = ΞΆ ^ j) :
i = j :=
begin
wlog hij : i β€ j,
apply le_antisymm hij,
rw β nat.sub_eq_zero_iff_le,
apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt (nat.sub_le_self _ _) hj),
apply h.dvd_of_pow_eq_one,
rw [β ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj,
β pow_add, nat.sub_add_cancel hij, H, one_mul]
end
lemma one : is_primitive_root (1 : M) 1 :=
{ pow_eq_one := pow_one _,
dvd_of_pow_eq_one := Ξ» l hl, one_dvd _ }
@[simp] lemma one_right_iff : is_primitive_root ΞΆ 1 β ΞΆ = 1 :=
begin
split,
{ intro h, rw [β pow_one ΞΆ, h.pow_eq_one] },
{ rintro rfl, exact one }
end
@[simp] lemma coe_units_iff {ΞΆ : units M} :
is_primitive_root (ΞΆ : M) k β is_primitive_root ΞΆ k :=
by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one]
lemma pow_of_coprime (h : is_primitive_root ΞΆ k) (i : β) (hi : i.coprime k) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : k = 0,
{ subst k, simp only [*, pow_one, nat.coprime_zero_right] at * },
rcases h.is_unit (nat.pos_of_ne_zero h0) with β¨ΞΆ, rflβ©,
rw [β units.coe_pow],
rw coe_units_iff at h β’,
refine
{ pow_eq_one := by rw [β pow_mul', pow_mul, h.pow_eq_one, one_pow],
dvd_of_pow_eq_one := _ },
intros l hl,
apply h.dvd_of_pow_eq_one,
rw [β pow_one ΞΆ, β gpow_coe_nat ΞΆ, β hi.gcd_eq_one, nat.gcd_eq_gcd_ab, gpow_add,
mul_pow, β gpow_coe_nat, β gpow_mul, mul_right_comm],
simp only [gpow_mul, hl, h.pow_eq_one, one_gpow, one_pow, one_mul, gpow_coe_nat]
end
lemma pow_iff_coprime (h : is_primitive_root ΞΆ k) (h0 : 0 < k) (i : β) :
is_primitive_root (ΞΆ ^ i) k β i.coprime k :=
begin
refine β¨_, h.pow_of_coprime iβ©,
intro hi,
obtain β¨a, haβ© := i.gcd_dvd_left k,
obtain β¨b, hbβ© := i.gcd_dvd_right k,
suffices : b = k,
{ rwa [this, β one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } },
rw [ha] at hi,
rw [mul_comm] at hb,
apply nat.dvd_antisymm β¨i.gcd k, hbβ© (hi.dvd_of_pow_eq_one b _),
rw [β pow_mul', β mul_assoc, β hb, pow_mul, h.pow_eq_one, one_pow]
end
end comm_monoid
section comm_group
variables {ΞΆ : G} (h : is_primitive_root ΞΆ k)
lemma gpow_eq_one : ΞΆ ^ (k : β€) = 1 := h.pow_eq_one
lemma gpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β€) :
ΞΆ ^ l = 1 β (k : β€) β£ l :=
begin
by_cases h0 : 0 β€ l,
{ lift l to β using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 β€ -l, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -l to β using this with l' hl',
rw [β dvd_neg, β hl'],
norm_cast,
rw [β h.pow_eq_one_iff_dvd, β inv_inj, β gpow_neg, β hl', gpow_coe_nat, one_inv] }
end
lemma inv (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΆβ»ΒΉ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, one_inv, eq_self_iff_true, inv_pow],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [β inv_inj, β inv_pow, hl, one_inv]
end }
@[simp] lemma inv_iff : is_primitive_root ΞΆβ»ΒΉ k β is_primitive_root ΞΆ k :=
by { refine β¨_, Ξ» h, inv hβ©, intro h, rw [β inv_inv ΞΆ], exact inv h }
lemma gpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β€) (hi : i.gcd k = 1) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : 0 β€ i,
{ lift i to β using h0, exact h.pow_of_coprime i hi },
have : 0 β€ -i, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -i to β using this with i' hi',
rw [β inv_iff, β gpow_neg, β hi'],
apply h.pow_of_coprime,
rw [int.gcd, β int.nat_abs_neg, β hi'] at hi,
exact hi
end
@[simp] lemma coe_subgroup_iff (H : subgroup G) {ΞΆ : H} :
is_primitive_root (ΞΆ : G) k β is_primitive_root ΞΆ k :=
by simp only [iff_def, β subgroup.coe_pow, β H.coe_one, β subtype.ext_iff]
end comm_group
section comm_group_with_zero
variables {ΞΆ : Gβ} (h : is_primitive_root ΞΆ k)
lemma fpow_eq_one : ΞΆ ^ (k : β€) = 1 := h.pow_eq_one
lemma fpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β€) :
ΞΆ ^ l = 1 β (k : β€) β£ l :=
begin
by_cases h0 : 0 β€ l,
{ lift l to β using h0, rw [fpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 β€ -l, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -l to β using this with l' hl',
rw [β dvd_neg, β hl'],
norm_cast,
rw [β h.pow_eq_one_iff_dvd, β inv_inj', β fpow_neg, β hl', fpow_coe_nat, inv_one] }
end
lemma inv' (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΆβ»ΒΉ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_pow'],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [β inv_inj', β inv_pow', hl, inv_one]
end }
@[simp] lemma inv_iff' : is_primitive_root ΞΆβ»ΒΉ k β is_primitive_root ΞΆ k :=
by { refine β¨_, Ξ» h, inv' hβ©, intro h, rw [β inv_inv' ΞΆ], exact inv' h }
lemma fpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β€) (hi : i.gcd k = 1) :
is_primitive_root (ΞΆ ^ i) k :=
begin
by_cases h0 : 0 β€ i,
{ lift i to β using h0, exact h.pow_of_coprime i hi },
have : 0 β€ -i, { simp only [not_le, neg_nonneg] at h0 β’, exact le_of_lt h0 },
lift -i to β using this with i' hi',
rw [β inv_iff', β fpow_neg, β hi'],
apply h.pow_of_coprime,
rw [int.gcd, β int.nat_abs_neg, β hi'] at hi,
exact hi
end
end comm_group_with_zero
section integral_domain
variables {ΞΆ : R}
@[simp] lemma primitive_roots_zero : primitive_roots 0 R = β
:=
begin
rw [β finset.val_eq_zero, β multiset.subset_zero, β nth_roots_zero (1 : R), primitive_roots],
simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero,
finset.filter_true_of_mem, finset.empty_val, not_false_iff,
multiset.zero_subset, nth_roots_zero]
end
@[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} :=
begin
apply finset.eq_singleton_iff_unique_mem.2,
split,
{ simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] },
{ intros x hx,
rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx,
exact hx }
end
lemma neg_one (p : β) [char_p R p] (hp : p β 2) : is_primitive_root (-1 : R) 2 :=
mk_of_lt (-1 : R) dec_trivial (by simp only [one_pow, neg_square]) $
begin
intros l hl0 hl2,
obtain rfl : l = 1,
{ unfreezingI { clear_dependent R p }, dec_trivial! },
simp only [pow_one, ne.def],
intro h,
suffices h2 : p β£ 2,
{ have := char_p.char_ne_one R p,
unfreezingI { clear_dependent R },
have aux := nat.le_of_dvd dec_trivial h2,
revert this hp h2, revert p, dec_trivial },
simp only [β char_p.cast_eq_zero_iff R p, nat.cast_bit0, nat.cast_one],
rw [bit0, β h, neg_add_self] { occs := occurrences.pos [1] }
end
lemma eq_neg_one_of_two_right (h : is_primitive_root ΞΆ 2) : ΞΆ = -1 :=
begin
apply (eq_or_eq_neg_of_pow_two_eq_pow_two ΞΆ 1 _).resolve_left,
{ rw [β pow_one ΞΆ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial },
{ simp only [h.pow_eq_one, one_pow] }
end
end integral_domain
section integral_domain
variables {ΞΆ : units R} (h : is_primitive_root ΞΆ k)
protected
lemma mem_roots_of_unity {n : β+} (h : is_primitive_root ΞΆ n) : ΞΆ β roots_of_unity n R :=
h.pow_eq_one
/-- The (additive) monoid equivalence between `zmod k`
and the powers of a primitive root of unity `ΞΆ`. -/
def zmod_equiv_gpowers (h : is_primitive_root ΞΆ k) : zmod k β+ additive (subgroup.gpowers ΞΆ) :=
add_equiv.of_bijective
(add_monoid_hom.lift_of_surjective (int.cast_add_hom _)
zmod.int_cast_surjective
{ to_fun := Ξ» i, additive.of_mul (β¨_, i, rflβ© : subgroup.gpowers ΞΆ),
map_zero' := by { simp only [gpow_zero], refl },
map_add' := by { intros i j, simp only [gpow_add], refl } }
(Ξ» i hi,
begin
simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k,
add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi β’,
obtain β¨i, rflβ© := hi,
simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat],
refl
end)) $
begin
split,
{ rw add_monoid_hom.injective_iff,
intros i hi,
rw subtype.ext_iff at hi,
have := (h.gpow_eq_one_iff_dvd _).mp hi,
rw [β (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm],
exact classical.some_spec (zmod.int_cast_surjective i) },
{ rintro β¨ΞΎ, i, rflβ©,
refine β¨int.cast_add_hom _ i, _β©,
rw [add_monoid_hom.lift_of_surjective_comp_apply],
refl }
end
@[simp] lemma zmod_equiv_gpowers_apply_coe_int (i : β€) :
h.zmod_equiv_gpowers i = additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ) :=
begin
apply add_monoid_hom.lift_of_surjective_comp_apply,
intros j hj,
simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k,
add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hj β’,
obtain β¨j, rflβ© := hj,
simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat],
refl
end
@[simp] lemma zmod_equiv_gpowers_apply_coe_nat (i : β) :
h.zmod_equiv_gpowers i = additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ) :=
begin
have : (i : zmod k) = (i : β€), by norm_cast,
simp only [this, zmod_equiv_gpowers_apply_coe_int, gpow_coe_nat],
refl
end
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow (i : β€) :
h.zmod_equiv_gpowers.symm (additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ)) = i :=
by rw [β h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_int]
@[simp] lemma zmod_equiv_gpowers_symm_apply_gpow' (i : β€) :
h.zmod_equiv_gpowers.symm β¨ΞΆ ^ i, i, rflβ© = i :=
h.zmod_equiv_gpowers_symm_apply_gpow i
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow (i : β) :
h.zmod_equiv_gpowers.symm (additive.of_mul (β¨ΞΆ ^ i, i, rflβ© : subgroup.gpowers ΞΆ)) = i :=
by rw [β h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_nat]
@[simp] lemma zmod_equiv_gpowers_symm_apply_pow' (i : β) :
h.zmod_equiv_gpowers.symm β¨ΞΆ ^ i, i, rflβ© = i :=
h.zmod_equiv_gpowers_symm_apply_pow i
lemma gpowers_eq {k : β+} {ΞΆ : units R} (h : is_primitive_root ΞΆ k) :
subgroup.gpowers ΞΆ = roots_of_unity k R :=
begin
apply subgroup.ext',
haveI : fact (0 < (k : β)) := k.pos,
haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ (h.zmod_equiv_gpowers).to_equiv,
refine @set.eq_of_subset_of_card_le (units R) (subgroup.gpowers ΞΆ) (roots_of_unity k R)
F (roots_of_unity.fintype R k)
(subgroup.gpowers_subset $ show ΞΆ β roots_of_unity k R, from h.pow_eq_one) _,
calc fintype.card (roots_of_unity k R)
β€ k : card_roots_of_unity R k
... = fintype.card (zmod k) : (zmod.card k).symm
... = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr (h.zmod_equiv_gpowers).to_equiv
end
lemma eq_pow_of_mem_roots_of_unity {k : β+} {ΞΆ ΞΎ : units R}
(h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ β roots_of_unity k R) :
β (i : β) (hi : i < k), ΞΆ ^ i = ΞΎ :=
begin
obtain β¨n, rflβ© : β n : β€, ΞΆ ^ n = ΞΎ, by rwa [β h.gpowers_eq] at hΞΎ,
have hk0 : (0 : β€) < k := by exact_mod_cast k.pos,
let i := n % k,
have hi0 : 0 β€ i := int.mod_nonneg _ (ne_of_gt hk0),
lift i to β using hi0 with iβ hiβ,
refine β¨iβ, _, _β©,
{ zify, rw [hiβ], exact int.mod_lt_of_pos _ hk0 },
{ have aux := h.gpow_eq_one, rw [β coe_coe] at aux,
rw [β gpow_coe_nat, hiβ, β int.mod_add_div n k, gpow_add, gpow_mul,
aux, one_gpow, mul_one] }
end
lemma eq_pow_of_pow_eq_one {k : β} {ΞΆ ΞΎ : R}
(h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ^ k = 1) (h0 : 0 < k) :
β i < k, ΞΆ ^ i = ΞΎ :=
begin
obtain β¨ΞΆ, rflβ© := h.is_unit h0,
obtain β¨ΞΎ, rflβ© := is_unit_of_pow_eq_one ΞΎ k hΞΎ h0,
obtain β¨k, rflβ© : β k' : β+, k = k' := β¨β¨k, h0β©, rflβ©,
simp only [β units.coe_pow, β units.ext_iff],
rw coe_units_iff at h,
apply h.eq_pow_of_mem_roots_of_unity,
rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hΞΎ, units.coe_one]
end
lemma is_primitive_root_iff' {k : β+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) :
is_primitive_root ΞΎ k β β (i < (k : β)) (hi : i.coprime k), ΞΆ ^ i = ΞΎ :=
begin
split,
{ intro hΞΎ,
obtain β¨i, hik, rflβ© := h.eq_pow_of_mem_roots_of_unity hΞΎ.pow_eq_one,
rw h.pow_iff_coprime k.pos at hΞΎ,
exact β¨i, hik, hΞΎ, rflβ© },
{ rintro β¨i, -, hi, rflβ©, exact h.pow_of_coprime i hi }
end
lemma is_primitive_root_iff {k : β} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) :
is_primitive_root ΞΎ k β β (i < k) (hi : i.coprime k), ΞΆ ^ i = ΞΎ :=
begin
split,
{ intro hΞΎ,
obtain β¨i, hik, rflβ© := h.eq_pow_of_pow_eq_one hΞΎ.pow_eq_one h0,
rw h.pow_iff_coprime h0 at hΞΎ,
exact β¨i, hik, hΞΎ, rflβ© },
{ rintro β¨i, -, hi, rflβ©, exact h.pow_of_coprime i hi }
end
lemma card_roots_of_unity' {n : β+} (h : is_primitive_root ΞΆ n) :
fintype.card (roots_of_unity n R) = n :=
begin
haveI : fact (0 < βn) := n.pos,
let e := h.zmod_equiv_gpowers,
haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ e.to_equiv,
calc fintype.card (roots_of_unity n R)
= fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr $ by rw h.gpowers_eq
... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm
... = n : zmod.card n
end
lemma card_roots_of_unity {ΞΆ : R} {n : β+} (h : is_primitive_root ΞΆ n) :
fintype.card (roots_of_unity n R) = n :=
begin
obtain β¨ΞΆ, hΞΆβ© := h.is_unit n.pos,
rw [β hΞΆ, is_primitive_root.coe_units_iff] at h,
exact h.card_roots_of_unity'
end
/-- The cardinality of the multiset `nth_roots βn (1 : R)` is `n`
if there is a primitive root of unity in `R`. -/
lemma card_nth_roots {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(nth_roots n (1 : R)).card = n :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.card_zero, nth_roots_zero] },
rw eq_iff_le_not_lt,
use card_nth_roots n 1,
{ rw [not_lt],
have hcard : fintype.card {x // x β nth_roots n (1 : R)}
β€ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.erase_dup_le _),
rw multiset.card_attach at hcard,
rw β pnat.to_pnat'_coe hpos at hcard h β’,
set m := nat.to_pnat' n,
rw [β fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard,
exact hcard }
end
/-- The multiset `nth_roots βn (1 : R)` has no repeated elements
if there is a primitive root of unity in `R`. -/
lemma nth_roots_nodup {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).nodup :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.nodup_zero, nth_roots_zero] },
apply (@multiset.erase_dup_eq_self R _ _).1,
rw eq_iff_le_not_lt,
split,
{ exact multiset.erase_dup_le (nth_roots n (1 : R)) },
{ by_contra ha,
replace ha := multiset.card_lt_of_lt ha,
rw card_nth_roots h at ha,
have hrw : (nth_roots n (1 : R)).erase_dup.card =
fintype.card {x // x β (nth_roots n (1 : R))},
{ set fs := (β¨(nth_roots n (1 : R)).erase_dup, multiset.nodup_erase_dup _β© : finset R),
rw [β finset.card_mk, β fintype.card_of_subtype fs _],
intro x,
simp only [multiset.mem_erase_dup, finset.mem_mk] },
rw β pnat.to_pnat'_coe hpos at h hrw ha,
set m := nat.to_pnat' n,
rw [hrw, β fintype.card_congr (roots_of_unity_equiv_nth_roots R m),
card_roots_of_unity h] at ha,
exact nat.lt_asymm ha ha }
end
@[simp] lemma card_nth_roots_finset {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(nth_roots_finset n R).card = n :=
by rw [nth_roots_finset, β multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots]
open_locale nat
/-- If an integral domain has a primitive `k`-th root of unity, then it has `Ο k` of them. -/
lemma card_primitive_roots {ΞΆ : R} {k : β} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) :
(primitive_roots k R).card = Ο k :=
begin
symmetry,
refine finset.card_congr (Ξ» i _, ΞΆ ^ i) _ _ _,
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i - hi,
rw mem_primitive_roots h0,
exact h.pow_of_coprime i hi.symm },
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i j hi - hj - H,
exact h.pow_inj hi hj H },
{ simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ],
intros ΞΎ hΞΎ,
rw [mem_primitive_roots h0, h.is_primitive_root_iff h0] at hΞΎ,
rcases hΞΎ with β¨i, hin, hi, Hβ©,
exact β¨i, β¨hin, hi.symmβ©, Hβ© }
end
/-- The sets `primitive_roots k R` are pairwise disjoint. -/
lemma disjoint {k l : β} (hk : 0 < k) (hl : 0 < l) (h : k β l) :
disjoint (primitive_roots k R) (primitive_roots l R) :=
begin
intro z,
simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots, hk, hl, iff_def],
rintro β¨β¨hzk, Hzkβ©, β¨hzl, Hzlβ©β©,
apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl]
end
/-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`,
then there is a `b`-th primitive root of unity in `R`. -/
lemma pow {ΞΆ : R} {n : β} {a b : β}
(hn : 0 < n) (h : is_primitive_root ΞΆ n) (hprod : n = a * b) :
is_primitive_root (ΞΆ ^ a) b :=
begin
subst n,
simp only [iff_def, β pow_mul, h.pow_eq_one, eq_self_iff_true, true_and],
intros l hl,
have ha0 : a β 0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn },
rwa β mul_dvd_mul_iff_left ha0,
exact h.dvd_of_pow_eq_one _ hl
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i β£ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bind_primitive_roots' {ΞΆ : R} {n : β+} (h : is_primitive_root ΞΆ n) :
nth_roots_finset n R = (nat.divisors βn).bind (Ξ» i, (primitive_roots i R)) :=
begin
symmetry,
apply finset.eq_of_subset_of_card_le,
{ intros x,
simp only [nth_roots_finset, β multiset.to_finset_eq (nth_roots_nodup h),
exists_prop, finset.mem_bind, finset.mem_filter, finset.mem_range, mem_nth_roots,
finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff],
rintro β¨a, β¨d, hdβ©, haβ©,
have hazero : 0 < a,
{ contrapose! hd with ha0,
simp only [le_zero_iff_eq, zero_mul, *] at *,
exact n.ne_zero },
rw mem_primitive_roots hazero at ha,
rw [hd, pow_mul, ha.pow_eq_one, one_pow] },
{ apply le_of_eq,
rw [h.card_nth_roots_finset, finset.card_bind],
{ rw [β nat.sum_totient n, nat.filter_dvd_eq_divisors (pnat.ne_zero n), sum_congr rfl]
{ occs := occurrences.pos [1] },
simp only [finset.mem_filter, finset.mem_range, nat.mem_divisors],
rintro k β¨H, hkβ©,
have hdvd := H,
rcases H with β¨d, hdβ©,
rw mul_comm at hd,
rw (h.pow n.pos hd).card_primitive_roots (pnat.pos_of_div_pos hdvd) },
{ intros i hi j hj hdiff,
simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hi hj,
exact disjoint (pnat.pos_of_div_pos hi) (pnat.pos_of_div_pos hj) hdiff } }
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i β£ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bind_primitive_roots {ΞΆ : R} {n : β} (hpos : 0 < n)
(h : is_primitive_root ΞΆ n) :
nth_roots_finset n R = (nat.divisors n).bind (Ξ» i, (primitive_roots i R)) :=
@nth_roots_one_eq_bind_primitive_roots' _ _ _ β¨n, hposβ© h
end integral_domain
end is_primitive_root
|
38635741c2ddbf10d14f1a0fb806781d0b4ff6be | 4727251e0cd73359b15b664c3170e5d754078599 | /src/group_theory/commensurable.lean | 7ac884e35107693624b38e35447f6f72de7b72b5 | [
"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,217 | lean | /-
Copyright (c) 2021 Chris Birkbeck. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Birkbeck
-/
import group_theory.index
import group_theory.quotient_group
import group_theory.subgroup.pointwise
import group_theory.group_action.conj_act
/-!
# Commensurability for subgroups
This file defines commensurability for subgroups of a group `G`. It then goes on to prove that
commensurability defines an equivalence relation and finally defines the commensurator of a subgroup
of `G`.
## Main definitions
* `commensurable`: defines commensurability for two subgroups `H`, `K` of `G`
* `commensurator`: defines the commensurator of a subgroup `H` of `G`.
## Implementation details
We define the commensurator of a subgroup `H` of `G` by first defining it as a subgroup of
`(conj_act G)`, which we call commensurator' and then taking the pre-image under
the map `G β (conj_act G)` to obtain our commensurator as a subgroup of `G`.
-/
variables {G : Type*} [group G]
/--Two subgroups `H K` of `G` are commensurable if `H β K` has finite index in both `H` and `K` -/
def commensurable (H K : subgroup G) : Prop :=
H.relindex K β 0 β§ K.relindex H β 0
namespace commensurable
open_locale pointwise
@[refl] protected lemma refl (H : subgroup G) : commensurable H H := by
simp [commensurable]
lemma comm {H K : subgroup G} : commensurable H K β commensurable K H := and.comm
@[symm] lemma symm {H K : subgroup G} : commensurable H K β commensurable K H := and.symm
@[trans] lemma trans {H K L : subgroup G} (hhk : commensurable H K) (hkl : commensurable K L) :
commensurable H L :=
β¨subgroup.relindex_ne_zero_trans hhk.1 hkl.1, subgroup.relindex_ne_zero_trans hkl.2 hhk.2β©
lemma equivalence : equivalence (@commensurable G _) :=
β¨commensurable.refl, Ξ» _ _, commensurable.symm, Ξ» _ _ _, commensurable.transβ©
/--Equivalence of `K/H β K` with `gKgβ»ΒΉ/gHgβ»ΒΉ β gKgβ»ΒΉ`-/
def quot_conj_equiv (H K : subgroup G) (g : conj_act G) :
K β§Έ (H.subgroup_of K) β (g β’ K).1 β§Έ ((g β’ H).subgroup_of (g β’ K)) :=
quotient.congr (K.equiv_smul g).to_equiv (Ξ» a b, by { rw [βquotient.eq', βquotient.eq',
quotient_group.eq', quotient_group.eq', subgroup.mem_subgroup_of, subgroup.mem_subgroup_of,
mul_equiv.to_equiv_eq_coe, mul_equiv.coe_to_equiv, βmul_equiv.map_inv, βmul_equiv.map_mul,
subgroup.equiv_smul_apply_coe, subgroup.smul_mem_pointwise_smul_iff] })
lemma commensurable_conj {H K : subgroup G} (g : conj_act G) :
commensurable H K β commensurable (g β’ H) (g β’ K) :=
and_congr (not_iff_not.mpr (eq.congr_left (cardinal.to_nat_congr (quot_conj_equiv H K g))))
(not_iff_not.mpr (eq.congr_left (cardinal.to_nat_congr (quot_conj_equiv K H g))))
lemma commensurable_inv (H : subgroup G) (g : conj_act G) :
commensurable (g β’ H) H β commensurable H (gβ»ΒΉ β’ H) :=
by rw [commensurable_conj, inv_smul_smul]
/-- For `H` a subgroup of `G`, this is the subgroup of all elements `g : conj_aut G`
such that `commensurable (g β’ H) H` -/
def commensurator' (H : subgroup G) : subgroup (conj_act G) :=
{ carrier := {g : conj_act G | commensurable (g β’ H) H},
one_mem' := by rw [set.mem_set_of_eq, one_smul],
mul_mem' := Ξ» a b ha hb, by
{ rw [set.mem_set_of_eq, mul_smul],
exact trans ((commensurable_conj a).mp hb) ha },
inv_mem' := Ξ» a ha, by rwa [set.mem_set_of_eq, comm, βcommensurable_inv] }
/-- For `H` a subgroup of `G`, this is the subgroup of all elements `g : G`
such that `commensurable (g H gβ»ΒΉ) H` -/
def commensurator (H : subgroup G) : subgroup G :=
(commensurator' H).comap (conj_act.to_conj_act.to_monoid_hom)
@[simp] lemma commensurator'_mem_iff (H : subgroup G) (g : conj_act G) :
g β (commensurator' H) β commensurable (g β’ H) H := iff.rfl
@[simp] lemma commensurator_mem_iff (H : subgroup G) (g : G) :
g β (commensurator H) β commensurable (conj_act.to_conj_act g β’ H) H := iff.rfl
lemma eq {H K : subgroup G} (hk : commensurable H K) :
commensurator H = commensurator K :=
subgroup.ext (Ξ» x, let hx := (commensurable_conj x).1 hk in
β¨Ξ» h, hx.symm.trans (h.trans hk), Ξ» h, hx.trans (h.trans hk.symm)β©)
end commensurable
|
eaec01e1a9b7e4f0b8090a411caad251ef868df3 | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/negation.lean | 68770db655a8077a22d82ea63181784ec5be72d9 | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 551 | lean | lemma L1 : β (p q:Prop), (p β q) β Β¬q β Β¬p :=
Ξ» p q h nq hp, nq (h hp)
--#check L1
lemma L1' : β (p q:Prop), (p β q) β Β¬q β Β¬p :=
Ξ» p q hpq hnq,
assume hp : p,
show false, from hnq (hpq hp)
--#check L1'
lemma L2 : β (p q:Prop), p β Β¬p β q :=
Ξ» p q hp hnp, false.elim (hnp hp)
--#check L2
lemma L2' : β (p q:Prop), p β Β¬p β q :=
Ξ» p q hp hnp, absurd hp hnp
--#check L2'
lemma L3 : β (p q r:Prop), Β¬p β q β (q β p) β r :=
Ξ» p q r hnp hq hqp, absurd (hqp hq) hnp
--#check L3
|
2d7fe0af5d2bed8b981d3aa94cb638e79fff5eee | 8c02fed42525b65813b55c064afe2484758d6d09 | /src/irsem_exec.lean | 1d493955a422a1bb328ef095898b2374255d6904 | [
"LicenseRef-scancode-generic-cla",
"MIT"
] | permissive | microsoft/AliveInLean | 3eac351a34154efedd3ffc4fe2fa4ec01b219e0d | 4b739dd6e4266b26a045613849df221374119871 | refs/heads/master | 1,691,419,737,939 | 1,689,365,567,000 | 1,689,365,568,000 | 131,156,103 | 23 | 18 | NOASSERTION | 1,660,342,040,000 | 1,524,747,538,000 | Lean | UTF-8 | Lean | false | false | 2,444 | lean | -- Copyright (c) Microsoft Corporation. All rights reserved.
-- Licensed under the MIT license.
import system.io
import smt2.syntax
import .irsem
import .lang
import .common
import .bitvector
open io
-- poisonty := bool -- If it is false, it is poison.
-- boolty := bool -- If it is false, it is UB.
def irsem_exec : irsem := { intty := bitvector, poisonty := bool, boolty := bool }
namespace irsem_exec
open irsem
@[reducible] instance iul: uint_like irsem_exec.intty := bitvector_is_uint_like
@[reducible] instance ihc: has_comp irsem_exec.intty irsem_exec.boolty := bitvector_has_comp
@[reducible] instance ioc: has_overflow_check irsem_exec.intty irsem_exec.boolty :=
bitvector_has_overflow_check
@[reducible] instance pbl: bool_like irsem_exec.poisonty := bool_is_bool_like
@[reducible] instance p2b: has_coe irsem_exec.poisonty irsem_exec.boolty := β¨@id boolβ©
@[reducible] instance bbl: bool_like irsem_exec.boolty := bool_is_bool_like
@[reducible] instance b2i: has_coe irsem_exec.boolty (irsem_exec.intty size.one) := bool_bitvec_has_coe
@[reducible] instance bhi (sz:size): has_ite irsem_exec.boolty (irsem_exec.intty sz) := bool_has_ite (bitvector sz)
@[reducible] instance bhi2 : has_ite irsem_exec.boolty irsem_exec.poisonty := bool_has_ite irsem_exec.poisonty
def give_poison: irsem.valty irsem_exec β irsem.valty irsem_exec
| (valty.ival sz o b) := valty.ival sz o
(@bool_like.ff irsem_exec.poisonty irsem_exec.pbl)
instance poisonty_tostr: has_to_string irsem_exec.poisonty :=
β¨Ξ» b:bool, to_string bβ©
instance valty_tostr: has_to_string irsem_exec.valty :=
β¨Ξ» b, match b with
| (irsem.valty.ival sz b p) := has_to_string.to_string (b.1) ++
", poison:" ++ to_string (~p)
endβ©
instance boolty_tostr: has_to_string irsem_exec.boolty :=
β¨Ξ» b:bool, to_string bβ©
instance tostr: has_to_string irsem_exec.irstate :=
β¨Ξ» st, irstate.to_string' irsem_exec stβ©
def int_to_smt (sz:size) : irsem_exec.intty sz β smt2.term
| z := smt2.term.const (smt2.special_constant.bitvec sz z.1)
def val_to_smt : irsem_exec.valty β smt2.term
| (irsem.valty.ival sz i _) := int_to_smt sz i
def poison_to_smt : irsem_exec.valty β smt2.term
| (irsem.valty.ival _ _ p) := smt2.term.const (smt2.special_constant.bool p)
def bool_to_smt : irsem_exec.boolty β smt2.term
| p := smt2.term.const (smt2.special_constant.bool p)
end irsem_exec
|
4071f25e996d2a6e287ca90b629b279e2e7eb694 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/order/partial_sups.lean | 79a1be0e73f962bf7bc37580c11b3bb9c6c16e40 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,783 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import data.finset.lattice
import data.set.pairwise
import order.preorder_hom
/-!
# The monotone sequence of partial supremums of a sequence
We define `partial_sups : (β β Ξ±) β β ββ Ξ±` inductively. For `f : β β Ξ±`, `partial_sups f` is
the sequence `f 0 `, `f 0 β f 1`, `f 0 β f 1 β f 2`, ... The point of this definition is that
* it doesn't need a `β¨`, as opposed to `β¨ (i β€ n), f i`.
* it doesn't need a `β₯`, as opposed to `(finset.range (n + 1)).sup f`.
* it avoids needing to prove that `finset.range (n + 1)` is nonempty to use `finset.sup'`.
Equivalence with those definitions is shown by `partial_sups_eq_bsupr`, `partial_sups_eq_sup_range`,
`partial_sups_eq_sup'_range` and respectively.
## Notes
One might dispute whether this sequence should start at `f 0` or `β₯`. We choose the former because :
* Starting at `β₯` requires... having a bottom element.
* `Ξ» f n, (finset.range n).sup f` is already effectively the sequence starting at `β₯`.
* If we started at `β₯` we wouldn't have the Galois insertion. See `partial_sups.gi`.
## TODO
One could generalize `partial_sups` to any locally finite bot preorder domain, in place of `β`.
Necessary for the TODO in the module docstring of `order.disjointed`.
-/
variables {Ξ± : Type*}
section semilattice_sup
variables [semilattice_sup Ξ±]
/-- The monotone sequence whose value at `n` is the supremum of the `f m` where `m β€ n`. -/
def partial_sups (f : β β Ξ±) : β ββ Ξ± :=
β¨@nat.rec (Ξ» _, Ξ±) (f 0) (Ξ» (n : β) (a : Ξ±), a β f (n + 1)),
monotone_nat_of_le_succ (Ξ» n, le_sup_left)β©
@[simp] lemma partial_sups_zero (f : β β Ξ±) : partial_sups f 0 = f 0 := rfl
@[simp] lemma partial_sups_succ (f : β β Ξ±) (n : β) :
partial_sups f (n + 1) = partial_sups f n β f (n + 1) := rfl
lemma le_partial_sups_of_le (f : β β Ξ±) {m n : β} (h : m β€ n) :
f m β€ partial_sups f n :=
begin
induction n with n ih,
{ cases h, apply le_refl, },
{ cases h with h h,
{ exact le_sup_right, },
{ exact (ih h).trans le_sup_left, } },
end
lemma le_partial_sups (f : β β Ξ±) :
f β€ partial_sups f :=
Ξ» n, le_partial_sups_of_le f le_rfl
lemma partial_sups_le (f : β β Ξ±) (n : β)
(a : Ξ±) (w : β m, m β€ n β f m β€ a) : partial_sups f n β€ a :=
begin
induction n with n ih,
{ apply w 0 le_rfl, },
{ exact sup_le (ih (Ξ» m p, w m (nat.le_succ_of_le p))) (w (n + 1) le_rfl) }
end
lemma monotone.partial_sups_eq {f : β β Ξ±} (hf : monotone f) :
(partial_sups f : β β Ξ±) = f :=
begin
ext n,
induction n with n ih,
{ refl },
{ rw [partial_sups_succ, ih, sup_eq_right.2 (hf (nat.le_succ _))] }
end
lemma partial_sups_mono : monotone (partial_sups : (β β Ξ±) β β ββ Ξ±) :=
begin
rintro f g h n,
induction n with n ih,
{ exact h 0 },
{ exact sup_le_sup ih (h _) }
end
/-- `partial_sups` forms a Galois insertion with the coercion from monotone functions to functions.
-/
def partial_sups.gi : galois_insertion (partial_sups : (β β Ξ±) β β ββ Ξ±) coe_fn :=
{ choice := Ξ» f h, β¨f, begin
convert (partial_sups f).monotone,
exact (le_partial_sups f).antisymm h,
endβ©,
gc := Ξ» f g, begin
refine β¨(le_partial_sups f).trans, Ξ» h, _β©,
convert partial_sups_mono h,
exact preorder_hom.ext _ _ g.monotone.partial_sups_eq.symm,
end,
le_l_u := Ξ» f, le_partial_sups f,
choice_eq := Ξ» f h, preorder_hom.ext _ _ ((le_partial_sups f).antisymm h) }
lemma partial_sups_eq_sup'_range (f : β β Ξ±) (n : β) :
partial_sups f n = (finset.range (n + 1)).sup' β¨n, finset.self_mem_range_succ nβ© f :=
begin
induction n with n ih,
{ simp },
{ dsimp [partial_sups] at ih β’,
simp_rw @finset.range_succ n.succ,
rw [ih, finset.sup'_insert, sup_comm] }
end
end semilattice_sup
lemma partial_sups_eq_sup_range [semilattice_sup_bot Ξ±] (f : β β Ξ±) (n : β) :
partial_sups f n = (finset.range (n + 1)).sup f :=
begin
induction n with n ih,
{ simp },
{ dsimp [partial_sups] at ih β’,
rw [finset.range_succ, finset.sup_insert, sup_comm, ih] }
end
/- Note this lemma requires a distributive lattice, so is not useful (or true) in situations such as
submodules. -/
lemma partial_sups_disjoint_of_disjoint [distrib_lattice_bot Ξ±]
(f : β β Ξ±) (h : pairwise (disjoint on f)) {m n : β} (hmn : m < n) :
disjoint (partial_sups f m) (f n) :=
begin
induction m with m ih,
{ exact h 0 n hmn.ne, },
{ rw [partial_sups_succ, disjoint_sup_left],
exact β¨ih (nat.lt_of_succ_lt hmn), h (m + 1) n hmn.neβ© }
end
section complete_lattice
variables [complete_lattice Ξ±]
lemma partial_sups_eq_bsupr (f : β β Ξ±) (n : β) :
partial_sups f n = β¨ (i β€ n), f i :=
begin
rw [partial_sups_eq_sup_range, finset.sup_eq_supr],
congr,
ext a,
exact supr_congr_Prop (by rw [finset.mem_range, nat.lt_succ_iff]) (Ξ» _, rfl),
end
@[simp] lemma supr_partial_sups_eq (f : β β Ξ±) :
(β¨ n, partial_sups f n) = β¨ n, f n :=
begin
refine (supr_le $ Ξ» n, _).antisymm (supr_le_supr $ le_partial_sups f),
rw partial_sups_eq_bsupr,
exact bsupr_le_supr _ _,
end
lemma supr_le_supr_of_partial_sups_le_partial_sups {f g : β β Ξ±}
(h : partial_sups f β€ partial_sups g) :
(β¨ n, f n) β€ β¨ n, g n :=
begin
rw [βsupr_partial_sups_eq f, βsupr_partial_sups_eq g],
exact supr_le_supr h,
end
lemma supr_eq_supr_of_partial_sups_eq_partial_sups {f g : β β Ξ±}
(h : partial_sups f = partial_sups g) :
(β¨ n, f n) = β¨ n, g n :=
by simp_rw [βsupr_partial_sups_eq f, βsupr_partial_sups_eq g, h]
end complete_lattice
|
406a554632eebcddb0b948173c4009f04bc972ce | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/matchtac.lean | 3df10bf5f527dc603a1d8125cd58323bb49c846e | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,598 | lean | new_frontend
theorem tst1 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (hβ : (a : Ξ±) β (as : List Ξ±) β xs = a :: as β p) (hβ : xs = [] β p) : p :=
by match h:xs with
| [] => exact hβ h
| z::zs => apply hβ z zs; assumption
theorem tst2 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (hβ : (a : Ξ±) β (as : List Ξ±) β xs = a :: as β p) (hβ : xs = [] β p) : p :=
by match h:xs with
| [] => ?nilCase
| z::zs => ?consCase;
case consCase => exact hβ z zs h;
case nilCase => exact hβ h
def tst3 {Ξ± Ξ² Ξ³ : Type} (h : Ξ± Γ Ξ² Γ Ξ³) : Ξ² Γ Ξ± Γ Ξ³ :=
by {
match h with
| (a, b, c) => exact (b, a, c)
}
theorem tst4 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (hβ : (a : Ξ±) β (as : List Ξ±) β xs = a :: as β p) (hβ : xs = [] β p) : p := by
match h:xs with
| [] => _
| z::zs => _
case match_2 => exact hβ z zs h
exact hβ h
theorem tst5 {p q r} (h : p β¨ q β¨ r) : r β¨ q β¨ p:= by
match h with
| Or.inl h => exact Or.inr (Or.inr h)
| Or.inr (Or.inl h) => ?c1
| Or.inr (Or.inr h) => ?c2
case c2 =>
apply Or.inl
assumption
case c1 =>
apply Or.inr
apply Or.inl
assumption
theorem tst6 {p q r} (h : p β¨ q β¨ r) : r β¨ q β¨ p:= by
match h with
| Or.inl h => exact Or.inr (Or.inr h)
| Or.inr (Or.inl h) => ?c1
| Or.inr (Or.inr h) =>
apply Or.inl
assumption
case c1 => apply Or.inr; apply Or.inl; assumption
theorem tst7 {p q r} (h : p β¨ q β¨ r) : r β¨ q β¨ p:=
by match h with
| Or.inl h =>
exact Or.inr (Or.inr h)
| Or.inr (Or.inl h) =>
apply Or.inr;
apply Or.inl;
assumption
| Or.inr (Or.inr h) =>
apply Or.inl;
assumption
inductive ListLast.{u} {Ξ± : Type u} : List Ξ± β Type u
| empty : ListLast []
| nonEmpty : (as : List Ξ±) β (a : Ξ±) β ListLast (as ++ [a])
axiom last {Ξ±} (xs : List Ξ±) : ListLast xs
axiom back {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : Ξ±
axiom popBack {Ξ±} : List Ξ± β List Ξ±
axiom backEq {Ξ±} [Inhabited Ξ±] : (xs : List Ξ±) β (x : Ξ±) β back (xs ++ [x]) = x
axiom popBackEq {Ξ±} : (xs : List Ξ±) β (x : Ξ±) β popBack (xs ++ [x]) = xs
theorem tst8 {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : xs β [] β xs = popBack xs ++ [back xs] :=
match xs, h:last xs with
| _, ListLast.empty => fun h => absurd rfl h
| _, ListLast.nonEmpty ys y => fun _ => sorry
theorem tst9 {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : xs β [] β xs = popBack xs ++ [back xs] := by
match xs, h:last xs with
| _, ListLast.empty => intro h; exact absurd rfl h
| _, ListLast.nonEmpty ys y => intro; rw [popBackEq, backEq]; exact rfl
|
90b009a7db2582043551c6b5332ad892f4255a93 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/unifhint2.lean | c31691f22ca9afafb1c185363be6f76a6331c1d8 | [
"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 | 639 | lean | /- The following hints are too expensive, but good enough for small natural numbers -/
unif_hint natAddBase (x y : Nat) where
y =?= 0
|-
Nat.add (Nat.succ x) y =?= Nat.succ x
unif_hint natAddStep (x y z w : Nat) where
y =?= Nat.succ w
z =?= Nat.add (Nat.succ x) w
|-
Nat.add (Nat.succ x) y =?= Nat.succ z
def BV (n : Nat) := { a : Array Bool // a.size = n }
def sext (x : BV s) (n : Nat) : BV (s+n) :=
β¨mkArray (s+n) false, Array.size_mkArray ..β©
def bvmul (x y : BV w) : BV w := x
def tst1 (x y : BV 64) : BV 128 :=
bvmul (sext x 64) (sext y _)
def tst2 (x y : BV 16) : BV 32 :=
bvmul (sext x 16) (sext y _)
|
570a0efff15ffa0072ab3d20782b97dcee65f992 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/125.lean | 71e066b80f8b0881cd545bbb43cc0a9cdecd81bf | [
"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 | 673 | lean | new_frontend
class HasElems (Ξ± : Type) : Type := (elems : Array Ξ±)
def elems (Ξ± : Type) [HasElems Ξ±] : Array Ξ± := HasElems.elems
inductive Foo : Type
| mk1 : Bool β Foo
| mk2 : Bool β Foo
open Foo
instance BoolElems : HasElems Bool := β¨#[false, true]β©
instance FooElems : HasElems Foo := β¨(elems Bool).map mk1 ++ (elems Bool).map mk2β©
def fooRepr (foo : Foo) :=
match foo with
| mk1 b => "OH " ++ toString b
| mk2 b => "DR " ++ toString b
instance : HasRepr Foo := β¨fooReprβ©
#eval elems Foo
#eval #[false, true].map Foo.mk1
def Foo.toBool : Foo β Bool
| Foo.mk1 b => b
| Foo.mk2 b => b
#eval #[Foo.mk1 false, Foo.mk2 true].map Foo.toBool
|
b1d66eb853218b33edd0dbfc5e06246582b29663 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/algebra/ordered_auto.lean | f544d9be8e86ddec17d99876355945ab52611b6f | [] | 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,578 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.algebra.basic
import Mathlib.algebra.module.ordered
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# Ordered algebras
An ordered algebra is an ordered semiring, which is an algebra over an ordered commutative semiring,
for which scalar multiplication is "compatible" with the two orders.
The prototypical example is 2x2 matrices over the reals or complexes (or indeed any C^* algebra)
where the ordering the one determined by the positive cone of positive operators,
i.e. `A β€ B` iff `B - A = star R * R` for some `R`.
(We don't yet have this example in mathlib.)
## Implementation
Because the axioms for an ordered algebra are exactly the same as those for the underlying
module being ordered, we don't actually introduce a new class, but just use the `ordered_semimodule`
mixin.
## Tags
ordered algebra
-/
theorem algebra_map_monotone {R : Type u_1} {A : Type u_2} [ordered_comm_ring R] [ordered_ring A]
[algebra R A] [ordered_semimodule R A] : monotone β(algebra_map R A) :=
sorry
protected instance linear_ordered_comm_ring.to_ordered_semimodule {R : Type u_1}
[linear_ordered_comm_ring R] : ordered_semimodule R R :=
ordered_semimodule.mk ordered_semiring.mul_lt_mul_of_pos_left
fun (a b c : R) (wβ : c β’ a < c β’ b) (wβ : 0 < c) => iff.mp (mul_lt_mul_left wβ) wβ
end Mathlib |
8864e7fdb3d76990c9c7c0389c2b01fbe3ecf240 | cf39355caa609c0f33405126beee2739aa3cb77e | /library/data/vector.lean | f329b45e2434f6eb29a3ff85528ae53ed4f7d2e7 | [
"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 | 4,562 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Tuples are lists of a fixed size.
It is implemented as a subtype.
-/
prelude
import init.data.list init.data.subtype init.meta.interactive init.data.fin
universes u v w
def vector (Ξ± : Type u) (n : β) := { l : list Ξ± // l.length = n }
namespace vector
variables {Ξ± : Type u} {Ξ² : Type v} {Ο : Type w}
variable {n : β}
instance [decidable_eq Ξ±] : decidable_eq (vector Ξ± n) :=
begin unfold vector, apply_instance end
@[pattern] def nil : vector Ξ± 0 := β¨[], rflβ©
@[pattern] def cons : Ξ± β vector Ξ± n β vector Ξ± (nat.succ n)
| a β¨ v, h β© := β¨ a::v, congr_arg nat.succ h β©
@[reducible] def length (v : vector Ξ± n) : β := n
open nat
def head : vector Ξ± (nat.succ n) β Ξ±
| β¨ [], h β© := by contradiction
| β¨ a :: v, h β© := a
theorem head_cons (a : Ξ±) : Ξ (v : vector Ξ± n), head (cons a v) = a
| β¨ l, h β© := rfl
def tail : vector Ξ± n β vector Ξ± (n - 1)
| β¨ [], h β© := β¨ [], congr_arg pred h β©
| β¨ a :: v, h β© := β¨ v, congr_arg pred h β©
theorem tail_cons (a : Ξ±) : Ξ (v : vector Ξ± n), tail (cons a v) = v
| β¨ l, h β© := rfl
@[simp] theorem cons_head_tail : β v : vector Ξ± (succ n), (cons (head v) (tail v)) = v
| β¨ [], h β© := by contradiction
| β¨ a :: v, h β© := rfl
def to_list (v : vector Ξ± n) : list Ξ± := v.1
def nth : Ξ (v : vector Ξ± n), fin n β Ξ± | β¨ l, h β© i := l.nth_le i.1 (by rw h; exact i.2)
def append {n m : nat} : vector Ξ± n β vector Ξ± m β vector Ξ± (n + m)
| β¨ lβ, hβ β© β¨ lβ, hβ β© := β¨ lβ ++ lβ, by simp * β©
@[elab_as_eliminator] def elim {Ξ±} {C : Ξ {n}, vector Ξ± n β Sort u} (H : βl : list Ξ±, C β¨l, rflβ©)
{n : nat} : Ξ (v : vector Ξ± n), C v
| β¨l, hβ© := match n, h with ._, rfl := H l end
/- map -/
def map (f : Ξ± β Ξ²) : vector Ξ± n β vector Ξ² n
| β¨ l, h β© := β¨ list.map f l, by simp * β©
@[simp] theorem map_nil (f : Ξ± β Ξ²) : map f nil = nil := rfl
theorem map_cons (f : Ξ± β Ξ²) (a : Ξ±) : Ξ (v : vector Ξ± n), map f (cons a v) = cons (f a) (map f v)
| β¨l,hβ© := rfl
def mapβ (f : Ξ± β Ξ² β Ο) : vector Ξ± n β vector Ξ² n β vector Ο n
| β¨ x, _ β© β¨ y, _ β© := β¨ list.mapβ f x y, by simp * β©
def repeat (a : Ξ±) (n : β) : vector Ξ± n :=
β¨ list.repeat a n, list.length_repeat a n β©
def drop (i : β) : vector Ξ± n β vector Ξ± (n - i)
| β¨l, pβ© := β¨ list.drop i l, by simp * β©
def take (i : β) : vector Ξ± n β vector Ξ± (min i n)
| β¨l, pβ© := β¨ list.take i l, by simp * β©
def remove_nth (i : fin n) : vector Ξ± n β vector Ξ± (n - 1)
| β¨l, pβ© := β¨ list.remove_nth l i.1, by rw [l.length_remove_nth i.1]; rw p; exact i.2 β©
def of_fn : Ξ {n}, (fin n β Ξ±) β vector Ξ± n
| 0 f := nil
| (n+1) f := cons (f 0) (of_fn (Ξ»i, f i.succ))
section accum
open prod
variable {Ο : Type}
def map_accumr (f : Ξ± β Ο β Ο Γ Ξ²) : vector Ξ± n β Ο β Ο Γ vector Ξ² n
| β¨ x, px β© c :=
let res := list.map_accumr f x c in
β¨ res.1, res.2, by simp * β©
def map_accumrβ {Ξ± Ξ² Ο Ο : Type} (f : Ξ± β Ξ² β Ο β Ο Γ Ο)
: vector Ξ± n β vector Ξ² n β Ο β Ο Γ vector Ο n
| β¨ x, px β© β¨ y, py β© c :=
let res := list.map_accumrβ f x y c in
β¨ res.1, res.2, by simp * β©
end accum
protected theorem eq {n : β} : β (a1 a2 : vector Ξ± n), to_list a1 = to_list a2 β a1 = a2
| β¨x, h1β© β¨._, h2β© rfl := rfl
protected theorem eq_nil (v : vector Ξ± 0) : v = nil :=
v.eq nil (list.eq_nil_of_length_eq_zero v.2)
@[simp] theorem to_list_mk (v : list Ξ±) (P : list.length v = n) : to_list (subtype.mk v P) = v :=
rfl
@[simp] theorem to_list_nil : to_list nil = @list.nil Ξ± :=
rfl
@[simp] theorem to_list_length (v : vector Ξ± n) : (to_list v).length = n := v.2
@[simp] theorem to_list_cons (a : Ξ±) (v : vector Ξ± n) : to_list (cons a v) = a :: to_list v :=
begin cases v, reflexivity end
@[simp] theorem to_list_append {n m : nat} (v : vector Ξ± n) (w : vector Ξ± m) : to_list (append v w) = to_list v ++ to_list w :=
begin cases v, cases w, reflexivity end
@[simp] theorem to_list_drop {n m : β} (v : vector Ξ± m) : to_list (drop n v) = list.drop n (to_list v) :=
begin cases v, reflexivity end
@[simp] theorem to_list_take {n m : β} (v : vector Ξ± m) : to_list (take n v) = list.take n (to_list v) :=
begin cases v, reflexivity end
end vector
|
271833b194db380b56f6dbd44b37ac96421b4357 | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world08/level10.lean | d1b6c7c8f7e09e5eb9aaff827f09e54b6a477b34 | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 157 | lean | lemma add_left_eq_zero {{a b : mynat}} (H : a + b = 0) : b = 0 :=
begin
cases b with d,
refl,
rw add_succ at H,
exfalso,
apply succ_ne_zero,
rw H,
refl,
end
|
d5c4e264c16bfe7928711b8aeb160c953131821d | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /counterexamples/sorgenfrey_line.lean | 6b21bb09c031dfe20595475dae84b6eaa7931787 | [
"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 | 14,242 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import topology.instances.irrational
import topology.algebra.order.archimedean
import topology.paracompact
import topology.metric_space.metrizable
import topology.metric_space.emetric_paracompact
import data.set.intervals.monotone
/-!
# Sorgenfrey line
In this file we define `sorgenfrey_line` (notation: `ββ`) to be the Sorgenfrey line. It is the real
line with the topology space structure generated by half-open intervals `set.Ico a b`.
We prove that this line is a completely normal Hausdorff space but its product with itself is not a
normal space. In particular, this implies that the topology on `ββ` is neither metrizable, nor
second countable.
## Notations
- `ββ`: Sorgenfrey line.
## TODO
Prove that the Sorgenfrey line is a paracompact space.
-/
open set filter topological_space
open_locale topological_space filter
noncomputable theory
/-- The Sorgenfrey line. It is the real line with the topology space structure generated by
half-open intervals `set.Ico a b`. -/
@[derive [conditionally_complete_linear_order, linear_ordered_field, archimedean]]
def sorgenfrey_line : Type := β
localized "notation (name := sorgenfrey_line) `ββ` := sorgenfrey_line" in sorgenfrey_line
namespace sorgenfrey_line
/-- Ring homomorphism between the Sorgenfrey line and the standard real line. -/
def to_real : ββ β+* β := ring_equiv.refl β
instance : topological_space ββ :=
topological_space.generate_from {s : set ββ | β a b : ββ, Ico a b = s}
lemma is_open_Ico (a b : ββ) : is_open (Ico a b) :=
topological_space.generate_open.basic _ β¨a, b, rflβ©
lemma is_open_Ici (a : ββ) : is_open (Ici a) :=
Union_Ico_right a βΈ is_open_Union (is_open_Ico a)
lemma nhds_basis_Ico (a : ββ) : (π a).has_basis (Ξ» b, a < b) (Ξ» b, Ico a b) :=
begin
rw topological_space.nhds_generate_from,
haveI : nonempty {x // x β€ a} := set.nonempty_Iic_subtype,
have : (β¨
(x : {i // i β€ a}), π (Ici βx)) = π (Ici a),
{ refine (is_least.is_glb _).infi_eq,
exact β¨β¨β¨a, le_rflβ©, rflβ©, forall_range_iff.2 $
Ξ» b, principal_mono.2 $ Ici_subset_Ici.2 b.2β©, },
simp only [mem_set_of_eq, infi_and, infi_exists, @infi_comm _ (_ β _),
@infi_comm _ (set ββ), infi_infi_eq_right],
simp_rw [@infi_comm _ ββ (_ β€ _), infi_subtype', β Ici_inter_Iio, β inf_principal, β inf_infi,
β infi_inf, this, infi_subtype],
suffices : (β¨
x β Ioi a, π (Iio x)).has_basis ((<) a) Iio, from this.principal_inf _,
refine has_basis_binfi_principal _ nonempty_Ioi,
exact directed_on_iff_directed.2 (directed_of_inf $ Ξ» x y hxy, Iio_subset_Iio hxy),
end
lemma nhds_basis_Ico_rat (a : ββ) :
(π a).has_countable_basis (Ξ» r : β, a < r) (Ξ» r, Ico a r) :=
begin
refine β¨(nhds_basis_Ico a).to_has_basis (Ξ» b hb, _) (Ξ» r hr, β¨_, hr, subset.rflβ©),
set.to_countable _β©,
rcases exists_rat_btwn hb with β¨r, har, hrbβ©,
exact β¨r, har, Ico_subset_Ico_right hrb.leβ©
end
lemma nhds_basis_Ico_inv_pnat (a : ββ) :
(π a).has_basis (Ξ» n : β+, true) (Ξ» n, Ico a (a + nβ»ΒΉ)) :=
begin
refine (nhds_basis_Ico a).to_has_basis (Ξ» b hb, _)
(Ξ» n hn, β¨_, lt_add_of_pos_right _ (inv_pos.2 $ nat.cast_pos.2 n.pos), subset.rflβ©),
rcases exists_nat_one_div_lt (sub_pos.2 hb) with β¨k, hkβ©,
rw [one_div] at hk,
rw [β nat.cast_add_one] at hk,
exact β¨k.succ_pnat, trivial, Ico_subset_Ico_right (le_sub_iff_add_le'.1 hk.le)β©
end
lemma nhds_countable_basis_Ico_inv_pnat (a : ββ) :
(π a).has_countable_basis (Ξ» n : β+, true) (Ξ» n, Ico a (a + nβ»ΒΉ)) :=
β¨nhds_basis_Ico_inv_pnat a, set.to_countable _β©
lemma nhds_antitone_basis_Ico_inv_pnat (a : ββ) :
(π a).has_antitone_basis (Ξ» n : β+, Ico a (a + nβ»ΒΉ)) :=
β¨nhds_basis_Ico_inv_pnat a, monotone_const.Ico $
antitone.const_add (Ξ» k l hkl, inv_le_inv_of_le (nat.cast_pos.2 k.pos) (nat.mono_cast hkl)) _β©
lemma is_open_iff {s : set ββ} : is_open s β β x β s, β y > x, Ico x y β s :=
is_open_iff_mem_nhds.trans $ forallβ_congr $ Ξ» x hx, (nhds_basis_Ico x).mem_iff
lemma is_closed_iff {s : set ββ} : is_closed s β β x β s, β y > x, disjoint (Ico x y) s :=
by simp only [β is_open_compl_iff, is_open_iff, mem_compl_iff, subset_compl_iff_disjoint_right]
lemma exists_Ico_disjoint_closed {a : ββ} {s : set ββ} (hs : is_closed s) (ha : a β s) :
β b > a, disjoint (Ico a b) s :=
is_closed_iff.1 hs a ha
@[simp] lemma map_to_real_nhds (a : ββ) : map to_real (π a) = π[β₯] (to_real a) :=
begin
refine ((nhds_basis_Ico a).map _).eq_of_same_basis _,
simpa only [to_real.image_eq_preimage] using nhds_within_Ici_basis_Ico (to_real a)
end
lemma nhds_eq_map (a : ββ) : π a = map to_real.symm (π[β₯] a.to_real) :=
by simp_rw [β map_to_real_nhds, map_map, (β), to_real.symm_apply_apply, map_id']
lemma nhds_eq_comap (a : ββ) : π a = comap to_real (π[β₯] a.to_real) :=
by rw [β map_to_real_nhds, comap_map to_real.injective]
@[continuity] lemma continuous_to_real : continuous to_real :=
continuous_iff_continuous_at.2 $ Ξ» x,
by { rw [continuous_at, tendsto, map_to_real_nhds], exact inf_le_left }
instance : order_closed_topology ββ :=
β¨is_closed_le_prod.preimage (continuous_to_real.prod_map continuous_to_real)β©
instance : has_continuous_add ββ :=
begin
refine β¨continuous_iff_continuous_at.2 _β©,
rintro β¨x, yβ©,
simp only [continuous_at, nhds_prod_eq, nhds_eq_map, nhds_eq_comap (x + y), prod_map_map_eq,
tendsto_comap_iff, tendsto_map'_iff, (β), β nhds_within_prod_eq],
exact (continuous_add.tendsto _).inf (maps_to.tendsto $ Ξ» x hx, add_le_add hx.1 hx.2)
end
lemma is_clopen_Ici (a : ββ) : is_clopen (Ici a) := β¨is_open_Ici a, is_closed_Iciβ©
lemma is_clopen_Iio (a : ββ) : is_clopen (Iio a) :=
by simpa only [compl_Ici] using (is_clopen_Ici a).compl
lemma is_clopen_Ico (a b : ββ) : is_clopen (Ico a b) :=
(is_clopen_Ici a).inter (is_clopen_Iio b)
instance : totally_disconnected_space ββ :=
β¨Ξ» s hs' hs x hx y hy, le_antisymm (hs.subset_clopen (is_clopen_Ici x) β¨x, hx, le_rflβ© hy)
(hs.subset_clopen (is_clopen_Ici y) β¨y, hy, le_rflβ© hx)β©
instance : first_countable_topology ββ := β¨Ξ» x, (nhds_basis_Ico_rat x).is_countably_generatedβ©
/-- Sorgenfrey line is a completely normal Hausdorff topological space. -/
instance : t5_space ββ :=
begin
/- Let `s` and `t` be disjoint closed sets. For each `x β s` we choose `X x` such that
`set.Ico x (X x)` is disjoint with `t`. Similarly, for each `y β t` we choose `Y y` such that
`set.Ico y (Y y)` is disjoint with `s`. Then `β x β s, Ico x (X x)` and `β y β t, Ico y (Y y)` are
disjoint open sets that include `s` and `t`. -/
refine β¨Ξ» s t hdβ hdβ, _β©,
choose! X hX hXd
using Ξ» x (hx : x β s), exists_Ico_disjoint_closed is_closed_closure (disjoint_left.1 hdβ hx),
choose! Y hY hYd
using Ξ» y (hy : y β t), exists_Ico_disjoint_closed is_closed_closure (disjoint_right.1 hdβ hy),
refine disjoint_of_disjoint_of_mem _
(bUnion_mem_nhds_set $ Ξ» x hx, (is_open_Ico x (X x)).mem_nhds $ left_mem_Ico.2 (hX x hx))
(bUnion_mem_nhds_set $ Ξ» y hy, (is_open_Ico y (Y y)).mem_nhds $ left_mem_Ico.2 (hY y hy)),
simp only [disjoint_Union_left, disjoint_Union_right, Ico_disjoint_Ico],
intros y hy x hx,
cases le_total x y with hle hle,
{ calc min (X x) (Y y) β€ X x : min_le_left _ _
... β€ y : not_lt.1 (Ξ» hyx, (hXd x hx).le_bot β¨β¨hle, hyxβ©, subset_closure hyβ©)
... β€ max x y : le_max_right _ _ },
{ calc min (X x) (Y y) β€ Y y : min_le_right _ _
... β€ x : not_lt.1 $ Ξ» hxy, (hYd y hy).le_bot β¨β¨hle, hxyβ©, subset_closure hxβ©
... β€ max x y : le_max_left _ _ }
end
lemma dense_range_coe_rat : dense_range (coe : β β ββ) :=
begin
refine dense_iff_inter_open.2 _,
rintro U Uo β¨x, hxβ©,
rcases is_open_iff.1 Uo _ hx with β¨y, hxy, hUβ©,
rcases exists_rat_btwn hxy with β¨z, hxz, hzyβ©,
exact β¨z, hU β¨hxz.le, hzyβ©, mem_range_self _β©
end
instance : separable_space ββ := β¨β¨_, countable_range _, dense_range_coe_ratβ©β©
lemma is_closed_antidiagonal (c : ββ) : is_closed {x : ββ Γ ββ | x.1 + x.2 = c} :=
is_closed_singleton.preimage continuous_add
lemma is_clopen_Ici_prod (x : ββ Γ ββ) : is_clopen (Ici x) :=
(Ici_prod_eq x).symm βΈ (is_clopen_Ici _).prod (is_clopen_Ici _)
/-- Any subset of an antidiagonal `{(x, y) : ββ Γ ββ| x + y = c}` is a closed set. -/
lemma is_closed_of_subset_antidiagonal {s : set (ββ Γ ββ)} {c : ββ}
(hs : β x : ββ Γ ββ, x β s β x.1 + x.2 = c) : is_closed s :=
begin
rw [β closure_subset_iff_is_closed],
rintro β¨x, yβ© H,
obtain rfl : x + y = c,
{ change (x, y) β {p : ββ Γ ββ | p.1 + p.2 = c},
exact closure_minimal (hs : s β {x | x.1 + x.2 = c}) (is_closed_antidiagonal c) H },
rcases mem_closure_iff.1 H (Ici (x, y)) (is_clopen_Ici_prod _).1 le_rfl
with β¨β¨x', y'β©, β¨hx : x β€ x', hy : y β€ y'β©, Hβ©,
convert H,
{ refine hx.antisymm _,
rwa [β add_le_add_iff_right, hs _ H, add_le_add_iff_left] },
{ refine hy.antisymm _,
rwa [β add_le_add_iff_left, hs _ H, add_le_add_iff_right] }
end
lemma nhds_prod_antitone_basis_inv_pnat (x y : ββ) :
(π (x, y)).has_antitone_basis (Ξ» n : β+, Ico x (x + nβ»ΒΉ) ΓΛ’ Ico y (y + nβ»ΒΉ)) :=
begin
rw [nhds_prod_eq],
exact (nhds_antitone_basis_Ico_inv_pnat x).prod (nhds_antitone_basis_Ico_inv_pnat y)
end
/-- The product of the Sorgenfrey line and itself is not a normal topological space. -/
lemma not_normal_space_prod : Β¬normal_space (ββ Γ ββ) :=
begin
have hβ : β {n : β+}, (0 : β) < nβ»ΒΉ, from Ξ» n, inv_pos.2 (nat.cast_pos.2 n.pos),
have hβ' : β {n : β+} {x : β}, x < x + nβ»ΒΉ, from Ξ» n x, lt_add_of_pos_right _ hβ,
introI,
/- Let `S` be the set of points `(x, y)` on the line `x + y = 0` such that `x` is rational.
Let `T` be the set of points `(x, y)` on the line `x + y = 0` such that `x` is irrational.
These sets are closed, see `sorgenfrey_line.is_closed_of_subset_antidiagonal`, and disjoint. -/
set S := {x : ββ Γ ββ | x.1 + x.2 = 0 β§ β r : β, βr = x.1},
set T := {x : ββ Γ ββ | x.1 + x.2 = 0 β§ irrational x.1.to_real},
have hSc : is_closed S, from is_closed_of_subset_antidiagonal (Ξ» x hx, hx.1),
have hTc : is_closed T, from is_closed_of_subset_antidiagonal (Ξ» x hx, hx.1),
have hd : disjoint S T,
{ rw disjoint_iff_inf_le,
rintro β¨x, yβ© β¨β¨-, r, rfl : _ = xβ©, -, hrβ©,
exact r.not_irrational hr },
/- Consider disjoint open sets `U β S` and `V β T`. -/
rcases normal_separation hSc hTc hd with β¨U, V, Uo, Vo, SU, TV, UVβ©,
/- For each point `(x, -x) β T`, choose a neighborhood
`Ico x (x + kβ»ΒΉ) ΓΛ’ Ico (-x) (-x + kβ»ΒΉ) β V`. -/
have : β x : ββ, irrational x.to_real β
β k : β+, Ico x (x + kβ»ΒΉ) ΓΛ’ Ico (-x) (-x + kβ»ΒΉ) β V,
{ intros x hx,
have hV : V β π (x, -x), from Vo.mem_nhds (@TV (x, -x) β¨add_neg_self x, hxβ©),
exact (nhds_prod_antitone_basis_inv_pnat _ _).mem_iff.1 hV },
choose! k hkV,
/- Since the set of irrational numbers is a dense GΞ΄ set in the usual topology of `β`, there
exists `N > 0` such that the set `C N = {x : β | irrational x β§ k x = N}` is dense in a nonempty
interval. In other words, the closure of this set has a nonempty interior. -/
set C : β+ β set β := Ξ» n, closure {x | irrational x β§ k (to_real.symm x) = n},
have H : {x : β | irrational x} β β n, C n,
from Ξ» x hx, mem_Union.2 β¨_, subset_closure β¨hx, rflβ©β©,
have Hd : dense (β n, interior (C n)) :=
is_GΞ΄_irrational.dense_Union_interior_of_closed dense_irrational (Ξ» _, is_closed_closure) H,
obtain β¨N, hNβ© : β n : β+, (interior $ C n).nonempty, from nonempty_Union.mp Hd.nonempty,
/- Choose a rational number `r` in the interior of the closure of `C N`, then choose `n β₯ N > 0`
such that `Ico r (r + nβ»ΒΉ) Γ Ico (-r) (-r + nβ»ΒΉ) β U`. -/
rcases rat.dense_range_cast.exists_mem_open is_open_interior hN with β¨r, hrβ©,
have hrU : ((r, -r) : ββ Γ ββ) β U, from @SU (r, -r) β¨add_neg_self _, r, rflβ©,
obtain β¨n, hnN, hnβ© : β n (hnN : N β€ n), Ico (r : ββ) (r + nβ»ΒΉ) ΓΛ’ Ico (-r : ββ) (-r + nβ»ΒΉ) β U,
from ((nhds_prod_antitone_basis_inv_pnat _ _).has_basis_ge N).mem_iff.1 (Uo.mem_nhds hrU),
/- Finally, choose `x β Ioo (r : β) (r + nβ»ΒΉ) β© C N`. Then `(x, -r)` belongs both to `U` and `V`,
so they are not disjoint. This contradiction completes the proof. -/
obtain β¨x, hxn, hx_irr, rflβ© :
β x : β, x β Ioo (r : β) (r + nβ»ΒΉ) β§ irrational x β§ k (to_real.symm x) = N,
{ have : (r : β) β closure (Ioo (r : β) (r + nβ»ΒΉ)),
{ rw [closure_Ioo hβ'.ne, left_mem_Icc], exact hβ'.le },
rcases mem_closure_iff_nhds.1 this _ (mem_interior_iff_mem_nhds.1 hr) with β¨x', hx', hx'Ξ΅β©,
exact mem_closure_iff.1 hx' _ is_open_Ioo hx'Ξ΅ },
refine UV.le_bot (_ : (to_real.symm x, -βr) β _),
refine β¨hn β¨_, _β©, hkV (to_real.symm x) hx_irr β¨_, _β©β©,
{ exact Ioo_subset_Ico_self hxn },
{ exact left_mem_Ico.2 hβ' },
{ exact left_mem_Ico.2 hβ' },
{ refine (nhds_antitone_basis_Ico_inv_pnat (-x)).2 hnN β¨neg_le_neg hxn.1.le, _β©,
simp only [add_neg_lt_iff_le_add', lt_neg_add_iff_add_lt],
exact hxn.2 }
end
/-- Topology on the Sorgenfrey line is not metrizable. -/
lemma not_metrizable_space : Β¬metrizable_space ββ :=
begin
introI,
letI := metrizable_space_metric ββ,
exact not_normal_space_prod infer_instance
end
/-- Topology on the Sorgenfrey line is not second countable. -/
lemma not_second_countable_topology : Β¬second_countable_topology ββ :=
by { introI, exact not_metrizable_space (metrizable_space_of_t3_second_countable _) }
end sorgenfrey_line
|
0d10eef2e38f06efa0e6f26309034866bd612a85 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/have6.lean | a0623ab631df7af99770541158e105b4541fda71 | [
"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 | 619 | lean | prelude
definition Prop : Type.{1} := Type.{0}
constant and : Prop β Prop β Prop
infixl `β§`:25 := and
constant and_intro : forall (a b : Prop), a β b β a β§ b
constants a b c d : Prop
axiom Ha : a
axiom Hb : b
axiom Hc : c
check
have a β§ b, from and_intro a b Ha Hb,
have b β§ a, from and_intro b a Hb Ha,
have H : a β§ b, from and_intro a b Ha Hb,
have H : a β§ b, from and_intro a b Ha Hb,
then have a β§ b, from and_intro a b Ha Hb,
then have b β§ a, from and_intro b a Hb Ha,
then have H : a β§ b, from and_intro a b Ha Hb,
then have H : a β§ b, from and_intro a b Ha Hb,
Ha
|
67a446e2605ffc36a7455c73350c91cfc6d140a3 | c31182a012eec69da0a1f6c05f42b0f0717d212d | /src/rescale/FiltrationPow.lean | 104d2a74f27d8ecbb66fc61c0a03ed8b7902b46a | [] | no_license | Ja1941/lean-liquid | fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc | 8e80ed0cbdf5145d6814e833a674eaf05a1495c1 | refs/heads/master | 1,689,437,983,362 | 1,628,362,719,000 | 1,628,362,719,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 657 | lean | import rescale.pseudo_normed_group
import pseudo_normed_group.FP
open_locale classical nnreal
open ProFiltPseuNormGrpWithTinv
universe variables u
@[simp] theorem Filtration_rescale (r' c N : ββ₯0) [fact (0 < r')]
(M) [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] :
((Filtration r').obj c).obj (of r' (rescale N M)) =
((Filtration r').obj (c * Nβ»ΒΉ)).obj (of r' M) := rfl
@[simps hom inv]
noncomputable def Filtration_cast_eq (r' cβ cβ : ββ₯0) (h : cβ = cβ) [fact (0 < r')] (M) :
((Filtration r').obj cβ).obj M β
((Filtration r').obj cβ).obj M :=
((Filtration r').map_iso $ category_theory.eq_to_iso h).app M
|
99a73411b7bc9c76d85b41e5f4e2f227871b8237 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Meta/Injective.lean | 14b73888b4c72af4bff6025aa5443898436641d1 | [
"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 | 6,799 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Transform
import Lean.Meta.Tactic.Injection
import Lean.Meta.Tactic.Apply
import Lean.Meta.Tactic.Cases
import Lean.Meta.Tactic.Subst
import Lean.Meta.Tactic.Simp.Types
import Lean.Meta.Tactic.Assumption
namespace Lean.Meta
private def mkAnd? (args : Array Expr) : Option Expr := Id.run <| do
if args.isEmpty then
return none
else
let mut result := args.back
for arg in args.reverse[1:] do
result := mkApp2 (mkConst ``And) arg result
return result
def elimOptParam (type : Expr) : CoreM Expr := do
Core.transform type fun e =>
if e.isAppOfArity ``optParam 2 then
return TransformStep.visit (e.getArg! 0)
else
return TransformStep.visit e
private partial def mkInjectiveTheoremTypeCore? (ctorVal : ConstructorVal) (useEq : Bool) : MetaM (Option Expr) := do
let us := ctorVal.levelParams.map mkLevelParam
let type β elimOptParam ctorVal.type
forallBoundedTelescope type ctorVal.numParams fun params type =>
forallTelescope type fun args1 resultType => do
let jp (args2 args2New : Array Expr) : MetaM (Option Expr) := do
let lhs := mkAppN (mkAppN (mkConst ctorVal.name us) params) args1
let rhs := mkAppN (mkAppN (mkConst ctorVal.name us) params) args2
let eq β mkEq lhs rhs
let mut eqs := #[]
for arg1 in args1, arg2 in args2 do
let arg1Type β inferType arg1
if !(β isProp arg1Type) && arg1 != arg2 then
if (β isDefEq arg1Type (β inferType arg2)) then
eqs := eqs.push (β mkEq arg1 arg2)
else
eqs := eqs.push (β mkHEq arg1 arg2)
if let some andEqs β mkAnd? eqs then
let result β
if useEq then
mkEq eq andEqs
else
mkArrow eq andEqs
mkForallFVars params (β mkForallFVars args1 (β mkForallFVars args2New result))
else
return none
let rec mkArgs2 (i : Nat) (type : Expr) (args2 args2New : Array Expr) : MetaM (Option Expr) := do
if h : i < args1.size then
match (β whnf type) with
| Expr.forallE n d b _ =>
let arg1 := args1.get β¨i, hβ©
if arg1.occurs resultType then
mkArgs2 (i + 1) (b.instantiate1 arg1) (args2.push arg1) args2New
else
withLocalDecl n (if useEq then BinderInfo.default else BinderInfo.implicit) d fun arg2 =>
mkArgs2 (i + 1) (b.instantiate1 arg2) (args2.push arg2) (args2New.push arg2)
| _ => throwError "unexpected constructor type for '{ctorVal.name}'"
else
jp args2 args2New
if useEq then
mkArgs2 0 type #[] #[]
else
withNewBinderInfos (params.map fun param => (param.fvarId!, BinderInfo.implicit)) <|
withNewBinderInfos (args1.map fun arg1 => (arg1.fvarId!, BinderInfo.implicit)) <|
mkArgs2 0 type #[] #[]
private def mkInjectiveTheoremType? (ctorVal : ConstructorVal) : MetaM (Option Expr) :=
mkInjectiveTheoremTypeCore? ctorVal false
private def injTheoremFailureHeader (ctorName : Name) : MessageData :=
m!"failed to prove injectivity theorem for constructor '{ctorName}', use 'set_option genInjectivity false' to disable the generation"
private def throwInjectiveTheoremFailure {Ξ±} (ctorName : Name) (mvarId : MVarId) : MetaM Ξ± :=
throwError "{injTheoremFailureHeader ctorName}{indentD <| MessageData.ofGoal mvarId}"
private def solveEqOfCtorEq (ctorName : Name) (mvarId : MVarId) (h : FVarId) : MetaM Unit := do
match (β injection mvarId h) with
| InjectionResult.solved => unreachable!
| InjectionResult.subgoal mvarId .. =>
(β splitAnd mvarId).forM fun mvarId =>
unless (β assumptionCore mvarId) do
throwInjectiveTheoremFailure ctorName mvarId
private def mkInjectiveTheoremValue (ctorName : Name) (targetType : Expr) : MetaM Expr :=
forallTelescopeReducing targetType fun xs type => do
let mvar β mkFreshExprSyntheticOpaqueMVar type
solveEqOfCtorEq ctorName mvar.mvarId! xs.back.fvarId!
mkLambdaFVars xs mvar
def mkInjectiveTheoremNameFor (ctorName : Name) : Name :=
ctorName ++ `inj
private def mkInjectiveTheorem (ctorVal : ConstructorVal) : MetaM Unit := do
let some type β mkInjectiveTheoremType? ctorVal
| return ()
let value β mkInjectiveTheoremValue ctorVal.name type
addDecl <| Declaration.thmDecl {
name := mkInjectiveTheoremNameFor ctorVal.name
levelParams := ctorVal.levelParams
type := (β instantiateMVars type)
value := (β instantiateMVars value)
}
def mkInjectiveEqTheoremNameFor (ctorName : Name) : Name :=
ctorName ++ `injEq
private def mkInjectiveEqTheoremType? (ctorVal : ConstructorVal) : MetaM (Option Expr) :=
mkInjectiveTheoremTypeCore? ctorVal true
private def mkInjectiveEqTheoremValue (ctorName : Name) (targetType : Expr) : MetaM Expr := do
forallTelescopeReducing targetType fun xs type => do
let mvar β mkFreshExprSyntheticOpaqueMVar type
let [mvarIdβ, mvarIdβ] β apply mvar.mvarId! (mkConst ``Eq.propIntro)
| throwError "unexpected number of subgoals when proving injective theorem for constructor '{ctorName}'"
let (h, mvarIdβ) β intro1 mvarIdβ
let (_, mvarIdβ) β intro1 mvarIdβ
solveEqOfCtorEq ctorName mvarIdβ h
let mvarIdβ β casesAnd mvarIdβ
let mvarIdβ β substEqs mvarIdβ
applyRefl mvarIdβ (injTheoremFailureHeader ctorName)
mkLambdaFVars xs mvar
private def mkInjectiveEqTheorem (ctorVal : ConstructorVal) : MetaM Unit := do
let some type β mkInjectiveEqTheoremType? ctorVal
| return ()
let value β mkInjectiveEqTheoremValue ctorVal.name type
let name := mkInjectiveEqTheoremNameFor ctorVal.name
addDecl <| Declaration.thmDecl {
name
levelParams := ctorVal.levelParams
type := (β instantiateMVars type)
value := (β instantiateMVars value)
}
addSimpLemma (ext := simpExtension) name (post := true) (inv := false) AttributeKind.global (prio := eval_prio default)
register_builtin_option genInjectivity : Bool := {
defValue := true
descr := "generate injectivity theorems for inductive datatype constructors"
}
def mkInjectiveTheorems (declName : Name) : MetaM Unit := do
if (β getEnv).contains ``Eq.propIntro && genInjectivity.get (β getOptions) && !(β isInductivePredicate declName) then
let info β getConstInfoInduct declName
unless info.isUnsafe do
for ctor in info.ctors do
let ctorVal β getConstInfoCtor ctor
if ctorVal.numFields > 0 then
mkInjectiveTheorem ctorVal
mkInjectiveEqTheorem ctorVal
end Lean.Meta
|
9ad3829ddb88cf5af7e7e596d6e0c93162687f7c | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/error_full_names.lean | 8ea3f51965ed2ec5f181e23d835c3dcc6ff1e801 | [
"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 | 166 | lean | namespace foo
open nat
inductive nat : Type | zero, foosucc : nat β nat
#check 0 + nat.zero --error
end foo
namespace foo
#check nat.succ nat.zero --error
end foo
|
928275afb81827f48223eaf900486786ec5b9a1f | a338c3e75cecad4fb8d091bfe505f7399febfd2b | /src/measure_theory/simple_func_dense.lean | 42fef8d33c733e1094922e5384b2e141b5554b17 | [
"Apache-2.0"
] | permissive | bacaimano/mathlib | 88eb7911a9054874fba2a2b74ccd0627c90188af | f2edc5a3529d95699b43514d6feb7eb11608723f | refs/heads/master | 1,686,410,075,833 | 1,625,497,070,000 | 1,625,497,070,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 38,693 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth
-/
import measure_theory.integrable_on
/-!
# Density of simple functions
Show that each Borel measurable function can be approximated,
both pointwise and in `Lα΅` norm, by a sequence of simple functions.
## Main definitions
* `measure_theory.simple_func.nearest_pt (e : β β Ξ±) (N : β) : Ξ± ββ β`: the `simple_func` sending
each `x : Ξ±` to the point `e k` which is the nearest to `x` among `e 0`, ..., `e N`.
* `measure_theory.simple_func.approx_on (f : Ξ² β Ξ±) (hf : measurable f) (s : set Ξ±) (yβ : Ξ±)
(hβ : yβ β s) [separable_space s] (n : β) : Ξ² ββ Ξ±` : a simple function that takes values in `s`
and approximates `f`.
* `measure_theory.L1.simple_func`, the type of `L1` simple functions (notation: `Ξ± βββ[ΞΌ] E`)
* `coe_to_L1`, the embedding of `Ξ± βββ[ΞΌ] E` into `Ξ± ββ[ΞΌ] E`
## Main results
* `tendsto_approx_on` (pointwise convergence): If `f x β s`, then the sequence of simple
approximations `measure_theory.simple_func.approx_on f hf s yβ hβ n`, evaluated at `x`,
tends to `f x` as `n` tends to `β`.
* `tendsto_approx_on_univ_Lp` (Lα΅ convergence): If `E` is a `normed_group` and `f` is measurable
and `mem_βp` (for `p < β`), then the simple functions `simple_func.approx_on f hf s 0 hβ n` may
be considered as elements of `Lp E p ΞΌ`, and they tend in Lα΅ to `f`.
* `tendsto_approx_on_univ_L1` (LΒΉ convergence): If `E` is a `normed_group` and `f` is measurable
and integrable, then the simple functions `simple_func.approx_on f hf s 0 hβ n` may be considered
as elements of `Lp E 1 ΞΌ`, and they tend in LΒΉ to `f`.
* `L1.simple_func.dense_embedding`: the embedding `coe_to_L1` of the `L1` simple functions into
`L1` is dense.
* `integrable.induction`: to prove a predicate for all elements of `L1`, it suffices to check that
it behaves correctly on simple functions in `L1`.
## TODO
For `E` finite-dimensional, simple functions `Ξ± ββ E` are dense in L^β -- prove this.
## Notations
* `Ξ± ββ Ξ²` (local notation): the type of simple functions `Ξ± β Ξ²`.
* `Ξ± βββ[ΞΌ] E`: the type of `L1` simple functions `Ξ± β Ξ²`.
-/
open set function filter topological_space ennreal emetric finset
open_locale classical topological_space ennreal measure_theory big_operators
variables {Ξ± Ξ² ΞΉ E F π : Type*}
noncomputable theory
namespace measure_theory
local infixr ` ββ `:25 := simple_func
namespace simple_func
/-! ### Pointwise approximation by simple functions -/
section pointwise
variables [measurable_space Ξ±] [emetric_space Ξ±] [opens_measurable_space Ξ±]
/-- `nearest_pt_ind e N x` is the index `k` such that `e k` is the nearest point to `x` among the
points `e 0`, ..., `e N`. If more than one point are at the same distance from `x`, then
`nearest_pt_ind e N x` returns the least of their indexes. -/
noncomputable def nearest_pt_ind (e : β β Ξ±) : β β Ξ± ββ β
| 0 := const Ξ± 0
| (N + 1) := piecewise (β k β€ N, {x | edist (e (N + 1)) x < edist (e k) x})
(measurable_set.Inter $ Ξ» k, measurable_set.Inter_Prop $ Ξ» hk,
measurable_set_lt measurable_edist_right measurable_edist_right)
(const Ξ± $ N + 1) (nearest_pt_ind N)
/-- `nearest_pt e N x` is the nearest point to `x` among the points `e 0`, ..., `e N`. If more than
one point are at the same distance from `x`, then `nearest_pt e N x` returns the point with the
least possible index. -/
noncomputable def nearest_pt (e : β β Ξ±) (N : β) : Ξ± ββ Ξ± :=
(nearest_pt_ind e N).map e
@[simp] lemma nearest_pt_ind_zero (e : β β Ξ±) : nearest_pt_ind e 0 = const Ξ± 0 := rfl
@[simp] lemma nearest_pt_zero (e : β β Ξ±) : nearest_pt e 0 = const Ξ± (e 0) := rfl
lemma nearest_pt_ind_succ (e : β β Ξ±) (N : β) (x : Ξ±) :
nearest_pt_ind e (N + 1) x =
if β k β€ N, edist (e (N + 1)) x < edist (e k) x
then N + 1 else nearest_pt_ind e N x :=
by { simp only [nearest_pt_ind, coe_piecewise, set.piecewise], congr, simp }
lemma nearest_pt_ind_le (e : β β Ξ±) (N : β) (x : Ξ±) : nearest_pt_ind e N x β€ N :=
begin
induction N with N ihN, { simp },
simp only [nearest_pt_ind_succ],
split_ifs,
exacts [le_rfl, ihN.trans N.le_succ]
end
lemma edist_nearest_pt_le (e : β β Ξ±) (x : Ξ±) {k N : β} (hk : k β€ N) :
edist (nearest_pt e N x) x β€ edist (e k) x :=
begin
induction N with N ihN generalizing k,
{ simp [nonpos_iff_eq_zero.1 hk, le_refl] },
{ simp only [nearest_pt, nearest_pt_ind_succ, map_apply],
split_ifs,
{ rcases hk.eq_or_lt with rfl|hk,
exacts [le_rfl, (h k (nat.lt_succ_iff.1 hk)).le] },
{ push_neg at h,
rcases h with β¨l, hlN, hxlβ©,
rcases hk.eq_or_lt with rfl|hk,
exacts [(ihN hlN).trans hxl, ihN (nat.lt_succ_iff.1 hk)] } }
end
lemma tendsto_nearest_pt {e : β β Ξ±} {x : Ξ±} (hx : x β closure (range e)) :
tendsto (Ξ» N, nearest_pt e N x) at_top (π x) :=
begin
refine (at_top_basis.tendsto_iff nhds_basis_eball).2 (Ξ» Ξ΅ hΞ΅, _),
rcases emetric.mem_closure_iff.1 hx Ξ΅ hΞ΅ with β¨_, β¨N, rflβ©, hNβ©,
rw [edist_comm] at hN,
exact β¨N, trivial, Ξ» n hn, (edist_nearest_pt_le e x hn).trans_lt hNβ©
end
variables [measurable_space Ξ²] {f : Ξ² β Ξ±}
/-- Approximate a measurable function by a sequence of simple functions `F n` such that
`F n x β s`. -/
noncomputable def approx_on (f : Ξ² β Ξ±) (hf : measurable f) (s : set Ξ±) (yβ : Ξ±) (hβ : yβ β s)
[separable_space s] (n : β) :
Ξ² ββ Ξ± :=
by haveI : nonempty s := β¨β¨yβ, hββ©β©;
exact comp (nearest_pt (Ξ» k, nat.cases_on k yβ (coe β dense_seq s) : β β Ξ±) n) f hf
@[simp] lemma approx_on_zero {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s)
[separable_space s] (x : Ξ²) :
approx_on f hf s yβ hβ 0 x = yβ :=
rfl
lemma approx_on_mem {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s)
[separable_space s] (n : β) (x : Ξ²) :
approx_on f hf s yβ hβ n x β s :=
begin
haveI : nonempty s := β¨β¨yβ, hββ©β©,
suffices : β n, (nat.cases_on n yβ (coe β dense_seq s) : Ξ±) β s, { apply this },
rintro (_|n),
exacts [hβ, subtype.mem _]
end
@[simp] lemma approx_on_comp {Ξ³ : Type*} [measurable_space Ξ³] {f : Ξ² β Ξ±} (hf : measurable f)
{g : Ξ³ β Ξ²} (hg : measurable g) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s) [separable_space s] (n : β) :
approx_on (f β g) (hf.comp hg) s yβ hβ n = (approx_on f hf s yβ hβ n).comp g hg :=
rfl
lemma tendsto_approx_on {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s)
[separable_space s] {x : Ξ²} (hx : f x β closure s) :
tendsto (Ξ» n, approx_on f hf s yβ hβ n x) at_top (π $ f x) :=
begin
haveI : nonempty s := β¨β¨yβ, hββ©β©,
rw [β @subtype.range_coe _ s, β image_univ, β (dense_range_dense_seq s).closure_eq] at hx,
simp only [approx_on, coe_comp],
refine tendsto_nearest_pt (closure_minimal _ is_closed_closure hx),
simp only [nat.range_cases_on, closure_union, range_comp coe],
exact subset.trans (image_closure_subset_closure_image continuous_subtype_coe)
(subset_union_right _ _)
end
lemma edist_approx_on_le {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s)
[separable_space s] (x : Ξ²) (n : β) :
edist (approx_on f hf s yβ hβ n x) (f x) β€ edist yβ (f x) :=
begin
dsimp only [approx_on, coe_comp, (β)],
exact edist_nearest_pt_le _ _ (zero_le _)
end
lemma edist_approx_on_y0_le {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} {yβ : Ξ±} (hβ : yβ β s)
[separable_space s] (x : Ξ²) (n : β) :
edist yβ (approx_on f hf s yβ hβ n x) β€ edist yβ (f x) + edist yβ (f x) :=
calc edist yβ (approx_on f hf s yβ hβ n x) β€
edist yβ (f x) + edist (approx_on f hf s yβ hβ n x) (f x) : edist_triangle_right _ _ _
... β€ edist yβ (f x) + edist yβ (f x) : add_le_add_left (edist_approx_on_le hf hβ x n) _
end pointwise
/-! ### Lp approximation by simple functions -/
section Lp
variables [measurable_space Ξ²]
variables [measurable_space E] [normed_group E] {q : β} {p : ββ₯0β}
lemma nnnorm_approx_on_le [opens_measurable_space E] {f : Ξ² β E} (hf : measurable f)
{s : set E} {yβ : E} (hβ : yβ β s) [separable_space s] (x : Ξ²) (n : β) :
β₯approx_on f hf s yβ hβ n x - f xβ₯β β€ β₯f x - yββ₯β :=
begin
have := edist_approx_on_le hf hβ x n,
rw edist_comm yβ at this,
simp only [edist_nndist, nndist_eq_nnnorm] at this,
exact_mod_cast this
end
lemma norm_approx_on_yβ_le [opens_measurable_space E] {f : Ξ² β E} (hf : measurable f)
{s : set E} {yβ : E} (hβ : yβ β s) [separable_space s] (x : Ξ²) (n : β) :
β₯approx_on f hf s yβ hβ n x - yββ₯ β€ β₯f x - yββ₯ + β₯f x - yββ₯ :=
begin
have := edist_approx_on_y0_le hf hβ x n,
repeat { rw [edist_comm yβ, edist_eq_coe_nnnorm_sub] at this },
exact_mod_cast this,
end
lemma norm_approx_on_zero_le [opens_measurable_space E] {f : Ξ² β E} (hf : measurable f)
{s : set E} (hβ : (0 : E) β s) [separable_space s] (x : Ξ²) (n : β) :
β₯approx_on f hf s 0 hβ n xβ₯ β€ β₯f xβ₯ + β₯f xβ₯ :=
begin
have := edist_approx_on_y0_le hf hβ x n,
simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this,
exact_mod_cast this,
end
lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E]
{f : Ξ² β E} (hf : measurable f) {s : set E} {yβ : E} (hβ : yβ β s) [separable_space s]
(hp_ne_top : p β β) {ΞΌ : measure Ξ²} (hΞΌ : βα΅ x βΞΌ, f x β closure s)
(hi : snorm (Ξ» x, f x - yβ) p ΞΌ < β) :
tendsto (Ξ» n, snorm (approx_on f hf s yβ hβ n - f) p ΞΌ) at_top (π 0) :=
begin
by_cases hp_zero : p = 0,
{ simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds },
have hp : 0 < p.to_real := to_real_pos_iff.mpr β¨bot_lt_iff_ne_bot.mpr hp_zero, hp_ne_topβ©,
suffices : tendsto (Ξ» n, β«β» x, β₯approx_on f hf s yβ hβ n x - f xβ₯β ^ p.to_real βΞΌ) at_top (π 0),
{ simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top],
convert continuous_rpow_const.continuous_at.tendsto.comp this;
simp [_root_.inv_pos.mpr hp] },
-- We simply check the conditions of the Dominated Convergence Theorem:
-- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable
have hF_meas : β n, measurable (Ξ» x, (β₯approx_on f hf s yβ hβ n x - f xβ₯β : ββ₯0β) ^ p.to_real),
{ simpa only [β edist_eq_coe_nnnorm_sub] using
Ξ» n, (approx_on f hf s yβ hβ n).measurable_bind (Ξ» y x, (edist y (f x)) ^ p.to_real)
(Ξ» y, (measurable_edist_right.comp hf).pow_const p.to_real) },
-- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly
-- bounded, at any given point, by `Ξ» x, β₯f x - yββ₯ ^ p.to_real`
have h_bound : β n, (Ξ» x, (β₯approx_on f hf s yβ hβ n x - f xβ₯β : ββ₯0β) ^ p.to_real)
β€α΅[ΞΌ] (Ξ» x, β₯f x - yββ₯β ^ p.to_real),
{ exact Ξ» n, eventually_of_forall
(Ξ» x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf hβ x n)) to_real_nonneg) },
-- (3) The bounding function `Ξ» x, β₯f x - yββ₯ ^ p.to_real` has finite integral
have h_fin : β«β» (a : Ξ²), β₯f a - yββ₯β ^ p.to_real βΞΌ < β€,
{ exact lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi },
-- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise
-- to zero
have h_lim : βα΅ (a : Ξ²) βΞΌ,
tendsto (Ξ» n, (β₯approx_on f hf s yβ hβ n a - f aβ₯β : ββ₯0β) ^ p.to_real) at_top (π 0),
{ filter_upwards [hΞΌ],
intros a ha,
have : tendsto (Ξ» n, (approx_on f hf s yβ hβ n) a - f a) at_top (π (f a - f a)),
{ exact (tendsto_approx_on hf hβ ha).sub tendsto_const_nhds },
convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm),
simp [zero_rpow_of_pos hp] },
-- Then we apply the Dominated Convergence Theorem
simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim,
end
lemma mem_βp_approx_on [borel_space E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_βp f p ΞΌ) {s : set E} {yβ : E}
(hβ : yβ β s) [separable_space s] (hiβ : mem_βp (Ξ» x, yβ) p ΞΌ) (n : β) :
mem_βp (approx_on f fmeas s yβ hβ n) p ΞΌ :=
begin
refine β¨(approx_on f fmeas s yβ hβ n).ae_measurable, _β©,
suffices : snorm (Ξ» x, approx_on f fmeas s yβ hβ n x - yβ) p ΞΌ < β€,
{ have : mem_βp (Ξ» x, approx_on f fmeas s yβ hβ n x - yβ) p ΞΌ :=
β¨(approx_on f fmeas s yβ hβ n - const Ξ² yβ).ae_measurable, thisβ©,
convert snorm_add_lt_top this hiβ,
ext x,
simp },
-- We don't necessarily have `mem_βp (Ξ» x, f x - yβ) p ΞΌ`, because the `ae_measurable` part
-- requires `ae_measurable.add`, which requires second-countability
have hf' : mem_βp (Ξ» x, β₯f x - yββ₯) p ΞΌ,
{ have h_meas : measurable (Ξ» x, β₯f x - yββ₯),
{ simp only [β dist_eq_norm],
exact (continuous_id.dist continuous_const).measurable.comp fmeas },
refine β¨h_meas.ae_measurable, _β©,
rw snorm_norm,
convert snorm_add_lt_top hf hiβ.neg,
ext x,
simp [sub_eq_add_neg] },
have : βα΅ x βΞΌ, β₯approx_on f fmeas s yβ hβ n x - yββ₯ β€ β₯(β₯f x - yββ₯ + β₯f x - yββ₯)β₯,
{ refine eventually_of_forall _,
intros x,
convert norm_approx_on_yβ_le fmeas hβ x n,
rw [real.norm_eq_abs, abs_of_nonneg],
exact add_nonneg (norm_nonneg _) (norm_nonneg _) },
calc snorm (Ξ» x, approx_on f fmeas s yβ hβ n x - yβ) p ΞΌ
β€ snorm (Ξ» x, β₯f x - yββ₯ + β₯f x - yββ₯) p ΞΌ : snorm_mono_ae this
... < β€ : snorm_add_lt_top hf' hf',
end
lemma tendsto_approx_on_univ_Lp_snorm [opens_measurable_space E] [second_countable_topology E]
{f : Ξ² β E} (hp_ne_top : p β β) {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : snorm f p ΞΌ < β) :
tendsto (Ξ» n, snorm (approx_on f fmeas univ 0 trivial n - f) p ΞΌ) at_top (π 0) :=
tendsto_approx_on_Lp_snorm fmeas trivial hp_ne_top (by simp) (by simpa using hf)
lemma mem_βp_approx_on_univ [borel_space E] [second_countable_topology E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_βp f p ΞΌ) (n : β) :
mem_βp (approx_on f fmeas univ 0 trivial n) p ΞΌ :=
mem_βp_approx_on fmeas hf (mem_univ _) zero_mem_βp n
lemma tendsto_approx_on_univ_Lp [borel_space E] [second_countable_topology E]
{f : Ξ² β E} [hp : fact (1 β€ p)] (hp_ne_top : p β β) {ΞΌ : measure Ξ²} (fmeas : measurable f)
(hf : mem_βp f p ΞΌ) :
tendsto (Ξ» n, (mem_βp_approx_on_univ fmeas hf n).to_Lp (approx_on f fmeas univ 0 trivial n))
at_top (π (hf.to_Lp f)) :=
by simp [Lp.tendsto_Lp_iff_tendsto_βp'', tendsto_approx_on_univ_Lp_snorm hp_ne_top fmeas hf.2]
end Lp
/-! ### L1 approximation by simple functions -/
section integrable
variables [measurable_space Ξ²]
variables [measurable_space E] [normed_group E]
lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E]
{f : Ξ² β E} (hf : measurable f) {s : set E} {yβ : E} (hβ : yβ β s) [separable_space s]
{ΞΌ : measure Ξ²} (hΞΌ : βα΅ x βΞΌ, f x β closure s) (hi : has_finite_integral (Ξ» x, f x - yβ) ΞΌ) :
tendsto (Ξ» n, β«β» x, β₯approx_on f hf s yβ hβ n x - f xβ₯β βΞΌ) at_top (π 0) :=
by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf hβ one_ne_top hΞΌ
(by simpa [snorm_one_eq_lintegral_nnnorm] using hi)
lemma integrable_approx_on [borel_space E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ)
{s : set E} {yβ : E} (hβ : yβ β s)
[separable_space s] (hiβ : integrable (Ξ» x, yβ) ΞΌ) (n : β) :
integrable (approx_on f fmeas s yβ hβ n) ΞΌ :=
begin
rw β mem_βp_one_iff_integrable at hf hiβ β’,
exact mem_βp_approx_on fmeas hf hβ hiβ n,
end
lemma tendsto_approx_on_univ_L1_nnnorm [opens_measurable_space E] [second_countable_topology E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) :
tendsto (Ξ» n, β«β» x, β₯approx_on f fmeas univ 0 trivial n x - f xβ₯β βΞΌ) at_top (π 0) :=
tendsto_approx_on_L1_nnnorm fmeas trivial (by simp) (by simpa using hf.2)
lemma integrable_approx_on_univ [borel_space E] [second_countable_topology E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) (n : β) :
integrable (approx_on f fmeas univ 0 trivial n) ΞΌ :=
integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n
local attribute [instance] fact_one_le_one_ennreal
lemma tendsto_approx_on_univ_L1 [borel_space E] [second_countable_topology E]
{f : Ξ² β E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) :
tendsto (Ξ» n, integrable.to_L1 (approx_on f fmeas univ 0 trivial n)
(integrable_approx_on_univ fmeas hf n)) at_top (π $ hf.to_L1 f) :=
tendsto_approx_on_univ_Lp one_ne_top fmeas _
end integrable
section simple_func_properties
variables [measurable_space Ξ±]
variables [normed_group E] [measurable_space E] [normed_group F]
variables {ΞΌ : measure Ξ±} {p : ββ₯0β}
/-!
### Properties of simple functions in `Lp` spaces
A simple function `f : Ξ± ββ E` into a normed group `E` verifies, for a measure `ΞΌ`:
- `mem_βp f 0 ΞΌ` and `mem_βp f β ΞΌ`, since `f` is a.e.-measurable and bounded,
- for `0 < p < β`, `mem_βp f p ΞΌ β integrable f ΞΌ β f.fin_meas_supp ΞΌ β β y β 0, ΞΌ (f β»ΒΉ' {y}) < β`.
-/
lemma exists_forall_norm_le (f : Ξ± ββ F) : β C, β x, β₯f xβ₯ β€ C :=
exists_forall_le (f.map (Ξ» x, β₯xβ₯))
lemma mem_βp_zero (f : Ξ± ββ E) (ΞΌ : measure Ξ±) : mem_βp f 0 ΞΌ :=
mem_βp_zero_iff_ae_measurable.mpr f.ae_measurable
lemma mem_βp_top (f : Ξ± ββ E) (ΞΌ : measure Ξ±) : mem_βp f β ΞΌ :=
let β¨C, hfCβ© := f.exists_forall_norm_le in
mem_βp_top_of_bound f.ae_measurable C $ eventually_of_forall hfC
protected lemma snorm'_eq {p : β} (f : Ξ± ββ F) (ΞΌ : measure Ξ±) :
snorm' f p ΞΌ = (β y in f.range, (nnnorm y : ββ₯0β) ^ p * ΞΌ (f β»ΒΉ' {y})) ^ (1/p) :=
have h_map : (Ξ» a, (nnnorm (f a) : ββ₯0β) ^ p) = f.map (Ξ» a : F, (nnnorm a : ββ₯0β) ^ p), by simp,
by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral]
lemma measure_preimage_lt_top_of_mem_βp (hp_pos : 0 < p) (hp_ne_top : p β β) (f : Ξ± ββ E)
(hf : mem_βp f p ΞΌ) (y : E) (hy_ne : y β 0) :
ΞΌ (f β»ΒΉ' {y}) < β :=
begin
have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos_iff.mpr β¨hp_pos, hp_ne_topβ©,
have hf_snorm := mem_βp.snorm_lt_top hf,
rw [snorm_eq_snorm' hp_pos.ne.symm hp_ne_top, f.snorm'_eq,
β @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]),
@ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]),
ennreal.sum_lt_top_iff] at hf_snorm,
by_cases hyf : y β f.range,
swap,
{ suffices h_empty : f β»ΒΉ' {y} = β
,
by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, },
ext1 x,
rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false],
refine Ξ» hxy, hyf _,
rw [mem_range, set.mem_range],
exact β¨x, hxyβ©, },
specialize hf_snorm y hyf,
rw ennreal.mul_lt_top_iff at hf_snorm,
cases hf_snorm,
{ exact hf_snorm.2, },
cases hf_snorm,
{ refine absurd _ hy_ne,
simpa [hp_pos_real] using hf_snorm, },
{ simp [hf_snorm], },
end
lemma mem_βp_of_finite_measure_preimage (p : ββ₯0β) {f : Ξ± ββ E} (hf : β y β 0, ΞΌ (f β»ΒΉ' {y}) < β) :
mem_βp f p ΞΌ :=
begin
by_cases hp0 : p = 0,
{ rw [hp0, mem_βp_zero_iff_ae_measurable], exact f.ae_measurable, },
by_cases hp_top : p = β,
{ rw hp_top, exact mem_βp_top f ΞΌ, },
refine β¨f.ae_measurable, _β©,
rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq],
refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (Ξ» y hy, _)).ne,
by_cases hy0 : y = 0,
{ simp [hy0, ennreal.to_real_pos_iff.mpr β¨lt_of_le_of_ne (zero_le _) (ne.symm hp0), hp_topβ©], },
{ refine ennreal.mul_lt_top _ (hf y hy0),
exact ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top, },
end
lemma mem_βp_iff {f : Ξ± ββ E} (hp_pos : 0 < p) (hp_ne_top : p β β) :
mem_βp f p ΞΌ β β y β 0, ΞΌ (f β»ΒΉ' {y}) < β :=
β¨Ξ» h, measure_preimage_lt_top_of_mem_βp hp_pos hp_ne_top f h,
Ξ» h, mem_βp_of_finite_measure_preimage p hβ©
lemma integrable_iff {f : Ξ± ββ E} : integrable f ΞΌ β β y β 0, ΞΌ (f β»ΒΉ' {y}) < β :=
mem_βp_one_iff_integrable.symm.trans $ mem_βp_iff ennreal.zero_lt_one ennreal.coe_ne_top
lemma mem_βp_iff_integrable {f : Ξ± ββ E} (hp_pos : 0 < p) (hp_ne_top : p β β) :
mem_βp f p ΞΌ β integrable f ΞΌ :=
(mem_βp_iff hp_pos hp_ne_top).trans integrable_iff.symm
lemma mem_βp_iff_fin_meas_supp {f : Ξ± ββ E} (hp_pos : 0 < p) (hp_ne_top : p β β) :
mem_βp f p ΞΌ β f.fin_meas_supp ΞΌ :=
(mem_βp_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm
lemma integrable_iff_fin_meas_supp {f : Ξ± ββ E} : integrable f ΞΌ β f.fin_meas_supp ΞΌ :=
integrable_iff.trans fin_meas_supp_iff.symm
lemma fin_meas_supp.integrable {f : Ξ± ββ E} (h : f.fin_meas_supp ΞΌ) : integrable f ΞΌ :=
integrable_iff_fin_meas_supp.2 h
lemma integrable_pair [measurable_space F] {f : Ξ± ββ E} {g : Ξ± ββ F} :
integrable f ΞΌ β integrable g ΞΌ β integrable (pair f g) ΞΌ :=
by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair
lemma mem_βp_of_finite_measure (f : Ξ± ββ E) (p : ββ₯0β) (ΞΌ : measure Ξ±) [finite_measure ΞΌ] :
mem_βp f p ΞΌ :=
let β¨C, hfCβ© := f.exists_forall_norm_le in
mem_βp.of_bound f.ae_measurable C $ eventually_of_forall hfC
lemma integrable_of_finite_measure [finite_measure ΞΌ] (f : Ξ± ββ E) : integrable f ΞΌ :=
mem_βp_one_iff_integrable.mp (f.mem_βp_of_finite_measure 1 ΞΌ)
lemma measure_preimage_lt_top_of_integrable (f : Ξ± ββ E) (hf : integrable f ΞΌ) {x : E}
(hx : x β 0) :
ΞΌ (f β»ΒΉ' {x}) < β :=
integrable_iff.mp hf x hx
end simple_func_properties
end simple_func
/-! Construction of the space of `L1` simple functions, and its dense embedding into `L1`. -/
namespace L1
open ae_eq_fun
variables
[measurable_space Ξ±]
[normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E]
[normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F]
{ΞΌ : measure Ξ±}
variables (Ξ± E ΞΌ)
/-- `L1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple
function. -/
def simple_func : add_subgroup (Lp E 1 ΞΌ) :=
{ carrier := {f : Ξ± ββ[ΞΌ] E | β (s : Ξ± ββ E), (ae_eq_fun.mk s s.ae_measurable : Ξ± ββ[ΞΌ] E) = f},
zero_mem' := β¨0, rflβ©,
add_mem' := Ξ» f g β¨s, hsβ© β¨t, htβ©, β¨s + t,
by simp only [βhs, βht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]β©,
neg_mem' := Ξ» f β¨s, hsβ©, β¨-s,
by simp only [βhs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]β© }
variables {Ξ± E ΞΌ}
notation Ξ± ` βββ[`:25 ΞΌ `] ` E := measure_theory.L1.simple_func Ξ± E ΞΌ
namespace simple_func
section instances
/-! Simple functions in L1 space form a `normed_space`. -/
instance : has_coe (Ξ± βββ[ΞΌ] E) (Ξ± ββ[ΞΌ] E) := coe_subtype
instance : has_coe_to_fun (Ξ± βββ[ΞΌ] E) := β¨Ξ» f, Ξ± β E, Ξ» f, β(f : Ξ± ββ[ΞΌ] E)β©
@[simp, norm_cast] lemma coe_coe (f : Ξ± βββ[ΞΌ] E) : β(f : Ξ± ββ[ΞΌ] E) = f := rfl
protected lemma eq {f g : Ξ± βββ[ΞΌ] E} : (f : Ξ± ββ[ΞΌ] E) = (g : Ξ± ββ[ΞΌ] E) β f = g := subtype.eq
protected lemma eq' {f g : Ξ± βββ[ΞΌ] E} : (f : Ξ± ββ[ΞΌ] E) = (g : Ξ± ββ[ΞΌ] E) β f = g :=
subtype.eq β subtype.eq
@[norm_cast] protected lemma eq_iff {f g : Ξ± βββ[ΞΌ] E} : (f : Ξ± ββ[ΞΌ] E) = g β f = g :=
subtype.ext_iff.symm
@[norm_cast] protected lemma eq_iff' {f g : Ξ± βββ[ΞΌ] E} : (f : Ξ± ββ[ΞΌ] E) = g β f = g :=
iff.intro (simple_func.eq') (congr_arg _)
/-- L1 simple functions forms a `normed_group`, with the metric being inherited from L1 space,
i.e., `dist f g = ennreal.to_real (β«β» a, edist (f a) (g a)`).
Not declared as an instance as `Ξ± βββ[ΞΌ] Ξ²` will only be useful in the construction of the Bochner
integral. -/
protected def normed_group : normed_group (Ξ± βββ[ΞΌ] E) := by apply_instance
local attribute [instance] simple_func.normed_group
/-- Functions `Ξ± βββ[ΞΌ] E` form an additive commutative group. -/
instance : inhabited (Ξ± βββ[ΞΌ] E) := β¨0β©
@[simp, norm_cast]
lemma coe_zero : ((0 : Ξ± βββ[ΞΌ] E) : Ξ± ββ[ΞΌ] E) = 0 := rfl
@[simp, norm_cast]
lemma coe_add (f g : Ξ± βββ[ΞΌ] E) : ((f + g : Ξ± βββ[ΞΌ] E) : Ξ± ββ[ΞΌ] E) = f + g := rfl
@[simp, norm_cast]
lemma coe_neg (f : Ξ± βββ[ΞΌ] E) : ((-f : Ξ± βββ[ΞΌ] E) : Ξ± ββ[ΞΌ] E) = -f := rfl
@[simp, norm_cast]
lemma coe_sub (f g : Ξ± βββ[ΞΌ] E) : ((f - g : Ξ± βββ[ΞΌ] E) : Ξ± ββ[ΞΌ] E) = f - g := rfl
@[simp] lemma edist_eq (f g : Ξ± βββ[ΞΌ] E) : edist f g = edist (f : Ξ± ββ[ΞΌ] E) (g : Ξ± ββ[ΞΌ] E) := rfl
@[simp] lemma dist_eq (f g : Ξ± βββ[ΞΌ] E) : dist f g = dist (f : Ξ± ββ[ΞΌ] E) (g : Ξ± ββ[ΞΌ] E) := rfl
lemma norm_eq (f : Ξ± βββ[ΞΌ] E) : β₯fβ₯ = β₯(f : Ξ± ββ[ΞΌ] E)β₯ := rfl
variables [normed_field π] [normed_space π E] [measurable_space π] [opens_measurable_space π]
/-- Not declared as an instance as `Ξ± βββ[ΞΌ] E` will only be useful in the construction of the
Bochner integral. -/
protected def has_scalar : has_scalar π (Ξ± βββ[ΞΌ] E) := β¨Ξ»k f, β¨k β’ f,
begin
rcases f with β¨f, β¨s, hsβ©β©,
use k β’ s,
apply eq.trans (smul_mk k s s.ae_measurable).symm _,
rw hs,
refl,
end β©β©
local attribute [instance, priority 10000] simple_func.has_scalar
@[simp, norm_cast] lemma coe_smul (c : π) (f : Ξ± βββ[ΞΌ] E) :
((c β’ f : Ξ± βββ[ΞΌ] E) : Ξ± ββ[ΞΌ] E) = c β’ (f : Ξ± ββ[ΞΌ] E) := rfl
/-- Not declared as an instance as `Ξ± βββ[ΞΌ] E` will only be useful in the construction of the
Bochner integral. -/
protected def module : module π (Ξ± βββ[ΞΌ] E) :=
{ one_smul := Ξ»f, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }),
mul_smul := Ξ»x y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }),
smul_add := Ξ»x f g, simple_func.eq (by { simp only [coe_smul], exact smul_add _ _ _ }),
smul_zero := Ξ»x, simple_func.eq (by { simp only [coe_smul], exact smul_zero _ }),
add_smul := Ξ»x y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }),
zero_smul := Ξ»f, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) }
local attribute [instance] simple_func.normed_group simple_func.module
/-- Not declared as an instance as `Ξ± βββ[ΞΌ] E` will only be useful in the construction of the
Bochner integral. -/
protected def normed_space : normed_space π (Ξ± βββ[ΞΌ] E) :=
β¨ Ξ»c f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } β©
end instances
local attribute [instance] simple_func.normed_group simple_func.normed_space
section to_L1
/-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/
@[reducible] def to_L1 (f : Ξ± ββ E) (hf : integrable f ΞΌ) : (Ξ± βββ[ΞΌ] E) :=
β¨hf.to_L1 f, β¨f, rflβ©β©
lemma to_L1_eq_to_L1 (f : Ξ± ββ E) (hf : integrable f ΞΌ) :
(to_L1 f hf : Ξ± ββ[ΞΌ] E) = hf.to_L1 f := rfl
lemma to_L1_eq_mk (f : Ξ± ββ E) (hf : integrable f ΞΌ) :
(to_L1 f hf : Ξ± ββ[ΞΌ] E) = ae_eq_fun.mk f f.ae_measurable := rfl
lemma to_L1_zero : to_L1 (0 : Ξ± ββ E) (integrable_zero Ξ± E ΞΌ) = 0 := rfl
lemma to_L1_add (f g : Ξ± ββ E) (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl
lemma to_L1_neg (f : Ξ± ββ E) (hf : integrable f ΞΌ) :
to_L1 (-f) hf.neg = -to_L1 f hf := rfl
lemma to_L1_sub (f g : Ξ± ββ E) (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) :
to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg :=
by { simp only [sub_eq_add_neg, β to_L1_neg, β to_L1_add], refl }
variables [normed_field π] [normed_space π E] [measurable_space π] [opens_measurable_space π]
lemma to_L1_smul (f : Ξ± ββ E) (hf : integrable f ΞΌ) (c : π) :
to_L1 (c β’ f) (hf.smul c) = c β’ to_L1 f hf := rfl
lemma norm_to_L1 (f : Ξ± ββ E) (hf : integrable f ΞΌ) :
β₯to_L1 f hfβ₯ = ennreal.to_real (β«β» a, edist (f a) 0 βΞΌ) :=
by simp [to_L1, integrable.norm_to_L1]
end to_L1
section to_simple_func
/-- Find a representative of a `L1.simple_func`. -/
def to_simple_func (f : Ξ± βββ[ΞΌ] E) : Ξ± ββ E := classical.some f.2
/-- `(to_simple_func f)` is measurable. -/
@[measurability]
protected lemma measurable (f : Ξ± βββ[ΞΌ] E) : measurable (to_simple_func f) :=
(to_simple_func f).measurable
@[measurability]
protected lemma ae_measurable (f : Ξ± βββ[ΞΌ] E) : ae_measurable (to_simple_func f) ΞΌ :=
(simple_func.measurable f).ae_measurable
/-- `to_simple_func f` is integrable. -/
protected lemma integrable (f : Ξ± βββ[ΞΌ] E) : integrable (to_simple_func f) ΞΌ :=
begin
apply (integrable_mk (simple_func.ae_measurable f)).1,
convert integrable_coe_fn f.val,
exact classical.some_spec f.2
end
lemma to_L1_to_simple_func (f : Ξ± βββ[ΞΌ] E) :
to_L1 (to_simple_func f) (simple_func.integrable f) = f :=
by { rw β simple_func.eq_iff', exact classical.some_spec f.2 }
lemma to_simple_func_to_L1 (f : Ξ± ββ E) (hfi : integrable f ΞΌ) :
to_simple_func (to_L1 f hfi) =α΅[ΞΌ] f :=
by { rw β mk_eq_mk, exact classical.some_spec (to_L1 f hfi).2 }
lemma to_simple_func_eq_to_fun (f : Ξ± βββ[ΞΌ] E) : to_simple_func f =α΅[ΞΌ] f :=
begin
simp_rw [β integrable.to_L1_eq_to_L1_iff (to_simple_func f) f (simple_func.integrable f)
(integrable_coe_fn βf), subtype.ext_iff],
convert classical.some_spec f.coe_prop,
exact integrable.to_L1_coe_fn _ _,
end
variables (E ΞΌ)
lemma zero_to_simple_func : to_simple_func (0 : Ξ± βββ[ΞΌ] E) =α΅[ΞΌ] 0 :=
begin
filter_upwards [to_simple_func_eq_to_fun (0 : Ξ± βββ[ΞΌ] E), Lp.coe_fn_zero E 1 ΞΌ],
assume a hβ hβ,
rwa hβ,
end
variables {E ΞΌ}
lemma add_to_simple_func (f g : Ξ± βββ[ΞΌ] E) :
to_simple_func (f + g) =α΅[ΞΌ] to_simple_func f + to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Ξ± ββ[ΞΌ] E) g],
assume a,
simp only [β coe_coe, coe_add, pi.add_apply],
iterate 4 { assume h, rw h }
end
lemma neg_to_simple_func (f : Ξ± βββ[ΞΌ] E) : to_simple_func (-f) =α΅[ΞΌ] - to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f,
Lp.coe_fn_neg (f : Ξ± ββ[ΞΌ] E)],
assume a,
simp only [pi.neg_apply, coe_neg, β coe_coe],
repeat { assume h, rw h }
end
lemma sub_to_simple_func (f g : Ξ± βββ[ΞΌ] E) :
to_simple_func (f - g) =α΅[ΞΌ] to_simple_func f - to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Ξ± ββ[ΞΌ] E) g],
assume a,
simp only [coe_sub, pi.sub_apply, β coe_coe],
repeat { assume h, rw h }
end
variables [normed_field π] [normed_space π E] [measurable_space π] [opens_measurable_space π]
lemma smul_to_simple_func (k : π) (f : Ξ± βββ[ΞΌ] E) :
to_simple_func (k β’ f) =α΅[ΞΌ] k β’ to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (k β’ f), to_simple_func_eq_to_fun f,
Lp.coe_fn_smul k (f : Ξ± ββ[ΞΌ] E)],
assume a,
simp only [pi.smul_apply, coe_smul, β coe_coe],
repeat { assume h, rw h }
end
lemma lintegral_edist_to_simple_func_lt_top (f g : Ξ± βββ[ΞΌ] E) :
β«β» (x : Ξ±), edist (to_simple_func f x) (to_simple_func g x) βΞΌ < β :=
begin
rw lintegral_rwβ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g),
exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _)
end
lemma dist_to_simple_func (f g : Ξ± βββ[ΞΌ] E) : dist f g =
ennreal.to_real (β«β» x, edist (to_simple_func f x) (to_simple_func g x) βΞΌ) :=
begin
rw [dist_eq, L1.dist_def, ennreal.to_real_eq_to_real],
{ rw lintegral_rwβ, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } },
{ exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) },
{ exact lintegral_edist_to_simple_func_lt_top _ _ }
end
lemma norm_to_simple_func (f : Ξ± βββ[ΞΌ] E) :
β₯fβ₯ = ennreal.to_real (β«β» (a : Ξ±), nnnorm ((to_simple_func f) a) βΞΌ) :=
calc β₯fβ₯ =
ennreal.to_real (β«β»x, edist ((to_simple_func f) x) (to_simple_func (0 : Ξ± βββ[ΞΌ] E) x) βΞΌ) :
begin
rw [β dist_zero_right, dist_to_simple_func]
end
... = ennreal.to_real (β«β» (x : Ξ±), (coe β nnnorm) ((to_simple_func f) x) βΞΌ) :
begin
rw lintegral_nnnorm_eq_lintegral_edist,
have : β«β» x, edist ((to_simple_func f) x) ((to_simple_func (0 : Ξ± βββ[ΞΌ] E)) x) βΞΌ =
β«β» x, edist ((to_simple_func f) x) 0 βΞΌ,
{ refine lintegral_congr_ae ((zero_to_simple_func E ΞΌ).mono (Ξ» a h, _)),
rw [h, pi.zero_apply] },
rw [ennreal.to_real_eq_to_real],
{ exact this },
{ exact lintegral_edist_to_simple_func_lt_top _ _ },
{ rw β this, exact lintegral_edist_to_simple_func_lt_top _ _ }
end
end to_simple_func
section coe_to_L1
protected lemma uniform_continuous : uniform_continuous (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
uniform_continuous_comap
protected lemma uniform_embedding : uniform_embedding (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
uniform_embedding_comap subtype.val_injective
protected lemma uniform_inducing : uniform_inducing (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
simple_func.uniform_embedding.to_uniform_inducing
protected lemma dense_embedding : dense_embedding (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
begin
apply simple_func.uniform_embedding.dense_embedding,
assume f,
rw mem_closure_iff_seq_limit,
have hfi' : integrable f ΞΌ := integrable_coe_fn f,
refine β¨Ξ» n, β(to_L1 (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n)
(simple_func.integrable_approx_on_univ (Lp.measurable f) hfi' n)), Ξ» n, mem_range_self _, _β©,
convert simple_func.tendsto_approx_on_univ_L1 (Lp.measurable f) hfi',
rw integrable.to_L1_coe_fn
end
protected lemma dense_inducing : dense_inducing (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
simple_func.dense_embedding.to_dense_inducing
protected lemma dense_range : dense_range (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)) :=
simple_func.dense_inducing.dense
variables [normed_field π] [normed_space π E] [measurable_space π] [opens_measurable_space π]
variables (Ξ± E π)
/-- The uniform and dense embedding of L1 simple functions into L1 functions. -/
def coe_to_L1 : (Ξ± βββ[ΞΌ] E) βL[π] (Ξ± ββ[ΞΌ] E) :=
{ to_fun := (coe : (Ξ± βββ[ΞΌ] E) β (Ξ± ββ[ΞΌ] E)),
map_add' := Ξ»f g, rfl,
map_smul' := Ξ»k f, rfl,
cont := L1.simple_func.uniform_continuous.continuous, }
variables {Ξ± E π}
end coe_to_L1
end simple_func
end L1
variables [measurable_space Ξ±] [normed_group E] [measurable_space E] {f g : Ξ± β E} {s t : set Ξ±}
{ΞΌ Ξ½ : measure Ξ±} {l l' : filter Ξ±} [borel_space E] [second_countable_topology E]
/-- To prove something for an arbitrary integrable function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `LΒΉ` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma integrable.induction (P : (Ξ± β E) β Prop)
(h_ind : β (c : E) β¦sβ¦, measurable_set s β ΞΌ s < β β P (s.indicator (Ξ» _, c)))
(h_add : β β¦f g : Ξ± β Eβ¦, disjoint (support f) (support g) β integrable f ΞΌ β integrable g ΞΌ β
P f β P g β P (f + g))
(h_closed : is_closed {f : Ξ± ββ[ΞΌ] E | P f} )
(h_ae : β β¦f gβ¦, f =α΅[ΞΌ] g β integrable f ΞΌ β P f β P g) :
β β¦f : Ξ± β Eβ¦ (hf : integrable f ΞΌ), P f :=
begin
have : β (f : simple_func Ξ± E), integrable f ΞΌ β P f,
{ refine simple_func.induction _ _,
{ intros c s hs h, dsimp only [simple_func.coe_const, simple_func.const_zero,
piecewise_eq_indicator, simple_func.coe_zero, simple_func.coe_piecewise] at h β’,
by_cases hc : c = 0,
{ subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, simp [const] },
apply h_ind c hs,
have : (nnnorm c : ββ₯0β) * ΞΌ s < β,
{ have := @comp_indicator _ _ _ _ (Ξ» x : E, (nnnorm x : ββ₯0β)) (const Ξ± c) s,
dsimp only at this,
have h' := h.has_finite_integral,
simpa [has_finite_integral, this, lintegral_indicator, hs] using h' },
exact ennreal.lt_top_of_mul_lt_top_right this (by simp [hc]) },
{ intros f g hfg hf hg int_fg,
rw [simple_func.coe_add, integrable_add hfg f.measurable g.measurable] at int_fg,
refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } },
have : β (f : Ξ± βββ[ΞΌ] E), P f,
{ intro f,
exact h_ae (L1.simple_func.to_simple_func_eq_to_fun f) (L1.simple_func.integrable f)
(this (L1.simple_func.to_simple_func f) (L1.simple_func.integrable f)) },
have : β (f : Ξ± ββ[ΞΌ] E), P f :=
Ξ» f, L1.simple_func.dense_range.induction_on f h_closed this,
exact Ξ» f hf, h_ae hf.coe_fn_to_L1 (L1.integrable_coe_fn _) (this (hf.to_L1 f)),
end
end measure_theory
|
fa8d44af50b4e1b4fd60dfda0a746b2d36326f52 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/local_context_auto.lean | 9829e6b0e9a518031eb350ef03e888a280fbc59f | [] | 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 | 179 | lean | import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.name
import Mathlib.Lean3Lib.init.meta.expr
import Mathlib.Lean3Lib.init.data.option.basic
namespace Mathlib
end Mathlib |
a1e50fd80016f18f4dccef121ddc8725694ec189 | ad3e8f15221a986da27c99f371922c0b3f5792b6 | /src/week06/solutions/e01_Z.lean | b90ec59cc6bd389c821abe108883e43be183c5f1 | [] | no_license | VArtem/lean-itmo | a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1 | dc44cd06f9f5b984d051831b3aaa7364e64c2dc4 | refs/heads/main | 1,683,761,214,467 | 1,622,821,295,000 | 1,622,821,295,000 | 357,236,048 | 12 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,041 | lean | import tactic
-- ΠΡΠ½ΠΎΠ²Π°Π½ΠΎ Π½Π° https://github.com/ImperialCollegeLondon/formalising-mathematics/tree/master/src/week_7
-- Π― ΡΠ±ΡΠ°Π» Π΄ΠΎΠ²ΠΎΠ»ΡΠ½ΠΎ ΠΌΠ½ΠΎΠ³ΠΎ ΠΌΠ°ΡΠ΅ΡΠΈΠ°Π»Π° ΠΎΡΡΡΠ΄Π°, ΠΏΠΎΡΠΌΠΎΡΡΠΈΡΠ΅ ΠΏΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΠΈ ΠΎΡΠΈΠ³ΠΈΠ½Π°Π», Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Kevin ΡΡ
ΠΎΠ΄ΠΈΡ Π³ΠΎΡΠ°Π·Π΄ΠΎ Π³Π»ΡΠ±ΠΆΠ΅ Π² ΠΊΠΎΠ½ΡΠ΅ΠΏΡΠΈΡ `quotient`
-- ΠΠΎΠ»ΡΡΠ΅ Π² ΠΊΠ½ΠΈΠ³Π΅ Theorem Proving in Lean: https://leanprover.github.io/theorem_proving_in_lean/axioms_and_computation.html#quotients
-- Π ΡΡΠΎΠΌ ΡΠ°ΠΉΠ»Π΅ ΠΌΡ ΠΈΠ·ΡΡΠΈΠΌ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΈΡ `quotient`
-- Π ΠΏΠΎΡΡΡΠΎΠΈΠΌ ΡΠ΅Π»ΡΠ΅ ΡΠΈΡΠ»Π° ΠΊΠ°ΠΊ ΠΊΠ»Π°ΡΡΡ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ ΠΏΠ°Ρ Π½Π°ΡΡΡΠ°Π»ΡΠ½ΡΡ
ΡΠΈΡΠ΅Π» (a, b)
-- ΠΠ΄Π΅ (a, b) β (c, d) β a + d = b + c
-- Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠ±Π»ΠΈΠΆΠ΅Π½ΠΈΠΈ `quotient` ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΠΈΡΠ°ΡΡ ΠΊΠ°ΠΊ "ΡΠΈΠΏ ΠΊΠ»Π°ΡΡΠΎΠ² ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ ΠΏΠΎ Π±ΠΈΠ½Π°ΡΠ½ΠΎΠΌΡ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΡ"
-- ΠΡΡΡΡ Ρ Π½Π°Ρ Π΅ΡΡΡ ΡΠΈΠΏ `Ξ±` ΠΈ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ `r : Ξ± β Ξ± β Prop`
-- `abbreviation` ΡΡΠΎ ΠΏΡΠΎΡΡΠΎ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΠ΅ΡΠΊΠΈΠΉ ΡΠΈΠ½ΠΎΠ½ΠΈΠΌ
abbreviation N2 := β Γ β
namespace N2
section product
def foo : β Γ β := (3, 4)
#reduce (foo.fst, foo.snd)
-- ΠΠ½Π°Π»ΠΎΠ³ΠΈΡΠ½ΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΈΡΠ°ΡΡ `foo.1` ΠΈΠ»ΠΈ `foo.2`
-- Π§ΡΠΎΠ±Ρ Π΄ΠΎΠΊΠ°Π·Π°ΡΡ ΡΠ°Π²Π΅Π½ΡΡΠ²ΠΎ Π΄Π²ΡΡ
ΠΏΠ°Ρ, ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ `ext`
example (X Y : Type) (s t : X Γ Y) (h1 : s.fst = t.fst) (h2 : s.snd = t.snd) :
s = t :=
begin
ext,
{ exact h1 },
{ exact h2 },
end
-- Π§ΡΠΎΠ±Ρ ΡΠ°Π·Π±ΠΈΡΡ ΠΏΠ°ΡΡ `x` Π½Π° Π΄Π²Π° Π½Π°ΡΡΡΠ°Π»ΡΠ½ΡΡ
ΡΠΈΡΠ»Π°, ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ `cases x with a b`
example (A B : Type) (x : A Γ B) : x = (x.1, x.2) :=
begin
cases x with a b,
-- β’ (a, b) = ((a, b).fst, (a, b).snd)
dsimp only, -- ΡΠΏΡΠΎΡΠ°Π΅Ρ `(a, b).fst` Π΄ΠΎ `a`.
-- β’ (a, b) = (a, b)
refl,
end
end product
-- ΠΠ°ΠΊ ΠΏΠΎΡΡΡΠΎΠΈΡΡ ΡΠ΅Π»ΡΠ΅ ΡΠΈΡΠ»Π° Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ Π½Π°ΡΡΡΠ°Π»ΡΠ½ΡΡ
?
-- Π‘ΡΠ°Π½Π΄Π°ΡΡΠ½Π°Ρ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΈΡ: ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ Π»ΠΈΠ±ΠΎ `n : β`, Π»ΠΈΠ±ΠΎ `-n-1`
#check β€
-- ΠΠ½Π°ΡΠ΅ ΡΠ΅Π»ΡΠ΅ ΡΠΈΡΠ»Π° ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅Π΄ΡΡΠ°Π²ΠΈΡΡ, ΠΊΠ°ΠΊ ΡΠ°Π·Π½ΠΎΡΡΡ Π΄Π²ΡΡ
Π½Π°ΡΡΡΠ°Π»ΡΠ½ΡΡ
ΡΠΈΡΠ΅Π», Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, -1 = 3 - 4
-- Π’Π°ΠΊΠΎΠ΅ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ Π½Π΅ΠΎΠ΄Π½ΠΎΠ·Π½Π°ΡΠ½ΠΎ, 3 - 4 = 5 - 6 = ...
-- Π ΠΊΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π΄Π²Π΅ ΠΏΠ°ΡΡ Π·Π°Π΄Π°ΡΡ ΠΎΠ΄Π½ΠΎ ΡΠΈΡΠ»ΠΎ? a - b = c - d β a + d = c + b
-- ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ `r : N2 β N2 β Prop`: r (a, b) (c, d) = a + d = c + b
def r (ab cd : N2) : Prop :=
ab.1 + cd.2 = cd.1 + ab.2
lemma r_def (ab cd : N2) : r ab cd β ab.1 + cd.2 = cd.1 + ab.2 := iff.rfl
lemma r_def' (a b c d : β) : r (a,b) (c,d) β a + d = c + b := iff.rfl
def r_refl : reflexive r :=
begin
-- `unfold reflexive` ΡΠ°ΡΠΊΡΠΎΠ΅Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠΊΠ°ΠΆΠ΅Ρ, ΠΊΠ°ΠΊ ΠΎΠ½ΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΎ
-- Π½Π΅ Π·Π°Π±ΡΠ²Π°ΠΉΡΠ΅, ΡΡΠΎ N2 = β Γ β, ΠΏΠΎΡΡΠΎΠΌΡ ΠΌΠΎΠΆΠ½ΠΎ Π΄Π΅Π»Π°ΡΡ `rintro β¨a, bβ©`
rintro β¨a, bβ©,
rw r_def',
end
def r_symm : symmetric r :=
begin
rintro β¨a, bβ© β¨c, dβ© h,
rw r_def' at *,
linarith,
end
def r_trans : transitive r :=
begin
intros x y z rxy ryz,
rw r_def at *,
linarith,
end
-- ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΠΈΠ½ΡΡΠ°Π½Ρ ΠΊΠ»Π°ΡΡΠ° `setoid`: ΡΡΠΎ ΠΏΡΠΎΡΡΠΎ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ Π½Π° `N2`
instance setoid : setoid N2 := β¨r, r_refl, r_symm, r_transβ©
-- Π’Π΅ΠΏΠ΅ΡΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ `β` (\~~ ΠΈΠ»ΠΈ \approx)
example (x y : N2) : x β y β r x y := iff.rfl
-- ΠΠΎΠ±Π°Π²ΠΈΠΌ simp-Π»Π΅ΠΌΠΌΡ Π΄Π»Ρ ΡΠΏΡΠΎΡΠ΅Π½ΠΈΡ ΠΆΠΈΠ·Π½ΠΈ Π² Π±ΡΠ΄ΡΡΠ΅ΠΌ
@[simp] lemma equiv_def (ab cd : N2) : ab β cd β ab.1 + cd.2 = cd.1 + ab.2 :=
begin
refl
end
@[simp] lemma equiv_def' (a b c d : β) : (a,b) β (c,d) β a + d = c + b := iff.rfl
end N2
open N2
-- ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ Z ΠΊΠ°ΠΊ `quotient` ΠΏΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΡ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ
def Z := quotient N2.setoid
namespace Z
-- ΠΠΎ Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠ°ΡΠ΅ ΡΠΈΠΏΠ° N2 ΠΌΠΎΠΆΠ½ΠΎ Π»Π΅Π³ΠΊΠΎ ΠΏΠΎΠ»ΡΡΠΈΡΡ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΈΠΏΠ° Z ΠΊΠ°ΠΊ `quotient.mk x`
-- ΠΠ»ΠΈ ΠΆΠ΅ `β¦xβ§`
def bar : Z := quotient.mk foo -- ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΠ΅Ρ ΡΠ΅Π»ΠΎΠΌΡ ΡΠΈΡΠ»Ρ -1.
example : bar = β¦fooβ§ := rfl
-- ΠΠΎΡΠΌΠΎΡΡΠΈΠΌ Π½Π° API `quotient`
example (x y : N2) : x β y β β¦xβ§ = β¦yβ§ := quotient.sound
example (x y : N2) : β¦xβ§ = β¦yβ§ β x β y := quotient.exact
example (x y : N2) : β¦xβ§ = β¦yβ§ β x β y := quotient.eq
-- ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΠΈ Π΄ΠΎΠΊΠ°ΠΆΠ΅ΠΌ Π²ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π°Π΄ Z, Π΄ΠΎΠΉΠ΄Ρ Π΄ΠΎ ΠΊΠΎΠΌΠΌΡΡΠ°ΡΠΈΠ²Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ»ΡΡΠ°
-- ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ 0 ΠΈ 1:
def zero : Z := β¦(0, 0)β§
def one : Z := β¦(1, 0)β§
instance : has_zero Z := β¨zeroβ©
instance : has_one Z := β¨oneβ©
@[simp] lemma zero_def : (0 : Z) = β¦(0, 0)β§ := rfl
@[simp] lemma one_def : (1 : Z) = β¦(1, 0)β§ := rfl
/-
ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠ΅ ΠΏΠΎ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΡ ΡΠΈΡΠ»ΠΎ. ΠΠ°ΠΊ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ ΡΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ `quotient`?
1) ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ Π²ΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ `N2 β Z`: ΠΏΠΎ `(a,b)` Π²ΡΠ΄Π°Π΅ΠΌ `β¦(b,a)β§`
2) ΠΠΎΠΊΠ°ΠΆΠ΅ΠΌ, ΡΡΠΎ ΡΡΠ° ΡΡΠ½ΠΊΡΠΈΡ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ½Π° Π½Π° ΠΊΠ»Π°ΡΡΠ°Ρ
ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎΡΡΠΈ:
`(a, b) β (c, d) β β¦(b, a)β§ = β¦(d, c)β§`
3) ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ `quotient.lift`, ΡΡΠΎΠ±Ρ ΠΏΠΎΠ»ΡΡΠΈΡΡ ΡΡΠ½ΠΊΡΠΈΡ `Z β Z`.
-/
def neg_aux (ab : N2) : Z := β¦(ab.2, ab.1)β§
@[simp] lemma neg_aux_def (ab : N2) : neg_aux ab = β¦(ab.2, ab.1)β§ := rfl
def neg : Z β Z := quotient.lift neg_aux
begin
-- β’ β (a b : N2), a β b β neg_aux a = neg_aux b
intros a b rab,
simp at *,
linarith,
end
-- Π½ΠΎΡΠ°ΡΠΈΡ `-z`
instance : has_neg Z := β¨negβ©
@[simp] lemma neg_def (a b : β) : (-β¦(a, b)β§ : Z) = β¦(b, a)β§ := rfl
/-
ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅. ΠΡΠ»ΠΈ Π±Ρ ΠΌΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ»ΠΈ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΡΠ°ΠΊ ΠΆΠ΅, ΠΊΠ°ΠΊ ΠΎΡΡΠΈΡΠ°Π½ΠΈΠ΅, ΡΠΎ ΠΏΡΠΈΡΠ»ΠΎΡΡ Π±Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ
`lift` Π΄Π²Π°ΠΆΠ΄Ρ: ΡΠ½Π°ΡΠ°Π»Π° ΠΏΠΎΠΊΠ°Π·Π°ΡΡ, ΡΡΠΎ ΡΡΠ½ΠΊΡΠΈΡ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠ° ΠΏΠΎ ΠΏΠ΅ΡΠ²ΠΎΠΌΡ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ, ΠΏΠΎΡΠΎΠΌ ΠΏΠΎ Π²ΡΠΎΡΠΎΠΌΡ.
ΠΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ `quotient.liftβ`, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ Π²Π·ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ `f : A β B β C`,
ΠΈ Π΄ΠΎΠΊΠ°Π·Π°ΡΠ΅Π»ΡΡΡΠ²ΠΎ, ΡΡΠΎ ΠΎΠ½Π° Π½Π΅ ΠΌΠ΅Π½ΡΠ΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, Π΅ΡΠ»ΠΈ Π·Π°ΠΌΠ΅Π½ΠΈΡΡ ΠΏΠ΅ΡΠ²ΡΠ΅ Π΄Π²Π° Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠ° Π½Π° ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΡΠ΅, ΠΈ Π²ΡΠ΄Π°Π΅Ρ ΡΡΠ½ΠΊΡΠΈΡ `A/~ β B/~ β C`.
-/
-- ΠΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΡ (note `(a-b)+(c-d)=(a+c)-(b+d)` )
def add_aux (ab cd : N2) : Z := β¦(ab.1 + cd.1, ab.2 + cd.2)β§
-- Π simp-Π»Π΅ΠΌΠΌΠ° Π΄Π»Ρ Π½Π΅Π³ΠΎ
@[simp] lemma add_aux_def (ab cd : N2) : add_aux ab cd = β¦(ab.1 + cd.1, ab.2 + cd.2)β§ := rfl
def add : Z β Z β Z := quotient.liftβ add_aux
begin
rintro β¨aβ, bββ© β¨aβ, bββ© β¨cβ, dββ© β¨cβ, dββ© rab rcd,
simp at *,
linarith,
end
instance : has_add Z := β¨addβ©
@[simp] lemma add_def (a b c d : β) :
(β¦(a, b)β§ + β¦(c, d)β§ : Z) = β¦(a+c, b+d)β§ :=
rfl
-- Π ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ Π²ΡΡΠΈΡΠ°Π½ΠΈΠ΅ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π»
def sub (x y : Z) : Z := x + -y
instance : has_sub Z := β¨subβ©
-- ΠΠΎΠΊΠ°ΠΆΠ΅ΠΌ, ΡΡΠΎ Z - ΠΊΠΎΠΌΠΌΡΡΠ°ΡΠΈΠ²Π½Π°Ρ Π³ΡΡΠΏΠΏΠ° ΠΏΠΎ ΡΠ»ΠΎΠΆΠ΅Π½ΠΈΡ
def add_comm_group : add_comm_group Z :=
{ zero := 0,
add := (+),
neg := has_neg.neg,
sub := has_sub.sub,
-- ΠΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ `quotient.induction_on` Π΄Π»Ρ Π΄ΠΎΠΊΠ°Π·Π°ΡΠ΅Π»ΡΡΡΠ²Π° Π»Π΅ΠΌΠΌ ΠΏΡΠΎ ΡΠ²ΠΎΠΉΡΡΠ²Π° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ Π½Π°Π΄ `quotient`
-- "ΠΡΠ»ΠΈ Π΄Π»Ρ Π²ΡΠ΅Ρ
`x : N2` Π²Π΅ΡΠ½ΠΎ `p β¦xβ§`, ΡΠΎ Π΄Π»Ρ Π²ΡΠ΅Ρ
`y : Z` Π²Π΅ΡΠ½ΠΎ p y"
zero_add := begin
intro x,
apply quotient.induction_on x, clear x,
rintro β¨a, bβ©,
simp,
end,
add_zero := begin
intro x,
apply quotient.induction_on x,
rintro β¨a, bβ©,
simp,
end,
-- ΠΠ»Ρ Π²Π΅ΡΡΠΈΠΉ Ρ 2 ΠΈ 3 Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠ°ΠΌΠΈ Π΅ΡΡΡ `quotient.induction_onβ` ΠΈ `quotient.induction_onβ`
-- ΠΠ΅ Π·Π°Π±ΡΠ²Π°ΠΉΡΠ΅ ΠΏΡΠΎ `linarith` ΠΈ `ring`
add_assoc := begin
intros a b c,
apply quotient.induction_onβ a b c,
rintro β¨xβ, xββ© β¨yβ, yββ© β¨zβ, zββ©,
simp,
ring,
end,
add_left_neg := begin
intro a,
apply quotient.induction_on a,
rintro β¨xβ, xββ©,
simp [add_comm],
end,
add_comm := begin
intros a b,
apply quotient.induction_onβ a b,
rintro β¨xβ, xββ© β¨yβ, yββ©,
simp,
ring,
end,
}
-- Π£ΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅: `(a-b)*(c-d) = (a*c+b*d)-(a*d+b*c)`
def mul_aux (ab cd : N2) : N2 :=
(ab.1 * cd.1 + ab.2 * cd.2, ab.1 * cd.2 + ab.2 * cd.1)
@[simp] lemma mul_aux_def (a b c d : β) :
mul_aux (a,b) (c,d) = (a*c+b*d,a*d+b*c) := rfl
-- ΠΠ»Ρ ΡΠ°Π·Π½ΠΎΠΎΠ±ΡΠ°Π·ΠΈΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΡΠ»Π΅Π³ΠΊΠ° ΠΈΠ½Π°ΡΠ΅.
-- ΠΠΌΠ΅ΡΡΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ `quotient.liftβ` (ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠ΅Π²ΡΠ°ΡΠ°Π΅Ρ `N2 β N2 β Z` Π² `Z β Z β Z`),
-- ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ `quotient.mapβ`, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠ΅Π²ΡΠ°ΡΠ°Π΅Ρ `N2 β N2 β N2` Π² `Z β Z β Z`.
-- `nlinarith` ΡΠΌΠ΅Π΅Ρ Π΄ΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡ Π½Π΅ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ΅Π»ΠΈ, ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΠΈΠ΅ Π½Π΅Π»ΠΈΠ½Π΅ΠΉΠ½ΡΠ΅ [Π½Π΅]ΡΠ°Π²Π΅Π½ΡΡΠ²Π°
def mul : Z β Z β Z := quotient.mapβ mul_aux
begin
rintro β¨aβ, bββ© β¨aβ, bββ© rab β¨cβ, dββ© β¨cβ, dββ© rcd,
simp at *,
nlinarith,
end
instance : has_mul Z := β¨mulβ©
@[simp] lemma mul_def (a b c d : β) :
(β¦(a, b)β§ * β¦(c, d)β§ : Z) = β¦(a*c+b*d, a*d+b*c)β§ := rfl
-- Π Π½Π°ΠΊΠΎΠ½Π΅Ρ, Π΄ΠΎΠΊΠ°ΠΆΠ΅ΠΌ, ΡΡΠΎ Z - ΠΊΠΎΠΌΠΌΡΡΠ°ΡΠΈΠ²Π½ΠΎΠ΅ ΠΊΠΎΠ»ΡΡΠΎ
def comm_ring : comm_ring Z :=
{ one := 1,
add := (+),
mul := (*),
mul_assoc := begin
intros x y z,
apply quotient.induction_onβ x y z,
clear x y z,
rintros β¨a, bβ© β¨c, dβ© β¨e, fβ©,
simp,
ring,
end,
one_mul := begin
intro a,
apply quotient.induction_on a, clear a,
rintro β¨x, yβ©,
simp,
end,
mul_one := begin
intro a,
apply quotient.induction_on a, clear a,
rintro β¨x, yβ©,
simp,
end,
left_distrib := begin
intros x y z,
apply quotient.induction_onβ x y z, clear x y z,
rintros β¨a, bβ© β¨c, dβ© β¨e, fβ©,
simp,
ring,
end,
right_distrib := begin
intros x y z,
apply quotient.induction_onβ x y z, clear x y z,
rintros β¨a, bβ© β¨c, dβ© β¨e, fβ©,
simp,
ring,
end,
mul_comm := begin
intros x y,
apply quotient.induction_onβ x y, clear x y,
rintros β¨a, bβ© β¨c, dβ©,
simp,
ring,
end,
..add_comm_group
}
end Z
|
d3e79f6f4fa0182068060842bede530e9b299932 | 8e6cad62ec62c6c348e5faaa3c3f2079012bdd69 | /src/measure_theory/measurable_space.lean | 26a1becfe4763a859f951994896c06737659dae6 | [
"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 | 61,802 | lean | /-
Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes HΓΆlzl, Mario Carneiro
-/
import data.set.disjointed
import data.set.countable
import data.indicator_function
import data.equiv.encodable.lattice
import data.tprod
import order.filter.lift
/-!
# Measurable spaces and measurable functions
This file defines measurable spaces and the functions and isomorphisms
between them.
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`.
## Main statements
The main theorem of this file is Dynkin's Ο-Ξ» theorem, which appears
here as an induction principle `induction_on_inter`. Suppose `s` is a
collection of subsets of `Ξ±` such that the intersection of two members
of `s` belongs to `s` whenever it is nonempty. Let `m` be the Ο-algebra
generated by `s`. In order to check that a predicate `C` holds on every
member of `m`, it suffices to check that `C` holds on the members of `s` and
that `C` is preserved by complementation and *disjoint* countable
unions.
## 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 classical filter
variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΄' : Type*} {ΞΉ : Sort*} {s t u : set Ξ±}
/-- A measurable space is a space equipped with a Ο-algebra. -/
structure measurable_space (Ξ± : Type*) :=
(measurable_set' : set Ξ± β Prop)
(measurable_set_empty : measurable_set' β
)
(measurable_set_compl : β s, measurable_set' s β measurable_set' sαΆ)
(measurable_set_Union : β f : β β set Ξ±, (β i, measurable_set' (f i)) β measurable_set' (β i, f i))
attribute [class] measurable_space
instance [h : measurable_space Ξ±] : measurable_space (order_dual Ξ±) := h
section
variable [measurable_space Ξ±]
/-- `measurable_set s` means that `s` is measurable (in the ambient measure space on `Ξ±`) -/
def measurable_set : set Ξ± β Prop := βΉmeasurable_space Ξ±βΊ.measurable_set'
@[simp] lemma measurable_set.empty : measurable_set (β
: set Ξ±) :=
βΉmeasurable_space Ξ±βΊ.measurable_set_empty
lemma measurable_set.compl : measurable_set s β measurable_set sαΆ :=
βΉmeasurable_space Ξ±βΊ.measurable_set_compl s
lemma measurable_set.of_compl (h : measurable_set sαΆ) : measurable_set s :=
compl_compl s βΈ h.compl
@[simp] lemma measurable_set.compl_iff : measurable_set sαΆ β measurable_set s :=
β¨measurable_set.of_compl, measurable_set.complβ©
@[simp] lemma measurable_set.univ : measurable_set (univ : set Ξ±) :=
by simpa using (@measurable_set.empty Ξ± _).compl
@[nontriviality] lemma subsingleton.measurable_set [subsingleton Ξ±] {s : set Ξ±} :
measurable_set s :=
subsingleton.set_cases measurable_set.empty measurable_set.univ s
lemma measurable_set.congr {s t : set Ξ±} (hs : measurable_set s) (h : s = t) :
measurable_set t :=
by rwa β h
lemma measurable_set.bUnion_decode2 [encodable Ξ²] β¦f : Ξ² β set Ξ±β¦ (h : β b, measurable_set (f b))
(n : β) : measurable_set (β b β decode2 Ξ² n, f b) :=
encodable.Union_decode2_cases measurable_set.empty h
lemma measurable_set.Union [encodable Ξ²] β¦f : Ξ² β set Ξ±β¦ (h : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
begin
rw β encodable.Union_decode2,
exact βΉmeasurable_space Ξ±βΊ.measurable_set_Union _ (measurable_set.bUnion_decode2 h)
end
lemma measurable_set.bUnion {f : Ξ² β set Ξ±} {s : set Ξ²} (hs : countable s)
(h : β b β s, measurable_set (f b)) : measurable_set (β b β s, f b) :=
begin
rw bUnion_eq_Union,
haveI := hs.to_encodable,
exact measurable_set.Union (by simpa using h)
end
lemma set.finite.measurable_set_bUnion {f : Ξ² β set Ξ±} {s : set Ξ²} (hs : finite s)
(h : β b β s, measurable_set (f b)) :
measurable_set (β b β s, f b) :=
measurable_set.bUnion hs.countable h
lemma finset.measurable_set_bUnion {f : Ξ² β set Ξ±} (s : finset Ξ²)
(h : β b β s, measurable_set (f b)) :
measurable_set (β b β s, f b) :=
s.finite_to_set.measurable_set_bUnion h
lemma measurable_set.sUnion {s : set (set Ξ±)} (hs : countable s) (h : β t β s, measurable_set t) :
measurable_set (ββ s) :=
by { rw sUnion_eq_bUnion, exact measurable_set.bUnion hs h }
lemma set.finite.measurable_set_sUnion {s : set (set Ξ±)} (hs : finite s)
(h : β t β s, measurable_set t) :
measurable_set (ββ s) :=
measurable_set.sUnion hs.countable h
lemma measurable_set.Union_Prop {p : Prop} {f : p β set Ξ±} (hf : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
by { by_cases p; simp [h, hf, measurable_set.empty] }
lemma measurable_set.Inter [encodable Ξ²] {f : Ξ² β set Ξ±} (h : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
measurable_set.compl_iff.1 $
by { rw compl_Inter, exact measurable_set.Union (Ξ» b, (h b).compl) }
section fintype
local attribute [instance] fintype.encodable
lemma measurable_set.Union_fintype [fintype Ξ²] {f : Ξ² β set Ξ±} (h : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
measurable_set.Union h
lemma measurable_set.Inter_fintype [fintype Ξ²] {f : Ξ² β set Ξ±} (h : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
measurable_set.Inter h
end fintype
lemma measurable_set.bInter {f : Ξ² β set Ξ±} {s : set Ξ²} (hs : countable s)
(h : β b β s, measurable_set (f b)) : measurable_set (β b β s, f b) :=
measurable_set.compl_iff.1 $
by { rw compl_bInter, exact measurable_set.bUnion hs (Ξ» b hb, (h b hb).compl) }
lemma set.finite.measurable_set_bInter {f : Ξ² β set Ξ±} {s : set Ξ²} (hs : finite s)
(h : β b β s, measurable_set (f b)) : measurable_set (β b β s, f b) :=
measurable_set.bInter hs.countable h
lemma finset.measurable_set_bInter {f : Ξ² β set Ξ±} (s : finset Ξ²)
(h : β b β s, measurable_set (f b)) : measurable_set (β b β s, f b) :=
s.finite_to_set.measurable_set_bInter h
lemma measurable_set.sInter {s : set (set Ξ±)} (hs : countable s) (h : β t β s, measurable_set t) :
measurable_set (ββ s) :=
by { rw sInter_eq_bInter, exact measurable_set.bInter hs h }
lemma set.finite.measurable_set_sInter {s : set (set Ξ±)} (hs : finite s)
(h : β t β s, measurable_set t) : measurable_set (ββ s) :=
measurable_set.sInter hs.countable h
lemma measurable_set.Inter_Prop {p : Prop} {f : p β set Ξ±} (hf : β b, measurable_set (f b)) :
measurable_set (β b, f b) :=
by { by_cases p; simp [h, hf, measurable_set.univ] }
@[simp] lemma measurable_set.union {sβ sβ : set Ξ±} (hβ : measurable_set sβ)
(hβ : measurable_set sβ) :
measurable_set (sβ βͺ sβ) :=
by { rw union_eq_Union, exact measurable_set.Union (bool.forall_bool.2 β¨hβ, hββ©) }
@[simp] lemma measurable_set.inter {sβ sβ : set Ξ±} (hβ : measurable_set sβ)
(hβ : measurable_set sβ) :
measurable_set (sβ β© sβ) :=
by { rw inter_eq_compl_compl_union_compl, exact (hβ.compl.union hβ.compl).compl }
@[simp] lemma measurable_set.diff {sβ sβ : set Ξ±} (hβ : measurable_set sβ)
(hβ : measurable_set sβ) :
measurable_set (sβ \ sβ) :=
hβ.inter hβ.compl
@[simp] lemma measurable_set.disjointed {f : β β set Ξ±} (h : β i, measurable_set (f i)) (n) :
measurable_set (disjointed f n) :=
disjointed_induct (h n) (assume t i ht, measurable_set.diff ht $ h _)
@[simp] lemma measurable_set.const (p : Prop) : measurable_set {a : Ξ± | p} :=
by { by_cases p; simp [h, measurable_set.empty]; apply measurable_set.univ }
/-- Every set has a measurable superset. Declare this as local instance as needed. -/
lemma nonempty_measurable_superset (s : set Ξ±) : nonempty { t // s β t β§ measurable_set t} :=
β¨β¨univ, subset_univ s, measurable_set.univβ©β©
end
@[ext] lemma measurable_space.ext : β {mβ mβ : measurable_space Ξ±},
(β s : set Ξ±, mβ.measurable_set' s β mβ.measurable_set' s) β mβ = mβ
| β¨sβ, _, _, _β© β¨sβ, _, _, _β© h :=
have sβ = sβ, from funext $ assume x, propext $ h x,
by subst this
@[ext] lemma measurable_space.ext_iff {mβ mβ : measurable_space Ξ±} :
mβ = mβ β (β s : set Ξ±, mβ.measurable_set' s β mβ.measurable_set' s) :=
β¨by { unfreezingI {rintro rfl}, intro s, refl }, measurable_space.extβ©
/-- A typeclass mixin for `measurable_space`s such that each singleton is measurable. -/
class measurable_singleton_class (Ξ± : Type*) [measurable_space Ξ±] : Prop :=
(measurable_set_singleton : β x, measurable_set ({x} : set Ξ±))
export measurable_singleton_class (measurable_set_singleton)
attribute [simp] measurable_set_singleton
section measurable_singleton_class
variables [measurable_space Ξ±] [measurable_singleton_class Ξ±]
lemma measurable_set_eq {a : Ξ±} : measurable_set {x | x = a} :=
measurable_set_singleton a
lemma measurable_set.insert {s : set Ξ±} (hs : measurable_set s) (a : Ξ±) :
measurable_set (insert a s) :=
(measurable_set_singleton a).union hs
@[simp] lemma measurable_set_insert {a : Ξ±} {s : set Ξ±} :
measurable_set (insert a s) β measurable_set s :=
β¨Ξ» h, if ha : a β s then by rwa β insert_eq_of_mem ha
else insert_diff_self_of_not_mem ha βΈ h.diff (measurable_set_singleton _),
Ξ» h, h.insert aβ©
lemma set.finite.measurable_set {s : set Ξ±} (hs : finite s) : measurable_set s :=
finite.induction_on hs measurable_set.empty $ Ξ» a s ha hsf hsm, hsm.insert _
protected lemma finset.measurable_set (s : finset Ξ±) : measurable_set (βs : set Ξ±) :=
s.finite_to_set.measurable_set
end measurable_singleton_class
namespace measurable_space
section complete_lattice
instance : partial_order (measurable_space Ξ±) :=
{ le := Ξ» mβ mβ, mβ.measurable_set' β€ mβ.measurable_set',
le_refl := assume a b, le_refl _,
le_trans := assume a b c, le_trans,
le_antisymm := assume a b hβ hβ, measurable_space.ext $ assume s, β¨hβ s, hβ sβ© }
/-- The smallest Ο-algebra containing a collection `s` of basic sets -/
inductive generate_measurable (s : set (set Ξ±)) : set Ξ± β Prop
| basic : β u β s, generate_measurable u
| empty : generate_measurable β
| compl : β s, generate_measurable s β generate_measurable sαΆ
| union : β f : β β set Ξ±, (β n, generate_measurable (f n)) β generate_measurable (β i, f i)
/-- Construct the smallest measure space containing a collection of basic sets -/
def generate_from (s : set (set Ξ±)) : measurable_space Ξ± :=
{ measurable_set' := generate_measurable s,
measurable_set_empty := generate_measurable.empty,
measurable_set_compl := generate_measurable.compl,
measurable_set_Union := generate_measurable.union }
lemma measurable_set_generate_from {s : set (set Ξ±)} {t : set Ξ±} (ht : t β s) :
(generate_from s).measurable_set' t :=
generate_measurable.basic t ht
lemma generate_from_le {s : set (set Ξ±)} {m : measurable_space Ξ±}
(h : β t β s, m.measurable_set' t) : generate_from s β€ m :=
assume t (ht : generate_measurable s t), ht.rec_on h
(measurable_set_empty m)
(assume s _ hs, measurable_set_compl m s hs)
(assume f _ hf, measurable_set_Union m f hf)
lemma generate_from_le_iff {s : set (set Ξ±)} (m : measurable_space Ξ±) :
generate_from s β€ m β s β {t | m.measurable_set' t} :=
iff.intro
(assume h u hu, h _ $ measurable_set_generate_from hu)
(assume h, generate_from_le h)
@[simp] lemma generate_from_measurable_set [measurable_space Ξ±] :
generate_from {s : set Ξ± | measurable_set s} = βΉ_βΊ :=
le_antisymm (generate_from_le $ Ξ» _, id) $ Ξ» s, measurable_set_generate_from
/-- If `g` is a collection of subsets of `Ξ±` such that the `Ο`-algebra generated from `g` contains
the same sets as `g`, then `g` was already a `Ο`-algebra. -/
protected def mk_of_closure (g : set (set Ξ±)) (hg : {t | (generate_from g).measurable_set' t} = g) :
measurable_space Ξ± :=
{ measurable_set' := Ξ» s, s β g,
measurable_set_empty := hg βΈ measurable_set_empty _,
measurable_set_compl := hg βΈ measurable_set_compl _,
measurable_set_Union := hg βΈ measurable_set_Union _ }
lemma mk_of_closure_sets {s : set (set Ξ±)}
{hs : {t | (generate_from s).measurable_set' t} = s} :
measurable_space.mk_of_closure s hs = generate_from s :=
measurable_space.ext $ assume t, show t β s β _, by { conv_lhs { rw [β hs] }, refl }
/-- We get a Galois insertion between `Ο`-algebras on `Ξ±` and `set (set Ξ±)` by using `generate_from`
on one side and the collection of measurable sets on the other side. -/
def gi_generate_from : galois_insertion (@generate_from Ξ±) (Ξ» m, {t | @measurable_set Ξ± m t}) :=
{ gc := assume s, generate_from_le_iff,
le_l_u := assume m s, measurable_set_generate_from,
choice :=
Ξ» g hg, measurable_space.mk_of_closure g $ le_antisymm hg $ (generate_from_le_iff _).1 le_rfl,
choice_eq := assume g hg, mk_of_closure_sets }
instance : complete_lattice (measurable_space Ξ±) :=
gi_generate_from.lift_complete_lattice
instance : inhabited (measurable_space Ξ±) := β¨β€β©
lemma measurable_set_bot_iff {s : set Ξ±} : @measurable_set Ξ± β₯ s β (s = β
β¨ s = univ) :=
let b : measurable_space Ξ± :=
{ measurable_set' := Ξ» s, s = β
β¨ s = univ,
measurable_set_empty := or.inl rfl,
measurable_set_compl := by simp [or_imp_distrib] {contextual := tt},
measurable_set_Union := assume f hf, classical.by_cases
(assume h : βi, f i = univ,
let β¨i, hiβ© := h in
or.inr $ eq_univ_of_univ_subset $ hi βΈ le_supr f i)
(assume h : Β¬ βi, f i = univ,
or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i,
(hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h β¨i, hiβ©)) } in
have b = β₯, from bot_unique $ assume s hs,
hs.elim (Ξ» s, s.symm βΈ @measurable_set_empty _ β₯) (Ξ» s, s.symm βΈ @measurable_set.univ _ β₯),
this βΈ iff.rfl
@[simp] theorem measurable_set_top {s : set Ξ±} : @measurable_set _ β€ s := trivial
@[simp] theorem measurable_set_inf {mβ mβ : measurable_space Ξ±} {s : set Ξ±} :
@measurable_set _ (mβ β mβ) s β @measurable_set _ mβ s β§ @measurable_set _ mβ s :=
iff.rfl
@[simp] theorem measurable_set_Inf {ms : set (measurable_space Ξ±)} {s : set Ξ±} :
@measurable_set _ (Inf ms) s β β m β ms, @measurable_set _ m s :=
show s β (β m β ms, {t | @measurable_set _ m t }) β _, by simp
@[simp] theorem measurable_set_infi {ΞΉ} {m : ΞΉ β measurable_space Ξ±} {s : set Ξ±} :
@measurable_set _ (infi m) s β β i, @measurable_set _ (m i) s :=
show s β (Ξ» m, {s | @measurable_set _ m s }) (infi m) β _,
by { rw (@gi_generate_from Ξ±).gc.u_infi, simp }
theorem measurable_set_sup {mβ mβ : measurable_space Ξ±} {s : set Ξ±} :
@measurable_set _ (mβ β mβ) s β generate_measurable (mβ.measurable_set' βͺ mβ.measurable_set') s :=
iff.refl _
theorem measurable_set_Sup {ms : set (measurable_space Ξ±)} {s : set Ξ±} :
@measurable_set _ (Sup ms) s β
generate_measurable {s : set Ξ± | β m β ms, @measurable_set _ m s} s :=
begin
change @measurable_set' _ (generate_from $ β m β ms, _) _ β _,
simp [generate_from, β set_of_exists]
end
theorem measurable_set_supr {ΞΉ} {m : ΞΉ β measurable_space Ξ±} {s : set Ξ±} :
@measurable_set _ (supr m) s β
generate_measurable {s : set Ξ± | β i, @measurable_set _ (m i) s} s :=
begin
convert @measurable_set_Sup _ (range m) s,
simp,
end
end complete_lattice
section functors
variables {m mβ mβ : measurable_space Ξ±} {m' : measurable_space Ξ²} {f : Ξ± β Ξ²} {g : Ξ² β Ξ±}
/-- The forward image of a measure 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, m.measurable_set' $ 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 measure 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', m.measurable_set' 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'] β© }
@[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 generate_from_le_generate_from {s t : set (set Ξ±)} (h : s β t) :
generate_from s β€ generate_from t :=
gi_generate_from.gc.monotone_l h
lemma generate_from_sup_generate_from {s t : set (set Ξ±)} :
generate_from s β generate_from t = generate_from (s βͺ t) :=
(@gi_generate_from Ξ±).gc.l_sup.symm
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
/-- A function `f` between measurable spaces is measurable if the preimage of every
measurable set is measurable. -/
def measurable [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β Ξ²) : Prop :=
β β¦t : set Ξ²β¦, measurable_set t β measurable_set (f β»ΒΉ' t)
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
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 [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³]
lemma measurable_id : measurable (@id Ξ±) := Ξ» t, id
lemma measurable.comp {g : Ξ² β Ξ³} {f : Ξ± β Ξ²} (hg : measurable g) (hf : measurable f) :
measurable (g β f) :=
Ξ» t ht, hf (hg ht)
@[nontriviality] lemma subsingleton.measurable [subsingleton Ξ±] {f : Ξ± β Ξ²} : measurable f :=
Ξ» s hs, @subsingleton.measurable_set Ξ± _ _ _
lemma measurable.piecewise {s : set Ξ±} {_ : decidable_pred s} {f g : Ξ± β Ξ²}
(hs : measurable_set s) (hf : measurable f) (hg : measurable g) :
measurable (piecewise s f g) :=
begin
intros t ht,
simp only [piecewise_preimage],
exact (hs.inter $ hf ht).union (hs.compl.inter $ 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} {f g : Ξ± β Ξ²}
(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
@[simp] lemma measurable_const {a : Ξ±} : measurable (Ξ» b : Ξ², a) :=
assume s hs, measurable_set.const (a β s)
lemma measurable.indicator [has_zero Ξ²] {s : set Ξ±} {f : Ξ± β Ξ²}
(hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) :=
hf.piecewise hs measurable_const
@[to_additive]
lemma measurable_one [has_one Ξ±] : measurable (1 : Ξ² β Ξ±) := @measurable_const _ _ _ _ 1
lemma measurable_of_not_nonempty (h : Β¬ nonempty Ξ±) (f : Ξ± β Ξ²) : measurable f :=
begin
assume s hs,
convert measurable_set.empty,
exact eq_empty_of_not_nonempty h _,
end
end measurable_functions
section constructions
variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³]
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 β := β€
lemma measurable_to_encodable [encodable Ξ±] {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_Prop $ Ξ» hy, _),
by_cases hyf : y β range f,
{ rcases hyf with β¨y, rflβ©,
apply h },
{ simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] }
end
lemma measurable_unit (f : unit β Ξ±) : measurable f :=
measurable_from_top
section nat
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_encodable
lemma measurable_find_greatest' {p : Ξ± β β β Prop}
{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 _ nat.find_greatest_le
lemma measurable_find_greatest {p : Ξ± β β β Prop} {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.Inter_Prop, measurable_set.compl, hN]; try { intros } }
end
lemma measurable_find {p : Ξ± β β β Prop} (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, _),
simp only [set.preimage, mem_singleton_iff, nat.find_eq_iff, set_of_and, set_of_forall,
β compl_set_of],
repeat { apply_rules [measurable_set.inter, hm, measurable_set.Inter, measurable_set.Inter_Prop,
measurable_set.compl]; try { intros } }
end
end nat
section subtype
instance {Ξ±} {p : Ξ± β Prop} [m : measurable_space Ξ±] : measurable_space (subtype p) :=
m.comap (coe : _ β Ξ±)
lemma measurable_subtype_coe {p : Ξ± β Prop} : measurable (coe : subtype p β Ξ±) :=
measurable_space.le_map_comap
lemma measurable.subtype_coe {p : Ξ² β Prop} {f : Ξ± β subtype p} (hf : measurable f) :
measurable (Ξ» a : Ξ±, (f a : Ξ²)) :=
measurable_subtype_coe.comp hf
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_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
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_measurable_on_compl_singleton [measurable_singleton_class Ξ±]
{f : Ξ± β Ξ²} (a : Ξ±) (hf : measurable (set.restrict f {x | x β a})) :
measurable f :=
measurable_of_measurable_union_cover _ _ measurable_set_eq measurable_set_eq.compl
(Ξ» x hx, classical.em _)
(@subsingleton.measurable {x | x = a} _ _ _ β¨Ξ» x y, subtype.eq $ x.2.trans y.2.symmβ© _) hf
end subtype
section prod
instance {Ξ± Ξ²} [mβ : measurable_space Ξ±] [mβ : measurable_space Ξ²] : measurable_space (Ξ± Γ Ξ²) :=
mβ.comap prod.fst β mβ.comap prod.snd
lemma measurable_fst : measurable (prod.fst : Ξ± Γ Ξ² β Ξ±) :=
measurable.of_comap_le le_sup_left
lemma measurable.fst {f : Ξ± β Ξ² Γ Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).1) :=
measurable_fst.comp hf
lemma measurable_snd : measurable (prod.snd : Ξ± Γ Ξ² β Ξ²) :=
measurable.of_comap_le le_sup_right
lemma measurable.snd {f : Ξ± β Ξ² Γ Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).2) :=
measurable_snd.comp hf
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 {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β©
lemma measurable.prod_mk {f : Ξ± β Ξ²} {g : Ξ± β Ξ³} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» a : Ξ±, (f a, g a)) :=
measurable.prod hf hg
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
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_swap : measurable (prod.swap : Ξ± Γ Ξ² β Ξ² Γ Ξ±) :=
measurable.prod measurable_snd measurable_fst
lemma measurable_swap_iff {f : Ξ± Γ Ξ² β Ξ³} : measurable (f β prod.swap) β measurable f :=
β¨Ξ» hf, by { convert hf.comp measurable_swap, ext β¨x, yβ©, refl }, Ξ» hf, hf.comp measurable_swapβ©
lemma measurable_set.prod {s : set Ξ±} {t : set Ξ²} (hs : measurable_set s) (ht : measurable_set t) :
measurable_set (s.prod t) :=
measurable_set.inter (measurable_fst hs) (measurable_snd ht)
lemma measurable_set_prod_of_nonempty {s : set Ξ±} {t : set Ξ²} (h : (s.prod t).nonempty) :
measurable_set (s.prod 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.prod t) := measurable_id.prod_mk measurable_const hst,
have : measurable_set (prod.mk x β»ΒΉ' s.prod t) := measurable_const.prod_mk measurable_id hst,
simp * at *
end
lemma measurable_set_prod {s : set Ξ±} {t : set Ξ²} :
measurable_set (s.prod t) β (measurable_set s β§ measurable_set t) β¨ s = β
β¨ t = β
:=
begin
cases (s.prod 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β©
end prod
section pi
variables {Ο : Ξ΄ β Type*}
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]
lemma measurable_pi_apply (a : Ξ΄) : measurable (Ξ» f : Ξ a, Ο a, f a) :=
measurable.of_comap_le $ le_supr _ a
lemma measurable.eval {a : Ξ΄} {g : Ξ± β Ξ a, Ο a}
(hg : measurable g) : measurable (Ξ» x, g x a) :=
(measurable_pi_apply a).comp hg
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. -/
lemma measurable_update (f : Ξ (a : Ξ΄), Ο a) {a : Ξ΄} : 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`. -/
lemma measurable_set.pi {s : set Ξ΄} {t : Ξ i : Ξ΄, set (Ο i)} (hs : countable s)
(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 [encodable Ξ΄] {t : Ξ i : Ξ΄, set (Ο i)}
(ht : β i, measurable_set (t i)) : measurable_set (pi univ t) :=
measurable_set.pi (countable_encodable _) (Ξ» i _, ht i)
lemma measurable_set_pi_of_nonempty {s : set Ξ΄} {t : Ξ i, set (Ο i)} (hs : countable s)
(h : (pi s t).nonempty) : measurable_set (pi s t) β β i β s, measurable_set (t i) :=
begin
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 : countable s) :
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
section fintype
local attribute [instance] fintype.encodable
lemma measurable_set.pi_fintype [fintype Ξ΄] {s : set Ξ΄} {t : Ξ i, set (Ο i)}
(ht : β i β s, measurable_set (t i)) : measurable_set (pi s t) :=
measurable_set.pi (countable_encodable _) ht
lemma measurable_set.univ_pi_fintype [fintype Ξ΄] {t : Ξ i, set (Ο i)}
(ht : β i, measurable_set (t i)) : measurable_set (pi univ t) :=
measurable_set.pi_fintype (Ξ» i _, ht i)
end fintype
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 : β {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' {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
lemma measurable_inl : measurable (@sum.inl Ξ± Ξ²) := measurable.of_le_map inf_le_left
lemma measurable_inr : measurable (@sum.inr Ξ± Ξ²) := measurable.of_le_map inf_le_right
lemma measurable_sum {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)
lemma measurable.sum_elim {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_range_inl : measurable_set (range sum.inl : set (Ξ± β Ξ²)) :=
by { rw [β image_univ], exact measurable_set.univ.inl_image }
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 }β©
lemma measurable_set_range_inr : 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
/-- 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_fun)
(measurable_inv_fun : measurable inv_fun)
infix ` βα΅ `:25 := measurable_equiv
namespace measurable_equiv
variables (Ξ± Ξ²) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄]
instance : has_coe_to_fun (Ξ± βα΅ Ξ²) :=
β¨Ξ» _, Ξ± β Ξ², Ξ» e, e.to_equivβ©
variables {Ξ± Ξ²}
lemma coe_eq (e : Ξ± βα΅ Ξ²) : (e : Ξ± β Ξ²) = e.to_equiv := rfl
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. -/
@[simps] 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. -/
@[simps] 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_symm_mk (e : Ξ± β Ξ²) (h1 : measurable e) (h2 : measurable e.symm) :
((β¨e, h1, h2β© : Ξ± βα΅ Ξ²).symm : Ξ² β Ξ±) = e.symm := 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
/-- 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_coe_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 [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this)
(Ξ» h, h.comp e.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 }
/-- `set.prod s t β (s Γ t)` as measurable spaces. -/
def set.prod (s : set Ξ±) (t : set Ξ²) : s.prod 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).prod univ)
((range sum.inr).prod univ)
(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_coe_iff.1 _,
refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _,
dsimp [(β)],
convert measurable_inl,
ext β¨a, cβ©, refl },
{ refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _,
refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_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. -/
noncomputable def pi_measurable_equiv_tprod {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 }
end measurable_equiv
/-- A pi-system is a collection of subsets of `Ξ±` that is closed under intersections of sets that
are not disjoint. Usually it is also required that the collection is nonempty, but we don't do
that here. -/
def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop :=
β s t β C, (s β© t : set Ξ±).nonempty β s β© t β C
namespace measurable_space
lemma is_pi_system_measurable_set [measurable_space Ξ±] :
is_pi_system {s : set Ξ± | measurable_set s} :=
Ξ» s t hs ht _, hs.inter ht
/-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set,
is closed under complementation and under countable union of pairwise disjoint sets.
The disjointness condition is the only difference with `Ο`-algebras.
The main purpose of Dynkin systems is to provide a powerful induction rule for Ο-algebras
generated by intersection stable set systems.
A Dynkin system is also known as a "Ξ»-system" or a "d-system".
-/
structure dynkin_system (Ξ± : Type*) :=
(has : set Ξ± β Prop)
(has_empty : has β
)
(has_compl : β {a}, has a β has aαΆ)
(has_Union_nat : β {f : β β set Ξ±}, pairwise (disjoint on f) β (β i, has (f i)) β has (β i, f i))
namespace dynkin_system
@[ext] lemma ext : β {dβ dβ : dynkin_system Ξ±}, (β s : set Ξ±, dβ.has s β dβ.has s) β dβ = dβ
| β¨sβ, _, _, _β© β¨sβ, _, _, _β© h := have sβ = sβ, from funext $ assume x, propext $ h x,
by subst this
variable (d : dynkin_system Ξ±)
lemma has_compl_iff {a} : d.has aαΆ β d.has a :=
β¨Ξ» h, by simpa using d.has_compl h, Ξ» h, d.has_compl hβ©
lemma has_univ : d.has univ :=
by simpa using d.has_compl d.has_empty
theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β set Ξ±}
(hd : pairwise (disjoint on f)) (h : β i, d.has (f i)) : d.has (β i, f i) :=
by { rw β encodable.Union_decode2, exact
d.has_Union_nat (Union_decode2_disjoint_on hd)
(Ξ» n, encodable.Union_decode2_cases d.has_empty h) }
theorem has_union {sβ sβ : set Ξ±}
(hβ : d.has sβ) (hβ : d.has sβ) (h : sβ β© sβ β β
) : d.has (sβ βͺ sβ) :=
by { rw union_eq_Union, exact
d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 β¨hβ, hββ©) }
lemma has_diff {sβ sβ : set Ξ±} (hβ : d.has sβ) (hβ : d.has sβ) (h : sβ β sβ) : d.has (sβ \ sβ) :=
begin
apply d.has_compl_iff.1,
simp [diff_eq, compl_inter],
exact d.has_union (d.has_compl hβ) hβ (Ξ» x β¨hβ, hββ©, hβ (h hβ)),
end
instance : partial_order (dynkin_system Ξ±) :=
{ le := Ξ» mβ mβ, mβ.has β€ mβ.has,
le_refl := assume a b, le_refl _,
le_trans := assume a b c, le_trans,
le_antisymm := assume a b hβ hβ, ext $ assume s, β¨hβ s, hβ sβ© }
/-- Every measurable space (Ο-algebra) forms a Dynkin system -/
def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± :=
{ has := m.measurable_set',
has_empty := m.measurable_set_empty,
has_compl := m.measurable_set_compl,
has_Union_nat := assume f _ hf, m.measurable_set_Union f hf }
lemma of_measurable_space_le_of_measurable_space_iff {mβ mβ : measurable_space Ξ±} :
of_measurable_space mβ β€ of_measurable_space mβ β mβ β€ mβ :=
iff.rfl
/-- The least Dynkin system containing a collection of basic sets.
This inductive type gives the underlying collection of sets. -/
inductive generate_has (s : set (set Ξ±)) : set Ξ± β Prop
| basic : β t β s, generate_has t
| empty : generate_has β
| compl : β {a}, generate_has a β generate_has aαΆ
| Union : β {f : β β set Ξ±}, pairwise (disjoint on f) β
(β i, generate_has (f i)) β generate_has (β i, f i)
lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sαΆ β generate_has C s :=
by { refine β¨_, generate_has.complβ©, intro h, convert generate_has.compl h, simp }
/-- The least Dynkin system containing a collection of basic sets. -/
def generate (s : set (set Ξ±)) : dynkin_system Ξ± :=
{ has := generate_has s,
has_empty := generate_has.empty,
has_compl := assume a, generate_has.compl,
has_Union_nat := assume f, generate_has.Union }
lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl
instance : inhabited (dynkin_system Ξ±) := β¨generate univβ©
/-- If a Dynkin system is closed under binary intersection, then it forms a `Ο`-algebra. -/
def to_measurable_space (h_inter : β sβ sβ, d.has sβ β d.has sβ β d.has (sβ β© sβ)) :=
{ measurable_space .
measurable_set' := d.has,
measurable_set_empty := d.has_empty,
measurable_set_compl := assume s h, d.has_compl h,
measurable_set_Union := assume f hf,
have β n, d.has (disjointed f n),
from assume n, disjointed_induct (hf n)
(assume t i h, h_inter _ _ h $ d.has_compl $ hf i),
have d.has (β n, disjointed f n), from d.has_Union disjoint_disjointed this,
by rwa [Union_disjointed] at this }
lemma of_measurable_space_to_measurable_space
(h_inter : β sβ sβ, d.has sβ β d.has sβ β d.has (sβ β© sβ)) :
of_measurable_space (d.to_measurable_space h_inter) = d :=
ext $ assume s, iff.rfl
/-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s β© t | t β d}`. -/
def restrict_on {s : set Ξ±} (h : d.has s) : dynkin_system Ξ± :=
{ has := Ξ» t, d.has (t β© s),
has_empty := by simp [d.has_empty],
has_compl := assume t hts,
have tαΆ β© s = ((t β© s)αΆ) \ sαΆ,
from set.ext $ assume x, by { by_cases x β s; simp [h] },
by { rw [this], exact d.has_diff (d.has_compl hts) (d.has_compl h)
(compl_subset_compl.mpr $ inter_subset_right _ _) },
has_Union_nat := assume f hd hf,
begin
rw [inter_comm, inter_Union],
apply d.has_Union_nat,
{ exact Ξ» i j h x β¨β¨_, hββ©, _, hββ©, hd i j h β¨hβ, hββ© },
{ simpa [inter_comm] using hf },
end }
lemma generate_le {s : set (set Ξ±)} (h : β t β s, d.has t) : generate s β€ d :=
Ξ» t ht, ht.rec_on h d.has_empty
(assume a _ h, d.has_compl h)
(assume f hd _ hf, d.has_Union hd hf)
lemma generate_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±}
(hs : (generate C).has s) : (generate_from C).measurable_set' s :=
generate_le (of_measurable_space (generate_from C)) (Ξ» t, measurable_set_generate_from) s hs
lemma generate_inter {s : set (set Ξ±)}
(hs : is_pi_system s) {tβ tβ : set Ξ±}
(htβ : (generate s).has tβ) (htβ : (generate s).has tβ) : (generate s).has (tβ β© tβ) :=
have generate s β€ (generate s).restrict_on htβ,
from generate_le _ $ assume sβ hsβ,
have (generate s).has sβ, from generate_has.basic sβ hsβ,
have generate s β€ (generate s).restrict_on this,
from generate_le _ $ assume sβ hsβ,
show (generate s).has (sβ β© sβ), from
(sβ β© sβ).eq_empty_or_nonempty.elim
(Ξ» h, h.symm βΈ generate_has.empty)
(Ξ» h, generate_has.basic _ (hs _ _ hsβ hsβ h)),
have (generate s).has (tβ β© sβ), from this _ htβ,
show (generate s).has (sβ β© tβ), by rwa [inter_comm],
this _ htβ
/--
If we have a collection of sets closed under binary intersections, then the Dynkin system it
generates is equal to the Ο-algebra it generates.
This result is known as the Ο-Ξ» theorem.
A collection of sets closed under binary intersection is called a "Ο-system" if it is non-empty.
-/
lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) :
generate_from s = (generate s).to_measurable_space (assume tβ tβ, generate_inter hs) :=
le_antisymm
(generate_from_le $ assume t ht, generate_has.basic t ht)
(of_measurable_space_le_of_measurable_space_iff.mp $
by { rw [of_measurable_space_to_measurable_space],
exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) })
end dynkin_system
lemma induction_on_inter {C : set Ξ± β Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±]
(h_eq : m = generate_from s)
(h_inter : is_pi_system s)
(h_empty : C β
) (h_basic : β t β s, C t) (h_compl : β t, measurable_set t β C t β C tαΆ)
(h_union : β f : β β set Ξ±, pairwise (disjoint on f) β
(β i, measurable_set (f i)) β (β i, C (f i)) β C (β i, f i)) :
β β¦tβ¦, measurable_set t β C t :=
have eq : measurable_set = dynkin_system.generate_has s,
by { rw [h_eq, dynkin_system.generate_from_eq h_inter], refl },
assume t ht,
have dynkin_system.generate_has s t, by rwa [eq] at ht,
this.rec_on h_basic h_empty
(assume t ht, h_compl t $ by { rw [eq], exact ht })
(assume f hf ht, h_union f hf $ assume i, by { rw [eq], exact ht _ })
end measurable_space
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_sets, measurable_set.empty, empty_subset _β©β©
instance is_measurably_generated_top : is_measurably_generated (β€ : filter Ξ±) :=
β¨Ξ» s hs, β¨univ, univ_mem_sets, 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_lift' {f : filter Ξ±} [is_measurably_generated f]
{p : set Ξ± β Prop} (h : βαΆ s in f.lift' powerset, p s) :
β s β f, measurable_set s β§ p s :=
let β¨s, hsf, hsβ© := eventually_lift'_powerset.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, htβ©,
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_sets hs'f hs'g, hmf.inter hmg, _β©,
exact subset.trans (inter_subset_inter hs'sf hs'sg) ht
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 β
_ 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_iff] at hs,
rcases hs with β¨t, ht, β¨V, hVf, hVsβ©β©,
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_iff],
refine β¨t, ht, U, hUf, subset.refl _β© },
{ haveI := ht.countable.to_encodable,
refine measurable_set.Inter (Ξ» i, (hU i).1) },
{ exact subset.trans (Inter_subset_Inter $ Ξ» i, (hU i).2) hVs }
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 _β©
|
04c088d6372caa05387d942d175d2f5b898fb662 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/data/nat/enat.lean | 45cb97c4043df9ea18f9997a17925170f62ac03a | [
"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 | 7,898 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
Natural numbers with infinity, represented as roption β.
-/
import data.pfun algebra.ordered_group
import tactic.norm_cast
open roption lattice
def enat : Type := roption β
namespace enat
instance : has_zero enat := β¨some 0β©
instance : has_one enat := β¨some 1β©
instance : has_add enat := β¨Ξ» x y, β¨x.dom β§ y.dom, Ξ» h, get x h.1 + get y h.2β©β©
instance : has_coe β enat := β¨someβ©
instance (n : β) : decidable (n : enat).dom := is_true trivial
@[simp] lemma coe_inj {x y : β} : (x : enat) = y β x = y := roption.some_inj
instance : add_comm_monoid enat :=
{ add := (+),
zero := (0),
add_comm := Ξ» x y, roption.ext' and.comm (Ξ» _ _, add_comm _ _),
zero_add := Ξ» x, roption.ext' (true_and _) (Ξ» _ _, zero_add _),
add_zero := Ξ» x, roption.ext' (and_true _) (Ξ» _ _, add_zero _),
add_assoc := Ξ» x y z, roption.ext' and.assoc (Ξ» _ _, add_assoc _ _ _) }
instance : has_le enat := β¨Ξ» x y, β h : y.dom β x.dom, β hy : y.dom, x.get (h hy) β€ y.get hyβ©
instance : has_top enat := β¨noneβ©
instance : has_bot enat := β¨0β©
instance : has_sup enat := β¨Ξ» x y, β¨x.dom β§ y.dom, Ξ» h, x.get h.1 β y.get h.2β©β©
@[elab_as_eliminator] protected lemma cases_on {P : enat β Prop} : β a : enat,
P β€ β (β n : β, P n) β P a :=
roption.induction_on
@[simp] lemma top_add (x : enat) : β€ + x = β€ :=
roption.ext' (false_and _) (Ξ» h, h.left.elim)
@[simp] lemma add_top (x : enat) : x + β€ = β€ :=
by rw [add_comm, top_add]
@[simp, squash_cast] lemma coe_zero : ((0 : β) : enat) = 0 := rfl
@[simp, squash_cast] lemma coe_one : ((1 : β) : enat) = 1 := rfl
@[simp, move_cast] lemma coe_add (x y : β) : ((x + y : β) : enat) = x + y :=
roption.ext' (and_true _).symm (Ξ» _ _, rfl)
@[simp] lemma coe_add_get {x : β} {y : enat} (h : ((x : enat) + y).dom) :
get ((x : enat) + y) h = x + get y h.2 := rfl
@[simp] lemma get_add {x y : enat} (h : (x + y).dom) :
get (x + y) h = x.get h.1 + y.get h.2 := rfl
@[simp, squash_cast] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x :=
roption.ext' (iff_of_true trivial h) (Ξ» _ _, rfl)
@[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl
@[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl
lemma dom_of_le_some {x : enat} {y : β} : x β€ y β x.dom :=
Ξ» β¨h, _β©, h trivial
instance : partial_order enat :=
{ le := (β€),
le_refl := Ξ» x, β¨id, Ξ» _, le_refl _β©,
le_trans := Ξ» x y z β¨hxyβ, hxyββ© β¨hyzβ, hyzββ©,
β¨hxyβ β hyzβ, Ξ» _, le_trans (hxyβ _) (hyzβ _)β©,
le_antisymm := Ξ» x y β¨hxyβ, hxyββ© β¨hyxβ, hyxββ©, roption.ext' β¨hyxβ, hxyββ©
(Ξ» _ _, le_antisymm (hxyβ _) (hyxβ _)) }
@[simp, elim_cast] lemma coe_le_coe {x y : β} : (x : enat) β€ y β x β€ y :=
β¨Ξ» β¨_, hβ©, h trivial, Ξ» h, β¨Ξ» _, trivial, Ξ» _, hβ©β©
@[simp, elim_cast] lemma coe_lt_coe {x y : β} : (x : enat) < y β x < y :=
by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe]
lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} :
x.get hx β€ y.get hy β x β€ y :=
by conv { to_lhs, rw [β coe_le_coe, coe_get, coe_get]}
instance semilattice_sup_bot : semilattice_sup_bot enat :=
{ sup := (β),
bot := (β₯),
bot_le := Ξ» _, β¨Ξ» _, trivial, Ξ» _, nat.zero_le _β©,
le_sup_left := Ξ» _ _, β¨and.left, Ξ» _, le_sup_leftβ©,
le_sup_right := Ξ» _ _, β¨and.right, Ξ» _, le_sup_rightβ©,
sup_le := Ξ» x y z β¨hxβ, hxββ© β¨hyβ, hyββ©, β¨Ξ» hz, β¨hxβ hz, hyβ hzβ©,
Ξ» _, sup_le (hxβ _) (hyβ _)β©,
..enat.partial_order }
instance order_top : order_top enat :=
{ top := (β€),
le_top := Ξ» x, β¨Ξ» h, false.elim h, Ξ» hy, false.elim hyβ©,
..enat.semilattice_sup_bot }
lemma coe_lt_top (x : β) : (x : enat) < β€ :=
lt_of_le_of_ne le_top (Ξ» h, absurd (congr_arg dom h) true_ne_false)
@[simp] lemma coe_ne_top (x : β) : (x : enat) β β€ := ne_of_lt (coe_lt_top x)
lemma pos_iff_one_le {x : enat} : 0 < x β 1 β€ x :=
enat.cases_on x β¨Ξ» _, le_top, Ξ» _, coe_lt_top _β©
(Ξ» n, β¨Ξ» h, enat.coe_le_coe.2 (enat.coe_lt_coe.1 h),
Ξ» h, enat.coe_lt_coe.2 (enat.coe_le_coe.1 h)β©)
noncomputable instance : decidable_linear_order enat :=
{ le_total := Ξ» x y, enat.cases_on x
(or.inr le_top) (enat.cases_on y (Ξ» _, or.inl le_top)
(Ξ» x y, (le_total x y).elim (or.inr β coe_le_coe.2)
(or.inl β coe_le_coe.2))),
decidable_le := classical.dec_rel _,
..enat.partial_order }
noncomputable instance : bounded_lattice enat :=
{ inf := min,
inf_le_left := min_le_left,
inf_le_right := min_le_right,
le_inf := Ξ» _ _ _, le_min,
..enat.order_top,
..enat.semilattice_sup_bot }
lemma sup_eq_max {a b : enat} : a β b = max a b :=
le_antisymm (sup_le (le_max_left _ _) (le_max_right _ _))
(max_le le_sup_left le_sup_right)
lemma inf_eq_min {a b : enat} : a β b = min a b := rfl
instance : ordered_comm_monoid enat :=
{ add_le_add_left := Ξ» a b β¨hβ, hββ© c,
enat.cases_on c (by simp)
(Ξ» c, β¨Ξ» h, and.intro trivial (hβ h.2),
Ξ» _, add_le_add_left (hβ _) cβ©),
lt_of_add_lt_add_left := Ξ» a b c, enat.cases_on a
(Ξ» h, by simpa [lt_irrefl] using h)
(Ξ» a, enat.cases_on b
(Ξ» h, absurd h (not_lt_of_ge (by rw add_top; exact le_top)))
(Ξ» b, enat.cases_on c
(Ξ» _, coe_lt_top _)
(Ξ» c h, coe_lt_coe.2 (by rw [β coe_add, β coe_add, coe_lt_coe] at h;
exact lt_of_add_lt_add_left h)))),
..enat.decidable_linear_order,
..enat.add_comm_monoid }
instance : canonically_ordered_monoid enat :=
{ le_iff_exists_add := Ξ» a b, enat.cases_on b
(iff_of_true le_top β¨β€, (add_top _).symmβ©)
(Ξ» b, enat.cases_on a
(iff_of_false (not_le_of_gt (coe_lt_top _))
(not_exists.2 (Ξ» x, ne_of_lt (by rw [top_add]; exact coe_lt_top _))))
(Ξ» a, β¨Ξ» h, β¨(b - a : β),
by rw [β coe_add, coe_inj, add_comm, nat.sub_add_cancel (coe_le_coe.1 h)]β©,
(Ξ» β¨c, hcβ©, enat.cases_on c
(Ξ» hc, hc.symm βΈ show (a : enat) β€ a + β€, by rw [add_top]; exact le_top)
(Ξ» c (hc : (b : enat) = a + c),
coe_le_coe.2 (by rw [β coe_add, coe_inj] at hc;
rw hc; exact nat.le_add_right _ _)) hc)β©)),
..enat.semilattice_sup_bot,
..enat.ordered_comm_monoid }
section with_top
def to_with_top (x : enat) [decidable x.dom]: with_top β := x.to_option
lemma to_with_top_top : to_with_top β€ = β€ := rfl
@[simp] lemma to_with_top_top' {h : decidable (β€ : enat).dom} : to_with_top β€ = β€ :=
by convert to_with_top_top
lemma to_with_top_zero : to_with_top 0 = 0 := rfl
@[simp] lemma to_with_top_zero' {h : decidable (0 : enat).dom}: to_with_top 0 = 0 :=
by convert to_with_top_zero
lemma to_with_top_coe (n : β) : to_with_top n = n := rfl
@[simp] lemma to_with_top_coe' (n : β) {h : decidable (n : enat).dom} : to_with_top (n : enat) = n :=
by convert to_with_top_coe n
@[simp] lemma to_with_top_le {x y : enat} : Ξ [decidable x.dom]
[decidable y.dom], by exactI to_with_top x β€ to_with_top y β x β€ y :=
enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp))
@[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] :
to_with_top x < to_with_top y β x < y :=
by simp only [lt_iff_le_not_le, to_with_top_le]
end with_top
lemma lt_wf : well_founded ((<) : enat β enat β Prop) :=
show well_founded (Ξ» a b : enat, a < b),
by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff};
exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf)
instance : has_well_founded enat := β¨(<), lt_wfβ©
end enat
|
1a83123e17c3f351203ef432f4b676e80ba0a2fc | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /src/Init/NotationExtra.lean | 916ec8d78282bdaef7f39de717d3cfe804d3da6e | [
"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 | 6,812 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Extra notation that depends on Init/Meta
-/
prelude
import Init.Meta
import Init.Data.Array.Subarray
namespace Lean
-- Auxiliary parsers and functions for declaring notation with binders
syntax binderIdent := ident <|> "_"
syntax unbracktedExplicitBinders := binderIdent+ (" : " term)?
syntax bracketedExplicitBinders := "(" binderIdent+ " : " term ")"
syntax explicitBinders := bracketedExplicitBinders+ <|> unbracktedExplicitBinders
def expandExplicitBindersAux (combinator : Syntax) (idents : Array Syntax) (type? : Option Syntax) (body : Syntax) : MacroM Syntax :=
let rec loop (i : Nat) (acc : Syntax) := do
match i with
| 0 => pure acc
| i+1 =>
let ident := idents[i][0]
let acc β match ident.isIdent, type? with
| true, none => `($combinator fun $ident => $acc)
| true, some type => `($combinator fun $ident:ident : $type => $acc)
| false, none => `($combinator fun _ => $acc)
| false, some type => `($combinator fun _ : $type => $acc)
loop i (acc.copyInfo (β getRef))
loop idents.size body
def expandBrackedBindersAux (combinator : Syntax) (binders : Array Syntax) (body : Syntax) : MacroM Syntax :=
let rec loop (i : Nat) (acc : Syntax) := do
match i with
| 0 => pure acc
| i+1 =>
let idents := binders[i][1].getArgs
let type := binders[i][3]
loop i (β expandExplicitBindersAux combinator idents (some type) acc)
loop binders.size body
def expandExplicitBinders (combinatorDeclName : Name) (explicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do
let combinator := mkIdentFrom (β getRef) combinatorDeclName
let explicitBinders := explicitBinders[0]
if explicitBinders.getKind == `Lean.unbracktedExplicitBinders then
let idents := explicitBinders[0].getArgs
let type? := if explicitBinders[1].isNone then none else some explicitBinders[1][1]
expandExplicitBindersAux combinator idents type? body
else if explicitBinders.getArgs.all (Β·.getKind == `Lean.bracketedExplicitBinders) then
expandBrackedBindersAux combinator explicitBinders.getArgs body
else
Macro.throwError "unexpected explicit binder"
def expandBrackedBinders (combinatorDeclName : Name) (bracketedExplicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do
let combinator := mkIdentFrom (β getRef) combinatorDeclName
expandBrackedBindersAux combinator #[bracketedExplicitBinders] body
syntax unifConstraint := term (" =?= " <|> " β ") term
syntax unifConstraintElem := colGe unifConstraint ", "?
syntax attrKind "unif_hint " (ident)? bracketedBinder* " where " withPosition(unifConstraintElem*) ("|-" <|> "β’ ") unifConstraint : command
private def mkHintBody (cs : Array Syntax) (p : Syntax) : MacroM Syntax := do
let mut body β `($(p[0]) = $(p[2]))
for c in cs.reverse do
body β `($(c[0][0]) = $(c[0][2]) β $body)
return body
macro_rules
| `($kind:attrKind unif_hint $bs:explicitBinder* where $cs* |- $p) => do
let body β mkHintBody cs p
`(@[$kind:attrKind unificationHint] def hint $bs:explicitBinder* : Sort _ := $body)
| `($kind:attrKind unif_hint $n:ident $bs* where $cs* |- $p) => do
let body β mkHintBody cs p
`(@[$kind:attrKind unificationHint] def $n:ident $bs:explicitBinder* : Sort _ := $body)
end Lean
open Lean
macro "β" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b
macro "exists" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b
macro "Ξ£" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Sigma xs b
macro "Ξ£'" xs:explicitBinders ", " b:term : term => expandExplicitBinders `PSigma xs b
macro:35 xs:bracketedExplicitBinders "Γ" b:term:35 : term => expandBrackedBinders `Sigma xs b
macro:35 xs:bracketedExplicitBinders "Γ'" b:term:35 : term => expandBrackedBinders `PSigma xs b
syntax "funext " (colGt term:max)+ : tactic
macro_rules
| `(tactic|funext $xs*) =>
if xs.size == 1 then
`(tactic| apply funext; intro $(xs[0]):term)
else
`(tactic| apply funext; intro $(xs[0]):term; funext $(xs[1:])*)
macro_rules
| `(%[ $[$x],* | $k ]) =>
if x.size < 8 then
x.foldrM (init := k) fun x k =>
`(List.cons $x $k)
else
let m := x.size / 2
let y := x[m:]
let z := x[:m]
`(let y := %[ $[$y],* | $k ]
%[ $[$z],* | y ])
/-
Expands
```
class abbrev C <params> := D_1, ..., D_n
```
into
```
class C <params> extends D_1, ..., D_n
instance <params> [D_1] ... [D_n] : C <params> :=
C.mk _ ... _ inferInstance ... inferInstance
```
-/
syntax "class " "abbrev " ident bracketedBinder* ":=" withPosition(group(colGe term ","?)*) : command
macro_rules
| `(class abbrev $name:ident $params* := $[ $parents:term $[,]? ]*) => do
let mut auxBinders := #[]
let mut typeArgs := #[]
let mut ctorArgs := #[]
let hole β `(_)
for param in params do
match param with
| `(bracketedBinder| ( $ids:ident* $[: $type:term]? ) ) =>
auxBinders := auxBinders.push (β `(bracketedBinder| { $ids:ident* }) )
typeArgs := typeArgs ++ ids
ctorArgs := ctorArgs ++ ids.map fun _ => hole
| `(bracketedBinder| [ $id:ident : $type:term ] ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs.push hole
ctorArgs := ctorArgs.push hole
| `(bracketedBinder| [ $type:term ] ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs.push hole
ctorArgs := ctorArgs.push hole
| `(bracketedBinder| { $ids:ident* $[: $type ]? } ) =>
auxBinders := auxBinders.push param
typeArgs := typeArgs ++ ids.map fun _ => hole
ctorArgs := typeArgs ++ ids.map fun _ => hole
| _ => Lean.Macro.throwErrorAt param "unexpected binder at `class abbrev` macro"
let inferInst β `(inferInstance)
for parent in parents do
auxBinders := auxBinders.push (β `(bracketedBinder| [ $parent:term ]))
ctorArgs := ctorArgs.push inferInst
let view := Lean.extractMacroScopes name.getId
let ctor := mkIdentFrom name { view with name := view.name ++ `mk }.review
`(class $name:ident $params* extends $[$parents:term],*
instance $auxBinders:explicitBinder* : @ $name:ident $typeArgs* :=
@ $ctor:ident $ctorArgs*)
/-
Similar to `first`, but succeeds only if one the given tactics solves the curretn goal.
-/
syntax "solve " "|"? sepBy1(tacticSeq, "|") : tactic
macro_rules
| `(tactic| solve $[|]? $ts:tacticSeq|*) => `(tactic| focus first $[($ts); done]|*)
|
9da21a648268976532e7933958447af537f1996c | 4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1 | /omin/presheaf2.lean | dd08128ea2a013eea2fe70deedc94465fec48919 | [] | no_license | rwbarton/lean-omin | da209ed061d64db65a8f7f71f198064986f30eb9 | fd733c6d95ef6f4743aae97de5e15df79877c00e | refs/heads/master | 1,674,408,673,325 | 1,607,343,535,000 | 1,607,343,535,000 | 285,150,399 | 9 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,052 | lean | import .def_coords
universe u
namespace o_minimal
open_locale finvec
variables {R : Type u} (S : struc R)
-- We set up a minimal theory of (literal) definable subsets of RβΏ
-- and definable functions between them.
/-- A bundled definable subset of some RβΏ. -/
structure Def : Type u :=
(ambdim : β)
(to_set : set (finvec ambdim R))
(is_definable : S.definable to_set)
variables {S}
instance : has_coe_to_sort (Def S) :=
β¨Type u, Ξ» X, X.to_setβ©
lemma Def.is_def_coords (X : Def S) : S.def_coords (set.univ : set X) :=
begin
unfold struc.def_coords,
convert X.is_definable,
simp
end
variables (S)
def struc.definable_fun {X Y : Def S} (f : X β Y) : Prop :=
S.def_coords {z : X Γ Y | f z.1 = z.2}
variables {S}
@[ext] structure Hom (X Y : Def S) : Type u :=
(to_fun : X β Y)
(is_definable : S.definable_fun to_fun)
instance {X Y : Def S} : has_coe_to_fun (Hom X Y) :=
β¨_, Ξ» f, f.to_funβ©
--instance : category_theory.has_hom (Def S) := { hom := Hom }
local infixr ` βΆ `:10 := Hom
@[simp] lemma Hom.to_fun_eq_coe {X Y : Def S} (f : X βΆ Y) :
f.to_fun = f :=
rfl
-- TODO float out proofs of definability of identity, composition
-- (useful later in presheaf stuff, notably representable instance)
def Def.id (X : Def S) : X βΆ X :=
{ to_fun := id,
is_definable := struc.def_coords.diag X.is_def_coords }
def Hom.comp {X Y Z : Def S} (g : Y βΆ Z) (f : X βΆ Y) : X βΆ Z :=
{ to_fun := g.to_fun β f.to_fun,
is_definable := begin
suffices : S.def_coords {p : X Γ Z | β y, f p.1 = y β§ g y = p.2},
{ convert this,
ext β¨x, zβ©,
simp [struc.definable_fun] },
have dXZY : S.def_coords (set.univ : set ((X Γ Z) Γ Y)) :=
(X.is_def_coords.prod_univ Z.is_def_coords).prod_univ Y.is_def_coords,
apply struc.def_coords.exists,
apply struc.def_coords.inter,
{ let Ο : (X Γ Z) Γ Y β X Γ Y := Ξ» p, (p.1.1, p.2),
have : is_reindexing R Ο :=
is_reindexing.prod R ((is_reindexing.fst R).comp R (is_reindexing.fst R)) (is_reindexing.snd R),
refine struc.def_coords.reindex dXZY this f.is_definable },
{ let Ο : (X Γ Z) Γ Y β Y Γ Z := Ξ» p, (p.2, p.1.2),
have : is_reindexing R Ο :=
is_reindexing.prod R (is_reindexing.snd R) ((is_reindexing.snd R).comp R (is_reindexing.fst R)),
refine struc.def_coords.reindex dXZY this g.is_definable },
end }
lemma Hom.comp_id {X Y : Def S} (f : X βΆ Y) : f.comp (Def.id X) = f :=
by { ext, refl }
lemma Hom.id_comp {X Y : Def S} (f : X βΆ Y) : (Def.id Y).comp f = f :=
by { ext, refl }
lemma Hom.comp_assoc {W X Y Z : Def S} (h : Y βΆ Z) (g : X βΆ Y) (f : W βΆ X) :
(h.comp g).comp f = h.comp (g.comp f) :=
rfl
def pt : Def S :=
{ ambdim := 0,
to_set := set.univ,
is_definable := S.definable_univ 0 }
instance pt.unique : unique (pt : Def S) :=
β¨β¨β¨fin_zero_elim, trivialβ©β©, Ξ» x, by { ext i, fin_cases i }β©
/-! ### Presheaf stuff -/
variables (S)
-- TODO: generalize to Sort?
class definable_psh (X : Type*) :=
(definable : Ξ {K : Def S}, (K β X) β Prop)
(definable_precomp : β {L K : Def S} (Ο : L βΆ K) {f : K β X},
definable f β definable (f β Ο))
-- TODO: apply bug??
def definable {X : Type*} [definable_psh S X] (x : X) : Prop :=
definable_psh.definable (Ξ» (_ : (pt : Def S)), x)
variables {S}
def definable_psh.definable' {X : Type*} (h : definable_psh S X) {K : Def S} (f : K β X) : Prop :=
definable_psh.definable f
instance Def.definable_psh (X : Def S) : definable_psh S X :=
{ definable := Ξ» K f, S.definable_fun f,
definable_precomp := begin
rintros L K Ο f h,
exact ((β¨f, hβ© : K βΆ X).comp Ο).is_definable
end }
lemma pt.definable {K : Def S} {f : K β (pt : Def S)} : definable_psh.definable f :=
begin
change S.definable _,
convert K.is_def_coords using 1,
ext x,
split,
{ rintros β¨β¨k, pβ©, -, rflβ©,
refine β¨k, trivial, _β©,
simp },
{ rintros β¨k, -, rflβ©,
refine β¨β¨k, default _β©, show _ = _, by cc, _β©,
simp }
end
instance {X Y : Type*} [definable_psh S X] [definable_psh S Y] : definable_psh S (X Γ Y) :=
{ definable := Ξ» K f, definable_psh.definable (prod.fst β f) β§ definable_psh.definable (prod.snd β f),
definable_precomp := begin
rintros L K Ο _ β¨hβ, hββ©,
exact β¨definable_psh.definable_precomp Ο hβ, definable_psh.definable_precomp Ο hββ©,
end }
instance function.definable_psh {X Y : Type*} [hX : definable_psh S X] [hY : definable_psh S Y] :
definable_psh S (X β Y) :=
{ definable := Ξ» K f, β (M : Def S) {g : M β K Γ X} (h : definable_psh.definable g),
definable_psh.definable (function.uncurry f β g),
definable_precomp := Ξ» L K Ο f hf M g hg, begin
suffices : definable_psh.definable (Ξ» m, (Ο (g m).1, (g m).2)),
{ apply hf M this },
split,
{ exact definable_psh.definable_precomp β¨Ξ» m, (g m).1, hg.1β©
(show definable_psh.definable Ο, from Ο.is_definable) },
{ exact hg.2 }
end }
lemma definable_fun {X Y : Type*} [definable_psh S X] [definable_psh S Y]
{f : X β Y} : definable S f β
β {K : Def S} (Ο : K β X), definable_psh.definable Ο β definable_psh.definable (f β Ο) :=
begin
split; intro H,
{ intros K Ο hΟ,
-- TODO: This proof is awkward
specialize H K,
swap,
{ exact (Ξ» k, (default _, Ο k)) },
exact H β¨pt.definable, hΟβ© },
{ intros K Ο hΟ,
exact H _ hΟ.2 }
end
lemma definable_app {X Y : Type*} [definable_psh S X] [definable_psh S Y]
{f : X β Y} (hf : definable S f) {x : X} (hx : definable S x) : definable S (f x) :=
begin
rw definable_fun at hf,
exact hf _ hx
end
lemma definable.app_ctx {Ξ X Y : Type*} [definable_psh S Ξ] [definable_psh S X] [definable_psh S Y]
{f : Ξ β X β Y} (hf : definable S f) {x : Ξ β X} (hx : definable S x) :
definable S (Ξ» Ξ³, f Ξ³ (x Ξ³)) :=
begin
rw definable_fun at β’ hf hx,
intros K Ο hΟ,
change definable_psh.definable (Ξ» k, f (Ο k) (x (Ο k))),
specialize hf Ο hΟ,
specialize hf K,
swap, { exact Ξ» k, (k, x (Ο k)) },
exact hf β¨(Def.id K).is_definable, hx Ο hΟβ©
end
lemma definable_yoneda {K : Def S} {X : Type*} [definable_psh S X]
{f : K β X} : definable_psh.definable f β definable S f :=
begin
rw definable_fun,
split,
{ intros h L Ο hΟ,
exact definable_psh.definable_precomp β¨Ο, hΟβ© h },
{ intros H,
refine H id _,
exact (Def.id K).is_definable }
end
lemma definable_prod_mk {X Y : Type*} [definable_psh S X] [definable_psh S Y] :
definable S (prod.mk : X β Y β X Γ Y) :=
-- I have no idea how to come up with these proofs.
-- Maybe we should tweak the type of definable_precomp (or write a lemma)
-- that takes the underlying map Ο and its proof of definability separately
-- so that Lean has a better chance of guessing what's happening?
Ξ» L g h L' g' h',
β¨definable_psh.definable_precomp β¨Ξ» x, (g' x).fst, h'.1β© h.2, h'.2β©
lemma definable_fst {X Y : Type*} [definable_psh S X] [definable_psh S Y] :
definable S (prod.fst : X Γ Y β X) :=
begin
rw definable_fun,
intros K Ο hΟ,
exact hΟ.1
end
lemma definable_snd {X Y : Type*} [definable_psh S X] [definable_psh S Y] :
definable S (prod.snd : X Γ Y β Y) :=
begin
rw definable_fun,
intros K Ο hΟ,
exact hΟ.2
end
lemma definable.prod_mk {W X Y : Type*} [definable_psh S W] [definable_psh S X] [definable_psh S Y]
{f : W β X} (hf : definable S f) {g : W β Y} (hg : definable S g) :
definable S (Ξ» w, (f w, g w)) :=
begin
rw definable_fun at β’ hf hg,
intros K Ο hΟ,
exact β¨hf Ο hΟ, hg Ο hΟβ©
end
lemma definable_funβ {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z]
{f : X β Y β Z} :
(β {L : Def S} (Ο : L β X), definable S Ο β definable S (f β Ο)) β
(β {L : Def S} (Ο : L β X Γ Y), definable S Ο β definable S (function.uncurry f β Ο)) :=
begin
split; intro H,
{ intros L Ο hΟ,
rw definable_fun at β’,
intros K Ο hΟ,
have : definable S (prod.fst β Ο),
{ rw βdefinable_yoneda at β’ hΟ,
exact hΟ.1 },
specialize H (Ξ» l, (Ο l).1) this,
rw definable_fun at H,
specialize H Ο hΟ,
specialize H K,
swap, { exact Ξ» k, (k, (Ο (Ο k)).2) },
refine H β¨(Def.id K).is_definable, _β©, clear H,
rw βdefinable_yoneda at hΟ,
exact definable_psh.definable_precomp β¨Ο, hΟβ© hΟ.2 },
{ intros L Ο hΟ,
rw definable_fun,
intros K Ο hΟ,
intros K' Ο' hΟ',
dsimp [function.uncurry, function.comp],
specialize H (Ξ» k', (Ο (Ο (Ο' k').1), (Ο' k').2)),
have : definable S (Ξ» k', (Ο (Ο (Ο' k').1), (Ο' k').2)),
{ rw βdefinable_yoneda at β’ hΟ,
split,
{ refine definable_psh.definable_precomp β¨Ξ» k', Ο (Ο' k').fst, _β© hΟ,
exact (Hom.comp β¨Ο, hΟβ© β¨_, hΟ'.1β©).is_definable },
{ exact hΟ'.2 } },
specialize H this,
rw definable_fun at H,
exact H _ (Def.id _).is_definable }
end
lemma definable_comp {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] :
definable S (function.comp : (Y β Z) β (X β Y) β (X β Z)) :=
begin
-- TODO: Make these 4 lines a lemma.
rw definable_fun,
intros Lβ Οβ hΟβ,
rw definable_yoneda at β’ hΟβ,
revert Lβ,
-- end lemma
rw definable_funβ,
rw definable_funβ,
rintros L Ο hΟ,
rw βdefinable_yoneda at hΟ,
obtain β¨β¨hΟβ, hΟββ©, hΟββ© := hΟ,
rw definable_yoneda at hΟβ hΟβ hΟβ,
dsimp [function.uncurry, function.comp],
exact hΟβ.app_ctx (hΟβ.app_ctx hΟβ)
end
lemma definable.comp {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z]
{g : Y β Z} (hg : definable S g) {f : X β Y} (hf : definable S f) :
definable S (g β f) :=
definable_app (definable_app definable_comp hg) hf
lemma definable.comp_ctx {Ξ X Y Z : Type*} [definable_psh S Ξ] [definable_psh S X] [definable_psh S Y] [definable_psh S Z]
{g : Ξ β Y β Z} (hg : definable S g) {f : Ξ β X β Y} (hf : definable S f) :
definable S (Ξ» Ξ³, g Ξ³ β f Ξ³) :=
definable.app_ctx (definable.comp definable_comp hg) hf
lemma definable_curry {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] :
definable S (function.curry : (X Γ Y β Z) β X β Y β Z) :=
begin
-- TODO: Make these 4 lines a lemma.
rw definable_fun,
intros Lβ Οβ hΟβ,
rw definable_yoneda at β’ hΟβ,
revert Lβ,
-- end lemma
rw definable_funβ,
rw definable_funβ,
rintros L Ο hΟ,
rw βdefinable_yoneda at hΟ,
obtain β¨β¨hΟβ, hΟββ©, hΟββ© := hΟ,
rw definable_yoneda at hΟβ hΟβ hΟβ,
exact definable.app_ctx hΟβ (hΟβ.prod_mk hΟβ)
end
instance Prop.definable_psh : definable_psh S Prop :=
{ definable := Ξ» K s, S.def_coords s,
definable_precomp := Ξ» L K Ο f hf, sorry } -- preimage
instance set.definable_psh {X : Type*} [definable_psh S X] : definable_psh S (set X) :=
show definable_psh S (X β Prop), by apply_instance
lemma definable_and : definable S (β§) :=
begin
suffices : definable S (Ξ» r : Prop Γ Prop, r.1 β§ r.2),
{ exact definable_app definable_curry this },
rw definable_fun,
rintros K Ο β¨hΟβ, hΟββ©,
exact hΟβ.inter hΟβ
end
lemma definable.and {W : Type*} [definable_psh S W]
{f : W β Prop} (hf : definable S f) {g : W β Prop} (hg : definable S g) :
definable S (Ξ» w, f w β§ g w) :=
definable.app_ctx (definable.comp definable_and hf) hg
lemma definable_inter {X : Type*} [definable_psh S X] :
definable S ((β©) : set X β set X β set X) :=
begin
suffices : definable S (Ξ» (r : set X Γ set X) (x : X), r.1 x β§ r.2 x),
{ exact (definable_app definable_curry this : _) },
-- TODO: Make these 4 lines a lemma.
rw definable_fun,
intros Lβ Οβ hΟβ,
rw definable_yoneda at β’ hΟβ,
revert Lβ,
-- end lemma
rw definable_funβ,
intros L Ο hΟ,
rw βdefinable_yoneda at hΟ,
obtain β¨β¨hΟβ, hΟββ©, hΟββ© := hΟ,
rw definable_yoneda at hΟβ hΟβ hΟβ,
apply definable.and,
{ exact hΟβ.app_ctx hΟβ },
{ exact hΟβ.app_ctx hΟβ }
end
/-
instance foo {X Y : Type*} [definable_psh S X] [definable_psh S Y]
{p : X β Prop}
: definable_psh S (Ξ (x : X) (h : p x), Y) :=
sorry
-/
lemma definable_definable {X : Type*} [definable_psh S X] :
definable S (definable S : X β Prop) :=
begin
rw definable_fun,
intros K Ο hΟ,
change S.def_coords _,
convert K.is_def_coords,
apply set.eq_univ_of_forall,
intro x,
change definable S (Ο x),
apply definable_app,
{ rw βdefinable_yoneda, exact hΟ },
-- now we need to know that every point of a representable guy
-- is definable. this needs definable constants!
sorry
-- In general, the definable elements of a structure
-- might or might not form a definable set.
-- Counterexample: take (β, +, *) without constants;
-- definable elements are the algebraic real numbers,
-- but only tame sets can be definable.
-- However, once the structure has definable constants,
-- then everything is definable and of course the set `univ` is definable.
end
-- Important note: it is definitely *not* true that
-- `definable S` = `set.univ` on *every* X with a definable_psh structure;
-- just represented guys.
/-
similarly, in an o-minimal structure:
lemma definable_finite [DUNLO R] [o_minimal S] :
definable S (set.finite : set R β Prop) := sorry
because "finite" is equivalent to "does not contain an interval"
on the tame = definable sets, which are the only ones that matter.
-/
instance self : definable_psh S R := sorry
variables (S)
#exit
class definable_fam {X : Type*} [definable_psh S X] (Y : X β Sort*) :=
(definable : Ξ {K : Def S} (x : K β X) (hx : definable_psh.definable x), (Ξ k, Y (x k)) β Prop)
-- s.t. blah blah blah...
instance moo {X : Type*} {Y : X β Type*} [definable_psh S X] [definable_fam S Y] :
definable_psh S (Ξ (x : X), Y x) :=
sorry
constant choice : Ξ (X : set R), X.nonempty β X
example : definable S (set.nonempty : set R β Prop) :=
sorry
instance : definable_fam S (set.nonempty : set R β Prop) := sorry
instance pi {X : Type*} [definable_psh S X] {Y Z : X β Sort*} [definable_fam S Y] [definable_fam S Z] :
definable_fam S (Ξ» x, Y x β Z x) :=
sorry
instance subtype {X : Type*} [definable_psh S X] : definable_fam S (Ξ» (s : set X), s) :=
sorry
example : definable S ((Ξ» x hxβ hxβ, choice x hxβ) : Ξ (X : set R), X.nonempty β X.nonempty β X) :=
sorry
-- can we do without this `definable_fam` stuff? even as a hack?
-- or maybe stick with this for now?
/- TODO:
* class represented [has_coordinates R X] expressing compatibility
* prove this notion of definability of functions, sets reduces
to the original one in the represented case
Then:
* prove stuff like `is_least : set R β R β Prop` is definable
-/
end o_minimal
|
72219305e0b51866a81f80b94c9d91fd4f2c7dfa | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/algebra/category/Module/basic.lean | 114af8126410241aaba9e741dbae2eef1b8e98be | [
"Apache-2.0"
] | permissive | khoek/mathlib | bc49a842910af13a3c372748310e86467d1dc766 | aa55f8b50354b3e11ba64792dcb06cccb2d8ee28 | refs/heads/master | 1,588,232,063,837 | 1,587,304,803,000 | 1,587,304,803,000 | 176,688,517 | 0 | 0 | Apache-2.0 | 1,553,070,585,000 | 1,553,070,585,000 | null | UTF-8 | Lean | false | false | 5,847 | lean | /-
Copyright (c) 2019 Robert A. Spencer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert A. Spencer, Markus Himmel
-/
import algebra.module
import algebra.punit_instances
import algebra.category.Group.basic
import category_theory.concrete_category
import category_theory.limits.shapes.zero
import category_theory.limits.shapes.kernels
import linear_algebra.basic
open category_theory
open category_theory.limits
open category_theory.limits.walking_parallel_pair
universe u
variables (R : Type u) [ring R]
/-- The category of R-modules and their morphisms. -/
structure Module :=
(carrier : Type u)
[is_add_comm_group : add_comm_group carrier]
[is_module : module R carrier]
attribute [instance] Module.is_add_comm_group Module.is_module
namespace Module
-- TODO revisit this after #1438 merges, to check coercions and instances are handled consistently
instance : has_coe_to_sort (Module R) :=
{ S := Type u, coe := Module.carrier }
instance : category (Module.{u} R) :=
{ hom := Ξ» M N, M ββ[R] N,
id := Ξ» M, 1,
comp := Ξ» A B C f g, g.comp f }
instance : concrete_category (Module.{u} R) :=
{ forget := { obj := Ξ» R, R, map := Ξ» R S f, (f : R β S) },
forget_faithful := { } }
instance has_forget_to_AddCommGroup : has_forgetβ (Module R) AddCommGroup :=
{ forgetβ :=
{ obj := Ξ» M, AddCommGroup.of M,
map := Ξ» Mβ Mβ f, linear_map.to_add_monoid_hom f } }
/-- The object in the category of R-modules associated to an R-module -/
def of (X : Type u) [add_comm_group X] [module R X] : Module R := β¨Xβ©
instance : inhabited (Module R) := β¨of R punitβ©
@[simp]
lemma of_apply (X : Type u) [add_comm_group X] [module R X] : (of R X : Type u) = X := rfl
variables {R}
/-- Forgetting to the underlying type and then building the bundled object returns the original module. -/
@[simps]
def of_self_iso (M : Module R) : Module.of R M β
M :=
{ hom := π M, inv := π M }
instance : subsingleton (of R punit) :=
by { rw of_apply R punit, apply_instance }
instance : has_zero_object.{u} (Module R) :=
{ zero := of R punit,
unique_to := Ξ» X,
{ default := (0 : punit ββ[R] X),
uniq := Ξ» _, linear_map.ext $ Ξ» x,
have h : x = 0, from subsingleton.elim _ _,
by simp only [h, linear_map.map_zero]},
unique_from := Ξ» X,
{ default := (0 : X ββ[R] punit),
uniq := Ξ» _, linear_map.ext $ Ξ» x, subsingleton.elim _ _ } }
variables {R} {M N U : Module R}
@[simp] lemma id_apply (m : M) : (π M : M β M) m = m := rfl
@[simp] lemma coe_comp (f : M βΆ N) (g : N βΆ U) :
((f β« g) : M β U) = g β f := rfl
instance hom_is_module_hom (f : M βΆ N) :
is_linear_map R (f : M β N) := linear_map.is_linear _
end Module
variables {R}
variables {Xβ Xβ : Type u}
/-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/
@[simps]
def linear_equiv.to_Module_iso
{gβ : add_comm_group Xβ} {gβ : add_comm_group Xβ} {mβ : module R Xβ} {mβ : module R Xβ} (e : Xβ ββ[R] Xβ) :
Module.of R Xβ β
Module.of R Xβ :=
{ hom := (e : Xβ ββ[R] Xβ),
inv := (e.symm : Xβ ββ[R] Xβ),
hom_inv_id' := begin ext, exact e.left_inv x, end,
inv_hom_id' := begin ext, exact e.right_inv x, end, }
namespace category_theory.iso
/-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/
@[simps]
def to_linear_equiv {X Y : Module.{u} R} (i : X β
Y) : X ββ[R] Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
add := by tidy,
smul := by tidy, }.
end category_theory.iso
/-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms in `Module` -/
@[simps]
def linear_equiv_iso_Group_iso {X Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X] [module R Y] :
(X ββ[R] Y) β
(Module.of R X β
Module.of R Y) :=
{ hom := Ξ» e, e.to_Module_iso,
inv := Ξ» i, i.to_linear_equiv, }
namespace Module
section zero_morphisms
instance : has_zero_morphisms.{u} (Module R) :=
{ has_zero := Ξ» M N, β¨0β©,
comp_zero' := Ξ» M N f Z, by ext; erw linear_map.zero_apply,
zero_comp' := Ξ» M N Z f, by ext; erw [linear_map.comp_apply, linear_map.zero_apply,
linear_map.zero_apply, linear_map.map_zero] }
end zero_morphisms
section kernel
variables {R} {M N : Module R} (f : M βΆ N)
/-- The cone on the equalizer diagram of f and 0 induced by the kernel of f -/
def kernel_cone : cone (parallel_pair f 0) :=
{ X := of R f.ker,
Ο :=
{ app := Ξ» j,
match j with
| zero := f.ker.subtype
| one := 0
end,
naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; tidy } } }
/-- The kernel of a linear map is a kernel in the categorical sense -/
def kernel_is_limit : is_limit (kernel_cone f) :=
{ lift := Ξ» s, linear_map.cod_restrict f.ker (fork.ΞΉ s) (Ξ» c, linear_map.mem_ker.2 $
by { erw [β@function.comp_apply _ _ _ f (fork.ΞΉ s) c, βcoe_comp, fork.condition,
has_zero_morphisms.comp_zero (fork.ΞΉ s) N], refl }),
fac' := Ξ» s j, linear_map.ext $ Ξ» x,
begin
rw [coe_comp, function.comp_app, βlinear_map.comp_apply],
cases j,
{ erw @linear_map.subtype_comp_cod_restrict _ _ _ _ _ _ _ _ (fork.ΞΉ s) f.ker _ },
{ rw [βfork.app_zero_left, βfork.app_zero_left], refl }
end,
uniq' := Ξ» s m h, linear_map.ext $ Ξ» x, subtype.ext.2 $
have hβ : (m β« (kernel_cone f).Ο.app zero).to_fun = (s.Ο.app zero).to_fun,
by { congr, exact h zero },
by convert @congr_fun _ _ _ _ hβ x }
end kernel
instance : has_kernels.{u} (Module R) :=
β¨Ξ» _ _ f, β¨kernel_cone f, kernel_is_limit fβ©β©
end Module
instance (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) :=
β¨ Ξ» N, Module.of R N β©
|
87a41d3724ff4bb8d8f5345f33acbf04c3d70d13 | 4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d | /src/Std/Data/PersistentArray.lean | c9c2701e519e1a67f7b57797a0eead9bea08465b | [
"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 | 13,822 | 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
-/
universe u v w
namespace Std
inductive PersistentArrayNode (Ξ± : Type u) where
| node (cs : Array (PersistentArrayNode Ξ±)) : PersistentArrayNode Ξ±
| leaf (vs : Array Ξ±) : PersistentArrayNode Ξ±
deriving Inhabited
namespace PersistentArrayNode
def isNode {Ξ±} : PersistentArrayNode Ξ± β Bool
| node _ => true
| leaf _ => false
end PersistentArrayNode
abbrev PersistentArray.initShift : USize := 5
abbrev PersistentArray.branching : USize := USize.ofNat (2 ^ PersistentArray.initShift.toNat)
structure PersistentArray (Ξ± : Type u) where
/- Recall that we run out of memory if we have more than `usizeSz/8` elements.
So, we can stop adding elements at `root` after `size > usizeSz`, and
keep growing the `tail`. This modification allow us to use `USize` instead
of `Nat` when traversing `root`. -/
root : PersistentArrayNode Ξ± := PersistentArrayNode.node (Array.mkEmpty PersistentArray.branching.toNat)
tail : Array Ξ± := Array.mkEmpty PersistentArray.branching.toNat
size : Nat := 0
shift : USize := PersistentArray.initShift
tailOff : Nat := 0
deriving Inhabited
abbrev PArray (Ξ± : Type u) := PersistentArray Ξ±
namespace PersistentArray
/- TODO: use proofs for showing that array accesses are not out of bounds.
We can do it after we reimplement the tactic framework. -/
variable {Ξ± : Type u}
open Std.PersistentArrayNode
def empty : PersistentArray Ξ± := {}
def isEmpty (a : PersistentArray Ξ±) : Bool := a.size == 0
def mkEmptyArray : Array Ξ± := Array.mkEmpty branching.toNat
abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift
abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift
abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1)
partial def getAux [Inhabited Ξ±] : PersistentArrayNode Ξ± β USize β USize β Ξ±
| node cs, i, shift => getAux (cs.get! (div2Shift i shift).toNat) (mod2Shift i shift) (shift - initShift)
| leaf cs, i, _ => cs.get! i.toNat
def get! [Inhabited Ξ±] (t : PersistentArray Ξ±) (i : Nat) : Ξ± :=
if i >= t.tailOff then
t.tail.get! (i - t.tailOff)
else
getAux t.root (USize.ofNat i) t.shift
def getOp [Inhabited Ξ±] (self : PersistentArray Ξ±) (idx : Nat) : Ξ± :=
self.get! idx
partial def setAux : PersistentArrayNode Ξ± β USize β USize β Ξ± β PersistentArrayNode Ξ±
| node cs, i, shift, a =>
let j := div2Shift i shift
let i := mod2Shift i shift
let shift := shift - initShift
node $ cs.modify j.toNat $ fun c => setAux c i shift a
| leaf cs, i, _, a => leaf (cs.set! i.toNat a)
def set (t : PersistentArray Ξ±) (i : Nat) (a : Ξ±) : PersistentArray Ξ± :=
if i >= t.tailOff then
{ t with tail := t.tail.set! (i - t.tailOff) a }
else
{ t with root := setAux t.root (USize.ofNat i) t.shift a }
@[specialize] partial def modifyAux [Inhabited Ξ±] (f : Ξ± β Ξ±) : PersistentArrayNode Ξ± β USize β USize β PersistentArrayNode Ξ±
| node cs, i, shift =>
let j := div2Shift i shift
let i := mod2Shift i shift
let shift := shift - initShift
node $ cs.modify j.toNat $ fun c => modifyAux f c i shift
| leaf cs, i, _ => leaf (cs.modify i.toNat f)
@[specialize] def modify [Inhabited Ξ±] (t : PersistentArray Ξ±) (i : Nat) (f : Ξ± β Ξ±) : PersistentArray Ξ± :=
if i >= t.tailOff then
{ t with tail := t.tail.modify (i - t.tailOff) f }
else
{ t with root := modifyAux f t.root (USize.ofNat i) t.shift }
partial def mkNewPath (shift : USize) (a : Array Ξ±) : PersistentArrayNode Ξ± :=
if shift == 0 then
leaf a
else
node (mkEmptyArray.push (mkNewPath (shift - initShift) a))
partial def insertNewLeaf : PersistentArrayNode Ξ± β USize β USize β Array Ξ± β PersistentArrayNode Ξ±
| node cs, i, shift, a =>
if i < branching then
node (cs.push (leaf a))
else
let j := div2Shift i shift
let i := mod2Shift i shift
let shift := shift - initShift
if j.toNat < cs.size then
node $ cs.modify j.toNat fun c => insertNewLeaf c i shift a
else
node $ cs.push $ mkNewPath shift a
| n, _, _, _ => n -- unreachable
def mkNewTail (t : PersistentArray Ξ±) : PersistentArray Ξ± :=
if t.size <= (mul2Shift 1 (t.shift + initShift)).toNat then
{ t with
tail := mkEmptyArray, root := insertNewLeaf t.root (USize.ofNat (t.size - 1)) t.shift t.tail,
tailOff := t.size }
else
{ t with
tail := #[],
root := let n := mkEmptyArray.push t.root;
node (n.push (mkNewPath t.shift t.tail)),
shift := t.shift + initShift,
tailOff := t.size }
def tooBig : Nat := USize.size / 8
def push (t : PersistentArray Ξ±) (a : Ξ±) : PersistentArray Ξ± :=
let r := { t with tail := t.tail.push a, size := t.size + 1 }
if r.tail.size < branching.toNat || t.size >= tooBig then
r
else
mkNewTail r
private def emptyArray {Ξ± : Type u} : Array (PersistentArrayNode Ξ±) :=
Array.mkEmpty PersistentArray.branching.toNat
partial def popLeaf : PersistentArrayNode Ξ± β Option (Array Ξ±) Γ Array (PersistentArrayNode Ξ±)
| n@(node cs) =>
if h : cs.size β 0 then
let idx : Fin cs.size := β¨cs.size - 1, by exact Nat.pred_lt hβ©
let last := cs.get idx
let cs' := cs.set idx arbitrary
match popLeaf last with
| (none, _) => (none, emptyArray)
| (some l, newLast) =>
if newLast.size == 0 then
let cs' := cs'.pop
if cs'.isEmpty then (some l, emptyArray) else (some l, cs')
else
(some l, cs'.set (Array.size_set cs idx _ βΈ idx) (node newLast))
else
(none, emptyArray)
| leaf vs => (some vs, emptyArray)
def pop (t : PersistentArray Ξ±) : PersistentArray Ξ± :=
if t.tail.size > 0 then
{ t with tail := t.tail.pop, size := t.size - 1 }
else
match popLeaf t.root with
| (none, _) => t
| (some last, newRoots) =>
let last := last.pop
let newSize := t.size - 1
let newTailOff := newSize - last.size
if newRoots.size == 1 && (newRoots.get! 0).isNode then
{ root := newRoots.get! 0,
shift := t.shift - initShift,
size := newSize,
tail := last,
tailOff := newTailOff }
else
{ t with
root := node newRoots,
size := newSize,
tail := last,
tailOff := newTailOff }
section
variable {m : Type v β Type w} [Monad m]
variable {Ξ² : Type v}
@[specialize] private partial def foldlMAux (f : Ξ² β Ξ± β m Ξ²) : PersistentArrayNode Ξ± β Ξ² β m Ξ²
| node cs, b => cs.foldlM (fun b c => foldlMAux f c b) b
| leaf vs, b => vs.foldlM f b
@[specialize] private partial def foldlFromMAux (f : Ξ² β Ξ± β m Ξ²) : PersistentArrayNode Ξ± β USize β USize β Ξ² β m Ξ²
| node cs, i, shift, b => do
let j := (div2Shift i shift).toNat
let b β foldlFromMAux f (cs.get! j) (mod2Shift i shift) (shift - initShift) b
cs.foldlM (init := b) (start := j+1) fun b c => foldlMAux f c b
| leaf vs, i, _, b => vs.foldlM (init := b) (start := i.toNat) f
@[specialize] def foldlM (t : PersistentArray Ξ±) (f : Ξ² β Ξ± β m Ξ²) (init : Ξ²) (start : Nat := 0) : m Ξ² := do
if start == 0 then
let b β foldlMAux f t.root init
t.tail.foldlM f b
else if start >= t.tailOff then
t.tail.foldlM (init := init) (start := start - t.tailOff) f
else do
let b β foldlFromMAux f t.root (USize.ofNat start) t.shift init;
t.tail.foldlM f b
@[specialize]
partial def forInAux {Ξ± : Type u} {Ξ² : Type v} {m : Type v β Type w} [Monad m] [inh : Inhabited Ξ²]
(f : Ξ± β Ξ² β m (ForInStep Ξ²)) (n : PersistentArrayNode Ξ±) (b : Ξ²) : m (ForInStep Ξ²) := do
let mut b := b
match n with
| leaf vs =>
for v in vs do
match (β f v b) with
| r@(ForInStep.done _) => return r
| ForInStep.yield bNew => b := bNew
return ForInStep.yield b
| node cs =>
for c in cs do
match (β forInAux f c b) with
| r@(ForInStep.done _) => return r
| ForInStep.yield bNew => b := bNew
return ForInStep.yield b
@[specialize] protected def forIn (t : PersistentArray Ξ±) (init : Ξ²) (f : Ξ± β Ξ² β m (ForInStep Ξ²)) : m Ξ² := do
match (β forInAux (inh := β¨initβ©) f t.root init) with
| ForInStep.done b => pure b
| ForInStep.yield b =>
let mut b := b
for v in t.tail do
match (β f v b) with
| ForInStep.done r => return r
| ForInStep.yield bNew => b := bNew
return b
instance : ForIn m (PersistentArray Ξ±) Ξ± where
forIn := PersistentArray.forIn
@[specialize] partial def findSomeMAux (f : Ξ± β m (Option Ξ²)) : PersistentArrayNode Ξ± β m (Option Ξ²)
| node cs => cs.findSomeM? (fun c => findSomeMAux f c)
| leaf vs => vs.findSomeM? f
@[specialize] def findSomeM? (t : PersistentArray Ξ±) (f : Ξ± β m (Option Ξ²)) : m (Option Ξ²) := do
match (β findSomeMAux f t.root) with
| none => t.tail.findSomeM? f
| some b => pure (some b)
@[specialize] partial def findSomeRevMAux (f : Ξ± β m (Option Ξ²)) : PersistentArrayNode Ξ± β m (Option Ξ²)
| node cs => cs.findSomeRevM? (fun c => findSomeRevMAux f c)
| leaf vs => vs.findSomeRevM? f
@[specialize] def findSomeRevM? (t : PersistentArray Ξ±) (f : Ξ± β m (Option Ξ²)) : m (Option Ξ²) := do
match (β t.tail.findSomeRevM? f) with
| none => findSomeRevMAux f t.root
| some b => pure (some b)
@[specialize] partial def forMAux (f : Ξ± β m PUnit) : PersistentArrayNode Ξ± β m PUnit
| node cs => cs.forM (fun c => forMAux f c)
| leaf vs => vs.forM f
@[specialize] def forM (t : PersistentArray Ξ±) (f : Ξ± β m PUnit) : m PUnit :=
forMAux f t.root *> t.tail.forM f
end
@[inline] def foldl {Ξ²} (t : PersistentArray Ξ±) (f : Ξ² β Ξ± β Ξ²) (init : Ξ²) (start : Nat := 0) : Ξ² :=
Id.run $ t.foldlM f init start
@[inline] def filter (as : PersistentArray Ξ±) (p : Ξ± β Bool) : PersistentArray Ξ± :=
as.foldl (init := {}) fun asNew a => if p a then asNew.push a else asNew
def toArray (t : PersistentArray Ξ±) : Array Ξ± :=
t.foldl Array.push #[]
def append (tβ tβ : PersistentArray Ξ±) : PersistentArray Ξ± :=
if tβ.isEmpty then
tβ
else
tβ.foldl PersistentArray.push tβ
instance : Append (PersistentArray Ξ±) := β¨appendβ©
@[inline] def findSome? {Ξ²} (t : PersistentArray Ξ±) (f : Ξ± β (Option Ξ²)) : Option Ξ² :=
Id.run $ t.findSomeM? f
@[inline] def findSomeRev? {Ξ²} (t : PersistentArray Ξ±) (f : Ξ± β (Option Ξ²)) : Option Ξ² :=
Id.run $ t.findSomeRevM? f
def toList (t : PersistentArray Ξ±) : List Ξ± :=
(t.foldl (init := []) fun xs x => x :: xs).reverse
section
variable {m : Type β Type w} [Monad m]
@[specialize] partial def anyMAux (p : Ξ± β m Bool) : PersistentArrayNode Ξ± β m Bool
| node cs => cs.anyM fun c => anyMAux p c
| leaf vs => vs.anyM p
@[specialize] def anyM (t : PersistentArray Ξ±) (p : Ξ± β m Bool) : m Bool :=
anyMAux p t.root <||> t.tail.anyM p
@[inline] def allM (a : PersistentArray Ξ±) (p : Ξ± β m Bool) : m Bool := do
let b β anyM a (fun v => do let b β p v; pure (not b))
pure (not b)
end
@[inline] def any (a : PersistentArray Ξ±) (p : Ξ± β Bool) : Bool :=
Id.run $ anyM a p
@[inline] def all (a : PersistentArray Ξ±) (p : Ξ± β Bool) : Bool :=
!any a fun v => !p v
section
variable {m : Type u β Type v} [Monad m]
variable {Ξ² : Type u}
@[specialize] partial def mapMAux (f : Ξ± β m Ξ²) : PersistentArrayNode Ξ± β m (PersistentArrayNode Ξ²)
| node cs => node <$> cs.mapM (fun c => mapMAux f c)
| leaf vs => leaf <$> vs.mapM f
@[specialize] def mapM (f : Ξ± β m Ξ²) (t : PersistentArray Ξ±) : m (PersistentArray Ξ²) := do
let root β mapMAux f t.root
let tail β t.tail.mapM f
pure { t with tail := tail, root := root }
end
@[inline] def map {Ξ²} (f : Ξ± β Ξ²) (t : PersistentArray Ξ±) : PersistentArray Ξ² :=
Id.run $ t.mapM f
structure Stats where
numNodes : Nat
depth : Nat
tailSize : Nat
partial def collectStats : PersistentArrayNode Ξ± β Stats β Nat β Stats
| node cs, s, d =>
cs.foldl (fun s c => collectStats c s (d+1))
{ s with
numNodes := s.numNodes + 1,
depth := Nat.max d s.depth }
| leaf vs, s, d => { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth }
def stats (r : PersistentArray Ξ±) : Stats :=
collectStats r.root { numNodes := 0, depth := 0, tailSize := r.tail.size } 0
def Stats.toString (s : Stats) : String :=
s!"\{nodes := {s.numNodes}, depth := {s.depth}, tail size := {s.tailSize}}"
instance : ToString Stats := β¨Stats.toStringβ©
end PersistentArray
def mkPersistentArray {Ξ± : Type u} (n : Nat) (v : Ξ±) : PArray Ξ± :=
n.fold (init := PersistentArray.empty) fun i p => p.push v
@[inline] def mkPArray {Ξ± : Type u} (n : Nat) (v : Ξ±) : PArray Ξ± :=
mkPersistentArray n v
end Std
open Std (PersistentArray PersistentArray.empty)
def List.toPersistentArrayAux {Ξ± : Type u} : List Ξ± β PersistentArray Ξ± β PersistentArray Ξ±
| [], t => t
| x::xs, t => toPersistentArrayAux xs (t.push x)
def List.toPersistentArray {Ξ± : Type u} (xs : List Ξ±) : PersistentArray Ξ± :=
xs.toPersistentArrayAux {}
def Array.toPersistentArray {Ξ± : Type u} (xs : Array Ξ±) : PersistentArray Ξ± :=
xs.foldl (init := PersistentArray.empty) fun p x => p.push x
@[inline] def Array.toPArray {Ξ± : Type u} (xs : Array Ξ±) : PersistentArray Ξ± :=
xs.toPersistentArray
|
bd02ef19fa1db9f34e8c4e8480f25ce13938388a | 618003631150032a5676f229d13a079ac875ff77 | /src/set_theory/pgame.lean | febe8a41fc3546a394fd0ed24b08168c02435118 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 39,216 | lean | /-
Copyright (c) 2019 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Mario Carneiro, Isabel Longbottom, Scott Morrison
-/
import logic.embedding
import data.nat.cast
/-!
# Combinatorial (pre-)games.
The basic theory of combinatorial games, following Conway's book `On Numbers and Games`. We
construct "pregames", define an ordering and arithmetic operations on them, then show that the
operations descend to "games", defined via the equivalence relation `p β q β p β€ q β§ q β€ p`.
The surreal numbers will be built as a quotient of a subtype of pregames.
A pregame (`pgame` below) is axiomatised via an inductive type, whose sole constructor takes two
types (thought of as indexing the the possible moves for the players Left and Right), and a pair of
functions out of these types to `pgame` (thought of as describing the resulting game after making a
move).
Combinatorial games themselves, as a quotient of pregames, are constructed in `game.lean`.
## Conway induction
By construction, the induction principle for pregames is exactly "Conway induction". That is, to
prove some predicate `pgame β Prop` holds for all pregames, it suffices to prove that for every
pregame `g`, if the predicate holds for every game resulting from making a move, then it also holds
for `g`.
While it is often convenient to work "by induction" on pregames, in some situations this becomes
awkward, so we also define accessor functions `left_moves`, `right_moves`, `move_left` and
`move_right`. There is a relation `subsequent p q`, saying that `p` can be reached by playing some
non-empty sequence of moves starting from `q`, an instance `well_founded subsequent`, and a local
tactic `pgame_wf_tac` which is helpful for discharging proof obligations in inductive proofs relying
on this relation.
## Order properties
Pregames have both a `β€` and a `<` relation, which are related in quite a subtle way. In particular,
it is worth noting that in Lean's (perhaps unfortunate?) definition of a `preorder`, we have
`lt_iff_le_not_le : β a b : Ξ±, a < b β (a β€ b β§ Β¬ b β€ a)`, but this is _not_ satisfied by the usual
`β€` and `<` relations on pregames. (It is satisfied once we restrict to the surreal numbers.) In
particular, `<` is not transitive; there is an example below showing `0 < star β§ star < 0`.
We do have
```
theorem not_le {x y : pgame} : Β¬ x β€ y β y < x := ...
theorem not_lt {x y : pgame} : Β¬ x < y β y β€ x := ...
```
The statement `0 β€ x` means that Left has a good response to any move by Right; in particular, the
theorem `zero_le` below states
```
0 β€ x β β j : x.right_moves, β i : (x.move_right j).left_moves, 0 β€ (x.move_right j).move_left i
```
On the other hand the statement `0 < x` means that Left has a good move right now; in particular the
theorem `zero_lt` below states
```
0 < x β β i : left_moves x, β j : right_moves (x.move_left i), 0 < (x.move_left i).move_right j
```
The theorems `le_def`, `lt_def`, give a recursive characterisation of each relation, in terms of
themselves two moves later. The theorems `le_def_lt` and `lt_def_lt` give recursive
characterisations of each relation in terms of the other relation one move later.
We define an equivalence relation `equiv p q β p β€ q β§ q β€ p`. Later, games will be defined as the
quotient by this relation.
## Algebraic structures
We next turn to defining the operations necessary to make games into a commutative additive group.
Addition is defined for $x = \{xL | xR\}$ and $y = \{yL | yR\}$ by $x + y = \{xL + y, x + yL | xR +
y, x + yR\}$. Negation is defined by $\{xL | xR\} = \{-xR | -xL\}$.
The order structures interact in the expected way with addition, so we have
```
theorem le_iff_sub_nonneg {x y : pgame} : x β€ y β 0 β€ y - x := sorry
theorem lt_iff_sub_pos {x y : pgame} : x < y β 0 < y - x := sorry
```
We show that these operations respect the equivalence relation, and hence descend to games. At the
level of games, these operations satisfy all the laws of a commutative group. To prove the necessary
equivalence relations at the level of pregames, we introduce the notion of a `relabelling` of a
game, and show, for example, that there is a relabelling between `x + (y + z)` and `(x + y) + z`.
## Future work
* The theory of dominated and reversible positions, and unique normal form for short games.
* Analysis of basic domineering positions.
* Impartial games, nim, and the Sprague-Grundy theorem.
* Hex.
* Temperature.
* The development of surreal numbers, based on this development of combinatorial games, is still
quite incomplete.
## References
The material here is all drawn from
* [Conway, *On numbers and games*][conway2001]
An interested reader may like to formalise some of the material from
* [Andreas Blass, *A game semantics for linear logic*][MR1167694]
* [AndrΓ© Joyal, *Remarques sur la thΓ©orie des jeux Γ deux personnes*][joyal1997]
-/
universes u
/-- The type of pre-games, before we have quotiented
by extensionality. In ZFC, a combinatorial game is constructed from
two sets of combinatorial games that have been constructed at an earlier
stage. To do this in type theory, we say that a pre-game is built
inductively from two families of pre-games indexed over any type
in Type u. The resulting type `pgame.{u}` lives in `Type (u+1)`,
reflecting that it is a proper class in ZFC. -/
inductive pgame : Type (u+1)
| mk : β Ξ± Ξ² : Type u, (Ξ± β pgame) β (Ξ² β pgame) β pgame
namespace pgame
/--
Construct a pre-game from list of pre-games describing the available moves for Left and Right.
-/
-- TODO provide some API describing the interaction with
-- `left_moves`, `right_moves`, `move_left` and `move_right` below.
-- TODO define this at the level of games, as well, and perhaps also for finsets of games.
def of_lists (L R : list pgame.{0}) : pgame.{0} :=
pgame.mk (fin L.length) (fin R.length) (Ξ» i, L.nth_le i.val i.is_lt) (Ξ» j, R.nth_le j.val j.is_lt)
/-- The indexing type for allowable moves by Left. -/
def left_moves : pgame β Type u
| (mk l _ _ _) := l
/-- The indexing type for allowable moves by Right. -/
def right_moves : pgame β Type u
| (mk _ r _ _) := r
/-- The new game after Left makes an allowed move. -/
def move_left : Ξ (g : pgame), left_moves g β pgame
| (mk l _ L _) i := L i
/-- The new game after Right makes an allowed move. -/
def move_right : Ξ (g : pgame), right_moves g β pgame
| (mk _ r _ R) j := R j
@[simp] lemma left_moves_mk {xl xr xL xR} : (β¨xl, xr, xL, xRβ© : pgame).left_moves = xl := rfl
@[simp] lemma move_left_mk {xl xr xL xR i} : (β¨xl, xr, xL, xRβ© : pgame).move_left i = xL i := rfl
@[simp] lemma right_moves_mk {xl xr xL xR} : (β¨xl, xr, xL, xRβ© : pgame).right_moves = xr := rfl
@[simp] lemma move_right_mk {xl xr xL xR j} : (β¨xl, xr, xL, xRβ© : pgame).move_right j = xR j := rfl
/-- `subsequent p q` says that `p` can be obtained by playing
some nonempty sequence of moves from `q`. -/
inductive subsequent : pgame β pgame β Prop
| left : Ξ (x : pgame) (i : x.left_moves), subsequent (x.move_left i) x
| right : Ξ (x : pgame) (j : x.right_moves), subsequent (x.move_right j) x
| trans : Ξ (x y z : pgame), subsequent x y β subsequent y z β subsequent x z
theorem wf_subsequent : well_founded subsequent :=
β¨Ξ» x, begin
induction x with l r L R IHl IHr,
refine β¨_, Ξ» y h, _β©,
generalize_hyp e : mk l r L R = x at h,
induction h with _ i _ j a b _ h1 h2 IH1 IH2; subst e,
{ apply IHl },
{ apply IHr },
{ exact acc.inv (IH2 rfl) h1 }
endβ©
instance : has_well_founded pgame :=
{ r := subsequent,
wf := wf_subsequent }
/-- A move by Left produces a subsequent game. (For use in pgame_wf_tac.) -/
lemma subsequent.left_move {xl xr} {xL : xl β pgame} {xR : xr β pgame} {i : xl} :
subsequent (xL i) (mk xl xr xL xR) :=
subsequent.left (mk xl xr xL xR) i
/-- A move by Right produces a subsequent game. (For use in pgame_wf_tac.) -/
lemma subsequent.right_move {xl xr} {xL : xl β pgame} {xR : xr β pgame} {j : xr} :
subsequent (xR j) (mk xl xr xL xR) :=
subsequent.right (mk xl xr xL xR) j
/-- A local tactic for proving well-foundedness of recursive definitions involving pregames. -/
meta def pgame_wf_tac :=
`[solve_by_elim
[psigma.lex.left, psigma.lex.right,
subsequent.left_move, subsequent.right_move,
subsequent.left, subsequent.right, subsequent.trans]
{ max_depth := 6 }]
/-- The pre-game `zero` is defined by `0 = { | }`. -/
instance : has_zero pgame := β¨β¨pempty, pempty, pempty.elim, pempty.elimβ©β©
@[simp] lemma zero_left_moves : (0 : pgame).left_moves = pempty := rfl
@[simp] lemma zero_right_moves : (0 : pgame).right_moves = pempty := rfl
instance : inhabited pgame := β¨0β©
/-- The pre-game `one` is defined by `1 = { 0 | }`. -/
instance : has_one pgame := β¨β¨punit, pempty, Ξ» _, 0, pempty.elimβ©β©
@[simp] lemma one_left_moves : (1 : pgame).left_moves = punit := rfl
@[simp] lemma one_move_left : (1 : pgame).move_left punit.star = 0 := rfl
@[simp] lemma one_right_moves : (1 : pgame).right_moves = pempty := rfl
/-- Define simultaneously by mutual induction the `<=` and `<`
relation on pre-games. The ZFC definition says that `x = {xL | xR}`
is less or equal to `y = {yL | yR}` if `β xβ β xL, xβ < y`
and `β yβ β yR, x < yβ`, where `x < y` is the same as `Β¬ y <= x`.
This is a tricky induction because it only decreases one side at
a time, and it also swaps the arguments in the definition of `<`.
The solution is to define `x < y` and `x <= y` simultaneously. -/
def le_lt : Ξ (x y : pgame), Prop Γ Prop
| (mk xl xr xL xR) (mk yl yr yL yR) :=
-- the orderings of the clauses here are carefully chosen so that
-- and.left/or.inl refer to moves by Left, and
-- and.right/or.inr refer to moves by Right.
((β i : xl, (le_lt (xL i) β¨yl, yr, yL, yRβ©).2) β§ (β j : yr, (le_lt β¨xl, xr, xL, xRβ© (yR j)).2),
(β i : yl, (le_lt β¨xl, xr, xL, xRβ© (yL i)).1) β¨ (β j : xr, (le_lt (xR j) β¨yl, yr, yL, yRβ©).1))
using_well_founded { dec_tac := pgame_wf_tac }
instance : has_le pgame := β¨Ξ» x y, (le_lt x y).1β©
instance : has_lt pgame := β¨Ξ» x y, (le_lt x y).2β©
/-- Definition of `x β€ y` on pre-games built using the constructor. -/
@[simp] theorem mk_le_mk {xl xr xL xR yl yr yL yR} :
(β¨xl, xr, xL, xRβ© : pgame) β€ β¨yl, yr, yL, yRβ© β
(β i, xL i < β¨yl, yr, yL, yRβ©) β§
(β j, (β¨xl, xr, xL, xRβ© : pgame) < yR j) := iff.rfl
/-- Definition of `x β€ y` on pre-games, in terms of `<` -/
theorem le_def_lt {x y : pgame} : x β€ y β
(β i : x.left_moves, x.move_left i < y) β§
(β j : y.right_moves, x < y.move_right j) :=
by { cases x, cases y, refl }
/-- Definition of `x < y` on pre-games built using the constructor. -/
@[simp] theorem mk_lt_mk {xl xr xL xR yl yr yL yR} :
(β¨xl, xr, xL, xRβ© : pgame) < β¨yl, yr, yL, yRβ© β
(β i, (β¨xl, xr, xL, xRβ© : pgame) β€ yL i) β¨
(β j, xR j β€ β¨yl, yr, yL, yRβ©) := iff.rfl
/-- Definition of `x < y` on pre-games, in terms of `β€` -/
theorem lt_def_le {x y : pgame} : x < y β
(β i : y.left_moves, x β€ y.move_left i) β¨
(β j : x.right_moves, x.move_right j β€ y) :=
by { cases x, cases y, refl }
/-- The definition of `x β€ y` on pre-games, in terms of `β€` two moves later. -/
theorem le_def {x y : pgame} : x β€ y β
(β i : x.left_moves,
(β i' : y.left_moves, x.move_left i β€ y.move_left i') β¨
(β j : (x.move_left i).right_moves, (x.move_left i).move_right j β€ y)) β§
(β j : y.right_moves,
(β i : (y.move_right j).left_moves, x β€ (y.move_right j).move_left i) β¨
(β j' : x.right_moves, x.move_right j' β€ y.move_right j)) :=
begin
rw [le_def_lt],
conv { to_lhs, simp only [lt_def_le] },
end
/-- The definition of `x < y` on pre-games, in terms of `<` two moves later. -/
theorem lt_def {x y : pgame} : x < y β
(β i : y.left_moves,
(β i' : x.left_moves, x.move_left i' < y.move_left i) β§
(β j : (y.move_left i).right_moves, x < (y.move_left i).move_right j)) β¨
(β j : x.right_moves,
(β i : (x.move_right j).left_moves, (x.move_right j).move_left i < y) β§
(β j' : y.right_moves, x.move_right j < y.move_right j')) :=
begin
rw [lt_def_le],
conv { to_lhs, simp only [le_def_lt] },
end
/-- The definition of `x β€ 0` on pre-games, in terms of `β€ 0` two moves later. -/
theorem le_zero {x : pgame} : x β€ 0 β
β i : x.left_moves, β j : (x.move_left i).right_moves, (x.move_left i).move_right j β€ 0 :=
begin
rw le_def,
dsimp,
simp [forall_pempty, exists_pempty]
end
/-- The definition of `0 β€ x` on pre-games, in terms of `0 β€` two moves later. -/
theorem zero_le {x : pgame} : 0 β€ x β
β j : x.right_moves, β i : (x.move_right j).left_moves, 0 β€ (x.move_right j).move_left i :=
begin
rw le_def,
dsimp,
simp [forall_pempty, exists_pempty]
end
/-- The definition of `x < 0` on pre-games, in terms of `< 0` two moves later. -/
theorem lt_zero {x : pgame} : x < 0 β
β j : x.right_moves, β i : (x.move_right j).left_moves, (x.move_right j).move_left i < 0 :=
begin
rw lt_def,
dsimp,
simp [forall_pempty, exists_pempty]
end
/-- The definition of `0 < x` on pre-games, in terms of `< x` two moves later. -/
theorem zero_lt {x : pgame} : 0 < x β
β i : x.left_moves, β j : (x.move_left i).right_moves, 0 < (x.move_left i).move_right j :=
begin
rw lt_def,
dsimp,
simp [forall_pempty, exists_pempty]
end
/-- Given a right-player-wins game, provide a response to any move by left. -/
noncomputable def right_response {x : pgame} (h : x β€ 0) (i : x.left_moves) :
(x.move_left i).right_moves :=
classical.some $ (le_zero.1 h) i
/-- Show that the response for right provided by `right_response`
preserves the right-player-wins condition. -/
lemma right_response_spec {x : pgame} (h : x β€ 0) (i : x.left_moves) :
(x.move_left i).move_right (right_response h i) β€ 0 :=
classical.some_spec $ (le_zero.1 h) i
/-- Given a left-player-wins game, provide a response to any move by right. -/
noncomputable def left_response {x : pgame} (h : 0 β€ x) (j : x.right_moves) :
(x.move_right j).left_moves :=
classical.some $ (zero_le.1 h) j
/-- Show that the response for left provided by `left_response`
preserves the left-player-wins condition. -/
lemma left_response_spec {x : pgame} (h : 0 β€ x) (j : x.right_moves) :
0 β€ (x.move_right j).move_left (left_response h j) :=
classical.some_spec $ (zero_le.1 h) j
theorem lt_of_le_mk {xl xr xL xR y i} :
(β¨xl, xr, xL, xRβ© : pgame) β€ y β xL i < y :=
by cases y; exact Ξ» h, h.1 i
theorem lt_of_mk_le {x : pgame} {yl yr yL yR i} :
x β€ β¨yl, yr, yL, yRβ© β x < yR i :=
by cases x; exact Ξ» h, h.2 i
theorem mk_lt_of_le {xl xr xL xR y i} :
(by exact xR i β€ y) β (β¨xl, xr, xL, xRβ© : pgame) < y :=
by cases y; exact Ξ» h, or.inr β¨i, hβ©
theorem lt_mk_of_le {x : pgame} {yl yr yL yR i} :
(by exact x β€ yL i) β x < β¨yl, yr, yL, yRβ© :=
by cases x; exact Ξ» h, or.inl β¨i, hβ©
theorem not_le_lt {x y : pgame} :
(Β¬ x β€ y β y < x) β§ (Β¬ x < y β y β€ x) :=
begin
induction x with xl xr xL xR IHxl IHxr generalizing y,
induction y with yl yr yL yR IHyl IHyr,
classical,
simp only [mk_le_mk, mk_lt_mk,
not_and_distrib, not_or_distrib, not_forall, not_exists,
and_comm, or_comm, IHxl, IHxr, IHyl, IHyr, iff_self, and_self]
end
theorem not_le {x y : pgame} : Β¬ x β€ y β y < x := not_le_lt.1
theorem not_lt {x y : pgame} : Β¬ x < y β y β€ x := not_le_lt.2
@[refl] theorem le_refl : β x : pgame, x β€ x
| β¨l, r, L, Rβ© :=
β¨Ξ» i, lt_mk_of_le (le_refl _), Ξ» i, mk_lt_of_le (le_refl _)β©
theorem lt_irrefl (x : pgame) : Β¬ x < x :=
not_lt.2 (le_refl _)
theorem ne_of_lt : β {x y : pgame}, x < y β x β y
| x _ h rfl := lt_irrefl x h
theorem le_trans_aux
{xl xr} {xL : xl β pgame} {xR : xr β pgame}
{yl yr} {yL : yl β pgame} {yR : yr β pgame}
{zl zr} {zL : zl β pgame} {zR : zr β pgame}
(hβ : β i, mk yl yr yL yR β€ mk zl zr zL zR β mk zl zr zL zR β€ xL i β mk yl yr yL yR β€ xL i)
(hβ : β i, zR i β€ mk xl xr xL xR β mk xl xr xL xR β€ mk yl yr yL yR β zR i β€ mk yl yr yL yR) :
mk xl xr xL xR β€ mk yl yr yL yR β
mk yl yr yL yR β€ mk zl zr zL zR β
mk xl xr xL xR β€ mk zl zr zL zR :=
Ξ» β¨xLy, xyRβ© β¨yLz, yzRβ©, β¨
Ξ» i, not_le.1 (Ξ» h, not_lt.2 (hβ _ β¨yLz, yzRβ© h) (xLy _)),
Ξ» i, not_le.1 (Ξ» h, not_lt.2 (hβ _ h β¨xLy, xyRβ©) (yzR _))β©
@[trans] theorem le_trans {x y z : pgame} : x β€ y β y β€ z β x β€ z :=
suffices β {x y z : pgame},
(x β€ y β y β€ z β x β€ z) β§ (y β€ z β z β€ x β y β€ x) β§ (z β€ x β x β€ y β z β€ y),
from this.1, begin
clear x y z, intros,
induction x with xl xr xL xR IHxl IHxr generalizing y z,
induction y with yl yr yL yR IHyl IHyr generalizing z,
induction z with zl zr zL zR IHzl IHzr,
exact β¨
le_trans_aux (Ξ» i, (IHxl _).2.1) (Ξ» i, (IHzr _).2.2),
le_trans_aux (Ξ» i, (IHyl _).2.2) (Ξ» i, (IHxr _).1),
le_trans_aux (Ξ» i, (IHzl _).1) (Ξ» i, (IHyr _).2.1)β©,
end
@[trans] theorem lt_of_le_of_lt {x y z : pgame} (hxy : x β€ y) (hyz : y < z) : x < z :=
begin
rw βnot_le at β’ hyz,
exact mt (Ξ» H, le_trans H hxy) hyz
end
@[trans] theorem lt_of_lt_of_le {x y z : pgame} (hxy : x < y) (hyz : y β€ z) : x < z :=
begin
rw βnot_le at β’ hxy,
exact mt (Ξ» H, le_trans hyz H) hxy
end
/-- Define the equivalence relation on pre-games. Two pre-games
`x`, `y` are equivalent if `x β€ y` and `y β€ x`. -/
def equiv (x y : pgame) : Prop := x β€ y β§ y β€ x
local infix ` β ` := pgame.equiv
@[refl] theorem equiv_refl (x) : x β x := β¨le_refl _, le_refl _β©
@[symm] theorem equiv_symm {x y} : x β y β y β x | β¨xy, yxβ© := β¨yx, xyβ©
@[trans] theorem equiv_trans {x y z} : x β y β y β z β x β z
| β¨xy, yxβ© β¨yz, zyβ© := β¨le_trans xy yz, le_trans zy yxβ©
theorem lt_of_lt_of_equiv {x y z} (hβ : x < y) (hβ : y β z) : x < z := lt_of_lt_of_le hβ hβ.1
theorem le_of_le_of_equiv {x y z} (hβ : x β€ y) (hβ : y β z) : x β€ z := le_trans hβ hβ.1
theorem lt_of_equiv_of_lt {x y z} (hβ : x β y) (hβ : y < z) : x < z := lt_of_le_of_lt hβ.1 hβ
theorem le_of_equiv_of_le {x y z} (hβ : x β y) (hβ : y β€ z) : x β€ z := le_trans hβ.1 hβ
theorem le_congr {xβ yβ xβ yβ} : xβ β xβ β yβ β yβ β (xβ β€ yβ β xβ β€ yβ)
| β¨x12, x21β© β¨y12, y21β© := β¨Ξ» h, le_trans x21 (le_trans h y12), Ξ» h, le_trans x12 (le_trans h y21)β©
theorem lt_congr {xβ yβ xβ yβ} (hx : xβ β xβ) (hy : yβ β yβ) : xβ < yβ β xβ < yβ :=
not_le.symm.trans $ (not_congr (le_congr hy hx)).trans not_le
/-- `restricted x y` says that Left always has no more moves in `x` than in `y`,
and Right always has no more moves in `y` than in `x` -/
inductive restricted : pgame.{u} β pgame.{u} β Type (u+1)
| mk : Ξ {x y : pgame} (L : x.left_moves β y.left_moves) (R : y.right_moves β x.right_moves),
(β (i : x.left_moves), restricted (x.move_left i) (y.move_left (L i))) β
(β (j : y.right_moves), restricted (x.move_right (R j)) (y.move_right j)) β restricted x y
/-- The identity restriction. -/
@[refl] def restricted.refl : Ξ (x : pgame), restricted x x
| (mk xl xr xL xR) :=
restricted.mk
id id
(Ξ» i, restricted.refl _) (Ξ» j, restricted.refl _)
using_well_founded { dec_tac := pgame_wf_tac }
-- TODO trans for restricted
theorem le_of_restricted : Ξ {x y : pgame} (r : restricted x y), x β€ y
| (mk xl xr xL xR) (mk yl yr yL yR)
(restricted.mk L_embedding R_embedding L_restriction R_restriction) :=
begin
rw le_def,
exact
β¨Ξ» i, or.inl β¨L_embedding i, le_of_restricted (L_restriction i)β©,
Ξ» i, or.inr β¨R_embedding i, le_of_restricted (R_restriction i)β©β©
end
/--
`relabelling x y` says that `x` and `y` are really the same game, just dressed up differently.
Specifically, there is a bijection between the moves for Left in `x` and in `y`, and similarly
for Right, and under these bijections we inductively have `relabelling`s for the consequent games.
-/
inductive relabelling : pgame.{u} β pgame.{u} β Type (u+1)
| mk : Ξ {x y : pgame} (L : x.left_moves β y.left_moves) (R : x.right_moves β y.right_moves),
(β (i : x.left_moves), relabelling (x.move_left i) (y.move_left (L i))) β
(β (j : y.right_moves), relabelling (x.move_right (R.symm j)) (y.move_right j)) β
relabelling x y
/-- If `x` is a relabelling of `y`, then Left and Right have the same moves in either game,
so `x` is a restriction of `y`. -/
def restricted_of_relabelling : Ξ {x y : pgame} (r : relabelling x y), restricted x y
| (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) :=
restricted.mk L_equiv.to_embedding R_equiv.symm.to_embedding
(Ξ» i, restricted_of_relabelling (L_relabelling i))
(Ξ» j, restricted_of_relabelling (R_relabelling j))
-- It's not the case that `restricted x y β restricted y x β relabelling x y`,
-- but if we insisted that the maps in a restriction were injective, then one
-- could use SchrΓΆder-Bernstein for do this.
/-- The identity relabelling. -/
@[refl] def relabelling.refl : Ξ (x : pgame), relabelling x x
| (mk xl xr xL xR) :=
relabelling.mk (equiv.refl _) (equiv.refl _)
(Ξ» i, relabelling.refl _) (Ξ» j, relabelling.refl _)
using_well_founded { dec_tac := pgame_wf_tac }
/-- Reverse a relabelling. -/
@[symm] def relabelling.symm : Ξ {x y : pgame}, relabelling x y β relabelling y x
| (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) :=
begin
refine relabelling.mk L_equiv.symm R_equiv.symm _ _,
{ intro i, simpa using (L_relabelling (L_equiv.symm i)).symm },
{ intro j, simpa using (R_relabelling (R_equiv j)).symm }
end
/-- Transitivity of relabelling -/
@[trans] def relabelling.trans :
Ξ {x y z : pgame}, relabelling x y β relabelling y z β relabelling x z
| (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR)
(relabelling.mk L_equivβ R_equivβ L_relabellingβ R_relabellingβ)
(relabelling.mk L_equivβ R_equivβ L_relabellingβ R_relabellingβ) :=
begin
refine relabelling.mk (L_equivβ.trans L_equivβ) (R_equivβ.trans R_equivβ) _ _,
{ intro i, simpa using (L_relabellingβ _).trans (L_relabellingβ _) },
{ intro j, simpa using (R_relabellingβ _).trans (R_relabellingβ _) },
end
theorem le_of_relabelling {x y : pgame} (r : relabelling x y) : x β€ y :=
le_of_restricted (restricted_of_relabelling r)
/-- A relabelling lets us prove equivalence of games. -/
theorem equiv_of_relabelling {x y : pgame} (r : relabelling x y) : x β y :=
β¨le_of_relabelling r, le_of_relabelling r.symmβ©
instance {x y : pgame} : has_coe (relabelling x y) (x β y) := β¨equiv_of_relabellingβ©
/-- Replace the types indexing the next moves for Left and Right by equivalent types. -/
def relabel {x : pgame} {xl' xr'} (el : x.left_moves β xl') (er : x.right_moves β xr') :=
pgame.mk xl' xr' (Ξ» i, x.move_left (el.symm i)) (Ξ» j, x.move_right (er.symm j))
@[simp] lemma relabel_move_left' {x : pgame} {xl' xr'}
(el : x.left_moves β xl') (er : x.right_moves β xr') (i : xl') :
move_left (relabel el er) i = x.move_left (el.symm i) :=
rfl
@[simp] lemma relabel_move_left {x : pgame} {xl' xr'}
(el : x.left_moves β xl') (er : x.right_moves β xr') (i : x.left_moves) :
move_left (relabel el er) (el i) = x.move_left i :=
by simp
@[simp] lemma relabel_move_right' {x : pgame} {xl' xr'}
(el : x.left_moves β xl') (er : x.right_moves β xr') (j : xr') :
move_right (relabel el er) j = x.move_right (er.symm j) :=
rfl
@[simp] lemma relabel_move_right {x : pgame} {xl' xr'}
(el : x.left_moves β xl') (er : x.right_moves β xr') (j : x.right_moves) :
move_right (relabel el er) (er j) = x.move_right j :=
by simp
/-- The game obtained by relabelling the next moves is a relabelling of the original game. -/
def relabel_relabelling {x : pgame} {xl' xr'} (el : x.left_moves β xl') (er : x.right_moves β xr') :
relabelling x (relabel el er) :=
relabelling.mk el er (Ξ» i, by simp) (Ξ» j, by simp)
/-- The negation of `{L | R}` is `{-R | -L}`. -/
def neg : pgame β pgame
| β¨l, r, L, Rβ© := β¨r, l, Ξ» i, neg (R i), Ξ» i, neg (L i)β©
instance : has_neg pgame := β¨negβ©
@[simp] lemma neg_def {xl xr xL xR} : -(mk xl xr xL xR) = mk xr xl (Ξ» j, -(xR j)) (Ξ» i, -(xL i)) :=
rfl
@[simp] theorem neg_neg : Ξ {x : pgame}, -(-x) = x
| (mk xl xr xL xR) :=
begin
dsimp [has_neg.neg, neg],
congr; funext i; apply neg_neg
end
@[simp] theorem neg_zero : -(0 : pgame) = 0 :=
begin
dsimp [has_zero.zero, has_neg.neg, neg],
congr; funext i; cases i
end
/-- An explicit equivalence between the moves for Left in `-x` and the moves for Right in `x`. -/
-- This equivalence is useful to avoid having to use `cases` unnecessarily.
def left_moves_neg (x : pgame) : (-x).left_moves β x.right_moves :=
by { cases x, refl }
/-- An explicit equivalence between the moves for Right in `-x` and the moves for Left in `x`. -/
def right_moves_neg (x : pgame) : (-x).right_moves β x.left_moves :=
by { cases x, refl }
@[simp] lemma move_right_left_moves_neg {x : pgame} (i : left_moves (-x)) :
move_right x ((left_moves_neg x) i) = -(move_left (-x) i) :=
begin
induction x,
exact neg_neg.symm
end
@[simp] lemma move_left_right_moves_neg_symm {x : pgame} (i : right_moves x) :
move_left (-x) ((left_moves_neg x).symm i) = -(move_right x i) :=
by { cases x, refl }
@[simp] lemma move_left_right_moves_neg {x : pgame} (i : right_moves (-x)) :
move_left x ((right_moves_neg x) i) = -(move_right (-x) i) :=
begin
induction x,
exact neg_neg.symm
end
@[simp] lemma move_right_right_moves_neg_symm {x : pgame} (i : left_moves x) :
move_right (-x) ((right_moves_neg x).symm i) = -(move_left x i) :=
by { cases x, refl }
theorem le_iff_neg_ge : Ξ {x y : pgame}, x β€ y β -y β€ -x
| (mk xl xr xL xR) (mk yl yr yL yR) :=
begin
rw [le_def],
rw [le_def],
dsimp [neg],
split,
{ intro h,
split,
{ intro i, have t := h.right i, cases t,
{ right, cases t,
use (@right_moves_neg (yR i)).symm t_w, convert le_iff_neg_ge.1 t_h, simp },
{ left, cases t,
use t_w, exact le_iff_neg_ge.1 t_h, } },
{ intro j, have t := h.left j, cases t,
{ right, cases t,
use t_w, exact le_iff_neg_ge.1 t_h, },
{ left, cases t,
use (@left_moves_neg (xL j)).symm t_w, convert le_iff_neg_ge.1 t_h, simp, } } },
{ intro h,
split,
{ intro i, have t := h.right i, cases t,
{ right, cases t,
use (@left_moves_neg (xL i)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp, },
{ left, cases t,
use t_w, exact le_iff_neg_ge.2 t_h, } },
{ intro j, have t := h.left j, cases t,
{ right, cases t,
use t_w, exact le_iff_neg_ge.2 t_h, },
{ left, cases t,
use (@right_moves_neg (yR j)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp } } },
end
using_well_founded { dec_tac := pgame_wf_tac }
theorem neg_congr {x y : pgame} (h : x β y) : -x β -y :=
β¨le_iff_neg_ge.1 h.2, le_iff_neg_ge.1 h.1β©
theorem lt_iff_neg_gt : Ξ {x y : pgame}, x < y β -y < -x :=
begin
classical,
intros,
rw [βnot_le, βnot_le, not_iff_not],
apply le_iff_neg_ge
end
theorem zero_le_iff_neg_le_zero {x : pgame} : 0 β€ x β -x β€ 0 :=
begin
convert le_iff_neg_ge,
rw neg_zero
end
theorem le_zero_iff_zero_le_neg {x : pgame} : x β€ 0 β 0 β€ -x :=
begin
convert le_iff_neg_ge,
rw neg_zero
end
/-- The sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/
def add (x y : pgame) : pgame :=
begin
induction x with xl xr xL xR IHxl IHxr generalizing y,
induction y with yl yr yL yR IHyl IHyr,
have y := mk yl yr yL yR,
refine β¨xl β yl, xr β yr, sum.rec _ _, sum.rec _ _β©,
{ exact Ξ» i, IHxl i y },
{ exact Ξ» i, IHyl i },
{ exact Ξ» i, IHxr i y },
{ exact Ξ» i, IHyr i }
end
instance : has_add pgame := β¨addβ©
/-- `x + 0` has exactly the same moves as `x`. -/
def add_zero_relabelling : Ξ (x : pgame.{u}), relabelling (x + 0) x
| (mk xl xr xL xR) :=
begin
refine β¨equiv.sum_pempty xl, equiv.sum_pempty xr, _, _β©,
{ rintro (β¨iβ©|β¨β¨β©β©),
apply add_zero_relabelling, },
{ rintro j,
apply add_zero_relabelling, }
end
/-- `x + 0` is equivalent to `x`. -/
lemma add_zero_equiv (x : pgame.{u}) : x + 0 β x :=
equiv_of_relabelling (add_zero_relabelling x)
/-- `0 + x` has exactly the same moves as `x`. -/
def zero_add_relabelling : Ξ (x : pgame.{u}), relabelling (0 + x) x
| (mk xl xr xL xR) :=
begin
refine β¨equiv.pempty_sum xl, equiv.pempty_sum xr, _, _β©,
{ rintro (β¨β¨β©β©|β¨iβ©),
apply zero_add_relabelling, },
{ rintro j,
apply zero_add_relabelling, }
end
/-- `0 + x` is equivalent to `x`. -/
lemma zero_add_equiv (x : pgame.{u}) : 0 + x β x :=
equiv_of_relabelling (zero_add_relabelling x)
/-- An explicit equivalence between the moves for Left in `x + y` and the type-theory sum
of the moves for Left in `x` and in `y`. -/
def left_moves_add (x y : pgame) : (x + y).left_moves β x.left_moves β y.left_moves :=
by { cases x, cases y, refl, }
/-- An explicit equivalence between the moves for Right in `x + y` and the type-theory sum
of the moves for Right in `x` and in `y`. -/
def right_moves_add (x y : pgame) : (x + y).right_moves β x.right_moves β y.right_moves :=
by { cases x, cases y, refl, }
@[simp] lemma mk_add_move_left_inl {xl xr yl yr} {xL xR yL yR} {i} :
(mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inl i) =
(mk xl xr xL xR).move_left i + (mk yl yr yL yR) :=
rfl
@[simp] lemma add_move_left_inl {x y : pgame} {i} :
(x + y).move_left ((@left_moves_add x y).symm (sum.inl i)) = x.move_left i + y :=
by { cases x, cases y, refl, }
@[simp] lemma mk_add_move_right_inl {xl xr yl yr} {xL xR yL yR} {i} :
(mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inl i) =
(mk xl xr xL xR).move_right i + (mk yl yr yL yR) :=
rfl
@[simp] lemma add_move_right_inl {x y : pgame} {i} :
(x + y).move_right ((@right_moves_add x y).symm (sum.inl i)) = x.move_right i + y :=
by { cases x, cases y, refl, }
@[simp] lemma mk_add_move_left_inr {xl xr yl yr} {xL xR yL yR} {i} :
(mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inr i) =
(mk xl xr xL xR) + (mk yl yr yL yR).move_left i :=
rfl
@[simp] lemma add_move_left_inr {x y : pgame} {i : y.left_moves} :
(x + y).move_left ((@left_moves_add x y).symm (sum.inr i)) = x + y.move_left i :=
by { cases x, cases y, refl, }
@[simp] lemma mk_add_move_right_inr {xl xr yl yr} {xL xR yL yR} {i} :
(mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inr i) =
(mk xl xr xL xR) + (mk yl yr yL yR).move_right i :=
rfl
@[simp] lemma add_move_right_inr {x y : pgame} {i} :
(x + y).move_right ((@right_moves_add x y).symm (sum.inr i)) = x + y.move_right i :=
by { cases x, cases y, refl, }
instance : has_sub pgame := β¨Ξ» x y, x + -yβ©
/-- `-(x+y)` has exactly the same moves as `-x + -y`. -/
def neg_add_relabelling : Ξ (x y : pgame), relabelling (-(x + y)) (-x + -y)
| (mk xl xr xL xR) (mk yl yr yL yR) :=
β¨equiv.refl _, equiv.refl _,
Ξ» j, sum.cases_on j
(Ξ» j, neg_add_relabelling (xR j) (mk yl yr yL yR))
(Ξ» j, neg_add_relabelling (mk xl xr xL xR) (yR j)),
Ξ» i, sum.cases_on i
(Ξ» i, neg_add_relabelling (xL i) (mk yl yr yL yR))
(Ξ» i, neg_add_relabelling (mk xl xr xL xR) (yL i))β©
using_well_founded { dec_tac := pgame_wf_tac }
theorem neg_add_le {x y : pgame} : -(x + y) β€ -x + -y :=
le_of_relabelling (neg_add_relabelling x y)
/-- `x+y` has exactly the same moves as `y+x`. -/
def add_comm_relabelling : Ξ (x y : pgame.{u}), relabelling (x + y) (y + x)
| (mk xl xr xL xR) (mk yl yr yL yR) :=
begin
refine β¨equiv.sum_comm _ _, equiv.sum_comm _ _, _, _β©;
rintros (_|_);
{ simp [left_moves_add, right_moves_add], apply add_comm_relabelling }
end
using_well_founded { dec_tac := pgame_wf_tac }
theorem add_comm_le {x y : pgame} : x + y β€ y + x :=
le_of_relabelling (add_comm_relabelling x y)
theorem add_comm_equiv {x y : pgame} : x + y β y + x :=
equiv_of_relabelling (add_comm_relabelling x y)
/-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/
def add_assoc_relabelling : Ξ (x y z : pgame.{u}), relabelling ((x + y) + z) (x + (y + z))
| (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) :=
begin
refine β¨equiv.sum_assoc _ _ _, equiv.sum_assoc _ _ _, _, _β©,
{ rintro (β¨i|iβ©|i),
{ apply add_assoc_relabelling, },
{ change relabelling
(mk xl xr xL xR + yL i + mk zl zr zL zR) (mk xl xr xL xR + (yL i + mk zl zr zL zR)),
apply add_assoc_relabelling, },
{ change relabelling
(mk xl xr xL xR + mk yl yr yL yR + zL i) (mk xl xr xL xR + (mk yl yr yL yR + zL i)),
apply add_assoc_relabelling, } },
{ rintro (j|β¨j|jβ©),
{ apply add_assoc_relabelling, },
{ change relabelling
(mk xl xr xL xR + yR j + mk zl zr zL zR) (mk xl xr xL xR + (yR j + mk zl zr zL zR)),
apply add_assoc_relabelling, },
{ change relabelling
(mk xl xr xL xR + mk yl yr yL yR + zR j) (mk xl xr xL xR + (mk yl yr yL yR + zR j)),
apply add_assoc_relabelling, } },
end
using_well_founded { dec_tac := pgame_wf_tac }
theorem add_assoc_equiv {x y z : pgame} : (x + y) + z β x + (y + z) :=
equiv_of_relabelling (add_assoc_relabelling x y z)
theorem add_le_add_right : Ξ {x y z : pgame} (h : x β€ y), x + z β€ y + z
| (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) :=
begin
intros h,
rw le_def,
split,
{ -- if Left plays first
intros i,
change xl β zl at i,
cases i,
{ -- either they play in x
rw le_def at h,
cases h,
have t := h_left i,
rcases t with β¨i', ihβ© | β¨j, jhβ©,
{ left,
refine β¨(left_moves_add _ _).inv_fun (sum.inl i'), _β©,
exact add_le_add_right ih, },
{ right,
refine β¨(right_moves_add _ _).inv_fun (sum.inl j), _β©,
convert add_le_add_right jh,
apply add_move_right_inl },
},
{ -- or play in z
left,
refine β¨(left_moves_add _ _).inv_fun (sum.inr i), _β©,
exact add_le_add_right h, }, },
{ -- if Right plays first
intros j,
change yr β zr at j,
cases j,
{ -- either they play in y
rw le_def at h,
cases h,
have t := h_right j,
rcases t with β¨i, ihβ© | β¨j', jhβ©,
{ left,
refine β¨(left_moves_add _ _).inv_fun (sum.inl i), _β©,
convert add_le_add_right ih,
apply add_move_left_inl },
{ right,
refine β¨(right_moves_add _ _).inv_fun (sum.inl j'), _β©,
exact add_le_add_right jh } },
{ -- or play in z
right,
refine β¨(right_moves_add _ _).inv_fun (sum.inr j), _β©,
exact add_le_add_right h } }
end
using_well_founded { dec_tac := pgame_wf_tac }
theorem add_le_add_left {x y z : pgame} (h : y β€ z) : x + y β€ x + z :=
calc x + y β€ y + x : add_comm_le
... β€ z + x : add_le_add_right h
... β€ x + z : add_comm_le
theorem add_congr {w x y z : pgame} (hβ : w β x) (hβ : y β z) : w + y β x + z :=
β¨calc w + y β€ w + z : add_le_add_left hβ.1
... β€ x + z : add_le_add_right hβ.1,
calc x + z β€ x + y : add_le_add_left hβ.2
... β€ w + y : add_le_add_right hβ.2β©
theorem add_left_neg_le_zero : Ξ {x : pgame}, (-x) + x β€ 0
| β¨xl, xr, xL, xRβ© :=
begin
rw [le_def],
split,
{ intro i,
change xr β xl at i,
cases i,
{ -- If Left played in -x, Right responds with the same move in x.
right,
refine β¨(right_moves_add _ _).inv_fun (sum.inr i), _β©,
convert @add_left_neg_le_zero (xR i),
exact add_move_right_inr },
{ -- If Left in x, Right responds with the same move in -x.
right,
dsimp,
refine β¨(right_moves_add _ _).inv_fun (sum.inl i), _β©,
convert @add_left_neg_le_zero (xL i),
exact add_move_right_inl }, },
{ rintro β¨β©, }
end
using_well_founded { dec_tac := pgame_wf_tac }
theorem zero_le_add_left_neg : Ξ {x : pgame}, 0 β€ (-x) + x :=
begin
intro x,
rw [le_iff_neg_ge, neg_zero],
exact le_trans neg_add_le add_left_neg_le_zero
end
theorem add_left_neg_equiv {x : pgame} : (-x) + x β 0 :=
β¨add_left_neg_le_zero, zero_le_add_left_negβ©
theorem add_right_neg_le_zero {x : pgame} : x + (-x) β€ 0 :=
calc x + (-x) β€ (-x) + x : add_comm_le
... β€ 0 : add_left_neg_le_zero
theorem zero_le_add_right_neg {x : pgame} : 0 β€ x + (-x) :=
calc 0 β€ (-x) + x : zero_le_add_left_neg
... β€ x + (-x) : add_comm_le
theorem add_lt_add_right {x y z : pgame} (h : x < y) : x + z < y + z :=
suffices y + z β€ x + z β y β€ x, by { rw βnot_le at β’ h, exact mt this h },
assume w,
calc y β€ y + 0 : le_of_relabelling (add_zero_relabelling _).symm
... β€ y + (z + -z) : add_le_add_left zero_le_add_right_neg
... β€ (y + z) + (-z) : le_of_relabelling (add_assoc_relabelling _ _ _).symm
... β€ (x + z) + (-z) : add_le_add_right w
... β€ x + (z + -z) : le_of_relabelling (add_assoc_relabelling _ _ _)
... β€ x + 0 : add_le_add_left add_right_neg_le_zero
... β€ x : le_of_relabelling (add_zero_relabelling _)
theorem add_lt_add_left {x y z : pgame} (h : y < z) : x + y < x + z :=
calc x + y β€ y + x : add_comm_le
... < z + x : add_lt_add_right h
... β€ x + z : add_comm_le
theorem le_iff_sub_nonneg {x y : pgame} : x β€ y β 0 β€ y - x :=
β¨Ξ» h, le_trans zero_le_add_right_neg (add_le_add_right h),
Ξ» h,
calc x β€ 0 + x : le_of_relabelling (zero_add_relabelling x).symm
... β€ (y - x) + x : add_le_add_right h
... β€ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _)
... β€ y + 0 : add_le_add_left (add_left_neg_le_zero)
... β€ y : le_of_relabelling (add_zero_relabelling y)β©
theorem lt_iff_sub_pos {x y : pgame} : x < y β 0 < y - x :=
β¨Ξ» h, lt_of_le_of_lt zero_le_add_right_neg (add_lt_add_right h),
Ξ» h,
calc x β€ 0 + x : le_of_relabelling (zero_add_relabelling x).symm
... < (y - x) + x : add_lt_add_right h
... β€ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _)
... β€ y + 0 : add_le_add_left (add_left_neg_le_zero)
... β€ y : le_of_relabelling (add_zero_relabelling y)β©
/-- The pre-game `star`, which is fuzzy/confused with zero. -/
def star : pgame := pgame.of_lists [0] [0]
theorem star_lt_zero : star < 0 :=
or.inr β¨β¨0, zero_lt_oneβ©, (by split; rintros β¨β©)β©
theorem zero_lt_star : 0 < star :=
or.inl β¨β¨0, zero_lt_oneβ©, (by split; rintros β¨β©)β©
/-- The pre-game `Ο`. (In fact all ordinals have game and surreal representatives.) -/
def omega : pgame := β¨ulift β, pempty, Ξ» n, βn.1, pempty.elimβ©
end pgame
|
4c0762a5bd1de92cef5eb31a2009bc5affd57d40 | 74caf7451c921a8d5ab9c6e2b828c9d0a35aae95 | /library/init/data/list/basic.lean | 06c689964edf18df67d076438d138de5ff1f8b69 | [
"Apache-2.0"
] | permissive | sakas--/lean | f37b6fad4fd4206f2891b89f0f8135f57921fc3f | 570d9052820be1d6442a5cc58ece37397f8a9e4c | refs/heads/master | 1,586,127,145,194 | 1,480,960,018,000 | 1,480,960,635,000 | 40,137,176 | 0 | 0 | null | 1,438,621,351,000 | 1,438,621,351,000 | null | UTF-8 | Lean | false | false | 3,499 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import init.logic init.data.nat.basic
open decidable list
notation h :: t := cons h t
notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l
universe variables u v
instance (Ξ± : Type u) : inhabited (list Ξ±) :=
β¨list.nilβ©
variables {Ξ± : Type u} {Ξ² : Type v}
namespace list
protected def append : list Ξ± β list Ξ± β list Ξ±
| [] l := l
| (h :: s) t := h :: (append s t)
instance : has_append (list Ξ±) :=
β¨list.appendβ©
protected def mem : Ξ± β list Ξ± β Prop
| a [] := false
| a (b :: l) := a = b β¨ mem a l
instance : has_mem Ξ± list :=
β¨list.memβ©
instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) : β (l : list Ξ±), decidable (a β l)
| [] := is_false not_false
| (b::l) :=
if hβ : a = b then is_true (or.inl hβ)
else match decidable_mem l with
| is_true hβ := is_true (or.inr hβ)
| is_false hβ := is_false (not_or hβ hβ)
end
def concat : list Ξ± β Ξ± β list Ξ±
| [] a := [a]
| (b::l) a := b :: concat l a
instance : has_emptyc (list Ξ±) :=
β¨list.nilβ©
protected def insert [decidable_eq Ξ±] (a : Ξ±) (l : list Ξ±) : list Ξ± :=
if a β l then l else concat l a
instance [decidable_eq Ξ±] : has_insert Ξ± list :=
β¨list.insertβ©
protected def union [decidable_eq Ξ±] : list Ξ± β list Ξ± β list Ξ±
| lβ [] := lβ
| lβ (a::lβ) := union (insert a lβ) lβ
instance [decidable_eq Ξ±] : has_union (list Ξ±) :=
β¨list.unionβ©
protected def inter [decidable_eq Ξ±] : list Ξ± β list Ξ± β list Ξ±
| [] lβ := []
| (a::lβ) lβ := if a β lβ then a :: inter lβ lβ else inter lβ lβ
instance [decidable_eq Ξ±] : has_inter (list Ξ±) :=
β¨list.interβ©
def length : list Ξ± β nat
| [] := 0
| (a :: l) := length l + 1
open option nat
def nth : list Ξ± β nat β option Ξ±
| [] n := none
| (a :: l) 0 := some a
| (a :: l) (n+1) := nth l n
def head [inhabited Ξ±] : list Ξ± β Ξ±
| [] := default Ξ±
| (a :: l) := a
def tail : list Ξ± β list Ξ±
| [] := []
| (a :: l) := l
def reverse : list Ξ± β list Ξ±
| [] := []
| (a :: l) := concat (reverse l) a
def map (f : Ξ± β Ξ²) : list Ξ± β list Ξ²
| [] := []
| (a :: l) := f a :: map l
def for : list Ξ± β (Ξ± β Ξ²) β list Ξ² :=
flip map
def join : list (list Ξ±) β list Ξ±
| [] := []
| (l :: ls) := append l (join ls)
def filter (p : Ξ± β Prop) [decidable_pred p] : list Ξ± β list Ξ±
| [] := []
| (a::l) := if p a then a :: filter l else filter l
def dropn : β β list Ξ± β list Ξ±
| 0 a := a
| (succ n) [] := []
| (succ n) (x::r) := dropn n r
definition foldl (f : Ξ± β Ξ² β Ξ±) : Ξ± β list Ξ² β Ξ±
| a [] := a
| a (b :: l) := foldl (f a b) l
definition foldr (f : Ξ± β Ξ² β Ξ²) : Ξ² β list Ξ± β Ξ²
| b [] := b
| b (a :: l) := f a (foldr b l)
definition any (l : list Ξ±) (p : Ξ± β bool) : bool :=
foldr (Ξ» a r, p a || r) ff l
definition all (l : list Ξ±) (p : Ξ± β bool) : bool :=
foldr (Ξ» a r, p a && r) tt l
def zip : list Ξ± β list Ξ² β list (prod Ξ± Ξ²)
| [] _ := []
| _ [] := []
| (x::xs) (y::ys) := (prod.mk x y) :: zip xs ys
def repeat (a : Ξ±) : β β list Ξ±
| 0 := []
| (succ n) := a :: repeat n
def iota : β β list β
| 0 := []
| (succ n) := iota n ++ [succ n]
end list
|
0b11be5a0979668fc59b0094ff4bcd85ace04009 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/linear_algebra/tensor_product.lean | ce1e87e72d4e166053290263eb1a4fe72f2bddbc | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 35,731 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import group_theory.congruence
import linear_algebra.basic
/-!
# Tensor product of semimodules over commutative semirings.
This file constructs the tensor product of semimodules over commutative semirings. Given a semiring
`R` and semimodules over it `M` and `N`, the standard construction of the tensor product is
`tensor_product R M N`. It is also a semimodule over `R`.
It comes with a canonical bilinear map `M β N β tensor_product R M N`.
Given any bilinear map `M β N β P`, there is a unique linear map `tensor_product R M N β P` whose
composition with the canonical bilinear map `M β N β tensor_product R M N` is the given bilinear
map `M β N β P`.
We start by proving basic lemmas about bilinear maps.
## Notations
This file uses the localized notation `M β N` and `M β[R] N` for `tensor_product R M N`, as well
as `m ββ n` and `m ββ[R] n` for `tensor_product.tmul R m n`.
## Tags
bilinear, tensor, tensor product
-/
namespace linear_map
section semiring
variables {R : Type*} [semiring R] {S : Type*} [semiring S]
variables {M : Type*} {N : Type*} {P : Type*}
variables {M' : Type*} {N' : Type*} {P' : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P]
variables [add_comm_group M'] [add_comm_group N'] [add_comm_group P']
variables [semimodule R M] [semimodule S N] [semimodule R P] [semimodule S P]
variables [semimodule R M'] [semimodule S N'] [semimodule R P'] [semimodule S P']
variables [smul_comm_class S R P] [smul_comm_class S R P']
include R
variables (R S)
/-- Create a bilinear map from a function that is linear in each component.
See `mkβ` for the special case where both arguments come from modules over the same ring. -/
def mkβ' (f : M β N β P)
(H1 : β mβ mβ n, f (mβ + mβ) n = f mβ n + f mβ n)
(H2 : β (c:R) m n, f (c β’ m) n = c β’ f m n)
(H3 : β m nβ nβ, f m (nβ + nβ) = f m nβ + f m nβ)
(H4 : β (c:S) m n, f m (c β’ n) = c β’ f m n) : M ββ[R] N ββ[S] P :=
β¨Ξ» m, β¨f m, H3 m, Ξ» c, H4 c mβ©,
Ξ» mβ mβ, linear_map.ext $ H1 mβ mβ,
Ξ» c m, linear_map.ext $ H2 c mβ©
variables {R S}
@[simp] theorem mkβ'_apply
(f : M β N β P) {H1 H2 H3 H4} (m : M) (n : N) :
(mkβ' R S f H1 H2 H3 H4 : M ββ[R] N ββ[S] P) m n = f m n := rfl
theorem extβ {f g : M ββ[R] N ββ[S] P}
(H : β m n, f m n = g m n) : f = g :=
linear_map.ext (Ξ» m, linear_map.ext $ Ξ» n, H m n)
section
local attribute [instance] smul_comm_class.symm
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map from `M Γ N` to
`P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def flip (f : M ββ[R] N ββ[S] P) : N ββ[S] M ββ[R] P :=
mkβ' S R (Ξ» n m, f m n)
(Ξ» nβ nβ m, (f m).map_add _ _)
(Ξ» c n m, (f m).map_smul _ _)
(Ξ» n mβ mβ, by rw f.map_add; refl)
(Ξ» c n m, by rw f.map_smul; refl)
end
@[simp] theorem flip_apply (f : M ββ[R] N ββ[S] P) (m : M) (n : N) : flip f n m = f m n := rfl
variables {R}
theorem flip_inj {f g : M ββ[R] N ββ[S] P} (H : flip f = flip g) : f = g :=
extβ $ Ξ» m n, show flip f n m = flip g n m, by rw H
theorem map_zeroβ (f : M ββ[R] N ββ[S] P) (y) : f 0 y = 0 :=
(flip f y).map_zero
theorem map_negβ (f : M' ββ[R] N ββ[S] P') (x y) : f (-x) y = -f x y :=
(flip f y).map_neg _
theorem map_subβ (f : M' ββ[R] N ββ[S] P') (x y z) : f (x - y) z = f x z - f y z :=
(flip f z).map_sub _ _
theorem map_addβ (f : M ββ[R] N ββ[S] P) (xβ xβ y) : f (xβ + xβ) y = f xβ y + f xβ y :=
(flip f y).map_add _ _
theorem map_smulβ (f : M ββ[R] N ββ[S] P) (r : R) (x y) : f (r β’ x) y = r β’ f x y :=
(flip f y).map_smul _ _
end semiring
section comm_semiring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q]
variables (R)
/-- Create a bilinear map from a function that is linear in each component.
This is a shorthand for `mkβ'` for the common case when `R = S`. -/
def mkβ (f : M β N β P)
(H1 : β mβ mβ n, f (mβ + mβ) n = f mβ n + f mβ n)
(H2 : β (c:R) m n, f (c β’ m) n = c β’ f m n)
(H3 : β m nβ nβ, f m (nβ + nβ) = f m nβ + f m nβ)
(H4 : β (c:R) m n, f m (c β’ n) = c β’ f m n) : M ββ[R] N ββ[R] P :=
mkβ' R R f H1 H2 H3 H4
@[simp] theorem mkβ_apply
(f : M β N β P) {H1 H2 H3 H4} (m : M) (n : N) :
(mkβ R f H1 H2 H3 H4 : M ββ[R] N ββ[R] P) m n = f m n := rfl
variables (R M N P)
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map `M β N β P`,
change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def lflip : (M ββ[R] N ββ[R] P) ββ[R] N ββ[R] M ββ[R] P :=
β¨flip, Ξ» _ _, rfl, Ξ» _ _, rflβ©
variables {R M N P}
variables (f : M ββ[R] N ββ[R] P)
@[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl
variables (R P)
/-- Composing a linear map `M β N` and a linear map `N β P` to form a linear map `M β P`. -/
def lcomp (f : M ββ[R] N) : (N ββ[R] P) ββ[R] M ββ[R] P :=
flip $ linear_map.comp (flip id) f
variables {R P}
@[simp] theorem lcomp_apply (f : M ββ[R] N) (g : N ββ P) (x : M) :
lcomp R P f g x = g (f x) := rfl
variables (R M N P)
/-- Composing a linear map `M β N` and a linear map `N β P` to form a linear map `M β P`. -/
def llcomp : (N ββ[R] P) ββ[R] (M ββ[R] N) ββ M ββ P :=
flip β¨lcomp R P,
Ξ» f f', extβ $ Ξ» g x, g.map_add _ _,
Ξ» (c : R) f, extβ $ Ξ» g x, g.map_smul _ _β©
variables {R M N P}
section
@[simp] theorem llcomp_apply (f : N ββ[R] P) (g : M ββ[R] N) (x : M) :
llcomp R M N P f g x = f (g x) := rfl
end
/-- Composing a linear map `Q β N` and a bilinear map `M β N β P` to
form a bilinear map `M β Q β P`. -/
def complβ (g : Q ββ N) : M ββ Q ββ P := (lcomp R _ g).comp f
@[simp] theorem complβ_apply (g : Q ββ[R] N) (m : M) (q : Q) :
f.complβ g m q = f m (g q) := rfl
/-- Composing a linear map `P β Q` and a bilinear map `M Γ N β P` to
form a bilinear map `M β N β Q`. -/
def comprβ (g : P ββ Q) : M ββ N ββ Q :=
linear_map.comp (llcomp R N P Q g) f
@[simp] theorem comprβ_apply (g : P ββ[R] Q) (m : M) (n : N) :
f.comprβ g m n = g (f m n) := rfl
variables (R M)
/-- Scalar multiplication as a bilinear map `R β M β M`. -/
def lsmul : R ββ M ββ M :=
mkβ R (β’) add_smul (Ξ» _ _ _, mul_smul _ _ _) smul_add
(Ξ» r s m, by simp only [smul_smul, smul_eq_mul, mul_comm])
variables {R M}
@[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r β’ m := rfl
end comm_semiring
end linear_map
section semiring
variables {R : Type*} [comm_semiring R]
variables {R' : Type*} [comm_semiring R']
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
[add_comm_monoid S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
variables [semimodule R' M] [semimodule R' N]
include R
variables (M N)
namespace tensor_product
section
-- open free_add_monoid
variables (R)
/-- The relation on `free_add_monoid (M Γ N)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (M Γ N) β free_add_monoid (M Γ N) β Prop
| of_zero_left : β n : N, eqv (free_add_monoid.of (0, n)) 0
| of_zero_right : β m : M, eqv (free_add_monoid.of (m, 0)) 0
| of_add_left : β (mβ mβ : M) (n : N), eqv
(free_add_monoid.of (mβ, n) + free_add_monoid.of (mβ, n)) (free_add_monoid.of (mβ + mβ, n))
| of_add_right : β (m : M) (nβ nβ : N), eqv
(free_add_monoid.of (m, nβ) + free_add_monoid.of (m, nβ)) (free_add_monoid.of (m, nβ + nβ))
| of_smul : β (r : R) (m : M) (n : N), eqv
(free_add_monoid.of (r β’ m, n)) (free_add_monoid.of (m, r β’ n))
| add_comm : β x y, eqv (x + y) (y + x)
end
end tensor_product
variables (R)
/-- The tensor product of two semimodules `M` and `N` over the same commutative semiring `R`.
The localized notations are `M β N` and `M β[R] N`, accessed by `open_locale tensor_product`. -/
def tensor_product : Type* :=
(add_con_gen (tensor_product.eqv R M N)).quotient
variables {R}
localized "infix ` β `:100 := tensor_product _" in tensor_product
localized "notation M ` β[`:100 R `] `:0 N:100 := tensor_product R M N" in tensor_product
namespace tensor_product
section module
instance : add_comm_monoid (M β[R] N) :=
{ add_comm := Ξ» x y, add_con.induction_onβ x y $ Ξ» x y, quotient.sound' $
add_con_gen.rel.of _ _ $ eqv.add_comm _ _,
.. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : inhabited (M β[R] N) := β¨0β©
variables (R) {M N}
/-- The canonical function `M β N β M β N`. The localized notations are `m ββ n` and `m ββ[R] n`,
accessed by `open_locale tensor_product`. -/
def tmul (m : M) (n : N) : M β[R] N := add_con.mk' _ $ free_add_monoid.of (m, n)
variables {R}
infix ` ββ `:100 := tmul _
notation x ` ββ[`:100 R `] `:0 y:100 := tmul R x y
@[elab_as_eliminator]
protected theorem induction_on
{C : (M β[R] N) β Prop}
(z : M β[R] N)
(C0 : C 0)
(C1 : β {x y}, C $ x ββ[R] y)
(Cp : β {x y}, C x β C y β C (x + y)) : C z :=
add_con.induction_on z $ Ξ» x, free_add_monoid.rec_on x C0 $ Ξ» β¨m, nβ© y ih,
by { rw add_con.coe_add, exact Cp C1 ih }
variables (M)
@[simp] lemma zero_tmul (n : N) : (0 : M) ββ[R] n = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _
variables {M}
lemma add_tmul (mβ mβ : M) (n : N) : (mβ + mβ) ββ n = mβ ββ n + mβ ββ[R] n :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _
variables (N)
@[simp] lemma tmul_zero (m : M) : m ββ[R] (0 : N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _
variables {N}
lemma tmul_add (m : M) (nβ nβ : N) : m ββ (nβ + nβ) = m ββ nβ + m ββ[R] nβ :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _
section
variables (R R' M N)
/--
A typeclass for `has_scalar` structures which can be moved across a tensor product.
This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that
we can also add an instance for `add_comm_group.int_module`, allowing `z β’` to be moved even if
`R` does not support negation.
Note that `semimodule R' (M β[R] N)` is available even without this typeclass on `R'`; it's only
needed if `tensor_product.smul_tmul`, `tensor_product.smul_tmul'`, or `tensor_product.tmul_smul` is
used.
-/
class compatible_smul :=
(smul_tmul : β (r : R') (m : M) (n : N), (r β’ m) ββ n = m ββ[R] (r β’ n))
end
/-- Note that this provides the default `compatible_smul R R M N` instance through
`mul_action.is_scalar_tower.left`. -/
@[priority 100]
instance compatible_smul.is_scalar_tower
[has_scalar R' R] [is_scalar_tower R' R M] [is_scalar_tower R' R N] :
compatible_smul R R' M N :=
β¨Ξ» r m n, begin
conv_lhs {rw β one_smul R m},
conv_rhs {rw β one_smul R n},
rw [βsmul_assoc, βsmul_assoc],
exact (quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _),
endβ©
/-- `smul` can be moved from one side of the product to the other .-/
lemma smul_tmul [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
(r β’ m) ββ n = m ββ[R] (r β’ n) :=
compatible_smul.smul_tmul _ _ _
/-- Auxiliary function to defining scalar multiplication on tensor product. -/
def smul.aux {R' : Type*} [has_scalar R' M] (r : R') : free_add_monoid (M Γ N) β+ M β[R] N :=
free_add_monoid.lift $ Ξ» p : M Γ N, (r β’ p.1) ββ p.2
theorem smul.aux_of {R' : Type*} [has_scalar R' M] (r : R') (m : M) (n : N) :
smul.aux r (free_add_monoid.of (m, n)) = (r β’ m) ββ[R] n :=
rfl
variables [smul_comm_class R R' M] [smul_comm_class R R' N]
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find. The `unused_arguments` is from one of the two comm_classes - while we only make use
-- of one, it makes sense to make the API symmetric.
@[nolint unused_arguments]
instance has_scalar' : has_scalar R' (M β[R] N) :=
β¨Ξ» r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r : _ β+ M β[R] N) $
add_con.add_con_gen_le $ Ξ» x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero]
| _, _, (eqv.of_add_left mβ mβ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul]
| _, _, (eqv.of_add_right m nβ nβ) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by rw [smul.aux_of, smul.aux_of, βsmul_comm, smul_tmul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
endβ©
instance : has_scalar R (M β[R] N) := tensor_product.has_scalar'
protected theorem smul_zero (r : R') : (r β’ 0 : M β[R] N) = 0 :=
add_monoid_hom.map_zero _
protected theorem smul_add (r : R') (x y : M β[R] N) :
r β’ (x + y) = r β’ x + r β’ y :=
add_monoid_hom.map_add _ _ _
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find.
instance semimodule' : semimodule R' (M β[R] N) :=
have β (r : R') (m : M) (n : N), r β’ (m ββ[R] n) = (r β’ m) ββ n := Ξ» _ _ _, rfl,
{ smul := (β’),
smul_add := Ξ» r x y, tensor_product.smul_add r x y,
mul_smul := Ξ» r s x, tensor_product.induction_on x
(by simp_rw tensor_product.smul_zero)
(Ξ» m n, by simp_rw [this, mul_smul])
(Ξ» x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }),
one_smul := Ξ» x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(Ξ» m n, by rw [this, one_smul])
(Ξ» x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy]),
add_smul := Ξ» r s x, tensor_product.induction_on x
(by simp_rw [tensor_product.smul_zero, add_zero])
(Ξ» m n, by simp_rw [this, add_smul, add_tmul])
(Ξ» x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] }),
smul_zero := Ξ» r, tensor_product.smul_zero r,
zero_smul := Ξ» x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(Ξ» m n, by rw [this, zero_smul, zero_tmul])
(Ξ» x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero]) }
instance : semimodule R (M β[R] N) := tensor_product.semimodule'
-- note that we don't actually need `compatible_smul` here, but we include it for symmetry
-- with `tmul_smul` to avoid exposing our asymmetric definition.
@[nolint unused_arguments]
theorem smul_tmul' [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
r β’ (m ββ[R] n) = (r β’ m) ββ n :=
rfl
@[simp] lemma tmul_smul [compatible_smul R R' M N] (r : R') (x : M) (y : N) :
x ββ (r β’ y) = r β’ (x ββ[R] y) :=
(smul_tmul _ _ _).symm
variables (R M N)
/-- The canonical bilinear map `M β N β M β[R] N`. -/
def mk : M ββ N ββ M β[R] N :=
linear_map.mkβ R (ββ) add_tmul (Ξ» c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul
variables {R M N}
@[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ββ n := rfl
lemma ite_tmul (xβ : M) (xβ : N) (P : Prop) [decidable P] :
(if P then xβ else 0) ββ[R] xβ = if P then xβ ββ xβ else 0 :=
by { split_ifs; simp }
lemma tmul_ite (xβ : M) (xβ : N) (P : Prop) [decidable P] :
xβ ββ[R] (if P then xβ else 0) = if P then xβ ββ xβ else 0 :=
by { split_ifs; simp }
section
open_locale big_operators
lemma sum_tmul {Ξ± : Type*} (s : finset Ξ±) (m : Ξ± β M) (n : N) :
(β a in s, m a) ββ[R] n = β a in s, m a ββ[R] n :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, add_tmul, ih], },
end
lemma tmul_sum (m : M) {Ξ± : Type*} (s : finset Ξ±) (n : Ξ± β N) :
m ββ[R] (β a in s, n a) = β a in s, m ββ[R] n a :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, tmul_add, ih], },
end
end
end module
section UMP
variables {M N P Q}
variables (f : M ββ[R] N ββ[R] P)
/-- Auxiliary function to constructing a linear map `M β N β P` given a bilinear map `M β N β P`
with the property that its composition with the canonical bilinear map `M β N β M β N` is
the given bilinear map `M β N β P`. -/
def lift_aux : (M β[R] N) β+ P :=
(add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ Ξ» p : M Γ N, f p.1 p.2) $
add_con.add_con_gen_le $ Ξ» x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zeroβ]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero]
| _, _, (eqv.of_add_left mβ mβ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_addβ]
| _, _, (eqv.of_add_right m nβ nβ) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add]
| _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $
by simp_rw [free_add_monoid.lift_eval_of, f.map_smulβ, (f m).map_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
lemma lift_aux_tmul (m n) : lift_aux f (m ββ n) = f m n :=
zero_add _
variable {f}
@[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r β’ x) = r β’ lift_aux f x :=
tensor_product.induction_on x (smul_zero _).symm
(Ξ» p q, by rw [β tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul])
(Ξ» p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add])
variable (f)
/-- Constructing a linear map `M β N β P` given a bilinear map `M β N β P` with the property that
its composition with the canonical bilinear map `M β N β M β N` is
the given bilinear map `M β N β P`. -/
def lift : M β N ββ P :=
{ map_smul' := lift_aux.smul,
.. lift_aux f }
variable {f}
@[simp] lemma lift.tmul (x y) : lift f (x ββ y) = f x y :=
zero_add _
@[simp] lemma lift.tmul' (x y) : (lift f).1 (x ββ y) = f x y :=
lift.tmul _ _
theorem ext {g h : (M β[R] N) ββ[R] P}
(H : β x y, g (x ββ y) = h (x ββ y)) : g = h :=
linear_map.ext $ Ξ» z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $
Ξ» x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy]
theorem lift.unique {g : (M β[R] N) ββ[R] P} (H : β x y, g (x ββ y) = f x y) :
g = lift f :=
ext $ Ξ» m n, by rw [H, lift.tmul]
theorem lift_mk : lift (mk R M N) = linear_map.id :=
eq.symm $ lift.unique $ Ξ» x y, rfl
theorem lift_comprβ (g : P ββ Q) : lift (f.comprβ g) = g.comp (lift f) :=
eq.symm $ lift.unique $ Ξ» x y, by simp
theorem lift_mk_comprβ (f : M β N ββ P) : lift ((mk R M N).comprβ f) = f :=
by rw [lift_comprβ f, lift_mk, linear_map.comp_id]
/--
Using this as the `@[ext]` lemma instead of `tensor_product.ext` allows `ext` to apply lemmas
specific to `M ββ _` and `N ββ _`.
See note [partially-applied ext lemmas]. -/
@[ext]
theorem mk_comprβ_inj {g h : M β N ββ P}
(H : (mk R M N).comprβ g = (mk R M N).comprβ h) : g = h :=
by rw [β lift_mk_comprβ g, H, lift_mk_comprβ]
example : M β N β (M β N β P) β P :=
Ξ» m, flip $ Ξ» f, f m
variables (R M N P)
/-- Linearly constructing a linear map `M β N β P` given a bilinear map `M β N β P`
with the property that its composition with the canonical bilinear map `M β N β M β N` is
the given bilinear map `M β N β P`. -/
def uncurry : (M ββ[R] N ββ[R] P) ββ[R] M β[R] N ββ[R] P :=
linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id)
variables {R M N P}
@[simp] theorem uncurry_apply (f : M ββ[R] N ββ[R] P) (m : M) (n : N) :
uncurry R M N P f (m ββ n) = f m n :=
by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl
variables (R M N P)
/-- A linear equivalence constructing a linear map `M β N β P` given a bilinear map `M β N β P`
with the property that its composition with the canonical bilinear map `M β N β M β N` is
the given bilinear map `M β N β P`. -/
def lift.equiv : (M ββ N ββ P) ββ (M β N ββ P) :=
{ inv_fun := Ξ» f, (mk R M N).comprβ f,
left_inv := Ξ» f, linear_map.extβ $ Ξ» m n, lift.tmul _ _,
right_inv := Ξ» f, ext $ Ξ» m n, lift.tmul _ _,
.. uncurry R M N P }
/-- Given a linear map `M β N β P`, compose it with the canonical bilinear map `M β N β M β N` to
form a bilinear map `M β N β P`. -/
def lcurry : (M β[R] N ββ[R] P) ββ[R] M ββ[R] N ββ[R] P :=
(lift.equiv R M N P).symm
variables {R M N P}
@[simp] theorem lcurry_apply (f : M β[R] N ββ[R] P) (m : M) (n : N) :
lcurry R M N P f m n = f (m ββ n) := rfl
/-- Given a linear map `M β N β P`, compose it with the canonical bilinear map `M β N β M β N` to
form a bilinear map `M β N β P`. -/
def curry (f : M β N ββ P) : M ββ N ββ P := lcurry R M N P f
@[simp] theorem curry_apply (f : M β N ββ[R] P) (m : M) (n : N) :
curry f m n = f (m ββ n) := rfl
theorem ext_threefold {g h : (M β[R] N) β[R] P ββ[R] Q}
(H : β x y z, g ((x ββ y) ββ z) = h ((x ββ y) ββ z)) : g = h :=
begin
ext x y z,
exact H x y z
end
-- We'll need this one for checking the pentagon identity!
theorem ext_fourfold {g h : ((M β[R] N) β[R] P) β[R] Q ββ[R] S}
(H : β w x y z, g (((w ββ x) ββ y) ββ z) = h (((w ββ x) ββ y) ββ z)) : g = h :=
begin
ext w x y z,
exact H w x y z,
end
end UMP
variables {M N}
section
variables (R M)
/--
The base ring is a left identity for the tensor product of modules, up to linear equivalence.
-/
protected def lid : R β M ββ M :=
linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1)
(linear_map.ext $ Ξ» _, by simp)
(ext $ Ξ» r m, by simp; rw [β tmul_smul, β smul_tmul, smul_eq_mul, mul_one])
end
@[simp] theorem lid_tmul (m : M) (r : R) :
((tensor_product.lid R M) : (R β M β M)) (r ββ m) = r β’ m :=
begin
dsimp [tensor_product.lid],
simp,
end
@[simp] lemma lid_symm_apply (m : M) :
(tensor_product.lid R M).symm m = 1 ββ m := rfl
section
variables (R M N)
/--
The tensor product of modules is commutative, up to linear equivalence.
-/
protected def comm : M β N ββ N β M :=
linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip)
(ext $ Ξ» m n, rfl)
(ext $ Ξ» m n, rfl)
@[simp] theorem comm_tmul (m : M) (n : N) :
(tensor_product.comm R M N) (m ββ n) = n ββ m := rfl
@[simp] theorem comm_symm_tmul (m : M) (n : N) :
(tensor_product.comm R M N).symm (n ββ m) = m ββ n := rfl
end
section
variables (R M)
/--
The base ring is a right identity for the tensor product of modules, up to linear equivalence.
-/
protected def rid : M β[R] R ββ M :=
linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M)
end
@[simp] theorem rid_tmul (m : M) (r : R) :
(tensor_product.rid R M) (m ββ r) = r β’ m :=
begin
dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid],
simp,
end
@[simp] lemma rid_symm_apply (m : M) :
(tensor_product.rid R M).symm m = m ββ 1 := rfl
open linear_map
section
variables (R M N P)
/-- The associator for tensor product of R-modules, as a linear equivalence. -/
protected def assoc : (M β[R] N) β[R] P ββ[R] M β[R] (N β[R] P) :=
begin
refine linear_equiv.of_linear
(lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _)
(lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _)
(mk_comprβ_inj $ linear_map.ext $ Ξ» m, ext $ Ξ» n p, _)
(mk_comprβ_inj $ flip_inj $ linear_map.ext $ Ξ» p, ext $ Ξ» m n, _);
repeat { rw lift.tmul <|> rw comprβ_apply <|> rw comp_apply <|>
rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|>
rw uncurry_apply <|> rw curry_apply <|> rw id_apply }
end
end
@[simp] theorem assoc_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P) ((m ββ n) ββ p) = m ββ (n ββ p) := rfl
@[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P).symm (m ββ (n ββ p)) = (m ββ n) ββ p := rfl
/-- The tensor product of a pair of linear maps between modules. -/
def map (f : M ββ[R] P) (g : N ββ Q) : M β N ββ[R] P β Q :=
lift $ comp (complβ (mk _ _ _) g) f
@[simp] theorem map_tmul (f : M ββ[R] P) (g : N ββ[R] Q) (m : M) (n : N) :
map f g (m ββ n) = f m ββ g n :=
rfl
section
variables {P' Q' : Type*}
variables [add_comm_monoid P'] [semimodule R P']
variables [add_comm_monoid Q'] [semimodule R Q']
lemma map_comp (fβ : P ββ[R] P') (fβ : M ββ[R] P) (gβ : Q ββ[R] Q') (gβ : N ββ[R] Q) :
map (fβ.comp fβ) (gβ.comp gβ) = (map fβ gβ).comp (map fβ gβ) :=
ext $ Ξ» _ _, by simp only [linear_map.comp_apply, map_tmul]
lemma lift_comp_map (i : P ββ[R] Q ββ[R] Q') (f : M ββ[R] P) (g : N ββ[R] Q) :
(lift i).comp (map f g) = lift ((i.comp f).complβ g) :=
ext $ Ξ» _ _, by simp only [lift.tmul, map_tmul, linear_map.complβ_apply, linear_map.comp_apply]
end
/-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent
then `M β N` and `P β Q` are linearly equivalent. -/
def congr (f : M ββ[R] P) (g : N ββ[R] Q) : M β N ββ[R] P β Q :=
linear_equiv.of_linear (map f g) (map f.symm g.symm)
(ext $ Ξ» m n, by simp; simp only [linear_equiv.apply_symm_apply])
(ext $ Ξ» m n, by simp; simp only [linear_equiv.symm_apply_apply])
@[simp] theorem congr_tmul (f : M ββ[R] P) (g : N ββ[R] Q) (m : M) (n : N) :
congr f g (m ββ n) = f m ββ g n :=
rfl
@[simp] theorem congr_symm_tmul (f : M ββ[R] P) (g : N ββ[R] Q) (p : P) (q : Q) :
(congr f g).symm (p ββ q) = f.symm p ββ g.symm q :=
rfl
end tensor_product
namespace linear_map
variables {R} (M) {N P Q}
/-- `ltensor M f : M β N ββ M β P` is the natural linear map induced by `f : N ββ P`. -/
def ltensor (f : N ββ[R] P) : M β N ββ[R] M β P :=
tensor_product.map id f
/-- `rtensor f M : Nβ β M ββ Nβ β M` is the natural linear map induced by `f : Nβ ββ Nβ`. -/
def rtensor (f : N ββ[R] P) : N β M ββ[R] P β M :=
tensor_product.map f id
variables (g : P ββ[R] Q) (f : N ββ[R] P)
@[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m ββ n) = m ββ (f n) := rfl
@[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n ββ m) = (f n) ββ m := rfl
open tensor_product
/-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N ββ P` to `M β f`. -/
def ltensor_hom : (N ββ[R] P) ββ[R] (M β[R] N ββ[R] M β[R] P) :=
{ to_fun := ltensor M,
map_add' := Ξ» f g, by {
ext x y, simp only [comprβ_apply, mk_apply, add_apply, ltensor_tmul, tmul_add] },
map_smul' := Ξ» r f, by {
ext x y, simp only [comprβ_apply, mk_apply, tmul_smul, smul_apply, ltensor_tmul] } }
/-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N ββ P` to `M β f`. -/
def rtensor_hom : (N ββ[R] P) ββ[R] (N β[R] M ββ[R] P β[R] M) :=
{ to_fun := Ξ» f, f.rtensor M,
map_add' := Ξ» f g, by {
ext x y, simp only [comprβ_apply, mk_apply, add_apply, rtensor_tmul, add_tmul] },
map_smul' := Ξ» r f, by {
ext x y, simp only [comprβ_apply, mk_apply, smul_tmul, tmul_smul, smul_apply, rtensor_tmul] } }
@[simp] lemma coe_ltensor_hom :
(ltensor_hom M : (N ββ[R] P) β (M β[R] N ββ[R] M β[R] P)) = ltensor M := rfl
@[simp] lemma coe_rtensor_hom :
(rtensor_hom M : (N ββ[R] P) β (N β[R] M ββ[R] P β[R] M)) = rtensor M := rfl
@[simp] lemma ltensor_add (f g : N ββ[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M :=
(ltensor_hom M).map_add f g
@[simp] lemma rtensor_add (f g : N ββ[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M :=
(rtensor_hom M).map_add f g
@[simp] lemma ltensor_zero : ltensor M (0 : N ββ[R] P) = 0 :=
(ltensor_hom M).map_zero
@[simp] lemma rtensor_zero : rtensor M (0 : N ββ[R] P) = 0 :=
(rtensor_hom M).map_zero
@[simp] lemma ltensor_smul (r : R) (f : N ββ[R] P) : (r β’ f).ltensor M = r β’ (f.ltensor M) :=
(ltensor_hom M).map_smul r f
@[simp] lemma rtensor_smul (r : R) (f : N ββ[R] P) : (r β’ f).rtensor M = r β’ (f.rtensor M) :=
(rtensor_hom M).map_smul r f
lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) :=
by { ext m n, simp only [comprβ_apply, mk_apply, comp_apply, ltensor_tmul] }
lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) :=
by { ext m n, simp only [comprβ_apply, mk_apply, comp_apply, rtensor_tmul] }
variables (N)
@[simp] lemma ltensor_id : (id : N ββ[R] N).ltensor M = id :=
by { ext m n, simp only [comprβ_apply, mk_apply, id_coe, id.def, ltensor_tmul] }
@[simp] lemma rtensor_id : (id : N ββ[R] N).rtensor M = id :=
by { ext m n, simp only [comprβ_apply, mk_apply, id_coe, id.def, rtensor_tmul] }
variables {N}
@[simp] lemma ltensor_comp_rtensor (f : M ββ[R] P) (g : N ββ[R] Q) :
(g.ltensor P).comp (f.rtensor N) = map f g :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_ltensor (f : M ββ[R] P) (g : N ββ[R] Q) :
(f.rtensor Q).comp (g.ltensor M) = map f g :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
@[simp] lemma map_comp_rtensor (f : M ββ[R] P) (g : N ββ[R] Q) (f' : S ββ[R] M) :
(map f g).comp (f'.rtensor _) = map (f.comp f') g :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
@[simp] lemma map_comp_ltensor (f : M ββ[R] P) (g : N ββ[R] Q) (g' : S ββ[R] N) :
(map f g).comp (g'.ltensor _) = map f (g.comp g') :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_map (f' : P ββ[R] S) (f : M ββ[R] P) (g : N ββ[R] Q) :
(f'.rtensor _).comp (map f g) = map (f'.comp f) g :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
@[simp] lemma ltensor_comp_map (g' : Q ββ[R] S) (f : M ββ[R] P) (g : N ββ[R] Q) :
(g'.ltensor _).comp (map f g) = map f (g'.comp g) :=
by simp only [ltensor, rtensor, β map_comp, id_comp, comp_id]
end linear_map
end semiring
section ring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q]
[add_comm_group S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
namespace tensor_product
open_locale tensor_product
open linear_map
variables (R)
/-- Auxiliary function to defining negation multiplication on tensor product. -/
def neg.aux : free_add_monoid (M Γ N) β+ M β[R] N :=
free_add_monoid.lift $ Ξ» p : M Γ N, (-p.1) ββ p.2
variables {R}
theorem neg.aux_of (m : M) (n : N) :
neg.aux R (free_add_monoid.of (m, n)) = (-m) ββ[R] n :=
rfl
instance : has_neg (M β[R] N) :=
{ neg := (add_con_gen (tensor_product.eqv R M N)).lift (neg.aux R) $ add_con.add_con_gen_le $
Ξ» x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, neg_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, tmul_zero]
| _, _, (eqv.of_add_left mβ mβ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, neg_add, add_tmul]
| _, _, (eqv.of_add_right m nβ nβ) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by simp_rw [neg.aux_of, tmul_smul s, smul_tmul', smul_neg]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end }
instance : add_comm_group (M β[R] N) :=
{ neg := has_neg.neg,
sub := _,
sub_eq_add_neg := Ξ» _ _, rfl,
add_left_neg := Ξ» x, tensor_product.induction_on x
(by { rw [add_zero], apply (neg.aux R).map_zero, })
(Ξ» x y, by { convert (add_tmul (-x) x y).symm, rw [add_left_neg, zero_tmul], })
(Ξ» x y hx hy, by {
unfold has_neg.neg sub_neg_monoid.neg,
rw add_monoid_hom.map_add,
ac_change (-x + x) + (-y + y) = 0,
rw [hx, hy, add_zero], }),
..(infer_instance : add_comm_monoid (M β[R] N)) }
lemma neg_tmul (m : M) (n : N) : (-m) ββ n = -(m ββ[R] n) := rfl
lemma tmul_neg (m : M) (n : N) : m ββ (-n) = -(m ββ[R] n) := (mk R M N _).map_neg _
lemma tmul_sub (m : M) (nβ nβ : N) : m ββ (nβ - nβ) = (m ββ[R] nβ) - (m ββ[R] nβ) :=
(mk R M N _).map_sub _ _
lemma sub_tmul (mβ mβ : M) (n : N) : (mβ - mβ) ββ n = (mβ ββ[R] n) - (mβ ββ[R] n) :=
(mk R M N).map_subβ _ _ _
/--
While the tensor product will automatically inherit a β€-module structure from
`add_comm_group.int_module`, that structure won't be compatible with lemmas like `tmul_smul` unless
we use a `β€-module` instance provided by `tensor_product.semimodule'`.
When `R` is a `ring` we get the required `tensor_product.compatible_smul` instance through
`is_scalar_tower`, but when it is only a `semiring` we need to build it from scratch.
The instance diamond in `compatible_smul` doesn't matter because it's in `Prop`.
-/
instance compatible_smul.int [semimodule β€ M] [semimodule β€ N] : compatible_smul R β€ M N :=
β¨Ξ» r m n, int.induction_on r
(by simp)
(Ξ» r ih, by simpa [add_smul, tmul_add, add_tmul] using ih)
(Ξ» r ih, by simpa [sub_smul, tmul_sub, sub_tmul] using ih)β©
end tensor_product
namespace linear_map
@[simp] lemma ltensor_sub (f g : N ββ[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M :=
by simp only [β coe_ltensor_hom, map_sub]
@[simp] lemma rtensor_sub (f g : N ββ[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M :=
by simp only [β coe_rtensor_hom, map_sub]
@[simp] lemma ltensor_neg (f : N ββ[R] P) : (-f).ltensor M = -(f.ltensor M) :=
by simp only [β coe_ltensor_hom, map_neg]
@[simp] lemma rtensor_neg (f : N ββ[R] P) : (-f).rtensor M = -(f.rtensor M) :=
by simp only [β coe_rtensor_hom, map_neg]
end linear_map
end ring
|
cab7e3344f740ac7dd158f93932a7e449a64ba15 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/unfoldDefEq.lean | f03ebafebe97fdc416be51d11c61c00621dad94c | [
"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 | 351 | lean | def Wrapper (n : Nat) : Type Γ Type :=
(Fin n, Fin n)
def some_property {n} (x : Fin n) : Prop :=
True
example (x : (Wrapper n).1) (h : some_property x) : True := by
unfold Wrapper at x
trace_state
simp at x
trace_state
sorry
example (x : (Wrapper n).1) (h : some_property x) : True := by
simp [Wrapper] at x
trace_state
sorry
|
4e0395759f3930c3eaf7d7ebe75fab99f586913a | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/coe8.lean | d58eb7db884236825132678a5e18afd9807f2909 | [
"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 | 357 | lean | import logic
namespace experiment
constant nat : Type.{1}
constant int : Type.{1}
constant of_nat : nat β int
coercion of_nat
constant nat_add : nat β nat β nat
constant int_add : int β int β int
infixl `+` := int_add
infixl `+` := nat_add
print "================"
constant tst (n m : nat) : @eq int (of_nat n + of_nat m) (n + m)
check tst
exit
|
f1adc3e55de304b55a5140443bfebc825399985e | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/compiler/float_cases_bug.lean | 1ac9b2673e84794e5e3146f18032cd4b0e3fd5b8 | [
"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 | 912 | lean |
inductive Term : Type
| const : Nat -> Term
| app : List Term -> Term
namespace Term
instance : Inhabited Term := β¨Term.const 0β©
partial def hasToString : Term -> String | const n => "CONST(" ++ toString n ++ ")" | app ts => "APP"
instance : ToString Term := β¨hasToStringβ©
end Term
open Term
structure MyState : Type := (ts : List Term)
def emit (t : Term) : StateM MyState Unit := modify (Ξ» ms => β¨t::ms.tsβ©)
partial def foo : MyState -> Term -> Term -> List Term
| msβ, t, u =>
let stateT : StateM MyState Unit := do {
match t with
| const _ => pure ()
| app _ => emit (const 1);
match t, u with
| app _, app _ => emit (app [])
| _, _ => pure () ;
match t, u with
| app _, app _ => emit (app [])
| _, _ => emit (const 2)
} ;
(stateT.run β¨[]β©).2.ts.reverse
def main : IO Unit := IO.println $ foo β¨[]β© (app []) (app [])
|
b39e9608c0df8304876979f3548c2c95d2cdb930 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/linear_algebra/adic_completion.lean | e4cb3721b2a5d8d2d653f622ea304da76f203713 | [] | 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 | 11,751 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.linear_algebra.smodeq
import Mathlib.ring_theory.ideal.operations
import Mathlib.PostPort
universes u_1 u_2 u_3
namespace Mathlib
/-!
# Completion of a module with respect to an ideal.
In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M`
with respect to an ideal `I`:
## Main definitions
- `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`.
- `is_precomplete I M`: this says that every Cauchy sequence converges.
- `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete.
- `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`.
- `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO)
with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is
precomplete.
-/
/-- A module `M` is Hausdorff with respect to an ideal `I` if `β I^n M = 0`. -/
def is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] :=
β (x : M), (β (n : β), smodeq (I ^ n β’ β€) x 0) β x = 0
/-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/
def is_precomplete {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] :=
β (f : β β M), (β {m n : β}, m β€ n β smodeq (I ^ m β’ β€) (f m) (f n)) β β (L : M), β (n : β), smodeq (I ^ n β’ β€) (f n) L
/-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/
def is_adic_complete {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] :=
is_Hausdorff I M β§ is_precomplete I M
/-- The Hausdorffification of a module with respect to an ideal. -/
def Hausdorffification {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] :=
submodule.quotient (infi fun (n : β) => I ^ n β’ β€)
/-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff.
In fact, this is only complete if the ideal is finitely generated. -/
def adic_completion {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : submodule R ((n : β) β submodule.quotient (I ^ n β’ β€)) :=
submodule.mk
(set_of
fun (f : (n : β) β submodule.quotient (I ^ n β’ β€)) =>
β {m n : β}, m β€ n β coe_fn (submodule.liftq (I ^ n β’ β€) (submodule.mkq (I ^ m β’ β€)) sorry) (f n) = f m)
sorry sorry sorry
namespace is_Hausdorff
protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff β₯ M :=
fun (x : M) (hx : β (n : β), smodeq (β₯ ^ n β’ β€) x 0) =>
eq.mpr (id (Eq.refl (x = 0)))
(eq.mp
(Eq.trans
((fun (U U_1 : submodule R M) (e_1 : U = U_1) (x x_1 : M) (e_2 : x = x_1) (y y_1 : M) (e_3 : y = y_1) =>
congr (congr (congr_arg smodeq e_1) e_2) e_3)
(β₯ ^ 1 β’ β€) β₯
(Eq.trans
((fun (αΎ° αΎ°_1 : ideal R) (e_2 : αΎ° = αΎ°_1) (αΎ°_2 αΎ°_3 : submodule R M) (e_3 : αΎ°_2 = αΎ°_3) =>
congr (congr_arg has_scalar.smul e_2) e_3)
(β₯ ^ 1) β₯ (pow_one β₯) β€ β€ (Eq.refl β€))
(submodule.bot_smul β€))
x x (Eq.refl x) 0 0 (Eq.refl 0))
(propext smodeq.bot))
(hx 1))
protected theorem subsingleton {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (h : is_Hausdorff β€ M) : subsingleton M := sorry
protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_Hausdorff I M :=
fun (x : M) (_x : β (n : β), smodeq (I ^ n β’ β€) x 0) => subsingleton.elim x 0
theorem infi_pow_smul {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] (h : is_Hausdorff I M) : (infi fun (n : β) => I ^ n β’ β€) = β₯ := sorry
end is_Hausdorff
namespace Hausdorffification
/-- The canonical linear map to the Hausdorffification. -/
def of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : linear_map R M (Hausdorffification I M) :=
submodule.mkq (infi fun (n : β) => I ^ n β’ β€)
theorem induction_on {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] {C : Hausdorffification I M β Prop} (x : Hausdorffification I M) (ih : β (x : M), C (coe_fn (of I M) x)) : C x :=
quotient.induction_on' x ih
protected instance is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff I (Hausdorffification I M) := sorry
/-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a
unique map from the Hausdorffification. -/
def lift {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) : linear_map R (Hausdorffification I M) N :=
submodule.liftq (infi fun (n : β) => I ^ n β’ β€) f sorry
theorem lift_of {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) (x : M) : coe_fn (lift I f) (coe_fn (of I M) x) = coe_fn f x :=
rfl
theorem lift_comp_of {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) : linear_map.comp (lift I f) (of I M) = f :=
linear_map.ext fun (_x : M) => rfl
/-- Uniqueness of lift. -/
theorem lift_eq {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) (g : linear_map R (Hausdorffification I M) N) (hg : linear_map.comp g (of I M) = f) : g = lift I f := sorry
end Hausdorffification
namespace is_precomplete
protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_precomplete β₯ M :=
fun (f : β β M) (hf : β {m n : β}, m β€ n β smodeq (β₯ ^ m β’ β€) (f m) (f n)) =>
Exists.intro (f 1)
fun (n : β) =>
nat.cases_on n
(eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (β₯ ^ 0 β’ β€) (f 0) (f 1))) (pow_zero β₯)))
(eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (1 β’ β€) (f 0) (f 1))) ideal.one_eq_top))
(eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (β€ β’ β€) (f 0) (f 1))) (submodule.top_smul β€))) smodeq.top)))
fun (n : β) =>
eq.mpr
(id
(Eq._oldrec (Eq.refl (smodeq (β₯ ^ Nat.succ n β’ β€) (f (Nat.succ n)) (f 1)))
(eq.mp (Eq._oldrec (Eq.refl (smodeq β₯ (f 1) (f (n + 1)))) (propext smodeq.bot))
(eq.mp (Eq._oldrec (Eq.refl (smodeq (β₯ β’ β€) (f 1) (f (n + 1)))) (submodule.bot_smul β€))
(eq.mp (Eq._oldrec (Eq.refl (smodeq (β₯ ^ 1 β’ β€) (f 1) (f (n + 1)))) (pow_one β₯))
(hf (nat.le_add_left 1 n)))))))
smodeq.refl
protected instance top {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_precomplete β€ M :=
fun (f : β β M) (hf : β {m n : β}, m β€ n β smodeq (β€ ^ m β’ β€) (f m) (f n)) =>
Exists.intro 0
fun (n : β) =>
eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (β€ ^ n β’ β€) (f n) 0)) (ideal.top_pow R n)))
(eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (β€ β’ β€) (f n) 0)) (submodule.top_smul β€))) smodeq.top)
protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_precomplete I M :=
fun (f : β β M) (hf : β {m n : β}, m β€ n β smodeq (I ^ m β’ β€) (f m) (f n)) =>
Exists.intro 0
fun (n : β) =>
eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (I ^ n β’ β€) (f n) 0)) (subsingleton.elim (f n) 0))) smodeq.refl
end is_precomplete
namespace adic_completion
/-- The canonical linear map to the completion. -/
def of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : linear_map R M β₯(adic_completion I M) :=
linear_map.mk (fun (x : M) => { val := fun (n : β) => coe_fn (submodule.mkq (I ^ n β’ β€)) x, property := sorry }) sorry
sorry
@[simp] theorem of_apply {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (x : M) (n : β) : subtype.val (coe_fn (of I M) x) n = coe_fn (submodule.mkq (I ^ n β’ β€)) x :=
rfl
/-- Linearly evaluating a sequence in the completion at a given input. -/
def eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) : linear_map R (β₯(adic_completion I M)) (submodule.quotient (I ^ n β’ β€)) :=
linear_map.mk (fun (f : β₯(adic_completion I M)) => subtype.val f n) sorry sorry
@[simp] theorem coe_eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) : β(eval I M n) = fun (f : β₯(adic_completion I M)) => subtype.val f n :=
rfl
theorem eval_apply {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) (f : β₯(adic_completion I M)) : coe_fn (eval I M n) f = subtype.val f n :=
rfl
theorem eval_of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) (x : M) : coe_fn (eval I M n) (coe_fn (of I M) x) = coe_fn (submodule.mkq (I ^ n β’ β€)) x :=
rfl
@[simp] theorem eval_comp_of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) : linear_map.comp (eval I M n) (of I M) = submodule.mkq (I ^ n β’ β€) :=
rfl
@[simp] theorem range_eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β) : linear_map.range (eval I M n) = β€ :=
iff.mpr linear_map.range_eq_top
fun (x : submodule.quotient (I ^ n β’ β€)) =>
quotient.induction_on' x fun (x : M) => Exists.intro (coe_fn (of I M) x) rfl
theorem ext {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] {x : β₯(adic_completion I M)} {y : β₯(adic_completion I M)} (h : β (n : β), coe_fn (eval I M n) x = coe_fn (eval I M n) y) : x = y :=
subtype.eq (funext h)
protected instance is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff I β₯(adic_completion I M) := sorry
end adic_completion
namespace is_adic_complete
protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_adic_complete β₯ M :=
{ left := is_Hausdorff.bot M, right := is_precomplete.bot M }
protected theorem subsingleton {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] (h : is_adic_complete β€ M) : subsingleton M :=
is_Hausdorff.subsingleton (and.left h)
protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_adic_complete I M :=
{ left := is_Hausdorff.of_subsingleton I M, right := is_precomplete.of_subsingleton I M }
|
f6bf4b00dcbdab31df3aee4fbd731920f5d5a5fe | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/data/finset/basic.lean | 0b70c1aee9cbdfbdaa345c4f72f491910151e86a | [
"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 | 104,999 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro
-/
import data.multiset.finset_ops
import tactic.monotonicity
import tactic.apply
import tactic.nth_rewrite
/-!
# Finite sets
mathlib has several different models for finite sets,
and it can be confusing when you're first getting used to them!
This file builds the basic theory of `finset Ξ±`,
modelled as a `multiset Ξ±` without duplicates.
It's "constructive" in the since that there is an underlying list of elements,
although this is wrapped in a quotient by permutations,
so anytime you actually use this list you're obligated to show you didn't depend on the ordering.
There's also the typeclass `fintype Ξ±`
(which asserts that there is some `finset Ξ±` containing every term of type `Ξ±`)
as well as the predicate `finite` on `s : set Ξ±` (which asserts `nonempty (fintype s)`).
-/
open multiset subtype nat function
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*}
/-- `finset Ξ±` is the type of finite sets of elements of `Ξ±`. It is implemented
as a multiset (a list up to permutation) which has no duplicate elements. -/
structure finset (Ξ± : Type*) :=
(val : multiset Ξ±)
(nodup : nodup val)
namespace finset
theorem eq_of_veq : β {s t : finset Ξ±}, s.1 = t.1 β s = t
| β¨s, _β© β¨t, _β© rfl := rfl
@[simp] theorem val_inj {s t : finset Ξ±} : s.1 = t.1 β s = t :=
β¨eq_of_veq, congr_arg _β©
@[simp] theorem erase_dup_eq_self [decidable_eq Ξ±] (s : finset Ξ±) : erase_dup s.1 = s.1 :=
erase_dup_eq_self.2 s.2
instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (finset Ξ±)
| sβ sβ := decidable_of_iff _ val_inj
/-! ### membership -/
instance : has_mem Ξ± (finset Ξ±) := β¨Ξ» a s, a β s.1β©
theorem mem_def {a : Ξ±} {s : finset Ξ±} : a β s β a β s.1 := iff.rfl
@[simp] theorem mem_mk {a : Ξ±} {s nd} : a β @finset.mk Ξ± s nd β a β s := iff.rfl
instance decidable_mem [h : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : decidable (a β s) :=
multiset.decidable_mem _ _
/-! ### set coercion -/
/-- Convert a finset to a set in the natural way. -/
instance : has_coe_t (finset Ξ±) (set Ξ±) := β¨Ξ» s, {x | x β s}β©
@[simp, norm_cast] lemma mem_coe {a : Ξ±} {s : finset Ξ±} : a β (s : set Ξ±) β a β s := iff.rfl
@[simp] lemma set_of_mem {Ξ±} {s : finset Ξ±} : {a | a β s} = s := rfl
@[simp] lemma coe_mem {s : finset Ξ±} (x : (s : set Ξ±)) : βx β s := x.2
@[simp] lemma mk_coe {s : finset Ξ±} (x : (s : set Ξ±)) {h} :
(β¨x, hβ© : (s : set Ξ±)) = x :=
subtype.coe_eta _ _
instance decidable_mem' [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) :
decidable (a β (s : set Ξ±)) := s.decidable_mem _
/-! ### extensionality -/
theorem ext_iff {sβ sβ : finset Ξ±} : sβ = sβ β β a, a β sβ β a β sβ :=
val_inj.symm.trans $ nodup_ext sβ.2 sβ.2
@[ext]
theorem ext {sβ sβ : finset Ξ±} : (β a, a β sβ β a β sβ) β sβ = sβ :=
ext_iff.2
@[simp, norm_cast] theorem coe_inj {sβ sβ : finset Ξ±} : (sβ : set Ξ±) = sβ β sβ = sβ :=
set.ext_iff.trans ext_iff.symm
lemma coe_injective {Ξ±} : injective (coe : finset Ξ± β set Ξ±) :=
Ξ» s t, coe_inj.1
/-! ### subset -/
instance : has_subset (finset Ξ±) := β¨Ξ» sβ sβ, β β¦aβ¦, a β sβ β a β sββ©
theorem subset_def {sβ sβ : finset Ξ±} : sβ β sβ β sβ.1 β sβ.1 := iff.rfl
@[simp] theorem subset.refl (s : finset Ξ±) : s β s := subset.refl _
theorem subset_of_eq {s t : finset Ξ±} (h : s = t) : s β t := h βΈ subset.refl _
theorem subset.trans {sβ sβ sβ : finset Ξ±} : sβ β sβ β sβ β sβ β sβ β sβ := subset.trans
theorem superset.trans {sβ sβ sβ : finset Ξ±} : sβ β sβ β sβ β sβ β sβ β sβ :=
Ξ» h' h, subset.trans h h'
-- TODO: these should be global attributes, but this will require fixing other files
local attribute [trans] subset.trans superset.trans
theorem mem_of_subset {sβ sβ : finset Ξ±} {a : Ξ±} : sβ β sβ β a β sβ β a β sβ := mem_of_subset
theorem subset.antisymm {sβ sβ : finset Ξ±} (Hβ : sβ β sβ) (Hβ : sβ β sβ) : sβ = sβ :=
ext $ Ξ» a, β¨@Hβ a, @Hβ aβ©
theorem subset_iff {sβ sβ : finset Ξ±} : sβ β sβ β β β¦xβ¦, x β sβ β x β sβ := iff.rfl
@[simp, norm_cast] theorem coe_subset {sβ sβ : finset Ξ±} :
(sβ : set Ξ±) β sβ β sβ β sβ := iff.rfl
@[simp] theorem val_le_iff {sβ sβ : finset Ξ±} : sβ.1 β€ sβ.1 β sβ β sβ := le_iff_subset sβ.2
instance : has_ssubset (finset Ξ±) := β¨Ξ»a b, a β b β§ Β¬ b β aβ©
instance : partial_order (finset Ξ±) :=
{ le := (β),
lt := (β),
le_refl := subset.refl,
le_trans := @subset.trans _,
le_antisymm := @subset.antisymm _ }
theorem subset.antisymm_iff {sβ sβ : finset Ξ±} : sβ = sβ β sβ β sβ β§ sβ β sβ :=
le_antisymm_iff
@[simp] theorem le_iff_subset {sβ sβ : finset Ξ±} : sβ β€ sβ β sβ β sβ := iff.rfl
@[simp] theorem lt_iff_ssubset {sβ sβ : finset Ξ±} : sβ < sβ β sβ β sβ := iff.rfl
@[simp, norm_cast] lemma coe_ssubset {sβ sβ : finset Ξ±} : (sβ : set Ξ±) β sβ β sβ β sβ :=
show (sβ : set Ξ±) β sβ β sβ β sβ β§ Β¬sβ β sβ,
by simp only [set.ssubset_def, finset.coe_subset]
@[simp] theorem val_lt_iff {sβ sβ : finset Ξ±} : sβ.1 < sβ.1 β sβ β sβ :=
and_congr val_le_iff $ not_congr val_le_iff
theorem ssubset_iff_of_subset {sβ sβ : finset Ξ±} (h : sβ β sβ) : sβ β sβ β β x β sβ, x β sβ :=
set.ssubset_iff_of_subset h
/-! ### Nonempty -/
/-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used
in theorem assumptions instead of `β x, x β s` or `s β β
` as it gives access to a nice API thanks
to the dot notation. -/
protected def nonempty (s : finset Ξ±) : Prop := β x:Ξ±, x β s
@[simp, norm_cast] lemma coe_nonempty {s : finset Ξ±} : (s:set Ξ±).nonempty β s.nonempty := iff.rfl
lemma nonempty.bex {s : finset Ξ±} (h : s.nonempty) : β x:Ξ±, x β s := h
lemma nonempty.mono {s t : finset Ξ±} (hst : s β t) (hs : s.nonempty) : t.nonempty :=
set.nonempty.mono hst hs
lemma nonempty.forall_const {s : finset Ξ±} (h : s.nonempty) {p : Prop} : (β x β s, p) β p :=
let β¨x, hxβ© := h in β¨Ξ» h, h x hx, Ξ» h x hx, hβ©
/-! ### empty -/
/-- The empty finset -/
protected def empty : finset Ξ± := β¨0, nodup_zeroβ©
instance : has_emptyc (finset Ξ±) := β¨finset.emptyβ©
instance : inhabited (finset Ξ±) := β¨β
β©
@[simp] theorem empty_val : (β
: finset Ξ±).1 = 0 := rfl
@[simp] theorem not_mem_empty (a : Ξ±) : a β (β
: finset Ξ±) := id
@[simp] theorem not_nonempty_empty : Β¬(β
: finset Ξ±).nonempty :=
Ξ» β¨x, hxβ©, not_mem_empty x hx
@[simp] theorem mk_zero : (β¨0, nodup_zeroβ© : finset Ξ±) = β
:= rfl
theorem ne_empty_of_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : s β β
:=
Ξ» e, not_mem_empty a $ e βΈ h
theorem nonempty.ne_empty {s : finset Ξ±} (h : s.nonempty) : s β β
:=
exists.elim h $ Ξ» a, ne_empty_of_mem
@[simp] theorem empty_subset (s : finset Ξ±) : β
β s := zero_subset _
theorem eq_empty_of_forall_not_mem {s : finset Ξ±} (H : βx, x β s) : s = β
:=
eq_of_veq (eq_zero_of_forall_not_mem H)
lemma eq_empty_iff_forall_not_mem {s : finset Ξ±} : s = β
β β x, x β s :=
β¨by rintro rfl x; exact id, Ξ» h, eq_empty_of_forall_not_mem hβ©
@[simp] theorem val_eq_zero {s : finset Ξ±} : s.1 = 0 β s = β
:= @val_inj _ s β
theorem subset_empty {s : finset Ξ±} : s β β
β s = β
:= subset_zero.trans val_eq_zero
theorem nonempty_of_ne_empty {s : finset Ξ±} (h : s β β
) : s.nonempty :=
exists_mem_of_ne_zero (mt val_eq_zero.1 h)
theorem nonempty_iff_ne_empty {s : finset Ξ±} : s.nonempty β s β β
:=
β¨nonempty.ne_empty, nonempty_of_ne_emptyβ©
@[simp] theorem not_nonempty_iff_eq_empty {s : finset Ξ±} : Β¬s.nonempty β s = β
:=
by { rw nonempty_iff_ne_empty, exact not_not, }
theorem eq_empty_or_nonempty (s : finset Ξ±) : s = β
β¨ s.nonempty :=
classical.by_cases or.inl (Ξ» h, or.inr (nonempty_of_ne_empty h))
@[simp] lemma coe_empty : ((β
: finset Ξ±) : set Ξ±) = β
:= rfl
/-- A `finset` for an empty type is empty. -/
lemma eq_empty_of_not_nonempty (h : Β¬ nonempty Ξ±) (s : finset Ξ±) : s = β
:=
finset.eq_empty_of_forall_not_mem $ Ξ» x, false.elim $ not_nonempty_iff_imp_false.1 h x
/-! ### singleton -/
/--
`{a} : finset a` is the set `{a}` containing `a` and nothing else.
This differs from `insert a β
` in that it does not require a `decidable_eq` instance for `Ξ±`.
-/
instance : has_singleton Ξ± (finset Ξ±) := β¨Ξ» a, β¨{a}, nodup_singleton aβ©β©
@[simp] theorem singleton_val (a : Ξ±) : ({a} : finset Ξ±).1 = a ::β 0 := rfl
@[simp] theorem mem_singleton {a b : Ξ±} : b β ({a} : finset Ξ±) β b = a := mem_singleton
theorem not_mem_singleton {a b : Ξ±} : a β ({b} : finset Ξ±) β a β b := not_congr mem_singleton
theorem mem_singleton_self (a : Ξ±) : a β ({a} : finset Ξ±) := or.inl rfl
theorem singleton_inj {a b : Ξ±} : ({a} : finset Ξ±) = {b} β a = b :=
β¨Ξ» h, mem_singleton.1 (h βΈ mem_singleton_self _), congr_arg _β©
@[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : finset Ξ±).nonempty := β¨a, mem_singleton_self aβ©
@[simp] theorem singleton_ne_empty (a : Ξ±) : ({a} : finset Ξ±) β β
:= (singleton_nonempty a).ne_empty
@[simp, norm_cast] lemma coe_singleton (a : Ξ±) : (({a} : finset Ξ±) : set Ξ±) = {a} :=
by { ext, simp }
lemma eq_singleton_iff_unique_mem {s : finset Ξ±} {a : Ξ±} :
s = {a} β a β s β§ β x β s, x = a :=
begin
split; intro t,
rw t,
refine β¨finset.mem_singleton_self _, Ξ» _, finset.mem_singleton.1β©,
ext, rw finset.mem_singleton,
refine β¨t.right _, Ξ» r, r.symm βΈ t.leftβ©
end
lemma eq_singleton_iff_nonempty_unique_mem {s : finset Ξ±} {a : Ξ±} :
s = {a} β s.nonempty β§ β x β s, x = a :=
begin
split,
{ intros h, subst h, simp, },
{ rintros β¨hne, h_uniqβ©, rw eq_singleton_iff_unique_mem, refine β¨_, h_uniqβ©,
rw β h_uniq hne.some hne.some_spec, apply hne.some_spec, },
end
lemma singleton_iff_unique_mem (s : finset Ξ±) : (β a, s = {a}) β β! a, a β s :=
by simp only [eq_singleton_iff_unique_mem, exists_unique]
lemma singleton_subset_set_iff {s : set Ξ±} {a : Ξ±} :
β({a} : finset Ξ±) β s β a β s :=
by rw [coe_singleton, set.singleton_subset_iff]
@[simp] lemma singleton_subset_iff {s : finset Ξ±} {a : Ξ±} :
{a} β s β a β s :=
singleton_subset_set_iff
/-! ### cons -/
/-- `cons a s h` is the set `{a} βͺ s` containing `a` and the elements of `s`. It is the same as
`insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq Ξ±`,
and the union is guaranteed to be disjoint. -/
def cons {Ξ±} (a : Ξ±) (s : finset Ξ±) (h : a β s) : finset Ξ± :=
β¨a ::β s.1, multiset.nodup_cons.2 β¨h, s.2β©β©
@[simp] theorem mem_cons {Ξ± a s h b} : b β @cons Ξ± a s h β b = a β¨ b β s :=
by rcases s with β¨β¨sβ©β©; apply list.mem_cons_iff
@[simp] theorem cons_val {a : Ξ±} {s : finset Ξ±} (h : a β s) : (cons a s h).1 = a ::β s.1 := rfl
@[simp] theorem mk_cons {a : Ξ±} {s : multiset Ξ±} (h : (a ::β s).nodup) :
(β¨a ::β s, hβ© : finset Ξ±) = cons a β¨s, (multiset.nodup_cons.1 h).2β© (multiset.nodup_cons.1 h).1 :=
rfl
@[simp] theorem nonempty_cons {a : Ξ±} {s : finset Ξ±} (h : a β s) : (cons a s h).nonempty :=
β¨a, mem_cons.2 (or.inl rfl)β©
@[simp] lemma nonempty_mk_coe : β {l : list Ξ±} {hl}, (β¨βl, hlβ© : finset Ξ±).nonempty β l β []
| [] hl := by simp
| (a::l) hl := by simp [β multiset.cons_coe]
/-! ### disjoint union -/
/-- `disj_union s t h` is the set such that `a β disj_union s t h` iff `a β s` or `a β t`.
It is the same as `s βͺ t`, but it does not require decidable equality on the type. The hypothesis
ensures that the sets are disjoint. -/
def disj_union {Ξ±} (s t : finset Ξ±) (h : β a β s, a β t) : finset Ξ± :=
β¨s.1 + t.1, multiset.nodup_add.2 β¨s.2, t.2, hβ©β©
@[simp] theorem mem_disj_union {Ξ± s t h a} :
a β @disj_union Ξ± s t h β a β s β¨ a β t :=
by rcases s with β¨β¨sβ©β©; rcases t with β¨β¨tβ©β©; apply list.mem_append
/-! ### insert -/
section decidable_eq
variables [decidable_eq Ξ±]
/-- `insert a s` is the set `{a} βͺ s` containing `a` and the elements of `s`. -/
instance : has_insert Ξ± (finset Ξ±) := β¨Ξ» a s, β¨_, nodup_ndinsert a s.2β©β©
theorem insert_def (a : Ξ±) (s : finset Ξ±) : insert a s = β¨_, nodup_ndinsert a s.2β© := rfl
@[simp] theorem insert_val (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = ndinsert a s.1 := rfl
theorem insert_val' (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = erase_dup (a ::β s.1) :=
by rw [erase_dup_cons, erase_dup_eq_self]; refl
theorem insert_val_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : (insert a s).1 = a ::β s.1 :=
by rw [insert_val, ndinsert_of_not_mem h]
@[simp] theorem mem_insert {a b : Ξ±} {s : finset Ξ±} : a β insert b s β a = b β¨ a β s := mem_ndinsert
theorem mem_insert_self (a : Ξ±) (s : finset Ξ±) : a β insert a s := mem_ndinsert_self a s.1
theorem mem_insert_of_mem {a b : Ξ±} {s : finset Ξ±} (h : a β s) : a β insert b s :=
mem_ndinsert_of_mem h
theorem mem_of_mem_insert_of_ne {a b : Ξ±} {s : finset Ξ±} (h : b β insert a s) : b β a β b β s :=
(mem_insert.1 h).resolve_left
@[simp] theorem cons_eq_insert {Ξ±} [decidable_eq Ξ±] (a s h) : @cons Ξ± a s h = insert a s :=
ext $ Ξ» a, by simp
@[simp, norm_cast] lemma coe_insert (a : Ξ±) (s : finset Ξ±) :
β(insert a s) = (insert a s : set Ξ±) :=
set.ext $ Ξ» x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
lemma mem_insert_coe {s : finset Ξ±} {x y : Ξ±} : x β insert y s β x β insert y (s : set Ξ±) :=
by simp
instance : is_lawful_singleton Ξ± (finset Ξ±) := β¨Ξ» a, by { ext, simp }β©
@[simp] theorem insert_eq_of_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : insert a s = s :=
eq_of_veq $ ndinsert_of_mem h
@[simp] theorem insert_singleton_self_eq (a : Ξ±) : ({a, a} : finset Ξ±) = {a} :=
insert_eq_of_mem $ mem_singleton_self _
theorem insert.comm (a b : Ξ±) (s : finset Ξ±) : insert a (insert b s) = insert b (insert a s) :=
ext $ Ξ» x, by simp only [mem_insert, or.left_comm]
theorem insert_singleton_comm (a b : Ξ±) : ({a, b} : finset Ξ±) = {b, a} :=
begin
ext,
simp [or.comm]
end
@[simp] theorem insert_idem (a : Ξ±) (s : finset Ξ±) : insert a (insert a s) = insert a s :=
ext $ Ξ» x, by simp only [mem_insert, or.assoc.symm, or_self]
@[simp] theorem insert_nonempty (a : Ξ±) (s : finset Ξ±) : (insert a s).nonempty :=
β¨a, mem_insert_self a sβ©
@[simp] theorem insert_ne_empty (a : Ξ±) (s : finset Ξ±) : insert a s β β
:=
(insert_nonempty a s).ne_empty
section
universe u
/-!
The universe annotation is required for the following instance, possibly this is a bug in Lean. See
leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F)
-/
instance {Ξ± : Type u} [decidable_eq Ξ±] (i : Ξ±) (s : finset Ξ±) :
nonempty.{u + 1} ((insert i s : finset Ξ±) : set Ξ±) :=
(finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype
end
lemma ne_insert_of_not_mem (s t : finset Ξ±) {a : Ξ±} (h : a β s) :
s β insert a t :=
by { contrapose! h, simp [h] }
theorem insert_subset {a : Ξ±} {s t : finset Ξ±} : insert a s β t β a β t β§ s β t :=
by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib]
theorem subset_insert (a : Ξ±) (s : finset Ξ±) : s β insert a s :=
Ξ» b, mem_insert_of_mem
theorem insert_subset_insert (a : Ξ±) {s t : finset Ξ±} (h : s β t) : insert a s β insert a t :=
insert_subset.2 β¨mem_insert_self _ _, subset.trans h (subset_insert _ _)β©
lemma ssubset_iff {s t : finset Ξ±} : s β t β (βa β s, insert a s β t) :=
by exact_mod_cast @set.ssubset_iff_insert Ξ± s t
lemma ssubset_insert {s : finset Ξ±} {a : Ξ±} (h : a β s) : s β insert a s :=
ssubset_iff.mpr β¨a, h, subset.refl _β©
@[elab_as_eliminator]
protected theorem induction {Ξ± : Type*} {p : finset Ξ± β Prop} [decidable_eq Ξ±]
(hβ : p β
) (hβ : β β¦a : Ξ±β¦ {s : finset Ξ±}, a β s β p s β p (insert a s)) : β s, p s
| β¨s, ndβ© := multiset.induction_on s (Ξ» _, hβ) (Ξ» a s IH nd, begin
cases nodup_cons.1 nd with m nd',
rw [β (eq_of_veq _ : insert a (finset.mk s _) = β¨a ::β s, ndβ©)],
{ exact hβ (by exact m) (IH nd') },
{ rw [insert_val, ndinsert_of_not_mem m] }
end) nd
/--
To prove a proposition about an arbitrary `finset Ξ±`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset Ξ±`,
then it holds for the `finset` obtained by inserting a new element.
-/
@[elab_as_eliminator]
protected theorem induction_on {Ξ± : Type*} {p : finset Ξ± β Prop} [decidable_eq Ξ±]
(s : finset Ξ±) (hβ : p β
) (hβ : β β¦a : Ξ±β¦ {s : finset Ξ±}, a β s β p s β p (insert a s)) : p s :=
finset.induction hβ hβ s
/--
To prove a proposition about `S : finset Ξ±`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset Ξ± β S`,
then it holds for the `finset` obtained by inserting a new element of `S`.
-/
@[elab_as_eliminator]
theorem induction_on' {Ξ± : Type*} {p : finset Ξ± β Prop} [decidable_eq Ξ±]
(S : finset Ξ±) (hβ : p β
) (hβ : β {a s}, a β S β s β S β a β s β p s β p (insert a s)) : p S :=
@finset.induction_on Ξ± (Ξ» T, T β S β p T) _ S (Ξ» _, hβ) (Ξ» a s has hqs hs,
let β¨hS, sSβ© := finset.insert_subset.1 hs in hβ hS sS has (hqs sS)) (finset.subset.refl S)
/-- Inserting an element to a finite set is equivalent to the option type. -/
def subtype_insert_equiv_option {t : finset Ξ±} {x : Ξ±} (h : x β t) :
{i // i β insert x t} β option {i // i β t} :=
begin
refine
{ to_fun := Ξ» y, if h : βy = x then none else some β¨y, (mem_insert.mp y.2).resolve_left hβ©,
inv_fun := Ξ» y, y.elim β¨x, mem_insert_self _ _β© $ Ξ» z, β¨z, mem_insert_of_mem z.2β©,
.. },
{ intro y, by_cases h : βy = x,
simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk],
simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] },
{ rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk],
have : βy β x, { rintro β¨β©, exact h y.2 },
simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta,
subtype.coe_mk] },
end
/-! ### union -/
/-- `s βͺ t` is the set such that `a β s βͺ t` iff `a β s` or `a β t`. -/
instance : has_union (finset Ξ±) := β¨Ξ» sβ sβ, β¨_, nodup_ndunion sβ.1 sβ.2β©β©
theorem union_val_nd (sβ sβ : finset Ξ±) : (sβ βͺ sβ).1 = ndunion sβ.1 sβ.1 := rfl
@[simp] theorem union_val (sβ sβ : finset Ξ±) : (sβ βͺ sβ).1 = sβ.1 βͺ sβ.1 :=
ndunion_eq_union sβ.2
@[simp] theorem mem_union {a : Ξ±} {sβ sβ : finset Ξ±} : a β sβ βͺ sβ β a β sβ β¨ a β sβ := mem_ndunion
@[simp] theorem disj_union_eq_union {Ξ±} [decidable_eq Ξ±] (s t h) : @disj_union Ξ± s t h = s βͺ t :=
ext $ Ξ» a, by simp
theorem mem_union_left {a : Ξ±} {sβ : finset Ξ±} (sβ : finset Ξ±) (h : a β sβ) : a β sβ βͺ sβ :=
mem_union.2 $ or.inl h
theorem mem_union_right {a : Ξ±} {sβ : finset Ξ±} (sβ : finset Ξ±) (h : a β sβ) : a β sβ βͺ sβ :=
mem_union.2 $ or.inr h
theorem forall_mem_union {sβ sβ : finset Ξ±} {p : Ξ± β Prop} :
(β ab β (sβ βͺ sβ), p ab) β (β a β sβ, p a) β§ (β b β sβ, p b) :=
β¨Ξ» h, β¨Ξ» a, h a β mem_union_left _, Ξ» b, h b β mem_union_right _β©,
Ξ» h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)β©
theorem not_mem_union {a : Ξ±} {sβ sβ : finset Ξ±} : a β sβ βͺ sβ β a β sβ β§ a β sβ :=
by rw [mem_union, not_or_distrib]
@[simp, norm_cast]
lemma coe_union (sβ sβ : finset Ξ±) : β(sβ βͺ sβ) = (sβ βͺ sβ : set Ξ±) := set.ext $ Ξ» x, mem_union
theorem union_subset {sβ sβ sβ : finset Ξ±} (hβ : sβ β sβ) (hβ : sβ β sβ) : sβ βͺ sβ β sβ :=
val_le_iff.1 (ndunion_le.2 β¨hβ, val_le_iff.2 hββ©)
theorem subset_union_left (sβ sβ : finset Ξ±) : sβ β sβ βͺ sβ := Ξ» x, mem_union_left _
theorem subset_union_right (sβ sβ : finset Ξ±) : sβ β sβ βͺ sβ := Ξ» x, mem_union_right _
lemma union_subset_union {s1 t1 s2 t2 : finset Ξ±} (h1 : s1 β t1) (h2 : s2 β t2) :
s1 βͺ s2 β t1 βͺ t2 :=
by { intros x hx, rw finset.mem_union at hx β’, tauto }
theorem union_comm (sβ sβ : finset Ξ±) : sβ βͺ sβ = sβ βͺ sβ :=
ext $ Ξ» x, by simp only [mem_union, or_comm]
instance : is_commutative (finset Ξ±) (βͺ) := β¨union_commβ©
@[simp] theorem union_assoc (sβ sβ sβ : finset Ξ±) : (sβ βͺ sβ) βͺ sβ = sβ βͺ (sβ βͺ sβ) :=
ext $ Ξ» x, by simp only [mem_union, or_assoc]
instance : is_associative (finset Ξ±) (βͺ) := β¨union_assocβ©
@[simp] theorem union_idempotent (s : finset Ξ±) : s βͺ s = s :=
ext $ Ξ» _, mem_union.trans $ or_self _
instance : is_idempotent (finset Ξ±) (βͺ) := β¨union_idempotentβ©
theorem union_left_comm (sβ sβ sβ : finset Ξ±) : sβ βͺ (sβ βͺ sβ) = sβ βͺ (sβ βͺ sβ) :=
ext $ Ξ» _, by simp only [mem_union, or.left_comm]
theorem union_right_comm (sβ sβ sβ : finset Ξ±) : (sβ βͺ sβ) βͺ sβ = (sβ βͺ sβ) βͺ sβ :=
ext $ Ξ» x, by simp only [mem_union, or_assoc, or_comm (x β sβ)]
theorem union_self (s : finset Ξ±) : s βͺ s = s := union_idempotent s
@[simp] theorem union_empty (s : finset Ξ±) : s βͺ β
= s :=
ext $ Ξ» x, mem_union.trans $ or_false _
@[simp] theorem empty_union (s : finset Ξ±) : β
βͺ s = s :=
ext $ Ξ» x, mem_union.trans $ false_or _
theorem insert_eq (a : Ξ±) (s : finset Ξ±) : insert a s = {a} βͺ s := rfl
@[simp] theorem insert_union (a : Ξ±) (s t : finset Ξ±) : insert a s βͺ t = insert a (s βͺ t) :=
by simp only [insert_eq, union_assoc]
@[simp] theorem union_insert (a : Ξ±) (s t : finset Ξ±) : s βͺ insert a t = insert a (s βͺ t) :=
by simp only [insert_eq, union_left_comm]
theorem insert_union_distrib (a : Ξ±) (s t : finset Ξ±) :
insert a (s βͺ t) = insert a s βͺ insert a t :=
by simp only [insert_union, union_insert, insert_idem]
@[simp] lemma union_eq_left_iff_subset {s t : finset Ξ±} :
s βͺ t = s β t β s :=
begin
split,
{ assume h,
have : t β s βͺ t := subset_union_right _ _,
rwa h at this },
{ assume h,
exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) }
end
@[simp] lemma left_eq_union_iff_subset {s t : finset Ξ±} :
s = s βͺ t β t β s :=
by rw [β union_eq_left_iff_subset, eq_comm]
@[simp] lemma union_eq_right_iff_subset {s t : finset Ξ±} :
t βͺ s = s β t β s :=
by rw [union_comm, union_eq_left_iff_subset]
@[simp] lemma right_eq_union_iff_subset {s t : finset Ξ±} :
s = t βͺ s β t β s :=
by rw [β union_eq_right_iff_subset, eq_comm]
/--
To prove a relation on pairs of `finset X`, it suffices to show that it is
* symmetric,
* it holds when one of the `finset`s is empty,
* it holds for pairs of singletons,
* if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a βͺ b, c]`.
-/
lemma induction_on_union (P : finset Ξ± β finset Ξ± β Prop)
(symm : β {a b}, P a b β P b a)
(empty_right : β {a}, P a β
)
(singletons : β {a b}, P {a} {b})
(union_of : β {a b c}, P a c β P b c β P (a βͺ b) c) :
β a b, P a b :=
begin
intros a b,
refine finset.induction_on b empty_right (Ξ» x s xs hi, symm _),
rw finset.insert_eq,
apply union_of _ (symm hi),
refine finset.induction_on a empty_right (Ξ» a t ta hi, symm _),
rw finset.insert_eq,
exact union_of singletons (symm hi),
end
/-! ### inter -/
/-- `s β© t` is the set such that `a β s β© t` iff `a β s` and `a β t`. -/
instance : has_inter (finset Ξ±) := β¨Ξ» sβ sβ, β¨_, nodup_ndinter sβ.1 sβ.2β©β©
theorem inter_val_nd (sβ sβ : finset Ξ±) : (sβ β© sβ).1 = ndinter sβ.1 sβ.1 := rfl
@[simp] theorem inter_val (sβ sβ : finset Ξ±) : (sβ β© sβ).1 = sβ.1 β© sβ.1 :=
ndinter_eq_inter sβ.2
@[simp] theorem mem_inter {a : Ξ±} {sβ sβ : finset Ξ±} : a β sβ β© sβ β a β sβ β§ a β sβ := mem_ndinter
theorem mem_of_mem_inter_left {a : Ξ±} {sβ sβ : finset Ξ±} (h : a β sβ β© sβ) :
a β sβ := (mem_inter.1 h).1
theorem mem_of_mem_inter_right {a : Ξ±} {sβ sβ : finset Ξ±} (h : a β sβ β© sβ) :
a β sβ := (mem_inter.1 h).2
theorem mem_inter_of_mem {a : Ξ±} {sβ sβ : finset Ξ±} : a β sβ β a β sβ β a β sβ β© sβ :=
and_imp.1 mem_inter.2
theorem inter_subset_left (sβ sβ : finset Ξ±) : sβ β© sβ β sβ := Ξ» a, mem_of_mem_inter_left
theorem inter_subset_right (sβ sβ : finset Ξ±) : sβ β© sβ β sβ := Ξ» a, mem_of_mem_inter_right
theorem subset_inter {sβ sβ sβ : finset Ξ±} : sβ β sβ β sβ β sβ β sβ β sβ β© sβ :=
by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial
@[simp, norm_cast]
lemma coe_inter (sβ sβ : finset Ξ±) : β(sβ β© sβ) = (sβ β© sβ : set Ξ±) := set.ext $ Ξ» _, mem_inter
@[simp] theorem union_inter_cancel_left {s t : finset Ξ±} : (s βͺ t) β© s = s :=
by rw [β coe_inj, coe_inter, coe_union, set.union_inter_cancel_left]
@[simp] theorem union_inter_cancel_right {s t : finset Ξ±} : (s βͺ t) β© t = t :=
by rw [β coe_inj, coe_inter, coe_union, set.union_inter_cancel_right]
theorem inter_comm (sβ sβ : finset Ξ±) : sβ β© sβ = sβ β© sβ :=
ext $ Ξ» _, by simp only [mem_inter, and_comm]
@[simp] theorem inter_assoc (sβ sβ sβ : finset Ξ±) : (sβ β© sβ) β© sβ = sβ β© (sβ β© sβ) :=
ext $ Ξ» _, by simp only [mem_inter, and_assoc]
theorem inter_left_comm (sβ sβ sβ : finset Ξ±) : sβ β© (sβ β© sβ) = sβ β© (sβ β© sβ) :=
ext $ Ξ» _, by simp only [mem_inter, and.left_comm]
theorem inter_right_comm (sβ sβ sβ : finset Ξ±) : (sβ β© sβ) β© sβ = (sβ β© sβ) β© sβ :=
ext $ Ξ» _, by simp only [mem_inter, and.right_comm]
@[simp] theorem inter_self (s : finset Ξ±) : s β© s = s :=
ext $ Ξ» _, mem_inter.trans $ and_self _
@[simp] theorem inter_empty (s : finset Ξ±) : s β© β
= β
:=
ext $ Ξ» _, mem_inter.trans $ and_false _
@[simp] theorem empty_inter (s : finset Ξ±) : β
β© s = β
:=
ext $ Ξ» _, mem_inter.trans $ false_and _
@[simp] lemma inter_union_self (s t : finset Ξ±) : s β© (t βͺ s) = s :=
by rw [inter_comm, union_inter_cancel_right]
@[simp] theorem insert_inter_of_mem {sβ sβ : finset Ξ±} {a : Ξ±} (h : a β sβ) :
insert a sβ β© sβ = insert a (sβ β© sβ) :=
ext $ Ξ» x, have x = a β¨ x β sβ β x β sβ, from or_iff_right_of_imp $ by rintro rfl; exact h,
by simp only [mem_inter, mem_insert, or_and_distrib_left, this]
@[simp] theorem inter_insert_of_mem {sβ sβ : finset Ξ±} {a : Ξ±} (h : a β sβ) :
sβ β© insert a sβ = insert a (sβ β© sβ) :=
by rw [inter_comm, insert_inter_of_mem h, inter_comm]
@[simp] theorem insert_inter_of_not_mem {sβ sβ : finset Ξ±} {a : Ξ±} (h : a β sβ) :
insert a sβ β© sβ = sβ β© sβ :=
ext $ Ξ» x, have Β¬ (x = a β§ x β sβ), by rintro β¨rfl, Hβ©; exact h H,
by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or]
@[simp] theorem inter_insert_of_not_mem {sβ sβ : finset Ξ±} {a : Ξ±} (h : a β sβ) :
sβ β© insert a sβ = sβ β© sβ :=
by rw [inter_comm, insert_inter_of_not_mem h, inter_comm]
@[simp] theorem singleton_inter_of_mem {a : Ξ±} {s : finset Ξ±} (H : a β s) : {a} β© s = {a} :=
show insert a β
β© s = insert a β
, by rw [insert_inter_of_mem H, empty_inter]
@[simp] theorem singleton_inter_of_not_mem {a : Ξ±} {s : finset Ξ±} (H : a β s) : {a} β© s = β
:=
eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x β¨rfl, hβ©; exact H h
@[simp] theorem inter_singleton_of_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : s β© {a} = {a} :=
by rw [inter_comm, singleton_inter_of_mem h]
@[simp] theorem inter_singleton_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : s β© {a} = β
:=
by rw [inter_comm, singleton_inter_of_not_mem h]
@[mono]
lemma inter_subset_inter {x y s t : finset Ξ±} (h : x β y) (h' : s β t) : x β© s β y β© t :=
begin
intros a a_in,
rw finset.mem_inter at a_in β’,
exact β¨h a_in.1, h' a_in.2β©
end
lemma inter_subset_inter_right {x y s : finset Ξ±} (h : x β y) : x β© s β y β© s :=
finset.inter_subset_inter h (finset.subset.refl _)
lemma inter_subset_inter_left {x y s : finset Ξ±} (h : x β y) : s β© x β s β© y :=
finset.inter_subset_inter (finset.subset.refl _) h
/-! ### lattice laws -/
instance : lattice (finset Ξ±) :=
{ sup := (βͺ),
sup_le := assume a b c, union_subset,
le_sup_left := subset_union_left,
le_sup_right := subset_union_right,
inf := (β©),
le_inf := assume a b c, subset_inter,
inf_le_left := inter_subset_left,
inf_le_right := inter_subset_right,
..finset.partial_order }
@[simp] theorem sup_eq_union (s t : finset Ξ±) : s β t = s βͺ t := rfl
@[simp] theorem inf_eq_inter (s t : finset Ξ±) : s β t = s β© t := rfl
instance : semilattice_inf_bot (finset Ξ±) :=
{ bot := β
, bot_le := empty_subset, ..finset.lattice }
instance {Ξ± : Type*} [decidable_eq Ξ±] : semilattice_sup_bot (finset Ξ±) :=
{ ..finset.semilattice_inf_bot, ..finset.lattice }
instance : distrib_lattice (finset Ξ±) :=
{ le_sup_inf := assume a b c, show (a βͺ b) β© (a βͺ c) β a βͺ b β© c,
by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt};
simp only [true_or, imp_true_iff, true_and, or_true],
..finset.lattice }
theorem inter_distrib_left (s t u : finset Ξ±) : s β© (t βͺ u) = (s β© t) βͺ (s β© u) := inf_sup_left
theorem inter_distrib_right (s t u : finset Ξ±) : (s βͺ t) β© u = (s β© u) βͺ (t β© u) := inf_sup_right
theorem union_distrib_left (s t u : finset Ξ±) : s βͺ (t β© u) = (s βͺ t) β© (s βͺ u) := sup_inf_left
theorem union_distrib_right (s t u : finset Ξ±) : (s β© t) βͺ u = (s βͺ u) β© (t βͺ u) := sup_inf_right
lemma union_eq_empty_iff (A B : finset Ξ±) : A βͺ B = β
β A = β
β§ B = β
:= sup_eq_bot_iff
/-! ### erase -/
/-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are
not equal to `a`. -/
def erase (s : finset Ξ±) (a : Ξ±) : finset Ξ± := β¨_, nodup_erase_of_nodup a s.2β©
@[simp] theorem erase_val (s : finset Ξ±) (a : Ξ±) : (erase s a).1 = s.1.erase a := rfl
@[simp] theorem mem_erase {a b : Ξ±} {s : finset Ξ±} : a β erase s b β a β b β§ a β s :=
mem_erase_iff_of_nodup s.2
theorem not_mem_erase (a : Ξ±) (s : finset Ξ±) : a β erase s a := mem_erase_of_nodup s.2
@[simp] theorem erase_empty (a : Ξ±) : erase β
a = β
:= rfl
theorem ne_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b β erase s a β b β a :=
by simp only [mem_erase]; exact and.left
theorem mem_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b β erase s a β b β s := mem_of_mem_erase
theorem mem_erase_of_ne_of_mem {a b : Ξ±} {s : finset Ξ±} : a β b β a β s β a β erase s b :=
by simp only [mem_erase]; exact and.intro
/-- An element of `s` that is not an element of `erase s a` must be
`a`. -/
lemma eq_of_mem_of_not_mem_erase {a b : Ξ±} {s : finset Ξ±} (hs : b β s)
(hsa : b β s.erase a) : b = a :=
begin
rw [mem_erase, not_and] at hsa,
exact not_imp_not.mp hsa hs
end
theorem erase_insert {a : Ξ±} {s : finset Ξ±} (h : a β s) : erase (insert a s) a = s :=
ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or];
apply and_iff_right_of_imp; rintro H rfl; exact h H
theorem insert_erase {a : Ξ±} {s : finset Ξ±} (h : a β s) : insert a (erase s a) = s :=
ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and];
apply or_iff_right_of_imp; rintro rfl; exact h
theorem erase_subset_erase (a : Ξ±) {s t : finset Ξ±} (h : s β t) : erase s a β erase t a :=
val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h
theorem erase_subset (a : Ξ±) (s : finset Ξ±) : erase s a β s := erase_subset _ _
@[simp, norm_cast] lemma coe_erase (a : Ξ±) (s : finset Ξ±) : β(erase s a) = (s \ {a} : set Ξ±) :=
set.ext $ Ξ» _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl
lemma erase_ssubset {a : Ξ±} {s : finset Ξ±} (h : a β s) : s.erase a β s :=
calc s.erase a β insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h
theorem erase_eq_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a β s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h
theorem subset_insert_iff {a : Ξ±} {s t : finset Ξ±} : s β insert a t β erase s a β t :=
by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp];
exact forall_congr (Ξ» x, forall_swap)
theorem erase_insert_subset (a : Ξ±) (s : finset Ξ±) : erase (insert a s) a β s :=
subset_insert_iff.1 $ subset.refl _
theorem insert_erase_subset (a : Ξ±) (s : finset Ξ±) : s β insert a (erase s a) :=
subset_insert_iff.2 $ subset.refl _
/-! ### sdiff -/
/-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/
instance : has_sdiff (finset Ξ±) := β¨Ξ»sβ sβ, β¨sβ.1 - sβ.1, nodup_of_le (sub_le_self _ _) sβ.2β©β©
@[simp] theorem mem_sdiff {a : Ξ±} {sβ sβ : finset Ξ±} :
a β sβ \ sβ β a β sβ β§ a β sβ := mem_sub_of_nodup sβ.2
lemma not_mem_sdiff_of_mem_right {a : Ξ±} {s t : finset Ξ±} (h : a β t) : a β s \ t :=
by simp only [mem_sdiff, h, not_true, not_false_iff, and_false]
theorem sdiff_union_of_subset {sβ sβ : finset Ξ±} (h : sβ β sβ) : (sβ \ sβ) βͺ sβ = sβ :=
ext $ Ξ» a, by simpa only [mem_sdiff, mem_union, or_comm,
or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a)
theorem union_sdiff_of_subset {sβ sβ : finset Ξ±} (h : sβ β sβ) : sβ βͺ (sβ \ sβ) = sβ :=
(union_comm _ _).trans (sdiff_union_of_subset h)
theorem inter_sdiff (s t u : finset Ξ±) : s β© (t \ u) = s β© t \ u :=
by { ext x, simp [and_assoc] }
@[simp] theorem inter_sdiff_self (sβ sβ : finset Ξ±) : sβ β© (sβ \ sβ) = β
:=
eq_empty_of_forall_not_mem $
by simp only [mem_inter, mem_sdiff]; rintro x β¨h, _, hnβ©; exact hn h
@[simp] theorem sdiff_inter_self (sβ sβ : finset Ξ±) : (sβ \ sβ) β© sβ = β
:=
(inter_comm _ _).trans (inter_sdiff_self _ _)
@[simp] theorem sdiff_self (sβ : finset Ξ±) : sβ \ sβ = β
:=
by ext; simp
theorem sdiff_inter_distrib_right (sβ sβ sβ : finset Ξ±) : sβ \ (sβ β© sβ) = (sβ \ sβ) βͺ (sβ \ sβ) :=
by ext; simp only [and_or_distrib_left, mem_union, not_and_distrib, mem_sdiff, mem_inter]
@[simp] theorem sdiff_inter_self_left (sβ sβ : finset Ξ±) : sβ \ (sβ β© sβ) = sβ \ sβ :=
by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union]
@[simp] theorem sdiff_inter_self_right (sβ sβ : finset Ξ±) : sβ \ (sβ β© sβ) = sβ \ sβ :=
by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty]
@[simp] theorem sdiff_empty {sβ : finset Ξ±} : sβ \ β
= sβ :=
ext (by simp)
@[mono]
theorem sdiff_subset_sdiff {sβ sβ tβ tβ : finset Ξ±} (hβ : tβ β tβ) (hβ : sβ β sβ) :
tβ \ sβ β tβ \ sβ :=
by simpa only [subset_iff, mem_sdiff, and_imp] using Ξ» a mβ mβ, and.intro (hβ mβ) (mt (@hβ _) mβ)
theorem sdiff_subset_self {sβ sβ : finset Ξ±} : sβ \ sβ β sβ :=
suffices sβ \ sβ β sβ \ β
, by simpa [sdiff_empty] using this,
sdiff_subset_sdiff (subset.refl _) (empty_subset _)
@[simp, norm_cast] lemma coe_sdiff (sβ sβ : finset Ξ±) : β(sβ \ sβ) = (sβ \ sβ : set Ξ±) :=
set.ext $ Ξ» _, mem_sdiff
@[simp] theorem union_sdiff_self_eq_union {s t : finset Ξ±} : s βͺ (t \ s) = s βͺ t :=
ext $ Ξ» a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left,
imp_and_distrib, and_iff_left id]
@[simp] theorem sdiff_union_self_eq_union {s t : finset Ξ±} : (s \ t) βͺ t = s βͺ t :=
by rw [union_comm, union_sdiff_self_eq_union, union_comm]
lemma union_sdiff_symm {s t : finset Ξ±} : s βͺ (t \ s) = t βͺ (s \ t) :=
by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm]
lemma sdiff_union_inter (s t : finset Ξ±) : (s \ t) βͺ (s β© t) = s :=
by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto }
@[simp] lemma sdiff_idem (s t : finset Ξ±) : s \ t \ t = s \ t :=
by { simp only [ext_iff, mem_sdiff], tauto }
lemma sdiff_eq_empty_iff_subset {s t : finset Ξ±} : s \ t = β
β s β t :=
by { rw [subset_iff, ext_iff], simp }
@[simp] lemma empty_sdiff (s : finset Ξ±) : β
\ s = β
:=
by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ }
lemma insert_sdiff_of_not_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x β t) :
(insert x s) \ t = insert x (s \ t) :=
begin
rw [β coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_not_mem s h
end
lemma insert_sdiff_of_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x β t) :
(insert x s) \ t = s \ t :=
begin
rw [β coe_inj, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_mem s h
end
@[simp] lemma insert_sdiff_insert (s t : finset Ξ±) (x : Ξ±) :
(insert x s) \ (insert x t) = s \ insert x t :=
insert_sdiff_of_mem _ (mem_insert_self _ _)
lemma sdiff_insert_of_not_mem {s : finset Ξ±} {x : Ξ±} (h : x β s) (t : finset Ξ±) :
s \ (insert x t) = s \ t :=
begin
refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (Ξ» y hy, _),
simp only [mem_sdiff, mem_insert, not_or_distrib] at hy β’,
exact β¨hy.1, Ξ» hxy, h $ hxy βΈ hy.1, hy.2β©
end
@[simp] lemma sdiff_subset (s t : finset Ξ±) : s \ t β s :=
by simp [subset_iff, mem_sdiff] {contextual := tt}
lemma union_sdiff_distrib (sβ sβ t : finset Ξ±) : (sβ βͺ sβ) \ t = sβ \ t βͺ sβ \ t :=
by { simp only [ext_iff, mem_sdiff, mem_union], tauto }
lemma sdiff_union_distrib (s tβ tβ : finset Ξ±) : s \ (tβ βͺ tβ) = (s \ tβ) β© (s \ tβ) :=
by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto }
lemma union_sdiff_self (s t : finset Ξ±) : (s βͺ t) \ t = s \ t :=
by rw [union_sdiff_distrib, sdiff_self, union_empty]
lemma sdiff_singleton_eq_erase (a : Ξ±) (s : finset Ξ±) : s \ singleton a = erase s a :=
by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto }
lemma sdiff_sdiff_self_left (s t : finset Ξ±) : s \ (s \ t) = s β© t :=
by { simp only [ext_iff, mem_sdiff, mem_inter], tauto }
lemma inter_eq_inter_of_sdiff_eq_sdiff {s tβ tβ : finset Ξ±} : s \ tβ = s \ tβ β s β© tβ = s β© tβ :=
by { simp only [ext_iff, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto }
end decidable_eq
/-! ### attach -/
/-- `attach s` takes the elements of `s` and forms a new set of elements of the
subtype `{x // x β s}`. -/
def attach (s : finset Ξ±) : finset {x // x β s} := β¨attach s.1, nodup_attach.2 s.2β©
theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : finset Ξ±} (hx : x β s) :
sizeof x < sizeof s := by
{ cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof],
apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx }
@[simp] theorem attach_val (s : finset Ξ±) : s.attach.1 = s.1.attach := rfl
@[simp] theorem mem_attach (s : finset Ξ±) : β x, x β s.attach := mem_attach _
@[simp] theorem attach_empty : attach (β
: finset Ξ±) = β
:= rfl
/-! ### piecewise -/
section piecewise
/-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its
complement. -/
def piecewise {Ξ± : Type*} {Ξ΄ : Ξ± β Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i) [βj, decidable (j β s)] :
Ξ i, Ξ΄ i :=
Ξ»i, if i β s then f i else g i
variables {Ξ΄ : Ξ± β Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i)
@[simp] lemma piecewise_insert_self [decidable_eq Ξ±] {j : Ξ±} [βi, decidable (i β insert j s)] :
(insert j s).piecewise f g j = f j :=
by simp [piecewise]
@[simp] lemma piecewise_empty [βi : Ξ±, decidable (i β (β
: finset Ξ±))] : piecewise β
f g = g :=
by { ext i, simp [piecewise] }
variable [βj, decidable (j β s)]
@[norm_cast] lemma piecewise_coe [βj, decidable (j β (s : set Ξ±))] :
(s : set Ξ±).piecewise f g = s.piecewise f g :=
by { ext, congr }
@[simp, priority 980]
lemma piecewise_eq_of_mem {i : Ξ±} (hi : i β s) : s.piecewise f g i = f i :=
by simp [piecewise, hi]
@[simp, priority 980]
lemma piecewise_eq_of_not_mem {i : Ξ±} (hi : i β s) : s.piecewise f g i = g i :=
by simp [piecewise, hi]
lemma piecewise_congr {f f' g g' : Ξ i, Ξ΄ i} (hf : β i β s, f i = f' i) (hg : β i β s, g i = g' i) :
s.piecewise f g = s.piecewise f' g' :=
funext $ Ξ» i, if_ctx_congr iff.rfl (hf i) (hg i)
@[simp, priority 990]
lemma piecewise_insert_of_ne [decidable_eq Ξ±] {i j : Ξ±} [βi, decidable (i β insert j s)]
(h : i β j) : (insert j s).piecewise f g i = s.piecewise f g i :=
by simp [piecewise, h]
lemma piecewise_insert [decidable_eq Ξ±] (j : Ξ±) [βi, decidable (i β insert j s)] :
(insert j s).piecewise f g = update (s.piecewise f g) j (f j) :=
begin
classical,
rw [β piecewise_coe, β piecewise_coe, β set.piecewise_insert, β coe_insert j s],
congr
end
lemma piecewise_cases {i} (p : Ξ΄ i β Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) :=
by by_cases hi : i β s; simpa [hi]
lemma piecewise_mem_set_pi {Ξ΄ : Ξ± β Type*} {t : set Ξ±} {t' : Ξ i, set (Ξ΄ i)}
{f g} (hf : f β set.pi t t') (hg : g β set.pi t t') : s.piecewise f g β set.pi t t' :=
by { classical, rw β piecewise_coe, exact set.piecewise_mem_pi βs hf hg }
lemma piecewise_singleton [decidable_eq Ξ±] (i : Ξ±) :
piecewise {i} f g = update g i (f i) :=
by rw [β insert_emptyc_eq, piecewise_insert, piecewise_empty]
lemma piecewise_piecewise_of_subset_left {s t : finset Ξ±} [Ξ i, decidable (i β s)]
[Ξ i, decidable (i β t)] (h : s β t) (fβ fβ g : Ξ a, Ξ΄ a) :
s.piecewise (t.piecewise fβ fβ) g = s.piecewise fβ g :=
s.piecewise_congr (Ξ» i hi, piecewise_eq_of_mem _ _ _ (h hi)) (Ξ» _ _, rfl)
@[simp] lemma piecewise_idem_left (fβ fβ g : Ξ a, Ξ΄ a) :
s.piecewise (s.piecewise fβ fβ) g = s.piecewise fβ g :=
piecewise_piecewise_of_subset_left (subset.refl _) _ _ _
lemma piecewise_piecewise_of_subset_right {s t : finset Ξ±} [Ξ i, decidable (i β s)]
[Ξ i, decidable (i β t)] (h : t β s) (f gβ gβ : Ξ a, Ξ΄ a) :
s.piecewise f (t.piecewise gβ gβ) = s.piecewise f gβ :=
s.piecewise_congr (Ξ» _ _, rfl) (Ξ» i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi))
@[simp] lemma piecewise_idem_right (f gβ gβ : Ξ a, Ξ΄ a) :
s.piecewise f (s.piecewise gβ gβ) = s.piecewise f gβ :=
piecewise_piecewise_of_subset_right (subset.refl _) f gβ gβ
lemma update_eq_piecewise {Ξ² : Type*} [decidable_eq Ξ±] (f : Ξ± β Ξ²) (i : Ξ±) (v : Ξ²) :
update f i v = piecewise (singleton i) (Ξ»j, v) f :=
(piecewise_singleton _ _ _).symm
lemma update_piecewise [decidable_eq Ξ±] (i : Ξ±) (v : Ξ΄ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) :=
begin
ext j,
rcases em (j = i) with (rfl|hj); by_cases hs : j β s; simp *
end
lemma update_piecewise_of_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i β s) (v : Ξ΄ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) g :=
begin
rw update_piecewise,
refine s.piecewise_congr (Ξ» _ _, rfl) (Ξ» j hj, update_noteq _ _ _),
exact Ξ» h, hj (h.symm βΈ hi)
end
lemma update_piecewise_of_not_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i β s) (v : Ξ΄ i) :
update (s.piecewise f g) i v = s.piecewise f (update g i v) :=
begin
rw update_piecewise,
refine s.piecewise_congr (Ξ» j hj, update_noteq _ _ _) (Ξ» _ _, rfl),
exact Ξ» h, hi (h βΈ hj)
end
lemma piecewise_le_of_le_of_le {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g h : Ξ i, Ξ΄ i}
(Hf : f β€ h) (Hg : g β€ h) : s.piecewise f g β€ h :=
Ξ» x, piecewise_cases s f g (β€ h x) (Hf x) (Hg x)
lemma le_piecewise_of_le_of_le {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g h : Ξ i, Ξ΄ i}
(Hf : h β€ f) (Hg : h β€ g) : h β€ s.piecewise f g :=
Ξ» x, piecewise_cases s f g (Ξ» y, h x β€ y) (Hf x) (Hg x)
lemma piecewise_le_piecewise' {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g f' g' : Ξ i, Ξ΄ i}
(Hf : β x β s, f x β€ f' x) (Hg : β x β s, g x β€ g' x) : s.piecewise f g β€ s.piecewise f' g' :=
Ξ» x, by { by_cases hx : x β s; simp [hx, *] }
lemma piecewise_le_piecewise {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g f' g' : Ξ i, Ξ΄ i}
(Hf : f β€ f') (Hg : g β€ g') : s.piecewise f g β€ s.piecewise f' g' :=
s.piecewise_le_piecewise' (Ξ» x _, Hf x) (Ξ» x _, Hg x)
lemma piecewise_mem_Icc_of_mem_of_mem {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f fβ g gβ : Ξ i, Ξ΄ i}
(hf : f β set.Icc fβ gβ) (hg : g β set.Icc fβ gβ) :
s.piecewise f g β set.Icc fβ gβ :=
β¨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2β©
lemma piecewise_mem_Icc {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g : Ξ i, Ξ΄ i} (h : f β€ g) :
s.piecewise f g β set.Icc f g :=
piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h)
lemma piecewise_mem_Icc' {Ξ΄ : Ξ± β Type*} [Ξ i, preorder (Ξ΄ i)] {f g : Ξ i, Ξ΄ i} (h : g β€ f) :
s.piecewise f g β set.Icc g f :=
piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h)
end piecewise
section decidable_pi_exists
variables {s : finset Ξ±}
instance decidable_dforall_finset {p : Ξ aβs, Prop} [hp : βa (h : a β s), decidable (p a h)] :
decidable (βa (h : a β s), p a h) :=
multiset.decidable_dforall_multiset
/-- decidable equality for functions whose domain is bounded by finsets -/
instance decidable_eq_pi_finset {Ξ² : Ξ± β Type*} [h : βa, decidable_eq (Ξ² a)] :
decidable_eq (Ξ aβs, Ξ² a) :=
multiset.decidable_eq_pi_multiset
instance decidable_dexists_finset {p : Ξ aβs, Prop} [hp : βa (h : a β s), decidable (p a h)] :
decidable (βa (h : a β s), p a h) :=
multiset.decidable_dexists_multiset
end decidable_pi_exists
/-! ### filter -/
section filter
variables (p q : Ξ± β Prop) [decidable_pred p] [decidable_pred q]
/-- `filter p s` is the set of elements of `s` that satisfy `p`. -/
def filter (s : finset Ξ±) : finset Ξ± :=
β¨_, nodup_filter p s.2β©
@[simp] theorem filter_val (s : finset Ξ±) : (filter p s).1 = s.1.filter p := rfl
@[simp] theorem filter_subset (s : finset Ξ±) : s.filter p β s := filter_subset _ _
variable {p}
@[simp] theorem mem_filter {s : finset Ξ±} {a : Ξ±} : a β s.filter p β a β s β§ p a := mem_filter
theorem filter_ssubset {s : finset Ξ±} : s.filter p β s β β x β s, Β¬ p x :=
β¨Ξ» h, let β¨x, hs, hpβ© := set.exists_of_ssubset h in β¨x, hs, mt (Ξ» hp, mem_filter.2 β¨hs, hpβ©) hpβ©,
Ξ» β¨x, hs, hpβ©, β¨s.filter_subset _, Ξ» h, hp (mem_filter.1 (h hs)).2β©β©
variable (p)
theorem filter_filter (s : finset Ξ±) : (s.filter p).filter q = s.filter (Ξ»a, p a β§ q a) :=
ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm]
lemma filter_true {s : finset Ξ±} [h : decidable_pred (Ξ» _, true)] :
@finset.filter Ξ± (Ξ» _, true) h s = s :=
by ext; simp
@[simp] theorem filter_false {h} (s : finset Ξ±) : @filter Ξ± (Ξ»a, false) h s = β
:=
ext $ assume a, by simp only [mem_filter, and_false]; refl
variables {p q}
/-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/
@[simp] lemma filter_true_of_mem {s : finset Ξ±} (h : β x β s, p x) : s.filter p = s :=
ext $ Ξ» x, β¨Ξ» h, (mem_filter.1 h).1, Ξ» hx, mem_filter.2 β¨hx, h x hxβ©β©
/-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `β
`. -/
lemma filter_false_of_mem {s : finset Ξ±} (h : β x β s, Β¬ p x) : s.filter p = β
:=
eq_empty_of_forall_not_mem (by simpa)
lemma filter_congr {s : finset Ξ±} (H : β x β s, p x β q x) : filter p s = filter q s :=
eq_of_veq $ filter_congr H
variables (p q)
lemma filter_empty : filter p β
= β
:= subset_empty.1 $ filter_subset _ _
lemma filter_subset_filter {s t : finset Ξ±} (h : s β t) : s.filter p β t.filter p :=
assume a ha, mem_filter.2 β¨h (mem_filter.1 ha).1, (mem_filter.1 ha).2β©
@[simp, norm_cast] lemma coe_filter (s : finset Ξ±) : β(s.filter p) = ({x β βs | p x} : set Ξ±) :=
set.ext $ Ξ» _, mem_filter
theorem filter_singleton (a : Ξ±) : filter p (singleton a) = if p a then singleton a else β
:=
by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
variable [decidable_eq Ξ±]
theorem filter_union (sβ sβ : finset Ξ±) : (sβ βͺ sβ).filter p = sβ.filter p βͺ sβ.filter p :=
ext $ Ξ» _, by simp only [mem_filter, mem_union, or_and_distrib_right]
theorem filter_union_right (s : finset Ξ±) : s.filter p βͺ s.filter q = s.filter (Ξ»x, p x β¨ q x) :=
ext $ Ξ» x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm]
lemma filter_mem_eq_inter {s t : finset Ξ±} [Ξ i, decidable (i β t)] :
s.filter (Ξ» i, i β t) = s β© t :=
ext $ Ξ» i, by rw [mem_filter, mem_inter]
theorem filter_inter (s t : finset Ξ±) : filter p s β© t = filter p (s β© t) :=
by { ext, simp only [mem_inter, mem_filter, and.right_comm] }
theorem inter_filter (s t : finset Ξ±) : s β© filter p t = filter p (s β© t) :=
by rw [inter_comm, filter_inter, inter_comm]
theorem filter_insert (a : Ξ±) (s : finset Ξ±) :
filter p (insert a s) = if p a then insert a (filter p s) else filter p s :=
by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
theorem filter_or [decidable_pred (Ξ» a, p a β¨ q a)] (s : finset Ξ±) :
s.filter (Ξ» a, p a β¨ q a) = s.filter p βͺ s.filter q :=
ext $ Ξ» _, by simp only [mem_filter, mem_union, and_or_distrib_left]
theorem filter_and [decidable_pred (Ξ» a, p a β§ q a)] (s : finset Ξ±) :
s.filter (Ξ» a, p a β§ q a) = s.filter p β© s.filter q :=
ext $ Ξ» _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self]
theorem filter_not [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) :
s.filter (Ξ» a, Β¬ p a) = s \ s.filter p :=
ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using Ξ» a, and_congr_right $
Ξ» h : a β s, (imp_iff_right h).symm.trans imp_not_comm
theorem sdiff_eq_filter (sβ sβ : finset Ξ±) :
sβ \ sβ = filter (β sβ) sβ := ext $ Ξ» _, by simp only [mem_sdiff, mem_filter]
theorem sdiff_eq_self (sβ sβ : finset Ξ±) :
sβ \ sβ = sβ β sβ β© sβ β β
:=
by { simp [subset.antisymm_iff,sdiff_subset_self],
split; intro h,
{ transitivity' ((sβ \ sβ) β© sβ), mono, simp },
{ calc sβ \ sβ
β sβ \ (sβ β© sβ) : by simp [(β)]
... β sβ \ β
: by mono using [(β)]
... β sβ : by simp [(β)] } }
theorem filter_union_filter_neg_eq [decidable_pred (Ξ» a, Β¬ p a)]
(s : finset Ξ±) : s.filter p βͺ s.filter (Ξ»a, Β¬ p a) = s :=
by simp only [filter_not, union_sdiff_of_subset (filter_subset p s)]
theorem filter_inter_filter_neg_eq (s : finset Ξ±) : s.filter p β© s.filter (Ξ»a, Β¬ p a) = β
:=
by simp only [filter_not, inter_sdiff_self]
lemma subset_union_elim {s : finset Ξ±} {tβ tβ : set Ξ±} (h : βs β tβ βͺ tβ) :
βsβ sβ : finset Ξ±, sβ βͺ sβ = s β§ βsβ β tβ β§ βsβ β tβ \ tβ :=
begin
classical,
refine β¨s.filter (β tβ), s.filter (β tβ), _, _ , _β©,
{ simp [filter_union_right, em] },
{ intro x, simp },
{ intro x, simp, intros hx hxβ, refine β¨or.resolve_left (h hx) hxβ, hxββ© }
end
/- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/
@[simp] lemma filter_congr_decidable {Ξ±} (s : finset Ξ±) (p : Ξ± β Prop) (h : decidable_pred p)
[decidable_pred p] : @filter Ξ± p h s = s.filter p :=
by congr
section classical
open_locale classical
/-- The following instance allows us to write `{ x β s | p x }` for `finset.filter s p`.
Since the former notation requires us to define this for all propositions `p`, and `finset.filter`
only works for decidable propositions, the notation `{ x β s | p x }` is only compatible with
classical logic because it uses `classical.prop_decidable`.
We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp`
unfolds the notation `{ x β s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the
simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance
for decidability.
-/
noncomputable instance {Ξ± : Type*} : has_sep Ξ± (finset Ξ±) := β¨Ξ» p x, x.filter pβ©
@[simp] lemma sep_def {Ξ± : Type*} (s : finset Ξ±) (p : Ξ± β Prop) : {x β s | p x} = s.filter p := rfl
end classical
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq'` with the equality the other way.
-/
-- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing
-- on, e.g. `x β s.filter(eq b)`.
lemma filter_eq [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) :
s.filter (eq b) = ite (b β s) {b} β
:=
begin
split_ifs,
{ ext,
simp only [mem_filter, mem_singleton],
exact β¨Ξ» h, h.2.symm, by { rintro β¨hβ©, exact β¨h, rflβ©, }β© },
{ ext,
simp only [mem_filter, not_and, iff_false, not_mem_empty],
rintros m β¨eβ©, exact h m, }
end
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq` with the equality the other way.
-/
lemma filter_eq' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) :
s.filter (Ξ» a, a = b) = ite (b β s) {b} β
:=
trans (filter_congr (Ξ» _ _, β¨eq.symm, eq.symmβ©)) (filter_eq s b)
lemma filter_ne [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) :
s.filter (Ξ» a, b β a) = s.erase b :=
by { ext, simp only [mem_filter, mem_erase, ne.def], cc, }
lemma filter_ne' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) :
s.filter (Ξ» a, a β b) = s.erase b :=
trans (filter_congr (Ξ» _ _, β¨ne.symm, ne.symmβ©)) (filter_ne s b)
end filter
/-! ### range -/
section range
variables {n m l : β}
/-- `range n` is the set of natural numbers less than `n`. -/
def range (n : β) : finset β := β¨_, nodup_range nβ©
@[simp] theorem range_coe (n : β) : (range n).1 = multiset.range n := rfl
@[simp] theorem mem_range : m β range n β m < n := mem_range
@[simp] theorem range_zero : range 0 = β
:= rfl
@[simp] theorem range_one : range 1 = {0} := rfl
theorem range_succ : range (succ n) = insert n (range n) :=
eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm
theorem range_add_one : range (n + 1) = insert n (range n) :=
range_succ
@[simp] theorem not_mem_range_self : n β range n := not_mem_range_self
@[simp] theorem self_mem_range_succ (n : β) : n β range (n + 1) := multiset.self_mem_range_succ n
@[simp] theorem range_subset {n m} : range n β range m β n β€ m := range_subset
theorem range_mono : monotone range := Ξ» _ _, range_subset.2
lemma mem_range_succ_iff {a b : β} : a β finset.range b.succ β a β€ b :=
finset.mem_range.trans nat.lt_succ_iff
end range
/- useful rules for calculations with quantifiers -/
theorem exists_mem_empty_iff (p : Ξ± β Prop) : (β x, x β (β
: finset Ξ±) β§ p x) β false :=
by simp only [not_mem_empty, false_and, exists_false]
theorem exists_mem_insert [d : decidable_eq Ξ±]
(a : Ξ±) (s : finset Ξ±) (p : Ξ± β Prop) :
(β x, x β insert a s β§ p x) β p a β¨ (β x, x β s β§ p x) :=
by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left]
theorem forall_mem_empty_iff (p : Ξ± β Prop) : (β x, x β (β
: finset Ξ±) β p x) β true :=
iff_true_intro $ Ξ» _, false.elim
theorem forall_mem_insert [d : decidable_eq Ξ±]
(a : Ξ±) (s : finset Ξ±) (p : Ξ± β Prop) :
(β x, x β insert a s β p x) β p a β§ (β x, x β s β p x) :=
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
end finset
/-- Equivalence between the set of natural numbers which are `β₯ k` and `β`, given by `n β n - k`. -/
def not_mem_range_equiv (k : β) : {n // n β range k} β β :=
{ to_fun := Ξ» i, i.1 - k,
inv_fun := Ξ» j, β¨j + k, by simpβ©,
left_inv :=
begin
assume j,
rw subtype.ext_iff_val,
apply nat.sub_add_cancel,
simpa using j.2
end,
right_inv := Ξ» j, nat.add_sub_cancel _ _ }
@[simp] lemma coe_not_mem_range_equiv (k : β) :
(not_mem_range_equiv k : {n // n β range k} β β) = (Ξ» i, i - k) := rfl
@[simp] lemma coe_not_mem_range_equiv_symm (k : β) :
((not_mem_range_equiv k).symm : β β {n // n β range k}) = Ξ» j, β¨j + k, by simpβ© := rfl
namespace option
/-- Construct an empty or singleton finset from an `option` -/
def to_finset (o : option Ξ±) : finset Ξ± :=
match o with
| none := β
| some a := {a}
end
@[simp] theorem to_finset_none : none.to_finset = (β
: finset Ξ±) := rfl
@[simp] theorem to_finset_some {a : Ξ±} : (some a).to_finset = {a} := rfl
@[simp] theorem mem_to_finset {a : Ξ±} {o : option Ξ±} : a β o.to_finset β a β o :=
by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl
end option
/-! ### erase_dup on list and multiset -/
namespace multiset
variable [decidable_eq Ξ±]
/-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/
def to_finset (s : multiset Ξ±) : finset Ξ± := β¨_, nodup_erase_dup sβ©
@[simp] theorem to_finset_val (s : multiset Ξ±) : s.to_finset.1 = s.erase_dup := rfl
theorem to_finset_eq {s : multiset Ξ±} (n : nodup s) : finset.mk s n = s.to_finset :=
finset.val_inj.1 (erase_dup_eq_self.2 n).symm
@[simp] theorem mem_to_finset {a : Ξ±} {s : multiset Ξ±} : a β s.to_finset β a β s :=
mem_erase_dup
@[simp] lemma to_finset_zero :
to_finset (0 : multiset Ξ±) = β
:=
rfl
@[simp] lemma to_finset_cons (a : Ξ±) (s : multiset Ξ±) :
to_finset (a ::β s) = insert a (to_finset s) :=
finset.eq_of_veq erase_dup_cons
@[simp] lemma to_finset_add (s t : multiset Ξ±) :
to_finset (s + t) = to_finset s βͺ to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_nsmul (s : multiset Ξ±) :
β(n : β) (hn : n β 0), (n β’β s).to_finset = s.to_finset
| 0 h := by contradiction
| (n+1) h :=
begin
by_cases n = 0,
{ rw [h, zero_add, one_nsmul] },
{ rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] }
end
@[simp] lemma to_finset_inter (s t : multiset Ξ±) :
to_finset (s β© t) = to_finset s β© to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_union (s t : multiset Ξ±) :
(s βͺ t).to_finset = s.to_finset βͺ t.to_finset :=
by ext; simp
theorem to_finset_eq_empty {m : multiset Ξ±} : m.to_finset = β
β m = 0 :=
finset.val_inj.symm.trans multiset.erase_dup_eq_zero
@[simp] lemma to_finset_subset (m1 m2 : multiset Ξ±) :
m1.to_finset β m2.to_finset β m1 β m2 :=
by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset]
end multiset
namespace finset
@[simp] lemma val_to_finset [decidable_eq Ξ±] (s : finset Ξ±) : s.val.to_finset = s :=
by { ext, rw [multiset.mem_to_finset, βmem_def] }
end finset
namespace list
variable [decidable_eq Ξ±]
/-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/
def to_finset (l : list Ξ±) : finset Ξ± := multiset.to_finset l
@[simp] theorem to_finset_val (l : list Ξ±) : l.to_finset.1 = (l.erase_dup : multiset Ξ±) := rfl
theorem to_finset_eq {l : list Ξ±} (n : nodup l) : @finset.mk Ξ± l n = l.to_finset :=
multiset.to_finset_eq n
@[simp] theorem mem_to_finset {a : Ξ±} {l : list Ξ±} : a β l.to_finset β a β l :=
mem_erase_dup
@[simp] theorem to_finset_nil : to_finset (@nil Ξ±) = β
:=
rfl
@[simp] theorem to_finset_cons {a : Ξ±} {l : list Ξ±} : to_finset (a :: l) = insert a (to_finset l) :=
finset.eq_of_veq $ by by_cases h : a β l; simp [finset.insert_val', multiset.erase_dup_cons, h]
lemma to_finset_surj_on : set.surj_on to_finset {l : list Ξ± | l.nodup} set.univ :=
begin
rintro s -,
cases s with t hl, induction t using quot.ind with l,
refine β¨l, hl, (to_finset_eq hl).symmβ©
end
theorem to_finset_surjective : surjective (to_finset : list Ξ± β finset Ξ±) :=
by { intro s, rcases to_finset_surj_on (set.mem_univ s) with β¨l, -, hlsβ©, exact β¨l, hlsβ© }
end list
namespace finset
/-! ### map -/
section map
open function
/-- When `f` is an embedding of `Ξ±` in `Ξ²` and `s` is a finset in `Ξ±`, then `s.map f` is the image
finset in `Ξ²`. The embedding condition guarantees that there are no duplicates in the image. -/
def map (f : Ξ± βͺ Ξ²) (s : finset Ξ±) : finset Ξ² :=
β¨s.1.map f, nodup_map f.2 s.2β©
@[simp] theorem map_val (f : Ξ± βͺ Ξ²) (s : finset Ξ±) : (map f s).1 = s.1.map f := rfl
@[simp] theorem map_empty (f : Ξ± βͺ Ξ²) : (β
: finset Ξ±).map f = β
:= rfl
variables {f : Ξ± βͺ Ξ²} {s : finset Ξ±}
@[simp] theorem mem_map {b : Ξ²} : b β s.map f β β a β s, f a = b :=
mem_map.trans $ by simp only [exists_prop]; refl
theorem mem_map' (f : Ξ± βͺ Ξ²) {a} {s : finset Ξ±} : f a β s.map f β a β s :=
mem_map_of_injective f.2
theorem mem_map_of_mem (f : Ξ± βͺ Ξ²) {a} {s : finset Ξ±} : a β s β f a β s.map f :=
(mem_map' _).2
@[simp, norm_cast] theorem coe_map (f : Ξ± βͺ Ξ²) (s : finset Ξ±) : (β(s.map f) : set Ξ²) = f '' βs :=
set.ext $ Ξ» x, mem_map.trans set.mem_image_iff_bex.symm
theorem coe_map_subset_range (f : Ξ± βͺ Ξ²) (s : finset Ξ±) : (β(s.map f) : set Ξ²) β set.range f :=
calc β(s.map f) = f '' βs : coe_map f s
... β set.range f : set.image_subset_range f βs
theorem map_to_finset [decidable_eq Ξ±] [decidable_eq Ξ²] {s : multiset Ξ±} :
s.to_finset.map f = (s.map f).to_finset :=
ext $ Ξ» _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset]
@[simp] theorem map_refl : s.map (embedding.refl _) = s :=
ext $ Ξ» _, by simpa only [mem_map, exists_prop] using exists_eq_right
theorem map_map {g : Ξ² βͺ Ξ³} : (s.map f).map g = s.map (f.trans g) :=
eq_of_veq $ by simp only [map_val, multiset.map_map]; refl
theorem map_subset_map {sβ sβ : finset Ξ±} : sβ.map f β sβ.map f β sβ β sβ :=
β¨Ξ» h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs,
Ξ» h, by simp [subset_def, map_subset_map h]β©
theorem map_inj {sβ sβ : finset Ξ±} : sβ.map f = sβ.map f β sβ = sβ :=
by simp only [subset.antisymm_iff, map_subset_map]
/-- Associate to an embedding `f` from `Ξ±` to `Ξ²` the embedding that maps a finset to its image
under `f`. -/
def map_embedding (f : Ξ± βͺ Ξ²) : finset Ξ± βͺ finset Ξ² := β¨map f, Ξ» sβ sβ, map_inj.1β©
@[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl
theorem map_filter {p : Ξ² β Prop} [decidable_pred p] :
(s.map f).filter p = (s.filter (p β f)).map f :=
ext $ Ξ» b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact
β¨by rintro β¨β¨x, h1, rflβ©, h2β©; exact β¨x, h1, h2, rflβ©,
by rintro β¨x, h1, h2, rflβ©; exact β¨β¨x, h1, rflβ©, h2β©β©
theorem map_union [decidable_eq Ξ±] [decidable_eq Ξ²]
{f : Ξ± βͺ Ξ²} (sβ sβ : finset Ξ±) : (sβ βͺ sβ).map f = sβ.map f βͺ sβ.map f :=
ext $ Ξ» _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib]
theorem map_inter [decidable_eq Ξ±] [decidable_eq Ξ²]
{f : Ξ± βͺ Ξ²} (sβ sβ : finset Ξ±) : (sβ β© sβ).map f = sβ.map f β© sβ.map f :=
ext $ Ξ» b, by simp only [mem_map, mem_inter, exists_prop]; exact
β¨by rintro β¨a, β¨mβ, mββ©, rflβ©; exact β¨β¨a, mβ, rflβ©, β¨a, mβ, rflβ©β©,
by rintro β¨β¨a, mβ, eβ©, β¨a', mβ, rflβ©β©; cases f.2 e; exact β¨_, β¨mβ, mββ©, rflβ©β©
@[simp] theorem map_singleton (f : Ξ± βͺ Ξ²) (a : Ξ±) : map f {a} = {f a} :=
ext $ Ξ» _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm
@[simp] theorem map_insert [decidable_eq Ξ±] [decidable_eq Ξ²]
(f : Ξ± βͺ Ξ²) (a : Ξ±) (s : finset Ξ±) :
(insert a s).map f = insert (f a) (s.map f) :=
by simp only [insert_eq, map_union, map_singleton]
@[simp] theorem map_eq_empty : s.map f = β
β s = β
:=
β¨Ξ» h, eq_empty_of_forall_not_mem $
Ξ» a m, ne_empty_of_mem (mem_map_of_mem _ m) h, Ξ» e, e.symm βΈ rflβ©
lemma attach_map_val {s : finset Ξ±} : s.attach.map (embedding.subtype _) = s :=
eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _
lemma nonempty.map (h : s.nonempty) (f : Ξ± βͺ Ξ²) : (s.map f).nonempty :=
let β¨a, haβ© := h in β¨f a, (mem_map' f).mpr haβ©
end map
lemma range_add_one' (n : β) :
range (n + 1) = insert 0 ((range n).map β¨Ξ»i, i + 1, assume i j, nat.succ.injβ©) :=
by ext (β¨β© | β¨nβ©); simp [nat.succ_eq_add_one, nat.zero_lt_succ n]
/-! ### image -/
section image
variables [decidable_eq Ξ²]
/-- `image f s` is the forward image of `s` under `f`. -/
def image (f : Ξ± β Ξ²) (s : finset Ξ±) : finset Ξ² := (s.1.map f).to_finset
@[simp] theorem image_val (f : Ξ± β Ξ²) (s : finset Ξ±) : (image f s).1 = (s.1.map f).erase_dup := rfl
@[simp] theorem image_empty (f : Ξ± β Ξ²) : (β
: finset Ξ±).image f = β
:= rfl
variables {f : Ξ± β Ξ²} {s : finset Ξ±}
@[simp] theorem mem_image {b : Ξ²} : b β s.image f β β a β s, f a = b :=
by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop]
theorem mem_image_of_mem (f : Ξ± β Ξ²) {a} {s : finset Ξ±} (h : a β s) : f a β s.image f :=
mem_image.2 β¨_, h, rflβ©
lemma filter_mem_image_eq_image (f : Ξ± β Ξ²) (s : finset Ξ±) (t : finset Ξ²) (h : β x β s, f x β t) :
t.filter (Ξ» y, y β s.image f) = s.image f :=
by { ext, rw [mem_filter, mem_image],
simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib],
rintros x xel rfl, exact h _ xel }
lemma fiber_nonempty_iff_mem_image (f : Ξ± β Ξ²) (s : finset Ξ±) (y : Ξ²) :
(s.filter (Ξ» x, f x = y)).nonempty β y β s.image f :=
by simp [finset.nonempty]
@[simp, norm_cast] lemma coe_image {f : Ξ± β Ξ²} : β(s.image f) = f '' βs :=
set.ext $ Ξ» _, mem_image.trans set.mem_image_iff_bex.symm
lemma nonempty.image (h : s.nonempty) (f : Ξ± β Ξ²) : (s.image f).nonempty :=
let β¨a, haβ© := h in β¨f a, mem_image_of_mem f haβ©
theorem image_to_finset [decidable_eq Ξ±] {s : multiset Ξ±} :
s.to_finset.image f = (s.map f).to_finset :=
ext $ Ξ» _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map]
theorem image_val_of_inj_on (H : βxβs, βyβs, f x = f y β x = y) : (image f s).1 = s.1.map f :=
multiset.erase_dup_eq_self.2 (nodup_map_on H s.2)
@[simp]
theorem image_id [decidable_eq Ξ±] : s.image id = s :=
ext $ Ξ» _, by simp only [mem_image, exists_prop, id, exists_eq_right]
theorem image_image [decidable_eq Ξ³] {g : Ξ² β Ξ³} : (s.image f).image g = s.image (g β f) :=
eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map]
theorem image_subset_image {sβ sβ : finset Ξ±} (h : sβ β sβ) : sβ.image f β sβ.image f :=
by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset',
multiset.map_subset_map h]
theorem image_subset_iff {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β Ξ²} :
s.image f β t β β x β s, f x β t :=
calc s.image f β t β f '' βs β βt : by norm_cast
... β _ : set.image_subset_iff
theorem image_mono (f : Ξ± β Ξ²) : monotone (finset.image f) := Ξ» _ _, image_subset_image
theorem coe_image_subset_range : β(s.image f) β set.range f :=
calc β(s.image f) = f '' βs : coe_image
... β set.range f : set.image_subset_range f βs
theorem image_filter {p : Ξ² β Prop} [decidable_pred p] :
(s.image f).filter p = (s.filter (p β f)).image f :=
ext $ Ξ» b, by simp only [mem_filter, mem_image, exists_prop]; exact
β¨by rintro β¨β¨x, h1, rflβ©, h2β©; exact β¨x, β¨h1, h2β©, rflβ©,
by rintro β¨x, β¨h1, h2β©, rflβ©; exact β¨β¨x, h1, rflβ©, h2β©β©
theorem image_union [decidable_eq Ξ±] {f : Ξ± β Ξ²} (sβ sβ : finset Ξ±) :
(sβ βͺ sβ).image f = sβ.image f βͺ sβ.image f :=
ext $ Ξ» _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right,
exists_or_distrib]
theorem image_inter [decidable_eq Ξ±] (sβ sβ : finset Ξ±) (hf : βx y, f x = f y β x = y) :
(sβ β© sβ).image f = sβ.image f β© sβ.image f :=
ext $ by simp only [mem_image, exists_prop, mem_inter]; exact Ξ» b,
β¨Ξ» β¨a, β¨mβ, mββ©, eβ©, β¨β¨a, mβ, eβ©, β¨a, mβ, eβ©β©,
Ξ» β¨β¨a, mβ, eββ©, β¨a', mβ, eββ©β©, β¨a, β¨mβ, hf _ _ (eβ.trans eβ.symm) βΈ mββ©, eββ©β©.
@[simp] theorem image_singleton (f : Ξ± β Ξ²) (a : Ξ±) : image f {a} = {f a} :=
ext $ Ξ» x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm
@[simp] theorem image_insert [decidable_eq Ξ±] (f : Ξ± β Ξ²) (a : Ξ±) (s : finset Ξ±) :
(insert a s).image f = insert (f a) (s.image f) :=
by simp only [insert_eq, image_singleton, image_union]
@[simp] theorem image_eq_empty : s.image f = β
β s = β
:=
β¨Ξ» h, eq_empty_of_forall_not_mem $
Ξ» a m, ne_empty_of_mem (mem_image_of_mem _ m) h, Ξ» e, e.symm βΈ rflβ©
lemma attach_image_val [decidable_eq Ξ±] {s : finset Ξ±} : s.attach.image subtype.val = s :=
eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self]
@[simp] lemma attach_insert [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} :
attach (insert a s) = insert (β¨a, mem_insert_self a sβ© : {x // x β insert a s})
((attach s).image (Ξ»x, β¨x.1, mem_insert_of_mem x.2β©)) :=
ext $ Ξ» β¨x, hxβ©, β¨or.cases_on (mem_insert.1 hx)
(Ξ» h : x = a, Ξ» _, mem_insert.2 $ or.inl $ subtype.eq h)
(Ξ» h : x β s, Ξ» _, mem_insert_of_mem $ mem_image.2 $ β¨β¨x, hβ©, mem_attach _ _, subtype.eq rflβ©),
Ξ» _, finset.mem_attach _ _β©
theorem map_eq_image (f : Ξ± βͺ Ξ²) (s : finset Ξ±) : s.map f = s.image f :=
eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm
lemma image_const {s : finset Ξ±} (h : s.nonempty) (b : Ξ²) : s.image (Ξ»a, b) = singleton b :=
ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right,
h.bex, true_and, mem_singleton, eq_comm]
/--
Because `finset.image` requires a `decidable_eq` instances for the target type,
we can only construct a `functor finset` when working classically.
-/
instance [Ξ P, decidable P] : functor finset :=
{ map := Ξ» Ξ± Ξ² f s, s.image f, }
instance [Ξ P, decidable P] : is_lawful_functor finset :=
{ id_map := Ξ» Ξ± x, image_id,
comp_map := Ξ» Ξ± Ξ² Ξ³ f g s, image_image.symm, }
/-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose
elements belong to `s`. -/
protected def subtype {Ξ±} (p : Ξ± β Prop) [decidable_pred p] (s : finset Ξ±) : finset (subtype p) :=
(s.filter p).attach.map β¨Ξ» x, β¨x.1, (finset.mem_filter.1 x.2).2β©,
Ξ» x y H, subtype.eq $ subtype.mk.inj Hβ©
@[simp] lemma mem_subtype {p : Ξ± β Prop} [decidable_pred p] {s : finset Ξ±} :
β{a : subtype p}, a β s.subtype p β (a : Ξ±) β s
| β¨a, haβ© := by simp [finset.subtype, ha]
lemma subtype_eq_empty {p : Ξ± β Prop} [decidable_pred p] {s : finset Ξ±} :
s.subtype p = β
β β x, p x β x β s :=
by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl
/-- `s.subtype p` converts back to `s.filter p` with
`embedding.subtype`. -/
@[simp] lemma subtype_map (p : Ξ± β Prop) [decidable_pred p] :
(s.subtype p).map (embedding.subtype _) = s.filter p :=
begin
ext x,
rw mem_map,
change (β a : {x // p x}, β H, (a : Ξ±) = x) β _,
split,
{ rintros β¨y, hy, hyvalβ©,
rw [mem_subtype, hyval] at hy,
rw mem_filter,
use hy,
rw β hyval,
use y.property },
{ intro hx,
rw mem_filter at hx,
use β¨β¨x, hx.2β©, mem_subtype.2 hx.1, rflβ© }
end
/-- If all elements of a `finset` satisfy the predicate `p`,
`s.subtype p` converts back to `s` with `embedding.subtype`. -/
lemma subtype_map_of_mem {p : Ξ± β Prop} [decidable_pred p] (h : β x β s, p x) :
(s.subtype p).map (embedding.subtype _) = s :=
by rw [subtype_map, filter_true_of_mem h]
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, all elements of the result have the property of
the subtype. -/
lemma property_of_mem_map_subtype {p : Ξ± β Prop} (s : finset {x // p x}) {a : Ξ±}
(h : a β s.map (embedding.subtype _)) : p a :=
begin
rcases mem_map.1 h with β¨x, hx, rflβ©,
exact x.2
end
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result does not contain any value that does
not satisfy the property of the subtype. -/
lemma not_mem_map_subtype_of_not_property {p : Ξ± β Prop} (s : finset {x // p x})
{a : Ξ±} (h : Β¬ p a) : a β (s.map (embedding.subtype _)) :=
mt s.property_of_mem_map_subtype h
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result is a subset of the set giving the
subtype. -/
lemma map_subtype_subset {t : set Ξ±} (s : finset t) :
β(s.map (embedding.subtype _)) β t :=
begin
intros a ha,
rw mem_coe at ha,
convert property_of_mem_map_subtype s ha
end
lemma subset_image_iff {f : Ξ± β Ξ²}
{s : finset Ξ²} {t : set Ξ±} : βs β f '' t β βs' : finset Ξ±, βs' β t β§ s'.image f = s :=
begin
classical,
split, swap,
{ rintro β¨s, hs, rflβ©, rw [coe_image], exact set.image_subset f hs },
intro h, induction s using finset.induction with a s has ih h,
{ refine β¨β
, set.empty_subset _, _β©,
convert finset.image_empty _ },
rw [finset.coe_insert, set.insert_subset] at h,
rcases ih h.2 with β¨s', hst, hsiβ©,
rcases h.1 with β¨x, hxt, rflβ©,
refine β¨insert x s', _, _β©,
{ rw [finset.coe_insert, set.insert_subset], exact β¨hxt, hstβ© },
rw [finset.image_insert, hsi],
congr
end
end image
end finset
theorem multiset.to_finset_map [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β Ξ²) (m : multiset Ξ±) :
(m.map f).to_finset = m.to_finset.image f :=
finset.val_inj.1 (multiset.erase_dup_map_erase_dup_eq _ _).symm
namespace finset
/-! ### card -/
section card
/-- `card s` is the cardinality (number of elements) of `s`. -/
def card (s : finset Ξ±) : nat := s.1.card
theorem card_def (s : finset Ξ±) : s.card = s.1.card := rfl
@[simp] lemma card_mk {m nodup} : (β¨m, nodupβ© : finset Ξ±).card = m.card := rfl
@[simp] theorem card_empty : card (β
: finset Ξ±) = 0 := rfl
@[simp] theorem card_eq_zero {s : finset Ξ±} : card s = 0 β s = β
:=
card_eq_zero.trans val_eq_zero
theorem card_pos {s : finset Ξ±} : 0 < card s β s.nonempty :=
pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm
theorem card_ne_zero_of_mem {s : finset Ξ±} {a : Ξ±} (h : a β s) : card s β 0 :=
(not_congr card_eq_zero).2 (ne_empty_of_mem h)
theorem card_eq_one {s : finset Ξ±} : s.card = 1 β β a, s = {a} :=
by cases s; simp only [multiset.card_eq_one, finset.card, β val_inj, singleton_val]
@[simp] theorem card_insert_of_not_mem [decidable_eq Ξ±]
{a : Ξ±} {s : finset Ξ±} (h : a β s) : card (insert a s) = card s + 1 :=
by simpa only [card_cons, card, insert_val] using
congr_arg multiset.card (ndinsert_of_not_mem h)
theorem card_insert_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±}
(h : a β s) : card (insert a s) = card s := by rw insert_eq_of_mem h
theorem card_insert_le [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : card (insert a s) β€ card s + 1 :=
by by_cases a β s; [{rw [insert_eq_of_mem h], apply nat.le_add_right},
rw [card_insert_of_not_mem h]]
@[simp] theorem card_singleton (a : Ξ±) : card ({a} : finset Ξ±) = 1 := card_singleton _
lemma card_singleton_inter [decidable_eq Ξ±] {x : Ξ±} {s : finset Ξ±} : ({x} β© s).card β€ 1 :=
begin
cases (finset.decidable_mem x s),
{ simp [finset.singleton_inter_of_not_mem h] },
{ simp [finset.singleton_inter_of_mem h] },
end
theorem card_erase_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} :
a β s β card (erase s a) = pred (card s) := card_erase_of_mem
theorem card_erase_lt_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} :
a β s β card (erase s a) < card s := card_erase_lt_of_mem
theorem card_erase_le [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} :
card (erase s a) β€ card s := card_erase_le
theorem pred_card_le_card_erase [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} :
card s - 1 β€ card (erase s a) :=
begin
by_cases h : a β s,
{ rw [card_erase_of_mem h], refl },
{ rw [erase_eq_of_not_mem h], apply nat.sub_le }
end
@[simp] theorem card_range (n : β) : card (range n) = n := card_range n
@[simp] theorem card_attach {s : finset Ξ±} : card (attach s) = card s := multiset.card_attach
end card
end finset
theorem multiset.to_finset_card_le [decidable_eq Ξ±] (m : multiset Ξ±) : m.to_finset.card β€ m.card :=
card_le_of_le (erase_dup_le _)
theorem list.to_finset_card_le [decidable_eq Ξ±] (l : list Ξ±) : l.to_finset.card β€ l.length :=
multiset.to_finset_card_le β¦lβ§
namespace finset
section card
theorem card_image_le [decidable_eq Ξ²] {f : Ξ± β Ξ²} {s : finset Ξ±} : card (image f s) β€ card s :=
by simpa only [card_map] using (s.1.map f).to_finset_card_le
theorem card_image_of_inj_on [decidable_eq Ξ²] {f : Ξ± β Ξ²} {s : finset Ξ±}
(H : βxβs, βyβs, f x = f y β x = y) : card (image f s) = card s :=
by simp only [card, image_val_of_inj_on H, card_map]
theorem card_image_of_injective [decidable_eq Ξ²] {f : Ξ± β Ξ²} (s : finset Ξ±)
(H : injective f) : card (image f s) = card s :=
card_image_of_inj_on $ Ξ» x _ y _ h, H h
lemma fiber_card_ne_zero_iff_mem_image (s : finset Ξ±) (f : Ξ± β Ξ²) [decidable_eq Ξ²] (y : Ξ²) :
(s.filter (Ξ» x, f x = y)).card β 0 β y β s.image f :=
by { rw [βpos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] }
@[simp] lemma card_map {Ξ± Ξ²} (f : Ξ± βͺ Ξ²) {s : finset Ξ±} : (s.map f).card = s.card :=
multiset.card_map _ _
@[simp] lemma card_subtype (p : Ξ± β Prop) [decidable_pred p] (s : finset Ξ±) :
(s.subtype p).card = (s.filter p).card :=
by simp [finset.subtype]
lemma card_eq_of_bijective {s : finset Ξ±} {n : β}
(f : βi, i < n β Ξ±)
(hf : βaβs, βi, βh:i<n, f i h = a) (hf' : βi (h : i < n), f i h β s)
(f_inj : βi j (hi : i < n) (hj : j < n), f i hi = f j hj β i = j) :
card s = n :=
begin
classical,
have : β (a : Ξ±), a β s β βi (hi : i β range n), f i (mem_range.1 hi) = a,
from assume a, β¨assume ha, let β¨i, hi, eqβ© := hf a ha in β¨i, mem_range.2 hi, eqβ©,
assume β¨i, hi, eqβ©, eq βΈ hf' i (mem_range.1 hi)β©,
have : s = ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)),
by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and],
calc card s = card ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)) :
by rw [this]
... = card ((range n).attach) :
card_image_of_injective _ $ assume β¨i, hiβ© β¨j, hjβ© eq,
subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
... = card (range n) : card_attach
... = n : card_range n
end
lemma card_eq_succ [decidable_eq Ξ±] {s : finset Ξ±} {n : β} :
s.card = n + 1 β (βa t, a β t β§ insert a t = s β§ card t = n) :=
iff.intro
(assume eq,
have 0 < card s, from eq.symm βΈ nat.zero_lt_succ _,
let β¨a, hasβ© := card_pos.mp this in
β¨a, s.erase a, s.not_mem_erase a, insert_erase has,
by simp only [eq, card_erase_of_mem has, pred_succ]β©)
(assume β¨a, t, hat, s_eq, n_eqβ©, s_eq βΈ n_eq βΈ card_insert_of_not_mem hat)
theorem card_le_of_subset {s t : finset Ξ±} : s β t β card s β€ card t :=
multiset.card_le_of_le β val_le_iff.mpr
theorem eq_of_subset_of_card_le {s t : finset Ξ±} (h : s β t) (hβ : card t β€ card s) : s = t :=
eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) hβ
lemma card_lt_card {s t : finset Ξ±} (h : s β t) : s.card < t.card :=
card_lt_of_lt (val_lt_iff.2 h)
lemma card_le_card_of_inj_on {s : finset Ξ±} {t : finset Ξ²}
(f : Ξ± β Ξ²) (hf : βaβs, f a β t) (f_inj : βaββs, βaββs, f aβ = f aβ β aβ = aβ) :
card s β€ card t :=
begin
classical,
calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj]
... β€ card t : card_le_of_subset $
assume x hx, match x, finset.mem_image.1 hx with _, β¨a, ha, rflβ© := hf a ha end
end
/--
If there are more pigeons than pigeonholes, then there are two pigeons
in the same pigeonhole.
-/
lemma exists_ne_map_eq_of_card_lt_of_maps_to {s : finset Ξ±} {t : finset Ξ²} (hc : t.card < s.card)
{f : Ξ± β Ξ²} (hf : β a β s, f a β t) :
β (x β s) (y β s), x β y β§ f x = f y :=
begin
classical, by_contra hz, push_neg at hz,
refine hc.not_le (card_le_card_of_inj_on f hf _),
intros x hx y hy, contrapose, exact hz x hx y hy,
end
lemma card_le_of_inj_on {n} {s : finset Ξ±}
(f : β β Ξ±) (hf : βi<n, f i β s) (f_inj : βi j, i<n β j<n β f i = f j β i = j) : n β€ card s :=
calc n = card (range n) : (card_range n).symm
... β€ card s : card_le_card_of_inj_on f
(by simpa only [mem_range])
(by simp only [mem_range]; exact assume aβ hβ aβ hβ, f_inj aβ aβ hβ hβ)
/-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to
define an object on `s`. Then one can inductively define an object on all finsets, starting from
the empty set and iterating. This can be used either to define data, or to prove properties. -/
@[elab_as_eliminator] def strong_induction_on {p : finset Ξ± β Sort*} :
β (s : finset Ξ±), (βs, (βt β s, p t) β p s) β p s
| β¨s, ndβ© ih := multiset.strong_induction_on s
(Ξ» s IH nd, ih β¨s, ndβ© (Ξ» β¨t, nd'β© ss, IH t (val_lt_iff.2 ss) nd')) nd
@[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq Ξ±] {p : finset Ξ± β Prop}
(s : finset Ξ±) (hβ : p β
) (hβ : β a s, a β s β (βt β s, p t) β p (insert a s)) : p s :=
finset.strong_induction_on s $ Ξ» s,
finset.induction_on s (Ξ» _, hβ) $ Ξ» a s n _ ih, hβ a s n $
Ξ» t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
lemma card_congr {s : finset Ξ±} {t : finset Ξ²} (f : Ξ a β s, Ξ²)
(hβ : β a ha, f a ha β t) (hβ : β a b ha hb, f a ha = f b hb β a = b)
(hβ : β b β t, β a ha, f a ha = b) : s.card = t.card :=
by haveI := classical.prop_decidable; exact
calc s.card = s.attach.card : card_attach.symm
... = (s.attach.image (Ξ» (a : {a // a β s}), f a.1 a.2)).card :
eq.symm (card_image_of_injective _ (Ξ» a b h, subtype.eq (hβ _ _ _ _ h)))
... = t.card : congr_arg card (finset.ext $ Ξ» b,
β¨Ξ» h, let β¨a, haβ, haββ© := mem_image.1 h in haβ βΈ hβ _ _,
Ξ» h, let β¨a, haβ, haββ© := hβ b h in mem_image.2 β¨β¨a, haββ©, by simp [haβ]β©β©)
lemma card_union_add_card_inter [decidable_eq Ξ±] (s t : finset Ξ±) :
(s βͺ t).card + (s β© t).card = s.card + t.card :=
finset.induction_on t (by simp) $ Ξ» a r har, by by_cases a β s; simp *; cc
lemma card_union_le [decidable_eq Ξ±] (s t : finset Ξ±) :
(s βͺ t).card β€ s.card + t.card :=
card_union_add_card_inter s t βΈ le_add_right _ _
lemma card_union_eq [decidable_eq Ξ±] {s t : finset Ξ±} (h : disjoint s t) :
(s βͺ t).card = s.card + t.card :=
begin
rw [β card_union_add_card_inter],
convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h
end
lemma surj_on_of_inj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²}
(f : Ξ a β s, Ξ²) (hf : β a ha, f a ha β t)
(hinj : β aβ aβ haβ haβ, f aβ haβ = f aβ haβ β aβ = aβ)
(hst : card t β€ card s) :
(β b β t, β a ha, b = f a ha) :=
by haveI := classical.dec_eq Ξ²; exact
Ξ» b hb,
have h : card (image (Ξ» (a : {a // a β s}), f a a.prop) (attach s)) = card s,
from @card_attach _ s βΈ card_image_of_injective _
(Ξ» β¨aβ, haββ© β¨aβ, haββ© h, subtype.eq $ hinj _ _ _ _ h),
have hβ : image (Ξ» a : {a // a β s}, f a a.prop) s.attach = t :=
eq_of_subset_of_card_le (Ξ» b h, let β¨a, haβ, haββ© := mem_image.1 h in
haβ βΈ hf _ _) (by simp [hst, h]),
begin
rw β hβ at hb,
rcases mem_image.1 hb with β¨a, haβ, haββ©,
exact β¨a, a.2, haβ.symmβ©,
end
open function
lemma inj_on_of_surj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²}
(f : Ξ a β s, Ξ²) (hf : β a ha, f a ha β t)
(hsurj : β b β t, β a ha, b = f a ha)
(hst : card s β€ card t)
β¦aβ aββ¦ (haβ : aβ β s) (haβ : aβ β s)
(haβaβ: f aβ haβ = f aβ haβ) : aβ = aβ :=
by haveI : inhabited {x // x β s} := β¨β¨aβ, haββ©β©; exact
let f' : {x // x β s} β {x // x β t} := Ξ» x, β¨f x.1 x.2, hf x.1 x.2β© in
let g : {x // x β t} β {x // x β s} :=
@surj_inv _ _ f'
(Ξ» x, let β¨y, hyβ, hyββ© := hsurj x.1 x.2 in β¨β¨y, hyββ©, subtype.eq hyβ.symmβ©) in
have hg : injective g, from injective_surj_inv _,
have hsg : surjective g, from Ξ» x,
let β¨y, hyβ© := surj_on_of_inj_on_of_card_le (Ξ» (x : {x // x β t}) (hx : x β t.attach), g x)
(Ξ» x _, show (g x) β s.attach, from mem_attach _ _)
(Ξ» x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in
β¨y, hy.snd.symmβ©,
have hif : injective f',
from (left_inverse_of_surjective_of_right_inverse hsg
(right_inverse_surj_inv _)).injective,
subtype.ext_iff_val.1 (@hif β¨aβ, haββ© β¨aβ, haββ© (subtype.eq haβaβ))
end card
section bUnion
/-!
### bUnion
This section is about the bounded union of an indexed family `t : Ξ± β finset Ξ²` of finite sets
over a finite set `s : finset Ξ±`.
-/
variables [decidable_eq Ξ²] {s : finset Ξ±} {t : Ξ± β finset Ξ²}
/-- `bUnion s t` is the union of `t x` over `x β s`.
(This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/
protected def bUnion (s : finset Ξ±) (t : Ξ± β finset Ξ²) : finset Ξ² :=
(s.1.bind (Ξ» a, (t a).1)).to_finset
@[simp] theorem bUnion_val (s : finset Ξ±) (t : Ξ± β finset Ξ²) :
(s.bUnion t).1 = (s.1.bind (Ξ» a, (t a).1)).erase_dup := rfl
@[simp] theorem bUnion_empty : finset.bUnion β
t = β
:= rfl
@[simp] theorem mem_bUnion {b : Ξ²} : b β s.bUnion t β βaβs, b β t a :=
by simp only [mem_def, bUnion_val, mem_erase_dup, mem_bind, exists_prop]
@[simp] theorem bUnion_insert [decidable_eq Ξ±] {a : Ξ±} : (insert a s).bUnion t = t a βͺ s.bUnion t :=
ext $ Ξ» x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert,
or_and_distrib_right, exists_or_distrib, exists_eq_left]
-- ext $ Ξ» x, by simp [or_and_distrib_right, exists_or_distrib]
@[simp] lemma singleton_bUnion {a : Ξ±} : finset.bUnion {a} t = t a :=
begin
classical,
rw [β insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty]
end
theorem bUnion_inter (s : finset Ξ±) (f : Ξ± β finset Ξ²) (t : finset Ξ²) :
s.bUnion f β© t = s.bUnion (Ξ» x, f x β© t) :=
begin
ext x,
simp only [mem_bUnion, mem_inter],
tauto
end
theorem inter_bUnion (t : finset Ξ²) (s : finset Ξ±) (f : Ξ± β finset Ξ²) :
t β© s.bUnion f = s.bUnion (Ξ» x, t β© f x) :=
by rw [inter_comm, bUnion_inter]; simp [inter_comm]
theorem image_bUnion [decidable_eq Ξ³] {f : Ξ± β Ξ²} {s : finset Ξ±} {t : Ξ² β finset Ξ³} :
(s.image f).bUnion t = s.bUnion (Ξ»a, t (f a)) :=
by haveI := classical.dec_eq Ξ±; exact
finset.induction_on s rfl (Ξ» a s has ih,
by simp only [image_insert, bUnion_insert, ih])
theorem bUnion_image [decidable_eq Ξ³] {s : finset Ξ±} {t : Ξ± β finset Ξ²} {f : Ξ² β Ξ³} :
(s.bUnion t).image f = s.bUnion (Ξ»a, (t a).image f) :=
by haveI := classical.dec_eq Ξ±; exact
finset.induction_on s rfl (Ξ» a s has ih,
by simp only [bUnion_insert, image_union, ih])
theorem bind_to_finset [decidable_eq Ξ±] (s : multiset Ξ±) (t : Ξ± β multiset Ξ²) :
(s.bind t).to_finset = s.to_finset.bUnion (Ξ»a, (t a).to_finset) :=
ext $ Ξ» x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop]
lemma bUnion_mono {tβ tβ : Ξ± β finset Ξ²} (h : βaβs, tβ a β tβ a) : s.bUnion tβ β s.bUnion tβ :=
have βb a, a β s β b β tβ a β (β (a : Ξ±), a β s β§ b β tβ a),
from assume b a ha hb, β¨a, ha, finset.mem_of_subset (h a ha) hbβ©,
by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop]
lemma bUnion_subset_bUnion_of_subset_left {Ξ± : Type*} {sβ sβ : finset Ξ±}
(t : Ξ± β finset Ξ²) (h : sβ β sβ) : sβ.bUnion t β sβ.bUnion t :=
begin
intro x,
simp only [and_imp, mem_bUnion, exists_prop],
exact Exists.imp (Ξ» a ha, β¨h ha.1, ha.2β©)
end
lemma bUnion_singleton {f : Ξ± β Ξ²} : s.bUnion (Ξ»a, {f a}) = s.image f :=
ext $ Ξ» x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm]
@[simp] lemma bUnion_singleton_eq_self [decidable_eq Ξ±] :
s.bUnion (singleton : Ξ± β finset Ξ±) = s :=
by { rw bUnion_singleton, exact image_id }
lemma bUnion_filter_eq_of_maps_to [decidable_eq Ξ±] {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β Ξ²}
(h : β x β s, f x β t) :
t.bUnion (Ξ»a, s.filter $ (Ξ»c, f c = a)) = s :=
begin
ext b,
suffices : (β a β t, b β s β§ f b = a) β b β s, by simpa,
exact β¨Ξ» β¨a, ha, hb, habβ©, hb, Ξ» hb, β¨f b, h b hb, hb, rflβ©β©
end
lemma image_bUnion_filter_eq [decidable_eq Ξ±] (s : finset Ξ²) (g : Ξ² β Ξ±) :
(s.image g).bUnion (Ξ»a, s.filter $ (Ξ»c, g c = a)) = s :=
bUnion_filter_eq_of_maps_to (Ξ» x, mem_image_of_mem g)
end bUnion
/-! ### prod -/
section prod
variables {s : finset Ξ±} {t : finset Ξ²}
/-- `product s t` is the set of pairs `(a, b)` such that `a β s` and `b β t`. -/
protected def product (s : finset Ξ±) (t : finset Ξ²) : finset (Ξ± Γ Ξ²) := β¨_, nodup_product s.2 t.2β©
@[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl
@[simp] theorem mem_product {p : Ξ± Γ Ξ²} : p β s.product t β p.1 β s β§ p.2 β t := mem_product
theorem subset_product [decidable_eq Ξ±] [decidable_eq Ξ²] {s : finset (Ξ± Γ Ξ²)} :
s β (s.image prod.fst).product (s.image prod.snd) :=
Ξ» p hp, mem_product.2 β¨mem_image_of_mem _ hp, mem_image_of_mem _ hpβ©
theorem product_eq_bUnion [decidable_eq Ξ±] [decidable_eq Ξ²] (s : finset Ξ±) (t : finset Ξ²) :
s.product t = s.bUnion (Ξ»a, t.image $ Ξ»b, (a, b)) :=
ext $ Ξ» β¨x, yβ©, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff,
and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left]
@[simp] theorem card_product (s : finset Ξ±) (t : finset Ξ²) : card (s.product t) = card s * card t :=
multiset.card_product _ _
theorem filter_product (p : Ξ± β Prop) (q : Ξ² β Prop) [decidable_pred p] [decidable_pred q] :
(s.product t).filter (Ξ» (x : Ξ± Γ Ξ²), p x.1 β§ q x.2) = (s.filter p).product (t.filter q) :=
by { ext β¨a, bβ©, simp only [mem_filter, mem_product], finish, }
lemma filter_product_card (s : finset Ξ±) (t : finset Ξ²)
(p : Ξ± β Prop) (q : Ξ² β Prop) [decidable_pred p] [decidable_pred q] :
((s.product t).filter (Ξ» (x : Ξ± Γ Ξ²), p x.1 β q x.2)).card =
(s.filter p).card * (t.filter q).card + (s.filter (not β p)).card * (t.filter (not β q)).card :=
begin
classical,
rw [β card_product, β card_product, β filter_product, β filter_product, β card_union_eq],
{ apply congr_arg, ext β¨a, bβ©, simp only [filter_union_right, mem_filter, mem_product],
split; intros; finish, },
{ rw disjoint_iff, change _ β© _ = β
, ext β¨a, bβ©, rw mem_inter, finish, },
end
end prod
/-! ### sigma -/
section sigma
variables {Ο : Ξ± β Type*} {s : finset Ξ±} {t : Ξ a, finset (Ο a)}
/-- `sigma s t` is the set of dependent pairs `β¨a, bβ©` such that `a β s` and `b β t a`. -/
protected def sigma (s : finset Ξ±) (t : Ξ a, finset (Ο a)) : finset (Ξ£a, Ο a) :=
β¨_, nodup_sigma s.2 (Ξ» a, (t a).2)β©
@[simp] theorem mem_sigma {p : sigma Ο} : p β s.sigma t β p.1 β s β§ p.2 β t (p.1) := mem_sigma
theorem sigma_mono {sβ sβ : finset Ξ±} {tβ tβ : Ξ a, finset (Ο a)}
(H1 : sβ β sβ) (H2 : βa, tβ a β tβ a) : sβ.sigma tβ β sβ.sigma tβ :=
Ξ» β¨x, sxβ© H, let β¨H3, H4β© := mem_sigma.1 H in mem_sigma.2 β¨H1 H3, H2 x H4β©
theorem sigma_eq_bUnion [decidable_eq (Ξ£ a, Ο a)] (s : finset Ξ±)
(t : Ξ a, finset (Ο a)) :
s.sigma t = s.bUnion (Ξ»a, (t a).map $ embedding.sigma_mk a) :=
by { ext β¨x, yβ©, simp [and.left_comm] }
end sigma
/-! ### disjoint -/
section disjoint
variable [decidable_eq Ξ±]
theorem disjoint_left {s t : finset Ξ±} : disjoint s t β β {a}, a β s β a β t :=
by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and,
and_imp]; refl
theorem disjoint_val {s t : finset Ξ±} : disjoint s t β s.1.disjoint t.1 :=
disjoint_left
theorem disjoint_iff_inter_eq_empty {s t : finset Ξ±} : disjoint s t β s β© t = β
:=
disjoint_iff
instance decidable_disjoint (U V : finset Ξ±) : decidable (disjoint U V) :=
decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff
theorem disjoint_right {s t : finset Ξ±} : disjoint s t β β {a}, a β t β a β s :=
by rw [disjoint.comm, disjoint_left]
theorem disjoint_iff_ne {s t : finset Ξ±} : disjoint s t β β a β s, β b β t, a β b :=
by simp only [disjoint_left, imp_not_comm, forall_eq']
theorem disjoint_of_subset_left {s t u : finset Ξ±} (h : s β u) (d : disjoint u t) : disjoint s t :=
disjoint_left.2 (Ξ» x mβ, (disjoint_left.1 d) (h mβ))
theorem disjoint_of_subset_right {s t u : finset Ξ±} (h : t β u) (d : disjoint s u) : disjoint s t :=
disjoint_right.2 (Ξ» x mβ, (disjoint_right.1 d) (h mβ))
@[simp] theorem disjoint_empty_left (s : finset Ξ±) : disjoint β
s := disjoint_bot_left
@[simp] theorem disjoint_empty_right (s : finset Ξ±) : disjoint s β
:= disjoint_bot_right
@[simp] theorem singleton_disjoint {s : finset Ξ±} {a : Ξ±} : disjoint (singleton a) s β a β s :=
by simp only [disjoint_left, mem_singleton, forall_eq]
@[simp] theorem disjoint_singleton {s : finset Ξ±} {a : Ξ±} : disjoint s (singleton a) β a β s :=
disjoint.comm.trans singleton_disjoint
@[simp] theorem disjoint_insert_left {a : Ξ±} {s t : finset Ξ±} :
disjoint (insert a s) t β a β t β§ disjoint s t :=
by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] theorem disjoint_insert_right {a : Ξ±} {s t : finset Ξ±} :
disjoint s (insert a t) β a β s β§ disjoint s t :=
disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm]
@[simp] theorem disjoint_union_left {s t u : finset Ξ±} :
disjoint (s βͺ t) u β disjoint s u β§ disjoint t u :=
by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right {s t u : finset Ξ±} :
disjoint s (t βͺ u) β disjoint s t β§ disjoint s u :=
by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib]
lemma sdiff_disjoint {s t : finset Ξ±} : disjoint (t \ s) s :=
disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2
lemma disjoint_sdiff {s t : finset Ξ±} : disjoint s (t \ s) :=
sdiff_disjoint.symm
lemma disjoint_sdiff_inter (s t : finset Ξ±) : disjoint (s \ t) (s β© t) :=
disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint
lemma sdiff_eq_self_iff_disjoint {s t : finset Ξ±} : s \ t = s β disjoint s t :=
by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty]
lemma sdiff_eq_self_of_disjoint {s t : finset Ξ±} (h : disjoint s t) : s \ t = s :=
sdiff_eq_self_iff_disjoint.2 h
lemma disjoint_self_iff_empty (s : finset Ξ±) : disjoint s s β s = β
:=
disjoint_self
lemma disjoint_bUnion_left {ΞΉ : Type*}
(s : finset ΞΉ) (f : ΞΉ β finset Ξ±) (t : finset Ξ±) :
disjoint (s.bUnion f) t β (βiβs, disjoint (f i) t) :=
begin
classical,
refine s.induction _ _,
{ simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] },
{ assume i s his ih,
simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] }
end
lemma disjoint_bUnion_right {ΞΉ : Type*}
(s : finset Ξ±) (t : finset ΞΉ) (f : ΞΉ β finset Ξ±) :
disjoint s (t.bUnion f) β (βiβt, disjoint s (f i)) :=
by simpa only [disjoint.comm] using disjoint_bUnion_left t f s
@[simp] theorem card_disjoint_union {s t : finset Ξ±} (h : disjoint s t) :
card (s βͺ t) = card s + card t :=
by rw [β card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero]
theorem card_sdiff {s t : finset Ξ±} (h : s β t) : card (t \ s) = card t - card s :=
suffices card (t \ s) = card ((t \ s) βͺ s) - card s, by rwa sdiff_union_of_subset h at this,
by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel]
lemma disjoint_filter {s : finset Ξ±} {p q : Ξ± β Prop} [decidable_pred p] [decidable_pred q] :
disjoint (s.filter p) (s.filter q) β (β x β s, p x β Β¬ q x) :=
by split; simp [disjoint_left] {contextual := tt}
lemma disjoint_filter_filter {s t : finset Ξ±} {p q : Ξ± β Prop} [decidable_pred p]
[decidable_pred q] :
(disjoint s t) β disjoint (s.filter p) (t.filter q) :=
disjoint.mono (filter_subset _ _) (filter_subset _ _)
lemma disjoint_iff_disjoint_coe {Ξ± : Type*} {a b : finset Ξ±} [decidable_eq Ξ±] :
disjoint a b β disjoint (βa : set Ξ±) (βb : set Ξ±) :=
by { rw [finset.disjoint_left, set.disjoint_left], refl }
lemma filter_card_add_filter_neg_card_eq_card {Ξ± : Type*} {s : finset Ξ±} (p : Ξ± β Prop)
[decidable_pred p] :
(s.filter p).card + (s.filter (not β p)).card = s.card :=
by { classical, simp [β card_union_eq, filter_union_filter_neg_eq, disjoint_filter], }
end disjoint
section self_prod
variables (s : finset Ξ±) [decidable_eq Ξ±]
/-- Given a finite set `s`, the diagonal, `s.diag` is the set of pairs of the form `(a, a)` for
`a β s`. -/
def diag := (s.product s).filter (Ξ» (a : Ξ± Γ Ξ±), a.fst = a.snd)
/-- Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a β b`
for `a, b β s`. -/
def off_diag := (s.product s).filter (Ξ» (a : Ξ± Γ Ξ±), a.fst β a.snd)
@[simp] lemma mem_diag (x : Ξ± Γ Ξ±) : x β s.diag β x.1 β s β§ x.1 = x.2 :=
by { simp only [diag, mem_filter, mem_product], split; intros; finish, }
@[simp] lemma mem_off_diag (x : Ξ± Γ Ξ±) : x β s.off_diag β x.1 β s β§ x.2 β s β§ x.1 β x.2 :=
by { simp only [off_diag, mem_filter, mem_product], split; intros; finish, }
@[simp] lemma diag_card : (diag s).card = s.card :=
begin
suffices : diag s = s.image (Ξ» a, (a, a)), { rw this, apply card_image_of_inj_on, finish, },
ext β¨aβ, aββ©, rw mem_diag, split; intros; finish,
end
@[simp] lemma off_diag_card : (off_diag s).card = s.card * s.card - s.card :=
begin
suffices : (diag s).card + (off_diag s).card = s.card * s.card,
{ nth_rewrite 2 β s.diag_card, finish, },
rw β card_product,
apply filter_card_add_filter_neg_card_eq_card,
end
end self_prod
/--
Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B
inside it.
-/
lemma exists_intermediate_set {A B : finset Ξ±} (i : β)
(hβ : i + card B β€ card A) (hβ : B β A) :
β (C : finset Ξ±), B β C β§ C β A β§ card C = i + card B :=
begin
classical,
rcases nat.le.dest hβ with β¨k, _β©,
clear hβ,
induction k with k ih generalizing A,
{ exact β¨A, hβ, subset.refl _, h.symmβ© },
{ have : (A \ B).nonempty,
{ rw [β card_pos, card_sdiff hβ, β h, nat.add_right_comm,
nat.add_sub_cancel, nat.add_succ],
apply nat.succ_pos },
rcases this with β¨a, haβ©,
have z : i + card B + k = card (erase A a),
{ rw [card_erase_of_mem, β h, nat.add_succ, nat.pred_succ],
rw mem_sdiff at ha,
exact ha.1 },
rcases ih _ z with β¨B', hB', B'subA', cardsβ©,
{ exact β¨B', hB', trans B'subA' (erase_subset _ _), cardsβ© },
{ rintros t th,
apply mem_erase_of_ne_of_mem _ (hβ th),
rintro rfl,
exact not_mem_sdiff_of_mem_right th ha } }
end
/-- We can shrink A to any smaller size. -/
lemma exists_smaller_set (A : finset Ξ±) (i : β) (hβ : i β€ card A) :
β (B : finset Ξ±), B β A β§ card B = i :=
let β¨B, _, xβ, xββ© := exists_intermediate_set i (by simpa) (empty_subset A) in β¨B, xβ, xββ©
/-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/
def fin_range (k : β) : finset (fin k) :=
β¨list.fin_range k, list.nodup_fin_range kβ©
@[simp]
lemma fin_range_card {k : β} : (fin_range k).card = k :=
by simp [fin_range]
@[simp]
lemma mem_fin_range {k : β} (m : fin k) : m β fin_range k :=
list.mem_fin_range m
@[simp] lemma coe_fin_range (k : β) : (fin_range k : set (fin k)) = set.univ :=
set.eq_univ_of_forall mem_fin_range
/-- Given a finset `s` of `β` contained in `{0,..., n-1}`, the corresponding finset in `fin n`
is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/
def attach_fin (s : finset β) {n : β} (h : β m β s, m < n) : finset (fin n) :=
β¨s.1.pmap (Ξ» a ha, β¨a, haβ©) h, multiset.nodup_pmap (Ξ» _ _ _ _, fin.veq_of_eq) s.2β©
@[simp] lemma mem_attach_fin {n : β} {s : finset β} (h : β m β s, m < n) {a : fin n} :
a β s.attach_fin h β (a : β) β s :=
β¨Ξ» h, let β¨b, hbβ, hbββ© := multiset.mem_pmap.1 h in hbβ βΈ hbβ,
Ξ» h, multiset.mem_pmap.2 β¨a, h, fin.eta _ _β©β©
@[simp] lemma card_attach_fin {n : β} (s : finset β) (h : β m β s, m < n) :
(s.attach_fin h).card = s.card := multiset.card_pmap _ _ _
/-! ### choose -/
section choose
variables (p : Ξ± β Prop) [decidable_pred p] (l : finset Ξ±)
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the corresponding subtype. -/
def choose_x (hp : (β! a, a β l β§ p a)) : { a // a β l β§ p a } :=
multiset.choose_x p l.val hp
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the ambient type. -/
def choose (hp : β! a, a β l β§ p a) : Ξ± := choose_x p l hp
lemma choose_spec (hp : β! a, a β l β§ p a) : choose p l hp β l β§ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : β! a, a β l β§ p a) : choose p l hp β l := (choose_spec _ _ _).1
lemma choose_property (hp : β! a, a β l β§ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
theorem lt_wf {Ξ±} : well_founded (@has_lt.lt (finset Ξ±) _) :=
have H : subrelation (@has_lt.lt (finset Ξ±) _)
(inv_image (<) card),
from Ξ» x y hxy, card_lt_card hxy,
subrelation.wf H $ inv_image.wf _ $ nat.lt_wf
end finset
namespace equiv
/-- Given an equivalence `Ξ±` to `Ξ²`, produce an equivalence between `finset Ξ±` and `finset Ξ²`. -/
protected def finset_congr (e : Ξ± β Ξ²) : finset Ξ± β finset Ξ² :=
{ to_fun := Ξ» s, s.map e.to_embedding,
inv_fun := Ξ» s, s.map e.symm.to_embedding,
left_inv := Ξ» s, by simp [finset.map_map],
right_inv := Ξ» s, by simp [finset.map_map] }
@[simp] lemma finset_congr_apply (e : Ξ± β Ξ²) (s : finset Ξ±) :
e.finset_congr s = s.map e.to_embedding :=
rfl
@[simp] lemma finset_congr_symm_apply (e : Ξ± β Ξ²) (s : finset Ξ²) :
e.finset_congr.symm s = s.map e.symm.to_embedding :=
rfl
end equiv
namespace list
variable [decidable_eq Ξ±]
theorem to_finset_card_of_nodup {l : list Ξ±} (h : l.nodup) : l.to_finset.card = l.length :=
congr_arg card $ (@multiset.erase_dup_eq_self Ξ± _ l).2 h
end list
namespace multiset
variable [decidable_eq Ξ±]
theorem to_finset_card_of_nodup {l : multiset Ξ±} (h : l.nodup) : l.to_finset.card = l.card :=
congr_arg card $ (@multiset.erase_dup_eq_self Ξ± _ l).2 h
lemma disjoint_to_finset (m1 m2 : multiset Ξ±) :
_root_.disjoint m1.to_finset m2.to_finset β m1.disjoint m2 :=
begin
rw finset.disjoint_iff_ne,
split,
{ intro h,
intros a ha1 ha2,
rw β multiset.mem_to_finset at ha1 ha2,
exact h _ ha1 _ ha2 rfl },
{ rintros h a ha b hb rfl,
rw multiset.mem_to_finset at ha hb,
exact h ha hb }
end
end multiset
|
8ebe5b5d4f42015a92acc35313d3ec697fcd3ed9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/calculus/iterated_deriv_auto.lean | 41b3501a1b57b123be26a5235cb79f3d63ab81b5 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,954 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.analysis.calculus.deriv
import Mathlib.analysis.calculus.times_cont_diff
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# One-dimensional iterated derivatives
We define the `n`-th derivative of a function `f : π β F` as a function
`iterated_deriv n f : π β F`, as well as a version on domains `iterated_deriv_within n f s : π β F`,
and prove their basic properties.
## Main definitions and results
Let `π` be a nondiscrete normed field, and `F` a normed vector space over `π`. Let `f : π β F`.
* `iterated_deriv n f` is the `n`-th derivative of `f`, seen as a function from `π` to `F`.
It is defined as the `n`-th FrΓ©chet derivative (which is a multilinear map) applied to the
vector `(1, ..., 1)`, to take advantage of all the existing framework, but we show that it
coincides with the naive iterative definition.
* `iterated_deriv_eq_iterate` states that the `n`-th derivative of `f` is obtained by starting
from `f` and differentiating it `n` times.
* `iterated_deriv_within n f s` is the `n`-th derivative of `f` within the domain `s`. It only
behaves well when `s` has the unique derivative property.
* `iterated_deriv_within_eq_iterate` states that the `n`-th derivative of `f` in the domain `s` is
obtained by starting from `f` and differentiating it `n` times within `s`. This only holds when
`s` has the unique derivative property.
## Implementation details
The results are deduced from the corresponding results for the more general (multilinear) iterated
FrΓ©chet derivative. For this, we write `iterated_deriv n f` as the composition of
`iterated_fderiv π n f` and a continuous linear equiv. As continuous linear equivs respect
differentiability and commute with differentiation, this makes it possible to prove readily that
the derivative of the `n`-th derivative is the `n+1`-th derivative in `iterated_deriv_within_succ`,
by translating the corresponding result `iterated_fderiv_within_succ_apply_left` for the
iterated FrΓ©chet derivative.
-/
/-- The `n`-th iterated derivative of a function from `π` to `F`, as a function from `π` to `F`. -/
def iterated_deriv {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2} [normed_group F]
[normed_space π F] (n : β) (f : π β F) (x : π) : F :=
coe_fn (iterated_fderiv π n f x) fun (i : fin n) => 1
/-- The `n`-th iterated derivative of a function from `π` to `F` within a set `s`, as a function
from `π` to `F`. -/
def iterated_deriv_within {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] (n : β) (f : π β F) (s : set π) (x : π) : F :=
coe_fn (iterated_fderiv_within π n f s x) fun (i : fin n) => 1
theorem iterated_deriv_within_univ {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_deriv_within n f set.univ = iterated_deriv n f :=
sorry
/-! ### Properties of the iterated derivative within a set -/
theorem iterated_deriv_within_eq_iterated_fderiv_within {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} {x : π} :
iterated_deriv_within n f s x =
coe_fn (iterated_fderiv_within π n f s x) fun (i : fin n) => 1 :=
rfl
/-- Write the iterated derivative as the composition of a continuous linear equiv and the iterated
FrΓ©chet derivative -/
theorem iterated_deriv_within_eq_equiv_comp {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} :
iterated_deriv_within n f s =
β(continuous_linear_equiv.symm (continuous_multilinear_map.pi_field_equiv π (fin n) F)) β
iterated_fderiv_within π n f s :=
funext fun (x : π) => Eq.refl (iterated_deriv_within n f s x)
/-- Write the iterated FrΓ©chet derivative as the composition of a continuous linear equiv and the
iterated derivative. -/
theorem iterated_fderiv_within_eq_equiv_comp {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} :
iterated_fderiv_within π n f s =
β(continuous_multilinear_map.pi_field_equiv π (fin n) F) β iterated_deriv_within n f s :=
sorry
/-- The `n`-th FrΓ©chet derivative applied to a vector `(m 0, ..., m (n-1))` is the derivative
multiplied by the product of the `m i`s. -/
theorem iterated_fderiv_within_apply_eq_iterated_deriv_within_mul_prod {π : Type u_1}
[nondiscrete_normed_field π] {F : Type u_2} [normed_group F] [normed_space π F] {n : β}
{f : π β F} {s : set π} {x : π} {m : fin n β π} :
coe_fn (iterated_fderiv_within π n f s x) m =
(finset.prod finset.univ fun (i : fin n) => m i) β’ iterated_deriv_within n f s x :=
sorry
@[simp] theorem iterated_deriv_within_zero {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {s : set π} :
iterated_deriv_within 0 f s = f :=
sorry
@[simp] theorem iterated_deriv_within_one {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {f : π β F} {s : set π} (hs : unique_diff_on π s) {x : π}
(hx : x β s) : iterated_deriv_within 1 f s x = deriv_within f s x :=
sorry
/-- If the first `n` derivatives within a set of a function are continuous, and its first `n-1`
derivatives are differentiable, then the function is `C^n`. This is not an equivalence in general,
but this is an equivalence when the set has unique derivatives, see
`times_cont_diff_on_iff_continuous_on_differentiable_on_deriv`. -/
theorem times_cont_diff_on_of_continuous_on_differentiable_on_deriv {π : Type u_1}
[nondiscrete_normed_field π] {F : Type u_2} [normed_group F] [normed_space π F] {f : π β F}
{s : set π} {n : with_top β}
(Hcont : β (m : β), βm β€ n β continuous_on (fun (x : π) => iterated_deriv_within m f s x) s)
(Hdiff :
β (m : β), βm < n β differentiable_on π (fun (x : π) => iterated_deriv_within m f s x) s) :
times_cont_diff_on π n f s :=
sorry
/-- To check that a function is `n` times continuously differentiable, it suffices to check that its
first `n` derivatives are differentiable. This is slightly too strong as the condition we
require on the `n`-th derivative is differentiability instead of continuity, but it has the
advantage of avoiding the discussion of continuity in the proof (and for `n = β` this is optimal).
-/
theorem times_cont_diff_on_of_differentiable_on_deriv {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {s : set π} {n : with_top β}
(h : β (m : β), βm β€ n β differentiable_on π (iterated_deriv_within m f s) s) :
times_cont_diff_on π n f s :=
sorry
/-- On a set with unique derivatives, a `C^n` function has derivatives up to `n` which are
continuous. -/
theorem times_cont_diff_on.continuous_on_iterated_deriv_within {π : Type u_1}
[nondiscrete_normed_field π] {F : Type u_2} [normed_group F] [normed_space π F] {f : π β F}
{s : set π} {n : with_top β} {m : β} (h : times_cont_diff_on π n f s) (hmn : βm β€ n)
(hs : unique_diff_on π s) : continuous_on (iterated_deriv_within m f s) s :=
sorry
/-- On a set with unique derivatives, a `C^n` function has derivatives less than `n` which are
differentiable. -/
theorem times_cont_diff_on.differentiable_on_iterated_deriv_within {π : Type u_1}
[nondiscrete_normed_field π] {F : Type u_2} [normed_group F] [normed_space π F] {f : π β F}
{s : set π} {n : with_top β} {m : β} (h : times_cont_diff_on π n f s) (hmn : βm < n)
(hs : unique_diff_on π s) : differentiable_on π (iterated_deriv_within m f s) s :=
sorry
/-- The property of being `C^n`, initially defined in terms of the FrΓ©chet derivative, can be
reformulated in terms of the one-dimensional derivative on sets with unique derivatives. -/
theorem times_cont_diff_on_iff_continuous_on_differentiable_on_deriv {π : Type u_1}
[nondiscrete_normed_field π] {F : Type u_2} [normed_group F] [normed_space π F] {f : π β F}
{s : set π} {n : with_top β} (hs : unique_diff_on π s) :
times_cont_diff_on π n f s β
(β (m : β), βm β€ n β continuous_on (iterated_deriv_within m f s) s) β§
β (m : β), βm < n β differentiable_on π (iterated_deriv_within m f s) s :=
sorry
/-- The `n+1`-th iterated derivative within a set with unique derivatives can be obtained by
differentiating the `n`-th iterated derivative. -/
theorem iterated_deriv_within_succ {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} {x : π}
(hxs : unique_diff_within_at π s x) :
iterated_deriv_within (n + 1) f s x = deriv_within (iterated_deriv_within n f s) s x :=
sorry
/-- The `n`-th iterated derivative within a set with unique derivatives can be obtained by
iterating `n` times the differentiation operation. -/
theorem iterated_deriv_within_eq_iterate {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} {x : π}
(hs : unique_diff_on π s) (hx : x β s) :
iterated_deriv_within n f s x = nat.iterate (fun (g : π β F) => deriv_within g s) n f x :=
sorry
/-- The `n+1`-th iterated derivative within a set with unique derivatives can be obtained by
taking the `n`-th derivative of the derivative. -/
theorem iterated_deriv_within_succ' {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} {s : set π} {x : π}
(hxs : unique_diff_on π s) (hx : x β s) :
iterated_deriv_within (n + 1) f s x = iterated_deriv_within n (deriv_within f s) s x :=
sorry
/-! ### Properties of the iterated derivative on the whole space -/
theorem iterated_deriv_eq_iterated_fderiv {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} {x : π} :
iterated_deriv n f x = coe_fn (iterated_fderiv π n f x) fun (i : fin n) => 1 :=
rfl
/-- Write the iterated derivative as the composition of a continuous linear equiv and the iterated
FrΓ©chet derivative -/
theorem iterated_deriv_eq_equiv_comp {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_deriv n f =
β(continuous_linear_equiv.symm (continuous_multilinear_map.pi_field_equiv π (fin n) F)) β
iterated_fderiv π n f :=
funext fun (x : π) => Eq.refl (iterated_deriv n f x)
/-- Write the iterated FrΓ©chet derivative as the composition of a continuous linear equiv and the
iterated derivative. -/
theorem iterated_fderiv_eq_equiv_comp {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_fderiv π n f =
β(continuous_multilinear_map.pi_field_equiv π (fin n) F) β iterated_deriv n f :=
sorry
/-- The `n`-th FrΓ©chet derivative applied to a vector `(m 0, ..., m (n-1))` is the derivative
multiplied by the product of the `m i`s. -/
theorem iterated_fderiv_apply_eq_iterated_deriv_mul_prod {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {n : β} {f : π β F} {x : π} {m : fin n β π} :
coe_fn (iterated_fderiv π n f x) m =
(finset.prod finset.univ fun (i : fin n) => m i) β’ iterated_deriv n f x :=
sorry
@[simp] theorem iterated_deriv_zero {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {f : π β F} : iterated_deriv 0 f = f :=
sorry
@[simp] theorem iterated_deriv_one {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {f : π β F} : iterated_deriv 1 f = deriv f :=
sorry
/-- The property of being `C^n`, initially defined in terms of the FrΓ©chet derivative, can be
reformulated in terms of the one-dimensional derivative. -/
theorem times_cont_diff_iff_iterated_deriv {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {n : with_top β} :
times_cont_diff π n f β
(β (m : β), βm β€ n β continuous (iterated_deriv m f)) β§
β (m : β), βm < n β differentiable π (iterated_deriv m f) :=
sorry
/-- To check that a function is `n` times continuously differentiable, it suffices to check that its
first `n` derivatives are differentiable. This is slightly too strong as the condition we
require on the `n`-th derivative is differentiability instead of continuity, but it has the
advantage of avoiding the discussion of continuity in the proof (and for `n = β` this is optimal).
-/
theorem times_cont_diff_of_differentiable_iterated_deriv {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {n : with_top β}
(h : β (m : β), βm β€ n β differentiable π (iterated_deriv m f)) : times_cont_diff π n f :=
iff.mpr times_cont_diff_iff_iterated_deriv
{ left := fun (m : β) (hm : βm β€ n) => differentiable.continuous (h m hm),
right := fun (m : β) (hm : βm < n) => h m (le_of_lt hm) }
theorem times_cont_diff.continuous_iterated_deriv {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {n : with_top β} (m : β)
(h : times_cont_diff π n f) (hmn : βm β€ n) : continuous (iterated_deriv m f) :=
and.left (iff.mp times_cont_diff_iff_iterated_deriv h) m hmn
theorem times_cont_diff.differentiable_iterated_deriv {π : Type u_1} [nondiscrete_normed_field π]
{F : Type u_2} [normed_group F] [normed_space π F] {f : π β F} {n : with_top β} (m : β)
(h : times_cont_diff π n f) (hmn : βm < n) : differentiable π (iterated_deriv m f) :=
and.right (iff.mp times_cont_diff_iff_iterated_deriv h) m hmn
/-- The `n+1`-th iterated derivative can be obtained by differentiating the `n`-th
iterated derivative. -/
theorem iterated_deriv_succ {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_deriv (n + 1) f = deriv (iterated_deriv n f) :=
sorry
/-- The `n`-th iterated derivative can be obtained by iterating `n` times the
differentiation operation. -/
theorem iterated_deriv_eq_iterate {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_deriv n f = nat.iterate deriv n f :=
sorry
/-- The `n+1`-th iterated derivative can be obtained by taking the `n`-th derivative of the
derivative. -/
theorem iterated_deriv_succ' {π : Type u_1} [nondiscrete_normed_field π] {F : Type u_2}
[normed_group F] [normed_space π F] {n : β} {f : π β F} :
iterated_deriv (n + 1) f = iterated_deriv n (deriv f) :=
sorry
end Mathlib |
fe973a7ae39dc84152f89375f87386b475b39887 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/algebra/subalgebra/tower.lean | 8edd9a4b26b9470b483b715cf51c7efe30ea3d1b | [
"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 | 4,406 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Anne Baanen
-/
import algebra.algebra.subalgebra.basic
import algebra.algebra.tower
/-!
# Subalgebras in towers of algebras
In this file we prove facts about subalgebras in towers of algebra.
An algebra tower A/S/R is expressed by having instances of `algebra A S`,
`algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the
compatibility condition `(r β’ s) β’ a = r β’ (s β’ a)`.
## Main results
* `is_scalar_tower.subalgebra`: if `A/S/R` is a tower and `Sβ` is a subalgebra
between `S` and `R`, then `A/S/Sβ` is a tower
* `is_scalar_tower.subalgebra'`: if `A/S/R` is a tower and `Sβ` is a subalgebra
between `S` and `R`, then `A/Sβ/R` is a tower
* `subalgebra.restrict_scalars`: turn an `S`-subalgebra of `A` into an `R`-subalgebra of `A`,
given that `A/S/R` is a tower
-/
open_locale pointwise
universes u v w uβ vβ
variables (R : Type u) (S : Type v) (A : Type w) (B : Type uβ) (M : Type vβ)
namespace algebra
variables [comm_semiring R] [semiring A] [algebra R A]
variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M]
variables {A}
lemma lmul_algebra_map (x : R) :
algebra.lmul R A (algebra_map R A x) = algebra.lsmul R A x :=
eq.symm $ linear_map.ext $ smul_def x
end algebra
namespace is_scalar_tower
section semiring
variables [comm_semiring R] [comm_semiring S] [semiring A]
variables [algebra R S] [algebra S A]
variables (R S A)
instance subalgebra (Sβ : subalgebra R S) : is_scalar_tower Sβ S A :=
of_algebra_map_eq $ Ξ» x, rfl
variables [algebra R A] [is_scalar_tower R S A]
instance subalgebra' (Sβ : subalgebra R S) : is_scalar_tower R Sβ A :=
@is_scalar_tower.of_algebra_map_eq R Sβ A _ _ _ _ _ _ $ Ξ» _,
(is_scalar_tower.algebra_map_apply R S A _ : _)
end semiring
end is_scalar_tower
namespace subalgebra
open is_scalar_tower
section semiring
variables (R) {S A B} [comm_semiring R] [comm_semiring S] [semiring A] [semiring B]
variables [algebra R S] [algebra S A] [algebra R A] [algebra S B] [algebra R B]
variables [is_scalar_tower R S A] [is_scalar_tower R S B]
/-- Given a tower `A / β₯U / S / R` of algebras, where `U` is an `S`-subalgebra of `A`, reinterpret
`U` as an `R`-subalgebra of `A`. -/
def restrict_scalars (U : subalgebra S A) : subalgebra R A :=
{ algebra_map_mem' := Ξ» x, by { rw algebra_map_apply R S A, exact U.algebra_map_mem _ },
.. U }
@[simp] lemma coe_restrict_scalars {U : subalgebra S A} :
(restrict_scalars R U : set A) = (U : set A) := rfl
@[simp] lemma restrict_scalars_top : restrict_scalars R (β€ : subalgebra S A) = β€ :=
set_like.coe_injective rfl
@[simp] lemma restrict_scalars_to_submodule {U : subalgebra S A} :
(U.restrict_scalars R).to_submodule = U.to_submodule.restrict_scalars R :=
set_like.coe_injective rfl
@[simp] lemma mem_restrict_scalars {U : subalgebra S A} {x : A} :
x β restrict_scalars R U β x β U := iff.rfl
lemma restrict_scalars_injective :
function.injective (restrict_scalars R : subalgebra S A β subalgebra R A) :=
Ξ» U V H, ext $ Ξ» x, by rw [β mem_restrict_scalars R, H, mem_restrict_scalars]
/-- Produces an `R`-algebra map from `U.restrict_scalars R` given an `S`-algebra map from `U`.
This is a special case of `alg_hom.restrict_scalars` that can be helpful in elaboration. -/
@[simp]
def of_restrict_scalars (U : subalgebra S A) (f : U ββ[S] B) : U.restrict_scalars R ββ[R] B :=
f.restrict_scalars R
end semiring
end subalgebra
namespace is_scalar_tower
open subalgebra
variables [comm_semiring R] [comm_semiring S] [comm_semiring A]
variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A]
theorem adjoin_range_to_alg_hom (t : set A) :
(algebra.adjoin (to_alg_hom R S A).range t).restrict_scalars R =
(algebra.adjoin S t).restrict_scalars R :=
subalgebra.ext $ Ξ» z,
show z β subsemiring.closure (set.range (algebra_map (to_alg_hom R S A).range A) βͺ t : set A) β
z β subsemiring.closure (set.range (algebra_map S A) βͺ t : set A),
from suffices set.range (algebra_map (to_alg_hom R S A).range A) = set.range (algebra_map S A),
by rw this,
by { ext z, exact β¨Ξ» β¨β¨x, y, h1β©, h2β©, β¨y, h2 βΈ h1β©, Ξ» β¨y, hyβ©, β¨β¨z, y, hyβ©, rflβ©β© }
end is_scalar_tower
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.